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