]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/lpfc/lpfc_els.c
Merge tag 'for-linus-5.6-ofs1' of git://git.kernel.org/pub/scm/linux/kernel/git/hubca...
[linux.git] / drivers / scsi / lpfc / lpfc_els.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 /* See Fibre Channel protocol T11 FC-LS for details */
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <uapi/scsi/fc/fc_fs.h>
34 #include <uapi/scsi/fc/fc_els.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_debugfs.h"
48
49 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
50                           struct lpfc_iocbq *);
51 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
52                         struct lpfc_iocbq *);
53 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
54 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
55                                 struct lpfc_nodelist *ndlp, uint8_t retry);
56 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
57                                   struct lpfc_iocbq *iocb);
58
59 static int lpfc_max_els_tries = 3;
60
61 /**
62  * lpfc_els_chk_latt - Check host link attention event for a vport
63  * @vport: pointer to a host virtual N_Port data structure.
64  *
65  * This routine checks whether there is an outstanding host link
66  * attention event during the discovery process with the @vport. It is done
67  * by reading the HBA's Host Attention (HA) register. If there is any host
68  * link attention events during this @vport's discovery process, the @vport
69  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
70  * be issued if the link state is not already in host link cleared state,
71  * and a return code shall indicate whether the host link attention event
72  * had happened.
73  *
74  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
75  * state in LPFC_VPORT_READY, the request for checking host link attention
76  * event will be ignored and a return code shall indicate no host link
77  * attention event had happened.
78  *
79  * Return codes
80  *   0 - no host link attention event happened
81  *   1 - host link attention event happened
82  **/
83 int
84 lpfc_els_chk_latt(struct lpfc_vport *vport)
85 {
86         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
87         struct lpfc_hba  *phba = vport->phba;
88         uint32_t ha_copy;
89
90         if (vport->port_state >= LPFC_VPORT_READY ||
91             phba->link_state == LPFC_LINK_DOWN ||
92             phba->sli_rev > LPFC_SLI_REV3)
93                 return 0;
94
95         /* Read the HBA Host Attention Register */
96         if (lpfc_readl(phba->HAregaddr, &ha_copy))
97                 return 1;
98
99         if (!(ha_copy & HA_LATT))
100                 return 0;
101
102         /* Pending Link Event during Discovery */
103         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
104                          "0237 Pending Link Event during "
105                          "Discovery: State x%x\n",
106                          phba->pport->port_state);
107
108         /* CLEAR_LA should re-enable link attention events and
109          * we should then immediately take a LATT event. The
110          * LATT processing should call lpfc_linkdown() which
111          * will cleanup any left over in-progress discovery
112          * events.
113          */
114         spin_lock_irq(shost->host_lock);
115         vport->fc_flag |= FC_ABORT_DISCOVERY;
116         spin_unlock_irq(shost->host_lock);
117
118         if (phba->link_state != LPFC_CLEAR_LA)
119                 lpfc_issue_clear_la(phba, vport);
120
121         return 1;
122 }
123
124 /**
125  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
126  * @vport: pointer to a host virtual N_Port data structure.
127  * @expectRsp: flag indicating whether response is expected.
128  * @cmdSize: size of the ELS command.
129  * @retry: number of retries to the command IOCB when it fails.
130  * @ndlp: pointer to a node-list data structure.
131  * @did: destination identifier.
132  * @elscmd: the ELS command code.
133  *
134  * This routine is used for allocating a lpfc-IOCB data structure from
135  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
136  * passed into the routine for discovery state machine to issue an Extended
137  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
138  * and preparation routine that is used by all the discovery state machine
139  * routines and the ELS command-specific fields will be later set up by
140  * the individual discovery machine routines after calling this routine
141  * allocating and preparing a generic IOCB data structure. It fills in the
142  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
143  * payload and response payload (if expected). The reference count on the
144  * ndlp is incremented by 1 and the reference to the ndlp is put into
145  * context1 of the IOCB data structure for this IOCB to hold the ndlp
146  * reference for the command's callback function to access later.
147  *
148  * Return code
149  *   Pointer to the newly allocated/prepared els iocb data structure
150  *   NULL - when els iocb data structure allocation/preparation failed
151  **/
152 struct lpfc_iocbq *
153 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
154                    uint16_t cmdSize, uint8_t retry,
155                    struct lpfc_nodelist *ndlp, uint32_t did,
156                    uint32_t elscmd)
157 {
158         struct lpfc_hba  *phba = vport->phba;
159         struct lpfc_iocbq *elsiocb;
160         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
161         struct ulp_bde64 *bpl;
162         IOCB_t *icmd;
163
164
165         if (!lpfc_is_link_up(phba))
166                 return NULL;
167
168         /* Allocate buffer for  command iocb */
169         elsiocb = lpfc_sli_get_iocbq(phba);
170
171         if (elsiocb == NULL)
172                 return NULL;
173
174         /*
175          * If this command is for fabric controller and HBA running
176          * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
177          */
178         if ((did == Fabric_DID) &&
179                 (phba->hba_flag & HBA_FIP_SUPPORT) &&
180                 ((elscmd == ELS_CMD_FLOGI) ||
181                  (elscmd == ELS_CMD_FDISC) ||
182                  (elscmd == ELS_CMD_LOGO)))
183                 switch (elscmd) {
184                 case ELS_CMD_FLOGI:
185                 elsiocb->iocb_flag |=
186                         ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
187                                         & LPFC_FIP_ELS_ID_MASK);
188                 break;
189                 case ELS_CMD_FDISC:
190                 elsiocb->iocb_flag |=
191                         ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
192                                         & LPFC_FIP_ELS_ID_MASK);
193                 break;
194                 case ELS_CMD_LOGO:
195                 elsiocb->iocb_flag |=
196                         ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
197                                         & LPFC_FIP_ELS_ID_MASK);
198                 break;
199                 }
200         else
201                 elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
202
203         icmd = &elsiocb->iocb;
204
205         /* fill in BDEs for command */
206         /* Allocate buffer for command payload */
207         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
208         if (pcmd)
209                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
210         if (!pcmd || !pcmd->virt)
211                 goto els_iocb_free_pcmb_exit;
212
213         INIT_LIST_HEAD(&pcmd->list);
214
215         /* Allocate buffer for response payload */
216         if (expectRsp) {
217                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
218                 if (prsp)
219                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
220                                                      &prsp->phys);
221                 if (!prsp || !prsp->virt)
222                         goto els_iocb_free_prsp_exit;
223                 INIT_LIST_HEAD(&prsp->list);
224         } else
225                 prsp = NULL;
226
227         /* Allocate buffer for Buffer ptr list */
228         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
229         if (pbuflist)
230                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
231                                                  &pbuflist->phys);
232         if (!pbuflist || !pbuflist->virt)
233                 goto els_iocb_free_pbuf_exit;
234
235         INIT_LIST_HEAD(&pbuflist->list);
236
237         if (expectRsp) {
238                 icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
239                 icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
240                 icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
241                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
242
243                 icmd->un.elsreq64.remoteID = did;               /* DID */
244                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
245                 if (elscmd == ELS_CMD_FLOGI)
246                         icmd->ulpTimeout = FF_DEF_RATOV * 2;
247                 else if (elscmd == ELS_CMD_LOGO)
248                         icmd->ulpTimeout = phba->fc_ratov;
249                 else
250                         icmd->ulpTimeout = phba->fc_ratov * 2;
251         } else {
252                 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
253                 icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
254                 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
255                 icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
256                 icmd->un.xseq64.xmit_els_remoteID = did;        /* DID */
257                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
258         }
259         icmd->ulpBdeCount = 1;
260         icmd->ulpLe = 1;
261         icmd->ulpClass = CLASS3;
262
263         /*
264          * If we have NPIV enabled, we want to send ELS traffic by VPI.
265          * For SLI4, since the driver controls VPIs we also want to include
266          * all ELS pt2pt protocol traffic as well.
267          */
268         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
269                 ((phba->sli_rev == LPFC_SLI_REV4) &&
270                     (vport->fc_flag & FC_PT2PT))) {
271
272                 if (expectRsp) {
273                         icmd->un.elsreq64.myID = vport->fc_myDID;
274
275                         /* For ELS_REQUEST64_CR, use the VPI by default */
276                         icmd->ulpContext = phba->vpi_ids[vport->vpi];
277                 }
278
279                 icmd->ulpCt_h = 0;
280                 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
281                 if (elscmd == ELS_CMD_ECHO)
282                         icmd->ulpCt_l = 0; /* context = invalid RPI */
283                 else
284                         icmd->ulpCt_l = 1; /* context = VPI */
285         }
286
287         bpl = (struct ulp_bde64 *) pbuflist->virt;
288         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
289         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
290         bpl->tus.f.bdeSize = cmdSize;
291         bpl->tus.f.bdeFlags = 0;
292         bpl->tus.w = le32_to_cpu(bpl->tus.w);
293
294         if (expectRsp) {
295                 bpl++;
296                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
297                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
298                 bpl->tus.f.bdeSize = FCELSSIZE;
299                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
300                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
301         }
302
303         /* prevent preparing iocb with NULL ndlp reference */
304         elsiocb->context1 = lpfc_nlp_get(ndlp);
305         if (!elsiocb->context1)
306                 goto els_iocb_free_pbuf_exit;
307         elsiocb->context2 = pcmd;
308         elsiocb->context3 = pbuflist;
309         elsiocb->retry = retry;
310         elsiocb->vport = vport;
311         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
312
313         if (prsp) {
314                 list_add(&prsp->list, &pcmd->list);
315         }
316         if (expectRsp) {
317                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
318                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
319                                  "0116 Xmit ELS command x%x to remote "
320                                  "NPORT x%x I/O tag: x%x, port state:x%x "
321                                  "rpi x%x fc_flag:x%x\n",
322                                  elscmd, did, elsiocb->iotag,
323                                  vport->port_state, ndlp->nlp_rpi,
324                                  vport->fc_flag);
325         } else {
326                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
327                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
328                                  "0117 Xmit ELS response x%x to remote "
329                                  "NPORT x%x I/O tag: x%x, size: x%x "
330                                  "port_state x%x  rpi x%x fc_flag x%x\n",
331                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
332                                  cmdSize, vport->port_state,
333                                  ndlp->nlp_rpi, vport->fc_flag);
334         }
335         return elsiocb;
336
337 els_iocb_free_pbuf_exit:
338         if (expectRsp)
339                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
340         kfree(pbuflist);
341
342 els_iocb_free_prsp_exit:
343         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
344         kfree(prsp);
345
346 els_iocb_free_pcmb_exit:
347         kfree(pcmd);
348         lpfc_sli_release_iocbq(phba, elsiocb);
349         return NULL;
350 }
351
352 /**
353  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
354  * @vport: pointer to a host virtual N_Port data structure.
355  *
356  * This routine issues a fabric registration login for a @vport. An
357  * active ndlp node with Fabric_DID must already exist for this @vport.
358  * The routine invokes two mailbox commands to carry out fabric registration
359  * login through the HBA firmware: the first mailbox command requests the
360  * HBA to perform link configuration for the @vport; and the second mailbox
361  * command requests the HBA to perform the actual fabric registration login
362  * with the @vport.
363  *
364  * Return code
365  *   0 - successfully issued fabric registration login for @vport
366  *   -ENXIO -- failed to issue fabric registration login for @vport
367  **/
368 int
369 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
370 {
371         struct lpfc_hba  *phba = vport->phba;
372         LPFC_MBOXQ_t *mbox;
373         struct lpfc_dmabuf *mp;
374         struct lpfc_nodelist *ndlp;
375         struct serv_parm *sp;
376         int rc;
377         int err = 0;
378
379         sp = &phba->fc_fabparam;
380         ndlp = lpfc_findnode_did(vport, Fabric_DID);
381         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
382                 err = 1;
383                 goto fail;
384         }
385
386         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
387         if (!mbox) {
388                 err = 2;
389                 goto fail;
390         }
391
392         vport->port_state = LPFC_FABRIC_CFG_LINK;
393         lpfc_config_link(phba, mbox);
394         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
395         mbox->vport = vport;
396
397         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
398         if (rc == MBX_NOT_FINISHED) {
399                 err = 3;
400                 goto fail_free_mbox;
401         }
402
403         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
404         if (!mbox) {
405                 err = 4;
406                 goto fail;
407         }
408         rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
409                           ndlp->nlp_rpi);
410         if (rc) {
411                 err = 5;
412                 goto fail_free_mbox;
413         }
414
415         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
416         mbox->vport = vport;
417         /* increment the reference count on ndlp to hold reference
418          * for the callback routine.
419          */
420         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
421
422         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
423         if (rc == MBX_NOT_FINISHED) {
424                 err = 6;
425                 goto fail_issue_reg_login;
426         }
427
428         return 0;
429
430 fail_issue_reg_login:
431         /* decrement the reference count on ndlp just incremented
432          * for the failed mbox command.
433          */
434         lpfc_nlp_put(ndlp);
435         mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
436         lpfc_mbuf_free(phba, mp->virt, mp->phys);
437         kfree(mp);
438 fail_free_mbox:
439         mempool_free(mbox, phba->mbox_mem_pool);
440
441 fail:
442         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
443         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
444                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
445         return -ENXIO;
446 }
447
448 /**
449  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
450  * @vport: pointer to a host virtual N_Port data structure.
451  *
452  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
453  * the @vport. This mailbox command is necessary for SLI4 port only.
454  *
455  * Return code
456  *   0 - successfully issued REG_VFI for @vport
457  *   A failure code otherwise.
458  **/
459 int
460 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
461 {
462         struct lpfc_hba  *phba = vport->phba;
463         LPFC_MBOXQ_t *mboxq = NULL;
464         struct lpfc_nodelist *ndlp;
465         struct lpfc_dmabuf *dmabuf = NULL;
466         int rc = 0;
467
468         /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
469         if ((phba->sli_rev == LPFC_SLI_REV4) &&
470             !(phba->link_flag & LS_LOOPBACK_MODE) &&
471             !(vport->fc_flag & FC_PT2PT)) {
472                 ndlp = lpfc_findnode_did(vport, Fabric_DID);
473                 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
474                         rc = -ENODEV;
475                         goto fail;
476                 }
477         }
478
479         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
480         if (!mboxq) {
481                 rc = -ENOMEM;
482                 goto fail;
483         }
484
485         /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
486         if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
487                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
488                 if (!dmabuf) {
489                         rc = -ENOMEM;
490                         goto fail;
491                 }
492                 dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
493                 if (!dmabuf->virt) {
494                         rc = -ENOMEM;
495                         goto fail;
496                 }
497                 memcpy(dmabuf->virt, &phba->fc_fabparam,
498                        sizeof(struct serv_parm));
499         }
500
501         vport->port_state = LPFC_FABRIC_CFG_LINK;
502         if (dmabuf)
503                 lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
504         else
505                 lpfc_reg_vfi(mboxq, vport, 0);
506
507         mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
508         mboxq->vport = vport;
509         mboxq->ctx_buf = dmabuf;
510         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
511         if (rc == MBX_NOT_FINISHED) {
512                 rc = -ENXIO;
513                 goto fail;
514         }
515         return 0;
516
517 fail:
518         if (mboxq)
519                 mempool_free(mboxq, phba->mbox_mem_pool);
520         if (dmabuf) {
521                 if (dmabuf->virt)
522                         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
523                 kfree(dmabuf);
524         }
525
526         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
527         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
528                 "0289 Issue Register VFI failed: Err %d\n", rc);
529         return rc;
530 }
531
532 /**
533  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
534  * @vport: pointer to a host virtual N_Port data structure.
535  *
536  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
537  * the @vport. This mailbox command is necessary for SLI4 port only.
538  *
539  * Return code
540  *   0 - successfully issued REG_VFI for @vport
541  *   A failure code otherwise.
542  **/
543 int
544 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
545 {
546         struct lpfc_hba *phba = vport->phba;
547         struct Scsi_Host *shost;
548         LPFC_MBOXQ_t *mboxq;
549         int rc;
550
551         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
552         if (!mboxq) {
553                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
554                                 "2556 UNREG_VFI mbox allocation failed"
555                                 "HBA state x%x\n", phba->pport->port_state);
556                 return -ENOMEM;
557         }
558
559         lpfc_unreg_vfi(mboxq, vport);
560         mboxq->vport = vport;
561         mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
562
563         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
564         if (rc == MBX_NOT_FINISHED) {
565                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
566                                 "2557 UNREG_VFI issue mbox failed rc x%x "
567                                 "HBA state x%x\n",
568                                 rc, phba->pport->port_state);
569                 mempool_free(mboxq, phba->mbox_mem_pool);
570                 return -EIO;
571         }
572
573         shost = lpfc_shost_from_vport(vport);
574         spin_lock_irq(shost->host_lock);
575         vport->fc_flag &= ~FC_VFI_REGISTERED;
576         spin_unlock_irq(shost->host_lock);
577         return 0;
578 }
579
580 /**
581  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
582  * @vport: pointer to a host virtual N_Port data structure.
583  * @sp: pointer to service parameter data structure.
584  *
585  * This routine is called from FLOGI/FDISC completion handler functions.
586  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
587  * node nodename is changed in the completion service parameter else return
588  * 0. This function also set flag in the vport data structure to delay
589  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
590  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
591  * node nodename is changed in the completion service parameter.
592  *
593  * Return code
594  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
595  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
596  *
597  **/
598 static uint8_t
599 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
600                 struct serv_parm *sp)
601 {
602         struct lpfc_hba *phba = vport->phba;
603         uint8_t fabric_param_changed = 0;
604         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
605
606         if ((vport->fc_prevDID != vport->fc_myDID) ||
607                 memcmp(&vport->fabric_portname, &sp->portName,
608                         sizeof(struct lpfc_name)) ||
609                 memcmp(&vport->fabric_nodename, &sp->nodeName,
610                         sizeof(struct lpfc_name)) ||
611                 (vport->vport_flag & FAWWPN_PARAM_CHG)) {
612                 fabric_param_changed = 1;
613                 vport->vport_flag &= ~FAWWPN_PARAM_CHG;
614         }
615         /*
616          * Word 1 Bit 31 in common service parameter is overloaded.
617          * Word 1 Bit 31 in FLOGI request is multiple NPort request
618          * Word 1 Bit 31 in FLOGI response is clean address bit
619          *
620          * If fabric parameter is changed and clean address bit is
621          * cleared delay nport discovery if
622          * - vport->fc_prevDID != 0 (not initial discovery) OR
623          * - lpfc_delay_discovery module parameter is set.
624          */
625         if (fabric_param_changed && !sp->cmn.clean_address_bit &&
626             (vport->fc_prevDID || phba->cfg_delay_discovery)) {
627                 spin_lock_irq(shost->host_lock);
628                 vport->fc_flag |= FC_DISC_DELAYED;
629                 spin_unlock_irq(shost->host_lock);
630         }
631
632         return fabric_param_changed;
633 }
634
635
636 /**
637  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
638  * @vport: pointer to a host virtual N_Port data structure.
639  * @ndlp: pointer to a node-list data structure.
640  * @sp: pointer to service parameter data structure.
641  * @irsp: pointer to the IOCB within the lpfc response IOCB.
642  *
643  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
644  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
645  * port in a fabric topology. It properly sets up the parameters to the @ndlp
646  * from the IOCB response. It also check the newly assigned N_Port ID to the
647  * @vport against the previously assigned N_Port ID. If it is different from
648  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
649  * is invoked on all the remaining nodes with the @vport to unregister the
650  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
651  * is invoked to register login to the fabric.
652  *
653  * Return code
654  *   0 - Success (currently, always return 0)
655  **/
656 static int
657 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
658                            struct serv_parm *sp, IOCB_t *irsp)
659 {
660         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
661         struct lpfc_hba  *phba = vport->phba;
662         struct lpfc_nodelist *np;
663         struct lpfc_nodelist *next_np;
664         uint8_t fabric_param_changed;
665
666         spin_lock_irq(shost->host_lock);
667         vport->fc_flag |= FC_FABRIC;
668         spin_unlock_irq(shost->host_lock);
669
670         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
671         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
672                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
673
674         phba->fc_edtovResol = sp->cmn.edtovResolution;
675         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
676
677         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
678                 spin_lock_irq(shost->host_lock);
679                 vport->fc_flag |= FC_PUBLIC_LOOP;
680                 spin_unlock_irq(shost->host_lock);
681         }
682
683         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
684         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
685         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
686         ndlp->nlp_class_sup = 0;
687         if (sp->cls1.classValid)
688                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
689         if (sp->cls2.classValid)
690                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
691         if (sp->cls3.classValid)
692                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
693         if (sp->cls4.classValid)
694                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
695         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
696                                 sp->cmn.bbRcvSizeLsb;
697
698         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
699         if (fabric_param_changed) {
700                 /* Reset FDMI attribute masks based on config parameter */
701                 if (phba->cfg_enable_SmartSAN ||
702                     (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
703                         /* Setup appropriate attribute masks */
704                         vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
705                         if (phba->cfg_enable_SmartSAN)
706                                 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
707                         else
708                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
709                 } else {
710                         vport->fdmi_hba_mask = 0;
711                         vport->fdmi_port_mask = 0;
712                 }
713
714         }
715         memcpy(&vport->fabric_portname, &sp->portName,
716                         sizeof(struct lpfc_name));
717         memcpy(&vport->fabric_nodename, &sp->nodeName,
718                         sizeof(struct lpfc_name));
719         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
720
721         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
722                 if (sp->cmn.response_multiple_NPort) {
723                         lpfc_printf_vlog(vport, KERN_WARNING,
724                                          LOG_ELS | LOG_VPORT,
725                                          "1816 FLOGI NPIV supported, "
726                                          "response data 0x%x\n",
727                                          sp->cmn.response_multiple_NPort);
728                         spin_lock_irq(&phba->hbalock);
729                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
730                         spin_unlock_irq(&phba->hbalock);
731                 } else {
732                         /* Because we asked f/w for NPIV it still expects us
733                         to call reg_vnpid atleast for the physcial host */
734                         lpfc_printf_vlog(vport, KERN_WARNING,
735                                          LOG_ELS | LOG_VPORT,
736                                          "1817 Fabric does not support NPIV "
737                                          "- configuring single port mode.\n");
738                         spin_lock_irq(&phba->hbalock);
739                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
740                         spin_unlock_irq(&phba->hbalock);
741                 }
742         }
743
744         /*
745          * For FC we need to do some special processing because of the SLI
746          * Port's default settings of the Common Service Parameters.
747          */
748         if ((phba->sli_rev == LPFC_SLI_REV4) &&
749             (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
750                 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
751                 if (fabric_param_changed)
752                         lpfc_unregister_fcf_prep(phba);
753
754                 /* This should just update the VFI CSPs*/
755                 if (vport->fc_flag & FC_VFI_REGISTERED)
756                         lpfc_issue_reg_vfi(vport);
757         }
758
759         if (fabric_param_changed &&
760                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
761
762                 /* If our NportID changed, we need to ensure all
763                  * remaining NPORTs get unreg_login'ed.
764                  */
765                 list_for_each_entry_safe(np, next_np,
766                                         &vport->fc_nodes, nlp_listp) {
767                         if (!NLP_CHK_NODE_ACT(np))
768                                 continue;
769                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
770                                    !(np->nlp_flag & NLP_NPR_ADISC))
771                                 continue;
772                         spin_lock_irq(shost->host_lock);
773                         np->nlp_flag &= ~NLP_NPR_ADISC;
774                         spin_unlock_irq(shost->host_lock);
775                         lpfc_unreg_rpi(vport, np);
776                 }
777                 lpfc_cleanup_pending_mbox(vport);
778
779                 if (phba->sli_rev == LPFC_SLI_REV4) {
780                         lpfc_sli4_unreg_all_rpis(vport);
781                         lpfc_mbx_unreg_vpi(vport);
782                         spin_lock_irq(shost->host_lock);
783                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
784                         spin_unlock_irq(shost->host_lock);
785                 }
786
787                 /*
788                  * For SLI3 and SLI4, the VPI needs to be reregistered in
789                  * response to this fabric parameter change event.
790                  */
791                 spin_lock_irq(shost->host_lock);
792                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
793                 spin_unlock_irq(shost->host_lock);
794         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
795                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
796                         /*
797                          * Driver needs to re-reg VPI in order for f/w
798                          * to update the MAC address.
799                          */
800                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
801                         lpfc_register_new_vport(phba, vport, ndlp);
802                         return 0;
803         }
804
805         if (phba->sli_rev < LPFC_SLI_REV4) {
806                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
807                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
808                     vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
809                         lpfc_register_new_vport(phba, vport, ndlp);
810                 else
811                         lpfc_issue_fabric_reglogin(vport);
812         } else {
813                 ndlp->nlp_type |= NLP_FABRIC;
814                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
815                 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
816                         (vport->vpi_state & LPFC_VPI_REGISTERED)) {
817                         lpfc_start_fdiscs(phba);
818                         lpfc_do_scr_ns_plogi(phba, vport);
819                 } else if (vport->fc_flag & FC_VFI_REGISTERED)
820                         lpfc_issue_init_vpi(vport);
821                 else {
822                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
823                                         "3135 Need register VFI: (x%x/%x)\n",
824                                         vport->fc_prevDID, vport->fc_myDID);
825                         lpfc_issue_reg_vfi(vport);
826                 }
827         }
828         return 0;
829 }
830
831 /**
832  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
833  * @vport: pointer to a host virtual N_Port data structure.
834  * @ndlp: pointer to a node-list data structure.
835  * @sp: pointer to service parameter data structure.
836  *
837  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
838  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
839  * in a point-to-point topology. First, the @vport's N_Port Name is compared
840  * with the received N_Port Name: if the @vport's N_Port Name is greater than
841  * the received N_Port Name lexicographically, this node shall assign local
842  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
843  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
844  * this node shall just wait for the remote node to issue PLOGI and assign
845  * N_Port IDs.
846  *
847  * Return code
848  *   0 - Success
849  *   -ENXIO - Fail
850  **/
851 static int
852 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
853                           struct serv_parm *sp)
854 {
855         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
856         struct lpfc_hba  *phba = vport->phba;
857         LPFC_MBOXQ_t *mbox;
858         int rc;
859
860         spin_lock_irq(shost->host_lock);
861         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
862         vport->fc_flag |= FC_PT2PT;
863         spin_unlock_irq(shost->host_lock);
864
865         /* If we are pt2pt with another NPort, force NPIV off! */
866         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
867
868         /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
869         if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
870                 lpfc_unregister_fcf_prep(phba);
871
872                 spin_lock_irq(shost->host_lock);
873                 vport->fc_flag &= ~FC_VFI_REGISTERED;
874                 spin_unlock_irq(shost->host_lock);
875                 phba->fc_topology_changed = 0;
876         }
877
878         rc = memcmp(&vport->fc_portname, &sp->portName,
879                     sizeof(vport->fc_portname));
880
881         if (rc >= 0) {
882                 /* This side will initiate the PLOGI */
883                 spin_lock_irq(shost->host_lock);
884                 vport->fc_flag |= FC_PT2PT_PLOGI;
885                 spin_unlock_irq(shost->host_lock);
886
887                 /*
888                  * N_Port ID cannot be 0, set our Id to LocalID
889                  * the other side will be RemoteID.
890                  */
891
892                 /* not equal */
893                 if (rc)
894                         vport->fc_myDID = PT2PT_LocalID;
895
896                 /* Decrement ndlp reference count indicating that ndlp can be
897                  * safely released when other references to it are done.
898                  */
899                 lpfc_nlp_put(ndlp);
900
901                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
902                 if (!ndlp) {
903                         /*
904                          * Cannot find existing Fabric ndlp, so allocate a
905                          * new one
906                          */
907                         ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
908                         if (!ndlp)
909                                 goto fail;
910                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
911                         ndlp = lpfc_enable_node(vport, ndlp,
912                                                 NLP_STE_UNUSED_NODE);
913                         if(!ndlp)
914                                 goto fail;
915                 }
916
917                 memcpy(&ndlp->nlp_portname, &sp->portName,
918                        sizeof(struct lpfc_name));
919                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
920                        sizeof(struct lpfc_name));
921                 /* Set state will put ndlp onto node list if not already done */
922                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
923                 spin_lock_irq(shost->host_lock);
924                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
925                 spin_unlock_irq(shost->host_lock);
926
927                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
928                 if (!mbox)
929                         goto fail;
930
931                 lpfc_config_link(phba, mbox);
932
933                 mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
934                 mbox->vport = vport;
935                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
936                 if (rc == MBX_NOT_FINISHED) {
937                         mempool_free(mbox, phba->mbox_mem_pool);
938                         goto fail;
939                 }
940         } else {
941                 /* This side will wait for the PLOGI, decrement ndlp reference
942                  * count indicating that ndlp can be released when other
943                  * references to it are done.
944                  */
945                 lpfc_nlp_put(ndlp);
946
947                 /* Start discovery - this should just do CLEAR_LA */
948                 lpfc_disc_start(vport);
949         }
950
951         return 0;
952 fail:
953         return -ENXIO;
954 }
955
956 /**
957  * lpfc_cmpl_els_flogi - Completion callback function for flogi
958  * @phba: pointer to lpfc hba data structure.
959  * @cmdiocb: pointer to lpfc command iocb data structure.
960  * @rspiocb: pointer to lpfc response iocb data structure.
961  *
962  * This routine is the top-level completion callback function for issuing
963  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
964  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
965  * retry has been made (either immediately or delayed with lpfc_els_retry()
966  * returning 1), the command IOCB will be released and function returned.
967  * If the retry attempt has been given up (possibly reach the maximum
968  * number of retries), one additional decrement of ndlp reference shall be
969  * invoked before going out after releasing the command IOCB. This will
970  * actually release the remote node (Note, lpfc_els_free_iocb() will also
971  * invoke one decrement of ndlp reference count). If no error reported in
972  * the IOCB status, the command Port ID field is used to determine whether
973  * this is a point-to-point topology or a fabric topology: if the Port ID
974  * field is assigned, it is a fabric topology; otherwise, it is a
975  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
976  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
977  * specific topology completion conditions.
978  **/
979 static void
980 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
981                     struct lpfc_iocbq *rspiocb)
982 {
983         struct lpfc_vport *vport = cmdiocb->vport;
984         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
985         IOCB_t *irsp = &rspiocb->iocb;
986         struct lpfc_nodelist *ndlp = cmdiocb->context1;
987         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
988         struct serv_parm *sp;
989         uint16_t fcf_index;
990         int rc;
991
992         /* Check to see if link went down during discovery */
993         if (lpfc_els_chk_latt(vport)) {
994                 /* One additional decrement on node reference count to
995                  * trigger the release of the node
996                  */
997                 lpfc_nlp_put(ndlp);
998                 goto out;
999         }
1000
1001         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1002                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
1003                 irsp->ulpStatus, irsp->un.ulpWord[4],
1004                 vport->port_state);
1005
1006         if (irsp->ulpStatus) {
1007                 /*
1008                  * In case of FIP mode, perform roundrobin FCF failover
1009                  * due to new FCF discovery
1010                  */
1011                 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
1012                     (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
1013                         if (phba->link_state < LPFC_LINK_UP)
1014                                 goto stop_rr_fcf_flogi;
1015                         if ((phba->fcoe_cvl_eventtag_attn ==
1016                              phba->fcoe_cvl_eventtag) &&
1017                             (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1018                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1019                             IOERR_SLI_ABORTED))
1020                                 goto stop_rr_fcf_flogi;
1021                         else
1022                                 phba->fcoe_cvl_eventtag_attn =
1023                                         phba->fcoe_cvl_eventtag;
1024                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1025                                         "2611 FLOGI failed on FCF (x%x), "
1026                                         "status:x%x/x%x, tmo:x%x, perform "
1027                                         "roundrobin FCF failover\n",
1028                                         phba->fcf.current_rec.fcf_indx,
1029                                         irsp->ulpStatus, irsp->un.ulpWord[4],
1030                                         irsp->ulpTimeout);
1031                         lpfc_sli4_set_fcf_flogi_fail(phba,
1032                                         phba->fcf.current_rec.fcf_indx);
1033                         fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1034                         rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1035                         if (rc)
1036                                 goto out;
1037                 }
1038
1039 stop_rr_fcf_flogi:
1040                 /* FLOGI failure */
1041                 if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1042                       ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1043                                         IOERR_LOOP_OPEN_FAILURE)))
1044                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1045                                         "2858 FLOGI failure Status:x%x/x%x "
1046                                         "TMO:x%x Data x%x x%x\n",
1047                                         irsp->ulpStatus, irsp->un.ulpWord[4],
1048                                         irsp->ulpTimeout, phba->hba_flag,
1049                                         phba->fcf.fcf_flag);
1050
1051                 /* Check for retry */
1052                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1053                         goto out;
1054
1055                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
1056                                  "0150 FLOGI failure Status:x%x/x%x "
1057                                  "xri x%x TMO:x%x\n",
1058                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1059                                  cmdiocb->sli4_xritag, irsp->ulpTimeout);
1060
1061                 /* If this is not a loop open failure, bail out */
1062                 if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1063                       ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1064                                         IOERR_LOOP_OPEN_FAILURE)))
1065                         goto flogifail;
1066
1067                 /* FLOGI failed, so there is no fabric */
1068                 spin_lock_irq(shost->host_lock);
1069                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1070                 spin_unlock_irq(shost->host_lock);
1071
1072                 /* If private loop, then allow max outstanding els to be
1073                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1074                  * alpa map would take too long otherwise.
1075                  */
1076                 if (phba->alpa_map[0] == 0)
1077                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1078                 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1079                     (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1080                      (vport->fc_prevDID != vport->fc_myDID) ||
1081                         phba->fc_topology_changed)) {
1082                         if (vport->fc_flag & FC_VFI_REGISTERED) {
1083                                 if (phba->fc_topology_changed) {
1084                                         lpfc_unregister_fcf_prep(phba);
1085                                         spin_lock_irq(shost->host_lock);
1086                                         vport->fc_flag &= ~FC_VFI_REGISTERED;
1087                                         spin_unlock_irq(shost->host_lock);
1088                                         phba->fc_topology_changed = 0;
1089                                 } else {
1090                                         lpfc_sli4_unreg_all_rpis(vport);
1091                                 }
1092                         }
1093
1094                         /* Do not register VFI if the driver aborted FLOGI */
1095                         if (!lpfc_error_lost_link(irsp))
1096                                 lpfc_issue_reg_vfi(vport);
1097                         lpfc_nlp_put(ndlp);
1098                         goto out;
1099                 }
1100                 goto flogifail;
1101         }
1102         spin_lock_irq(shost->host_lock);
1103         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1104         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1105         spin_unlock_irq(shost->host_lock);
1106
1107         /*
1108          * The FLogI succeeded.  Sync the data for the CPU before
1109          * accessing it.
1110          */
1111         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1112         if (!prsp)
1113                 goto out;
1114         sp = prsp->virt + sizeof(uint32_t);
1115
1116         /* FLOGI completes successfully */
1117         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1118                          "0101 FLOGI completes successfully, I/O tag:x%x, "
1119                          "xri x%x Data: x%x x%x x%x x%x x%x %x\n",
1120                          cmdiocb->iotag, cmdiocb->sli4_xritag,
1121                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1122                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1123                          vport->port_state, vport->fc_flag);
1124
1125         if (vport->port_state == LPFC_FLOGI) {
1126                 /*
1127                  * If Common Service Parameters indicate Nport
1128                  * we are point to point, if Fport we are Fabric.
1129                  */
1130                 if (sp->cmn.fPort)
1131                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1132                 else if (!(phba->hba_flag & HBA_FCOE_MODE))
1133                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1134                 else {
1135                         lpfc_printf_vlog(vport, KERN_ERR,
1136                                 LOG_FIP | LOG_ELS,
1137                                 "2831 FLOGI response with cleared Fabric "
1138                                 "bit fcf_index 0x%x "
1139                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1140                                 "Fabric Name "
1141                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1142                                 phba->fcf.current_rec.fcf_indx,
1143                                 phba->fcf.current_rec.switch_name[0],
1144                                 phba->fcf.current_rec.switch_name[1],
1145                                 phba->fcf.current_rec.switch_name[2],
1146                                 phba->fcf.current_rec.switch_name[3],
1147                                 phba->fcf.current_rec.switch_name[4],
1148                                 phba->fcf.current_rec.switch_name[5],
1149                                 phba->fcf.current_rec.switch_name[6],
1150                                 phba->fcf.current_rec.switch_name[7],
1151                                 phba->fcf.current_rec.fabric_name[0],
1152                                 phba->fcf.current_rec.fabric_name[1],
1153                                 phba->fcf.current_rec.fabric_name[2],
1154                                 phba->fcf.current_rec.fabric_name[3],
1155                                 phba->fcf.current_rec.fabric_name[4],
1156                                 phba->fcf.current_rec.fabric_name[5],
1157                                 phba->fcf.current_rec.fabric_name[6],
1158                                 phba->fcf.current_rec.fabric_name[7]);
1159                         lpfc_nlp_put(ndlp);
1160                         spin_lock_irq(&phba->hbalock);
1161                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1162                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1163                         spin_unlock_irq(&phba->hbalock);
1164                         phba->fcf.fcf_redisc_attempted = 0; /* reset */
1165                         goto out;
1166                 }
1167                 if (!rc) {
1168                         /* Mark the FCF discovery process done */
1169                         if (phba->hba_flag & HBA_FIP_SUPPORT)
1170                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1171                                                 LOG_ELS,
1172                                                 "2769 FLOGI to FCF (x%x) "
1173                                                 "completed successfully\n",
1174                                                 phba->fcf.current_rec.fcf_indx);
1175                         spin_lock_irq(&phba->hbalock);
1176                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1177                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1178                         spin_unlock_irq(&phba->hbalock);
1179                         phba->fcf.fcf_redisc_attempted = 0; /* reset */
1180                         goto out;
1181                 }
1182         }
1183
1184 flogifail:
1185         spin_lock_irq(&phba->hbalock);
1186         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1187         spin_unlock_irq(&phba->hbalock);
1188
1189         lpfc_nlp_put(ndlp);
1190
1191         if (!lpfc_error_lost_link(irsp)) {
1192                 /* FLOGI failed, so just use loop map to make discovery list */
1193                 lpfc_disc_list_loopmap(vport);
1194
1195                 /* Start discovery */
1196                 lpfc_disc_start(vport);
1197         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1198                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1199                          IOERR_SLI_ABORTED) &&
1200                         ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1201                          IOERR_SLI_DOWN))) &&
1202                         (phba->link_state != LPFC_CLEAR_LA)) {
1203                 /* If FLOGI failed enable link interrupt. */
1204                 lpfc_issue_clear_la(phba, vport);
1205         }
1206 out:
1207         lpfc_els_free_iocb(phba, cmdiocb);
1208 }
1209
1210 /**
1211  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1212  *                           aborted during a link down
1213  * @phba: pointer to lpfc hba data structure.
1214  * @cmdiocb: pointer to lpfc command iocb data structure.
1215  * @rspiocb: pointer to lpfc response iocb data structure.
1216  *
1217  */
1218 static void
1219 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1220                         struct lpfc_iocbq *rspiocb)
1221 {
1222         IOCB_t *irsp;
1223         uint32_t *pcmd;
1224         uint32_t cmd;
1225
1226         pcmd = (uint32_t *)(((struct lpfc_dmabuf *)cmdiocb->context2)->virt);
1227         cmd = *pcmd;
1228         irsp = &rspiocb->iocb;
1229
1230         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1231                         "6445 ELS completes after LINK_DOWN: "
1232                         " Status %x/%x cmd x%x flg x%x\n",
1233                         irsp->ulpStatus, irsp->un.ulpWord[4], cmd,
1234                         cmdiocb->iocb_flag);
1235
1236         if (cmdiocb->iocb_flag & LPFC_IO_FABRIC) {
1237                 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
1238                 atomic_dec(&phba->fabric_iocb_count);
1239         }
1240         lpfc_els_free_iocb(phba, cmdiocb);
1241 }
1242
1243 /**
1244  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1245  * @vport: pointer to a host virtual N_Port data structure.
1246  * @ndlp: pointer to a node-list data structure.
1247  * @retry: number of retries to the command IOCB.
1248  *
1249  * This routine issues a Fabric Login (FLOGI) Request ELS command
1250  * for a @vport. The initiator service parameters are put into the payload
1251  * of the FLOGI Request IOCB and the top-level callback function pointer
1252  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1253  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1254  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1255  *
1256  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1257  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1258  * will be stored into the context1 field of the IOCB for the completion
1259  * callback function to the FLOGI ELS command.
1260  *
1261  * Return code
1262  *   0 - successfully issued flogi iocb for @vport
1263  *   1 - failed to issue flogi iocb for @vport
1264  **/
1265 static int
1266 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1267                      uint8_t retry)
1268 {
1269         struct lpfc_hba  *phba = vport->phba;
1270         struct serv_parm *sp;
1271         IOCB_t *icmd;
1272         struct lpfc_iocbq *elsiocb;
1273         struct lpfc_iocbq defer_flogi_acc;
1274         uint8_t *pcmd;
1275         uint16_t cmdsize;
1276         uint32_t tmo, did;
1277         int rc;
1278
1279         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1280         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1281                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1282
1283         if (!elsiocb)
1284                 return 1;
1285
1286         icmd = &elsiocb->iocb;
1287         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1288
1289         /* For FLOGI request, remainder of payload is service parameters */
1290         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1291         pcmd += sizeof(uint32_t);
1292         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1293         sp = (struct serv_parm *) pcmd;
1294
1295         /* Setup CSPs accordingly for Fabric */
1296         sp->cmn.e_d_tov = 0;
1297         sp->cmn.w2.r_a_tov = 0;
1298         sp->cmn.virtual_fabric_support = 0;
1299         sp->cls1.classValid = 0;
1300         if (sp->cmn.fcphLow < FC_PH3)
1301                 sp->cmn.fcphLow = FC_PH3;
1302         if (sp->cmn.fcphHigh < FC_PH3)
1303                 sp->cmn.fcphHigh = FC_PH3;
1304
1305         if  (phba->sli_rev == LPFC_SLI_REV4) {
1306                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1307                     LPFC_SLI_INTF_IF_TYPE_0) {
1308                         elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1309                         elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1310                         /* FLOGI needs to be 3 for WQE FCFI */
1311                         /* Set the fcfi to the fcfi we registered with */
1312                         elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1313                 }
1314                 /* Can't do SLI4 class2 without support sequence coalescing */
1315                 sp->cls2.classValid = 0;
1316                 sp->cls2.seqDelivery = 0;
1317         } else {
1318                 /* Historical, setting sequential-delivery bit for SLI3 */
1319                 sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1320                 sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1321                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1322                         sp->cmn.request_multiple_Nport = 1;
1323                         /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1324                         icmd->ulpCt_h = 1;
1325                         icmd->ulpCt_l = 0;
1326                 } else
1327                         sp->cmn.request_multiple_Nport = 0;
1328         }
1329
1330         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1331                 icmd->un.elsreq64.myID = 0;
1332                 icmd->un.elsreq64.fl = 1;
1333         }
1334
1335         tmo = phba->fc_ratov;
1336         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1337         lpfc_set_disctmo(vport);
1338         phba->fc_ratov = tmo;
1339
1340         phba->fc_stat.elsXmitFLOGI++;
1341         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1342
1343         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1344                 "Issue FLOGI:     opt:x%x",
1345                 phba->sli3_options, 0, 0);
1346
1347         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1348
1349         phba->hba_flag |= HBA_FLOGI_ISSUED;
1350
1351         /* Check for a deferred FLOGI ACC condition */
1352         if (phba->defer_flogi_acc_flag) {
1353                 did = vport->fc_myDID;
1354                 vport->fc_myDID = Fabric_DID;
1355
1356                 memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1357
1358                 defer_flogi_acc.iocb.ulpContext = phba->defer_flogi_acc_rx_id;
1359                 defer_flogi_acc.iocb.unsli3.rcvsli3.ox_id =
1360                                                 phba->defer_flogi_acc_ox_id;
1361
1362                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1363                                  "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1364                                  " ox_id: x%x, hba_flag x%x\n",
1365                                  phba->defer_flogi_acc_rx_id,
1366                                  phba->defer_flogi_acc_ox_id, phba->hba_flag);
1367
1368                 /* Send deferred FLOGI ACC */
1369                 lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1370                                  ndlp, NULL);
1371
1372                 phba->defer_flogi_acc_flag = false;
1373
1374                 vport->fc_myDID = did;
1375         }
1376
1377         if (rc == IOCB_ERROR) {
1378                 lpfc_els_free_iocb(phba, elsiocb);
1379                 return 1;
1380         }
1381         return 0;
1382 }
1383
1384 /**
1385  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1386  * @phba: pointer to lpfc hba data structure.
1387  *
1388  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1389  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1390  * list and issues an abort IOCB commond on each outstanding IOCB that
1391  * contains a active Fabric_DID ndlp. Note that this function is to issue
1392  * the abort IOCB command on all the outstanding IOCBs, thus when this
1393  * function returns, it does not guarantee all the IOCBs are actually aborted.
1394  *
1395  * Return code
1396  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1397  **/
1398 int
1399 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1400 {
1401         struct lpfc_sli_ring *pring;
1402         struct lpfc_iocbq *iocb, *next_iocb;
1403         struct lpfc_nodelist *ndlp;
1404         IOCB_t *icmd;
1405
1406         /* Abort outstanding I/O on NPort <nlp_DID> */
1407         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1408                         "0201 Abort outstanding I/O on NPort x%x\n",
1409                         Fabric_DID);
1410
1411         pring = lpfc_phba_elsring(phba);
1412         if (unlikely(!pring))
1413                 return -EIO;
1414
1415         /*
1416          * Check the txcmplq for an iocb that matches the nport the driver is
1417          * searching for.
1418          */
1419         spin_lock_irq(&phba->hbalock);
1420         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1421                 icmd = &iocb->iocb;
1422                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1423                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
1424                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1425                             (ndlp->nlp_DID == Fabric_DID))
1426                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1427                 }
1428         }
1429         spin_unlock_irq(&phba->hbalock);
1430
1431         return 0;
1432 }
1433
1434 /**
1435  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1436  * @vport: pointer to a host virtual N_Port data structure.
1437  *
1438  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1439  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1440  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1441  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1442  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1443  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1444  * @vport.
1445  *
1446  * Return code
1447  *   0 - failed to issue initial flogi for @vport
1448  *   1 - successfully issued initial flogi for @vport
1449  **/
1450 int
1451 lpfc_initial_flogi(struct lpfc_vport *vport)
1452 {
1453         struct lpfc_nodelist *ndlp;
1454
1455         vport->port_state = LPFC_FLOGI;
1456         lpfc_set_disctmo(vport);
1457
1458         /* First look for the Fabric ndlp */
1459         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1460         if (!ndlp) {
1461                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1462                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1463                 if (!ndlp)
1464                         return 0;
1465                 /* Set the node type */
1466                 ndlp->nlp_type |= NLP_FABRIC;
1467                 /* Put ndlp onto node list */
1468                 lpfc_enqueue_node(vport, ndlp);
1469         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1470                 /* re-setup ndlp without removing from node list */
1471                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1472                 if (!ndlp)
1473                         return 0;
1474         }
1475
1476         if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1477                 /* This decrement of reference count to node shall kick off
1478                  * the release of the node.
1479                  */
1480                 lpfc_nlp_put(ndlp);
1481                 return 0;
1482         }
1483         return 1;
1484 }
1485
1486 /**
1487  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1488  * @vport: pointer to a host virtual N_Port data structure.
1489  *
1490  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1491  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1492  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1493  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1494  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1495  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1496  * @vport.
1497  *
1498  * Return code
1499  *   0 - failed to issue initial fdisc for @vport
1500  *   1 - successfully issued initial fdisc for @vport
1501  **/
1502 int
1503 lpfc_initial_fdisc(struct lpfc_vport *vport)
1504 {
1505         struct lpfc_nodelist *ndlp;
1506
1507         /* First look for the Fabric ndlp */
1508         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1509         if (!ndlp) {
1510                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1511                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1512                 if (!ndlp)
1513                         return 0;
1514                 /* Put ndlp onto node list */
1515                 lpfc_enqueue_node(vport, ndlp);
1516         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1517                 /* re-setup ndlp without removing from node list */
1518                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1519                 if (!ndlp)
1520                         return 0;
1521         }
1522
1523         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1524                 /* decrement node reference count to trigger the release of
1525                  * the node.
1526                  */
1527                 lpfc_nlp_put(ndlp);
1528                 return 0;
1529         }
1530         return 1;
1531 }
1532
1533 /**
1534  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1535  * @vport: pointer to a host virtual N_Port data structure.
1536  *
1537  * This routine checks whether there are more remaining Port Logins
1538  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1539  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1540  * to issue ELS PLOGIs up to the configured discover threads with the
1541  * @vport (@vport->cfg_discovery_threads). The function also decrement
1542  * the @vport's num_disc_node by 1 if it is not already 0.
1543  **/
1544 void
1545 lpfc_more_plogi(struct lpfc_vport *vport)
1546 {
1547         if (vport->num_disc_nodes)
1548                 vport->num_disc_nodes--;
1549
1550         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1551         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1552                          "0232 Continue discovery with %d PLOGIs to go "
1553                          "Data: x%x x%x x%x\n",
1554                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1555                          vport->fc_flag, vport->port_state);
1556         /* Check to see if there are more PLOGIs to be sent */
1557         if (vport->fc_flag & FC_NLP_MORE)
1558                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1559                 lpfc_els_disc_plogi(vport);
1560
1561         return;
1562 }
1563
1564 /**
1565  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1566  * @phba: pointer to lpfc hba data structure.
1567  * @prsp: pointer to response IOCB payload.
1568  * @ndlp: pointer to a node-list data structure.
1569  *
1570  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1571  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1572  * The following cases are considered N_Port confirmed:
1573  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1574  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1575  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1576  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1577  * 1) if there is a node on vport list other than the @ndlp with the same
1578  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1579  * on that node to release the RPI associated with the node; 2) if there is
1580  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1581  * into, a new node shall be allocated (or activated). In either case, the
1582  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1583  * be released and the new_ndlp shall be put on to the vport node list and
1584  * its pointer returned as the confirmed node.
1585  *
1586  * Note that before the @ndlp got "released", the keepDID from not-matching
1587  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1588  * of the @ndlp. This is because the release of @ndlp is actually to put it
1589  * into an inactive state on the vport node list and the vport node list
1590  * management algorithm does not allow two node with a same DID.
1591  *
1592  * Return code
1593  *   pointer to the PLOGI N_Port @ndlp
1594  **/
1595 static struct lpfc_nodelist *
1596 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1597                          struct lpfc_nodelist *ndlp)
1598 {
1599         struct lpfc_vport *vport = ndlp->vport;
1600         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1601         struct lpfc_nodelist *new_ndlp;
1602         struct lpfc_rport_data *rdata;
1603         struct fc_rport *rport;
1604         struct serv_parm *sp;
1605         uint8_t  name[sizeof(struct lpfc_name)];
1606         uint32_t rc, keepDID = 0, keep_nlp_flag = 0;
1607         uint32_t keep_new_nlp_flag = 0;
1608         uint16_t keep_nlp_state;
1609         u32 keep_nlp_fc4_type = 0;
1610         struct lpfc_nvme_rport *keep_nrport = NULL;
1611         int  put_node;
1612         int  put_rport;
1613         unsigned long *active_rrqs_xri_bitmap = NULL;
1614
1615         /* Fabric nodes can have the same WWPN so we don't bother searching
1616          * by WWPN.  Just return the ndlp that was given to us.
1617          */
1618         if (ndlp->nlp_type & NLP_FABRIC)
1619                 return ndlp;
1620
1621         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1622         memset(name, 0, sizeof(struct lpfc_name));
1623
1624         /* Now we find out if the NPort we are logging into, matches the WWPN
1625          * we have for that ndlp. If not, we have some work to do.
1626          */
1627         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1628
1629         /* return immediately if the WWPN matches ndlp */
1630         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1631                 return ndlp;
1632
1633         if (phba->sli_rev == LPFC_SLI_REV4) {
1634                 active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1635                                                        GFP_KERNEL);
1636                 if (active_rrqs_xri_bitmap)
1637                         memset(active_rrqs_xri_bitmap, 0,
1638                                phba->cfg_rrq_xri_bitmap_sz);
1639         }
1640
1641         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1642                          "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1643                          "new_ndlp x%x x%x x%x\n",
1644                          ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1645                          (new_ndlp ? new_ndlp->nlp_DID : 0),
1646                          (new_ndlp ? new_ndlp->nlp_flag : 0),
1647                          (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1648
1649         if (!new_ndlp) {
1650                 rc = memcmp(&ndlp->nlp_portname, name,
1651                             sizeof(struct lpfc_name));
1652                 if (!rc) {
1653                         if (active_rrqs_xri_bitmap)
1654                                 mempool_free(active_rrqs_xri_bitmap,
1655                                              phba->active_rrq_pool);
1656                         return ndlp;
1657                 }
1658                 new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
1659                 if (!new_ndlp) {
1660                         if (active_rrqs_xri_bitmap)
1661                                 mempool_free(active_rrqs_xri_bitmap,
1662                                              phba->active_rrq_pool);
1663                         return ndlp;
1664                 }
1665         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1666                 rc = memcmp(&ndlp->nlp_portname, name,
1667                             sizeof(struct lpfc_name));
1668                 if (!rc) {
1669                         if (active_rrqs_xri_bitmap)
1670                                 mempool_free(active_rrqs_xri_bitmap,
1671                                              phba->active_rrq_pool);
1672                         return ndlp;
1673                 }
1674                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1675                                                 NLP_STE_UNUSED_NODE);
1676                 if (!new_ndlp) {
1677                         if (active_rrqs_xri_bitmap)
1678                                 mempool_free(active_rrqs_xri_bitmap,
1679                                              phba->active_rrq_pool);
1680                         return ndlp;
1681                 }
1682                 keepDID = new_ndlp->nlp_DID;
1683                 if ((phba->sli_rev == LPFC_SLI_REV4) && active_rrqs_xri_bitmap)
1684                         memcpy(active_rrqs_xri_bitmap,
1685                                new_ndlp->active_rrqs_xri_bitmap,
1686                                phba->cfg_rrq_xri_bitmap_sz);
1687         } else {
1688                 keepDID = new_ndlp->nlp_DID;
1689                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1690                     active_rrqs_xri_bitmap)
1691                         memcpy(active_rrqs_xri_bitmap,
1692                                new_ndlp->active_rrqs_xri_bitmap,
1693                                phba->cfg_rrq_xri_bitmap_sz);
1694         }
1695
1696         /* At this point in this routine, we know new_ndlp will be
1697          * returned. however, any previous GID_FTs that were done
1698          * would have updated nlp_fc4_type in ndlp, so we must ensure
1699          * new_ndlp has the right value.
1700          */
1701         if (vport->fc_flag & FC_FABRIC) {
1702                 keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1703                 new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1704         }
1705
1706         lpfc_unreg_rpi(vport, new_ndlp);
1707         new_ndlp->nlp_DID = ndlp->nlp_DID;
1708         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1709         if (phba->sli_rev == LPFC_SLI_REV4)
1710                 memcpy(new_ndlp->active_rrqs_xri_bitmap,
1711                        ndlp->active_rrqs_xri_bitmap,
1712                        phba->cfg_rrq_xri_bitmap_sz);
1713
1714         spin_lock_irq(shost->host_lock);
1715         keep_new_nlp_flag = new_ndlp->nlp_flag;
1716         keep_nlp_flag = ndlp->nlp_flag;
1717         new_ndlp->nlp_flag = ndlp->nlp_flag;
1718
1719         /* if new_ndlp had NLP_UNREG_INP set, keep it */
1720         if (keep_new_nlp_flag & NLP_UNREG_INP)
1721                 new_ndlp->nlp_flag |= NLP_UNREG_INP;
1722         else
1723                 new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1724
1725         /* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1726         if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1727                 new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1728         else
1729                 new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1730
1731         ndlp->nlp_flag = keep_new_nlp_flag;
1732
1733         /* if ndlp had NLP_UNREG_INP set, keep it */
1734         if (keep_nlp_flag & NLP_UNREG_INP)
1735                 ndlp->nlp_flag |= NLP_UNREG_INP;
1736         else
1737                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
1738
1739         /* if ndlp had NLP_RPI_REGISTERED set, keep it */
1740         if (keep_nlp_flag & NLP_RPI_REGISTERED)
1741                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1742         else
1743                 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1744
1745         spin_unlock_irq(shost->host_lock);
1746
1747         /* Set nlp_states accordingly */
1748         keep_nlp_state = new_ndlp->nlp_state;
1749         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1750
1751         /* interchange the nvme remoteport structs */
1752         keep_nrport = new_ndlp->nrport;
1753         new_ndlp->nrport = ndlp->nrport;
1754
1755         /* Move this back to NPR state */
1756         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1757                 /* The new_ndlp is replacing ndlp totally, so we need
1758                  * to put ndlp on UNUSED list and try to free it.
1759                  */
1760                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1761                          "3179 PLOGI confirm NEW: %x %x\n",
1762                          new_ndlp->nlp_DID, keepDID);
1763
1764                 /* Fix up the rport accordingly */
1765                 rport =  ndlp->rport;
1766                 if (rport) {
1767                         rdata = rport->dd_data;
1768                         if (rdata->pnode == ndlp) {
1769                                 /* break the link before dropping the ref */
1770                                 ndlp->rport = NULL;
1771                                 lpfc_nlp_put(ndlp);
1772                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1773                                 new_ndlp->rport = rport;
1774                         }
1775                         new_ndlp->nlp_type = ndlp->nlp_type;
1776                 }
1777
1778                 /* Fix up the nvme rport */
1779                 if (ndlp->nrport) {
1780                         ndlp->nrport = NULL;
1781                         lpfc_nlp_put(ndlp);
1782                 }
1783
1784                 /* We shall actually free the ndlp with both nlp_DID and
1785                  * nlp_portname fields equals 0 to avoid any ndlp on the
1786                  * nodelist never to be used.
1787                  */
1788                 if (ndlp->nlp_DID == 0) {
1789                         spin_lock_irq(&phba->ndlp_lock);
1790                         NLP_SET_FREE_REQ(ndlp);
1791                         spin_unlock_irq(&phba->ndlp_lock);
1792                 }
1793
1794                 /* Two ndlps cannot have the same did on the nodelist.
1795                  * Note: for this case, ndlp has a NULL WWPN so setting
1796                  * the nlp_fc4_type isn't required.
1797                  */
1798                 ndlp->nlp_DID = keepDID;
1799                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1800                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1801                     active_rrqs_xri_bitmap)
1802                         memcpy(ndlp->active_rrqs_xri_bitmap,
1803                                active_rrqs_xri_bitmap,
1804                                phba->cfg_rrq_xri_bitmap_sz);
1805
1806                 if (!NLP_CHK_NODE_ACT(ndlp))
1807                         lpfc_drop_node(vport, ndlp);
1808         }
1809         else {
1810                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1811                          "3180 PLOGI confirm SWAP: %x %x\n",
1812                          new_ndlp->nlp_DID, keepDID);
1813
1814                 lpfc_unreg_rpi(vport, ndlp);
1815
1816                 /* Two ndlps cannot have the same did and the fc4
1817                  * type must be transferred because the ndlp is in
1818                  * flight.
1819                  */
1820                 ndlp->nlp_DID = keepDID;
1821                 ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1822
1823                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1824                     active_rrqs_xri_bitmap)
1825                         memcpy(ndlp->active_rrqs_xri_bitmap,
1826                                active_rrqs_xri_bitmap,
1827                                phba->cfg_rrq_xri_bitmap_sz);
1828
1829                 /* Since we are switching over to the new_ndlp,
1830                  * reset the old ndlp state
1831                  */
1832                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1833                     (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1834                         keep_nlp_state = NLP_STE_NPR_NODE;
1835                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1836
1837                 /* Previous ndlp no longer active with nvme host transport.
1838                  * Remove reference from earlier registration unless the
1839                  * nvme host took care of it.
1840                  */
1841                 if (ndlp->nrport)
1842                         lpfc_nlp_put(ndlp);
1843                 ndlp->nrport = keep_nrport;
1844
1845                 /* Fix up the rport accordingly */
1846                 rport = ndlp->rport;
1847                 if (rport) {
1848                         rdata = rport->dd_data;
1849                         put_node = rdata->pnode != NULL;
1850                         put_rport = ndlp->rport != NULL;
1851                         rdata->pnode = NULL;
1852                         ndlp->rport = NULL;
1853                         if (put_node)
1854                                 lpfc_nlp_put(ndlp);
1855                         if (put_rport)
1856                                 put_device(&rport->dev);
1857                 }
1858         }
1859         if (phba->sli_rev == LPFC_SLI_REV4 &&
1860             active_rrqs_xri_bitmap)
1861                 mempool_free(active_rrqs_xri_bitmap,
1862                              phba->active_rrq_pool);
1863
1864         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1865                          "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1866                          new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1867                          new_ndlp->nlp_fc4_type);
1868
1869         return new_ndlp;
1870 }
1871
1872 /**
1873  * lpfc_end_rscn - Check and handle more rscn for a vport
1874  * @vport: pointer to a host virtual N_Port data structure.
1875  *
1876  * This routine checks whether more Registration State Change
1877  * Notifications (RSCNs) came in while the discovery state machine was in
1878  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1879  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1880  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1881  * handling the RSCNs.
1882  **/
1883 void
1884 lpfc_end_rscn(struct lpfc_vport *vport)
1885 {
1886         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1887
1888         if (vport->fc_flag & FC_RSCN_MODE) {
1889                 /*
1890                  * Check to see if more RSCNs came in while we were
1891                  * processing this one.
1892                  */
1893                 if (vport->fc_rscn_id_cnt ||
1894                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1895                         lpfc_els_handle_rscn(vport);
1896                 else {
1897                         spin_lock_irq(shost->host_lock);
1898                         vport->fc_flag &= ~FC_RSCN_MODE;
1899                         spin_unlock_irq(shost->host_lock);
1900                 }
1901         }
1902 }
1903
1904 /**
1905  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1906  * @phba: pointer to lpfc hba data structure.
1907  * @cmdiocb: pointer to lpfc command iocb data structure.
1908  * @rspiocb: pointer to lpfc response iocb data structure.
1909  *
1910  * This routine will call the clear rrq function to free the rrq and
1911  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1912  * exist then the clear_rrq is still called because the rrq needs to
1913  * be freed.
1914  **/
1915
1916 static void
1917 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1918                     struct lpfc_iocbq *rspiocb)
1919 {
1920         struct lpfc_vport *vport = cmdiocb->vport;
1921         IOCB_t *irsp;
1922         struct lpfc_nodelist *ndlp;
1923         struct lpfc_node_rrq *rrq;
1924
1925         /* we pass cmdiocb to state machine which needs rspiocb as well */
1926         rrq = cmdiocb->context_un.rrq;
1927         cmdiocb->context_un.rsp_iocb = rspiocb;
1928
1929         irsp = &rspiocb->iocb;
1930         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1931                 "RRQ cmpl:      status:x%x/x%x did:x%x",
1932                 irsp->ulpStatus, irsp->un.ulpWord[4],
1933                 irsp->un.elsreq64.remoteID);
1934
1935         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1936         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1937                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1938                                  "2882 RRQ completes to NPort x%x "
1939                                  "with no ndlp. Data: x%x x%x x%x\n",
1940                                  irsp->un.elsreq64.remoteID,
1941                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1942                                  irsp->ulpIoTag);
1943                 goto out;
1944         }
1945
1946         /* rrq completes to NPort <nlp_DID> */
1947         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1948                          "2880 RRQ completes to NPort x%x "
1949                          "Data: x%x x%x x%x x%x x%x\n",
1950                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1951                          irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1952
1953         if (irsp->ulpStatus) {
1954                 /* Check for retry */
1955                 /* RRQ failed Don't print the vport to vport rjts */
1956                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1957                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1958                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1959                         (phba)->pport->cfg_log_verbose & LOG_ELS)
1960                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1961                                  "2881 RRQ failure DID:%06X Status:x%x/x%x\n",
1962                                  ndlp->nlp_DID, irsp->ulpStatus,
1963                                  irsp->un.ulpWord[4]);
1964         }
1965 out:
1966         if (rrq)
1967                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1968         lpfc_els_free_iocb(phba, cmdiocb);
1969         return;
1970 }
1971 /**
1972  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1973  * @phba: pointer to lpfc hba data structure.
1974  * @cmdiocb: pointer to lpfc command iocb data structure.
1975  * @rspiocb: pointer to lpfc response iocb data structure.
1976  *
1977  * This routine is the completion callback function for issuing the Port
1978  * Login (PLOGI) command. For PLOGI completion, there must be an active
1979  * ndlp on the vport node list that matches the remote node ID from the
1980  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1981  * ignored and command IOCB released. The PLOGI response IOCB status is
1982  * checked for error conditons. If there is error status reported, PLOGI
1983  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1984  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1985  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1986  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1987  * there are additional N_Port nodes with the vport that need to perform
1988  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1989  * PLOGIs.
1990  **/
1991 static void
1992 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1993                     struct lpfc_iocbq *rspiocb)
1994 {
1995         struct lpfc_vport *vport = cmdiocb->vport;
1996         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1997         IOCB_t *irsp;
1998         struct lpfc_nodelist *ndlp;
1999         struct lpfc_dmabuf *prsp;
2000         int disc;
2001
2002         /* we pass cmdiocb to state machine which needs rspiocb as well */
2003         cmdiocb->context_un.rsp_iocb = rspiocb;
2004
2005         irsp = &rspiocb->iocb;
2006         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2007                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
2008                 irsp->ulpStatus, irsp->un.ulpWord[4],
2009                 irsp->un.elsreq64.remoteID);
2010
2011         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
2012         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2013                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2014                                  "0136 PLOGI completes to NPort x%x "
2015                                  "with no ndlp. Data: x%x x%x x%x\n",
2016                                  irsp->un.elsreq64.remoteID,
2017                                  irsp->ulpStatus, irsp->un.ulpWord[4],
2018                                  irsp->ulpIoTag);
2019                 goto out;
2020         }
2021
2022         /* Since ndlp can be freed in the disc state machine, note if this node
2023          * is being used during discovery.
2024          */
2025         spin_lock_irq(shost->host_lock);
2026         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2027         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2028         spin_unlock_irq(shost->host_lock);
2029
2030         /* PLOGI completes to NPort <nlp_DID> */
2031         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2032                          "0102 PLOGI completes to NPort x%06x "
2033                          "Data: x%x x%x x%x x%x x%x\n",
2034                          ndlp->nlp_DID, ndlp->nlp_fc4_type,
2035                          irsp->ulpStatus, irsp->un.ulpWord[4],
2036                          disc, vport->num_disc_nodes);
2037
2038         /* Check to see if link went down during discovery */
2039         if (lpfc_els_chk_latt(vport)) {
2040                 spin_lock_irq(shost->host_lock);
2041                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2042                 spin_unlock_irq(shost->host_lock);
2043                 goto out;
2044         }
2045
2046         if (irsp->ulpStatus) {
2047                 /* Check for retry */
2048                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2049                         /* ELS command is being retried */
2050                         if (disc) {
2051                                 spin_lock_irq(shost->host_lock);
2052                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2053                                 spin_unlock_irq(shost->host_lock);
2054                         }
2055                         goto out;
2056                 }
2057                 /* PLOGI failed Don't print the vport to vport rjts */
2058                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
2059                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
2060                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
2061                         (phba)->pport->cfg_log_verbose & LOG_ELS)
2062                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2063                                  "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
2064                                  ndlp->nlp_DID, irsp->ulpStatus,
2065                                  irsp->un.ulpWord[4]);
2066                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2067                 if (!lpfc_error_lost_link(irsp))
2068                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2069                                                 NLP_EVT_CMPL_PLOGI);
2070         } else {
2071                 /* Good status, call state machine */
2072                 prsp = list_entry(((struct lpfc_dmabuf *)
2073                                    cmdiocb->context2)->list.next,
2074                                   struct lpfc_dmabuf, list);
2075                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2076                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2077                                              NLP_EVT_CMPL_PLOGI);
2078         }
2079
2080         if (disc && vport->num_disc_nodes) {
2081                 /* Check to see if there are more PLOGIs to be sent */
2082                 lpfc_more_plogi(vport);
2083
2084                 if (vport->num_disc_nodes == 0) {
2085                         spin_lock_irq(shost->host_lock);
2086                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2087                         spin_unlock_irq(shost->host_lock);
2088
2089                         lpfc_can_disctmo(vport);
2090                         lpfc_end_rscn(vport);
2091                 }
2092         }
2093
2094 out:
2095         lpfc_els_free_iocb(phba, cmdiocb);
2096         return;
2097 }
2098
2099 /**
2100  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2101  * @vport: pointer to a host virtual N_Port data structure.
2102  * @did: destination port identifier.
2103  * @retry: number of retries to the command IOCB.
2104  *
2105  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2106  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2107  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2108  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
2109  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2110  *
2111  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2112  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2113  * will be stored into the context1 field of the IOCB for the completion
2114  * callback function to the PLOGI ELS command.
2115  *
2116  * Return code
2117  *   0 - Successfully issued a plogi for @vport
2118  *   1 - failed to issue a plogi for @vport
2119  **/
2120 int
2121 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2122 {
2123         struct lpfc_hba  *phba = vport->phba;
2124         struct Scsi_Host *shost;
2125         struct serv_parm *sp;
2126         struct lpfc_nodelist *ndlp;
2127         struct lpfc_iocbq *elsiocb;
2128         uint8_t *pcmd;
2129         uint16_t cmdsize;
2130         int ret;
2131
2132         ndlp = lpfc_findnode_did(vport, did);
2133
2134         if (ndlp) {
2135                 /* Defer the processing of the issue PLOGI until after the
2136                  * outstanding UNREG_RPI mbox command completes, unless we
2137                  * are going offline. This logic does not apply for Fabric DIDs
2138                  */
2139                 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2140                     ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2141                     !(vport->fc_flag & FC_OFFLINE_MODE)) {
2142                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2143                                          "4110 Issue PLOGI x%x deferred "
2144                                          "on NPort x%x rpi x%x Data: x%px\n",
2145                                          ndlp->nlp_defer_did, ndlp->nlp_DID,
2146                                          ndlp->nlp_rpi, ndlp);
2147
2148                         /* We can only defer 1st PLOGI */
2149                         if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2150                                 ndlp->nlp_defer_did = did;
2151                         return 0;
2152                 }
2153                 if (!NLP_CHK_NODE_ACT(ndlp))
2154                         ndlp = NULL;
2155         }
2156
2157         /* If ndlp is not NULL, we will bump the reference count on it */
2158         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2159         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2160                                      ELS_CMD_PLOGI);
2161         if (!elsiocb)
2162                 return 1;
2163
2164         shost = lpfc_shost_from_vport(vport);
2165         spin_lock_irq(shost->host_lock);
2166         ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2167         spin_unlock_irq(shost->host_lock);
2168
2169         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2170
2171         /* For PLOGI request, remainder of payload is service parameters */
2172         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2173         pcmd += sizeof(uint32_t);
2174         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2175         sp = (struct serv_parm *) pcmd;
2176
2177         /*
2178          * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2179          * to device on remote loops work.
2180          */
2181         if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2182                 sp->cmn.altBbCredit = 1;
2183
2184         if (sp->cmn.fcphLow < FC_PH_4_3)
2185                 sp->cmn.fcphLow = FC_PH_4_3;
2186
2187         if (sp->cmn.fcphHigh < FC_PH3)
2188                 sp->cmn.fcphHigh = FC_PH3;
2189
2190         sp->cmn.valid_vendor_ver_level = 0;
2191         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2192         sp->cmn.bbRcvSizeMsb &= 0xF;
2193
2194         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2195                 "Issue PLOGI:     did:x%x",
2196                 did, 0, 0);
2197
2198         /* If our firmware supports this feature, convey that
2199          * information to the target using the vendor specific field.
2200          */
2201         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2202                 sp->cmn.valid_vendor_ver_level = 1;
2203                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2204                 sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2205         }
2206
2207         phba->fc_stat.elsXmitPLOGI++;
2208         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
2209         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2210
2211         if (ret == IOCB_ERROR) {
2212                 lpfc_els_free_iocb(phba, elsiocb);
2213                 return 1;
2214         }
2215         return 0;
2216 }
2217
2218 /**
2219  * lpfc_cmpl_els_prli - Completion callback function for prli
2220  * @phba: pointer to lpfc hba data structure.
2221  * @cmdiocb: pointer to lpfc command iocb data structure.
2222  * @rspiocb: pointer to lpfc response iocb data structure.
2223  *
2224  * This routine is the completion callback function for a Process Login
2225  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2226  * status. If there is error status reported, PRLI retry shall be attempted
2227  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2228  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2229  * ndlp to mark the PRLI completion.
2230  **/
2231 static void
2232 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2233                    struct lpfc_iocbq *rspiocb)
2234 {
2235         struct lpfc_vport *vport = cmdiocb->vport;
2236         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2237         IOCB_t *irsp;
2238         struct lpfc_nodelist *ndlp;
2239         char *mode;
2240
2241         /* we pass cmdiocb to state machine which needs rspiocb as well */
2242         cmdiocb->context_un.rsp_iocb = rspiocb;
2243
2244         irsp = &(rspiocb->iocb);
2245         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2246         spin_lock_irq(shost->host_lock);
2247         ndlp->nlp_flag &= ~NLP_PRLI_SND;
2248
2249         /* Driver supports multiple FC4 types.  Counters matter. */
2250         vport->fc_prli_sent--;
2251         ndlp->fc4_prli_sent--;
2252         spin_unlock_irq(shost->host_lock);
2253
2254         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2255                 "PRLI cmpl:       status:x%x/x%x did:x%x",
2256                 irsp->ulpStatus, irsp->un.ulpWord[4],
2257                 ndlp->nlp_DID);
2258
2259         /* PRLI completes to NPort <nlp_DID> */
2260         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2261                          "0103 PRLI completes to NPort x%06x "
2262                          "Data: x%x x%x x%x x%x\n",
2263                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2264                          vport->num_disc_nodes, ndlp->fc4_prli_sent);
2265
2266         /* Check to see if link went down during discovery */
2267         if (lpfc_els_chk_latt(vport))
2268                 goto out;
2269
2270         if (irsp->ulpStatus) {
2271                 /* Check for retry */
2272                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2273                         /* ELS command is being retried */
2274                         goto out;
2275                 }
2276
2277                 /* If we don't send GFT_ID to Fabric, a PRLI error
2278                  * could be expected.
2279                  */
2280                 if ((vport->fc_flag & FC_FABRIC) ||
2281                     (vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH))
2282                         mode = KERN_ERR;
2283                 else
2284                         mode = KERN_INFO;
2285
2286                 /* PRLI failed */
2287                 lpfc_printf_vlog(vport, mode, LOG_ELS,
2288                                  "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2289                                  "data: x%x\n",
2290                                  ndlp->nlp_DID, irsp->ulpStatus,
2291                                  irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2292
2293                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2294                 if (lpfc_error_lost_link(irsp))
2295                         goto out;
2296                 else
2297                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2298                                                 NLP_EVT_CMPL_PRLI);
2299         } else {
2300                 /* Good status, call state machine.  However, if another
2301                  * PRLI is outstanding, don't call the state machine
2302                  * because final disposition to Mapped or Unmapped is
2303                  * completed there.
2304                  */
2305                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2306                                         NLP_EVT_CMPL_PRLI);
2307         }
2308
2309 out:
2310         lpfc_els_free_iocb(phba, cmdiocb);
2311         return;
2312 }
2313
2314 /**
2315  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2316  * @vport: pointer to a host virtual N_Port data structure.
2317  * @ndlp: pointer to a node-list data structure.
2318  * @retry: number of retries to the command IOCB.
2319  *
2320  * This routine issues a Process Login (PRLI) ELS command for the
2321  * @vport. The PRLI service parameters are set up in the payload of the
2322  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2323  * is put to the IOCB completion callback func field before invoking the
2324  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2325  *
2326  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2327  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2328  * will be stored into the context1 field of the IOCB for the completion
2329  * callback function to the PRLI ELS command.
2330  *
2331  * Return code
2332  *   0 - successfully issued prli iocb command for @vport
2333  *   1 - failed to issue prli iocb command for @vport
2334  **/
2335 int
2336 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2337                     uint8_t retry)
2338 {
2339         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2340         struct lpfc_hba *phba = vport->phba;
2341         PRLI *npr;
2342         struct lpfc_nvme_prli *npr_nvme;
2343         struct lpfc_iocbq *elsiocb;
2344         uint8_t *pcmd;
2345         uint16_t cmdsize;
2346         u32 local_nlp_type, elscmd;
2347
2348         /*
2349          * If we are in RSCN mode, the FC4 types supported from a
2350          * previous GFT_ID command may not be accurate. So, if we
2351          * are a NVME Initiator, always look for the possibility of
2352          * the remote NPort beng a NVME Target.
2353          */
2354         if (phba->sli_rev == LPFC_SLI_REV4 &&
2355             vport->fc_flag & FC_RSCN_MODE &&
2356             vport->nvmei_support)
2357                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2358         local_nlp_type = ndlp->nlp_fc4_type;
2359
2360         /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2361          * fields here before any of them can complete.
2362          */
2363         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2364         ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2365         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2366         ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
2367         ndlp->nvme_fb_size = 0;
2368
2369  send_next_prli:
2370         if (local_nlp_type & NLP_FC4_FCP) {
2371                 /* Payload is 4 + 16 = 20 x14 bytes. */
2372                 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2373                 elscmd = ELS_CMD_PRLI;
2374         } else if (local_nlp_type & NLP_FC4_NVME) {
2375                 /* Payload is 4 + 20 = 24 x18 bytes. */
2376                 cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2377                 elscmd = ELS_CMD_NVMEPRLI;
2378         } else {
2379                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2380                                  "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2381                                  ndlp->nlp_fc4_type, ndlp->nlp_DID);
2382                 return 1;
2383         }
2384
2385         /* SLI3 ports don't support NVME.  If this rport is a strict NVME
2386          * FC4 type, implicitly LOGO.
2387          */
2388         if (phba->sli_rev == LPFC_SLI_REV3 &&
2389             ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2390                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2391                                  "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2392                                  ndlp->nlp_type);
2393                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2394                 return 1;
2395         }
2396
2397         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2398                                      ndlp->nlp_DID, elscmd);
2399         if (!elsiocb)
2400                 return 1;
2401
2402         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2403
2404         /* For PRLI request, remainder of payload is service parameters */
2405         memset(pcmd, 0, cmdsize);
2406
2407         if (local_nlp_type & NLP_FC4_FCP) {
2408                 /* Remainder of payload is FCP PRLI parameter page.
2409                  * Note: this data structure is defined as
2410                  * BE/LE in the structure definition so no
2411                  * byte swap call is made.
2412                  */
2413                 *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2414                 pcmd += sizeof(uint32_t);
2415                 npr = (PRLI *)pcmd;
2416
2417                 /*
2418                  * If our firmware version is 3.20 or later,
2419                  * set the following bits for FC-TAPE support.
2420                  */
2421                 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2422                         npr->ConfmComplAllowed = 1;
2423                         npr->Retry = 1;
2424                         npr->TaskRetryIdReq = 1;
2425                 }
2426                 npr->estabImagePair = 1;
2427                 npr->readXferRdyDis = 1;
2428                 if (vport->cfg_first_burst_size)
2429                         npr->writeXferRdyDis = 1;
2430
2431                 /* For FCP support */
2432                 npr->prliType = PRLI_FCP_TYPE;
2433                 npr->initiatorFunc = 1;
2434                 elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2435
2436                 /* Remove FCP type - processed. */
2437                 local_nlp_type &= ~NLP_FC4_FCP;
2438         } else if (local_nlp_type & NLP_FC4_NVME) {
2439                 /* Remainder of payload is NVME PRLI parameter page.
2440                  * This data structure is the newer definition that
2441                  * uses bf macros so a byte swap is required.
2442                  */
2443                 *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2444                 pcmd += sizeof(uint32_t);
2445                 npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2446                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2447                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2448                 if (phba->nsler) {
2449                         bf_set(prli_nsler, npr_nvme, 1);
2450                         bf_set(prli_conf, npr_nvme, 1);
2451                 }
2452
2453                 /* Only initiators request first burst. */
2454                 if ((phba->cfg_nvme_enable_fb) &&
2455                     !phba->nvmet_support)
2456                         bf_set(prli_fba, npr_nvme, 1);
2457
2458                 if (phba->nvmet_support) {
2459                         bf_set(prli_tgt, npr_nvme, 1);
2460                         bf_set(prli_disc, npr_nvme, 1);
2461                 } else {
2462                         bf_set(prli_init, npr_nvme, 1);
2463                         bf_set(prli_conf, npr_nvme, 1);
2464                 }
2465
2466                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2467                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2468                 elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2469
2470                 /* Remove NVME type - processed. */
2471                 local_nlp_type &= ~NLP_FC4_NVME;
2472         }
2473
2474         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2475                 "Issue PRLI:      did:x%x",
2476                 ndlp->nlp_DID, 0, 0);
2477
2478         phba->fc_stat.elsXmitPRLI++;
2479         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2480         spin_lock_irq(shost->host_lock);
2481         ndlp->nlp_flag |= NLP_PRLI_SND;
2482
2483         /* The vport counters are used for lpfc_scan_finished, but
2484          * the ndlp is used to track outstanding PRLIs for different
2485          * FC4 types.
2486          */
2487         vport->fc_prli_sent++;
2488         ndlp->fc4_prli_sent++;
2489         spin_unlock_irq(shost->host_lock);
2490         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2491             IOCB_ERROR) {
2492                 spin_lock_irq(shost->host_lock);
2493                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2494                 spin_unlock_irq(shost->host_lock);
2495                 lpfc_els_free_iocb(phba, elsiocb);
2496                 return 1;
2497         }
2498
2499
2500         /* The driver supports 2 FC4 types.  Make sure
2501          * a PRLI is issued for all types before exiting.
2502          */
2503         if (phba->sli_rev == LPFC_SLI_REV4 &&
2504             local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2505                 goto send_next_prli;
2506
2507         return 0;
2508 }
2509
2510 /**
2511  * lpfc_rscn_disc - Perform rscn discovery for a vport
2512  * @vport: pointer to a host virtual N_Port data structure.
2513  *
2514  * This routine performs Registration State Change Notification (RSCN)
2515  * discovery for a @vport. If the @vport's node port recovery count is not
2516  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2517  * the nodes that need recovery. If none of the PLOGI were needed through
2518  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2519  * invoked to check and handle possible more RSCN came in during the period
2520  * of processing the current ones.
2521  **/
2522 static void
2523 lpfc_rscn_disc(struct lpfc_vport *vport)
2524 {
2525         lpfc_can_disctmo(vport);
2526
2527         /* RSCN discovery */
2528         /* go thru NPR nodes and issue ELS PLOGIs */
2529         if (vport->fc_npr_cnt)
2530                 if (lpfc_els_disc_plogi(vport))
2531                         return;
2532
2533         lpfc_end_rscn(vport);
2534 }
2535
2536 /**
2537  * lpfc_adisc_done - Complete the adisc phase of discovery
2538  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2539  *
2540  * This function is called when the final ADISC is completed during discovery.
2541  * This function handles clearing link attention or issuing reg_vpi depending
2542  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2543  * discovery.
2544  * This function is called with no locks held.
2545  **/
2546 static void
2547 lpfc_adisc_done(struct lpfc_vport *vport)
2548 {
2549         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2550         struct lpfc_hba   *phba = vport->phba;
2551
2552         /*
2553          * For NPIV, cmpl_reg_vpi will set port_state to READY,
2554          * and continue discovery.
2555          */
2556         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2557             !(vport->fc_flag & FC_RSCN_MODE) &&
2558             (phba->sli_rev < LPFC_SLI_REV4)) {
2559                 /* The ADISCs are complete.  Doesn't matter if they
2560                  * succeeded or failed because the ADISC completion
2561                  * routine guarantees to call the state machine and
2562                  * the RPI is either unregistered (failed ADISC response)
2563                  * or the RPI is still valid and the node is marked
2564                  * mapped for a target.  The exchanges should be in the
2565                  * correct state. This code is specific to SLI3.
2566                  */
2567                 lpfc_issue_clear_la(phba, vport);
2568                 lpfc_issue_reg_vpi(phba, vport);
2569                 return;
2570         }
2571         /*
2572         * For SLI2, we need to set port_state to READY
2573         * and continue discovery.
2574         */
2575         if (vport->port_state < LPFC_VPORT_READY) {
2576                 /* If we get here, there is nothing to ADISC */
2577                 lpfc_issue_clear_la(phba, vport);
2578                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2579                         vport->num_disc_nodes = 0;
2580                         /* go thru NPR list, issue ELS PLOGIs */
2581                         if (vport->fc_npr_cnt)
2582                                 lpfc_els_disc_plogi(vport);
2583                         if (!vport->num_disc_nodes) {
2584                                 spin_lock_irq(shost->host_lock);
2585                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2586                                 spin_unlock_irq(shost->host_lock);
2587                                 lpfc_can_disctmo(vport);
2588                                 lpfc_end_rscn(vport);
2589                         }
2590                 }
2591                 vport->port_state = LPFC_VPORT_READY;
2592         } else
2593                 lpfc_rscn_disc(vport);
2594 }
2595
2596 /**
2597  * lpfc_more_adisc - Issue more adisc as needed
2598  * @vport: pointer to a host virtual N_Port data structure.
2599  *
2600  * This routine determines whether there are more ndlps on a @vport
2601  * node list need to have Address Discover (ADISC) issued. If so, it will
2602  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2603  * remaining nodes which need to have ADISC sent.
2604  **/
2605 void
2606 lpfc_more_adisc(struct lpfc_vport *vport)
2607 {
2608         if (vport->num_disc_nodes)
2609                 vport->num_disc_nodes--;
2610         /* Continue discovery with <num_disc_nodes> ADISCs to go */
2611         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2612                          "0210 Continue discovery with %d ADISCs to go "
2613                          "Data: x%x x%x x%x\n",
2614                          vport->num_disc_nodes, vport->fc_adisc_cnt,
2615                          vport->fc_flag, vport->port_state);
2616         /* Check to see if there are more ADISCs to be sent */
2617         if (vport->fc_flag & FC_NLP_MORE) {
2618                 lpfc_set_disctmo(vport);
2619                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2620                 lpfc_els_disc_adisc(vport);
2621         }
2622         if (!vport->num_disc_nodes)
2623                 lpfc_adisc_done(vport);
2624         return;
2625 }
2626
2627 /**
2628  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2629  * @phba: pointer to lpfc hba data structure.
2630  * @cmdiocb: pointer to lpfc command iocb data structure.
2631  * @rspiocb: pointer to lpfc response iocb data structure.
2632  *
2633  * This routine is the completion function for issuing the Address Discover
2634  * (ADISC) command. It first checks to see whether link went down during
2635  * the discovery process. If so, the node will be marked as node port
2636  * recovery for issuing discover IOCB by the link attention handler and
2637  * exit. Otherwise, the response status is checked. If error was reported
2638  * in the response status, the ADISC command shall be retried by invoking
2639  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2640  * the response status, the state machine is invoked to set transition
2641  * with respect to NLP_EVT_CMPL_ADISC event.
2642  **/
2643 static void
2644 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2645                     struct lpfc_iocbq *rspiocb)
2646 {
2647         struct lpfc_vport *vport = cmdiocb->vport;
2648         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2649         IOCB_t *irsp;
2650         struct lpfc_nodelist *ndlp;
2651         int  disc;
2652
2653         /* we pass cmdiocb to state machine which needs rspiocb as well */
2654         cmdiocb->context_un.rsp_iocb = rspiocb;
2655
2656         irsp = &(rspiocb->iocb);
2657         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2658
2659         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2660                 "ADISC cmpl:      status:x%x/x%x did:x%x",
2661                 irsp->ulpStatus, irsp->un.ulpWord[4],
2662                 ndlp->nlp_DID);
2663
2664         /* Since ndlp can be freed in the disc state machine, note if this node
2665          * is being used during discovery.
2666          */
2667         spin_lock_irq(shost->host_lock);
2668         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2669         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2670         spin_unlock_irq(shost->host_lock);
2671         /* ADISC completes to NPort <nlp_DID> */
2672         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2673                          "0104 ADISC completes to NPort x%x "
2674                          "Data: x%x x%x x%x x%x x%x\n",
2675                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2676                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
2677         /* Check to see if link went down during discovery */
2678         if (lpfc_els_chk_latt(vport)) {
2679                 spin_lock_irq(shost->host_lock);
2680                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2681                 spin_unlock_irq(shost->host_lock);
2682                 goto out;
2683         }
2684
2685         if (irsp->ulpStatus) {
2686                 /* Check for retry */
2687                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2688                         /* ELS command is being retried */
2689                         if (disc) {
2690                                 spin_lock_irq(shost->host_lock);
2691                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2692                                 spin_unlock_irq(shost->host_lock);
2693                                 lpfc_set_disctmo(vport);
2694                         }
2695                         goto out;
2696                 }
2697                 /* ADISC failed */
2698                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2699                                  "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2700                                  ndlp->nlp_DID, irsp->ulpStatus,
2701                                  irsp->un.ulpWord[4]);
2702                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2703                 if (!lpfc_error_lost_link(irsp))
2704                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2705                                                 NLP_EVT_CMPL_ADISC);
2706         } else
2707                 /* Good status, call state machine */
2708                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2709                                         NLP_EVT_CMPL_ADISC);
2710
2711         /* Check to see if there are more ADISCs to be sent */
2712         if (disc && vport->num_disc_nodes)
2713                 lpfc_more_adisc(vport);
2714 out:
2715         lpfc_els_free_iocb(phba, cmdiocb);
2716         return;
2717 }
2718
2719 /**
2720  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2721  * @vport: pointer to a virtual N_Port data structure.
2722  * @ndlp: pointer to a node-list data structure.
2723  * @retry: number of retries to the command IOCB.
2724  *
2725  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2726  * @vport. It prepares the payload of the ADISC ELS command, updates the
2727  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2728  * to issue the ADISC ELS command.
2729  *
2730  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2731  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2732  * will be stored into the context1 field of the IOCB for the completion
2733  * callback function to the ADISC ELS command.
2734  *
2735  * Return code
2736  *   0 - successfully issued adisc
2737  *   1 - failed to issue adisc
2738  **/
2739 int
2740 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2741                      uint8_t retry)
2742 {
2743         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2744         struct lpfc_hba  *phba = vport->phba;
2745         ADISC *ap;
2746         struct lpfc_iocbq *elsiocb;
2747         uint8_t *pcmd;
2748         uint16_t cmdsize;
2749
2750         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2751         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2752                                      ndlp->nlp_DID, ELS_CMD_ADISC);
2753         if (!elsiocb)
2754                 return 1;
2755
2756         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2757
2758         /* For ADISC request, remainder of payload is service parameters */
2759         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2760         pcmd += sizeof(uint32_t);
2761
2762         /* Fill in ADISC payload */
2763         ap = (ADISC *) pcmd;
2764         ap->hardAL_PA = phba->fc_pref_ALPA;
2765         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2766         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2767         ap->DID = be32_to_cpu(vport->fc_myDID);
2768
2769         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2770                 "Issue ADISC:     did:x%x",
2771                 ndlp->nlp_DID, 0, 0);
2772
2773         phba->fc_stat.elsXmitADISC++;
2774         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2775         spin_lock_irq(shost->host_lock);
2776         ndlp->nlp_flag |= NLP_ADISC_SND;
2777         spin_unlock_irq(shost->host_lock);
2778         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2779             IOCB_ERROR) {
2780                 spin_lock_irq(shost->host_lock);
2781                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2782                 spin_unlock_irq(shost->host_lock);
2783                 lpfc_els_free_iocb(phba, elsiocb);
2784                 return 1;
2785         }
2786         return 0;
2787 }
2788
2789 /**
2790  * lpfc_cmpl_els_logo - Completion callback function for logo
2791  * @phba: pointer to lpfc hba data structure.
2792  * @cmdiocb: pointer to lpfc command iocb data structure.
2793  * @rspiocb: pointer to lpfc response iocb data structure.
2794  *
2795  * This routine is the completion function for issuing the ELS Logout (LOGO)
2796  * command. If no error status was reported from the LOGO response, the
2797  * state machine of the associated ndlp shall be invoked for transition with
2798  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2799  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2800  **/
2801 static void
2802 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2803                    struct lpfc_iocbq *rspiocb)
2804 {
2805         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2806         struct lpfc_vport *vport = ndlp->vport;
2807         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2808         IOCB_t *irsp;
2809         struct lpfcMboxq *mbox;
2810         unsigned long flags;
2811         uint32_t skip_recovery = 0;
2812
2813         /* we pass cmdiocb to state machine which needs rspiocb as well */
2814         cmdiocb->context_un.rsp_iocb = rspiocb;
2815
2816         irsp = &(rspiocb->iocb);
2817         spin_lock_irq(shost->host_lock);
2818         ndlp->nlp_flag &= ~NLP_LOGO_SND;
2819         spin_unlock_irq(shost->host_lock);
2820
2821         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2822                 "LOGO cmpl:       status:x%x/x%x did:x%x",
2823                 irsp->ulpStatus, irsp->un.ulpWord[4],
2824                 ndlp->nlp_DID);
2825
2826         /* LOGO completes to NPort <nlp_DID> */
2827         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2828                          "0105 LOGO completes to NPort x%x "
2829                          "Data: x%x x%x x%x x%x\n",
2830                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2831                          irsp->ulpTimeout, vport->num_disc_nodes);
2832
2833         if (lpfc_els_chk_latt(vport)) {
2834                 skip_recovery = 1;
2835                 goto out;
2836         }
2837
2838         /* Check to see if link went down during discovery */
2839         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2840                 /* NLP_EVT_DEVICE_RM should unregister the RPI
2841                  * which should abort all outstanding IOs.
2842                  */
2843                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2844                                         NLP_EVT_DEVICE_RM);
2845                 skip_recovery = 1;
2846                 goto out;
2847         }
2848
2849         /* The LOGO will not be retried on failure.  A LOGO was
2850          * issued to the remote rport and a ACC or RJT or no Answer are
2851          * all acceptable.  Note the failure and move forward with
2852          * discovery.  The PLOGI will retry.
2853          */
2854         if (irsp->ulpStatus) {
2855                 /* LOGO failed */
2856                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2857                                  "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
2858                                  ndlp->nlp_DID, irsp->ulpStatus,
2859                                  irsp->un.ulpWord[4]);
2860                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2861                 if (lpfc_error_lost_link(irsp)) {
2862                         skip_recovery = 1;
2863                         goto out;
2864                 }
2865         }
2866
2867         /* Call state machine. This will unregister the rpi if needed. */
2868         lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2869
2870 out:
2871         lpfc_els_free_iocb(phba, cmdiocb);
2872         /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2873         if ((vport->fc_flag & FC_PT2PT) &&
2874                 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2875                 phba->pport->fc_myDID = 0;
2876
2877                 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2878                     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
2879                         if (phba->nvmet_support)
2880                                 lpfc_nvmet_update_targetport(phba);
2881                         else
2882                                 lpfc_nvme_update_localport(phba->pport);
2883                 }
2884
2885                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2886                 if (mbox) {
2887                         lpfc_config_link(phba, mbox);
2888                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2889                         mbox->vport = vport;
2890                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2891                                 MBX_NOT_FINISHED) {
2892                                 mempool_free(mbox, phba->mbox_mem_pool);
2893                                 skip_recovery = 1;
2894                         }
2895                 }
2896         }
2897
2898         /*
2899          * If the node is a target, the handling attempts to recover the port.
2900          * For any other port type, the rpi is unregistered as an implicit
2901          * LOGO.
2902          */
2903         if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2904             skip_recovery == 0) {
2905                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2906                 spin_lock_irqsave(shost->host_lock, flags);
2907                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2908                 spin_unlock_irqrestore(shost->host_lock, flags);
2909
2910                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2911                                  "3187 LOGO completes to NPort x%x: Start "
2912                                  "Recovery Data: x%x x%x x%x x%x\n",
2913                                  ndlp->nlp_DID, irsp->ulpStatus,
2914                                  irsp->un.ulpWord[4], irsp->ulpTimeout,
2915                                  vport->num_disc_nodes);
2916                 lpfc_disc_start(vport);
2917         }
2918         return;
2919 }
2920
2921 /**
2922  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2923  * @vport: pointer to a virtual N_Port data structure.
2924  * @ndlp: pointer to a node-list data structure.
2925  * @retry: number of retries to the command IOCB.
2926  *
2927  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2928  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2929  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2930  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2931  *
2932  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2933  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2934  * will be stored into the context1 field of the IOCB for the completion
2935  * callback function to the LOGO ELS command.
2936  *
2937  * Callers of this routine are expected to unregister the RPI first
2938  *
2939  * Return code
2940  *   0 - successfully issued logo
2941  *   1 - failed to issue logo
2942  **/
2943 int
2944 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2945                     uint8_t retry)
2946 {
2947         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2948         struct lpfc_hba  *phba = vport->phba;
2949         struct lpfc_iocbq *elsiocb;
2950         uint8_t *pcmd;
2951         uint16_t cmdsize;
2952         int rc;
2953
2954         spin_lock_irq(shost->host_lock);
2955         if (ndlp->nlp_flag & NLP_LOGO_SND) {
2956                 spin_unlock_irq(shost->host_lock);
2957                 return 0;
2958         }
2959         spin_unlock_irq(shost->host_lock);
2960
2961         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2962         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2963                                      ndlp->nlp_DID, ELS_CMD_LOGO);
2964         if (!elsiocb)
2965                 return 1;
2966
2967         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2968         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2969         pcmd += sizeof(uint32_t);
2970
2971         /* Fill in LOGO payload */
2972         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2973         pcmd += sizeof(uint32_t);
2974         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2975
2976         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2977                 "Issue LOGO:      did:x%x",
2978                 ndlp->nlp_DID, 0, 0);
2979
2980         phba->fc_stat.elsXmitLOGO++;
2981         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2982         spin_lock_irq(shost->host_lock);
2983         ndlp->nlp_flag |= NLP_LOGO_SND;
2984         ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2985         spin_unlock_irq(shost->host_lock);
2986         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2987         if (rc == IOCB_ERROR) {
2988                 spin_lock_irq(shost->host_lock);
2989                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2990                 spin_unlock_irq(shost->host_lock);
2991                 lpfc_els_free_iocb(phba, elsiocb);
2992                 return 1;
2993         }
2994
2995         spin_lock_irq(shost->host_lock);
2996         ndlp->nlp_prev_state = ndlp->nlp_state;
2997         spin_unlock_irq(shost->host_lock);
2998         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
2999         return 0;
3000 }
3001
3002 /**
3003  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3004  * @phba: pointer to lpfc hba data structure.
3005  * @cmdiocb: pointer to lpfc command iocb data structure.
3006  * @rspiocb: pointer to lpfc response iocb data structure.
3007  *
3008  * This routine is a generic completion callback function for ELS commands.
3009  * Specifically, it is the callback function which does not need to perform
3010  * any command specific operations. It is currently used by the ELS command
3011  * issuing routines for the ELS State Change  Request (SCR),
3012  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
3013  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
3014  * certain debug loggings, this callback function simply invokes the
3015  * lpfc_els_chk_latt() routine to check whether link went down during the
3016  * discovery process.
3017  **/
3018 static void
3019 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3020                   struct lpfc_iocbq *rspiocb)
3021 {
3022         struct lpfc_vport *vport = cmdiocb->vport;
3023         IOCB_t *irsp;
3024
3025         irsp = &rspiocb->iocb;
3026
3027         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3028                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3029                 irsp->ulpStatus, irsp->un.ulpWord[4],
3030                 irsp->un.elsreq64.remoteID);
3031         /* ELS cmd tag <ulpIoTag> completes */
3032         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3033                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3034                          irsp->ulpIoTag, irsp->ulpStatus,
3035                          irsp->un.ulpWord[4], irsp->ulpTimeout);
3036         /* Check to see if link went down during discovery */
3037         lpfc_els_chk_latt(vport);
3038         lpfc_els_free_iocb(phba, cmdiocb);
3039         return;
3040 }
3041
3042 /**
3043  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3044  * @vport: pointer to a host virtual N_Port data structure.
3045  * @nportid: N_Port identifier to the remote node.
3046  * @retry: number of retries to the command IOCB.
3047  *
3048  * This routine issues a State Change Request (SCR) to a fabric node
3049  * on a @vport. The remote node @nportid is passed into the function. It
3050  * first search the @vport node list to find the matching ndlp. If no such
3051  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3052  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3053  * routine is invoked to send the SCR IOCB.
3054  *
3055  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3056  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3057  * will be stored into the context1 field of the IOCB for the completion
3058  * callback function to the SCR ELS command.
3059  *
3060  * Return code
3061  *   0 - Successfully issued scr command
3062  *   1 - Failed to issue scr command
3063  **/
3064 int
3065 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3066 {
3067         struct lpfc_hba  *phba = vport->phba;
3068         struct lpfc_iocbq *elsiocb;
3069         uint8_t *pcmd;
3070         uint16_t cmdsize;
3071         struct lpfc_nodelist *ndlp;
3072
3073         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3074
3075         ndlp = lpfc_findnode_did(vport, nportid);
3076         if (!ndlp) {
3077                 ndlp = lpfc_nlp_init(vport, nportid);
3078                 if (!ndlp)
3079                         return 1;
3080                 lpfc_enqueue_node(vport, ndlp);
3081         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3082                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3083                 if (!ndlp)
3084                         return 1;
3085         }
3086
3087         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3088                                      ndlp->nlp_DID, ELS_CMD_SCR);
3089
3090         if (!elsiocb) {
3091                 /* This will trigger the release of the node just
3092                  * allocated
3093                  */
3094                 lpfc_nlp_put(ndlp);
3095                 return 1;
3096         }
3097
3098         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3099
3100         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3101         pcmd += sizeof(uint32_t);
3102
3103         /* For SCR, remainder of payload is SCR parameter page */
3104         memset(pcmd, 0, sizeof(SCR));
3105         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3106
3107         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3108                 "Issue SCR:       did:x%x",
3109                 ndlp->nlp_DID, 0, 0);
3110
3111         phba->fc_stat.elsXmitSCR++;
3112         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3113         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3114             IOCB_ERROR) {
3115                 /* The additional lpfc_nlp_put will cause the following
3116                  * lpfc_els_free_iocb routine to trigger the rlease of
3117                  * the node.
3118                  */
3119                 lpfc_nlp_put(ndlp);
3120                 lpfc_els_free_iocb(phba, elsiocb);
3121                 return 1;
3122         }
3123         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3124          * trigger the release of node.
3125          */
3126         if (!(vport->fc_flag & FC_PT2PT))
3127                 lpfc_nlp_put(ndlp);
3128         return 0;
3129 }
3130
3131 /**
3132  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3133  *   or the other nport (pt2pt).
3134  * @vport: pointer to a host virtual N_Port data structure.
3135  * @retry: number of retries to the command IOCB.
3136  *
3137  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3138  *  when connected to a fabric, or to the remote port when connected
3139  *  in point-to-point mode. When sent to the Fabric Controller, it will
3140  *  replay the RSCN to registered recipients.
3141  *
3142  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3143  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3144  * will be stored into the context1 field of the IOCB for the completion
3145  * callback function to the RSCN ELS command.
3146  *
3147  * Return code
3148  *   0 - Successfully issued RSCN command
3149  *   1 - Failed to issue RSCN command
3150  **/
3151 int
3152 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3153 {
3154         struct lpfc_hba *phba = vport->phba;
3155         struct lpfc_iocbq *elsiocb;
3156         struct lpfc_nodelist *ndlp;
3157         struct {
3158                 struct fc_els_rscn rscn;
3159                 struct fc_els_rscn_page portid;
3160         } *event;
3161         uint32_t nportid;
3162         uint16_t cmdsize = sizeof(*event);
3163
3164         /* Not supported for private loop */
3165         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3166             !(vport->fc_flag & FC_PUBLIC_LOOP))
3167                 return 1;
3168
3169         if (vport->fc_flag & FC_PT2PT) {
3170                 /* find any mapped nport - that would be the other nport */
3171                 ndlp = lpfc_findnode_mapped(vport);
3172                 if (!ndlp)
3173                         return 1;
3174         } else {
3175                 nportid = FC_FID_FCTRL;
3176                 /* find the fabric controller node */
3177                 ndlp = lpfc_findnode_did(vport, nportid);
3178                 if (!ndlp) {
3179                         /* if one didn't exist, make one */
3180                         ndlp = lpfc_nlp_init(vport, nportid);
3181                         if (!ndlp)
3182                                 return 1;
3183                         lpfc_enqueue_node(vport, ndlp);
3184                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3185                         ndlp = lpfc_enable_node(vport, ndlp,
3186                                                 NLP_STE_UNUSED_NODE);
3187                         if (!ndlp)
3188                                 return 1;
3189                 }
3190         }
3191
3192         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3193                                      ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3194
3195         if (!elsiocb) {
3196                 /* This will trigger the release of the node just
3197                  * allocated
3198                  */
3199                 lpfc_nlp_put(ndlp);
3200                 return 1;
3201         }
3202
3203         event = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3204
3205         event->rscn.rscn_cmd = ELS_RSCN;
3206         event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3207         event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3208
3209         nportid = vport->fc_myDID;
3210         /* appears that page flags must be 0 for fabric to broadcast RSCN */
3211         event->portid.rscn_page_flags = 0;
3212         event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3213         event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3214         event->portid.rscn_fid[2] = nportid & 0x000000FF;
3215
3216         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3217                               "Issue RSCN:       did:x%x",
3218                               ndlp->nlp_DID, 0, 0);
3219
3220         phba->fc_stat.elsXmitRSCN++;
3221         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3222         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3223             IOCB_ERROR) {
3224                 /* The additional lpfc_nlp_put will cause the following
3225                  * lpfc_els_free_iocb routine to trigger the rlease of
3226                  * the node.
3227                  */
3228                 lpfc_nlp_put(ndlp);
3229                 lpfc_els_free_iocb(phba, elsiocb);
3230                 return 1;
3231         }
3232         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3233          * trigger the release of node.
3234          */
3235         if (!(vport->fc_flag & FC_PT2PT))
3236                 lpfc_nlp_put(ndlp);
3237
3238         return 0;
3239 }
3240
3241 /**
3242  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3243  * @vport: pointer to a host virtual N_Port data structure.
3244  * @nportid: N_Port identifier to the remote node.
3245  * @retry: number of retries to the command IOCB.
3246  *
3247  * This routine issues a Fibre Channel Address Resolution Response
3248  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3249  * is passed into the function. It first search the @vport node list to find
3250  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3251  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3252  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3253  *
3254  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3255  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3256  * will be stored into the context1 field of the IOCB for the completion
3257  * callback function to the PARPR ELS command.
3258  *
3259  * Return code
3260  *   0 - Successfully issued farpr command
3261  *   1 - Failed to issue farpr command
3262  **/
3263 static int
3264 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3265 {
3266         struct lpfc_hba  *phba = vport->phba;
3267         struct lpfc_iocbq *elsiocb;
3268         FARP *fp;
3269         uint8_t *pcmd;
3270         uint32_t *lp;
3271         uint16_t cmdsize;
3272         struct lpfc_nodelist *ondlp;
3273         struct lpfc_nodelist *ndlp;
3274
3275         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3276
3277         ndlp = lpfc_findnode_did(vport, nportid);
3278         if (!ndlp) {
3279                 ndlp = lpfc_nlp_init(vport, nportid);
3280                 if (!ndlp)
3281                         return 1;
3282                 lpfc_enqueue_node(vport, ndlp);
3283         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3284                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3285                 if (!ndlp)
3286                         return 1;
3287         }
3288
3289         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3290                                      ndlp->nlp_DID, ELS_CMD_RNID);
3291         if (!elsiocb) {
3292                 /* This will trigger the release of the node just
3293                  * allocated
3294                  */
3295                 lpfc_nlp_put(ndlp);
3296                 return 1;
3297         }
3298
3299         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3300
3301         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3302         pcmd += sizeof(uint32_t);
3303
3304         /* Fill in FARPR payload */
3305         fp = (FARP *) (pcmd);
3306         memset(fp, 0, sizeof(FARP));
3307         lp = (uint32_t *) pcmd;
3308         *lp++ = be32_to_cpu(nportid);
3309         *lp++ = be32_to_cpu(vport->fc_myDID);
3310         fp->Rflags = 0;
3311         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3312
3313         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3314         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3315         ondlp = lpfc_findnode_did(vport, nportid);
3316         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
3317                 memcpy(&fp->OportName, &ondlp->nlp_portname,
3318                        sizeof(struct lpfc_name));
3319                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3320                        sizeof(struct lpfc_name));
3321         }
3322
3323         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3324                 "Issue FARPR:     did:x%x",
3325                 ndlp->nlp_DID, 0, 0);
3326
3327         phba->fc_stat.elsXmitFARPR++;
3328         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3329         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3330             IOCB_ERROR) {
3331                 /* The additional lpfc_nlp_put will cause the following
3332                  * lpfc_els_free_iocb routine to trigger the release of
3333                  * the node.
3334                  */
3335                 lpfc_nlp_put(ndlp);
3336                 lpfc_els_free_iocb(phba, elsiocb);
3337                 return 1;
3338         }
3339         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3340          * trigger the release of the node.
3341          */
3342         lpfc_nlp_put(ndlp);
3343         return 0;
3344 }
3345
3346 /**
3347  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
3348  * @vport: pointer to a host virtual N_Port data structure.
3349  * @nlp: pointer to a node-list data structure.
3350  *
3351  * This routine cancels the timer with a delayed IOCB-command retry for
3352  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
3353  * removes the ELS retry event if it presents. In addition, if the
3354  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
3355  * commands are sent for the @vport's nodes that require issuing discovery
3356  * ADISC.
3357  **/
3358 void
3359 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
3360 {
3361         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3362         struct lpfc_work_evt *evtp;
3363
3364         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
3365                 return;
3366         spin_lock_irq(shost->host_lock);
3367         nlp->nlp_flag &= ~NLP_DELAY_TMO;
3368         spin_unlock_irq(shost->host_lock);
3369         del_timer_sync(&nlp->nlp_delayfunc);
3370         nlp->nlp_last_elscmd = 0;
3371         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
3372                 list_del_init(&nlp->els_retry_evt.evt_listp);
3373                 /* Decrement nlp reference count held for the delayed retry */
3374                 evtp = &nlp->els_retry_evt;
3375                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
3376         }
3377         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
3378                 spin_lock_irq(shost->host_lock);
3379                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3380                 spin_unlock_irq(shost->host_lock);
3381                 if (vport->num_disc_nodes) {
3382                         if (vport->port_state < LPFC_VPORT_READY) {
3383                                 /* Check if there are more ADISCs to be sent */
3384                                 lpfc_more_adisc(vport);
3385                         } else {
3386                                 /* Check if there are more PLOGIs to be sent */
3387                                 lpfc_more_plogi(vport);
3388                                 if (vport->num_disc_nodes == 0) {
3389                                         spin_lock_irq(shost->host_lock);
3390                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
3391                                         spin_unlock_irq(shost->host_lock);
3392                                         lpfc_can_disctmo(vport);
3393                                         lpfc_end_rscn(vport);
3394                                 }
3395                         }
3396                 }
3397         }
3398         return;
3399 }
3400
3401 /**
3402  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
3403  * @ptr: holder for the pointer to the timer function associated data (ndlp).
3404  *
3405  * This routine is invoked by the ndlp delayed-function timer to check
3406  * whether there is any pending ELS retry event(s) with the node. If not, it
3407  * simply returns. Otherwise, if there is at least one ELS delayed event, it
3408  * adds the delayed events to the HBA work list and invokes the
3409  * lpfc_worker_wake_up() routine to wake up worker thread to process the
3410  * event. Note that lpfc_nlp_get() is called before posting the event to
3411  * the work list to hold reference count of ndlp so that it guarantees the
3412  * reference to ndlp will still be available when the worker thread gets
3413  * to the event associated with the ndlp.
3414  **/
3415 void
3416 lpfc_els_retry_delay(struct timer_list *t)
3417 {
3418         struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
3419         struct lpfc_vport *vport = ndlp->vport;
3420         struct lpfc_hba   *phba = vport->phba;
3421         unsigned long flags;
3422         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
3423
3424         spin_lock_irqsave(&phba->hbalock, flags);
3425         if (!list_empty(&evtp->evt_listp)) {
3426                 spin_unlock_irqrestore(&phba->hbalock, flags);
3427                 return;
3428         }
3429
3430         /* We need to hold the node by incrementing the reference
3431          * count until the queued work is done
3432          */
3433         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
3434         if (evtp->evt_arg1) {
3435                 evtp->evt = LPFC_EVT_ELS_RETRY;
3436                 list_add_tail(&evtp->evt_listp, &phba->work_list);
3437                 lpfc_worker_wake_up(phba);
3438         }
3439         spin_unlock_irqrestore(&phba->hbalock, flags);
3440         return;
3441 }
3442
3443 /**
3444  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
3445  * @ndlp: pointer to a node-list data structure.
3446  *
3447  * This routine is the worker-thread handler for processing the @ndlp delayed
3448  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3449  * the last ELS command from the associated ndlp and invokes the proper ELS
3450  * function according to the delayed ELS command to retry the command.
3451  **/
3452 void
3453 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3454 {
3455         struct lpfc_vport *vport = ndlp->vport;
3456         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3457         uint32_t cmd, retry;
3458
3459         spin_lock_irq(shost->host_lock);
3460         cmd = ndlp->nlp_last_elscmd;
3461         ndlp->nlp_last_elscmd = 0;
3462
3463         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
3464                 spin_unlock_irq(shost->host_lock);
3465                 return;
3466         }
3467
3468         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3469         spin_unlock_irq(shost->host_lock);
3470         /*
3471          * If a discovery event readded nlp_delayfunc after timer
3472          * firing and before processing the timer, cancel the
3473          * nlp_delayfunc.
3474          */
3475         del_timer_sync(&ndlp->nlp_delayfunc);
3476         retry = ndlp->nlp_retry;
3477         ndlp->nlp_retry = 0;
3478
3479         switch (cmd) {
3480         case ELS_CMD_FLOGI:
3481                 lpfc_issue_els_flogi(vport, ndlp, retry);
3482                 break;
3483         case ELS_CMD_PLOGI:
3484                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
3485                         ndlp->nlp_prev_state = ndlp->nlp_state;
3486                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3487                 }
3488                 break;
3489         case ELS_CMD_ADISC:
3490                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
3491                         ndlp->nlp_prev_state = ndlp->nlp_state;
3492                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3493                 }
3494                 break;
3495         case ELS_CMD_PRLI:
3496         case ELS_CMD_NVMEPRLI:
3497                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
3498                         ndlp->nlp_prev_state = ndlp->nlp_state;
3499                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3500                 }
3501                 break;
3502         case ELS_CMD_LOGO:
3503                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
3504                         ndlp->nlp_prev_state = ndlp->nlp_state;
3505                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3506                 }
3507                 break;
3508         case ELS_CMD_FDISC:
3509                 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3510                         lpfc_issue_els_fdisc(vport, ndlp, retry);
3511                 break;
3512         }
3513         return;
3514 }
3515
3516 /**
3517  * lpfc_link_reset - Issue link reset
3518  * @vport: pointer to a virtual N_Port data structure.
3519  *
3520  * This routine performs link reset by sending INIT_LINK mailbox command.
3521  * For SLI-3 adapter, link attention interrupt is enabled before issuing
3522  * INIT_LINK mailbox command.
3523  *
3524  * Return code
3525  *   0 - Link reset initiated successfully
3526  *   1 - Failed to initiate link reset
3527  **/
3528 int
3529 lpfc_link_reset(struct lpfc_vport *vport)
3530 {
3531         struct lpfc_hba *phba = vport->phba;
3532         LPFC_MBOXQ_t *mbox;
3533         uint32_t control;
3534         int rc;
3535
3536         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3537                          "2851 Attempt link reset\n");
3538         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3539         if (!mbox) {
3540                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3541                                 "2852 Failed to allocate mbox memory");
3542                 return 1;
3543         }
3544
3545         /* Enable Link attention interrupts */
3546         if (phba->sli_rev <= LPFC_SLI_REV3) {
3547                 spin_lock_irq(&phba->hbalock);
3548                 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3549                 control = readl(phba->HCregaddr);
3550                 control |= HC_LAINT_ENA;
3551                 writel(control, phba->HCregaddr);
3552                 readl(phba->HCregaddr); /* flush */
3553                 spin_unlock_irq(&phba->hbalock);
3554         }
3555
3556         lpfc_init_link(phba, mbox, phba->cfg_topology,
3557                        phba->cfg_link_speed);
3558         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3559         mbox->vport = vport;
3560         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3561         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
3562                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3563                                 "2853 Failed to issue INIT_LINK "
3564                                 "mbox command, rc:x%x\n", rc);
3565                 mempool_free(mbox, phba->mbox_mem_pool);
3566                 return 1;
3567         }
3568
3569         return 0;
3570 }
3571
3572 /**
3573  * lpfc_els_retry - Make retry decision on an els command iocb
3574  * @phba: pointer to lpfc hba data structure.
3575  * @cmdiocb: pointer to lpfc command iocb data structure.
3576  * @rspiocb: pointer to lpfc response iocb data structure.
3577  *
3578  * This routine makes a retry decision on an ELS command IOCB, which has
3579  * failed. The following ELS IOCBs use this function for retrying the command
3580  * when previously issued command responsed with error status: FLOGI, PLOGI,
3581  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3582  * returned error status, it makes the decision whether a retry shall be
3583  * issued for the command, and whether a retry shall be made immediately or
3584  * delayed. In the former case, the corresponding ELS command issuing-function
3585  * is called to retry the command. In the later case, the ELS command shall
3586  * be posted to the ndlp delayed event and delayed function timer set to the
3587  * ndlp for the delayed command issusing.
3588  *
3589  * Return code
3590  *   0 - No retry of els command is made
3591  *   1 - Immediate or delayed retry of els command is made
3592  **/
3593 static int
3594 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3595                struct lpfc_iocbq *rspiocb)
3596 {
3597         struct lpfc_vport *vport = cmdiocb->vport;
3598         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3599         IOCB_t *irsp = &rspiocb->iocb;
3600         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3601         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3602         uint32_t *elscmd;
3603         struct ls_rjt stat;
3604         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
3605         int logerr = 0;
3606         uint32_t cmd = 0;
3607         uint32_t did;
3608         int link_reset = 0, rc;
3609
3610
3611         /* Note: context2 may be 0 for internal driver abort
3612          * of delays ELS command.
3613          */
3614
3615         if (pcmd && pcmd->virt) {
3616                 elscmd = (uint32_t *) (pcmd->virt);
3617                 cmd = *elscmd++;
3618         }
3619
3620         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3621                 did = ndlp->nlp_DID;
3622         else {
3623                 /* We should only hit this case for retrying PLOGI */
3624                 did = irsp->un.elsreq64.remoteID;
3625                 ndlp = lpfc_findnode_did(vport, did);
3626                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3627                     && (cmd != ELS_CMD_PLOGI))
3628                         return 1;
3629         }
3630
3631         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3632                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
3633                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3634
3635         switch (irsp->ulpStatus) {
3636         case IOSTAT_FCP_RSP_ERROR:
3637                 break;
3638         case IOSTAT_REMOTE_STOP:
3639                 if (phba->sli_rev == LPFC_SLI_REV4) {
3640                         /* This IO was aborted by the target, we don't
3641                          * know the rxid and because we did not send the
3642                          * ABTS we cannot generate and RRQ.
3643                          */
3644                         lpfc_set_rrq_active(phba, ndlp,
3645                                          cmdiocb->sli4_lxritag, 0, 0);
3646                 }
3647                 break;
3648         case IOSTAT_LOCAL_REJECT:
3649                 switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
3650                 case IOERR_LOOP_OPEN_FAILURE:
3651                         if (cmd == ELS_CMD_FLOGI) {
3652                                 if (PCI_DEVICE_ID_HORNET ==
3653                                         phba->pcidev->device) {
3654                                         phba->fc_topology = LPFC_TOPOLOGY_LOOP;
3655                                         phba->pport->fc_myDID = 0;
3656                                         phba->alpa_map[0] = 0;
3657                                         phba->alpa_map[1] = 0;
3658                                 }
3659                         }
3660                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
3661                                 delay = 1000;
3662                         retry = 1;
3663                         break;
3664
3665                 case IOERR_ILLEGAL_COMMAND:
3666                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3667                                          "0124 Retry illegal cmd x%x "
3668                                          "retry:x%x delay:x%x\n",
3669                                          cmd, cmdiocb->retry, delay);
3670                         retry = 1;
3671                         /* All command's retry policy */
3672                         maxretry = 8;
3673                         if (cmdiocb->retry > 2)
3674                                 delay = 1000;
3675                         break;
3676
3677                 case IOERR_NO_RESOURCES:
3678                         logerr = 1; /* HBA out of resources */
3679                         retry = 1;
3680                         if (cmdiocb->retry > 100)
3681                                 delay = 100;
3682                         maxretry = 250;
3683                         break;
3684
3685                 case IOERR_ILLEGAL_FRAME:
3686                         delay = 100;
3687                         retry = 1;
3688                         break;
3689
3690                 case IOERR_INVALID_RPI:
3691                         if (cmd == ELS_CMD_PLOGI &&
3692                             did == NameServer_DID) {
3693                                 /* Continue forever if plogi to */
3694                                 /* the nameserver fails */
3695                                 maxretry = 0;
3696                                 delay = 100;
3697                         }
3698                         retry = 1;
3699                         break;
3700
3701                 case IOERR_SEQUENCE_TIMEOUT:
3702                         if (cmd == ELS_CMD_PLOGI &&
3703                             did == NameServer_DID &&
3704                             (cmdiocb->retry + 1) == maxretry) {
3705                                 /* Reset the Link */
3706                                 link_reset = 1;
3707                                 break;
3708                         }
3709                         retry = 1;
3710                         delay = 100;
3711                         break;
3712                 }
3713                 break;
3714
3715         case IOSTAT_NPORT_RJT:
3716         case IOSTAT_FABRIC_RJT:
3717                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3718                         retry = 1;
3719                         break;
3720                 }
3721                 break;
3722
3723         case IOSTAT_NPORT_BSY:
3724         case IOSTAT_FABRIC_BSY:
3725                 logerr = 1; /* Fabric / Remote NPort out of resources */
3726                 retry = 1;
3727                 break;
3728
3729         case IOSTAT_LS_RJT:
3730                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3731                 /* Added for Vendor specifc support
3732                  * Just keep retrying for these Rsn / Exp codes
3733                  */
3734                 switch (stat.un.b.lsRjtRsnCode) {
3735                 case LSRJT_UNABLE_TPC:
3736                         /* The driver has a VALID PLOGI but the rport has
3737                          * rejected the PRLI - can't do it now.  Delay
3738                          * for 1 second and try again - don't care about
3739                          * the explanation.
3740                          */
3741                         if (cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) {
3742                                 delay = 1000;
3743                                 maxretry = lpfc_max_els_tries + 1;
3744                                 retry = 1;
3745                                 break;
3746                         }
3747
3748                         /* Legacy bug fix code for targets with PLOGI delays. */
3749                         if (stat.un.b.lsRjtRsnCodeExp ==
3750                             LSEXP_CMD_IN_PROGRESS) {
3751                                 if (cmd == ELS_CMD_PLOGI) {
3752                                         delay = 1000;
3753                                         maxretry = 48;
3754                                 }
3755                                 retry = 1;
3756                                 break;
3757                         }
3758                         if (stat.un.b.lsRjtRsnCodeExp ==
3759                             LSEXP_CANT_GIVE_DATA) {
3760                                 if (cmd == ELS_CMD_PLOGI) {
3761                                         delay = 1000;
3762                                         maxretry = 48;
3763                                 }
3764                                 retry = 1;
3765                                 break;
3766                         }
3767                         if (cmd == ELS_CMD_PLOGI) {
3768                                 delay = 1000;
3769                                 maxretry = lpfc_max_els_tries + 1;
3770                                 retry = 1;
3771                                 break;
3772                         }
3773                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3774                           (cmd == ELS_CMD_FDISC) &&
3775                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
3776                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3777                                                  "0125 FDISC Failed (x%x). "
3778                                                  "Fabric out of resources\n",
3779                                                  stat.un.lsRjtError);
3780                                 lpfc_vport_set_state(vport,
3781                                                      FC_VPORT_NO_FABRIC_RSCS);
3782                         }
3783                         break;
3784
3785                 case LSRJT_LOGICAL_BSY:
3786                         if ((cmd == ELS_CMD_PLOGI) ||
3787                             (cmd == ELS_CMD_PRLI) ||
3788                             (cmd == ELS_CMD_NVMEPRLI)) {
3789                                 delay = 1000;
3790                                 maxretry = 48;
3791                         } else if (cmd == ELS_CMD_FDISC) {
3792                                 /* FDISC retry policy */
3793                                 maxretry = 48;
3794                                 if (cmdiocb->retry >= 32)
3795                                         delay = 1000;
3796                         }
3797                         retry = 1;
3798                         break;
3799
3800                 case LSRJT_LOGICAL_ERR:
3801                         /* There are some cases where switches return this
3802                          * error when they are not ready and should be returning
3803                          * Logical Busy. We should delay every time.
3804                          */
3805                         if (cmd == ELS_CMD_FDISC &&
3806                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3807                                 maxretry = 3;
3808                                 delay = 1000;
3809                                 retry = 1;
3810                         } else if (cmd == ELS_CMD_FLOGI &&
3811                                    stat.un.b.lsRjtRsnCodeExp ==
3812                                                 LSEXP_NOTHING_MORE) {
3813                                 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
3814                                 retry = 1;
3815                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3816                                                  "0820 FLOGI Failed (x%x). "
3817                                                  "BBCredit Not Supported\n",
3818                                                  stat.un.lsRjtError);
3819                         }
3820                         break;
3821
3822                 case LSRJT_PROTOCOL_ERR:
3823                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3824                           (cmd == ELS_CMD_FDISC) &&
3825                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3826                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3827                           ) {
3828                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3829                                                  "0122 FDISC Failed (x%x). "
3830                                                  "Fabric Detected Bad WWN\n",
3831                                                  stat.un.lsRjtError);
3832                                 lpfc_vport_set_state(vport,
3833                                                      FC_VPORT_FABRIC_REJ_WWN);
3834                         }
3835                         break;
3836                 case LSRJT_VENDOR_UNIQUE:
3837                         if ((stat.un.b.vendorUnique == 0x45) &&
3838                             (cmd == ELS_CMD_FLOGI)) {
3839                                 goto out_retry;
3840                         }
3841                         break;
3842                 case LSRJT_CMD_UNSUPPORTED:
3843                         /* lpfc nvmet returns this type of LS_RJT when it
3844                          * receives an FCP PRLI because lpfc nvmet only
3845                          * support NVME.  ELS request is terminated for FCP4
3846                          * on this rport.
3847                          */
3848                         if (stat.un.b.lsRjtRsnCodeExp ==
3849                             LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
3850                                 spin_lock_irq(shost->host_lock);
3851                                 ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
3852                                 spin_unlock_irq(shost->host_lock);
3853                                 retry = 0;
3854                                 goto out_retry;
3855                         }
3856                         break;
3857                 }
3858                 break;
3859
3860         case IOSTAT_INTERMED_RSP:
3861         case IOSTAT_BA_RJT:
3862                 break;
3863
3864         default:
3865                 break;
3866         }
3867
3868         if (link_reset) {
3869                 rc = lpfc_link_reset(vport);
3870                 if (rc) {
3871                         /* Do not give up. Retry PLOGI one more time and attempt
3872                          * link reset if PLOGI fails again.
3873                          */
3874                         retry = 1;
3875                         delay = 100;
3876                         goto out_retry;
3877                 }
3878                 return 1;
3879         }
3880
3881         if (did == FDMI_DID)
3882                 retry = 1;
3883
3884         if ((cmd == ELS_CMD_FLOGI) &&
3885             (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
3886             !lpfc_error_lost_link(irsp)) {
3887                 /* FLOGI retry policy */
3888                 retry = 1;
3889                 /* retry FLOGI forever */
3890                 if (phba->link_flag != LS_LOOPBACK_MODE)
3891                         maxretry = 0;
3892                 else
3893                         maxretry = 2;
3894
3895                 if (cmdiocb->retry >= 100)
3896                         delay = 5000;
3897                 else if (cmdiocb->retry >= 32)
3898                         delay = 1000;
3899         } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
3900                 /* retry FDISCs every second up to devloss */
3901                 retry = 1;
3902                 maxretry = vport->cfg_devloss_tmo;
3903                 delay = 1000;
3904         }
3905
3906         cmdiocb->retry++;
3907         if (maxretry && (cmdiocb->retry >= maxretry)) {
3908                 phba->fc_stat.elsRetryExceeded++;
3909                 retry = 0;
3910         }
3911
3912         if ((vport->load_flag & FC_UNLOADING) != 0)
3913                 retry = 0;
3914
3915 out_retry:
3916         if (retry) {
3917                 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3918                         /* Stop retrying PLOGI and FDISC if in FCF discovery */
3919                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3920                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3921                                                  "2849 Stop retry ELS command "
3922                                                  "x%x to remote NPORT x%x, "
3923                                                  "Data: x%x x%x\n", cmd, did,
3924                                                  cmdiocb->retry, delay);
3925                                 return 0;
3926                         }
3927                 }
3928
3929                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
3930                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3931                                  "0107 Retry ELS command x%x to remote "
3932                                  "NPORT x%x Data: x%x x%x\n",
3933                                  cmd, did, cmdiocb->retry, delay);
3934
3935                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3936                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
3937                         ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
3938                         IOERR_NO_RESOURCES))) {
3939                         /* Don't reset timer for no resources */
3940
3941                         /* If discovery / RSCN timer is running, reset it */
3942                         if (timer_pending(&vport->fc_disctmo) ||
3943                             (vport->fc_flag & FC_RSCN_MODE))
3944                                 lpfc_set_disctmo(vport);
3945                 }
3946
3947                 phba->fc_stat.elsXmitRetry++;
3948                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
3949                         phba->fc_stat.elsDelayRetry++;
3950                         ndlp->nlp_retry = cmdiocb->retry;
3951
3952                         /* delay is specified in milliseconds */
3953                         mod_timer(&ndlp->nlp_delayfunc,
3954                                 jiffies + msecs_to_jiffies(delay));
3955                         spin_lock_irq(shost->host_lock);
3956                         ndlp->nlp_flag |= NLP_DELAY_TMO;
3957                         spin_unlock_irq(shost->host_lock);
3958
3959                         ndlp->nlp_prev_state = ndlp->nlp_state;
3960                         if ((cmd == ELS_CMD_PRLI) ||
3961                             (cmd == ELS_CMD_NVMEPRLI))
3962                                 lpfc_nlp_set_state(vport, ndlp,
3963                                         NLP_STE_PRLI_ISSUE);
3964                         else
3965                                 lpfc_nlp_set_state(vport, ndlp,
3966                                         NLP_STE_NPR_NODE);
3967                         ndlp->nlp_last_elscmd = cmd;
3968
3969                         return 1;
3970                 }
3971                 switch (cmd) {
3972                 case ELS_CMD_FLOGI:
3973                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
3974                         return 1;
3975                 case ELS_CMD_FDISC:
3976                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3977                         return 1;
3978                 case ELS_CMD_PLOGI:
3979                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3980                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3981                                 lpfc_nlp_set_state(vport, ndlp,
3982                                                    NLP_STE_PLOGI_ISSUE);
3983                         }
3984                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
3985                         return 1;
3986                 case ELS_CMD_ADISC:
3987                         ndlp->nlp_prev_state = ndlp->nlp_state;
3988                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3989                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
3990                         return 1;
3991                 case ELS_CMD_PRLI:
3992                 case ELS_CMD_NVMEPRLI:
3993                         ndlp->nlp_prev_state = ndlp->nlp_state;
3994                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3995                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
3996                         return 1;
3997                 case ELS_CMD_LOGO:
3998                         ndlp->nlp_prev_state = ndlp->nlp_state;
3999                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4000                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
4001                         return 1;
4002                 }
4003         }
4004         /* No retry ELS command <elsCmd> to remote NPORT <did> */
4005         if (logerr) {
4006                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4007                          "0137 No retry ELS command x%x to remote "
4008                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
4009                          cmd, did, irsp->ulpStatus,
4010                          irsp->un.ulpWord[4]);
4011         }
4012         else {
4013                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4014                          "0108 No retry ELS command x%x to remote "
4015                          "NPORT x%x Retried:%d Error:x%x/%x\n",
4016                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
4017                          irsp->un.ulpWord[4]);
4018         }
4019         return 0;
4020 }
4021
4022 /**
4023  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
4024  * @phba: pointer to lpfc hba data structure.
4025  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
4026  *
4027  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
4028  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
4029  * checks to see whether there is a lpfc DMA buffer associated with the
4030  * response of the command IOCB. If so, it will be released before releasing
4031  * the lpfc DMA buffer associated with the IOCB itself.
4032  *
4033  * Return code
4034  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4035  **/
4036 static int
4037 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
4038 {
4039         struct lpfc_dmabuf *buf_ptr;
4040
4041         /* Free the response before processing the command. */
4042         if (!list_empty(&buf_ptr1->list)) {
4043                 list_remove_head(&buf_ptr1->list, buf_ptr,
4044                                  struct lpfc_dmabuf,
4045                                  list);
4046                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4047                 kfree(buf_ptr);
4048         }
4049         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
4050         kfree(buf_ptr1);
4051         return 0;
4052 }
4053
4054 /**
4055  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
4056  * @phba: pointer to lpfc hba data structure.
4057  * @buf_ptr: pointer to the lpfc dma buffer data structure.
4058  *
4059  * This routine releases the lpfc Direct Memory Access (DMA) buffer
4060  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
4061  * pool.
4062  *
4063  * Return code
4064  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4065  **/
4066 static int
4067 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
4068 {
4069         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4070         kfree(buf_ptr);
4071         return 0;
4072 }
4073
4074 /**
4075  * lpfc_els_free_iocb - Free a command iocb and its associated resources
4076  * @phba: pointer to lpfc hba data structure.
4077  * @elsiocb: pointer to lpfc els command iocb data structure.
4078  *
4079  * This routine frees a command IOCB and its associated resources. The
4080  * command IOCB data structure contains the reference to various associated
4081  * resources, these fields must be set to NULL if the associated reference
4082  * not present:
4083  *   context1 - reference to ndlp
4084  *   context2 - reference to cmd
4085  *   context2->next - reference to rsp
4086  *   context3 - reference to bpl
4087  *
4088  * It first properly decrements the reference count held on ndlp for the
4089  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
4090  * set, it invokes the lpfc_els_free_data() routine to release the Direct
4091  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
4092  * adds the DMA buffer the @phba data structure for the delayed release.
4093  * If reference to the Buffer Pointer List (BPL) is present, the
4094  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
4095  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
4096  * invoked to release the IOCB data structure back to @phba IOCBQ list.
4097  *
4098  * Return code
4099  *   0 - Success (currently, always return 0)
4100  **/
4101 int
4102 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
4103 {
4104         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
4105         struct lpfc_nodelist *ndlp;
4106
4107         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
4108         if (ndlp) {
4109                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
4110                         lpfc_nlp_put(ndlp);
4111
4112                         /* If the ndlp is not being used by another discovery
4113                          * thread, free it.
4114                          */
4115                         if (!lpfc_nlp_not_used(ndlp)) {
4116                                 /* If ndlp is being used by another discovery
4117                                  * thread, just clear NLP_DEFER_RM
4118                                  */
4119                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
4120                         }
4121                 }
4122                 else
4123                         lpfc_nlp_put(ndlp);
4124                 elsiocb->context1 = NULL;
4125         }
4126         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
4127         if (elsiocb->context2) {
4128                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
4129                         /* Firmware could still be in progress of DMAing
4130                          * payload, so don't free data buffer till after
4131                          * a hbeat.
4132                          */
4133                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
4134                         buf_ptr = elsiocb->context2;
4135                         elsiocb->context2 = NULL;
4136                         if (buf_ptr) {
4137                                 buf_ptr1 = NULL;
4138                                 spin_lock_irq(&phba->hbalock);
4139                                 if (!list_empty(&buf_ptr->list)) {
4140                                         list_remove_head(&buf_ptr->list,
4141                                                 buf_ptr1, struct lpfc_dmabuf,
4142                                                 list);
4143                                         INIT_LIST_HEAD(&buf_ptr1->list);
4144                                         list_add_tail(&buf_ptr1->list,
4145                                                 &phba->elsbuf);
4146                                         phba->elsbuf_cnt++;
4147                                 }
4148                                 INIT_LIST_HEAD(&buf_ptr->list);
4149                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
4150                                 phba->elsbuf_cnt++;
4151                                 spin_unlock_irq(&phba->hbalock);
4152                         }
4153                 } else {
4154                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
4155                         lpfc_els_free_data(phba, buf_ptr1);
4156                         elsiocb->context2 = NULL;
4157                 }
4158         }
4159
4160         if (elsiocb->context3) {
4161                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
4162                 lpfc_els_free_bpl(phba, buf_ptr);
4163                 elsiocb->context3 = NULL;
4164         }
4165         lpfc_sli_release_iocbq(phba, elsiocb);
4166         return 0;
4167 }
4168
4169 /**
4170  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
4171  * @phba: pointer to lpfc hba data structure.
4172  * @cmdiocb: pointer to lpfc command iocb data structure.
4173  * @rspiocb: pointer to lpfc response iocb data structure.
4174  *
4175  * This routine is the completion callback function to the Logout (LOGO)
4176  * Accept (ACC) Response ELS command. This routine is invoked to indicate
4177  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
4178  * release the ndlp if it has the last reference remaining (reference count
4179  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
4180  * field to NULL to inform the following lpfc_els_free_iocb() routine no
4181  * ndlp reference count needs to be decremented. Otherwise, the ndlp
4182  * reference use-count shall be decremented by the lpfc_els_free_iocb()
4183  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
4184  * IOCB data structure.
4185  **/
4186 static void
4187 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4188                        struct lpfc_iocbq *rspiocb)
4189 {
4190         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4191         struct lpfc_vport *vport = cmdiocb->vport;
4192         IOCB_t *irsp;
4193
4194         irsp = &rspiocb->iocb;
4195         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4196                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
4197                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
4198         /* ACC to LOGO completes to NPort <nlp_DID> */
4199         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4200                          "0109 ACC to LOGO completes to NPort x%x "
4201                          "Data: x%x x%x x%x\n",
4202                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4203                          ndlp->nlp_rpi);
4204
4205         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
4206                 /* NPort Recovery mode or node is just allocated */
4207                 if (!lpfc_nlp_not_used(ndlp)) {
4208                         /* If the ndlp is being used by another discovery
4209                          * thread, just unregister the RPI.
4210                          */
4211                         lpfc_unreg_rpi(vport, ndlp);
4212                 } else {
4213                         /* Indicate the node has already released, should
4214                          * not reference to it from within lpfc_els_free_iocb.
4215                          */
4216                         cmdiocb->context1 = NULL;
4217                 }
4218         }
4219
4220         /*
4221          * The driver received a LOGO from the rport and has ACK'd it.
4222          * At this point, the driver is done so release the IOCB
4223          */
4224         lpfc_els_free_iocb(phba, cmdiocb);
4225 }
4226
4227 /**
4228  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
4229  * @phba: pointer to lpfc hba data structure.
4230  * @pmb: pointer to the driver internal queue element for mailbox command.
4231  *
4232  * This routine is the completion callback function for unregister default
4233  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
4234  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
4235  * decrements the ndlp reference count held for this completion callback
4236  * function. After that, it invokes the lpfc_nlp_not_used() to check
4237  * whether there is only one reference left on the ndlp. If so, it will
4238  * perform one more decrement and trigger the release of the ndlp.
4239  **/
4240 void
4241 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4242 {
4243         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4244         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4245
4246         pmb->ctx_buf = NULL;
4247         pmb->ctx_ndlp = NULL;
4248
4249         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4250         kfree(mp);
4251         mempool_free(pmb, phba->mbox_mem_pool);
4252         if (ndlp) {
4253                 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4254                                  "0006 rpi%x DID:%x flg:%x %d map:%x x%px\n",
4255                                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4256                                  kref_read(&ndlp->kref),
4257                                  ndlp->nlp_usg_map, ndlp);
4258                 if (NLP_CHK_NODE_ACT(ndlp)) {
4259                         lpfc_nlp_put(ndlp);
4260                         /* This is the end of the default RPI cleanup logic for
4261                          * this ndlp. If no other discovery threads are using
4262                          * this ndlp, free all resources associated with it.
4263                          */
4264                         lpfc_nlp_not_used(ndlp);
4265                 } else {
4266                         lpfc_drop_node(ndlp->vport, ndlp);
4267                 }
4268         }
4269
4270         return;
4271 }
4272
4273 /**
4274  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
4275  * @phba: pointer to lpfc hba data structure.
4276  * @cmdiocb: pointer to lpfc command iocb data structure.
4277  * @rspiocb: pointer to lpfc response iocb data structure.
4278  *
4279  * This routine is the completion callback function for ELS Response IOCB
4280  * command. In normal case, this callback function just properly sets the
4281  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
4282  * field in the command IOCB is not NULL, the referred mailbox command will
4283  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
4284  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
4285  * link down event occurred during the discovery, the lpfc_nlp_not_used()
4286  * routine shall be invoked trying to release the ndlp if no other threads
4287  * are currently referring it.
4288  **/
4289 static void
4290 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4291                   struct lpfc_iocbq *rspiocb)
4292 {
4293         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4294         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
4295         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
4296         IOCB_t  *irsp;
4297         uint8_t *pcmd;
4298         LPFC_MBOXQ_t *mbox = NULL;
4299         struct lpfc_dmabuf *mp = NULL;
4300         uint32_t ls_rjt = 0;
4301
4302         irsp = &rspiocb->iocb;
4303
4304         if (!vport) {
4305                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
4306                                 "3177 ELS response failed\n");
4307                 goto out;
4308         }
4309         if (cmdiocb->context_un.mbox)
4310                 mbox = cmdiocb->context_un.mbox;
4311
4312         /* First determine if this is a LS_RJT cmpl. Note, this callback
4313          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
4314          */
4315         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4316         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4317             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
4318                 /* A LS_RJT associated with Default RPI cleanup has its own
4319                  * separate code path.
4320                  */
4321                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4322                         ls_rjt = 1;
4323         }
4324
4325         /* Check to see if link went down during discovery */
4326         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
4327                 if (mbox) {
4328                         mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
4329                         if (mp) {
4330                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4331                                 kfree(mp);
4332                         }
4333                         mempool_free(mbox, phba->mbox_mem_pool);
4334                 }
4335                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4336                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4337                         if (lpfc_nlp_not_used(ndlp)) {
4338                                 ndlp = NULL;
4339                                 /* Indicate the node has already released,
4340                                  * should not reference to it from within
4341                                  * the routine lpfc_els_free_iocb.
4342                                  */
4343                                 cmdiocb->context1 = NULL;
4344                         }
4345                 goto out;
4346         }
4347
4348         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4349                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
4350                 irsp->ulpStatus, irsp->un.ulpWord[4],
4351                 cmdiocb->iocb.un.elsreq64.remoteID);
4352         /* ELS response tag <ulpIoTag> completes */
4353         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4354                          "0110 ELS response tag x%x completes "
4355                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
4356                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
4357                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
4358                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4359                          ndlp->nlp_rpi);
4360         if (mbox) {
4361                 if ((rspiocb->iocb.ulpStatus == 0)
4362                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
4363                         if (!lpfc_unreg_rpi(vport, ndlp) &&
4364                             (!(vport->fc_flag & FC_PT2PT)) &&
4365                             (ndlp->nlp_state ==  NLP_STE_PLOGI_ISSUE ||
4366                              ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE)) {
4367                                 lpfc_printf_vlog(vport, KERN_INFO,
4368                                         LOG_DISCOVERY,
4369                                         "0314 PLOGI recov DID x%x "
4370                                         "Data: x%x x%x x%x\n",
4371                                         ndlp->nlp_DID, ndlp->nlp_state,
4372                                         ndlp->nlp_rpi, ndlp->nlp_flag);
4373                                 mp = mbox->ctx_buf;
4374                                 if (mp) {
4375                                         lpfc_mbuf_free(phba, mp->virt,
4376                                                        mp->phys);
4377                                         kfree(mp);
4378                                 }
4379                                 mempool_free(mbox, phba->mbox_mem_pool);
4380                                 goto out;
4381                         }
4382
4383                         /* Increment reference count to ndlp to hold the
4384                          * reference to ndlp for the callback function.
4385                          */
4386                         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4387                         mbox->vport = vport;
4388                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
4389                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4390                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4391                         }
4392                         else {
4393                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
4394                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4395                                 lpfc_nlp_set_state(vport, ndlp,
4396                                            NLP_STE_REG_LOGIN_ISSUE);
4397                         }
4398
4399                         ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
4400                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4401                             != MBX_NOT_FINISHED)
4402                                 goto out;
4403
4404                         /* Decrement the ndlp reference count we
4405                          * set for this failed mailbox command.
4406                          */
4407                         lpfc_nlp_put(ndlp);
4408                         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4409
4410                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
4411                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4412                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
4413                                 "Data: x%x x%x x%x\n",
4414                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4415                                 ndlp->nlp_rpi);
4416
4417                         if (lpfc_nlp_not_used(ndlp)) {
4418                                 ndlp = NULL;
4419                                 /* Indicate node has already been released,
4420                                  * should not reference to it from within
4421                                  * the routine lpfc_els_free_iocb.
4422                                  */
4423                                 cmdiocb->context1 = NULL;
4424                         }
4425                 } else {
4426                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
4427                         if (!lpfc_error_lost_link(irsp) &&
4428                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
4429                                 if (lpfc_nlp_not_used(ndlp)) {
4430                                         ndlp = NULL;
4431                                         /* Indicate node has already been
4432                                          * released, should not reference
4433                                          * to it from within the routine
4434                                          * lpfc_els_free_iocb.
4435                                          */
4436                                         cmdiocb->context1 = NULL;
4437                                 }
4438                         }
4439                 }
4440                 mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
4441                 if (mp) {
4442                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4443                         kfree(mp);
4444                 }
4445                 mempool_free(mbox, phba->mbox_mem_pool);
4446         }
4447 out:
4448         if (ndlp && NLP_CHK_NODE_ACT(ndlp) && shost) {
4449                 spin_lock_irq(shost->host_lock);
4450                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
4451                 spin_unlock_irq(shost->host_lock);
4452
4453                 /* If the node is not being used by another discovery thread,
4454                  * and we are sending a reject, we are done with it.
4455                  * Release driver reference count here and free associated
4456                  * resources.
4457                  */
4458                 if (ls_rjt)
4459                         if (lpfc_nlp_not_used(ndlp))
4460                                 /* Indicate node has already been released,
4461                                  * should not reference to it from within
4462                                  * the routine lpfc_els_free_iocb.
4463                                  */
4464                                 cmdiocb->context1 = NULL;
4465
4466         }
4467
4468         lpfc_els_free_iocb(phba, cmdiocb);
4469         return;
4470 }
4471
4472 /**
4473  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
4474  * @vport: pointer to a host virtual N_Port data structure.
4475  * @flag: the els command code to be accepted.
4476  * @oldiocb: pointer to the original lpfc command iocb data structure.
4477  * @ndlp: pointer to a node-list data structure.
4478  * @mbox: pointer to the driver internal queue element for mailbox command.
4479  *
4480  * This routine prepares and issues an Accept (ACC) response IOCB
4481  * command. It uses the @flag to properly set up the IOCB field for the
4482  * specific ACC response command to be issued and invokes the
4483  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
4484  * @mbox pointer is passed in, it will be put into the context_un.mbox
4485  * field of the IOCB for the completion callback function to issue the
4486  * mailbox command to the HBA later when callback is invoked.
4487  *
4488  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4489  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4490  * will be stored into the context1 field of the IOCB for the completion
4491  * callback function to the corresponding response ELS IOCB command.
4492  *
4493  * Return code
4494  *   0 - Successfully issued acc response
4495  *   1 - Failed to issue acc response
4496  **/
4497 int
4498 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
4499                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4500                  LPFC_MBOXQ_t *mbox)
4501 {
4502         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4503         struct lpfc_hba  *phba = vport->phba;
4504         IOCB_t *icmd;
4505         IOCB_t *oldcmd;
4506         struct lpfc_iocbq *elsiocb;
4507         uint8_t *pcmd;
4508         struct serv_parm *sp;
4509         uint16_t cmdsize;
4510         int rc;
4511         ELS_PKT *els_pkt_ptr;
4512
4513         oldcmd = &oldiocb->iocb;
4514
4515         switch (flag) {
4516         case ELS_CMD_ACC:
4517                 cmdsize = sizeof(uint32_t);
4518                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4519                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4520                 if (!elsiocb) {
4521                         spin_lock_irq(shost->host_lock);
4522                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4523                         spin_unlock_irq(shost->host_lock);
4524                         return 1;
4525                 }
4526
4527                 icmd = &elsiocb->iocb;
4528                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4529                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4530                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4531                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4532                 pcmd += sizeof(uint32_t);
4533
4534                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4535                         "Issue ACC:       did:x%x flg:x%x",
4536                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4537                 break;
4538         case ELS_CMD_FLOGI:
4539         case ELS_CMD_PLOGI:
4540                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
4541                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4542                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4543                 if (!elsiocb)
4544                         return 1;
4545
4546                 icmd = &elsiocb->iocb;
4547                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4548                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4549                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4550
4551                 if (mbox)
4552                         elsiocb->context_un.mbox = mbox;
4553
4554                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4555                 pcmd += sizeof(uint32_t);
4556                 sp = (struct serv_parm *)pcmd;
4557
4558                 if (flag == ELS_CMD_FLOGI) {
4559                         /* Copy the received service parameters back */
4560                         memcpy(sp, &phba->fc_fabparam,
4561                                sizeof(struct serv_parm));
4562
4563                         /* Clear the F_Port bit */
4564                         sp->cmn.fPort = 0;
4565
4566                         /* Mark all class service parameters as invalid */
4567                         sp->cls1.classValid = 0;
4568                         sp->cls2.classValid = 0;
4569                         sp->cls3.classValid = 0;
4570                         sp->cls4.classValid = 0;
4571
4572                         /* Copy our worldwide names */
4573                         memcpy(&sp->portName, &vport->fc_sparam.portName,
4574                                sizeof(struct lpfc_name));
4575                         memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
4576                                sizeof(struct lpfc_name));
4577                 } else {
4578                         memcpy(pcmd, &vport->fc_sparam,
4579                                sizeof(struct serv_parm));
4580
4581                         sp->cmn.valid_vendor_ver_level = 0;
4582                         memset(sp->un.vendorVersion, 0,
4583                                sizeof(sp->un.vendorVersion));
4584                         sp->cmn.bbRcvSizeMsb &= 0xF;
4585
4586                         /* If our firmware supports this feature, convey that
4587                          * info to the target using the vendor specific field.
4588                          */
4589                         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
4590                                 sp->cmn.valid_vendor_ver_level = 1;
4591                                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
4592                                 sp->un.vv.flags =
4593                                         cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
4594                         }
4595                 }
4596
4597                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4598                         "Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
4599                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4600                 break;
4601         case ELS_CMD_PRLO:
4602                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
4603                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4604                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
4605                 if (!elsiocb)
4606                         return 1;
4607
4608                 icmd = &elsiocb->iocb;
4609                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4610                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4611                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4612
4613                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
4614                        sizeof(uint32_t) + sizeof(PRLO));
4615                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
4616                 els_pkt_ptr = (ELS_PKT *) pcmd;
4617                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
4618
4619                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4620                         "Issue ACC PRLO:  did:x%x flg:x%x",
4621                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4622                 break;
4623         default:
4624                 return 1;
4625         }
4626         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
4627                 spin_lock_irq(shost->host_lock);
4628                 if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4629                         ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
4630                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4631                 spin_unlock_irq(shost->host_lock);
4632                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4633         } else {
4634                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4635         }
4636
4637         phba->fc_stat.elsXmitACC++;
4638         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4639         if (rc == IOCB_ERROR) {
4640                 lpfc_els_free_iocb(phba, elsiocb);
4641                 return 1;
4642         }
4643         return 0;
4644 }
4645
4646 /**
4647  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
4648  * @vport: pointer to a virtual N_Port data structure.
4649  * @rejectError:
4650  * @oldiocb: pointer to the original lpfc command iocb data structure.
4651  * @ndlp: pointer to a node-list data structure.
4652  * @mbox: pointer to the driver internal queue element for mailbox command.
4653  *
4654  * This routine prepares and issue an Reject (RJT) response IOCB
4655  * command. If a @mbox pointer is passed in, it will be put into the
4656  * context_un.mbox field of the IOCB for the completion callback function
4657  * to issue to the HBA later.
4658  *
4659  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4660  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4661  * will be stored into the context1 field of the IOCB for the completion
4662  * callback function to the reject response ELS IOCB command.
4663  *
4664  * Return code
4665  *   0 - Successfully issued reject response
4666  *   1 - Failed to issue reject response
4667  **/
4668 int
4669 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
4670                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4671                     LPFC_MBOXQ_t *mbox)
4672 {
4673         struct lpfc_hba  *phba = vport->phba;
4674         IOCB_t *icmd;
4675         IOCB_t *oldcmd;
4676         struct lpfc_iocbq *elsiocb;
4677         uint8_t *pcmd;
4678         uint16_t cmdsize;
4679         int rc;
4680
4681         cmdsize = 2 * sizeof(uint32_t);
4682         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4683                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
4684         if (!elsiocb)
4685                 return 1;
4686
4687         icmd = &elsiocb->iocb;
4688         oldcmd = &oldiocb->iocb;
4689         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4690         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4691         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4692
4693         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
4694         pcmd += sizeof(uint32_t);
4695         *((uint32_t *) (pcmd)) = rejectError;
4696
4697         if (mbox)
4698                 elsiocb->context_un.mbox = mbox;
4699
4700         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
4701         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4702                          "0129 Xmit ELS RJT x%x response tag x%x "
4703                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4704                          "rpi x%x\n",
4705                          rejectError, elsiocb->iotag,
4706                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4707                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
4708         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4709                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
4710                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4711
4712         phba->fc_stat.elsXmitLSRJT++;
4713         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4714         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4715
4716         if (rc == IOCB_ERROR) {
4717                 lpfc_els_free_iocb(phba, elsiocb);
4718                 return 1;
4719         }
4720         return 0;
4721 }
4722
4723 /**
4724  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
4725  * @vport: pointer to a virtual N_Port data structure.
4726  * @oldiocb: pointer to the original lpfc command iocb data structure.
4727  * @ndlp: pointer to a node-list data structure.
4728  *
4729  * This routine prepares and issues an Accept (ACC) response to Address
4730  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4731  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4732  *
4733  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4734  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4735  * will be stored into the context1 field of the IOCB for the completion
4736  * callback function to the ADISC Accept response ELS IOCB command.
4737  *
4738  * Return code
4739  *   0 - Successfully issued acc adisc response
4740  *   1 - Failed to issue adisc acc response
4741  **/
4742 int
4743 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4744                        struct lpfc_nodelist *ndlp)
4745 {
4746         struct lpfc_hba  *phba = vport->phba;
4747         ADISC *ap;
4748         IOCB_t *icmd, *oldcmd;
4749         struct lpfc_iocbq *elsiocb;
4750         uint8_t *pcmd;
4751         uint16_t cmdsize;
4752         int rc;
4753
4754         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
4755         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4756                                      ndlp->nlp_DID, ELS_CMD_ACC);
4757         if (!elsiocb)
4758                 return 1;
4759
4760         icmd = &elsiocb->iocb;
4761         oldcmd = &oldiocb->iocb;
4762         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4763         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4764
4765         /* Xmit ADISC ACC response tag <ulpIoTag> */
4766         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4767                          "0130 Xmit ADISC ACC response iotag x%x xri: "
4768                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4769                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4770                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4771                          ndlp->nlp_rpi);
4772         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4773
4774         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4775         pcmd += sizeof(uint32_t);
4776
4777         ap = (ADISC *) (pcmd);
4778         ap->hardAL_PA = phba->fc_pref_ALPA;
4779         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4780         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4781         ap->DID = be32_to_cpu(vport->fc_myDID);
4782
4783         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4784                 "Issue ACC ADISC: did:x%x flg:x%x",
4785                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4786
4787         phba->fc_stat.elsXmitACC++;
4788         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4789         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4790         if (rc == IOCB_ERROR) {
4791                 lpfc_els_free_iocb(phba, elsiocb);
4792                 return 1;
4793         }
4794
4795         /* Xmit ELS ACC response tag <ulpIoTag> */
4796         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4797                          "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
4798                          "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
4799                          "RPI: x%x, fc_flag x%x\n",
4800                          rc, elsiocb->iotag, elsiocb->sli4_xritag,
4801                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4802                          ndlp->nlp_rpi, vport->fc_flag);
4803         return 0;
4804 }
4805
4806 /**
4807  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
4808  * @vport: pointer to a virtual N_Port data structure.
4809  * @oldiocb: pointer to the original lpfc command iocb data structure.
4810  * @ndlp: pointer to a node-list data structure.
4811  *
4812  * This routine prepares and issues an Accept (ACC) response to Process
4813  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
4814  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4815  *
4816  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4817  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4818  * will be stored into the context1 field of the IOCB for the completion
4819  * callback function to the PRLI Accept response ELS IOCB command.
4820  *
4821  * Return code
4822  *   0 - Successfully issued acc prli response
4823  *   1 - Failed to issue acc prli response
4824  **/
4825 int
4826 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4827                       struct lpfc_nodelist *ndlp)
4828 {
4829         struct lpfc_hba  *phba = vport->phba;
4830         PRLI *npr;
4831         struct lpfc_nvme_prli *npr_nvme;
4832         lpfc_vpd_t *vpd;
4833         IOCB_t *icmd;
4834         IOCB_t *oldcmd;
4835         struct lpfc_iocbq *elsiocb;
4836         uint8_t *pcmd;
4837         uint16_t cmdsize;
4838         uint32_t prli_fc4_req, *req_payload;
4839         struct lpfc_dmabuf *req_buf;
4840         int rc;
4841         u32 elsrspcmd;
4842
4843         /* Need the incoming PRLI payload to determine if the ACC is for an
4844          * FC4 or NVME PRLI type.  The PRLI type is at word 1.
4845          */
4846         req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
4847         req_payload = (((uint32_t *)req_buf->virt) + 1);
4848
4849         /* PRLI type payload is at byte 3 for FCP or NVME. */
4850         prli_fc4_req = be32_to_cpu(*req_payload);
4851         prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
4852         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4853                          "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
4854                          prli_fc4_req, *((uint32_t *)req_payload));
4855
4856         if (prli_fc4_req == PRLI_FCP_TYPE) {
4857                 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
4858                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
4859         } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4860                 cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
4861                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
4862         } else {
4863                 return 1;
4864         }
4865
4866         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4867                 ndlp->nlp_DID, elsrspcmd);
4868         if (!elsiocb)
4869                 return 1;
4870
4871         icmd = &elsiocb->iocb;
4872         oldcmd = &oldiocb->iocb;
4873         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4874         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4875
4876         /* Xmit PRLI ACC response tag <ulpIoTag> */
4877         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4878                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
4879                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4880                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4881                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4882                          ndlp->nlp_rpi);
4883         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4884         memset(pcmd, 0, cmdsize);
4885
4886         *((uint32_t *)(pcmd)) = elsrspcmd;
4887         pcmd += sizeof(uint32_t);
4888
4889         /* For PRLI, remainder of payload is PRLI parameter page */
4890         vpd = &phba->vpd;
4891
4892         if (prli_fc4_req == PRLI_FCP_TYPE) {
4893                 /*
4894                  * If the remote port is a target and our firmware version
4895                  * is 3.20 or later, set the following bits for FC-TAPE
4896                  * support.
4897                  */
4898                 npr = (PRLI *) pcmd;
4899                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4900                     (vpd->rev.feaLevelHigh >= 0x02)) {
4901                         npr->ConfmComplAllowed = 1;
4902                         npr->Retry = 1;
4903                         npr->TaskRetryIdReq = 1;
4904                 }
4905                 npr->acceptRspCode = PRLI_REQ_EXECUTED;
4906                 npr->estabImagePair = 1;
4907                 npr->readXferRdyDis = 1;
4908                 npr->ConfmComplAllowed = 1;
4909                 npr->prliType = PRLI_FCP_TYPE;
4910                 npr->initiatorFunc = 1;
4911         } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4912                 /* Respond with an NVME PRLI Type */
4913                 npr_nvme = (struct lpfc_nvme_prli *) pcmd;
4914                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
4915                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
4916                 bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
4917                 if (phba->nvmet_support) {
4918                         bf_set(prli_tgt, npr_nvme, 1);
4919                         bf_set(prli_disc, npr_nvme, 1);
4920                         if (phba->cfg_nvme_enable_fb) {
4921                                 bf_set(prli_fba, npr_nvme, 1);
4922
4923                                 /* TBD.  Target mode needs to post buffers
4924                                  * that support the configured first burst
4925                                  * byte size.
4926                                  */
4927                                 bf_set(prli_fb_sz, npr_nvme,
4928                                        phba->cfg_nvmet_fb_size);
4929                         }
4930                 } else {
4931                         bf_set(prli_init, npr_nvme, 1);
4932                 }
4933
4934                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
4935                                  "6015 NVME issue PRLI ACC word1 x%08x "
4936                                  "word4 x%08x word5 x%08x flag x%x, "
4937                                  "fcp_info x%x nlp_type x%x\n",
4938                                  npr_nvme->word1, npr_nvme->word4,
4939                                  npr_nvme->word5, ndlp->nlp_flag,
4940                                  ndlp->nlp_fcp_info, ndlp->nlp_type);
4941                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
4942                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
4943                 npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
4944         } else
4945                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4946                                  "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
4947                                  prli_fc4_req, ndlp->nlp_fc4_type,
4948                                  ndlp->nlp_DID);
4949
4950         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4951                 "Issue ACC PRLI:  did:x%x flg:x%x",
4952                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4953
4954         phba->fc_stat.elsXmitACC++;
4955         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4956
4957         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4958         if (rc == IOCB_ERROR) {
4959                 lpfc_els_free_iocb(phba, elsiocb);
4960                 return 1;
4961         }
4962         return 0;
4963 }
4964
4965 /**
4966  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
4967  * @vport: pointer to a virtual N_Port data structure.
4968  * @format: rnid command format.
4969  * @oldiocb: pointer to the original lpfc command iocb data structure.
4970  * @ndlp: pointer to a node-list data structure.
4971  *
4972  * This routine issues a Request Node Identification Data (RNID) Accept
4973  * (ACC) response. It constructs the RNID ACC response command according to
4974  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4975  * issue the response. Note that this command does not need to hold the ndlp
4976  * reference count for the callback. So, the ndlp reference count taken by
4977  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4978  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4979  * there is no ndlp reference available.
4980  *
4981  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4982  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4983  * will be stored into the context1 field of the IOCB for the completion
4984  * callback function. However, for the RNID Accept Response ELS command,
4985  * this is undone later by this routine after the IOCB is allocated.
4986  *
4987  * Return code
4988  *   0 - Successfully issued acc rnid response
4989  *   1 - Failed to issue acc rnid response
4990  **/
4991 static int
4992 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
4993                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4994 {
4995         struct lpfc_hba  *phba = vport->phba;
4996         RNID *rn;
4997         IOCB_t *icmd, *oldcmd;
4998         struct lpfc_iocbq *elsiocb;
4999         uint8_t *pcmd;
5000         uint16_t cmdsize;
5001         int rc;
5002
5003         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
5004                                         + (2 * sizeof(struct lpfc_name));
5005         if (format)
5006                 cmdsize += sizeof(RNID_TOP_DISC);
5007
5008         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5009                                      ndlp->nlp_DID, ELS_CMD_ACC);
5010         if (!elsiocb)
5011                 return 1;
5012
5013         icmd = &elsiocb->iocb;
5014         oldcmd = &oldiocb->iocb;
5015         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
5016         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5017
5018         /* Xmit RNID ACC response tag <ulpIoTag> */
5019         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5020                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
5021                          elsiocb->iotag, elsiocb->iocb.ulpContext);
5022         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5023         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5024         pcmd += sizeof(uint32_t);
5025
5026         memset(pcmd, 0, sizeof(RNID));
5027         rn = (RNID *) (pcmd);
5028         rn->Format = format;
5029         rn->CommonLen = (2 * sizeof(struct lpfc_name));
5030         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5031         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5032         switch (format) {
5033         case 0:
5034                 rn->SpecificLen = 0;
5035                 break;
5036         case RNID_TOPOLOGY_DISC:
5037                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
5038                 memcpy(&rn->un.topologyDisc.portName,
5039                        &vport->fc_portname, sizeof(struct lpfc_name));
5040                 rn->un.topologyDisc.unitType = RNID_HBA;
5041                 rn->un.topologyDisc.physPort = 0;
5042                 rn->un.topologyDisc.attachedNodes = 0;
5043                 break;
5044         default:
5045                 rn->CommonLen = 0;
5046                 rn->SpecificLen = 0;
5047                 break;
5048         }
5049
5050         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5051                 "Issue ACC RNID:  did:x%x flg:x%x",
5052                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
5053
5054         phba->fc_stat.elsXmitACC++;
5055         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5056
5057         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5058         if (rc == IOCB_ERROR) {
5059                 lpfc_els_free_iocb(phba, elsiocb);
5060                 return 1;
5061         }
5062         return 0;
5063 }
5064
5065 /**
5066  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
5067  * @vport: pointer to a virtual N_Port data structure.
5068  * @iocb: pointer to the lpfc command iocb data structure.
5069  * @ndlp: pointer to a node-list data structure.
5070  *
5071  * Return
5072  **/
5073 static void
5074 lpfc_els_clear_rrq(struct lpfc_vport *vport,
5075                    struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
5076 {
5077         struct lpfc_hba  *phba = vport->phba;
5078         uint8_t *pcmd;
5079         struct RRQ *rrq;
5080         uint16_t rxid;
5081         uint16_t xri;
5082         struct lpfc_node_rrq *prrq;
5083
5084
5085         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
5086         pcmd += sizeof(uint32_t);
5087         rrq = (struct RRQ *)pcmd;
5088         rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
5089         rxid = bf_get(rrq_rxid, rrq);
5090
5091         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5092                         "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
5093                         " x%x x%x\n",
5094                         be32_to_cpu(bf_get(rrq_did, rrq)),
5095                         bf_get(rrq_oxid, rrq),
5096                         rxid,
5097                         iocb->iotag, iocb->iocb.ulpContext);
5098
5099         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5100                 "Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
5101                 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
5102         if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
5103                 xri = bf_get(rrq_oxid, rrq);
5104         else
5105                 xri = rxid;
5106         prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
5107         if (prrq)
5108                 lpfc_clr_rrq_active(phba, xri, prrq);
5109         return;
5110 }
5111
5112 /**
5113  * lpfc_els_rsp_echo_acc - Issue echo acc response
5114  * @vport: pointer to a virtual N_Port data structure.
5115  * @data: pointer to echo data to return in the accept.
5116  * @oldiocb: pointer to the original lpfc command iocb data structure.
5117  * @ndlp: pointer to a node-list data structure.
5118  *
5119  * Return code
5120  *   0 - Successfully issued acc echo response
5121  *   1 - Failed to issue acc echo response
5122  **/
5123 static int
5124 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
5125                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5126 {
5127         struct lpfc_hba  *phba = vport->phba;
5128         struct lpfc_iocbq *elsiocb;
5129         uint8_t *pcmd;
5130         uint16_t cmdsize;
5131         int rc;
5132
5133         cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
5134
5135         /* The accumulated length can exceed the BPL_SIZE.  For
5136          * now, use this as the limit
5137          */
5138         if (cmdsize > LPFC_BPL_SIZE)
5139                 cmdsize = LPFC_BPL_SIZE;
5140         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5141                                      ndlp->nlp_DID, ELS_CMD_ACC);
5142         if (!elsiocb)
5143                 return 1;
5144
5145         elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
5146         elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
5147
5148         /* Xmit ECHO ACC response tag <ulpIoTag> */
5149         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5150                          "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
5151                          elsiocb->iotag, elsiocb->iocb.ulpContext);
5152         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5153         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5154         pcmd += sizeof(uint32_t);
5155         memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
5156
5157         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5158                 "Issue ACC ECHO:  did:x%x flg:x%x",
5159                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
5160
5161         phba->fc_stat.elsXmitACC++;
5162         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5163
5164         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5165         if (rc == IOCB_ERROR) {
5166                 lpfc_els_free_iocb(phba, elsiocb);
5167                 return 1;
5168         }
5169         return 0;
5170 }
5171
5172 /**
5173  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
5174  * @vport: pointer to a host virtual N_Port data structure.
5175  *
5176  * This routine issues Address Discover (ADISC) ELS commands to those
5177  * N_Ports which are in node port recovery state and ADISC has not been issued
5178  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
5179  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
5180  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
5181  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
5182  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
5183  * IOCBs quit for later pick up. On the other hand, after walking through
5184  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
5185  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
5186  * no more ADISC need to be sent.
5187  *
5188  * Return code
5189  *    The number of N_Ports with adisc issued.
5190  **/
5191 int
5192 lpfc_els_disc_adisc(struct lpfc_vport *vport)
5193 {
5194         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5195         struct lpfc_nodelist *ndlp, *next_ndlp;
5196         int sentadisc = 0;
5197
5198         /* go thru NPR nodes and issue any remaining ELS ADISCs */
5199         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5200                 if (!NLP_CHK_NODE_ACT(ndlp))
5201                         continue;
5202                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5203                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5204                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
5205                         spin_lock_irq(shost->host_lock);
5206                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5207                         spin_unlock_irq(shost->host_lock);
5208                         ndlp->nlp_prev_state = ndlp->nlp_state;
5209                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
5210                         lpfc_issue_els_adisc(vport, ndlp, 0);
5211                         sentadisc++;
5212                         vport->num_disc_nodes++;
5213                         if (vport->num_disc_nodes >=
5214                             vport->cfg_discovery_threads) {
5215                                 spin_lock_irq(shost->host_lock);
5216                                 vport->fc_flag |= FC_NLP_MORE;
5217                                 spin_unlock_irq(shost->host_lock);
5218                                 break;
5219                         }
5220                 }
5221         }
5222         if (sentadisc == 0) {
5223                 spin_lock_irq(shost->host_lock);
5224                 vport->fc_flag &= ~FC_NLP_MORE;
5225                 spin_unlock_irq(shost->host_lock);
5226         }
5227         return sentadisc;
5228 }
5229
5230 /**
5231  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
5232  * @vport: pointer to a host virtual N_Port data structure.
5233  *
5234  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
5235  * which are in node port recovery state, with a @vport. Each time an ELS
5236  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
5237  * the per @vport number of discover count (num_disc_nodes) shall be
5238  * incremented. If the num_disc_nodes reaches a pre-configured threshold
5239  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
5240  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
5241  * later pick up. On the other hand, after walking through all the ndlps with
5242  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
5243  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
5244  * PLOGI need to be sent.
5245  *
5246  * Return code
5247  *   The number of N_Ports with plogi issued.
5248  **/
5249 int
5250 lpfc_els_disc_plogi(struct lpfc_vport *vport)
5251 {
5252         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5253         struct lpfc_nodelist *ndlp, *next_ndlp;
5254         int sentplogi = 0;
5255
5256         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
5257         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5258                 if (!NLP_CHK_NODE_ACT(ndlp))
5259                         continue;
5260                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5261                                 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5262                                 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
5263                                 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
5264                         ndlp->nlp_prev_state = ndlp->nlp_state;
5265                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5266                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5267                         sentplogi++;
5268                         vport->num_disc_nodes++;
5269                         if (vport->num_disc_nodes >=
5270                                         vport->cfg_discovery_threads) {
5271                                 spin_lock_irq(shost->host_lock);
5272                                 vport->fc_flag |= FC_NLP_MORE;
5273                                 spin_unlock_irq(shost->host_lock);
5274                                 break;
5275                         }
5276                 }
5277         }
5278
5279         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5280                          "6452 Discover PLOGI %d flag x%x\n",
5281                          sentplogi, vport->fc_flag);
5282
5283         if (sentplogi) {
5284                 lpfc_set_disctmo(vport);
5285         }
5286         else {
5287                 spin_lock_irq(shost->host_lock);
5288                 vport->fc_flag &= ~FC_NLP_MORE;
5289                 spin_unlock_irq(shost->host_lock);
5290         }
5291         return sentplogi;
5292 }
5293
5294 static uint32_t
5295 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
5296                 uint32_t word0)
5297 {
5298
5299         desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
5300         desc->payload.els_req = word0;
5301         desc->length = cpu_to_be32(sizeof(desc->payload));
5302
5303         return sizeof(struct fc_rdp_link_service_desc);
5304 }
5305
5306 static uint32_t
5307 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
5308                 uint8_t *page_a0, uint8_t *page_a2)
5309 {
5310         uint16_t wavelength;
5311         uint16_t temperature;
5312         uint16_t rx_power;
5313         uint16_t tx_bias;
5314         uint16_t tx_power;
5315         uint16_t vcc;
5316         uint16_t flag = 0;
5317         struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
5318         struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
5319
5320         desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
5321
5322         trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
5323                         &page_a0[SSF_TRANSCEIVER_CODE_B4];
5324         trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
5325                         &page_a0[SSF_TRANSCEIVER_CODE_B5];
5326
5327         if ((trasn_code_byte4->fc_sw_laser) ||
5328             (trasn_code_byte5->fc_sw_laser_sl) ||
5329             (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
5330                 flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
5331         } else if (trasn_code_byte4->fc_lw_laser) {
5332                 wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
5333                         page_a0[SSF_WAVELENGTH_B0];
5334                 if (wavelength == SFP_WAVELENGTH_LC1310)
5335                         flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
5336                 if (wavelength == SFP_WAVELENGTH_LL1550)
5337                         flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
5338         }
5339         /* check if its SFP+ */
5340         flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
5341                         SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
5342                                         << SFP_FLAG_CT_SHIFT;
5343
5344         /* check if its OPTICAL */
5345         flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
5346                         SFP_FLAG_IS_OPTICAL_PORT : 0)
5347                                         << SFP_FLAG_IS_OPTICAL_SHIFT;
5348
5349         temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
5350                 page_a2[SFF_TEMPERATURE_B0]);
5351         vcc = (page_a2[SFF_VCC_B1] << 8 |
5352                 page_a2[SFF_VCC_B0]);
5353         tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
5354                 page_a2[SFF_TXPOWER_B0]);
5355         tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
5356                 page_a2[SFF_TX_BIAS_CURRENT_B0]);
5357         rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
5358                 page_a2[SFF_RXPOWER_B0]);
5359         desc->sfp_info.temperature = cpu_to_be16(temperature);
5360         desc->sfp_info.rx_power = cpu_to_be16(rx_power);
5361         desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
5362         desc->sfp_info.tx_power = cpu_to_be16(tx_power);
5363         desc->sfp_info.vcc = cpu_to_be16(vcc);
5364
5365         desc->sfp_info.flags = cpu_to_be16(flag);
5366         desc->length = cpu_to_be32(sizeof(desc->sfp_info));
5367
5368         return sizeof(struct fc_rdp_sfp_desc);
5369 }
5370
5371 static uint32_t
5372 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
5373                 READ_LNK_VAR *stat)
5374 {
5375         uint32_t type;
5376
5377         desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
5378
5379         type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
5380
5381         desc->info.port_type = cpu_to_be32(type);
5382
5383         desc->info.link_status.link_failure_cnt =
5384                 cpu_to_be32(stat->linkFailureCnt);
5385         desc->info.link_status.loss_of_synch_cnt =
5386                 cpu_to_be32(stat->lossSyncCnt);
5387         desc->info.link_status.loss_of_signal_cnt =
5388                 cpu_to_be32(stat->lossSignalCnt);
5389         desc->info.link_status.primitive_seq_proto_err =
5390                 cpu_to_be32(stat->primSeqErrCnt);
5391         desc->info.link_status.invalid_trans_word =
5392                 cpu_to_be32(stat->invalidXmitWord);
5393         desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
5394
5395         desc->length = cpu_to_be32(sizeof(desc->info));
5396
5397         return sizeof(struct fc_rdp_link_error_status_desc);
5398 }
5399
5400 static uint32_t
5401 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
5402                       struct lpfc_vport *vport)
5403 {
5404         uint32_t bbCredit;
5405
5406         desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
5407
5408         bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
5409                         (vport->fc_sparam.cmn.bbCreditMsb << 8);
5410         desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
5411         if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
5412                 bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
5413                         (vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
5414                 desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
5415         } else {
5416                 desc->bbc_info.attached_port_bbc = 0;
5417         }
5418
5419         desc->bbc_info.rtt = 0;
5420         desc->length = cpu_to_be32(sizeof(desc->bbc_info));
5421
5422         return sizeof(struct fc_rdp_bbc_desc);
5423 }
5424
5425 static uint32_t
5426 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
5427                            struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
5428 {
5429         uint32_t flags = 0;
5430
5431         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5432
5433         desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
5434         desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
5435         desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
5436         desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
5437
5438         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5439                 flags |= RDP_OET_HIGH_ALARM;
5440         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5441                 flags |= RDP_OET_LOW_ALARM;
5442         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5443                 flags |= RDP_OET_HIGH_WARNING;
5444         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5445                 flags |= RDP_OET_LOW_WARNING;
5446
5447         flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
5448         desc->oed_info.function_flags = cpu_to_be32(flags);
5449         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5450         return sizeof(struct fc_rdp_oed_sfp_desc);
5451 }
5452
5453 static uint32_t
5454 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
5455                               struct fc_rdp_oed_sfp_desc *desc,
5456                               uint8_t *page_a2)
5457 {
5458         uint32_t flags = 0;
5459
5460         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5461
5462         desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
5463         desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
5464         desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
5465         desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
5466
5467         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5468                 flags |= RDP_OET_HIGH_ALARM;
5469         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5470                 flags |= RDP_OET_LOW_ALARM;
5471         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5472                 flags |= RDP_OET_HIGH_WARNING;
5473         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5474                 flags |= RDP_OET_LOW_WARNING;
5475
5476         flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
5477         desc->oed_info.function_flags = cpu_to_be32(flags);
5478         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5479         return sizeof(struct fc_rdp_oed_sfp_desc);
5480 }
5481
5482 static uint32_t
5483 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
5484                              struct fc_rdp_oed_sfp_desc *desc,
5485                              uint8_t *page_a2)
5486 {
5487         uint32_t flags = 0;
5488
5489         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5490
5491         desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
5492         desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
5493         desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
5494         desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
5495
5496         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5497                 flags |= RDP_OET_HIGH_ALARM;
5498         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
5499                 flags |= RDP_OET_LOW_ALARM;
5500         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5501                 flags |= RDP_OET_HIGH_WARNING;
5502         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
5503                 flags |= RDP_OET_LOW_WARNING;
5504
5505         flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
5506         desc->oed_info.function_flags = cpu_to_be32(flags);
5507         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5508         return sizeof(struct fc_rdp_oed_sfp_desc);
5509 }
5510
5511 static uint32_t
5512 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
5513                               struct fc_rdp_oed_sfp_desc *desc,
5514                               uint8_t *page_a2)
5515 {
5516         uint32_t flags = 0;
5517
5518         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5519
5520         desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
5521         desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
5522         desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
5523         desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
5524
5525         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5526                 flags |= RDP_OET_HIGH_ALARM;
5527         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
5528                 flags |= RDP_OET_LOW_ALARM;
5529         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5530                 flags |= RDP_OET_HIGH_WARNING;
5531         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
5532                 flags |= RDP_OET_LOW_WARNING;
5533
5534         flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
5535         desc->oed_info.function_flags = cpu_to_be32(flags);
5536         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5537         return sizeof(struct fc_rdp_oed_sfp_desc);
5538 }
5539
5540
5541 static uint32_t
5542 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
5543                               struct fc_rdp_oed_sfp_desc *desc,
5544                               uint8_t *page_a2)
5545 {
5546         uint32_t flags = 0;
5547
5548         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5549
5550         desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
5551         desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
5552         desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
5553         desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
5554
5555         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5556                 flags |= RDP_OET_HIGH_ALARM;
5557         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
5558                 flags |= RDP_OET_LOW_ALARM;
5559         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5560                 flags |= RDP_OET_HIGH_WARNING;
5561         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
5562                 flags |= RDP_OET_LOW_WARNING;
5563
5564         flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
5565         desc->oed_info.function_flags = cpu_to_be32(flags);
5566         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5567         return sizeof(struct fc_rdp_oed_sfp_desc);
5568 }
5569
5570 static uint32_t
5571 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
5572                       uint8_t *page_a0, struct lpfc_vport *vport)
5573 {
5574         desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
5575         memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
5576         memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
5577         memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
5578         memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
5579         memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
5580         desc->length = cpu_to_be32(sizeof(desc->opd_info));
5581         return sizeof(struct fc_rdp_opd_sfp_desc);
5582 }
5583
5584 static uint32_t
5585 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
5586 {
5587         if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
5588                 return 0;
5589         desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
5590
5591         desc->info.CorrectedBlocks =
5592                 cpu_to_be32(stat->fecCorrBlkCount);
5593         desc->info.UncorrectableBlocks =
5594                 cpu_to_be32(stat->fecUncorrBlkCount);
5595
5596         desc->length = cpu_to_be32(sizeof(desc->info));
5597
5598         return sizeof(struct fc_fec_rdp_desc);
5599 }
5600
5601 static uint32_t
5602 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
5603 {
5604         uint16_t rdp_cap = 0;
5605         uint16_t rdp_speed;
5606
5607         desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
5608
5609         switch (phba->fc_linkspeed) {
5610         case LPFC_LINK_SPEED_1GHZ:
5611                 rdp_speed = RDP_PS_1GB;
5612                 break;
5613         case LPFC_LINK_SPEED_2GHZ:
5614                 rdp_speed = RDP_PS_2GB;
5615                 break;
5616         case LPFC_LINK_SPEED_4GHZ:
5617                 rdp_speed = RDP_PS_4GB;
5618                 break;
5619         case LPFC_LINK_SPEED_8GHZ:
5620                 rdp_speed = RDP_PS_8GB;
5621                 break;
5622         case LPFC_LINK_SPEED_10GHZ:
5623                 rdp_speed = RDP_PS_10GB;
5624                 break;
5625         case LPFC_LINK_SPEED_16GHZ:
5626                 rdp_speed = RDP_PS_16GB;
5627                 break;
5628         case LPFC_LINK_SPEED_32GHZ:
5629                 rdp_speed = RDP_PS_32GB;
5630                 break;
5631         case LPFC_LINK_SPEED_64GHZ:
5632                 rdp_speed = RDP_PS_64GB;
5633                 break;
5634         default:
5635                 rdp_speed = RDP_PS_UNKNOWN;
5636                 break;
5637         }
5638
5639         desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
5640
5641         if (phba->lmt & LMT_128Gb)
5642                 rdp_cap |= RDP_PS_128GB;
5643         if (phba->lmt & LMT_64Gb)
5644                 rdp_cap |= RDP_PS_64GB;
5645         if (phba->lmt & LMT_32Gb)
5646                 rdp_cap |= RDP_PS_32GB;
5647         if (phba->lmt & LMT_16Gb)
5648                 rdp_cap |= RDP_PS_16GB;
5649         if (phba->lmt & LMT_10Gb)
5650                 rdp_cap |= RDP_PS_10GB;
5651         if (phba->lmt & LMT_8Gb)
5652                 rdp_cap |= RDP_PS_8GB;
5653         if (phba->lmt & LMT_4Gb)
5654                 rdp_cap |= RDP_PS_4GB;
5655         if (phba->lmt & LMT_2Gb)
5656                 rdp_cap |= RDP_PS_2GB;
5657         if (phba->lmt & LMT_1Gb)
5658                 rdp_cap |= RDP_PS_1GB;
5659
5660         if (rdp_cap == 0)
5661                 rdp_cap = RDP_CAP_UNKNOWN;
5662         if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
5663                 rdp_cap |= RDP_CAP_USER_CONFIGURED;
5664
5665         desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
5666         desc->length = cpu_to_be32(sizeof(desc->info));
5667         return sizeof(struct fc_rdp_port_speed_desc);
5668 }
5669
5670 static uint32_t
5671 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
5672                 struct lpfc_vport *vport)
5673 {
5674
5675         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5676
5677         memcpy(desc->port_names.wwnn, &vport->fc_nodename,
5678                         sizeof(desc->port_names.wwnn));
5679
5680         memcpy(desc->port_names.wwpn, &vport->fc_portname,
5681                         sizeof(desc->port_names.wwpn));
5682
5683         desc->length = cpu_to_be32(sizeof(desc->port_names));
5684         return sizeof(struct fc_rdp_port_name_desc);
5685 }
5686
5687 static uint32_t
5688 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
5689                 struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5690 {
5691
5692         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5693         if (vport->fc_flag & FC_FABRIC) {
5694                 memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
5695                        sizeof(desc->port_names.wwnn));
5696
5697                 memcpy(desc->port_names.wwpn, &vport->fabric_portname,
5698                        sizeof(desc->port_names.wwpn));
5699         } else {  /* Point to Point */
5700                 memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
5701                        sizeof(desc->port_names.wwnn));
5702
5703                 memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
5704                        sizeof(desc->port_names.wwpn));
5705         }
5706
5707         desc->length = cpu_to_be32(sizeof(desc->port_names));
5708         return sizeof(struct fc_rdp_port_name_desc);
5709 }
5710
5711 static void
5712 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
5713                 int status)
5714 {
5715         struct lpfc_nodelist *ndlp = rdp_context->ndlp;
5716         struct lpfc_vport *vport = ndlp->vport;
5717         struct lpfc_iocbq *elsiocb;
5718         struct ulp_bde64 *bpl;
5719         IOCB_t *icmd;
5720         uint8_t *pcmd;
5721         struct ls_rjt *stat;
5722         struct fc_rdp_res_frame *rdp_res;
5723         uint32_t cmdsize, len;
5724         uint16_t *flag_ptr;
5725         int rc;
5726
5727         if (status != SUCCESS)
5728                 goto error;
5729
5730         /* This will change once we know the true size of the RDP payload */
5731         cmdsize = sizeof(struct fc_rdp_res_frame);
5732
5733         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
5734                         lpfc_max_els_tries, rdp_context->ndlp,
5735                         rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
5736         lpfc_nlp_put(ndlp);
5737         if (!elsiocb)
5738                 goto free_rdp_context;
5739
5740         icmd = &elsiocb->iocb;
5741         icmd->ulpContext = rdp_context->rx_id;
5742         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5743
5744         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5745                         "2171 Xmit RDP response tag x%x xri x%x, "
5746                         "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
5747                         elsiocb->iotag, elsiocb->iocb.ulpContext,
5748                         ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5749                         ndlp->nlp_rpi);
5750         rdp_res = (struct fc_rdp_res_frame *)
5751                 (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5752         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5753         memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
5754         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5755
5756         /* Update Alarm and Warning */
5757         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
5758         phba->sfp_alarm |= *flag_ptr;
5759         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
5760         phba->sfp_warning |= *flag_ptr;
5761
5762         /* For RDP payload */
5763         len = 8;
5764         len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
5765                                          (len + pcmd), ELS_CMD_RDP);
5766
5767         len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
5768                         rdp_context->page_a0, rdp_context->page_a2);
5769         len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
5770                                   phba);
5771         len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
5772                                        (len + pcmd), &rdp_context->link_stat);
5773         len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
5774                                              (len + pcmd), vport);
5775         len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
5776                                         (len + pcmd), vport, ndlp);
5777         len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
5778                         &rdp_context->link_stat);
5779         len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
5780                                      &rdp_context->link_stat, vport);
5781         len += lpfc_rdp_res_oed_temp_desc(phba,
5782                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5783                                 rdp_context->page_a2);
5784         len += lpfc_rdp_res_oed_voltage_desc(phba,
5785                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5786                                 rdp_context->page_a2);
5787         len += lpfc_rdp_res_oed_txbias_desc(phba,
5788                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5789                                 rdp_context->page_a2);
5790         len += lpfc_rdp_res_oed_txpower_desc(phba,
5791                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5792                                 rdp_context->page_a2);
5793         len += lpfc_rdp_res_oed_rxpower_desc(phba,
5794                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5795                                 rdp_context->page_a2);
5796         len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
5797                                      rdp_context->page_a0, vport);
5798
5799         rdp_res->length = cpu_to_be32(len - 8);
5800         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5801
5802         /* Now that we know the true size of the payload, update the BPL */
5803         bpl = (struct ulp_bde64 *)
5804                 (((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
5805         bpl->tus.f.bdeSize = len;
5806         bpl->tus.f.bdeFlags = 0;
5807         bpl->tus.w = le32_to_cpu(bpl->tus.w);
5808
5809         phba->fc_stat.elsXmitACC++;
5810         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5811         if (rc == IOCB_ERROR)
5812                 lpfc_els_free_iocb(phba, elsiocb);
5813
5814         kfree(rdp_context);
5815
5816         return;
5817 error:
5818         cmdsize = 2 * sizeof(uint32_t);
5819         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
5820                         ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
5821         lpfc_nlp_put(ndlp);
5822         if (!elsiocb)
5823                 goto free_rdp_context;
5824
5825         icmd = &elsiocb->iocb;
5826         icmd->ulpContext = rdp_context->rx_id;
5827         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5828         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5829
5830         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5831         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5832         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5833
5834         phba->fc_stat.elsXmitLSRJT++;
5835         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5836         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5837
5838         if (rc == IOCB_ERROR)
5839                 lpfc_els_free_iocb(phba, elsiocb);
5840 free_rdp_context:
5841         kfree(rdp_context);
5842 }
5843
5844 static int
5845 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
5846 {
5847         LPFC_MBOXQ_t *mbox = NULL;
5848         int rc;
5849
5850         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5851         if (!mbox) {
5852                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
5853                                 "7105 failed to allocate mailbox memory");
5854                 return 1;
5855         }
5856
5857         if (lpfc_sli4_dump_page_a0(phba, mbox))
5858                 goto prep_mbox_fail;
5859         mbox->vport = rdp_context->ndlp->vport;
5860         mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
5861         mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
5862         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5863         if (rc == MBX_NOT_FINISHED)
5864                 goto issue_mbox_fail;
5865
5866         return 0;
5867
5868 prep_mbox_fail:
5869 issue_mbox_fail:
5870         mempool_free(mbox, phba->mbox_mem_pool);
5871         return 1;
5872 }
5873
5874 /*
5875  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
5876  * @vport: pointer to a host virtual N_Port data structure.
5877  * @cmdiocb: pointer to lpfc command iocb data structure.
5878  * @ndlp: pointer to a node-list data structure.
5879  *
5880  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
5881  * IOCB. First, the payload of the unsolicited RDP is checked.
5882  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
5883  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
5884  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
5885  * gather all data and send RDP response.
5886  *
5887  * Return code
5888  *   0 - Sent the acc response
5889  *   1 - Sent the reject response.
5890  */
5891 static int
5892 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5893                 struct lpfc_nodelist *ndlp)
5894 {
5895         struct lpfc_hba *phba = vport->phba;
5896         struct lpfc_dmabuf *pcmd;
5897         uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
5898         struct fc_rdp_req_frame *rdp_req;
5899         struct lpfc_rdp_context *rdp_context;
5900         IOCB_t *cmd = NULL;
5901         struct ls_rjt stat;
5902
5903         if (phba->sli_rev < LPFC_SLI_REV4 ||
5904             bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
5905                                                 LPFC_SLI_INTF_IF_TYPE_2) {
5906                 rjt_err = LSRJT_UNABLE_TPC;
5907                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5908                 goto error;
5909         }
5910
5911         if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
5912                 rjt_err = LSRJT_UNABLE_TPC;
5913                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5914                 goto error;
5915         }
5916
5917         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5918         rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
5919
5920         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5921                          "2422 ELS RDP Request "
5922                          "dec len %d tag x%x port_id %d len %d\n",
5923                          be32_to_cpu(rdp_req->rdp_des_length),
5924                          be32_to_cpu(rdp_req->nport_id_desc.tag),
5925                          be32_to_cpu(rdp_req->nport_id_desc.nport_id),
5926                          be32_to_cpu(rdp_req->nport_id_desc.length));
5927
5928         if (sizeof(struct fc_rdp_nport_desc) !=
5929                         be32_to_cpu(rdp_req->rdp_des_length))
5930                 goto rjt_logerr;
5931         if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
5932                 goto rjt_logerr;
5933         if (RDP_NPORT_ID_SIZE !=
5934                         be32_to_cpu(rdp_req->nport_id_desc.length))
5935                 goto rjt_logerr;
5936         rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
5937         if (!rdp_context) {
5938                 rjt_err = LSRJT_UNABLE_TPC;
5939                 goto error;
5940         }
5941
5942         cmd = &cmdiocb->iocb;
5943         rdp_context->ndlp = lpfc_nlp_get(ndlp);
5944         rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
5945         rdp_context->rx_id = cmd->ulpContext;
5946         rdp_context->cmpl = lpfc_els_rdp_cmpl;
5947         if (lpfc_get_rdp_info(phba, rdp_context)) {
5948                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
5949                                  "2423 Unable to send mailbox");
5950                 kfree(rdp_context);
5951                 rjt_err = LSRJT_UNABLE_TPC;
5952                 lpfc_nlp_put(ndlp);
5953                 goto error;
5954         }
5955
5956         return 0;
5957
5958 rjt_logerr:
5959         rjt_err = LSRJT_LOGICAL_ERR;
5960
5961 error:
5962         memset(&stat, 0, sizeof(stat));
5963         stat.un.b.lsRjtRsnCode = rjt_err;
5964         stat.un.b.lsRjtRsnCodeExp = rjt_expl;
5965         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5966         return 1;
5967 }
5968
5969
5970 static void
5971 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5972 {
5973         MAILBOX_t *mb;
5974         IOCB_t *icmd;
5975         uint8_t *pcmd;
5976         struct lpfc_iocbq *elsiocb;
5977         struct lpfc_nodelist *ndlp;
5978         struct ls_rjt *stat;
5979         union lpfc_sli4_cfg_shdr *shdr;
5980         struct lpfc_lcb_context *lcb_context;
5981         struct fc_lcb_res_frame *lcb_res;
5982         uint32_t cmdsize, shdr_status, shdr_add_status;
5983         int rc;
5984
5985         mb = &pmb->u.mb;
5986         lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
5987         ndlp = lcb_context->ndlp;
5988         pmb->ctx_ndlp = NULL;
5989         pmb->ctx_buf = NULL;
5990
5991         shdr = (union lpfc_sli4_cfg_shdr *)
5992                         &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
5993         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5994         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5995
5996         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
5997                                 "0194 SET_BEACON_CONFIG mailbox "
5998                                 "completed with status x%x add_status x%x,"
5999                                 " mbx status x%x\n",
6000                                 shdr_status, shdr_add_status, mb->mbxStatus);
6001
6002         if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
6003             (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
6004             (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
6005                 mempool_free(pmb, phba->mbox_mem_pool);
6006                 goto error;
6007         }
6008
6009         mempool_free(pmb, phba->mbox_mem_pool);
6010         cmdsize = sizeof(struct fc_lcb_res_frame);
6011         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6012                         lpfc_max_els_tries, ndlp,
6013                         ndlp->nlp_DID, ELS_CMD_ACC);
6014
6015         /* Decrement the ndlp reference count from previous mbox command */
6016         lpfc_nlp_put(ndlp);
6017
6018         if (!elsiocb)
6019                 goto free_lcb_context;
6020
6021         lcb_res = (struct fc_lcb_res_frame *)
6022                 (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6023
6024         memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
6025         icmd = &elsiocb->iocb;
6026         icmd->ulpContext = lcb_context->rx_id;
6027         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
6028
6029         pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6030         *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
6031         lcb_res->lcb_sub_command = lcb_context->sub_command;
6032         lcb_res->lcb_type = lcb_context->type;
6033         lcb_res->capability = lcb_context->capability;
6034         lcb_res->lcb_frequency = lcb_context->frequency;
6035         lcb_res->lcb_duration = lcb_context->duration;
6036         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6037         phba->fc_stat.elsXmitACC++;
6038         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6039         if (rc == IOCB_ERROR)
6040                 lpfc_els_free_iocb(phba, elsiocb);
6041
6042         kfree(lcb_context);
6043         return;
6044
6045 error:
6046         cmdsize = sizeof(struct fc_lcb_res_frame);
6047         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6048                         lpfc_max_els_tries, ndlp,
6049                         ndlp->nlp_DID, ELS_CMD_LS_RJT);
6050         lpfc_nlp_put(ndlp);
6051         if (!elsiocb)
6052                 goto free_lcb_context;
6053
6054         icmd = &elsiocb->iocb;
6055         icmd->ulpContext = lcb_context->rx_id;
6056         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
6057         pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6058
6059         *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
6060         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
6061         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6062
6063         if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
6064                 stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
6065
6066         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6067         phba->fc_stat.elsXmitLSRJT++;
6068         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6069         if (rc == IOCB_ERROR)
6070                 lpfc_els_free_iocb(phba, elsiocb);
6071 free_lcb_context:
6072         kfree(lcb_context);
6073 }
6074
6075 static int
6076 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
6077                      struct lpfc_lcb_context *lcb_context,
6078                      uint32_t beacon_state)
6079 {
6080         struct lpfc_hba *phba = vport->phba;
6081         union lpfc_sli4_cfg_shdr *cfg_shdr;
6082         LPFC_MBOXQ_t *mbox = NULL;
6083         uint32_t len;
6084         int rc;
6085
6086         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6087         if (!mbox)
6088                 return 1;
6089
6090         cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
6091         len = sizeof(struct lpfc_mbx_set_beacon_config) -
6092                 sizeof(struct lpfc_sli4_cfg_mhdr);
6093         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6094                          LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
6095                          LPFC_SLI4_MBX_EMBED);
6096         mbox->ctx_ndlp = (void *)lcb_context;
6097         mbox->vport = phba->pport;
6098         mbox->mbox_cmpl = lpfc_els_lcb_rsp;
6099         bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
6100                phba->sli4_hba.physical_port);
6101         bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
6102                beacon_state);
6103         mbox->u.mqe.un.beacon_config.word5 = 0;         /* Reserved */
6104
6105         /*
6106          *      Check bv1s bit before issuing the mailbox
6107          *      if bv1s == 1, LCB V1 supported
6108          *      else, LCB V0 supported
6109          */
6110
6111         if (phba->sli4_hba.pc_sli4_params.bv1s) {
6112                 /* COMMON_SET_BEACON_CONFIG_V1 */
6113                 cfg_shdr->request.word9 = BEACON_VERSION_V1;
6114                 lcb_context->capability |= LCB_CAPABILITY_DURATION;
6115                 bf_set(lpfc_mbx_set_beacon_port_type,
6116                        &mbox->u.mqe.un.beacon_config, 0);
6117                 bf_set(lpfc_mbx_set_beacon_duration_v1,
6118                        &mbox->u.mqe.un.beacon_config,
6119                        be16_to_cpu(lcb_context->duration));
6120         } else {
6121                 /* COMMON_SET_BEACON_CONFIG_V0 */
6122                 if (be16_to_cpu(lcb_context->duration) != 0) {
6123                         mempool_free(mbox, phba->mbox_mem_pool);
6124                         return 1;
6125                 }
6126                 cfg_shdr->request.word9 = BEACON_VERSION_V0;
6127                 lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
6128                 bf_set(lpfc_mbx_set_beacon_state,
6129                        &mbox->u.mqe.un.beacon_config, beacon_state);
6130                 bf_set(lpfc_mbx_set_beacon_port_type,
6131                        &mbox->u.mqe.un.beacon_config, 1);
6132                 bf_set(lpfc_mbx_set_beacon_duration,
6133                        &mbox->u.mqe.un.beacon_config,
6134                        be16_to_cpu(lcb_context->duration));
6135         }
6136
6137         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6138         if (rc == MBX_NOT_FINISHED) {
6139                 mempool_free(mbox, phba->mbox_mem_pool);
6140                 return 1;
6141         }
6142
6143         return 0;
6144 }
6145
6146
6147 /**
6148  * lpfc_els_rcv_lcb - Process an unsolicited LCB
6149  * @vport: pointer to a host virtual N_Port data structure.
6150  * @cmdiocb: pointer to lpfc command iocb data structure.
6151  * @ndlp: pointer to a node-list data structure.
6152  *
6153  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
6154  * First, the payload of the unsolicited LCB is checked.
6155  * Then based on Subcommand beacon will either turn on or off.
6156  *
6157  * Return code
6158  * 0 - Sent the acc response
6159  * 1 - Sent the reject response.
6160  **/
6161 static int
6162 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6163                  struct lpfc_nodelist *ndlp)
6164 {
6165         struct lpfc_hba *phba = vport->phba;
6166         struct lpfc_dmabuf *pcmd;
6167         uint8_t *lp;
6168         struct fc_lcb_request_frame *beacon;
6169         struct lpfc_lcb_context *lcb_context;
6170         uint8_t state, rjt_err;
6171         struct ls_rjt stat;
6172
6173         pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
6174         lp = (uint8_t *)pcmd->virt;
6175         beacon = (struct fc_lcb_request_frame *)pcmd->virt;
6176
6177         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6178                         "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
6179                         "type x%x frequency %x duration x%x\n",
6180                         lp[0], lp[1], lp[2],
6181                         beacon->lcb_command,
6182                         beacon->lcb_sub_command,
6183                         beacon->lcb_type,
6184                         beacon->lcb_frequency,
6185                         be16_to_cpu(beacon->lcb_duration));
6186
6187         if (beacon->lcb_sub_command != LPFC_LCB_ON &&
6188             beacon->lcb_sub_command != LPFC_LCB_OFF) {
6189                 rjt_err = LSRJT_CMD_UNSUPPORTED;
6190                 goto rjt;
6191         }
6192
6193         if (phba->sli_rev < LPFC_SLI_REV4  ||
6194             phba->hba_flag & HBA_FCOE_MODE ||
6195             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
6196             LPFC_SLI_INTF_IF_TYPE_2)) {
6197                 rjt_err = LSRJT_CMD_UNSUPPORTED;
6198                 goto rjt;
6199         }
6200
6201         lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
6202         if (!lcb_context) {
6203                 rjt_err = LSRJT_UNABLE_TPC;
6204                 goto rjt;
6205         }
6206
6207         state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
6208         lcb_context->sub_command = beacon->lcb_sub_command;
6209         lcb_context->capability = 0;
6210         lcb_context->type = beacon->lcb_type;
6211         lcb_context->frequency = beacon->lcb_frequency;
6212         lcb_context->duration = beacon->lcb_duration;
6213         lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6214         lcb_context->rx_id = cmdiocb->iocb.ulpContext;
6215         lcb_context->ndlp = lpfc_nlp_get(ndlp);
6216         if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
6217                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
6218                                  LOG_ELS, "0193 failed to send mail box");
6219                 kfree(lcb_context);
6220                 lpfc_nlp_put(ndlp);
6221                 rjt_err = LSRJT_UNABLE_TPC;
6222                 goto rjt;
6223         }
6224         return 0;
6225 rjt:
6226         memset(&stat, 0, sizeof(stat));
6227         stat.un.b.lsRjtRsnCode = rjt_err;
6228         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6229         return 1;
6230 }
6231
6232
6233 /**
6234  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
6235  * @vport: pointer to a host virtual N_Port data structure.
6236  *
6237  * This routine cleans up any Registration State Change Notification
6238  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
6239  * @vport together with the host_lock is used to prevent multiple thread
6240  * trying to access the RSCN array on a same @vport at the same time.
6241  **/
6242 void
6243 lpfc_els_flush_rscn(struct lpfc_vport *vport)
6244 {
6245         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6246         struct lpfc_hba  *phba = vport->phba;
6247         int i;
6248
6249         spin_lock_irq(shost->host_lock);
6250         if (vport->fc_rscn_flush) {
6251                 /* Another thread is walking fc_rscn_id_list on this vport */
6252                 spin_unlock_irq(shost->host_lock);
6253                 return;
6254         }
6255         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
6256         vport->fc_rscn_flush = 1;
6257         spin_unlock_irq(shost->host_lock);
6258
6259         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6260                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
6261                 vport->fc_rscn_id_list[i] = NULL;
6262         }
6263         spin_lock_irq(shost->host_lock);
6264         vport->fc_rscn_id_cnt = 0;
6265         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
6266         spin_unlock_irq(shost->host_lock);
6267         lpfc_can_disctmo(vport);
6268         /* Indicate we are done walking this fc_rscn_id_list */
6269         vport->fc_rscn_flush = 0;
6270 }
6271
6272 /**
6273  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
6274  * @vport: pointer to a host virtual N_Port data structure.
6275  * @did: remote destination port identifier.
6276  *
6277  * This routine checks whether there is any pending Registration State
6278  * Configuration Notification (RSCN) to a @did on @vport.
6279  *
6280  * Return code
6281  *   None zero - The @did matched with a pending rscn
6282  *   0 - not able to match @did with a pending rscn
6283  **/
6284 int
6285 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
6286 {
6287         D_ID ns_did;
6288         D_ID rscn_did;
6289         uint32_t *lp;
6290         uint32_t payload_len, i;
6291         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6292
6293         ns_did.un.word = did;
6294
6295         /* Never match fabric nodes for RSCNs */
6296         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6297                 return 0;
6298
6299         /* If we are doing a FULL RSCN rediscovery, match everything */
6300         if (vport->fc_flag & FC_RSCN_DISCOVERY)
6301                 return did;
6302
6303         spin_lock_irq(shost->host_lock);
6304         if (vport->fc_rscn_flush) {
6305                 /* Another thread is walking fc_rscn_id_list on this vport */
6306                 spin_unlock_irq(shost->host_lock);
6307                 return 0;
6308         }
6309         /* Indicate we are walking fc_rscn_id_list on this vport */
6310         vport->fc_rscn_flush = 1;
6311         spin_unlock_irq(shost->host_lock);
6312         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6313                 lp = vport->fc_rscn_id_list[i]->virt;
6314                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6315                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
6316                 while (payload_len) {
6317                         rscn_did.un.word = be32_to_cpu(*lp++);
6318                         payload_len -= sizeof(uint32_t);
6319                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
6320                         case RSCN_ADDRESS_FORMAT_PORT:
6321                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6322                                     && (ns_did.un.b.area == rscn_did.un.b.area)
6323                                     && (ns_did.un.b.id == rscn_did.un.b.id))
6324                                         goto return_did_out;
6325                                 break;
6326                         case RSCN_ADDRESS_FORMAT_AREA:
6327                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6328                                     && (ns_did.un.b.area == rscn_did.un.b.area))
6329                                         goto return_did_out;
6330                                 break;
6331                         case RSCN_ADDRESS_FORMAT_DOMAIN:
6332                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
6333                                         goto return_did_out;
6334                                 break;
6335                         case RSCN_ADDRESS_FORMAT_FABRIC:
6336                                 goto return_did_out;
6337                         }
6338                 }
6339         }
6340         /* Indicate we are done with walking fc_rscn_id_list on this vport */
6341         vport->fc_rscn_flush = 0;
6342         return 0;
6343 return_did_out:
6344         /* Indicate we are done with walking fc_rscn_id_list on this vport */
6345         vport->fc_rscn_flush = 0;
6346         return did;
6347 }
6348
6349 /**
6350  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
6351  * @vport: pointer to a host virtual N_Port data structure.
6352  *
6353  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
6354  * state machine for a @vport's nodes that are with pending RSCN (Registration
6355  * State Change Notification).
6356  *
6357  * Return code
6358  *   0 - Successful (currently alway return 0)
6359  **/
6360 static int
6361 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
6362 {
6363         struct lpfc_nodelist *ndlp = NULL;
6364
6365         /* Move all affected nodes by pending RSCNs to NPR state. */
6366         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6367                 if (!NLP_CHK_NODE_ACT(ndlp) ||
6368                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
6369                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
6370                         continue;
6371
6372                 /* NVME Target mode does not do RSCN Recovery. */
6373                 if (vport->phba->nvmet_support)
6374                         continue;
6375
6376                 /* If we are in the process of doing discovery on this
6377                  * NPort, let it continue on its own.
6378                  */
6379                 switch (ndlp->nlp_state) {
6380                 case  NLP_STE_PLOGI_ISSUE:
6381                 case  NLP_STE_ADISC_ISSUE:
6382                 case  NLP_STE_REG_LOGIN_ISSUE:
6383                 case  NLP_STE_PRLI_ISSUE:
6384                 case  NLP_STE_LOGO_ISSUE:
6385                         continue;
6386                 }
6387
6388                 /* Check to see if we need to NVME rescan this target
6389                  * remoteport.
6390                  */
6391                 if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
6392                     ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
6393                         lpfc_nvme_rescan_port(vport, ndlp);
6394
6395                 lpfc_disc_state_machine(vport, ndlp, NULL,
6396                                         NLP_EVT_DEVICE_RECOVERY);
6397                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
6398         }
6399         return 0;
6400 }
6401
6402 /**
6403  * lpfc_send_rscn_event - Send an RSCN event to management application
6404  * @vport: pointer to a host virtual N_Port data structure.
6405  * @cmdiocb: pointer to lpfc command iocb data structure.
6406  *
6407  * lpfc_send_rscn_event sends an RSCN netlink event to management
6408  * applications.
6409  */
6410 static void
6411 lpfc_send_rscn_event(struct lpfc_vport *vport,
6412                 struct lpfc_iocbq *cmdiocb)
6413 {
6414         struct lpfc_dmabuf *pcmd;
6415         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6416         uint32_t *payload_ptr;
6417         uint32_t payload_len;
6418         struct lpfc_rscn_event_header *rscn_event_data;
6419
6420         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6421         payload_ptr = (uint32_t *) pcmd->virt;
6422         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
6423
6424         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
6425                 payload_len, GFP_KERNEL);
6426         if (!rscn_event_data) {
6427                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6428                         "0147 Failed to allocate memory for RSCN event\n");
6429                 return;
6430         }
6431         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
6432         rscn_event_data->payload_length = payload_len;
6433         memcpy(rscn_event_data->rscn_payload, payload_ptr,
6434                 payload_len);
6435
6436         fc_host_post_vendor_event(shost,
6437                 fc_get_event_number(),
6438                 sizeof(struct lpfc_rscn_event_header) + payload_len,
6439                 (char *)rscn_event_data,
6440                 LPFC_NL_VENDOR_ID);
6441
6442         kfree(rscn_event_data);
6443 }
6444
6445 /**
6446  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
6447  * @vport: pointer to a host virtual N_Port data structure.
6448  * @cmdiocb: pointer to lpfc command iocb data structure.
6449  * @ndlp: pointer to a node-list data structure.
6450  *
6451  * This routine processes an unsolicited RSCN (Registration State Change
6452  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
6453  * to invoke fc_host_post_event() routine to the FC transport layer. If the
6454  * discover state machine is about to begin discovery, it just accepts the
6455  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
6456  * contains N_Port IDs for other vports on this HBA, it just accepts the
6457  * RSCN and ignore processing it. If the state machine is in the recovery
6458  * state, the fc_rscn_id_list of this @vport is walked and the
6459  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
6460  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
6461  * routine is invoked to handle the RSCN event.
6462  *
6463  * Return code
6464  *   0 - Just sent the acc response
6465  *   1 - Sent the acc response and waited for name server completion
6466  **/
6467 static int
6468 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6469                   struct lpfc_nodelist *ndlp)
6470 {
6471         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6472         struct lpfc_hba  *phba = vport->phba;
6473         struct lpfc_dmabuf *pcmd;
6474         uint32_t *lp, *datap;
6475         uint32_t payload_len, length, nportid, *cmd;
6476         int rscn_cnt;
6477         int rscn_id = 0, hba_id = 0;
6478         int i, tmo;
6479
6480         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6481         lp = (uint32_t *) pcmd->virt;
6482
6483         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6484         payload_len -= sizeof(uint32_t);        /* take off word 0 */
6485         /* RSCN received */
6486         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6487                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
6488                          vport->fc_flag, payload_len, *lp,
6489                          vport->fc_rscn_id_cnt);
6490
6491         /* Send an RSCN event to the management application */
6492         lpfc_send_rscn_event(vport, cmdiocb);
6493
6494         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
6495                 fc_host_post_event(shost, fc_get_event_number(),
6496                         FCH_EVT_RSCN, lp[i]);
6497
6498         /* Check if RSCN is coming from a direct-connected remote NPort */
6499         if (vport->fc_flag & FC_PT2PT) {
6500                 /* If so, just ACC it, no other action needed for now */
6501                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6502                                  "2024 pt2pt RSCN %08x Data: x%x x%x\n",
6503                                  *lp, vport->fc_flag, payload_len);
6504                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6505
6506                 /* Check to see if we need to NVME rescan this target
6507                  * remoteport.
6508                  */
6509                 if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
6510                     ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
6511                         lpfc_nvme_rescan_port(vport, ndlp);
6512                 return 0;
6513         }
6514
6515         /* If we are about to begin discovery, just ACC the RSCN.
6516          * Discovery processing will satisfy it.
6517          */
6518         if (vport->port_state <= LPFC_NS_QRY) {
6519                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6520                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
6521                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6522
6523                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6524                 return 0;
6525         }
6526
6527         /* If this RSCN just contains NPortIDs for other vports on this HBA,
6528          * just ACC and ignore it.
6529          */
6530         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6531                 !(vport->cfg_peer_port_login)) {
6532                 i = payload_len;
6533                 datap = lp;
6534                 while (i > 0) {
6535                         nportid = *datap++;
6536                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
6537                         i -= sizeof(uint32_t);
6538                         rscn_id++;
6539                         if (lpfc_find_vport_by_did(phba, nportid))
6540                                 hba_id++;
6541                 }
6542                 if (rscn_id == hba_id) {
6543                         /* ALL NPortIDs in RSCN are on HBA */
6544                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6545                                          "0219 Ignore RSCN "
6546                                          "Data: x%x x%x x%x x%x\n",
6547                                          vport->fc_flag, payload_len,
6548                                          *lp, vport->fc_rscn_id_cnt);
6549                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6550                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
6551                                 ndlp->nlp_DID, vport->port_state,
6552                                 ndlp->nlp_flag);
6553
6554                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
6555                                 ndlp, NULL);
6556                         return 0;
6557                 }
6558         }
6559
6560         spin_lock_irq(shost->host_lock);
6561         if (vport->fc_rscn_flush) {
6562                 /* Another thread is walking fc_rscn_id_list on this vport */
6563                 vport->fc_flag |= FC_RSCN_DISCOVERY;
6564                 spin_unlock_irq(shost->host_lock);
6565                 /* Send back ACC */
6566                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6567                 return 0;
6568         }
6569         /* Indicate we are walking fc_rscn_id_list on this vport */
6570         vport->fc_rscn_flush = 1;
6571         spin_unlock_irq(shost->host_lock);
6572         /* Get the array count after successfully have the token */
6573         rscn_cnt = vport->fc_rscn_id_cnt;
6574         /* If we are already processing an RSCN, save the received
6575          * RSCN payload buffer, cmdiocb->context2 to process later.
6576          */
6577         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
6578                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6579                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
6580                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6581
6582                 spin_lock_irq(shost->host_lock);
6583                 vport->fc_flag |= FC_RSCN_DEFERRED;
6584
6585                 /* Restart disctmo if its already running */
6586                 if (vport->fc_flag & FC_DISC_TMO) {
6587                         tmo = ((phba->fc_ratov * 3) + 3);
6588                         mod_timer(&vport->fc_disctmo,
6589                                   jiffies + msecs_to_jiffies(1000 * tmo));
6590                 }
6591                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
6592                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
6593                         vport->fc_flag |= FC_RSCN_MODE;
6594                         spin_unlock_irq(shost->host_lock);
6595                         if (rscn_cnt) {
6596                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
6597                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
6598                         }
6599                         if ((rscn_cnt) &&
6600                             (payload_len + length <= LPFC_BPL_SIZE)) {
6601                                 *cmd &= ELS_CMD_MASK;
6602                                 *cmd |= cpu_to_be32(payload_len + length);
6603                                 memcpy(((uint8_t *)cmd) + length, lp,
6604                                        payload_len);
6605                         } else {
6606                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
6607                                 vport->fc_rscn_id_cnt++;
6608                                 /* If we zero, cmdiocb->context2, the calling
6609                                  * routine will not try to free it.
6610                                  */
6611                                 cmdiocb->context2 = NULL;
6612                         }
6613                         /* Deferred RSCN */
6614                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6615                                          "0235 Deferred RSCN "
6616                                          "Data: x%x x%x x%x\n",
6617                                          vport->fc_rscn_id_cnt, vport->fc_flag,
6618                                          vport->port_state);
6619                 } else {
6620                         vport->fc_flag |= FC_RSCN_DISCOVERY;
6621                         spin_unlock_irq(shost->host_lock);
6622                         /* ReDiscovery RSCN */
6623                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6624                                          "0234 ReDiscovery RSCN "
6625                                          "Data: x%x x%x x%x\n",
6626                                          vport->fc_rscn_id_cnt, vport->fc_flag,
6627                                          vport->port_state);
6628                 }
6629                 /* Indicate we are done walking fc_rscn_id_list on this vport */
6630                 vport->fc_rscn_flush = 0;
6631                 /* Send back ACC */
6632                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6633                 /* send RECOVERY event for ALL nodes that match RSCN payload */
6634                 lpfc_rscn_recovery_check(vport);
6635                 return 0;
6636         }
6637         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6638                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
6639                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6640
6641         spin_lock_irq(shost->host_lock);
6642         vport->fc_flag |= FC_RSCN_MODE;
6643         spin_unlock_irq(shost->host_lock);
6644         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
6645         /* Indicate we are done walking fc_rscn_id_list on this vport */
6646         vport->fc_rscn_flush = 0;
6647         /*
6648          * If we zero, cmdiocb->context2, the calling routine will
6649          * not try to free it.
6650          */
6651         cmdiocb->context2 = NULL;
6652         lpfc_set_disctmo(vport);
6653         /* Send back ACC */
6654         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6655         /* send RECOVERY event for ALL nodes that match RSCN payload */
6656         lpfc_rscn_recovery_check(vport);
6657         return lpfc_els_handle_rscn(vport);
6658 }
6659
6660 /**
6661  * lpfc_els_handle_rscn - Handle rscn for a vport
6662  * @vport: pointer to a host virtual N_Port data structure.
6663  *
6664  * This routine handles the Registration State Configuration Notification
6665  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
6666  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
6667  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
6668  * NameServer shall be issued. If CT command to the NameServer fails to be
6669  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
6670  * RSCN activities with the @vport.
6671  *
6672  * Return code
6673  *   0 - Cleaned up rscn on the @vport
6674  *   1 - Wait for plogi to name server before proceed
6675  **/
6676 int
6677 lpfc_els_handle_rscn(struct lpfc_vport *vport)
6678 {
6679         struct lpfc_nodelist *ndlp;
6680         struct lpfc_hba  *phba = vport->phba;
6681
6682         /* Ignore RSCN if the port is being torn down. */
6683         if (vport->load_flag & FC_UNLOADING) {
6684                 lpfc_els_flush_rscn(vport);
6685                 return 0;
6686         }
6687
6688         /* Start timer for RSCN processing */
6689         lpfc_set_disctmo(vport);
6690
6691         /* RSCN processed */
6692         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6693                          "0215 RSCN processed Data: x%x x%x x%x x%x x%x x%x\n",
6694                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
6695                          vport->port_state, vport->num_disc_nodes,
6696                          vport->gidft_inp);
6697
6698         /* To process RSCN, first compare RSCN data with NameServer */
6699         vport->fc_ns_retry = 0;
6700         vport->num_disc_nodes = 0;
6701
6702         ndlp = lpfc_findnode_did(vport, NameServer_DID);
6703         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
6704             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
6705                 /* Good ndlp, issue CT Request to NameServer.  Need to
6706                  * know how many gidfts were issued.  If none, then just
6707                  * flush the RSCN.  Otherwise, the outstanding requests
6708                  * need to complete.
6709                  */
6710                 if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
6711                         if (lpfc_issue_gidft(vport) > 0)
6712                                 return 1;
6713                 } else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
6714                         if (lpfc_issue_gidpt(vport) > 0)
6715                                 return 1;
6716                 } else {
6717                         return 1;
6718                 }
6719         } else {
6720                 /* Nameserver login in question.  Revalidate. */
6721                 if (ndlp) {
6722                         ndlp = lpfc_enable_node(vport, ndlp,
6723                                                 NLP_STE_PLOGI_ISSUE);
6724                         if (!ndlp) {
6725                                 lpfc_els_flush_rscn(vport);
6726                                 return 0;
6727                         }
6728                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
6729                 } else {
6730                         ndlp = lpfc_nlp_init(vport, NameServer_DID);
6731                         if (!ndlp) {
6732                                 lpfc_els_flush_rscn(vport);
6733                                 return 0;
6734                         }
6735                         ndlp->nlp_prev_state = ndlp->nlp_state;
6736                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6737                 }
6738                 ndlp->nlp_type |= NLP_FABRIC;
6739                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
6740                 /* Wait for NameServer login cmpl before we can
6741                  * continue
6742                  */
6743                 return 1;
6744         }
6745
6746         lpfc_els_flush_rscn(vport);
6747         return 0;
6748 }
6749
6750 /**
6751  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
6752  * @vport: pointer to a host virtual N_Port data structure.
6753  * @cmdiocb: pointer to lpfc command iocb data structure.
6754  * @ndlp: pointer to a node-list data structure.
6755  *
6756  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
6757  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
6758  * point topology. As an unsolicited FLOGI should not be received in a loop
6759  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
6760  * lpfc_check_sparm() routine is invoked to check the parameters in the
6761  * unsolicited FLOGI. If parameters validation failed, the routine
6762  * lpfc_els_rsp_reject() shall be called with reject reason code set to
6763  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
6764  * FLOGI shall be compared with the Port WWN of the @vport to determine who
6765  * will initiate PLOGI. The higher lexicographical value party shall has
6766  * higher priority (as the winning port) and will initiate PLOGI and
6767  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
6768  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
6769  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
6770  *
6771  * Return code
6772  *   0 - Successfully processed the unsolicited flogi
6773  *   1 - Failed to process the unsolicited flogi
6774  **/
6775 static int
6776 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6777                    struct lpfc_nodelist *ndlp)
6778 {
6779         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6780         struct lpfc_hba  *phba = vport->phba;
6781         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6782         uint32_t *lp = (uint32_t *) pcmd->virt;
6783         IOCB_t *icmd = &cmdiocb->iocb;
6784         struct serv_parm *sp;
6785         LPFC_MBOXQ_t *mbox;
6786         uint32_t cmd, did;
6787         int rc;
6788         uint32_t fc_flag = 0;
6789         uint32_t port_state = 0;
6790
6791         cmd = *lp++;
6792         sp = (struct serv_parm *) lp;
6793
6794         /* FLOGI received */
6795
6796         lpfc_set_disctmo(vport);
6797
6798         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6799                 /* We should never receive a FLOGI in loop mode, ignore it */
6800                 did = icmd->un.elsreq64.remoteID;
6801
6802                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
6803                    Loop Mode */
6804                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6805                                  "0113 An FLOGI ELS command x%x was "
6806                                  "received from DID x%x in Loop Mode\n",
6807                                  cmd, did);
6808                 return 1;
6809         }
6810
6811         (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
6812
6813         /*
6814          * If our portname is greater than the remote portname,
6815          * then we initiate Nport login.
6816          */
6817
6818         rc = memcmp(&vport->fc_portname, &sp->portName,
6819                     sizeof(struct lpfc_name));
6820
6821         if (!rc) {
6822                 if (phba->sli_rev < LPFC_SLI_REV4) {
6823                         mbox = mempool_alloc(phba->mbox_mem_pool,
6824                                              GFP_KERNEL);
6825                         if (!mbox)
6826                                 return 1;
6827                         lpfc_linkdown(phba);
6828                         lpfc_init_link(phba, mbox,
6829                                        phba->cfg_topology,
6830                                        phba->cfg_link_speed);
6831                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6832                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6833                         mbox->vport = vport;
6834                         rc = lpfc_sli_issue_mbox(phba, mbox,
6835                                                  MBX_NOWAIT);
6836                         lpfc_set_loopback_flag(phba);
6837                         if (rc == MBX_NOT_FINISHED)
6838                                 mempool_free(mbox, phba->mbox_mem_pool);
6839                         return 1;
6840                 }
6841
6842                 /* abort the flogi coming back to ourselves
6843                  * due to external loopback on the port.
6844                  */
6845                 lpfc_els_abort_flogi(phba);
6846                 return 0;
6847
6848         } else if (rc > 0) {    /* greater than */
6849                 spin_lock_irq(shost->host_lock);
6850                 vport->fc_flag |= FC_PT2PT_PLOGI;
6851                 spin_unlock_irq(shost->host_lock);
6852
6853                 /* If we have the high WWPN we can assign our own
6854                  * myDID; otherwise, we have to WAIT for a PLOGI
6855                  * from the remote NPort to find out what it
6856                  * will be.
6857                  */
6858                 vport->fc_myDID = PT2PT_LocalID;
6859         } else {
6860                 vport->fc_myDID = PT2PT_RemoteID;
6861         }
6862
6863         /*
6864          * The vport state should go to LPFC_FLOGI only
6865          * AFTER we issue a FLOGI, not receive one.
6866          */
6867         spin_lock_irq(shost->host_lock);
6868         fc_flag = vport->fc_flag;
6869         port_state = vport->port_state;
6870         vport->fc_flag |= FC_PT2PT;
6871         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6872
6873         /* Acking an unsol FLOGI.  Count 1 for link bounce
6874          * work-around.
6875          */
6876         vport->rcv_flogi_cnt++;
6877         spin_unlock_irq(shost->host_lock);
6878         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6879                          "3311 Rcv Flogi PS x%x new PS x%x "
6880                          "fc_flag x%x new fc_flag x%x\n",
6881                          port_state, vport->port_state,
6882                          fc_flag, vport->fc_flag);
6883
6884         /*
6885          * We temporarily set fc_myDID to make it look like we are
6886          * a Fabric. This is done just so we end up with the right
6887          * did / sid on the FLOGI ACC rsp.
6888          */
6889         did = vport->fc_myDID;
6890         vport->fc_myDID = Fabric_DID;
6891
6892         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
6893
6894         /* Defer ACC response until AFTER we issue a FLOGI */
6895         if (!(phba->hba_flag & HBA_FLOGI_ISSUED)) {
6896                 phba->defer_flogi_acc_rx_id = cmdiocb->iocb.ulpContext;
6897                 phba->defer_flogi_acc_ox_id =
6898                                         cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6899
6900                 vport->fc_myDID = did;
6901
6902                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6903                                  "3344 Deferring FLOGI ACC: rx_id: x%x,"
6904                                  " ox_id: x%x, hba_flag x%x\n",
6905                                  phba->defer_flogi_acc_rx_id,
6906                                  phba->defer_flogi_acc_ox_id, phba->hba_flag);
6907
6908                 phba->defer_flogi_acc_flag = true;
6909
6910                 return 0;
6911         }
6912
6913         /* Send back ACC */
6914         lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
6915
6916         /* Now lets put fc_myDID back to what its supposed to be */
6917         vport->fc_myDID = did;
6918
6919         return 0;
6920 }
6921
6922 /**
6923  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
6924  * @vport: pointer to a host virtual N_Port data structure.
6925  * @cmdiocb: pointer to lpfc command iocb data structure.
6926  * @ndlp: pointer to a node-list data structure.
6927  *
6928  * This routine processes Request Node Identification Data (RNID) IOCB
6929  * received as an ELS unsolicited event. Only when the RNID specified format
6930  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
6931  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
6932  * Accept (ACC) the RNID ELS command. All the other RNID formats are
6933  * rejected by invoking the lpfc_els_rsp_reject() routine.
6934  *
6935  * Return code
6936  *   0 - Successfully processed rnid iocb (currently always return 0)
6937  **/
6938 static int
6939 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6940                   struct lpfc_nodelist *ndlp)
6941 {
6942         struct lpfc_dmabuf *pcmd;
6943         uint32_t *lp;
6944         RNID *rn;
6945         struct ls_rjt stat;
6946
6947         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6948         lp = (uint32_t *) pcmd->virt;
6949
6950         lp++;
6951         rn = (RNID *) lp;
6952
6953         /* RNID received */
6954
6955         switch (rn->Format) {
6956         case 0:
6957         case RNID_TOPOLOGY_DISC:
6958                 /* Send back ACC */
6959                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
6960                 break;
6961         default:
6962                 /* Reject this request because format not supported */
6963                 stat.un.b.lsRjtRsvd0 = 0;
6964                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6965                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6966                 stat.un.b.vendorUnique = 0;
6967                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6968                         NULL);
6969         }
6970         return 0;
6971 }
6972
6973 /**
6974  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
6975  * @vport: pointer to a host virtual N_Port data structure.
6976  * @cmdiocb: pointer to lpfc command iocb data structure.
6977  * @ndlp: pointer to a node-list data structure.
6978  *
6979  * Return code
6980  *   0 - Successfully processed echo iocb (currently always return 0)
6981  **/
6982 static int
6983 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6984                   struct lpfc_nodelist *ndlp)
6985 {
6986         uint8_t *pcmd;
6987
6988         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
6989
6990         /* skip over first word of echo command to find echo data */
6991         pcmd += sizeof(uint32_t);
6992
6993         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
6994         return 0;
6995 }
6996
6997 /**
6998  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
6999  * @vport: pointer to a host virtual N_Port data structure.
7000  * @cmdiocb: pointer to lpfc command iocb data structure.
7001  * @ndlp: pointer to a node-list data structure.
7002  *
7003  * This routine processes a Link Incident Report Registration(LIRR) IOCB
7004  * received as an ELS unsolicited event. Currently, this function just invokes
7005  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
7006  *
7007  * Return code
7008  *   0 - Successfully processed lirr iocb (currently always return 0)
7009  **/
7010 static int
7011 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7012                   struct lpfc_nodelist *ndlp)
7013 {
7014         struct ls_rjt stat;
7015
7016         /* For now, unconditionally reject this command */
7017         stat.un.b.lsRjtRsvd0 = 0;
7018         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7019         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7020         stat.un.b.vendorUnique = 0;
7021         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7022         return 0;
7023 }
7024
7025 /**
7026  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
7027  * @vport: pointer to a host virtual N_Port data structure.
7028  * @cmdiocb: pointer to lpfc command iocb data structure.
7029  * @ndlp: pointer to a node-list data structure.
7030  *
7031  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
7032  * received as an ELS unsolicited event. A request to RRQ shall only
7033  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
7034  * Nx_Port N_Port_ID of the target Exchange is the same as the
7035  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
7036  * not accepted, an LS_RJT with reason code "Unable to perform
7037  * command request" and reason code explanation "Invalid Originator
7038  * S_ID" shall be returned. For now, we just unconditionally accept
7039  * RRQ from the target.
7040  **/
7041 static void
7042 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7043                  struct lpfc_nodelist *ndlp)
7044 {
7045         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7046         if (vport->phba->sli_rev == LPFC_SLI_REV4)
7047                 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
7048 }
7049
7050 /**
7051  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
7052  * @phba: pointer to lpfc hba data structure.
7053  * @pmb: pointer to the driver internal queue element for mailbox command.
7054  *
7055  * This routine is the completion callback function for the MBX_READ_LNK_STAT
7056  * mailbox command. This callback function is to actually send the Accept
7057  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
7058  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
7059  * mailbox command, constructs the RPS response with the link statistics
7060  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
7061  * response to the RPS.
7062  *
7063  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7064  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7065  * will be stored into the context1 field of the IOCB for the completion
7066  * callback function to the RPS Accept Response ELS IOCB command.
7067  *
7068  **/
7069 static void
7070 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7071 {
7072         MAILBOX_t *mb;
7073         IOCB_t *icmd;
7074         struct RLS_RSP *rls_rsp;
7075         uint8_t *pcmd;
7076         struct lpfc_iocbq *elsiocb;
7077         struct lpfc_nodelist *ndlp;
7078         uint16_t oxid;
7079         uint16_t rxid;
7080         uint32_t cmdsize;
7081
7082         mb = &pmb->u.mb;
7083
7084         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
7085         rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
7086         oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
7087         pmb->ctx_buf = NULL;
7088         pmb->ctx_ndlp = NULL;
7089
7090         if (mb->mbxStatus) {
7091                 mempool_free(pmb, phba->mbox_mem_pool);
7092                 return;
7093         }
7094
7095         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
7096         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7097                                      lpfc_max_els_tries, ndlp,
7098                                      ndlp->nlp_DID, ELS_CMD_ACC);
7099
7100         /* Decrement the ndlp reference count from previous mbox command */
7101         lpfc_nlp_put(ndlp);
7102
7103         if (!elsiocb) {
7104                 mempool_free(pmb, phba->mbox_mem_pool);
7105                 return;
7106         }
7107
7108         icmd = &elsiocb->iocb;
7109         icmd->ulpContext = rxid;
7110         icmd->unsli3.rcvsli3.ox_id = oxid;
7111
7112         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7113         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7114         pcmd += sizeof(uint32_t); /* Skip past command */
7115         rls_rsp = (struct RLS_RSP *)pcmd;
7116
7117         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
7118         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
7119         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
7120         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
7121         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
7122         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7123         mempool_free(pmb, phba->mbox_mem_pool);
7124         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
7125         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7126                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
7127                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
7128                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7129                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7130                          ndlp->nlp_rpi);
7131         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7132         phba->fc_stat.elsXmitACC++;
7133         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7134                 lpfc_els_free_iocb(phba, elsiocb);
7135 }
7136
7137 /**
7138  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
7139  * @phba: pointer to lpfc hba data structure.
7140  * @pmb: pointer to the driver internal queue element for mailbox command.
7141  *
7142  * This routine is the completion callback function for the MBX_READ_LNK_STAT
7143  * mailbox command. This callback function is to actually send the Accept
7144  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
7145  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
7146  * mailbox command, constructs the RPS response with the link statistics
7147  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
7148  * response to the RPS.
7149  *
7150  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7151  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7152  * will be stored into the context1 field of the IOCB for the completion
7153  * callback function to the RPS Accept Response ELS IOCB command.
7154  *
7155  **/
7156 static void
7157 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7158 {
7159         MAILBOX_t *mb;
7160         IOCB_t *icmd;
7161         RPS_RSP *rps_rsp;
7162         uint8_t *pcmd;
7163         struct lpfc_iocbq *elsiocb;
7164         struct lpfc_nodelist *ndlp;
7165         uint16_t status;
7166         uint16_t oxid;
7167         uint16_t rxid;
7168         uint32_t cmdsize;
7169
7170         mb = &pmb->u.mb;
7171
7172         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
7173         rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
7174         oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
7175         pmb->ctx_ndlp = NULL;
7176         pmb->ctx_buf = NULL;
7177
7178         if (mb->mbxStatus) {
7179                 mempool_free(pmb, phba->mbox_mem_pool);
7180                 return;
7181         }
7182
7183         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
7184         mempool_free(pmb, phba->mbox_mem_pool);
7185         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7186                                      lpfc_max_els_tries, ndlp,
7187                                      ndlp->nlp_DID, ELS_CMD_ACC);
7188
7189         /* Decrement the ndlp reference count from previous mbox command */
7190         lpfc_nlp_put(ndlp);
7191
7192         if (!elsiocb)
7193                 return;
7194
7195         icmd = &elsiocb->iocb;
7196         icmd->ulpContext = rxid;
7197         icmd->unsli3.rcvsli3.ox_id = oxid;
7198
7199         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7200         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7201         pcmd += sizeof(uint32_t); /* Skip past command */
7202         rps_rsp = (RPS_RSP *)pcmd;
7203
7204         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
7205                 status = 0x10;
7206         else
7207                 status = 0x8;
7208         if (phba->pport->fc_flag & FC_FABRIC)
7209                 status |= 0x4;
7210
7211         rps_rsp->rsvd1 = 0;
7212         rps_rsp->portStatus = cpu_to_be16(status);
7213         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
7214         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
7215         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
7216         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
7217         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
7218         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7219         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
7220         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7221                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
7222                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
7223                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7224                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7225                          ndlp->nlp_rpi);
7226         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7227         phba->fc_stat.elsXmitACC++;
7228         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7229                 lpfc_els_free_iocb(phba, elsiocb);
7230         return;
7231 }
7232
7233 /**
7234  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
7235  * @vport: pointer to a host virtual N_Port data structure.
7236  * @cmdiocb: pointer to lpfc command iocb data structure.
7237  * @ndlp: pointer to a node-list data structure.
7238  *
7239  * This routine processes Read Port Status (RPL) IOCB received as an
7240  * ELS unsolicited event. It first checks the remote port state. If the
7241  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7242  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7243  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7244  * for reading the HBA link statistics. It is for the callback function,
7245  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
7246  * to actually sending out RPL Accept (ACC) response.
7247  *
7248  * Return codes
7249  *   0 - Successfully processed rls iocb (currently always return 0)
7250  **/
7251 static int
7252 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7253                  struct lpfc_nodelist *ndlp)
7254 {
7255         struct lpfc_hba *phba = vport->phba;
7256         LPFC_MBOXQ_t *mbox;
7257         struct ls_rjt stat;
7258
7259         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7260             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7261                 /* reject the unsolicited RPS request and done with it */
7262                 goto reject_out;
7263
7264         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7265         if (mbox) {
7266                 lpfc_read_lnk_stat(phba, mbox);
7267                 mbox->ctx_buf = (void *)((unsigned long)
7268                         ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7269                         cmdiocb->iocb.ulpContext)); /* rx_id */
7270                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
7271                 mbox->vport = vport;
7272                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
7273                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7274                         != MBX_NOT_FINISHED)
7275                         /* Mbox completion will send ELS Response */
7276                         return 0;
7277                 /* Decrement reference count used for the failed mbox
7278                  * command.
7279                  */
7280                 lpfc_nlp_put(ndlp);
7281                 mempool_free(mbox, phba->mbox_mem_pool);
7282         }
7283 reject_out:
7284         /* issue rejection response */
7285         stat.un.b.lsRjtRsvd0 = 0;
7286         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7287         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7288         stat.un.b.vendorUnique = 0;
7289         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7290         return 0;
7291 }
7292
7293 /**
7294  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
7295  * @vport: pointer to a host virtual N_Port data structure.
7296  * @cmdiocb: pointer to lpfc command iocb data structure.
7297  * @ndlp: pointer to a node-list data structure.
7298  *
7299  * This routine processes Read Timout Value (RTV) IOCB received as an
7300  * ELS unsolicited event. It first checks the remote port state. If the
7301  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7302  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7303  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
7304  * Value (RTV) unsolicited IOCB event.
7305  *
7306  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7307  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7308  * will be stored into the context1 field of the IOCB for the completion
7309  * callback function to the RPS Accept Response ELS IOCB command.
7310  *
7311  * Return codes
7312  *   0 - Successfully processed rtv iocb (currently always return 0)
7313  **/
7314 static int
7315 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7316                  struct lpfc_nodelist *ndlp)
7317 {
7318         struct lpfc_hba *phba = vport->phba;
7319         struct ls_rjt stat;
7320         struct RTV_RSP *rtv_rsp;
7321         uint8_t *pcmd;
7322         struct lpfc_iocbq *elsiocb;
7323         uint32_t cmdsize;
7324
7325
7326         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7327             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7328                 /* reject the unsolicited RPS request and done with it */
7329                 goto reject_out;
7330
7331         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
7332         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7333                                      lpfc_max_els_tries, ndlp,
7334                                      ndlp->nlp_DID, ELS_CMD_ACC);
7335
7336         if (!elsiocb)
7337                 return 1;
7338
7339         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7340         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7341         pcmd += sizeof(uint32_t); /* Skip past command */
7342
7343         /* use the command's xri in the response */
7344         elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
7345         elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7346
7347         rtv_rsp = (struct RTV_RSP *)pcmd;
7348
7349         /* populate RTV payload */
7350         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
7351         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
7352         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
7353         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
7354         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
7355
7356         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
7357         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7358                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
7359                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
7360                          "Data: x%x x%x x%x\n",
7361                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7362                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7363                          ndlp->nlp_rpi,
7364                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
7365         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7366         phba->fc_stat.elsXmitACC++;
7367         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7368                 lpfc_els_free_iocb(phba, elsiocb);
7369         return 0;
7370
7371 reject_out:
7372         /* issue rejection response */
7373         stat.un.b.lsRjtRsvd0 = 0;
7374         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7375         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7376         stat.un.b.vendorUnique = 0;
7377         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7378         return 0;
7379 }
7380
7381 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
7382  * @vport: pointer to a host virtual N_Port data structure.
7383  * @cmdiocb: pointer to lpfc command iocb data structure.
7384  * @ndlp: pointer to a node-list data structure.
7385  *
7386  * This routine processes Read Port Status (RPS) IOCB received as an
7387  * ELS unsolicited event. It first checks the remote port state. If the
7388  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7389  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
7390  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7391  * for reading the HBA link statistics. It is for the callback function,
7392  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
7393  * to actually sending out RPS Accept (ACC) response.
7394  *
7395  * Return codes
7396  *   0 - Successfully processed rps iocb (currently always return 0)
7397  **/
7398 static int
7399 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7400                  struct lpfc_nodelist *ndlp)
7401 {
7402         struct lpfc_hba *phba = vport->phba;
7403         uint32_t *lp;
7404         uint8_t flag;
7405         LPFC_MBOXQ_t *mbox;
7406         struct lpfc_dmabuf *pcmd;
7407         RPS *rps;
7408         struct ls_rjt stat;
7409
7410         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7411             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7412                 /* reject the unsolicited RPS request and done with it */
7413                 goto reject_out;
7414
7415         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7416         lp = (uint32_t *) pcmd->virt;
7417         flag = (be32_to_cpu(*lp++) & 0xf);
7418         rps = (RPS *) lp;
7419
7420         if ((flag == 0) ||
7421             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
7422             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
7423                                     sizeof(struct lpfc_name)) == 0))) {
7424
7425                 printk("Fix me....\n");
7426                 dump_stack();
7427                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7428                 if (mbox) {
7429                         lpfc_read_lnk_stat(phba, mbox);
7430                         mbox->ctx_buf = (void *)((unsigned long)
7431                                 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7432                                 cmdiocb->iocb.ulpContext)); /* rx_id */
7433                         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
7434                         mbox->vport = vport;
7435                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
7436                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7437                                 != MBX_NOT_FINISHED)
7438                                 /* Mbox completion will send ELS Response */
7439                                 return 0;
7440                         /* Decrement reference count used for the failed mbox
7441                          * command.
7442                          */
7443                         lpfc_nlp_put(ndlp);
7444                         mempool_free(mbox, phba->mbox_mem_pool);
7445                 }
7446         }
7447
7448 reject_out:
7449         /* issue rejection response */
7450         stat.un.b.lsRjtRsvd0 = 0;
7451         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7452         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7453         stat.un.b.vendorUnique = 0;
7454         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7455         return 0;
7456 }
7457
7458 /* lpfc_issue_els_rrq - Process an unsolicited rps iocb
7459  * @vport: pointer to a host virtual N_Port data structure.
7460  * @ndlp: pointer to a node-list data structure.
7461  * @did: DID of the target.
7462  * @rrq: Pointer to the rrq struct.
7463  *
7464  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
7465  * Successful the the completion handler will clear the RRQ.
7466  *
7467  * Return codes
7468  *   0 - Successfully sent rrq els iocb.
7469  *   1 - Failed to send rrq els iocb.
7470  **/
7471 static int
7472 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7473                         uint32_t did, struct lpfc_node_rrq *rrq)
7474 {
7475         struct lpfc_hba  *phba = vport->phba;
7476         struct RRQ *els_rrq;
7477         struct lpfc_iocbq *elsiocb;
7478         uint8_t *pcmd;
7479         uint16_t cmdsize;
7480         int ret;
7481
7482
7483         if (ndlp != rrq->ndlp)
7484                 ndlp = rrq->ndlp;
7485         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7486                 return 1;
7487
7488         /* If ndlp is not NULL, we will bump the reference count on it */
7489         cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
7490         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
7491                                      ELS_CMD_RRQ);
7492         if (!elsiocb)
7493                 return 1;
7494
7495         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7496
7497         /* For RRQ request, remainder of payload is Exchange IDs */
7498         *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
7499         pcmd += sizeof(uint32_t);
7500         els_rrq = (struct RRQ *) pcmd;
7501
7502         bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
7503         bf_set(rrq_rxid, els_rrq, rrq->rxid);
7504         bf_set(rrq_did, els_rrq, vport->fc_myDID);
7505         els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
7506         els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
7507
7508
7509         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7510                 "Issue RRQ:     did:x%x",
7511                 did, rrq->xritag, rrq->rxid);
7512         elsiocb->context_un.rrq = rrq;
7513         elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
7514         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7515
7516         if (ret == IOCB_ERROR) {
7517                 lpfc_els_free_iocb(phba, elsiocb);
7518                 return 1;
7519         }
7520         return 0;
7521 }
7522
7523 /**
7524  * lpfc_send_rrq - Sends ELS RRQ if needed.
7525  * @phba: pointer to lpfc hba data structure.
7526  * @rrq: pointer to the active rrq.
7527  *
7528  * This routine will call the lpfc_issue_els_rrq if the rrq is
7529  * still active for the xri. If this function returns a failure then
7530  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
7531  *
7532  * Returns 0 Success.
7533  *         1 Failure.
7534  **/
7535 int
7536 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
7537 {
7538         struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
7539                                                        rrq->nlp_DID);
7540         if (!ndlp)
7541                 return 1;
7542
7543         if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
7544                 return lpfc_issue_els_rrq(rrq->vport, ndlp,
7545                                          rrq->nlp_DID, rrq);
7546         else
7547                 return 1;
7548 }
7549
7550 /**
7551  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
7552  * @vport: pointer to a host virtual N_Port data structure.
7553  * @cmdsize: size of the ELS command.
7554  * @oldiocb: pointer to the original lpfc command iocb data structure.
7555  * @ndlp: pointer to a node-list data structure.
7556  *
7557  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
7558  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
7559  *
7560  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7561  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7562  * will be stored into the context1 field of the IOCB for the completion
7563  * callback function to the RPL Accept Response ELS command.
7564  *
7565  * Return code
7566  *   0 - Successfully issued ACC RPL ELS command
7567  *   1 - Failed to issue ACC RPL ELS command
7568  **/
7569 static int
7570 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
7571                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7572 {
7573         struct lpfc_hba *phba = vport->phba;
7574         IOCB_t *icmd, *oldcmd;
7575         RPL_RSP rpl_rsp;
7576         struct lpfc_iocbq *elsiocb;
7577         uint8_t *pcmd;
7578
7579         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
7580                                      ndlp->nlp_DID, ELS_CMD_ACC);
7581
7582         if (!elsiocb)
7583                 return 1;
7584
7585         icmd = &elsiocb->iocb;
7586         oldcmd = &oldiocb->iocb;
7587         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
7588         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7589
7590         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7591         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7592         pcmd += sizeof(uint16_t);
7593         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
7594         pcmd += sizeof(uint16_t);
7595
7596         /* Setup the RPL ACC payload */
7597         rpl_rsp.listLen = be32_to_cpu(1);
7598         rpl_rsp.index = 0;
7599         rpl_rsp.port_num_blk.portNum = 0;
7600         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
7601         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7602             sizeof(struct lpfc_name));
7603         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7604         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
7605         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7606                          "0120 Xmit ELS RPL ACC response tag x%x "
7607                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
7608                          "rpi x%x\n",
7609                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7610                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7611                          ndlp->nlp_rpi);
7612         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7613         phba->fc_stat.elsXmitACC++;
7614         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7615             IOCB_ERROR) {
7616                 lpfc_els_free_iocb(phba, elsiocb);
7617                 return 1;
7618         }
7619         return 0;
7620 }
7621
7622 /**
7623  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
7624  * @vport: pointer to a host virtual N_Port data structure.
7625  * @cmdiocb: pointer to lpfc command iocb data structure.
7626  * @ndlp: pointer to a node-list data structure.
7627  *
7628  * This routine processes Read Port List (RPL) IOCB received as an ELS
7629  * unsolicited event. It first checks the remote port state. If the remote
7630  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
7631  * invokes the lpfc_els_rsp_reject() routine to send reject response.
7632  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
7633  * to accept the RPL.
7634  *
7635  * Return code
7636  *   0 - Successfully processed rpl iocb (currently always return 0)
7637  **/
7638 static int
7639 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7640                  struct lpfc_nodelist *ndlp)
7641 {
7642         struct lpfc_dmabuf *pcmd;
7643         uint32_t *lp;
7644         uint32_t maxsize;
7645         uint16_t cmdsize;
7646         RPL *rpl;
7647         struct ls_rjt stat;
7648
7649         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7650             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
7651                 /* issue rejection response */
7652                 stat.un.b.lsRjtRsvd0 = 0;
7653                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7654                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7655                 stat.un.b.vendorUnique = 0;
7656                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
7657                         NULL);
7658                 /* rejected the unsolicited RPL request and done with it */
7659                 return 0;
7660         }
7661
7662         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7663         lp = (uint32_t *) pcmd->virt;
7664         rpl = (RPL *) (lp + 1);
7665         maxsize = be32_to_cpu(rpl->maxsize);
7666
7667         /* We support only one port */
7668         if ((rpl->index == 0) &&
7669             ((maxsize == 0) ||
7670              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
7671                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
7672         } else {
7673                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
7674         }
7675         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
7676
7677         return 0;
7678 }
7679
7680 /**
7681  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
7682  * @vport: pointer to a virtual N_Port data structure.
7683  * @cmdiocb: pointer to lpfc command iocb data structure.
7684  * @ndlp: pointer to a node-list data structure.
7685  *
7686  * This routine processes Fibre Channel Address Resolution Protocol
7687  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
7688  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
7689  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
7690  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
7691  * remote PortName is compared against the FC PortName stored in the @vport
7692  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
7693  * compared against the FC NodeName stored in the @vport data structure.
7694  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
7695  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
7696  * invoked to send out FARP Response to the remote node. Before sending the
7697  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
7698  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
7699  * routine is invoked to log into the remote port first.
7700  *
7701  * Return code
7702  *   0 - Either the FARP Match Mode not supported or successfully processed
7703  **/
7704 static int
7705 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7706                   struct lpfc_nodelist *ndlp)
7707 {
7708         struct lpfc_dmabuf *pcmd;
7709         uint32_t *lp;
7710         IOCB_t *icmd;
7711         FARP *fp;
7712         uint32_t cnt, did;
7713
7714         icmd = &cmdiocb->iocb;
7715         did = icmd->un.elsreq64.remoteID;
7716         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7717         lp = (uint32_t *) pcmd->virt;
7718
7719         lp++;
7720         fp = (FARP *) lp;
7721         /* FARP-REQ received from DID <did> */
7722         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7723                          "0601 FARP-REQ received from DID x%x\n", did);
7724         /* We will only support match on WWPN or WWNN */
7725         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
7726                 return 0;
7727         }
7728
7729         cnt = 0;
7730         /* If this FARP command is searching for my portname */
7731         if (fp->Mflags & FARP_MATCH_PORT) {
7732                 if (memcmp(&fp->RportName, &vport->fc_portname,
7733                            sizeof(struct lpfc_name)) == 0)
7734                         cnt = 1;
7735         }
7736
7737         /* If this FARP command is searching for my nodename */
7738         if (fp->Mflags & FARP_MATCH_NODE) {
7739                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
7740                            sizeof(struct lpfc_name)) == 0)
7741                         cnt = 1;
7742         }
7743
7744         if (cnt) {
7745                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
7746                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
7747                         /* Log back into the node before sending the FARP. */
7748                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
7749                                 ndlp->nlp_prev_state = ndlp->nlp_state;
7750                                 lpfc_nlp_set_state(vport, ndlp,
7751                                                    NLP_STE_PLOGI_ISSUE);
7752                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
7753                         }
7754
7755                         /* Send a FARP response to that node */
7756                         if (fp->Rflags & FARP_REQUEST_FARPR)
7757                                 lpfc_issue_els_farpr(vport, did, 0);
7758                 }
7759         }
7760         return 0;
7761 }
7762
7763 /**
7764  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
7765  * @vport: pointer to a host virtual N_Port data structure.
7766  * @cmdiocb: pointer to lpfc command iocb data structure.
7767  * @ndlp: pointer to a node-list data structure.
7768  *
7769  * This routine processes Fibre Channel Address Resolution Protocol
7770  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
7771  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
7772  * the FARP response request.
7773  *
7774  * Return code
7775  *   0 - Successfully processed FARPR IOCB (currently always return 0)
7776  **/
7777 static int
7778 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7779                    struct lpfc_nodelist  *ndlp)
7780 {
7781         struct lpfc_dmabuf *pcmd;
7782         uint32_t *lp;
7783         IOCB_t *icmd;
7784         uint32_t did;
7785
7786         icmd = &cmdiocb->iocb;
7787         did = icmd->un.elsreq64.remoteID;
7788         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7789         lp = (uint32_t *) pcmd->virt;
7790
7791         lp++;
7792         /* FARP-RSP received from DID <did> */
7793         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7794                          "0600 FARP-RSP received from DID x%x\n", did);
7795         /* ACCEPT the Farp resp request */
7796         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7797
7798         return 0;
7799 }
7800
7801 /**
7802  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
7803  * @vport: pointer to a host virtual N_Port data structure.
7804  * @cmdiocb: pointer to lpfc command iocb data structure.
7805  * @fan_ndlp: pointer to a node-list data structure.
7806  *
7807  * This routine processes a Fabric Address Notification (FAN) IOCB
7808  * command received as an ELS unsolicited event. The FAN ELS command will
7809  * only be processed on a physical port (i.e., the @vport represents the
7810  * physical port). The fabric NodeName and PortName from the FAN IOCB are
7811  * compared against those in the phba data structure. If any of those is
7812  * different, the lpfc_initial_flogi() routine is invoked to initialize
7813  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
7814  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
7815  * is invoked to register login to the fabric.
7816  *
7817  * Return code
7818  *   0 - Successfully processed fan iocb (currently always return 0).
7819  **/
7820 static int
7821 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7822                  struct lpfc_nodelist *fan_ndlp)
7823 {
7824         struct lpfc_hba *phba = vport->phba;
7825         uint32_t *lp;
7826         FAN *fp;
7827
7828         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
7829         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
7830         fp = (FAN *) ++lp;
7831         /* FAN received; Fan does not have a reply sequence */
7832         if ((vport == phba->pport) &&
7833             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
7834                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
7835                             sizeof(struct lpfc_name))) ||
7836                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
7837                             sizeof(struct lpfc_name)))) {
7838                         /* This port has switched fabrics. FLOGI is required */
7839                         lpfc_issue_init_vfi(vport);
7840                 } else {
7841                         /* FAN verified - skip FLOGI */
7842                         vport->fc_myDID = vport->fc_prevDID;
7843                         if (phba->sli_rev < LPFC_SLI_REV4)
7844                                 lpfc_issue_fabric_reglogin(vport);
7845                         else {
7846                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7847                                         "3138 Need register VFI: (x%x/%x)\n",
7848                                         vport->fc_prevDID, vport->fc_myDID);
7849                                 lpfc_issue_reg_vfi(vport);
7850                         }
7851                 }
7852         }
7853         return 0;
7854 }
7855
7856 /**
7857  * lpfc_els_timeout - Handler funciton to the els timer
7858  * @ptr: holder for the timer function associated data.
7859  *
7860  * This routine is invoked by the ELS timer after timeout. It posts the ELS
7861  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
7862  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
7863  * up the worker thread. It is for the worker thread to invoke the routine
7864  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
7865  **/
7866 void
7867 lpfc_els_timeout(struct timer_list *t)
7868 {
7869         struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
7870         struct lpfc_hba   *phba = vport->phba;
7871         uint32_t tmo_posted;
7872         unsigned long iflag;
7873
7874         spin_lock_irqsave(&vport->work_port_lock, iflag);
7875         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
7876         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7877                 vport->work_port_events |= WORKER_ELS_TMO;
7878         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
7879
7880         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7881                 lpfc_worker_wake_up(phba);
7882         return;
7883 }
7884
7885
7886 /**
7887  * lpfc_els_timeout_handler - Process an els timeout event
7888  * @vport: pointer to a virtual N_Port data structure.
7889  *
7890  * This routine is the actual handler function that processes an ELS timeout
7891  * event. It walks the ELS ring to get and abort all the IOCBs (except the
7892  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
7893  * invoking the lpfc_sli_issue_abort_iotag() routine.
7894  **/
7895 void
7896 lpfc_els_timeout_handler(struct lpfc_vport *vport)
7897 {
7898         struct lpfc_hba  *phba = vport->phba;
7899         struct lpfc_sli_ring *pring;
7900         struct lpfc_iocbq *tmp_iocb, *piocb;
7901         IOCB_t *cmd = NULL;
7902         struct lpfc_dmabuf *pcmd;
7903         uint32_t els_command = 0;
7904         uint32_t timeout;
7905         uint32_t remote_ID = 0xffffffff;
7906         LIST_HEAD(abort_list);
7907
7908
7909         timeout = (uint32_t)(phba->fc_ratov << 1);
7910
7911         pring = lpfc_phba_elsring(phba);
7912         if (unlikely(!pring))
7913                 return;
7914
7915         if ((phba->pport->load_flag & FC_UNLOADING))
7916                 return;
7917         spin_lock_irq(&phba->hbalock);
7918         if (phba->sli_rev == LPFC_SLI_REV4)
7919                 spin_lock(&pring->ring_lock);
7920
7921         if ((phba->pport->load_flag & FC_UNLOADING)) {
7922                 if (phba->sli_rev == LPFC_SLI_REV4)
7923                         spin_unlock(&pring->ring_lock);
7924                 spin_unlock_irq(&phba->hbalock);
7925                 return;
7926         }
7927
7928         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7929                 cmd = &piocb->iocb;
7930
7931                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
7932                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7933                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
7934                         continue;
7935
7936                 if (piocb->vport != vport)
7937                         continue;
7938
7939                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
7940                 if (pcmd)
7941                         els_command = *(uint32_t *) (pcmd->virt);
7942
7943                 if (els_command == ELS_CMD_FARP ||
7944                     els_command == ELS_CMD_FARPR ||
7945                     els_command == ELS_CMD_FDISC)
7946                         continue;
7947
7948                 if (piocb->drvrTimeout > 0) {
7949                         if (piocb->drvrTimeout >= timeout)
7950                                 piocb->drvrTimeout -= timeout;
7951                         else
7952                                 piocb->drvrTimeout = 0;
7953                         continue;
7954                 }
7955
7956                 remote_ID = 0xffffffff;
7957                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
7958                         remote_ID = cmd->un.elsreq64.remoteID;
7959                 else {
7960                         struct lpfc_nodelist *ndlp;
7961                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
7962                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
7963                                 remote_ID = ndlp->nlp_DID;
7964                 }
7965                 list_add_tail(&piocb->dlist, &abort_list);
7966         }
7967         if (phba->sli_rev == LPFC_SLI_REV4)
7968                 spin_unlock(&pring->ring_lock);
7969         spin_unlock_irq(&phba->hbalock);
7970
7971         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
7972                 cmd = &piocb->iocb;
7973                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7974                          "0127 ELS timeout Data: x%x x%x x%x "
7975                          "x%x\n", els_command,
7976                          remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
7977                 spin_lock_irq(&phba->hbalock);
7978                 list_del_init(&piocb->dlist);
7979                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
7980                 spin_unlock_irq(&phba->hbalock);
7981         }
7982
7983         if (!list_empty(&pring->txcmplq))
7984                 if (!(phba->pport->load_flag & FC_UNLOADING))
7985                         mod_timer(&vport->els_tmofunc,
7986                                   jiffies + msecs_to_jiffies(1000 * timeout));
7987 }
7988
7989 /**
7990  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
7991  * @vport: pointer to a host virtual N_Port data structure.
7992  *
7993  * This routine is used to clean up all the outstanding ELS commands on a
7994  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
7995  * routine. After that, it walks the ELS transmit queue to remove all the
7996  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
7997  * the IOCBs with a non-NULL completion callback function, the callback
7998  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7999  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
8000  * callback function, the IOCB will simply be released. Finally, it walks
8001  * the ELS transmit completion queue to issue an abort IOCB to any transmit
8002  * completion queue IOCB that is associated with the @vport and is not
8003  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
8004  * part of the discovery state machine) out to HBA by invoking the
8005  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
8006  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
8007  * the IOCBs are aborted when this function returns.
8008  **/
8009 void
8010 lpfc_els_flush_cmd(struct lpfc_vport *vport)
8011 {
8012         LIST_HEAD(abort_list);
8013         struct lpfc_hba  *phba = vport->phba;
8014         struct lpfc_sli_ring *pring;
8015         struct lpfc_iocbq *tmp_iocb, *piocb;
8016         IOCB_t *cmd = NULL;
8017         unsigned long iflags = 0;
8018
8019         lpfc_fabric_abort_vport(vport);
8020
8021         /*
8022          * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
8023          * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
8024          * ultimately grabs the ring_lock, the driver must splice the list into
8025          * a working list and release the locks before calling the abort.
8026          */
8027         spin_lock_irqsave(&phba->hbalock, iflags);
8028         pring = lpfc_phba_elsring(phba);
8029
8030         /* Bail out if we've no ELS wq, like in PCI error recovery case. */
8031         if (unlikely(!pring)) {
8032                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8033                 return;
8034         }
8035
8036         if (phba->sli_rev == LPFC_SLI_REV4)
8037                 spin_lock(&pring->ring_lock);
8038
8039         /* First we need to issue aborts to outstanding cmds on txcmpl */
8040         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
8041                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
8042                         continue;
8043
8044                 if (piocb->vport != vport)
8045                         continue;
8046
8047                 if (piocb->iocb_flag & LPFC_DRIVER_ABORTED)
8048                         continue;
8049
8050                 /* On the ELS ring we can have ELS_REQUESTs or
8051                  * GEN_REQUESTs waiting for a response.
8052                  */
8053                 cmd = &piocb->iocb;
8054                 if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
8055                         list_add_tail(&piocb->dlist, &abort_list);
8056
8057                         /* If the link is down when flushing ELS commands
8058                          * the firmware will not complete them till after
8059                          * the link comes back up. This may confuse
8060                          * discovery for the new link up, so we need to
8061                          * change the compl routine to just clean up the iocb
8062                          * and avoid any retry logic.
8063                          */
8064                         if (phba->link_state == LPFC_LINK_DOWN)
8065                                 piocb->iocb_cmpl = lpfc_cmpl_els_link_down;
8066                 }
8067                 if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR)
8068                         list_add_tail(&piocb->dlist, &abort_list);
8069         }
8070
8071         if (phba->sli_rev == LPFC_SLI_REV4)
8072                 spin_unlock(&pring->ring_lock);
8073         spin_unlock_irqrestore(&phba->hbalock, iflags);
8074
8075         /* Abort each txcmpl iocb on aborted list and remove the dlist links. */
8076         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
8077                 spin_lock_irqsave(&phba->hbalock, iflags);
8078                 list_del_init(&piocb->dlist);
8079                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
8080                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8081         }
8082         if (!list_empty(&abort_list))
8083                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8084                                  "3387 abort list for txq not empty\n");
8085         INIT_LIST_HEAD(&abort_list);
8086
8087         spin_lock_irqsave(&phba->hbalock, iflags);
8088         if (phba->sli_rev == LPFC_SLI_REV4)
8089                 spin_lock(&pring->ring_lock);
8090
8091         /* No need to abort the txq list,
8092          * just queue them up for lpfc_sli_cancel_iocbs
8093          */
8094         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
8095                 cmd = &piocb->iocb;
8096
8097                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
8098                         continue;
8099                 }
8100
8101                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
8102                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
8103                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
8104                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8105                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
8106                         continue;
8107
8108                 if (piocb->vport != vport)
8109                         continue;
8110
8111                 list_del_init(&piocb->list);
8112                 list_add_tail(&piocb->list, &abort_list);
8113         }
8114
8115         /* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
8116         if (vport == phba->pport) {
8117                 list_for_each_entry_safe(piocb, tmp_iocb,
8118                                          &phba->fabric_iocb_list, list) {
8119                         cmd = &piocb->iocb;
8120                         list_del_init(&piocb->list);
8121                         list_add_tail(&piocb->list, &abort_list);
8122                 }
8123         }
8124
8125         if (phba->sli_rev == LPFC_SLI_REV4)
8126                 spin_unlock(&pring->ring_lock);
8127         spin_unlock_irqrestore(&phba->hbalock, iflags);
8128
8129         /* Cancel all the IOCBs from the completions list */
8130         lpfc_sli_cancel_iocbs(phba, &abort_list,
8131                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
8132
8133         return;
8134 }
8135
8136 /**
8137  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
8138  * @phba: pointer to lpfc hba data structure.
8139  *
8140  * This routine is used to clean up all the outstanding ELS commands on a
8141  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
8142  * routine. After that, it walks the ELS transmit queue to remove all the
8143  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
8144  * the IOCBs with the completion callback function associated, the callback
8145  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
8146  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
8147  * callback function associated, the IOCB will simply be released. Finally,
8148  * it walks the ELS transmit completion queue to issue an abort IOCB to any
8149  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
8150  * management plane IOCBs that are not part of the discovery state machine)
8151  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
8152  **/
8153 void
8154 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
8155 {
8156         struct lpfc_vport *vport;
8157
8158         spin_lock_irq(&phba->port_list_lock);
8159         list_for_each_entry(vport, &phba->port_list, listentry)
8160                 lpfc_els_flush_cmd(vport);
8161         spin_unlock_irq(&phba->port_list_lock);
8162
8163         return;
8164 }
8165
8166 /**
8167  * lpfc_send_els_failure_event - Posts an ELS command failure event
8168  * @phba: Pointer to hba context object.
8169  * @cmdiocbp: Pointer to command iocb which reported error.
8170  * @rspiocbp: Pointer to response iocb which reported error.
8171  *
8172  * This function sends an event when there is an ELS command
8173  * failure.
8174  **/
8175 void
8176 lpfc_send_els_failure_event(struct lpfc_hba *phba,
8177                         struct lpfc_iocbq *cmdiocbp,
8178                         struct lpfc_iocbq *rspiocbp)
8179 {
8180         struct lpfc_vport *vport = cmdiocbp->vport;
8181         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8182         struct lpfc_lsrjt_event lsrjt_event;
8183         struct lpfc_fabric_event_header fabric_event;
8184         struct ls_rjt stat;
8185         struct lpfc_nodelist *ndlp;
8186         uint32_t *pcmd;
8187
8188         ndlp = cmdiocbp->context1;
8189         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8190                 return;
8191
8192         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
8193                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
8194                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
8195                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
8196                         sizeof(struct lpfc_name));
8197                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
8198                         sizeof(struct lpfc_name));
8199                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8200                         cmdiocbp->context2)->virt);
8201                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
8202                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
8203                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
8204                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
8205                 fc_host_post_vendor_event(shost,
8206                         fc_get_event_number(),
8207                         sizeof(lsrjt_event),
8208                         (char *)&lsrjt_event,
8209                         LPFC_NL_VENDOR_ID);
8210                 return;
8211         }
8212         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
8213                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
8214                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
8215                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
8216                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
8217                 else
8218                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
8219                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
8220                         sizeof(struct lpfc_name));
8221                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
8222                         sizeof(struct lpfc_name));
8223                 fc_host_post_vendor_event(shost,
8224                         fc_get_event_number(),
8225                         sizeof(fabric_event),
8226                         (char *)&fabric_event,
8227                         LPFC_NL_VENDOR_ID);
8228                 return;
8229         }
8230
8231 }
8232
8233 /**
8234  * lpfc_send_els_event - Posts unsolicited els event
8235  * @vport: Pointer to vport object.
8236  * @ndlp: Pointer FC node object.
8237  * @cmd: ELS command code.
8238  *
8239  * This function posts an event when there is an incoming
8240  * unsolicited ELS command.
8241  **/
8242 static void
8243 lpfc_send_els_event(struct lpfc_vport *vport,
8244                     struct lpfc_nodelist *ndlp,
8245                     uint32_t *payload)
8246 {
8247         struct lpfc_els_event_header *els_data = NULL;
8248         struct lpfc_logo_event *logo_data = NULL;
8249         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8250
8251         if (*payload == ELS_CMD_LOGO) {
8252                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
8253                 if (!logo_data) {
8254                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8255                                 "0148 Failed to allocate memory "
8256                                 "for LOGO event\n");
8257                         return;
8258                 }
8259                 els_data = &logo_data->header;
8260         } else {
8261                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
8262                         GFP_KERNEL);
8263                 if (!els_data) {
8264                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8265                                 "0149 Failed to allocate memory "
8266                                 "for ELS event\n");
8267                         return;
8268                 }
8269         }
8270         els_data->event_type = FC_REG_ELS_EVENT;
8271         switch (*payload) {
8272         case ELS_CMD_PLOGI:
8273                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
8274                 break;
8275         case ELS_CMD_PRLO:
8276                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
8277                 break;
8278         case ELS_CMD_ADISC:
8279                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
8280                 break;
8281         case ELS_CMD_LOGO:
8282                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
8283                 /* Copy the WWPN in the LOGO payload */
8284                 memcpy(logo_data->logo_wwpn, &payload[2],
8285                         sizeof(struct lpfc_name));
8286                 break;
8287         default:
8288                 kfree(els_data);
8289                 return;
8290         }
8291         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
8292         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
8293         if (*payload == ELS_CMD_LOGO) {
8294                 fc_host_post_vendor_event(shost,
8295                         fc_get_event_number(),
8296                         sizeof(struct lpfc_logo_event),
8297                         (char *)logo_data,
8298                         LPFC_NL_VENDOR_ID);
8299                 kfree(logo_data);
8300         } else {
8301                 fc_host_post_vendor_event(shost,
8302                         fc_get_event_number(),
8303                         sizeof(struct lpfc_els_event_header),
8304                         (char *)els_data,
8305                         LPFC_NL_VENDOR_ID);
8306                 kfree(els_data);
8307         }
8308
8309         return;
8310 }
8311
8312
8313 /**
8314  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
8315  * @phba: pointer to lpfc hba data structure.
8316  * @pring: pointer to a SLI ring.
8317  * @vport: pointer to a host virtual N_Port data structure.
8318  * @elsiocb: pointer to lpfc els command iocb data structure.
8319  *
8320  * This routine is used for processing the IOCB associated with a unsolicited
8321  * event. It first determines whether there is an existing ndlp that matches
8322  * the DID from the unsolicited IOCB. If not, it will create a new one with
8323  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
8324  * IOCB is then used to invoke the proper routine and to set up proper state
8325  * of the discovery state machine.
8326  **/
8327 static void
8328 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8329                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
8330 {
8331         struct Scsi_Host  *shost;
8332         struct lpfc_nodelist *ndlp;
8333         struct ls_rjt stat;
8334         uint32_t *payload;
8335         uint32_t cmd, did, newnode;
8336         uint8_t rjt_exp, rjt_err = 0, init_link = 0;
8337         IOCB_t *icmd = &elsiocb->iocb;
8338         LPFC_MBOXQ_t *mbox;
8339
8340         if (!vport || !(elsiocb->context2))
8341                 goto dropit;
8342
8343         newnode = 0;
8344         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
8345         cmd = *payload;
8346         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
8347                 lpfc_post_buffer(phba, pring, 1);
8348
8349         did = icmd->un.rcvels.remoteID;
8350         if (icmd->ulpStatus) {
8351                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8352                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
8353                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
8354                 goto dropit;
8355         }
8356
8357         /* Check to see if link went down during discovery */
8358         if (lpfc_els_chk_latt(vport))
8359                 goto dropit;
8360
8361         /* Ignore traffic received during vport shutdown. */
8362         if (vport->load_flag & FC_UNLOADING)
8363                 goto dropit;
8364
8365         /* If NPort discovery is delayed drop incoming ELS */
8366         if ((vport->fc_flag & FC_DISC_DELAYED) &&
8367                         (cmd != ELS_CMD_PLOGI))
8368                 goto dropit;
8369
8370         ndlp = lpfc_findnode_did(vport, did);
8371         if (!ndlp) {
8372                 /* Cannot find existing Fabric ndlp, so allocate a new one */
8373                 ndlp = lpfc_nlp_init(vport, did);
8374                 if (!ndlp)
8375                         goto dropit;
8376                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8377                 newnode = 1;
8378                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8379                         ndlp->nlp_type |= NLP_FABRIC;
8380         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
8381                 ndlp = lpfc_enable_node(vport, ndlp,
8382                                         NLP_STE_UNUSED_NODE);
8383                 if (!ndlp)
8384                         goto dropit;
8385                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8386                 newnode = 1;
8387                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8388                         ndlp->nlp_type |= NLP_FABRIC;
8389         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
8390                 /* This is similar to the new node path */
8391                 ndlp = lpfc_nlp_get(ndlp);
8392                 if (!ndlp)
8393                         goto dropit;
8394                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8395                 newnode = 1;
8396         }
8397
8398         phba->fc_stat.elsRcvFrame++;
8399
8400         /*
8401          * Do not process any unsolicited ELS commands
8402          * if the ndlp is in DEV_LOSS
8403          */
8404         shost = lpfc_shost_from_vport(vport);
8405         spin_lock_irq(shost->host_lock);
8406         if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
8407                 spin_unlock_irq(shost->host_lock);
8408                 goto dropit;
8409         }
8410         spin_unlock_irq(shost->host_lock);
8411
8412         elsiocb->context1 = lpfc_nlp_get(ndlp);
8413         elsiocb->vport = vport;
8414
8415         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
8416                 cmd &= ELS_CMD_MASK;
8417         }
8418         /* ELS command <elsCmd> received from NPORT <did> */
8419         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8420                          "0112 ELS command x%x received from NPORT x%x "
8421                          "Data: x%x x%x x%x x%x\n",
8422                         cmd, did, vport->port_state, vport->fc_flag,
8423                         vport->fc_myDID, vport->fc_prevDID);
8424
8425         /* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
8426         if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
8427             (cmd != ELS_CMD_FLOGI) &&
8428             !((cmd == ELS_CMD_PLOGI) && (vport->fc_flag & FC_PT2PT))) {
8429                 rjt_err = LSRJT_LOGICAL_BSY;
8430                 rjt_exp = LSEXP_NOTHING_MORE;
8431                 goto lsrjt;
8432         }
8433
8434         switch (cmd) {
8435         case ELS_CMD_PLOGI:
8436                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8437                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
8438                         did, vport->port_state, ndlp->nlp_flag);
8439
8440                 phba->fc_stat.elsRcvPLOGI++;
8441                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
8442                 if (phba->sli_rev == LPFC_SLI_REV4 &&
8443                     (phba->pport->fc_flag & FC_PT2PT)) {
8444                         vport->fc_prevDID = vport->fc_myDID;
8445                         /* Our DID needs to be updated before registering
8446                          * the vfi. This is done in lpfc_rcv_plogi but
8447                          * that is called after the reg_vfi.
8448                          */
8449                         vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
8450                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8451                                          "3312 Remote port assigned DID x%x "
8452                                          "%x\n", vport->fc_myDID,
8453                                          vport->fc_prevDID);
8454                 }
8455
8456                 lpfc_send_els_event(vport, ndlp, payload);
8457
8458                 /* If Nport discovery is delayed, reject PLOGIs */
8459                 if (vport->fc_flag & FC_DISC_DELAYED) {
8460                         rjt_err = LSRJT_UNABLE_TPC;
8461                         rjt_exp = LSEXP_NOTHING_MORE;
8462                         break;
8463                 }
8464
8465                 if (vport->port_state < LPFC_DISC_AUTH) {
8466                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
8467                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
8468                                 rjt_err = LSRJT_UNABLE_TPC;
8469                                 rjt_exp = LSEXP_NOTHING_MORE;
8470                                 break;
8471                         }
8472                 }
8473
8474                 spin_lock_irq(shost->host_lock);
8475                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
8476                 spin_unlock_irq(shost->host_lock);
8477
8478                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8479                                         NLP_EVT_RCV_PLOGI);
8480
8481                 break;
8482         case ELS_CMD_FLOGI:
8483                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8484                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
8485                         did, vport->port_state, ndlp->nlp_flag);
8486
8487                 phba->fc_stat.elsRcvFLOGI++;
8488
8489                 /* If the driver believes fabric discovery is done and is ready,
8490                  * bounce the link.  There is some descrepancy.
8491                  */
8492                 if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
8493                     vport->fc_flag & FC_PT2PT &&
8494                     vport->rcv_flogi_cnt >= 1) {
8495                         rjt_err = LSRJT_LOGICAL_BSY;
8496                         rjt_exp = LSEXP_NOTHING_MORE;
8497                         init_link++;
8498                         goto lsrjt;
8499                 }
8500
8501                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
8502                 if (newnode)
8503                         lpfc_nlp_put(ndlp);
8504                 break;
8505         case ELS_CMD_LOGO:
8506                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8507                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
8508                         did, vport->port_state, ndlp->nlp_flag);
8509
8510                 phba->fc_stat.elsRcvLOGO++;
8511                 lpfc_send_els_event(vport, ndlp, payload);
8512                 if (vport->port_state < LPFC_DISC_AUTH) {
8513                         rjt_err = LSRJT_UNABLE_TPC;
8514                         rjt_exp = LSEXP_NOTHING_MORE;
8515                         break;
8516                 }
8517                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
8518                 break;
8519         case ELS_CMD_PRLO:
8520                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8521                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
8522                         did, vport->port_state, ndlp->nlp_flag);
8523
8524                 phba->fc_stat.elsRcvPRLO++;
8525                 lpfc_send_els_event(vport, ndlp, payload);
8526                 if (vport->port_state < LPFC_DISC_AUTH) {
8527                         rjt_err = LSRJT_UNABLE_TPC;
8528                         rjt_exp = LSEXP_NOTHING_MORE;
8529                         break;
8530                 }
8531                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
8532                 break;
8533         case ELS_CMD_LCB:
8534                 phba->fc_stat.elsRcvLCB++;
8535                 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
8536                 break;
8537         case ELS_CMD_RDP:
8538                 phba->fc_stat.elsRcvRDP++;
8539                 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
8540                 break;
8541         case ELS_CMD_RSCN:
8542                 phba->fc_stat.elsRcvRSCN++;
8543                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
8544                 if (newnode)
8545                         lpfc_nlp_put(ndlp);
8546                 break;
8547         case ELS_CMD_ADISC:
8548                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8549                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
8550                         did, vport->port_state, ndlp->nlp_flag);
8551
8552                 lpfc_send_els_event(vport, ndlp, payload);
8553                 phba->fc_stat.elsRcvADISC++;
8554                 if (vport->port_state < LPFC_DISC_AUTH) {
8555                         rjt_err = LSRJT_UNABLE_TPC;
8556                         rjt_exp = LSEXP_NOTHING_MORE;
8557                         break;
8558                 }
8559                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8560                                         NLP_EVT_RCV_ADISC);
8561                 break;
8562         case ELS_CMD_PDISC:
8563                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8564                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
8565                         did, vport->port_state, ndlp->nlp_flag);
8566
8567                 phba->fc_stat.elsRcvPDISC++;
8568                 if (vport->port_state < LPFC_DISC_AUTH) {
8569                         rjt_err = LSRJT_UNABLE_TPC;
8570                         rjt_exp = LSEXP_NOTHING_MORE;
8571                         break;
8572                 }
8573                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8574                                         NLP_EVT_RCV_PDISC);
8575                 break;
8576         case ELS_CMD_FARPR:
8577                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8578                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
8579                         did, vport->port_state, ndlp->nlp_flag);
8580
8581                 phba->fc_stat.elsRcvFARPR++;
8582                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
8583                 break;
8584         case ELS_CMD_FARP:
8585                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8586                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
8587                         did, vport->port_state, ndlp->nlp_flag);
8588
8589                 phba->fc_stat.elsRcvFARP++;
8590                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
8591                 break;
8592         case ELS_CMD_FAN:
8593                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8594                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
8595                         did, vport->port_state, ndlp->nlp_flag);
8596
8597                 phba->fc_stat.elsRcvFAN++;
8598                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
8599                 break;
8600         case ELS_CMD_PRLI:
8601         case ELS_CMD_NVMEPRLI:
8602                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8603                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
8604                         did, vport->port_state, ndlp->nlp_flag);
8605
8606                 phba->fc_stat.elsRcvPRLI++;
8607                 if ((vport->port_state < LPFC_DISC_AUTH) &&
8608                     (vport->fc_flag & FC_FABRIC)) {
8609                         rjt_err = LSRJT_UNABLE_TPC;
8610                         rjt_exp = LSEXP_NOTHING_MORE;
8611                         break;
8612                 }
8613                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
8614                 break;
8615         case ELS_CMD_LIRR:
8616                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8617                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
8618                         did, vport->port_state, ndlp->nlp_flag);
8619
8620                 phba->fc_stat.elsRcvLIRR++;
8621                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
8622                 if (newnode)
8623                         lpfc_nlp_put(ndlp);
8624                 break;
8625         case ELS_CMD_RLS:
8626                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8627                         "RCV RLS:         did:x%x/ste:x%x flg:x%x",
8628                         did, vport->port_state, ndlp->nlp_flag);
8629
8630                 phba->fc_stat.elsRcvRLS++;
8631                 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
8632                 if (newnode)
8633                         lpfc_nlp_put(ndlp);
8634                 break;
8635         case ELS_CMD_RPS:
8636                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8637                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
8638                         did, vport->port_state, ndlp->nlp_flag);
8639
8640                 phba->fc_stat.elsRcvRPS++;
8641                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
8642                 if (newnode)
8643                         lpfc_nlp_put(ndlp);
8644                 break;
8645         case ELS_CMD_RPL:
8646                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8647                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
8648                         did, vport->port_state, ndlp->nlp_flag);
8649
8650                 phba->fc_stat.elsRcvRPL++;
8651                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
8652                 if (newnode)
8653                         lpfc_nlp_put(ndlp);
8654                 break;
8655         case ELS_CMD_RNID:
8656                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8657                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
8658                         did, vport->port_state, ndlp->nlp_flag);
8659
8660                 phba->fc_stat.elsRcvRNID++;
8661                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
8662                 if (newnode)
8663                         lpfc_nlp_put(ndlp);
8664                 break;
8665         case ELS_CMD_RTV:
8666                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8667                         "RCV RTV:        did:x%x/ste:x%x flg:x%x",
8668                         did, vport->port_state, ndlp->nlp_flag);
8669                 phba->fc_stat.elsRcvRTV++;
8670                 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
8671                 if (newnode)
8672                         lpfc_nlp_put(ndlp);
8673                 break;
8674         case ELS_CMD_RRQ:
8675                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8676                         "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
8677                         did, vport->port_state, ndlp->nlp_flag);
8678
8679                 phba->fc_stat.elsRcvRRQ++;
8680                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
8681                 if (newnode)
8682                         lpfc_nlp_put(ndlp);
8683                 break;
8684         case ELS_CMD_ECHO:
8685                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8686                         "RCV ECHO:        did:x%x/ste:x%x flg:x%x",
8687                         did, vport->port_state, ndlp->nlp_flag);
8688
8689                 phba->fc_stat.elsRcvECHO++;
8690                 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
8691                 if (newnode)
8692                         lpfc_nlp_put(ndlp);
8693                 break;
8694         case ELS_CMD_REC:
8695                 /* receive this due to exchange closed */
8696                 rjt_err = LSRJT_UNABLE_TPC;
8697                 rjt_exp = LSEXP_INVALID_OX_RX;
8698                 break;
8699         case ELS_CMD_FPIN:
8700                 /*
8701                  * Received FPIN from fabric - pass it to the
8702                  * transport FPIN handler.
8703                  */
8704                 fc_host_fpin_rcv(shost, elsiocb->iocb.unsli3.rcvsli3.acc_len,
8705                                 (char *)payload);
8706                 break;
8707         default:
8708                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8709                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
8710                         cmd, did, vport->port_state);
8711
8712                 /* Unsupported ELS command, reject */
8713                 rjt_err = LSRJT_CMD_UNSUPPORTED;
8714                 rjt_exp = LSEXP_NOTHING_MORE;
8715
8716                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
8717                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8718                                  "0115 Unknown ELS command x%x "
8719                                  "received from NPORT x%x\n", cmd, did);
8720                 if (newnode)
8721                         lpfc_nlp_put(ndlp);
8722                 break;
8723         }
8724
8725 lsrjt:
8726         /* check if need to LS_RJT received ELS cmd */
8727         if (rjt_err) {
8728                 memset(&stat, 0, sizeof(stat));
8729                 stat.un.b.lsRjtRsnCode = rjt_err;
8730                 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
8731                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
8732                         NULL);
8733         }
8734
8735         lpfc_nlp_put(elsiocb->context1);
8736         elsiocb->context1 = NULL;
8737
8738         /* Special case.  Driver received an unsolicited command that
8739          * unsupportable given the driver's current state.  Reset the
8740          * link and start over.
8741          */
8742         if (init_link) {
8743                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8744                 if (!mbox)
8745                         return;
8746                 lpfc_linkdown(phba);
8747                 lpfc_init_link(phba, mbox,
8748                                phba->cfg_topology,
8749                                phba->cfg_link_speed);
8750                 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8751                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8752                 mbox->vport = vport;
8753                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
8754                     MBX_NOT_FINISHED)
8755                         mempool_free(mbox, phba->mbox_mem_pool);
8756         }
8757
8758         return;
8759
8760 dropit:
8761         if (vport && !(vport->load_flag & FC_UNLOADING))
8762                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8763                         "0111 Dropping received ELS cmd "
8764                         "Data: x%x x%x x%x\n",
8765                         icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
8766         phba->fc_stat.elsRcvDrop++;
8767 }
8768
8769 /**
8770  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
8771  * @phba: pointer to lpfc hba data structure.
8772  * @pring: pointer to a SLI ring.
8773  * @elsiocb: pointer to lpfc els iocb data structure.
8774  *
8775  * This routine is used to process an unsolicited event received from a SLI
8776  * (Service Level Interface) ring. The actual processing of the data buffer
8777  * associated with the unsolicited event is done by invoking the routine
8778  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
8779  * SLI ring on which the unsolicited event was received.
8780  **/
8781 void
8782 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8783                      struct lpfc_iocbq *elsiocb)
8784 {
8785         struct lpfc_vport *vport = phba->pport;
8786         IOCB_t *icmd = &elsiocb->iocb;
8787         dma_addr_t paddr;
8788         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
8789         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
8790
8791         elsiocb->context1 = NULL;
8792         elsiocb->context2 = NULL;
8793         elsiocb->context3 = NULL;
8794
8795         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
8796                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
8797         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
8798                    (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
8799                    IOERR_RCV_BUFFER_WAITING) {
8800                 phba->fc_stat.NoRcvBuf++;
8801                 /* Not enough posted buffers; Try posting more buffers */
8802                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
8803                         lpfc_post_buffer(phba, pring, 0);
8804                 return;
8805         }
8806
8807         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
8808             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
8809              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
8810                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
8811                         vport = phba->pport;
8812                 else
8813                         vport = lpfc_find_vport_by_vpid(phba,
8814                                                 icmd->unsli3.rcvsli3.vpi);
8815         }
8816
8817         /* If there are no BDEs associated
8818          * with this IOCB, there is nothing to do.
8819          */
8820         if (icmd->ulpBdeCount == 0)
8821                 return;
8822
8823         /* type of ELS cmd is first 32bit word
8824          * in packet
8825          */
8826         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
8827                 elsiocb->context2 = bdeBuf1;
8828         } else {
8829                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
8830                                  icmd->un.cont64[0].addrLow);
8831                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
8832                                                              paddr);
8833         }
8834
8835         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8836         /*
8837          * The different unsolicited event handlers would tell us
8838          * if they are done with "mp" by setting context2 to NULL.
8839          */
8840         if (elsiocb->context2) {
8841                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
8842                 elsiocb->context2 = NULL;
8843         }
8844
8845         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
8846         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
8847             icmd->ulpBdeCount == 2) {
8848                 elsiocb->context2 = bdeBuf2;
8849                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8850                 /* free mp if we are done with it */
8851                 if (elsiocb->context2) {
8852                         lpfc_in_buf_free(phba, elsiocb->context2);
8853                         elsiocb->context2 = NULL;
8854                 }
8855         }
8856 }
8857
8858 static void
8859 lpfc_start_fdmi(struct lpfc_vport *vport)
8860 {
8861         struct lpfc_nodelist *ndlp;
8862
8863         /* If this is the first time, allocate an ndlp and initialize
8864          * it. Otherwise, make sure the node is enabled and then do the
8865          * login.
8866          */
8867         ndlp = lpfc_findnode_did(vport, FDMI_DID);
8868         if (!ndlp) {
8869                 ndlp = lpfc_nlp_init(vport, FDMI_DID);
8870                 if (ndlp) {
8871                         ndlp->nlp_type |= NLP_FABRIC;
8872                 } else {
8873                         return;
8874                 }
8875         }
8876         if (!NLP_CHK_NODE_ACT(ndlp))
8877                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
8878
8879         if (ndlp) {
8880                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8881                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
8882         }
8883 }
8884
8885 /**
8886  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
8887  * @phba: pointer to lpfc hba data structure.
8888  * @vport: pointer to a virtual N_Port data structure.
8889  *
8890  * This routine issues a Port Login (PLOGI) to the Name Server with
8891  * State Change Request (SCR) for a @vport. This routine will create an
8892  * ndlp for the Name Server associated to the @vport if such node does
8893  * not already exist. The PLOGI to Name Server is issued by invoking the
8894  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
8895  * (FDMI) is configured to the @vport, a FDMI node will be created and
8896  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
8897  **/
8898 void
8899 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
8900 {
8901         struct lpfc_nodelist *ndlp;
8902         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8903
8904         /*
8905          * If lpfc_delay_discovery parameter is set and the clean address
8906          * bit is cleared and fc fabric parameters chenged, delay FC NPort
8907          * discovery.
8908          */
8909         spin_lock_irq(shost->host_lock);
8910         if (vport->fc_flag & FC_DISC_DELAYED) {
8911                 spin_unlock_irq(shost->host_lock);
8912                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
8913                                 "3334 Delay fc port discovery for %d seconds\n",
8914                                 phba->fc_ratov);
8915                 mod_timer(&vport->delayed_disc_tmo,
8916                         jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
8917                 return;
8918         }
8919         spin_unlock_irq(shost->host_lock);
8920
8921         ndlp = lpfc_findnode_did(vport, NameServer_DID);
8922         if (!ndlp) {
8923                 ndlp = lpfc_nlp_init(vport, NameServer_DID);
8924                 if (!ndlp) {
8925                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8926                                 lpfc_disc_start(vport);
8927                                 return;
8928                         }
8929                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8930                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8931                                          "0251 NameServer login: no memory\n");
8932                         return;
8933                 }
8934         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
8935                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
8936                 if (!ndlp) {
8937                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8938                                 lpfc_disc_start(vport);
8939                                 return;
8940                         }
8941                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8942                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8943                                         "0348 NameServer login: node freed\n");
8944                         return;
8945                 }
8946         }
8947         ndlp->nlp_type |= NLP_FABRIC;
8948
8949         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8950
8951         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
8952                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8953                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8954                                  "0252 Cannot issue NameServer login\n");
8955                 return;
8956         }
8957
8958         if ((phba->cfg_enable_SmartSAN ||
8959              (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
8960              (vport->load_flag & FC_ALLOW_FDMI))
8961                 lpfc_start_fdmi(vport);
8962 }
8963
8964 /**
8965  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
8966  * @phba: pointer to lpfc hba data structure.
8967  * @pmb: pointer to the driver internal queue element for mailbox command.
8968  *
8969  * This routine is the completion callback function to register new vport
8970  * mailbox command. If the new vport mailbox command completes successfully,
8971  * the fabric registration login shall be performed on physical port (the
8972  * new vport created is actually a physical port, with VPI 0) or the port
8973  * login to Name Server for State Change Request (SCR) will be performed
8974  * on virtual port (real virtual port, with VPI greater than 0).
8975  **/
8976 static void
8977 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8978 {
8979         struct lpfc_vport *vport = pmb->vport;
8980         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
8981         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
8982         MAILBOX_t *mb = &pmb->u.mb;
8983         int rc;
8984
8985         spin_lock_irq(shost->host_lock);
8986         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
8987         spin_unlock_irq(shost->host_lock);
8988
8989         if (mb->mbxStatus) {
8990                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
8991                                 "0915 Register VPI failed : Status: x%x"
8992                                 " upd bit: x%x \n", mb->mbxStatus,
8993                                  mb->un.varRegVpi.upd);
8994                 if (phba->sli_rev == LPFC_SLI_REV4 &&
8995                         mb->un.varRegVpi.upd)
8996                         goto mbox_err_exit ;
8997
8998                 switch (mb->mbxStatus) {
8999                 case 0x11:      /* unsupported feature */
9000                 case 0x9603:    /* max_vpi exceeded */
9001                 case 0x9602:    /* Link event since CLEAR_LA */
9002                         /* giving up on vport registration */
9003                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9004                         spin_lock_irq(shost->host_lock);
9005                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
9006                         spin_unlock_irq(shost->host_lock);
9007                         lpfc_can_disctmo(vport);
9008                         break;
9009                 /* If reg_vpi fail with invalid VPI status, re-init VPI */
9010                 case 0x20:
9011                         spin_lock_irq(shost->host_lock);
9012                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9013                         spin_unlock_irq(shost->host_lock);
9014                         lpfc_init_vpi(phba, pmb, vport->vpi);
9015                         pmb->vport = vport;
9016                         pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
9017                         rc = lpfc_sli_issue_mbox(phba, pmb,
9018                                 MBX_NOWAIT);
9019                         if (rc == MBX_NOT_FINISHED) {
9020                                 lpfc_printf_vlog(vport,
9021                                         KERN_ERR, LOG_MBOX,
9022                                         "2732 Failed to issue INIT_VPI"
9023                                         " mailbox command\n");
9024                         } else {
9025                                 lpfc_nlp_put(ndlp);
9026                                 return;
9027                         }
9028                         /* fall through */
9029                 default:
9030                         /* Try to recover from this error */
9031                         if (phba->sli_rev == LPFC_SLI_REV4)
9032                                 lpfc_sli4_unreg_all_rpis(vport);
9033                         lpfc_mbx_unreg_vpi(vport);
9034                         spin_lock_irq(shost->host_lock);
9035                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9036                         spin_unlock_irq(shost->host_lock);
9037                         if (mb->mbxStatus == MBX_NOT_FINISHED)
9038                                 break;
9039                         if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
9040                             !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
9041                                 if (phba->sli_rev == LPFC_SLI_REV4)
9042                                         lpfc_issue_init_vfi(vport);
9043                                 else
9044                                         lpfc_initial_flogi(vport);
9045                         } else {
9046                                 lpfc_initial_fdisc(vport);
9047                         }
9048                         break;
9049                 }
9050         } else {
9051                 spin_lock_irq(shost->host_lock);
9052                 vport->vpi_state |= LPFC_VPI_REGISTERED;
9053                 spin_unlock_irq(shost->host_lock);
9054                 if (vport == phba->pport) {
9055                         if (phba->sli_rev < LPFC_SLI_REV4)
9056                                 lpfc_issue_fabric_reglogin(vport);
9057                         else {
9058                                 /*
9059                                  * If the physical port is instantiated using
9060                                  * FDISC, do not start vport discovery.
9061                                  */
9062                                 if (vport->port_state != LPFC_FDISC)
9063                                         lpfc_start_fdiscs(phba);
9064                                 lpfc_do_scr_ns_plogi(phba, vport);
9065                         }
9066                 } else
9067                         lpfc_do_scr_ns_plogi(phba, vport);
9068         }
9069 mbox_err_exit:
9070         /* Now, we decrement the ndlp reference count held for this
9071          * callback function
9072          */
9073         lpfc_nlp_put(ndlp);
9074
9075         mempool_free(pmb, phba->mbox_mem_pool);
9076         return;
9077 }
9078
9079 /**
9080  * lpfc_register_new_vport - Register a new vport with a HBA
9081  * @phba: pointer to lpfc hba data structure.
9082  * @vport: pointer to a host virtual N_Port data structure.
9083  * @ndlp: pointer to a node-list data structure.
9084  *
9085  * This routine registers the @vport as a new virtual port with a HBA.
9086  * It is done through a registering vpi mailbox command.
9087  **/
9088 void
9089 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
9090                         struct lpfc_nodelist *ndlp)
9091 {
9092         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9093         LPFC_MBOXQ_t *mbox;
9094
9095         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9096         if (mbox) {
9097                 lpfc_reg_vpi(vport, mbox);
9098                 mbox->vport = vport;
9099                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
9100                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
9101                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
9102                     == MBX_NOT_FINISHED) {
9103                         /* mailbox command not success, decrement ndlp
9104                          * reference count for this command
9105                          */
9106                         lpfc_nlp_put(ndlp);
9107                         mempool_free(mbox, phba->mbox_mem_pool);
9108
9109                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
9110                                 "0253 Register VPI: Can't send mbox\n");
9111                         goto mbox_err_exit;
9112                 }
9113         } else {
9114                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
9115                                  "0254 Register VPI: no memory\n");
9116                 goto mbox_err_exit;
9117         }
9118         return;
9119
9120 mbox_err_exit:
9121         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9122         spin_lock_irq(shost->host_lock);
9123         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
9124         spin_unlock_irq(shost->host_lock);
9125         return;
9126 }
9127
9128 /**
9129  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
9130  * @phba: pointer to lpfc hba data structure.
9131  *
9132  * This routine cancels the retry delay timers to all the vports.
9133  **/
9134 void
9135 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
9136 {
9137         struct lpfc_vport **vports;
9138         struct lpfc_nodelist *ndlp;
9139         uint32_t link_state;
9140         int i;
9141
9142         /* Treat this failure as linkdown for all vports */
9143         link_state = phba->link_state;
9144         lpfc_linkdown(phba);
9145         phba->link_state = link_state;
9146
9147         vports = lpfc_create_vport_work_array(phba);
9148
9149         if (vports) {
9150                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9151                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
9152                         if (ndlp)
9153                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
9154                         lpfc_els_flush_cmd(vports[i]);
9155                 }
9156                 lpfc_destroy_vport_work_array(phba, vports);
9157         }
9158 }
9159
9160 /**
9161  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
9162  * @phba: pointer to lpfc hba data structure.
9163  *
9164  * This routine abort all pending discovery commands and
9165  * start a timer to retry FLOGI for the physical port
9166  * discovery.
9167  **/
9168 void
9169 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
9170 {
9171         struct lpfc_nodelist *ndlp;
9172         struct Scsi_Host  *shost;
9173
9174         /* Cancel the all vports retry delay retry timers */
9175         lpfc_cancel_all_vport_retry_delay_timer(phba);
9176
9177         /* If fabric require FLOGI, then re-instantiate physical login */
9178         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
9179         if (!ndlp)
9180                 return;
9181
9182         shost = lpfc_shost_from_vport(phba->pport);
9183         mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
9184         spin_lock_irq(shost->host_lock);
9185         ndlp->nlp_flag |= NLP_DELAY_TMO;
9186         spin_unlock_irq(shost->host_lock);
9187         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
9188         phba->pport->port_state = LPFC_FLOGI;
9189         return;
9190 }
9191
9192 /**
9193  * lpfc_fabric_login_reqd - Check if FLOGI required.
9194  * @phba: pointer to lpfc hba data structure.
9195  * @cmdiocb: pointer to FDISC command iocb.
9196  * @rspiocb: pointer to FDISC response iocb.
9197  *
9198  * This routine checks if a FLOGI is reguired for FDISC
9199  * to succeed.
9200  **/
9201 static int
9202 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
9203                 struct lpfc_iocbq *cmdiocb,
9204                 struct lpfc_iocbq *rspiocb)
9205 {
9206
9207         if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
9208                 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
9209                 return 0;
9210         else
9211                 return 1;
9212 }
9213
9214 /**
9215  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
9216  * @phba: pointer to lpfc hba data structure.
9217  * @cmdiocb: pointer to lpfc command iocb data structure.
9218  * @rspiocb: pointer to lpfc response iocb data structure.
9219  *
9220  * This routine is the completion callback function to a Fabric Discover
9221  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
9222  * single threaded, each FDISC completion callback function will reset
9223  * the discovery timer for all vports such that the timers will not get
9224  * unnecessary timeout. The function checks the FDISC IOCB status. If error
9225  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
9226  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
9227  * assigned to the vport has been changed with the completion of the FDISC
9228  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
9229  * are unregistered from the HBA, and then the lpfc_register_new_vport()
9230  * routine is invoked to register new vport with the HBA. Otherwise, the
9231  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
9232  * Server for State Change Request (SCR).
9233  **/
9234 static void
9235 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9236                     struct lpfc_iocbq *rspiocb)
9237 {
9238         struct lpfc_vport *vport = cmdiocb->vport;
9239         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
9240         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
9241         struct lpfc_nodelist *np;
9242         struct lpfc_nodelist *next_np;
9243         IOCB_t *irsp = &rspiocb->iocb;
9244         struct lpfc_iocbq *piocb;
9245         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
9246         struct serv_parm *sp;
9247         uint8_t fabric_param_changed;
9248
9249         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9250                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
9251                          irsp->ulpStatus, irsp->un.ulpWord[4],
9252                          vport->fc_prevDID);
9253         /* Since all FDISCs are being single threaded, we
9254          * must reset the discovery timer for ALL vports
9255          * waiting to send FDISC when one completes.
9256          */
9257         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
9258                 lpfc_set_disctmo(piocb->vport);
9259         }
9260
9261         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9262                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
9263                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
9264
9265         if (irsp->ulpStatus) {
9266
9267                 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
9268                         lpfc_retry_pport_discovery(phba);
9269                         goto out;
9270                 }
9271
9272                 /* Check for retry */
9273                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
9274                         goto out;
9275                 /* FDISC failed */
9276                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9277                                  "0126 FDISC failed. (x%x/x%x)\n",
9278                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
9279                 goto fdisc_failed;
9280         }
9281         spin_lock_irq(shost->host_lock);
9282         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
9283         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
9284         vport->fc_flag |= FC_FABRIC;
9285         if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
9286                 vport->fc_flag |=  FC_PUBLIC_LOOP;
9287         spin_unlock_irq(shost->host_lock);
9288
9289         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
9290         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
9291         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
9292         if (!prsp)
9293                 goto out;
9294         sp = prsp->virt + sizeof(uint32_t);
9295         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
9296         memcpy(&vport->fabric_portname, &sp->portName,
9297                 sizeof(struct lpfc_name));
9298         memcpy(&vport->fabric_nodename, &sp->nodeName,
9299                 sizeof(struct lpfc_name));
9300         if (fabric_param_changed &&
9301                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9302                 /* If our NportID changed, we need to ensure all
9303                  * remaining NPORTs get unreg_login'ed so we can
9304                  * issue unreg_vpi.
9305                  */
9306                 list_for_each_entry_safe(np, next_np,
9307                         &vport->fc_nodes, nlp_listp) {
9308                         if (!NLP_CHK_NODE_ACT(ndlp) ||
9309                             (np->nlp_state != NLP_STE_NPR_NODE) ||
9310                             !(np->nlp_flag & NLP_NPR_ADISC))
9311                                 continue;
9312                         spin_lock_irq(shost->host_lock);
9313                         np->nlp_flag &= ~NLP_NPR_ADISC;
9314                         spin_unlock_irq(shost->host_lock);
9315                         lpfc_unreg_rpi(vport, np);
9316                 }
9317                 lpfc_cleanup_pending_mbox(vport);
9318
9319                 if (phba->sli_rev == LPFC_SLI_REV4)
9320                         lpfc_sli4_unreg_all_rpis(vport);
9321
9322                 lpfc_mbx_unreg_vpi(vport);
9323                 spin_lock_irq(shost->host_lock);
9324                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9325                 if (phba->sli_rev == LPFC_SLI_REV4)
9326                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
9327                 else
9328                         vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
9329                 spin_unlock_irq(shost->host_lock);
9330         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
9331                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9332                 /*
9333                  * Driver needs to re-reg VPI in order for f/w
9334                  * to update the MAC address.
9335                  */
9336                 lpfc_register_new_vport(phba, vport, ndlp);
9337                 goto out;
9338         }
9339
9340         if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
9341                 lpfc_issue_init_vpi(vport);
9342         else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
9343                 lpfc_register_new_vport(phba, vport, ndlp);
9344         else
9345                 lpfc_do_scr_ns_plogi(phba, vport);
9346         goto out;
9347 fdisc_failed:
9348         if (vport->fc_vport &&
9349             (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
9350                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9351         /* Cancel discovery timer */
9352         lpfc_can_disctmo(vport);
9353         lpfc_nlp_put(ndlp);
9354 out:
9355         lpfc_els_free_iocb(phba, cmdiocb);
9356 }
9357
9358 /**
9359  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
9360  * @vport: pointer to a virtual N_Port data structure.
9361  * @ndlp: pointer to a node-list data structure.
9362  * @retry: number of retries to the command IOCB.
9363  *
9364  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
9365  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
9366  * routine to issue the IOCB, which makes sure only one outstanding fabric
9367  * IOCB will be sent off HBA at any given time.
9368  *
9369  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9370  * will be incremented by 1 for holding the ndlp and the reference to ndlp
9371  * will be stored into the context1 field of the IOCB for the completion
9372  * callback function to the FDISC ELS command.
9373  *
9374  * Return code
9375  *   0 - Successfully issued fdisc iocb command
9376  *   1 - Failed to issue fdisc iocb command
9377  **/
9378 static int
9379 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
9380                      uint8_t retry)
9381 {
9382         struct lpfc_hba *phba = vport->phba;
9383         IOCB_t *icmd;
9384         struct lpfc_iocbq *elsiocb;
9385         struct serv_parm *sp;
9386         uint8_t *pcmd;
9387         uint16_t cmdsize;
9388         int did = ndlp->nlp_DID;
9389         int rc;
9390
9391         vport->port_state = LPFC_FDISC;
9392         vport->fc_myDID = 0;
9393         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
9394         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
9395                                      ELS_CMD_FDISC);
9396         if (!elsiocb) {
9397                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9398                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9399                                  "0255 Issue FDISC: no IOCB\n");
9400                 return 1;
9401         }
9402
9403         icmd = &elsiocb->iocb;
9404         icmd->un.elsreq64.myID = 0;
9405         icmd->un.elsreq64.fl = 1;
9406
9407         /*
9408          * SLI3 ports require a different context type value than SLI4.
9409          * Catch SLI3 ports here and override the prep.
9410          */
9411         if (phba->sli_rev == LPFC_SLI_REV3) {
9412                 icmd->ulpCt_h = 1;
9413                 icmd->ulpCt_l = 0;
9414         }
9415
9416         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9417         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
9418         pcmd += sizeof(uint32_t); /* CSP Word 1 */
9419         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
9420         sp = (struct serv_parm *) pcmd;
9421         /* Setup CSPs accordingly for Fabric */
9422         sp->cmn.e_d_tov = 0;
9423         sp->cmn.w2.r_a_tov = 0;
9424         sp->cmn.virtual_fabric_support = 0;
9425         sp->cls1.classValid = 0;
9426         sp->cls2.seqDelivery = 1;
9427         sp->cls3.seqDelivery = 1;
9428
9429         pcmd += sizeof(uint32_t); /* CSP Word 2 */
9430         pcmd += sizeof(uint32_t); /* CSP Word 3 */
9431         pcmd += sizeof(uint32_t); /* CSP Word 4 */
9432         pcmd += sizeof(uint32_t); /* Port Name */
9433         memcpy(pcmd, &vport->fc_portname, 8);
9434         pcmd += sizeof(uint32_t); /* Node Name */
9435         pcmd += sizeof(uint32_t); /* Node Name */
9436         memcpy(pcmd, &vport->fc_nodename, 8);
9437         sp->cmn.valid_vendor_ver_level = 0;
9438         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
9439         lpfc_set_disctmo(vport);
9440
9441         phba->fc_stat.elsXmitFDISC++;
9442         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
9443
9444         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9445                 "Issue FDISC:     did:x%x",
9446                 did, 0, 0);
9447
9448         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
9449         if (rc == IOCB_ERROR) {
9450                 lpfc_els_free_iocb(phba, elsiocb);
9451                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9452                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9453                                  "0256 Issue FDISC: Cannot send IOCB\n");
9454                 return 1;
9455         }
9456         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
9457         return 0;
9458 }
9459
9460 /**
9461  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
9462  * @phba: pointer to lpfc hba data structure.
9463  * @cmdiocb: pointer to lpfc command iocb data structure.
9464  * @rspiocb: pointer to lpfc response iocb data structure.
9465  *
9466  * This routine is the completion callback function to the issuing of a LOGO
9467  * ELS command off a vport. It frees the command IOCB and then decrement the
9468  * reference count held on ndlp for this completion function, indicating that
9469  * the reference to the ndlp is no long needed. Note that the
9470  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
9471  * callback function and an additional explicit ndlp reference decrementation
9472  * will trigger the actual release of the ndlp.
9473  **/
9474 static void
9475 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9476                         struct lpfc_iocbq *rspiocb)
9477 {
9478         struct lpfc_vport *vport = cmdiocb->vport;
9479         IOCB_t *irsp;
9480         struct lpfc_nodelist *ndlp;
9481         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9482
9483         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
9484         irsp = &rspiocb->iocb;
9485         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9486                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
9487                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
9488
9489         lpfc_els_free_iocb(phba, cmdiocb);
9490         vport->unreg_vpi_cmpl = VPORT_ERROR;
9491
9492         /* Trigger the release of the ndlp after logo */
9493         lpfc_nlp_put(ndlp);
9494
9495         /* NPIV LOGO completes to NPort <nlp_DID> */
9496         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9497                          "2928 NPIV LOGO completes to NPort x%x "
9498                          "Data: x%x x%x x%x x%x\n",
9499                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
9500                          irsp->ulpTimeout, vport->num_disc_nodes);
9501
9502         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
9503                 spin_lock_irq(shost->host_lock);
9504                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
9505                 vport->fc_flag &= ~FC_FABRIC;
9506                 spin_unlock_irq(shost->host_lock);
9507                 lpfc_can_disctmo(vport);
9508         }
9509 }
9510
9511 /**
9512  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
9513  * @vport: pointer to a virtual N_Port data structure.
9514  * @ndlp: pointer to a node-list data structure.
9515  *
9516  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
9517  *
9518  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9519  * will be incremented by 1 for holding the ndlp and the reference to ndlp
9520  * will be stored into the context1 field of the IOCB for the completion
9521  * callback function to the LOGO ELS command.
9522  *
9523  * Return codes
9524  *   0 - Successfully issued logo off the @vport
9525  *   1 - Failed to issue logo off the @vport
9526  **/
9527 int
9528 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
9529 {
9530         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9531         struct lpfc_hba  *phba = vport->phba;
9532         struct lpfc_iocbq *elsiocb;
9533         uint8_t *pcmd;
9534         uint16_t cmdsize;
9535
9536         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
9537         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
9538                                      ELS_CMD_LOGO);
9539         if (!elsiocb)
9540                 return 1;
9541
9542         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9543         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
9544         pcmd += sizeof(uint32_t);
9545
9546         /* Fill in LOGO payload */
9547         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
9548         pcmd += sizeof(uint32_t);
9549         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
9550
9551         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9552                 "Issue LOGO npiv  did:x%x flg:x%x",
9553                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
9554
9555         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
9556         spin_lock_irq(shost->host_lock);
9557         ndlp->nlp_flag |= NLP_LOGO_SND;
9558         spin_unlock_irq(shost->host_lock);
9559         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
9560             IOCB_ERROR) {
9561                 spin_lock_irq(shost->host_lock);
9562                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
9563                 spin_unlock_irq(shost->host_lock);
9564                 lpfc_els_free_iocb(phba, elsiocb);
9565                 return 1;
9566         }
9567         return 0;
9568 }
9569
9570 /**
9571  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
9572  * @ptr: holder for the timer function associated data.
9573  *
9574  * This routine is invoked by the fabric iocb block timer after
9575  * timeout. It posts the fabric iocb block timeout event by setting the
9576  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
9577  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
9578  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
9579  * posted event WORKER_FABRIC_BLOCK_TMO.
9580  **/
9581 void
9582 lpfc_fabric_block_timeout(struct timer_list *t)
9583 {
9584         struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
9585         unsigned long iflags;
9586         uint32_t tmo_posted;
9587
9588         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9589         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
9590         if (!tmo_posted)
9591                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
9592         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9593
9594         if (!tmo_posted)
9595                 lpfc_worker_wake_up(phba);
9596         return;
9597 }
9598
9599 /**
9600  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
9601  * @phba: pointer to lpfc hba data structure.
9602  *
9603  * This routine issues one fabric iocb from the driver internal list to
9604  * the HBA. It first checks whether it's ready to issue one fabric iocb to
9605  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
9606  * remove one pending fabric iocb from the driver internal list and invokes
9607  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
9608  **/
9609 static void
9610 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
9611 {
9612         struct lpfc_iocbq *iocb;
9613         unsigned long iflags;
9614         int ret;
9615         IOCB_t *cmd;
9616
9617 repeat:
9618         iocb = NULL;
9619         spin_lock_irqsave(&phba->hbalock, iflags);
9620         /* Post any pending iocb to the SLI layer */
9621         if (atomic_read(&phba->fabric_iocb_count) == 0) {
9622                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
9623                                  list);
9624                 if (iocb)
9625                         /* Increment fabric iocb count to hold the position */
9626                         atomic_inc(&phba->fabric_iocb_count);
9627         }
9628         spin_unlock_irqrestore(&phba->hbalock, iflags);
9629         if (iocb) {
9630                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9631                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9632                 iocb->iocb_flag |= LPFC_IO_FABRIC;
9633
9634                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9635                         "Fabric sched1:   ste:x%x",
9636                         iocb->vport->port_state, 0, 0);
9637
9638                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
9639
9640                 if (ret == IOCB_ERROR) {
9641                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9642                         iocb->fabric_iocb_cmpl = NULL;
9643                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9644                         cmd = &iocb->iocb;
9645                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
9646                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
9647                         iocb->iocb_cmpl(phba, iocb, iocb);
9648
9649                         atomic_dec(&phba->fabric_iocb_count);
9650                         goto repeat;
9651                 }
9652         }
9653
9654         return;
9655 }
9656
9657 /**
9658  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
9659  * @phba: pointer to lpfc hba data structure.
9660  *
9661  * This routine unblocks the  issuing fabric iocb command. The function
9662  * will clear the fabric iocb block bit and then invoke the routine
9663  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
9664  * from the driver internal fabric iocb list.
9665  **/
9666 void
9667 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
9668 {
9669         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9670
9671         lpfc_resume_fabric_iocbs(phba);
9672         return;
9673 }
9674
9675 /**
9676  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
9677  * @phba: pointer to lpfc hba data structure.
9678  *
9679  * This routine blocks the issuing fabric iocb for a specified amount of
9680  * time (currently 100 ms). This is done by set the fabric iocb block bit
9681  * and set up a timeout timer for 100ms. When the block bit is set, no more
9682  * fabric iocb will be issued out of the HBA.
9683  **/
9684 static void
9685 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
9686 {
9687         int blocked;
9688
9689         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9690         /* Start a timer to unblock fabric iocbs after 100ms */
9691         if (!blocked)
9692                 mod_timer(&phba->fabric_block_timer,
9693                           jiffies + msecs_to_jiffies(100));
9694
9695         return;
9696 }
9697
9698 /**
9699  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
9700  * @phba: pointer to lpfc hba data structure.
9701  * @cmdiocb: pointer to lpfc command iocb data structure.
9702  * @rspiocb: pointer to lpfc response iocb data structure.
9703  *
9704  * This routine is the callback function that is put to the fabric iocb's
9705  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
9706  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
9707  * function first restores and invokes the original iocb's callback function
9708  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
9709  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
9710  **/
9711 static void
9712 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9713         struct lpfc_iocbq *rspiocb)
9714 {
9715         struct ls_rjt stat;
9716
9717         BUG_ON((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
9718
9719         switch (rspiocb->iocb.ulpStatus) {
9720                 case IOSTAT_NPORT_RJT:
9721                 case IOSTAT_FABRIC_RJT:
9722                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
9723                                 lpfc_block_fabric_iocbs(phba);
9724                         }
9725                         break;
9726
9727                 case IOSTAT_NPORT_BSY:
9728                 case IOSTAT_FABRIC_BSY:
9729                         lpfc_block_fabric_iocbs(phba);
9730                         break;
9731
9732                 case IOSTAT_LS_RJT:
9733                         stat.un.lsRjtError =
9734                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
9735                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
9736                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
9737                                 lpfc_block_fabric_iocbs(phba);
9738                         break;
9739         }
9740
9741         BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
9742
9743         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
9744         cmdiocb->fabric_iocb_cmpl = NULL;
9745         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
9746         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
9747
9748         atomic_dec(&phba->fabric_iocb_count);
9749         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
9750                 /* Post any pending iocbs to HBA */
9751                 lpfc_resume_fabric_iocbs(phba);
9752         }
9753 }
9754
9755 /**
9756  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
9757  * @phba: pointer to lpfc hba data structure.
9758  * @iocb: pointer to lpfc command iocb data structure.
9759  *
9760  * This routine is used as the top-level API for issuing a fabric iocb command
9761  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
9762  * function makes sure that only one fabric bound iocb will be outstanding at
9763  * any given time. As such, this function will first check to see whether there
9764  * is already an outstanding fabric iocb on the wire. If so, it will put the
9765  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
9766  * issued later. Otherwise, it will issue the iocb on the wire and update the
9767  * fabric iocb count it indicate that there is one fabric iocb on the wire.
9768  *
9769  * Note, this implementation has a potential sending out fabric IOCBs out of
9770  * order. The problem is caused by the construction of the "ready" boolen does
9771  * not include the condition that the internal fabric IOCB list is empty. As
9772  * such, it is possible a fabric IOCB issued by this routine might be "jump"
9773  * ahead of the fabric IOCBs in the internal list.
9774  *
9775  * Return code
9776  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
9777  *   IOCB_ERROR - failed to issue fabric iocb
9778  **/
9779 static int
9780 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
9781 {
9782         unsigned long iflags;
9783         int ready;
9784         int ret;
9785
9786         BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
9787
9788         spin_lock_irqsave(&phba->hbalock, iflags);
9789         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
9790                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9791
9792         if (ready)
9793                 /* Increment fabric iocb count to hold the position */
9794                 atomic_inc(&phba->fabric_iocb_count);
9795         spin_unlock_irqrestore(&phba->hbalock, iflags);
9796         if (ready) {
9797                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9798                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9799                 iocb->iocb_flag |= LPFC_IO_FABRIC;
9800
9801                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9802                         "Fabric sched2:   ste:x%x",
9803                         iocb->vport->port_state, 0, 0);
9804
9805                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
9806
9807                 if (ret == IOCB_ERROR) {
9808                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9809                         iocb->fabric_iocb_cmpl = NULL;
9810                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9811                         atomic_dec(&phba->fabric_iocb_count);
9812                 }
9813         } else {
9814                 spin_lock_irqsave(&phba->hbalock, iflags);
9815                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
9816                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9817                 ret = IOCB_SUCCESS;
9818         }
9819         return ret;
9820 }
9821
9822 /**
9823  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
9824  * @vport: pointer to a virtual N_Port data structure.
9825  *
9826  * This routine aborts all the IOCBs associated with a @vport from the
9827  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9828  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9829  * list, removes each IOCB associated with the @vport off the list, set the
9830  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9831  * associated with the IOCB.
9832  **/
9833 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
9834 {
9835         LIST_HEAD(completions);
9836         struct lpfc_hba  *phba = vport->phba;
9837         struct lpfc_iocbq *tmp_iocb, *piocb;
9838
9839         spin_lock_irq(&phba->hbalock);
9840         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9841                                  list) {
9842
9843                 if (piocb->vport != vport)
9844                         continue;
9845
9846                 list_move_tail(&piocb->list, &completions);
9847         }
9848         spin_unlock_irq(&phba->hbalock);
9849
9850         /* Cancel all the IOCBs from the completions list */
9851         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9852                               IOERR_SLI_ABORTED);
9853 }
9854
9855 /**
9856  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
9857  * @ndlp: pointer to a node-list data structure.
9858  *
9859  * This routine aborts all the IOCBs associated with an @ndlp from the
9860  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9861  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9862  * list, removes each IOCB associated with the @ndlp off the list, set the
9863  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9864  * associated with the IOCB.
9865  **/
9866 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
9867 {
9868         LIST_HEAD(completions);
9869         struct lpfc_hba  *phba = ndlp->phba;
9870         struct lpfc_iocbq *tmp_iocb, *piocb;
9871         struct lpfc_sli_ring *pring;
9872
9873         pring = lpfc_phba_elsring(phba);
9874
9875         if (unlikely(!pring))
9876                 return;
9877
9878         spin_lock_irq(&phba->hbalock);
9879         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9880                                  list) {
9881                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
9882
9883                         list_move_tail(&piocb->list, &completions);
9884                 }
9885         }
9886         spin_unlock_irq(&phba->hbalock);
9887
9888         /* Cancel all the IOCBs from the completions list */
9889         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9890                               IOERR_SLI_ABORTED);
9891 }
9892
9893 /**
9894  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
9895  * @phba: pointer to lpfc hba data structure.
9896  *
9897  * This routine aborts all the IOCBs currently on the driver internal
9898  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
9899  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
9900  * list, removes IOCBs off the list, set the status feild to
9901  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
9902  * the IOCB.
9903  **/
9904 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
9905 {
9906         LIST_HEAD(completions);
9907
9908         spin_lock_irq(&phba->hbalock);
9909         list_splice_init(&phba->fabric_iocb_list, &completions);
9910         spin_unlock_irq(&phba->hbalock);
9911
9912         /* Cancel all the IOCBs from the completions list */
9913         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9914                               IOERR_SLI_ABORTED);
9915 }
9916
9917 /**
9918  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
9919  * @vport: pointer to lpfc vport data structure.
9920  *
9921  * This routine is invoked by the vport cleanup for deletions and the cleanup
9922  * for an ndlp on removal.
9923  **/
9924 void
9925 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
9926 {
9927         struct lpfc_hba *phba = vport->phba;
9928         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
9929         unsigned long iflag = 0;
9930
9931         spin_lock_irqsave(&phba->hbalock, iflag);
9932         spin_lock(&phba->sli4_hba.sgl_list_lock);
9933         list_for_each_entry_safe(sglq_entry, sglq_next,
9934                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
9935                 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
9936                         sglq_entry->ndlp = NULL;
9937         }
9938         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9939         spin_unlock_irqrestore(&phba->hbalock, iflag);
9940         return;
9941 }
9942
9943 /**
9944  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
9945  * @phba: pointer to lpfc hba data structure.
9946  * @axri: pointer to the els xri abort wcqe structure.
9947  *
9948  * This routine is invoked by the worker thread to process a SLI4 slow-path
9949  * ELS aborted xri.
9950  **/
9951 void
9952 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
9953                           struct sli4_wcqe_xri_aborted *axri)
9954 {
9955         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
9956         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
9957         uint16_t lxri = 0;
9958
9959         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
9960         unsigned long iflag = 0;
9961         struct lpfc_nodelist *ndlp;
9962         struct lpfc_sli_ring *pring;
9963
9964         pring = lpfc_phba_elsring(phba);
9965
9966         spin_lock_irqsave(&phba->hbalock, iflag);
9967         spin_lock(&phba->sli4_hba.sgl_list_lock);
9968         list_for_each_entry_safe(sglq_entry, sglq_next,
9969                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
9970                 if (sglq_entry->sli4_xritag == xri) {
9971                         list_del(&sglq_entry->list);
9972                         ndlp = sglq_entry->ndlp;
9973                         sglq_entry->ndlp = NULL;
9974                         list_add_tail(&sglq_entry->list,
9975                                 &phba->sli4_hba.lpfc_els_sgl_list);
9976                         sglq_entry->state = SGL_FREED;
9977                         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9978                         spin_unlock_irqrestore(&phba->hbalock, iflag);
9979                         lpfc_set_rrq_active(phba, ndlp,
9980                                 sglq_entry->sli4_lxritag,
9981                                 rxid, 1);
9982
9983                         /* Check if TXQ queue needs to be serviced */
9984                         if (pring && !list_empty(&pring->txq))
9985                                 lpfc_worker_wake_up(phba);
9986                         return;
9987                 }
9988         }
9989         spin_unlock(&phba->sli4_hba.sgl_list_lock);
9990         lxri = lpfc_sli4_xri_inrange(phba, xri);
9991         if (lxri == NO_XRI) {
9992                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9993                 return;
9994         }
9995         spin_lock(&phba->sli4_hba.sgl_list_lock);
9996         sglq_entry = __lpfc_get_active_sglq(phba, lxri);
9997         if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
9998                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
9999                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10000                 return;
10001         }
10002         sglq_entry->state = SGL_XRI_ABORTED;
10003         spin_unlock(&phba->sli4_hba.sgl_list_lock);
10004         spin_unlock_irqrestore(&phba->hbalock, iflag);
10005         return;
10006 }
10007
10008 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
10009  * @vport: pointer to virtual port object.
10010  * @ndlp: nodelist pointer for the impacted node.
10011  *
10012  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
10013  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
10014  * the driver is required to send a LOGO to the remote node before it
10015  * attempts to recover its login to the remote node.
10016  */
10017 void
10018 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
10019                            struct lpfc_nodelist *ndlp)
10020 {
10021         struct Scsi_Host *shost;
10022         struct lpfc_hba *phba;
10023         unsigned long flags = 0;
10024
10025         shost = lpfc_shost_from_vport(vport);
10026         phba = vport->phba;
10027         if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
10028                 lpfc_printf_log(phba, KERN_INFO,
10029                                 LOG_SLI, "3093 No rport recovery needed. "
10030                                 "rport in state 0x%x\n", ndlp->nlp_state);
10031                 return;
10032         }
10033         lpfc_printf_log(phba, KERN_ERR,
10034                         LOG_ELS | LOG_FCP_ERROR | LOG_NVME_IOERR,
10035                         "3094 Start rport recovery on shost id 0x%x "
10036                         "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
10037                         "flags 0x%x\n",
10038                         shost->host_no, ndlp->nlp_DID,
10039                         vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
10040                         ndlp->nlp_flag);
10041         /*
10042          * The rport is not responding.  Remove the FCP-2 flag to prevent
10043          * an ADISC in the follow-up recovery code.
10044          */
10045         spin_lock_irqsave(shost->host_lock, flags);
10046         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
10047         ndlp->nlp_flag |= NLP_ISSUE_LOGO;
10048         spin_unlock_irqrestore(shost->host_lock, flags);
10049         lpfc_unreg_rpi(vport, ndlp);
10050 }
10051