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