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