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