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