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