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