scsi: lpfc: Reset link or adapter instead of doing infinite nameserver PLOGI retry
[sfrench/cifs-2.6.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 TMO:x%x\n",
1061                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1062                                  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                          "Data: x%x x%x x%x x%x x%x x%x\n", cmdiocb->iotag,
1117                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1118                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1119                          vport->port_state, vport->fc_flag);
1120
1121         if (vport->port_state == LPFC_FLOGI) {
1122                 /*
1123                  * If Common Service Parameters indicate Nport
1124                  * we are point to point, if Fport we are Fabric.
1125                  */
1126                 if (sp->cmn.fPort)
1127                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1128                 else if (!(phba->hba_flag & HBA_FCOE_MODE))
1129                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1130                 else {
1131                         lpfc_printf_vlog(vport, KERN_ERR,
1132                                 LOG_FIP | LOG_ELS,
1133                                 "2831 FLOGI response with cleared Fabric "
1134                                 "bit fcf_index 0x%x "
1135                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1136                                 "Fabric Name "
1137                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1138                                 phba->fcf.current_rec.fcf_indx,
1139                                 phba->fcf.current_rec.switch_name[0],
1140                                 phba->fcf.current_rec.switch_name[1],
1141                                 phba->fcf.current_rec.switch_name[2],
1142                                 phba->fcf.current_rec.switch_name[3],
1143                                 phba->fcf.current_rec.switch_name[4],
1144                                 phba->fcf.current_rec.switch_name[5],
1145                                 phba->fcf.current_rec.switch_name[6],
1146                                 phba->fcf.current_rec.switch_name[7],
1147                                 phba->fcf.current_rec.fabric_name[0],
1148                                 phba->fcf.current_rec.fabric_name[1],
1149                                 phba->fcf.current_rec.fabric_name[2],
1150                                 phba->fcf.current_rec.fabric_name[3],
1151                                 phba->fcf.current_rec.fabric_name[4],
1152                                 phba->fcf.current_rec.fabric_name[5],
1153                                 phba->fcf.current_rec.fabric_name[6],
1154                                 phba->fcf.current_rec.fabric_name[7]);
1155                         lpfc_nlp_put(ndlp);
1156                         spin_lock_irq(&phba->hbalock);
1157                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1158                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1159                         spin_unlock_irq(&phba->hbalock);
1160                         goto out;
1161                 }
1162                 if (!rc) {
1163                         /* Mark the FCF discovery process done */
1164                         if (phba->hba_flag & HBA_FIP_SUPPORT)
1165                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1166                                                 LOG_ELS,
1167                                                 "2769 FLOGI to FCF (x%x) "
1168                                                 "completed successfully\n",
1169                                                 phba->fcf.current_rec.fcf_indx);
1170                         spin_lock_irq(&phba->hbalock);
1171                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1172                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1173                         spin_unlock_irq(&phba->hbalock);
1174                         goto out;
1175                 }
1176         }
1177
1178 flogifail:
1179         spin_lock_irq(&phba->hbalock);
1180         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1181         spin_unlock_irq(&phba->hbalock);
1182
1183         lpfc_nlp_put(ndlp);
1184
1185         if (!lpfc_error_lost_link(irsp)) {
1186                 /* FLOGI failed, so just use loop map to make discovery list */
1187                 lpfc_disc_list_loopmap(vport);
1188
1189                 /* Start discovery */
1190                 lpfc_disc_start(vport);
1191         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1192                         (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1193                          IOERR_SLI_ABORTED) &&
1194                         ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1195                          IOERR_SLI_DOWN))) &&
1196                         (phba->link_state != LPFC_CLEAR_LA)) {
1197                 /* If FLOGI failed enable link interrupt. */
1198                 lpfc_issue_clear_la(phba, vport);
1199         }
1200 out:
1201         lpfc_els_free_iocb(phba, cmdiocb);
1202 }
1203
1204 /**
1205  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1206  * @vport: pointer to a host virtual N_Port data structure.
1207  * @ndlp: pointer to a node-list data structure.
1208  * @retry: number of retries to the command IOCB.
1209  *
1210  * This routine issues a Fabric Login (FLOGI) Request ELS command
1211  * for a @vport. The initiator service parameters are put into the payload
1212  * of the FLOGI Request IOCB and the top-level callback function pointer
1213  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1214  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1215  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1216  *
1217  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1218  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1219  * will be stored into the context1 field of the IOCB for the completion
1220  * callback function to the FLOGI ELS command.
1221  *
1222  * Return code
1223  *   0 - successfully issued flogi iocb for @vport
1224  *   1 - failed to issue flogi iocb for @vport
1225  **/
1226 static int
1227 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1228                      uint8_t retry)
1229 {
1230         struct lpfc_hba  *phba = vport->phba;
1231         struct serv_parm *sp;
1232         IOCB_t *icmd;
1233         struct lpfc_iocbq *elsiocb;
1234         uint8_t *pcmd;
1235         uint16_t cmdsize;
1236         uint32_t tmo;
1237         int rc;
1238
1239         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1240         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1241                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1242
1243         if (!elsiocb)
1244                 return 1;
1245
1246         icmd = &elsiocb->iocb;
1247         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1248
1249         /* For FLOGI request, remainder of payload is service parameters */
1250         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1251         pcmd += sizeof(uint32_t);
1252         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1253         sp = (struct serv_parm *) pcmd;
1254
1255         /* Setup CSPs accordingly for Fabric */
1256         sp->cmn.e_d_tov = 0;
1257         sp->cmn.w2.r_a_tov = 0;
1258         sp->cmn.virtual_fabric_support = 0;
1259         sp->cls1.classValid = 0;
1260         if (sp->cmn.fcphLow < FC_PH3)
1261                 sp->cmn.fcphLow = FC_PH3;
1262         if (sp->cmn.fcphHigh < FC_PH3)
1263                 sp->cmn.fcphHigh = FC_PH3;
1264
1265         if  (phba->sli_rev == LPFC_SLI_REV4) {
1266                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1267                     LPFC_SLI_INTF_IF_TYPE_0) {
1268                         elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1269                         elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1270                         /* FLOGI needs to be 3 for WQE FCFI */
1271                         /* Set the fcfi to the fcfi we registered with */
1272                         elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1273                 }
1274                 /* Can't do SLI4 class2 without support sequence coalescing */
1275                 sp->cls2.classValid = 0;
1276                 sp->cls2.seqDelivery = 0;
1277         } else {
1278                 /* Historical, setting sequential-delivery bit for SLI3 */
1279                 sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1280                 sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1281                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1282                         sp->cmn.request_multiple_Nport = 1;
1283                         /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1284                         icmd->ulpCt_h = 1;
1285                         icmd->ulpCt_l = 0;
1286                 } else
1287                         sp->cmn.request_multiple_Nport = 0;
1288         }
1289
1290         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1291                 icmd->un.elsreq64.myID = 0;
1292                 icmd->un.elsreq64.fl = 1;
1293         }
1294
1295         tmo = phba->fc_ratov;
1296         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1297         lpfc_set_disctmo(vport);
1298         phba->fc_ratov = tmo;
1299
1300         phba->fc_stat.elsXmitFLOGI++;
1301         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1302
1303         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1304                 "Issue FLOGI:     opt:x%x",
1305                 phba->sli3_options, 0, 0);
1306
1307         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1308         if (rc == IOCB_ERROR) {
1309                 lpfc_els_free_iocb(phba, elsiocb);
1310                 return 1;
1311         }
1312         return 0;
1313 }
1314
1315 /**
1316  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1317  * @phba: pointer to lpfc hba data structure.
1318  *
1319  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1320  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1321  * list and issues an abort IOCB commond on each outstanding IOCB that
1322  * contains a active Fabric_DID ndlp. Note that this function is to issue
1323  * the abort IOCB command on all the outstanding IOCBs, thus when this
1324  * function returns, it does not guarantee all the IOCBs are actually aborted.
1325  *
1326  * Return code
1327  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1328  **/
1329 int
1330 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1331 {
1332         struct lpfc_sli_ring *pring;
1333         struct lpfc_iocbq *iocb, *next_iocb;
1334         struct lpfc_nodelist *ndlp;
1335         IOCB_t *icmd;
1336
1337         /* Abort outstanding I/O on NPort <nlp_DID> */
1338         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1339                         "0201 Abort outstanding I/O on NPort x%x\n",
1340                         Fabric_DID);
1341
1342         pring = lpfc_phba_elsring(phba);
1343
1344         /*
1345          * Check the txcmplq for an iocb that matches the nport the driver is
1346          * searching for.
1347          */
1348         spin_lock_irq(&phba->hbalock);
1349         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1350                 icmd = &iocb->iocb;
1351                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1352                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
1353                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1354                             (ndlp->nlp_DID == Fabric_DID))
1355                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1356                 }
1357         }
1358         spin_unlock_irq(&phba->hbalock);
1359
1360         return 0;
1361 }
1362
1363 /**
1364  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1365  * @vport: pointer to a host virtual N_Port data structure.
1366  *
1367  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1368  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1369  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1370  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1371  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1372  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1373  * @vport.
1374  *
1375  * Return code
1376  *   0 - failed to issue initial flogi for @vport
1377  *   1 - successfully issued initial flogi for @vport
1378  **/
1379 int
1380 lpfc_initial_flogi(struct lpfc_vport *vport)
1381 {
1382         struct lpfc_nodelist *ndlp;
1383
1384         vport->port_state = LPFC_FLOGI;
1385         lpfc_set_disctmo(vport);
1386
1387         /* First look for the Fabric ndlp */
1388         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1389         if (!ndlp) {
1390                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1391                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1392                 if (!ndlp)
1393                         return 0;
1394                 /* Set the node type */
1395                 ndlp->nlp_type |= NLP_FABRIC;
1396                 /* Put ndlp onto node list */
1397                 lpfc_enqueue_node(vport, ndlp);
1398         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1399                 /* re-setup ndlp without removing from node list */
1400                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1401                 if (!ndlp)
1402                         return 0;
1403         }
1404
1405         if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1406                 /* This decrement of reference count to node shall kick off
1407                  * the release of the node.
1408                  */
1409                 lpfc_nlp_put(ndlp);
1410                 return 0;
1411         }
1412         return 1;
1413 }
1414
1415 /**
1416  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1417  * @vport: pointer to a host virtual N_Port data structure.
1418  *
1419  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1420  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1421  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1422  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1423  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1424  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1425  * @vport.
1426  *
1427  * Return code
1428  *   0 - failed to issue initial fdisc for @vport
1429  *   1 - successfully issued initial fdisc for @vport
1430  **/
1431 int
1432 lpfc_initial_fdisc(struct lpfc_vport *vport)
1433 {
1434         struct lpfc_nodelist *ndlp;
1435
1436         /* First look for the Fabric ndlp */
1437         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1438         if (!ndlp) {
1439                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1440                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1441                 if (!ndlp)
1442                         return 0;
1443                 /* Put ndlp onto node list */
1444                 lpfc_enqueue_node(vport, ndlp);
1445         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1446                 /* re-setup ndlp without removing from node list */
1447                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1448                 if (!ndlp)
1449                         return 0;
1450         }
1451
1452         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1453                 /* decrement node reference count to trigger the release of
1454                  * the node.
1455                  */
1456                 lpfc_nlp_put(ndlp);
1457                 return 0;
1458         }
1459         return 1;
1460 }
1461
1462 /**
1463  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1464  * @vport: pointer to a host virtual N_Port data structure.
1465  *
1466  * This routine checks whether there are more remaining Port Logins
1467  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1468  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1469  * to issue ELS PLOGIs up to the configured discover threads with the
1470  * @vport (@vport->cfg_discovery_threads). The function also decrement
1471  * the @vport's num_disc_node by 1 if it is not already 0.
1472  **/
1473 void
1474 lpfc_more_plogi(struct lpfc_vport *vport)
1475 {
1476         if (vport->num_disc_nodes)
1477                 vport->num_disc_nodes--;
1478
1479         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1480         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1481                          "0232 Continue discovery with %d PLOGIs to go "
1482                          "Data: x%x x%x x%x\n",
1483                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1484                          vport->fc_flag, vport->port_state);
1485         /* Check to see if there are more PLOGIs to be sent */
1486         if (vport->fc_flag & FC_NLP_MORE)
1487                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1488                 lpfc_els_disc_plogi(vport);
1489
1490         return;
1491 }
1492
1493 /**
1494  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1495  * @phba: pointer to lpfc hba data structure.
1496  * @prsp: pointer to response IOCB payload.
1497  * @ndlp: pointer to a node-list data structure.
1498  *
1499  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1500  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1501  * The following cases are considered N_Port confirmed:
1502  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1503  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1504  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1505  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1506  * 1) if there is a node on vport list other than the @ndlp with the same
1507  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1508  * on that node to release the RPI associated with the node; 2) if there is
1509  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1510  * into, a new node shall be allocated (or activated). In either case, the
1511  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1512  * be released and the new_ndlp shall be put on to the vport node list and
1513  * its pointer returned as the confirmed node.
1514  *
1515  * Note that before the @ndlp got "released", the keepDID from not-matching
1516  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1517  * of the @ndlp. This is because the release of @ndlp is actually to put it
1518  * into an inactive state on the vport node list and the vport node list
1519  * management algorithm does not allow two node with a same DID.
1520  *
1521  * Return code
1522  *   pointer to the PLOGI N_Port @ndlp
1523  **/
1524 static struct lpfc_nodelist *
1525 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1526                          struct lpfc_nodelist *ndlp)
1527 {
1528         struct lpfc_vport *vport = ndlp->vport;
1529         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1530         struct lpfc_nodelist *new_ndlp;
1531         struct lpfc_rport_data *rdata;
1532         struct fc_rport *rport;
1533         struct serv_parm *sp;
1534         uint8_t  name[sizeof(struct lpfc_name)];
1535         uint32_t rc, keepDID = 0, keep_nlp_flag = 0;
1536         uint16_t keep_nlp_state;
1537         struct lpfc_nvme_rport *keep_nrport = NULL;
1538         int  put_node;
1539         int  put_rport;
1540         unsigned long *active_rrqs_xri_bitmap = NULL;
1541
1542         /* Fabric nodes can have the same WWPN so we don't bother searching
1543          * by WWPN.  Just return the ndlp that was given to us.
1544          */
1545         if (ndlp->nlp_type & NLP_FABRIC)
1546                 return ndlp;
1547
1548         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1549         memset(name, 0, sizeof(struct lpfc_name));
1550
1551         /* Now we find out if the NPort we are logging into, matches the WWPN
1552          * we have for that ndlp. If not, we have some work to do.
1553          */
1554         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1555
1556         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1557                 return ndlp;
1558         if (phba->sli_rev == LPFC_SLI_REV4) {
1559                 active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1560                                                        GFP_KERNEL);
1561                 if (active_rrqs_xri_bitmap)
1562                         memset(active_rrqs_xri_bitmap, 0,
1563                                phba->cfg_rrq_xri_bitmap_sz);
1564         }
1565
1566         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1567                  "3178 PLOGI confirm: ndlp %p x%x: new_ndlp %p\n",
1568                  ndlp, ndlp->nlp_DID, new_ndlp);
1569
1570         if (!new_ndlp) {
1571                 rc = memcmp(&ndlp->nlp_portname, name,
1572                             sizeof(struct lpfc_name));
1573                 if (!rc) {
1574                         if (active_rrqs_xri_bitmap)
1575                                 mempool_free(active_rrqs_xri_bitmap,
1576                                              phba->active_rrq_pool);
1577                         return ndlp;
1578                 }
1579                 new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
1580                 if (!new_ndlp) {
1581                         if (active_rrqs_xri_bitmap)
1582                                 mempool_free(active_rrqs_xri_bitmap,
1583                                              phba->active_rrq_pool);
1584                         return ndlp;
1585                 }
1586         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1587                 rc = memcmp(&ndlp->nlp_portname, name,
1588                             sizeof(struct lpfc_name));
1589                 if (!rc) {
1590                         if (active_rrqs_xri_bitmap)
1591                                 mempool_free(active_rrqs_xri_bitmap,
1592                                              phba->active_rrq_pool);
1593                         return ndlp;
1594                 }
1595                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1596                                                 NLP_STE_UNUSED_NODE);
1597                 if (!new_ndlp) {
1598                         if (active_rrqs_xri_bitmap)
1599                                 mempool_free(active_rrqs_xri_bitmap,
1600                                              phba->active_rrq_pool);
1601                         return ndlp;
1602                 }
1603                 keepDID = new_ndlp->nlp_DID;
1604                 if ((phba->sli_rev == LPFC_SLI_REV4) && active_rrqs_xri_bitmap)
1605                         memcpy(active_rrqs_xri_bitmap,
1606                                new_ndlp->active_rrqs_xri_bitmap,
1607                                phba->cfg_rrq_xri_bitmap_sz);
1608         } else {
1609                 keepDID = new_ndlp->nlp_DID;
1610                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1611                     active_rrqs_xri_bitmap)
1612                         memcpy(active_rrqs_xri_bitmap,
1613                                new_ndlp->active_rrqs_xri_bitmap,
1614                                phba->cfg_rrq_xri_bitmap_sz);
1615         }
1616
1617         lpfc_unreg_rpi(vport, new_ndlp);
1618         new_ndlp->nlp_DID = ndlp->nlp_DID;
1619         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1620         if (phba->sli_rev == LPFC_SLI_REV4)
1621                 memcpy(new_ndlp->active_rrqs_xri_bitmap,
1622                        ndlp->active_rrqs_xri_bitmap,
1623                        phba->cfg_rrq_xri_bitmap_sz);
1624
1625         spin_lock_irq(shost->host_lock);
1626         keep_nlp_flag = new_ndlp->nlp_flag;
1627         new_ndlp->nlp_flag = ndlp->nlp_flag;
1628         ndlp->nlp_flag = keep_nlp_flag;
1629         spin_unlock_irq(shost->host_lock);
1630
1631         /* Set nlp_states accordingly */
1632         keep_nlp_state = new_ndlp->nlp_state;
1633         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1634
1635         /* interchange the nvme remoteport structs */
1636         keep_nrport = new_ndlp->nrport;
1637         new_ndlp->nrport = ndlp->nrport;
1638
1639         /* Move this back to NPR state */
1640         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1641                 /* The new_ndlp is replacing ndlp totally, so we need
1642                  * to put ndlp on UNUSED list and try to free it.
1643                  */
1644                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1645                          "3179 PLOGI confirm NEW: %x %x\n",
1646                          new_ndlp->nlp_DID, keepDID);
1647
1648                 /* Fix up the rport accordingly */
1649                 rport =  ndlp->rport;
1650                 if (rport) {
1651                         rdata = rport->dd_data;
1652                         if (rdata->pnode == ndlp) {
1653                                 /* break the link before dropping the ref */
1654                                 ndlp->rport = NULL;
1655                                 lpfc_nlp_put(ndlp);
1656                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1657                                 new_ndlp->rport = rport;
1658                         }
1659                         new_ndlp->nlp_type = ndlp->nlp_type;
1660                 }
1661
1662                 /* Fix up the nvme rport */
1663                 if (ndlp->nrport) {
1664                         ndlp->nrport = NULL;
1665                         lpfc_nlp_put(ndlp);
1666                         new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1667                 }
1668
1669                 /* We shall actually free the ndlp with both nlp_DID and
1670                  * nlp_portname fields equals 0 to avoid any ndlp on the
1671                  * nodelist never to be used.
1672                  */
1673                 if (ndlp->nlp_DID == 0) {
1674                         spin_lock_irq(&phba->ndlp_lock);
1675                         NLP_SET_FREE_REQ(ndlp);
1676                         spin_unlock_irq(&phba->ndlp_lock);
1677                 }
1678
1679                 /* Two ndlps cannot have the same did on the nodelist */
1680                 ndlp->nlp_DID = keepDID;
1681                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1682                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1683                     active_rrqs_xri_bitmap)
1684                         memcpy(ndlp->active_rrqs_xri_bitmap,
1685                                active_rrqs_xri_bitmap,
1686                                phba->cfg_rrq_xri_bitmap_sz);
1687
1688                 if (!NLP_CHK_NODE_ACT(ndlp))
1689                         lpfc_drop_node(vport, ndlp);
1690         }
1691         else {
1692                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1693                          "3180 PLOGI confirm SWAP: %x %x\n",
1694                          new_ndlp->nlp_DID, keepDID);
1695
1696                 lpfc_unreg_rpi(vport, ndlp);
1697
1698                 /* Two ndlps cannot have the same did */
1699                 ndlp->nlp_DID = keepDID;
1700                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1701                     active_rrqs_xri_bitmap)
1702                         memcpy(ndlp->active_rrqs_xri_bitmap,
1703                                active_rrqs_xri_bitmap,
1704                                phba->cfg_rrq_xri_bitmap_sz);
1705
1706                 /* Since we are switching over to the new_ndlp,
1707                  * reset the old ndlp state
1708                  */
1709                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1710                     (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1711                         keep_nlp_state = NLP_STE_NPR_NODE;
1712                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1713
1714                 /* Previous ndlp no longer active with nvme host transport.
1715                  * Remove reference from earlier registration unless the
1716                  * nvme host took care of it.
1717                  */
1718                 if (ndlp->nrport)
1719                         lpfc_nlp_put(ndlp);
1720                 ndlp->nrport = keep_nrport;
1721
1722                 /* Fix up the rport accordingly */
1723                 rport = ndlp->rport;
1724                 if (rport) {
1725                         rdata = rport->dd_data;
1726                         put_node = rdata->pnode != NULL;
1727                         put_rport = ndlp->rport != NULL;
1728                         rdata->pnode = NULL;
1729                         ndlp->rport = NULL;
1730                         if (put_node)
1731                                 lpfc_nlp_put(ndlp);
1732                         if (put_rport)
1733                                 put_device(&rport->dev);
1734                 }
1735         }
1736         if (phba->sli_rev == LPFC_SLI_REV4 &&
1737             active_rrqs_xri_bitmap)
1738                 mempool_free(active_rrqs_xri_bitmap,
1739                              phba->active_rrq_pool);
1740         return new_ndlp;
1741 }
1742
1743 /**
1744  * lpfc_end_rscn - Check and handle more rscn for a vport
1745  * @vport: pointer to a host virtual N_Port data structure.
1746  *
1747  * This routine checks whether more Registration State Change
1748  * Notifications (RSCNs) came in while the discovery state machine was in
1749  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1750  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1751  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1752  * handling the RSCNs.
1753  **/
1754 void
1755 lpfc_end_rscn(struct lpfc_vport *vport)
1756 {
1757         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1758
1759         if (vport->fc_flag & FC_RSCN_MODE) {
1760                 /*
1761                  * Check to see if more RSCNs came in while we were
1762                  * processing this one.
1763                  */
1764                 if (vport->fc_rscn_id_cnt ||
1765                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1766                         lpfc_els_handle_rscn(vport);
1767                 else {
1768                         spin_lock_irq(shost->host_lock);
1769                         vport->fc_flag &= ~FC_RSCN_MODE;
1770                         spin_unlock_irq(shost->host_lock);
1771                 }
1772         }
1773 }
1774
1775 /**
1776  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1777  * @phba: pointer to lpfc hba data structure.
1778  * @cmdiocb: pointer to lpfc command iocb data structure.
1779  * @rspiocb: pointer to lpfc response iocb data structure.
1780  *
1781  * This routine will call the clear rrq function to free the rrq and
1782  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1783  * exist then the clear_rrq is still called because the rrq needs to
1784  * be freed.
1785  **/
1786
1787 static void
1788 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1789                     struct lpfc_iocbq *rspiocb)
1790 {
1791         struct lpfc_vport *vport = cmdiocb->vport;
1792         IOCB_t *irsp;
1793         struct lpfc_nodelist *ndlp;
1794         struct lpfc_node_rrq *rrq;
1795
1796         /* we pass cmdiocb to state machine which needs rspiocb as well */
1797         rrq = cmdiocb->context_un.rrq;
1798         cmdiocb->context_un.rsp_iocb = rspiocb;
1799
1800         irsp = &rspiocb->iocb;
1801         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1802                 "RRQ cmpl:      status:x%x/x%x did:x%x",
1803                 irsp->ulpStatus, irsp->un.ulpWord[4],
1804                 irsp->un.elsreq64.remoteID);
1805
1806         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1807         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1808                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1809                                  "2882 RRQ completes to NPort x%x "
1810                                  "with no ndlp. Data: x%x x%x x%x\n",
1811                                  irsp->un.elsreq64.remoteID,
1812                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1813                                  irsp->ulpIoTag);
1814                 goto out;
1815         }
1816
1817         /* rrq completes to NPort <nlp_DID> */
1818         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1819                          "2880 RRQ completes to NPort x%x "
1820                          "Data: x%x x%x x%x x%x x%x\n",
1821                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1822                          irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1823
1824         if (irsp->ulpStatus) {
1825                 /* Check for retry */
1826                 /* RRQ failed Don't print the vport to vport rjts */
1827                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1828                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1829                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1830                         (phba)->pport->cfg_log_verbose & LOG_ELS)
1831                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1832                                  "2881 RRQ failure DID:%06X Status:x%x/x%x\n",
1833                                  ndlp->nlp_DID, irsp->ulpStatus,
1834                                  irsp->un.ulpWord[4]);
1835         }
1836 out:
1837         if (rrq)
1838                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1839         lpfc_els_free_iocb(phba, cmdiocb);
1840         return;
1841 }
1842 /**
1843  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1844  * @phba: pointer to lpfc hba data structure.
1845  * @cmdiocb: pointer to lpfc command iocb data structure.
1846  * @rspiocb: pointer to lpfc response iocb data structure.
1847  *
1848  * This routine is the completion callback function for issuing the Port
1849  * Login (PLOGI) command. For PLOGI completion, there must be an active
1850  * ndlp on the vport node list that matches the remote node ID from the
1851  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1852  * ignored and command IOCB released. The PLOGI response IOCB status is
1853  * checked for error conditons. If there is error status reported, PLOGI
1854  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1855  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1856  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1857  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1858  * there are additional N_Port nodes with the vport that need to perform
1859  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1860  * PLOGIs.
1861  **/
1862 static void
1863 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1864                     struct lpfc_iocbq *rspiocb)
1865 {
1866         struct lpfc_vport *vport = cmdiocb->vport;
1867         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1868         IOCB_t *irsp;
1869         struct lpfc_nodelist *ndlp;
1870         struct lpfc_dmabuf *prsp;
1871         int disc, rc;
1872
1873         /* we pass cmdiocb to state machine which needs rspiocb as well */
1874         cmdiocb->context_un.rsp_iocb = rspiocb;
1875
1876         irsp = &rspiocb->iocb;
1877         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1878                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1879                 irsp->ulpStatus, irsp->un.ulpWord[4],
1880                 irsp->un.elsreq64.remoteID);
1881
1882         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1883         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1884                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1885                                  "0136 PLOGI completes to NPort x%x "
1886                                  "with no ndlp. Data: x%x x%x x%x\n",
1887                                  irsp->un.elsreq64.remoteID,
1888                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1889                                  irsp->ulpIoTag);
1890                 goto out;
1891         }
1892
1893         /* Since ndlp can be freed in the disc state machine, note if this node
1894          * is being used during discovery.
1895          */
1896         spin_lock_irq(shost->host_lock);
1897         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1898         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1899         spin_unlock_irq(shost->host_lock);
1900         rc   = 0;
1901
1902         /* PLOGI completes to NPort <nlp_DID> */
1903         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1904                          "0102 PLOGI completes to NPort x%06x "
1905                          "Data: x%x x%x x%x x%x x%x\n",
1906                          ndlp->nlp_DID, ndlp->nlp_fc4_type,
1907                          irsp->ulpStatus, irsp->un.ulpWord[4],
1908                          disc, vport->num_disc_nodes);
1909
1910         /* Check to see if link went down during discovery */
1911         if (lpfc_els_chk_latt(vport)) {
1912                 spin_lock_irq(shost->host_lock);
1913                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1914                 spin_unlock_irq(shost->host_lock);
1915                 goto out;
1916         }
1917
1918         if (irsp->ulpStatus) {
1919                 /* Check for retry */
1920                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1921                         /* ELS command is being retried */
1922                         if (disc) {
1923                                 spin_lock_irq(shost->host_lock);
1924                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1925                                 spin_unlock_irq(shost->host_lock);
1926                         }
1927                         goto out;
1928                 }
1929                 /* PLOGI failed Don't print the vport to vport rjts */
1930                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1931                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1932                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1933                         (phba)->pport->cfg_log_verbose & LOG_ELS)
1934                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1935                                  "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1936                                  ndlp->nlp_DID, irsp->ulpStatus,
1937                                  irsp->un.ulpWord[4]);
1938                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1939                 if (lpfc_error_lost_link(irsp))
1940                         rc = NLP_STE_FREED_NODE;
1941                 else
1942                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1943                                                      NLP_EVT_CMPL_PLOGI);
1944         } else {
1945                 /* Good status, call state machine */
1946                 prsp = list_entry(((struct lpfc_dmabuf *)
1947                                    cmdiocb->context2)->list.next,
1948                                   struct lpfc_dmabuf, list);
1949                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1950                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1951                                              NLP_EVT_CMPL_PLOGI);
1952         }
1953
1954         if (disc && vport->num_disc_nodes) {
1955                 /* Check to see if there are more PLOGIs to be sent */
1956                 lpfc_more_plogi(vport);
1957
1958                 if (vport->num_disc_nodes == 0) {
1959                         spin_lock_irq(shost->host_lock);
1960                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1961                         spin_unlock_irq(shost->host_lock);
1962
1963                         lpfc_can_disctmo(vport);
1964                         lpfc_end_rscn(vport);
1965                 }
1966         }
1967
1968 out:
1969         lpfc_els_free_iocb(phba, cmdiocb);
1970         return;
1971 }
1972
1973 /**
1974  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1975  * @vport: pointer to a host virtual N_Port data structure.
1976  * @did: destination port identifier.
1977  * @retry: number of retries to the command IOCB.
1978  *
1979  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1980  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1981  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1982  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1983  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1984  *
1985  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1986  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1987  * will be stored into the context1 field of the IOCB for the completion
1988  * callback function to the PLOGI ELS command.
1989  *
1990  * Return code
1991  *   0 - Successfully issued a plogi for @vport
1992  *   1 - failed to issue a plogi for @vport
1993  **/
1994 int
1995 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1996 {
1997         struct lpfc_hba  *phba = vport->phba;
1998         struct Scsi_Host *shost;
1999         struct serv_parm *sp;
2000         struct lpfc_nodelist *ndlp;
2001         struct lpfc_iocbq *elsiocb;
2002         uint8_t *pcmd;
2003         uint16_t cmdsize;
2004         int ret;
2005
2006         ndlp = lpfc_findnode_did(vport, did);
2007         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
2008                 ndlp = NULL;
2009
2010         /* If ndlp is not NULL, we will bump the reference count on it */
2011         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2012         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2013                                      ELS_CMD_PLOGI);
2014         if (!elsiocb)
2015                 return 1;
2016
2017         shost = lpfc_shost_from_vport(vport);
2018         spin_lock_irq(shost->host_lock);
2019         ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2020         spin_unlock_irq(shost->host_lock);
2021
2022         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2023
2024         /* For PLOGI request, remainder of payload is service parameters */
2025         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2026         pcmd += sizeof(uint32_t);
2027         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2028         sp = (struct serv_parm *) pcmd;
2029
2030         /*
2031          * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2032          * to device on remote loops work.
2033          */
2034         if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2035                 sp->cmn.altBbCredit = 1;
2036
2037         if (sp->cmn.fcphLow < FC_PH_4_3)
2038                 sp->cmn.fcphLow = FC_PH_4_3;
2039
2040         if (sp->cmn.fcphHigh < FC_PH3)
2041                 sp->cmn.fcphHigh = FC_PH3;
2042
2043         sp->cmn.valid_vendor_ver_level = 0;
2044         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2045         sp->cmn.bbRcvSizeMsb &= 0xF;
2046
2047         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2048                 "Issue PLOGI:     did:x%x",
2049                 did, 0, 0);
2050
2051         /* If our firmware supports this feature, convey that
2052          * information to the target using the vendor specific field.
2053          */
2054         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2055                 sp->cmn.valid_vendor_ver_level = 1;
2056                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2057                 sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2058         }
2059
2060         phba->fc_stat.elsXmitPLOGI++;
2061         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
2062         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2063
2064         if (ret == IOCB_ERROR) {
2065                 lpfc_els_free_iocb(phba, elsiocb);
2066                 return 1;
2067         }
2068         return 0;
2069 }
2070
2071 /**
2072  * lpfc_cmpl_els_prli - Completion callback function for prli
2073  * @phba: pointer to lpfc hba data structure.
2074  * @cmdiocb: pointer to lpfc command iocb data structure.
2075  * @rspiocb: pointer to lpfc response iocb data structure.
2076  *
2077  * This routine is the completion callback function for a Process Login
2078  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2079  * status. If there is error status reported, PRLI retry shall be attempted
2080  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2081  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2082  * ndlp to mark the PRLI completion.
2083  **/
2084 static void
2085 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2086                    struct lpfc_iocbq *rspiocb)
2087 {
2088         struct lpfc_vport *vport = cmdiocb->vport;
2089         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2090         IOCB_t *irsp;
2091         struct lpfc_nodelist *ndlp;
2092
2093         /* we pass cmdiocb to state machine which needs rspiocb as well */
2094         cmdiocb->context_un.rsp_iocb = rspiocb;
2095
2096         irsp = &(rspiocb->iocb);
2097         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2098         spin_lock_irq(shost->host_lock);
2099         ndlp->nlp_flag &= ~NLP_PRLI_SND;
2100
2101         /* Driver supports multiple FC4 types.  Counters matter. */
2102         vport->fc_prli_sent--;
2103         ndlp->fc4_prli_sent--;
2104         spin_unlock_irq(shost->host_lock);
2105
2106         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2107                 "PRLI cmpl:       status:x%x/x%x did:x%x",
2108                 irsp->ulpStatus, irsp->un.ulpWord[4],
2109                 ndlp->nlp_DID);
2110
2111         /* PRLI completes to NPort <nlp_DID> */
2112         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2113                          "0103 PRLI completes to NPort x%06x "
2114                          "Data: x%x x%x x%x x%x\n",
2115                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2116                          vport->num_disc_nodes, ndlp->fc4_prli_sent);
2117
2118         /* Check to see if link went down during discovery */
2119         if (lpfc_els_chk_latt(vport))
2120                 goto out;
2121
2122         if (irsp->ulpStatus) {
2123                 /* Check for retry */
2124                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2125                         /* ELS command is being retried */
2126                         goto out;
2127                 }
2128
2129                 /* PRLI failed */
2130                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2131                                  "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2132                                  "data: x%x\n",
2133                                  ndlp->nlp_DID, irsp->ulpStatus,
2134                                  irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2135
2136                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2137                 if (lpfc_error_lost_link(irsp))
2138                         goto out;
2139                 else
2140                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2141                                                 NLP_EVT_CMPL_PRLI);
2142         } else
2143                 /* Good status, call state machine.  However, if another
2144                  * PRLI is outstanding, don't call the state machine
2145                  * because final disposition to Mapped or Unmapped is
2146                  * completed there.
2147                  */
2148                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2149                                         NLP_EVT_CMPL_PRLI);
2150
2151 out:
2152         lpfc_els_free_iocb(phba, cmdiocb);
2153         return;
2154 }
2155
2156 /**
2157  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2158  * @vport: pointer to a host virtual N_Port data structure.
2159  * @ndlp: pointer to a node-list data structure.
2160  * @retry: number of retries to the command IOCB.
2161  *
2162  * This routine issues a Process Login (PRLI) ELS command for the
2163  * @vport. The PRLI service parameters are set up in the payload of the
2164  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2165  * is put to the IOCB completion callback func field before invoking the
2166  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2167  *
2168  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2169  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2170  * will be stored into the context1 field of the IOCB for the completion
2171  * callback function to the PRLI ELS command.
2172  *
2173  * Return code
2174  *   0 - successfully issued prli iocb command for @vport
2175  *   1 - failed to issue prli iocb command for @vport
2176  **/
2177 int
2178 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2179                     uint8_t retry)
2180 {
2181         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2182         struct lpfc_hba *phba = vport->phba;
2183         PRLI *npr;
2184         struct lpfc_nvme_prli *npr_nvme;
2185         struct lpfc_iocbq *elsiocb;
2186         uint8_t *pcmd;
2187         uint16_t cmdsize;
2188         u32 local_nlp_type, elscmd;
2189
2190         /*
2191          * If we are in RSCN mode, the FC4 types supported from a
2192          * previous GFT_ID command may not be accurate. So, if we
2193          * are a NVME Initiator, always look for the possibility of
2194          * the remote NPort beng a NVME Target.
2195          */
2196         if (phba->sli_rev == LPFC_SLI_REV4 &&
2197             vport->fc_flag & FC_RSCN_MODE &&
2198             vport->nvmei_support)
2199                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2200         local_nlp_type = ndlp->nlp_fc4_type;
2201
2202         /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2203          * fields here before any of them can complete.
2204          */
2205         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2206         ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2207         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2208         ndlp->nlp_flag &= ~NLP_FIRSTBURST;
2209         ndlp->nvme_fb_size = 0;
2210
2211  send_next_prli:
2212         if (local_nlp_type & NLP_FC4_FCP) {
2213                 /* Payload is 4 + 16 = 20 x14 bytes. */
2214                 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2215                 elscmd = ELS_CMD_PRLI;
2216         } else if (local_nlp_type & NLP_FC4_NVME) {
2217                 /* Payload is 4 + 20 = 24 x18 bytes. */
2218                 cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2219                 elscmd = ELS_CMD_NVMEPRLI;
2220         } else {
2221                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2222                                  "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2223                                  ndlp->nlp_fc4_type, ndlp->nlp_DID);
2224                 return 1;
2225         }
2226
2227         /* SLI3 ports don't support NVME.  If this rport is a strict NVME
2228          * FC4 type, implicitly LOGO.
2229          */
2230         if (phba->sli_rev == LPFC_SLI_REV3 &&
2231             ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2232                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2233                                  "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2234                                  ndlp->nlp_type);
2235                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2236                 return 1;
2237         }
2238
2239         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2240                                      ndlp->nlp_DID, elscmd);
2241         if (!elsiocb)
2242                 return 1;
2243
2244         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2245
2246         /* For PRLI request, remainder of payload is service parameters */
2247         memset(pcmd, 0, cmdsize);
2248
2249         if (local_nlp_type & NLP_FC4_FCP) {
2250                 /* Remainder of payload is FCP PRLI parameter page.
2251                  * Note: this data structure is defined as
2252                  * BE/LE in the structure definition so no
2253                  * byte swap call is made.
2254                  */
2255                 *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2256                 pcmd += sizeof(uint32_t);
2257                 npr = (PRLI *)pcmd;
2258
2259                 /*
2260                  * If our firmware version is 3.20 or later,
2261                  * set the following bits for FC-TAPE support.
2262                  */
2263                 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2264                         npr->ConfmComplAllowed = 1;
2265                         npr->Retry = 1;
2266                         npr->TaskRetryIdReq = 1;
2267                 }
2268                 npr->estabImagePair = 1;
2269                 npr->readXferRdyDis = 1;
2270                 if (vport->cfg_first_burst_size)
2271                         npr->writeXferRdyDis = 1;
2272
2273                 /* For FCP support */
2274                 npr->prliType = PRLI_FCP_TYPE;
2275                 npr->initiatorFunc = 1;
2276                 elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2277
2278                 /* Remove FCP type - processed. */
2279                 local_nlp_type &= ~NLP_FC4_FCP;
2280         } else if (local_nlp_type & NLP_FC4_NVME) {
2281                 /* Remainder of payload is NVME PRLI parameter page.
2282                  * This data structure is the newer definition that
2283                  * uses bf macros so a byte swap is required.
2284                  */
2285                 *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2286                 pcmd += sizeof(uint32_t);
2287                 npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2288                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2289                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2290
2291                 /* Only initiators request first burst. */
2292                 if ((phba->cfg_nvme_enable_fb) &&
2293                     !phba->nvmet_support)
2294                         bf_set(prli_fba, npr_nvme, 1);
2295
2296                 if (phba->nvmet_support) {
2297                         bf_set(prli_tgt, npr_nvme, 1);
2298                         bf_set(prli_disc, npr_nvme, 1);
2299                 } else {
2300                         bf_set(prli_init, npr_nvme, 1);
2301                         bf_set(prli_conf, npr_nvme, 1);
2302                 }
2303
2304                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2305                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2306                 elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2307
2308                 /* Remove NVME type - processed. */
2309                 local_nlp_type &= ~NLP_FC4_NVME;
2310         }
2311
2312         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2313                 "Issue PRLI:      did:x%x",
2314                 ndlp->nlp_DID, 0, 0);
2315
2316         phba->fc_stat.elsXmitPRLI++;
2317         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2318         spin_lock_irq(shost->host_lock);
2319         ndlp->nlp_flag |= NLP_PRLI_SND;
2320
2321         /* The vport counters are used for lpfc_scan_finished, but
2322          * the ndlp is used to track outstanding PRLIs for different
2323          * FC4 types.
2324          */
2325         vport->fc_prli_sent++;
2326         ndlp->fc4_prli_sent++;
2327         spin_unlock_irq(shost->host_lock);
2328         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2329             IOCB_ERROR) {
2330                 spin_lock_irq(shost->host_lock);
2331                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2332                 spin_unlock_irq(shost->host_lock);
2333                 lpfc_els_free_iocb(phba, elsiocb);
2334                 return 1;
2335         }
2336
2337
2338         /* The driver supports 2 FC4 types.  Make sure
2339          * a PRLI is issued for all types before exiting.
2340          */
2341         if (phba->sli_rev == LPFC_SLI_REV4 &&
2342             local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2343                 goto send_next_prli;
2344
2345         return 0;
2346 }
2347
2348 /**
2349  * lpfc_rscn_disc - Perform rscn discovery for a vport
2350  * @vport: pointer to a host virtual N_Port data structure.
2351  *
2352  * This routine performs Registration State Change Notification (RSCN)
2353  * discovery for a @vport. If the @vport's node port recovery count is not
2354  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2355  * the nodes that need recovery. If none of the PLOGI were needed through
2356  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2357  * invoked to check and handle possible more RSCN came in during the period
2358  * of processing the current ones.
2359  **/
2360 static void
2361 lpfc_rscn_disc(struct lpfc_vport *vport)
2362 {
2363         lpfc_can_disctmo(vport);
2364
2365         /* RSCN discovery */
2366         /* go thru NPR nodes and issue ELS PLOGIs */
2367         if (vport->fc_npr_cnt)
2368                 if (lpfc_els_disc_plogi(vport))
2369                         return;
2370
2371         lpfc_end_rscn(vport);
2372 }
2373
2374 /**
2375  * lpfc_adisc_done - Complete the adisc phase of discovery
2376  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2377  *
2378  * This function is called when the final ADISC is completed during discovery.
2379  * This function handles clearing link attention or issuing reg_vpi depending
2380  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2381  * discovery.
2382  * This function is called with no locks held.
2383  **/
2384 static void
2385 lpfc_adisc_done(struct lpfc_vport *vport)
2386 {
2387         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2388         struct lpfc_hba   *phba = vport->phba;
2389
2390         /*
2391          * For NPIV, cmpl_reg_vpi will set port_state to READY,
2392          * and continue discovery.
2393          */
2394         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2395             !(vport->fc_flag & FC_RSCN_MODE) &&
2396             (phba->sli_rev < LPFC_SLI_REV4)) {
2397                 /* The ADISCs are complete.  Doesn't matter if they
2398                  * succeeded or failed because the ADISC completion
2399                  * routine guarantees to call the state machine and
2400                  * the RPI is either unregistered (failed ADISC response)
2401                  * or the RPI is still valid and the node is marked
2402                  * mapped for a target.  The exchanges should be in the
2403                  * correct state. This code is specific to SLI3.
2404                  */
2405                 lpfc_issue_clear_la(phba, vport);
2406                 lpfc_issue_reg_vpi(phba, vport);
2407                 return;
2408         }
2409         /*
2410         * For SLI2, we need to set port_state to READY
2411         * and continue discovery.
2412         */
2413         if (vport->port_state < LPFC_VPORT_READY) {
2414                 /* If we get here, there is nothing to ADISC */
2415                 lpfc_issue_clear_la(phba, vport);
2416                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2417                         vport->num_disc_nodes = 0;
2418                         /* go thru NPR list, issue ELS PLOGIs */
2419                         if (vport->fc_npr_cnt)
2420                                 lpfc_els_disc_plogi(vport);
2421                         if (!vport->num_disc_nodes) {
2422                                 spin_lock_irq(shost->host_lock);
2423                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2424                                 spin_unlock_irq(shost->host_lock);
2425                                 lpfc_can_disctmo(vport);
2426                                 lpfc_end_rscn(vport);
2427                         }
2428                 }
2429                 vport->port_state = LPFC_VPORT_READY;
2430         } else
2431                 lpfc_rscn_disc(vport);
2432 }
2433
2434 /**
2435  * lpfc_more_adisc - Issue more adisc as needed
2436  * @vport: pointer to a host virtual N_Port data structure.
2437  *
2438  * This routine determines whether there are more ndlps on a @vport
2439  * node list need to have Address Discover (ADISC) issued. If so, it will
2440  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2441  * remaining nodes which need to have ADISC sent.
2442  **/
2443 void
2444 lpfc_more_adisc(struct lpfc_vport *vport)
2445 {
2446         if (vport->num_disc_nodes)
2447                 vport->num_disc_nodes--;
2448         /* Continue discovery with <num_disc_nodes> ADISCs to go */
2449         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2450                          "0210 Continue discovery with %d ADISCs to go "
2451                          "Data: x%x x%x x%x\n",
2452                          vport->num_disc_nodes, vport->fc_adisc_cnt,
2453                          vport->fc_flag, vport->port_state);
2454         /* Check to see if there are more ADISCs to be sent */
2455         if (vport->fc_flag & FC_NLP_MORE) {
2456                 lpfc_set_disctmo(vport);
2457                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2458                 lpfc_els_disc_adisc(vport);
2459         }
2460         if (!vport->num_disc_nodes)
2461                 lpfc_adisc_done(vport);
2462         return;
2463 }
2464
2465 /**
2466  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2467  * @phba: pointer to lpfc hba data structure.
2468  * @cmdiocb: pointer to lpfc command iocb data structure.
2469  * @rspiocb: pointer to lpfc response iocb data structure.
2470  *
2471  * This routine is the completion function for issuing the Address Discover
2472  * (ADISC) command. It first checks to see whether link went down during
2473  * the discovery process. If so, the node will be marked as node port
2474  * recovery for issuing discover IOCB by the link attention handler and
2475  * exit. Otherwise, the response status is checked. If error was reported
2476  * in the response status, the ADISC command shall be retried by invoking
2477  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2478  * the response status, the state machine is invoked to set transition
2479  * with respect to NLP_EVT_CMPL_ADISC event.
2480  **/
2481 static void
2482 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2483                     struct lpfc_iocbq *rspiocb)
2484 {
2485         struct lpfc_vport *vport = cmdiocb->vport;
2486         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2487         IOCB_t *irsp;
2488         struct lpfc_nodelist *ndlp;
2489         int  disc;
2490
2491         /* we pass cmdiocb to state machine which needs rspiocb as well */
2492         cmdiocb->context_un.rsp_iocb = rspiocb;
2493
2494         irsp = &(rspiocb->iocb);
2495         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2496
2497         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2498                 "ADISC cmpl:      status:x%x/x%x did:x%x",
2499                 irsp->ulpStatus, irsp->un.ulpWord[4],
2500                 ndlp->nlp_DID);
2501
2502         /* Since ndlp can be freed in the disc state machine, note if this node
2503          * is being used during discovery.
2504          */
2505         spin_lock_irq(shost->host_lock);
2506         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2507         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2508         spin_unlock_irq(shost->host_lock);
2509         /* ADISC completes to NPort <nlp_DID> */
2510         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2511                          "0104 ADISC completes to NPort x%x "
2512                          "Data: x%x x%x x%x x%x x%x\n",
2513                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2514                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
2515         /* Check to see if link went down during discovery */
2516         if (lpfc_els_chk_latt(vport)) {
2517                 spin_lock_irq(shost->host_lock);
2518                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2519                 spin_unlock_irq(shost->host_lock);
2520                 goto out;
2521         }
2522
2523         if (irsp->ulpStatus) {
2524                 /* Check for retry */
2525                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2526                         /* ELS command is being retried */
2527                         if (disc) {
2528                                 spin_lock_irq(shost->host_lock);
2529                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2530                                 spin_unlock_irq(shost->host_lock);
2531                                 lpfc_set_disctmo(vport);
2532                         }
2533                         goto out;
2534                 }
2535                 /* ADISC failed */
2536                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2537                                  "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2538                                  ndlp->nlp_DID, irsp->ulpStatus,
2539                                  irsp->un.ulpWord[4]);
2540                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2541                 if (!lpfc_error_lost_link(irsp))
2542                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2543                                                 NLP_EVT_CMPL_ADISC);
2544         } else
2545                 /* Good status, call state machine */
2546                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2547                                         NLP_EVT_CMPL_ADISC);
2548
2549         /* Check to see if there are more ADISCs to be sent */
2550         if (disc && vport->num_disc_nodes)
2551                 lpfc_more_adisc(vport);
2552 out:
2553         lpfc_els_free_iocb(phba, cmdiocb);
2554         return;
2555 }
2556
2557 /**
2558  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2559  * @vport: pointer to a virtual N_Port data structure.
2560  * @ndlp: pointer to a node-list data structure.
2561  * @retry: number of retries to the command IOCB.
2562  *
2563  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2564  * @vport. It prepares the payload of the ADISC ELS command, updates the
2565  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2566  * to issue the ADISC ELS command.
2567  *
2568  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2569  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2570  * will be stored into the context1 field of the IOCB for the completion
2571  * callback function to the ADISC ELS command.
2572  *
2573  * Return code
2574  *   0 - successfully issued adisc
2575  *   1 - failed to issue adisc
2576  **/
2577 int
2578 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2579                      uint8_t retry)
2580 {
2581         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2582         struct lpfc_hba  *phba = vport->phba;
2583         ADISC *ap;
2584         struct lpfc_iocbq *elsiocb;
2585         uint8_t *pcmd;
2586         uint16_t cmdsize;
2587
2588         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2589         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2590                                      ndlp->nlp_DID, ELS_CMD_ADISC);
2591         if (!elsiocb)
2592                 return 1;
2593
2594         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2595
2596         /* For ADISC request, remainder of payload is service parameters */
2597         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2598         pcmd += sizeof(uint32_t);
2599
2600         /* Fill in ADISC payload */
2601         ap = (ADISC *) pcmd;
2602         ap->hardAL_PA = phba->fc_pref_ALPA;
2603         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2604         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2605         ap->DID = be32_to_cpu(vport->fc_myDID);
2606
2607         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2608                 "Issue ADISC:     did:x%x",
2609                 ndlp->nlp_DID, 0, 0);
2610
2611         phba->fc_stat.elsXmitADISC++;
2612         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2613         spin_lock_irq(shost->host_lock);
2614         ndlp->nlp_flag |= NLP_ADISC_SND;
2615         spin_unlock_irq(shost->host_lock);
2616         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2617             IOCB_ERROR) {
2618                 spin_lock_irq(shost->host_lock);
2619                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2620                 spin_unlock_irq(shost->host_lock);
2621                 lpfc_els_free_iocb(phba, elsiocb);
2622                 return 1;
2623         }
2624         return 0;
2625 }
2626
2627 /**
2628  * lpfc_cmpl_els_logo - Completion callback function for logo
2629  * @phba: pointer to lpfc hba data structure.
2630  * @cmdiocb: pointer to lpfc command iocb data structure.
2631  * @rspiocb: pointer to lpfc response iocb data structure.
2632  *
2633  * This routine is the completion function for issuing the ELS Logout (LOGO)
2634  * command. If no error status was reported from the LOGO response, the
2635  * state machine of the associated ndlp shall be invoked for transition with
2636  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2637  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2638  **/
2639 static void
2640 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2641                    struct lpfc_iocbq *rspiocb)
2642 {
2643         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2644         struct lpfc_vport *vport = ndlp->vport;
2645         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2646         IOCB_t *irsp;
2647         struct lpfcMboxq *mbox;
2648         unsigned long flags;
2649         uint32_t skip_recovery = 0;
2650
2651         /* we pass cmdiocb to state machine which needs rspiocb as well */
2652         cmdiocb->context_un.rsp_iocb = rspiocb;
2653
2654         irsp = &(rspiocb->iocb);
2655         spin_lock_irq(shost->host_lock);
2656         ndlp->nlp_flag &= ~NLP_LOGO_SND;
2657         spin_unlock_irq(shost->host_lock);
2658
2659         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2660                 "LOGO cmpl:       status:x%x/x%x did:x%x",
2661                 irsp->ulpStatus, irsp->un.ulpWord[4],
2662                 ndlp->nlp_DID);
2663
2664         /* LOGO completes to NPort <nlp_DID> */
2665         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2666                          "0105 LOGO completes to NPort x%x "
2667                          "Data: x%x x%x x%x x%x\n",
2668                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2669                          irsp->ulpTimeout, vport->num_disc_nodes);
2670
2671         if (lpfc_els_chk_latt(vport)) {
2672                 skip_recovery = 1;
2673                 goto out;
2674         }
2675
2676         /* Check to see if link went down during discovery */
2677         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2678                 /* NLP_EVT_DEVICE_RM should unregister the RPI
2679                  * which should abort all outstanding IOs.
2680                  */
2681                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2682                                         NLP_EVT_DEVICE_RM);
2683                 skip_recovery = 1;
2684                 goto out;
2685         }
2686
2687         /* The LOGO will not be retried on failure.  A LOGO was
2688          * issued to the remote rport and a ACC or RJT or no Answer are
2689          * all acceptable.  Note the failure and move forward with
2690          * discovery.  The PLOGI will retry.
2691          */
2692         if (irsp->ulpStatus) {
2693                 /* LOGO failed */
2694                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2695                                  "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
2696                                  ndlp->nlp_DID, irsp->ulpStatus,
2697                                  irsp->un.ulpWord[4]);
2698                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2699                 if (lpfc_error_lost_link(irsp)) {
2700                         skip_recovery = 1;
2701                         goto out;
2702                 }
2703         }
2704
2705         /* Call state machine. This will unregister the rpi if needed. */
2706         lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2707
2708 out:
2709         lpfc_els_free_iocb(phba, cmdiocb);
2710         /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2711         if ((vport->fc_flag & FC_PT2PT) &&
2712                 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2713                 phba->pport->fc_myDID = 0;
2714
2715                 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2716                     (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
2717                         if (phba->nvmet_support)
2718                                 lpfc_nvmet_update_targetport(phba);
2719                         else
2720                                 lpfc_nvme_update_localport(phba->pport);
2721                 }
2722
2723                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2724                 if (mbox) {
2725                         lpfc_config_link(phba, mbox);
2726                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2727                         mbox->vport = vport;
2728                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2729                                 MBX_NOT_FINISHED) {
2730                                 mempool_free(mbox, phba->mbox_mem_pool);
2731                                 skip_recovery = 1;
2732                         }
2733                 }
2734         }
2735
2736         /*
2737          * If the node is a target, the handling attempts to recover the port.
2738          * For any other port type, the rpi is unregistered as an implicit
2739          * LOGO.
2740          */
2741         if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2742             skip_recovery == 0) {
2743                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2744                 spin_lock_irqsave(shost->host_lock, flags);
2745                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2746                 spin_unlock_irqrestore(shost->host_lock, flags);
2747
2748                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2749                                  "3187 LOGO completes to NPort x%x: Start "
2750                                  "Recovery Data: x%x x%x x%x x%x\n",
2751                                  ndlp->nlp_DID, irsp->ulpStatus,
2752                                  irsp->un.ulpWord[4], irsp->ulpTimeout,
2753                                  vport->num_disc_nodes);
2754                 lpfc_disc_start(vport);
2755         }
2756         return;
2757 }
2758
2759 /**
2760  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2761  * @vport: pointer to a virtual N_Port data structure.
2762  * @ndlp: pointer to a node-list data structure.
2763  * @retry: number of retries to the command IOCB.
2764  *
2765  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2766  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2767  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2768  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2769  *
2770  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2771  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2772  * will be stored into the context1 field of the IOCB for the completion
2773  * callback function to the LOGO ELS command.
2774  *
2775  * Callers of this routine are expected to unregister the RPI first
2776  *
2777  * Return code
2778  *   0 - successfully issued logo
2779  *   1 - failed to issue logo
2780  **/
2781 int
2782 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2783                     uint8_t retry)
2784 {
2785         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2786         struct lpfc_hba  *phba = vport->phba;
2787         struct lpfc_iocbq *elsiocb;
2788         uint8_t *pcmd;
2789         uint16_t cmdsize;
2790         int rc;
2791
2792         spin_lock_irq(shost->host_lock);
2793         if (ndlp->nlp_flag & NLP_LOGO_SND) {
2794                 spin_unlock_irq(shost->host_lock);
2795                 return 0;
2796         }
2797         spin_unlock_irq(shost->host_lock);
2798
2799         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2800         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2801                                      ndlp->nlp_DID, ELS_CMD_LOGO);
2802         if (!elsiocb)
2803                 return 1;
2804
2805         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2806         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2807         pcmd += sizeof(uint32_t);
2808
2809         /* Fill in LOGO payload */
2810         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2811         pcmd += sizeof(uint32_t);
2812         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2813
2814         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2815                 "Issue LOGO:      did:x%x",
2816                 ndlp->nlp_DID, 0, 0);
2817
2818         phba->fc_stat.elsXmitLOGO++;
2819         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2820         spin_lock_irq(shost->host_lock);
2821         ndlp->nlp_flag |= NLP_LOGO_SND;
2822         ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2823         spin_unlock_irq(shost->host_lock);
2824         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2825         if (rc == IOCB_ERROR) {
2826                 spin_lock_irq(shost->host_lock);
2827                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2828                 spin_unlock_irq(shost->host_lock);
2829                 lpfc_els_free_iocb(phba, elsiocb);
2830                 return 1;
2831         }
2832
2833         spin_lock_irq(shost->host_lock);
2834         ndlp->nlp_prev_state = ndlp->nlp_state;
2835         spin_unlock_irq(shost->host_lock);
2836         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
2837         return 0;
2838 }
2839
2840 /**
2841  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
2842  * @phba: pointer to lpfc hba data structure.
2843  * @cmdiocb: pointer to lpfc command iocb data structure.
2844  * @rspiocb: pointer to lpfc response iocb data structure.
2845  *
2846  * This routine is a generic completion callback function for ELS commands.
2847  * Specifically, it is the callback function which does not need to perform
2848  * any command specific operations. It is currently used by the ELS command
2849  * issuing routines for the ELS State Change  Request (SCR),
2850  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2851  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2852  * certain debug loggings, this callback function simply invokes the
2853  * lpfc_els_chk_latt() routine to check whether link went down during the
2854  * discovery process.
2855  **/
2856 static void
2857 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2858                   struct lpfc_iocbq *rspiocb)
2859 {
2860         struct lpfc_vport *vport = cmdiocb->vport;
2861         IOCB_t *irsp;
2862
2863         irsp = &rspiocb->iocb;
2864
2865         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2866                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2867                 irsp->ulpStatus, irsp->un.ulpWord[4],
2868                 irsp->un.elsreq64.remoteID);
2869         /* ELS cmd tag <ulpIoTag> completes */
2870         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2871                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2872                          irsp->ulpIoTag, irsp->ulpStatus,
2873                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2874         /* Check to see if link went down during discovery */
2875         lpfc_els_chk_latt(vport);
2876         lpfc_els_free_iocb(phba, cmdiocb);
2877         return;
2878 }
2879
2880 /**
2881  * lpfc_issue_els_scr - Issue a scr to an node on a vport
2882  * @vport: pointer to a host virtual N_Port data structure.
2883  * @nportid: N_Port identifier to the remote node.
2884  * @retry: number of retries to the command IOCB.
2885  *
2886  * This routine issues a State Change Request (SCR) to a fabric node
2887  * on a @vport. The remote node @nportid is passed into the function. It
2888  * first search the @vport node list to find the matching ndlp. If no such
2889  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2890  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2891  * routine is invoked to send the SCR IOCB.
2892  *
2893  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2894  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2895  * will be stored into the context1 field of the IOCB for the completion
2896  * callback function to the SCR ELS command.
2897  *
2898  * Return code
2899  *   0 - Successfully issued scr command
2900  *   1 - Failed to issue scr command
2901  **/
2902 int
2903 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2904 {
2905         struct lpfc_hba  *phba = vport->phba;
2906         struct lpfc_iocbq *elsiocb;
2907         uint8_t *pcmd;
2908         uint16_t cmdsize;
2909         struct lpfc_nodelist *ndlp;
2910
2911         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2912
2913         ndlp = lpfc_findnode_did(vport, nportid);
2914         if (!ndlp) {
2915                 ndlp = lpfc_nlp_init(vport, nportid);
2916                 if (!ndlp)
2917                         return 1;
2918                 lpfc_enqueue_node(vport, ndlp);
2919         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2920                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2921                 if (!ndlp)
2922                         return 1;
2923         }
2924
2925         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2926                                      ndlp->nlp_DID, ELS_CMD_SCR);
2927
2928         if (!elsiocb) {
2929                 /* This will trigger the release of the node just
2930                  * allocated
2931                  */
2932                 lpfc_nlp_put(ndlp);
2933                 return 1;
2934         }
2935
2936         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2937
2938         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2939         pcmd += sizeof(uint32_t);
2940
2941         /* For SCR, remainder of payload is SCR parameter page */
2942         memset(pcmd, 0, sizeof(SCR));
2943         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2944
2945         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2946                 "Issue SCR:       did:x%x",
2947                 ndlp->nlp_DID, 0, 0);
2948
2949         phba->fc_stat.elsXmitSCR++;
2950         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2951         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2952             IOCB_ERROR) {
2953                 /* The additional lpfc_nlp_put will cause the following
2954                  * lpfc_els_free_iocb routine to trigger the rlease of
2955                  * the node.
2956                  */
2957                 lpfc_nlp_put(ndlp);
2958                 lpfc_els_free_iocb(phba, elsiocb);
2959                 return 1;
2960         }
2961         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2962          * trigger the release of node.
2963          */
2964         if (!(vport->fc_flag & FC_PT2PT))
2965                 lpfc_nlp_put(ndlp);
2966         return 0;
2967 }
2968
2969 /**
2970  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2971  * @vport: pointer to a host virtual N_Port data structure.
2972  * @nportid: N_Port identifier to the remote node.
2973  * @retry: number of retries to the command IOCB.
2974  *
2975  * This routine issues a Fibre Channel Address Resolution Response
2976  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2977  * is passed into the function. It first search the @vport node list to find
2978  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2979  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2980  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2981  *
2982  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2983  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2984  * will be stored into the context1 field of the IOCB for the completion
2985  * callback function to the PARPR ELS command.
2986  *
2987  * Return code
2988  *   0 - Successfully issued farpr command
2989  *   1 - Failed to issue farpr command
2990  **/
2991 static int
2992 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2993 {
2994         struct lpfc_hba  *phba = vport->phba;
2995         struct lpfc_iocbq *elsiocb;
2996         FARP *fp;
2997         uint8_t *pcmd;
2998         uint32_t *lp;
2999         uint16_t cmdsize;
3000         struct lpfc_nodelist *ondlp;
3001         struct lpfc_nodelist *ndlp;
3002
3003         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3004
3005         ndlp = lpfc_findnode_did(vport, nportid);
3006         if (!ndlp) {
3007                 ndlp = lpfc_nlp_init(vport, nportid);
3008                 if (!ndlp)
3009                         return 1;
3010                 lpfc_enqueue_node(vport, ndlp);
3011         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3012                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3013                 if (!ndlp)
3014                         return 1;
3015         }
3016
3017         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3018                                      ndlp->nlp_DID, ELS_CMD_RNID);
3019         if (!elsiocb) {
3020                 /* This will trigger the release of the node just
3021                  * allocated
3022                  */
3023                 lpfc_nlp_put(ndlp);
3024                 return 1;
3025         }
3026
3027         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3028
3029         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3030         pcmd += sizeof(uint32_t);
3031
3032         /* Fill in FARPR payload */
3033         fp = (FARP *) (pcmd);
3034         memset(fp, 0, sizeof(FARP));
3035         lp = (uint32_t *) pcmd;
3036         *lp++ = be32_to_cpu(nportid);
3037         *lp++ = be32_to_cpu(vport->fc_myDID);
3038         fp->Rflags = 0;
3039         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3040
3041         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3042         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3043         ondlp = lpfc_findnode_did(vport, nportid);
3044         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
3045                 memcpy(&fp->OportName, &ondlp->nlp_portname,
3046                        sizeof(struct lpfc_name));
3047                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3048                        sizeof(struct lpfc_name));
3049         }
3050
3051         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3052                 "Issue FARPR:     did:x%x",
3053                 ndlp->nlp_DID, 0, 0);
3054
3055         phba->fc_stat.elsXmitFARPR++;
3056         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3057         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3058             IOCB_ERROR) {
3059                 /* The additional lpfc_nlp_put will cause the following
3060                  * lpfc_els_free_iocb routine to trigger the release of
3061                  * the node.
3062                  */
3063                 lpfc_nlp_put(ndlp);
3064                 lpfc_els_free_iocb(phba, elsiocb);
3065                 return 1;
3066         }
3067         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3068          * trigger the release of the node.
3069          */
3070         lpfc_nlp_put(ndlp);
3071         return 0;
3072 }
3073
3074 /**
3075  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
3076  * @vport: pointer to a host virtual N_Port data structure.
3077  * @nlp: pointer to a node-list data structure.
3078  *
3079  * This routine cancels the timer with a delayed IOCB-command retry for
3080  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
3081  * removes the ELS retry event if it presents. In addition, if the
3082  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
3083  * commands are sent for the @vport's nodes that require issuing discovery
3084  * ADISC.
3085  **/
3086 void
3087 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
3088 {
3089         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3090         struct lpfc_work_evt *evtp;
3091
3092         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
3093                 return;
3094         spin_lock_irq(shost->host_lock);
3095         nlp->nlp_flag &= ~NLP_DELAY_TMO;
3096         spin_unlock_irq(shost->host_lock);
3097         del_timer_sync(&nlp->nlp_delayfunc);
3098         nlp->nlp_last_elscmd = 0;
3099         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
3100                 list_del_init(&nlp->els_retry_evt.evt_listp);
3101                 /* Decrement nlp reference count held for the delayed retry */
3102                 evtp = &nlp->els_retry_evt;
3103                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
3104         }
3105         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
3106                 spin_lock_irq(shost->host_lock);
3107                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3108                 spin_unlock_irq(shost->host_lock);
3109                 if (vport->num_disc_nodes) {
3110                         if (vport->port_state < LPFC_VPORT_READY) {
3111                                 /* Check if there are more ADISCs to be sent */
3112                                 lpfc_more_adisc(vport);
3113                         } else {
3114                                 /* Check if there are more PLOGIs to be sent */
3115                                 lpfc_more_plogi(vport);
3116                                 if (vport->num_disc_nodes == 0) {
3117                                         spin_lock_irq(shost->host_lock);
3118                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
3119                                         spin_unlock_irq(shost->host_lock);
3120                                         lpfc_can_disctmo(vport);
3121                                         lpfc_end_rscn(vport);
3122                                 }
3123                         }
3124                 }
3125         }
3126         return;
3127 }
3128
3129 /**
3130  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
3131  * @ptr: holder for the pointer to the timer function associated data (ndlp).
3132  *
3133  * This routine is invoked by the ndlp delayed-function timer to check
3134  * whether there is any pending ELS retry event(s) with the node. If not, it
3135  * simply returns. Otherwise, if there is at least one ELS delayed event, it
3136  * adds the delayed events to the HBA work list and invokes the
3137  * lpfc_worker_wake_up() routine to wake up worker thread to process the
3138  * event. Note that lpfc_nlp_get() is called before posting the event to
3139  * the work list to hold reference count of ndlp so that it guarantees the
3140  * reference to ndlp will still be available when the worker thread gets
3141  * to the event associated with the ndlp.
3142  **/
3143 void
3144 lpfc_els_retry_delay(struct timer_list *t)
3145 {
3146         struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
3147         struct lpfc_vport *vport = ndlp->vport;
3148         struct lpfc_hba   *phba = vport->phba;
3149         unsigned long flags;
3150         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
3151
3152         spin_lock_irqsave(&phba->hbalock, flags);
3153         if (!list_empty(&evtp->evt_listp)) {
3154                 spin_unlock_irqrestore(&phba->hbalock, flags);
3155                 return;
3156         }
3157
3158         /* We need to hold the node by incrementing the reference
3159          * count until the queued work is done
3160          */
3161         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
3162         if (evtp->evt_arg1) {
3163                 evtp->evt = LPFC_EVT_ELS_RETRY;
3164                 list_add_tail(&evtp->evt_listp, &phba->work_list);
3165                 lpfc_worker_wake_up(phba);
3166         }
3167         spin_unlock_irqrestore(&phba->hbalock, flags);
3168         return;
3169 }
3170
3171 /**
3172  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
3173  * @ndlp: pointer to a node-list data structure.
3174  *
3175  * This routine is the worker-thread handler for processing the @ndlp delayed
3176  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3177  * the last ELS command from the associated ndlp and invokes the proper ELS
3178  * function according to the delayed ELS command to retry the command.
3179  **/
3180 void
3181 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3182 {
3183         struct lpfc_vport *vport = ndlp->vport;
3184         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3185         uint32_t cmd, retry;
3186
3187         spin_lock_irq(shost->host_lock);
3188         cmd = ndlp->nlp_last_elscmd;
3189         ndlp->nlp_last_elscmd = 0;
3190
3191         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
3192                 spin_unlock_irq(shost->host_lock);
3193                 return;
3194         }
3195
3196         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3197         spin_unlock_irq(shost->host_lock);
3198         /*
3199          * If a discovery event readded nlp_delayfunc after timer
3200          * firing and before processing the timer, cancel the
3201          * nlp_delayfunc.
3202          */
3203         del_timer_sync(&ndlp->nlp_delayfunc);
3204         retry = ndlp->nlp_retry;
3205         ndlp->nlp_retry = 0;
3206
3207         switch (cmd) {
3208         case ELS_CMD_FLOGI:
3209                 lpfc_issue_els_flogi(vport, ndlp, retry);
3210                 break;
3211         case ELS_CMD_PLOGI:
3212                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
3213                         ndlp->nlp_prev_state = ndlp->nlp_state;
3214                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3215                 }
3216                 break;
3217         case ELS_CMD_ADISC:
3218                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
3219                         ndlp->nlp_prev_state = ndlp->nlp_state;
3220                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3221                 }
3222                 break;
3223         case ELS_CMD_PRLI:
3224         case ELS_CMD_NVMEPRLI:
3225                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
3226                         ndlp->nlp_prev_state = ndlp->nlp_state;
3227                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3228                 }
3229                 break;
3230         case ELS_CMD_LOGO:
3231                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
3232                         ndlp->nlp_prev_state = ndlp->nlp_state;
3233                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3234                 }
3235                 break;
3236         case ELS_CMD_FDISC:
3237                 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3238                         lpfc_issue_els_fdisc(vport, ndlp, retry);
3239                 break;
3240         }
3241         return;
3242 }
3243
3244 /**
3245  * lpfc_link_reset - Issue link reset
3246  * @vport: pointer to a virtual N_Port data structure.
3247  *
3248  * This routine performs link reset by sending INIT_LINK mailbox command.
3249  * For SLI-3 adapter, link attention interrupt is enabled before issuing
3250  * INIT_LINK mailbox command.
3251  *
3252  * Return code
3253  *   0 - Link reset initiated successfully
3254  *   1 - Failed to initiate link reset
3255  **/
3256 int
3257 lpfc_link_reset(struct lpfc_vport *vport)
3258 {
3259         struct lpfc_hba *phba = vport->phba;
3260         LPFC_MBOXQ_t *mbox;
3261         uint32_t control;
3262         int rc;
3263
3264         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3265                          "2851 Attempt link reset\n");
3266         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3267         if (!mbox) {
3268                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3269                                 "2852 Failed to allocate mbox memory");
3270                 return 1;
3271         }
3272
3273         /* Enable Link attention interrupts */
3274         if (phba->sli_rev <= LPFC_SLI_REV3) {
3275                 spin_lock_irq(&phba->hbalock);
3276                 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3277                 control = readl(phba->HCregaddr);
3278                 control |= HC_LAINT_ENA;
3279                 writel(control, phba->HCregaddr);
3280                 readl(phba->HCregaddr); /* flush */
3281                 spin_unlock_irq(&phba->hbalock);
3282         }
3283
3284         lpfc_init_link(phba, mbox, phba->cfg_topology,
3285                        phba->cfg_link_speed);
3286         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3287         mbox->vport = vport;
3288         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3289         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
3290                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3291                                 "2853 Failed to issue INIT_LINK "
3292                                 "mbox command, rc:x%x\n", rc);
3293                 mempool_free(mbox, phba->mbox_mem_pool);
3294                 return 1;
3295         }
3296
3297         return 0;
3298 }
3299
3300 /**
3301  * lpfc_els_retry - Make retry decision on an els command iocb
3302  * @phba: pointer to lpfc hba data structure.
3303  * @cmdiocb: pointer to lpfc command iocb data structure.
3304  * @rspiocb: pointer to lpfc response iocb data structure.
3305  *
3306  * This routine makes a retry decision on an ELS command IOCB, which has
3307  * failed. The following ELS IOCBs use this function for retrying the command
3308  * when previously issued command responsed with error status: FLOGI, PLOGI,
3309  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3310  * returned error status, it makes the decision whether a retry shall be
3311  * issued for the command, and whether a retry shall be made immediately or
3312  * delayed. In the former case, the corresponding ELS command issuing-function
3313  * is called to retry the command. In the later case, the ELS command shall
3314  * be posted to the ndlp delayed event and delayed function timer set to the
3315  * ndlp for the delayed command issusing.
3316  *
3317  * Return code
3318  *   0 - No retry of els command is made
3319  *   1 - Immediate or delayed retry of els command is made
3320  **/
3321 static int
3322 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3323                struct lpfc_iocbq *rspiocb)
3324 {
3325         struct lpfc_vport *vport = cmdiocb->vport;
3326         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3327         IOCB_t *irsp = &rspiocb->iocb;
3328         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3329         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3330         uint32_t *elscmd;
3331         struct ls_rjt stat;
3332         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
3333         int logerr = 0;
3334         uint32_t cmd = 0;
3335         uint32_t did;
3336         int link_reset = 0, rc;
3337
3338
3339         /* Note: context2 may be 0 for internal driver abort
3340          * of delays ELS command.
3341          */
3342
3343         if (pcmd && pcmd->virt) {
3344                 elscmd = (uint32_t *) (pcmd->virt);
3345                 cmd = *elscmd++;
3346         }
3347
3348         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3349                 did = ndlp->nlp_DID;
3350         else {
3351                 /* We should only hit this case for retrying PLOGI */
3352                 did = irsp->un.elsreq64.remoteID;
3353                 ndlp = lpfc_findnode_did(vport, did);
3354                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3355                     && (cmd != ELS_CMD_PLOGI))
3356                         return 1;
3357         }
3358
3359         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3360                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
3361                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3362
3363         switch (irsp->ulpStatus) {
3364         case IOSTAT_FCP_RSP_ERROR:
3365                 break;
3366         case IOSTAT_REMOTE_STOP:
3367                 if (phba->sli_rev == LPFC_SLI_REV4) {
3368                         /* This IO was aborted by the target, we don't
3369                          * know the rxid and because we did not send the
3370                          * ABTS we cannot generate and RRQ.
3371                          */
3372                         lpfc_set_rrq_active(phba, ndlp,
3373                                          cmdiocb->sli4_lxritag, 0, 0);
3374                 }
3375                 break;
3376         case IOSTAT_LOCAL_REJECT:
3377                 switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
3378                 case IOERR_LOOP_OPEN_FAILURE:
3379                         if (cmd == ELS_CMD_FLOGI) {
3380                                 if (PCI_DEVICE_ID_HORNET ==
3381                                         phba->pcidev->device) {
3382                                         phba->fc_topology = LPFC_TOPOLOGY_LOOP;
3383                                         phba->pport->fc_myDID = 0;
3384                                         phba->alpa_map[0] = 0;
3385                                         phba->alpa_map[1] = 0;
3386                                 }
3387                         }
3388                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
3389                                 delay = 1000;
3390                         retry = 1;
3391                         break;
3392
3393                 case IOERR_ILLEGAL_COMMAND:
3394                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3395                                          "0124 Retry illegal cmd x%x "
3396                                          "retry:x%x delay:x%x\n",
3397                                          cmd, cmdiocb->retry, delay);
3398                         retry = 1;
3399                         /* All command's retry policy */
3400                         maxretry = 8;
3401                         if (cmdiocb->retry > 2)
3402                                 delay = 1000;
3403                         break;
3404
3405                 case IOERR_NO_RESOURCES:
3406                         logerr = 1; /* HBA out of resources */
3407                         retry = 1;
3408                         if (cmdiocb->retry > 100)
3409                                 delay = 100;
3410                         maxretry = 250;
3411                         break;
3412
3413                 case IOERR_ILLEGAL_FRAME:
3414                         delay = 100;
3415                         retry = 1;
3416                         break;
3417
3418                 case IOERR_INVALID_RPI:
3419                         if (cmd == ELS_CMD_PLOGI &&
3420                             did == NameServer_DID) {
3421                                 /* Continue forever if plogi to */
3422                                 /* the nameserver fails */
3423                                 maxretry = 0;
3424                                 delay = 100;
3425                         }
3426                         retry = 1;
3427                         break;
3428
3429                 case IOERR_SEQUENCE_TIMEOUT:
3430                         if (cmd == ELS_CMD_PLOGI &&
3431                             did == NameServer_DID &&
3432                             (cmdiocb->retry + 1) == maxretry) {
3433                                 /* Reset the Link */
3434                                 link_reset = 1;
3435                                 break;
3436                         }
3437                         retry = 1;
3438                         delay = 100;
3439                         break;
3440                 }
3441                 break;
3442
3443         case IOSTAT_NPORT_RJT:
3444         case IOSTAT_FABRIC_RJT:
3445                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3446                         retry = 1;
3447                         break;
3448                 }
3449                 break;
3450
3451         case IOSTAT_NPORT_BSY:
3452         case IOSTAT_FABRIC_BSY:
3453                 logerr = 1; /* Fabric / Remote NPort out of resources */
3454                 retry = 1;
3455                 break;
3456
3457         case IOSTAT_LS_RJT:
3458                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3459                 /* Added for Vendor specifc support
3460                  * Just keep retrying for these Rsn / Exp codes
3461                  */
3462                 switch (stat.un.b.lsRjtRsnCode) {
3463                 case LSRJT_UNABLE_TPC:
3464                         /* The driver has a VALID PLOGI but the rport has
3465                          * rejected the PRLI - can't do it now.  Delay
3466                          * for 1 second and try again - don't care about
3467                          * the explanation.
3468                          */
3469                         if (cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) {
3470                                 delay = 1000;
3471                                 maxretry = lpfc_max_els_tries + 1;
3472                                 retry = 1;
3473                                 break;
3474                         }
3475
3476                         /* Legacy bug fix code for targets with PLOGI delays. */
3477                         if (stat.un.b.lsRjtRsnCodeExp ==
3478                             LSEXP_CMD_IN_PROGRESS) {
3479                                 if (cmd == ELS_CMD_PLOGI) {
3480                                         delay = 1000;
3481                                         maxretry = 48;
3482                                 }
3483                                 retry = 1;
3484                                 break;
3485                         }
3486                         if (stat.un.b.lsRjtRsnCodeExp ==
3487                             LSEXP_CANT_GIVE_DATA) {
3488                                 if (cmd == ELS_CMD_PLOGI) {
3489                                         delay = 1000;
3490                                         maxretry = 48;
3491                                 }
3492                                 retry = 1;
3493                                 break;
3494                         }
3495                         if (cmd == ELS_CMD_PLOGI) {
3496                                 delay = 1000;
3497                                 maxretry = lpfc_max_els_tries + 1;
3498                                 retry = 1;
3499                                 break;
3500                         }
3501                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3502                           (cmd == ELS_CMD_FDISC) &&
3503                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
3504                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3505                                                  "0125 FDISC Failed (x%x). "
3506                                                  "Fabric out of resources\n",
3507                                                  stat.un.lsRjtError);
3508                                 lpfc_vport_set_state(vport,
3509                                                      FC_VPORT_NO_FABRIC_RSCS);
3510                         }
3511                         break;
3512
3513                 case LSRJT_LOGICAL_BSY:
3514                         if ((cmd == ELS_CMD_PLOGI) ||
3515                             (cmd == ELS_CMD_PRLI) ||
3516                             (cmd == ELS_CMD_NVMEPRLI)) {
3517                                 delay = 1000;
3518                                 maxretry = 48;
3519                         } else if (cmd == ELS_CMD_FDISC) {
3520                                 /* FDISC retry policy */
3521                                 maxretry = 48;
3522                                 if (cmdiocb->retry >= 32)
3523                                         delay = 1000;
3524                         }
3525                         retry = 1;
3526                         break;
3527
3528                 case LSRJT_LOGICAL_ERR:
3529                         /* There are some cases where switches return this
3530                          * error when they are not ready and should be returning
3531                          * Logical Busy. We should delay every time.
3532                          */
3533                         if (cmd == ELS_CMD_FDISC &&
3534                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3535                                 maxretry = 3;
3536                                 delay = 1000;
3537                                 retry = 1;
3538                         } else if (cmd == ELS_CMD_FLOGI &&
3539                                    stat.un.b.lsRjtRsnCodeExp ==
3540                                                 LSEXP_NOTHING_MORE) {
3541                                 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
3542                                 retry = 1;
3543                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3544                                                  "0820 FLOGI Failed (x%x). "
3545                                                  "BBCredit Not Supported\n",
3546                                                  stat.un.lsRjtError);
3547                         }
3548                         break;
3549
3550                 case LSRJT_PROTOCOL_ERR:
3551                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3552                           (cmd == ELS_CMD_FDISC) &&
3553                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3554                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3555                           ) {
3556                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3557                                                  "0122 FDISC Failed (x%x). "
3558                                                  "Fabric Detected Bad WWN\n",
3559                                                  stat.un.lsRjtError);
3560                                 lpfc_vport_set_state(vport,
3561                                                      FC_VPORT_FABRIC_REJ_WWN);
3562                         }
3563                         break;
3564                 case LSRJT_VENDOR_UNIQUE:
3565                         if ((stat.un.b.vendorUnique == 0x45) &&
3566                             (cmd == ELS_CMD_FLOGI)) {
3567                                 goto out_retry;
3568                         }
3569                         break;
3570                 case LSRJT_CMD_UNSUPPORTED:
3571                         /* lpfc nvmet returns this type of LS_RJT when it
3572                          * receives an FCP PRLI because lpfc nvmet only
3573                          * support NVME.  ELS request is terminated for FCP4
3574                          * on this rport.
3575                          */
3576                         if (stat.un.b.lsRjtRsnCodeExp ==
3577                             LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
3578                                 spin_lock_irq(shost->host_lock);
3579                                 ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
3580                                 spin_unlock_irq(shost->host_lock);
3581                                 retry = 0;
3582                                 goto out_retry;
3583                         }
3584                         break;
3585                 }
3586                 break;
3587
3588         case IOSTAT_INTERMED_RSP:
3589         case IOSTAT_BA_RJT:
3590                 break;
3591
3592         default:
3593                 break;
3594         }
3595
3596         if (link_reset) {
3597                 rc = lpfc_link_reset(vport);
3598                 if (rc) {
3599                         /* Do not give up. Retry PLOGI one more time and attempt
3600                          * link reset if PLOGI fails again.
3601                          */
3602                         retry = 1;
3603                         delay = 100;
3604                         goto out_retry;
3605                 }
3606                 return 1;
3607         }
3608
3609         if (did == FDMI_DID)
3610                 retry = 1;
3611
3612         if ((cmd == ELS_CMD_FLOGI) &&
3613             (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
3614             !lpfc_error_lost_link(irsp)) {
3615                 /* FLOGI retry policy */
3616                 retry = 1;
3617                 /* retry FLOGI forever */
3618                 if (phba->link_flag != LS_LOOPBACK_MODE)
3619                         maxretry = 0;
3620                 else
3621                         maxretry = 2;
3622
3623                 if (cmdiocb->retry >= 100)
3624                         delay = 5000;
3625                 else if (cmdiocb->retry >= 32)
3626                         delay = 1000;
3627         } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
3628                 /* retry FDISCs every second up to devloss */
3629                 retry = 1;
3630                 maxretry = vport->cfg_devloss_tmo;
3631                 delay = 1000;
3632         }
3633
3634         cmdiocb->retry++;
3635         if (maxretry && (cmdiocb->retry >= maxretry)) {
3636                 phba->fc_stat.elsRetryExceeded++;
3637                 retry = 0;
3638         }
3639
3640         if ((vport->load_flag & FC_UNLOADING) != 0)
3641                 retry = 0;
3642
3643 out_retry:
3644         if (retry) {
3645                 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3646                         /* Stop retrying PLOGI and FDISC if in FCF discovery */
3647                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3648                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3649                                                  "2849 Stop retry ELS command "
3650                                                  "x%x to remote NPORT x%x, "
3651                                                  "Data: x%x x%x\n", cmd, did,
3652                                                  cmdiocb->retry, delay);
3653                                 return 0;
3654                         }
3655                 }
3656
3657                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
3658                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3659                                  "0107 Retry ELS command x%x to remote "
3660                                  "NPORT x%x Data: x%x x%x\n",
3661                                  cmd, did, cmdiocb->retry, delay);
3662
3663                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3664                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
3665                         ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
3666                         IOERR_NO_RESOURCES))) {
3667                         /* Don't reset timer for no resources */
3668
3669                         /* If discovery / RSCN timer is running, reset it */
3670                         if (timer_pending(&vport->fc_disctmo) ||
3671                             (vport->fc_flag & FC_RSCN_MODE))
3672                                 lpfc_set_disctmo(vport);
3673                 }
3674
3675                 phba->fc_stat.elsXmitRetry++;
3676                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
3677                         phba->fc_stat.elsDelayRetry++;
3678                         ndlp->nlp_retry = cmdiocb->retry;
3679
3680                         /* delay is specified in milliseconds */
3681                         mod_timer(&ndlp->nlp_delayfunc,
3682                                 jiffies + msecs_to_jiffies(delay));
3683                         spin_lock_irq(shost->host_lock);
3684                         ndlp->nlp_flag |= NLP_DELAY_TMO;
3685                         spin_unlock_irq(shost->host_lock);
3686
3687                         ndlp->nlp_prev_state = ndlp->nlp_state;
3688                         if ((cmd == ELS_CMD_PRLI) ||
3689                             (cmd == ELS_CMD_NVMEPRLI))
3690                                 lpfc_nlp_set_state(vport, ndlp,
3691                                         NLP_STE_PRLI_ISSUE);
3692                         else
3693                                 lpfc_nlp_set_state(vport, ndlp,
3694                                         NLP_STE_NPR_NODE);
3695                         ndlp->nlp_last_elscmd = cmd;
3696
3697                         return 1;
3698                 }
3699                 switch (cmd) {
3700                 case ELS_CMD_FLOGI:
3701                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
3702                         return 1;
3703                 case ELS_CMD_FDISC:
3704                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3705                         return 1;
3706                 case ELS_CMD_PLOGI:
3707                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3708                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3709                                 lpfc_nlp_set_state(vport, ndlp,
3710                                                    NLP_STE_PLOGI_ISSUE);
3711                         }
3712                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
3713                         return 1;
3714                 case ELS_CMD_ADISC:
3715                         ndlp->nlp_prev_state = ndlp->nlp_state;
3716                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3717                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
3718                         return 1;
3719                 case ELS_CMD_PRLI:
3720                 case ELS_CMD_NVMEPRLI:
3721                         ndlp->nlp_prev_state = ndlp->nlp_state;
3722                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3723                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
3724                         return 1;
3725                 case ELS_CMD_LOGO:
3726                         ndlp->nlp_prev_state = ndlp->nlp_state;
3727                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3728                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
3729                         return 1;
3730                 }
3731         }
3732         /* No retry ELS command <elsCmd> to remote NPORT <did> */
3733         if (logerr) {
3734                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3735                          "0137 No retry ELS command x%x to remote "
3736                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
3737                          cmd, did, irsp->ulpStatus,
3738                          irsp->un.ulpWord[4]);
3739         }
3740         else {
3741                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3742                          "0108 No retry ELS command x%x to remote "
3743                          "NPORT x%x Retried:%d Error:x%x/%x\n",
3744                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
3745                          irsp->un.ulpWord[4]);
3746         }
3747         return 0;
3748 }
3749
3750 /**
3751  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
3752  * @phba: pointer to lpfc hba data structure.
3753  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3754  *
3755  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3756  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3757  * checks to see whether there is a lpfc DMA buffer associated with the
3758  * response of the command IOCB. If so, it will be released before releasing
3759  * the lpfc DMA buffer associated with the IOCB itself.
3760  *
3761  * Return code
3762  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3763  **/
3764 static int
3765 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3766 {
3767         struct lpfc_dmabuf *buf_ptr;
3768
3769         /* Free the response before processing the command. */
3770         if (!list_empty(&buf_ptr1->list)) {
3771                 list_remove_head(&buf_ptr1->list, buf_ptr,
3772                                  struct lpfc_dmabuf,
3773                                  list);
3774                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3775                 kfree(buf_ptr);
3776         }
3777         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3778         kfree(buf_ptr1);
3779         return 0;
3780 }
3781
3782 /**
3783  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
3784  * @phba: pointer to lpfc hba data structure.
3785  * @buf_ptr: pointer to the lpfc dma buffer data structure.
3786  *
3787  * This routine releases the lpfc Direct Memory Access (DMA) buffer
3788  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3789  * pool.
3790  *
3791  * Return code
3792  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3793  **/
3794 static int
3795 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3796 {
3797         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3798         kfree(buf_ptr);
3799         return 0;
3800 }
3801
3802 /**
3803  * lpfc_els_free_iocb - Free a command iocb and its associated resources
3804  * @phba: pointer to lpfc hba data structure.
3805  * @elsiocb: pointer to lpfc els command iocb data structure.
3806  *
3807  * This routine frees a command IOCB and its associated resources. The
3808  * command IOCB data structure contains the reference to various associated
3809  * resources, these fields must be set to NULL if the associated reference
3810  * not present:
3811  *   context1 - reference to ndlp
3812  *   context2 - reference to cmd
3813  *   context2->next - reference to rsp
3814  *   context3 - reference to bpl
3815  *
3816  * It first properly decrements the reference count held on ndlp for the
3817  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3818  * set, it invokes the lpfc_els_free_data() routine to release the Direct
3819  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3820  * adds the DMA buffer the @phba data structure for the delayed release.
3821  * If reference to the Buffer Pointer List (BPL) is present, the
3822  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3823  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3824  * invoked to release the IOCB data structure back to @phba IOCBQ list.
3825  *
3826  * Return code
3827  *   0 - Success (currently, always return 0)
3828  **/
3829 int
3830 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
3831 {
3832         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
3833         struct lpfc_nodelist *ndlp;
3834
3835         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3836         if (ndlp) {
3837                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3838                         lpfc_nlp_put(ndlp);
3839
3840                         /* If the ndlp is not being used by another discovery
3841                          * thread, free it.
3842                          */
3843                         if (!lpfc_nlp_not_used(ndlp)) {
3844                                 /* If ndlp is being used by another discovery
3845                                  * thread, just clear NLP_DEFER_RM
3846                                  */
3847                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3848                         }
3849                 }
3850                 else
3851                         lpfc_nlp_put(ndlp);
3852                 elsiocb->context1 = NULL;
3853         }
3854         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
3855         if (elsiocb->context2) {
3856                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3857                         /* Firmware could still be in progress of DMAing
3858                          * payload, so don't free data buffer till after
3859                          * a hbeat.
3860                          */
3861                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3862                         buf_ptr = elsiocb->context2;
3863                         elsiocb->context2 = NULL;
3864                         if (buf_ptr) {
3865                                 buf_ptr1 = NULL;
3866                                 spin_lock_irq(&phba->hbalock);
3867                                 if (!list_empty(&buf_ptr->list)) {
3868                                         list_remove_head(&buf_ptr->list,
3869                                                 buf_ptr1, struct lpfc_dmabuf,
3870                                                 list);
3871                                         INIT_LIST_HEAD(&buf_ptr1->list);
3872                                         list_add_tail(&buf_ptr1->list,
3873                                                 &phba->elsbuf);
3874                                         phba->elsbuf_cnt++;
3875                                 }
3876                                 INIT_LIST_HEAD(&buf_ptr->list);
3877                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3878                                 phba->elsbuf_cnt++;
3879                                 spin_unlock_irq(&phba->hbalock);
3880                         }
3881                 } else {
3882                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3883                         lpfc_els_free_data(phba, buf_ptr1);
3884                         elsiocb->context2 = NULL;
3885                 }
3886         }
3887
3888         if (elsiocb->context3) {
3889                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
3890                 lpfc_els_free_bpl(phba, buf_ptr);
3891                 elsiocb->context3 = NULL;
3892         }
3893         lpfc_sli_release_iocbq(phba, elsiocb);
3894         return 0;
3895 }
3896
3897 /**
3898  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
3899  * @phba: pointer to lpfc hba data structure.
3900  * @cmdiocb: pointer to lpfc command iocb data structure.
3901  * @rspiocb: pointer to lpfc response iocb data structure.
3902  *
3903  * This routine is the completion callback function to the Logout (LOGO)
3904  * Accept (ACC) Response ELS command. This routine is invoked to indicate
3905  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3906  * release the ndlp if it has the last reference remaining (reference count
3907  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3908  * field to NULL to inform the following lpfc_els_free_iocb() routine no
3909  * ndlp reference count needs to be decremented. Otherwise, the ndlp
3910  * reference use-count shall be decremented by the lpfc_els_free_iocb()
3911  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3912  * IOCB data structure.
3913  **/
3914 static void
3915 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3916                        struct lpfc_iocbq *rspiocb)
3917 {
3918         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3919         struct lpfc_vport *vport = cmdiocb->vport;
3920         IOCB_t *irsp;
3921
3922         irsp = &rspiocb->iocb;
3923         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3924                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
3925                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
3926         /* ACC to LOGO completes to NPort <nlp_DID> */
3927         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3928                          "0109 ACC to LOGO completes to NPort x%x "
3929                          "Data: x%x x%x x%x\n",
3930                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3931                          ndlp->nlp_rpi);
3932
3933         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3934                 /* NPort Recovery mode or node is just allocated */
3935                 if (!lpfc_nlp_not_used(ndlp)) {
3936                         /* If the ndlp is being used by another discovery
3937                          * thread, just unregister the RPI.
3938                          */
3939                         lpfc_unreg_rpi(vport, ndlp);
3940                 } else {
3941                         /* Indicate the node has already released, should
3942                          * not reference to it from within lpfc_els_free_iocb.
3943                          */
3944                         cmdiocb->context1 = NULL;
3945                 }
3946         }
3947
3948         /*
3949          * The driver received a LOGO from the rport and has ACK'd it.
3950          * At this point, the driver is done so release the IOCB
3951          */
3952         lpfc_els_free_iocb(phba, cmdiocb);
3953 }
3954
3955 /**
3956  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
3957  * @phba: pointer to lpfc hba data structure.
3958  * @pmb: pointer to the driver internal queue element for mailbox command.
3959  *
3960  * This routine is the completion callback function for unregister default
3961  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3962  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3963  * decrements the ndlp reference count held for this completion callback
3964  * function. After that, it invokes the lpfc_nlp_not_used() to check
3965  * whether there is only one reference left on the ndlp. If so, it will
3966  * perform one more decrement and trigger the release of the ndlp.
3967  **/
3968 void
3969 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3970 {
3971         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3972         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3973
3974         pmb->context1 = NULL;
3975         pmb->context2 = NULL;
3976
3977         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3978         kfree(mp);
3979         mempool_free(pmb, phba->mbox_mem_pool);
3980         if (ndlp) {
3981                 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3982                                  "0006 rpi%x DID:%x flg:%x %d map:%x %p\n",
3983                                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3984                                  kref_read(&ndlp->kref),
3985                                  ndlp->nlp_usg_map, ndlp);
3986                 if (NLP_CHK_NODE_ACT(ndlp)) {
3987                         lpfc_nlp_put(ndlp);
3988                         /* This is the end of the default RPI cleanup logic for
3989                          * this ndlp. If no other discovery threads are using
3990                          * this ndlp, free all resources associated with it.
3991                          */
3992                         lpfc_nlp_not_used(ndlp);
3993                 } else {
3994                         lpfc_drop_node(ndlp->vport, ndlp);
3995                 }
3996         }
3997
3998         return;
3999 }
4000
4001 /**
4002  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
4003  * @phba: pointer to lpfc hba data structure.
4004  * @cmdiocb: pointer to lpfc command iocb data structure.
4005  * @rspiocb: pointer to lpfc response iocb data structure.
4006  *
4007  * This routine is the completion callback function for ELS Response IOCB
4008  * command. In normal case, this callback function just properly sets the
4009  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
4010  * field in the command IOCB is not NULL, the referred mailbox command will
4011  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
4012  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
4013  * link down event occurred during the discovery, the lpfc_nlp_not_used()
4014  * routine shall be invoked trying to release the ndlp if no other threads
4015  * are currently referring it.
4016  **/
4017 static void
4018 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4019                   struct lpfc_iocbq *rspiocb)
4020 {
4021         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4022         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
4023         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
4024         IOCB_t  *irsp;
4025         uint8_t *pcmd;
4026         LPFC_MBOXQ_t *mbox = NULL;
4027         struct lpfc_dmabuf *mp = NULL;
4028         uint32_t ls_rjt = 0;
4029
4030         irsp = &rspiocb->iocb;
4031
4032         if (cmdiocb->context_un.mbox)
4033                 mbox = cmdiocb->context_un.mbox;
4034
4035         /* First determine if this is a LS_RJT cmpl. Note, this callback
4036          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
4037          */
4038         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4039         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4040             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
4041                 /* A LS_RJT associated with Default RPI cleanup has its own
4042                  * separate code path.
4043                  */
4044                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4045                         ls_rjt = 1;
4046         }
4047
4048         /* Check to see if link went down during discovery */
4049         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
4050                 if (mbox) {
4051                         mp = (struct lpfc_dmabuf *) mbox->context1;
4052                         if (mp) {
4053                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4054                                 kfree(mp);
4055                         }
4056                         mempool_free(mbox, phba->mbox_mem_pool);
4057                 }
4058                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4059                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4060                         if (lpfc_nlp_not_used(ndlp)) {
4061                                 ndlp = NULL;
4062                                 /* Indicate the node has already released,
4063                                  * should not reference to it from within
4064                                  * the routine lpfc_els_free_iocb.
4065                                  */
4066                                 cmdiocb->context1 = NULL;
4067                         }
4068                 goto out;
4069         }
4070
4071         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4072                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
4073                 irsp->ulpStatus, irsp->un.ulpWord[4],
4074                 cmdiocb->iocb.un.elsreq64.remoteID);
4075         /* ELS response tag <ulpIoTag> completes */
4076         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4077                          "0110 ELS response tag x%x completes "
4078                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
4079                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
4080                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
4081                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4082                          ndlp->nlp_rpi);
4083         if (mbox) {
4084                 if ((rspiocb->iocb.ulpStatus == 0)
4085                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
4086                         if (!lpfc_unreg_rpi(vport, ndlp) &&
4087                             (ndlp->nlp_state ==  NLP_STE_PLOGI_ISSUE ||
4088                              ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE)) {
4089                                 lpfc_printf_vlog(vport, KERN_INFO,
4090                                         LOG_DISCOVERY,
4091                                         "0314 PLOGI recov DID x%x "
4092                                         "Data: x%x x%x x%x\n",
4093                                         ndlp->nlp_DID, ndlp->nlp_state,
4094                                         ndlp->nlp_rpi, ndlp->nlp_flag);
4095                                 mp = mbox->context1;
4096                                 if (mp) {
4097                                         lpfc_mbuf_free(phba, mp->virt,
4098                                                        mp->phys);
4099                                         kfree(mp);
4100                                 }
4101                                 mempool_free(mbox, phba->mbox_mem_pool);
4102                                 goto out;
4103                         }
4104
4105                         /* Increment reference count to ndlp to hold the
4106                          * reference to ndlp for the callback function.
4107                          */
4108                         mbox->context2 = lpfc_nlp_get(ndlp);
4109                         mbox->vport = vport;
4110                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
4111                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4112                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4113                         }
4114                         else {
4115                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
4116                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4117                                 lpfc_nlp_set_state(vport, ndlp,
4118                                            NLP_STE_REG_LOGIN_ISSUE);
4119                         }
4120
4121                         ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
4122                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4123                             != MBX_NOT_FINISHED)
4124                                 goto out;
4125
4126                         /* Decrement the ndlp reference count we
4127                          * set for this failed mailbox command.
4128                          */
4129                         lpfc_nlp_put(ndlp);
4130                         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4131
4132                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
4133                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4134                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
4135                                 "Data: x%x x%x x%x\n",
4136                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4137                                 ndlp->nlp_rpi);
4138
4139                         if (lpfc_nlp_not_used(ndlp)) {
4140                                 ndlp = NULL;
4141                                 /* Indicate node has already been released,
4142                                  * should not reference to it from within
4143                                  * the routine lpfc_els_free_iocb.
4144                                  */
4145                                 cmdiocb->context1 = NULL;
4146                         }
4147                 } else {
4148                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
4149                         if (!lpfc_error_lost_link(irsp) &&
4150                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
4151                                 if (lpfc_nlp_not_used(ndlp)) {
4152                                         ndlp = NULL;
4153                                         /* Indicate node has already been
4154                                          * released, should not reference
4155                                          * to it from within the routine
4156                                          * lpfc_els_free_iocb.
4157                                          */
4158                                         cmdiocb->context1 = NULL;
4159                                 }
4160                         }
4161                 }
4162                 mp = (struct lpfc_dmabuf *) mbox->context1;
4163                 if (mp) {
4164                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4165                         kfree(mp);
4166                 }
4167                 mempool_free(mbox, phba->mbox_mem_pool);
4168         }
4169 out:
4170         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
4171                 spin_lock_irq(shost->host_lock);
4172                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
4173                 spin_unlock_irq(shost->host_lock);
4174
4175                 /* If the node is not being used by another discovery thread,
4176                  * and we are sending a reject, we are done with it.
4177                  * Release driver reference count here and free associated
4178                  * resources.
4179                  */
4180                 if (ls_rjt)
4181                         if (lpfc_nlp_not_used(ndlp))
4182                                 /* Indicate node has already been released,
4183                                  * should not reference to it from within
4184                                  * the routine lpfc_els_free_iocb.
4185                                  */
4186                                 cmdiocb->context1 = NULL;
4187
4188         }
4189
4190         lpfc_els_free_iocb(phba, cmdiocb);
4191         return;
4192 }
4193
4194 /**
4195  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
4196  * @vport: pointer to a host virtual N_Port data structure.
4197  * @flag: the els command code to be accepted.
4198  * @oldiocb: pointer to the original lpfc command iocb data structure.
4199  * @ndlp: pointer to a node-list data structure.
4200  * @mbox: pointer to the driver internal queue element for mailbox command.
4201  *
4202  * This routine prepares and issues an Accept (ACC) response IOCB
4203  * command. It uses the @flag to properly set up the IOCB field for the
4204  * specific ACC response command to be issued and invokes the
4205  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
4206  * @mbox pointer is passed in, it will be put into the context_un.mbox
4207  * field of the IOCB for the completion callback function to issue the
4208  * mailbox command to the HBA later when callback is invoked.
4209  *
4210  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4211  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4212  * will be stored into the context1 field of the IOCB for the completion
4213  * callback function to the corresponding response ELS IOCB command.
4214  *
4215  * Return code
4216  *   0 - Successfully issued acc response
4217  *   1 - Failed to issue acc response
4218  **/
4219 int
4220 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
4221                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4222                  LPFC_MBOXQ_t *mbox)
4223 {
4224         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4225         struct lpfc_hba  *phba = vport->phba;
4226         IOCB_t *icmd;
4227         IOCB_t *oldcmd;
4228         struct lpfc_iocbq *elsiocb;
4229         uint8_t *pcmd;
4230         struct serv_parm *sp;
4231         uint16_t cmdsize;
4232         int rc;
4233         ELS_PKT *els_pkt_ptr;
4234
4235         oldcmd = &oldiocb->iocb;
4236
4237         switch (flag) {
4238         case ELS_CMD_ACC:
4239                 cmdsize = sizeof(uint32_t);
4240                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4241                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4242                 if (!elsiocb) {
4243                         spin_lock_irq(shost->host_lock);
4244                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4245                         spin_unlock_irq(shost->host_lock);
4246                         return 1;
4247                 }
4248
4249                 icmd = &elsiocb->iocb;
4250                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4251                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4252                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4253                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4254                 pcmd += sizeof(uint32_t);
4255
4256                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4257                         "Issue ACC:       did:x%x flg:x%x",
4258                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4259                 break;
4260         case ELS_CMD_FLOGI:
4261         case ELS_CMD_PLOGI:
4262                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
4263                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4264                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4265                 if (!elsiocb)
4266                         return 1;
4267
4268                 icmd = &elsiocb->iocb;
4269                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4270                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4271                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4272
4273                 if (mbox)
4274                         elsiocb->context_un.mbox = mbox;
4275
4276                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4277                 pcmd += sizeof(uint32_t);
4278                 sp = (struct serv_parm *)pcmd;
4279
4280                 if (flag == ELS_CMD_FLOGI) {
4281                         /* Copy the received service parameters back */
4282                         memcpy(sp, &phba->fc_fabparam,
4283                                sizeof(struct serv_parm));
4284
4285                         /* Clear the F_Port bit */
4286                         sp->cmn.fPort = 0;
4287
4288                         /* Mark all class service parameters as invalid */
4289                         sp->cls1.classValid = 0;
4290                         sp->cls2.classValid = 0;
4291                         sp->cls3.classValid = 0;
4292                         sp->cls4.classValid = 0;
4293
4294                         /* Copy our worldwide names */
4295                         memcpy(&sp->portName, &vport->fc_sparam.portName,
4296                                sizeof(struct lpfc_name));
4297                         memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
4298                                sizeof(struct lpfc_name));
4299                 } else {
4300                         memcpy(pcmd, &vport->fc_sparam,
4301                                sizeof(struct serv_parm));
4302
4303                         sp->cmn.valid_vendor_ver_level = 0;
4304                         memset(sp->un.vendorVersion, 0,
4305                                sizeof(sp->un.vendorVersion));
4306                         sp->cmn.bbRcvSizeMsb &= 0xF;
4307
4308                         /* If our firmware supports this feature, convey that
4309                          * info to the target using the vendor specific field.
4310                          */
4311                         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
4312                                 sp->cmn.valid_vendor_ver_level = 1;
4313                                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
4314                                 sp->un.vv.flags =
4315                                         cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
4316                         }
4317                 }
4318
4319                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4320                         "Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
4321                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4322                 break;
4323         case ELS_CMD_PRLO:
4324                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
4325                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4326                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
4327                 if (!elsiocb)
4328                         return 1;
4329
4330                 icmd = &elsiocb->iocb;
4331                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4332                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4333                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4334
4335                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
4336                        sizeof(uint32_t) + sizeof(PRLO));
4337                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
4338                 els_pkt_ptr = (ELS_PKT *) pcmd;
4339                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
4340
4341                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4342                         "Issue ACC PRLO:  did:x%x flg:x%x",
4343                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
4344                 break;
4345         default:
4346                 return 1;
4347         }
4348         /* Xmit ELS ACC response tag <ulpIoTag> */
4349         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4350                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
4351                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x "
4352                          "fc_flag x%x\n",
4353                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4354                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4355                          ndlp->nlp_rpi, vport->fc_flag);
4356         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
4357                 spin_lock_irq(shost->host_lock);
4358                 if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4359                         ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
4360                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4361                 spin_unlock_irq(shost->host_lock);
4362                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4363         } else {
4364                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4365         }
4366
4367         phba->fc_stat.elsXmitACC++;
4368         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4369         if (rc == IOCB_ERROR) {
4370                 lpfc_els_free_iocb(phba, elsiocb);
4371                 return 1;
4372         }
4373         return 0;
4374 }
4375
4376 /**
4377  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
4378  * @vport: pointer to a virtual N_Port data structure.
4379  * @rejectError:
4380  * @oldiocb: pointer to the original lpfc command iocb data structure.
4381  * @ndlp: pointer to a node-list data structure.
4382  * @mbox: pointer to the driver internal queue element for mailbox command.
4383  *
4384  * This routine prepares and issue an Reject (RJT) response IOCB
4385  * command. If a @mbox pointer is passed in, it will be put into the
4386  * context_un.mbox field of the IOCB for the completion callback function
4387  * to issue to the HBA later.
4388  *
4389  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4390  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4391  * will be stored into the context1 field of the IOCB for the completion
4392  * callback function to the reject response ELS IOCB command.
4393  *
4394  * Return code
4395  *   0 - Successfully issued reject response
4396  *   1 - Failed to issue reject response
4397  **/
4398 int
4399 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
4400                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4401                     LPFC_MBOXQ_t *mbox)
4402 {
4403         struct lpfc_hba  *phba = vport->phba;
4404         IOCB_t *icmd;
4405         IOCB_t *oldcmd;
4406         struct lpfc_iocbq *elsiocb;
4407         uint8_t *pcmd;
4408         uint16_t cmdsize;
4409         int rc;
4410
4411         cmdsize = 2 * sizeof(uint32_t);
4412         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4413                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
4414         if (!elsiocb)
4415                 return 1;
4416
4417         icmd = &elsiocb->iocb;
4418         oldcmd = &oldiocb->iocb;
4419         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4420         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4421         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4422
4423         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
4424         pcmd += sizeof(uint32_t);
4425         *((uint32_t *) (pcmd)) = rejectError;
4426
4427         if (mbox)
4428                 elsiocb->context_un.mbox = mbox;
4429
4430         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
4431         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4432                          "0129 Xmit ELS RJT x%x response tag x%x "
4433                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4434                          "rpi x%x\n",
4435                          rejectError, elsiocb->iotag,
4436                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4437                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
4438         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4439                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
4440                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4441
4442         phba->fc_stat.elsXmitLSRJT++;
4443         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4444         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4445
4446         if (rc == IOCB_ERROR) {
4447                 lpfc_els_free_iocb(phba, elsiocb);
4448                 return 1;
4449         }
4450         return 0;
4451 }
4452
4453 /**
4454  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
4455  * @vport: pointer to a virtual N_Port data structure.
4456  * @oldiocb: pointer to the original lpfc command iocb data structure.
4457  * @ndlp: pointer to a node-list data structure.
4458  *
4459  * This routine prepares and issues an Accept (ACC) response to Address
4460  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4461  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4462  *
4463  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4464  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4465  * will be stored into the context1 field of the IOCB for the completion
4466  * callback function to the ADISC Accept response ELS IOCB command.
4467  *
4468  * Return code
4469  *   0 - Successfully issued acc adisc response
4470  *   1 - Failed to issue adisc acc response
4471  **/
4472 int
4473 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4474                        struct lpfc_nodelist *ndlp)
4475 {
4476         struct lpfc_hba  *phba = vport->phba;
4477         ADISC *ap;
4478         IOCB_t *icmd, *oldcmd;
4479         struct lpfc_iocbq *elsiocb;
4480         uint8_t *pcmd;
4481         uint16_t cmdsize;
4482         int rc;
4483
4484         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
4485         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4486                                      ndlp->nlp_DID, ELS_CMD_ACC);
4487         if (!elsiocb)
4488                 return 1;
4489
4490         icmd = &elsiocb->iocb;
4491         oldcmd = &oldiocb->iocb;
4492         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4493         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4494
4495         /* Xmit ADISC ACC response tag <ulpIoTag> */
4496         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4497                          "0130 Xmit ADISC ACC response iotag x%x xri: "
4498                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4499                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4500                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4501                          ndlp->nlp_rpi);
4502         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4503
4504         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4505         pcmd += sizeof(uint32_t);
4506
4507         ap = (ADISC *) (pcmd);
4508         ap->hardAL_PA = phba->fc_pref_ALPA;
4509         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4510         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4511         ap->DID = be32_to_cpu(vport->fc_myDID);
4512
4513         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4514                 "Issue ACC ADISC: did:x%x flg:x%x",
4515                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4516
4517         phba->fc_stat.elsXmitACC++;
4518         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4519         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4520         if (rc == IOCB_ERROR) {
4521                 lpfc_els_free_iocb(phba, elsiocb);
4522                 return 1;
4523         }
4524         return 0;
4525 }
4526
4527 /**
4528  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
4529  * @vport: pointer to a virtual N_Port data structure.
4530  * @oldiocb: pointer to the original lpfc command iocb data structure.
4531  * @ndlp: pointer to a node-list data structure.
4532  *
4533  * This routine prepares and issues an Accept (ACC) response to Process
4534  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
4535  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4536  *
4537  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4538  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4539  * will be stored into the context1 field of the IOCB for the completion
4540  * callback function to the PRLI Accept response ELS IOCB command.
4541  *
4542  * Return code
4543  *   0 - Successfully issued acc prli response
4544  *   1 - Failed to issue acc prli response
4545  **/
4546 int
4547 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4548                       struct lpfc_nodelist *ndlp)
4549 {
4550         struct lpfc_hba  *phba = vport->phba;
4551         PRLI *npr;
4552         struct lpfc_nvme_prli *npr_nvme;
4553         lpfc_vpd_t *vpd;
4554         IOCB_t *icmd;
4555         IOCB_t *oldcmd;
4556         struct lpfc_iocbq *elsiocb;
4557         uint8_t *pcmd;
4558         uint16_t cmdsize;
4559         uint32_t prli_fc4_req, *req_payload;
4560         struct lpfc_dmabuf *req_buf;
4561         int rc;
4562         u32 elsrspcmd;
4563
4564         /* Need the incoming PRLI payload to determine if the ACC is for an
4565          * FC4 or NVME PRLI type.  The PRLI type is at word 1.
4566          */
4567         req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
4568         req_payload = (((uint32_t *)req_buf->virt) + 1);
4569
4570         /* PRLI type payload is at byte 3 for FCP or NVME. */
4571         prli_fc4_req = be32_to_cpu(*req_payload);
4572         prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
4573         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4574                          "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
4575                          prli_fc4_req, *((uint32_t *)req_payload));
4576
4577         if (prli_fc4_req == PRLI_FCP_TYPE) {
4578                 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
4579                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
4580         } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4581                 cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
4582                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
4583         } else {
4584                 return 1;
4585         }
4586
4587         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4588                 ndlp->nlp_DID, elsrspcmd);
4589         if (!elsiocb)
4590                 return 1;
4591
4592         icmd = &elsiocb->iocb;
4593         oldcmd = &oldiocb->iocb;
4594         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4595         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4596
4597         /* Xmit PRLI ACC response tag <ulpIoTag> */
4598         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4599                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
4600                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4601                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4602                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4603                          ndlp->nlp_rpi);
4604         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4605         memset(pcmd, 0, cmdsize);
4606
4607         *((uint32_t *)(pcmd)) = elsrspcmd;
4608         pcmd += sizeof(uint32_t);
4609
4610         /* For PRLI, remainder of payload is PRLI parameter page */
4611         vpd = &phba->vpd;
4612
4613         if (prli_fc4_req == PRLI_FCP_TYPE) {
4614                 /*
4615                  * If the remote port is a target and our firmware version
4616                  * is 3.20 or later, set the following bits for FC-TAPE
4617                  * support.
4618                  */
4619                 npr = (PRLI *) pcmd;
4620                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4621                     (vpd->rev.feaLevelHigh >= 0x02)) {
4622                         npr->ConfmComplAllowed = 1;
4623                         npr->Retry = 1;
4624                         npr->TaskRetryIdReq = 1;
4625                 }
4626                 npr->acceptRspCode = PRLI_REQ_EXECUTED;
4627                 npr->estabImagePair = 1;
4628                 npr->readXferRdyDis = 1;
4629                 npr->ConfmComplAllowed = 1;
4630                 npr->prliType = PRLI_FCP_TYPE;
4631                 npr->initiatorFunc = 1;
4632         } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4633                 /* Respond with an NVME PRLI Type */
4634                 npr_nvme = (struct lpfc_nvme_prli *) pcmd;
4635                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
4636                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
4637                 bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
4638                 if (phba->nvmet_support) {
4639                         bf_set(prli_tgt, npr_nvme, 1);
4640                         bf_set(prli_disc, npr_nvme, 1);
4641                         if (phba->cfg_nvme_enable_fb) {
4642                                 bf_set(prli_fba, npr_nvme, 1);
4643
4644                                 /* TBD.  Target mode needs to post buffers
4645                                  * that support the configured first burst
4646                                  * byte size.
4647                                  */
4648                                 bf_set(prli_fb_sz, npr_nvme,
4649                                        phba->cfg_nvmet_fb_size);
4650                         }
4651                 } else {
4652                         bf_set(prli_init, npr_nvme, 1);
4653                 }
4654
4655                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
4656                                  "6015 NVME issue PRLI ACC word1 x%08x "
4657                                  "word4 x%08x word5 x%08x flag x%x, "
4658                                  "fcp_info x%x nlp_type x%x\n",
4659                                  npr_nvme->word1, npr_nvme->word4,
4660                                  npr_nvme->word5, ndlp->nlp_flag,
4661                                  ndlp->nlp_fcp_info, ndlp->nlp_type);
4662                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
4663                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
4664                 npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
4665         } else
4666                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4667                                  "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
4668                                  prli_fc4_req, ndlp->nlp_fc4_type,
4669                                  ndlp->nlp_DID);
4670
4671         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4672                 "Issue ACC PRLI:  did:x%x flg:x%x",
4673                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4674
4675         phba->fc_stat.elsXmitACC++;
4676         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4677
4678         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4679         if (rc == IOCB_ERROR) {
4680                 lpfc_els_free_iocb(phba, elsiocb);
4681                 return 1;
4682         }
4683         return 0;
4684 }
4685
4686 /**
4687  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
4688  * @vport: pointer to a virtual N_Port data structure.
4689  * @format: rnid command format.
4690  * @oldiocb: pointer to the original lpfc command iocb data structure.
4691  * @ndlp: pointer to a node-list data structure.
4692  *
4693  * This routine issues a Request Node Identification Data (RNID) Accept
4694  * (ACC) response. It constructs the RNID ACC response command according to
4695  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4696  * issue the response. Note that this command does not need to hold the ndlp
4697  * reference count for the callback. So, the ndlp reference count taken by
4698  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4699  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4700  * there is no ndlp reference available.
4701  *
4702  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4703  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4704  * will be stored into the context1 field of the IOCB for the completion
4705  * callback function. However, for the RNID Accept Response ELS command,
4706  * this is undone later by this routine after the IOCB is allocated.
4707  *
4708  * Return code
4709  *   0 - Successfully issued acc rnid response
4710  *   1 - Failed to issue acc rnid response
4711  **/
4712 static int
4713 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
4714                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4715 {
4716         struct lpfc_hba  *phba = vport->phba;
4717         RNID *rn;
4718         IOCB_t *icmd, *oldcmd;
4719         struct lpfc_iocbq *elsiocb;
4720         uint8_t *pcmd;
4721         uint16_t cmdsize;
4722         int rc;
4723
4724         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4725                                         + (2 * sizeof(struct lpfc_name));
4726         if (format)
4727                 cmdsize += sizeof(RNID_TOP_DISC);
4728
4729         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4730                                      ndlp->nlp_DID, ELS_CMD_ACC);
4731         if (!elsiocb)
4732                 return 1;
4733
4734         icmd = &elsiocb->iocb;
4735         oldcmd = &oldiocb->iocb;
4736         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4737         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4738
4739         /* Xmit RNID ACC response tag <ulpIoTag> */
4740         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4741                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
4742                          elsiocb->iotag, elsiocb->iocb.ulpContext);
4743         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4744         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4745         pcmd += sizeof(uint32_t);
4746
4747         memset(pcmd, 0, sizeof(RNID));
4748         rn = (RNID *) (pcmd);
4749         rn->Format = format;
4750         rn->CommonLen = (2 * sizeof(struct lpfc_name));
4751         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4752         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4753         switch (format) {
4754         case 0:
4755                 rn->SpecificLen = 0;
4756                 break;
4757         case RNID_TOPOLOGY_DISC:
4758                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
4759                 memcpy(&rn->un.topologyDisc.portName,
4760                        &vport->fc_portname, sizeof(struct lpfc_name));
4761                 rn->un.topologyDisc.unitType = RNID_HBA;
4762                 rn->un.topologyDisc.physPort = 0;
4763                 rn->un.topologyDisc.attachedNodes = 0;
4764                 break;
4765         default:
4766                 rn->CommonLen = 0;
4767                 rn->SpecificLen = 0;
4768                 break;
4769         }
4770
4771         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4772                 "Issue ACC RNID:  did:x%x flg:x%x",
4773                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4774
4775         phba->fc_stat.elsXmitACC++;
4776         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4777
4778         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4779         if (rc == IOCB_ERROR) {
4780                 lpfc_els_free_iocb(phba, elsiocb);
4781                 return 1;
4782         }
4783         return 0;
4784 }
4785
4786 /**
4787  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
4788  * @vport: pointer to a virtual N_Port data structure.
4789  * @iocb: pointer to the lpfc command iocb data structure.
4790  * @ndlp: pointer to a node-list data structure.
4791  *
4792  * Return
4793  **/
4794 static void
4795 lpfc_els_clear_rrq(struct lpfc_vport *vport,
4796                    struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
4797 {
4798         struct lpfc_hba  *phba = vport->phba;
4799         uint8_t *pcmd;
4800         struct RRQ *rrq;
4801         uint16_t rxid;
4802         uint16_t xri;
4803         struct lpfc_node_rrq *prrq;
4804
4805
4806         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
4807         pcmd += sizeof(uint32_t);
4808         rrq = (struct RRQ *)pcmd;
4809         rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
4810         rxid = bf_get(rrq_rxid, rrq);
4811
4812         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4813                         "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
4814                         " x%x x%x\n",
4815                         be32_to_cpu(bf_get(rrq_did, rrq)),
4816                         bf_get(rrq_oxid, rrq),
4817                         rxid,
4818                         iocb->iotag, iocb->iocb.ulpContext);
4819
4820         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4821                 "Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
4822                 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
4823         if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
4824                 xri = bf_get(rrq_oxid, rrq);
4825         else
4826                 xri = rxid;
4827         prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
4828         if (prrq)
4829                 lpfc_clr_rrq_active(phba, xri, prrq);
4830         return;
4831 }
4832
4833 /**
4834  * lpfc_els_rsp_echo_acc - Issue echo acc response
4835  * @vport: pointer to a virtual N_Port data structure.
4836  * @data: pointer to echo data to return in the accept.
4837  * @oldiocb: pointer to the original lpfc command iocb data structure.
4838  * @ndlp: pointer to a node-list data structure.
4839  *
4840  * Return code
4841  *   0 - Successfully issued acc echo response
4842  *   1 - Failed to issue acc echo response
4843  **/
4844 static int
4845 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
4846                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4847 {
4848         struct lpfc_hba  *phba = vport->phba;
4849         struct lpfc_iocbq *elsiocb;
4850         uint8_t *pcmd;
4851         uint16_t cmdsize;
4852         int rc;
4853
4854         cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
4855
4856         /* The accumulated length can exceed the BPL_SIZE.  For
4857          * now, use this as the limit
4858          */
4859         if (cmdsize > LPFC_BPL_SIZE)
4860                 cmdsize = LPFC_BPL_SIZE;
4861         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4862                                      ndlp->nlp_DID, ELS_CMD_ACC);
4863         if (!elsiocb)
4864                 return 1;
4865
4866         elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
4867         elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
4868
4869         /* Xmit ECHO ACC response tag <ulpIoTag> */
4870         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4871                          "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
4872                          elsiocb->iotag, elsiocb->iocb.ulpContext);
4873         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4874         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4875         pcmd += sizeof(uint32_t);
4876         memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
4877
4878         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4879                 "Issue ACC ECHO:  did:x%x flg:x%x",
4880                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4881
4882         phba->fc_stat.elsXmitACC++;
4883         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4884
4885         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4886         if (rc == IOCB_ERROR) {
4887                 lpfc_els_free_iocb(phba, elsiocb);
4888                 return 1;
4889         }
4890         return 0;
4891 }
4892
4893 /**
4894  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
4895  * @vport: pointer to a host virtual N_Port data structure.
4896  *
4897  * This routine issues Address Discover (ADISC) ELS commands to those
4898  * N_Ports which are in node port recovery state and ADISC has not been issued
4899  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
4900  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
4901  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
4902  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
4903  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4904  * IOCBs quit for later pick up. On the other hand, after walking through
4905  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4906  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4907  * no more ADISC need to be sent.
4908  *
4909  * Return code
4910  *    The number of N_Ports with adisc issued.
4911  **/
4912 int
4913 lpfc_els_disc_adisc(struct lpfc_vport *vport)
4914 {
4915         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4916         struct lpfc_nodelist *ndlp, *next_ndlp;
4917         int sentadisc = 0;
4918
4919         /* go thru NPR nodes and issue any remaining ELS ADISCs */
4920         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4921                 if (!NLP_CHK_NODE_ACT(ndlp))
4922                         continue;
4923                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4924                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4925                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
4926                         spin_lock_irq(shost->host_lock);
4927                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4928                         spin_unlock_irq(shost->host_lock);
4929                         ndlp->nlp_prev_state = ndlp->nlp_state;
4930                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4931                         lpfc_issue_els_adisc(vport, ndlp, 0);
4932                         sentadisc++;
4933                         vport->num_disc_nodes++;
4934                         if (vport->num_disc_nodes >=
4935                             vport->cfg_discovery_threads) {
4936                                 spin_lock_irq(shost->host_lock);
4937                                 vport->fc_flag |= FC_NLP_MORE;
4938                                 spin_unlock_irq(shost->host_lock);
4939                                 break;
4940                         }
4941                 }
4942         }
4943         if (sentadisc == 0) {
4944                 spin_lock_irq(shost->host_lock);
4945                 vport->fc_flag &= ~FC_NLP_MORE;
4946                 spin_unlock_irq(shost->host_lock);
4947         }
4948         return sentadisc;
4949 }
4950
4951 /**
4952  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
4953  * @vport: pointer to a host virtual N_Port data structure.
4954  *
4955  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4956  * which are in node port recovery state, with a @vport. Each time an ELS
4957  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4958  * the per @vport number of discover count (num_disc_nodes) shall be
4959  * incremented. If the num_disc_nodes reaches a pre-configured threshold
4960  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4961  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4962  * later pick up. On the other hand, after walking through all the ndlps with
4963  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4964  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4965  * PLOGI need to be sent.
4966  *
4967  * Return code
4968  *   The number of N_Ports with plogi issued.
4969  **/
4970 int
4971 lpfc_els_disc_plogi(struct lpfc_vport *vport)
4972 {
4973         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4974         struct lpfc_nodelist *ndlp, *next_ndlp;
4975         int sentplogi = 0;
4976
4977         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
4978         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4979                 if (!NLP_CHK_NODE_ACT(ndlp))
4980                         continue;
4981                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4982                                 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4983                                 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4984                                 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4985                         ndlp->nlp_prev_state = ndlp->nlp_state;
4986                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4987                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4988                         sentplogi++;
4989                         vport->num_disc_nodes++;
4990                         if (vport->num_disc_nodes >=
4991                                         vport->cfg_discovery_threads) {
4992                                 spin_lock_irq(shost->host_lock);
4993                                 vport->fc_flag |= FC_NLP_MORE;
4994                                 spin_unlock_irq(shost->host_lock);
4995                                 break;
4996                         }
4997                 }
4998         }
4999         if (sentplogi) {
5000                 lpfc_set_disctmo(vport);
5001         }
5002         else {
5003                 spin_lock_irq(shost->host_lock);
5004                 vport->fc_flag &= ~FC_NLP_MORE;
5005                 spin_unlock_irq(shost->host_lock);
5006         }
5007         return sentplogi;
5008 }
5009
5010 static uint32_t
5011 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
5012                 uint32_t word0)
5013 {
5014
5015         desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
5016         desc->payload.els_req = word0;
5017         desc->length = cpu_to_be32(sizeof(desc->payload));
5018
5019         return sizeof(struct fc_rdp_link_service_desc);
5020 }
5021
5022 static uint32_t
5023 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
5024                 uint8_t *page_a0, uint8_t *page_a2)
5025 {
5026         uint16_t wavelength;
5027         uint16_t temperature;
5028         uint16_t rx_power;
5029         uint16_t tx_bias;
5030         uint16_t tx_power;
5031         uint16_t vcc;
5032         uint16_t flag = 0;
5033         struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
5034         struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
5035
5036         desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
5037
5038         trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
5039                         &page_a0[SSF_TRANSCEIVER_CODE_B4];
5040         trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
5041                         &page_a0[SSF_TRANSCEIVER_CODE_B5];
5042
5043         if ((trasn_code_byte4->fc_sw_laser) ||
5044             (trasn_code_byte5->fc_sw_laser_sl) ||
5045             (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
5046                 flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
5047         } else if (trasn_code_byte4->fc_lw_laser) {
5048                 wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
5049                         page_a0[SSF_WAVELENGTH_B0];
5050                 if (wavelength == SFP_WAVELENGTH_LC1310)
5051                         flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
5052                 if (wavelength == SFP_WAVELENGTH_LL1550)
5053                         flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
5054         }
5055         /* check if its SFP+ */
5056         flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
5057                         SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
5058                                         << SFP_FLAG_CT_SHIFT;
5059
5060         /* check if its OPTICAL */
5061         flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
5062                         SFP_FLAG_IS_OPTICAL_PORT : 0)
5063                                         << SFP_FLAG_IS_OPTICAL_SHIFT;
5064
5065         temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
5066                 page_a2[SFF_TEMPERATURE_B0]);
5067         vcc = (page_a2[SFF_VCC_B1] << 8 |
5068                 page_a2[SFF_VCC_B0]);
5069         tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
5070                 page_a2[SFF_TXPOWER_B0]);
5071         tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
5072                 page_a2[SFF_TX_BIAS_CURRENT_B0]);
5073         rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
5074                 page_a2[SFF_RXPOWER_B0]);
5075         desc->sfp_info.temperature = cpu_to_be16(temperature);
5076         desc->sfp_info.rx_power = cpu_to_be16(rx_power);
5077         desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
5078         desc->sfp_info.tx_power = cpu_to_be16(tx_power);
5079         desc->sfp_info.vcc = cpu_to_be16(vcc);
5080
5081         desc->sfp_info.flags = cpu_to_be16(flag);
5082         desc->length = cpu_to_be32(sizeof(desc->sfp_info));
5083
5084         return sizeof(struct fc_rdp_sfp_desc);
5085 }
5086
5087 static uint32_t
5088 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
5089                 READ_LNK_VAR *stat)
5090 {
5091         uint32_t type;
5092
5093         desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
5094
5095         type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
5096
5097         desc->info.port_type = cpu_to_be32(type);
5098
5099         desc->info.link_status.link_failure_cnt =
5100                 cpu_to_be32(stat->linkFailureCnt);
5101         desc->info.link_status.loss_of_synch_cnt =
5102                 cpu_to_be32(stat->lossSyncCnt);
5103         desc->info.link_status.loss_of_signal_cnt =
5104                 cpu_to_be32(stat->lossSignalCnt);
5105         desc->info.link_status.primitive_seq_proto_err =
5106                 cpu_to_be32(stat->primSeqErrCnt);
5107         desc->info.link_status.invalid_trans_word =
5108                 cpu_to_be32(stat->invalidXmitWord);
5109         desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
5110
5111         desc->length = cpu_to_be32(sizeof(desc->info));
5112
5113         return sizeof(struct fc_rdp_link_error_status_desc);
5114 }
5115
5116 static uint32_t
5117 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
5118                       struct lpfc_vport *vport)
5119 {
5120         uint32_t bbCredit;
5121
5122         desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
5123
5124         bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
5125                         (vport->fc_sparam.cmn.bbCreditMsb << 8);
5126         desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
5127         if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
5128                 bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
5129                         (vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
5130                 desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
5131         } else {
5132                 desc->bbc_info.attached_port_bbc = 0;
5133         }
5134
5135         desc->bbc_info.rtt = 0;
5136         desc->length = cpu_to_be32(sizeof(desc->bbc_info));
5137
5138         return sizeof(struct fc_rdp_bbc_desc);
5139 }
5140
5141 static uint32_t
5142 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
5143                            struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
5144 {
5145         uint32_t flags = 0;
5146
5147         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5148
5149         desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
5150         desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
5151         desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
5152         desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
5153
5154         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5155                 flags |= RDP_OET_HIGH_ALARM;
5156         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5157                 flags |= RDP_OET_LOW_ALARM;
5158         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5159                 flags |= RDP_OET_HIGH_WARNING;
5160         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5161                 flags |= RDP_OET_LOW_WARNING;
5162
5163         flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
5164         desc->oed_info.function_flags = cpu_to_be32(flags);
5165         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5166         return sizeof(struct fc_rdp_oed_sfp_desc);
5167 }
5168
5169 static uint32_t
5170 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
5171                               struct fc_rdp_oed_sfp_desc *desc,
5172                               uint8_t *page_a2)
5173 {
5174         uint32_t flags = 0;
5175
5176         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5177
5178         desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
5179         desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
5180         desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
5181         desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
5182
5183         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5184                 flags |= RDP_OET_HIGH_ALARM;
5185         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5186                 flags |= RDP_OET_LOW_ALARM;
5187         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5188                 flags |= RDP_OET_HIGH_WARNING;
5189         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5190                 flags |= RDP_OET_LOW_WARNING;
5191
5192         flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
5193         desc->oed_info.function_flags = cpu_to_be32(flags);
5194         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5195         return sizeof(struct fc_rdp_oed_sfp_desc);
5196 }
5197
5198 static uint32_t
5199 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
5200                              struct fc_rdp_oed_sfp_desc *desc,
5201                              uint8_t *page_a2)
5202 {
5203         uint32_t flags = 0;
5204
5205         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5206
5207         desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
5208         desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
5209         desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
5210         desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
5211
5212         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5213                 flags |= RDP_OET_HIGH_ALARM;
5214         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
5215                 flags |= RDP_OET_LOW_ALARM;
5216         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5217                 flags |= RDP_OET_HIGH_WARNING;
5218         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
5219                 flags |= RDP_OET_LOW_WARNING;
5220
5221         flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
5222         desc->oed_info.function_flags = cpu_to_be32(flags);
5223         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5224         return sizeof(struct fc_rdp_oed_sfp_desc);
5225 }
5226
5227 static uint32_t
5228 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
5229                               struct fc_rdp_oed_sfp_desc *desc,
5230                               uint8_t *page_a2)
5231 {
5232         uint32_t flags = 0;
5233
5234         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5235
5236         desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
5237         desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
5238         desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
5239         desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
5240
5241         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5242                 flags |= RDP_OET_HIGH_ALARM;
5243         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
5244                 flags |= RDP_OET_LOW_ALARM;
5245         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5246                 flags |= RDP_OET_HIGH_WARNING;
5247         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
5248                 flags |= RDP_OET_LOW_WARNING;
5249
5250         flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
5251         desc->oed_info.function_flags = cpu_to_be32(flags);
5252         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5253         return sizeof(struct fc_rdp_oed_sfp_desc);
5254 }
5255
5256
5257 static uint32_t
5258 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
5259                               struct fc_rdp_oed_sfp_desc *desc,
5260                               uint8_t *page_a2)
5261 {
5262         uint32_t flags = 0;
5263
5264         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5265
5266         desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
5267         desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
5268         desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
5269         desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
5270
5271         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5272                 flags |= RDP_OET_HIGH_ALARM;
5273         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
5274                 flags |= RDP_OET_LOW_ALARM;
5275         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5276                 flags |= RDP_OET_HIGH_WARNING;
5277         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
5278                 flags |= RDP_OET_LOW_WARNING;
5279
5280         flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
5281         desc->oed_info.function_flags = cpu_to_be32(flags);
5282         desc->length = cpu_to_be32(sizeof(desc->oed_info));
5283         return sizeof(struct fc_rdp_oed_sfp_desc);
5284 }
5285
5286 static uint32_t
5287 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
5288                       uint8_t *page_a0, struct lpfc_vport *vport)
5289 {
5290         desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
5291         memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
5292         memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
5293         memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
5294         memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
5295         memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
5296         desc->length = cpu_to_be32(sizeof(desc->opd_info));
5297         return sizeof(struct fc_rdp_opd_sfp_desc);
5298 }
5299
5300 static uint32_t
5301 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
5302 {
5303         if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
5304                 return 0;
5305         desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
5306
5307         desc->info.CorrectedBlocks =
5308                 cpu_to_be32(stat->fecCorrBlkCount);
5309         desc->info.UncorrectableBlocks =
5310                 cpu_to_be32(stat->fecUncorrBlkCount);
5311
5312         desc->length = cpu_to_be32(sizeof(desc->info));
5313
5314         return sizeof(struct fc_fec_rdp_desc);
5315 }
5316
5317 static uint32_t
5318 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
5319 {
5320         uint16_t rdp_cap = 0;
5321         uint16_t rdp_speed;
5322
5323         desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
5324
5325         switch (phba->fc_linkspeed) {
5326         case LPFC_LINK_SPEED_1GHZ:
5327                 rdp_speed = RDP_PS_1GB;
5328                 break;
5329         case LPFC_LINK_SPEED_2GHZ:
5330                 rdp_speed = RDP_PS_2GB;
5331                 break;
5332         case LPFC_LINK_SPEED_4GHZ:
5333                 rdp_speed = RDP_PS_4GB;
5334                 break;
5335         case LPFC_LINK_SPEED_8GHZ:
5336                 rdp_speed = RDP_PS_8GB;
5337                 break;
5338         case LPFC_LINK_SPEED_10GHZ:
5339                 rdp_speed = RDP_PS_10GB;
5340                 break;
5341         case LPFC_LINK_SPEED_16GHZ:
5342                 rdp_speed = RDP_PS_16GB;
5343                 break;
5344         case LPFC_LINK_SPEED_32GHZ:
5345                 rdp_speed = RDP_PS_32GB;
5346                 break;
5347         case LPFC_LINK_SPEED_64GHZ:
5348                 rdp_speed = RDP_PS_64GB;
5349                 break;
5350         default:
5351                 rdp_speed = RDP_PS_UNKNOWN;
5352                 break;
5353         }
5354
5355         desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
5356
5357         if (phba->lmt & LMT_64Gb)
5358                 rdp_cap |= RDP_PS_64GB;
5359         if (phba->lmt & LMT_32Gb)
5360                 rdp_cap |= RDP_PS_32GB;
5361         if (phba->lmt & LMT_16Gb)
5362                 rdp_cap |= RDP_PS_16GB;
5363         if (phba->lmt & LMT_10Gb)
5364                 rdp_cap |= RDP_PS_10GB;
5365         if (phba->lmt & LMT_8Gb)
5366                 rdp_cap |= RDP_PS_8GB;
5367         if (phba->lmt & LMT_4Gb)
5368                 rdp_cap |= RDP_PS_4GB;
5369         if (phba->lmt & LMT_2Gb)
5370                 rdp_cap |= RDP_PS_2GB;
5371         if (phba->lmt & LMT_1Gb)
5372                 rdp_cap |= RDP_PS_1GB;
5373
5374         if (rdp_cap == 0)
5375                 rdp_cap = RDP_CAP_UNKNOWN;
5376         if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
5377                 rdp_cap |= RDP_CAP_USER_CONFIGURED;
5378
5379         desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
5380         desc->length = cpu_to_be32(sizeof(desc->info));
5381         return sizeof(struct fc_rdp_port_speed_desc);
5382 }
5383
5384 static uint32_t
5385 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
5386                 struct lpfc_vport *vport)
5387 {
5388
5389         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5390
5391         memcpy(desc->port_names.wwnn, &vport->fc_nodename,
5392                         sizeof(desc->port_names.wwnn));
5393
5394         memcpy(desc->port_names.wwpn, &vport->fc_portname,
5395                         sizeof(desc->port_names.wwpn));
5396
5397         desc->length = cpu_to_be32(sizeof(desc->port_names));
5398         return sizeof(struct fc_rdp_port_name_desc);
5399 }
5400
5401 static uint32_t
5402 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
5403                 struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5404 {
5405
5406         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5407         if (vport->fc_flag & FC_FABRIC) {
5408                 memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
5409                                 sizeof(desc->port_names.wwnn));
5410
5411                 memcpy(desc->port_names.wwpn, &vport->fabric_portname,
5412                                 sizeof(desc->port_names.wwpn));
5413         } else {  /* Point to Point */
5414                 memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
5415                                 sizeof(desc->port_names.wwnn));
5416
5417                 memcpy(desc->port_names.wwnn, &ndlp->nlp_portname,
5418                                 sizeof(desc->port_names.wwpn));
5419         }
5420
5421         desc->length = cpu_to_be32(sizeof(desc->port_names));
5422         return sizeof(struct fc_rdp_port_name_desc);
5423 }
5424
5425 static void
5426 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
5427                 int status)
5428 {
5429         struct lpfc_nodelist *ndlp = rdp_context->ndlp;
5430         struct lpfc_vport *vport = ndlp->vport;
5431         struct lpfc_iocbq *elsiocb;
5432         struct ulp_bde64 *bpl;
5433         IOCB_t *icmd;
5434         uint8_t *pcmd;
5435         struct ls_rjt *stat;
5436         struct fc_rdp_res_frame *rdp_res;
5437         uint32_t cmdsize, len;
5438         uint16_t *flag_ptr;
5439         int rc;
5440
5441         if (status != SUCCESS)
5442                 goto error;
5443
5444         /* This will change once we know the true size of the RDP payload */
5445         cmdsize = sizeof(struct fc_rdp_res_frame);
5446
5447         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
5448                         lpfc_max_els_tries, rdp_context->ndlp,
5449                         rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
5450         lpfc_nlp_put(ndlp);
5451         if (!elsiocb)
5452                 goto free_rdp_context;
5453
5454         icmd = &elsiocb->iocb;
5455         icmd->ulpContext = rdp_context->rx_id;
5456         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5457
5458         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5459                         "2171 Xmit RDP response tag x%x xri x%x, "
5460                         "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
5461                         elsiocb->iotag, elsiocb->iocb.ulpContext,
5462                         ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5463                         ndlp->nlp_rpi);
5464         rdp_res = (struct fc_rdp_res_frame *)
5465                 (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5466         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5467         memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
5468         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5469
5470         /* Update Alarm and Warning */
5471         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
5472         phba->sfp_alarm |= *flag_ptr;
5473         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
5474         phba->sfp_warning |= *flag_ptr;
5475
5476         /* For RDP payload */
5477         len = 8;
5478         len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
5479                                          (len + pcmd), ELS_CMD_RDP);
5480
5481         len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
5482                         rdp_context->page_a0, rdp_context->page_a2);
5483         len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
5484                                   phba);
5485         len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
5486                                        (len + pcmd), &rdp_context->link_stat);
5487         len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
5488                                              (len + pcmd), vport);
5489         len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
5490                                         (len + pcmd), vport, ndlp);
5491         len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
5492                         &rdp_context->link_stat);
5493         len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
5494                                      &rdp_context->link_stat, vport);
5495         len += lpfc_rdp_res_oed_temp_desc(phba,
5496                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5497                                 rdp_context->page_a2);
5498         len += lpfc_rdp_res_oed_voltage_desc(phba,
5499                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5500                                 rdp_context->page_a2);
5501         len += lpfc_rdp_res_oed_txbias_desc(phba,
5502                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5503                                 rdp_context->page_a2);
5504         len += lpfc_rdp_res_oed_txpower_desc(phba,
5505                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5506                                 rdp_context->page_a2);
5507         len += lpfc_rdp_res_oed_rxpower_desc(phba,
5508                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5509                                 rdp_context->page_a2);
5510         len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
5511                                      rdp_context->page_a0, vport);
5512
5513         rdp_res->length = cpu_to_be32(len - 8);
5514         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5515
5516         /* Now that we know the true size of the payload, update the BPL */
5517         bpl = (struct ulp_bde64 *)
5518                 (((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
5519         bpl->tus.f.bdeSize = len;
5520         bpl->tus.f.bdeFlags = 0;
5521         bpl->tus.w = le32_to_cpu(bpl->tus.w);
5522
5523         phba->fc_stat.elsXmitACC++;
5524         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5525         if (rc == IOCB_ERROR)
5526                 lpfc_els_free_iocb(phba, elsiocb);
5527
5528         kfree(rdp_context);
5529
5530         return;
5531 error:
5532         cmdsize = 2 * sizeof(uint32_t);
5533         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
5534                         ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
5535         lpfc_nlp_put(ndlp);
5536         if (!elsiocb)
5537                 goto free_rdp_context;
5538
5539         icmd = &elsiocb->iocb;
5540         icmd->ulpContext = rdp_context->rx_id;
5541         icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5542         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5543
5544         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5545         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5546         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5547
5548         phba->fc_stat.elsXmitLSRJT++;
5549         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5550         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5551
5552         if (rc == IOCB_ERROR)
5553                 lpfc_els_free_iocb(phba, elsiocb);
5554 free_rdp_context:
5555         kfree(rdp_context);
5556 }
5557
5558 static int
5559 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
5560 {
5561         LPFC_MBOXQ_t *mbox = NULL;
5562         int rc;
5563
5564         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5565         if (!mbox) {
5566                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
5567                                 "7105 failed to allocate mailbox memory");
5568                 return 1;
5569         }
5570
5571         if (lpfc_sli4_dump_page_a0(phba, mbox))
5572                 goto prep_mbox_fail;
5573         mbox->vport = rdp_context->ndlp->vport;
5574         mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
5575         mbox->context2 = (struct lpfc_rdp_context *) rdp_context;
5576         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5577         if (rc == MBX_NOT_FINISHED)
5578                 goto issue_mbox_fail;
5579
5580         return 0;
5581
5582 prep_mbox_fail:
5583 issue_mbox_fail:
5584         mempool_free(mbox, phba->mbox_mem_pool);
5585         return 1;
5586 }
5587
5588 /*
5589  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
5590  * @vport: pointer to a host virtual N_Port data structure.
5591  * @cmdiocb: pointer to lpfc command iocb data structure.
5592  * @ndlp: pointer to a node-list data structure.
5593  *
5594  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
5595  * IOCB. First, the payload of the unsolicited RDP is checked.
5596  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
5597  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
5598  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
5599  * gather all data and send RDP response.
5600  *
5601  * Return code
5602  *   0 - Sent the acc response
5603  *   1 - Sent the reject response.
5604  */
5605 static int
5606 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5607                 struct lpfc_nodelist *ndlp)
5608 {
5609         struct lpfc_hba *phba = vport->phba;
5610         struct lpfc_dmabuf *pcmd;
5611         uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
5612         struct fc_rdp_req_frame *rdp_req;
5613         struct lpfc_rdp_context *rdp_context;
5614         IOCB_t *cmd = NULL;
5615         struct ls_rjt stat;
5616
5617         if (phba->sli_rev < LPFC_SLI_REV4 ||
5618             bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
5619                                                 LPFC_SLI_INTF_IF_TYPE_2) {
5620                 rjt_err = LSRJT_UNABLE_TPC;
5621                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5622                 goto error;
5623         }
5624
5625         if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
5626                 rjt_err = LSRJT_UNABLE_TPC;
5627                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5628                 goto error;
5629         }
5630
5631         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5632         rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
5633
5634         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5635                          "2422 ELS RDP Request "
5636                          "dec len %d tag x%x port_id %d len %d\n",
5637                          be32_to_cpu(rdp_req->rdp_des_length),
5638                          be32_to_cpu(rdp_req->nport_id_desc.tag),
5639                          be32_to_cpu(rdp_req->nport_id_desc.nport_id),
5640                          be32_to_cpu(rdp_req->nport_id_desc.length));
5641
5642         if (sizeof(struct fc_rdp_nport_desc) !=
5643                         be32_to_cpu(rdp_req->rdp_des_length))
5644                 goto rjt_logerr;
5645         if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
5646                 goto rjt_logerr;
5647         if (RDP_NPORT_ID_SIZE !=
5648                         be32_to_cpu(rdp_req->nport_id_desc.length))
5649                 goto rjt_logerr;
5650         rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
5651         if (!rdp_context) {
5652                 rjt_err = LSRJT_UNABLE_TPC;
5653                 goto error;
5654         }
5655
5656         cmd = &cmdiocb->iocb;
5657         rdp_context->ndlp = lpfc_nlp_get(ndlp);
5658         rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
5659         rdp_context->rx_id = cmd->ulpContext;
5660         rdp_context->cmpl = lpfc_els_rdp_cmpl;
5661         if (lpfc_get_rdp_info(phba, rdp_context)) {
5662                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
5663                                  "2423 Unable to send mailbox");
5664                 kfree(rdp_context);
5665                 rjt_err = LSRJT_UNABLE_TPC;
5666                 lpfc_nlp_put(ndlp);
5667                 goto error;
5668         }
5669
5670         return 0;
5671
5672 rjt_logerr:
5673         rjt_err = LSRJT_LOGICAL_ERR;
5674
5675 error:
5676         memset(&stat, 0, sizeof(stat));
5677         stat.un.b.lsRjtRsnCode = rjt_err;
5678         stat.un.b.lsRjtRsnCodeExp = rjt_expl;
5679         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5680         return 1;
5681 }
5682
5683
5684 static void
5685 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5686 {
5687         MAILBOX_t *mb;
5688         IOCB_t *icmd;
5689         uint8_t *pcmd;
5690         struct lpfc_iocbq *elsiocb;
5691         struct lpfc_nodelist *ndlp;
5692         struct ls_rjt *stat;
5693         union lpfc_sli4_cfg_shdr *shdr;
5694         struct lpfc_lcb_context *lcb_context;
5695         struct fc_lcb_res_frame *lcb_res;
5696         uint32_t cmdsize, shdr_status, shdr_add_status;
5697         int rc;
5698
5699         mb = &pmb->u.mb;
5700         lcb_context = (struct lpfc_lcb_context *)pmb->context1;
5701         ndlp = lcb_context->ndlp;
5702         pmb->context1 = NULL;
5703         pmb->context2 = NULL;
5704
5705         shdr = (union lpfc_sli4_cfg_shdr *)
5706                         &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
5707         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5708         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5709
5710         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
5711                                 "0194 SET_BEACON_CONFIG mailbox "
5712                                 "completed with status x%x add_status x%x,"
5713                                 " mbx status x%x\n",
5714                                 shdr_status, shdr_add_status, mb->mbxStatus);
5715
5716         if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
5717             (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
5718             (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
5719                 mempool_free(pmb, phba->mbox_mem_pool);
5720                 goto error;
5721         }
5722
5723         mempool_free(pmb, phba->mbox_mem_pool);
5724         cmdsize = sizeof(struct fc_lcb_res_frame);
5725         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5726                         lpfc_max_els_tries, ndlp,
5727                         ndlp->nlp_DID, ELS_CMD_ACC);
5728
5729         /* Decrement the ndlp reference count from previous mbox command */
5730         lpfc_nlp_put(ndlp);
5731
5732         if (!elsiocb)
5733                 goto free_lcb_context;
5734
5735         lcb_res = (struct fc_lcb_res_frame *)
5736                 (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5737
5738         memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
5739         icmd = &elsiocb->iocb;
5740         icmd->ulpContext = lcb_context->rx_id;
5741         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
5742
5743         pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5744         *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
5745         lcb_res->lcb_sub_command = lcb_context->sub_command;
5746         lcb_res->lcb_type = lcb_context->type;
5747         lcb_res->capability = lcb_context->capability;
5748         lcb_res->lcb_frequency = lcb_context->frequency;
5749         lcb_res->lcb_duration = lcb_context->duration;
5750         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5751         phba->fc_stat.elsXmitACC++;
5752         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5753         if (rc == IOCB_ERROR)
5754                 lpfc_els_free_iocb(phba, elsiocb);
5755
5756         kfree(lcb_context);
5757         return;
5758
5759 error:
5760         cmdsize = sizeof(struct fc_lcb_res_frame);
5761         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5762                         lpfc_max_els_tries, ndlp,
5763                         ndlp->nlp_DID, ELS_CMD_LS_RJT);
5764         lpfc_nlp_put(ndlp);
5765         if (!elsiocb)
5766                 goto free_lcb_context;
5767
5768         icmd = &elsiocb->iocb;
5769         icmd->ulpContext = lcb_context->rx_id;
5770         icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
5771         pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5772
5773         *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
5774         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5775         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5776
5777         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5778         phba->fc_stat.elsXmitLSRJT++;
5779         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5780         if (rc == IOCB_ERROR)
5781                 lpfc_els_free_iocb(phba, elsiocb);
5782 free_lcb_context:
5783         kfree(lcb_context);
5784 }
5785
5786 static int
5787 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
5788                      struct lpfc_lcb_context *lcb_context,
5789                      uint32_t beacon_state)
5790 {
5791         struct lpfc_hba *phba = vport->phba;
5792         union lpfc_sli4_cfg_shdr *cfg_shdr;
5793         LPFC_MBOXQ_t *mbox = NULL;
5794         uint32_t len;
5795         int rc;
5796
5797         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5798         if (!mbox)
5799                 return 1;
5800
5801         cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
5802         len = sizeof(struct lpfc_mbx_set_beacon_config) -
5803                 sizeof(struct lpfc_sli4_cfg_mhdr);
5804         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5805                          LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
5806                          LPFC_SLI4_MBX_EMBED);
5807         mbox->context1 = (void *)lcb_context;
5808         mbox->vport = phba->pport;
5809         mbox->mbox_cmpl = lpfc_els_lcb_rsp;
5810         bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
5811                phba->sli4_hba.physical_port);
5812         bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
5813                beacon_state);
5814         mbox->u.mqe.un.beacon_config.word5 = 0;         /* Reserved */
5815
5816         /*
5817          *      Check bv1s bit before issuing the mailbox
5818          *      if bv1s == 1, LCB V1 supported
5819          *      else, LCB V0 supported
5820          */
5821
5822         if (phba->sli4_hba.pc_sli4_params.bv1s) {
5823                 /* COMMON_SET_BEACON_CONFIG_V1 */
5824                 cfg_shdr->request.word9 = BEACON_VERSION_V1;
5825                 lcb_context->capability |= LCB_CAPABILITY_DURATION;
5826                 bf_set(lpfc_mbx_set_beacon_port_type,
5827                        &mbox->u.mqe.un.beacon_config, 0);
5828                 bf_set(lpfc_mbx_set_beacon_duration_v1,
5829                        &mbox->u.mqe.un.beacon_config,
5830                        be16_to_cpu(lcb_context->duration));
5831         } else {
5832                 /* COMMON_SET_BEACON_CONFIG_V0 */
5833                 if (be16_to_cpu(lcb_context->duration) != 0) {
5834                         mempool_free(mbox, phba->mbox_mem_pool);
5835                         return 1;
5836                 }
5837                 cfg_shdr->request.word9 = BEACON_VERSION_V0;
5838                 lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
5839                 bf_set(lpfc_mbx_set_beacon_state,
5840                        &mbox->u.mqe.un.beacon_config, beacon_state);
5841                 bf_set(lpfc_mbx_set_beacon_port_type,
5842                        &mbox->u.mqe.un.beacon_config, 1);
5843                 bf_set(lpfc_mbx_set_beacon_duration,
5844                        &mbox->u.mqe.un.beacon_config,
5845                        be16_to_cpu(lcb_context->duration));
5846         }
5847
5848         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5849         if (rc == MBX_NOT_FINISHED) {
5850                 mempool_free(mbox, phba->mbox_mem_pool);
5851                 return 1;
5852         }
5853
5854         return 0;
5855 }
5856
5857
5858 /**
5859  * lpfc_els_rcv_lcb - Process an unsolicited LCB
5860  * @vport: pointer to a host virtual N_Port data structure.
5861  * @cmdiocb: pointer to lpfc command iocb data structure.
5862  * @ndlp: pointer to a node-list data structure.
5863  *
5864  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
5865  * First, the payload of the unsolicited LCB is checked.
5866  * Then based on Subcommand beacon will either turn on or off.
5867  *
5868  * Return code
5869  * 0 - Sent the acc response
5870  * 1 - Sent the reject response.
5871  **/
5872 static int
5873 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5874                  struct lpfc_nodelist *ndlp)
5875 {
5876         struct lpfc_hba *phba = vport->phba;
5877         struct lpfc_dmabuf *pcmd;
5878         uint8_t *lp;
5879         struct fc_lcb_request_frame *beacon;
5880         struct lpfc_lcb_context *lcb_context;
5881         uint8_t state, rjt_err;
5882         struct ls_rjt stat;
5883
5884         pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
5885         lp = (uint8_t *)pcmd->virt;
5886         beacon = (struct fc_lcb_request_frame *)pcmd->virt;
5887
5888         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5889                         "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
5890                         "type x%x frequency %x duration x%x\n",
5891                         lp[0], lp[1], lp[2],
5892                         beacon->lcb_command,
5893                         beacon->lcb_sub_command,
5894                         beacon->lcb_type,
5895                         beacon->lcb_frequency,
5896                         be16_to_cpu(beacon->lcb_duration));
5897
5898         if (beacon->lcb_sub_command != LPFC_LCB_ON &&
5899             beacon->lcb_sub_command != LPFC_LCB_OFF) {
5900                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5901                 goto rjt;
5902         }
5903
5904         if (phba->sli_rev < LPFC_SLI_REV4  ||
5905             phba->hba_flag & HBA_FCOE_MODE ||
5906             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
5907             LPFC_SLI_INTF_IF_TYPE_2)) {
5908                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5909                 goto rjt;
5910         }
5911
5912         lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
5913         if (!lcb_context) {
5914                 rjt_err = LSRJT_UNABLE_TPC;
5915                 goto rjt;
5916         }
5917
5918         state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
5919         lcb_context->sub_command = beacon->lcb_sub_command;
5920         lcb_context->capability = 0;
5921         lcb_context->type = beacon->lcb_type;
5922         lcb_context->frequency = beacon->lcb_frequency;
5923         lcb_context->duration = beacon->lcb_duration;
5924         lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
5925         lcb_context->rx_id = cmdiocb->iocb.ulpContext;
5926         lcb_context->ndlp = lpfc_nlp_get(ndlp);
5927         if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
5928                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
5929                                  LOG_ELS, "0193 failed to send mail box");
5930                 kfree(lcb_context);
5931                 lpfc_nlp_put(ndlp);
5932                 rjt_err = LSRJT_UNABLE_TPC;
5933                 goto rjt;
5934         }
5935         return 0;
5936 rjt:
5937         memset(&stat, 0, sizeof(stat));
5938         stat.un.b.lsRjtRsnCode = rjt_err;
5939         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5940         return 1;
5941 }
5942
5943
5944 /**
5945  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
5946  * @vport: pointer to a host virtual N_Port data structure.
5947  *
5948  * This routine cleans up any Registration State Change Notification
5949  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
5950  * @vport together with the host_lock is used to prevent multiple thread
5951  * trying to access the RSCN array on a same @vport at the same time.
5952  **/
5953 void
5954 lpfc_els_flush_rscn(struct lpfc_vport *vport)
5955 {
5956         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5957         struct lpfc_hba  *phba = vport->phba;
5958         int i;
5959
5960         spin_lock_irq(shost->host_lock);
5961         if (vport->fc_rscn_flush) {
5962                 /* Another thread is walking fc_rscn_id_list on this vport */
5963                 spin_unlock_irq(shost->host_lock);
5964                 return;
5965         }
5966         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
5967         vport->fc_rscn_flush = 1;
5968         spin_unlock_irq(shost->host_lock);
5969
5970         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
5971                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
5972                 vport->fc_rscn_id_list[i] = NULL;
5973         }
5974         spin_lock_irq(shost->host_lock);
5975         vport->fc_rscn_id_cnt = 0;
5976         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
5977         spin_unlock_irq(shost->host_lock);
5978         lpfc_can_disctmo(vport);
5979         /* Indicate we are done walking this fc_rscn_id_list */
5980         vport->fc_rscn_flush = 0;
5981 }
5982
5983 /**
5984  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
5985  * @vport: pointer to a host virtual N_Port data structure.
5986  * @did: remote destination port identifier.
5987  *
5988  * This routine checks whether there is any pending Registration State
5989  * Configuration Notification (RSCN) to a @did on @vport.
5990  *
5991  * Return code
5992  *   None zero - The @did matched with a pending rscn
5993  *   0 - not able to match @did with a pending rscn
5994  **/
5995 int
5996 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
5997 {
5998         D_ID ns_did;
5999         D_ID rscn_did;
6000         uint32_t *lp;
6001         uint32_t payload_len, i;
6002         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6003
6004         ns_did.un.word = did;
6005
6006         /* Never match fabric nodes for RSCNs */
6007         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6008                 return 0;
6009
6010         /* If we are doing a FULL RSCN rediscovery, match everything */
6011         if (vport->fc_flag & FC_RSCN_DISCOVERY)
6012                 return did;
6013
6014         spin_lock_irq(shost->host_lock);
6015         if (vport->fc_rscn_flush) {
6016                 /* Another thread is walking fc_rscn_id_list on this vport */
6017                 spin_unlock_irq(shost->host_lock);
6018                 return 0;
6019         }
6020         /* Indicate we are walking fc_rscn_id_list on this vport */
6021         vport->fc_rscn_flush = 1;
6022         spin_unlock_irq(shost->host_lock);
6023         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6024                 lp = vport->fc_rscn_id_list[i]->virt;
6025                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6026                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
6027                 while (payload_len) {
6028                         rscn_did.un.word = be32_to_cpu(*lp++);
6029                         payload_len -= sizeof(uint32_t);
6030                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
6031                         case RSCN_ADDRESS_FORMAT_PORT:
6032                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6033                                     && (ns_did.un.b.area == rscn_did.un.b.area)
6034                                     && (ns_did.un.b.id == rscn_did.un.b.id))
6035                                         goto return_did_out;
6036                                 break;
6037                         case RSCN_ADDRESS_FORMAT_AREA:
6038                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6039                                     && (ns_did.un.b.area == rscn_did.un.b.area))
6040                                         goto return_did_out;
6041                                 break;
6042                         case RSCN_ADDRESS_FORMAT_DOMAIN:
6043                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
6044                                         goto return_did_out;
6045                                 break;
6046                         case RSCN_ADDRESS_FORMAT_FABRIC:
6047                                 goto return_did_out;
6048                         }
6049                 }
6050         }
6051         /* Indicate we are done with walking fc_rscn_id_list on this vport */
6052         vport->fc_rscn_flush = 0;
6053         return 0;
6054 return_did_out:
6055         /* Indicate we are done with walking fc_rscn_id_list on this vport */
6056         vport->fc_rscn_flush = 0;
6057         return did;
6058 }
6059
6060 /**
6061  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
6062  * @vport: pointer to a host virtual N_Port data structure.
6063  *
6064  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
6065  * state machine for a @vport's nodes that are with pending RSCN (Registration
6066  * State Change Notification).
6067  *
6068  * Return code
6069  *   0 - Successful (currently alway return 0)
6070  **/
6071 static int
6072 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
6073 {
6074         struct lpfc_nodelist *ndlp = NULL;
6075
6076         /* Move all affected nodes by pending RSCNs to NPR state. */
6077         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6078                 if (!NLP_CHK_NODE_ACT(ndlp) ||
6079                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
6080                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
6081                         continue;
6082
6083                 /* NVME Target mode does not do RSCN Recovery. */
6084                 if (vport->phba->nvmet_support)
6085                         continue;
6086
6087                 lpfc_disc_state_machine(vport, ndlp, NULL,
6088                                         NLP_EVT_DEVICE_RECOVERY);
6089                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
6090         }
6091         return 0;
6092 }
6093
6094 /**
6095  * lpfc_send_rscn_event - Send an RSCN event to management application
6096  * @vport: pointer to a host virtual N_Port data structure.
6097  * @cmdiocb: pointer to lpfc command iocb data structure.
6098  *
6099  * lpfc_send_rscn_event sends an RSCN netlink event to management
6100  * applications.
6101  */
6102 static void
6103 lpfc_send_rscn_event(struct lpfc_vport *vport,
6104                 struct lpfc_iocbq *cmdiocb)
6105 {
6106         struct lpfc_dmabuf *pcmd;
6107         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6108         uint32_t *payload_ptr;
6109         uint32_t payload_len;
6110         struct lpfc_rscn_event_header *rscn_event_data;
6111
6112         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6113         payload_ptr = (uint32_t *) pcmd->virt;
6114         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
6115
6116         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
6117                 payload_len, GFP_KERNEL);
6118         if (!rscn_event_data) {
6119                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6120                         "0147 Failed to allocate memory for RSCN event\n");
6121                 return;
6122         }
6123         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
6124         rscn_event_data->payload_length = payload_len;
6125         memcpy(rscn_event_data->rscn_payload, payload_ptr,
6126                 payload_len);
6127
6128         fc_host_post_vendor_event(shost,
6129                 fc_get_event_number(),
6130                 sizeof(struct lpfc_rscn_event_header) + payload_len,
6131                 (char *)rscn_event_data,
6132                 LPFC_NL_VENDOR_ID);
6133
6134         kfree(rscn_event_data);
6135 }
6136
6137 /**
6138  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
6139  * @vport: pointer to a host virtual N_Port data structure.
6140  * @cmdiocb: pointer to lpfc command iocb data structure.
6141  * @ndlp: pointer to a node-list data structure.
6142  *
6143  * This routine processes an unsolicited RSCN (Registration State Change
6144  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
6145  * to invoke fc_host_post_event() routine to the FC transport layer. If the
6146  * discover state machine is about to begin discovery, it just accepts the
6147  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
6148  * contains N_Port IDs for other vports on this HBA, it just accepts the
6149  * RSCN and ignore processing it. If the state machine is in the recovery
6150  * state, the fc_rscn_id_list of this @vport is walked and the
6151  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
6152  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
6153  * routine is invoked to handle the RSCN event.
6154  *
6155  * Return code
6156  *   0 - Just sent the acc response
6157  *   1 - Sent the acc response and waited for name server completion
6158  **/
6159 static int
6160 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6161                   struct lpfc_nodelist *ndlp)
6162 {
6163         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6164         struct lpfc_hba  *phba = vport->phba;
6165         struct lpfc_dmabuf *pcmd;
6166         uint32_t *lp, *datap;
6167         uint32_t payload_len, length, nportid, *cmd;
6168         int rscn_cnt;
6169         int rscn_id = 0, hba_id = 0;
6170         int i;
6171
6172         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6173         lp = (uint32_t *) pcmd->virt;
6174
6175         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6176         payload_len -= sizeof(uint32_t);        /* take off word 0 */
6177         /* RSCN received */
6178         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6179                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
6180                          vport->fc_flag, payload_len, *lp,
6181                          vport->fc_rscn_id_cnt);
6182
6183         /* Send an RSCN event to the management application */
6184         lpfc_send_rscn_event(vport, cmdiocb);
6185
6186         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
6187                 fc_host_post_event(shost, fc_get_event_number(),
6188                         FCH_EVT_RSCN, lp[i]);
6189
6190         /* If we are about to begin discovery, just ACC the RSCN.
6191          * Discovery processing will satisfy it.
6192          */
6193         if (vport->port_state <= LPFC_NS_QRY) {
6194                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6195                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
6196                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6197
6198                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6199                 return 0;
6200         }
6201
6202         /* If this RSCN just contains NPortIDs for other vports on this HBA,
6203          * just ACC and ignore it.
6204          */
6205         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6206                 !(vport->cfg_peer_port_login)) {
6207                 i = payload_len;
6208                 datap = lp;
6209                 while (i > 0) {
6210                         nportid = *datap++;
6211                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
6212                         i -= sizeof(uint32_t);
6213                         rscn_id++;
6214                         if (lpfc_find_vport_by_did(phba, nportid))
6215                                 hba_id++;
6216                 }
6217                 if (rscn_id == hba_id) {
6218                         /* ALL NPortIDs in RSCN are on HBA */
6219                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6220                                          "0219 Ignore RSCN "
6221                                          "Data: x%x x%x x%x x%x\n",
6222                                          vport->fc_flag, payload_len,
6223                                          *lp, vport->fc_rscn_id_cnt);
6224                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6225                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
6226                                 ndlp->nlp_DID, vport->port_state,
6227                                 ndlp->nlp_flag);
6228
6229                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
6230                                 ndlp, NULL);
6231                         return 0;
6232                 }
6233         }
6234
6235         spin_lock_irq(shost->host_lock);
6236         if (vport->fc_rscn_flush) {
6237                 /* Another thread is walking fc_rscn_id_list on this vport */
6238                 vport->fc_flag |= FC_RSCN_DISCOVERY;
6239                 spin_unlock_irq(shost->host_lock);
6240                 /* Send back ACC */
6241                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6242                 return 0;
6243         }
6244         /* Indicate we are walking fc_rscn_id_list on this vport */
6245         vport->fc_rscn_flush = 1;
6246         spin_unlock_irq(shost->host_lock);
6247         /* Get the array count after successfully have the token */
6248         rscn_cnt = vport->fc_rscn_id_cnt;
6249         /* If we are already processing an RSCN, save the received
6250          * RSCN payload buffer, cmdiocb->context2 to process later.
6251          */
6252         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
6253                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6254                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
6255                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6256
6257                 spin_lock_irq(shost->host_lock);
6258                 vport->fc_flag |= FC_RSCN_DEFERRED;
6259                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
6260                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
6261                         vport->fc_flag |= FC_RSCN_MODE;
6262                         spin_unlock_irq(shost->host_lock);
6263                         if (rscn_cnt) {
6264                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
6265                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
6266                         }
6267                         if ((rscn_cnt) &&
6268                             (payload_len + length <= LPFC_BPL_SIZE)) {
6269                                 *cmd &= ELS_CMD_MASK;
6270                                 *cmd |= cpu_to_be32(payload_len + length);
6271                                 memcpy(((uint8_t *)cmd) + length, lp,
6272                                        payload_len);
6273                         } else {
6274                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
6275                                 vport->fc_rscn_id_cnt++;
6276                                 /* If we zero, cmdiocb->context2, the calling
6277                                  * routine will not try to free it.
6278                                  */
6279                                 cmdiocb->context2 = NULL;
6280                         }
6281                         /* Deferred RSCN */
6282                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6283                                          "0235 Deferred RSCN "
6284                                          "Data: x%x x%x x%x\n",
6285                                          vport->fc_rscn_id_cnt, vport->fc_flag,
6286                                          vport->port_state);
6287                 } else {
6288                         vport->fc_flag |= FC_RSCN_DISCOVERY;
6289                         spin_unlock_irq(shost->host_lock);
6290                         /* ReDiscovery RSCN */
6291                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6292                                          "0234 ReDiscovery RSCN "
6293                                          "Data: x%x x%x x%x\n",
6294                                          vport->fc_rscn_id_cnt, vport->fc_flag,
6295                                          vport->port_state);
6296                 }
6297                 /* Indicate we are done walking fc_rscn_id_list on this vport */
6298                 vport->fc_rscn_flush = 0;
6299                 /* Send back ACC */
6300                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6301                 /* send RECOVERY event for ALL nodes that match RSCN payload */
6302                 lpfc_rscn_recovery_check(vport);
6303                 return 0;
6304         }
6305         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6306                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
6307                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6308
6309         spin_lock_irq(shost->host_lock);
6310         vport->fc_flag |= FC_RSCN_MODE;
6311         spin_unlock_irq(shost->host_lock);
6312         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
6313         /* Indicate we are done walking fc_rscn_id_list on this vport */
6314         vport->fc_rscn_flush = 0;
6315         /*
6316          * If we zero, cmdiocb->context2, the calling routine will
6317          * not try to free it.
6318          */
6319         cmdiocb->context2 = NULL;
6320         lpfc_set_disctmo(vport);
6321         /* Send back ACC */
6322         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6323         /* send RECOVERY event for ALL nodes that match RSCN payload */
6324         lpfc_rscn_recovery_check(vport);
6325         return lpfc_els_handle_rscn(vport);
6326 }
6327
6328 /**
6329  * lpfc_els_handle_rscn - Handle rscn for a vport
6330  * @vport: pointer to a host virtual N_Port data structure.
6331  *
6332  * This routine handles the Registration State Configuration Notification
6333  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
6334  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
6335  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
6336  * NameServer shall be issued. If CT command to the NameServer fails to be
6337  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
6338  * RSCN activities with the @vport.
6339  *
6340  * Return code
6341  *   0 - Cleaned up rscn on the @vport
6342  *   1 - Wait for plogi to name server before proceed
6343  **/
6344 int
6345 lpfc_els_handle_rscn(struct lpfc_vport *vport)
6346 {
6347         struct lpfc_nodelist *ndlp;
6348
6349         /* Ignore RSCN if the port is being torn down. */
6350         if (vport->load_flag & FC_UNLOADING) {
6351                 lpfc_els_flush_rscn(vport);
6352                 return 0;
6353         }
6354
6355         /* Start timer for RSCN processing */
6356         lpfc_set_disctmo(vport);
6357
6358         /* RSCN processed */
6359         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6360                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
6361                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
6362                          vport->port_state);
6363
6364         /* To process RSCN, first compare RSCN data with NameServer */
6365         vport->fc_ns_retry = 0;
6366         vport->num_disc_nodes = 0;
6367
6368         ndlp = lpfc_findnode_did(vport, NameServer_DID);
6369         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
6370             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
6371                 /* Good ndlp, issue CT Request to NameServer.  Need to
6372                  * know how many gidfts were issued.  If none, then just
6373                  * flush the RSCN.  Otherwise, the outstanding requests
6374                  * need to complete.
6375                  */
6376                 if (lpfc_issue_gidft(vport) > 0)
6377                         return 1;
6378         } else {
6379                 /* Nameserver login in question.  Revalidate. */
6380                 if (ndlp) {
6381                         ndlp = lpfc_enable_node(vport, ndlp,
6382                                                 NLP_STE_PLOGI_ISSUE);
6383                         if (!ndlp) {
6384                                 lpfc_els_flush_rscn(vport);
6385                                 return 0;
6386                         }
6387                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
6388                 } else {
6389                         ndlp = lpfc_nlp_init(vport, NameServer_DID);
6390                         if (!ndlp) {
6391                                 lpfc_els_flush_rscn(vport);
6392                                 return 0;
6393                         }
6394                         ndlp->nlp_prev_state = ndlp->nlp_state;
6395                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6396                 }
6397                 ndlp->nlp_type |= NLP_FABRIC;
6398                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
6399                 /* Wait for NameServer login cmpl before we can
6400                  * continue
6401                  */
6402                 return 1;
6403         }
6404
6405         lpfc_els_flush_rscn(vport);
6406         return 0;
6407 }
6408
6409 /**
6410  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
6411  * @vport: pointer to a host virtual N_Port data structure.
6412  * @cmdiocb: pointer to lpfc command iocb data structure.
6413  * @ndlp: pointer to a node-list data structure.
6414  *
6415  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
6416  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
6417  * point topology. As an unsolicited FLOGI should not be received in a loop
6418  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
6419  * lpfc_check_sparm() routine is invoked to check the parameters in the
6420  * unsolicited FLOGI. If parameters validation failed, the routine
6421  * lpfc_els_rsp_reject() shall be called with reject reason code set to
6422  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
6423  * FLOGI shall be compared with the Port WWN of the @vport to determine who
6424  * will initiate PLOGI. The higher lexicographical value party shall has
6425  * higher priority (as the winning port) and will initiate PLOGI and
6426  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
6427  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
6428  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
6429  *
6430  * Return code
6431  *   0 - Successfully processed the unsolicited flogi
6432  *   1 - Failed to process the unsolicited flogi
6433  **/
6434 static int
6435 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6436                    struct lpfc_nodelist *ndlp)
6437 {
6438         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6439         struct lpfc_hba  *phba = vport->phba;
6440         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6441         uint32_t *lp = (uint32_t *) pcmd->virt;
6442         IOCB_t *icmd = &cmdiocb->iocb;
6443         struct serv_parm *sp;
6444         LPFC_MBOXQ_t *mbox;
6445         uint32_t cmd, did;
6446         int rc;
6447         uint32_t fc_flag = 0;
6448         uint32_t port_state = 0;
6449
6450         cmd = *lp++;
6451         sp = (struct serv_parm *) lp;
6452
6453         /* FLOGI received */
6454
6455         lpfc_set_disctmo(vport);
6456
6457         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6458                 /* We should never receive a FLOGI in loop mode, ignore it */
6459                 did = icmd->un.elsreq64.remoteID;
6460
6461                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
6462                    Loop Mode */
6463                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6464                                  "0113 An FLOGI ELS command x%x was "
6465                                  "received from DID x%x in Loop Mode\n",
6466                                  cmd, did);
6467                 return 1;
6468         }
6469
6470         (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
6471
6472         /*
6473          * If our portname is greater than the remote portname,
6474          * then we initiate Nport login.
6475          */
6476
6477         rc = memcmp(&vport->fc_portname, &sp->portName,
6478                     sizeof(struct lpfc_name));
6479
6480         if (!rc) {
6481                 if (phba->sli_rev < LPFC_SLI_REV4) {
6482                         mbox = mempool_alloc(phba->mbox_mem_pool,
6483                                              GFP_KERNEL);
6484                         if (!mbox)
6485                                 return 1;
6486                         lpfc_linkdown(phba);
6487                         lpfc_init_link(phba, mbox,
6488                                        phba->cfg_topology,
6489                                        phba->cfg_link_speed);
6490                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6491                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6492                         mbox->vport = vport;
6493                         rc = lpfc_sli_issue_mbox(phba, mbox,
6494                                                  MBX_NOWAIT);
6495                         lpfc_set_loopback_flag(phba);
6496                         if (rc == MBX_NOT_FINISHED)
6497                                 mempool_free(mbox, phba->mbox_mem_pool);
6498                         return 1;
6499                 }
6500
6501                 /* abort the flogi coming back to ourselves
6502                  * due to external loopback on the port.
6503                  */
6504                 lpfc_els_abort_flogi(phba);
6505                 return 0;
6506
6507         } else if (rc > 0) {    /* greater than */
6508                 spin_lock_irq(shost->host_lock);
6509                 vport->fc_flag |= FC_PT2PT_PLOGI;
6510                 spin_unlock_irq(shost->host_lock);
6511
6512                 /* If we have the high WWPN we can assign our own
6513                  * myDID; otherwise, we have to WAIT for a PLOGI
6514                  * from the remote NPort to find out what it
6515                  * will be.
6516                  */
6517                 vport->fc_myDID = PT2PT_LocalID;
6518         } else {
6519                 vport->fc_myDID = PT2PT_RemoteID;
6520         }
6521
6522         /*
6523          * The vport state should go to LPFC_FLOGI only
6524          * AFTER we issue a FLOGI, not receive one.
6525          */
6526         spin_lock_irq(shost->host_lock);
6527         fc_flag = vport->fc_flag;
6528         port_state = vport->port_state;
6529         vport->fc_flag |= FC_PT2PT;
6530         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6531         spin_unlock_irq(shost->host_lock);
6532         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6533                          "3311 Rcv Flogi PS x%x new PS x%x "
6534                          "fc_flag x%x new fc_flag x%x\n",
6535                          port_state, vport->port_state,
6536                          fc_flag, vport->fc_flag);
6537
6538         /*
6539          * We temporarily set fc_myDID to make it look like we are
6540          * a Fabric. This is done just so we end up with the right
6541          * did / sid on the FLOGI ACC rsp.
6542          */
6543         did = vport->fc_myDID;
6544         vport->fc_myDID = Fabric_DID;
6545
6546         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
6547
6548         /* Send back ACC */
6549         lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
6550
6551         /* Now lets put fc_myDID back to what its supposed to be */
6552         vport->fc_myDID = did;
6553
6554         return 0;
6555 }
6556
6557 /**
6558  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
6559  * @vport: pointer to a host virtual N_Port data structure.
6560  * @cmdiocb: pointer to lpfc command iocb data structure.
6561  * @ndlp: pointer to a node-list data structure.
6562  *
6563  * This routine processes Request Node Identification Data (RNID) IOCB
6564  * received as an ELS unsolicited event. Only when the RNID specified format
6565  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
6566  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
6567  * Accept (ACC) the RNID ELS command. All the other RNID formats are
6568  * rejected by invoking the lpfc_els_rsp_reject() routine.
6569  *
6570  * Return code
6571  *   0 - Successfully processed rnid iocb (currently always return 0)
6572  **/
6573 static int
6574 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6575                   struct lpfc_nodelist *ndlp)
6576 {
6577         struct lpfc_dmabuf *pcmd;
6578         uint32_t *lp;
6579         RNID *rn;
6580         struct ls_rjt stat;
6581         uint32_t cmd;
6582
6583         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6584         lp = (uint32_t *) pcmd->virt;
6585
6586         cmd = *lp++;
6587         rn = (RNID *) lp;
6588
6589         /* RNID received */
6590
6591         switch (rn->Format) {
6592         case 0:
6593         case RNID_TOPOLOGY_DISC:
6594                 /* Send back ACC */
6595                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
6596                 break;
6597         default:
6598                 /* Reject this request because format not supported */
6599                 stat.un.b.lsRjtRsvd0 = 0;
6600                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6601                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6602                 stat.un.b.vendorUnique = 0;
6603                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6604                         NULL);
6605         }
6606         return 0;
6607 }
6608
6609 /**
6610  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
6611  * @vport: pointer to a host virtual N_Port data structure.
6612  * @cmdiocb: pointer to lpfc command iocb data structure.
6613  * @ndlp: pointer to a node-list data structure.
6614  *
6615  * Return code
6616  *   0 - Successfully processed echo iocb (currently always return 0)
6617  **/
6618 static int
6619 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6620                   struct lpfc_nodelist *ndlp)
6621 {
6622         uint8_t *pcmd;
6623
6624         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
6625
6626         /* skip over first word of echo command to find echo data */
6627         pcmd += sizeof(uint32_t);
6628
6629         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
6630         return 0;
6631 }
6632
6633 /**
6634  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
6635  * @vport: pointer to a host virtual N_Port data structure.
6636  * @cmdiocb: pointer to lpfc command iocb data structure.
6637  * @ndlp: pointer to a node-list data structure.
6638  *
6639  * This routine processes a Link Incident Report Registration(LIRR) IOCB
6640  * received as an ELS unsolicited event. Currently, this function just invokes
6641  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
6642  *
6643  * Return code
6644  *   0 - Successfully processed lirr iocb (currently always return 0)
6645  **/
6646 static int
6647 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6648                   struct lpfc_nodelist *ndlp)
6649 {
6650         struct ls_rjt stat;
6651
6652         /* For now, unconditionally reject this command */
6653         stat.un.b.lsRjtRsvd0 = 0;
6654         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6655         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6656         stat.un.b.vendorUnique = 0;
6657         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6658         return 0;
6659 }
6660
6661 /**
6662  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
6663  * @vport: pointer to a host virtual N_Port data structure.
6664  * @cmdiocb: pointer to lpfc command iocb data structure.
6665  * @ndlp: pointer to a node-list data structure.
6666  *
6667  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
6668  * received as an ELS unsolicited event. A request to RRQ shall only
6669  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
6670  * Nx_Port N_Port_ID of the target Exchange is the same as the
6671  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
6672  * not accepted, an LS_RJT with reason code "Unable to perform
6673  * command request" and reason code explanation "Invalid Originator
6674  * S_ID" shall be returned. For now, we just unconditionally accept
6675  * RRQ from the target.
6676  **/
6677 static void
6678 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6679                  struct lpfc_nodelist *ndlp)
6680 {
6681         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6682         if (vport->phba->sli_rev == LPFC_SLI_REV4)
6683                 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
6684 }
6685
6686 /**
6687  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6688  * @phba: pointer to lpfc hba data structure.
6689  * @pmb: pointer to the driver internal queue element for mailbox command.
6690  *
6691  * This routine is the completion callback function for the MBX_READ_LNK_STAT
6692  * mailbox command. This callback function is to actually send the Accept
6693  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6694  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6695  * mailbox command, constructs the RPS response with the link statistics
6696  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6697  * response to the RPS.
6698  *
6699  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6700  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6701  * will be stored into the context1 field of the IOCB for the completion
6702  * callback function to the RPS Accept Response ELS IOCB command.
6703  *
6704  **/
6705 static void
6706 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6707 {
6708         MAILBOX_t *mb;
6709         IOCB_t *icmd;
6710         struct RLS_RSP *rls_rsp;
6711         uint8_t *pcmd;
6712         struct lpfc_iocbq *elsiocb;
6713         struct lpfc_nodelist *ndlp;
6714         uint16_t oxid;
6715         uint16_t rxid;
6716         uint32_t cmdsize;
6717
6718         mb = &pmb->u.mb;
6719
6720         ndlp = (struct lpfc_nodelist *) pmb->context2;
6721         rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
6722         oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
6723         pmb->context1 = NULL;
6724         pmb->context2 = NULL;
6725
6726         if (mb->mbxStatus) {
6727                 mempool_free(pmb, phba->mbox_mem_pool);
6728                 return;
6729         }
6730
6731         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
6732         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6733                                      lpfc_max_els_tries, ndlp,
6734                                      ndlp->nlp_DID, ELS_CMD_ACC);
6735
6736         /* Decrement the ndlp reference count from previous mbox command */
6737         lpfc_nlp_put(ndlp);
6738
6739         if (!elsiocb) {
6740                 mempool_free(pmb, phba->mbox_mem_pool);
6741                 return;
6742         }
6743
6744         icmd = &elsiocb->iocb;
6745         icmd->ulpContext = rxid;
6746         icmd->unsli3.rcvsli3.ox_id = oxid;
6747
6748         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6749         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6750         pcmd += sizeof(uint32_t); /* Skip past command */
6751         rls_rsp = (struct RLS_RSP *)pcmd;
6752
6753         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6754         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6755         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6756         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6757         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6758         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
6759         mempool_free(pmb, phba->mbox_mem_pool);
6760         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6761         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6762                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
6763                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6764                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6765                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6766                          ndlp->nlp_rpi);
6767         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6768         phba->fc_stat.elsXmitACC++;
6769         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6770                 lpfc_els_free_iocb(phba, elsiocb);
6771 }
6772
6773 /**
6774  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6775  * @phba: pointer to lpfc hba data structure.
6776  * @pmb: pointer to the driver internal queue element for mailbox command.
6777  *
6778  * This routine is the completion callback function for the MBX_READ_LNK_STAT
6779  * mailbox command. This callback function is to actually send the Accept
6780  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6781  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6782  * mailbox command, constructs the RPS response with the link statistics
6783  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6784  * response to the RPS.
6785  *
6786  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6787  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6788  * will be stored into the context1 field of the IOCB for the completion
6789  * callback function to the RPS Accept Response ELS IOCB command.
6790  *
6791  **/
6792 static void
6793 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6794 {
6795         MAILBOX_t *mb;
6796         IOCB_t *icmd;
6797         RPS_RSP *rps_rsp;
6798         uint8_t *pcmd;
6799         struct lpfc_iocbq *elsiocb;
6800         struct lpfc_nodelist *ndlp;
6801         uint16_t status;
6802         uint16_t oxid;
6803         uint16_t rxid;
6804         uint32_t cmdsize;
6805
6806         mb = &pmb->u.mb;
6807
6808         ndlp = (struct lpfc_nodelist *) pmb->context2;
6809         rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
6810         oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
6811         pmb->context1 = NULL;
6812         pmb->context2 = NULL;
6813
6814         if (mb->mbxStatus) {
6815                 mempool_free(pmb, phba->mbox_mem_pool);
6816                 return;
6817         }
6818
6819         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
6820         mempool_free(pmb, phba->mbox_mem_pool);
6821         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6822                                      lpfc_max_els_tries, ndlp,
6823                                      ndlp->nlp_DID, ELS_CMD_ACC);
6824
6825         /* Decrement the ndlp reference count from previous mbox command */
6826         lpfc_nlp_put(ndlp);
6827
6828         if (!elsiocb)
6829                 return;
6830
6831         icmd = &elsiocb->iocb;
6832         icmd->ulpContext = rxid;
6833         icmd->unsli3.rcvsli3.ox_id = oxid;
6834
6835         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6836         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6837         pcmd += sizeof(uint32_t); /* Skip past command */
6838         rps_rsp = (RPS_RSP *)pcmd;
6839
6840         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
6841                 status = 0x10;
6842         else
6843                 status = 0x8;
6844         if (phba->pport->fc_flag & FC_FABRIC)
6845                 status |= 0x4;
6846
6847         rps_rsp->rsvd1 = 0;
6848         rps_rsp->portStatus = cpu_to_be16(status);
6849         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6850         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6851         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6852         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6853         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6854         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
6855         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
6856         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6857                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
6858                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6859                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6860                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6861                          ndlp->nlp_rpi);
6862         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6863         phba->fc_stat.elsXmitACC++;
6864         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6865                 lpfc_els_free_iocb(phba, elsiocb);
6866         return;
6867 }
6868
6869 /**
6870  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
6871  * @vport: pointer to a host virtual N_Port data structure.
6872  * @cmdiocb: pointer to lpfc command iocb data structure.
6873  * @ndlp: pointer to a node-list data structure.
6874  *
6875  * This routine processes Read Port Status (RPL) IOCB received as an
6876  * ELS unsolicited event. It first checks the remote port state. If the
6877  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6878  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6879  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
6880  * for reading the HBA link statistics. It is for the callback function,
6881  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
6882  * to actually sending out RPL Accept (ACC) response.
6883  *
6884  * Return codes
6885  *   0 - Successfully processed rls iocb (currently always return 0)
6886  **/
6887 static int
6888 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6889                  struct lpfc_nodelist *ndlp)
6890 {
6891         struct lpfc_hba *phba = vport->phba;
6892         LPFC_MBOXQ_t *mbox;
6893         struct ls_rjt stat;
6894
6895         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6896             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6897                 /* reject the unsolicited RPS request and done with it */
6898                 goto reject_out;
6899
6900         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
6901         if (mbox) {
6902                 lpfc_read_lnk_stat(phba, mbox);
6903                 mbox->context1 = (void *)((unsigned long)
6904                         ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
6905                         cmdiocb->iocb.ulpContext)); /* rx_id */
6906                 mbox->context2 = lpfc_nlp_get(ndlp);
6907                 mbox->vport = vport;
6908                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
6909                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6910                         != MBX_NOT_FINISHED)
6911                         /* Mbox completion will send ELS Response */
6912                         return 0;
6913                 /* Decrement reference count used for the failed mbox
6914                  * command.
6915                  */
6916                 lpfc_nlp_put(ndlp);
6917                 mempool_free(mbox, phba->mbox_mem_pool);
6918         }
6919 reject_out:
6920         /* issue rejection response */
6921         stat.un.b.lsRjtRsvd0 = 0;
6922         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6923         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6924         stat.un.b.vendorUnique = 0;
6925         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6926         return 0;
6927 }
6928
6929 /**
6930  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
6931  * @vport: pointer to a host virtual N_Port data structure.
6932  * @cmdiocb: pointer to lpfc command iocb data structure.
6933  * @ndlp: pointer to a node-list data structure.
6934  *
6935  * This routine processes Read Timout Value (RTV) IOCB received as an
6936  * ELS unsolicited event. It first checks the remote port state. If the
6937  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6938  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6939  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
6940  * Value (RTV) unsolicited IOCB event.
6941  *
6942  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6943  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6944  * will be stored into the context1 field of the IOCB for the completion
6945  * callback function to the RPS Accept Response ELS IOCB command.
6946  *
6947  * Return codes
6948  *   0 - Successfully processed rtv iocb (currently always return 0)
6949  **/
6950 static int
6951 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6952                  struct lpfc_nodelist *ndlp)
6953 {
6954         struct lpfc_hba *phba = vport->phba;
6955         struct ls_rjt stat;
6956         struct RTV_RSP *rtv_rsp;
6957         uint8_t *pcmd;
6958         struct lpfc_iocbq *elsiocb;
6959         uint32_t cmdsize;
6960
6961
6962         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6963             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6964                 /* reject the unsolicited RPS request and done with it */
6965                 goto reject_out;
6966
6967         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
6968         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6969                                      lpfc_max_els_tries, ndlp,
6970                                      ndlp->nlp_DID, ELS_CMD_ACC);
6971
6972         if (!elsiocb)
6973                 return 1;
6974
6975         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6976         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6977         pcmd += sizeof(uint32_t); /* Skip past command */
6978
6979         /* use the command's xri in the response */
6980         elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
6981         elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6982
6983         rtv_rsp = (struct RTV_RSP *)pcmd;
6984
6985         /* populate RTV payload */
6986         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
6987         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
6988         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
6989         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
6990         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
6991
6992         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6993         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6994                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
6995                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
6996                          "Data: x%x x%x x%x\n",
6997                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6998                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6999                          ndlp->nlp_rpi,
7000                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
7001         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7002         phba->fc_stat.elsXmitACC++;
7003         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7004                 lpfc_els_free_iocb(phba, elsiocb);
7005         return 0;
7006
7007 reject_out:
7008         /* issue rejection response */
7009         stat.un.b.lsRjtRsvd0 = 0;
7010         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7011         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7012         stat.un.b.vendorUnique = 0;
7013         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7014         return 0;
7015 }
7016
7017 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
7018  * @vport: pointer to a host virtual N_Port data structure.
7019  * @cmdiocb: pointer to lpfc command iocb data structure.
7020  * @ndlp: pointer to a node-list data structure.
7021  *
7022  * This routine processes Read Port Status (RPS) IOCB received as an
7023  * ELS unsolicited event. It first checks the remote port state. If the
7024  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7025  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
7026  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7027  * for reading the HBA link statistics. It is for the callback function,
7028  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
7029  * to actually sending out RPS Accept (ACC) response.
7030  *
7031  * Return codes
7032  *   0 - Successfully processed rps iocb (currently always return 0)
7033  **/
7034 static int
7035 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7036                  struct lpfc_nodelist *ndlp)
7037 {
7038         struct lpfc_hba *phba = vport->phba;
7039         uint32_t *lp;
7040         uint8_t flag;
7041         LPFC_MBOXQ_t *mbox;
7042         struct lpfc_dmabuf *pcmd;
7043         RPS *rps;
7044         struct ls_rjt stat;
7045
7046         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7047             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7048                 /* reject the unsolicited RPS request and done with it */
7049                 goto reject_out;
7050
7051         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7052         lp = (uint32_t *) pcmd->virt;
7053         flag = (be32_to_cpu(*lp++) & 0xf);
7054         rps = (RPS *) lp;
7055
7056         if ((flag == 0) ||
7057             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
7058             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
7059                                     sizeof(struct lpfc_name)) == 0))) {
7060
7061                 printk("Fix me....\n");
7062                 dump_stack();
7063                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7064                 if (mbox) {
7065                         lpfc_read_lnk_stat(phba, mbox);
7066                         mbox->context1 = (void *)((unsigned long)
7067                                 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7068                                 cmdiocb->iocb.ulpContext)); /* rx_id */
7069                         mbox->context2 = lpfc_nlp_get(ndlp);
7070                         mbox->vport = vport;
7071                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
7072                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7073                                 != MBX_NOT_FINISHED)
7074                                 /* Mbox completion will send ELS Response */
7075                                 return 0;
7076                         /* Decrement reference count used for the failed mbox
7077                          * command.
7078                          */
7079                         lpfc_nlp_put(ndlp);
7080                         mempool_free(mbox, phba->mbox_mem_pool);
7081                 }
7082         }
7083
7084 reject_out:
7085         /* issue rejection response */
7086         stat.un.b.lsRjtRsvd0 = 0;
7087         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7088         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7089         stat.un.b.vendorUnique = 0;
7090         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7091         return 0;
7092 }
7093
7094 /* lpfc_issue_els_rrq - Process an unsolicited rps iocb
7095  * @vport: pointer to a host virtual N_Port data structure.
7096  * @ndlp: pointer to a node-list data structure.
7097  * @did: DID of the target.
7098  * @rrq: Pointer to the rrq struct.
7099  *
7100  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
7101  * Successful the the completion handler will clear the RRQ.
7102  *
7103  * Return codes
7104  *   0 - Successfully sent rrq els iocb.
7105  *   1 - Failed to send rrq els iocb.
7106  **/
7107 static int
7108 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7109                         uint32_t did, struct lpfc_node_rrq *rrq)
7110 {
7111         struct lpfc_hba  *phba = vport->phba;
7112         struct RRQ *els_rrq;
7113         struct lpfc_iocbq *elsiocb;
7114         uint8_t *pcmd;
7115         uint16_t cmdsize;
7116         int ret;
7117
7118
7119         if (ndlp != rrq->ndlp)
7120                 ndlp = rrq->ndlp;
7121         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7122                 return 1;
7123
7124         /* If ndlp is not NULL, we will bump the reference count on it */
7125         cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
7126         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
7127                                      ELS_CMD_RRQ);
7128         if (!elsiocb)
7129                 return 1;
7130
7131         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7132
7133         /* For RRQ request, remainder of payload is Exchange IDs */
7134         *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
7135         pcmd += sizeof(uint32_t);
7136         els_rrq = (struct RRQ *) pcmd;
7137
7138         bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
7139         bf_set(rrq_rxid, els_rrq, rrq->rxid);
7140         bf_set(rrq_did, els_rrq, vport->fc_myDID);
7141         els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
7142         els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
7143
7144
7145         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7146                 "Issue RRQ:     did:x%x",
7147                 did, rrq->xritag, rrq->rxid);
7148         elsiocb->context_un.rrq = rrq;
7149         elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
7150         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7151
7152         if (ret == IOCB_ERROR) {
7153                 lpfc_els_free_iocb(phba, elsiocb);
7154                 return 1;
7155         }
7156         return 0;
7157 }
7158
7159 /**
7160  * lpfc_send_rrq - Sends ELS RRQ if needed.
7161  * @phba: pointer to lpfc hba data structure.
7162  * @rrq: pointer to the active rrq.
7163  *
7164  * This routine will call the lpfc_issue_els_rrq if the rrq is
7165  * still active for the xri. If this function returns a failure then
7166  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
7167  *
7168  * Returns 0 Success.
7169  *         1 Failure.
7170  **/
7171 int
7172 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
7173 {
7174         struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
7175                                                         rrq->nlp_DID);
7176         if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
7177                 return lpfc_issue_els_rrq(rrq->vport, ndlp,
7178                                          rrq->nlp_DID, rrq);
7179         else
7180                 return 1;
7181 }
7182
7183 /**
7184  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
7185  * @vport: pointer to a host virtual N_Port data structure.
7186  * @cmdsize: size of the ELS command.
7187  * @oldiocb: pointer to the original lpfc command iocb data structure.
7188  * @ndlp: pointer to a node-list data structure.
7189  *
7190  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
7191  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
7192  *
7193  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7194  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7195  * will be stored into the context1 field of the IOCB for the completion
7196  * callback function to the RPL Accept Response ELS command.
7197  *
7198  * Return code
7199  *   0 - Successfully issued ACC RPL ELS command
7200  *   1 - Failed to issue ACC RPL ELS command
7201  **/
7202 static int
7203 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
7204                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7205 {
7206         struct lpfc_hba *phba = vport->phba;
7207         IOCB_t *icmd, *oldcmd;
7208         RPL_RSP rpl_rsp;
7209         struct lpfc_iocbq *elsiocb;
7210         uint8_t *pcmd;
7211
7212         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
7213                                      ndlp->nlp_DID, ELS_CMD_ACC);
7214
7215         if (!elsiocb)
7216                 return 1;
7217
7218         icmd = &elsiocb->iocb;
7219         oldcmd = &oldiocb->iocb;
7220         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
7221         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7222
7223         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7224         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7225         pcmd += sizeof(uint16_t);
7226         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
7227         pcmd += sizeof(uint16_t);
7228
7229         /* Setup the RPL ACC payload */
7230         rpl_rsp.listLen = be32_to_cpu(1);
7231         rpl_rsp.index = 0;
7232         rpl_rsp.port_num_blk.portNum = 0;
7233         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
7234         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7235             sizeof(struct lpfc_name));
7236         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7237         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
7238         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7239                          "0120 Xmit ELS RPL ACC response tag x%x "
7240                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
7241                          "rpi x%x\n",
7242                          elsiocb->iotag, elsiocb->iocb.ulpContext,
7243                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7244                          ndlp->nlp_rpi);
7245         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7246         phba->fc_stat.elsXmitACC++;
7247         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7248             IOCB_ERROR) {
7249                 lpfc_els_free_iocb(phba, elsiocb);
7250                 return 1;
7251         }
7252         return 0;
7253 }
7254
7255 /**
7256  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
7257  * @vport: pointer to a host virtual N_Port data structure.
7258  * @cmdiocb: pointer to lpfc command iocb data structure.
7259  * @ndlp: pointer to a node-list data structure.
7260  *
7261  * This routine processes Read Port List (RPL) IOCB received as an ELS
7262  * unsolicited event. It first checks the remote port state. If the remote
7263  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
7264  * invokes the lpfc_els_rsp_reject() routine to send reject response.
7265  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
7266  * to accept the RPL.
7267  *
7268  * Return code
7269  *   0 - Successfully processed rpl iocb (currently always return 0)
7270  **/
7271 static int
7272 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7273                  struct lpfc_nodelist *ndlp)
7274 {
7275         struct lpfc_dmabuf *pcmd;
7276         uint32_t *lp;
7277         uint32_t maxsize;
7278         uint16_t cmdsize;
7279         RPL *rpl;
7280         struct ls_rjt stat;
7281
7282         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7283             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
7284                 /* issue rejection response */
7285                 stat.un.b.lsRjtRsvd0 = 0;
7286                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7287                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7288                 stat.un.b.vendorUnique = 0;
7289                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
7290                         NULL);
7291                 /* rejected the unsolicited RPL request and done with it */
7292                 return 0;
7293         }
7294
7295         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7296         lp = (uint32_t *) pcmd->virt;
7297         rpl = (RPL *) (lp + 1);
7298         maxsize = be32_to_cpu(rpl->maxsize);
7299
7300         /* We support only one port */
7301         if ((rpl->index == 0) &&
7302             ((maxsize == 0) ||
7303              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
7304                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
7305         } else {
7306                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
7307         }
7308         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
7309
7310         return 0;
7311 }
7312
7313 /**
7314  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
7315  * @vport: pointer to a virtual N_Port data structure.
7316  * @cmdiocb: pointer to lpfc command iocb data structure.
7317  * @ndlp: pointer to a node-list data structure.
7318  *
7319  * This routine processes Fibre Channel Address Resolution Protocol
7320  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
7321  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
7322  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
7323  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
7324  * remote PortName is compared against the FC PortName stored in the @vport
7325  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
7326  * compared against the FC NodeName stored in the @vport data structure.
7327  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
7328  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
7329  * invoked to send out FARP Response to the remote node. Before sending the
7330  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
7331  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
7332  * routine is invoked to log into the remote port first.
7333  *
7334  * Return code
7335  *   0 - Either the FARP Match Mode not supported or successfully processed
7336  **/
7337 static int
7338 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7339                   struct lpfc_nodelist *ndlp)
7340 {
7341         struct lpfc_dmabuf *pcmd;
7342         uint32_t *lp;
7343         IOCB_t *icmd;
7344         FARP *fp;
7345         uint32_t cmd, cnt, did;
7346
7347         icmd = &cmdiocb->iocb;
7348         did = icmd->un.elsreq64.remoteID;
7349         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7350         lp = (uint32_t *) pcmd->virt;
7351
7352         cmd = *lp++;
7353         fp = (FARP *) lp;
7354         /* FARP-REQ received from DID <did> */
7355         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7356                          "0601 FARP-REQ received from DID x%x\n", did);
7357         /* We will only support match on WWPN or WWNN */
7358         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
7359                 return 0;
7360         }
7361
7362         cnt = 0;
7363         /* If this FARP command is searching for my portname */
7364         if (fp->Mflags & FARP_MATCH_PORT) {
7365                 if (memcmp(&fp->RportName, &vport->fc_portname,
7366                            sizeof(struct lpfc_name)) == 0)
7367                         cnt = 1;
7368         }
7369
7370         /* If this FARP command is searching for my nodename */
7371         if (fp->Mflags & FARP_MATCH_NODE) {
7372                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
7373                            sizeof(struct lpfc_name)) == 0)
7374                         cnt = 1;
7375         }
7376
7377         if (cnt) {
7378                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
7379                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
7380                         /* Log back into the node before sending the FARP. */
7381                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
7382                                 ndlp->nlp_prev_state = ndlp->nlp_state;
7383                                 lpfc_nlp_set_state(vport, ndlp,
7384                                                    NLP_STE_PLOGI_ISSUE);
7385                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
7386                         }
7387
7388                         /* Send a FARP response to that node */
7389                         if (fp->Rflags & FARP_REQUEST_FARPR)
7390                                 lpfc_issue_els_farpr(vport, did, 0);
7391                 }
7392         }
7393         return 0;
7394 }
7395
7396 /**
7397  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
7398  * @vport: pointer to a host virtual N_Port data structure.
7399  * @cmdiocb: pointer to lpfc command iocb data structure.
7400  * @ndlp: pointer to a node-list data structure.
7401  *
7402  * This routine processes Fibre Channel Address Resolution Protocol
7403  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
7404  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
7405  * the FARP response request.
7406  *
7407  * Return code
7408  *   0 - Successfully processed FARPR IOCB (currently always return 0)
7409  **/
7410 static int
7411 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7412                    struct lpfc_nodelist  *ndlp)
7413 {
7414         struct lpfc_dmabuf *pcmd;
7415         uint32_t *lp;
7416         IOCB_t *icmd;
7417         uint32_t cmd, did;
7418
7419         icmd = &cmdiocb->iocb;
7420         did = icmd->un.elsreq64.remoteID;
7421         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7422         lp = (uint32_t *) pcmd->virt;
7423
7424         cmd = *lp++;
7425         /* FARP-RSP received from DID <did> */
7426         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7427                          "0600 FARP-RSP received from DID x%x\n", did);
7428         /* ACCEPT the Farp resp request */
7429         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7430
7431         return 0;
7432 }
7433
7434 /**
7435  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
7436  * @vport: pointer to a host virtual N_Port data structure.
7437  * @cmdiocb: pointer to lpfc command iocb data structure.
7438  * @fan_ndlp: pointer to a node-list data structure.
7439  *
7440  * This routine processes a Fabric Address Notification (FAN) IOCB
7441  * command received as an ELS unsolicited event. The FAN ELS command will
7442  * only be processed on a physical port (i.e., the @vport represents the
7443  * physical port). The fabric NodeName and PortName from the FAN IOCB are
7444  * compared against those in the phba data structure. If any of those is
7445  * different, the lpfc_initial_flogi() routine is invoked to initialize
7446  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
7447  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
7448  * is invoked to register login to the fabric.
7449  *
7450  * Return code
7451  *   0 - Successfully processed fan iocb (currently always return 0).
7452  **/
7453 static int
7454 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7455                  struct lpfc_nodelist *fan_ndlp)
7456 {
7457         struct lpfc_hba *phba = vport->phba;
7458         uint32_t *lp;
7459         FAN *fp;
7460
7461         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
7462         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
7463         fp = (FAN *) ++lp;
7464         /* FAN received; Fan does not have a reply sequence */
7465         if ((vport == phba->pport) &&
7466             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
7467                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
7468                             sizeof(struct lpfc_name))) ||
7469                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
7470                             sizeof(struct lpfc_name)))) {
7471                         /* This port has switched fabrics. FLOGI is required */
7472                         lpfc_issue_init_vfi(vport);
7473                 } else {
7474                         /* FAN verified - skip FLOGI */
7475                         vport->fc_myDID = vport->fc_prevDID;
7476                         if (phba->sli_rev < LPFC_SLI_REV4)
7477                                 lpfc_issue_fabric_reglogin(vport);
7478                         else {
7479                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7480                                         "3138 Need register VFI: (x%x/%x)\n",
7481                                         vport->fc_prevDID, vport->fc_myDID);
7482                                 lpfc_issue_reg_vfi(vport);
7483                         }
7484                 }
7485         }
7486         return 0;
7487 }
7488
7489 /**
7490  * lpfc_els_timeout - Handler funciton to the els timer
7491  * @ptr: holder for the timer function associated data.
7492  *
7493  * This routine is invoked by the ELS timer after timeout. It posts the ELS
7494  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
7495  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
7496  * up the worker thread. It is for the worker thread to invoke the routine
7497  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
7498  **/
7499 void
7500 lpfc_els_timeout(struct timer_list *t)
7501 {
7502         struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
7503         struct lpfc_hba   *phba = vport->phba;
7504         uint32_t tmo_posted;
7505         unsigned long iflag;
7506
7507         spin_lock_irqsave(&vport->work_port_lock, iflag);
7508         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
7509         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7510                 vport->work_port_events |= WORKER_ELS_TMO;
7511         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
7512
7513         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7514                 lpfc_worker_wake_up(phba);
7515         return;
7516 }
7517
7518
7519 /**
7520  * lpfc_els_timeout_handler - Process an els timeout event
7521  * @vport: pointer to a virtual N_Port data structure.
7522  *
7523  * This routine is the actual handler function that processes an ELS timeout
7524  * event. It walks the ELS ring to get and abort all the IOCBs (except the
7525  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
7526  * invoking the lpfc_sli_issue_abort_iotag() routine.
7527  **/
7528 void
7529 lpfc_els_timeout_handler(struct lpfc_vport *vport)
7530 {
7531         struct lpfc_hba  *phba = vport->phba;
7532         struct lpfc_sli_ring *pring;
7533         struct lpfc_iocbq *tmp_iocb, *piocb;
7534         IOCB_t *cmd = NULL;
7535         struct lpfc_dmabuf *pcmd;
7536         uint32_t els_command = 0;
7537         uint32_t timeout;
7538         uint32_t remote_ID = 0xffffffff;
7539         LIST_HEAD(abort_list);
7540
7541
7542         timeout = (uint32_t)(phba->fc_ratov << 1);
7543
7544         pring = lpfc_phba_elsring(phba);
7545         if (unlikely(!pring))
7546                 return;
7547
7548         if ((phba->pport->load_flag & FC_UNLOADING))
7549                 return;
7550         spin_lock_irq(&phba->hbalock);
7551         if (phba->sli_rev == LPFC_SLI_REV4)
7552                 spin_lock(&pring->ring_lock);
7553
7554         if ((phba->pport->load_flag & FC_UNLOADING)) {
7555                 if (phba->sli_rev == LPFC_SLI_REV4)
7556                         spin_unlock(&pring->ring_lock);
7557                 spin_unlock_irq(&phba->hbalock);
7558                 return;
7559         }
7560
7561         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7562                 cmd = &piocb->iocb;
7563
7564                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
7565                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7566                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
7567                         continue;
7568
7569                 if (piocb->vport != vport)
7570                         continue;
7571
7572                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
7573                 if (pcmd)
7574                         els_command = *(uint32_t *) (pcmd->virt);
7575
7576                 if (els_command == ELS_CMD_FARP ||
7577                     els_command == ELS_CMD_FARPR ||
7578                     els_command == ELS_CMD_FDISC)
7579                         continue;
7580
7581                 if (piocb->drvrTimeout > 0) {
7582                         if (piocb->drvrTimeout >= timeout)
7583                                 piocb->drvrTimeout -= timeout;
7584                         else
7585                                 piocb->drvrTimeout = 0;
7586                         continue;
7587                 }
7588
7589                 remote_ID = 0xffffffff;
7590                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
7591                         remote_ID = cmd->un.elsreq64.remoteID;
7592                 else {
7593                         struct lpfc_nodelist *ndlp;
7594                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
7595                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
7596                                 remote_ID = ndlp->nlp_DID;
7597                 }
7598                 list_add_tail(&piocb->dlist, &abort_list);
7599         }
7600         if (phba->sli_rev == LPFC_SLI_REV4)
7601                 spin_unlock(&pring->ring_lock);
7602         spin_unlock_irq(&phba->hbalock);
7603
7604         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
7605                 cmd = &piocb->iocb;
7606                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7607                          "0127 ELS timeout Data: x%x x%x x%x "
7608                          "x%x\n", els_command,
7609                          remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
7610                 spin_lock_irq(&phba->hbalock);
7611                 list_del_init(&piocb->dlist);
7612                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
7613                 spin_unlock_irq(&phba->hbalock);
7614         }
7615
7616         if (!list_empty(&pring->txcmplq))
7617                 if (!(phba->pport->load_flag & FC_UNLOADING))
7618                         mod_timer(&vport->els_tmofunc,
7619                                   jiffies + msecs_to_jiffies(1000 * timeout));
7620 }
7621
7622 /**
7623  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
7624  * @vport: pointer to a host virtual N_Port data structure.
7625  *
7626  * This routine is used to clean up all the outstanding ELS commands on a
7627  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
7628  * routine. After that, it walks the ELS transmit queue to remove all the
7629  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
7630  * the IOCBs with a non-NULL completion callback function, the callback
7631  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7632  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
7633  * callback function, the IOCB will simply be released. Finally, it walks
7634  * the ELS transmit completion queue to issue an abort IOCB to any transmit
7635  * completion queue IOCB that is associated with the @vport and is not
7636  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
7637  * part of the discovery state machine) out to HBA by invoking the
7638  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
7639  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
7640  * the IOCBs are aborted when this function returns.
7641  **/
7642 void
7643 lpfc_els_flush_cmd(struct lpfc_vport *vport)
7644 {
7645         LIST_HEAD(abort_list);
7646         struct lpfc_hba  *phba = vport->phba;
7647         struct lpfc_sli_ring *pring;
7648         struct lpfc_iocbq *tmp_iocb, *piocb;
7649         IOCB_t *cmd = NULL;
7650
7651         lpfc_fabric_abort_vport(vport);
7652         /*
7653          * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
7654          * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
7655          * ultimately grabs the ring_lock, the driver must splice the list into
7656          * a working list and release the locks before calling the abort.
7657          */
7658         spin_lock_irq(&phba->hbalock);
7659         pring = lpfc_phba_elsring(phba);
7660
7661         /* Bail out if we've no ELS wq, like in PCI error recovery case. */
7662         if (unlikely(!pring)) {
7663                 spin_unlock_irq(&phba->hbalock);
7664                 return;
7665         }
7666
7667         if (phba->sli_rev == LPFC_SLI_REV4)
7668                 spin_lock(&pring->ring_lock);
7669
7670         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7671                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
7672                         continue;
7673
7674                 if (piocb->vport != vport)
7675                         continue;
7676                 list_add_tail(&piocb->dlist, &abort_list);
7677         }
7678         if (phba->sli_rev == LPFC_SLI_REV4)
7679                 spin_unlock(&pring->ring_lock);
7680         spin_unlock_irq(&phba->hbalock);
7681         /* Abort each iocb on the aborted list and remove the dlist links. */
7682         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
7683                 spin_lock_irq(&phba->hbalock);
7684                 list_del_init(&piocb->dlist);
7685                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
7686                 spin_unlock_irq(&phba->hbalock);
7687         }
7688         if (!list_empty(&abort_list))
7689                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7690                                  "3387 abort list for txq not empty\n");
7691         INIT_LIST_HEAD(&abort_list);
7692
7693         spin_lock_irq(&phba->hbalock);
7694         if (phba->sli_rev == LPFC_SLI_REV4)
7695                 spin_lock(&pring->ring_lock);
7696
7697         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
7698                 cmd = &piocb->iocb;
7699
7700                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
7701                         continue;
7702                 }
7703
7704                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
7705                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
7706                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
7707                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7708                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
7709                         continue;
7710
7711                 if (piocb->vport != vport)
7712                         continue;
7713
7714                 list_del_init(&piocb->list);
7715                 list_add_tail(&piocb->list, &abort_list);
7716         }
7717         if (phba->sli_rev == LPFC_SLI_REV4)
7718                 spin_unlock(&pring->ring_lock);
7719         spin_unlock_irq(&phba->hbalock);
7720
7721         /* Cancell all the IOCBs from the completions list */
7722         lpfc_sli_cancel_iocbs(phba, &abort_list,
7723                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
7724
7725         return;
7726 }
7727
7728 /**
7729  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
7730  * @phba: pointer to lpfc hba data structure.
7731  *
7732  * This routine is used to clean up all the outstanding ELS commands on a
7733  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
7734  * routine. After that, it walks the ELS transmit queue to remove all the
7735  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
7736  * the IOCBs with the completion callback function associated, the callback
7737  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7738  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
7739  * callback function associated, the IOCB will simply be released. Finally,
7740  * it walks the ELS transmit completion queue to issue an abort IOCB to any
7741  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
7742  * management plane IOCBs that are not part of the discovery state machine)
7743  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
7744  **/
7745 void
7746 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
7747 {
7748         struct lpfc_vport *vport;
7749
7750         spin_lock_irq(&phba->port_list_lock);
7751         list_for_each_entry(vport, &phba->port_list, listentry)
7752                 lpfc_els_flush_cmd(vport);
7753         spin_unlock_irq(&phba->port_list_lock);
7754
7755         return;
7756 }
7757
7758 /**
7759  * lpfc_send_els_failure_event - Posts an ELS command failure event
7760  * @phba: Pointer to hba context object.
7761  * @cmdiocbp: Pointer to command iocb which reported error.
7762  * @rspiocbp: Pointer to response iocb which reported error.
7763  *
7764  * This function sends an event when there is an ELS command
7765  * failure.
7766  **/
7767 void
7768 lpfc_send_els_failure_event(struct lpfc_hba *phba,
7769                         struct lpfc_iocbq *cmdiocbp,
7770                         struct lpfc_iocbq *rspiocbp)
7771 {
7772         struct lpfc_vport *vport = cmdiocbp->vport;
7773         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7774         struct lpfc_lsrjt_event lsrjt_event;
7775         struct lpfc_fabric_event_header fabric_event;
7776         struct ls_rjt stat;
7777         struct lpfc_nodelist *ndlp;
7778         uint32_t *pcmd;
7779
7780         ndlp = cmdiocbp->context1;
7781         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7782                 return;
7783
7784         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
7785                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
7786                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
7787                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
7788                         sizeof(struct lpfc_name));
7789                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
7790                         sizeof(struct lpfc_name));
7791                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7792                         cmdiocbp->context2)->virt);
7793                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
7794                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
7795                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
7796                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
7797                 fc_host_post_vendor_event(shost,
7798                         fc_get_event_number(),
7799                         sizeof(lsrjt_event),
7800                         (char *)&lsrjt_event,
7801                         LPFC_NL_VENDOR_ID);
7802                 return;
7803         }
7804         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
7805                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
7806                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
7807                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
7808                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
7809                 else
7810                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
7811                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
7812                         sizeof(struct lpfc_name));
7813                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
7814                         sizeof(struct lpfc_name));
7815                 fc_host_post_vendor_event(shost,
7816                         fc_get_event_number(),
7817                         sizeof(fabric_event),
7818                         (char *)&fabric_event,
7819                         LPFC_NL_VENDOR_ID);
7820                 return;
7821         }
7822
7823 }
7824
7825 /**
7826  * lpfc_send_els_event - Posts unsolicited els event
7827  * @vport: Pointer to vport object.
7828  * @ndlp: Pointer FC node object.
7829  * @cmd: ELS command code.
7830  *
7831  * This function posts an event when there is an incoming
7832  * unsolicited ELS command.
7833  **/
7834 static void
7835 lpfc_send_els_event(struct lpfc_vport *vport,
7836                     struct lpfc_nodelist *ndlp,
7837                     uint32_t *payload)
7838 {
7839         struct lpfc_els_event_header *els_data = NULL;
7840         struct lpfc_logo_event *logo_data = NULL;
7841         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7842
7843         if (*payload == ELS_CMD_LOGO) {
7844                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
7845                 if (!logo_data) {
7846                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7847                                 "0148 Failed to allocate memory "
7848                                 "for LOGO event\n");
7849                         return;
7850                 }
7851                 els_data = &logo_data->header;
7852         } else {
7853                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
7854                         GFP_KERNEL);
7855                 if (!els_data) {
7856                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7857                                 "0149 Failed to allocate memory "
7858                                 "for ELS event\n");
7859                         return;
7860                 }
7861         }
7862         els_data->event_type = FC_REG_ELS_EVENT;
7863         switch (*payload) {
7864         case ELS_CMD_PLOGI:
7865                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
7866                 break;
7867         case ELS_CMD_PRLO:
7868                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
7869                 break;
7870         case ELS_CMD_ADISC:
7871                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
7872                 break;
7873         case ELS_CMD_LOGO:
7874                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
7875                 /* Copy the WWPN in the LOGO payload */
7876                 memcpy(logo_data->logo_wwpn, &payload[2],
7877                         sizeof(struct lpfc_name));
7878                 break;
7879         default:
7880                 kfree(els_data);
7881                 return;
7882         }
7883         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
7884         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
7885         if (*payload == ELS_CMD_LOGO) {
7886                 fc_host_post_vendor_event(shost,
7887                         fc_get_event_number(),
7888                         sizeof(struct lpfc_logo_event),
7889                         (char *)logo_data,
7890                         LPFC_NL_VENDOR_ID);
7891                 kfree(logo_data);
7892         } else {
7893                 fc_host_post_vendor_event(shost,
7894                         fc_get_event_number(),
7895                         sizeof(struct lpfc_els_event_header),
7896                         (char *)els_data,
7897                         LPFC_NL_VENDOR_ID);
7898                 kfree(els_data);
7899         }
7900
7901         return;
7902 }
7903
7904
7905 /**
7906  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
7907  * @phba: pointer to lpfc hba data structure.
7908  * @pring: pointer to a SLI ring.
7909  * @vport: pointer to a host virtual N_Port data structure.
7910  * @elsiocb: pointer to lpfc els command iocb data structure.
7911  *
7912  * This routine is used for processing the IOCB associated with a unsolicited
7913  * event. It first determines whether there is an existing ndlp that matches
7914  * the DID from the unsolicited IOCB. If not, it will create a new one with
7915  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
7916  * IOCB is then used to invoke the proper routine and to set up proper state
7917  * of the discovery state machine.
7918  **/
7919 static void
7920 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7921                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
7922 {
7923         struct Scsi_Host  *shost;
7924         struct lpfc_nodelist *ndlp;
7925         struct ls_rjt stat;
7926         uint32_t *payload;
7927         uint32_t cmd, did, newnode;
7928         uint8_t rjt_exp, rjt_err = 0;
7929         IOCB_t *icmd = &elsiocb->iocb;
7930
7931         if (!vport || !(elsiocb->context2))
7932                 goto dropit;
7933
7934         newnode = 0;
7935         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
7936         cmd = *payload;
7937         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
7938                 lpfc_post_buffer(phba, pring, 1);
7939
7940         did = icmd->un.rcvels.remoteID;
7941         if (icmd->ulpStatus) {
7942                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7943                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
7944                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
7945                 goto dropit;
7946         }
7947
7948         /* Check to see if link went down during discovery */
7949         if (lpfc_els_chk_latt(vport))
7950                 goto dropit;
7951
7952         /* Ignore traffic received during vport shutdown. */
7953         if (vport->load_flag & FC_UNLOADING)
7954                 goto dropit;
7955
7956         /* If NPort discovery is delayed drop incoming ELS */
7957         if ((vport->fc_flag & FC_DISC_DELAYED) &&
7958                         (cmd != ELS_CMD_PLOGI))
7959                 goto dropit;
7960
7961         ndlp = lpfc_findnode_did(vport, did);
7962         if (!ndlp) {
7963                 /* Cannot find existing Fabric ndlp, so allocate a new one */
7964                 ndlp = lpfc_nlp_init(vport, did);
7965                 if (!ndlp)
7966                         goto dropit;
7967                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7968                 newnode = 1;
7969                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7970                         ndlp->nlp_type |= NLP_FABRIC;
7971         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
7972                 ndlp = lpfc_enable_node(vport, ndlp,
7973                                         NLP_STE_UNUSED_NODE);
7974                 if (!ndlp)
7975                         goto dropit;
7976                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7977                 newnode = 1;
7978                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7979                         ndlp->nlp_type |= NLP_FABRIC;
7980         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
7981                 /* This is similar to the new node path */
7982                 ndlp = lpfc_nlp_get(ndlp);
7983                 if (!ndlp)
7984                         goto dropit;
7985                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7986                 newnode = 1;
7987         }
7988
7989         phba->fc_stat.elsRcvFrame++;
7990
7991         /*
7992          * Do not process any unsolicited ELS commands
7993          * if the ndlp is in DEV_LOSS
7994          */
7995         shost = lpfc_shost_from_vport(vport);
7996         spin_lock_irq(shost->host_lock);
7997         if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
7998                 spin_unlock_irq(shost->host_lock);
7999                 goto dropit;
8000         }
8001         spin_unlock_irq(shost->host_lock);
8002
8003         elsiocb->context1 = lpfc_nlp_get(ndlp);
8004         elsiocb->vport = vport;
8005
8006         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
8007                 cmd &= ELS_CMD_MASK;
8008         }
8009         /* ELS command <elsCmd> received from NPORT <did> */
8010         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8011                          "0112 ELS command x%x received from NPORT x%x "
8012                          "Data: x%x x%x x%x x%x\n",
8013                         cmd, did, vport->port_state, vport->fc_flag,
8014                         vport->fc_myDID, vport->fc_prevDID);
8015
8016         /* reject till our FLOGI completes */
8017         if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
8018             (cmd != ELS_CMD_FLOGI)) {
8019                 rjt_err = LSRJT_LOGICAL_BSY;
8020                 rjt_exp = LSEXP_NOTHING_MORE;
8021                 goto lsrjt;
8022         }
8023
8024         switch (cmd) {
8025         case ELS_CMD_PLOGI:
8026                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8027                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
8028                         did, vport->port_state, ndlp->nlp_flag);
8029
8030                 phba->fc_stat.elsRcvPLOGI++;
8031                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
8032                 if (phba->sli_rev == LPFC_SLI_REV4 &&
8033                     (phba->pport->fc_flag & FC_PT2PT)) {
8034                         vport->fc_prevDID = vport->fc_myDID;
8035                         /* Our DID needs to be updated before registering
8036                          * the vfi. This is done in lpfc_rcv_plogi but
8037                          * that is called after the reg_vfi.
8038                          */
8039                         vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
8040                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8041                                          "3312 Remote port assigned DID x%x "
8042                                          "%x\n", vport->fc_myDID,
8043                                          vport->fc_prevDID);
8044                 }
8045
8046                 lpfc_send_els_event(vport, ndlp, payload);
8047
8048                 /* If Nport discovery is delayed, reject PLOGIs */
8049                 if (vport->fc_flag & FC_DISC_DELAYED) {
8050                         rjt_err = LSRJT_UNABLE_TPC;
8051                         rjt_exp = LSEXP_NOTHING_MORE;
8052                         break;
8053                 }
8054
8055                 if (vport->port_state < LPFC_DISC_AUTH) {
8056                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
8057                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
8058                                 rjt_err = LSRJT_UNABLE_TPC;
8059                                 rjt_exp = LSEXP_NOTHING_MORE;
8060                                 break;
8061                         }
8062                 }
8063
8064                 spin_lock_irq(shost->host_lock);
8065                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
8066                 spin_unlock_irq(shost->host_lock);
8067
8068                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8069                                         NLP_EVT_RCV_PLOGI);
8070
8071                 break;
8072         case ELS_CMD_FLOGI:
8073                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8074                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
8075                         did, vport->port_state, ndlp->nlp_flag);
8076
8077                 phba->fc_stat.elsRcvFLOGI++;
8078                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
8079                 if (newnode)
8080                         lpfc_nlp_put(ndlp);
8081                 break;
8082         case ELS_CMD_LOGO:
8083                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8084                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
8085                         did, vport->port_state, ndlp->nlp_flag);
8086
8087                 phba->fc_stat.elsRcvLOGO++;
8088                 lpfc_send_els_event(vport, ndlp, payload);
8089                 if (vport->port_state < LPFC_DISC_AUTH) {
8090                         rjt_err = LSRJT_UNABLE_TPC;
8091                         rjt_exp = LSEXP_NOTHING_MORE;
8092                         break;
8093                 }
8094                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
8095                 break;
8096         case ELS_CMD_PRLO:
8097                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8098                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
8099                         did, vport->port_state, ndlp->nlp_flag);
8100
8101                 phba->fc_stat.elsRcvPRLO++;
8102                 lpfc_send_els_event(vport, ndlp, payload);
8103                 if (vport->port_state < LPFC_DISC_AUTH) {
8104                         rjt_err = LSRJT_UNABLE_TPC;
8105                         rjt_exp = LSEXP_NOTHING_MORE;
8106                         break;
8107                 }
8108                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
8109                 break;
8110         case ELS_CMD_LCB:
8111                 phba->fc_stat.elsRcvLCB++;
8112                 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
8113                 break;
8114         case ELS_CMD_RDP:
8115                 phba->fc_stat.elsRcvRDP++;
8116                 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
8117                 break;
8118         case ELS_CMD_RSCN:
8119                 phba->fc_stat.elsRcvRSCN++;
8120                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
8121                 if (newnode)
8122                         lpfc_nlp_put(ndlp);
8123                 break;
8124         case ELS_CMD_ADISC:
8125                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8126                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
8127                         did, vport->port_state, ndlp->nlp_flag);
8128
8129                 lpfc_send_els_event(vport, ndlp, payload);
8130                 phba->fc_stat.elsRcvADISC++;
8131                 if (vport->port_state < LPFC_DISC_AUTH) {
8132                         rjt_err = LSRJT_UNABLE_TPC;
8133                         rjt_exp = LSEXP_NOTHING_MORE;
8134                         break;
8135                 }
8136                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8137                                         NLP_EVT_RCV_ADISC);
8138                 break;
8139         case ELS_CMD_PDISC:
8140                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8141                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
8142                         did, vport->port_state, ndlp->nlp_flag);
8143
8144                 phba->fc_stat.elsRcvPDISC++;
8145                 if (vport->port_state < LPFC_DISC_AUTH) {
8146                         rjt_err = LSRJT_UNABLE_TPC;
8147                         rjt_exp = LSEXP_NOTHING_MORE;
8148                         break;
8149                 }
8150                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8151                                         NLP_EVT_RCV_PDISC);
8152                 break;
8153         case ELS_CMD_FARPR:
8154                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8155                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
8156                         did, vport->port_state, ndlp->nlp_flag);
8157
8158                 phba->fc_stat.elsRcvFARPR++;
8159                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
8160                 break;
8161         case ELS_CMD_FARP:
8162                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8163                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
8164                         did, vport->port_state, ndlp->nlp_flag);
8165
8166                 phba->fc_stat.elsRcvFARP++;
8167                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
8168                 break;
8169         case ELS_CMD_FAN:
8170                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8171                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
8172                         did, vport->port_state, ndlp->nlp_flag);
8173
8174                 phba->fc_stat.elsRcvFAN++;
8175                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
8176                 break;
8177         case ELS_CMD_PRLI:
8178         case ELS_CMD_NVMEPRLI:
8179                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8180                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
8181                         did, vport->port_state, ndlp->nlp_flag);
8182
8183                 phba->fc_stat.elsRcvPRLI++;
8184                 if ((vport->port_state < LPFC_DISC_AUTH) &&
8185                     (vport->fc_flag & FC_FABRIC)) {
8186                         rjt_err = LSRJT_UNABLE_TPC;
8187                         rjt_exp = LSEXP_NOTHING_MORE;
8188                         break;
8189                 }
8190                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
8191                 break;
8192         case ELS_CMD_LIRR:
8193                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8194                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
8195                         did, vport->port_state, ndlp->nlp_flag);
8196
8197                 phba->fc_stat.elsRcvLIRR++;
8198                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
8199                 if (newnode)
8200                         lpfc_nlp_put(ndlp);
8201                 break;
8202         case ELS_CMD_RLS:
8203                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8204                         "RCV RLS:         did:x%x/ste:x%x flg:x%x",
8205                         did, vport->port_state, ndlp->nlp_flag);
8206
8207                 phba->fc_stat.elsRcvRLS++;
8208                 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
8209                 if (newnode)
8210                         lpfc_nlp_put(ndlp);
8211                 break;
8212         case ELS_CMD_RPS:
8213                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8214                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
8215                         did, vport->port_state, ndlp->nlp_flag);
8216
8217                 phba->fc_stat.elsRcvRPS++;
8218                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
8219                 if (newnode)
8220                         lpfc_nlp_put(ndlp);
8221                 break;
8222         case ELS_CMD_RPL:
8223                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8224                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
8225                         did, vport->port_state, ndlp->nlp_flag);
8226
8227                 phba->fc_stat.elsRcvRPL++;
8228                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
8229                 if (newnode)
8230                         lpfc_nlp_put(ndlp);
8231                 break;
8232         case ELS_CMD_RNID:
8233                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8234                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
8235                         did, vport->port_state, ndlp->nlp_flag);
8236
8237                 phba->fc_stat.elsRcvRNID++;
8238                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
8239                 if (newnode)
8240                         lpfc_nlp_put(ndlp);
8241                 break;
8242         case ELS_CMD_RTV:
8243                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8244                         "RCV RTV:        did:x%x/ste:x%x flg:x%x",
8245                         did, vport->port_state, ndlp->nlp_flag);
8246                 phba->fc_stat.elsRcvRTV++;
8247                 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
8248                 if (newnode)
8249                         lpfc_nlp_put(ndlp);
8250                 break;
8251         case ELS_CMD_RRQ:
8252                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8253                         "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
8254                         did, vport->port_state, ndlp->nlp_flag);
8255
8256                 phba->fc_stat.elsRcvRRQ++;
8257                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
8258                 if (newnode)
8259                         lpfc_nlp_put(ndlp);
8260                 break;
8261         case ELS_CMD_ECHO:
8262                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8263                         "RCV ECHO:        did:x%x/ste:x%x flg:x%x",
8264                         did, vport->port_state, ndlp->nlp_flag);
8265
8266                 phba->fc_stat.elsRcvECHO++;
8267                 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
8268                 if (newnode)
8269                         lpfc_nlp_put(ndlp);
8270                 break;
8271         case ELS_CMD_REC:
8272                 /* receive this due to exchange closed */
8273                 rjt_err = LSRJT_UNABLE_TPC;
8274                 rjt_exp = LSEXP_INVALID_OX_RX;
8275                 break;
8276         default:
8277                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8278                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
8279                         cmd, did, vport->port_state);
8280
8281                 /* Unsupported ELS command, reject */
8282                 rjt_err = LSRJT_CMD_UNSUPPORTED;
8283                 rjt_exp = LSEXP_NOTHING_MORE;
8284
8285                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
8286                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8287                                  "0115 Unknown ELS command x%x "
8288                                  "received from NPORT x%x\n", cmd, did);
8289                 if (newnode)
8290                         lpfc_nlp_put(ndlp);
8291                 break;
8292         }
8293
8294 lsrjt:
8295         /* check if need to LS_RJT received ELS cmd */
8296         if (rjt_err) {
8297                 memset(&stat, 0, sizeof(stat));
8298                 stat.un.b.lsRjtRsnCode = rjt_err;
8299                 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
8300                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
8301                         NULL);
8302         }
8303
8304         lpfc_nlp_put(elsiocb->context1);
8305         elsiocb->context1 = NULL;
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