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