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