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