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