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