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