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