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