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