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