These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / 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
5177         if (phba->hba_flag & HBA_FCOE_MODE) {
5178                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5179                 goto rjt;
5180         }
5181         if (beacon->lcb_frequency == 0) {
5182                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5183                 goto rjt;
5184         }
5185         if ((beacon->lcb_type != LPFC_LCB_GREEN) &&
5186             (beacon->lcb_type != LPFC_LCB_AMBER)) {
5187                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5188                 goto rjt;
5189         }
5190         if ((beacon->lcb_sub_command != LPFC_LCB_ON) &&
5191             (beacon->lcb_sub_command != LPFC_LCB_OFF)) {
5192                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5193                 goto rjt;
5194         }
5195         if ((beacon->lcb_sub_command == LPFC_LCB_ON) &&
5196             (beacon->lcb_type != LPFC_LCB_GREEN) &&
5197             (beacon->lcb_type != LPFC_LCB_AMBER)) {
5198                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5199                 goto rjt;
5200         }
5201         if (be16_to_cpu(beacon->lcb_duration) != 0) {
5202                 rjt_err = LSRJT_CMD_UNSUPPORTED;
5203                 goto rjt;
5204         }
5205
5206         lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
5207         if (!lcb_context) {
5208                 rjt_err = LSRJT_UNABLE_TPC;
5209                 goto rjt;
5210         }
5211
5212         state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
5213         lcb_context->sub_command = beacon->lcb_sub_command;
5214         lcb_context->type = beacon->lcb_type;
5215         lcb_context->frequency = beacon->lcb_frequency;
5216         lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
5217         lcb_context->rx_id = cmdiocb->iocb.ulpContext;
5218         lcb_context->ndlp = lpfc_nlp_get(ndlp);
5219         if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
5220                 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
5221                                  LOG_ELS, "0193 failed to send mail box");
5222                 kfree(lcb_context);
5223                 lpfc_nlp_put(ndlp);
5224                 rjt_err = LSRJT_UNABLE_TPC;
5225                 goto rjt;
5226         }
5227         return 0;
5228 rjt:
5229         memset(&stat, 0, sizeof(stat));
5230         stat.un.b.lsRjtRsnCode = rjt_err;
5231         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5232         return 1;
5233 }
5234
5235
5236 /**
5237  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
5238  * @vport: pointer to a host virtual N_Port data structure.
5239  *
5240  * This routine cleans up any Registration State Change Notification
5241  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
5242  * @vport together with the host_lock is used to prevent multiple thread
5243  * trying to access the RSCN array on a same @vport at the same time.
5244  **/
5245 void
5246 lpfc_els_flush_rscn(struct lpfc_vport *vport)
5247 {
5248         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5249         struct lpfc_hba  *phba = vport->phba;
5250         int i;
5251
5252         spin_lock_irq(shost->host_lock);
5253         if (vport->fc_rscn_flush) {
5254                 /* Another thread is walking fc_rscn_id_list on this vport */
5255                 spin_unlock_irq(shost->host_lock);
5256                 return;
5257         }
5258         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
5259         vport->fc_rscn_flush = 1;
5260         spin_unlock_irq(shost->host_lock);
5261
5262         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
5263                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
5264                 vport->fc_rscn_id_list[i] = NULL;
5265         }
5266         spin_lock_irq(shost->host_lock);
5267         vport->fc_rscn_id_cnt = 0;
5268         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
5269         spin_unlock_irq(shost->host_lock);
5270         lpfc_can_disctmo(vport);
5271         /* Indicate we are done walking this fc_rscn_id_list */
5272         vport->fc_rscn_flush = 0;
5273 }
5274
5275 /**
5276  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
5277  * @vport: pointer to a host virtual N_Port data structure.
5278  * @did: remote destination port identifier.
5279  *
5280  * This routine checks whether there is any pending Registration State
5281  * Configuration Notification (RSCN) to a @did on @vport.
5282  *
5283  * Return code
5284  *   None zero - The @did matched with a pending rscn
5285  *   0 - not able to match @did with a pending rscn
5286  **/
5287 int
5288 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
5289 {
5290         D_ID ns_did;
5291         D_ID rscn_did;
5292         uint32_t *lp;
5293         uint32_t payload_len, i;
5294         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5295
5296         ns_did.un.word = did;
5297
5298         /* Never match fabric nodes for RSCNs */
5299         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5300                 return 0;
5301
5302         /* If we are doing a FULL RSCN rediscovery, match everything */
5303         if (vport->fc_flag & FC_RSCN_DISCOVERY)
5304                 return did;
5305
5306         spin_lock_irq(shost->host_lock);
5307         if (vport->fc_rscn_flush) {
5308                 /* Another thread is walking fc_rscn_id_list on this vport */
5309                 spin_unlock_irq(shost->host_lock);
5310                 return 0;
5311         }
5312         /* Indicate we are walking fc_rscn_id_list on this vport */
5313         vport->fc_rscn_flush = 1;
5314         spin_unlock_irq(shost->host_lock);
5315         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
5316                 lp = vport->fc_rscn_id_list[i]->virt;
5317                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
5318                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
5319                 while (payload_len) {
5320                         rscn_did.un.word = be32_to_cpu(*lp++);
5321                         payload_len -= sizeof(uint32_t);
5322                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
5323                         case RSCN_ADDRESS_FORMAT_PORT:
5324                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
5325                                     && (ns_did.un.b.area == rscn_did.un.b.area)
5326                                     && (ns_did.un.b.id == rscn_did.un.b.id))
5327                                         goto return_did_out;
5328                                 break;
5329                         case RSCN_ADDRESS_FORMAT_AREA:
5330                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
5331                                     && (ns_did.un.b.area == rscn_did.un.b.area))
5332                                         goto return_did_out;
5333                                 break;
5334                         case RSCN_ADDRESS_FORMAT_DOMAIN:
5335                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
5336                                         goto return_did_out;
5337                                 break;
5338                         case RSCN_ADDRESS_FORMAT_FABRIC:
5339                                 goto return_did_out;
5340                         }
5341                 }
5342         }
5343         /* Indicate we are done with walking fc_rscn_id_list on this vport */
5344         vport->fc_rscn_flush = 0;
5345         return 0;
5346 return_did_out:
5347         /* Indicate we are done with walking fc_rscn_id_list on this vport */
5348         vport->fc_rscn_flush = 0;
5349         return did;
5350 }
5351
5352 /**
5353  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
5354  * @vport: pointer to a host virtual N_Port data structure.
5355  *
5356  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
5357  * state machine for a @vport's nodes that are with pending RSCN (Registration
5358  * State Change Notification).
5359  *
5360  * Return code
5361  *   0 - Successful (currently alway return 0)
5362  **/
5363 static int
5364 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
5365 {
5366         struct lpfc_nodelist *ndlp = NULL;
5367
5368         /* Move all affected nodes by pending RSCNs to NPR state. */
5369         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5370                 if (!NLP_CHK_NODE_ACT(ndlp) ||
5371                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
5372                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
5373                         continue;
5374                 lpfc_disc_state_machine(vport, ndlp, NULL,
5375                                         NLP_EVT_DEVICE_RECOVERY);
5376                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
5377         }
5378         return 0;
5379 }
5380
5381 /**
5382  * lpfc_send_rscn_event - Send an RSCN event to management application
5383  * @vport: pointer to a host virtual N_Port data structure.
5384  * @cmdiocb: pointer to lpfc command iocb data structure.
5385  *
5386  * lpfc_send_rscn_event sends an RSCN netlink event to management
5387  * applications.
5388  */
5389 static void
5390 lpfc_send_rscn_event(struct lpfc_vport *vport,
5391                 struct lpfc_iocbq *cmdiocb)
5392 {
5393         struct lpfc_dmabuf *pcmd;
5394         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5395         uint32_t *payload_ptr;
5396         uint32_t payload_len;
5397         struct lpfc_rscn_event_header *rscn_event_data;
5398
5399         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5400         payload_ptr = (uint32_t *) pcmd->virt;
5401         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
5402
5403         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
5404                 payload_len, GFP_KERNEL);
5405         if (!rscn_event_data) {
5406                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5407                         "0147 Failed to allocate memory for RSCN event\n");
5408                 return;
5409         }
5410         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
5411         rscn_event_data->payload_length = payload_len;
5412         memcpy(rscn_event_data->rscn_payload, payload_ptr,
5413                 payload_len);
5414
5415         fc_host_post_vendor_event(shost,
5416                 fc_get_event_number(),
5417                 sizeof(struct lpfc_rscn_event_header) + payload_len,
5418                 (char *)rscn_event_data,
5419                 LPFC_NL_VENDOR_ID);
5420
5421         kfree(rscn_event_data);
5422 }
5423
5424 /**
5425  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
5426  * @vport: pointer to a host virtual N_Port data structure.
5427  * @cmdiocb: pointer to lpfc command iocb data structure.
5428  * @ndlp: pointer to a node-list data structure.
5429  *
5430  * This routine processes an unsolicited RSCN (Registration State Change
5431  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
5432  * to invoke fc_host_post_event() routine to the FC transport layer. If the
5433  * discover state machine is about to begin discovery, it just accepts the
5434  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
5435  * contains N_Port IDs for other vports on this HBA, it just accepts the
5436  * RSCN and ignore processing it. If the state machine is in the recovery
5437  * state, the fc_rscn_id_list of this @vport is walked and the
5438  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
5439  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
5440  * routine is invoked to handle the RSCN event.
5441  *
5442  * Return code
5443  *   0 - Just sent the acc response
5444  *   1 - Sent the acc response and waited for name server completion
5445  **/
5446 static int
5447 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5448                   struct lpfc_nodelist *ndlp)
5449 {
5450         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5451         struct lpfc_hba  *phba = vport->phba;
5452         struct lpfc_dmabuf *pcmd;
5453         uint32_t *lp, *datap;
5454         uint32_t payload_len, length, nportid, *cmd;
5455         int rscn_cnt;
5456         int rscn_id = 0, hba_id = 0;
5457         int i;
5458
5459         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5460         lp = (uint32_t *) pcmd->virt;
5461
5462         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
5463         payload_len -= sizeof(uint32_t);        /* take off word 0 */
5464         /* RSCN received */
5465         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5466                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
5467                          vport->fc_flag, payload_len, *lp,
5468                          vport->fc_rscn_id_cnt);
5469
5470         /* Send an RSCN event to the management application */
5471         lpfc_send_rscn_event(vport, cmdiocb);
5472
5473         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
5474                 fc_host_post_event(shost, fc_get_event_number(),
5475                         FCH_EVT_RSCN, lp[i]);
5476
5477         /* If we are about to begin discovery, just ACC the RSCN.
5478          * Discovery processing will satisfy it.
5479          */
5480         if (vport->port_state <= LPFC_NS_QRY) {
5481                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5482                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
5483                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
5484
5485                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5486                 return 0;
5487         }
5488
5489         /* If this RSCN just contains NPortIDs for other vports on this HBA,
5490          * just ACC and ignore it.
5491          */
5492         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5493                 !(vport->cfg_peer_port_login)) {
5494                 i = payload_len;
5495                 datap = lp;
5496                 while (i > 0) {
5497                         nportid = *datap++;
5498                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
5499                         i -= sizeof(uint32_t);
5500                         rscn_id++;
5501                         if (lpfc_find_vport_by_did(phba, nportid))
5502                                 hba_id++;
5503                 }
5504                 if (rscn_id == hba_id) {
5505                         /* ALL NPortIDs in RSCN are on HBA */
5506                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5507                                          "0219 Ignore RSCN "
5508                                          "Data: x%x x%x x%x x%x\n",
5509                                          vport->fc_flag, payload_len,
5510                                          *lp, vport->fc_rscn_id_cnt);
5511                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5512                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
5513                                 ndlp->nlp_DID, vport->port_state,
5514                                 ndlp->nlp_flag);
5515
5516                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
5517                                 ndlp, NULL);
5518                         return 0;
5519                 }
5520         }
5521
5522         spin_lock_irq(shost->host_lock);
5523         if (vport->fc_rscn_flush) {
5524                 /* Another thread is walking fc_rscn_id_list on this vport */
5525                 vport->fc_flag |= FC_RSCN_DISCOVERY;
5526                 spin_unlock_irq(shost->host_lock);
5527                 /* Send back ACC */
5528                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5529                 return 0;
5530         }
5531         /* Indicate we are walking fc_rscn_id_list on this vport */
5532         vport->fc_rscn_flush = 1;
5533         spin_unlock_irq(shost->host_lock);
5534         /* Get the array count after successfully have the token */
5535         rscn_cnt = vport->fc_rscn_id_cnt;
5536         /* If we are already processing an RSCN, save the received
5537          * RSCN payload buffer, cmdiocb->context2 to process later.
5538          */
5539         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
5540                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5541                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
5542                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
5543
5544                 spin_lock_irq(shost->host_lock);
5545                 vport->fc_flag |= FC_RSCN_DEFERRED;
5546                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
5547                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
5548                         vport->fc_flag |= FC_RSCN_MODE;
5549                         spin_unlock_irq(shost->host_lock);
5550                         if (rscn_cnt) {
5551                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
5552                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
5553                         }
5554                         if ((rscn_cnt) &&
5555                             (payload_len + length <= LPFC_BPL_SIZE)) {
5556                                 *cmd &= ELS_CMD_MASK;
5557                                 *cmd |= cpu_to_be32(payload_len + length);
5558                                 memcpy(((uint8_t *)cmd) + length, lp,
5559                                        payload_len);
5560                         } else {
5561                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
5562                                 vport->fc_rscn_id_cnt++;
5563                                 /* If we zero, cmdiocb->context2, the calling
5564                                  * routine will not try to free it.
5565                                  */
5566                                 cmdiocb->context2 = NULL;
5567                         }
5568                         /* Deferred RSCN */
5569                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5570                                          "0235 Deferred RSCN "
5571                                          "Data: x%x x%x x%x\n",
5572                                          vport->fc_rscn_id_cnt, vport->fc_flag,
5573                                          vport->port_state);
5574                 } else {
5575                         vport->fc_flag |= FC_RSCN_DISCOVERY;
5576                         spin_unlock_irq(shost->host_lock);
5577                         /* ReDiscovery RSCN */
5578                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5579                                          "0234 ReDiscovery RSCN "
5580                                          "Data: x%x x%x x%x\n",
5581                                          vport->fc_rscn_id_cnt, vport->fc_flag,
5582                                          vport->port_state);
5583                 }
5584                 /* Indicate we are done walking fc_rscn_id_list on this vport */
5585                 vport->fc_rscn_flush = 0;
5586                 /* Send back ACC */
5587                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5588                 /* send RECOVERY event for ALL nodes that match RSCN payload */
5589                 lpfc_rscn_recovery_check(vport);
5590                 spin_lock_irq(shost->host_lock);
5591                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
5592                 spin_unlock_irq(shost->host_lock);
5593                 return 0;
5594         }
5595         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5596                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
5597                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
5598
5599         spin_lock_irq(shost->host_lock);
5600         vport->fc_flag |= FC_RSCN_MODE;
5601         spin_unlock_irq(shost->host_lock);
5602         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
5603         /* Indicate we are done walking fc_rscn_id_list on this vport */
5604         vport->fc_rscn_flush = 0;
5605         /*
5606          * If we zero, cmdiocb->context2, the calling routine will
5607          * not try to free it.
5608          */
5609         cmdiocb->context2 = NULL;
5610         lpfc_set_disctmo(vport);
5611         /* Send back ACC */
5612         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5613         /* send RECOVERY event for ALL nodes that match RSCN payload */
5614         lpfc_rscn_recovery_check(vport);
5615         return lpfc_els_handle_rscn(vport);
5616 }
5617
5618 /**
5619  * lpfc_els_handle_rscn - Handle rscn for a vport
5620  * @vport: pointer to a host virtual N_Port data structure.
5621  *
5622  * This routine handles the Registration State Configuration Notification
5623  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
5624  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
5625  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
5626  * NameServer shall be issued. If CT command to the NameServer fails to be
5627  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
5628  * RSCN activities with the @vport.
5629  *
5630  * Return code
5631  *   0 - Cleaned up rscn on the @vport
5632  *   1 - Wait for plogi to name server before proceed
5633  **/
5634 int
5635 lpfc_els_handle_rscn(struct lpfc_vport *vport)
5636 {
5637         struct lpfc_nodelist *ndlp;
5638         struct lpfc_hba *phba = vport->phba;
5639
5640         /* Ignore RSCN if the port is being torn down. */
5641         if (vport->load_flag & FC_UNLOADING) {
5642                 lpfc_els_flush_rscn(vport);
5643                 return 0;
5644         }
5645
5646         /* Start timer for RSCN processing */
5647         lpfc_set_disctmo(vport);
5648
5649         /* RSCN processed */
5650         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5651                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
5652                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
5653                          vport->port_state);
5654
5655         /* To process RSCN, first compare RSCN data with NameServer */
5656         vport->fc_ns_retry = 0;
5657         vport->num_disc_nodes = 0;
5658
5659         ndlp = lpfc_findnode_did(vport, NameServer_DID);
5660         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
5661             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
5662                 /* Good ndlp, issue CT Request to NameServer */
5663                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
5664                         /* Wait for NameServer query cmpl before we can
5665                            continue */
5666                         return 1;
5667         } else {
5668                 /* If login to NameServer does not exist, issue one */
5669                 /* Good status, issue PLOGI to NameServer */
5670                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
5671                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5672                         /* Wait for NameServer login cmpl before we can
5673                            continue */
5674                         return 1;
5675
5676                 if (ndlp) {
5677                         ndlp = lpfc_enable_node(vport, ndlp,
5678                                                 NLP_STE_PLOGI_ISSUE);
5679                         if (!ndlp) {
5680                                 lpfc_els_flush_rscn(vport);
5681                                 return 0;
5682                         }
5683                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
5684                 } else {
5685                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5686                         if (!ndlp) {
5687                                 lpfc_els_flush_rscn(vport);
5688                                 return 0;
5689                         }
5690                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
5691                         ndlp->nlp_prev_state = ndlp->nlp_state;
5692                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5693                 }
5694                 ndlp->nlp_type |= NLP_FABRIC;
5695                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
5696                 /* Wait for NameServer login cmpl before we can
5697                  * continue
5698                  */
5699                 return 1;
5700         }
5701
5702         lpfc_els_flush_rscn(vport);
5703         return 0;
5704 }
5705
5706 /**
5707  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
5708  * @vport: pointer to a host virtual N_Port data structure.
5709  * @cmdiocb: pointer to lpfc command iocb data structure.
5710  * @ndlp: pointer to a node-list data structure.
5711  *
5712  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
5713  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
5714  * point topology. As an unsolicited FLOGI should not be received in a loop
5715  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
5716  * lpfc_check_sparm() routine is invoked to check the parameters in the
5717  * unsolicited FLOGI. If parameters validation failed, the routine
5718  * lpfc_els_rsp_reject() shall be called with reject reason code set to
5719  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
5720  * FLOGI shall be compared with the Port WWN of the @vport to determine who
5721  * will initiate PLOGI. The higher lexicographical value party shall has
5722  * higher priority (as the winning port) and will initiate PLOGI and
5723  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
5724  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
5725  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
5726  *
5727  * Return code
5728  *   0 - Successfully processed the unsolicited flogi
5729  *   1 - Failed to process the unsolicited flogi
5730  **/
5731 static int
5732 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5733                    struct lpfc_nodelist *ndlp)
5734 {
5735         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5736         struct lpfc_hba  *phba = vport->phba;
5737         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5738         uint32_t *lp = (uint32_t *) pcmd->virt;
5739         IOCB_t *icmd = &cmdiocb->iocb;
5740         struct serv_parm *sp;
5741         LPFC_MBOXQ_t *mbox;
5742         struct ls_rjt stat;
5743         uint32_t cmd, did;
5744         int rc;
5745         uint32_t fc_flag = 0;
5746         uint32_t port_state = 0;
5747
5748         cmd = *lp++;
5749         sp = (struct serv_parm *) lp;
5750
5751         /* FLOGI received */
5752
5753         lpfc_set_disctmo(vport);
5754
5755         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
5756                 /* We should never receive a FLOGI in loop mode, ignore it */
5757                 did = icmd->un.elsreq64.remoteID;
5758
5759                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
5760                    Loop Mode */
5761                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5762                                  "0113 An FLOGI ELS command x%x was "
5763                                  "received from DID x%x in Loop Mode\n",
5764                                  cmd, did);
5765                 return 1;
5766         }
5767
5768         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
5769                 /* For a FLOGI we accept, then if our portname is greater
5770                  * then the remote portname we initiate Nport login.
5771                  */
5772
5773                 rc = memcmp(&vport->fc_portname, &sp->portName,
5774                             sizeof(struct lpfc_name));
5775
5776                 if (!rc) {
5777                         if (phba->sli_rev < LPFC_SLI_REV4) {
5778                                 mbox = mempool_alloc(phba->mbox_mem_pool,
5779                                                      GFP_KERNEL);
5780                                 if (!mbox)
5781                                         return 1;
5782                                 lpfc_linkdown(phba);
5783                                 lpfc_init_link(phba, mbox,
5784                                                phba->cfg_topology,
5785                                                phba->cfg_link_speed);
5786                                 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5787                                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5788                                 mbox->vport = vport;
5789                                 rc = lpfc_sli_issue_mbox(phba, mbox,
5790                                                          MBX_NOWAIT);
5791                                 lpfc_set_loopback_flag(phba);
5792                                 if (rc == MBX_NOT_FINISHED)
5793                                         mempool_free(mbox, phba->mbox_mem_pool);
5794                                 return 1;
5795                         } else {
5796                                 /* abort the flogi coming back to ourselves
5797                                  * due to external loopback on the port.
5798                                  */
5799                                 lpfc_els_abort_flogi(phba);
5800                                 return 0;
5801                         }
5802                 } else if (rc > 0) {    /* greater than */
5803                         spin_lock_irq(shost->host_lock);
5804                         vport->fc_flag |= FC_PT2PT_PLOGI;
5805                         spin_unlock_irq(shost->host_lock);
5806
5807                         /* If we have the high WWPN we can assign our own
5808                          * myDID; otherwise, we have to WAIT for a PLOGI
5809                          * from the remote NPort to find out what it
5810                          * will be.
5811                          */
5812                         vport->fc_myDID = PT2PT_LocalID;
5813                 } else
5814                         vport->fc_myDID = PT2PT_RemoteID;
5815
5816                 /*
5817                  * The vport state should go to LPFC_FLOGI only
5818                  * AFTER we issue a FLOGI, not receive one.
5819                  */
5820                 spin_lock_irq(shost->host_lock);
5821                 fc_flag = vport->fc_flag;
5822                 port_state = vport->port_state;
5823                 vport->fc_flag |= FC_PT2PT;
5824                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
5825                 spin_unlock_irq(shost->host_lock);
5826                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5827                                  "3311 Rcv Flogi PS x%x new PS x%x "
5828                                  "fc_flag x%x new fc_flag x%x\n",
5829                                  port_state, vport->port_state,
5830                                  fc_flag, vport->fc_flag);
5831
5832                 /*
5833                  * We temporarily set fc_myDID to make it look like we are
5834                  * a Fabric. This is done just so we end up with the right
5835                  * did / sid on the FLOGI ACC rsp.
5836                  */
5837                 did = vport->fc_myDID;
5838                 vport->fc_myDID = Fabric_DID;
5839
5840         } else {
5841                 /* Reject this request because invalid parameters */
5842                 stat.un.b.lsRjtRsvd0 = 0;
5843                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5844                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
5845                 stat.un.b.vendorUnique = 0;
5846
5847                 /*
5848                  * We temporarily set fc_myDID to make it look like we are
5849                  * a Fabric. This is done just so we end up with the right
5850                  * did / sid on the FLOGI LS_RJT rsp.
5851                  */
5852                 did = vport->fc_myDID;
5853                 vport->fc_myDID = Fabric_DID;
5854
5855                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5856                         NULL);
5857
5858                 /* Now lets put fc_myDID back to what its supposed to be */
5859                 vport->fc_myDID = did;
5860
5861                 return 1;
5862         }
5863
5864         /* send our FLOGI first */
5865         if (vport->port_state < LPFC_FLOGI) {
5866                 vport->fc_myDID = 0;
5867                 lpfc_initial_flogi(vport);
5868                 vport->fc_myDID = Fabric_DID;
5869         }
5870
5871         /* Send back ACC */
5872         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
5873
5874         /* Now lets put fc_myDID back to what its supposed to be */
5875         vport->fc_myDID = did;
5876
5877         if (!(vport->fc_flag & FC_PT2PT_PLOGI)) {
5878
5879                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5880                 if (!mbox)
5881                         goto fail;
5882
5883                 lpfc_config_link(phba, mbox);
5884
5885                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5886                 mbox->vport = vport;
5887                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5888                 if (rc == MBX_NOT_FINISHED) {
5889                         mempool_free(mbox, phba->mbox_mem_pool);
5890                         goto fail;
5891                 }
5892         }
5893
5894         return 0;
5895 fail:
5896         return 1;
5897 }
5898
5899 /**
5900  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
5901  * @vport: pointer to a host virtual N_Port data structure.
5902  * @cmdiocb: pointer to lpfc command iocb data structure.
5903  * @ndlp: pointer to a node-list data structure.
5904  *
5905  * This routine processes Request Node Identification Data (RNID) IOCB
5906  * received as an ELS unsolicited event. Only when the RNID specified format
5907  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
5908  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
5909  * Accept (ACC) the RNID ELS command. All the other RNID formats are
5910  * rejected by invoking the lpfc_els_rsp_reject() routine.
5911  *
5912  * Return code
5913  *   0 - Successfully processed rnid iocb (currently always return 0)
5914  **/
5915 static int
5916 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5917                   struct lpfc_nodelist *ndlp)
5918 {
5919         struct lpfc_dmabuf *pcmd;
5920         uint32_t *lp;
5921         RNID *rn;
5922         struct ls_rjt stat;
5923         uint32_t cmd;
5924
5925         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5926         lp = (uint32_t *) pcmd->virt;
5927
5928         cmd = *lp++;
5929         rn = (RNID *) lp;
5930
5931         /* RNID received */
5932
5933         switch (rn->Format) {
5934         case 0:
5935         case RNID_TOPOLOGY_DISC:
5936                 /* Send back ACC */
5937                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
5938                 break;
5939         default:
5940                 /* Reject this request because format not supported */
5941                 stat.un.b.lsRjtRsvd0 = 0;
5942                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5943                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5944                 stat.un.b.vendorUnique = 0;
5945                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5946                         NULL);
5947         }
5948         return 0;
5949 }
5950
5951 /**
5952  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
5953  * @vport: pointer to a host virtual N_Port data structure.
5954  * @cmdiocb: pointer to lpfc command iocb data structure.
5955  * @ndlp: pointer to a node-list data structure.
5956  *
5957  * Return code
5958  *   0 - Successfully processed echo iocb (currently always return 0)
5959  **/
5960 static int
5961 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5962                   struct lpfc_nodelist *ndlp)
5963 {
5964         uint8_t *pcmd;
5965
5966         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
5967
5968         /* skip over first word of echo command to find echo data */
5969         pcmd += sizeof(uint32_t);
5970
5971         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
5972         return 0;
5973 }
5974
5975 /**
5976  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
5977  * @vport: pointer to a host virtual N_Port data structure.
5978  * @cmdiocb: pointer to lpfc command iocb data structure.
5979  * @ndlp: pointer to a node-list data structure.
5980  *
5981  * This routine processes a Link Incident Report Registration(LIRR) IOCB
5982  * received as an ELS unsolicited event. Currently, this function just invokes
5983  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
5984  *
5985  * Return code
5986  *   0 - Successfully processed lirr iocb (currently always return 0)
5987  **/
5988 static int
5989 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5990                   struct lpfc_nodelist *ndlp)
5991 {
5992         struct ls_rjt stat;
5993
5994         /* For now, unconditionally reject this command */
5995         stat.un.b.lsRjtRsvd0 = 0;
5996         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5997         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5998         stat.un.b.vendorUnique = 0;
5999         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6000         return 0;
6001 }
6002
6003 /**
6004  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
6005  * @vport: pointer to a host virtual N_Port data structure.
6006  * @cmdiocb: pointer to lpfc command iocb data structure.
6007  * @ndlp: pointer to a node-list data structure.
6008  *
6009  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
6010  * received as an ELS unsolicited event. A request to RRQ shall only
6011  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
6012  * Nx_Port N_Port_ID of the target Exchange is the same as the
6013  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
6014  * not accepted, an LS_RJT with reason code "Unable to perform
6015  * command request" and reason code explanation "Invalid Originator
6016  * S_ID" shall be returned. For now, we just unconditionally accept
6017  * RRQ from the target.
6018  **/
6019 static void
6020 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6021                  struct lpfc_nodelist *ndlp)
6022 {
6023         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6024         if (vport->phba->sli_rev == LPFC_SLI_REV4)
6025                 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
6026 }
6027
6028 /**
6029  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6030  * @phba: pointer to lpfc hba data structure.
6031  * @pmb: pointer to the driver internal queue element for mailbox command.
6032  *
6033  * This routine is the completion callback function for the MBX_READ_LNK_STAT
6034  * mailbox command. This callback function is to actually send the Accept
6035  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6036  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6037  * mailbox command, constructs the RPS response with the link statistics
6038  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6039  * response to the RPS.
6040  *
6041  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6042  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6043  * will be stored into the context1 field of the IOCB for the completion
6044  * callback function to the RPS Accept Response ELS IOCB command.
6045  *
6046  **/
6047 static void
6048 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6049 {
6050         MAILBOX_t *mb;
6051         IOCB_t *icmd;
6052         struct RLS_RSP *rls_rsp;
6053         uint8_t *pcmd;
6054         struct lpfc_iocbq *elsiocb;
6055         struct lpfc_nodelist *ndlp;
6056         uint16_t oxid;
6057         uint16_t rxid;
6058         uint32_t cmdsize;
6059
6060         mb = &pmb->u.mb;
6061
6062         ndlp = (struct lpfc_nodelist *) pmb->context2;
6063         rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
6064         oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
6065         pmb->context1 = NULL;
6066         pmb->context2 = NULL;
6067
6068         if (mb->mbxStatus) {
6069                 mempool_free(pmb, phba->mbox_mem_pool);
6070                 return;
6071         }
6072
6073         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
6074         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6075                                      lpfc_max_els_tries, ndlp,
6076                                      ndlp->nlp_DID, ELS_CMD_ACC);
6077
6078         /* Decrement the ndlp reference count from previous mbox command */
6079         lpfc_nlp_put(ndlp);
6080
6081         if (!elsiocb) {
6082                 mempool_free(pmb, phba->mbox_mem_pool);
6083                 return;
6084         }
6085
6086         icmd = &elsiocb->iocb;
6087         icmd->ulpContext = rxid;
6088         icmd->unsli3.rcvsli3.ox_id = oxid;
6089
6090         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6091         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6092         pcmd += sizeof(uint32_t); /* Skip past command */
6093         rls_rsp = (struct RLS_RSP *)pcmd;
6094
6095         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6096         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6097         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6098         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6099         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6100         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
6101         mempool_free(pmb, phba->mbox_mem_pool);
6102         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6103         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6104                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
6105                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6106                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6107                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6108                          ndlp->nlp_rpi);
6109         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6110         phba->fc_stat.elsXmitACC++;
6111         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6112                 lpfc_els_free_iocb(phba, elsiocb);
6113 }
6114
6115 /**
6116  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6117  * @phba: pointer to lpfc hba data structure.
6118  * @pmb: pointer to the driver internal queue element for mailbox command.
6119  *
6120  * This routine is the completion callback function for the MBX_READ_LNK_STAT
6121  * mailbox command. This callback function is to actually send the Accept
6122  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6123  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6124  * mailbox command, constructs the RPS response with the link statistics
6125  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6126  * response to the RPS.
6127  *
6128  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6129  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6130  * will be stored into the context1 field of the IOCB for the completion
6131  * callback function to the RPS Accept Response ELS IOCB command.
6132  *
6133  **/
6134 static void
6135 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6136 {
6137         MAILBOX_t *mb;
6138         IOCB_t *icmd;
6139         RPS_RSP *rps_rsp;
6140         uint8_t *pcmd;
6141         struct lpfc_iocbq *elsiocb;
6142         struct lpfc_nodelist *ndlp;
6143         uint16_t status;
6144         uint16_t oxid;
6145         uint16_t rxid;
6146         uint32_t cmdsize;
6147
6148         mb = &pmb->u.mb;
6149
6150         ndlp = (struct lpfc_nodelist *) pmb->context2;
6151         rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
6152         oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
6153         pmb->context1 = NULL;
6154         pmb->context2 = NULL;
6155
6156         if (mb->mbxStatus) {
6157                 mempool_free(pmb, phba->mbox_mem_pool);
6158                 return;
6159         }
6160
6161         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
6162         mempool_free(pmb, phba->mbox_mem_pool);
6163         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6164                                      lpfc_max_els_tries, ndlp,
6165                                      ndlp->nlp_DID, ELS_CMD_ACC);
6166
6167         /* Decrement the ndlp reference count from previous mbox command */
6168         lpfc_nlp_put(ndlp);
6169
6170         if (!elsiocb)
6171                 return;
6172
6173         icmd = &elsiocb->iocb;
6174         icmd->ulpContext = rxid;
6175         icmd->unsli3.rcvsli3.ox_id = oxid;
6176
6177         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6178         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6179         pcmd += sizeof(uint32_t); /* Skip past command */
6180         rps_rsp = (RPS_RSP *)pcmd;
6181
6182         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
6183                 status = 0x10;
6184         else
6185                 status = 0x8;
6186         if (phba->pport->fc_flag & FC_FABRIC)
6187                 status |= 0x4;
6188
6189         rps_rsp->rsvd1 = 0;
6190         rps_rsp->portStatus = cpu_to_be16(status);
6191         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6192         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6193         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6194         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6195         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6196         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
6197         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
6198         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6199                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
6200                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6201                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6202                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6203                          ndlp->nlp_rpi);
6204         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6205         phba->fc_stat.elsXmitACC++;
6206         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6207                 lpfc_els_free_iocb(phba, elsiocb);
6208         return;
6209 }
6210
6211 /**
6212  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
6213  * @vport: pointer to a host virtual N_Port data structure.
6214  * @cmdiocb: pointer to lpfc command iocb data structure.
6215  * @ndlp: pointer to a node-list data structure.
6216  *
6217  * This routine processes Read Port Status (RPL) IOCB received as an
6218  * ELS unsolicited event. It first checks the remote port state. If the
6219  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6220  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6221  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
6222  * for reading the HBA link statistics. It is for the callback function,
6223  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
6224  * to actually sending out RPL Accept (ACC) response.
6225  *
6226  * Return codes
6227  *   0 - Successfully processed rls iocb (currently always return 0)
6228  **/
6229 static int
6230 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6231                  struct lpfc_nodelist *ndlp)
6232 {
6233         struct lpfc_hba *phba = vport->phba;
6234         LPFC_MBOXQ_t *mbox;
6235         struct ls_rjt stat;
6236
6237         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6238             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6239                 /* reject the unsolicited RPS request and done with it */
6240                 goto reject_out;
6241
6242         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
6243         if (mbox) {
6244                 lpfc_read_lnk_stat(phba, mbox);
6245                 mbox->context1 = (void *)((unsigned long)
6246                         ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
6247                         cmdiocb->iocb.ulpContext)); /* rx_id */
6248                 mbox->context2 = lpfc_nlp_get(ndlp);
6249                 mbox->vport = vport;
6250                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
6251                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6252                         != MBX_NOT_FINISHED)
6253                         /* Mbox completion will send ELS Response */
6254                         return 0;
6255                 /* Decrement reference count used for the failed mbox
6256                  * command.
6257                  */
6258                 lpfc_nlp_put(ndlp);
6259                 mempool_free(mbox, phba->mbox_mem_pool);
6260         }
6261 reject_out:
6262         /* issue rejection response */
6263         stat.un.b.lsRjtRsvd0 = 0;
6264         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6265         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6266         stat.un.b.vendorUnique = 0;
6267         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6268         return 0;
6269 }
6270
6271 /**
6272  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
6273  * @vport: pointer to a host virtual N_Port data structure.
6274  * @cmdiocb: pointer to lpfc command iocb data structure.
6275  * @ndlp: pointer to a node-list data structure.
6276  *
6277  * This routine processes Read Timout Value (RTV) IOCB received as an
6278  * ELS unsolicited event. It first checks the remote port state. If the
6279  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6280  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6281  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
6282  * Value (RTV) unsolicited IOCB event.
6283  *
6284  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6285  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6286  * will be stored into the context1 field of the IOCB for the completion
6287  * callback function to the RPS Accept Response ELS IOCB command.
6288  *
6289  * Return codes
6290  *   0 - Successfully processed rtv iocb (currently always return 0)
6291  **/
6292 static int
6293 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6294                  struct lpfc_nodelist *ndlp)
6295 {
6296         struct lpfc_hba *phba = vport->phba;
6297         struct ls_rjt stat;
6298         struct RTV_RSP *rtv_rsp;
6299         uint8_t *pcmd;
6300         struct lpfc_iocbq *elsiocb;
6301         uint32_t cmdsize;
6302
6303
6304         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6305             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6306                 /* reject the unsolicited RPS request and done with it */
6307                 goto reject_out;
6308
6309         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
6310         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6311                                      lpfc_max_els_tries, ndlp,
6312                                      ndlp->nlp_DID, ELS_CMD_ACC);
6313
6314         if (!elsiocb)
6315                 return 1;
6316
6317         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6318                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6319         pcmd += sizeof(uint32_t); /* Skip past command */
6320
6321         /* use the command's xri in the response */
6322         elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
6323         elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6324
6325         rtv_rsp = (struct RTV_RSP *)pcmd;
6326
6327         /* populate RTV payload */
6328         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
6329         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
6330         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
6331         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
6332         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
6333
6334         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6335         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6336                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
6337                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
6338                          "Data: x%x x%x x%x\n",
6339                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6340                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6341                          ndlp->nlp_rpi,
6342                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
6343         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6344         phba->fc_stat.elsXmitACC++;
6345         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6346                 lpfc_els_free_iocb(phba, elsiocb);
6347         return 0;
6348
6349 reject_out:
6350         /* issue rejection response */
6351         stat.un.b.lsRjtRsvd0 = 0;
6352         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6353         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6354         stat.un.b.vendorUnique = 0;
6355         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6356         return 0;
6357 }
6358
6359 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
6360  * @vport: pointer to a host virtual N_Port data structure.
6361  * @cmdiocb: pointer to lpfc command iocb data structure.
6362  * @ndlp: pointer to a node-list data structure.
6363  *
6364  * This routine processes Read Port Status (RPS) IOCB received as an
6365  * ELS unsolicited event. It first checks the remote port state. If the
6366  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6367  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
6368  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
6369  * for reading the HBA link statistics. It is for the callback function,
6370  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
6371  * to actually sending out RPS Accept (ACC) response.
6372  *
6373  * Return codes
6374  *   0 - Successfully processed rps iocb (currently always return 0)
6375  **/
6376 static int
6377 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6378                  struct lpfc_nodelist *ndlp)
6379 {
6380         struct lpfc_hba *phba = vport->phba;
6381         uint32_t *lp;
6382         uint8_t flag;
6383         LPFC_MBOXQ_t *mbox;
6384         struct lpfc_dmabuf *pcmd;
6385         RPS *rps;
6386         struct ls_rjt stat;
6387
6388         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6389             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
6390                 /* reject the unsolicited RPS request and done with it */
6391                 goto reject_out;
6392
6393         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6394         lp = (uint32_t *) pcmd->virt;
6395         flag = (be32_to_cpu(*lp++) & 0xf);
6396         rps = (RPS *) lp;
6397
6398         if ((flag == 0) ||
6399             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
6400             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
6401                                     sizeof(struct lpfc_name)) == 0))) {
6402
6403                 printk("Fix me....\n");
6404                 dump_stack();
6405                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
6406                 if (mbox) {
6407                         lpfc_read_lnk_stat(phba, mbox);
6408                         mbox->context1 = (void *)((unsigned long)
6409                                 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
6410                                 cmdiocb->iocb.ulpContext)); /* rx_id */
6411                         mbox->context2 = lpfc_nlp_get(ndlp);
6412                         mbox->vport = vport;
6413                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
6414                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6415                                 != MBX_NOT_FINISHED)
6416                                 /* Mbox completion will send ELS Response */
6417                                 return 0;
6418                         /* Decrement reference count used for the failed mbox
6419                          * command.
6420                          */
6421                         lpfc_nlp_put(ndlp);
6422                         mempool_free(mbox, phba->mbox_mem_pool);
6423                 }
6424         }
6425
6426 reject_out:
6427         /* issue rejection response */
6428         stat.un.b.lsRjtRsvd0 = 0;
6429         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6430         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6431         stat.un.b.vendorUnique = 0;
6432         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6433         return 0;
6434 }
6435
6436 /* lpfc_issue_els_rrq - Process an unsolicited rps iocb
6437  * @vport: pointer to a host virtual N_Port data structure.
6438  * @ndlp: pointer to a node-list data structure.
6439  * @did: DID of the target.
6440  * @rrq: Pointer to the rrq struct.
6441  *
6442  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
6443  * Successful the the completion handler will clear the RRQ.
6444  *
6445  * Return codes
6446  *   0 - Successfully sent rrq els iocb.
6447  *   1 - Failed to send rrq els iocb.
6448  **/
6449 static int
6450 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
6451                         uint32_t did, struct lpfc_node_rrq *rrq)
6452 {
6453         struct lpfc_hba  *phba = vport->phba;
6454         struct RRQ *els_rrq;
6455         struct lpfc_iocbq *elsiocb;
6456         uint8_t *pcmd;
6457         uint16_t cmdsize;
6458         int ret;
6459
6460
6461         if (ndlp != rrq->ndlp)
6462                 ndlp = rrq->ndlp;
6463         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
6464                 return 1;
6465
6466         /* If ndlp is not NULL, we will bump the reference count on it */
6467         cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
6468         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
6469                                      ELS_CMD_RRQ);
6470         if (!elsiocb)
6471                 return 1;
6472
6473         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6474
6475         /* For RRQ request, remainder of payload is Exchange IDs */
6476         *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
6477         pcmd += sizeof(uint32_t);
6478         els_rrq = (struct RRQ *) pcmd;
6479
6480         bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
6481         bf_set(rrq_rxid, els_rrq, rrq->rxid);
6482         bf_set(rrq_did, els_rrq, vport->fc_myDID);
6483         els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
6484         els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
6485
6486
6487         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6488                 "Issue RRQ:     did:x%x",
6489                 did, rrq->xritag, rrq->rxid);
6490         elsiocb->context_un.rrq = rrq;
6491         elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
6492         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6493
6494         if (ret == IOCB_ERROR) {
6495                 lpfc_els_free_iocb(phba, elsiocb);
6496                 return 1;
6497         }
6498         return 0;
6499 }
6500
6501 /**
6502  * lpfc_send_rrq - Sends ELS RRQ if needed.
6503  * @phba: pointer to lpfc hba data structure.
6504  * @rrq: pointer to the active rrq.
6505  *
6506  * This routine will call the lpfc_issue_els_rrq if the rrq is
6507  * still active for the xri. If this function returns a failure then
6508  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
6509  *
6510  * Returns 0 Success.
6511  *         1 Failure.
6512  **/
6513 int
6514 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
6515 {
6516         struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
6517                                                         rrq->nlp_DID);
6518         if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
6519                 return lpfc_issue_els_rrq(rrq->vport, ndlp,
6520                                          rrq->nlp_DID, rrq);
6521         else
6522                 return 1;
6523 }
6524
6525 /**
6526  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
6527  * @vport: pointer to a host virtual N_Port data structure.
6528  * @cmdsize: size of the ELS command.
6529  * @oldiocb: pointer to the original lpfc command iocb data structure.
6530  * @ndlp: pointer to a node-list data structure.
6531  *
6532  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
6533  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
6534  *
6535  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6536  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6537  * will be stored into the context1 field of the IOCB for the completion
6538  * callback function to the RPL Accept Response ELS command.
6539  *
6540  * Return code
6541  *   0 - Successfully issued ACC RPL ELS command
6542  *   1 - Failed to issue ACC RPL ELS command
6543  **/
6544 static int
6545 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
6546                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6547 {
6548         struct lpfc_hba *phba = vport->phba;
6549         IOCB_t *icmd, *oldcmd;
6550         RPL_RSP rpl_rsp;
6551         struct lpfc_iocbq *elsiocb;
6552         uint8_t *pcmd;
6553
6554         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6555                                      ndlp->nlp_DID, ELS_CMD_ACC);
6556
6557         if (!elsiocb)
6558                 return 1;
6559
6560         icmd = &elsiocb->iocb;
6561         oldcmd = &oldiocb->iocb;
6562         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
6563         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
6564
6565         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6566         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6567         pcmd += sizeof(uint16_t);
6568         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
6569         pcmd += sizeof(uint16_t);
6570
6571         /* Setup the RPL ACC payload */
6572         rpl_rsp.listLen = be32_to_cpu(1);
6573         rpl_rsp.index = 0;
6574         rpl_rsp.port_num_blk.portNum = 0;
6575         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
6576         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
6577             sizeof(struct lpfc_name));
6578         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
6579         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
6580         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6581                          "0120 Xmit ELS RPL ACC response tag x%x "
6582                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
6583                          "rpi x%x\n",
6584                          elsiocb->iotag, elsiocb->iocb.ulpContext,
6585                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6586                          ndlp->nlp_rpi);
6587         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6588         phba->fc_stat.elsXmitACC++;
6589         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
6590             IOCB_ERROR) {
6591                 lpfc_els_free_iocb(phba, elsiocb);
6592                 return 1;
6593         }
6594         return 0;
6595 }
6596
6597 /**
6598  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
6599  * @vport: pointer to a host virtual N_Port data structure.
6600  * @cmdiocb: pointer to lpfc command iocb data structure.
6601  * @ndlp: pointer to a node-list data structure.
6602  *
6603  * This routine processes Read Port List (RPL) IOCB received as an ELS
6604  * unsolicited event. It first checks the remote port state. If the remote
6605  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
6606  * invokes the lpfc_els_rsp_reject() routine to send reject response.
6607  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
6608  * to accept the RPL.
6609  *
6610  * Return code
6611  *   0 - Successfully processed rpl iocb (currently always return 0)
6612  **/
6613 static int
6614 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6615                  struct lpfc_nodelist *ndlp)
6616 {
6617         struct lpfc_dmabuf *pcmd;
6618         uint32_t *lp;
6619         uint32_t maxsize;
6620         uint16_t cmdsize;
6621         RPL *rpl;
6622         struct ls_rjt stat;
6623
6624         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6625             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
6626                 /* issue rejection response */
6627                 stat.un.b.lsRjtRsvd0 = 0;
6628                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6629                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6630                 stat.un.b.vendorUnique = 0;
6631                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6632                         NULL);
6633                 /* rejected the unsolicited RPL request and done with it */
6634                 return 0;
6635         }
6636
6637         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6638         lp = (uint32_t *) pcmd->virt;
6639         rpl = (RPL *) (lp + 1);
6640         maxsize = be32_to_cpu(rpl->maxsize);
6641
6642         /* We support only one port */
6643         if ((rpl->index == 0) &&
6644             ((maxsize == 0) ||
6645              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
6646                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
6647         } else {
6648                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
6649         }
6650         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
6651
6652         return 0;
6653 }
6654
6655 /**
6656  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
6657  * @vport: pointer to a virtual N_Port data structure.
6658  * @cmdiocb: pointer to lpfc command iocb data structure.
6659  * @ndlp: pointer to a node-list data structure.
6660  *
6661  * This routine processes Fibre Channel Address Resolution Protocol
6662  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
6663  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
6664  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
6665  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
6666  * remote PortName is compared against the FC PortName stored in the @vport
6667  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
6668  * compared against the FC NodeName stored in the @vport data structure.
6669  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
6670  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
6671  * invoked to send out FARP Response to the remote node. Before sending the
6672  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
6673  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
6674  * routine is invoked to log into the remote port first.
6675  *
6676  * Return code
6677  *   0 - Either the FARP Match Mode not supported or successfully processed
6678  **/
6679 static int
6680 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6681                   struct lpfc_nodelist *ndlp)
6682 {
6683         struct lpfc_dmabuf *pcmd;
6684         uint32_t *lp;
6685         IOCB_t *icmd;
6686         FARP *fp;
6687         uint32_t cmd, cnt, did;
6688
6689         icmd = &cmdiocb->iocb;
6690         did = icmd->un.elsreq64.remoteID;
6691         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6692         lp = (uint32_t *) pcmd->virt;
6693
6694         cmd = *lp++;
6695         fp = (FARP *) lp;
6696         /* FARP-REQ received from DID <did> */
6697         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6698                          "0601 FARP-REQ received from DID x%x\n", did);
6699         /* We will only support match on WWPN or WWNN */
6700         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
6701                 return 0;
6702         }
6703
6704         cnt = 0;
6705         /* If this FARP command is searching for my portname */
6706         if (fp->Mflags & FARP_MATCH_PORT) {
6707                 if (memcmp(&fp->RportName, &vport->fc_portname,
6708                            sizeof(struct lpfc_name)) == 0)
6709                         cnt = 1;
6710         }
6711
6712         /* If this FARP command is searching for my nodename */
6713         if (fp->Mflags & FARP_MATCH_NODE) {
6714                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
6715                            sizeof(struct lpfc_name)) == 0)
6716                         cnt = 1;
6717         }
6718
6719         if (cnt) {
6720                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
6721                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
6722                         /* Log back into the node before sending the FARP. */
6723                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
6724                                 ndlp->nlp_prev_state = ndlp->nlp_state;
6725                                 lpfc_nlp_set_state(vport, ndlp,
6726                                                    NLP_STE_PLOGI_ISSUE);
6727                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6728                         }
6729
6730                         /* Send a FARP response to that node */
6731                         if (fp->Rflags & FARP_REQUEST_FARPR)
6732                                 lpfc_issue_els_farpr(vport, did, 0);
6733                 }
6734         }
6735         return 0;
6736 }
6737
6738 /**
6739  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
6740  * @vport: pointer to a host virtual N_Port data structure.
6741  * @cmdiocb: pointer to lpfc command iocb data structure.
6742  * @ndlp: pointer to a node-list data structure.
6743  *
6744  * This routine processes Fibre Channel Address Resolution Protocol
6745  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
6746  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
6747  * the FARP response request.
6748  *
6749  * Return code
6750  *   0 - Successfully processed FARPR IOCB (currently always return 0)
6751  **/
6752 static int
6753 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6754                    struct lpfc_nodelist  *ndlp)
6755 {
6756         struct lpfc_dmabuf *pcmd;
6757         uint32_t *lp;
6758         IOCB_t *icmd;
6759         uint32_t cmd, did;
6760
6761         icmd = &cmdiocb->iocb;
6762         did = icmd->un.elsreq64.remoteID;
6763         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6764         lp = (uint32_t *) pcmd->virt;
6765
6766         cmd = *lp++;
6767         /* FARP-RSP received from DID <did> */
6768         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6769                          "0600 FARP-RSP received from DID x%x\n", did);
6770         /* ACCEPT the Farp resp request */
6771         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6772
6773         return 0;
6774 }
6775
6776 /**
6777  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
6778  * @vport: pointer to a host virtual N_Port data structure.
6779  * @cmdiocb: pointer to lpfc command iocb data structure.
6780  * @fan_ndlp: pointer to a node-list data structure.
6781  *
6782  * This routine processes a Fabric Address Notification (FAN) IOCB
6783  * command received as an ELS unsolicited event. The FAN ELS command will
6784  * only be processed on a physical port (i.e., the @vport represents the
6785  * physical port). The fabric NodeName and PortName from the FAN IOCB are
6786  * compared against those in the phba data structure. If any of those is
6787  * different, the lpfc_initial_flogi() routine is invoked to initialize
6788  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
6789  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
6790  * is invoked to register login to the fabric.
6791  *
6792  * Return code
6793  *   0 - Successfully processed fan iocb (currently always return 0).
6794  **/
6795 static int
6796 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6797                  struct lpfc_nodelist *fan_ndlp)
6798 {
6799         struct lpfc_hba *phba = vport->phba;
6800         uint32_t *lp;
6801         FAN *fp;
6802
6803         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
6804         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
6805         fp = (FAN *) ++lp;
6806         /* FAN received; Fan does not have a reply sequence */
6807         if ((vport == phba->pport) &&
6808             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
6809                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
6810                             sizeof(struct lpfc_name))) ||
6811                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
6812                             sizeof(struct lpfc_name)))) {
6813                         /* This port has switched fabrics. FLOGI is required */
6814                         lpfc_issue_init_vfi(vport);
6815                 } else {
6816                         /* FAN verified - skip FLOGI */
6817                         vport->fc_myDID = vport->fc_prevDID;
6818                         if (phba->sli_rev < LPFC_SLI_REV4)
6819                                 lpfc_issue_fabric_reglogin(vport);
6820                         else {
6821                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6822                                         "3138 Need register VFI: (x%x/%x)\n",
6823                                         vport->fc_prevDID, vport->fc_myDID);
6824                                 lpfc_issue_reg_vfi(vport);
6825                         }
6826                 }
6827         }
6828         return 0;
6829 }
6830
6831 /**
6832  * lpfc_els_timeout - Handler funciton to the els timer
6833  * @ptr: holder for the timer function associated data.
6834  *
6835  * This routine is invoked by the ELS timer after timeout. It posts the ELS
6836  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
6837  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
6838  * up the worker thread. It is for the worker thread to invoke the routine
6839  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
6840  **/
6841 void
6842 lpfc_els_timeout(unsigned long ptr)
6843 {
6844         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
6845         struct lpfc_hba   *phba = vport->phba;
6846         uint32_t tmo_posted;
6847         unsigned long iflag;
6848
6849         spin_lock_irqsave(&vport->work_port_lock, iflag);
6850         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
6851         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
6852                 vport->work_port_events |= WORKER_ELS_TMO;
6853         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
6854
6855         if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
6856                 lpfc_worker_wake_up(phba);
6857         return;
6858 }
6859
6860
6861 /**
6862  * lpfc_els_timeout_handler - Process an els timeout event
6863  * @vport: pointer to a virtual N_Port data structure.
6864  *
6865  * This routine is the actual handler function that processes an ELS timeout
6866  * event. It walks the ELS ring to get and abort all the IOCBs (except the
6867  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
6868  * invoking the lpfc_sli_issue_abort_iotag() routine.
6869  **/
6870 void
6871 lpfc_els_timeout_handler(struct lpfc_vport *vport)
6872 {
6873         struct lpfc_hba  *phba = vport->phba;
6874         struct lpfc_sli_ring *pring;
6875         struct lpfc_iocbq *tmp_iocb, *piocb;
6876         IOCB_t *cmd = NULL;
6877         struct lpfc_dmabuf *pcmd;
6878         uint32_t els_command = 0;
6879         uint32_t timeout;
6880         uint32_t remote_ID = 0xffffffff;
6881         LIST_HEAD(abort_list);
6882
6883
6884         timeout = (uint32_t)(phba->fc_ratov << 1);
6885
6886         pring = &phba->sli.ring[LPFC_ELS_RING];
6887         if ((phba->pport->load_flag & FC_UNLOADING))
6888                 return;
6889         spin_lock_irq(&phba->hbalock);
6890         if (phba->sli_rev == LPFC_SLI_REV4)
6891                 spin_lock(&pring->ring_lock);
6892
6893         if ((phba->pport->load_flag & FC_UNLOADING)) {
6894                 if (phba->sli_rev == LPFC_SLI_REV4)
6895                         spin_unlock(&pring->ring_lock);
6896                 spin_unlock_irq(&phba->hbalock);
6897                 return;
6898         }
6899
6900         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6901                 cmd = &piocb->iocb;
6902
6903                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
6904                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6905                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6906                         continue;
6907
6908                 if (piocb->vport != vport)
6909                         continue;
6910
6911                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
6912                 if (pcmd)
6913                         els_command = *(uint32_t *) (pcmd->virt);
6914
6915                 if (els_command == ELS_CMD_FARP ||
6916                     els_command == ELS_CMD_FARPR ||
6917                     els_command == ELS_CMD_FDISC)
6918                         continue;
6919
6920                 if (piocb->drvrTimeout > 0) {
6921                         if (piocb->drvrTimeout >= timeout)
6922                                 piocb->drvrTimeout -= timeout;
6923                         else
6924                                 piocb->drvrTimeout = 0;
6925                         continue;
6926                 }
6927
6928                 remote_ID = 0xffffffff;
6929                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
6930                         remote_ID = cmd->un.elsreq64.remoteID;
6931                 else {
6932                         struct lpfc_nodelist *ndlp;
6933                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
6934                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
6935                                 remote_ID = ndlp->nlp_DID;
6936                 }
6937                 list_add_tail(&piocb->dlist, &abort_list);
6938         }
6939         if (phba->sli_rev == LPFC_SLI_REV4)
6940                 spin_unlock(&pring->ring_lock);
6941         spin_unlock_irq(&phba->hbalock);
6942
6943         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
6944                 cmd = &piocb->iocb;
6945                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6946                          "0127 ELS timeout Data: x%x x%x x%x "
6947                          "x%x\n", els_command,
6948                          remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
6949                 spin_lock_irq(&phba->hbalock);
6950                 list_del_init(&piocb->dlist);
6951                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6952                 spin_unlock_irq(&phba->hbalock);
6953         }
6954
6955         if (!list_empty(&phba->sli.ring[LPFC_ELS_RING].txcmplq))
6956                 if (!(phba->pport->load_flag & FC_UNLOADING))
6957                         mod_timer(&vport->els_tmofunc,
6958                                   jiffies + msecs_to_jiffies(1000 * timeout));
6959 }
6960
6961 /**
6962  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
6963  * @vport: pointer to a host virtual N_Port data structure.
6964  *
6965  * This routine is used to clean up all the outstanding ELS commands on a
6966  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
6967  * routine. After that, it walks the ELS transmit queue to remove all the
6968  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
6969  * the IOCBs with a non-NULL completion callback function, the callback
6970  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
6971  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
6972  * callback function, the IOCB will simply be released. Finally, it walks
6973  * the ELS transmit completion queue to issue an abort IOCB to any transmit
6974  * completion queue IOCB that is associated with the @vport and is not
6975  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
6976  * part of the discovery state machine) out to HBA by invoking the
6977  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
6978  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
6979  * the IOCBs are aborted when this function returns.
6980  **/
6981 void
6982 lpfc_els_flush_cmd(struct lpfc_vport *vport)
6983 {
6984         LIST_HEAD(abort_list);
6985         struct lpfc_hba  *phba = vport->phba;
6986         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6987         struct lpfc_iocbq *tmp_iocb, *piocb;
6988         IOCB_t *cmd = NULL;
6989
6990         lpfc_fabric_abort_vport(vport);
6991         /*
6992          * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
6993          * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
6994          * ultimately grabs the ring_lock, the driver must splice the list into
6995          * a working list and release the locks before calling the abort.
6996          */
6997         spin_lock_irq(&phba->hbalock);
6998         if (phba->sli_rev == LPFC_SLI_REV4)
6999                 spin_lock(&pring->ring_lock);
7000
7001         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7002                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
7003                         continue;
7004
7005                 if (piocb->vport != vport)
7006                         continue;
7007                 list_add_tail(&piocb->dlist, &abort_list);
7008         }
7009         if (phba->sli_rev == LPFC_SLI_REV4)
7010                 spin_unlock(&pring->ring_lock);
7011         spin_unlock_irq(&phba->hbalock);
7012         /* Abort each iocb on the aborted list and remove the dlist links. */
7013         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
7014                 spin_lock_irq(&phba->hbalock);
7015                 list_del_init(&piocb->dlist);
7016                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
7017                 spin_unlock_irq(&phba->hbalock);
7018         }
7019         if (!list_empty(&abort_list))
7020                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7021                                  "3387 abort list for txq not empty\n");
7022         INIT_LIST_HEAD(&abort_list);
7023
7024         spin_lock_irq(&phba->hbalock);
7025         if (phba->sli_rev == LPFC_SLI_REV4)
7026                 spin_lock(&pring->ring_lock);
7027
7028         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
7029                 cmd = &piocb->iocb;
7030
7031                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
7032                         continue;
7033                 }
7034
7035                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
7036                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
7037                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
7038                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7039                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
7040                         continue;
7041
7042                 if (piocb->vport != vport)
7043                         continue;
7044
7045                 list_del_init(&piocb->list);
7046                 list_add_tail(&piocb->list, &abort_list);
7047         }
7048         if (phba->sli_rev == LPFC_SLI_REV4)
7049                 spin_unlock(&pring->ring_lock);
7050         spin_unlock_irq(&phba->hbalock);
7051
7052         /* Cancell all the IOCBs from the completions list */
7053         lpfc_sli_cancel_iocbs(phba, &abort_list,
7054                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
7055
7056         return;
7057 }
7058
7059 /**
7060  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
7061  * @phba: pointer to lpfc hba data structure.
7062  *
7063  * This routine is used to clean up all the outstanding ELS commands on a
7064  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
7065  * routine. After that, it walks the ELS transmit queue to remove all the
7066  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
7067  * the IOCBs with the completion callback function associated, the callback
7068  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7069  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
7070  * callback function associated, the IOCB will simply be released. Finally,
7071  * it walks the ELS transmit completion queue to issue an abort IOCB to any
7072  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
7073  * management plane IOCBs that are not part of the discovery state machine)
7074  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
7075  **/
7076 void
7077 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
7078 {
7079         struct lpfc_vport *vport;
7080         list_for_each_entry(vport, &phba->port_list, listentry)
7081                 lpfc_els_flush_cmd(vport);
7082
7083         return;
7084 }
7085
7086 /**
7087  * lpfc_send_els_failure_event - Posts an ELS command failure event
7088  * @phba: Pointer to hba context object.
7089  * @cmdiocbp: Pointer to command iocb which reported error.
7090  * @rspiocbp: Pointer to response iocb which reported error.
7091  *
7092  * This function sends an event when there is an ELS command
7093  * failure.
7094  **/
7095 void
7096 lpfc_send_els_failure_event(struct lpfc_hba *phba,
7097                         struct lpfc_iocbq *cmdiocbp,
7098                         struct lpfc_iocbq *rspiocbp)
7099 {
7100         struct lpfc_vport *vport = cmdiocbp->vport;
7101         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7102         struct lpfc_lsrjt_event lsrjt_event;
7103         struct lpfc_fabric_event_header fabric_event;
7104         struct ls_rjt stat;
7105         struct lpfc_nodelist *ndlp;
7106         uint32_t *pcmd;
7107
7108         ndlp = cmdiocbp->context1;
7109         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7110                 return;
7111
7112         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
7113                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
7114                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
7115                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
7116                         sizeof(struct lpfc_name));
7117                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
7118                         sizeof(struct lpfc_name));
7119                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7120                         cmdiocbp->context2)->virt);
7121                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
7122                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
7123                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
7124                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
7125                 fc_host_post_vendor_event(shost,
7126                         fc_get_event_number(),
7127                         sizeof(lsrjt_event),
7128                         (char *)&lsrjt_event,
7129                         LPFC_NL_VENDOR_ID);
7130                 return;
7131         }
7132         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
7133                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
7134                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
7135                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
7136                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
7137                 else
7138                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
7139                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
7140                         sizeof(struct lpfc_name));
7141                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
7142                         sizeof(struct lpfc_name));
7143                 fc_host_post_vendor_event(shost,
7144                         fc_get_event_number(),
7145                         sizeof(fabric_event),
7146                         (char *)&fabric_event,
7147                         LPFC_NL_VENDOR_ID);
7148                 return;
7149         }
7150
7151 }
7152
7153 /**
7154  * lpfc_send_els_event - Posts unsolicited els event
7155  * @vport: Pointer to vport object.
7156  * @ndlp: Pointer FC node object.
7157  * @cmd: ELS command code.
7158  *
7159  * This function posts an event when there is an incoming
7160  * unsolicited ELS command.
7161  **/
7162 static void
7163 lpfc_send_els_event(struct lpfc_vport *vport,
7164                     struct lpfc_nodelist *ndlp,
7165                     uint32_t *payload)
7166 {
7167         struct lpfc_els_event_header *els_data = NULL;
7168         struct lpfc_logo_event *logo_data = NULL;
7169         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7170
7171         if (*payload == ELS_CMD_LOGO) {
7172                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
7173                 if (!logo_data) {
7174                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7175                                 "0148 Failed to allocate memory "
7176                                 "for LOGO event\n");
7177                         return;
7178                 }
7179                 els_data = &logo_data->header;
7180         } else {
7181                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
7182                         GFP_KERNEL);
7183                 if (!els_data) {
7184                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7185                                 "0149 Failed to allocate memory "
7186                                 "for ELS event\n");
7187                         return;
7188                 }
7189         }
7190         els_data->event_type = FC_REG_ELS_EVENT;
7191         switch (*payload) {
7192         case ELS_CMD_PLOGI:
7193                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
7194                 break;
7195         case ELS_CMD_PRLO:
7196                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
7197                 break;
7198         case ELS_CMD_ADISC:
7199                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
7200                 break;
7201         case ELS_CMD_LOGO:
7202                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
7203                 /* Copy the WWPN in the LOGO payload */
7204                 memcpy(logo_data->logo_wwpn, &payload[2],
7205                         sizeof(struct lpfc_name));
7206                 break;
7207         default:
7208                 kfree(els_data);
7209                 return;
7210         }
7211         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
7212         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
7213         if (*payload == ELS_CMD_LOGO) {
7214                 fc_host_post_vendor_event(shost,
7215                         fc_get_event_number(),
7216                         sizeof(struct lpfc_logo_event),
7217                         (char *)logo_data,
7218                         LPFC_NL_VENDOR_ID);
7219                 kfree(logo_data);
7220         } else {
7221                 fc_host_post_vendor_event(shost,
7222                         fc_get_event_number(),
7223                         sizeof(struct lpfc_els_event_header),
7224                         (char *)els_data,
7225                         LPFC_NL_VENDOR_ID);
7226                 kfree(els_data);
7227         }
7228
7229         return;
7230 }
7231
7232
7233 /**
7234  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
7235  * @phba: pointer to lpfc hba data structure.
7236  * @pring: pointer to a SLI ring.
7237  * @vport: pointer to a host virtual N_Port data structure.
7238  * @elsiocb: pointer to lpfc els command iocb data structure.
7239  *
7240  * This routine is used for processing the IOCB associated with a unsolicited
7241  * event. It first determines whether there is an existing ndlp that matches
7242  * the DID from the unsolicited IOCB. If not, it will create a new one with
7243  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
7244  * IOCB is then used to invoke the proper routine and to set up proper state
7245  * of the discovery state machine.
7246  **/
7247 static void
7248 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7249                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
7250 {
7251         struct Scsi_Host  *shost;
7252         struct lpfc_nodelist *ndlp;
7253         struct ls_rjt stat;
7254         uint32_t *payload;
7255         uint32_t cmd, did, newnode;
7256         uint8_t rjt_exp, rjt_err = 0;
7257         IOCB_t *icmd = &elsiocb->iocb;
7258
7259         if (!vport || !(elsiocb->context2))
7260                 goto dropit;
7261
7262         newnode = 0;
7263         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
7264         cmd = *payload;
7265         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
7266                 lpfc_post_buffer(phba, pring, 1);
7267
7268         did = icmd->un.rcvels.remoteID;
7269         if (icmd->ulpStatus) {
7270                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7271                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
7272                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
7273                 goto dropit;
7274         }
7275
7276         /* Check to see if link went down during discovery */
7277         if (lpfc_els_chk_latt(vport))
7278                 goto dropit;
7279
7280         /* Ignore traffic received during vport shutdown. */
7281         if (vport->load_flag & FC_UNLOADING)
7282                 goto dropit;
7283
7284         /* If NPort discovery is delayed drop incoming ELS */
7285         if ((vport->fc_flag & FC_DISC_DELAYED) &&
7286                         (cmd != ELS_CMD_PLOGI))
7287                 goto dropit;
7288
7289         ndlp = lpfc_findnode_did(vport, did);
7290         if (!ndlp) {
7291                 /* Cannot find existing Fabric ndlp, so allocate a new one */
7292                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
7293                 if (!ndlp)
7294                         goto dropit;
7295
7296                 lpfc_nlp_init(vport, ndlp, did);
7297                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7298                 newnode = 1;
7299                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7300                         ndlp->nlp_type |= NLP_FABRIC;
7301         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
7302                 ndlp = lpfc_enable_node(vport, ndlp,
7303                                         NLP_STE_UNUSED_NODE);
7304                 if (!ndlp)
7305                         goto dropit;
7306                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7307                 newnode = 1;
7308                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7309                         ndlp->nlp_type |= NLP_FABRIC;
7310         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
7311                 /* This is similar to the new node path */
7312                 ndlp = lpfc_nlp_get(ndlp);
7313                 if (!ndlp)
7314                         goto dropit;
7315                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
7316                 newnode = 1;
7317         }
7318
7319         phba->fc_stat.elsRcvFrame++;
7320
7321         /*
7322          * Do not process any unsolicited ELS commands
7323          * if the ndlp is in DEV_LOSS
7324          */
7325         shost = lpfc_shost_from_vport(vport);
7326         spin_lock_irq(shost->host_lock);
7327         if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
7328                 spin_unlock_irq(shost->host_lock);
7329                 goto dropit;
7330         }
7331         spin_unlock_irq(shost->host_lock);
7332
7333         elsiocb->context1 = lpfc_nlp_get(ndlp);
7334         elsiocb->vport = vport;
7335
7336         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
7337                 cmd &= ELS_CMD_MASK;
7338         }
7339         /* ELS command <elsCmd> received from NPORT <did> */
7340         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7341                          "0112 ELS command x%x received from NPORT x%x "
7342                          "Data: x%x x%x x%x x%x\n",
7343                         cmd, did, vport->port_state, vport->fc_flag,
7344                         vport->fc_myDID, vport->fc_prevDID);
7345
7346         /* reject till our FLOGI completes */
7347         if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
7348                 (cmd != ELS_CMD_FLOGI)) {
7349                 rjt_err = LSRJT_UNABLE_TPC;
7350                 rjt_exp = LSEXP_NOTHING_MORE;
7351                 goto lsrjt;
7352         }
7353
7354         switch (cmd) {
7355         case ELS_CMD_PLOGI:
7356                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7357                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
7358                         did, vport->port_state, ndlp->nlp_flag);
7359
7360                 phba->fc_stat.elsRcvPLOGI++;
7361                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
7362                 if (phba->sli_rev == LPFC_SLI_REV4 &&
7363                     (phba->pport->fc_flag & FC_PT2PT)) {
7364                         vport->fc_prevDID = vport->fc_myDID;
7365                         /* Our DID needs to be updated before registering
7366                          * the vfi. This is done in lpfc_rcv_plogi but
7367                          * that is called after the reg_vfi.
7368                          */
7369                         vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
7370                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7371                                          "3312 Remote port assigned DID x%x "
7372                                          "%x\n", vport->fc_myDID,
7373                                          vport->fc_prevDID);
7374                 }
7375
7376                 lpfc_send_els_event(vport, ndlp, payload);
7377
7378                 /* If Nport discovery is delayed, reject PLOGIs */
7379                 if (vport->fc_flag & FC_DISC_DELAYED) {
7380                         rjt_err = LSRJT_UNABLE_TPC;
7381                         rjt_exp = LSEXP_NOTHING_MORE;
7382                         break;
7383                 }
7384                 if (vport->port_state < LPFC_DISC_AUTH) {
7385                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
7386                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
7387                                 rjt_err = LSRJT_UNABLE_TPC;
7388                                 rjt_exp = LSEXP_NOTHING_MORE;
7389                                 break;
7390                         }
7391                 }
7392
7393                 spin_lock_irq(shost->host_lock);
7394                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
7395                 spin_unlock_irq(shost->host_lock);
7396
7397                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
7398                                         NLP_EVT_RCV_PLOGI);
7399
7400                 break;
7401         case ELS_CMD_FLOGI:
7402                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7403                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
7404                         did, vport->port_state, ndlp->nlp_flag);
7405
7406                 phba->fc_stat.elsRcvFLOGI++;
7407                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
7408                 if (newnode)
7409                         lpfc_nlp_put(ndlp);
7410                 break;
7411         case ELS_CMD_LOGO:
7412                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7413                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
7414                         did, vport->port_state, ndlp->nlp_flag);
7415
7416                 phba->fc_stat.elsRcvLOGO++;
7417                 lpfc_send_els_event(vport, ndlp, payload);
7418                 if (vport->port_state < LPFC_DISC_AUTH) {
7419                         rjt_err = LSRJT_UNABLE_TPC;
7420                         rjt_exp = LSEXP_NOTHING_MORE;
7421                         break;
7422                 }
7423                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
7424                 break;
7425         case ELS_CMD_PRLO:
7426                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7427                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
7428                         did, vport->port_state, ndlp->nlp_flag);
7429
7430                 phba->fc_stat.elsRcvPRLO++;
7431                 lpfc_send_els_event(vport, ndlp, payload);
7432                 if (vport->port_state < LPFC_DISC_AUTH) {
7433                         rjt_err = LSRJT_UNABLE_TPC;
7434                         rjt_exp = LSEXP_NOTHING_MORE;
7435                         break;
7436                 }
7437                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
7438                 break;
7439         case ELS_CMD_LCB:
7440                 phba->fc_stat.elsRcvLCB++;
7441                 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
7442                 break;
7443         case ELS_CMD_RDP:
7444                 phba->fc_stat.elsRcvRDP++;
7445                 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
7446                 break;
7447         case ELS_CMD_RSCN:
7448                 phba->fc_stat.elsRcvRSCN++;
7449                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
7450                 if (newnode)
7451                         lpfc_nlp_put(ndlp);
7452                 break;
7453         case ELS_CMD_ADISC:
7454                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7455                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
7456                         did, vport->port_state, ndlp->nlp_flag);
7457
7458                 lpfc_send_els_event(vport, ndlp, payload);
7459                 phba->fc_stat.elsRcvADISC++;
7460                 if (vport->port_state < LPFC_DISC_AUTH) {
7461                         rjt_err = LSRJT_UNABLE_TPC;
7462                         rjt_exp = LSEXP_NOTHING_MORE;
7463                         break;
7464                 }
7465                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
7466                                         NLP_EVT_RCV_ADISC);
7467                 break;
7468         case ELS_CMD_PDISC:
7469                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7470                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
7471                         did, vport->port_state, ndlp->nlp_flag);
7472
7473                 phba->fc_stat.elsRcvPDISC++;
7474                 if (vport->port_state < LPFC_DISC_AUTH) {
7475                         rjt_err = LSRJT_UNABLE_TPC;
7476                         rjt_exp = LSEXP_NOTHING_MORE;
7477                         break;
7478                 }
7479                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
7480                                         NLP_EVT_RCV_PDISC);
7481                 break;
7482         case ELS_CMD_FARPR:
7483                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7484                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
7485                         did, vport->port_state, ndlp->nlp_flag);
7486
7487                 phba->fc_stat.elsRcvFARPR++;
7488                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
7489                 break;
7490         case ELS_CMD_FARP:
7491                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7492                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
7493                         did, vport->port_state, ndlp->nlp_flag);
7494
7495                 phba->fc_stat.elsRcvFARP++;
7496                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
7497                 break;
7498         case ELS_CMD_FAN:
7499                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7500                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
7501                         did, vport->port_state, ndlp->nlp_flag);
7502
7503                 phba->fc_stat.elsRcvFAN++;
7504                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
7505                 break;
7506         case ELS_CMD_PRLI:
7507                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7508                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
7509                         did, vport->port_state, ndlp->nlp_flag);
7510
7511                 phba->fc_stat.elsRcvPRLI++;
7512                 if (vport->port_state < LPFC_DISC_AUTH) {
7513                         rjt_err = LSRJT_UNABLE_TPC;
7514                         rjt_exp = LSEXP_NOTHING_MORE;
7515                         break;
7516                 }
7517                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
7518                 break;
7519         case ELS_CMD_LIRR:
7520                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7521                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
7522                         did, vport->port_state, ndlp->nlp_flag);
7523
7524                 phba->fc_stat.elsRcvLIRR++;
7525                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
7526                 if (newnode)
7527                         lpfc_nlp_put(ndlp);
7528                 break;
7529         case ELS_CMD_RLS:
7530                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7531                         "RCV RLS:         did:x%x/ste:x%x flg:x%x",
7532                         did, vport->port_state, ndlp->nlp_flag);
7533
7534                 phba->fc_stat.elsRcvRLS++;
7535                 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
7536                 if (newnode)
7537                         lpfc_nlp_put(ndlp);
7538                 break;
7539         case ELS_CMD_RPS:
7540                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7541                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
7542                         did, vport->port_state, ndlp->nlp_flag);
7543
7544                 phba->fc_stat.elsRcvRPS++;
7545                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
7546                 if (newnode)
7547                         lpfc_nlp_put(ndlp);
7548                 break;
7549         case ELS_CMD_RPL:
7550                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7551                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
7552                         did, vport->port_state, ndlp->nlp_flag);
7553
7554                 phba->fc_stat.elsRcvRPL++;
7555                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
7556                 if (newnode)
7557                         lpfc_nlp_put(ndlp);
7558                 break;
7559         case ELS_CMD_RNID:
7560                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7561                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
7562                         did, vport->port_state, ndlp->nlp_flag);
7563
7564                 phba->fc_stat.elsRcvRNID++;
7565                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
7566                 if (newnode)
7567                         lpfc_nlp_put(ndlp);
7568                 break;
7569         case ELS_CMD_RTV:
7570                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7571                         "RCV RTV:        did:x%x/ste:x%x flg:x%x",
7572                         did, vport->port_state, ndlp->nlp_flag);
7573                 phba->fc_stat.elsRcvRTV++;
7574                 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
7575                 if (newnode)
7576                         lpfc_nlp_put(ndlp);
7577                 break;
7578         case ELS_CMD_RRQ:
7579                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7580                         "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
7581                         did, vport->port_state, ndlp->nlp_flag);
7582
7583                 phba->fc_stat.elsRcvRRQ++;
7584                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
7585                 if (newnode)
7586                         lpfc_nlp_put(ndlp);
7587                 break;
7588         case ELS_CMD_ECHO:
7589                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7590                         "RCV ECHO:        did:x%x/ste:x%x flg:x%x",
7591                         did, vport->port_state, ndlp->nlp_flag);
7592
7593                 phba->fc_stat.elsRcvECHO++;
7594                 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
7595                 if (newnode)
7596                         lpfc_nlp_put(ndlp);
7597                 break;
7598         case ELS_CMD_REC:
7599                         /* receive this due to exchange closed */
7600                         rjt_err = LSRJT_UNABLE_TPC;
7601                         rjt_exp = LSEXP_INVALID_OX_RX;
7602                 break;
7603         default:
7604                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7605                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
7606                         cmd, did, vport->port_state);
7607
7608                 /* Unsupported ELS command, reject */
7609                 rjt_err = LSRJT_CMD_UNSUPPORTED;
7610                 rjt_exp = LSEXP_NOTHING_MORE;
7611
7612                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
7613                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7614                                  "0115 Unknown ELS command x%x "
7615                                  "received from NPORT x%x\n", cmd, did);
7616                 if (newnode)
7617                         lpfc_nlp_put(ndlp);
7618                 break;
7619         }
7620
7621 lsrjt:
7622         /* check if need to LS_RJT received ELS cmd */
7623         if (rjt_err) {
7624                 memset(&stat, 0, sizeof(stat));
7625                 stat.un.b.lsRjtRsnCode = rjt_err;
7626                 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
7627                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
7628                         NULL);
7629         }
7630
7631         lpfc_nlp_put(elsiocb->context1);
7632         elsiocb->context1 = NULL;
7633         return;
7634
7635 dropit:
7636         if (vport && !(vport->load_flag & FC_UNLOADING))
7637                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7638                         "0111 Dropping received ELS cmd "
7639                         "Data: x%x x%x x%x\n",
7640                         icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
7641         phba->fc_stat.elsRcvDrop++;
7642 }
7643
7644 /**
7645  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
7646  * @phba: pointer to lpfc hba data structure.
7647  * @pring: pointer to a SLI ring.
7648  * @elsiocb: pointer to lpfc els iocb data structure.
7649  *
7650  * This routine is used to process an unsolicited event received from a SLI
7651  * (Service Level Interface) ring. The actual processing of the data buffer
7652  * associated with the unsolicited event is done by invoking the routine
7653  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
7654  * SLI ring on which the unsolicited event was received.
7655  **/
7656 void
7657 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7658                      struct lpfc_iocbq *elsiocb)
7659 {
7660         struct lpfc_vport *vport = phba->pport;
7661         IOCB_t *icmd = &elsiocb->iocb;
7662         dma_addr_t paddr;
7663         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
7664         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
7665
7666         elsiocb->context1 = NULL;
7667         elsiocb->context2 = NULL;
7668         elsiocb->context3 = NULL;
7669
7670         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
7671                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
7672         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
7673                    (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
7674                    IOERR_RCV_BUFFER_WAITING) {
7675                 phba->fc_stat.NoRcvBuf++;
7676                 /* Not enough posted buffers; Try posting more buffers */
7677                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
7678                         lpfc_post_buffer(phba, pring, 0);
7679                 return;
7680         }
7681
7682         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
7683             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
7684              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
7685                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
7686                         vport = phba->pport;
7687                 else
7688                         vport = lpfc_find_vport_by_vpid(phba,
7689                                                 icmd->unsli3.rcvsli3.vpi);
7690         }
7691
7692         /* If there are no BDEs associated
7693          * with this IOCB, there is nothing to do.
7694          */
7695         if (icmd->ulpBdeCount == 0)
7696                 return;
7697
7698         /* type of ELS cmd is first 32bit word
7699          * in packet
7700          */
7701         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
7702                 elsiocb->context2 = bdeBuf1;
7703         } else {
7704                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
7705                                  icmd->un.cont64[0].addrLow);
7706                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
7707                                                              paddr);
7708         }
7709
7710         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
7711         /*
7712          * The different unsolicited event handlers would tell us
7713          * if they are done with "mp" by setting context2 to NULL.
7714          */
7715         if (elsiocb->context2) {
7716                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
7717                 elsiocb->context2 = NULL;
7718         }
7719
7720         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
7721         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
7722             icmd->ulpBdeCount == 2) {
7723                 elsiocb->context2 = bdeBuf2;
7724                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
7725                 /* free mp if we are done with it */
7726                 if (elsiocb->context2) {
7727                         lpfc_in_buf_free(phba, elsiocb->context2);
7728                         elsiocb->context2 = NULL;
7729                 }
7730         }
7731 }
7732
7733 /**
7734  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
7735  * @phba: pointer to lpfc hba data structure.
7736  * @vport: pointer to a virtual N_Port data structure.
7737  *
7738  * This routine issues a Port Login (PLOGI) to the Name Server with
7739  * State Change Request (SCR) for a @vport. This routine will create an
7740  * ndlp for the Name Server associated to the @vport if such node does
7741  * not already exist. The PLOGI to Name Server is issued by invoking the
7742  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
7743  * (FDMI) is configured to the @vport, a FDMI node will be created and
7744  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
7745  **/
7746 void
7747 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
7748 {
7749         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
7750         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7751
7752         /*
7753          * If lpfc_delay_discovery parameter is set and the clean address
7754          * bit is cleared and fc fabric parameters chenged, delay FC NPort
7755          * discovery.
7756          */
7757         spin_lock_irq(shost->host_lock);
7758         if (vport->fc_flag & FC_DISC_DELAYED) {
7759                 spin_unlock_irq(shost->host_lock);
7760                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
7761                                 "3334 Delay fc port discovery for %d seconds\n",
7762                                 phba->fc_ratov);
7763                 mod_timer(&vport->delayed_disc_tmo,
7764                         jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
7765                 return;
7766         }
7767         spin_unlock_irq(shost->host_lock);
7768
7769         ndlp = lpfc_findnode_did(vport, NameServer_DID);
7770         if (!ndlp) {
7771                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
7772                 if (!ndlp) {
7773                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7774                                 lpfc_disc_start(vport);
7775                                 return;
7776                         }
7777                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7778                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7779                                          "0251 NameServer login: no memory\n");
7780                         return;
7781                 }
7782                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
7783         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
7784                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
7785                 if (!ndlp) {
7786                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7787                                 lpfc_disc_start(vport);
7788                                 return;
7789                         }
7790                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7791                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7792                                         "0348 NameServer login: node freed\n");
7793                         return;
7794                 }
7795         }
7796         ndlp->nlp_type |= NLP_FABRIC;
7797
7798         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7799
7800         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
7801                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7802                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7803                                  "0252 Cannot issue NameServer login\n");
7804                 return;
7805         }
7806
7807         if (vport->cfg_fdmi_on & LPFC_FDMI_SUPPORT) {
7808                 /* If this is the first time, allocate an ndlp and initialize
7809                  * it. Otherwise, make sure the node is enabled and then do the
7810                  * login.
7811                  */
7812                 ndlp_fdmi = lpfc_findnode_did(vport, FDMI_DID);
7813                 if (!ndlp_fdmi) {
7814                         ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
7815                                                   GFP_KERNEL);
7816                         if (ndlp_fdmi) {
7817                                 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
7818                                 ndlp_fdmi->nlp_type |= NLP_FABRIC;
7819                         } else
7820                                 return;
7821                 }
7822                 if (!NLP_CHK_NODE_ACT(ndlp_fdmi))
7823                         ndlp_fdmi = lpfc_enable_node(vport,
7824                                                      ndlp_fdmi,
7825                                                      NLP_STE_NPR_NODE);
7826
7827                 if (ndlp_fdmi) {
7828                         lpfc_nlp_set_state(vport, ndlp_fdmi,
7829                                            NLP_STE_PLOGI_ISSUE);
7830                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID, 0);
7831                 }
7832         }
7833 }
7834
7835 /**
7836  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
7837  * @phba: pointer to lpfc hba data structure.
7838  * @pmb: pointer to the driver internal queue element for mailbox command.
7839  *
7840  * This routine is the completion callback function to register new vport
7841  * mailbox command. If the new vport mailbox command completes successfully,
7842  * the fabric registration login shall be performed on physical port (the
7843  * new vport created is actually a physical port, with VPI 0) or the port
7844  * login to Name Server for State Change Request (SCR) will be performed
7845  * on virtual port (real virtual port, with VPI greater than 0).
7846  **/
7847 static void
7848 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7849 {
7850         struct lpfc_vport *vport = pmb->vport;
7851         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
7852         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
7853         MAILBOX_t *mb = &pmb->u.mb;
7854         int rc;
7855
7856         spin_lock_irq(shost->host_lock);
7857         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
7858         spin_unlock_irq(shost->host_lock);
7859
7860         if (mb->mbxStatus) {
7861                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7862                                 "0915 Register VPI failed : Status: x%x"
7863                                 " upd bit: x%x \n", mb->mbxStatus,
7864                                  mb->un.varRegVpi.upd);
7865                 if (phba->sli_rev == LPFC_SLI_REV4 &&
7866                         mb->un.varRegVpi.upd)
7867                         goto mbox_err_exit ;
7868
7869                 switch (mb->mbxStatus) {
7870                 case 0x11:      /* unsupported feature */
7871                 case 0x9603:    /* max_vpi exceeded */
7872                 case 0x9602:    /* Link event since CLEAR_LA */
7873                         /* giving up on vport registration */
7874                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7875                         spin_lock_irq(shost->host_lock);
7876                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
7877                         spin_unlock_irq(shost->host_lock);
7878                         lpfc_can_disctmo(vport);
7879                         break;
7880                 /* If reg_vpi fail with invalid VPI status, re-init VPI */
7881                 case 0x20:
7882                         spin_lock_irq(shost->host_lock);
7883                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7884                         spin_unlock_irq(shost->host_lock);
7885                         lpfc_init_vpi(phba, pmb, vport->vpi);
7886                         pmb->vport = vport;
7887                         pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
7888                         rc = lpfc_sli_issue_mbox(phba, pmb,
7889                                 MBX_NOWAIT);
7890                         if (rc == MBX_NOT_FINISHED) {
7891                                 lpfc_printf_vlog(vport,
7892                                         KERN_ERR, LOG_MBOX,
7893                                         "2732 Failed to issue INIT_VPI"
7894                                         " mailbox command\n");
7895                         } else {
7896                                 lpfc_nlp_put(ndlp);
7897                                 return;
7898                         }
7899
7900                 default:
7901                         /* Try to recover from this error */
7902                         if (phba->sli_rev == LPFC_SLI_REV4)
7903                                 lpfc_sli4_unreg_all_rpis(vport);
7904                         lpfc_mbx_unreg_vpi(vport);
7905                         spin_lock_irq(shost->host_lock);
7906                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7907                         spin_unlock_irq(shost->host_lock);
7908                         if (vport->port_type == LPFC_PHYSICAL_PORT
7909                                 && !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
7910                                 lpfc_issue_init_vfi(vport);
7911                         else
7912                                 lpfc_initial_fdisc(vport);
7913                         break;
7914                 }
7915         } else {
7916                 spin_lock_irq(shost->host_lock);
7917                 vport->vpi_state |= LPFC_VPI_REGISTERED;
7918                 spin_unlock_irq(shost->host_lock);
7919                 if (vport == phba->pport) {
7920                         if (phba->sli_rev < LPFC_SLI_REV4)
7921                                 lpfc_issue_fabric_reglogin(vport);
7922                         else {
7923                                 /*
7924                                  * If the physical port is instantiated using
7925                                  * FDISC, do not start vport discovery.
7926                                  */
7927                                 if (vport->port_state != LPFC_FDISC)
7928                                         lpfc_start_fdiscs(phba);
7929                                 lpfc_do_scr_ns_plogi(phba, vport);
7930                         }
7931                 } else
7932                         lpfc_do_scr_ns_plogi(phba, vport);
7933         }
7934 mbox_err_exit:
7935         /* Now, we decrement the ndlp reference count held for this
7936          * callback function
7937          */
7938         lpfc_nlp_put(ndlp);
7939
7940         mempool_free(pmb, phba->mbox_mem_pool);
7941         return;
7942 }
7943
7944 /**
7945  * lpfc_register_new_vport - Register a new vport with a HBA
7946  * @phba: pointer to lpfc hba data structure.
7947  * @vport: pointer to a host virtual N_Port data structure.
7948  * @ndlp: pointer to a node-list data structure.
7949  *
7950  * This routine registers the @vport as a new virtual port with a HBA.
7951  * It is done through a registering vpi mailbox command.
7952  **/
7953 void
7954 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
7955                         struct lpfc_nodelist *ndlp)
7956 {
7957         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7958         LPFC_MBOXQ_t *mbox;
7959
7960         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7961         if (mbox) {
7962                 lpfc_reg_vpi(vport, mbox);
7963                 mbox->vport = vport;
7964                 mbox->context2 = lpfc_nlp_get(ndlp);
7965                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
7966                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7967                     == MBX_NOT_FINISHED) {
7968                         /* mailbox command not success, decrement ndlp
7969                          * reference count for this command
7970                          */
7971                         lpfc_nlp_put(ndlp);
7972                         mempool_free(mbox, phba->mbox_mem_pool);
7973
7974                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7975                                 "0253 Register VPI: Can't send mbox\n");
7976                         goto mbox_err_exit;
7977                 }
7978         } else {
7979                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7980                                  "0254 Register VPI: no memory\n");
7981                 goto mbox_err_exit;
7982         }
7983         return;
7984
7985 mbox_err_exit:
7986         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7987         spin_lock_irq(shost->host_lock);
7988         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
7989         spin_unlock_irq(shost->host_lock);
7990         return;
7991 }
7992
7993 /**
7994  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
7995  * @phba: pointer to lpfc hba data structure.
7996  *
7997  * This routine cancels the retry delay timers to all the vports.
7998  **/
7999 void
8000 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
8001 {
8002         struct lpfc_vport **vports;
8003         struct lpfc_nodelist *ndlp;
8004         uint32_t link_state;
8005         int i;
8006
8007         /* Treat this failure as linkdown for all vports */
8008         link_state = phba->link_state;
8009         lpfc_linkdown(phba);
8010         phba->link_state = link_state;
8011
8012         vports = lpfc_create_vport_work_array(phba);
8013
8014         if (vports) {
8015                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8016                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
8017                         if (ndlp)
8018                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
8019                         lpfc_els_flush_cmd(vports[i]);
8020                 }
8021                 lpfc_destroy_vport_work_array(phba, vports);
8022         }
8023 }
8024
8025 /**
8026  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
8027  * @phba: pointer to lpfc hba data structure.
8028  *
8029  * This routine abort all pending discovery commands and
8030  * start a timer to retry FLOGI for the physical port
8031  * discovery.
8032  **/
8033 void
8034 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
8035 {
8036         struct lpfc_nodelist *ndlp;
8037         struct Scsi_Host  *shost;
8038
8039         /* Cancel the all vports retry delay retry timers */
8040         lpfc_cancel_all_vport_retry_delay_timer(phba);
8041
8042         /* If fabric require FLOGI, then re-instantiate physical login */
8043         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
8044         if (!ndlp)
8045                 return;
8046
8047         shost = lpfc_shost_from_vport(phba->pport);
8048         mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
8049         spin_lock_irq(shost->host_lock);
8050         ndlp->nlp_flag |= NLP_DELAY_TMO;
8051         spin_unlock_irq(shost->host_lock);
8052         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
8053         phba->pport->port_state = LPFC_FLOGI;
8054         return;
8055 }
8056
8057 /**
8058  * lpfc_fabric_login_reqd - Check if FLOGI required.
8059  * @phba: pointer to lpfc hba data structure.
8060  * @cmdiocb: pointer to FDISC command iocb.
8061  * @rspiocb: pointer to FDISC response iocb.
8062  *
8063  * This routine checks if a FLOGI is reguired for FDISC
8064  * to succeed.
8065  **/
8066 static int
8067 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
8068                 struct lpfc_iocbq *cmdiocb,
8069                 struct lpfc_iocbq *rspiocb)
8070 {
8071
8072         if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
8073                 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
8074                 return 0;
8075         else
8076                 return 1;
8077 }
8078
8079 /**
8080  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
8081  * @phba: pointer to lpfc hba data structure.
8082  * @cmdiocb: pointer to lpfc command iocb data structure.
8083  * @rspiocb: pointer to lpfc response iocb data structure.
8084  *
8085  * This routine is the completion callback function to a Fabric Discover
8086  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
8087  * single threaded, each FDISC completion callback function will reset
8088  * the discovery timer for all vports such that the timers will not get
8089  * unnecessary timeout. The function checks the FDISC IOCB status. If error
8090  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
8091  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
8092  * assigned to the vport has been changed with the completion of the FDISC
8093  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
8094  * are unregistered from the HBA, and then the lpfc_register_new_vport()
8095  * routine is invoked to register new vport with the HBA. Otherwise, the
8096  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
8097  * Server for State Change Request (SCR).
8098  **/
8099 static void
8100 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8101                     struct lpfc_iocbq *rspiocb)
8102 {
8103         struct lpfc_vport *vport = cmdiocb->vport;
8104         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
8105         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
8106         struct lpfc_nodelist *np;
8107         struct lpfc_nodelist *next_np;
8108         IOCB_t *irsp = &rspiocb->iocb;
8109         struct lpfc_iocbq *piocb;
8110         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
8111         struct serv_parm *sp;
8112         uint8_t fabric_param_changed;
8113
8114         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8115                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
8116                          irsp->ulpStatus, irsp->un.ulpWord[4],
8117                          vport->fc_prevDID);
8118         /* Since all FDISCs are being single threaded, we
8119          * must reset the discovery timer for ALL vports
8120          * waiting to send FDISC when one completes.
8121          */
8122         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
8123                 lpfc_set_disctmo(piocb->vport);
8124         }
8125
8126         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8127                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
8128                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
8129
8130         if (irsp->ulpStatus) {
8131
8132                 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
8133                         lpfc_retry_pport_discovery(phba);
8134                         goto out;
8135                 }
8136
8137                 /* Check for retry */
8138                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
8139                         goto out;
8140                 /* FDISC failed */
8141                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8142                                  "0126 FDISC failed. (x%x/x%x)\n",
8143                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
8144                 goto fdisc_failed;
8145         }
8146         spin_lock_irq(shost->host_lock);
8147         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
8148         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
8149         vport->fc_flag |= FC_FABRIC;
8150         if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
8151                 vport->fc_flag |=  FC_PUBLIC_LOOP;
8152         spin_unlock_irq(shost->host_lock);
8153
8154         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
8155         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
8156         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
8157         if (!prsp)
8158                 goto out;
8159         sp = prsp->virt + sizeof(uint32_t);
8160         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
8161         memcpy(&vport->fabric_portname, &sp->portName,
8162                 sizeof(struct lpfc_name));
8163         memcpy(&vport->fabric_nodename, &sp->nodeName,
8164                 sizeof(struct lpfc_name));
8165         if (fabric_param_changed &&
8166                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
8167                 /* If our NportID changed, we need to ensure all
8168                  * remaining NPORTs get unreg_login'ed so we can
8169                  * issue unreg_vpi.
8170                  */
8171                 list_for_each_entry_safe(np, next_np,
8172                         &vport->fc_nodes, nlp_listp) {
8173                         if (!NLP_CHK_NODE_ACT(ndlp) ||
8174                             (np->nlp_state != NLP_STE_NPR_NODE) ||
8175                             !(np->nlp_flag & NLP_NPR_ADISC))
8176                                 continue;
8177                         spin_lock_irq(shost->host_lock);
8178                         np->nlp_flag &= ~NLP_NPR_ADISC;
8179                         spin_unlock_irq(shost->host_lock);
8180                         lpfc_unreg_rpi(vport, np);
8181                 }
8182                 lpfc_cleanup_pending_mbox(vport);
8183
8184                 if (phba->sli_rev == LPFC_SLI_REV4)
8185                         lpfc_sli4_unreg_all_rpis(vport);
8186
8187                 lpfc_mbx_unreg_vpi(vport);
8188                 spin_lock_irq(shost->host_lock);
8189                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
8190                 if (phba->sli_rev == LPFC_SLI_REV4)
8191                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
8192                 else
8193                         vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
8194                 spin_unlock_irq(shost->host_lock);
8195         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
8196                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
8197                 /*
8198                  * Driver needs to re-reg VPI in order for f/w
8199                  * to update the MAC address.
8200                  */
8201                 lpfc_register_new_vport(phba, vport, ndlp);
8202                 goto out;
8203         }
8204
8205         if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
8206                 lpfc_issue_init_vpi(vport);
8207         else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
8208                 lpfc_register_new_vport(phba, vport, ndlp);
8209         else
8210                 lpfc_do_scr_ns_plogi(phba, vport);
8211         goto out;
8212 fdisc_failed:
8213         if (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS)
8214                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8215         /* Cancel discovery timer */
8216         lpfc_can_disctmo(vport);
8217         lpfc_nlp_put(ndlp);
8218 out:
8219         lpfc_els_free_iocb(phba, cmdiocb);
8220 }
8221
8222 /**
8223  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
8224  * @vport: pointer to a virtual N_Port data structure.
8225  * @ndlp: pointer to a node-list data structure.
8226  * @retry: number of retries to the command IOCB.
8227  *
8228  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
8229  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
8230  * routine to issue the IOCB, which makes sure only one outstanding fabric
8231  * IOCB will be sent off HBA at any given time.
8232  *
8233  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
8234  * will be incremented by 1 for holding the ndlp and the reference to ndlp
8235  * will be stored into the context1 field of the IOCB for the completion
8236  * callback function to the FDISC ELS command.
8237  *
8238  * Return code
8239  *   0 - Successfully issued fdisc iocb command
8240  *   1 - Failed to issue fdisc iocb command
8241  **/
8242 static int
8243 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
8244                      uint8_t retry)
8245 {
8246         struct lpfc_hba *phba = vport->phba;
8247         IOCB_t *icmd;
8248         struct lpfc_iocbq *elsiocb;
8249         struct serv_parm *sp;
8250         uint8_t *pcmd;
8251         uint16_t cmdsize;
8252         int did = ndlp->nlp_DID;
8253         int rc;
8254
8255         vport->port_state = LPFC_FDISC;
8256         vport->fc_myDID = 0;
8257         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
8258         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
8259                                      ELS_CMD_FDISC);
8260         if (!elsiocb) {
8261                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8262                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8263                                  "0255 Issue FDISC: no IOCB\n");
8264                 return 1;
8265         }
8266
8267         icmd = &elsiocb->iocb;
8268         icmd->un.elsreq64.myID = 0;
8269         icmd->un.elsreq64.fl = 1;
8270
8271         /*
8272          * SLI3 ports require a different context type value than SLI4.
8273          * Catch SLI3 ports here and override the prep.
8274          */
8275         if (phba->sli_rev == LPFC_SLI_REV3) {
8276                 icmd->ulpCt_h = 1;
8277                 icmd->ulpCt_l = 0;
8278         }
8279
8280         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8281         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
8282         pcmd += sizeof(uint32_t); /* CSP Word 1 */
8283         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
8284         sp = (struct serv_parm *) pcmd;
8285         /* Setup CSPs accordingly for Fabric */
8286         sp->cmn.e_d_tov = 0;
8287         sp->cmn.w2.r_a_tov = 0;
8288         sp->cmn.virtual_fabric_support = 0;
8289         sp->cls1.classValid = 0;
8290         sp->cls2.seqDelivery = 1;
8291         sp->cls3.seqDelivery = 1;
8292
8293         pcmd += sizeof(uint32_t); /* CSP Word 2 */
8294         pcmd += sizeof(uint32_t); /* CSP Word 3 */
8295         pcmd += sizeof(uint32_t); /* CSP Word 4 */
8296         pcmd += sizeof(uint32_t); /* Port Name */
8297         memcpy(pcmd, &vport->fc_portname, 8);
8298         pcmd += sizeof(uint32_t); /* Node Name */
8299         pcmd += sizeof(uint32_t); /* Node Name */
8300         memcpy(pcmd, &vport->fc_nodename, 8);
8301
8302         lpfc_set_disctmo(vport);
8303
8304         phba->fc_stat.elsXmitFDISC++;
8305         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
8306
8307         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8308                 "Issue FDISC:     did:x%x",
8309                 did, 0, 0);
8310
8311         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
8312         if (rc == IOCB_ERROR) {
8313                 lpfc_els_free_iocb(phba, elsiocb);
8314                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8315                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8316                                  "0256 Issue FDISC: Cannot send IOCB\n");
8317                 return 1;
8318         }
8319         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
8320         return 0;
8321 }
8322
8323 /**
8324  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
8325  * @phba: pointer to lpfc hba data structure.
8326  * @cmdiocb: pointer to lpfc command iocb data structure.
8327  * @rspiocb: pointer to lpfc response iocb data structure.
8328  *
8329  * This routine is the completion callback function to the issuing of a LOGO
8330  * ELS command off a vport. It frees the command IOCB and then decrement the
8331  * reference count held on ndlp for this completion function, indicating that
8332  * the reference to the ndlp is no long needed. Note that the
8333  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
8334  * callback function and an additional explicit ndlp reference decrementation
8335  * will trigger the actual release of the ndlp.
8336  **/
8337 static void
8338 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8339                         struct lpfc_iocbq *rspiocb)
8340 {
8341         struct lpfc_vport *vport = cmdiocb->vport;
8342         IOCB_t *irsp;
8343         struct lpfc_nodelist *ndlp;
8344         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8345
8346         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
8347         irsp = &rspiocb->iocb;
8348         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8349                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
8350                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
8351
8352         lpfc_els_free_iocb(phba, cmdiocb);
8353         vport->unreg_vpi_cmpl = VPORT_ERROR;
8354
8355         /* Trigger the release of the ndlp after logo */
8356         lpfc_nlp_put(ndlp);
8357
8358         /* NPIV LOGO completes to NPort <nlp_DID> */
8359         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8360                          "2928 NPIV LOGO completes to NPort x%x "
8361                          "Data: x%x x%x x%x x%x\n",
8362                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
8363                          irsp->ulpTimeout, vport->num_disc_nodes);
8364
8365         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
8366                 spin_lock_irq(shost->host_lock);
8367                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
8368                 vport->fc_flag &= ~FC_FABRIC;
8369                 spin_unlock_irq(shost->host_lock);
8370                 lpfc_can_disctmo(vport);
8371         }
8372 }
8373
8374 /**
8375  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
8376  * @vport: pointer to a virtual N_Port data structure.
8377  * @ndlp: pointer to a node-list data structure.
8378  *
8379  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
8380  *
8381  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
8382  * will be incremented by 1 for holding the ndlp and the reference to ndlp
8383  * will be stored into the context1 field of the IOCB for the completion
8384  * callback function to the LOGO ELS command.
8385  *
8386  * Return codes
8387  *   0 - Successfully issued logo off the @vport
8388  *   1 - Failed to issue logo off the @vport
8389  **/
8390 int
8391 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
8392 {
8393         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8394         struct lpfc_hba  *phba = vport->phba;
8395         struct lpfc_iocbq *elsiocb;
8396         uint8_t *pcmd;
8397         uint16_t cmdsize;
8398
8399         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
8400         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
8401                                      ELS_CMD_LOGO);
8402         if (!elsiocb)
8403                 return 1;
8404
8405         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8406         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
8407         pcmd += sizeof(uint32_t);
8408
8409         /* Fill in LOGO payload */
8410         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
8411         pcmd += sizeof(uint32_t);
8412         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
8413
8414         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8415                 "Issue LOGO npiv  did:x%x flg:x%x",
8416                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
8417
8418         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
8419         spin_lock_irq(shost->host_lock);
8420         ndlp->nlp_flag |= NLP_LOGO_SND;
8421         spin_unlock_irq(shost->host_lock);
8422         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
8423             IOCB_ERROR) {
8424                 spin_lock_irq(shost->host_lock);
8425                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
8426                 spin_unlock_irq(shost->host_lock);
8427                 lpfc_els_free_iocb(phba, elsiocb);
8428                 return 1;
8429         }
8430         return 0;
8431 }
8432
8433 /**
8434  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
8435  * @ptr: holder for the timer function associated data.
8436  *
8437  * This routine is invoked by the fabric iocb block timer after
8438  * timeout. It posts the fabric iocb block timeout event by setting the
8439  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
8440  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
8441  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
8442  * posted event WORKER_FABRIC_BLOCK_TMO.
8443  **/
8444 void
8445 lpfc_fabric_block_timeout(unsigned long ptr)
8446 {
8447         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
8448         unsigned long iflags;
8449         uint32_t tmo_posted;
8450
8451         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8452         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
8453         if (!tmo_posted)
8454                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
8455         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8456
8457         if (!tmo_posted)
8458                 lpfc_worker_wake_up(phba);
8459         return;
8460 }
8461
8462 /**
8463  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
8464  * @phba: pointer to lpfc hba data structure.
8465  *
8466  * This routine issues one fabric iocb from the driver internal list to
8467  * the HBA. It first checks whether it's ready to issue one fabric iocb to
8468  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
8469  * remove one pending fabric iocb from the driver internal list and invokes
8470  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
8471  **/
8472 static void
8473 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
8474 {
8475         struct lpfc_iocbq *iocb;
8476         unsigned long iflags;
8477         int ret;
8478         IOCB_t *cmd;
8479
8480 repeat:
8481         iocb = NULL;
8482         spin_lock_irqsave(&phba->hbalock, iflags);
8483         /* Post any pending iocb to the SLI layer */
8484         if (atomic_read(&phba->fabric_iocb_count) == 0) {
8485                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
8486                                  list);
8487                 if (iocb)
8488                         /* Increment fabric iocb count to hold the position */
8489                         atomic_inc(&phba->fabric_iocb_count);
8490         }
8491         spin_unlock_irqrestore(&phba->hbalock, iflags);
8492         if (iocb) {
8493                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
8494                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
8495                 iocb->iocb_flag |= LPFC_IO_FABRIC;
8496
8497                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
8498                         "Fabric sched1:   ste:x%x",
8499                         iocb->vport->port_state, 0, 0);
8500
8501                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
8502
8503                 if (ret == IOCB_ERROR) {
8504                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
8505                         iocb->fabric_iocb_cmpl = NULL;
8506                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
8507                         cmd = &iocb->iocb;
8508                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
8509                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
8510                         iocb->iocb_cmpl(phba, iocb, iocb);
8511
8512                         atomic_dec(&phba->fabric_iocb_count);
8513                         goto repeat;
8514                 }
8515         }
8516
8517         return;
8518 }
8519
8520 /**
8521  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
8522  * @phba: pointer to lpfc hba data structure.
8523  *
8524  * This routine unblocks the  issuing fabric iocb command. The function
8525  * will clear the fabric iocb block bit and then invoke the routine
8526  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
8527  * from the driver internal fabric iocb list.
8528  **/
8529 void
8530 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
8531 {
8532         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8533
8534         lpfc_resume_fabric_iocbs(phba);
8535         return;
8536 }
8537
8538 /**
8539  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
8540  * @phba: pointer to lpfc hba data structure.
8541  *
8542  * This routine blocks the issuing fabric iocb for a specified amount of
8543  * time (currently 100 ms). This is done by set the fabric iocb block bit
8544  * and set up a timeout timer for 100ms. When the block bit is set, no more
8545  * fabric iocb will be issued out of the HBA.
8546  **/
8547 static void
8548 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
8549 {
8550         int blocked;
8551
8552         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8553         /* Start a timer to unblock fabric iocbs after 100ms */
8554         if (!blocked)
8555                 mod_timer(&phba->fabric_block_timer,
8556                           jiffies + msecs_to_jiffies(100));
8557
8558         return;
8559 }
8560
8561 /**
8562  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
8563  * @phba: pointer to lpfc hba data structure.
8564  * @cmdiocb: pointer to lpfc command iocb data structure.
8565  * @rspiocb: pointer to lpfc response iocb data structure.
8566  *
8567  * This routine is the callback function that is put to the fabric iocb's
8568  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
8569  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
8570  * function first restores and invokes the original iocb's callback function
8571  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
8572  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
8573  **/
8574 static void
8575 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8576         struct lpfc_iocbq *rspiocb)
8577 {
8578         struct ls_rjt stat;
8579
8580         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
8581                 BUG();
8582
8583         switch (rspiocb->iocb.ulpStatus) {
8584                 case IOSTAT_NPORT_RJT:
8585                 case IOSTAT_FABRIC_RJT:
8586                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
8587                                 lpfc_block_fabric_iocbs(phba);
8588                         }
8589                         break;
8590
8591                 case IOSTAT_NPORT_BSY:
8592                 case IOSTAT_FABRIC_BSY:
8593                         lpfc_block_fabric_iocbs(phba);
8594                         break;
8595
8596                 case IOSTAT_LS_RJT:
8597                         stat.un.lsRjtError =
8598                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
8599                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
8600                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
8601                                 lpfc_block_fabric_iocbs(phba);
8602                         break;
8603         }
8604
8605         if (atomic_read(&phba->fabric_iocb_count) == 0)
8606                 BUG();
8607
8608         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
8609         cmdiocb->fabric_iocb_cmpl = NULL;
8610         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
8611         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
8612
8613         atomic_dec(&phba->fabric_iocb_count);
8614         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
8615                 /* Post any pending iocbs to HBA */
8616                 lpfc_resume_fabric_iocbs(phba);
8617         }
8618 }
8619
8620 /**
8621  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
8622  * @phba: pointer to lpfc hba data structure.
8623  * @iocb: pointer to lpfc command iocb data structure.
8624  *
8625  * This routine is used as the top-level API for issuing a fabric iocb command
8626  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
8627  * function makes sure that only one fabric bound iocb will be outstanding at
8628  * any given time. As such, this function will first check to see whether there
8629  * is already an outstanding fabric iocb on the wire. If so, it will put the
8630  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
8631  * issued later. Otherwise, it will issue the iocb on the wire and update the
8632  * fabric iocb count it indicate that there is one fabric iocb on the wire.
8633  *
8634  * Note, this implementation has a potential sending out fabric IOCBs out of
8635  * order. The problem is caused by the construction of the "ready" boolen does
8636  * not include the condition that the internal fabric IOCB list is empty. As
8637  * such, it is possible a fabric IOCB issued by this routine might be "jump"
8638  * ahead of the fabric IOCBs in the internal list.
8639  *
8640  * Return code
8641  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
8642  *   IOCB_ERROR - failed to issue fabric iocb
8643  **/
8644 static int
8645 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
8646 {
8647         unsigned long iflags;
8648         int ready;
8649         int ret;
8650
8651         if (atomic_read(&phba->fabric_iocb_count) > 1)
8652                 BUG();
8653
8654         spin_lock_irqsave(&phba->hbalock, iflags);
8655         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
8656                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8657
8658         if (ready)
8659                 /* Increment fabric iocb count to hold the position */
8660                 atomic_inc(&phba->fabric_iocb_count);
8661         spin_unlock_irqrestore(&phba->hbalock, iflags);
8662         if (ready) {
8663                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
8664                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
8665                 iocb->iocb_flag |= LPFC_IO_FABRIC;
8666
8667                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
8668                         "Fabric sched2:   ste:x%x",
8669                         iocb->vport->port_state, 0, 0);
8670
8671                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
8672
8673                 if (ret == IOCB_ERROR) {
8674                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
8675                         iocb->fabric_iocb_cmpl = NULL;
8676                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
8677                         atomic_dec(&phba->fabric_iocb_count);
8678                 }
8679         } else {
8680                 spin_lock_irqsave(&phba->hbalock, iflags);
8681                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
8682                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8683                 ret = IOCB_SUCCESS;
8684         }
8685         return ret;
8686 }
8687
8688 /**
8689  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
8690  * @vport: pointer to a virtual N_Port data structure.
8691  *
8692  * This routine aborts all the IOCBs associated with a @vport from the
8693  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
8694  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
8695  * list, removes each IOCB associated with the @vport off the list, set the
8696  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
8697  * associated with the IOCB.
8698  **/
8699 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
8700 {
8701         LIST_HEAD(completions);
8702         struct lpfc_hba  *phba = vport->phba;
8703         struct lpfc_iocbq *tmp_iocb, *piocb;
8704
8705         spin_lock_irq(&phba->hbalock);
8706         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
8707                                  list) {
8708
8709                 if (piocb->vport != vport)
8710                         continue;
8711
8712                 list_move_tail(&piocb->list, &completions);
8713         }
8714         spin_unlock_irq(&phba->hbalock);
8715
8716         /* Cancel all the IOCBs from the completions list */
8717         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8718                               IOERR_SLI_ABORTED);
8719 }
8720
8721 /**
8722  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
8723  * @ndlp: pointer to a node-list data structure.
8724  *
8725  * This routine aborts all the IOCBs associated with an @ndlp from the
8726  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
8727  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
8728  * list, removes each IOCB associated with the @ndlp off the list, set the
8729  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
8730  * associated with the IOCB.
8731  **/
8732 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
8733 {
8734         LIST_HEAD(completions);
8735         struct lpfc_hba  *phba = ndlp->phba;
8736         struct lpfc_iocbq *tmp_iocb, *piocb;
8737         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8738
8739         spin_lock_irq(&phba->hbalock);
8740         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
8741                                  list) {
8742                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
8743
8744                         list_move_tail(&piocb->list, &completions);
8745                 }
8746         }
8747         spin_unlock_irq(&phba->hbalock);
8748
8749         /* Cancel all the IOCBs from the completions list */
8750         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8751                               IOERR_SLI_ABORTED);
8752 }
8753
8754 /**
8755  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
8756  * @phba: pointer to lpfc hba data structure.
8757  *
8758  * This routine aborts all the IOCBs currently on the driver internal
8759  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
8760  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
8761  * list, removes IOCBs off the list, set the status feild to
8762  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
8763  * the IOCB.
8764  **/
8765 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
8766 {
8767         LIST_HEAD(completions);
8768
8769         spin_lock_irq(&phba->hbalock);
8770         list_splice_init(&phba->fabric_iocb_list, &completions);
8771         spin_unlock_irq(&phba->hbalock);
8772
8773         /* Cancel all the IOCBs from the completions list */
8774         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8775                               IOERR_SLI_ABORTED);
8776 }
8777
8778 /**
8779  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
8780  * @vport: pointer to lpfc vport data structure.
8781  *
8782  * This routine is invoked by the vport cleanup for deletions and the cleanup
8783  * for an ndlp on removal.
8784  **/
8785 void
8786 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
8787 {
8788         struct lpfc_hba *phba = vport->phba;
8789         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8790         unsigned long iflag = 0;
8791
8792         spin_lock_irqsave(&phba->hbalock, iflag);
8793         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
8794         list_for_each_entry_safe(sglq_entry, sglq_next,
8795                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
8796                 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
8797                         sglq_entry->ndlp = NULL;
8798         }
8799         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8800         spin_unlock_irqrestore(&phba->hbalock, iflag);
8801         return;
8802 }
8803
8804 /**
8805  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
8806  * @phba: pointer to lpfc hba data structure.
8807  * @axri: pointer to the els xri abort wcqe structure.
8808  *
8809  * This routine is invoked by the worker thread to process a SLI4 slow-path
8810  * ELS aborted xri.
8811  **/
8812 void
8813 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
8814                           struct sli4_wcqe_xri_aborted *axri)
8815 {
8816         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
8817         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
8818         uint16_t lxri = 0;
8819
8820         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8821         unsigned long iflag = 0;
8822         struct lpfc_nodelist *ndlp;
8823         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8824
8825         spin_lock_irqsave(&phba->hbalock, iflag);
8826         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
8827         list_for_each_entry_safe(sglq_entry, sglq_next,
8828                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
8829                 if (sglq_entry->sli4_xritag == xri) {
8830                         list_del(&sglq_entry->list);
8831                         ndlp = sglq_entry->ndlp;
8832                         sglq_entry->ndlp = NULL;
8833                         spin_lock(&pring->ring_lock);
8834                         list_add_tail(&sglq_entry->list,
8835                                 &phba->sli4_hba.lpfc_sgl_list);
8836                         sglq_entry->state = SGL_FREED;
8837                         spin_unlock(&pring->ring_lock);
8838                         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8839                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8840                         lpfc_set_rrq_active(phba, ndlp,
8841                                 sglq_entry->sli4_lxritag,
8842                                 rxid, 1);
8843
8844                         /* Check if TXQ queue needs to be serviced */
8845                         if (!(list_empty(&pring->txq)))
8846                                 lpfc_worker_wake_up(phba);
8847                         return;
8848                 }
8849         }
8850         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8851         lxri = lpfc_sli4_xri_inrange(phba, xri);
8852         if (lxri == NO_XRI) {
8853                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8854                 return;
8855         }
8856         spin_lock(&pring->ring_lock);
8857         sglq_entry = __lpfc_get_active_sglq(phba, lxri);
8858         if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
8859                 spin_unlock(&pring->ring_lock);
8860                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8861                 return;
8862         }
8863         sglq_entry->state = SGL_XRI_ABORTED;
8864         spin_unlock(&pring->ring_lock);
8865         spin_unlock_irqrestore(&phba->hbalock, iflag);
8866         return;
8867 }
8868
8869 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
8870  * @vport: pointer to virtual port object.
8871  * @ndlp: nodelist pointer for the impacted node.
8872  *
8873  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
8874  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
8875  * the driver is required to send a LOGO to the remote node before it
8876  * attempts to recover its login to the remote node.
8877  */
8878 void
8879 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8880                            struct lpfc_nodelist *ndlp)
8881 {
8882         struct Scsi_Host *shost;
8883         struct lpfc_hba *phba;
8884         unsigned long flags = 0;
8885
8886         shost = lpfc_shost_from_vport(vport);
8887         phba = vport->phba;
8888         if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8889                 lpfc_printf_log(phba, KERN_INFO,
8890                                 LOG_SLI, "3093 No rport recovery needed. "
8891                                 "rport in state 0x%x\n", ndlp->nlp_state);
8892                 return;
8893         }
8894         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8895                         "3094 Start rport recovery on shost id 0x%x "
8896                         "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8897                         "flags 0x%x\n",
8898                         shost->host_no, ndlp->nlp_DID,
8899                         vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8900                         ndlp->nlp_flag);
8901         /*
8902          * The rport is not responding.  Remove the FCP-2 flag to prevent
8903          * an ADISC in the follow-up recovery code.
8904          */
8905         spin_lock_irqsave(shost->host_lock, flags);
8906         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8907         spin_unlock_irqrestore(shost->host_lock, flags);
8908         lpfc_issue_els_logo(vport, ndlp, 0);
8909         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
8910 }
8911