dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / scsi / lpfc / lpfc_els.c
blobfd8fe1202dbe96e0e4e08745818b3d5068576681
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2015 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
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. *
20 *******************************************************************/
21 /* See Fibre Channel protocol T11 FC-LS for details */
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
33 #include "lpfc_hw4.h"
34 #include "lpfc_hw.h"
35 #include "lpfc_sli.h"
36 #include "lpfc_sli4.h"
37 #include "lpfc_nl.h"
38 #include "lpfc_disc.h"
39 #include "lpfc_scsi.h"
40 #include "lpfc.h"
41 #include "lpfc_logmsg.h"
42 #include "lpfc_crtn.h"
43 #include "lpfc_vport.h"
44 #include "lpfc_debugfs.h"
46 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
47 struct lpfc_iocbq *);
48 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
49 struct lpfc_iocbq *);
50 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
51 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
52 struct lpfc_nodelist *ndlp, uint8_t retry);
53 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
54 struct lpfc_iocbq *iocb);
56 static int lpfc_max_els_tries = 3;
58 /**
59 * lpfc_els_chk_latt - Check host link attention event for a vport
60 * @vport: pointer to a host virtual N_Port data structure.
62 * This routine checks whether there is an outstanding host link
63 * attention event during the discovery process with the @vport. It is done
64 * by reading the HBA's Host Attention (HA) register. If there is any host
65 * link attention events during this @vport's discovery process, the @vport
66 * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
67 * be issued if the link state is not already in host link cleared state,
68 * and a return code shall indicate whether the host link attention event
69 * had happened.
71 * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
72 * state in LPFC_VPORT_READY, the request for checking host link attention
73 * event will be ignored and a return code shall indicate no host link
74 * attention event had happened.
76 * Return codes
77 * 0 - no host link attention event happened
78 * 1 - host link attention event happened
79 **/
80 int
81 lpfc_els_chk_latt(struct lpfc_vport *vport)
83 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
84 struct lpfc_hba *phba = vport->phba;
85 uint32_t ha_copy;
87 if (vport->port_state >= LPFC_VPORT_READY ||
88 phba->link_state == LPFC_LINK_DOWN ||
89 phba->sli_rev > LPFC_SLI_REV3)
90 return 0;
92 /* Read the HBA Host Attention Register */
93 if (lpfc_readl(phba->HAregaddr, &ha_copy))
94 return 1;
96 if (!(ha_copy & HA_LATT))
97 return 0;
99 /* Pending Link Event during Discovery */
100 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
101 "0237 Pending Link Event during "
102 "Discovery: State x%x\n",
103 phba->pport->port_state);
105 /* CLEAR_LA should re-enable link attention events and
106 * we should then immediately take a LATT event. The
107 * LATT processing should call lpfc_linkdown() which
108 * will cleanup any left over in-progress discovery
109 * events.
111 spin_lock_irq(shost->host_lock);
112 vport->fc_flag |= FC_ABORT_DISCOVERY;
113 spin_unlock_irq(shost->host_lock);
115 if (phba->link_state != LPFC_CLEAR_LA)
116 lpfc_issue_clear_la(phba, vport);
118 return 1;
122 * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
123 * @vport: pointer to a host virtual N_Port data structure.
124 * @expectRsp: flag indicating whether response is expected.
125 * @cmdSize: size of the ELS command.
126 * @retry: number of retries to the command IOCB when it fails.
127 * @ndlp: pointer to a node-list data structure.
128 * @did: destination identifier.
129 * @elscmd: the ELS command code.
131 * This routine is used for allocating a lpfc-IOCB data structure from
132 * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
133 * passed into the routine for discovery state machine to issue an Extended
134 * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
135 * and preparation routine that is used by all the discovery state machine
136 * routines and the ELS command-specific fields will be later set up by
137 * the individual discovery machine routines after calling this routine
138 * allocating and preparing a generic IOCB data structure. It fills in the
139 * Buffer Descriptor Entries (BDEs), allocates buffers for both command
140 * payload and response payload (if expected). The reference count on the
141 * ndlp is incremented by 1 and the reference to the ndlp is put into
142 * context1 of the IOCB data structure for this IOCB to hold the ndlp
143 * reference for the command's callback function to access later.
145 * Return code
146 * Pointer to the newly allocated/prepared els iocb data structure
147 * NULL - when els iocb data structure allocation/preparation failed
149 struct lpfc_iocbq *
150 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
151 uint16_t cmdSize, uint8_t retry,
152 struct lpfc_nodelist *ndlp, uint32_t did,
153 uint32_t elscmd)
155 struct lpfc_hba *phba = vport->phba;
156 struct lpfc_iocbq *elsiocb;
157 struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
158 struct ulp_bde64 *bpl;
159 IOCB_t *icmd;
162 if (!lpfc_is_link_up(phba))
163 return NULL;
165 /* Allocate buffer for command iocb */
166 elsiocb = lpfc_sli_get_iocbq(phba);
168 if (elsiocb == NULL)
169 return NULL;
172 * If this command is for fabric controller and HBA running
173 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
175 if ((did == Fabric_DID) &&
176 (phba->hba_flag & HBA_FIP_SUPPORT) &&
177 ((elscmd == ELS_CMD_FLOGI) ||
178 (elscmd == ELS_CMD_FDISC) ||
179 (elscmd == ELS_CMD_LOGO)))
180 switch (elscmd) {
181 case ELS_CMD_FLOGI:
182 elsiocb->iocb_flag |=
183 ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
184 & LPFC_FIP_ELS_ID_MASK);
185 break;
186 case ELS_CMD_FDISC:
187 elsiocb->iocb_flag |=
188 ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
189 & LPFC_FIP_ELS_ID_MASK);
190 break;
191 case ELS_CMD_LOGO:
192 elsiocb->iocb_flag |=
193 ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
194 & LPFC_FIP_ELS_ID_MASK);
195 break;
197 else
198 elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
200 icmd = &elsiocb->iocb;
202 /* fill in BDEs for command */
203 /* Allocate buffer for command payload */
204 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
205 if (pcmd)
206 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
207 if (!pcmd || !pcmd->virt)
208 goto els_iocb_free_pcmb_exit;
210 INIT_LIST_HEAD(&pcmd->list);
212 /* Allocate buffer for response payload */
213 if (expectRsp) {
214 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
215 if (prsp)
216 prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
217 &prsp->phys);
218 if (!prsp || !prsp->virt)
219 goto els_iocb_free_prsp_exit;
220 INIT_LIST_HEAD(&prsp->list);
221 } else
222 prsp = NULL;
224 /* Allocate buffer for Buffer ptr list */
225 pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
226 if (pbuflist)
227 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
228 &pbuflist->phys);
229 if (!pbuflist || !pbuflist->virt)
230 goto els_iocb_free_pbuf_exit;
232 INIT_LIST_HEAD(&pbuflist->list);
234 if (expectRsp) {
235 icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
236 icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
237 icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
238 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
240 icmd->un.elsreq64.remoteID = did; /* DID */
241 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
242 if (elscmd == ELS_CMD_FLOGI)
243 icmd->ulpTimeout = FF_DEF_RATOV * 2;
244 else
245 icmd->ulpTimeout = phba->fc_ratov * 2;
246 } else {
247 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
248 icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
249 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
250 icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
251 icmd->un.xseq64.xmit_els_remoteID = did; /* DID */
252 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
254 icmd->ulpBdeCount = 1;
255 icmd->ulpLe = 1;
256 icmd->ulpClass = CLASS3;
259 * If we have NPIV enabled, we want to send ELS traffic by VPI.
260 * For SLI4, since the driver controls VPIs we also want to include
261 * all ELS pt2pt protocol traffic as well.
263 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
264 ((phba->sli_rev == LPFC_SLI_REV4) &&
265 (vport->fc_flag & FC_PT2PT))) {
267 if (expectRsp) {
268 icmd->un.elsreq64.myID = vport->fc_myDID;
270 /* For ELS_REQUEST64_CR, use the VPI by default */
271 icmd->ulpContext = phba->vpi_ids[vport->vpi];
274 icmd->ulpCt_h = 0;
275 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
276 if (elscmd == ELS_CMD_ECHO)
277 icmd->ulpCt_l = 0; /* context = invalid RPI */
278 else
279 icmd->ulpCt_l = 1; /* context = VPI */
282 bpl = (struct ulp_bde64 *) pbuflist->virt;
283 bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
284 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
285 bpl->tus.f.bdeSize = cmdSize;
286 bpl->tus.f.bdeFlags = 0;
287 bpl->tus.w = le32_to_cpu(bpl->tus.w);
289 if (expectRsp) {
290 bpl++;
291 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
292 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
293 bpl->tus.f.bdeSize = FCELSSIZE;
294 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
295 bpl->tus.w = le32_to_cpu(bpl->tus.w);
298 /* prevent preparing iocb with NULL ndlp reference */
299 elsiocb->context1 = lpfc_nlp_get(ndlp);
300 if (!elsiocb->context1)
301 goto els_iocb_free_pbuf_exit;
302 elsiocb->context2 = pcmd;
303 elsiocb->context3 = pbuflist;
304 elsiocb->retry = retry;
305 elsiocb->vport = vport;
306 elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
308 if (prsp) {
309 list_add(&prsp->list, &pcmd->list);
311 if (expectRsp) {
312 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
313 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
314 "0116 Xmit ELS command x%x to remote "
315 "NPORT x%x I/O tag: x%x, port state:x%x"
316 " fc_flag:x%x\n",
317 elscmd, did, elsiocb->iotag,
318 vport->port_state,
319 vport->fc_flag);
320 } else {
321 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
322 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
323 "0117 Xmit ELS response x%x to remote "
324 "NPORT x%x I/O tag: x%x, size: x%x "
325 "port_state x%x fc_flag x%x\n",
326 elscmd, ndlp->nlp_DID, elsiocb->iotag,
327 cmdSize, vport->port_state,
328 vport->fc_flag);
330 return elsiocb;
332 els_iocb_free_pbuf_exit:
333 if (expectRsp)
334 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
335 kfree(pbuflist);
337 els_iocb_free_prsp_exit:
338 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
339 kfree(prsp);
341 els_iocb_free_pcmb_exit:
342 kfree(pcmd);
343 lpfc_sli_release_iocbq(phba, elsiocb);
344 return NULL;
348 * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
349 * @vport: pointer to a host virtual N_Port data structure.
351 * This routine issues a fabric registration login for a @vport. An
352 * active ndlp node with Fabric_DID must already exist for this @vport.
353 * The routine invokes two mailbox commands to carry out fabric registration
354 * login through the HBA firmware: the first mailbox command requests the
355 * HBA to perform link configuration for the @vport; and the second mailbox
356 * command requests the HBA to perform the actual fabric registration login
357 * with the @vport.
359 * Return code
360 * 0 - successfully issued fabric registration login for @vport
361 * -ENXIO -- failed to issue fabric registration login for @vport
364 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
366 struct lpfc_hba *phba = vport->phba;
367 LPFC_MBOXQ_t *mbox;
368 struct lpfc_dmabuf *mp;
369 struct lpfc_nodelist *ndlp;
370 struct serv_parm *sp;
371 int rc;
372 int err = 0;
374 sp = &phba->fc_fabparam;
375 ndlp = lpfc_findnode_did(vport, Fabric_DID);
376 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
377 err = 1;
378 goto fail;
381 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
382 if (!mbox) {
383 err = 2;
384 goto fail;
387 vport->port_state = LPFC_FABRIC_CFG_LINK;
388 lpfc_config_link(phba, mbox);
389 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
390 mbox->vport = vport;
392 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
393 if (rc == MBX_NOT_FINISHED) {
394 err = 3;
395 goto fail_free_mbox;
398 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
399 if (!mbox) {
400 err = 4;
401 goto fail;
403 rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
404 ndlp->nlp_rpi);
405 if (rc) {
406 err = 5;
407 goto fail_free_mbox;
410 mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
411 mbox->vport = vport;
412 /* increment the reference count on ndlp to hold reference
413 * for the callback routine.
415 mbox->context2 = lpfc_nlp_get(ndlp);
417 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
418 if (rc == MBX_NOT_FINISHED) {
419 err = 6;
420 goto fail_issue_reg_login;
423 return 0;
425 fail_issue_reg_login:
426 /* decrement the reference count on ndlp just incremented
427 * for the failed mbox command.
429 lpfc_nlp_put(ndlp);
430 mp = (struct lpfc_dmabuf *) mbox->context1;
431 lpfc_mbuf_free(phba, mp->virt, mp->phys);
432 kfree(mp);
433 fail_free_mbox:
434 mempool_free(mbox, phba->mbox_mem_pool);
436 fail:
437 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
438 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
439 "0249 Cannot issue Register Fabric login: Err %d\n", err);
440 return -ENXIO;
444 * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
445 * @vport: pointer to a host virtual N_Port data structure.
447 * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
448 * the @vport. This mailbox command is necessary for SLI4 port only.
450 * Return code
451 * 0 - successfully issued REG_VFI for @vport
452 * A failure code otherwise.
455 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
457 struct lpfc_hba *phba = vport->phba;
458 LPFC_MBOXQ_t *mboxq = NULL;
459 struct lpfc_nodelist *ndlp;
460 struct lpfc_dmabuf *dmabuf = NULL;
461 int rc = 0;
463 /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
464 if ((phba->sli_rev == LPFC_SLI_REV4) &&
465 !(phba->link_flag & LS_LOOPBACK_MODE) &&
466 !(vport->fc_flag & FC_PT2PT)) {
467 ndlp = lpfc_findnode_did(vport, Fabric_DID);
468 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
469 rc = -ENODEV;
470 goto fail;
474 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
475 if (!mboxq) {
476 rc = -ENOMEM;
477 goto fail;
480 /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
481 if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
482 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
483 if (!dmabuf) {
484 rc = -ENOMEM;
485 goto fail;
487 dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
488 if (!dmabuf->virt) {
489 rc = -ENOMEM;
490 goto fail;
492 memcpy(dmabuf->virt, &phba->fc_fabparam,
493 sizeof(struct serv_parm));
496 vport->port_state = LPFC_FABRIC_CFG_LINK;
497 if (dmabuf)
498 lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
499 else
500 lpfc_reg_vfi(mboxq, vport, 0);
502 mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
503 mboxq->vport = vport;
504 mboxq->context1 = dmabuf;
505 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
506 if (rc == MBX_NOT_FINISHED) {
507 rc = -ENXIO;
508 goto fail;
510 return 0;
512 fail:
513 if (mboxq)
514 mempool_free(mboxq, phba->mbox_mem_pool);
515 if (dmabuf) {
516 if (dmabuf->virt)
517 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
518 kfree(dmabuf);
521 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
522 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
523 "0289 Issue Register VFI failed: Err %d\n", rc);
524 return rc;
528 * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
529 * @vport: pointer to a host virtual N_Port data structure.
531 * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
532 * the @vport. This mailbox command is necessary for SLI4 port only.
534 * Return code
535 * 0 - successfully issued REG_VFI for @vport
536 * A failure code otherwise.
539 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
541 struct lpfc_hba *phba = vport->phba;
542 struct Scsi_Host *shost;
543 LPFC_MBOXQ_t *mboxq;
544 int rc;
546 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
547 if (!mboxq) {
548 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
549 "2556 UNREG_VFI mbox allocation failed"
550 "HBA state x%x\n", phba->pport->port_state);
551 return -ENOMEM;
554 lpfc_unreg_vfi(mboxq, vport);
555 mboxq->vport = vport;
556 mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
558 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
559 if (rc == MBX_NOT_FINISHED) {
560 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
561 "2557 UNREG_VFI issue mbox failed rc x%x "
562 "HBA state x%x\n",
563 rc, phba->pport->port_state);
564 mempool_free(mboxq, phba->mbox_mem_pool);
565 return -EIO;
568 shost = lpfc_shost_from_vport(vport);
569 spin_lock_irq(shost->host_lock);
570 vport->fc_flag &= ~FC_VFI_REGISTERED;
571 spin_unlock_irq(shost->host_lock);
572 return 0;
576 * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
577 * @vport: pointer to a host virtual N_Port data structure.
578 * @sp: pointer to service parameter data structure.
580 * This routine is called from FLOGI/FDISC completion handler functions.
581 * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
582 * node nodename is changed in the completion service parameter else return
583 * 0. This function also set flag in the vport data structure to delay
584 * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
585 * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
586 * node nodename is changed in the completion service parameter.
588 * Return code
589 * 0 - FCID and Fabric Nodename and Fabric portname is not changed.
590 * 1 - FCID or Fabric Nodename or Fabric portname is changed.
593 static uint8_t
594 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
595 struct serv_parm *sp)
597 uint8_t fabric_param_changed = 0;
598 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
600 if ((vport->fc_prevDID != vport->fc_myDID) ||
601 memcmp(&vport->fabric_portname, &sp->portName,
602 sizeof(struct lpfc_name)) ||
603 memcmp(&vport->fabric_nodename, &sp->nodeName,
604 sizeof(struct lpfc_name)))
605 fabric_param_changed = 1;
608 * Word 1 Bit 31 in common service parameter is overloaded.
609 * Word 1 Bit 31 in FLOGI request is multiple NPort request
610 * Word 1 Bit 31 in FLOGI response is clean address bit
612 * If fabric parameter is changed and clean address bit is
613 * cleared delay nport discovery if
614 * - vport->fc_prevDID != 0 (not initial discovery) OR
615 * - lpfc_delay_discovery module parameter is set.
617 if (fabric_param_changed && !sp->cmn.clean_address_bit &&
618 (vport->fc_prevDID || lpfc_delay_discovery)) {
619 spin_lock_irq(shost->host_lock);
620 vport->fc_flag |= FC_DISC_DELAYED;
621 spin_unlock_irq(shost->host_lock);
624 return fabric_param_changed;
629 * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
630 * @vport: pointer to a host virtual N_Port data structure.
631 * @ndlp: pointer to a node-list data structure.
632 * @sp: pointer to service parameter data structure.
633 * @irsp: pointer to the IOCB within the lpfc response IOCB.
635 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
636 * function to handle the completion of a Fabric Login (FLOGI) into a fabric
637 * port in a fabric topology. It properly sets up the parameters to the @ndlp
638 * from the IOCB response. It also check the newly assigned N_Port ID to the
639 * @vport against the previously assigned N_Port ID. If it is different from
640 * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
641 * is invoked on all the remaining nodes with the @vport to unregister the
642 * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
643 * is invoked to register login to the fabric.
645 * Return code
646 * 0 - Success (currently, always return 0)
648 static int
649 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
650 struct serv_parm *sp, IOCB_t *irsp)
652 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
653 struct lpfc_hba *phba = vport->phba;
654 struct lpfc_nodelist *np;
655 struct lpfc_nodelist *next_np;
656 uint8_t fabric_param_changed;
658 spin_lock_irq(shost->host_lock);
659 vport->fc_flag |= FC_FABRIC;
660 spin_unlock_irq(shost->host_lock);
662 phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
663 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
664 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
666 phba->fc_edtovResol = sp->cmn.edtovResolution;
667 phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
669 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
670 spin_lock_irq(shost->host_lock);
671 vport->fc_flag |= FC_PUBLIC_LOOP;
672 spin_unlock_irq(shost->host_lock);
675 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
676 memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
677 memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
678 ndlp->nlp_class_sup = 0;
679 if (sp->cls1.classValid)
680 ndlp->nlp_class_sup |= FC_COS_CLASS1;
681 if (sp->cls2.classValid)
682 ndlp->nlp_class_sup |= FC_COS_CLASS2;
683 if (sp->cls3.classValid)
684 ndlp->nlp_class_sup |= FC_COS_CLASS3;
685 if (sp->cls4.classValid)
686 ndlp->nlp_class_sup |= FC_COS_CLASS4;
687 ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
688 sp->cmn.bbRcvSizeLsb;
690 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
691 memcpy(&vport->fabric_portname, &sp->portName,
692 sizeof(struct lpfc_name));
693 memcpy(&vport->fabric_nodename, &sp->nodeName,
694 sizeof(struct lpfc_name));
695 memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
697 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
698 if (sp->cmn.response_multiple_NPort) {
699 lpfc_printf_vlog(vport, KERN_WARNING,
700 LOG_ELS | LOG_VPORT,
701 "1816 FLOGI NPIV supported, "
702 "response data 0x%x\n",
703 sp->cmn.response_multiple_NPort);
704 spin_lock_irq(&phba->hbalock);
705 phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
706 spin_unlock_irq(&phba->hbalock);
707 } else {
708 /* Because we asked f/w for NPIV it still expects us
709 to call reg_vnpid atleast for the physcial host */
710 lpfc_printf_vlog(vport, KERN_WARNING,
711 LOG_ELS | LOG_VPORT,
712 "1817 Fabric does not support NPIV "
713 "- configuring single port mode.\n");
714 spin_lock_irq(&phba->hbalock);
715 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
716 spin_unlock_irq(&phba->hbalock);
721 * For FC we need to do some special processing because of the SLI
722 * Port's default settings of the Common Service Parameters.
724 if ((phba->sli_rev == LPFC_SLI_REV4) &&
725 (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
726 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
727 if (fabric_param_changed)
728 lpfc_unregister_fcf_prep(phba);
730 /* This should just update the VFI CSPs*/
731 if (vport->fc_flag & FC_VFI_REGISTERED)
732 lpfc_issue_reg_vfi(vport);
735 if (fabric_param_changed &&
736 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
738 /* If our NportID changed, we need to ensure all
739 * remaining NPORTs get unreg_login'ed.
741 list_for_each_entry_safe(np, next_np,
742 &vport->fc_nodes, nlp_listp) {
743 if (!NLP_CHK_NODE_ACT(np))
744 continue;
745 if ((np->nlp_state != NLP_STE_NPR_NODE) ||
746 !(np->nlp_flag & NLP_NPR_ADISC))
747 continue;
748 spin_lock_irq(shost->host_lock);
749 np->nlp_flag &= ~NLP_NPR_ADISC;
750 spin_unlock_irq(shost->host_lock);
751 lpfc_unreg_rpi(vport, np);
753 lpfc_cleanup_pending_mbox(vport);
755 if (phba->sli_rev == LPFC_SLI_REV4) {
756 lpfc_sli4_unreg_all_rpis(vport);
757 lpfc_mbx_unreg_vpi(vport);
758 spin_lock_irq(shost->host_lock);
759 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
760 spin_unlock_irq(shost->host_lock);
764 * For SLI3 and SLI4, the VPI needs to be reregistered in
765 * response to this fabric parameter change event.
767 spin_lock_irq(shost->host_lock);
768 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
769 spin_unlock_irq(shost->host_lock);
770 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
771 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
773 * Driver needs to re-reg VPI in order for f/w
774 * to update the MAC address.
776 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
777 lpfc_register_new_vport(phba, vport, ndlp);
778 return 0;
781 if (phba->sli_rev < LPFC_SLI_REV4) {
782 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
783 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
784 vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
785 lpfc_register_new_vport(phba, vport, ndlp);
786 else
787 lpfc_issue_fabric_reglogin(vport);
788 } else {
789 ndlp->nlp_type |= NLP_FABRIC;
790 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
791 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
792 (vport->vpi_state & LPFC_VPI_REGISTERED)) {
793 lpfc_start_fdiscs(phba);
794 lpfc_do_scr_ns_plogi(phba, vport);
795 } else if (vport->fc_flag & FC_VFI_REGISTERED)
796 lpfc_issue_init_vpi(vport);
797 else {
798 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
799 "3135 Need register VFI: (x%x/%x)\n",
800 vport->fc_prevDID, vport->fc_myDID);
801 lpfc_issue_reg_vfi(vport);
804 return 0;
808 * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
809 * @vport: pointer to a host virtual N_Port data structure.
810 * @ndlp: pointer to a node-list data structure.
811 * @sp: pointer to service parameter data structure.
813 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
814 * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
815 * in a point-to-point topology. First, the @vport's N_Port Name is compared
816 * with the received N_Port Name: if the @vport's N_Port Name is greater than
817 * the received N_Port Name lexicographically, this node shall assign local
818 * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
819 * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
820 * this node shall just wait for the remote node to issue PLOGI and assign
821 * N_Port IDs.
823 * Return code
824 * 0 - Success
825 * -ENXIO - Fail
827 static int
828 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
829 struct serv_parm *sp)
831 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
832 struct lpfc_hba *phba = vport->phba;
833 LPFC_MBOXQ_t *mbox;
834 int rc;
836 spin_lock_irq(shost->host_lock);
837 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
838 vport->fc_flag |= FC_PT2PT;
839 spin_unlock_irq(shost->host_lock);
841 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
842 if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
843 lpfc_unregister_fcf_prep(phba);
845 spin_lock_irq(shost->host_lock);
846 vport->fc_flag &= ~FC_VFI_REGISTERED;
847 spin_unlock_irq(shost->host_lock);
848 phba->fc_topology_changed = 0;
851 rc = memcmp(&vport->fc_portname, &sp->portName,
852 sizeof(vport->fc_portname));
854 if (rc >= 0) {
855 /* This side will initiate the PLOGI */
856 spin_lock_irq(shost->host_lock);
857 vport->fc_flag |= FC_PT2PT_PLOGI;
858 spin_unlock_irq(shost->host_lock);
861 * N_Port ID cannot be 0, set our Id to LocalID
862 * the other side will be RemoteID.
865 /* not equal */
866 if (rc)
867 vport->fc_myDID = PT2PT_LocalID;
869 /* Decrement ndlp reference count indicating that ndlp can be
870 * safely released when other references to it are done.
872 lpfc_nlp_put(ndlp);
874 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
875 if (!ndlp) {
877 * Cannot find existing Fabric ndlp, so allocate a
878 * new one
880 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
881 if (!ndlp)
882 goto fail;
883 lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
884 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
885 ndlp = lpfc_enable_node(vport, ndlp,
886 NLP_STE_UNUSED_NODE);
887 if(!ndlp)
888 goto fail;
891 memcpy(&ndlp->nlp_portname, &sp->portName,
892 sizeof(struct lpfc_name));
893 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
894 sizeof(struct lpfc_name));
895 /* Set state will put ndlp onto node list if not already done */
896 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
897 spin_lock_irq(shost->host_lock);
898 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
899 spin_unlock_irq(shost->host_lock);
900 } else
901 /* This side will wait for the PLOGI, decrement ndlp reference
902 * count indicating that ndlp can be released when other
903 * references to it are done.
905 lpfc_nlp_put(ndlp);
907 /* If we are pt2pt with another NPort, force NPIV off! */
908 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
910 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
911 if (!mbox)
912 goto fail;
914 lpfc_config_link(phba, mbox);
916 mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
917 mbox->vport = vport;
918 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
919 if (rc == MBX_NOT_FINISHED) {
920 mempool_free(mbox, phba->mbox_mem_pool);
921 goto fail;
924 return 0;
925 fail:
926 return -ENXIO;
930 * lpfc_cmpl_els_flogi - Completion callback function for flogi
931 * @phba: pointer to lpfc hba data structure.
932 * @cmdiocb: pointer to lpfc command iocb data structure.
933 * @rspiocb: pointer to lpfc response iocb data structure.
935 * This routine is the top-level completion callback function for issuing
936 * a Fabric Login (FLOGI) command. If the response IOCB reported error,
937 * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
938 * retry has been made (either immediately or delayed with lpfc_els_retry()
939 * returning 1), the command IOCB will be released and function returned.
940 * If the retry attempt has been given up (possibly reach the maximum
941 * number of retries), one additional decrement of ndlp reference shall be
942 * invoked before going out after releasing the command IOCB. This will
943 * actually release the remote node (Note, lpfc_els_free_iocb() will also
944 * invoke one decrement of ndlp reference count). If no error reported in
945 * the IOCB status, the command Port ID field is used to determine whether
946 * this is a point-to-point topology or a fabric topology: if the Port ID
947 * field is assigned, it is a fabric topology; otherwise, it is a
948 * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
949 * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
950 * specific topology completion conditions.
952 static void
953 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
954 struct lpfc_iocbq *rspiocb)
956 struct lpfc_vport *vport = cmdiocb->vport;
957 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
958 IOCB_t *irsp = &rspiocb->iocb;
959 struct lpfc_nodelist *ndlp = cmdiocb->context1;
960 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
961 struct serv_parm *sp;
962 uint16_t fcf_index;
963 int rc;
965 /* Check to see if link went down during discovery */
966 if (lpfc_els_chk_latt(vport)) {
967 /* One additional decrement on node reference count to
968 * trigger the release of the node
970 lpfc_nlp_put(ndlp);
971 goto out;
974 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
975 "FLOGI cmpl: status:x%x/x%x state:x%x",
976 irsp->ulpStatus, irsp->un.ulpWord[4],
977 vport->port_state);
979 if (irsp->ulpStatus) {
981 * In case of FIP mode, perform roundrobin FCF failover
982 * due to new FCF discovery
984 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
985 (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
986 if (phba->link_state < LPFC_LINK_UP)
987 goto stop_rr_fcf_flogi;
988 if ((phba->fcoe_cvl_eventtag_attn ==
989 phba->fcoe_cvl_eventtag) &&
990 (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
991 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
992 IOERR_SLI_ABORTED))
993 goto stop_rr_fcf_flogi;
994 else
995 phba->fcoe_cvl_eventtag_attn =
996 phba->fcoe_cvl_eventtag;
997 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
998 "2611 FLOGI failed on FCF (x%x), "
999 "status:x%x/x%x, tmo:x%x, perform "
1000 "roundrobin FCF failover\n",
1001 phba->fcf.current_rec.fcf_indx,
1002 irsp->ulpStatus, irsp->un.ulpWord[4],
1003 irsp->ulpTimeout);
1004 lpfc_sli4_set_fcf_flogi_fail(phba,
1005 phba->fcf.current_rec.fcf_indx);
1006 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1007 rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1008 if (rc)
1009 goto out;
1012 stop_rr_fcf_flogi:
1013 /* FLOGI failure */
1014 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1015 "2858 FLOGI failure Status:x%x/x%x TMO:x%x "
1016 "Data x%x x%x\n",
1017 irsp->ulpStatus, irsp->un.ulpWord[4],
1018 irsp->ulpTimeout, phba->hba_flag,
1019 phba->fcf.fcf_flag);
1021 /* Check for retry */
1022 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1023 goto out;
1025 /* FLOGI failure */
1026 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1027 "0100 FLOGI failure Status:x%x/x%x TMO:x%x\n",
1028 irsp->ulpStatus, irsp->un.ulpWord[4],
1029 irsp->ulpTimeout);
1031 /* FLOGI failed, so there is no fabric */
1032 spin_lock_irq(shost->host_lock);
1033 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1034 spin_unlock_irq(shost->host_lock);
1036 /* If private loop, then allow max outstanding els to be
1037 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1038 * alpa map would take too long otherwise.
1040 if (phba->alpa_map[0] == 0)
1041 vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1042 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1043 (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1044 (vport->fc_prevDID != vport->fc_myDID) ||
1045 phba->fc_topology_changed)) {
1046 if (vport->fc_flag & FC_VFI_REGISTERED) {
1047 if (phba->fc_topology_changed) {
1048 lpfc_unregister_fcf_prep(phba);
1049 spin_lock_irq(shost->host_lock);
1050 vport->fc_flag &= ~FC_VFI_REGISTERED;
1051 spin_unlock_irq(shost->host_lock);
1052 phba->fc_topology_changed = 0;
1053 } else {
1054 lpfc_sli4_unreg_all_rpis(vport);
1058 /* Do not register VFI if the driver aborted FLOGI */
1059 if (!lpfc_error_lost_link(irsp))
1060 lpfc_issue_reg_vfi(vport);
1061 lpfc_nlp_put(ndlp);
1062 goto out;
1064 goto flogifail;
1066 spin_lock_irq(shost->host_lock);
1067 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1068 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1069 spin_unlock_irq(shost->host_lock);
1072 * The FLogI succeeded. Sync the data for the CPU before
1073 * accessing it.
1075 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1076 if (!prsp)
1077 goto out;
1078 sp = prsp->virt + sizeof(uint32_t);
1080 /* FLOGI completes successfully */
1081 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1082 "0101 FLOGI completes successfully, I/O tag:x%x, "
1083 "Data: x%x x%x x%x x%x x%x x%x\n", cmdiocb->iotag,
1084 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1085 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1086 vport->port_state, vport->fc_flag);
1088 if (vport->port_state == LPFC_FLOGI) {
1090 * If Common Service Parameters indicate Nport
1091 * we are point to point, if Fport we are Fabric.
1093 if (sp->cmn.fPort)
1094 rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1095 else if (!(phba->hba_flag & HBA_FCOE_MODE))
1096 rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1097 else {
1098 lpfc_printf_vlog(vport, KERN_ERR,
1099 LOG_FIP | LOG_ELS,
1100 "2831 FLOGI response with cleared Fabric "
1101 "bit fcf_index 0x%x "
1102 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1103 "Fabric Name "
1104 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1105 phba->fcf.current_rec.fcf_indx,
1106 phba->fcf.current_rec.switch_name[0],
1107 phba->fcf.current_rec.switch_name[1],
1108 phba->fcf.current_rec.switch_name[2],
1109 phba->fcf.current_rec.switch_name[3],
1110 phba->fcf.current_rec.switch_name[4],
1111 phba->fcf.current_rec.switch_name[5],
1112 phba->fcf.current_rec.switch_name[6],
1113 phba->fcf.current_rec.switch_name[7],
1114 phba->fcf.current_rec.fabric_name[0],
1115 phba->fcf.current_rec.fabric_name[1],
1116 phba->fcf.current_rec.fabric_name[2],
1117 phba->fcf.current_rec.fabric_name[3],
1118 phba->fcf.current_rec.fabric_name[4],
1119 phba->fcf.current_rec.fabric_name[5],
1120 phba->fcf.current_rec.fabric_name[6],
1121 phba->fcf.current_rec.fabric_name[7]);
1122 lpfc_nlp_put(ndlp);
1123 spin_lock_irq(&phba->hbalock);
1124 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1125 phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1126 spin_unlock_irq(&phba->hbalock);
1127 goto out;
1129 if (!rc) {
1130 /* Mark the FCF discovery process done */
1131 if (phba->hba_flag & HBA_FIP_SUPPORT)
1132 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1133 LOG_ELS,
1134 "2769 FLOGI to FCF (x%x) "
1135 "completed successfully\n",
1136 phba->fcf.current_rec.fcf_indx);
1137 spin_lock_irq(&phba->hbalock);
1138 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1139 phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1140 spin_unlock_irq(&phba->hbalock);
1141 goto out;
1145 flogifail:
1146 spin_lock_irq(&phba->hbalock);
1147 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1148 spin_unlock_irq(&phba->hbalock);
1150 lpfc_nlp_put(ndlp);
1152 if (!lpfc_error_lost_link(irsp)) {
1153 /* FLOGI failed, so just use loop map to make discovery list */
1154 lpfc_disc_list_loopmap(vport);
1156 /* Start discovery */
1157 lpfc_disc_start(vport);
1158 } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1159 (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1160 IOERR_SLI_ABORTED) &&
1161 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1162 IOERR_SLI_DOWN))) &&
1163 (phba->link_state != LPFC_CLEAR_LA)) {
1164 /* If FLOGI failed enable link interrupt. */
1165 lpfc_issue_clear_la(phba, vport);
1167 out:
1168 lpfc_els_free_iocb(phba, cmdiocb);
1172 * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1173 * @vport: pointer to a host virtual N_Port data structure.
1174 * @ndlp: pointer to a node-list data structure.
1175 * @retry: number of retries to the command IOCB.
1177 * This routine issues a Fabric Login (FLOGI) Request ELS command
1178 * for a @vport. The initiator service parameters are put into the payload
1179 * of the FLOGI Request IOCB and the top-level callback function pointer
1180 * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1181 * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1182 * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1184 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1185 * will be incremented by 1 for holding the ndlp and the reference to ndlp
1186 * will be stored into the context1 field of the IOCB for the completion
1187 * callback function to the FLOGI ELS command.
1189 * Return code
1190 * 0 - successfully issued flogi iocb for @vport
1191 * 1 - failed to issue flogi iocb for @vport
1193 static int
1194 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1195 uint8_t retry)
1197 struct lpfc_hba *phba = vport->phba;
1198 struct serv_parm *sp;
1199 IOCB_t *icmd;
1200 struct lpfc_iocbq *elsiocb;
1201 uint8_t *pcmd;
1202 uint16_t cmdsize;
1203 uint32_t tmo;
1204 int rc;
1206 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1207 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1208 ndlp->nlp_DID, ELS_CMD_FLOGI);
1210 if (!elsiocb)
1211 return 1;
1213 icmd = &elsiocb->iocb;
1214 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1216 /* For FLOGI request, remainder of payload is service parameters */
1217 *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1218 pcmd += sizeof(uint32_t);
1219 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1220 sp = (struct serv_parm *) pcmd;
1222 /* Setup CSPs accordingly for Fabric */
1223 sp->cmn.e_d_tov = 0;
1224 sp->cmn.w2.r_a_tov = 0;
1225 sp->cmn.virtual_fabric_support = 0;
1226 sp->cls1.classValid = 0;
1227 if (sp->cmn.fcphLow < FC_PH3)
1228 sp->cmn.fcphLow = FC_PH3;
1229 if (sp->cmn.fcphHigh < FC_PH3)
1230 sp->cmn.fcphHigh = FC_PH3;
1232 if (phba->sli_rev == LPFC_SLI_REV4) {
1233 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1234 LPFC_SLI_INTF_IF_TYPE_0) {
1235 elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1236 elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1237 /* FLOGI needs to be 3 for WQE FCFI */
1238 /* Set the fcfi to the fcfi we registered with */
1239 elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1241 /* Can't do SLI4 class2 without support sequence coalescing */
1242 sp->cls2.classValid = 0;
1243 sp->cls2.seqDelivery = 0;
1244 } else {
1245 /* Historical, setting sequential-delivery bit for SLI3 */
1246 sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1247 sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1248 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1249 sp->cmn.request_multiple_Nport = 1;
1250 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1251 icmd->ulpCt_h = 1;
1252 icmd->ulpCt_l = 0;
1253 } else
1254 sp->cmn.request_multiple_Nport = 0;
1257 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1258 icmd->un.elsreq64.myID = 0;
1259 icmd->un.elsreq64.fl = 1;
1262 tmo = phba->fc_ratov;
1263 phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1264 lpfc_set_disctmo(vport);
1265 phba->fc_ratov = tmo;
1267 phba->fc_stat.elsXmitFLOGI++;
1268 elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1270 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1271 "Issue FLOGI: opt:x%x",
1272 phba->sli3_options, 0, 0);
1274 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1275 if (rc == IOCB_ERROR) {
1276 lpfc_els_free_iocb(phba, elsiocb);
1277 return 1;
1279 return 0;
1283 * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1284 * @phba: pointer to lpfc hba data structure.
1286 * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1287 * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1288 * list and issues an abort IOCB commond on each outstanding IOCB that
1289 * contains a active Fabric_DID ndlp. Note that this function is to issue
1290 * the abort IOCB command on all the outstanding IOCBs, thus when this
1291 * function returns, it does not guarantee all the IOCBs are actually aborted.
1293 * Return code
1294 * 0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1297 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1299 struct lpfc_sli_ring *pring;
1300 struct lpfc_iocbq *iocb, *next_iocb;
1301 struct lpfc_nodelist *ndlp;
1302 IOCB_t *icmd;
1304 /* Abort outstanding I/O on NPort <nlp_DID> */
1305 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1306 "0201 Abort outstanding I/O on NPort x%x\n",
1307 Fabric_DID);
1309 pring = &phba->sli.ring[LPFC_ELS_RING];
1312 * Check the txcmplq for an iocb that matches the nport the driver is
1313 * searching for.
1315 spin_lock_irq(&phba->hbalock);
1316 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1317 icmd = &iocb->iocb;
1318 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1319 ndlp = (struct lpfc_nodelist *)(iocb->context1);
1320 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1321 (ndlp->nlp_DID == Fabric_DID))
1322 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1325 spin_unlock_irq(&phba->hbalock);
1327 return 0;
1331 * lpfc_initial_flogi - Issue an initial fabric login for a vport
1332 * @vport: pointer to a host virtual N_Port data structure.
1334 * This routine issues an initial Fabric Login (FLOGI) for the @vport
1335 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1336 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1337 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1338 * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1339 * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1340 * @vport.
1342 * Return code
1343 * 0 - failed to issue initial flogi for @vport
1344 * 1 - successfully issued initial flogi for @vport
1347 lpfc_initial_flogi(struct lpfc_vport *vport)
1349 struct lpfc_hba *phba = vport->phba;
1350 struct lpfc_nodelist *ndlp;
1352 vport->port_state = LPFC_FLOGI;
1353 lpfc_set_disctmo(vport);
1355 /* First look for the Fabric ndlp */
1356 ndlp = lpfc_findnode_did(vport, Fabric_DID);
1357 if (!ndlp) {
1358 /* Cannot find existing Fabric ndlp, so allocate a new one */
1359 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1360 if (!ndlp)
1361 return 0;
1362 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1363 /* Set the node type */
1364 ndlp->nlp_type |= NLP_FABRIC;
1365 /* Put ndlp onto node list */
1366 lpfc_enqueue_node(vport, ndlp);
1367 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1368 /* re-setup ndlp without removing from node list */
1369 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1370 if (!ndlp)
1371 return 0;
1374 if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1375 /* This decrement of reference count to node shall kick off
1376 * the release of the node.
1378 lpfc_nlp_put(ndlp);
1379 return 0;
1381 return 1;
1385 * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1386 * @vport: pointer to a host virtual N_Port data structure.
1388 * This routine issues an initial Fabric Discover (FDISC) for the @vport
1389 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1390 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1391 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1392 * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1393 * is then invoked with the @vport and the ndlp to perform the FDISC for the
1394 * @vport.
1396 * Return code
1397 * 0 - failed to issue initial fdisc for @vport
1398 * 1 - successfully issued initial fdisc for @vport
1401 lpfc_initial_fdisc(struct lpfc_vport *vport)
1403 struct lpfc_hba *phba = vport->phba;
1404 struct lpfc_nodelist *ndlp;
1406 /* First look for the Fabric ndlp */
1407 ndlp = lpfc_findnode_did(vport, Fabric_DID);
1408 if (!ndlp) {
1409 /* Cannot find existing Fabric ndlp, so allocate a new one */
1410 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1411 if (!ndlp)
1412 return 0;
1413 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1414 /* Put ndlp onto node list */
1415 lpfc_enqueue_node(vport, ndlp);
1416 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1417 /* re-setup ndlp without removing from node list */
1418 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1419 if (!ndlp)
1420 return 0;
1423 if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1424 /* decrement node reference count to trigger the release of
1425 * the node.
1427 lpfc_nlp_put(ndlp);
1428 return 0;
1430 return 1;
1434 * lpfc_more_plogi - Check and issue remaining plogis for a vport
1435 * @vport: pointer to a host virtual N_Port data structure.
1437 * This routine checks whether there are more remaining Port Logins
1438 * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1439 * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1440 * to issue ELS PLOGIs up to the configured discover threads with the
1441 * @vport (@vport->cfg_discovery_threads). The function also decrement
1442 * the @vport's num_disc_node by 1 if it is not already 0.
1444 void
1445 lpfc_more_plogi(struct lpfc_vport *vport)
1447 if (vport->num_disc_nodes)
1448 vport->num_disc_nodes--;
1450 /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1451 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1452 "0232 Continue discovery with %d PLOGIs to go "
1453 "Data: x%x x%x x%x\n",
1454 vport->num_disc_nodes, vport->fc_plogi_cnt,
1455 vport->fc_flag, vport->port_state);
1456 /* Check to see if there are more PLOGIs to be sent */
1457 if (vport->fc_flag & FC_NLP_MORE)
1458 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1459 lpfc_els_disc_plogi(vport);
1461 return;
1465 * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1466 * @phba: pointer to lpfc hba data structure.
1467 * @prsp: pointer to response IOCB payload.
1468 * @ndlp: pointer to a node-list data structure.
1470 * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1471 * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1472 * The following cases are considered N_Port confirmed:
1473 * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1474 * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1475 * it does not have WWPN assigned either. If the WWPN is confirmed, the
1476 * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1477 * 1) if there is a node on vport list other than the @ndlp with the same
1478 * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1479 * on that node to release the RPI associated with the node; 2) if there is
1480 * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1481 * into, a new node shall be allocated (or activated). In either case, the
1482 * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1483 * be released and the new_ndlp shall be put on to the vport node list and
1484 * its pointer returned as the confirmed node.
1486 * Note that before the @ndlp got "released", the keepDID from not-matching
1487 * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1488 * of the @ndlp. This is because the release of @ndlp is actually to put it
1489 * into an inactive state on the vport node list and the vport node list
1490 * management algorithm does not allow two node with a same DID.
1492 * Return code
1493 * pointer to the PLOGI N_Port @ndlp
1495 static struct lpfc_nodelist *
1496 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1497 struct lpfc_nodelist *ndlp)
1499 struct lpfc_vport *vport = ndlp->vport;
1500 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1501 struct lpfc_nodelist *new_ndlp;
1502 struct lpfc_rport_data *rdata;
1503 struct fc_rport *rport;
1504 struct serv_parm *sp;
1505 uint8_t name[sizeof(struct lpfc_name)];
1506 uint32_t rc, keepDID = 0, keep_nlp_flag = 0;
1507 uint16_t keep_nlp_state;
1508 int put_node;
1509 int put_rport;
1510 unsigned long *active_rrqs_xri_bitmap = NULL;
1512 /* Fabric nodes can have the same WWPN so we don't bother searching
1513 * by WWPN. Just return the ndlp that was given to us.
1515 if (ndlp->nlp_type & NLP_FABRIC)
1516 return ndlp;
1518 sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1519 memset(name, 0, sizeof(struct lpfc_name));
1521 /* Now we find out if the NPort we are logging into, matches the WWPN
1522 * we have for that ndlp. If not, we have some work to do.
1524 new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1526 if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1527 return ndlp;
1528 if (phba->sli_rev == LPFC_SLI_REV4) {
1529 active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1530 GFP_KERNEL);
1531 if (active_rrqs_xri_bitmap)
1532 memset(active_rrqs_xri_bitmap, 0,
1533 phba->cfg_rrq_xri_bitmap_sz);
1536 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1537 "3178 PLOGI confirm: ndlp %p x%x: new_ndlp %p\n",
1538 ndlp, ndlp->nlp_DID, new_ndlp);
1540 if (!new_ndlp) {
1541 rc = memcmp(&ndlp->nlp_portname, name,
1542 sizeof(struct lpfc_name));
1543 if (!rc) {
1544 if (active_rrqs_xri_bitmap)
1545 mempool_free(active_rrqs_xri_bitmap,
1546 phba->active_rrq_pool);
1547 return ndlp;
1549 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1550 if (!new_ndlp) {
1551 if (active_rrqs_xri_bitmap)
1552 mempool_free(active_rrqs_xri_bitmap,
1553 phba->active_rrq_pool);
1554 return ndlp;
1556 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1557 } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1558 rc = memcmp(&ndlp->nlp_portname, name,
1559 sizeof(struct lpfc_name));
1560 if (!rc) {
1561 if (active_rrqs_xri_bitmap)
1562 mempool_free(active_rrqs_xri_bitmap,
1563 phba->active_rrq_pool);
1564 return ndlp;
1566 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1567 NLP_STE_UNUSED_NODE);
1568 if (!new_ndlp) {
1569 if (active_rrqs_xri_bitmap)
1570 mempool_free(active_rrqs_xri_bitmap,
1571 phba->active_rrq_pool);
1572 return ndlp;
1574 keepDID = new_ndlp->nlp_DID;
1575 if ((phba->sli_rev == LPFC_SLI_REV4) && active_rrqs_xri_bitmap)
1576 memcpy(active_rrqs_xri_bitmap,
1577 new_ndlp->active_rrqs_xri_bitmap,
1578 phba->cfg_rrq_xri_bitmap_sz);
1579 } else {
1580 keepDID = new_ndlp->nlp_DID;
1581 if (phba->sli_rev == LPFC_SLI_REV4 &&
1582 active_rrqs_xri_bitmap)
1583 memcpy(active_rrqs_xri_bitmap,
1584 new_ndlp->active_rrqs_xri_bitmap,
1585 phba->cfg_rrq_xri_bitmap_sz);
1588 lpfc_unreg_rpi(vport, new_ndlp);
1589 new_ndlp->nlp_DID = ndlp->nlp_DID;
1590 new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1591 if (phba->sli_rev == LPFC_SLI_REV4)
1592 memcpy(new_ndlp->active_rrqs_xri_bitmap,
1593 ndlp->active_rrqs_xri_bitmap,
1594 phba->cfg_rrq_xri_bitmap_sz);
1596 spin_lock_irq(shost->host_lock);
1597 keep_nlp_flag = new_ndlp->nlp_flag;
1598 new_ndlp->nlp_flag = ndlp->nlp_flag;
1599 ndlp->nlp_flag = keep_nlp_flag;
1600 spin_unlock_irq(shost->host_lock);
1602 /* Set nlp_states accordingly */
1603 keep_nlp_state = new_ndlp->nlp_state;
1604 lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1606 /* Move this back to NPR state */
1607 if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1608 /* The new_ndlp is replacing ndlp totally, so we need
1609 * to put ndlp on UNUSED list and try to free it.
1611 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1612 "3179 PLOGI confirm NEW: %x %x\n",
1613 new_ndlp->nlp_DID, keepDID);
1615 /* Fix up the rport accordingly */
1616 rport = ndlp->rport;
1617 if (rport) {
1618 rdata = rport->dd_data;
1619 if (rdata->pnode == ndlp) {
1620 /* break the link before dropping the ref */
1621 ndlp->rport = NULL;
1622 lpfc_nlp_put(ndlp);
1623 rdata->pnode = lpfc_nlp_get(new_ndlp);
1624 new_ndlp->rport = rport;
1626 new_ndlp->nlp_type = ndlp->nlp_type;
1628 /* We shall actually free the ndlp with both nlp_DID and
1629 * nlp_portname fields equals 0 to avoid any ndlp on the
1630 * nodelist never to be used.
1632 if (ndlp->nlp_DID == 0) {
1633 spin_lock_irq(&phba->ndlp_lock);
1634 NLP_SET_FREE_REQ(ndlp);
1635 spin_unlock_irq(&phba->ndlp_lock);
1638 /* Two ndlps cannot have the same did on the nodelist */
1639 ndlp->nlp_DID = keepDID;
1640 if (phba->sli_rev == LPFC_SLI_REV4 &&
1641 active_rrqs_xri_bitmap)
1642 memcpy(ndlp->active_rrqs_xri_bitmap,
1643 active_rrqs_xri_bitmap,
1644 phba->cfg_rrq_xri_bitmap_sz);
1646 if (!NLP_CHK_NODE_ACT(ndlp))
1647 lpfc_drop_node(vport, ndlp);
1649 else {
1650 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1651 "3180 PLOGI confirm SWAP: %x %x\n",
1652 new_ndlp->nlp_DID, keepDID);
1654 lpfc_unreg_rpi(vport, ndlp);
1656 /* Two ndlps cannot have the same did */
1657 ndlp->nlp_DID = keepDID;
1658 if (phba->sli_rev == LPFC_SLI_REV4 &&
1659 active_rrqs_xri_bitmap)
1660 memcpy(ndlp->active_rrqs_xri_bitmap,
1661 active_rrqs_xri_bitmap,
1662 phba->cfg_rrq_xri_bitmap_sz);
1664 /* Since we are switching over to the new_ndlp,
1665 * reset the old ndlp state
1667 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1668 (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1669 keep_nlp_state = NLP_STE_NPR_NODE;
1670 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1672 /* Fix up the rport accordingly */
1673 rport = ndlp->rport;
1674 if (rport) {
1675 rdata = rport->dd_data;
1676 put_node = rdata->pnode != NULL;
1677 put_rport = ndlp->rport != NULL;
1678 rdata->pnode = NULL;
1679 ndlp->rport = NULL;
1680 if (put_node)
1681 lpfc_nlp_put(ndlp);
1682 if (put_rport)
1683 put_device(&rport->dev);
1686 if (phba->sli_rev == LPFC_SLI_REV4 &&
1687 active_rrqs_xri_bitmap)
1688 mempool_free(active_rrqs_xri_bitmap,
1689 phba->active_rrq_pool);
1690 return new_ndlp;
1694 * lpfc_end_rscn - Check and handle more rscn for a vport
1695 * @vport: pointer to a host virtual N_Port data structure.
1697 * This routine checks whether more Registration State Change
1698 * Notifications (RSCNs) came in while the discovery state machine was in
1699 * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1700 * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1701 * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1702 * handling the RSCNs.
1704 void
1705 lpfc_end_rscn(struct lpfc_vport *vport)
1707 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1709 if (vport->fc_flag & FC_RSCN_MODE) {
1711 * Check to see if more RSCNs came in while we were
1712 * processing this one.
1714 if (vport->fc_rscn_id_cnt ||
1715 (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1716 lpfc_els_handle_rscn(vport);
1717 else {
1718 spin_lock_irq(shost->host_lock);
1719 vport->fc_flag &= ~FC_RSCN_MODE;
1720 spin_unlock_irq(shost->host_lock);
1726 * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1727 * @phba: pointer to lpfc hba data structure.
1728 * @cmdiocb: pointer to lpfc command iocb data structure.
1729 * @rspiocb: pointer to lpfc response iocb data structure.
1731 * This routine will call the clear rrq function to free the rrq and
1732 * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1733 * exist then the clear_rrq is still called because the rrq needs to
1734 * be freed.
1737 static void
1738 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1739 struct lpfc_iocbq *rspiocb)
1741 struct lpfc_vport *vport = cmdiocb->vport;
1742 IOCB_t *irsp;
1743 struct lpfc_nodelist *ndlp;
1744 struct lpfc_node_rrq *rrq;
1746 /* we pass cmdiocb to state machine which needs rspiocb as well */
1747 rrq = cmdiocb->context_un.rrq;
1748 cmdiocb->context_un.rsp_iocb = rspiocb;
1750 irsp = &rspiocb->iocb;
1751 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1752 "RRQ cmpl: status:x%x/x%x did:x%x",
1753 irsp->ulpStatus, irsp->un.ulpWord[4],
1754 irsp->un.elsreq64.remoteID);
1756 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1757 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1758 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1759 "2882 RRQ completes to NPort x%x "
1760 "with no ndlp. Data: x%x x%x x%x\n",
1761 irsp->un.elsreq64.remoteID,
1762 irsp->ulpStatus, irsp->un.ulpWord[4],
1763 irsp->ulpIoTag);
1764 goto out;
1767 /* rrq completes to NPort <nlp_DID> */
1768 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1769 "2880 RRQ completes to NPort x%x "
1770 "Data: x%x x%x x%x x%x x%x\n",
1771 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1772 irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1774 if (irsp->ulpStatus) {
1775 /* Check for retry */
1776 /* RRQ failed Don't print the vport to vport rjts */
1777 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1778 (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1779 ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1780 (phba)->pport->cfg_log_verbose & LOG_ELS)
1781 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1782 "2881 RRQ failure DID:%06X Status:x%x/x%x\n",
1783 ndlp->nlp_DID, irsp->ulpStatus,
1784 irsp->un.ulpWord[4]);
1786 out:
1787 if (rrq)
1788 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1789 lpfc_els_free_iocb(phba, cmdiocb);
1790 return;
1793 * lpfc_cmpl_els_plogi - Completion callback function for plogi
1794 * @phba: pointer to lpfc hba data structure.
1795 * @cmdiocb: pointer to lpfc command iocb data structure.
1796 * @rspiocb: pointer to lpfc response iocb data structure.
1798 * This routine is the completion callback function for issuing the Port
1799 * Login (PLOGI) command. For PLOGI completion, there must be an active
1800 * ndlp on the vport node list that matches the remote node ID from the
1801 * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1802 * ignored and command IOCB released. The PLOGI response IOCB status is
1803 * checked for error conditons. If there is error status reported, PLOGI
1804 * retry shall be attempted by invoking the lpfc_els_retry() routine.
1805 * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1806 * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1807 * (DSM) is set for this PLOGI completion. Finally, it checks whether
1808 * there are additional N_Port nodes with the vport that need to perform
1809 * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1810 * PLOGIs.
1812 static void
1813 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1814 struct lpfc_iocbq *rspiocb)
1816 struct lpfc_vport *vport = cmdiocb->vport;
1817 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1818 IOCB_t *irsp;
1819 struct lpfc_nodelist *ndlp;
1820 struct lpfc_dmabuf *prsp;
1821 int disc, rc;
1823 /* we pass cmdiocb to state machine which needs rspiocb as well */
1824 cmdiocb->context_un.rsp_iocb = rspiocb;
1826 irsp = &rspiocb->iocb;
1827 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1828 "PLOGI cmpl: status:x%x/x%x did:x%x",
1829 irsp->ulpStatus, irsp->un.ulpWord[4],
1830 irsp->un.elsreq64.remoteID);
1832 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1833 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1834 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1835 "0136 PLOGI completes to NPort x%x "
1836 "with no ndlp. Data: x%x x%x x%x\n",
1837 irsp->un.elsreq64.remoteID,
1838 irsp->ulpStatus, irsp->un.ulpWord[4],
1839 irsp->ulpIoTag);
1840 goto out;
1843 /* Since ndlp can be freed in the disc state machine, note if this node
1844 * is being used during discovery.
1846 spin_lock_irq(shost->host_lock);
1847 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1848 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1849 spin_unlock_irq(shost->host_lock);
1850 rc = 0;
1852 /* PLOGI completes to NPort <nlp_DID> */
1853 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1854 "0102 PLOGI completes to NPort x%x "
1855 "Data: x%x x%x x%x x%x x%x\n",
1856 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1857 irsp->ulpTimeout, disc, vport->num_disc_nodes);
1858 /* Check to see if link went down during discovery */
1859 if (lpfc_els_chk_latt(vport)) {
1860 spin_lock_irq(shost->host_lock);
1861 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1862 spin_unlock_irq(shost->host_lock);
1863 goto out;
1866 if (irsp->ulpStatus) {
1867 /* Check for retry */
1868 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1869 /* ELS command is being retried */
1870 if (disc) {
1871 spin_lock_irq(shost->host_lock);
1872 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1873 spin_unlock_irq(shost->host_lock);
1875 goto out;
1877 /* PLOGI failed Don't print the vport to vport rjts */
1878 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1879 (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1880 ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1881 (phba)->pport->cfg_log_verbose & LOG_ELS)
1882 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1883 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1884 ndlp->nlp_DID, irsp->ulpStatus,
1885 irsp->un.ulpWord[4]);
1886 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1887 if (lpfc_error_lost_link(irsp))
1888 rc = NLP_STE_FREED_NODE;
1889 else
1890 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1891 NLP_EVT_CMPL_PLOGI);
1892 } else {
1893 /* Good status, call state machine */
1894 prsp = list_entry(((struct lpfc_dmabuf *)
1895 cmdiocb->context2)->list.next,
1896 struct lpfc_dmabuf, list);
1897 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1898 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1899 NLP_EVT_CMPL_PLOGI);
1902 if (disc && vport->num_disc_nodes) {
1903 /* Check to see if there are more PLOGIs to be sent */
1904 lpfc_more_plogi(vport);
1906 if (vport->num_disc_nodes == 0) {
1907 spin_lock_irq(shost->host_lock);
1908 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1909 spin_unlock_irq(shost->host_lock);
1911 lpfc_can_disctmo(vport);
1912 lpfc_end_rscn(vport);
1916 out:
1917 lpfc_els_free_iocb(phba, cmdiocb);
1918 return;
1922 * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1923 * @vport: pointer to a host virtual N_Port data structure.
1924 * @did: destination port identifier.
1925 * @retry: number of retries to the command IOCB.
1927 * This routine issues a Port Login (PLOGI) command to a remote N_Port
1928 * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1929 * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1930 * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1931 * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1933 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1934 * will be incremented by 1 for holding the ndlp and the reference to ndlp
1935 * will be stored into the context1 field of the IOCB for the completion
1936 * callback function to the PLOGI ELS command.
1938 * Return code
1939 * 0 - Successfully issued a plogi for @vport
1940 * 1 - failed to issue a plogi for @vport
1943 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1945 struct lpfc_hba *phba = vport->phba;
1946 struct serv_parm *sp;
1947 struct lpfc_nodelist *ndlp;
1948 struct lpfc_iocbq *elsiocb;
1949 uint8_t *pcmd;
1950 uint16_t cmdsize;
1951 int ret;
1953 ndlp = lpfc_findnode_did(vport, did);
1954 if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1955 ndlp = NULL;
1957 /* If ndlp is not NULL, we will bump the reference count on it */
1958 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1959 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1960 ELS_CMD_PLOGI);
1961 if (!elsiocb)
1962 return 1;
1964 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1966 /* For PLOGI request, remainder of payload is service parameters */
1967 *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1968 pcmd += sizeof(uint32_t);
1969 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1970 sp = (struct serv_parm *) pcmd;
1973 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
1974 * to device on remote loops work.
1976 if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
1977 sp->cmn.altBbCredit = 1;
1979 if (sp->cmn.fcphLow < FC_PH_4_3)
1980 sp->cmn.fcphLow = FC_PH_4_3;
1982 if (sp->cmn.fcphHigh < FC_PH3)
1983 sp->cmn.fcphHigh = FC_PH3;
1985 sp->cmn.valid_vendor_ver_level = 0;
1986 memset(sp->vendorVersion, 0, sizeof(sp->vendorVersion));
1988 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1989 "Issue PLOGI: did:x%x",
1990 did, 0, 0);
1992 phba->fc_stat.elsXmitPLOGI++;
1993 elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1994 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
1996 if (ret == IOCB_ERROR) {
1997 lpfc_els_free_iocb(phba, elsiocb);
1998 return 1;
2000 return 0;
2004 * lpfc_cmpl_els_prli - Completion callback function for prli
2005 * @phba: pointer to lpfc hba data structure.
2006 * @cmdiocb: pointer to lpfc command iocb data structure.
2007 * @rspiocb: pointer to lpfc response iocb data structure.
2009 * This routine is the completion callback function for a Process Login
2010 * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2011 * status. If there is error status reported, PRLI retry shall be attempted
2012 * by invoking the lpfc_els_retry() routine. Otherwise, the state
2013 * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2014 * ndlp to mark the PRLI completion.
2016 static void
2017 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2018 struct lpfc_iocbq *rspiocb)
2020 struct lpfc_vport *vport = cmdiocb->vport;
2021 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2022 IOCB_t *irsp;
2023 struct lpfc_nodelist *ndlp;
2025 /* we pass cmdiocb to state machine which needs rspiocb as well */
2026 cmdiocb->context_un.rsp_iocb = rspiocb;
2028 irsp = &(rspiocb->iocb);
2029 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2030 spin_lock_irq(shost->host_lock);
2031 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2032 spin_unlock_irq(shost->host_lock);
2034 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2035 "PRLI cmpl: status:x%x/x%x did:x%x",
2036 irsp->ulpStatus, irsp->un.ulpWord[4],
2037 ndlp->nlp_DID);
2038 /* PRLI completes to NPort <nlp_DID> */
2039 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2040 "0103 PRLI completes to NPort x%x "
2041 "Data: x%x x%x x%x x%x\n",
2042 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2043 irsp->ulpTimeout, vport->num_disc_nodes);
2045 vport->fc_prli_sent--;
2046 /* Check to see if link went down during discovery */
2047 if (lpfc_els_chk_latt(vport))
2048 goto out;
2050 if (irsp->ulpStatus) {
2051 /* Check for retry */
2052 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2053 /* ELS command is being retried */
2054 goto out;
2056 /* PRLI failed */
2057 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2058 "2754 PRLI failure DID:%06X Status:x%x/x%x\n",
2059 ndlp->nlp_DID, irsp->ulpStatus,
2060 irsp->un.ulpWord[4]);
2061 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2062 if (lpfc_error_lost_link(irsp))
2063 goto out;
2064 else
2065 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2066 NLP_EVT_CMPL_PRLI);
2067 } else
2068 /* Good status, call state machine */
2069 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2070 NLP_EVT_CMPL_PRLI);
2071 out:
2072 lpfc_els_free_iocb(phba, cmdiocb);
2073 return;
2077 * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2078 * @vport: pointer to a host virtual N_Port data structure.
2079 * @ndlp: pointer to a node-list data structure.
2080 * @retry: number of retries to the command IOCB.
2082 * This routine issues a Process Login (PRLI) ELS command for the
2083 * @vport. The PRLI service parameters are set up in the payload of the
2084 * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2085 * is put to the IOCB completion callback func field before invoking the
2086 * routine lpfc_sli_issue_iocb() to send out PRLI command.
2088 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2089 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2090 * will be stored into the context1 field of the IOCB for the completion
2091 * callback function to the PRLI ELS command.
2093 * Return code
2094 * 0 - successfully issued prli iocb command for @vport
2095 * 1 - failed to issue prli iocb command for @vport
2098 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2099 uint8_t retry)
2101 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2102 struct lpfc_hba *phba = vport->phba;
2103 PRLI *npr;
2104 struct lpfc_iocbq *elsiocb;
2105 uint8_t *pcmd;
2106 uint16_t cmdsize;
2108 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2109 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2110 ndlp->nlp_DID, ELS_CMD_PRLI);
2111 if (!elsiocb)
2112 return 1;
2114 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2116 /* For PRLI request, remainder of payload is service parameters */
2117 memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
2118 *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
2119 pcmd += sizeof(uint32_t);
2121 /* For PRLI, remainder of payload is PRLI parameter page */
2122 npr = (PRLI *) pcmd;
2124 * If our firmware version is 3.20 or later,
2125 * set the following bits for FC-TAPE support.
2127 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2128 npr->ConfmComplAllowed = 1;
2129 npr->Retry = 1;
2130 npr->TaskRetryIdReq = 1;
2132 npr->estabImagePair = 1;
2133 npr->readXferRdyDis = 1;
2134 if (vport->cfg_first_burst_size)
2135 npr->writeXferRdyDis = 1;
2137 /* For FCP support */
2138 npr->prliType = PRLI_FCP_TYPE;
2139 npr->initiatorFunc = 1;
2141 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2142 "Issue PRLI: did:x%x",
2143 ndlp->nlp_DID, 0, 0);
2145 phba->fc_stat.elsXmitPRLI++;
2146 elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2147 spin_lock_irq(shost->host_lock);
2148 ndlp->nlp_flag |= NLP_PRLI_SND;
2149 spin_unlock_irq(shost->host_lock);
2150 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2151 IOCB_ERROR) {
2152 spin_lock_irq(shost->host_lock);
2153 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2154 spin_unlock_irq(shost->host_lock);
2155 lpfc_els_free_iocb(phba, elsiocb);
2156 return 1;
2158 vport->fc_prli_sent++;
2159 return 0;
2163 * lpfc_rscn_disc - Perform rscn discovery for a vport
2164 * @vport: pointer to a host virtual N_Port data structure.
2166 * This routine performs Registration State Change Notification (RSCN)
2167 * discovery for a @vport. If the @vport's node port recovery count is not
2168 * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2169 * the nodes that need recovery. If none of the PLOGI were needed through
2170 * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2171 * invoked to check and handle possible more RSCN came in during the period
2172 * of processing the current ones.
2174 static void
2175 lpfc_rscn_disc(struct lpfc_vport *vport)
2177 lpfc_can_disctmo(vport);
2179 /* RSCN discovery */
2180 /* go thru NPR nodes and issue ELS PLOGIs */
2181 if (vport->fc_npr_cnt)
2182 if (lpfc_els_disc_plogi(vport))
2183 return;
2185 lpfc_end_rscn(vport);
2189 * lpfc_adisc_done - Complete the adisc phase of discovery
2190 * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2192 * This function is called when the final ADISC is completed during discovery.
2193 * This function handles clearing link attention or issuing reg_vpi depending
2194 * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2195 * discovery.
2196 * This function is called with no locks held.
2198 static void
2199 lpfc_adisc_done(struct lpfc_vport *vport)
2201 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2202 struct lpfc_hba *phba = vport->phba;
2205 * For NPIV, cmpl_reg_vpi will set port_state to READY,
2206 * and continue discovery.
2208 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2209 !(vport->fc_flag & FC_RSCN_MODE) &&
2210 (phba->sli_rev < LPFC_SLI_REV4)) {
2211 /* The ADISCs are complete. Doesn't matter if they
2212 * succeeded or failed because the ADISC completion
2213 * routine guarantees to call the state machine and
2214 * the RPI is either unregistered (failed ADISC response)
2215 * or the RPI is still valid and the node is marked
2216 * mapped for a target. The exchanges should be in the
2217 * correct state. This code is specific to SLI3.
2219 lpfc_issue_clear_la(phba, vport);
2220 lpfc_issue_reg_vpi(phba, vport);
2221 return;
2224 * For SLI2, we need to set port_state to READY
2225 * and continue discovery.
2227 if (vport->port_state < LPFC_VPORT_READY) {
2228 /* If we get here, there is nothing to ADISC */
2229 lpfc_issue_clear_la(phba, vport);
2230 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2231 vport->num_disc_nodes = 0;
2232 /* go thru NPR list, issue ELS PLOGIs */
2233 if (vport->fc_npr_cnt)
2234 lpfc_els_disc_plogi(vport);
2235 if (!vport->num_disc_nodes) {
2236 spin_lock_irq(shost->host_lock);
2237 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2238 spin_unlock_irq(shost->host_lock);
2239 lpfc_can_disctmo(vport);
2240 lpfc_end_rscn(vport);
2243 vport->port_state = LPFC_VPORT_READY;
2244 } else
2245 lpfc_rscn_disc(vport);
2249 * lpfc_more_adisc - Issue more adisc as needed
2250 * @vport: pointer to a host virtual N_Port data structure.
2252 * This routine determines whether there are more ndlps on a @vport
2253 * node list need to have Address Discover (ADISC) issued. If so, it will
2254 * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2255 * remaining nodes which need to have ADISC sent.
2257 void
2258 lpfc_more_adisc(struct lpfc_vport *vport)
2260 if (vport->num_disc_nodes)
2261 vport->num_disc_nodes--;
2262 /* Continue discovery with <num_disc_nodes> ADISCs to go */
2263 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2264 "0210 Continue discovery with %d ADISCs to go "
2265 "Data: x%x x%x x%x\n",
2266 vport->num_disc_nodes, vport->fc_adisc_cnt,
2267 vport->fc_flag, vport->port_state);
2268 /* Check to see if there are more ADISCs to be sent */
2269 if (vport->fc_flag & FC_NLP_MORE) {
2270 lpfc_set_disctmo(vport);
2271 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2272 lpfc_els_disc_adisc(vport);
2274 if (!vport->num_disc_nodes)
2275 lpfc_adisc_done(vport);
2276 return;
2280 * lpfc_cmpl_els_adisc - Completion callback function for adisc
2281 * @phba: pointer to lpfc hba data structure.
2282 * @cmdiocb: pointer to lpfc command iocb data structure.
2283 * @rspiocb: pointer to lpfc response iocb data structure.
2285 * This routine is the completion function for issuing the Address Discover
2286 * (ADISC) command. It first checks to see whether link went down during
2287 * the discovery process. If so, the node will be marked as node port
2288 * recovery for issuing discover IOCB by the link attention handler and
2289 * exit. Otherwise, the response status is checked. If error was reported
2290 * in the response status, the ADISC command shall be retried by invoking
2291 * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2292 * the response status, the state machine is invoked to set transition
2293 * with respect to NLP_EVT_CMPL_ADISC event.
2295 static void
2296 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2297 struct lpfc_iocbq *rspiocb)
2299 struct lpfc_vport *vport = cmdiocb->vport;
2300 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2301 IOCB_t *irsp;
2302 struct lpfc_nodelist *ndlp;
2303 int disc;
2305 /* we pass cmdiocb to state machine which needs rspiocb as well */
2306 cmdiocb->context_un.rsp_iocb = rspiocb;
2308 irsp = &(rspiocb->iocb);
2309 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2311 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2312 "ADISC cmpl: status:x%x/x%x did:x%x",
2313 irsp->ulpStatus, irsp->un.ulpWord[4],
2314 ndlp->nlp_DID);
2316 /* Since ndlp can be freed in the disc state machine, note if this node
2317 * is being used during discovery.
2319 spin_lock_irq(shost->host_lock);
2320 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2321 ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2322 spin_unlock_irq(shost->host_lock);
2323 /* ADISC completes to NPort <nlp_DID> */
2324 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2325 "0104 ADISC completes to NPort x%x "
2326 "Data: x%x x%x x%x x%x x%x\n",
2327 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2328 irsp->ulpTimeout, disc, vport->num_disc_nodes);
2329 /* Check to see if link went down during discovery */
2330 if (lpfc_els_chk_latt(vport)) {
2331 spin_lock_irq(shost->host_lock);
2332 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2333 spin_unlock_irq(shost->host_lock);
2334 goto out;
2337 if (irsp->ulpStatus) {
2338 /* Check for retry */
2339 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2340 /* ELS command is being retried */
2341 if (disc) {
2342 spin_lock_irq(shost->host_lock);
2343 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2344 spin_unlock_irq(shost->host_lock);
2345 lpfc_set_disctmo(vport);
2347 goto out;
2349 /* ADISC failed */
2350 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2351 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2352 ndlp->nlp_DID, irsp->ulpStatus,
2353 irsp->un.ulpWord[4]);
2354 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2355 if (!lpfc_error_lost_link(irsp))
2356 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2357 NLP_EVT_CMPL_ADISC);
2358 } else
2359 /* Good status, call state machine */
2360 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2361 NLP_EVT_CMPL_ADISC);
2363 /* Check to see if there are more ADISCs to be sent */
2364 if (disc && vport->num_disc_nodes)
2365 lpfc_more_adisc(vport);
2366 out:
2367 lpfc_els_free_iocb(phba, cmdiocb);
2368 return;
2372 * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2373 * @vport: pointer to a virtual N_Port data structure.
2374 * @ndlp: pointer to a node-list data structure.
2375 * @retry: number of retries to the command IOCB.
2377 * This routine issues an Address Discover (ADISC) for an @ndlp on a
2378 * @vport. It prepares the payload of the ADISC ELS command, updates the
2379 * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2380 * to issue the ADISC ELS command.
2382 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2383 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2384 * will be stored into the context1 field of the IOCB for the completion
2385 * callback function to the ADISC ELS command.
2387 * Return code
2388 * 0 - successfully issued adisc
2389 * 1 - failed to issue adisc
2392 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2393 uint8_t retry)
2395 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2396 struct lpfc_hba *phba = vport->phba;
2397 ADISC *ap;
2398 struct lpfc_iocbq *elsiocb;
2399 uint8_t *pcmd;
2400 uint16_t cmdsize;
2402 cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2403 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2404 ndlp->nlp_DID, ELS_CMD_ADISC);
2405 if (!elsiocb)
2406 return 1;
2408 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2410 /* For ADISC request, remainder of payload is service parameters */
2411 *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2412 pcmd += sizeof(uint32_t);
2414 /* Fill in ADISC payload */
2415 ap = (ADISC *) pcmd;
2416 ap->hardAL_PA = phba->fc_pref_ALPA;
2417 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2418 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2419 ap->DID = be32_to_cpu(vport->fc_myDID);
2421 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2422 "Issue ADISC: did:x%x",
2423 ndlp->nlp_DID, 0, 0);
2425 phba->fc_stat.elsXmitADISC++;
2426 elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2427 spin_lock_irq(shost->host_lock);
2428 ndlp->nlp_flag |= NLP_ADISC_SND;
2429 spin_unlock_irq(shost->host_lock);
2430 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2431 IOCB_ERROR) {
2432 spin_lock_irq(shost->host_lock);
2433 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2434 spin_unlock_irq(shost->host_lock);
2435 lpfc_els_free_iocb(phba, elsiocb);
2436 return 1;
2438 return 0;
2442 * lpfc_cmpl_els_logo - Completion callback function for logo
2443 * @phba: pointer to lpfc hba data structure.
2444 * @cmdiocb: pointer to lpfc command iocb data structure.
2445 * @rspiocb: pointer to lpfc response iocb data structure.
2447 * This routine is the completion function for issuing the ELS Logout (LOGO)
2448 * command. If no error status was reported from the LOGO response, the
2449 * state machine of the associated ndlp shall be invoked for transition with
2450 * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2451 * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2453 static void
2454 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2455 struct lpfc_iocbq *rspiocb)
2457 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2458 struct lpfc_vport *vport = ndlp->vport;
2459 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2460 IOCB_t *irsp;
2461 struct lpfcMboxq *mbox;
2462 unsigned long flags;
2463 uint32_t skip_recovery = 0;
2465 /* we pass cmdiocb to state machine which needs rspiocb as well */
2466 cmdiocb->context_un.rsp_iocb = rspiocb;
2468 irsp = &(rspiocb->iocb);
2469 spin_lock_irq(shost->host_lock);
2470 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2471 spin_unlock_irq(shost->host_lock);
2473 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2474 "LOGO cmpl: status:x%x/x%x did:x%x",
2475 irsp->ulpStatus, irsp->un.ulpWord[4],
2476 ndlp->nlp_DID);
2478 /* LOGO completes to NPort <nlp_DID> */
2479 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2480 "0105 LOGO completes to NPort x%x "
2481 "Data: x%x x%x x%x x%x\n",
2482 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2483 irsp->ulpTimeout, vport->num_disc_nodes);
2485 if (lpfc_els_chk_latt(vport)) {
2486 skip_recovery = 1;
2487 goto out;
2490 /* Check to see if link went down during discovery */
2491 if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2492 /* NLP_EVT_DEVICE_RM should unregister the RPI
2493 * which should abort all outstanding IOs.
2495 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2496 NLP_EVT_DEVICE_RM);
2497 skip_recovery = 1;
2498 goto out;
2501 if (irsp->ulpStatus) {
2502 /* Check for retry */
2503 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2504 /* ELS command is being retried */
2505 skip_recovery = 1;
2506 goto out;
2508 /* LOGO failed */
2509 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2510 "2756 LOGO failure DID:%06X Status:x%x/x%x\n",
2511 ndlp->nlp_DID, irsp->ulpStatus,
2512 irsp->un.ulpWord[4]);
2513 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2514 if (lpfc_error_lost_link(irsp)) {
2515 skip_recovery = 1;
2516 goto out;
2520 /* Call state machine. This will unregister the rpi if needed. */
2521 lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2523 out:
2524 lpfc_els_free_iocb(phba, cmdiocb);
2525 /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2526 if ((vport->fc_flag & FC_PT2PT) &&
2527 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2528 phba->pport->fc_myDID = 0;
2529 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2530 if (mbox) {
2531 lpfc_config_link(phba, mbox);
2532 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2533 mbox->vport = vport;
2534 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2535 MBX_NOT_FINISHED) {
2536 mempool_free(mbox, phba->mbox_mem_pool);
2537 skip_recovery = 1;
2543 * If the node is a target, the handling attempts to recover the port.
2544 * For any other port type, the rpi is unregistered as an implicit
2545 * LOGO.
2547 if ((ndlp->nlp_type & NLP_FCP_TARGET) && (skip_recovery == 0)) {
2548 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2549 spin_lock_irqsave(shost->host_lock, flags);
2550 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2551 spin_unlock_irqrestore(shost->host_lock, flags);
2553 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2554 "3187 LOGO completes to NPort x%x: Start "
2555 "Recovery Data: x%x x%x x%x x%x\n",
2556 ndlp->nlp_DID, irsp->ulpStatus,
2557 irsp->un.ulpWord[4], irsp->ulpTimeout,
2558 vport->num_disc_nodes);
2559 lpfc_disc_start(vport);
2561 return;
2565 * lpfc_issue_els_logo - Issue a logo to an node on a vport
2566 * @vport: pointer to a virtual N_Port data structure.
2567 * @ndlp: pointer to a node-list data structure.
2568 * @retry: number of retries to the command IOCB.
2570 * This routine constructs and issues an ELS Logout (LOGO) iocb command
2571 * to a remote node, referred by an @ndlp on a @vport. It constructs the
2572 * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2573 * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2575 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2576 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2577 * will be stored into the context1 field of the IOCB for the completion
2578 * callback function to the LOGO ELS command.
2580 * Return code
2581 * 0 - successfully issued logo
2582 * 1 - failed to issue logo
2585 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2586 uint8_t retry)
2588 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2589 struct lpfc_hba *phba = vport->phba;
2590 struct lpfc_iocbq *elsiocb;
2591 uint8_t *pcmd;
2592 uint16_t cmdsize;
2593 int rc;
2595 spin_lock_irq(shost->host_lock);
2596 if (ndlp->nlp_flag & NLP_LOGO_SND) {
2597 spin_unlock_irq(shost->host_lock);
2598 return 0;
2600 spin_unlock_irq(shost->host_lock);
2602 cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2603 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2604 ndlp->nlp_DID, ELS_CMD_LOGO);
2605 if (!elsiocb)
2606 return 1;
2608 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2609 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2610 pcmd += sizeof(uint32_t);
2612 /* Fill in LOGO payload */
2613 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2614 pcmd += sizeof(uint32_t);
2615 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2617 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2618 "Issue LOGO: did:x%x",
2619 ndlp->nlp_DID, 0, 0);
2622 * If we are issuing a LOGO, we may try to recover the remote NPort
2623 * by issuing a PLOGI later. Even though we issue ELS cmds by the
2624 * VPI, if we have a valid RPI, and that RPI gets unreg'ed while
2625 * that ELS command is in-flight, the HBA returns a IOERR_INVALID_RPI
2626 * for that ELS cmd. To avoid this situation, lets get rid of the
2627 * RPI right now, before any ELS cmds are sent.
2629 spin_lock_irq(shost->host_lock);
2630 ndlp->nlp_flag |= NLP_ISSUE_LOGO;
2631 spin_unlock_irq(shost->host_lock);
2632 if (lpfc_unreg_rpi(vport, ndlp)) {
2633 lpfc_els_free_iocb(phba, elsiocb);
2634 return 0;
2637 phba->fc_stat.elsXmitLOGO++;
2638 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2639 spin_lock_irq(shost->host_lock);
2640 ndlp->nlp_flag |= NLP_LOGO_SND;
2641 ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2642 spin_unlock_irq(shost->host_lock);
2643 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2645 if (rc == IOCB_ERROR) {
2646 spin_lock_irq(shost->host_lock);
2647 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2648 spin_unlock_irq(shost->host_lock);
2649 lpfc_els_free_iocb(phba, elsiocb);
2650 return 1;
2652 return 0;
2656 * lpfc_cmpl_els_cmd - Completion callback function for generic els command
2657 * @phba: pointer to lpfc hba data structure.
2658 * @cmdiocb: pointer to lpfc command iocb data structure.
2659 * @rspiocb: pointer to lpfc response iocb data structure.
2661 * This routine is a generic completion callback function for ELS commands.
2662 * Specifically, it is the callback function which does not need to perform
2663 * any command specific operations. It is currently used by the ELS command
2664 * issuing routines for the ELS State Change Request (SCR),
2665 * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2666 * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2667 * certain debug loggings, this callback function simply invokes the
2668 * lpfc_els_chk_latt() routine to check whether link went down during the
2669 * discovery process.
2671 static void
2672 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2673 struct lpfc_iocbq *rspiocb)
2675 struct lpfc_vport *vport = cmdiocb->vport;
2676 IOCB_t *irsp;
2678 irsp = &rspiocb->iocb;
2680 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2681 "ELS cmd cmpl: status:x%x/x%x did:x%x",
2682 irsp->ulpStatus, irsp->un.ulpWord[4],
2683 irsp->un.elsreq64.remoteID);
2684 /* ELS cmd tag <ulpIoTag> completes */
2685 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2686 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2687 irsp->ulpIoTag, irsp->ulpStatus,
2688 irsp->un.ulpWord[4], irsp->ulpTimeout);
2689 /* Check to see if link went down during discovery */
2690 lpfc_els_chk_latt(vport);
2691 lpfc_els_free_iocb(phba, cmdiocb);
2692 return;
2696 * lpfc_issue_els_scr - Issue a scr to an node on a vport
2697 * @vport: pointer to a host virtual N_Port data structure.
2698 * @nportid: N_Port identifier to the remote node.
2699 * @retry: number of retries to the command IOCB.
2701 * This routine issues a State Change Request (SCR) to a fabric node
2702 * on a @vport. The remote node @nportid is passed into the function. It
2703 * first search the @vport node list to find the matching ndlp. If no such
2704 * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2705 * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2706 * routine is invoked to send the SCR IOCB.
2708 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2709 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2710 * will be stored into the context1 field of the IOCB for the completion
2711 * callback function to the SCR ELS command.
2713 * Return code
2714 * 0 - Successfully issued scr command
2715 * 1 - Failed to issue scr command
2718 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2720 struct lpfc_hba *phba = vport->phba;
2721 struct lpfc_iocbq *elsiocb;
2722 uint8_t *pcmd;
2723 uint16_t cmdsize;
2724 struct lpfc_nodelist *ndlp;
2726 cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2728 ndlp = lpfc_findnode_did(vport, nportid);
2729 if (!ndlp) {
2730 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2731 if (!ndlp)
2732 return 1;
2733 lpfc_nlp_init(vport, ndlp, nportid);
2734 lpfc_enqueue_node(vport, ndlp);
2735 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2736 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2737 if (!ndlp)
2738 return 1;
2741 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2742 ndlp->nlp_DID, ELS_CMD_SCR);
2744 if (!elsiocb) {
2745 /* This will trigger the release of the node just
2746 * allocated
2748 lpfc_nlp_put(ndlp);
2749 return 1;
2752 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2754 *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2755 pcmd += sizeof(uint32_t);
2757 /* For SCR, remainder of payload is SCR parameter page */
2758 memset(pcmd, 0, sizeof(SCR));
2759 ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2761 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2762 "Issue SCR: did:x%x",
2763 ndlp->nlp_DID, 0, 0);
2765 phba->fc_stat.elsXmitSCR++;
2766 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2767 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2768 IOCB_ERROR) {
2769 /* The additional lpfc_nlp_put will cause the following
2770 * lpfc_els_free_iocb routine to trigger the rlease of
2771 * the node.
2773 lpfc_nlp_put(ndlp);
2774 lpfc_els_free_iocb(phba, elsiocb);
2775 return 1;
2777 /* This will cause the callback-function lpfc_cmpl_els_cmd to
2778 * trigger the release of node.
2781 lpfc_nlp_put(ndlp);
2782 return 0;
2786 * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2787 * @vport: pointer to a host virtual N_Port data structure.
2788 * @nportid: N_Port identifier to the remote node.
2789 * @retry: number of retries to the command IOCB.
2791 * This routine issues a Fibre Channel Address Resolution Response
2792 * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2793 * is passed into the function. It first search the @vport node list to find
2794 * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2795 * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2796 * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2798 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2799 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2800 * will be stored into the context1 field of the IOCB for the completion
2801 * callback function to the PARPR ELS command.
2803 * Return code
2804 * 0 - Successfully issued farpr command
2805 * 1 - Failed to issue farpr command
2807 static int
2808 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2810 struct lpfc_hba *phba = vport->phba;
2811 struct lpfc_iocbq *elsiocb;
2812 FARP *fp;
2813 uint8_t *pcmd;
2814 uint32_t *lp;
2815 uint16_t cmdsize;
2816 struct lpfc_nodelist *ondlp;
2817 struct lpfc_nodelist *ndlp;
2819 cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2821 ndlp = lpfc_findnode_did(vport, nportid);
2822 if (!ndlp) {
2823 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2824 if (!ndlp)
2825 return 1;
2826 lpfc_nlp_init(vport, ndlp, nportid);
2827 lpfc_enqueue_node(vport, ndlp);
2828 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2829 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2830 if (!ndlp)
2831 return 1;
2834 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2835 ndlp->nlp_DID, ELS_CMD_RNID);
2836 if (!elsiocb) {
2837 /* This will trigger the release of the node just
2838 * allocated
2840 lpfc_nlp_put(ndlp);
2841 return 1;
2844 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2846 *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2847 pcmd += sizeof(uint32_t);
2849 /* Fill in FARPR payload */
2850 fp = (FARP *) (pcmd);
2851 memset(fp, 0, sizeof(FARP));
2852 lp = (uint32_t *) pcmd;
2853 *lp++ = be32_to_cpu(nportid);
2854 *lp++ = be32_to_cpu(vport->fc_myDID);
2855 fp->Rflags = 0;
2856 fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2858 memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2859 memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2860 ondlp = lpfc_findnode_did(vport, nportid);
2861 if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2862 memcpy(&fp->OportName, &ondlp->nlp_portname,
2863 sizeof(struct lpfc_name));
2864 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2865 sizeof(struct lpfc_name));
2868 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2869 "Issue FARPR: did:x%x",
2870 ndlp->nlp_DID, 0, 0);
2872 phba->fc_stat.elsXmitFARPR++;
2873 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2874 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2875 IOCB_ERROR) {
2876 /* The additional lpfc_nlp_put will cause the following
2877 * lpfc_els_free_iocb routine to trigger the release of
2878 * the node.
2880 lpfc_nlp_put(ndlp);
2881 lpfc_els_free_iocb(phba, elsiocb);
2882 return 1;
2884 /* This will cause the callback-function lpfc_cmpl_els_cmd to
2885 * trigger the release of the node.
2887 lpfc_nlp_put(ndlp);
2888 return 0;
2892 * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
2893 * @vport: pointer to a host virtual N_Port data structure.
2894 * @nlp: pointer to a node-list data structure.
2896 * This routine cancels the timer with a delayed IOCB-command retry for
2897 * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2898 * removes the ELS retry event if it presents. In addition, if the
2899 * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2900 * commands are sent for the @vport's nodes that require issuing discovery
2901 * ADISC.
2903 void
2904 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2906 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2907 struct lpfc_work_evt *evtp;
2909 if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2910 return;
2911 spin_lock_irq(shost->host_lock);
2912 nlp->nlp_flag &= ~NLP_DELAY_TMO;
2913 spin_unlock_irq(shost->host_lock);
2914 del_timer_sync(&nlp->nlp_delayfunc);
2915 nlp->nlp_last_elscmd = 0;
2916 if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2917 list_del_init(&nlp->els_retry_evt.evt_listp);
2918 /* Decrement nlp reference count held for the delayed retry */
2919 evtp = &nlp->els_retry_evt;
2920 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2922 if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2923 spin_lock_irq(shost->host_lock);
2924 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2925 spin_unlock_irq(shost->host_lock);
2926 if (vport->num_disc_nodes) {
2927 if (vport->port_state < LPFC_VPORT_READY) {
2928 /* Check if there are more ADISCs to be sent */
2929 lpfc_more_adisc(vport);
2930 } else {
2931 /* Check if there are more PLOGIs to be sent */
2932 lpfc_more_plogi(vport);
2933 if (vport->num_disc_nodes == 0) {
2934 spin_lock_irq(shost->host_lock);
2935 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2936 spin_unlock_irq(shost->host_lock);
2937 lpfc_can_disctmo(vport);
2938 lpfc_end_rscn(vport);
2943 return;
2947 * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
2948 * @ptr: holder for the pointer to the timer function associated data (ndlp).
2950 * This routine is invoked by the ndlp delayed-function timer to check
2951 * whether there is any pending ELS retry event(s) with the node. If not, it
2952 * simply returns. Otherwise, if there is at least one ELS delayed event, it
2953 * adds the delayed events to the HBA work list and invokes the
2954 * lpfc_worker_wake_up() routine to wake up worker thread to process the
2955 * event. Note that lpfc_nlp_get() is called before posting the event to
2956 * the work list to hold reference count of ndlp so that it guarantees the
2957 * reference to ndlp will still be available when the worker thread gets
2958 * to the event associated with the ndlp.
2960 void
2961 lpfc_els_retry_delay(unsigned long ptr)
2963 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2964 struct lpfc_vport *vport = ndlp->vport;
2965 struct lpfc_hba *phba = vport->phba;
2966 unsigned long flags;
2967 struct lpfc_work_evt *evtp = &ndlp->els_retry_evt;
2969 spin_lock_irqsave(&phba->hbalock, flags);
2970 if (!list_empty(&evtp->evt_listp)) {
2971 spin_unlock_irqrestore(&phba->hbalock, flags);
2972 return;
2975 /* We need to hold the node by incrementing the reference
2976 * count until the queued work is done
2978 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
2979 if (evtp->evt_arg1) {
2980 evtp->evt = LPFC_EVT_ELS_RETRY;
2981 list_add_tail(&evtp->evt_listp, &phba->work_list);
2982 lpfc_worker_wake_up(phba);
2984 spin_unlock_irqrestore(&phba->hbalock, flags);
2985 return;
2989 * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
2990 * @ndlp: pointer to a node-list data structure.
2992 * This routine is the worker-thread handler for processing the @ndlp delayed
2993 * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2994 * the last ELS command from the associated ndlp and invokes the proper ELS
2995 * function according to the delayed ELS command to retry the command.
2997 void
2998 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3000 struct lpfc_vport *vport = ndlp->vport;
3001 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3002 uint32_t cmd, retry;
3004 spin_lock_irq(shost->host_lock);
3005 cmd = ndlp->nlp_last_elscmd;
3006 ndlp->nlp_last_elscmd = 0;
3008 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
3009 spin_unlock_irq(shost->host_lock);
3010 return;
3013 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3014 spin_unlock_irq(shost->host_lock);
3016 * If a discovery event readded nlp_delayfunc after timer
3017 * firing and before processing the timer, cancel the
3018 * nlp_delayfunc.
3020 del_timer_sync(&ndlp->nlp_delayfunc);
3021 retry = ndlp->nlp_retry;
3022 ndlp->nlp_retry = 0;
3024 switch (cmd) {
3025 case ELS_CMD_FLOGI:
3026 lpfc_issue_els_flogi(vport, ndlp, retry);
3027 break;
3028 case ELS_CMD_PLOGI:
3029 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
3030 ndlp->nlp_prev_state = ndlp->nlp_state;
3031 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3033 break;
3034 case ELS_CMD_ADISC:
3035 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
3036 ndlp->nlp_prev_state = ndlp->nlp_state;
3037 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3039 break;
3040 case ELS_CMD_PRLI:
3041 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
3042 ndlp->nlp_prev_state = ndlp->nlp_state;
3043 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3045 break;
3046 case ELS_CMD_LOGO:
3047 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
3048 ndlp->nlp_prev_state = ndlp->nlp_state;
3049 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3051 break;
3052 case ELS_CMD_FDISC:
3053 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3054 lpfc_issue_els_fdisc(vport, ndlp, retry);
3055 break;
3057 return;
3061 * lpfc_els_retry - Make retry decision on an els command iocb
3062 * @phba: pointer to lpfc hba data structure.
3063 * @cmdiocb: pointer to lpfc command iocb data structure.
3064 * @rspiocb: pointer to lpfc response iocb data structure.
3066 * This routine makes a retry decision on an ELS command IOCB, which has
3067 * failed. The following ELS IOCBs use this function for retrying the command
3068 * when previously issued command responsed with error status: FLOGI, PLOGI,
3069 * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3070 * returned error status, it makes the decision whether a retry shall be
3071 * issued for the command, and whether a retry shall be made immediately or
3072 * delayed. In the former case, the corresponding ELS command issuing-function
3073 * is called to retry the command. In the later case, the ELS command shall
3074 * be posted to the ndlp delayed event and delayed function timer set to the
3075 * ndlp for the delayed command issusing.
3077 * Return code
3078 * 0 - No retry of els command is made
3079 * 1 - Immediate or delayed retry of els command is made
3081 static int
3082 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3083 struct lpfc_iocbq *rspiocb)
3085 struct lpfc_vport *vport = cmdiocb->vport;
3086 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3087 IOCB_t *irsp = &rspiocb->iocb;
3088 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3089 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3090 uint32_t *elscmd;
3091 struct ls_rjt stat;
3092 int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
3093 int logerr = 0;
3094 uint32_t cmd = 0;
3095 uint32_t did;
3098 /* Note: context2 may be 0 for internal driver abort
3099 * of delays ELS command.
3102 if (pcmd && pcmd->virt) {
3103 elscmd = (uint32_t *) (pcmd->virt);
3104 cmd = *elscmd++;
3107 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3108 did = ndlp->nlp_DID;
3109 else {
3110 /* We should only hit this case for retrying PLOGI */
3111 did = irsp->un.elsreq64.remoteID;
3112 ndlp = lpfc_findnode_did(vport, did);
3113 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3114 && (cmd != ELS_CMD_PLOGI))
3115 return 1;
3118 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3119 "Retry ELS: wd7:x%x wd4:x%x did:x%x",
3120 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3122 switch (irsp->ulpStatus) {
3123 case IOSTAT_FCP_RSP_ERROR:
3124 break;
3125 case IOSTAT_REMOTE_STOP:
3126 if (phba->sli_rev == LPFC_SLI_REV4) {
3127 /* This IO was aborted by the target, we don't
3128 * know the rxid and because we did not send the
3129 * ABTS we cannot generate and RRQ.
3131 lpfc_set_rrq_active(phba, ndlp,
3132 cmdiocb->sli4_lxritag, 0, 0);
3134 break;
3135 case IOSTAT_LOCAL_REJECT:
3136 switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
3137 case IOERR_LOOP_OPEN_FAILURE:
3138 if (cmd == ELS_CMD_FLOGI) {
3139 if (PCI_DEVICE_ID_HORNET ==
3140 phba->pcidev->device) {
3141 phba->fc_topology = LPFC_TOPOLOGY_LOOP;
3142 phba->pport->fc_myDID = 0;
3143 phba->alpa_map[0] = 0;
3144 phba->alpa_map[1] = 0;
3147 if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
3148 delay = 1000;
3149 retry = 1;
3150 break;
3152 case IOERR_ILLEGAL_COMMAND:
3153 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3154 "0124 Retry illegal cmd x%x "
3155 "retry:x%x delay:x%x\n",
3156 cmd, cmdiocb->retry, delay);
3157 retry = 1;
3158 /* All command's retry policy */
3159 maxretry = 8;
3160 if (cmdiocb->retry > 2)
3161 delay = 1000;
3162 break;
3164 case IOERR_NO_RESOURCES:
3165 logerr = 1; /* HBA out of resources */
3166 retry = 1;
3167 if (cmdiocb->retry > 100)
3168 delay = 100;
3169 maxretry = 250;
3170 break;
3172 case IOERR_ILLEGAL_FRAME:
3173 delay = 100;
3174 retry = 1;
3175 break;
3177 case IOERR_SEQUENCE_TIMEOUT:
3178 case IOERR_INVALID_RPI:
3179 if (cmd == ELS_CMD_PLOGI &&
3180 did == NameServer_DID) {
3181 /* Continue forever if plogi to */
3182 /* the nameserver fails */
3183 maxretry = 0;
3184 delay = 100;
3186 retry = 1;
3187 break;
3189 break;
3191 case IOSTAT_NPORT_RJT:
3192 case IOSTAT_FABRIC_RJT:
3193 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3194 retry = 1;
3195 break;
3197 break;
3199 case IOSTAT_NPORT_BSY:
3200 case IOSTAT_FABRIC_BSY:
3201 logerr = 1; /* Fabric / Remote NPort out of resources */
3202 retry = 1;
3203 break;
3205 case IOSTAT_LS_RJT:
3206 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3207 /* Added for Vendor specifc support
3208 * Just keep retrying for these Rsn / Exp codes
3210 switch (stat.un.b.lsRjtRsnCode) {
3211 case LSRJT_UNABLE_TPC:
3212 if (stat.un.b.lsRjtRsnCodeExp ==
3213 LSEXP_CMD_IN_PROGRESS) {
3214 if (cmd == ELS_CMD_PLOGI) {
3215 delay = 1000;
3216 maxretry = 48;
3218 retry = 1;
3219 break;
3221 if (stat.un.b.lsRjtRsnCodeExp ==
3222 LSEXP_CANT_GIVE_DATA) {
3223 if (cmd == ELS_CMD_PLOGI) {
3224 delay = 1000;
3225 maxretry = 48;
3227 retry = 1;
3228 break;
3230 if ((cmd == ELS_CMD_PLOGI) ||
3231 (cmd == ELS_CMD_PRLI)) {
3232 delay = 1000;
3233 maxretry = lpfc_max_els_tries + 1;
3234 retry = 1;
3235 break;
3237 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3238 (cmd == ELS_CMD_FDISC) &&
3239 (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
3240 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3241 "0125 FDISC Failed (x%x). "
3242 "Fabric out of resources\n",
3243 stat.un.lsRjtError);
3244 lpfc_vport_set_state(vport,
3245 FC_VPORT_NO_FABRIC_RSCS);
3247 break;
3249 case LSRJT_LOGICAL_BSY:
3250 if ((cmd == ELS_CMD_PLOGI) ||
3251 (cmd == ELS_CMD_PRLI)) {
3252 delay = 1000;
3253 maxretry = 48;
3254 } else if (cmd == ELS_CMD_FDISC) {
3255 /* FDISC retry policy */
3256 maxretry = 48;
3257 if (cmdiocb->retry >= 32)
3258 delay = 1000;
3260 retry = 1;
3261 break;
3263 case LSRJT_LOGICAL_ERR:
3264 /* There are some cases where switches return this
3265 * error when they are not ready and should be returning
3266 * Logical Busy. We should delay every time.
3268 if (cmd == ELS_CMD_FDISC &&
3269 stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3270 maxretry = 3;
3271 delay = 1000;
3272 retry = 1;
3273 break;
3275 case LSRJT_PROTOCOL_ERR:
3276 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3277 (cmd == ELS_CMD_FDISC) &&
3278 ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3279 (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3281 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3282 "0122 FDISC Failed (x%x). "
3283 "Fabric Detected Bad WWN\n",
3284 stat.un.lsRjtError);
3285 lpfc_vport_set_state(vport,
3286 FC_VPORT_FABRIC_REJ_WWN);
3288 break;
3290 break;
3292 case IOSTAT_INTERMED_RSP:
3293 case IOSTAT_BA_RJT:
3294 break;
3296 default:
3297 break;
3300 if (did == FDMI_DID)
3301 retry = 1;
3303 if ((cmd == ELS_CMD_FLOGI) &&
3304 (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
3305 !lpfc_error_lost_link(irsp)) {
3306 /* FLOGI retry policy */
3307 retry = 1;
3308 /* retry FLOGI forever */
3309 if (phba->link_flag != LS_LOOPBACK_MODE)
3310 maxretry = 0;
3311 else
3312 maxretry = 2;
3314 if (cmdiocb->retry >= 100)
3315 delay = 5000;
3316 else if (cmdiocb->retry >= 32)
3317 delay = 1000;
3318 } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
3319 /* retry FDISCs every second up to devloss */
3320 retry = 1;
3321 maxretry = vport->cfg_devloss_tmo;
3322 delay = 1000;
3325 cmdiocb->retry++;
3326 if (maxretry && (cmdiocb->retry >= maxretry)) {
3327 phba->fc_stat.elsRetryExceeded++;
3328 retry = 0;
3331 if ((vport->load_flag & FC_UNLOADING) != 0)
3332 retry = 0;
3334 if (retry) {
3335 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3336 /* Stop retrying PLOGI and FDISC if in FCF discovery */
3337 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3338 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3339 "2849 Stop retry ELS command "
3340 "x%x to remote NPORT x%x, "
3341 "Data: x%x x%x\n", cmd, did,
3342 cmdiocb->retry, delay);
3343 return 0;
3347 /* Retry ELS command <elsCmd> to remote NPORT <did> */
3348 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3349 "0107 Retry ELS command x%x to remote "
3350 "NPORT x%x Data: x%x x%x\n",
3351 cmd, did, cmdiocb->retry, delay);
3353 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3354 ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
3355 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
3356 IOERR_NO_RESOURCES))) {
3357 /* Don't reset timer for no resources */
3359 /* If discovery / RSCN timer is running, reset it */
3360 if (timer_pending(&vport->fc_disctmo) ||
3361 (vport->fc_flag & FC_RSCN_MODE))
3362 lpfc_set_disctmo(vport);
3365 phba->fc_stat.elsXmitRetry++;
3366 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
3367 phba->fc_stat.elsDelayRetry++;
3368 ndlp->nlp_retry = cmdiocb->retry;
3370 /* delay is specified in milliseconds */
3371 mod_timer(&ndlp->nlp_delayfunc,
3372 jiffies + msecs_to_jiffies(delay));
3373 spin_lock_irq(shost->host_lock);
3374 ndlp->nlp_flag |= NLP_DELAY_TMO;
3375 spin_unlock_irq(shost->host_lock);
3377 ndlp->nlp_prev_state = ndlp->nlp_state;
3378 if (cmd == ELS_CMD_PRLI)
3379 lpfc_nlp_set_state(vport, ndlp,
3380 NLP_STE_PRLI_ISSUE);
3381 else
3382 lpfc_nlp_set_state(vport, ndlp,
3383 NLP_STE_NPR_NODE);
3384 ndlp->nlp_last_elscmd = cmd;
3386 return 1;
3388 switch (cmd) {
3389 case ELS_CMD_FLOGI:
3390 lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
3391 return 1;
3392 case ELS_CMD_FDISC:
3393 lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3394 return 1;
3395 case ELS_CMD_PLOGI:
3396 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3397 ndlp->nlp_prev_state = ndlp->nlp_state;
3398 lpfc_nlp_set_state(vport, ndlp,
3399 NLP_STE_PLOGI_ISSUE);
3401 lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
3402 return 1;
3403 case ELS_CMD_ADISC:
3404 ndlp->nlp_prev_state = ndlp->nlp_state;
3405 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3406 lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
3407 return 1;
3408 case ELS_CMD_PRLI:
3409 ndlp->nlp_prev_state = ndlp->nlp_state;
3410 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3411 lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
3412 return 1;
3413 case ELS_CMD_LOGO:
3414 ndlp->nlp_prev_state = ndlp->nlp_state;
3415 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3416 lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
3417 return 1;
3420 /* No retry ELS command <elsCmd> to remote NPORT <did> */
3421 if (logerr) {
3422 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3423 "0137 No retry ELS command x%x to remote "
3424 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
3425 cmd, did, irsp->ulpStatus,
3426 irsp->un.ulpWord[4]);
3428 else {
3429 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3430 "0108 No retry ELS command x%x to remote "
3431 "NPORT x%x Retried:%d Error:x%x/%x\n",
3432 cmd, did, cmdiocb->retry, irsp->ulpStatus,
3433 irsp->un.ulpWord[4]);
3435 return 0;
3439 * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
3440 * @phba: pointer to lpfc hba data structure.
3441 * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3443 * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3444 * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3445 * checks to see whether there is a lpfc DMA buffer associated with the
3446 * response of the command IOCB. If so, it will be released before releasing
3447 * the lpfc DMA buffer associated with the IOCB itself.
3449 * Return code
3450 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3452 static int
3453 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3455 struct lpfc_dmabuf *buf_ptr;
3457 /* Free the response before processing the command. */
3458 if (!list_empty(&buf_ptr1->list)) {
3459 list_remove_head(&buf_ptr1->list, buf_ptr,
3460 struct lpfc_dmabuf,
3461 list);
3462 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3463 kfree(buf_ptr);
3465 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3466 kfree(buf_ptr1);
3467 return 0;
3471 * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
3472 * @phba: pointer to lpfc hba data structure.
3473 * @buf_ptr: pointer to the lpfc dma buffer data structure.
3475 * This routine releases the lpfc Direct Memory Access (DMA) buffer
3476 * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3477 * pool.
3479 * Return code
3480 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3482 static int
3483 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3485 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3486 kfree(buf_ptr);
3487 return 0;
3491 * lpfc_els_free_iocb - Free a command iocb and its associated resources
3492 * @phba: pointer to lpfc hba data structure.
3493 * @elsiocb: pointer to lpfc els command iocb data structure.
3495 * This routine frees a command IOCB and its associated resources. The
3496 * command IOCB data structure contains the reference to various associated
3497 * resources, these fields must be set to NULL if the associated reference
3498 * not present:
3499 * context1 - reference to ndlp
3500 * context2 - reference to cmd
3501 * context2->next - reference to rsp
3502 * context3 - reference to bpl
3504 * It first properly decrements the reference count held on ndlp for the
3505 * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3506 * set, it invokes the lpfc_els_free_data() routine to release the Direct
3507 * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3508 * adds the DMA buffer the @phba data structure for the delayed release.
3509 * If reference to the Buffer Pointer List (BPL) is present, the
3510 * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3511 * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3512 * invoked to release the IOCB data structure back to @phba IOCBQ list.
3514 * Return code
3515 * 0 - Success (currently, always return 0)
3518 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
3520 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
3521 struct lpfc_nodelist *ndlp;
3523 ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3524 if (ndlp) {
3525 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3526 lpfc_nlp_put(ndlp);
3528 /* If the ndlp is not being used by another discovery
3529 * thread, free it.
3531 if (!lpfc_nlp_not_used(ndlp)) {
3532 /* If ndlp is being used by another discovery
3533 * thread, just clear NLP_DEFER_RM
3535 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3538 else
3539 lpfc_nlp_put(ndlp);
3540 elsiocb->context1 = NULL;
3542 /* context2 = cmd, context2->next = rsp, context3 = bpl */
3543 if (elsiocb->context2) {
3544 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3545 /* Firmware could still be in progress of DMAing
3546 * payload, so don't free data buffer till after
3547 * a hbeat.
3549 elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3550 buf_ptr = elsiocb->context2;
3551 elsiocb->context2 = NULL;
3552 if (buf_ptr) {
3553 buf_ptr1 = NULL;
3554 spin_lock_irq(&phba->hbalock);
3555 if (!list_empty(&buf_ptr->list)) {
3556 list_remove_head(&buf_ptr->list,
3557 buf_ptr1, struct lpfc_dmabuf,
3558 list);
3559 INIT_LIST_HEAD(&buf_ptr1->list);
3560 list_add_tail(&buf_ptr1->list,
3561 &phba->elsbuf);
3562 phba->elsbuf_cnt++;
3564 INIT_LIST_HEAD(&buf_ptr->list);
3565 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3566 phba->elsbuf_cnt++;
3567 spin_unlock_irq(&phba->hbalock);
3569 } else {
3570 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3571 lpfc_els_free_data(phba, buf_ptr1);
3572 elsiocb->context2 = NULL;
3576 if (elsiocb->context3) {
3577 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
3578 lpfc_els_free_bpl(phba, buf_ptr);
3579 elsiocb->context3 = NULL;
3581 lpfc_sli_release_iocbq(phba, elsiocb);
3582 return 0;
3586 * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
3587 * @phba: pointer to lpfc hba data structure.
3588 * @cmdiocb: pointer to lpfc command iocb data structure.
3589 * @rspiocb: pointer to lpfc response iocb data structure.
3591 * This routine is the completion callback function to the Logout (LOGO)
3592 * Accept (ACC) Response ELS command. This routine is invoked to indicate
3593 * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3594 * release the ndlp if it has the last reference remaining (reference count
3595 * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3596 * field to NULL to inform the following lpfc_els_free_iocb() routine no
3597 * ndlp reference count needs to be decremented. Otherwise, the ndlp
3598 * reference use-count shall be decremented by the lpfc_els_free_iocb()
3599 * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3600 * IOCB data structure.
3602 static void
3603 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3604 struct lpfc_iocbq *rspiocb)
3606 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3607 struct lpfc_vport *vport = cmdiocb->vport;
3608 IOCB_t *irsp;
3610 irsp = &rspiocb->iocb;
3611 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3612 "ACC LOGO cmpl: status:x%x/x%x did:x%x",
3613 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
3614 /* ACC to LOGO completes to NPort <nlp_DID> */
3615 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3616 "0109 ACC to LOGO completes to NPort x%x "
3617 "Data: x%x x%x x%x\n",
3618 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3619 ndlp->nlp_rpi);
3621 if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3622 /* NPort Recovery mode or node is just allocated */
3623 if (!lpfc_nlp_not_used(ndlp)) {
3624 /* If the ndlp is being used by another discovery
3625 * thread, just unregister the RPI.
3627 lpfc_unreg_rpi(vport, ndlp);
3628 } else {
3629 /* Indicate the node has already released, should
3630 * not reference to it from within lpfc_els_free_iocb.
3632 cmdiocb->context1 = NULL;
3637 * The driver received a LOGO from the rport and has ACK'd it.
3638 * At this point, the driver is done so release the IOCB
3640 lpfc_els_free_iocb(phba, cmdiocb);
3644 * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
3645 * @phba: pointer to lpfc hba data structure.
3646 * @pmb: pointer to the driver internal queue element for mailbox command.
3648 * This routine is the completion callback function for unregister default
3649 * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3650 * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3651 * decrements the ndlp reference count held for this completion callback
3652 * function. After that, it invokes the lpfc_nlp_not_used() to check
3653 * whether there is only one reference left on the ndlp. If so, it will
3654 * perform one more decrement and trigger the release of the ndlp.
3656 void
3657 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3659 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3660 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3662 pmb->context1 = NULL;
3663 pmb->context2 = NULL;
3665 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3666 kfree(mp);
3667 mempool_free(pmb, phba->mbox_mem_pool);
3668 if (ndlp) {
3669 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3670 "0006 rpi%x DID:%x flg:%x %d map:%x %p\n",
3671 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3672 atomic_read(&ndlp->kref.refcount),
3673 ndlp->nlp_usg_map, ndlp);
3674 if (NLP_CHK_NODE_ACT(ndlp)) {
3675 lpfc_nlp_put(ndlp);
3676 /* This is the end of the default RPI cleanup logic for
3677 * this ndlp. If no other discovery threads are using
3678 * this ndlp, free all resources associated with it.
3680 lpfc_nlp_not_used(ndlp);
3681 } else {
3682 lpfc_drop_node(ndlp->vport, ndlp);
3686 return;
3690 * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
3691 * @phba: pointer to lpfc hba data structure.
3692 * @cmdiocb: pointer to lpfc command iocb data structure.
3693 * @rspiocb: pointer to lpfc response iocb data structure.
3695 * This routine is the completion callback function for ELS Response IOCB
3696 * command. In normal case, this callback function just properly sets the
3697 * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3698 * field in the command IOCB is not NULL, the referred mailbox command will
3699 * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3700 * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3701 * link down event occurred during the discovery, the lpfc_nlp_not_used()
3702 * routine shall be invoked trying to release the ndlp if no other threads
3703 * are currently referring it.
3705 static void
3706 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3707 struct lpfc_iocbq *rspiocb)
3709 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3710 struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3711 struct Scsi_Host *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
3712 IOCB_t *irsp;
3713 uint8_t *pcmd;
3714 LPFC_MBOXQ_t *mbox = NULL;
3715 struct lpfc_dmabuf *mp = NULL;
3716 uint32_t ls_rjt = 0;
3718 irsp = &rspiocb->iocb;
3720 if (cmdiocb->context_un.mbox)
3721 mbox = cmdiocb->context_un.mbox;
3723 /* First determine if this is a LS_RJT cmpl. Note, this callback
3724 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3726 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3727 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3728 (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3729 /* A LS_RJT associated with Default RPI cleanup has its own
3730 * separate code path.
3732 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3733 ls_rjt = 1;
3736 /* Check to see if link went down during discovery */
3737 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3738 if (mbox) {
3739 mp = (struct lpfc_dmabuf *) mbox->context1;
3740 if (mp) {
3741 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3742 kfree(mp);
3744 mempool_free(mbox, phba->mbox_mem_pool);
3746 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3747 (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3748 if (lpfc_nlp_not_used(ndlp)) {
3749 ndlp = NULL;
3750 /* Indicate the node has already released,
3751 * should not reference to it from within
3752 * the routine lpfc_els_free_iocb.
3754 cmdiocb->context1 = NULL;
3756 goto out;
3759 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3760 "ELS rsp cmpl: status:x%x/x%x did:x%x",
3761 irsp->ulpStatus, irsp->un.ulpWord[4],
3762 cmdiocb->iocb.un.elsreq64.remoteID);
3763 /* ELS response tag <ulpIoTag> completes */
3764 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3765 "0110 ELS response tag x%x completes "
3766 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3767 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3768 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3769 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3770 ndlp->nlp_rpi);
3771 if (mbox) {
3772 if ((rspiocb->iocb.ulpStatus == 0)
3773 && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3774 lpfc_unreg_rpi(vport, ndlp);
3775 /* Increment reference count to ndlp to hold the
3776 * reference to ndlp for the callback function.
3778 mbox->context2 = lpfc_nlp_get(ndlp);
3779 mbox->vport = vport;
3780 if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3781 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3782 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3784 else {
3785 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3786 ndlp->nlp_prev_state = ndlp->nlp_state;
3787 lpfc_nlp_set_state(vport, ndlp,
3788 NLP_STE_REG_LOGIN_ISSUE);
3791 ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
3792 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3793 != MBX_NOT_FINISHED)
3794 goto out;
3796 /* Decrement the ndlp reference count we
3797 * set for this failed mailbox command.
3799 lpfc_nlp_put(ndlp);
3800 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3802 /* ELS rsp: Cannot issue reg_login for <NPortid> */
3803 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3804 "0138 ELS rsp: Cannot issue reg_login for x%x "
3805 "Data: x%x x%x x%x\n",
3806 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3807 ndlp->nlp_rpi);
3809 if (lpfc_nlp_not_used(ndlp)) {
3810 ndlp = NULL;
3811 /* Indicate node has already been released,
3812 * should not reference to it from within
3813 * the routine lpfc_els_free_iocb.
3815 cmdiocb->context1 = NULL;
3817 } else {
3818 /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3819 if (!lpfc_error_lost_link(irsp) &&
3820 ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3821 if (lpfc_nlp_not_used(ndlp)) {
3822 ndlp = NULL;
3823 /* Indicate node has already been
3824 * released, should not reference
3825 * to it from within the routine
3826 * lpfc_els_free_iocb.
3828 cmdiocb->context1 = NULL;
3832 mp = (struct lpfc_dmabuf *) mbox->context1;
3833 if (mp) {
3834 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3835 kfree(mp);
3837 mempool_free(mbox, phba->mbox_mem_pool);
3839 out:
3840 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3841 spin_lock_irq(shost->host_lock);
3842 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3843 spin_unlock_irq(shost->host_lock);
3845 /* If the node is not being used by another discovery thread,
3846 * and we are sending a reject, we are done with it.
3847 * Release driver reference count here and free associated
3848 * resources.
3850 if (ls_rjt)
3851 if (lpfc_nlp_not_used(ndlp))
3852 /* Indicate node has already been released,
3853 * should not reference to it from within
3854 * the routine lpfc_els_free_iocb.
3856 cmdiocb->context1 = NULL;
3860 lpfc_els_free_iocb(phba, cmdiocb);
3861 return;
3865 * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
3866 * @vport: pointer to a host virtual N_Port data structure.
3867 * @flag: the els command code to be accepted.
3868 * @oldiocb: pointer to the original lpfc command iocb data structure.
3869 * @ndlp: pointer to a node-list data structure.
3870 * @mbox: pointer to the driver internal queue element for mailbox command.
3872 * This routine prepares and issues an Accept (ACC) response IOCB
3873 * command. It uses the @flag to properly set up the IOCB field for the
3874 * specific ACC response command to be issued and invokes the
3875 * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3876 * @mbox pointer is passed in, it will be put into the context_un.mbox
3877 * field of the IOCB for the completion callback function to issue the
3878 * mailbox command to the HBA later when callback is invoked.
3880 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3881 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3882 * will be stored into the context1 field of the IOCB for the completion
3883 * callback function to the corresponding response ELS IOCB command.
3885 * Return code
3886 * 0 - Successfully issued acc response
3887 * 1 - Failed to issue acc response
3890 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3891 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3892 LPFC_MBOXQ_t *mbox)
3894 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3895 struct lpfc_hba *phba = vport->phba;
3896 IOCB_t *icmd;
3897 IOCB_t *oldcmd;
3898 struct lpfc_iocbq *elsiocb;
3899 uint8_t *pcmd;
3900 struct serv_parm *sp;
3901 uint16_t cmdsize;
3902 int rc;
3903 ELS_PKT *els_pkt_ptr;
3905 oldcmd = &oldiocb->iocb;
3907 switch (flag) {
3908 case ELS_CMD_ACC:
3909 cmdsize = sizeof(uint32_t);
3910 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3911 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3912 if (!elsiocb) {
3913 spin_lock_irq(shost->host_lock);
3914 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3915 spin_unlock_irq(shost->host_lock);
3916 return 1;
3919 icmd = &elsiocb->iocb;
3920 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3921 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3922 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3923 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3924 pcmd += sizeof(uint32_t);
3926 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3927 "Issue ACC: did:x%x flg:x%x",
3928 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3929 break;
3930 case ELS_CMD_FLOGI:
3931 case ELS_CMD_PLOGI:
3932 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3933 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3934 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3935 if (!elsiocb)
3936 return 1;
3938 icmd = &elsiocb->iocb;
3939 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3940 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3941 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3943 if (mbox)
3944 elsiocb->context_un.mbox = mbox;
3946 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3947 pcmd += sizeof(uint32_t);
3948 sp = (struct serv_parm *)pcmd;
3950 if (flag == ELS_CMD_FLOGI) {
3951 /* Copy the received service parameters back */
3952 memcpy(sp, &phba->fc_fabparam,
3953 sizeof(struct serv_parm));
3955 /* Clear the F_Port bit */
3956 sp->cmn.fPort = 0;
3958 /* Mark all class service parameters as invalid */
3959 sp->cls1.classValid = 0;
3960 sp->cls2.classValid = 0;
3961 sp->cls3.classValid = 0;
3962 sp->cls4.classValid = 0;
3964 /* Copy our worldwide names */
3965 memcpy(&sp->portName, &vport->fc_sparam.portName,
3966 sizeof(struct lpfc_name));
3967 memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
3968 sizeof(struct lpfc_name));
3969 } else {
3970 memcpy(pcmd, &vport->fc_sparam,
3971 sizeof(struct serv_parm));
3973 sp->cmn.valid_vendor_ver_level = 0;
3974 memset(sp->vendorVersion, 0, sizeof(sp->vendorVersion));
3977 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3978 "Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
3979 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3980 break;
3981 case ELS_CMD_PRLO:
3982 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3983 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3984 ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3985 if (!elsiocb)
3986 return 1;
3988 icmd = &elsiocb->iocb;
3989 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3990 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3991 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3993 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3994 sizeof(uint32_t) + sizeof(PRLO));
3995 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3996 els_pkt_ptr = (ELS_PKT *) pcmd;
3997 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3999 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4000 "Issue ACC PRLO: did:x%x flg:x%x",
4001 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4002 break;
4003 default:
4004 return 1;
4006 /* Xmit ELS ACC response tag <ulpIoTag> */
4007 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4008 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
4009 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x "
4010 "fc_flag x%x\n",
4011 elsiocb->iotag, elsiocb->iocb.ulpContext,
4012 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4013 ndlp->nlp_rpi, vport->fc_flag);
4014 if (ndlp->nlp_flag & NLP_LOGO_ACC) {
4015 spin_lock_irq(shost->host_lock);
4016 if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4017 ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
4018 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4019 spin_unlock_irq(shost->host_lock);
4020 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4021 } else {
4022 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4025 phba->fc_stat.elsXmitACC++;
4026 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4027 if (rc == IOCB_ERROR) {
4028 lpfc_els_free_iocb(phba, elsiocb);
4029 return 1;
4031 return 0;
4035 * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
4036 * @vport: pointer to a virtual N_Port data structure.
4037 * @rejectError:
4038 * @oldiocb: pointer to the original lpfc command iocb data structure.
4039 * @ndlp: pointer to a node-list data structure.
4040 * @mbox: pointer to the driver internal queue element for mailbox command.
4042 * This routine prepares and issue an Reject (RJT) response IOCB
4043 * command. If a @mbox pointer is passed in, it will be put into the
4044 * context_un.mbox field of the IOCB for the completion callback function
4045 * to issue to the HBA later.
4047 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4048 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4049 * will be stored into the context1 field of the IOCB for the completion
4050 * callback function to the reject response ELS IOCB command.
4052 * Return code
4053 * 0 - Successfully issued reject response
4054 * 1 - Failed to issue reject response
4057 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
4058 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4059 LPFC_MBOXQ_t *mbox)
4061 struct lpfc_hba *phba = vport->phba;
4062 IOCB_t *icmd;
4063 IOCB_t *oldcmd;
4064 struct lpfc_iocbq *elsiocb;
4065 uint8_t *pcmd;
4066 uint16_t cmdsize;
4067 int rc;
4069 cmdsize = 2 * sizeof(uint32_t);
4070 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4071 ndlp->nlp_DID, ELS_CMD_LS_RJT);
4072 if (!elsiocb)
4073 return 1;
4075 icmd = &elsiocb->iocb;
4076 oldcmd = &oldiocb->iocb;
4077 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4078 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4079 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4081 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
4082 pcmd += sizeof(uint32_t);
4083 *((uint32_t *) (pcmd)) = rejectError;
4085 if (mbox)
4086 elsiocb->context_un.mbox = mbox;
4088 /* Xmit ELS RJT <err> response tag <ulpIoTag> */
4089 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4090 "0129 Xmit ELS RJT x%x response tag x%x "
4091 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4092 "rpi x%x\n",
4093 rejectError, elsiocb->iotag,
4094 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4095 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
4096 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4097 "Issue LS_RJT: did:x%x flg:x%x err:x%x",
4098 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4100 phba->fc_stat.elsXmitLSRJT++;
4101 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4102 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4104 if (rc == IOCB_ERROR) {
4105 lpfc_els_free_iocb(phba, elsiocb);
4106 return 1;
4108 return 0;
4112 * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
4113 * @vport: pointer to a virtual N_Port data structure.
4114 * @oldiocb: pointer to the original lpfc command iocb data structure.
4115 * @ndlp: pointer to a node-list data structure.
4117 * This routine prepares and issues an Accept (ACC) response to Address
4118 * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4119 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4121 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4122 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4123 * will be stored into the context1 field of the IOCB for the completion
4124 * callback function to the ADISC Accept response ELS IOCB command.
4126 * Return code
4127 * 0 - Successfully issued acc adisc response
4128 * 1 - Failed to issue adisc acc response
4131 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4132 struct lpfc_nodelist *ndlp)
4134 struct lpfc_hba *phba = vport->phba;
4135 ADISC *ap;
4136 IOCB_t *icmd, *oldcmd;
4137 struct lpfc_iocbq *elsiocb;
4138 uint8_t *pcmd;
4139 uint16_t cmdsize;
4140 int rc;
4142 cmdsize = sizeof(uint32_t) + sizeof(ADISC);
4143 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4144 ndlp->nlp_DID, ELS_CMD_ACC);
4145 if (!elsiocb)
4146 return 1;
4148 icmd = &elsiocb->iocb;
4149 oldcmd = &oldiocb->iocb;
4150 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4151 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4153 /* Xmit ADISC ACC response tag <ulpIoTag> */
4154 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4155 "0130 Xmit ADISC ACC response iotag x%x xri: "
4156 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4157 elsiocb->iotag, elsiocb->iocb.ulpContext,
4158 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4159 ndlp->nlp_rpi);
4160 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4162 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4163 pcmd += sizeof(uint32_t);
4165 ap = (ADISC *) (pcmd);
4166 ap->hardAL_PA = phba->fc_pref_ALPA;
4167 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4168 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4169 ap->DID = be32_to_cpu(vport->fc_myDID);
4171 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4172 "Issue ACC ADISC: did:x%x flg:x%x",
4173 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4175 phba->fc_stat.elsXmitACC++;
4176 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4177 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4178 if (rc == IOCB_ERROR) {
4179 lpfc_els_free_iocb(phba, elsiocb);
4180 return 1;
4182 return 0;
4186 * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
4187 * @vport: pointer to a virtual N_Port data structure.
4188 * @oldiocb: pointer to the original lpfc command iocb data structure.
4189 * @ndlp: pointer to a node-list data structure.
4191 * This routine prepares and issues an Accept (ACC) response to Process
4192 * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
4193 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4195 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4196 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4197 * will be stored into the context1 field of the IOCB for the completion
4198 * callback function to the PRLI Accept response ELS IOCB command.
4200 * Return code
4201 * 0 - Successfully issued acc prli response
4202 * 1 - Failed to issue acc prli response
4205 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4206 struct lpfc_nodelist *ndlp)
4208 struct lpfc_hba *phba = vport->phba;
4209 PRLI *npr;
4210 lpfc_vpd_t *vpd;
4211 IOCB_t *icmd;
4212 IOCB_t *oldcmd;
4213 struct lpfc_iocbq *elsiocb;
4214 uint8_t *pcmd;
4215 uint16_t cmdsize;
4216 int rc;
4218 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
4219 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4220 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
4221 if (!elsiocb)
4222 return 1;
4224 icmd = &elsiocb->iocb;
4225 oldcmd = &oldiocb->iocb;
4226 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4227 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4229 /* Xmit PRLI ACC response tag <ulpIoTag> */
4230 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4231 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
4232 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4233 elsiocb->iotag, elsiocb->iocb.ulpContext,
4234 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4235 ndlp->nlp_rpi);
4236 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4238 *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
4239 pcmd += sizeof(uint32_t);
4241 /* For PRLI, remainder of payload is PRLI parameter page */
4242 memset(pcmd, 0, sizeof(PRLI));
4244 npr = (PRLI *) pcmd;
4245 vpd = &phba->vpd;
4247 * If the remote port is a target and our firmware version is 3.20 or
4248 * later, set the following bits for FC-TAPE support.
4250 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4251 (vpd->rev.feaLevelHigh >= 0x02)) {
4252 npr->ConfmComplAllowed = 1;
4253 npr->Retry = 1;
4254 npr->TaskRetryIdReq = 1;
4257 npr->acceptRspCode = PRLI_REQ_EXECUTED;
4258 npr->estabImagePair = 1;
4259 npr->readXferRdyDis = 1;
4260 npr->ConfmComplAllowed = 1;
4262 npr->prliType = PRLI_FCP_TYPE;
4263 npr->initiatorFunc = 1;
4265 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4266 "Issue ACC PRLI: did:x%x flg:x%x",
4267 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4269 phba->fc_stat.elsXmitACC++;
4270 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4272 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4273 if (rc == IOCB_ERROR) {
4274 lpfc_els_free_iocb(phba, elsiocb);
4275 return 1;
4277 return 0;
4281 * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
4282 * @vport: pointer to a virtual N_Port data structure.
4283 * @format: rnid command format.
4284 * @oldiocb: pointer to the original lpfc command iocb data structure.
4285 * @ndlp: pointer to a node-list data structure.
4287 * This routine issues a Request Node Identification Data (RNID) Accept
4288 * (ACC) response. It constructs the RNID ACC response command according to
4289 * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4290 * issue the response. Note that this command does not need to hold the ndlp
4291 * reference count for the callback. So, the ndlp reference count taken by
4292 * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4293 * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4294 * there is no ndlp reference available.
4296 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4297 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4298 * will be stored into the context1 field of the IOCB for the completion
4299 * callback function. However, for the RNID Accept Response ELS command,
4300 * this is undone later by this routine after the IOCB is allocated.
4302 * Return code
4303 * 0 - Successfully issued acc rnid response
4304 * 1 - Failed to issue acc rnid response
4306 static int
4307 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
4308 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4310 struct lpfc_hba *phba = vport->phba;
4311 RNID *rn;
4312 IOCB_t *icmd, *oldcmd;
4313 struct lpfc_iocbq *elsiocb;
4314 uint8_t *pcmd;
4315 uint16_t cmdsize;
4316 int rc;
4318 cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4319 + (2 * sizeof(struct lpfc_name));
4320 if (format)
4321 cmdsize += sizeof(RNID_TOP_DISC);
4323 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4324 ndlp->nlp_DID, ELS_CMD_ACC);
4325 if (!elsiocb)
4326 return 1;
4328 icmd = &elsiocb->iocb;
4329 oldcmd = &oldiocb->iocb;
4330 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4331 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4333 /* Xmit RNID ACC response tag <ulpIoTag> */
4334 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4335 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
4336 elsiocb->iotag, elsiocb->iocb.ulpContext);
4337 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4338 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4339 pcmd += sizeof(uint32_t);
4341 memset(pcmd, 0, sizeof(RNID));
4342 rn = (RNID *) (pcmd);
4343 rn->Format = format;
4344 rn->CommonLen = (2 * sizeof(struct lpfc_name));
4345 memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4346 memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4347 switch (format) {
4348 case 0:
4349 rn->SpecificLen = 0;
4350 break;
4351 case RNID_TOPOLOGY_DISC:
4352 rn->SpecificLen = sizeof(RNID_TOP_DISC);
4353 memcpy(&rn->un.topologyDisc.portName,
4354 &vport->fc_portname, sizeof(struct lpfc_name));
4355 rn->un.topologyDisc.unitType = RNID_HBA;
4356 rn->un.topologyDisc.physPort = 0;
4357 rn->un.topologyDisc.attachedNodes = 0;
4358 break;
4359 default:
4360 rn->CommonLen = 0;
4361 rn->SpecificLen = 0;
4362 break;
4365 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4366 "Issue ACC RNID: did:x%x flg:x%x",
4367 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4369 phba->fc_stat.elsXmitACC++;
4370 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4372 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4373 if (rc == IOCB_ERROR) {
4374 lpfc_els_free_iocb(phba, elsiocb);
4375 return 1;
4377 return 0;
4381 * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
4382 * @vport: pointer to a virtual N_Port data structure.
4383 * @iocb: pointer to the lpfc command iocb data structure.
4384 * @ndlp: pointer to a node-list data structure.
4386 * Return
4388 static void
4389 lpfc_els_clear_rrq(struct lpfc_vport *vport,
4390 struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
4392 struct lpfc_hba *phba = vport->phba;
4393 uint8_t *pcmd;
4394 struct RRQ *rrq;
4395 uint16_t rxid;
4396 uint16_t xri;
4397 struct lpfc_node_rrq *prrq;
4400 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
4401 pcmd += sizeof(uint32_t);
4402 rrq = (struct RRQ *)pcmd;
4403 rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
4404 rxid = bf_get(rrq_rxid, rrq);
4406 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4407 "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
4408 " x%x x%x\n",
4409 be32_to_cpu(bf_get(rrq_did, rrq)),
4410 bf_get(rrq_oxid, rrq),
4411 rxid,
4412 iocb->iotag, iocb->iocb.ulpContext);
4414 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4415 "Clear RRQ: did:x%x flg:x%x exchg:x%.08x",
4416 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
4417 if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
4418 xri = bf_get(rrq_oxid, rrq);
4419 else
4420 xri = rxid;
4421 prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
4422 if (prrq)
4423 lpfc_clr_rrq_active(phba, xri, prrq);
4424 return;
4428 * lpfc_els_rsp_echo_acc - Issue echo acc response
4429 * @vport: pointer to a virtual N_Port data structure.
4430 * @data: pointer to echo data to return in the accept.
4431 * @oldiocb: pointer to the original lpfc command iocb data structure.
4432 * @ndlp: pointer to a node-list data structure.
4434 * Return code
4435 * 0 - Successfully issued acc echo response
4436 * 1 - Failed to issue acc echo response
4438 static int
4439 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
4440 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4442 struct lpfc_hba *phba = vport->phba;
4443 struct lpfc_iocbq *elsiocb;
4444 uint8_t *pcmd;
4445 uint16_t cmdsize;
4446 int rc;
4448 cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
4450 /* The accumulated length can exceed the BPL_SIZE. For
4451 * now, use this as the limit
4453 if (cmdsize > LPFC_BPL_SIZE)
4454 cmdsize = LPFC_BPL_SIZE;
4455 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4456 ndlp->nlp_DID, ELS_CMD_ACC);
4457 if (!elsiocb)
4458 return 1;
4460 elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext; /* Xri / rx_id */
4461 elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
4463 /* Xmit ECHO ACC response tag <ulpIoTag> */
4464 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4465 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
4466 elsiocb->iotag, elsiocb->iocb.ulpContext);
4467 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4468 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4469 pcmd += sizeof(uint32_t);
4470 memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
4472 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4473 "Issue ACC ECHO: did:x%x flg:x%x",
4474 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4476 phba->fc_stat.elsXmitACC++;
4477 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4479 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4480 if (rc == IOCB_ERROR) {
4481 lpfc_els_free_iocb(phba, elsiocb);
4482 return 1;
4484 return 0;
4488 * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
4489 * @vport: pointer to a host virtual N_Port data structure.
4491 * This routine issues Address Discover (ADISC) ELS commands to those
4492 * N_Ports which are in node port recovery state and ADISC has not been issued
4493 * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
4494 * lpfc_issue_els_adisc() routine, the per @vport number of discover count
4495 * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
4496 * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
4497 * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4498 * IOCBs quit for later pick up. On the other hand, after walking through
4499 * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4500 * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4501 * no more ADISC need to be sent.
4503 * Return code
4504 * The number of N_Ports with adisc issued.
4507 lpfc_els_disc_adisc(struct lpfc_vport *vport)
4509 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4510 struct lpfc_nodelist *ndlp, *next_ndlp;
4511 int sentadisc = 0;
4513 /* go thru NPR nodes and issue any remaining ELS ADISCs */
4514 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4515 if (!NLP_CHK_NODE_ACT(ndlp))
4516 continue;
4517 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4518 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4519 (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
4520 spin_lock_irq(shost->host_lock);
4521 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4522 spin_unlock_irq(shost->host_lock);
4523 ndlp->nlp_prev_state = ndlp->nlp_state;
4524 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4525 lpfc_issue_els_adisc(vport, ndlp, 0);
4526 sentadisc++;
4527 vport->num_disc_nodes++;
4528 if (vport->num_disc_nodes >=
4529 vport->cfg_discovery_threads) {
4530 spin_lock_irq(shost->host_lock);
4531 vport->fc_flag |= FC_NLP_MORE;
4532 spin_unlock_irq(shost->host_lock);
4533 break;
4537 if (sentadisc == 0) {
4538 spin_lock_irq(shost->host_lock);
4539 vport->fc_flag &= ~FC_NLP_MORE;
4540 spin_unlock_irq(shost->host_lock);
4542 return sentadisc;
4546 * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
4547 * @vport: pointer to a host virtual N_Port data structure.
4549 * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4550 * which are in node port recovery state, with a @vport. Each time an ELS
4551 * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4552 * the per @vport number of discover count (num_disc_nodes) shall be
4553 * incremented. If the num_disc_nodes reaches a pre-configured threshold
4554 * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4555 * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4556 * later pick up. On the other hand, after walking through all the ndlps with
4557 * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4558 * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4559 * PLOGI need to be sent.
4561 * Return code
4562 * The number of N_Ports with plogi issued.
4565 lpfc_els_disc_plogi(struct lpfc_vport *vport)
4567 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4568 struct lpfc_nodelist *ndlp, *next_ndlp;
4569 int sentplogi = 0;
4571 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
4572 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4573 if (!NLP_CHK_NODE_ACT(ndlp))
4574 continue;
4575 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4576 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4577 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4578 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4579 ndlp->nlp_prev_state = ndlp->nlp_state;
4580 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4581 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4582 sentplogi++;
4583 vport->num_disc_nodes++;
4584 if (vport->num_disc_nodes >=
4585 vport->cfg_discovery_threads) {
4586 spin_lock_irq(shost->host_lock);
4587 vport->fc_flag |= FC_NLP_MORE;
4588 spin_unlock_irq(shost->host_lock);
4589 break;
4593 if (sentplogi) {
4594 lpfc_set_disctmo(vport);
4596 else {
4597 spin_lock_irq(shost->host_lock);
4598 vport->fc_flag &= ~FC_NLP_MORE;
4599 spin_unlock_irq(shost->host_lock);
4601 return sentplogi;
4604 void
4605 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
4606 uint32_t word0)
4609 desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
4610 desc->payload.els_req = word0;
4611 desc->length = cpu_to_be32(sizeof(desc->payload));
4614 void
4615 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
4616 uint8_t *page_a0, uint8_t *page_a2)
4618 uint16_t wavelength;
4619 uint16_t temperature;
4620 uint16_t rx_power;
4621 uint16_t tx_bias;
4622 uint16_t tx_power;
4623 uint16_t vcc;
4624 uint16_t flag = 0;
4625 struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
4626 struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
4628 desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
4630 trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
4631 &page_a0[SSF_TRANSCEIVER_CODE_B4];
4632 trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
4633 &page_a0[SSF_TRANSCEIVER_CODE_B5];
4635 if ((trasn_code_byte4->fc_sw_laser) ||
4636 (trasn_code_byte5->fc_sw_laser_sl) ||
4637 (trasn_code_byte5->fc_sw_laser_sn)) { /* check if its short WL */
4638 flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
4639 } else if (trasn_code_byte4->fc_lw_laser) {
4640 wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
4641 page_a0[SSF_WAVELENGTH_B0];
4642 if (wavelength == SFP_WAVELENGTH_LC1310)
4643 flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
4644 if (wavelength == SFP_WAVELENGTH_LL1550)
4645 flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
4647 /* check if its SFP+ */
4648 flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
4649 SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
4650 << SFP_FLAG_CT_SHIFT;
4652 /* check if its OPTICAL */
4653 flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
4654 SFP_FLAG_IS_OPTICAL_PORT : 0)
4655 << SFP_FLAG_IS_OPTICAL_SHIFT;
4657 temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
4658 page_a2[SFF_TEMPERATURE_B0]);
4659 vcc = (page_a2[SFF_VCC_B1] << 8 |
4660 page_a2[SFF_VCC_B0]);
4661 tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
4662 page_a2[SFF_TXPOWER_B0]);
4663 tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
4664 page_a2[SFF_TX_BIAS_CURRENT_B0]);
4665 rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
4666 page_a2[SFF_RXPOWER_B0]);
4667 desc->sfp_info.temperature = cpu_to_be16(temperature);
4668 desc->sfp_info.rx_power = cpu_to_be16(rx_power);
4669 desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
4670 desc->sfp_info.tx_power = cpu_to_be16(tx_power);
4671 desc->sfp_info.vcc = cpu_to_be16(vcc);
4673 desc->sfp_info.flags = cpu_to_be16(flag);
4674 desc->length = cpu_to_be32(sizeof(desc->sfp_info));
4677 void
4678 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
4679 READ_LNK_VAR *stat)
4681 uint32_t type;
4683 desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
4685 type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
4687 desc->info.port_type = cpu_to_be32(type);
4689 desc->info.link_status.link_failure_cnt =
4690 cpu_to_be32(stat->linkFailureCnt);
4691 desc->info.link_status.loss_of_synch_cnt =
4692 cpu_to_be32(stat->lossSyncCnt);
4693 desc->info.link_status.loss_of_signal_cnt =
4694 cpu_to_be32(stat->lossSignalCnt);
4695 desc->info.link_status.primitive_seq_proto_err =
4696 cpu_to_be32(stat->primSeqErrCnt);
4697 desc->info.link_status.invalid_trans_word =
4698 cpu_to_be32(stat->invalidXmitWord);
4699 desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
4701 desc->length = cpu_to_be32(sizeof(desc->info));
4704 void
4705 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
4707 uint16_t rdp_cap = 0;
4708 uint16_t rdp_speed;
4710 desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
4712 switch (phba->fc_linkspeed) {
4713 case LPFC_LINK_SPEED_1GHZ:
4714 rdp_speed = RDP_PS_1GB;
4715 break;
4716 case LPFC_LINK_SPEED_2GHZ:
4717 rdp_speed = RDP_PS_2GB;
4718 break;
4719 case LPFC_LINK_SPEED_4GHZ:
4720 rdp_speed = RDP_PS_4GB;
4721 break;
4722 case LPFC_LINK_SPEED_8GHZ:
4723 rdp_speed = RDP_PS_8GB;
4724 break;
4725 case LPFC_LINK_SPEED_10GHZ:
4726 rdp_speed = RDP_PS_10GB;
4727 break;
4728 case LPFC_LINK_SPEED_16GHZ:
4729 rdp_speed = RDP_PS_16GB;
4730 break;
4731 default:
4732 rdp_speed = RDP_PS_UNKNOWN;
4733 break;
4736 desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
4738 if (phba->lmt & LMT_32Gb)
4739 rdp_cap |= RDP_PS_32GB;
4740 if (phba->lmt & LMT_16Gb)
4741 rdp_cap |= RDP_PS_16GB;
4742 if (phba->lmt & LMT_10Gb)
4743 rdp_cap |= RDP_PS_10GB;
4744 if (phba->lmt & LMT_8Gb)
4745 rdp_cap |= RDP_PS_8GB;
4746 if (phba->lmt & LMT_4Gb)
4747 rdp_cap |= RDP_PS_4GB;
4748 if (phba->lmt & LMT_2Gb)
4749 rdp_cap |= RDP_PS_2GB;
4750 if (phba->lmt & LMT_1Gb)
4751 rdp_cap |= RDP_PS_1GB;
4753 if (rdp_cap == 0)
4754 rdp_cap = RDP_CAP_UNKNOWN;
4756 desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
4757 desc->length = cpu_to_be32(sizeof(desc->info));
4760 void
4761 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
4762 struct lpfc_hba *phba)
4765 desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
4767 memcpy(desc->port_names.wwnn, phba->wwnn,
4768 sizeof(desc->port_names.wwnn));
4770 memcpy(desc->port_names.wwpn, &phba->wwpn,
4771 sizeof(desc->port_names.wwpn));
4773 desc->length = cpu_to_be32(sizeof(desc->port_names));
4776 void
4777 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
4778 struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4781 desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
4782 if (vport->fc_flag & FC_FABRIC) {
4783 memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
4784 sizeof(desc->port_names.wwnn));
4786 memcpy(desc->port_names.wwpn, &vport->fabric_portname,
4787 sizeof(desc->port_names.wwpn));
4788 } else { /* Point to Point */
4789 memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
4790 sizeof(desc->port_names.wwnn));
4792 memcpy(desc->port_names.wwnn, &ndlp->nlp_portname,
4793 sizeof(desc->port_names.wwpn));
4796 desc->length = cpu_to_be32(sizeof(desc->port_names));
4799 void
4800 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
4801 int status)
4803 struct lpfc_nodelist *ndlp = rdp_context->ndlp;
4804 struct lpfc_vport *vport = ndlp->vport;
4805 struct lpfc_iocbq *elsiocb;
4806 IOCB_t *icmd;
4807 uint8_t *pcmd;
4808 struct ls_rjt *stat;
4809 struct fc_rdp_res_frame *rdp_res;
4810 uint32_t cmdsize;
4811 int rc;
4813 if (status != SUCCESS)
4814 goto error;
4815 cmdsize = sizeof(struct fc_rdp_res_frame);
4817 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
4818 lpfc_max_els_tries, rdp_context->ndlp,
4819 rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
4820 lpfc_nlp_put(ndlp);
4821 if (!elsiocb)
4822 goto free_rdp_context;
4824 icmd = &elsiocb->iocb;
4825 icmd->ulpContext = rdp_context->rx_id;
4826 icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
4828 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4829 "2171 Xmit RDP response tag x%x xri x%x, "
4830 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
4831 elsiocb->iotag, elsiocb->iocb.ulpContext,
4832 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4833 ndlp->nlp_rpi);
4834 rdp_res = (struct fc_rdp_res_frame *)
4835 (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4836 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4837 memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
4838 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4840 /* For RDP payload */
4841 lpfc_rdp_res_link_service(&rdp_res->link_service_desc, ELS_CMD_RDP);
4843 lpfc_rdp_res_sfp_desc(&rdp_res->sfp_desc,
4844 rdp_context->page_a0, rdp_context->page_a2);
4845 lpfc_rdp_res_speed(&rdp_res->portspeed_desc, phba);
4846 lpfc_rdp_res_link_error(&rdp_res->link_error_desc,
4847 &rdp_context->link_stat);
4848 lpfc_rdp_res_diag_port_names(&rdp_res->diag_port_names_desc, phba);
4849 lpfc_rdp_res_attach_port_names(&rdp_res->attached_port_names_desc,
4850 vport, ndlp);
4851 rdp_res->length = cpu_to_be32(RDP_DESC_PAYLOAD_SIZE);
4853 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4855 phba->fc_stat.elsXmitACC++;
4856 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4857 if (rc == IOCB_ERROR)
4858 lpfc_els_free_iocb(phba, elsiocb);
4860 kfree(rdp_context);
4862 return;
4863 error:
4864 cmdsize = 2 * sizeof(uint32_t);
4865 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
4866 ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
4867 lpfc_nlp_put(ndlp);
4868 if (!elsiocb)
4869 goto free_rdp_context;
4871 icmd = &elsiocb->iocb;
4872 icmd->ulpContext = rdp_context->rx_id;
4873 icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
4874 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4876 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
4877 stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
4878 stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4880 phba->fc_stat.elsXmitLSRJT++;
4881 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4882 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4884 if (rc == IOCB_ERROR)
4885 lpfc_els_free_iocb(phba, elsiocb);
4886 free_rdp_context:
4887 kfree(rdp_context);
4891 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
4893 LPFC_MBOXQ_t *mbox = NULL;
4894 int rc;
4896 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4897 if (!mbox) {
4898 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
4899 "7105 failed to allocate mailbox memory");
4900 return 1;
4903 if (lpfc_sli4_dump_page_a0(phba, mbox))
4904 goto prep_mbox_fail;
4905 mbox->vport = rdp_context->ndlp->vport;
4906 mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
4907 mbox->context2 = (struct lpfc_rdp_context *) rdp_context;
4908 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4909 if (rc == MBX_NOT_FINISHED)
4910 goto issue_mbox_fail;
4912 return 0;
4914 prep_mbox_fail:
4915 issue_mbox_fail:
4916 mempool_free(mbox, phba->mbox_mem_pool);
4917 return 1;
4921 * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
4922 * @vport: pointer to a host virtual N_Port data structure.
4923 * @cmdiocb: pointer to lpfc command iocb data structure.
4924 * @ndlp: pointer to a node-list data structure.
4926 * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
4927 * IOCB. First, the payload of the unsolicited RDP is checked.
4928 * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
4929 * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
4930 * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
4931 * gather all data and send RDP response.
4933 * Return code
4934 * 0 - Sent the acc response
4935 * 1 - Sent the reject response.
4937 static int
4938 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4939 struct lpfc_nodelist *ndlp)
4941 struct lpfc_hba *phba = vport->phba;
4942 struct lpfc_dmabuf *pcmd;
4943 uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
4944 struct fc_rdp_req_frame *rdp_req;
4945 struct lpfc_rdp_context *rdp_context;
4946 IOCB_t *cmd = NULL;
4947 struct ls_rjt stat;
4949 if (phba->sli_rev < LPFC_SLI_REV4 ||
4950 (bf_get(lpfc_sli_intf_if_type,
4951 &phba->sli4_hba.sli_intf) !=
4952 LPFC_SLI_INTF_IF_TYPE_2)) {
4953 rjt_err = LSRJT_UNABLE_TPC;
4954 rjt_expl = LSEXP_REQ_UNSUPPORTED;
4955 goto error;
4958 if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
4959 rjt_err = LSRJT_UNABLE_TPC;
4960 rjt_expl = LSEXP_REQ_UNSUPPORTED;
4961 goto error;
4964 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4965 rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
4968 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4969 "2422 ELS RDP Request "
4970 "dec len %d tag x%x port_id %d len %d\n",
4971 be32_to_cpu(rdp_req->rdp_des_length),
4972 be32_to_cpu(rdp_req->nport_id_desc.tag),
4973 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
4974 be32_to_cpu(rdp_req->nport_id_desc.length));
4976 if (sizeof(struct fc_rdp_nport_desc) !=
4977 be32_to_cpu(rdp_req->rdp_des_length))
4978 goto rjt_logerr;
4979 if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
4980 goto rjt_logerr;
4981 if (RDP_NPORT_ID_SIZE !=
4982 be32_to_cpu(rdp_req->nport_id_desc.length))
4983 goto rjt_logerr;
4984 rdp_context = kmalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
4985 if (!rdp_context) {
4986 rjt_err = LSRJT_UNABLE_TPC;
4987 goto error;
4990 memset(rdp_context, 0, sizeof(struct lpfc_rdp_context));
4991 cmd = &cmdiocb->iocb;
4992 rdp_context->ndlp = lpfc_nlp_get(ndlp);
4993 rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
4994 rdp_context->rx_id = cmd->ulpContext;
4995 rdp_context->cmpl = lpfc_els_rdp_cmpl;
4996 if (lpfc_get_rdp_info(phba, rdp_context)) {
4997 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
4998 "2423 Unable to send mailbox");
4999 kfree(rdp_context);
5000 rjt_err = LSRJT_UNABLE_TPC;
5001 lpfc_nlp_put(ndlp);
5002 goto error;
5005 return 0;
5007 rjt_logerr:
5008 rjt_err = LSRJT_LOGICAL_ERR;
5010 error:
5011 memset(&stat, 0, sizeof(stat));
5012 stat.un.b.lsRjtRsnCode = rjt_err;
5013 stat.un.b.lsRjtRsnCodeExp = rjt_expl;
5014 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5015 return 1;
5019 static void
5020 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5022 MAILBOX_t *mb;
5023 IOCB_t *icmd;
5024 uint8_t *pcmd;
5025 struct lpfc_iocbq *elsiocb;
5026 struct lpfc_nodelist *ndlp;
5027 struct ls_rjt *stat;
5028 union lpfc_sli4_cfg_shdr *shdr;
5029 struct lpfc_lcb_context *lcb_context;
5030 struct fc_lcb_res_frame *lcb_res;
5031 uint32_t cmdsize, shdr_status, shdr_add_status;
5032 int rc;
5034 mb = &pmb->u.mb;
5035 lcb_context = (struct lpfc_lcb_context *)pmb->context1;
5036 ndlp = lcb_context->ndlp;
5037 pmb->context1 = NULL;
5038 pmb->context2 = NULL;
5040 shdr = (union lpfc_sli4_cfg_shdr *)
5041 &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
5042 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5043 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5045 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
5046 "0194 SET_BEACON_CONFIG mailbox "
5047 "completed with status x%x add_status x%x,"
5048 " mbx status x%x\n",
5049 shdr_status, shdr_add_status, mb->mbxStatus);
5051 if (mb->mbxStatus && !(shdr_status &&
5052 shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)) {
5053 mempool_free(pmb, phba->mbox_mem_pool);
5054 goto error;
5057 mempool_free(pmb, phba->mbox_mem_pool);
5058 cmdsize = sizeof(struct fc_lcb_res_frame);
5059 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5060 lpfc_max_els_tries, ndlp,
5061 ndlp->nlp_DID, ELS_CMD_ACC);
5063 /* Decrement the ndlp reference count from previous mbox command */
5064 lpfc_nlp_put(ndlp);
5066 if (!elsiocb)
5067 goto free_lcb_context;
5069 lcb_res = (struct fc_lcb_res_frame *)
5070 (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5072 icmd = &elsiocb->iocb;
5073 icmd->ulpContext = lcb_context->rx_id;
5074 icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
5076 pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5077 *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
5078 lcb_res->lcb_sub_command = lcb_context->sub_command;
5079 lcb_res->lcb_type = lcb_context->type;
5080 lcb_res->lcb_frequency = lcb_context->frequency;
5081 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5082 phba->fc_stat.elsXmitACC++;
5083 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5084 if (rc == IOCB_ERROR)
5085 lpfc_els_free_iocb(phba, elsiocb);
5087 kfree(lcb_context);
5088 return;
5090 error:
5091 cmdsize = sizeof(struct fc_lcb_res_frame);
5092 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5093 lpfc_max_els_tries, ndlp,
5094 ndlp->nlp_DID, ELS_CMD_LS_RJT);
5095 lpfc_nlp_put(ndlp);
5096 if (!elsiocb)
5097 goto free_lcb_context;
5099 icmd = &elsiocb->iocb;
5100 icmd->ulpContext = lcb_context->rx_id;
5101 icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
5102 pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5104 *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
5105 stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5106 stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5108 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5109 phba->fc_stat.elsXmitLSRJT++;
5110 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5111 if (rc == IOCB_ERROR)
5112 lpfc_els_free_iocb(phba, elsiocb);
5113 free_lcb_context:
5114 kfree(lcb_context);
5117 static int
5118 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
5119 struct lpfc_lcb_context *lcb_context,
5120 uint32_t beacon_state)
5122 struct lpfc_hba *phba = vport->phba;
5123 LPFC_MBOXQ_t *mbox = NULL;
5124 uint32_t len;
5125 int rc;
5127 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5128 if (!mbox)
5129 return 1;
5131 len = sizeof(struct lpfc_mbx_set_beacon_config) -
5132 sizeof(struct lpfc_sli4_cfg_mhdr);
5133 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5134 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
5135 LPFC_SLI4_MBX_EMBED);
5136 mbox->context1 = (void *)lcb_context;
5137 mbox->vport = phba->pport;
5138 mbox->mbox_cmpl = lpfc_els_lcb_rsp;
5139 bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
5140 phba->sli4_hba.physical_port);
5141 bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
5142 beacon_state);
5143 bf_set(lpfc_mbx_set_beacon_port_type, &mbox->u.mqe.un.beacon_config, 1);
5144 bf_set(lpfc_mbx_set_beacon_duration, &mbox->u.mqe.un.beacon_config, 0);
5145 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5146 if (rc == MBX_NOT_FINISHED) {
5147 mempool_free(mbox, phba->mbox_mem_pool);
5148 return 1;
5151 return 0;
5156 * lpfc_els_rcv_lcb - Process an unsolicited LCB
5157 * @vport: pointer to a host virtual N_Port data structure.
5158 * @cmdiocb: pointer to lpfc command iocb data structure.
5159 * @ndlp: pointer to a node-list data structure.
5161 * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
5162 * First, the payload of the unsolicited LCB is checked.
5163 * Then based on Subcommand beacon will either turn on or off.
5165 * Return code
5166 * 0 - Sent the acc response
5167 * 1 - Sent the reject response.
5169 static int
5170 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5171 struct lpfc_nodelist *ndlp)
5173 struct lpfc_hba *phba = vport->phba;
5174 struct lpfc_dmabuf *pcmd;
5175 uint8_t *lp;
5176 struct fc_lcb_request_frame *beacon;
5177 struct lpfc_lcb_context *lcb_context;
5178 uint8_t state, rjt_err;
5179 struct ls_rjt stat;
5181 pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
5182 lp = (uint8_t *)pcmd->virt;
5183 beacon = (struct fc_lcb_request_frame *)pcmd->virt;
5185 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5186 "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
5187 "type x%x frequency %x duration x%x\n",
5188 lp[0], lp[1], lp[2],
5189 beacon->lcb_command,
5190 beacon->lcb_sub_command,
5191 beacon->lcb_type,
5192 beacon->lcb_frequency,
5193 be16_to_cpu(beacon->lcb_duration));
5195 if (phba->sli_rev < LPFC_SLI_REV4 ||
5196 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
5197 LPFC_SLI_INTF_IF_TYPE_2)) {
5198 rjt_err = LSRJT_CMD_UNSUPPORTED;
5199 goto rjt;
5202 if (phba->hba_flag & HBA_FCOE_MODE) {
5203 rjt_err = LSRJT_CMD_UNSUPPORTED;
5204 goto rjt;
5206 if (beacon->lcb_frequency == 0) {
5207 rjt_err = LSRJT_CMD_UNSUPPORTED;
5208 goto rjt;
5210 if ((beacon->lcb_type != LPFC_LCB_GREEN) &&
5211 (beacon->lcb_type != LPFC_LCB_AMBER)) {
5212 rjt_err = LSRJT_CMD_UNSUPPORTED;
5213 goto rjt;
5215 if ((beacon->lcb_sub_command != LPFC_LCB_ON) &&
5216 (beacon->lcb_sub_command != LPFC_LCB_OFF)) {
5217 rjt_err = LSRJT_CMD_UNSUPPORTED;
5218 goto rjt;
5220 if ((beacon->lcb_sub_command == LPFC_LCB_ON) &&
5221 (beacon->lcb_type != LPFC_LCB_GREEN) &&
5222 (beacon->lcb_type != LPFC_LCB_AMBER)) {
5223 rjt_err = LSRJT_CMD_UNSUPPORTED;
5224 goto rjt;
5226 if (be16_to_cpu(beacon->lcb_duration) != 0) {
5227 rjt_err = LSRJT_CMD_UNSUPPORTED;
5228 goto rjt;
5231 lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
5232 if (!lcb_context) {
5233 rjt_err = LSRJT_UNABLE_TPC;
5234 goto rjt;
5237 state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
5238 lcb_context->sub_command = beacon->lcb_sub_command;
5239 lcb_context->type = beacon->lcb_type;
5240 lcb_context->frequency = beacon->lcb_frequency;
5241 lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
5242 lcb_context->rx_id = cmdiocb->iocb.ulpContext;
5243 lcb_context->ndlp = lpfc_nlp_get(ndlp);
5244 if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
5245 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
5246 LOG_ELS, "0193 failed to send mail box");
5247 kfree(lcb_context);
5248 lpfc_nlp_put(ndlp);
5249 rjt_err = LSRJT_UNABLE_TPC;
5250 goto rjt;
5252 return 0;
5253 rjt:
5254 memset(&stat, 0, sizeof(stat));
5255 stat.un.b.lsRjtRsnCode = rjt_err;
5256 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5257 return 1;
5262 * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
5263 * @vport: pointer to a host virtual N_Port data structure.
5265 * This routine cleans up any Registration State Change Notification
5266 * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
5267 * @vport together with the host_lock is used to prevent multiple thread
5268 * trying to access the RSCN array on a same @vport at the same time.
5270 void
5271 lpfc_els_flush_rscn(struct lpfc_vport *vport)
5273 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5274 struct lpfc_hba *phba = vport->phba;
5275 int i;
5277 spin_lock_irq(shost->host_lock);
5278 if (vport->fc_rscn_flush) {
5279 /* Another thread is walking fc_rscn_id_list on this vport */
5280 spin_unlock_irq(shost->host_lock);
5281 return;
5283 /* Indicate we are walking lpfc_els_flush_rscn on this vport */
5284 vport->fc_rscn_flush = 1;
5285 spin_unlock_irq(shost->host_lock);
5287 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
5288 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
5289 vport->fc_rscn_id_list[i] = NULL;
5291 spin_lock_irq(shost->host_lock);
5292 vport->fc_rscn_id_cnt = 0;
5293 vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
5294 spin_unlock_irq(shost->host_lock);
5295 lpfc_can_disctmo(vport);
5296 /* Indicate we are done walking this fc_rscn_id_list */
5297 vport->fc_rscn_flush = 0;
5301 * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
5302 * @vport: pointer to a host virtual N_Port data structure.
5303 * @did: remote destination port identifier.
5305 * This routine checks whether there is any pending Registration State
5306 * Configuration Notification (RSCN) to a @did on @vport.
5308 * Return code
5309 * None zero - The @did matched with a pending rscn
5310 * 0 - not able to match @did with a pending rscn
5313 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
5315 D_ID ns_did;
5316 D_ID rscn_did;
5317 uint32_t *lp;
5318 uint32_t payload_len, i;
5319 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5321 ns_did.un.word = did;
5323 /* Never match fabric nodes for RSCNs */
5324 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5325 return 0;
5327 /* If we are doing a FULL RSCN rediscovery, match everything */
5328 if (vport->fc_flag & FC_RSCN_DISCOVERY)
5329 return did;
5331 spin_lock_irq(shost->host_lock);
5332 if (vport->fc_rscn_flush) {
5333 /* Another thread is walking fc_rscn_id_list on this vport */
5334 spin_unlock_irq(shost->host_lock);
5335 return 0;
5337 /* Indicate we are walking fc_rscn_id_list on this vport */
5338 vport->fc_rscn_flush = 1;
5339 spin_unlock_irq(shost->host_lock);
5340 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
5341 lp = vport->fc_rscn_id_list[i]->virt;
5342 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
5343 payload_len -= sizeof(uint32_t); /* take off word 0 */
5344 while (payload_len) {
5345 rscn_did.un.word = be32_to_cpu(*lp++);
5346 payload_len -= sizeof(uint32_t);
5347 switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
5348 case RSCN_ADDRESS_FORMAT_PORT:
5349 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
5350 && (ns_did.un.b.area == rscn_did.un.b.area)
5351 && (ns_did.un.b.id == rscn_did.un.b.id))
5352 goto return_did_out;
5353 break;
5354 case RSCN_ADDRESS_FORMAT_AREA:
5355 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
5356 && (ns_did.un.b.area == rscn_did.un.b.area))
5357 goto return_did_out;
5358 break;
5359 case RSCN_ADDRESS_FORMAT_DOMAIN:
5360 if (ns_did.un.b.domain == rscn_did.un.b.domain)
5361 goto return_did_out;
5362 break;
5363 case RSCN_ADDRESS_FORMAT_FABRIC:
5364 goto return_did_out;
5368 /* Indicate we are done with walking fc_rscn_id_list on this vport */
5369 vport->fc_rscn_flush = 0;
5370 return 0;
5371 return_did_out:
5372 /* Indicate we are done with walking fc_rscn_id_list on this vport */
5373 vport->fc_rscn_flush = 0;
5374 return did;
5378 * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
5379 * @vport: pointer to a host virtual N_Port data structure.
5381 * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
5382 * state machine for a @vport's nodes that are with pending RSCN (Registration
5383 * State Change Notification).
5385 * Return code
5386 * 0 - Successful (currently alway return 0)
5388 static int
5389 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
5391 struct lpfc_nodelist *ndlp = NULL;
5393 /* Move all affected nodes by pending RSCNs to NPR state. */
5394 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5395 if (!NLP_CHK_NODE_ACT(ndlp) ||
5396 (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
5397 !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
5398 continue;
5399 lpfc_disc_state_machine(vport, ndlp, NULL,
5400 NLP_EVT_DEVICE_RECOVERY);
5401 lpfc_cancel_retry_delay_tmo(vport, ndlp);
5403 return 0;
5407 * lpfc_send_rscn_event - Send an RSCN event to management application
5408 * @vport: pointer to a host virtual N_Port data structure.
5409 * @cmdiocb: pointer to lpfc command iocb data structure.
5411 * lpfc_send_rscn_event sends an RSCN netlink event to management
5412 * applications.
5414 static void
5415 lpfc_send_rscn_event(struct lpfc_vport *vport,
5416 struct lpfc_iocbq *cmdiocb)
5418 struct lpfc_dmabuf *pcmd;
5419 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5420 uint32_t *payload_ptr;
5421 uint32_t payload_len;
5422 struct lpfc_rscn_event_header *rscn_event_data;
5424 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5425 payload_ptr = (uint32_t *) pcmd->virt;
5426 payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
5428 rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
5429 payload_len, GFP_KERNEL);
5430 if (!rscn_event_data) {
5431 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5432 "0147 Failed to allocate memory for RSCN event\n");
5433 return;
5435 rscn_event_data->event_type = FC_REG_RSCN_EVENT;
5436 rscn_event_data->payload_length = payload_len;
5437 memcpy(rscn_event_data->rscn_payload, payload_ptr,
5438 payload_len);
5440 fc_host_post_vendor_event(shost,
5441 fc_get_event_number(),
5442 sizeof(struct lpfc_rscn_event_header) + payload_len,
5443 (char *)rscn_event_data,
5444 LPFC_NL_VENDOR_ID);
5446 kfree(rscn_event_data);
5450 * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
5451 * @vport: pointer to a host virtual N_Port data structure.
5452 * @cmdiocb: pointer to lpfc command iocb data structure.
5453 * @ndlp: pointer to a node-list data structure.
5455 * This routine processes an unsolicited RSCN (Registration State Change
5456 * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
5457 * to invoke fc_host_post_event() routine to the FC transport layer. If the
5458 * discover state machine is about to begin discovery, it just accepts the
5459 * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
5460 * contains N_Port IDs for other vports on this HBA, it just accepts the
5461 * RSCN and ignore processing it. If the state machine is in the recovery
5462 * state, the fc_rscn_id_list of this @vport is walked and the
5463 * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
5464 * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
5465 * routine is invoked to handle the RSCN event.
5467 * Return code
5468 * 0 - Just sent the acc response
5469 * 1 - Sent the acc response and waited for name server completion
5471 static int
5472 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5473 struct lpfc_nodelist *ndlp)
5475 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5476 struct lpfc_hba *phba = vport->phba;
5477 struct lpfc_dmabuf *pcmd;
5478 uint32_t *lp, *datap;
5479 uint32_t payload_len, length, nportid, *cmd;
5480 int rscn_cnt;
5481 int rscn_id = 0, hba_id = 0;
5482 int i;
5484 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5485 lp = (uint32_t *) pcmd->virt;
5487 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
5488 payload_len -= sizeof(uint32_t); /* take off word 0 */
5489 /* RSCN received */
5490 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5491 "0214 RSCN received Data: x%x x%x x%x x%x\n",
5492 vport->fc_flag, payload_len, *lp,
5493 vport->fc_rscn_id_cnt);
5495 /* Send an RSCN event to the management application */
5496 lpfc_send_rscn_event(vport, cmdiocb);
5498 for (i = 0; i < payload_len/sizeof(uint32_t); i++)
5499 fc_host_post_event(shost, fc_get_event_number(),
5500 FCH_EVT_RSCN, lp[i]);
5502 /* If we are about to begin discovery, just ACC the RSCN.
5503 * Discovery processing will satisfy it.
5505 if (vport->port_state <= LPFC_NS_QRY) {
5506 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5507 "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
5508 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
5510 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5511 return 0;
5514 /* If this RSCN just contains NPortIDs for other vports on this HBA,
5515 * just ACC and ignore it.
5517 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5518 !(vport->cfg_peer_port_login)) {
5519 i = payload_len;
5520 datap = lp;
5521 while (i > 0) {
5522 nportid = *datap++;
5523 nportid = ((be32_to_cpu(nportid)) & Mask_DID);
5524 i -= sizeof(uint32_t);
5525 rscn_id++;
5526 if (lpfc_find_vport_by_did(phba, nportid))
5527 hba_id++;
5529 if (rscn_id == hba_id) {
5530 /* ALL NPortIDs in RSCN are on HBA */
5531 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5532 "0219 Ignore RSCN "
5533 "Data: x%x x%x x%x x%x\n",
5534 vport->fc_flag, payload_len,
5535 *lp, vport->fc_rscn_id_cnt);
5536 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5537 "RCV RSCN vport: did:x%x/ste:x%x flg:x%x",
5538 ndlp->nlp_DID, vport->port_state,
5539 ndlp->nlp_flag);
5541 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
5542 ndlp, NULL);
5543 return 0;
5547 spin_lock_irq(shost->host_lock);
5548 if (vport->fc_rscn_flush) {
5549 /* Another thread is walking fc_rscn_id_list on this vport */
5550 vport->fc_flag |= FC_RSCN_DISCOVERY;
5551 spin_unlock_irq(shost->host_lock);
5552 /* Send back ACC */
5553 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5554 return 0;
5556 /* Indicate we are walking fc_rscn_id_list on this vport */
5557 vport->fc_rscn_flush = 1;
5558 spin_unlock_irq(shost->host_lock);
5559 /* Get the array count after successfully have the token */
5560 rscn_cnt = vport->fc_rscn_id_cnt;
5561 /* If we are already processing an RSCN, save the received
5562 * RSCN payload buffer, cmdiocb->context2 to process later.
5564 if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
5565 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5566 "RCV RSCN defer: did:x%x/ste:x%x flg:x%x",
5567 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
5569 spin_lock_irq(shost->host_lock);
5570 vport->fc_flag |= FC_RSCN_DEFERRED;
5571 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
5572 !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
5573 vport->fc_flag |= FC_RSCN_MODE;
5574 spin_unlock_irq(shost->host_lock);
5575 if (rscn_cnt) {
5576 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
5577 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
5579 if ((rscn_cnt) &&
5580 (payload_len + length <= LPFC_BPL_SIZE)) {
5581 *cmd &= ELS_CMD_MASK;
5582 *cmd |= cpu_to_be32(payload_len + length);
5583 memcpy(((uint8_t *)cmd) + length, lp,
5584 payload_len);
5585 } else {
5586 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
5587 vport->fc_rscn_id_cnt++;
5588 /* If we zero, cmdiocb->context2, the calling
5589 * routine will not try to free it.
5591 cmdiocb->context2 = NULL;
5593 /* Deferred RSCN */
5594 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5595 "0235 Deferred RSCN "
5596 "Data: x%x x%x x%x\n",
5597 vport->fc_rscn_id_cnt, vport->fc_flag,
5598 vport->port_state);
5599 } else {
5600 vport->fc_flag |= FC_RSCN_DISCOVERY;
5601 spin_unlock_irq(shost->host_lock);
5602 /* ReDiscovery RSCN */
5603 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5604 "0234 ReDiscovery RSCN "
5605 "Data: x%x x%x x%x\n",
5606 vport->fc_rscn_id_cnt, vport->fc_flag,
5607 vport->port_state);
5609 /* Indicate we are done walking fc_rscn_id_list on this vport */
5610 vport->fc_rscn_flush = 0;
5611 /* Send back ACC */
5612 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5613 /* send RECOVERY event for ALL nodes that match RSCN payload */
5614 lpfc_rscn_recovery_check(vport);
5615 spin_lock_irq(shost->host_lock);
5616 vport->fc_flag &= ~FC_RSCN_DEFERRED;
5617 spin_unlock_irq(shost->host_lock);
5618 return 0;
5620 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5621 "RCV RSCN: did:x%x/ste:x%x flg:x%x",
5622 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
5624 spin_lock_irq(shost->host_lock);
5625 vport->fc_flag |= FC_RSCN_MODE;
5626 spin_unlock_irq(shost->host_lock);
5627 vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
5628 /* Indicate we are done walking fc_rscn_id_list on this vport */
5629 vport->fc_rscn_flush = 0;
5631 * If we zero, cmdiocb->context2, the calling routine will
5632 * not try to free it.
5634 cmdiocb->context2 = NULL;
5635 lpfc_set_disctmo(vport);
5636 /* Send back ACC */
5637 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5638 /* send RECOVERY event for ALL nodes that match RSCN payload */
5639 lpfc_rscn_recovery_check(vport);
5640 return lpfc_els_handle_rscn(vport);
5644 * lpfc_els_handle_rscn - Handle rscn for a vport
5645 * @vport: pointer to a host virtual N_Port data structure.
5647 * This routine handles the Registration State Configuration Notification
5648 * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
5649 * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
5650 * if the ndlp to NameServer exists, a Common Transport (CT) command to the
5651 * NameServer shall be issued. If CT command to the NameServer fails to be
5652 * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
5653 * RSCN activities with the @vport.
5655 * Return code
5656 * 0 - Cleaned up rscn on the @vport
5657 * 1 - Wait for plogi to name server before proceed
5660 lpfc_els_handle_rscn(struct lpfc_vport *vport)
5662 struct lpfc_nodelist *ndlp;
5663 struct lpfc_hba *phba = vport->phba;
5665 /* Ignore RSCN if the port is being torn down. */
5666 if (vport->load_flag & FC_UNLOADING) {
5667 lpfc_els_flush_rscn(vport);
5668 return 0;
5671 /* Start timer for RSCN processing */
5672 lpfc_set_disctmo(vport);
5674 /* RSCN processed */
5675 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5676 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
5677 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
5678 vport->port_state);
5680 /* To process RSCN, first compare RSCN data with NameServer */
5681 vport->fc_ns_retry = 0;
5682 vport->num_disc_nodes = 0;
5684 ndlp = lpfc_findnode_did(vport, NameServer_DID);
5685 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
5686 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
5687 /* Good ndlp, issue CT Request to NameServer */
5688 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
5689 /* Wait for NameServer query cmpl before we can
5690 continue */
5691 return 1;
5692 } else {
5693 /* If login to NameServer does not exist, issue one */
5694 /* Good status, issue PLOGI to NameServer */
5695 ndlp = lpfc_findnode_did(vport, NameServer_DID);
5696 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5697 /* Wait for NameServer login cmpl before we can
5698 continue */
5699 return 1;
5701 if (ndlp) {
5702 ndlp = lpfc_enable_node(vport, ndlp,
5703 NLP_STE_PLOGI_ISSUE);
5704 if (!ndlp) {
5705 lpfc_els_flush_rscn(vport);
5706 return 0;
5708 ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
5709 } else {
5710 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5711 if (!ndlp) {
5712 lpfc_els_flush_rscn(vport);
5713 return 0;
5715 lpfc_nlp_init(vport, ndlp, NameServer_DID);
5716 ndlp->nlp_prev_state = ndlp->nlp_state;
5717 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5719 ndlp->nlp_type |= NLP_FABRIC;
5720 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
5721 /* Wait for NameServer login cmpl before we can
5722 * continue
5724 return 1;
5727 lpfc_els_flush_rscn(vport);
5728 return 0;
5732 * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
5733 * @vport: pointer to a host virtual N_Port data structure.
5734 * @cmdiocb: pointer to lpfc command iocb data structure.
5735 * @ndlp: pointer to a node-list data structure.
5737 * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
5738 * unsolicited event. An unsolicited FLOGI can be received in a point-to-
5739 * point topology. As an unsolicited FLOGI should not be received in a loop
5740 * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
5741 * lpfc_check_sparm() routine is invoked to check the parameters in the
5742 * unsolicited FLOGI. If parameters validation failed, the routine
5743 * lpfc_els_rsp_reject() shall be called with reject reason code set to
5744 * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
5745 * FLOGI shall be compared with the Port WWN of the @vport to determine who
5746 * will initiate PLOGI. The higher lexicographical value party shall has
5747 * higher priority (as the winning port) and will initiate PLOGI and
5748 * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
5749 * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
5750 * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
5752 * Return code
5753 * 0 - Successfully processed the unsolicited flogi
5754 * 1 - Failed to process the unsolicited flogi
5756 static int
5757 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5758 struct lpfc_nodelist *ndlp)
5760 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5761 struct lpfc_hba *phba = vport->phba;
5762 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5763 uint32_t *lp = (uint32_t *) pcmd->virt;
5764 IOCB_t *icmd = &cmdiocb->iocb;
5765 struct serv_parm *sp;
5766 LPFC_MBOXQ_t *mbox;
5767 uint32_t cmd, did;
5768 int rc;
5769 uint32_t fc_flag = 0;
5770 uint32_t port_state = 0;
5772 cmd = *lp++;
5773 sp = (struct serv_parm *) lp;
5775 /* FLOGI received */
5777 lpfc_set_disctmo(vport);
5779 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
5780 /* We should never receive a FLOGI in loop mode, ignore it */
5781 did = icmd->un.elsreq64.remoteID;
5783 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
5784 Loop Mode */
5785 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5786 "0113 An FLOGI ELS command x%x was "
5787 "received from DID x%x in Loop Mode\n",
5788 cmd, did);
5789 return 1;
5792 (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
5796 * If our portname is greater than the remote portname,
5797 * then we initiate Nport login.
5800 rc = memcmp(&vport->fc_portname, &sp->portName,
5801 sizeof(struct lpfc_name));
5803 if (!rc) {
5804 if (phba->sli_rev < LPFC_SLI_REV4) {
5805 mbox = mempool_alloc(phba->mbox_mem_pool,
5806 GFP_KERNEL);
5807 if (!mbox)
5808 return 1;
5809 lpfc_linkdown(phba);
5810 lpfc_init_link(phba, mbox,
5811 phba->cfg_topology,
5812 phba->cfg_link_speed);
5813 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5814 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5815 mbox->vport = vport;
5816 rc = lpfc_sli_issue_mbox(phba, mbox,
5817 MBX_NOWAIT);
5818 lpfc_set_loopback_flag(phba);
5819 if (rc == MBX_NOT_FINISHED)
5820 mempool_free(mbox, phba->mbox_mem_pool);
5821 return 1;
5824 /* abort the flogi coming back to ourselves
5825 * due to external loopback on the port.
5827 lpfc_els_abort_flogi(phba);
5828 return 0;
5830 } else if (rc > 0) { /* greater than */
5831 spin_lock_irq(shost->host_lock);
5832 vport->fc_flag |= FC_PT2PT_PLOGI;
5833 spin_unlock_irq(shost->host_lock);
5835 /* If we have the high WWPN we can assign our own
5836 * myDID; otherwise, we have to WAIT for a PLOGI
5837 * from the remote NPort to find out what it
5838 * will be.
5840 vport->fc_myDID = PT2PT_LocalID;
5841 } else {
5842 vport->fc_myDID = PT2PT_RemoteID;
5846 * The vport state should go to LPFC_FLOGI only
5847 * AFTER we issue a FLOGI, not receive one.
5849 spin_lock_irq(shost->host_lock);
5850 fc_flag = vport->fc_flag;
5851 port_state = vport->port_state;
5852 vport->fc_flag |= FC_PT2PT;
5853 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
5854 spin_unlock_irq(shost->host_lock);
5855 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5856 "3311 Rcv Flogi PS x%x new PS x%x "
5857 "fc_flag x%x new fc_flag x%x\n",
5858 port_state, vport->port_state,
5859 fc_flag, vport->fc_flag);
5862 * We temporarily set fc_myDID to make it look like we are
5863 * a Fabric. This is done just so we end up with the right
5864 * did / sid on the FLOGI ACC rsp.
5866 did = vport->fc_myDID;
5867 vport->fc_myDID = Fabric_DID;
5869 memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
5871 /* Send back ACC */
5872 lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
5874 /* Now lets put fc_myDID back to what its supposed to be */
5875 vport->fc_myDID = did;
5877 return 0;
5881 * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
5882 * @vport: pointer to a host virtual N_Port data structure.
5883 * @cmdiocb: pointer to lpfc command iocb data structure.
5884 * @ndlp: pointer to a node-list data structure.
5886 * This routine processes Request Node Identification Data (RNID) IOCB
5887 * received as an ELS unsolicited event. Only when the RNID specified format
5888 * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
5889 * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
5890 * Accept (ACC) the RNID ELS command. All the other RNID formats are
5891 * rejected by invoking the lpfc_els_rsp_reject() routine.
5893 * Return code
5894 * 0 - Successfully processed rnid iocb (currently always return 0)
5896 static int
5897 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5898 struct lpfc_nodelist *ndlp)
5900 struct lpfc_dmabuf *pcmd;
5901 uint32_t *lp;
5902 RNID *rn;
5903 struct ls_rjt stat;
5904 uint32_t cmd;
5906 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5907 lp = (uint32_t *) pcmd->virt;
5909 cmd = *lp++;
5910 rn = (RNID *) lp;
5912 /* RNID received */
5914 switch (rn->Format) {
5915 case 0:
5916 case RNID_TOPOLOGY_DISC:
5917 /* Send back ACC */
5918 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
5919 break;
5920 default:
5921 /* Reject this request because format not supported */
5922 stat.un.b.lsRjtRsvd0 = 0;
5923 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5924 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5925 stat.un.b.vendorUnique = 0;
5926 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5927 NULL);
5929 return 0;
5933 * lpfc_els_rcv_echo - Process an unsolicited echo iocb
5934 * @vport: pointer to a host virtual N_Port data structure.
5935 * @cmdiocb: pointer to lpfc command iocb data structure.
5936 * @ndlp: pointer to a node-list data structure.
5938 * Return code
5939 * 0 - Successfully processed echo iocb (currently always return 0)
5941 static int
5942 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5943 struct lpfc_nodelist *ndlp)
5945 uint8_t *pcmd;
5947 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
5949 /* skip over first word of echo command to find echo data */
5950 pcmd += sizeof(uint32_t);
5952 lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
5953 return 0;
5957 * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
5958 * @vport: pointer to a host virtual N_Port data structure.
5959 * @cmdiocb: pointer to lpfc command iocb data structure.
5960 * @ndlp: pointer to a node-list data structure.
5962 * This routine processes a Link Incident Report Registration(LIRR) IOCB
5963 * received as an ELS unsolicited event. Currently, this function just invokes
5964 * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
5966 * Return code
5967 * 0 - Successfully processed lirr iocb (currently always return 0)
5969 static int
5970 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5971 struct lpfc_nodelist *ndlp)
5973 struct ls_rjt stat;
5975 /* For now, unconditionally reject this command */
5976 stat.un.b.lsRjtRsvd0 = 0;
5977 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5978 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5979 stat.un.b.vendorUnique = 0;
5980 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5981 return 0;
5985 * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
5986 * @vport: pointer to a host virtual N_Port data structure.
5987 * @cmdiocb: pointer to lpfc command iocb data structure.
5988 * @ndlp: pointer to a node-list data structure.
5990 * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
5991 * received as an ELS unsolicited event. A request to RRQ shall only
5992 * be accepted if the Originator Nx_Port N_Port_ID or the Responder
5993 * Nx_Port N_Port_ID of the target Exchange is the same as the
5994 * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
5995 * not accepted, an LS_RJT with reason code "Unable to perform
5996 * command request" and reason code explanation "Invalid Originator
5997 * S_ID" shall be returned. For now, we just unconditionally accept
5998 * RRQ from the target.
6000 static void
6001 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6002 struct lpfc_nodelist *ndlp)
6004 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6005 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6006 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
6010 * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6011 * @phba: pointer to lpfc hba data structure.
6012 * @pmb: pointer to the driver internal queue element for mailbox command.
6014 * This routine is the completion callback function for the MBX_READ_LNK_STAT
6015 * mailbox command. This callback function is to actually send the Accept
6016 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6017 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6018 * mailbox command, constructs the RPS response with the link statistics
6019 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6020 * response to the RPS.
6022 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6023 * will be incremented by 1 for holding the ndlp and the reference to ndlp
6024 * will be stored into the context1 field of the IOCB for the completion
6025 * callback function to the RPS Accept Response ELS IOCB command.
6028 static void
6029 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6031 MAILBOX_t *mb;
6032 IOCB_t *icmd;
6033 struct RLS_RSP *rls_rsp;
6034 uint8_t *pcmd;
6035 struct lpfc_iocbq *elsiocb;
6036 struct lpfc_nodelist *ndlp;
6037 uint16_t oxid;
6038 uint16_t rxid;
6039 uint32_t cmdsize;
6041 mb = &pmb->u.mb;
6043 ndlp = (struct lpfc_nodelist *) pmb->context2;
6044 rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
6045 oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
6046 pmb->context1 = NULL;
6047 pmb->context2 = NULL;
6049 if (mb->mbxStatus) {
6050 mempool_free(pmb, phba->mbox_mem_pool);
6051 return;
6054 cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
6055 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6056 lpfc_max_els_tries, ndlp,
6057 ndlp->nlp_DID, ELS_CMD_ACC);
6059 /* Decrement the ndlp reference count from previous mbox command */
6060 lpfc_nlp_put(ndlp);
6062 if (!elsiocb) {
6063 mempool_free(pmb, phba->mbox_mem_pool);
6064 return;
6067 icmd = &elsiocb->iocb;
6068 icmd->ulpContext = rxid;
6069 icmd->unsli3.rcvsli3.ox_id = oxid;
6071 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6072 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6073 pcmd += sizeof(uint32_t); /* Skip past command */
6074 rls_rsp = (struct RLS_RSP *)pcmd;
6076 rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6077 rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6078 rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6079 rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6080 rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6081 rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
6082 mempool_free(pmb, phba->mbox_mem_pool);
6083 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6084 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6085 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
6086 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6087 elsiocb->iotag, elsiocb->iocb.ulpContext,
6088 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6089 ndlp->nlp_rpi);
6090 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6091 phba->fc_stat.elsXmitACC++;
6092 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6093 lpfc_els_free_iocb(phba, elsiocb);
6097 * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6098 * @phba: pointer to lpfc hba data structure.
6099 * @pmb: pointer to the driver internal queue element for mailbox command.
6101 * This routine is the completion callback function for the MBX_READ_LNK_STAT
6102 * mailbox command. This callback function is to actually send the Accept
6103 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6104 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6105 * mailbox command, constructs the RPS response with the link statistics
6106 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6107 * response to the RPS.
6109 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6110 * will be incremented by 1 for holding the ndlp and the reference to ndlp
6111 * will be stored into the context1 field of the IOCB for the completion
6112 * callback function to the RPS Accept Response ELS IOCB command.
6115 static void
6116 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6118 MAILBOX_t *mb;
6119 IOCB_t *icmd;
6120 RPS_RSP *rps_rsp;
6121 uint8_t *pcmd;
6122 struct lpfc_iocbq *elsiocb;
6123 struct lpfc_nodelist *ndlp;
6124 uint16_t status;
6125 uint16_t oxid;
6126 uint16_t rxid;
6127 uint32_t cmdsize;
6129 mb = &pmb->u.mb;
6131 ndlp = (struct lpfc_nodelist *) pmb->context2;
6132 rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
6133 oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
6134 pmb->context1 = NULL;
6135 pmb->context2 = NULL;
6137 if (mb->mbxStatus) {
6138 mempool_free(pmb, phba->mbox_mem_pool);
6139 return;
6142 cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
6143 mempool_free(pmb, phba->mbox_mem_pool);
6144 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6145 lpfc_max_els_tries, ndlp,
6146 ndlp->nlp_DID, ELS_CMD_ACC);
6148 /* Decrement the ndlp reference count from previous mbox command */
6149 lpfc_nlp_put(ndlp);
6151 if (!elsiocb)
6152 return;
6154 icmd = &elsiocb->iocb;
6155 icmd->ulpContext = rxid;
6156 icmd->unsli3.rcvsli3.ox_id = oxid;
6158 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6159 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6160 pcmd += sizeof(uint32_t); /* Skip past command */
6161 rps_rsp = (RPS_RSP *)pcmd;
6163 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
6164 status = 0x10;
6165 else
6166 status = 0x8;
6167 if (phba->pport->fc_flag & FC_FABRIC)
6168 status |= 0x4;
6170 rps_rsp->rsvd1 = 0;
6171 rps_rsp->portStatus = cpu_to_be16(status);
6172 rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6173 rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6174 rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6175 rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6176 rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6177 rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
6178 /* Xmit ELS RPS ACC response tag <ulpIoTag> */
6179 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6180 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
6181 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6182 elsiocb->iotag, elsiocb->iocb.ulpContext,
6183 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6184 ndlp->nlp_rpi);
6185 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6186 phba->fc_stat.elsXmitACC++;
6187 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6188 lpfc_els_free_iocb(phba, elsiocb);
6189 return;
6193 * lpfc_els_rcv_rls - Process an unsolicited rls iocb
6194 * @vport: pointer to a host virtual N_Port data structure.
6195 * @cmdiocb: pointer to lpfc command iocb data structure.
6196 * @ndlp: pointer to a node-list data structure.
6198 * This routine processes Read Port Status (RPL) IOCB received as an
6199 * ELS unsolicited event. It first checks the remote port state. If the
6200 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6201 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6202 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
6203 * for reading the HBA link statistics. It is for the callback function,
6204 * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
6205 * to actually sending out RPL Accept (ACC) response.
6207 * Return codes
6208 * 0 - Successfully processed rls iocb (currently always return 0)
6210 static int
6211 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6212 struct lpfc_nodelist *ndlp)
6214 struct lpfc_hba *phba = vport->phba;
6215 LPFC_MBOXQ_t *mbox;
6216 struct ls_rjt stat;
6218 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6219 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6220 /* reject the unsolicited RPS request and done with it */
6221 goto reject_out;
6223 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
6224 if (mbox) {
6225 lpfc_read_lnk_stat(phba, mbox);
6226 mbox->context1 = (void *)((unsigned long)
6227 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
6228 cmdiocb->iocb.ulpContext)); /* rx_id */
6229 mbox->context2 = lpfc_nlp_get(ndlp);
6230 mbox->vport = vport;
6231 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
6232 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6233 != MBX_NOT_FINISHED)
6234 /* Mbox completion will send ELS Response */
6235 return 0;
6236 /* Decrement reference count used for the failed mbox
6237 * command.
6239 lpfc_nlp_put(ndlp);
6240 mempool_free(mbox, phba->mbox_mem_pool);
6242 reject_out:
6243 /* issue rejection response */
6244 stat.un.b.lsRjtRsvd0 = 0;
6245 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6246 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6247 stat.un.b.vendorUnique = 0;
6248 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6249 return 0;
6253 * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
6254 * @vport: pointer to a host virtual N_Port data structure.
6255 * @cmdiocb: pointer to lpfc command iocb data structure.
6256 * @ndlp: pointer to a node-list data structure.
6258 * This routine processes Read Timout Value (RTV) IOCB received as an
6259 * ELS unsolicited event. It first checks the remote port state. If the
6260 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6261 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6262 * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
6263 * Value (RTV) unsolicited IOCB event.
6265 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6266 * will be incremented by 1 for holding the ndlp and the reference to ndlp
6267 * will be stored into the context1 field of the IOCB for the completion
6268 * callback function to the RPS Accept Response ELS IOCB command.
6270 * Return codes
6271 * 0 - Successfully processed rtv iocb (currently always return 0)
6273 static int
6274 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6275 struct lpfc_nodelist *ndlp)
6277 struct lpfc_hba *phba = vport->phba;
6278 struct ls_rjt stat;
6279 struct RTV_RSP *rtv_rsp;
6280 uint8_t *pcmd;
6281 struct lpfc_iocbq *elsiocb;
6282 uint32_t cmdsize;
6285 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6286 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6287 /* reject the unsolicited RPS request and done with it */
6288 goto reject_out;
6290 cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
6291 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6292 lpfc_max_els_tries, ndlp,
6293 ndlp->nlp_DID, ELS_CMD_ACC);
6295 if (!elsiocb)
6296 return 1;
6298 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6299 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6300 pcmd += sizeof(uint32_t); /* Skip past command */
6302 /* use the command's xri in the response */
6303 elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext; /* Xri / rx_id */
6304 elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6306 rtv_rsp = (struct RTV_RSP *)pcmd;
6308 /* populate RTV payload */
6309 rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
6310 rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
6311 bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
6312 bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
6313 rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
6315 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6316 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6317 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
6318 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
6319 "Data: x%x x%x x%x\n",
6320 elsiocb->iotag, elsiocb->iocb.ulpContext,
6321 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6322 ndlp->nlp_rpi,
6323 rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
6324 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6325 phba->fc_stat.elsXmitACC++;
6326 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6327 lpfc_els_free_iocb(phba, elsiocb);
6328 return 0;
6330 reject_out:
6331 /* issue rejection response */
6332 stat.un.b.lsRjtRsvd0 = 0;
6333 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6334 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6335 stat.un.b.vendorUnique = 0;
6336 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6337 return 0;
6340 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
6341 * @vport: pointer to a host virtual N_Port data structure.
6342 * @cmdiocb: pointer to lpfc command iocb data structure.
6343 * @ndlp: pointer to a node-list data structure.
6345 * This routine processes Read Port Status (RPS) IOCB received as an
6346 * ELS unsolicited event. It first checks the remote port state. If the
6347 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6348 * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
6349 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
6350 * for reading the HBA link statistics. It is for the callback function,
6351 * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
6352 * to actually sending out RPS Accept (ACC) response.
6354 * Return codes
6355 * 0 - Successfully processed rps iocb (currently always return 0)
6357 static int
6358 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6359 struct lpfc_nodelist *ndlp)
6361 struct lpfc_hba *phba = vport->phba;
6362 uint32_t *lp;
6363 uint8_t flag;
6364 LPFC_MBOXQ_t *mbox;
6365 struct lpfc_dmabuf *pcmd;
6366 RPS *rps;
6367 struct ls_rjt stat;
6369 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6370 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6371 /* reject the unsolicited RPS request and done with it */
6372 goto reject_out;
6374 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6375 lp = (uint32_t *) pcmd->virt;
6376 flag = (be32_to_cpu(*lp++) & 0xf);
6377 rps = (RPS *) lp;
6379 if ((flag == 0) ||
6380 ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
6381 ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
6382 sizeof(struct lpfc_name)) == 0))) {
6384 printk("Fix me....\n");
6385 dump_stack();
6386 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
6387 if (mbox) {
6388 lpfc_read_lnk_stat(phba, mbox);
6389 mbox->context1 = (void *)((unsigned long)
6390 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
6391 cmdiocb->iocb.ulpContext)); /* rx_id */
6392 mbox->context2 = lpfc_nlp_get(ndlp);
6393 mbox->vport = vport;
6394 mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
6395 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6396 != MBX_NOT_FINISHED)
6397 /* Mbox completion will send ELS Response */
6398 return 0;
6399 /* Decrement reference count used for the failed mbox
6400 * command.
6402 lpfc_nlp_put(ndlp);
6403 mempool_free(mbox, phba->mbox_mem_pool);
6407 reject_out:
6408 /* issue rejection response */
6409 stat.un.b.lsRjtRsvd0 = 0;
6410 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6411 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6412 stat.un.b.vendorUnique = 0;
6413 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6414 return 0;
6417 /* lpfc_issue_els_rrq - Process an unsolicited rps iocb
6418 * @vport: pointer to a host virtual N_Port data structure.
6419 * @ndlp: pointer to a node-list data structure.
6420 * @did: DID of the target.
6421 * @rrq: Pointer to the rrq struct.
6423 * Build a ELS RRQ command and send it to the target. If the issue_iocb is
6424 * Successful the the completion handler will clear the RRQ.
6426 * Return codes
6427 * 0 - Successfully sent rrq els iocb.
6428 * 1 - Failed to send rrq els iocb.
6430 static int
6431 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
6432 uint32_t did, struct lpfc_node_rrq *rrq)
6434 struct lpfc_hba *phba = vport->phba;
6435 struct RRQ *els_rrq;
6436 struct lpfc_iocbq *elsiocb;
6437 uint8_t *pcmd;
6438 uint16_t cmdsize;
6439 int ret;
6442 if (ndlp != rrq->ndlp)
6443 ndlp = rrq->ndlp;
6444 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
6445 return 1;
6447 /* If ndlp is not NULL, we will bump the reference count on it */
6448 cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
6449 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
6450 ELS_CMD_RRQ);
6451 if (!elsiocb)
6452 return 1;
6454 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6456 /* For RRQ request, remainder of payload is Exchange IDs */
6457 *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
6458 pcmd += sizeof(uint32_t);
6459 els_rrq = (struct RRQ *) pcmd;
6461 bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
6462 bf_set(rrq_rxid, els_rrq, rrq->rxid);
6463 bf_set(rrq_did, els_rrq, vport->fc_myDID);
6464 els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
6465 els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
6468 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6469 "Issue RRQ: did:x%x",
6470 did, rrq->xritag, rrq->rxid);
6471 elsiocb->context_un.rrq = rrq;
6472 elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
6473 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6475 if (ret == IOCB_ERROR) {
6476 lpfc_els_free_iocb(phba, elsiocb);
6477 return 1;
6479 return 0;
6483 * lpfc_send_rrq - Sends ELS RRQ if needed.
6484 * @phba: pointer to lpfc hba data structure.
6485 * @rrq: pointer to the active rrq.
6487 * This routine will call the lpfc_issue_els_rrq if the rrq is
6488 * still active for the xri. If this function returns a failure then
6489 * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
6491 * Returns 0 Success.
6492 * 1 Failure.
6495 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
6497 struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
6498 rrq->nlp_DID);
6499 if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
6500 return lpfc_issue_els_rrq(rrq->vport, ndlp,
6501 rrq->nlp_DID, rrq);
6502 else
6503 return 1;
6507 * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
6508 * @vport: pointer to a host virtual N_Port data structure.
6509 * @cmdsize: size of the ELS command.
6510 * @oldiocb: pointer to the original lpfc command iocb data structure.
6511 * @ndlp: pointer to a node-list data structure.
6513 * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
6514 * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
6516 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6517 * will be incremented by 1 for holding the ndlp and the reference to ndlp
6518 * will be stored into the context1 field of the IOCB for the completion
6519 * callback function to the RPL Accept Response ELS command.
6521 * Return code
6522 * 0 - Successfully issued ACC RPL ELS command
6523 * 1 - Failed to issue ACC RPL ELS command
6525 static int
6526 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
6527 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6529 struct lpfc_hba *phba = vport->phba;
6530 IOCB_t *icmd, *oldcmd;
6531 RPL_RSP rpl_rsp;
6532 struct lpfc_iocbq *elsiocb;
6533 uint8_t *pcmd;
6535 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6536 ndlp->nlp_DID, ELS_CMD_ACC);
6538 if (!elsiocb)
6539 return 1;
6541 icmd = &elsiocb->iocb;
6542 oldcmd = &oldiocb->iocb;
6543 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6544 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
6546 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6547 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6548 pcmd += sizeof(uint16_t);
6549 *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
6550 pcmd += sizeof(uint16_t);
6552 /* Setup the RPL ACC payload */
6553 rpl_rsp.listLen = be32_to_cpu(1);
6554 rpl_rsp.index = 0;
6555 rpl_rsp.port_num_blk.portNum = 0;
6556 rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
6557 memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
6558 sizeof(struct lpfc_name));
6559 memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
6560 /* Xmit ELS RPL ACC response tag <ulpIoTag> */
6561 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6562 "0120 Xmit ELS RPL ACC response tag x%x "
6563 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
6564 "rpi x%x\n",
6565 elsiocb->iotag, elsiocb->iocb.ulpContext,
6566 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6567 ndlp->nlp_rpi);
6568 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6569 phba->fc_stat.elsXmitACC++;
6570 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
6571 IOCB_ERROR) {
6572 lpfc_els_free_iocb(phba, elsiocb);
6573 return 1;
6575 return 0;
6579 * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
6580 * @vport: pointer to a host virtual N_Port data structure.
6581 * @cmdiocb: pointer to lpfc command iocb data structure.
6582 * @ndlp: pointer to a node-list data structure.
6584 * This routine processes Read Port List (RPL) IOCB received as an ELS
6585 * unsolicited event. It first checks the remote port state. If the remote
6586 * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
6587 * invokes the lpfc_els_rsp_reject() routine to send reject response.
6588 * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
6589 * to accept the RPL.
6591 * Return code
6592 * 0 - Successfully processed rpl iocb (currently always return 0)
6594 static int
6595 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6596 struct lpfc_nodelist *ndlp)
6598 struct lpfc_dmabuf *pcmd;
6599 uint32_t *lp;
6600 uint32_t maxsize;
6601 uint16_t cmdsize;
6602 RPL *rpl;
6603 struct ls_rjt stat;
6605 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6606 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
6607 /* issue rejection response */
6608 stat.un.b.lsRjtRsvd0 = 0;
6609 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6610 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6611 stat.un.b.vendorUnique = 0;
6612 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6613 NULL);
6614 /* rejected the unsolicited RPL request and done with it */
6615 return 0;
6618 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6619 lp = (uint32_t *) pcmd->virt;
6620 rpl = (RPL *) (lp + 1);
6621 maxsize = be32_to_cpu(rpl->maxsize);
6623 /* We support only one port */
6624 if ((rpl->index == 0) &&
6625 ((maxsize == 0) ||
6626 ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
6627 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
6628 } else {
6629 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
6631 lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
6633 return 0;
6637 * lpfc_els_rcv_farp - Process an unsolicited farp request els command
6638 * @vport: pointer to a virtual N_Port data structure.
6639 * @cmdiocb: pointer to lpfc command iocb data structure.
6640 * @ndlp: pointer to a node-list data structure.
6642 * This routine processes Fibre Channel Address Resolution Protocol
6643 * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
6644 * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
6645 * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
6646 * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
6647 * remote PortName is compared against the FC PortName stored in the @vport
6648 * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
6649 * compared against the FC NodeName stored in the @vport data structure.
6650 * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
6651 * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
6652 * invoked to send out FARP Response to the remote node. Before sending the
6653 * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
6654 * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
6655 * routine is invoked to log into the remote port first.
6657 * Return code
6658 * 0 - Either the FARP Match Mode not supported or successfully processed
6660 static int
6661 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6662 struct lpfc_nodelist *ndlp)
6664 struct lpfc_dmabuf *pcmd;
6665 uint32_t *lp;
6666 IOCB_t *icmd;
6667 FARP *fp;
6668 uint32_t cmd, cnt, did;
6670 icmd = &cmdiocb->iocb;
6671 did = icmd->un.elsreq64.remoteID;
6672 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6673 lp = (uint32_t *) pcmd->virt;
6675 cmd = *lp++;
6676 fp = (FARP *) lp;
6677 /* FARP-REQ received from DID <did> */
6678 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6679 "0601 FARP-REQ received from DID x%x\n", did);
6680 /* We will only support match on WWPN or WWNN */
6681 if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
6682 return 0;
6685 cnt = 0;
6686 /* If this FARP command is searching for my portname */
6687 if (fp->Mflags & FARP_MATCH_PORT) {
6688 if (memcmp(&fp->RportName, &vport->fc_portname,
6689 sizeof(struct lpfc_name)) == 0)
6690 cnt = 1;
6693 /* If this FARP command is searching for my nodename */
6694 if (fp->Mflags & FARP_MATCH_NODE) {
6695 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
6696 sizeof(struct lpfc_name)) == 0)
6697 cnt = 1;
6700 if (cnt) {
6701 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
6702 (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
6703 /* Log back into the node before sending the FARP. */
6704 if (fp->Rflags & FARP_REQUEST_PLOGI) {
6705 ndlp->nlp_prev_state = ndlp->nlp_state;
6706 lpfc_nlp_set_state(vport, ndlp,
6707 NLP_STE_PLOGI_ISSUE);
6708 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6711 /* Send a FARP response to that node */
6712 if (fp->Rflags & FARP_REQUEST_FARPR)
6713 lpfc_issue_els_farpr(vport, did, 0);
6716 return 0;
6720 * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
6721 * @vport: pointer to a host virtual N_Port data structure.
6722 * @cmdiocb: pointer to lpfc command iocb data structure.
6723 * @ndlp: pointer to a node-list data structure.
6725 * This routine processes Fibre Channel Address Resolution Protocol
6726 * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
6727 * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
6728 * the FARP response request.
6730 * Return code
6731 * 0 - Successfully processed FARPR IOCB (currently always return 0)
6733 static int
6734 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6735 struct lpfc_nodelist *ndlp)
6737 struct lpfc_dmabuf *pcmd;
6738 uint32_t *lp;
6739 IOCB_t *icmd;
6740 uint32_t cmd, did;
6742 icmd = &cmdiocb->iocb;
6743 did = icmd->un.elsreq64.remoteID;
6744 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6745 lp = (uint32_t *) pcmd->virt;
6747 cmd = *lp++;
6748 /* FARP-RSP received from DID <did> */
6749 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6750 "0600 FARP-RSP received from DID x%x\n", did);
6751 /* ACCEPT the Farp resp request */
6752 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6754 return 0;
6758 * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
6759 * @vport: pointer to a host virtual N_Port data structure.
6760 * @cmdiocb: pointer to lpfc command iocb data structure.
6761 * @fan_ndlp: pointer to a node-list data structure.
6763 * This routine processes a Fabric Address Notification (FAN) IOCB
6764 * command received as an ELS unsolicited event. The FAN ELS command will
6765 * only be processed on a physical port (i.e., the @vport represents the
6766 * physical port). The fabric NodeName and PortName from the FAN IOCB are
6767 * compared against those in the phba data structure. If any of those is
6768 * different, the lpfc_initial_flogi() routine is invoked to initialize
6769 * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
6770 * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
6771 * is invoked to register login to the fabric.
6773 * Return code
6774 * 0 - Successfully processed fan iocb (currently always return 0).
6776 static int
6777 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6778 struct lpfc_nodelist *fan_ndlp)
6780 struct lpfc_hba *phba = vport->phba;
6781 uint32_t *lp;
6782 FAN *fp;
6784 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
6785 lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
6786 fp = (FAN *) ++lp;
6787 /* FAN received; Fan does not have a reply sequence */
6788 if ((vport == phba->pport) &&
6789 (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
6790 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
6791 sizeof(struct lpfc_name))) ||
6792 (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
6793 sizeof(struct lpfc_name)))) {
6794 /* This port has switched fabrics. FLOGI is required */
6795 lpfc_issue_init_vfi(vport);
6796 } else {
6797 /* FAN verified - skip FLOGI */
6798 vport->fc_myDID = vport->fc_prevDID;
6799 if (phba->sli_rev < LPFC_SLI_REV4)
6800 lpfc_issue_fabric_reglogin(vport);
6801 else {
6802 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6803 "3138 Need register VFI: (x%x/%x)\n",
6804 vport->fc_prevDID, vport->fc_myDID);
6805 lpfc_issue_reg_vfi(vport);
6809 return 0;
6813 * lpfc_els_timeout - Handler funciton to the els timer
6814 * @ptr: holder for the timer function associated data.
6816 * This routine is invoked by the ELS timer after timeout. It posts the ELS
6817 * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
6818 * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
6819 * up the worker thread. It is for the worker thread to invoke the routine
6820 * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
6822 void
6823 lpfc_els_timeout(unsigned long ptr)
6825 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
6826 struct lpfc_hba *phba = vport->phba;
6827 uint32_t tmo_posted;
6828 unsigned long iflag;
6830 spin_lock_irqsave(&vport->work_port_lock, iflag);
6831 tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
6832 if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
6833 vport->work_port_events |= WORKER_ELS_TMO;
6834 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
6836 if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
6837 lpfc_worker_wake_up(phba);
6838 return;
6843 * lpfc_els_timeout_handler - Process an els timeout event
6844 * @vport: pointer to a virtual N_Port data structure.
6846 * This routine is the actual handler function that processes an ELS timeout
6847 * event. It walks the ELS ring to get and abort all the IOCBs (except the
6848 * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
6849 * invoking the lpfc_sli_issue_abort_iotag() routine.
6851 void
6852 lpfc_els_timeout_handler(struct lpfc_vport *vport)
6854 struct lpfc_hba *phba = vport->phba;
6855 struct lpfc_sli_ring *pring;
6856 struct lpfc_iocbq *tmp_iocb, *piocb;
6857 IOCB_t *cmd = NULL;
6858 struct lpfc_dmabuf *pcmd;
6859 uint32_t els_command = 0;
6860 uint32_t timeout;
6861 uint32_t remote_ID = 0xffffffff;
6862 LIST_HEAD(abort_list);
6865 timeout = (uint32_t)(phba->fc_ratov << 1);
6867 pring = &phba->sli.ring[LPFC_ELS_RING];
6868 if ((phba->pport->load_flag & FC_UNLOADING))
6869 return;
6870 spin_lock_irq(&phba->hbalock);
6871 if (phba->sli_rev == LPFC_SLI_REV4)
6872 spin_lock(&pring->ring_lock);
6874 if ((phba->pport->load_flag & FC_UNLOADING)) {
6875 if (phba->sli_rev == LPFC_SLI_REV4)
6876 spin_unlock(&pring->ring_lock);
6877 spin_unlock_irq(&phba->hbalock);
6878 return;
6881 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6882 cmd = &piocb->iocb;
6884 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
6885 piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6886 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6887 continue;
6889 if (piocb->vport != vport)
6890 continue;
6892 pcmd = (struct lpfc_dmabuf *) piocb->context2;
6893 if (pcmd)
6894 els_command = *(uint32_t *) (pcmd->virt);
6896 if (els_command == ELS_CMD_FARP ||
6897 els_command == ELS_CMD_FARPR ||
6898 els_command == ELS_CMD_FDISC)
6899 continue;
6901 if (piocb->drvrTimeout > 0) {
6902 if (piocb->drvrTimeout >= timeout)
6903 piocb->drvrTimeout -= timeout;
6904 else
6905 piocb->drvrTimeout = 0;
6906 continue;
6909 remote_ID = 0xffffffff;
6910 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
6911 remote_ID = cmd->un.elsreq64.remoteID;
6912 else {
6913 struct lpfc_nodelist *ndlp;
6914 ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
6915 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
6916 remote_ID = ndlp->nlp_DID;
6918 list_add_tail(&piocb->dlist, &abort_list);
6920 if (phba->sli_rev == LPFC_SLI_REV4)
6921 spin_unlock(&pring->ring_lock);
6922 spin_unlock_irq(&phba->hbalock);
6924 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
6925 cmd = &piocb->iocb;
6926 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6927 "0127 ELS timeout Data: x%x x%x x%x "
6928 "x%x\n", els_command,
6929 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
6930 spin_lock_irq(&phba->hbalock);
6931 list_del_init(&piocb->dlist);
6932 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6933 spin_unlock_irq(&phba->hbalock);
6936 if (!list_empty(&phba->sli.ring[LPFC_ELS_RING].txcmplq))
6937 if (!(phba->pport->load_flag & FC_UNLOADING))
6938 mod_timer(&vport->els_tmofunc,
6939 jiffies + msecs_to_jiffies(1000 * timeout));
6943 * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
6944 * @vport: pointer to a host virtual N_Port data structure.
6946 * This routine is used to clean up all the outstanding ELS commands on a
6947 * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
6948 * routine. After that, it walks the ELS transmit queue to remove all the
6949 * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
6950 * the IOCBs with a non-NULL completion callback function, the callback
6951 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
6952 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
6953 * callback function, the IOCB will simply be released. Finally, it walks
6954 * the ELS transmit completion queue to issue an abort IOCB to any transmit
6955 * completion queue IOCB that is associated with the @vport and is not
6956 * an IOCB from libdfc (i.e., the management plane IOCBs that are not
6957 * part of the discovery state machine) out to HBA by invoking the
6958 * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
6959 * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
6960 * the IOCBs are aborted when this function returns.
6962 void
6963 lpfc_els_flush_cmd(struct lpfc_vport *vport)
6965 LIST_HEAD(abort_list);
6966 struct lpfc_hba *phba = vport->phba;
6967 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6968 struct lpfc_iocbq *tmp_iocb, *piocb;
6969 IOCB_t *cmd = NULL;
6971 lpfc_fabric_abort_vport(vport);
6973 * For SLI3, only the hbalock is required. But SLI4 needs to coordinate
6974 * with the ring insert operation. Because lpfc_sli_issue_abort_iotag
6975 * ultimately grabs the ring_lock, the driver must splice the list into
6976 * a working list and release the locks before calling the abort.
6978 spin_lock_irq(&phba->hbalock);
6979 if (phba->sli_rev == LPFC_SLI_REV4)
6980 spin_lock(&pring->ring_lock);
6982 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6983 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
6984 continue;
6986 if (piocb->vport != vport)
6987 continue;
6988 list_add_tail(&piocb->dlist, &abort_list);
6990 if (phba->sli_rev == LPFC_SLI_REV4)
6991 spin_unlock(&pring->ring_lock);
6992 spin_unlock_irq(&phba->hbalock);
6993 /* Abort each iocb on the aborted list and remove the dlist links. */
6994 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
6995 spin_lock_irq(&phba->hbalock);
6996 list_del_init(&piocb->dlist);
6997 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6998 spin_unlock_irq(&phba->hbalock);
7000 if (!list_empty(&abort_list))
7001 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7002 "3387 abort list for txq not empty\n");
7003 INIT_LIST_HEAD(&abort_list);
7005 spin_lock_irq(&phba->hbalock);
7006 if (phba->sli_rev == LPFC_SLI_REV4)
7007 spin_lock(&pring->ring_lock);
7009 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
7010 cmd = &piocb->iocb;
7012 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
7013 continue;
7016 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
7017 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
7018 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
7019 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7020 cmd->ulpCommand == CMD_ABORT_XRI_CN)
7021 continue;
7023 if (piocb->vport != vport)
7024 continue;
7026 list_del_init(&piocb->list);
7027 list_add_tail(&piocb->list, &abort_list);
7029 if (phba->sli_rev == LPFC_SLI_REV4)
7030 spin_unlock(&pring->ring_lock);
7031 spin_unlock_irq(&phba->hbalock);
7033 /* Cancell all the IOCBs from the completions list */
7034 lpfc_sli_cancel_iocbs(phba, &abort_list,
7035 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
7037 return;
7041 * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
7042 * @phba: pointer to lpfc hba data structure.
7044 * This routine is used to clean up all the outstanding ELS commands on a
7045 * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
7046 * routine. After that, it walks the ELS transmit queue to remove all the
7047 * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
7048 * the IOCBs with the completion callback function associated, the callback
7049 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7050 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
7051 * callback function associated, the IOCB will simply be released. Finally,
7052 * it walks the ELS transmit completion queue to issue an abort IOCB to any
7053 * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
7054 * management plane IOCBs that are not part of the discovery state machine)
7055 * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
7057 void
7058 lpfc_els_flush_all_cmd(struct lpfc_hba *phba)
7060 struct lpfc_vport *vport;
7061 list_for_each_entry(vport, &phba->port_list, listentry)
7062 lpfc_els_flush_cmd(vport);
7064 return;
7068 * lpfc_send_els_failure_event - Posts an ELS command failure event
7069 * @phba: Pointer to hba context object.
7070 * @cmdiocbp: Pointer to command iocb which reported error.
7071 * @rspiocbp: Pointer to response iocb which reported error.
7073 * This function sends an event when there is an ELS command
7074 * failure.
7076 void
7077 lpfc_send_els_failure_event(struct lpfc_hba *phba,
7078 struct lpfc_iocbq *cmdiocbp,
7079 struct lpfc_iocbq *rspiocbp)
7081 struct lpfc_vport *vport = cmdiocbp->vport;
7082 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7083 struct lpfc_lsrjt_event lsrjt_event;
7084 struct lpfc_fabric_event_header fabric_event;
7085 struct ls_rjt stat;
7086 struct lpfc_nodelist *ndlp;
7087 uint32_t *pcmd;
7089 ndlp = cmdiocbp->context1;
7090 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7091 return;
7093 if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
7094 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
7095 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
7096 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
7097 sizeof(struct lpfc_name));
7098 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
7099 sizeof(struct lpfc_name));
7100 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7101 cmdiocbp->context2)->virt);
7102 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
7103 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
7104 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
7105 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
7106 fc_host_post_vendor_event(shost,
7107 fc_get_event_number(),
7108 sizeof(lsrjt_event),
7109 (char *)&lsrjt_event,
7110 LPFC_NL_VENDOR_ID);
7111 return;
7113 if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
7114 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
7115 fabric_event.event_type = FC_REG_FABRIC_EVENT;
7116 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
7117 fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
7118 else
7119 fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
7120 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
7121 sizeof(struct lpfc_name));
7122 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
7123 sizeof(struct lpfc_name));
7124 fc_host_post_vendor_event(shost,
7125 fc_get_event_number(),
7126 sizeof(fabric_event),
7127 (char *)&fabric_event,
7128 LPFC_NL_VENDOR_ID);
7129 return;
7135 * lpfc_send_els_event - Posts unsolicited els event
7136 * @vport: Pointer to vport object.
7137 * @ndlp: Pointer FC node object.
7138 * @cmd: ELS command code.
7140 * This function posts an event when there is an incoming
7141 * unsolicited ELS command.
7143 static void
7144 lpfc_send_els_event(struct lpfc_vport *vport,
7145 struct lpfc_nodelist *ndlp,
7146 uint32_t *payload)
7148 struct lpfc_els_event_header *els_data = NULL;
7149 struct lpfc_logo_event *logo_data = NULL;
7150 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7152 if (*payload == ELS_CMD_LOGO) {
7153 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
7154 if (!logo_data) {
7155 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7156 "0148 Failed to allocate memory "
7157 "for LOGO event\n");
7158 return;
7160 els_data = &logo_data->header;
7161 } else {
7162 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
7163 GFP_KERNEL);
7164 if (!els_data) {
7165 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7166 "0149 Failed to allocate memory "
7167 "for ELS event\n");
7168 return;
7171 els_data->event_type = FC_REG_ELS_EVENT;
7172 switch (*payload) {
7173 case ELS_CMD_PLOGI:
7174 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
7175 break;
7176 case ELS_CMD_PRLO:
7177 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
7178 break;
7179 case ELS_CMD_ADISC:
7180 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
7181 break;
7182 case ELS_CMD_LOGO:
7183 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
7184 /* Copy the WWPN in the LOGO payload */
7185 memcpy(logo_data->logo_wwpn, &payload[2],
7186 sizeof(struct lpfc_name));
7187 break;
7188 default:
7189 kfree(els_data);
7190 return;
7192 memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
7193 memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
7194 if (*payload == ELS_CMD_LOGO) {
7195 fc_host_post_vendor_event(shost,
7196 fc_get_event_number(),
7197 sizeof(struct lpfc_logo_event),
7198 (char *)logo_data,
7199 LPFC_NL_VENDOR_ID);
7200 kfree(logo_data);
7201 } else {
7202 fc_host_post_vendor_event(shost,
7203 fc_get_event_number(),
7204 sizeof(struct lpfc_els_event_header),
7205 (char *)els_data,
7206 LPFC_NL_VENDOR_ID);
7207 kfree(els_data);
7210 return;
7215 * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
7216 * @phba: pointer to lpfc hba data structure.
7217 * @pring: pointer to a SLI ring.
7218 * @vport: pointer to a host virtual N_Port data structure.
7219 * @elsiocb: pointer to lpfc els command iocb data structure.
7221 * This routine is used for processing the IOCB associated with a unsolicited
7222 * event. It first determines whether there is an existing ndlp that matches
7223 * the DID from the unsolicited IOCB. If not, it will create a new one with
7224 * the DID from the unsolicited IOCB. The ELS command from the unsolicited
7225 * IOCB is then used to invoke the proper routine and to set up proper state
7226 * of the discovery state machine.
7228 static void
7229 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7230 struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
7232 struct Scsi_Host *shost;
7233 struct lpfc_nodelist *ndlp;
7234 struct ls_rjt stat;
7235 uint32_t *payload;
7236 uint32_t cmd, did, newnode;
7237 uint8_t rjt_exp, rjt_err = 0;
7238 IOCB_t *icmd = &elsiocb->iocb;
7240 if (!vport || !(elsiocb->context2))
7241 goto dropit;
7243 newnode = 0;
7244 payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
7245 cmd = *payload;
7246 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
7247 lpfc_post_buffer(phba, pring, 1);
7249 did = icmd->un.rcvels.remoteID;
7250 if (icmd->ulpStatus) {
7251 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7252 "RCV Unsol ELS: status:x%x/x%x did:x%x",
7253 icmd->ulpStatus, icmd->un.ulpWord[4], did);
7254 goto dropit;
7257 /* Check to see if link went down during discovery */
7258 if (lpfc_els_chk_latt(vport))
7259 goto dropit;
7261 /* Ignore traffic received during vport shutdown. */
7262 if (vport->load_flag & FC_UNLOADING)
7263 goto dropit;
7265 /* If NPort discovery is delayed drop incoming ELS */
7266 if ((vport->fc_flag & FC_DISC_DELAYED) &&
7267 (cmd != ELS_CMD_PLOGI))
7268 goto dropit;
7270 ndlp = lpfc_findnode_did(vport, did);
7271 if (!ndlp) {
7272 /* Cannot find existing Fabric ndlp, so allocate a new one */
7273 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
7274 if (!ndlp)
7275 goto dropit;
7277 lpfc_nlp_init(vport, ndlp, did);
7278 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7279 newnode = 1;
7280 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7281 ndlp->nlp_type |= NLP_FABRIC;
7282 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
7283 ndlp = lpfc_enable_node(vport, ndlp,
7284 NLP_STE_UNUSED_NODE);
7285 if (!ndlp)
7286 goto dropit;
7287 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7288 newnode = 1;
7289 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7290 ndlp->nlp_type |= NLP_FABRIC;
7291 } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
7292 /* This is similar to the new node path */
7293 ndlp = lpfc_nlp_get(ndlp);
7294 if (!ndlp)
7295 goto dropit;
7296 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7297 newnode = 1;
7300 phba->fc_stat.elsRcvFrame++;
7303 * Do not process any unsolicited ELS commands
7304 * if the ndlp is in DEV_LOSS
7306 shost = lpfc_shost_from_vport(vport);
7307 spin_lock_irq(shost->host_lock);
7308 if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
7309 spin_unlock_irq(shost->host_lock);
7310 goto dropit;
7312 spin_unlock_irq(shost->host_lock);
7314 elsiocb->context1 = lpfc_nlp_get(ndlp);
7315 elsiocb->vport = vport;
7317 if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
7318 cmd &= ELS_CMD_MASK;
7320 /* ELS command <elsCmd> received from NPORT <did> */
7321 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7322 "0112 ELS command x%x received from NPORT x%x "
7323 "Data: x%x x%x x%x x%x\n",
7324 cmd, did, vport->port_state, vport->fc_flag,
7325 vport->fc_myDID, vport->fc_prevDID);
7327 /* reject till our FLOGI completes */
7328 if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
7329 (cmd != ELS_CMD_FLOGI)) {
7330 rjt_err = LSRJT_UNABLE_TPC;
7331 rjt_exp = LSEXP_NOTHING_MORE;
7332 goto lsrjt;
7335 switch (cmd) {
7336 case ELS_CMD_PLOGI:
7337 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7338 "RCV PLOGI: did:x%x/ste:x%x flg:x%x",
7339 did, vport->port_state, ndlp->nlp_flag);
7341 phba->fc_stat.elsRcvPLOGI++;
7342 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
7343 if (phba->sli_rev == LPFC_SLI_REV4 &&
7344 (phba->pport->fc_flag & FC_PT2PT)) {
7345 vport->fc_prevDID = vport->fc_myDID;
7346 /* Our DID needs to be updated before registering
7347 * the vfi. This is done in lpfc_rcv_plogi but
7348 * that is called after the reg_vfi.
7350 vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
7351 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7352 "3312 Remote port assigned DID x%x "
7353 "%x\n", vport->fc_myDID,
7354 vport->fc_prevDID);
7357 lpfc_send_els_event(vport, ndlp, payload);
7359 /* If Nport discovery is delayed, reject PLOGIs */
7360 if (vport->fc_flag & FC_DISC_DELAYED) {
7361 rjt_err = LSRJT_UNABLE_TPC;
7362 rjt_exp = LSEXP_NOTHING_MORE;
7363 break;
7366 if (vport->port_state < LPFC_DISC_AUTH) {
7367 if (!(phba->pport->fc_flag & FC_PT2PT) ||
7368 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
7369 rjt_err = LSRJT_UNABLE_TPC;
7370 rjt_exp = LSEXP_NOTHING_MORE;
7371 break;
7375 spin_lock_irq(shost->host_lock);
7376 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
7377 spin_unlock_irq(shost->host_lock);
7379 lpfc_disc_state_machine(vport, ndlp, elsiocb,
7380 NLP_EVT_RCV_PLOGI);
7382 break;
7383 case ELS_CMD_FLOGI:
7384 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7385 "RCV FLOGI: did:x%x/ste:x%x flg:x%x",
7386 did, vport->port_state, ndlp->nlp_flag);
7388 phba->fc_stat.elsRcvFLOGI++;
7389 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
7390 if (newnode)
7391 lpfc_nlp_put(ndlp);
7392 break;
7393 case ELS_CMD_LOGO:
7394 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7395 "RCV LOGO: did:x%x/ste:x%x flg:x%x",
7396 did, vport->port_state, ndlp->nlp_flag);
7398 phba->fc_stat.elsRcvLOGO++;
7399 lpfc_send_els_event(vport, ndlp, payload);
7400 if (vport->port_state < LPFC_DISC_AUTH) {
7401 rjt_err = LSRJT_UNABLE_TPC;
7402 rjt_exp = LSEXP_NOTHING_MORE;
7403 break;
7405 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
7406 break;
7407 case ELS_CMD_PRLO:
7408 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7409 "RCV PRLO: did:x%x/ste:x%x flg:x%x",
7410 did, vport->port_state, ndlp->nlp_flag);
7412 phba->fc_stat.elsRcvPRLO++;
7413 lpfc_send_els_event(vport, ndlp, payload);
7414 if (vport->port_state < LPFC_DISC_AUTH) {
7415 rjt_err = LSRJT_UNABLE_TPC;
7416 rjt_exp = LSEXP_NOTHING_MORE;
7417 break;
7419 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
7420 break;
7421 case ELS_CMD_LCB:
7422 phba->fc_stat.elsRcvLCB++;
7423 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
7424 break;
7425 case ELS_CMD_RDP:
7426 phba->fc_stat.elsRcvRDP++;
7427 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
7428 break;
7429 case ELS_CMD_RSCN:
7430 phba->fc_stat.elsRcvRSCN++;
7431 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
7432 if (newnode)
7433 lpfc_nlp_put(ndlp);
7434 break;
7435 case ELS_CMD_ADISC:
7436 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7437 "RCV ADISC: did:x%x/ste:x%x flg:x%x",
7438 did, vport->port_state, ndlp->nlp_flag);
7440 lpfc_send_els_event(vport, ndlp, payload);
7441 phba->fc_stat.elsRcvADISC++;
7442 if (vport->port_state < LPFC_DISC_AUTH) {
7443 rjt_err = LSRJT_UNABLE_TPC;
7444 rjt_exp = LSEXP_NOTHING_MORE;
7445 break;
7447 lpfc_disc_state_machine(vport, ndlp, elsiocb,
7448 NLP_EVT_RCV_ADISC);
7449 break;
7450 case ELS_CMD_PDISC:
7451 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7452 "RCV PDISC: did:x%x/ste:x%x flg:x%x",
7453 did, vport->port_state, ndlp->nlp_flag);
7455 phba->fc_stat.elsRcvPDISC++;
7456 if (vport->port_state < LPFC_DISC_AUTH) {
7457 rjt_err = LSRJT_UNABLE_TPC;
7458 rjt_exp = LSEXP_NOTHING_MORE;
7459 break;
7461 lpfc_disc_state_machine(vport, ndlp, elsiocb,
7462 NLP_EVT_RCV_PDISC);
7463 break;
7464 case ELS_CMD_FARPR:
7465 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7466 "RCV FARPR: did:x%x/ste:x%x flg:x%x",
7467 did, vport->port_state, ndlp->nlp_flag);
7469 phba->fc_stat.elsRcvFARPR++;
7470 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
7471 break;
7472 case ELS_CMD_FARP:
7473 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7474 "RCV FARP: did:x%x/ste:x%x flg:x%x",
7475 did, vport->port_state, ndlp->nlp_flag);
7477 phba->fc_stat.elsRcvFARP++;
7478 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
7479 break;
7480 case ELS_CMD_FAN:
7481 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7482 "RCV FAN: did:x%x/ste:x%x flg:x%x",
7483 did, vport->port_state, ndlp->nlp_flag);
7485 phba->fc_stat.elsRcvFAN++;
7486 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
7487 break;
7488 case ELS_CMD_PRLI:
7489 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7490 "RCV PRLI: did:x%x/ste:x%x flg:x%x",
7491 did, vport->port_state, ndlp->nlp_flag);
7493 phba->fc_stat.elsRcvPRLI++;
7494 if ((vport->port_state < LPFC_DISC_AUTH) &&
7495 (vport->fc_flag & FC_FABRIC)) {
7496 rjt_err = LSRJT_UNABLE_TPC;
7497 rjt_exp = LSEXP_NOTHING_MORE;
7498 break;
7500 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
7501 break;
7502 case ELS_CMD_LIRR:
7503 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7504 "RCV LIRR: did:x%x/ste:x%x flg:x%x",
7505 did, vport->port_state, ndlp->nlp_flag);
7507 phba->fc_stat.elsRcvLIRR++;
7508 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
7509 if (newnode)
7510 lpfc_nlp_put(ndlp);
7511 break;
7512 case ELS_CMD_RLS:
7513 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7514 "RCV RLS: did:x%x/ste:x%x flg:x%x",
7515 did, vport->port_state, ndlp->nlp_flag);
7517 phba->fc_stat.elsRcvRLS++;
7518 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
7519 if (newnode)
7520 lpfc_nlp_put(ndlp);
7521 break;
7522 case ELS_CMD_RPS:
7523 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7524 "RCV RPS: did:x%x/ste:x%x flg:x%x",
7525 did, vport->port_state, ndlp->nlp_flag);
7527 phba->fc_stat.elsRcvRPS++;
7528 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
7529 if (newnode)
7530 lpfc_nlp_put(ndlp);
7531 break;
7532 case ELS_CMD_RPL:
7533 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7534 "RCV RPL: did:x%x/ste:x%x flg:x%x",
7535 did, vport->port_state, ndlp->nlp_flag);
7537 phba->fc_stat.elsRcvRPL++;
7538 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
7539 if (newnode)
7540 lpfc_nlp_put(ndlp);
7541 break;
7542 case ELS_CMD_RNID:
7543 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7544 "RCV RNID: did:x%x/ste:x%x flg:x%x",
7545 did, vport->port_state, ndlp->nlp_flag);
7547 phba->fc_stat.elsRcvRNID++;
7548 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
7549 if (newnode)
7550 lpfc_nlp_put(ndlp);
7551 break;
7552 case ELS_CMD_RTV:
7553 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7554 "RCV RTV: did:x%x/ste:x%x flg:x%x",
7555 did, vport->port_state, ndlp->nlp_flag);
7556 phba->fc_stat.elsRcvRTV++;
7557 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
7558 if (newnode)
7559 lpfc_nlp_put(ndlp);
7560 break;
7561 case ELS_CMD_RRQ:
7562 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7563 "RCV RRQ: did:x%x/ste:x%x flg:x%x",
7564 did, vport->port_state, ndlp->nlp_flag);
7566 phba->fc_stat.elsRcvRRQ++;
7567 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
7568 if (newnode)
7569 lpfc_nlp_put(ndlp);
7570 break;
7571 case ELS_CMD_ECHO:
7572 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7573 "RCV ECHO: did:x%x/ste:x%x flg:x%x",
7574 did, vport->port_state, ndlp->nlp_flag);
7576 phba->fc_stat.elsRcvECHO++;
7577 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
7578 if (newnode)
7579 lpfc_nlp_put(ndlp);
7580 break;
7581 case ELS_CMD_REC:
7582 /* receive this due to exchange closed */
7583 rjt_err = LSRJT_UNABLE_TPC;
7584 rjt_exp = LSEXP_INVALID_OX_RX;
7585 break;
7586 default:
7587 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7588 "RCV ELS cmd: cmd:x%x did:x%x/ste:x%x",
7589 cmd, did, vport->port_state);
7591 /* Unsupported ELS command, reject */
7592 rjt_err = LSRJT_CMD_UNSUPPORTED;
7593 rjt_exp = LSEXP_NOTHING_MORE;
7595 /* Unknown ELS command <elsCmd> received from NPORT <did> */
7596 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7597 "0115 Unknown ELS command x%x "
7598 "received from NPORT x%x\n", cmd, did);
7599 if (newnode)
7600 lpfc_nlp_put(ndlp);
7601 break;
7604 lsrjt:
7605 /* check if need to LS_RJT received ELS cmd */
7606 if (rjt_err) {
7607 memset(&stat, 0, sizeof(stat));
7608 stat.un.b.lsRjtRsnCode = rjt_err;
7609 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
7610 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
7611 NULL);
7614 lpfc_nlp_put(elsiocb->context1);
7615 elsiocb->context1 = NULL;
7616 return;
7618 dropit:
7619 if (vport && !(vport->load_flag & FC_UNLOADING))
7620 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7621 "0111 Dropping received ELS cmd "
7622 "Data: x%x x%x x%x\n",
7623 icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
7624 phba->fc_stat.elsRcvDrop++;
7628 * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
7629 * @phba: pointer to lpfc hba data structure.
7630 * @pring: pointer to a SLI ring.
7631 * @elsiocb: pointer to lpfc els iocb data structure.
7633 * This routine is used to process an unsolicited event received from a SLI
7634 * (Service Level Interface) ring. The actual processing of the data buffer
7635 * associated with the unsolicited event is done by invoking the routine
7636 * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
7637 * SLI ring on which the unsolicited event was received.
7639 void
7640 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7641 struct lpfc_iocbq *elsiocb)
7643 struct lpfc_vport *vport = phba->pport;
7644 IOCB_t *icmd = &elsiocb->iocb;
7645 dma_addr_t paddr;
7646 struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
7647 struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
7649 elsiocb->context1 = NULL;
7650 elsiocb->context2 = NULL;
7651 elsiocb->context3 = NULL;
7653 if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
7654 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
7655 } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
7656 (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
7657 IOERR_RCV_BUFFER_WAITING) {
7658 phba->fc_stat.NoRcvBuf++;
7659 /* Not enough posted buffers; Try posting more buffers */
7660 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
7661 lpfc_post_buffer(phba, pring, 0);
7662 return;
7665 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
7666 (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
7667 icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
7668 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
7669 vport = phba->pport;
7670 else
7671 vport = lpfc_find_vport_by_vpid(phba,
7672 icmd->unsli3.rcvsli3.vpi);
7675 /* If there are no BDEs associated
7676 * with this IOCB, there is nothing to do.
7678 if (icmd->ulpBdeCount == 0)
7679 return;
7681 /* type of ELS cmd is first 32bit word
7682 * in packet
7684 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
7685 elsiocb->context2 = bdeBuf1;
7686 } else {
7687 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
7688 icmd->un.cont64[0].addrLow);
7689 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
7690 paddr);
7693 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
7695 * The different unsolicited event handlers would tell us
7696 * if they are done with "mp" by setting context2 to NULL.
7698 if (elsiocb->context2) {
7699 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
7700 elsiocb->context2 = NULL;
7703 /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
7704 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
7705 icmd->ulpBdeCount == 2) {
7706 elsiocb->context2 = bdeBuf2;
7707 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
7708 /* free mp if we are done with it */
7709 if (elsiocb->context2) {
7710 lpfc_in_buf_free(phba, elsiocb->context2);
7711 elsiocb->context2 = NULL;
7717 * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
7718 * @phba: pointer to lpfc hba data structure.
7719 * @vport: pointer to a virtual N_Port data structure.
7721 * This routine issues a Port Login (PLOGI) to the Name Server with
7722 * State Change Request (SCR) for a @vport. This routine will create an
7723 * ndlp for the Name Server associated to the @vport if such node does
7724 * not already exist. The PLOGI to Name Server is issued by invoking the
7725 * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
7726 * (FDMI) is configured to the @vport, a FDMI node will be created and
7727 * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
7729 void
7730 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
7732 struct lpfc_nodelist *ndlp, *ndlp_fdmi;
7733 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7736 * If lpfc_delay_discovery parameter is set and the clean address
7737 * bit is cleared and fc fabric parameters chenged, delay FC NPort
7738 * discovery.
7740 spin_lock_irq(shost->host_lock);
7741 if (vport->fc_flag & FC_DISC_DELAYED) {
7742 spin_unlock_irq(shost->host_lock);
7743 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
7744 "3334 Delay fc port discovery for %d seconds\n",
7745 phba->fc_ratov);
7746 mod_timer(&vport->delayed_disc_tmo,
7747 jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
7748 return;
7750 spin_unlock_irq(shost->host_lock);
7752 ndlp = lpfc_findnode_did(vport, NameServer_DID);
7753 if (!ndlp) {
7754 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
7755 if (!ndlp) {
7756 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7757 lpfc_disc_start(vport);
7758 return;
7760 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7761 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7762 "0251 NameServer login: no memory\n");
7763 return;
7765 lpfc_nlp_init(vport, ndlp, NameServer_DID);
7766 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
7767 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
7768 if (!ndlp) {
7769 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7770 lpfc_disc_start(vport);
7771 return;
7773 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7774 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7775 "0348 NameServer login: node freed\n");
7776 return;
7779 ndlp->nlp_type |= NLP_FABRIC;
7781 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7783 if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
7784 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7785 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7786 "0252 Cannot issue NameServer login\n");
7787 return;
7790 if (vport->cfg_fdmi_on & LPFC_FDMI_SUPPORT) {
7791 /* If this is the first time, allocate an ndlp and initialize
7792 * it. Otherwise, make sure the node is enabled and then do the
7793 * login.
7795 ndlp_fdmi = lpfc_findnode_did(vport, FDMI_DID);
7796 if (!ndlp_fdmi) {
7797 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
7798 GFP_KERNEL);
7799 if (ndlp_fdmi) {
7800 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
7801 ndlp_fdmi->nlp_type |= NLP_FABRIC;
7802 } else
7803 return;
7805 if (!NLP_CHK_NODE_ACT(ndlp_fdmi))
7806 ndlp_fdmi = lpfc_enable_node(vport,
7807 ndlp_fdmi,
7808 NLP_STE_NPR_NODE);
7810 if (ndlp_fdmi) {
7811 lpfc_nlp_set_state(vport, ndlp_fdmi,
7812 NLP_STE_PLOGI_ISSUE);
7813 lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID, 0);
7819 * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
7820 * @phba: pointer to lpfc hba data structure.
7821 * @pmb: pointer to the driver internal queue element for mailbox command.
7823 * This routine is the completion callback function to register new vport
7824 * mailbox command. If the new vport mailbox command completes successfully,
7825 * the fabric registration login shall be performed on physical port (the
7826 * new vport created is actually a physical port, with VPI 0) or the port
7827 * login to Name Server for State Change Request (SCR) will be performed
7828 * on virtual port (real virtual port, with VPI greater than 0).
7830 static void
7831 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7833 struct lpfc_vport *vport = pmb->vport;
7834 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7835 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
7836 MAILBOX_t *mb = &pmb->u.mb;
7837 int rc;
7839 spin_lock_irq(shost->host_lock);
7840 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
7841 spin_unlock_irq(shost->host_lock);
7843 if (mb->mbxStatus) {
7844 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7845 "0915 Register VPI failed : Status: x%x"
7846 " upd bit: x%x \n", mb->mbxStatus,
7847 mb->un.varRegVpi.upd);
7848 if (phba->sli_rev == LPFC_SLI_REV4 &&
7849 mb->un.varRegVpi.upd)
7850 goto mbox_err_exit ;
7852 switch (mb->mbxStatus) {
7853 case 0x11: /* unsupported feature */
7854 case 0x9603: /* max_vpi exceeded */
7855 case 0x9602: /* Link event since CLEAR_LA */
7856 /* giving up on vport registration */
7857 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7858 spin_lock_irq(shost->host_lock);
7859 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
7860 spin_unlock_irq(shost->host_lock);
7861 lpfc_can_disctmo(vport);
7862 break;
7863 /* If reg_vpi fail with invalid VPI status, re-init VPI */
7864 case 0x20:
7865 spin_lock_irq(shost->host_lock);
7866 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7867 spin_unlock_irq(shost->host_lock);
7868 lpfc_init_vpi(phba, pmb, vport->vpi);
7869 pmb->vport = vport;
7870 pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
7871 rc = lpfc_sli_issue_mbox(phba, pmb,
7872 MBX_NOWAIT);
7873 if (rc == MBX_NOT_FINISHED) {
7874 lpfc_printf_vlog(vport,
7875 KERN_ERR, LOG_MBOX,
7876 "2732 Failed to issue INIT_VPI"
7877 " mailbox command\n");
7878 } else {
7879 lpfc_nlp_put(ndlp);
7880 return;
7883 default:
7884 /* Try to recover from this error */
7885 if (phba->sli_rev == LPFC_SLI_REV4)
7886 lpfc_sli4_unreg_all_rpis(vport);
7887 lpfc_mbx_unreg_vpi(vport);
7888 spin_lock_irq(shost->host_lock);
7889 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7890 spin_unlock_irq(shost->host_lock);
7891 if (mb->mbxStatus == MBX_NOT_FINISHED)
7892 break;
7893 if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
7894 !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
7895 if (phba->sli_rev == LPFC_SLI_REV4)
7896 lpfc_issue_init_vfi(vport);
7897 else
7898 lpfc_initial_flogi(vport);
7899 } else {
7900 lpfc_initial_fdisc(vport);
7902 break;
7904 } else {
7905 spin_lock_irq(shost->host_lock);
7906 vport->vpi_state |= LPFC_VPI_REGISTERED;
7907 spin_unlock_irq(shost->host_lock);
7908 if (vport == phba->pport) {
7909 if (phba->sli_rev < LPFC_SLI_REV4)
7910 lpfc_issue_fabric_reglogin(vport);
7911 else {
7913 * If the physical port is instantiated using
7914 * FDISC, do not start vport discovery.
7916 if (vport->port_state != LPFC_FDISC)
7917 lpfc_start_fdiscs(phba);
7918 lpfc_do_scr_ns_plogi(phba, vport);
7920 } else
7921 lpfc_do_scr_ns_plogi(phba, vport);
7923 mbox_err_exit:
7924 /* Now, we decrement the ndlp reference count held for this
7925 * callback function
7927 lpfc_nlp_put(ndlp);
7929 mempool_free(pmb, phba->mbox_mem_pool);
7930 return;
7934 * lpfc_register_new_vport - Register a new vport with a HBA
7935 * @phba: pointer to lpfc hba data structure.
7936 * @vport: pointer to a host virtual N_Port data structure.
7937 * @ndlp: pointer to a node-list data structure.
7939 * This routine registers the @vport as a new virtual port with a HBA.
7940 * It is done through a registering vpi mailbox command.
7942 void
7943 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
7944 struct lpfc_nodelist *ndlp)
7946 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7947 LPFC_MBOXQ_t *mbox;
7949 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7950 if (mbox) {
7951 lpfc_reg_vpi(vport, mbox);
7952 mbox->vport = vport;
7953 mbox->context2 = lpfc_nlp_get(ndlp);
7954 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
7955 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7956 == MBX_NOT_FINISHED) {
7957 /* mailbox command not success, decrement ndlp
7958 * reference count for this command
7960 lpfc_nlp_put(ndlp);
7961 mempool_free(mbox, phba->mbox_mem_pool);
7963 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7964 "0253 Register VPI: Can't send mbox\n");
7965 goto mbox_err_exit;
7967 } else {
7968 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7969 "0254 Register VPI: no memory\n");
7970 goto mbox_err_exit;
7972 return;
7974 mbox_err_exit:
7975 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7976 spin_lock_irq(shost->host_lock);
7977 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
7978 spin_unlock_irq(shost->host_lock);
7979 return;
7983 * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
7984 * @phba: pointer to lpfc hba data structure.
7986 * This routine cancels the retry delay timers to all the vports.
7988 void
7989 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
7991 struct lpfc_vport **vports;
7992 struct lpfc_nodelist *ndlp;
7993 uint32_t link_state;
7994 int i;
7996 /* Treat this failure as linkdown for all vports */
7997 link_state = phba->link_state;
7998 lpfc_linkdown(phba);
7999 phba->link_state = link_state;
8001 vports = lpfc_create_vport_work_array(phba);
8003 if (vports) {
8004 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8005 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
8006 if (ndlp)
8007 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
8008 lpfc_els_flush_cmd(vports[i]);
8010 lpfc_destroy_vport_work_array(phba, vports);
8015 * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
8016 * @phba: pointer to lpfc hba data structure.
8018 * This routine abort all pending discovery commands and
8019 * start a timer to retry FLOGI for the physical port
8020 * discovery.
8022 void
8023 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
8025 struct lpfc_nodelist *ndlp;
8026 struct Scsi_Host *shost;
8028 /* Cancel the all vports retry delay retry timers */
8029 lpfc_cancel_all_vport_retry_delay_timer(phba);
8031 /* If fabric require FLOGI, then re-instantiate physical login */
8032 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
8033 if (!ndlp)
8034 return;
8036 shost = lpfc_shost_from_vport(phba->pport);
8037 mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
8038 spin_lock_irq(shost->host_lock);
8039 ndlp->nlp_flag |= NLP_DELAY_TMO;
8040 spin_unlock_irq(shost->host_lock);
8041 ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
8042 phba->pport->port_state = LPFC_FLOGI;
8043 return;
8047 * lpfc_fabric_login_reqd - Check if FLOGI required.
8048 * @phba: pointer to lpfc hba data structure.
8049 * @cmdiocb: pointer to FDISC command iocb.
8050 * @rspiocb: pointer to FDISC response iocb.
8052 * This routine checks if a FLOGI is reguired for FDISC
8053 * to succeed.
8055 static int
8056 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
8057 struct lpfc_iocbq *cmdiocb,
8058 struct lpfc_iocbq *rspiocb)
8061 if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
8062 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
8063 return 0;
8064 else
8065 return 1;
8069 * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
8070 * @phba: pointer to lpfc hba data structure.
8071 * @cmdiocb: pointer to lpfc command iocb data structure.
8072 * @rspiocb: pointer to lpfc response iocb data structure.
8074 * This routine is the completion callback function to a Fabric Discover
8075 * (FDISC) ELS command. Since all the FDISC ELS commands are issued
8076 * single threaded, each FDISC completion callback function will reset
8077 * the discovery timer for all vports such that the timers will not get
8078 * unnecessary timeout. The function checks the FDISC IOCB status. If error
8079 * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
8080 * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
8081 * assigned to the vport has been changed with the completion of the FDISC
8082 * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
8083 * are unregistered from the HBA, and then the lpfc_register_new_vport()
8084 * routine is invoked to register new vport with the HBA. Otherwise, the
8085 * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
8086 * Server for State Change Request (SCR).
8088 static void
8089 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8090 struct lpfc_iocbq *rspiocb)
8092 struct lpfc_vport *vport = cmdiocb->vport;
8093 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8094 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
8095 struct lpfc_nodelist *np;
8096 struct lpfc_nodelist *next_np;
8097 IOCB_t *irsp = &rspiocb->iocb;
8098 struct lpfc_iocbq *piocb;
8099 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
8100 struct serv_parm *sp;
8101 uint8_t fabric_param_changed;
8103 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8104 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
8105 irsp->ulpStatus, irsp->un.ulpWord[4],
8106 vport->fc_prevDID);
8107 /* Since all FDISCs are being single threaded, we
8108 * must reset the discovery timer for ALL vports
8109 * waiting to send FDISC when one completes.
8111 list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
8112 lpfc_set_disctmo(piocb->vport);
8115 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8116 "FDISC cmpl: status:x%x/x%x prevdid:x%x",
8117 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
8119 if (irsp->ulpStatus) {
8121 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
8122 lpfc_retry_pport_discovery(phba);
8123 goto out;
8126 /* Check for retry */
8127 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
8128 goto out;
8129 /* FDISC failed */
8130 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8131 "0126 FDISC failed. (x%x/x%x)\n",
8132 irsp->ulpStatus, irsp->un.ulpWord[4]);
8133 goto fdisc_failed;
8135 spin_lock_irq(shost->host_lock);
8136 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
8137 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
8138 vport->fc_flag |= FC_FABRIC;
8139 if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
8140 vport->fc_flag |= FC_PUBLIC_LOOP;
8141 spin_unlock_irq(shost->host_lock);
8143 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
8144 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
8145 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
8146 if (!prsp)
8147 goto out;
8148 sp = prsp->virt + sizeof(uint32_t);
8149 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
8150 memcpy(&vport->fabric_portname, &sp->portName,
8151 sizeof(struct lpfc_name));
8152 memcpy(&vport->fabric_nodename, &sp->nodeName,
8153 sizeof(struct lpfc_name));
8154 if (fabric_param_changed &&
8155 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
8156 /* If our NportID changed, we need to ensure all
8157 * remaining NPORTs get unreg_login'ed so we can
8158 * issue unreg_vpi.
8160 list_for_each_entry_safe(np, next_np,
8161 &vport->fc_nodes, nlp_listp) {
8162 if (!NLP_CHK_NODE_ACT(ndlp) ||
8163 (np->nlp_state != NLP_STE_NPR_NODE) ||
8164 !(np->nlp_flag & NLP_NPR_ADISC))
8165 continue;
8166 spin_lock_irq(shost->host_lock);
8167 np->nlp_flag &= ~NLP_NPR_ADISC;
8168 spin_unlock_irq(shost->host_lock);
8169 lpfc_unreg_rpi(vport, np);
8171 lpfc_cleanup_pending_mbox(vport);
8173 if (phba->sli_rev == LPFC_SLI_REV4)
8174 lpfc_sli4_unreg_all_rpis(vport);
8176 lpfc_mbx_unreg_vpi(vport);
8177 spin_lock_irq(shost->host_lock);
8178 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
8179 if (phba->sli_rev == LPFC_SLI_REV4)
8180 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
8181 else
8182 vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
8183 spin_unlock_irq(shost->host_lock);
8184 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
8185 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
8187 * Driver needs to re-reg VPI in order for f/w
8188 * to update the MAC address.
8190 lpfc_register_new_vport(phba, vport, ndlp);
8191 goto out;
8194 if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
8195 lpfc_issue_init_vpi(vport);
8196 else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
8197 lpfc_register_new_vport(phba, vport, ndlp);
8198 else
8199 lpfc_do_scr_ns_plogi(phba, vport);
8200 goto out;
8201 fdisc_failed:
8202 if (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS)
8203 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8204 /* Cancel discovery timer */
8205 lpfc_can_disctmo(vport);
8206 lpfc_nlp_put(ndlp);
8207 out:
8208 lpfc_els_free_iocb(phba, cmdiocb);
8212 * lpfc_issue_els_fdisc - Issue a fdisc iocb command
8213 * @vport: pointer to a virtual N_Port data structure.
8214 * @ndlp: pointer to a node-list data structure.
8215 * @retry: number of retries to the command IOCB.
8217 * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
8218 * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
8219 * routine to issue the IOCB, which makes sure only one outstanding fabric
8220 * IOCB will be sent off HBA at any given time.
8222 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
8223 * will be incremented by 1 for holding the ndlp and the reference to ndlp
8224 * will be stored into the context1 field of the IOCB for the completion
8225 * callback function to the FDISC ELS command.
8227 * Return code
8228 * 0 - Successfully issued fdisc iocb command
8229 * 1 - Failed to issue fdisc iocb command
8231 static int
8232 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
8233 uint8_t retry)
8235 struct lpfc_hba *phba = vport->phba;
8236 IOCB_t *icmd;
8237 struct lpfc_iocbq *elsiocb;
8238 struct serv_parm *sp;
8239 uint8_t *pcmd;
8240 uint16_t cmdsize;
8241 int did = ndlp->nlp_DID;
8242 int rc;
8244 vport->port_state = LPFC_FDISC;
8245 vport->fc_myDID = 0;
8246 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
8247 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
8248 ELS_CMD_FDISC);
8249 if (!elsiocb) {
8250 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8251 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8252 "0255 Issue FDISC: no IOCB\n");
8253 return 1;
8256 icmd = &elsiocb->iocb;
8257 icmd->un.elsreq64.myID = 0;
8258 icmd->un.elsreq64.fl = 1;
8261 * SLI3 ports require a different context type value than SLI4.
8262 * Catch SLI3 ports here and override the prep.
8264 if (phba->sli_rev == LPFC_SLI_REV3) {
8265 icmd->ulpCt_h = 1;
8266 icmd->ulpCt_l = 0;
8269 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8270 *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
8271 pcmd += sizeof(uint32_t); /* CSP Word 1 */
8272 memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
8273 sp = (struct serv_parm *) pcmd;
8274 /* Setup CSPs accordingly for Fabric */
8275 sp->cmn.e_d_tov = 0;
8276 sp->cmn.w2.r_a_tov = 0;
8277 sp->cmn.virtual_fabric_support = 0;
8278 sp->cls1.classValid = 0;
8279 sp->cls2.seqDelivery = 1;
8280 sp->cls3.seqDelivery = 1;
8282 pcmd += sizeof(uint32_t); /* CSP Word 2 */
8283 pcmd += sizeof(uint32_t); /* CSP Word 3 */
8284 pcmd += sizeof(uint32_t); /* CSP Word 4 */
8285 pcmd += sizeof(uint32_t); /* Port Name */
8286 memcpy(pcmd, &vport->fc_portname, 8);
8287 pcmd += sizeof(uint32_t); /* Node Name */
8288 pcmd += sizeof(uint32_t); /* Node Name */
8289 memcpy(pcmd, &vport->fc_nodename, 8);
8291 lpfc_set_disctmo(vport);
8293 phba->fc_stat.elsXmitFDISC++;
8294 elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
8296 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8297 "Issue FDISC: did:x%x",
8298 did, 0, 0);
8300 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
8301 if (rc == IOCB_ERROR) {
8302 lpfc_els_free_iocb(phba, elsiocb);
8303 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8304 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8305 "0256 Issue FDISC: Cannot send IOCB\n");
8306 return 1;
8308 lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
8309 return 0;
8313 * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
8314 * @phba: pointer to lpfc hba data structure.
8315 * @cmdiocb: pointer to lpfc command iocb data structure.
8316 * @rspiocb: pointer to lpfc response iocb data structure.
8318 * This routine is the completion callback function to the issuing of a LOGO
8319 * ELS command off a vport. It frees the command IOCB and then decrement the
8320 * reference count held on ndlp for this completion function, indicating that
8321 * the reference to the ndlp is no long needed. Note that the
8322 * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
8323 * callback function and an additional explicit ndlp reference decrementation
8324 * will trigger the actual release of the ndlp.
8326 static void
8327 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8328 struct lpfc_iocbq *rspiocb)
8330 struct lpfc_vport *vport = cmdiocb->vport;
8331 IOCB_t *irsp;
8332 struct lpfc_nodelist *ndlp;
8333 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8335 ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
8336 irsp = &rspiocb->iocb;
8337 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8338 "LOGO npiv cmpl: status:x%x/x%x did:x%x",
8339 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
8341 lpfc_els_free_iocb(phba, cmdiocb);
8342 vport->unreg_vpi_cmpl = VPORT_ERROR;
8344 /* Trigger the release of the ndlp after logo */
8345 lpfc_nlp_put(ndlp);
8347 /* NPIV LOGO completes to NPort <nlp_DID> */
8348 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8349 "2928 NPIV LOGO completes to NPort x%x "
8350 "Data: x%x x%x x%x x%x\n",
8351 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
8352 irsp->ulpTimeout, vport->num_disc_nodes);
8354 if (irsp->ulpStatus == IOSTAT_SUCCESS) {
8355 spin_lock_irq(shost->host_lock);
8356 vport->fc_flag &= ~FC_NDISC_ACTIVE;
8357 vport->fc_flag &= ~FC_FABRIC;
8358 spin_unlock_irq(shost->host_lock);
8359 lpfc_can_disctmo(vport);
8364 * lpfc_issue_els_npiv_logo - Issue a logo off a vport
8365 * @vport: pointer to a virtual N_Port data structure.
8366 * @ndlp: pointer to a node-list data structure.
8368 * This routine issues a LOGO ELS command to an @ndlp off a @vport.
8370 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
8371 * will be incremented by 1 for holding the ndlp and the reference to ndlp
8372 * will be stored into the context1 field of the IOCB for the completion
8373 * callback function to the LOGO ELS command.
8375 * Return codes
8376 * 0 - Successfully issued logo off the @vport
8377 * 1 - Failed to issue logo off the @vport
8380 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
8382 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8383 struct lpfc_hba *phba = vport->phba;
8384 struct lpfc_iocbq *elsiocb;
8385 uint8_t *pcmd;
8386 uint16_t cmdsize;
8388 cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
8389 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
8390 ELS_CMD_LOGO);
8391 if (!elsiocb)
8392 return 1;
8394 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8395 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
8396 pcmd += sizeof(uint32_t);
8398 /* Fill in LOGO payload */
8399 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
8400 pcmd += sizeof(uint32_t);
8401 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
8403 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8404 "Issue LOGO npiv did:x%x flg:x%x",
8405 ndlp->nlp_DID, ndlp->nlp_flag, 0);
8407 elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
8408 spin_lock_irq(shost->host_lock);
8409 ndlp->nlp_flag |= NLP_LOGO_SND;
8410 spin_unlock_irq(shost->host_lock);
8411 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
8412 IOCB_ERROR) {
8413 spin_lock_irq(shost->host_lock);
8414 ndlp->nlp_flag &= ~NLP_LOGO_SND;
8415 spin_unlock_irq(shost->host_lock);
8416 lpfc_els_free_iocb(phba, elsiocb);
8417 return 1;
8419 return 0;
8423 * lpfc_fabric_block_timeout - Handler function to the fabric block timer
8424 * @ptr: holder for the timer function associated data.
8426 * This routine is invoked by the fabric iocb block timer after
8427 * timeout. It posts the fabric iocb block timeout event by setting the
8428 * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
8429 * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
8430 * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
8431 * posted event WORKER_FABRIC_BLOCK_TMO.
8433 void
8434 lpfc_fabric_block_timeout(unsigned long ptr)
8436 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
8437 unsigned long iflags;
8438 uint32_t tmo_posted;
8440 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8441 tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
8442 if (!tmo_posted)
8443 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
8444 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8446 if (!tmo_posted)
8447 lpfc_worker_wake_up(phba);
8448 return;
8452 * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
8453 * @phba: pointer to lpfc hba data structure.
8455 * This routine issues one fabric iocb from the driver internal list to
8456 * the HBA. It first checks whether it's ready to issue one fabric iocb to
8457 * the HBA (whether there is no outstanding fabric iocb). If so, it shall
8458 * remove one pending fabric iocb from the driver internal list and invokes
8459 * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
8461 static void
8462 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
8464 struct lpfc_iocbq *iocb;
8465 unsigned long iflags;
8466 int ret;
8467 IOCB_t *cmd;
8469 repeat:
8470 iocb = NULL;
8471 spin_lock_irqsave(&phba->hbalock, iflags);
8472 /* Post any pending iocb to the SLI layer */
8473 if (atomic_read(&phba->fabric_iocb_count) == 0) {
8474 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
8475 list);
8476 if (iocb)
8477 /* Increment fabric iocb count to hold the position */
8478 atomic_inc(&phba->fabric_iocb_count);
8480 spin_unlock_irqrestore(&phba->hbalock, iflags);
8481 if (iocb) {
8482 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
8483 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
8484 iocb->iocb_flag |= LPFC_IO_FABRIC;
8486 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
8487 "Fabric sched1: ste:x%x",
8488 iocb->vport->port_state, 0, 0);
8490 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
8492 if (ret == IOCB_ERROR) {
8493 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
8494 iocb->fabric_iocb_cmpl = NULL;
8495 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
8496 cmd = &iocb->iocb;
8497 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
8498 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
8499 iocb->iocb_cmpl(phba, iocb, iocb);
8501 atomic_dec(&phba->fabric_iocb_count);
8502 goto repeat;
8506 return;
8510 * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
8511 * @phba: pointer to lpfc hba data structure.
8513 * This routine unblocks the issuing fabric iocb command. The function
8514 * will clear the fabric iocb block bit and then invoke the routine
8515 * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
8516 * from the driver internal fabric iocb list.
8518 void
8519 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
8521 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8523 lpfc_resume_fabric_iocbs(phba);
8524 return;
8528 * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
8529 * @phba: pointer to lpfc hba data structure.
8531 * This routine blocks the issuing fabric iocb for a specified amount of
8532 * time (currently 100 ms). This is done by set the fabric iocb block bit
8533 * and set up a timeout timer for 100ms. When the block bit is set, no more
8534 * fabric iocb will be issued out of the HBA.
8536 static void
8537 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
8539 int blocked;
8541 blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8542 /* Start a timer to unblock fabric iocbs after 100ms */
8543 if (!blocked)
8544 mod_timer(&phba->fabric_block_timer,
8545 jiffies + msecs_to_jiffies(100));
8547 return;
8551 * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
8552 * @phba: pointer to lpfc hba data structure.
8553 * @cmdiocb: pointer to lpfc command iocb data structure.
8554 * @rspiocb: pointer to lpfc response iocb data structure.
8556 * This routine is the callback function that is put to the fabric iocb's
8557 * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
8558 * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
8559 * function first restores and invokes the original iocb's callback function
8560 * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
8561 * fabric bound iocb from the driver internal fabric iocb list onto the wire.
8563 static void
8564 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8565 struct lpfc_iocbq *rspiocb)
8567 struct ls_rjt stat;
8569 if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
8570 BUG();
8572 switch (rspiocb->iocb.ulpStatus) {
8573 case IOSTAT_NPORT_RJT:
8574 case IOSTAT_FABRIC_RJT:
8575 if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
8576 lpfc_block_fabric_iocbs(phba);
8578 break;
8580 case IOSTAT_NPORT_BSY:
8581 case IOSTAT_FABRIC_BSY:
8582 lpfc_block_fabric_iocbs(phba);
8583 break;
8585 case IOSTAT_LS_RJT:
8586 stat.un.lsRjtError =
8587 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
8588 if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
8589 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
8590 lpfc_block_fabric_iocbs(phba);
8591 break;
8594 if (atomic_read(&phba->fabric_iocb_count) == 0)
8595 BUG();
8597 cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
8598 cmdiocb->fabric_iocb_cmpl = NULL;
8599 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
8600 cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
8602 atomic_dec(&phba->fabric_iocb_count);
8603 if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
8604 /* Post any pending iocbs to HBA */
8605 lpfc_resume_fabric_iocbs(phba);
8610 * lpfc_issue_fabric_iocb - Issue a fabric iocb command
8611 * @phba: pointer to lpfc hba data structure.
8612 * @iocb: pointer to lpfc command iocb data structure.
8614 * This routine is used as the top-level API for issuing a fabric iocb command
8615 * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
8616 * function makes sure that only one fabric bound iocb will be outstanding at
8617 * any given time. As such, this function will first check to see whether there
8618 * is already an outstanding fabric iocb on the wire. If so, it will put the
8619 * newly issued iocb onto the driver internal fabric iocb list, waiting to be
8620 * issued later. Otherwise, it will issue the iocb on the wire and update the
8621 * fabric iocb count it indicate that there is one fabric iocb on the wire.
8623 * Note, this implementation has a potential sending out fabric IOCBs out of
8624 * order. The problem is caused by the construction of the "ready" boolen does
8625 * not include the condition that the internal fabric IOCB list is empty. As
8626 * such, it is possible a fabric IOCB issued by this routine might be "jump"
8627 * ahead of the fabric IOCBs in the internal list.
8629 * Return code
8630 * IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
8631 * IOCB_ERROR - failed to issue fabric iocb
8633 static int
8634 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
8636 unsigned long iflags;
8637 int ready;
8638 int ret;
8640 if (atomic_read(&phba->fabric_iocb_count) > 1)
8641 BUG();
8643 spin_lock_irqsave(&phba->hbalock, iflags);
8644 ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
8645 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8647 if (ready)
8648 /* Increment fabric iocb count to hold the position */
8649 atomic_inc(&phba->fabric_iocb_count);
8650 spin_unlock_irqrestore(&phba->hbalock, iflags);
8651 if (ready) {
8652 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
8653 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
8654 iocb->iocb_flag |= LPFC_IO_FABRIC;
8656 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
8657 "Fabric sched2: ste:x%x",
8658 iocb->vport->port_state, 0, 0);
8660 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
8662 if (ret == IOCB_ERROR) {
8663 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
8664 iocb->fabric_iocb_cmpl = NULL;
8665 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
8666 atomic_dec(&phba->fabric_iocb_count);
8668 } else {
8669 spin_lock_irqsave(&phba->hbalock, iflags);
8670 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
8671 spin_unlock_irqrestore(&phba->hbalock, iflags);
8672 ret = IOCB_SUCCESS;
8674 return ret;
8678 * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
8679 * @vport: pointer to a virtual N_Port data structure.
8681 * This routine aborts all the IOCBs associated with a @vport from the
8682 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
8683 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
8684 * list, removes each IOCB associated with the @vport off the list, set the
8685 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
8686 * associated with the IOCB.
8688 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
8690 LIST_HEAD(completions);
8691 struct lpfc_hba *phba = vport->phba;
8692 struct lpfc_iocbq *tmp_iocb, *piocb;
8694 spin_lock_irq(&phba->hbalock);
8695 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
8696 list) {
8698 if (piocb->vport != vport)
8699 continue;
8701 list_move_tail(&piocb->list, &completions);
8703 spin_unlock_irq(&phba->hbalock);
8705 /* Cancel all the IOCBs from the completions list */
8706 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8707 IOERR_SLI_ABORTED);
8711 * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
8712 * @ndlp: pointer to a node-list data structure.
8714 * This routine aborts all the IOCBs associated with an @ndlp from the
8715 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
8716 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
8717 * list, removes each IOCB associated with the @ndlp off the list, set the
8718 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
8719 * associated with the IOCB.
8721 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
8723 LIST_HEAD(completions);
8724 struct lpfc_hba *phba = ndlp->phba;
8725 struct lpfc_iocbq *tmp_iocb, *piocb;
8726 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8728 spin_lock_irq(&phba->hbalock);
8729 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
8730 list) {
8731 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
8733 list_move_tail(&piocb->list, &completions);
8736 spin_unlock_irq(&phba->hbalock);
8738 /* Cancel all the IOCBs from the completions list */
8739 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8740 IOERR_SLI_ABORTED);
8744 * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
8745 * @phba: pointer to lpfc hba data structure.
8747 * This routine aborts all the IOCBs currently on the driver internal
8748 * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
8749 * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
8750 * list, removes IOCBs off the list, set the status feild to
8751 * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
8752 * the IOCB.
8754 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
8756 LIST_HEAD(completions);
8758 spin_lock_irq(&phba->hbalock);
8759 list_splice_init(&phba->fabric_iocb_list, &completions);
8760 spin_unlock_irq(&phba->hbalock);
8762 /* Cancel all the IOCBs from the completions list */
8763 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8764 IOERR_SLI_ABORTED);
8768 * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
8769 * @vport: pointer to lpfc vport data structure.
8771 * This routine is invoked by the vport cleanup for deletions and the cleanup
8772 * for an ndlp on removal.
8774 void
8775 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
8777 struct lpfc_hba *phba = vport->phba;
8778 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8779 unsigned long iflag = 0;
8781 spin_lock_irqsave(&phba->hbalock, iflag);
8782 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
8783 list_for_each_entry_safe(sglq_entry, sglq_next,
8784 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
8785 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
8786 sglq_entry->ndlp = NULL;
8788 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8789 spin_unlock_irqrestore(&phba->hbalock, iflag);
8790 return;
8794 * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
8795 * @phba: pointer to lpfc hba data structure.
8796 * @axri: pointer to the els xri abort wcqe structure.
8798 * This routine is invoked by the worker thread to process a SLI4 slow-path
8799 * ELS aborted xri.
8801 void
8802 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
8803 struct sli4_wcqe_xri_aborted *axri)
8805 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
8806 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
8807 uint16_t lxri = 0;
8809 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8810 unsigned long iflag = 0;
8811 struct lpfc_nodelist *ndlp;
8812 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8814 spin_lock_irqsave(&phba->hbalock, iflag);
8815 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
8816 list_for_each_entry_safe(sglq_entry, sglq_next,
8817 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
8818 if (sglq_entry->sli4_xritag == xri) {
8819 list_del(&sglq_entry->list);
8820 ndlp = sglq_entry->ndlp;
8821 sglq_entry->ndlp = NULL;
8822 spin_lock(&pring->ring_lock);
8823 list_add_tail(&sglq_entry->list,
8824 &phba->sli4_hba.lpfc_sgl_list);
8825 sglq_entry->state = SGL_FREED;
8826 spin_unlock(&pring->ring_lock);
8827 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8828 spin_unlock_irqrestore(&phba->hbalock, iflag);
8829 lpfc_set_rrq_active(phba, ndlp,
8830 sglq_entry->sli4_lxritag,
8831 rxid, 1);
8833 /* Check if TXQ queue needs to be serviced */
8834 if (!(list_empty(&pring->txq)))
8835 lpfc_worker_wake_up(phba);
8836 return;
8839 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8840 lxri = lpfc_sli4_xri_inrange(phba, xri);
8841 if (lxri == NO_XRI) {
8842 spin_unlock_irqrestore(&phba->hbalock, iflag);
8843 return;
8845 spin_lock(&pring->ring_lock);
8846 sglq_entry = __lpfc_get_active_sglq(phba, lxri);
8847 if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
8848 spin_unlock(&pring->ring_lock);
8849 spin_unlock_irqrestore(&phba->hbalock, iflag);
8850 return;
8852 sglq_entry->state = SGL_XRI_ABORTED;
8853 spin_unlock(&pring->ring_lock);
8854 spin_unlock_irqrestore(&phba->hbalock, iflag);
8855 return;
8858 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
8859 * @vport: pointer to virtual port object.
8860 * @ndlp: nodelist pointer for the impacted node.
8862 * The driver calls this routine in response to an SLI4 XRI ABORT CQE
8863 * or an SLI3 ASYNC_STATUS_CN event from the port. For either event,
8864 * the driver is required to send a LOGO to the remote node before it
8865 * attempts to recover its login to the remote node.
8867 void
8868 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8869 struct lpfc_nodelist *ndlp)
8871 struct Scsi_Host *shost;
8872 struct lpfc_hba *phba;
8873 unsigned long flags = 0;
8875 shost = lpfc_shost_from_vport(vport);
8876 phba = vport->phba;
8877 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8878 lpfc_printf_log(phba, KERN_INFO,
8879 LOG_SLI, "3093 No rport recovery needed. "
8880 "rport in state 0x%x\n", ndlp->nlp_state);
8881 return;
8883 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8884 "3094 Start rport recovery on shost id 0x%x "
8885 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8886 "flags 0x%x\n",
8887 shost->host_no, ndlp->nlp_DID,
8888 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8889 ndlp->nlp_flag);
8891 * The rport is not responding. Remove the FCP-2 flag to prevent
8892 * an ADISC in the follow-up recovery code.
8894 spin_lock_irqsave(shost->host_lock, flags);
8895 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8896 spin_unlock_irqrestore(shost->host_lock, flags);
8897 lpfc_issue_els_logo(vport, ndlp, 0);
8898 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);