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