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