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