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