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