]> err.no Git - linux-2.6/blob - drivers/scsi/lpfc/lpfc_els.c
[SCSI] lpfc 8.2.3 : FC Discovery Fixes
[linux-2.6] / 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-2007 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
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_disc.h"
34 #include "lpfc_scsi.h"
35 #include "lpfc.h"
36 #include "lpfc_logmsg.h"
37 #include "lpfc_crtn.h"
38 #include "lpfc_vport.h"
39 #include "lpfc_debugfs.h"
40
41 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
42                           struct lpfc_iocbq *);
43 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
44                         struct lpfc_iocbq *);
45
46 static int lpfc_max_els_tries = 3;
47
48 int
49 lpfc_els_chk_latt(struct lpfc_vport *vport)
50 {
51         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
52         struct lpfc_hba  *phba = vport->phba;
53         uint32_t ha_copy;
54
55         if (vport->port_state >= LPFC_VPORT_READY ||
56             phba->link_state == LPFC_LINK_DOWN)
57                 return 0;
58
59         /* Read the HBA Host Attention Register */
60         ha_copy = readl(phba->HAregaddr);
61
62         if (!(ha_copy & HA_LATT))
63                 return 0;
64
65         /* Pending Link Event during Discovery */
66         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
67                          "0237 Pending Link Event during "
68                          "Discovery: State x%x\n",
69                          phba->pport->port_state);
70
71         /* CLEAR_LA should re-enable link attention events and
72          * we should then imediately take a LATT event. The
73          * LATT processing should call lpfc_linkdown() which
74          * will cleanup any left over in-progress discovery
75          * events.
76          */
77         spin_lock_irq(shost->host_lock);
78         vport->fc_flag |= FC_ABORT_DISCOVERY;
79         spin_unlock_irq(shost->host_lock);
80
81         if (phba->link_state != LPFC_CLEAR_LA)
82                 lpfc_issue_clear_la(phba, vport);
83
84         return 1;
85 }
86
87 static struct lpfc_iocbq *
88 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
89                    uint16_t cmdSize, uint8_t retry,
90                    struct lpfc_nodelist *ndlp, uint32_t did,
91                    uint32_t elscmd)
92 {
93         struct lpfc_hba  *phba = vport->phba;
94         struct lpfc_iocbq *elsiocb;
95         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
96         struct ulp_bde64 *bpl;
97         IOCB_t *icmd;
98
99
100         if (!lpfc_is_link_up(phba))
101                 return NULL;
102
103         /* Allocate buffer for  command iocb */
104         elsiocb = lpfc_sli_get_iocbq(phba);
105
106         if (elsiocb == NULL)
107                 return NULL;
108         icmd = &elsiocb->iocb;
109
110         /* fill in BDEs for command */
111         /* Allocate buffer for command payload */
112         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
113         if (pcmd)
114                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
115         if (!pcmd || !pcmd->virt) {
116                 kfree(pcmd);
117
118                 lpfc_sli_release_iocbq(phba, elsiocb);
119                 return NULL;
120         }
121
122         INIT_LIST_HEAD(&pcmd->list);
123
124         /* Allocate buffer for response payload */
125         if (expectRsp) {
126                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
127                 if (prsp)
128                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
129                                                      &prsp->phys);
130                 if (!prsp || !prsp->virt) {
131                         kfree(prsp);
132                         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
133                         kfree(pcmd);
134                         lpfc_sli_release_iocbq(phba, elsiocb);
135                         return NULL;
136                 }
137                 INIT_LIST_HEAD(&prsp->list);
138         } else {
139                 prsp = NULL;
140         }
141
142         /* Allocate buffer for Buffer ptr list */
143         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
144         if (pbuflist)
145                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
146                                                  &pbuflist->phys);
147         if (!pbuflist || !pbuflist->virt) {
148                 lpfc_sli_release_iocbq(phba, elsiocb);
149                 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
150                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
151                 kfree(pcmd);
152                 kfree(prsp);
153                 kfree(pbuflist);
154                 return NULL;
155         }
156
157         INIT_LIST_HEAD(&pbuflist->list);
158
159         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
160         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
161         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
162         icmd->un.elsreq64.remoteID = did;       /* DID */
163         if (expectRsp) {
164                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
165                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
166                 icmd->ulpTimeout = phba->fc_ratov * 2;
167         } else {
168                 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
169                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
170         }
171         icmd->ulpBdeCount = 1;
172         icmd->ulpLe = 1;
173         icmd->ulpClass = CLASS3;
174
175         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
176                 icmd->un.elsreq64.myID = vport->fc_myDID;
177
178                 /* For ELS_REQUEST64_CR, use the VPI by default */
179                 icmd->ulpContext = vport->vpi;
180                 icmd->ulpCt_h = 0;
181                 icmd->ulpCt_l = 1;
182         }
183
184         bpl = (struct ulp_bde64 *) pbuflist->virt;
185         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
186         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
187         bpl->tus.f.bdeSize = cmdSize;
188         bpl->tus.f.bdeFlags = 0;
189         bpl->tus.w = le32_to_cpu(bpl->tus.w);
190
191         if (expectRsp) {
192                 bpl++;
193                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
194                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
195                 bpl->tus.f.bdeSize = FCELSSIZE;
196                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
197                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
198         }
199
200         elsiocb->context1 = lpfc_nlp_get(ndlp);
201         elsiocb->context2 = pcmd;
202         elsiocb->context3 = pbuflist;
203         elsiocb->retry = retry;
204         elsiocb->vport = vport;
205         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
206
207         if (prsp) {
208                 list_add(&prsp->list, &pcmd->list);
209         }
210         if (expectRsp) {
211                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
212                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
213                                  "0116 Xmit ELS command x%x to remote "
214                                  "NPORT x%x I/O tag: x%x, port state: x%x\n",
215                                  elscmd, did, elsiocb->iotag,
216                                  vport->port_state);
217         } else {
218                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
219                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
220                                  "0117 Xmit ELS response x%x to remote "
221                                  "NPORT x%x I/O tag: x%x, size: x%x\n",
222                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
223                                  cmdSize);
224         }
225         return elsiocb;
226 }
227
228
229 static int
230 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
231 {
232         struct lpfc_hba  *phba = vport->phba;
233         LPFC_MBOXQ_t *mbox;
234         struct lpfc_dmabuf *mp;
235         struct lpfc_nodelist *ndlp;
236         struct serv_parm *sp;
237         int rc;
238         int err = 0;
239
240         sp = &phba->fc_fabparam;
241         ndlp = lpfc_findnode_did(vport, Fabric_DID);
242         if (!ndlp) {
243                 err = 1;
244                 goto fail;
245         }
246
247         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
248         if (!mbox) {
249                 err = 2;
250                 goto fail;
251         }
252
253         vport->port_state = LPFC_FABRIC_CFG_LINK;
254         lpfc_config_link(phba, mbox);
255         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
256         mbox->vport = vport;
257
258         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
259         if (rc == MBX_NOT_FINISHED) {
260                 err = 3;
261                 goto fail_free_mbox;
262         }
263
264         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
265         if (!mbox) {
266                 err = 4;
267                 goto fail;
268         }
269         rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
270                             0);
271         if (rc) {
272                 err = 5;
273                 goto fail_free_mbox;
274         }
275
276         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
277         mbox->vport = vport;
278         mbox->context2 = lpfc_nlp_get(ndlp);
279
280         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
281         if (rc == MBX_NOT_FINISHED) {
282                 err = 6;
283                 goto fail_issue_reg_login;
284         }
285
286         return 0;
287
288 fail_issue_reg_login:
289         lpfc_nlp_put(ndlp);
290         mp = (struct lpfc_dmabuf *) mbox->context1;
291         lpfc_mbuf_free(phba, mp->virt, mp->phys);
292         kfree(mp);
293 fail_free_mbox:
294         mempool_free(mbox, phba->mbox_mem_pool);
295
296 fail:
297         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
298         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
299                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
300         return -ENXIO;
301 }
302
303 static int
304 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
305                            struct serv_parm *sp, IOCB_t *irsp)
306 {
307         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
308         struct lpfc_hba  *phba = vport->phba;
309         struct lpfc_nodelist *np;
310         struct lpfc_nodelist *next_np;
311
312         spin_lock_irq(shost->host_lock);
313         vport->fc_flag |= FC_FABRIC;
314         spin_unlock_irq(shost->host_lock);
315
316         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
317         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
318                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
319
320         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
321
322         if (phba->fc_topology == TOPOLOGY_LOOP) {
323                 spin_lock_irq(shost->host_lock);
324                 vport->fc_flag |= FC_PUBLIC_LOOP;
325                 spin_unlock_irq(shost->host_lock);
326         } else {
327                 /*
328                  * If we are a N-port connected to a Fabric, fixup sparam's so
329                  * logins to devices on remote loops work.
330                  */
331                 vport->fc_sparam.cmn.altBbCredit = 1;
332         }
333
334         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
335         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
336         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
337         ndlp->nlp_class_sup = 0;
338         if (sp->cls1.classValid)
339                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
340         if (sp->cls2.classValid)
341                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
342         if (sp->cls3.classValid)
343                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
344         if (sp->cls4.classValid)
345                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
346         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
347                                 sp->cmn.bbRcvSizeLsb;
348         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
349
350         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
351                 if (sp->cmn.response_multiple_NPort) {
352                         lpfc_printf_vlog(vport, KERN_WARNING,
353                                          LOG_ELS | LOG_VPORT,
354                                          "1816 FLOGI NPIV supported, "
355                                          "response data 0x%x\n",
356                                          sp->cmn.response_multiple_NPort);
357                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
358                 } else {
359                         /* Because we asked f/w for NPIV it still expects us
360                         to call reg_vnpid atleast for the physcial host */
361                         lpfc_printf_vlog(vport, KERN_WARNING,
362                                          LOG_ELS | LOG_VPORT,
363                                          "1817 Fabric does not support NPIV "
364                                          "- configuring single port mode.\n");
365                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
366                 }
367         }
368
369         if ((vport->fc_prevDID != vport->fc_myDID) &&
370                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
371
372                 /* If our NportID changed, we need to ensure all
373                  * remaining NPORTs get unreg_login'ed.
374                  */
375                 list_for_each_entry_safe(np, next_np,
376                                         &vport->fc_nodes, nlp_listp) {
377                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
378                                    !(np->nlp_flag & NLP_NPR_ADISC))
379                                 continue;
380                         spin_lock_irq(shost->host_lock);
381                         np->nlp_flag &= ~NLP_NPR_ADISC;
382                         spin_unlock_irq(shost->host_lock);
383                         lpfc_unreg_rpi(vport, np);
384                 }
385                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
386                         lpfc_mbx_unreg_vpi(vport);
387                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
388                 }
389         }
390
391         ndlp->nlp_sid = irsp->un.ulpWord[4] & Mask_DID;
392         lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
393
394         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
395             vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
396                 lpfc_register_new_vport(phba, vport, ndlp);
397                 return 0;
398         }
399         lpfc_issue_fabric_reglogin(vport);
400         return 0;
401 }
402
403 /*
404  * We FLOGIed into an NPort, initiate pt2pt protocol
405  */
406 static int
407 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
408                           struct serv_parm *sp)
409 {
410         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
411         struct lpfc_hba  *phba = vport->phba;
412         LPFC_MBOXQ_t *mbox;
413         int rc;
414
415         spin_lock_irq(shost->host_lock);
416         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
417         spin_unlock_irq(shost->host_lock);
418
419         phba->fc_edtov = FF_DEF_EDTOV;
420         phba->fc_ratov = FF_DEF_RATOV;
421         rc = memcmp(&vport->fc_portname, &sp->portName,
422                     sizeof(vport->fc_portname));
423         if (rc >= 0) {
424                 /* This side will initiate the PLOGI */
425                 spin_lock_irq(shost->host_lock);
426                 vport->fc_flag |= FC_PT2PT_PLOGI;
427                 spin_unlock_irq(shost->host_lock);
428
429                 /*
430                  * N_Port ID cannot be 0, set our to LocalID the other
431                  * side will be RemoteID.
432                  */
433
434                 /* not equal */
435                 if (rc)
436                         vport->fc_myDID = PT2PT_LocalID;
437
438                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
439                 if (!mbox)
440                         goto fail;
441
442                 lpfc_config_link(phba, mbox);
443
444                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
445                 mbox->vport = vport;
446                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
447                 if (rc == MBX_NOT_FINISHED) {
448                         mempool_free(mbox, phba->mbox_mem_pool);
449                         goto fail;
450                 }
451                 lpfc_nlp_put(ndlp);
452
453                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
454                 if (!ndlp) {
455                         /*
456                          * Cannot find existing Fabric ndlp, so allocate a
457                          * new one
458                          */
459                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
460                         if (!ndlp)
461                                 goto fail;
462
463                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
464                 }
465
466                 memcpy(&ndlp->nlp_portname, &sp->portName,
467                        sizeof(struct lpfc_name));
468                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
469                        sizeof(struct lpfc_name));
470                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
471                 spin_lock_irq(shost->host_lock);
472                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
473                 spin_unlock_irq(shost->host_lock);
474         } else {
475                 /* This side will wait for the PLOGI */
476                 lpfc_nlp_put(ndlp);
477         }
478
479         spin_lock_irq(shost->host_lock);
480         vport->fc_flag |= FC_PT2PT;
481         spin_unlock_irq(shost->host_lock);
482
483         /* Start discovery - this should just do CLEAR_LA */
484         lpfc_disc_start(vport);
485         return 0;
486 fail:
487         return -ENXIO;
488 }
489
490 static void
491 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
492                     struct lpfc_iocbq *rspiocb)
493 {
494         struct lpfc_vport *vport = cmdiocb->vport;
495         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
496         IOCB_t *irsp = &rspiocb->iocb;
497         struct lpfc_nodelist *ndlp = cmdiocb->context1;
498         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
499         struct serv_parm *sp;
500         int rc;
501
502         /* Check to see if link went down during discovery */
503         if (lpfc_els_chk_latt(vport)) {
504                 lpfc_nlp_put(ndlp);
505                 goto out;
506         }
507
508         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
509                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
510                 irsp->ulpStatus, irsp->un.ulpWord[4],
511                 vport->port_state);
512
513         if (irsp->ulpStatus) {
514                 /* Check for retry */
515                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
516                         goto out;
517
518                 /* FLOGI failed, so there is no fabric */
519                 spin_lock_irq(shost->host_lock);
520                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
521                 spin_unlock_irq(shost->host_lock);
522
523                 /* If private loop, then allow max outstanding els to be
524                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
525                  * alpa map would take too long otherwise.
526                  */
527                 if (phba->alpa_map[0] == 0) {
528                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
529                 }
530
531                 /* FLOGI failure */
532                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
533                                  "0100 FLOGI failure Data: x%x x%x "
534                                  "x%x\n",
535                                  irsp->ulpStatus, irsp->un.ulpWord[4],
536                                  irsp->ulpTimeout);
537                 goto flogifail;
538         }
539
540         /*
541          * The FLogI succeeded.  Sync the data for the CPU before
542          * accessing it.
543          */
544         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
545
546         sp = prsp->virt + sizeof(uint32_t);
547
548         /* FLOGI completes successfully */
549         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
550                          "0101 FLOGI completes sucessfully "
551                          "Data: x%x x%x x%x x%x\n",
552                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
553                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
554
555         if (vport->port_state == LPFC_FLOGI) {
556                 /*
557                  * If Common Service Parameters indicate Nport
558                  * we are point to point, if Fport we are Fabric.
559                  */
560                 if (sp->cmn.fPort)
561                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
562                 else
563                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
564
565                 if (!rc)
566                         goto out;
567         }
568
569 flogifail:
570         lpfc_nlp_put(ndlp);
571
572         if (!lpfc_error_lost_link(irsp)) {
573                 /* FLOGI failed, so just use loop map to make discovery list */
574                 lpfc_disc_list_loopmap(vport);
575
576                 /* Start discovery */
577                 lpfc_disc_start(vport);
578         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
579                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
580                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
581                         (phba->link_state != LPFC_CLEAR_LA)) {
582                 /* If FLOGI failed enable link interrupt. */
583                 lpfc_issue_clear_la(phba, vport);
584         }
585 out:
586         lpfc_els_free_iocb(phba, cmdiocb);
587 }
588
589 static int
590 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
591                      uint8_t retry)
592 {
593         struct lpfc_hba  *phba = vport->phba;
594         struct serv_parm *sp;
595         IOCB_t *icmd;
596         struct lpfc_iocbq *elsiocb;
597         struct lpfc_sli_ring *pring;
598         uint8_t *pcmd;
599         uint16_t cmdsize;
600         uint32_t tmo;
601         int rc;
602
603         pring = &phba->sli.ring[LPFC_ELS_RING];
604
605         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
606         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
607                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
608
609         if (!elsiocb)
610                 return 1;
611
612         icmd = &elsiocb->iocb;
613         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
614
615         /* For FLOGI request, remainder of payload is service parameters */
616         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
617         pcmd += sizeof(uint32_t);
618         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
619         sp = (struct serv_parm *) pcmd;
620
621         /* Setup CSPs accordingly for Fabric */
622         sp->cmn.e_d_tov = 0;
623         sp->cmn.w2.r_a_tov = 0;
624         sp->cls1.classValid = 0;
625         sp->cls2.seqDelivery = 1;
626         sp->cls3.seqDelivery = 1;
627         if (sp->cmn.fcphLow < FC_PH3)
628                 sp->cmn.fcphLow = FC_PH3;
629         if (sp->cmn.fcphHigh < FC_PH3)
630                 sp->cmn.fcphHigh = FC_PH3;
631
632         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
633                 sp->cmn.request_multiple_Nport = 1;
634
635                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
636                 icmd->ulpCt_h = 1;
637                 icmd->ulpCt_l = 0;
638         }
639
640         if (phba->fc_topology != TOPOLOGY_LOOP) {
641                 icmd->un.elsreq64.myID = 0;
642                 icmd->un.elsreq64.fl = 1;
643         }
644
645         tmo = phba->fc_ratov;
646         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
647         lpfc_set_disctmo(vport);
648         phba->fc_ratov = tmo;
649
650         phba->fc_stat.elsXmitFLOGI++;
651         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
652
653         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
654                 "Issue FLOGI:     opt:x%x",
655                 phba->sli3_options, 0, 0);
656
657         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
658         if (rc == IOCB_ERROR) {
659                 lpfc_els_free_iocb(phba, elsiocb);
660                 return 1;
661         }
662         return 0;
663 }
664
665 int
666 lpfc_els_abort_flogi(struct lpfc_hba *phba)
667 {
668         struct lpfc_sli_ring *pring;
669         struct lpfc_iocbq *iocb, *next_iocb;
670         struct lpfc_nodelist *ndlp;
671         IOCB_t *icmd;
672
673         /* Abort outstanding I/O on NPort <nlp_DID> */
674         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
675                         "0201 Abort outstanding I/O on NPort x%x\n",
676                         Fabric_DID);
677
678         pring = &phba->sli.ring[LPFC_ELS_RING];
679
680         /*
681          * Check the txcmplq for an iocb that matches the nport the driver is
682          * searching for.
683          */
684         spin_lock_irq(&phba->hbalock);
685         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
686                 icmd = &iocb->iocb;
687                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
688                     icmd->un.elsreq64.bdl.ulpIoTag32) {
689                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
690                         if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
691                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
692                         }
693                 }
694         }
695         spin_unlock_irq(&phba->hbalock);
696
697         return 0;
698 }
699
700 int
701 lpfc_initial_flogi(struct lpfc_vport *vport)
702 {
703         struct lpfc_hba *phba = vport->phba;
704         struct lpfc_nodelist *ndlp;
705
706         vport->port_state = LPFC_FLOGI;
707         lpfc_set_disctmo(vport);
708
709         /* First look for the Fabric ndlp */
710         ndlp = lpfc_findnode_did(vport, Fabric_DID);
711         if (!ndlp) {
712                 /* Cannot find existing Fabric ndlp, so allocate a new one */
713                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
714                 if (!ndlp)
715                         return 0;
716                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
717         } else {
718                 lpfc_dequeue_node(vport, ndlp);
719         }
720
721         if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
722                 lpfc_nlp_put(ndlp);
723         }
724         return 1;
725 }
726
727 int
728 lpfc_initial_fdisc(struct lpfc_vport *vport)
729 {
730         struct lpfc_hba *phba = vport->phba;
731         struct lpfc_nodelist *ndlp;
732
733         /* First look for the Fabric ndlp */
734         ndlp = lpfc_findnode_did(vport, Fabric_DID);
735         if (!ndlp) {
736                 /* Cannot find existing Fabric ndlp, so allocate a new one */
737                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
738                 if (!ndlp)
739                         return 0;
740                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
741         } else {
742                 lpfc_dequeue_node(vport, ndlp);
743         }
744         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
745                 lpfc_nlp_put(ndlp);
746         }
747         return 1;
748 }
749
750 void
751 lpfc_more_plogi(struct lpfc_vport *vport)
752 {
753         int sentplogi;
754
755         if (vport->num_disc_nodes)
756                 vport->num_disc_nodes--;
757
758         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
759         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
760                          "0232 Continue discovery with %d PLOGIs to go "
761                          "Data: x%x x%x x%x\n",
762                          vport->num_disc_nodes, vport->fc_plogi_cnt,
763                          vport->fc_flag, vport->port_state);
764         /* Check to see if there are more PLOGIs to be sent */
765         if (vport->fc_flag & FC_NLP_MORE)
766                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
767                 sentplogi = lpfc_els_disc_plogi(vport);
768
769         return;
770 }
771
772 static struct lpfc_nodelist *
773 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
774                          struct lpfc_nodelist *ndlp)
775 {
776         struct lpfc_vport    *vport = ndlp->vport;
777         struct lpfc_nodelist *new_ndlp;
778         struct serv_parm *sp;
779         uint8_t  name[sizeof(struct lpfc_name)];
780         uint32_t rc;
781
782         /* Fabric nodes can have the same WWPN so we don't bother searching
783          * by WWPN.  Just return the ndlp that was given to us.
784          */
785         if (ndlp->nlp_type & NLP_FABRIC)
786                 return ndlp;
787
788         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
789         memset(name, 0, sizeof(struct lpfc_name));
790
791         /* Now we find out if the NPort we are logging into, matches the WWPN
792          * we have for that ndlp. If not, we have some work to do.
793          */
794         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
795
796         if (new_ndlp == ndlp)
797                 return ndlp;
798
799         if (!new_ndlp) {
800                 rc = memcmp(&ndlp->nlp_portname, name,
801                             sizeof(struct lpfc_name));
802                 if (!rc)
803                         return ndlp;
804                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
805                 if (!new_ndlp)
806                         return ndlp;
807
808                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
809         }
810
811         lpfc_unreg_rpi(vport, new_ndlp);
812         new_ndlp->nlp_DID = ndlp->nlp_DID;
813         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
814         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
815
816         /* Move this back to NPR state */
817         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
818                 /* The new_ndlp is replacing ndlp totally, so we need
819                  * to put ndlp on UNUSED list and try to free it.
820                  */
821                 lpfc_drop_node(vport, ndlp);
822         }
823         else {
824                 lpfc_unreg_rpi(vport, ndlp);
825                 ndlp->nlp_DID = 0; /* Two ndlps cannot have the same did */
826                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
827         }
828         return new_ndlp;
829 }
830
831 void
832 lpfc_end_rscn(struct lpfc_vport *vport)
833 {
834         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
835
836         if (vport->fc_flag & FC_RSCN_MODE) {
837                 /*
838                  * Check to see if more RSCNs came in while we were
839                  * processing this one.
840                  */
841                 if (vport->fc_rscn_id_cnt ||
842                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
843                         lpfc_els_handle_rscn(vport);
844                 else {
845                         spin_lock_irq(shost->host_lock);
846                         vport->fc_flag &= ~FC_RSCN_MODE;
847                         spin_unlock_irq(shost->host_lock);
848                 }
849         }
850 }
851
852 static void
853 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
854                     struct lpfc_iocbq *rspiocb)
855 {
856         struct lpfc_vport *vport = cmdiocb->vport;
857         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
858         IOCB_t *irsp;
859         struct lpfc_nodelist *ndlp;
860         struct lpfc_dmabuf *prsp;
861         int disc, rc, did, type;
862
863         /* we pass cmdiocb to state machine which needs rspiocb as well */
864         cmdiocb->context_un.rsp_iocb = rspiocb;
865
866         irsp = &rspiocb->iocb;
867         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
868                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
869                 irsp->ulpStatus, irsp->un.ulpWord[4],
870                 irsp->un.elsreq64.remoteID);
871
872         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
873         if (!ndlp) {
874                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
875                                  "0136 PLOGI completes to NPort x%x "
876                                  "with no ndlp. Data: x%x x%x x%x\n",
877                                  irsp->un.elsreq64.remoteID,
878                                  irsp->ulpStatus, irsp->un.ulpWord[4],
879                                  irsp->ulpIoTag);
880                 goto out;
881         }
882
883         /* Since ndlp can be freed in the disc state machine, note if this node
884          * is being used during discovery.
885          */
886         spin_lock_irq(shost->host_lock);
887         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
888         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
889         spin_unlock_irq(shost->host_lock);
890         rc   = 0;
891
892         /* PLOGI completes to NPort <nlp_DID> */
893         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
894                          "0102 PLOGI completes to NPort x%x "
895                          "Data: x%x x%x x%x x%x x%x\n",
896                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
897                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
898         /* Check to see if link went down during discovery */
899         if (lpfc_els_chk_latt(vport)) {
900                 spin_lock_irq(shost->host_lock);
901                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
902                 spin_unlock_irq(shost->host_lock);
903                 goto out;
904         }
905
906         /* ndlp could be freed in DSM, save these values now */
907         type = ndlp->nlp_type;
908         did = ndlp->nlp_DID;
909
910         if (irsp->ulpStatus) {
911                 /* Check for retry */
912                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
913                         /* ELS command is being retried */
914                         if (disc) {
915                                 spin_lock_irq(shost->host_lock);
916                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
917                                 spin_unlock_irq(shost->host_lock);
918                         }
919                         goto out;
920                 }
921                 /* PLOGI failed */
922                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
923                 if (lpfc_error_lost_link(irsp)) {
924                         rc = NLP_STE_FREED_NODE;
925                 } else {
926                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
927                                                      NLP_EVT_CMPL_PLOGI);
928                 }
929         } else {
930                 /* Good status, call state machine */
931                 prsp = list_entry(((struct lpfc_dmabuf *)
932                                    cmdiocb->context2)->list.next,
933                                   struct lpfc_dmabuf, list);
934                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
935                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
936                                              NLP_EVT_CMPL_PLOGI);
937         }
938
939         if (disc && vport->num_disc_nodes) {
940                 /* Check to see if there are more PLOGIs to be sent */
941                 lpfc_more_plogi(vport);
942
943                 if (vport->num_disc_nodes == 0) {
944                         spin_lock_irq(shost->host_lock);
945                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
946                         spin_unlock_irq(shost->host_lock);
947
948                         lpfc_can_disctmo(vport);
949                         lpfc_end_rscn(vport);
950                 }
951         }
952
953 out:
954         lpfc_els_free_iocb(phba, cmdiocb);
955         return;
956 }
957
958 int
959 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
960 {
961         struct lpfc_hba  *phba = vport->phba;
962         struct serv_parm *sp;
963         IOCB_t *icmd;
964         struct lpfc_nodelist *ndlp;
965         struct lpfc_iocbq *elsiocb;
966         struct lpfc_sli_ring *pring;
967         struct lpfc_sli *psli;
968         uint8_t *pcmd;
969         uint16_t cmdsize;
970         int ret;
971
972         psli = &phba->sli;
973         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
974
975         ndlp = lpfc_findnode_did(vport, did);
976         /* If ndlp if not NULL, we will bump the reference count on it */
977
978         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
979         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
980                                      ELS_CMD_PLOGI);
981         if (!elsiocb)
982                 return 1;
983
984         icmd = &elsiocb->iocb;
985         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
986
987         /* For PLOGI request, remainder of payload is service parameters */
988         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
989         pcmd += sizeof(uint32_t);
990         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
991         sp = (struct serv_parm *) pcmd;
992
993         if (sp->cmn.fcphLow < FC_PH_4_3)
994                 sp->cmn.fcphLow = FC_PH_4_3;
995
996         if (sp->cmn.fcphHigh < FC_PH3)
997                 sp->cmn.fcphHigh = FC_PH3;
998
999         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1000                 "Issue PLOGI:     did:x%x",
1001                 did, 0, 0);
1002
1003         phba->fc_stat.elsXmitPLOGI++;
1004         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1005         ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1006
1007         if (ret == IOCB_ERROR) {
1008                 lpfc_els_free_iocb(phba, elsiocb);
1009                 return 1;
1010         }
1011         return 0;
1012 }
1013
1014 static void
1015 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1016                    struct lpfc_iocbq *rspiocb)
1017 {
1018         struct lpfc_vport *vport = cmdiocb->vport;
1019         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1020         IOCB_t *irsp;
1021         struct lpfc_sli *psli;
1022         struct lpfc_nodelist *ndlp;
1023
1024         psli = &phba->sli;
1025         /* we pass cmdiocb to state machine which needs rspiocb as well */
1026         cmdiocb->context_un.rsp_iocb = rspiocb;
1027
1028         irsp = &(rspiocb->iocb);
1029         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1030         spin_lock_irq(shost->host_lock);
1031         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1032         spin_unlock_irq(shost->host_lock);
1033
1034         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1035                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1036                 irsp->ulpStatus, irsp->un.ulpWord[4],
1037                 ndlp->nlp_DID);
1038         /* PRLI completes to NPort <nlp_DID> */
1039         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1040                          "0103 PRLI completes to NPort x%x "
1041                          "Data: x%x x%x x%x x%x\n",
1042                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1043                          irsp->ulpTimeout, vport->num_disc_nodes);
1044
1045         vport->fc_prli_sent--;
1046         /* Check to see if link went down during discovery */
1047         if (lpfc_els_chk_latt(vport))
1048                 goto out;
1049
1050         if (irsp->ulpStatus) {
1051                 /* Check for retry */
1052                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1053                         /* ELS command is being retried */
1054                         goto out;
1055                 }
1056                 /* PRLI failed */
1057                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1058                 if (lpfc_error_lost_link(irsp)) {
1059                         goto out;
1060                 } else {
1061                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1062                                                 NLP_EVT_CMPL_PRLI);
1063                 }
1064         } else {
1065                 /* Good status, call state machine */
1066                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1067                                         NLP_EVT_CMPL_PRLI);
1068         }
1069
1070 out:
1071         lpfc_els_free_iocb(phba, cmdiocb);
1072         return;
1073 }
1074
1075 int
1076 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1077                     uint8_t retry)
1078 {
1079         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1080         struct lpfc_hba *phba = vport->phba;
1081         PRLI *npr;
1082         IOCB_t *icmd;
1083         struct lpfc_iocbq *elsiocb;
1084         struct lpfc_sli_ring *pring;
1085         struct lpfc_sli *psli;
1086         uint8_t *pcmd;
1087         uint16_t cmdsize;
1088
1089         psli = &phba->sli;
1090         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1091
1092         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1093         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1094                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1095         if (!elsiocb)
1096                 return 1;
1097
1098         icmd = &elsiocb->iocb;
1099         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1100
1101         /* For PRLI request, remainder of payload is service parameters */
1102         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1103         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1104         pcmd += sizeof(uint32_t);
1105
1106         /* For PRLI, remainder of payload is PRLI parameter page */
1107         npr = (PRLI *) pcmd;
1108         /*
1109          * If our firmware version is 3.20 or later,
1110          * set the following bits for FC-TAPE support.
1111          */
1112         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1113                 npr->ConfmComplAllowed = 1;
1114                 npr->Retry = 1;
1115                 npr->TaskRetryIdReq = 1;
1116         }
1117         npr->estabImagePair = 1;
1118         npr->readXferRdyDis = 1;
1119
1120         /* For FCP support */
1121         npr->prliType = PRLI_FCP_TYPE;
1122         npr->initiatorFunc = 1;
1123
1124         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1125                 "Issue PRLI:      did:x%x",
1126                 ndlp->nlp_DID, 0, 0);
1127
1128         phba->fc_stat.elsXmitPRLI++;
1129         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1130         spin_lock_irq(shost->host_lock);
1131         ndlp->nlp_flag |= NLP_PRLI_SND;
1132         spin_unlock_irq(shost->host_lock);
1133         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1134                 spin_lock_irq(shost->host_lock);
1135                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1136                 spin_unlock_irq(shost->host_lock);
1137                 lpfc_els_free_iocb(phba, elsiocb);
1138                 return 1;
1139         }
1140         vport->fc_prli_sent++;
1141         return 0;
1142 }
1143
1144 static void
1145 lpfc_more_adisc(struct lpfc_vport *vport)
1146 {
1147         int sentadisc;
1148
1149         if (vport->num_disc_nodes)
1150                 vport->num_disc_nodes--;
1151         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1152         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1153                          "0210 Continue discovery with %d ADISCs to go "
1154                          "Data: x%x x%x x%x\n",
1155                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1156                          vport->fc_flag, vport->port_state);
1157         /* Check to see if there are more ADISCs to be sent */
1158         if (vport->fc_flag & FC_NLP_MORE) {
1159                 lpfc_set_disctmo(vport);
1160                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1161                 sentadisc = lpfc_els_disc_adisc(vport);
1162         }
1163         return;
1164 }
1165
1166 static void
1167 lpfc_rscn_disc(struct lpfc_vport *vport)
1168 {
1169         lpfc_can_disctmo(vport);
1170
1171         /* RSCN discovery */
1172         /* go thru NPR nodes and issue ELS PLOGIs */
1173         if (vport->fc_npr_cnt)
1174                 if (lpfc_els_disc_plogi(vport))
1175                         return;
1176
1177         lpfc_end_rscn(vport);
1178 }
1179
1180 static void
1181 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1182                     struct lpfc_iocbq *rspiocb)
1183 {
1184         struct lpfc_vport *vport = cmdiocb->vport;
1185         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1186         IOCB_t *irsp;
1187         struct lpfc_nodelist *ndlp;
1188         int  disc;
1189
1190         /* we pass cmdiocb to state machine which needs rspiocb as well */
1191         cmdiocb->context_un.rsp_iocb = rspiocb;
1192
1193         irsp = &(rspiocb->iocb);
1194         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1195
1196         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1197                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1198                 irsp->ulpStatus, irsp->un.ulpWord[4],
1199                 ndlp->nlp_DID);
1200
1201         /* Since ndlp can be freed in the disc state machine, note if this node
1202          * is being used during discovery.
1203          */
1204         spin_lock_irq(shost->host_lock);
1205         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1206         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1207         spin_unlock_irq(shost->host_lock);
1208         /* ADISC completes to NPort <nlp_DID> */
1209         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1210                          "0104 ADISC completes to NPort x%x "
1211                          "Data: x%x x%x x%x x%x x%x\n",
1212                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1213                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1214         /* Check to see if link went down during discovery */
1215         if (lpfc_els_chk_latt(vport)) {
1216                 spin_lock_irq(shost->host_lock);
1217                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1218                 spin_unlock_irq(shost->host_lock);
1219                 goto out;
1220         }
1221
1222         if (irsp->ulpStatus) {
1223                 /* Check for retry */
1224                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1225                         /* ELS command is being retried */
1226                         if (disc) {
1227                                 spin_lock_irq(shost->host_lock);
1228                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1229                                 spin_unlock_irq(shost->host_lock);
1230                                 lpfc_set_disctmo(vport);
1231                         }
1232                         goto out;
1233                 }
1234                 /* ADISC failed */
1235                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1236                 if (!lpfc_error_lost_link(irsp)) {
1237                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1238                                                 NLP_EVT_CMPL_ADISC);
1239                 }
1240         } else {
1241                 /* Good status, call state machine */
1242                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1243                                         NLP_EVT_CMPL_ADISC);
1244         }
1245
1246         if (disc && vport->num_disc_nodes) {
1247                 /* Check to see if there are more ADISCs to be sent */
1248                 lpfc_more_adisc(vport);
1249
1250                 /* Check to see if we are done with ADISC authentication */
1251                 if (vport->num_disc_nodes == 0) {
1252                         /* If we get here, there is nothing left to ADISC */
1253                         /*
1254                          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1255                          * and continue discovery.
1256                          */
1257                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1258                            !(vport->fc_flag & FC_RSCN_MODE)) {
1259                                 lpfc_issue_reg_vpi(phba, vport);
1260                                 goto out;
1261                         }
1262                         /*
1263                          * For SLI2, we need to set port_state to READY
1264                          * and continue discovery.
1265                          */
1266                         if (vport->port_state < LPFC_VPORT_READY) {
1267                                 /* If we get here, there is nothing to ADISC */
1268                                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1269                                         lpfc_issue_clear_la(phba, vport);
1270
1271                                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1272                                         vport->num_disc_nodes = 0;
1273                                         /* go thru NPR list, issue ELS PLOGIs */
1274                                         if (vport->fc_npr_cnt)
1275                                                 lpfc_els_disc_plogi(vport);
1276
1277                                         if (!vport->num_disc_nodes) {
1278                                                 spin_lock_irq(shost->host_lock);
1279                                                 vport->fc_flag &=
1280                                                         ~FC_NDISC_ACTIVE;
1281                                                 spin_unlock_irq(
1282                                                         shost->host_lock);
1283                                                 lpfc_can_disctmo(vport);
1284                                         }
1285                                 }
1286                                 vport->port_state = LPFC_VPORT_READY;
1287                         } else {
1288                                 lpfc_rscn_disc(vport);
1289                         }
1290                 }
1291         }
1292 out:
1293         lpfc_els_free_iocb(phba, cmdiocb);
1294         return;
1295 }
1296
1297 int
1298 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1299                      uint8_t retry)
1300 {
1301         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1302         struct lpfc_hba  *phba = vport->phba;
1303         ADISC *ap;
1304         IOCB_t *icmd;
1305         struct lpfc_iocbq *elsiocb;
1306         struct lpfc_sli *psli = &phba->sli;
1307         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1308         uint8_t *pcmd;
1309         uint16_t cmdsize;
1310
1311         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1312         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1313                                      ndlp->nlp_DID, ELS_CMD_ADISC);
1314         if (!elsiocb)
1315                 return 1;
1316
1317         icmd = &elsiocb->iocb;
1318         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1319
1320         /* For ADISC request, remainder of payload is service parameters */
1321         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1322         pcmd += sizeof(uint32_t);
1323
1324         /* Fill in ADISC payload */
1325         ap = (ADISC *) pcmd;
1326         ap->hardAL_PA = phba->fc_pref_ALPA;
1327         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1328         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1329         ap->DID = be32_to_cpu(vport->fc_myDID);
1330
1331         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1332                 "Issue ADISC:     did:x%x",
1333                 ndlp->nlp_DID, 0, 0);
1334
1335         phba->fc_stat.elsXmitADISC++;
1336         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1337         spin_lock_irq(shost->host_lock);
1338         ndlp->nlp_flag |= NLP_ADISC_SND;
1339         spin_unlock_irq(shost->host_lock);
1340         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1341                 spin_lock_irq(shost->host_lock);
1342                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1343                 spin_unlock_irq(shost->host_lock);
1344                 lpfc_els_free_iocb(phba, elsiocb);
1345                 return 1;
1346         }
1347         return 0;
1348 }
1349
1350 static void
1351 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1352                    struct lpfc_iocbq *rspiocb)
1353 {
1354         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1355         struct lpfc_vport *vport = ndlp->vport;
1356         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1357         IOCB_t *irsp;
1358         struct lpfc_sli *psli;
1359
1360         psli = &phba->sli;
1361         /* we pass cmdiocb to state machine which needs rspiocb as well */
1362         cmdiocb->context_un.rsp_iocb = rspiocb;
1363
1364         irsp = &(rspiocb->iocb);
1365         spin_lock_irq(shost->host_lock);
1366         ndlp->nlp_flag &= ~NLP_LOGO_SND;
1367         spin_unlock_irq(shost->host_lock);
1368
1369         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1370                 "LOGO cmpl:       status:x%x/x%x did:x%x",
1371                 irsp->ulpStatus, irsp->un.ulpWord[4],
1372                 ndlp->nlp_DID);
1373         /* LOGO completes to NPort <nlp_DID> */
1374         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1375                          "0105 LOGO completes to NPort x%x "
1376                          "Data: x%x x%x x%x x%x\n",
1377                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1378                          irsp->ulpTimeout, vport->num_disc_nodes);
1379         /* Check to see if link went down during discovery */
1380         if (lpfc_els_chk_latt(vport))
1381                 goto out;
1382
1383         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1384                 /* NLP_EVT_DEVICE_RM should unregister the RPI
1385                  * which should abort all outstanding IOs.
1386                  */
1387                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1388                                         NLP_EVT_DEVICE_RM);
1389                 goto out;
1390         }
1391
1392         if (irsp->ulpStatus) {
1393                 /* Check for retry */
1394                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1395                         /* ELS command is being retried */
1396                         goto out;
1397                 /* LOGO failed */
1398                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1399                 if (lpfc_error_lost_link(irsp))
1400                         goto out;
1401                 else
1402                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1403                                                 NLP_EVT_CMPL_LOGO);
1404         } else {
1405                 /* Good status, call state machine.
1406                  * This will unregister the rpi if needed.
1407                  */
1408                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1409                                         NLP_EVT_CMPL_LOGO);
1410         }
1411
1412 out:
1413         lpfc_els_free_iocb(phba, cmdiocb);
1414         return;
1415 }
1416
1417 int
1418 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1419                     uint8_t retry)
1420 {
1421         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1422         struct lpfc_hba  *phba = vport->phba;
1423         IOCB_t *icmd;
1424         struct lpfc_iocbq *elsiocb;
1425         struct lpfc_sli_ring *pring;
1426         struct lpfc_sli *psli;
1427         uint8_t *pcmd;
1428         uint16_t cmdsize;
1429         int rc;
1430
1431         psli = &phba->sli;
1432         pring = &psli->ring[LPFC_ELS_RING];
1433
1434         spin_lock_irq(shost->host_lock);
1435         if (ndlp->nlp_flag & NLP_LOGO_SND) {
1436                 spin_unlock_irq(shost->host_lock);
1437                 return 0;
1438         }
1439         spin_unlock_irq(shost->host_lock);
1440
1441         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
1442         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1443                                      ndlp->nlp_DID, ELS_CMD_LOGO);
1444         if (!elsiocb)
1445                 return 1;
1446
1447         icmd = &elsiocb->iocb;
1448         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1449         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1450         pcmd += sizeof(uint32_t);
1451
1452         /* Fill in LOGO payload */
1453         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1454         pcmd += sizeof(uint32_t);
1455         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
1456
1457         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1458                 "Issue LOGO:      did:x%x",
1459                 ndlp->nlp_DID, 0, 0);
1460
1461         phba->fc_stat.elsXmitLOGO++;
1462         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1463         spin_lock_irq(shost->host_lock);
1464         ndlp->nlp_flag |= NLP_LOGO_SND;
1465         spin_unlock_irq(shost->host_lock);
1466         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1467
1468         if (rc == IOCB_ERROR) {
1469                 spin_lock_irq(shost->host_lock);
1470                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1471                 spin_unlock_irq(shost->host_lock);
1472                 lpfc_els_free_iocb(phba, elsiocb);
1473                 return 1;
1474         }
1475         return 0;
1476 }
1477
1478 static void
1479 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1480                   struct lpfc_iocbq *rspiocb)
1481 {
1482         struct lpfc_vport *vport = cmdiocb->vport;
1483         IOCB_t *irsp;
1484
1485         irsp = &rspiocb->iocb;
1486
1487         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1488                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
1489                 irsp->ulpStatus, irsp->un.ulpWord[4],
1490                 irsp->un.elsreq64.remoteID);
1491         /* ELS cmd tag <ulpIoTag> completes */
1492         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1493                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
1494                          irsp->ulpIoTag, irsp->ulpStatus,
1495                          irsp->un.ulpWord[4], irsp->ulpTimeout);
1496         /* Check to see if link went down during discovery */
1497         lpfc_els_chk_latt(vport);
1498         lpfc_els_free_iocb(phba, cmdiocb);
1499         return;
1500 }
1501
1502 int
1503 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
1504 {
1505         struct lpfc_hba  *phba = vport->phba;
1506         IOCB_t *icmd;
1507         struct lpfc_iocbq *elsiocb;
1508         struct lpfc_sli_ring *pring;
1509         struct lpfc_sli *psli;
1510         uint8_t *pcmd;
1511         uint16_t cmdsize;
1512         struct lpfc_nodelist *ndlp;
1513
1514         psli = &phba->sli;
1515         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1516         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
1517         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1518         if (!ndlp)
1519                 return 1;
1520
1521         lpfc_nlp_init(vport, ndlp, nportid);
1522
1523         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1524                                      ndlp->nlp_DID, ELS_CMD_SCR);
1525
1526         if (!elsiocb) {
1527                 lpfc_nlp_put(ndlp);
1528                 return 1;
1529         }
1530
1531         icmd = &elsiocb->iocb;
1532         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1533
1534         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
1535         pcmd += sizeof(uint32_t);
1536
1537         /* For SCR, remainder of payload is SCR parameter page */
1538         memset(pcmd, 0, sizeof(SCR));
1539         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
1540
1541         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1542                 "Issue SCR:       did:x%x",
1543                 ndlp->nlp_DID, 0, 0);
1544
1545         phba->fc_stat.elsXmitSCR++;
1546         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
1547         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1548                 lpfc_nlp_put(ndlp);
1549                 lpfc_els_free_iocb(phba, elsiocb);
1550                 return 1;
1551         }
1552         lpfc_nlp_put(ndlp);
1553         return 0;
1554 }
1555
1556 static int
1557 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
1558 {
1559         struct lpfc_hba  *phba = vport->phba;
1560         IOCB_t *icmd;
1561         struct lpfc_iocbq *elsiocb;
1562         struct lpfc_sli_ring *pring;
1563         struct lpfc_sli *psli;
1564         FARP *fp;
1565         uint8_t *pcmd;
1566         uint32_t *lp;
1567         uint16_t cmdsize;
1568         struct lpfc_nodelist *ondlp;
1569         struct lpfc_nodelist *ndlp;
1570
1571         psli = &phba->sli;
1572         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1573         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
1574         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1575         if (!ndlp)
1576                 return 1;
1577
1578         lpfc_nlp_init(vport, ndlp, nportid);
1579
1580         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1581                                      ndlp->nlp_DID, ELS_CMD_RNID);
1582         if (!elsiocb) {
1583                 lpfc_nlp_put(ndlp);
1584                 return 1;
1585         }
1586
1587         icmd = &elsiocb->iocb;
1588         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1589
1590         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
1591         pcmd += sizeof(uint32_t);
1592
1593         /* Fill in FARPR payload */
1594         fp = (FARP *) (pcmd);
1595         memset(fp, 0, sizeof(FARP));
1596         lp = (uint32_t *) pcmd;
1597         *lp++ = be32_to_cpu(nportid);
1598         *lp++ = be32_to_cpu(vport->fc_myDID);
1599         fp->Rflags = 0;
1600         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
1601
1602         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
1603         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1604         ondlp = lpfc_findnode_did(vport, nportid);
1605         if (ondlp) {
1606                 memcpy(&fp->OportName, &ondlp->nlp_portname,
1607                        sizeof(struct lpfc_name));
1608                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
1609                        sizeof(struct lpfc_name));
1610         }
1611
1612         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1613                 "Issue FARPR:     did:x%x",
1614                 ndlp->nlp_DID, 0, 0);
1615
1616         phba->fc_stat.elsXmitFARPR++;
1617         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
1618         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1619                 lpfc_nlp_put(ndlp);
1620                 lpfc_els_free_iocb(phba, elsiocb);
1621                 return 1;
1622         }
1623         lpfc_nlp_put(ndlp);
1624         return 0;
1625 }
1626
1627 void
1628 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
1629 {
1630         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1631
1632         spin_lock_irq(shost->host_lock);
1633         nlp->nlp_flag &= ~NLP_DELAY_TMO;
1634         spin_unlock_irq(shost->host_lock);
1635         del_timer_sync(&nlp->nlp_delayfunc);
1636         nlp->nlp_last_elscmd = 0;
1637
1638         if (!list_empty(&nlp->els_retry_evt.evt_listp))
1639                 list_del_init(&nlp->els_retry_evt.evt_listp);
1640
1641         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
1642                 spin_lock_irq(shost->host_lock);
1643                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1644                 spin_unlock_irq(shost->host_lock);
1645                 if (vport->num_disc_nodes) {
1646                         /* Check to see if there are more
1647                          * PLOGIs to be sent
1648                          */
1649                         lpfc_more_plogi(vport);
1650
1651                         if (vport->num_disc_nodes == 0) {
1652                                 spin_lock_irq(shost->host_lock);
1653                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1654                                 spin_unlock_irq(shost->host_lock);
1655                                 lpfc_can_disctmo(vport);
1656                                 lpfc_end_rscn(vport);
1657                         }
1658                 }
1659         }
1660         return;
1661 }
1662
1663 void
1664 lpfc_els_retry_delay(unsigned long ptr)
1665 {
1666         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
1667         struct lpfc_vport *vport = ndlp->vport;
1668         struct lpfc_hba   *phba = vport->phba;
1669         unsigned long flags;
1670         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
1671
1672         ndlp = (struct lpfc_nodelist *) ptr;
1673         phba = ndlp->vport->phba;
1674         evtp = &ndlp->els_retry_evt;
1675
1676         spin_lock_irqsave(&phba->hbalock, flags);
1677         if (!list_empty(&evtp->evt_listp)) {
1678                 spin_unlock_irqrestore(&phba->hbalock, flags);
1679                 return;
1680         }
1681
1682         evtp->evt_arg1  = ndlp;
1683         evtp->evt       = LPFC_EVT_ELS_RETRY;
1684         list_add_tail(&evtp->evt_listp, &phba->work_list);
1685         if (phba->work_wait)
1686                 lpfc_worker_wake_up(phba);
1687
1688         spin_unlock_irqrestore(&phba->hbalock, flags);
1689         return;
1690 }
1691
1692 void
1693 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
1694 {
1695         struct lpfc_vport *vport = ndlp->vport;
1696         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1697         uint32_t cmd, did, retry;
1698
1699         spin_lock_irq(shost->host_lock);
1700         did = ndlp->nlp_DID;
1701         cmd = ndlp->nlp_last_elscmd;
1702         ndlp->nlp_last_elscmd = 0;
1703
1704         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1705                 spin_unlock_irq(shost->host_lock);
1706                 return;
1707         }
1708
1709         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1710         spin_unlock_irq(shost->host_lock);
1711         /*
1712          * If a discovery event readded nlp_delayfunc after timer
1713          * firing and before processing the timer, cancel the
1714          * nlp_delayfunc.
1715          */
1716         del_timer_sync(&ndlp->nlp_delayfunc);
1717         retry = ndlp->nlp_retry;
1718
1719         switch (cmd) {
1720         case ELS_CMD_FLOGI:
1721                 lpfc_issue_els_flogi(vport, ndlp, retry);
1722                 break;
1723         case ELS_CMD_PLOGI:
1724                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
1725                         ndlp->nlp_prev_state = ndlp->nlp_state;
1726                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1727                 }
1728                 break;
1729         case ELS_CMD_ADISC:
1730                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
1731                         ndlp->nlp_prev_state = ndlp->nlp_state;
1732                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
1733                 }
1734                 break;
1735         case ELS_CMD_PRLI:
1736                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
1737                         ndlp->nlp_prev_state = ndlp->nlp_state;
1738                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1739                 }
1740                 break;
1741         case ELS_CMD_LOGO:
1742                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
1743                         ndlp->nlp_prev_state = ndlp->nlp_state;
1744                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1745                 }
1746                 break;
1747         case ELS_CMD_FDISC:
1748                 lpfc_issue_els_fdisc(vport, ndlp, retry);
1749                 break;
1750         }
1751         return;
1752 }
1753
1754 static int
1755 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1756                struct lpfc_iocbq *rspiocb)
1757 {
1758         struct lpfc_vport *vport = cmdiocb->vport;
1759         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1760         IOCB_t *irsp = &rspiocb->iocb;
1761         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1762         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1763         uint32_t *elscmd;
1764         struct ls_rjt stat;
1765         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
1766         int logerr = 0;
1767         uint32_t cmd = 0;
1768         uint32_t did;
1769
1770
1771         /* Note: context2 may be 0 for internal driver abort
1772          * of delays ELS command.
1773          */
1774
1775         if (pcmd && pcmd->virt) {
1776                 elscmd = (uint32_t *) (pcmd->virt);
1777                 cmd = *elscmd++;
1778         }
1779
1780         if (ndlp)
1781                 did = ndlp->nlp_DID;
1782         else {
1783                 /* We should only hit this case for retrying PLOGI */
1784                 did = irsp->un.elsreq64.remoteID;
1785                 ndlp = lpfc_findnode_did(vport, did);
1786                 if (!ndlp && (cmd != ELS_CMD_PLOGI))
1787                         return 1;
1788         }
1789
1790         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1791                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
1792                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
1793
1794         switch (irsp->ulpStatus) {
1795         case IOSTAT_FCP_RSP_ERROR:
1796         case IOSTAT_REMOTE_STOP:
1797                 break;
1798
1799         case IOSTAT_LOCAL_REJECT:
1800                 switch ((irsp->un.ulpWord[4] & 0xff)) {
1801                 case IOERR_LOOP_OPEN_FAILURE:
1802                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
1803                                 delay = 1000;
1804                         retry = 1;
1805                         break;
1806
1807                 case IOERR_ILLEGAL_COMMAND:
1808                         if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) &&
1809                             (cmd == ELS_CMD_FDISC)) {
1810                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1811                                                  "0124 FDISC failed (3/6) "
1812                                                  "retrying...\n");
1813                                 lpfc_mbx_unreg_vpi(vport);
1814                                 retry = 1;
1815                                 /* FDISC retry policy */
1816                                 maxretry = 48;
1817                                 if (cmdiocb->retry >= 32)
1818                                         delay = 1000;
1819                         }
1820                         break;
1821
1822                 case IOERR_NO_RESOURCES:
1823                         logerr = 1; /* HBA out of resources */
1824                         retry = 1;
1825                         if (cmdiocb->retry > 100)
1826                                 delay = 100;
1827                         maxretry = 250;
1828                         break;
1829
1830                 case IOERR_ILLEGAL_FRAME:
1831                         delay = 100;
1832                         retry = 1;
1833                         break;
1834
1835                 case IOERR_SEQUENCE_TIMEOUT:
1836                 case IOERR_INVALID_RPI:
1837                         retry = 1;
1838                         break;
1839                 }
1840                 break;
1841
1842         case IOSTAT_NPORT_RJT:
1843         case IOSTAT_FABRIC_RJT:
1844                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
1845                         retry = 1;
1846                         break;
1847                 }
1848                 break;
1849
1850         case IOSTAT_NPORT_BSY:
1851         case IOSTAT_FABRIC_BSY:
1852                 logerr = 1; /* Fabric / Remote NPort out of resources */
1853                 retry = 1;
1854                 break;
1855
1856         case IOSTAT_LS_RJT:
1857                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
1858                 /* Added for Vendor specifc support
1859                  * Just keep retrying for these Rsn / Exp codes
1860                  */
1861                 switch (stat.un.b.lsRjtRsnCode) {
1862                 case LSRJT_UNABLE_TPC:
1863                         if (stat.un.b.lsRjtRsnCodeExp ==
1864                             LSEXP_CMD_IN_PROGRESS) {
1865                                 if (cmd == ELS_CMD_PLOGI) {
1866                                         delay = 1000;
1867                                         maxretry = 48;
1868                                 }
1869                                 retry = 1;
1870                                 break;
1871                         }
1872                         if (cmd == ELS_CMD_PLOGI) {
1873                                 delay = 1000;
1874                                 maxretry = lpfc_max_els_tries + 1;
1875                                 retry = 1;
1876                                 break;
1877                         }
1878                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1879                           (cmd == ELS_CMD_FDISC) &&
1880                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
1881                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1882                                                  "0125 FDISC Failed (x%x). "
1883                                                  "Fabric out of resources\n",
1884                                                  stat.un.lsRjtError);
1885                                 lpfc_vport_set_state(vport,
1886                                                      FC_VPORT_NO_FABRIC_RSCS);
1887                         }
1888                         break;
1889
1890                 case LSRJT_LOGICAL_BSY:
1891                         if ((cmd == ELS_CMD_PLOGI) ||
1892                             (cmd == ELS_CMD_PRLI)) {
1893                                 delay = 1000;
1894                                 maxretry = 48;
1895                         } else if (cmd == ELS_CMD_FDISC) {
1896                                 /* FDISC retry policy */
1897                                 maxretry = 48;
1898                                 if (cmdiocb->retry >= 32)
1899                                         delay = 1000;
1900                         }
1901                         retry = 1;
1902                         break;
1903
1904                 case LSRJT_LOGICAL_ERR:
1905                 case LSRJT_PROTOCOL_ERR:
1906                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1907                           (cmd == ELS_CMD_FDISC) &&
1908                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
1909                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
1910                           ) {
1911                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1912                                                  "0123 FDISC Failed (x%x). "
1913                                                  "Fabric Detected Bad WWN\n",
1914                                                  stat.un.lsRjtError);
1915                                 lpfc_vport_set_state(vport,
1916                                                      FC_VPORT_FABRIC_REJ_WWN);
1917                         }
1918                         break;
1919                 }
1920                 break;
1921
1922         case IOSTAT_INTERMED_RSP:
1923         case IOSTAT_BA_RJT:
1924                 break;
1925
1926         default:
1927                 break;
1928         }
1929
1930         if (did == FDMI_DID)
1931                 retry = 1;
1932
1933         if ((cmd == ELS_CMD_FLOGI) &&
1934             (phba->fc_topology != TOPOLOGY_LOOP)) {
1935                 /* FLOGI retry policy */
1936                 retry = 1;
1937                 maxretry = 48;
1938                 if (cmdiocb->retry >= 32)
1939                         delay = 1000;
1940         }
1941
1942         if ((++cmdiocb->retry) >= maxretry) {
1943                 phba->fc_stat.elsRetryExceeded++;
1944                 retry = 0;
1945         }
1946
1947         if ((vport->load_flag & FC_UNLOADING) != 0)
1948                 retry = 0;
1949
1950         if (retry) {
1951
1952                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
1953                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1954                                  "0107 Retry ELS command x%x to remote "
1955                                  "NPORT x%x Data: x%x x%x\n",
1956                                  cmd, did, cmdiocb->retry, delay);
1957
1958                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
1959                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1960                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
1961                         /* Don't reset timer for no resources */
1962
1963                         /* If discovery / RSCN timer is running, reset it */
1964                         if (timer_pending(&vport->fc_disctmo) ||
1965                             (vport->fc_flag & FC_RSCN_MODE))
1966                                 lpfc_set_disctmo(vport);
1967                 }
1968
1969                 phba->fc_stat.elsXmitRetry++;
1970                 if (ndlp && delay) {
1971                         phba->fc_stat.elsDelayRetry++;
1972                         ndlp->nlp_retry = cmdiocb->retry;
1973
1974                         /* delay is specified in milliseconds */
1975                         mod_timer(&ndlp->nlp_delayfunc,
1976                                 jiffies + msecs_to_jiffies(delay));
1977                         spin_lock_irq(shost->host_lock);
1978                         ndlp->nlp_flag |= NLP_DELAY_TMO;
1979                         spin_unlock_irq(shost->host_lock);
1980
1981                         ndlp->nlp_prev_state = ndlp->nlp_state;
1982                         if (cmd == ELS_CMD_PRLI)
1983                                 lpfc_nlp_set_state(vport, ndlp,
1984                                         NLP_STE_REG_LOGIN_ISSUE);
1985                         else
1986                                 lpfc_nlp_set_state(vport, ndlp,
1987                                         NLP_STE_NPR_NODE);
1988                         ndlp->nlp_last_elscmd = cmd;
1989
1990                         return 1;
1991                 }
1992                 switch (cmd) {
1993                 case ELS_CMD_FLOGI:
1994                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
1995                         return 1;
1996                 case ELS_CMD_FDISC:
1997                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
1998                         return 1;
1999                 case ELS_CMD_PLOGI:
2000                         if (ndlp) {
2001                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2002                                 lpfc_nlp_set_state(vport, ndlp,
2003                                                    NLP_STE_PLOGI_ISSUE);
2004                         }
2005                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2006                         return 1;
2007                 case ELS_CMD_ADISC:
2008                         ndlp->nlp_prev_state = ndlp->nlp_state;
2009                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2010                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2011                         return 1;
2012                 case ELS_CMD_PRLI:
2013                         ndlp->nlp_prev_state = ndlp->nlp_state;
2014                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2015                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2016                         return 1;
2017                 case ELS_CMD_LOGO:
2018                         ndlp->nlp_prev_state = ndlp->nlp_state;
2019                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2020                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2021                         return 1;
2022                 }
2023         }
2024         /* No retry ELS command <elsCmd> to remote NPORT <did> */
2025         if (logerr) {
2026                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2027                          "0137 No retry ELS command x%x to remote "
2028                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2029                          cmd, did, irsp->ulpStatus,
2030                          irsp->un.ulpWord[4]);
2031         }
2032         else {
2033                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2034                          "0108 No retry ELS command x%x to remote "
2035                          "NPORT x%x Retried:%d Error:x%x/%x\n",
2036                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
2037                          irsp->un.ulpWord[4]);
2038         }
2039         return 0;
2040 }
2041
2042 int
2043 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2044 {
2045         struct lpfc_dmabuf *buf_ptr;
2046
2047         /* Free the response before processing the command.  */
2048         if (!list_empty(&buf_ptr1->list)) {
2049                 list_remove_head(&buf_ptr1->list, buf_ptr,
2050                                  struct lpfc_dmabuf,
2051                                  list);
2052                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2053                 kfree(buf_ptr);
2054         }
2055         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2056         kfree(buf_ptr1);
2057         return 0;
2058 }
2059
2060 int
2061 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
2062 {
2063         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2064         kfree(buf_ptr);
2065         return 0;
2066 }
2067
2068 int
2069 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2070 {
2071         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2072
2073         if (elsiocb->context1) {
2074                 lpfc_nlp_put(elsiocb->context1);
2075                 elsiocb->context1 = NULL;
2076         }
2077         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
2078         if (elsiocb->context2) {
2079                 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2080                 lpfc_els_free_data(phba, buf_ptr1);
2081         }
2082
2083         if (elsiocb->context3) {
2084                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2085                 lpfc_els_free_bpl(phba, buf_ptr);
2086         }
2087         lpfc_sli_release_iocbq(phba, elsiocb);
2088         return 0;
2089 }
2090
2091 static void
2092 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2093                        struct lpfc_iocbq *rspiocb)
2094 {
2095         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2096         struct lpfc_vport *vport = cmdiocb->vport;
2097         IOCB_t *irsp;
2098
2099         irsp = &rspiocb->iocb;
2100         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2101                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2102                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2103         /* ACC to LOGO completes to NPort <nlp_DID> */
2104         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2105                          "0109 ACC to LOGO completes to NPort x%x "
2106                          "Data: x%x x%x x%x\n",
2107                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2108                          ndlp->nlp_rpi);
2109
2110         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
2111                 /* NPort Recovery mode or node is just allocated */
2112                 if (!lpfc_nlp_not_used(ndlp)) {
2113                         /* If the ndlp is being used by another discovery
2114                          * thread, just unregister the RPI.
2115                          */
2116                         lpfc_unreg_rpi(vport, ndlp);
2117                 }
2118         }
2119         lpfc_els_free_iocb(phba, cmdiocb);
2120         return;
2121 }
2122
2123 void
2124 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2125 {
2126         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2127         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2128
2129         pmb->context1 = NULL;
2130         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2131         kfree(mp);
2132         mempool_free(pmb, phba->mbox_mem_pool);
2133         lpfc_nlp_put(ndlp);
2134
2135         /* This is the end of the default RPI cleanup logic for this
2136          * ndlp. If no other discovery threads are using this ndlp.
2137          * we should free all resources associated with it.
2138          */
2139         lpfc_nlp_not_used(ndlp);
2140         return;
2141 }
2142
2143 static void
2144 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2145                   struct lpfc_iocbq *rspiocb)
2146 {
2147         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2148         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2149         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2150         IOCB_t  *irsp;
2151         uint8_t *pcmd;
2152         LPFC_MBOXQ_t *mbox = NULL;
2153         struct lpfc_dmabuf *mp = NULL;
2154         uint32_t ls_rjt = 0;
2155
2156         irsp = &rspiocb->iocb;
2157
2158         if (cmdiocb->context_un.mbox)
2159                 mbox = cmdiocb->context_un.mbox;
2160
2161         /* First determine if this is a LS_RJT cmpl */
2162         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
2163         if (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT) {
2164                 /* A LS_RJT associated with Default RPI cleanup
2165                  * has its own seperate code path.
2166                  */
2167                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2168                         ls_rjt = 1;
2169         }
2170
2171         /* Check to see if link went down during discovery */
2172         if (!ndlp || lpfc_els_chk_latt(vport)) {
2173                 if (mbox) {
2174                         mp = (struct lpfc_dmabuf *) mbox->context1;
2175                         if (mp) {
2176                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2177                                 kfree(mp);
2178                         }
2179                         mempool_free(mbox, phba->mbox_mem_pool);
2180                 }
2181                 if (ndlp && (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2182                         if (lpfc_nlp_not_used(ndlp))
2183                                 ndlp = NULL;
2184                 goto out;
2185         }
2186
2187         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2188                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
2189                 irsp->ulpStatus, irsp->un.ulpWord[4],
2190                 cmdiocb->iocb.un.elsreq64.remoteID);
2191         /* ELS response tag <ulpIoTag> completes */
2192         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2193                          "0110 ELS response tag x%x completes "
2194                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
2195                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
2196                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
2197                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2198                          ndlp->nlp_rpi);
2199         if (mbox) {
2200                 if ((rspiocb->iocb.ulpStatus == 0)
2201                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
2202                         lpfc_unreg_rpi(vport, ndlp);
2203                         mbox->context2 = lpfc_nlp_get(ndlp);
2204                         mbox->vport = vport;
2205                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
2206                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2207                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2208                         }
2209                         else {
2210                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
2211                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2212                                 lpfc_nlp_set_state(vport, ndlp,
2213                                            NLP_STE_REG_LOGIN_ISSUE);
2214                         }
2215                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
2216                             != MBX_NOT_FINISHED) {
2217                                 goto out;
2218                         }
2219
2220                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
2221                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2222                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
2223                                 "Data: x%x x%x x%x\n",
2224                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2225                                 ndlp->nlp_rpi);
2226
2227                         if (lpfc_nlp_not_used(ndlp))
2228                                 ndlp = NULL;
2229                 } else {
2230                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
2231                         if (!lpfc_error_lost_link(irsp) &&
2232                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
2233                                 if (lpfc_nlp_not_used(ndlp))
2234                                         ndlp = NULL;
2235                         }
2236                 }
2237                 mp = (struct lpfc_dmabuf *) mbox->context1;
2238                 if (mp) {
2239                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2240                         kfree(mp);
2241                 }
2242                 mempool_free(mbox, phba->mbox_mem_pool);
2243         }
2244 out:
2245         if (ndlp) {
2246                 spin_lock_irq(shost->host_lock);
2247                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
2248                 spin_unlock_irq(shost->host_lock);
2249
2250                 /* If the node is not being used by another discovery thread,
2251                  * and we are sending a reject, we are done with it.
2252                  * Release driver reference count here and free associated
2253                  * resources.
2254                  */
2255                 if (ls_rjt)
2256                         lpfc_nlp_not_used(ndlp);
2257         }
2258
2259         lpfc_els_free_iocb(phba, cmdiocb);
2260         return;
2261 }
2262
2263 int
2264 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
2265                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2266                  LPFC_MBOXQ_t *mbox)
2267 {
2268         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2269         struct lpfc_hba  *phba = vport->phba;
2270         IOCB_t *icmd;
2271         IOCB_t *oldcmd;
2272         struct lpfc_iocbq *elsiocb;
2273         struct lpfc_sli_ring *pring;
2274         struct lpfc_sli *psli;
2275         uint8_t *pcmd;
2276         uint16_t cmdsize;
2277         int rc;
2278         ELS_PKT *els_pkt_ptr;
2279
2280         psli = &phba->sli;
2281         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2282         oldcmd = &oldiocb->iocb;
2283
2284         switch (flag) {
2285         case ELS_CMD_ACC:
2286                 cmdsize = sizeof(uint32_t);
2287                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2288                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2289                 if (!elsiocb) {
2290                         spin_lock_irq(shost->host_lock);
2291                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2292                         spin_unlock_irq(shost->host_lock);
2293                         return 1;
2294                 }
2295
2296                 icmd = &elsiocb->iocb;
2297                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2298                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2299                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2300                 pcmd += sizeof(uint32_t);
2301
2302                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2303                         "Issue ACC:       did:x%x flg:x%x",
2304                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
2305                 break;
2306         case ELS_CMD_PLOGI:
2307                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
2308                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2309                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2310                 if (!elsiocb)
2311                         return 1;
2312
2313                 icmd = &elsiocb->iocb;
2314                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2315                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2316
2317                 if (mbox)
2318                         elsiocb->context_un.mbox = mbox;
2319
2320                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2321                 pcmd += sizeof(uint32_t);
2322                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2323
2324                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2325                         "Issue ACC PLOGI: did:x%x flg:x%x",
2326                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
2327                 break;
2328         case ELS_CMD_PRLO:
2329                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
2330                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2331                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
2332                 if (!elsiocb)
2333                         return 1;
2334
2335                 icmd = &elsiocb->iocb;
2336                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2337                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2338
2339                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
2340                        sizeof(uint32_t) + sizeof(PRLO));
2341                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
2342                 els_pkt_ptr = (ELS_PKT *) pcmd;
2343                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
2344
2345                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2346                         "Issue ACC PRLO:  did:x%x flg:x%x",
2347                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
2348                 break;
2349         default:
2350                 return 1;
2351         }
2352         /* Xmit ELS ACC response tag <ulpIoTag> */
2353         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2354                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
2355                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
2356                          elsiocb->iotag, elsiocb->iocb.ulpContext,
2357                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2358                          ndlp->nlp_rpi);
2359         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2360                 spin_lock_irq(shost->host_lock);
2361                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2362                 spin_unlock_irq(shost->host_lock);
2363                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
2364         } else {
2365                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2366         }
2367
2368         phba->fc_stat.elsXmitACC++;
2369         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2370         if (rc == IOCB_ERROR) {
2371                 lpfc_els_free_iocb(phba, elsiocb);
2372                 return 1;
2373         }
2374         return 0;
2375 }
2376
2377 int
2378 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
2379                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2380                     LPFC_MBOXQ_t *mbox)
2381 {
2382         struct lpfc_hba  *phba = vport->phba;
2383         IOCB_t *icmd;
2384         IOCB_t *oldcmd;
2385         struct lpfc_iocbq *elsiocb;
2386         struct lpfc_sli_ring *pring;
2387         struct lpfc_sli *psli;
2388         uint8_t *pcmd;
2389         uint16_t cmdsize;
2390         int rc;
2391
2392         psli = &phba->sli;
2393         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2394
2395         cmdsize = 2 * sizeof(uint32_t);
2396         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2397                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
2398         if (!elsiocb)
2399                 return 1;
2400
2401         icmd = &elsiocb->iocb;
2402         oldcmd = &oldiocb->iocb;
2403         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2404         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2405
2406         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
2407         pcmd += sizeof(uint32_t);
2408         *((uint32_t *) (pcmd)) = rejectError;
2409
2410         if (mbox)
2411                 elsiocb->context_un.mbox = mbox;
2412
2413         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
2414         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2415                          "0129 Xmit ELS RJT x%x response tag x%x "
2416                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
2417                          "rpi x%x\n",
2418                          rejectError, elsiocb->iotag,
2419                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
2420                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
2421         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2422                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
2423                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
2424
2425         phba->fc_stat.elsXmitLSRJT++;
2426         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2427         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2428
2429         if (rc == IOCB_ERROR) {
2430                 lpfc_els_free_iocb(phba, elsiocb);
2431                 return 1;
2432         }
2433         return 0;
2434 }
2435
2436 int
2437 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2438                        struct lpfc_nodelist *ndlp)
2439 {
2440         struct lpfc_hba  *phba = vport->phba;
2441         struct lpfc_sli  *psli = &phba->sli;
2442         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2443         ADISC *ap;
2444         IOCB_t *icmd, *oldcmd;
2445         struct lpfc_iocbq *elsiocb;
2446         uint8_t *pcmd;
2447         uint16_t cmdsize;
2448         int rc;
2449
2450         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
2451         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2452                                      ndlp->nlp_DID, ELS_CMD_ACC);
2453         if (!elsiocb)
2454                 return 1;
2455
2456         icmd = &elsiocb->iocb;
2457         oldcmd = &oldiocb->iocb;
2458         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2459
2460         /* Xmit ADISC ACC response tag <ulpIoTag> */
2461         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2462                          "0130 Xmit ADISC ACC response iotag x%x xri: "
2463                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
2464                          elsiocb->iotag, elsiocb->iocb.ulpContext,
2465                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2466                          ndlp->nlp_rpi);
2467         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2468
2469         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2470         pcmd += sizeof(uint32_t);
2471
2472         ap = (ADISC *) (pcmd);
2473         ap->hardAL_PA = phba->fc_pref_ALPA;
2474         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2475         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2476         ap->DID = be32_to_cpu(vport->fc_myDID);
2477
2478         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2479                 "Issue ACC ADISC: did:x%x flg:x%x",
2480                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2481
2482         phba->fc_stat.elsXmitACC++;
2483         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2484         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2485         if (rc == IOCB_ERROR) {
2486                 lpfc_els_free_iocb(phba, elsiocb);
2487                 return 1;
2488         }
2489         return 0;
2490 }
2491
2492 int
2493 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2494                       struct lpfc_nodelist *ndlp)
2495 {
2496         struct lpfc_hba  *phba = vport->phba;
2497         PRLI *npr;
2498         lpfc_vpd_t *vpd;
2499         IOCB_t *icmd;
2500         IOCB_t *oldcmd;
2501         struct lpfc_iocbq *elsiocb;
2502         struct lpfc_sli_ring *pring;
2503         struct lpfc_sli *psli;
2504         uint8_t *pcmd;
2505         uint16_t cmdsize;
2506         int rc;
2507
2508         psli = &phba->sli;
2509         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2510
2511         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
2512         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2513                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2514         if (!elsiocb)
2515                 return 1;
2516
2517         icmd = &elsiocb->iocb;
2518         oldcmd = &oldiocb->iocb;
2519         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2520         /* Xmit PRLI ACC response tag <ulpIoTag> */
2521         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2522                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
2523                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
2524                          elsiocb->iotag, elsiocb->iocb.ulpContext,
2525                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2526                          ndlp->nlp_rpi);
2527         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2528
2529         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2530         pcmd += sizeof(uint32_t);
2531
2532         /* For PRLI, remainder of payload is PRLI parameter page */
2533         memset(pcmd, 0, sizeof(PRLI));
2534
2535         npr = (PRLI *) pcmd;
2536         vpd = &phba->vpd;
2537         /*
2538          * If our firmware version is 3.20 or later,
2539          * set the following bits for FC-TAPE support.
2540          */
2541         if (vpd->rev.feaLevelHigh >= 0x02) {
2542                 npr->ConfmComplAllowed = 1;
2543                 npr->Retry = 1;
2544                 npr->TaskRetryIdReq = 1;
2545         }
2546
2547         npr->acceptRspCode = PRLI_REQ_EXECUTED;
2548         npr->estabImagePair = 1;
2549         npr->readXferRdyDis = 1;
2550         npr->ConfmComplAllowed = 1;
2551
2552         npr->prliType = PRLI_FCP_TYPE;
2553         npr->initiatorFunc = 1;
2554
2555         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2556                 "Issue ACC PRLI:  did:x%x flg:x%x",
2557                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2558
2559         phba->fc_stat.elsXmitACC++;
2560         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2561
2562         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2563         if (rc == IOCB_ERROR) {
2564                 lpfc_els_free_iocb(phba, elsiocb);
2565                 return 1;
2566         }
2567         return 0;
2568 }
2569
2570 static int
2571 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
2572                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
2573 {
2574         struct lpfc_hba  *phba = vport->phba;
2575         RNID *rn;
2576         IOCB_t *icmd, *oldcmd;
2577         struct lpfc_iocbq *elsiocb;
2578         struct lpfc_sli_ring *pring;
2579         struct lpfc_sli *psli;
2580         uint8_t *pcmd;
2581         uint16_t cmdsize;
2582         int rc;
2583
2584         psli = &phba->sli;
2585         pring = &psli->ring[LPFC_ELS_RING];
2586
2587         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
2588                                         + (2 * sizeof(struct lpfc_name));
2589         if (format)
2590                 cmdsize += sizeof(RNID_TOP_DISC);
2591
2592         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2593                                      ndlp->nlp_DID, ELS_CMD_ACC);
2594         if (!elsiocb)
2595                 return 1;
2596
2597         icmd = &elsiocb->iocb;
2598         oldcmd = &oldiocb->iocb;
2599         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2600         /* Xmit RNID ACC response tag <ulpIoTag> */
2601         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2602                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
2603                          elsiocb->iotag, elsiocb->iocb.ulpContext);
2604         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2605         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2606         pcmd += sizeof(uint32_t);
2607
2608         memset(pcmd, 0, sizeof(RNID));
2609         rn = (RNID *) (pcmd);
2610         rn->Format = format;
2611         rn->CommonLen = (2 * sizeof(struct lpfc_name));
2612         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2613         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2614         switch (format) {
2615         case 0:
2616                 rn->SpecificLen = 0;
2617                 break;
2618         case RNID_TOPOLOGY_DISC:
2619                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
2620                 memcpy(&rn->un.topologyDisc.portName,
2621                        &vport->fc_portname, sizeof(struct lpfc_name));
2622                 rn->un.topologyDisc.unitType = RNID_HBA;
2623                 rn->un.topologyDisc.physPort = 0;
2624                 rn->un.topologyDisc.attachedNodes = 0;
2625                 break;
2626         default:
2627                 rn->CommonLen = 0;
2628                 rn->SpecificLen = 0;
2629                 break;
2630         }
2631
2632         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2633                 "Issue ACC RNID:  did:x%x flg:x%x",
2634                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2635
2636         phba->fc_stat.elsXmitACC++;
2637         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2638         lpfc_nlp_put(ndlp);
2639         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
2640                                     * it could be freed */
2641
2642         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2643         if (rc == IOCB_ERROR) {
2644                 lpfc_els_free_iocb(phba, elsiocb);
2645                 return 1;
2646         }
2647         return 0;
2648 }
2649
2650 int
2651 lpfc_els_disc_adisc(struct lpfc_vport *vport)
2652 {
2653         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2654         struct lpfc_nodelist *ndlp, *next_ndlp;
2655         int sentadisc = 0;
2656
2657         /* go thru NPR nodes and issue any remaining ELS ADISCs */
2658         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2659                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2660                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
2661                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
2662                         spin_lock_irq(shost->host_lock);
2663                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2664                         spin_unlock_irq(shost->host_lock);
2665                         ndlp->nlp_prev_state = ndlp->nlp_state;
2666                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2667                         lpfc_issue_els_adisc(vport, ndlp, 0);
2668                         sentadisc++;
2669                         vport->num_disc_nodes++;
2670                         if (vport->num_disc_nodes >=
2671                             vport->cfg_discovery_threads) {
2672                                 spin_lock_irq(shost->host_lock);
2673                                 vport->fc_flag |= FC_NLP_MORE;
2674                                 spin_unlock_irq(shost->host_lock);
2675                                 break;
2676                         }
2677                 }
2678         }
2679         if (sentadisc == 0) {
2680                 spin_lock_irq(shost->host_lock);
2681                 vport->fc_flag &= ~FC_NLP_MORE;
2682                 spin_unlock_irq(shost->host_lock);
2683         }
2684         return sentadisc;
2685 }
2686
2687 int
2688 lpfc_els_disc_plogi(struct lpfc_vport *vport)
2689 {
2690         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2691         struct lpfc_nodelist *ndlp, *next_ndlp;
2692         int sentplogi = 0;
2693
2694         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
2695         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2696                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2697                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
2698                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
2699                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
2700                         ndlp->nlp_prev_state = ndlp->nlp_state;
2701                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2702                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2703                         sentplogi++;
2704                         vport->num_disc_nodes++;
2705                         if (vport->num_disc_nodes >=
2706                             vport->cfg_discovery_threads) {
2707                                 spin_lock_irq(shost->host_lock);
2708                                 vport->fc_flag |= FC_NLP_MORE;
2709                                 spin_unlock_irq(shost->host_lock);
2710                                 break;
2711                         }
2712                 }
2713         }
2714         if (sentplogi) {
2715                 lpfc_set_disctmo(vport);
2716         }
2717         else {
2718                 spin_lock_irq(shost->host_lock);
2719                 vport->fc_flag &= ~FC_NLP_MORE;
2720                 spin_unlock_irq(shost->host_lock);
2721         }
2722         return sentplogi;
2723 }
2724
2725 void
2726 lpfc_els_flush_rscn(struct lpfc_vport *vport)
2727 {
2728         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2729         struct lpfc_hba  *phba = vport->phba;
2730         int i;
2731
2732         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2733                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
2734                 vport->fc_rscn_id_list[i] = NULL;
2735         }
2736         spin_lock_irq(shost->host_lock);
2737         vport->fc_rscn_id_cnt = 0;
2738         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
2739         spin_unlock_irq(shost->host_lock);
2740         lpfc_can_disctmo(vport);
2741 }
2742
2743 int
2744 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
2745 {
2746         D_ID ns_did;
2747         D_ID rscn_did;
2748         uint32_t *lp;
2749         uint32_t payload_len, i;
2750
2751         ns_did.un.word = did;
2752
2753         /* Never match fabric nodes for RSCNs */
2754         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2755                 return 0;
2756
2757         /* If we are doing a FULL RSCN rediscovery, match everything */
2758         if (vport->fc_flag & FC_RSCN_DISCOVERY)
2759                 return did;
2760
2761         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2762                 lp = vport->fc_rscn_id_list[i]->virt;
2763                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
2764                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
2765                 while (payload_len) {
2766                         rscn_did.un.word = be32_to_cpu(*lp++);
2767                         payload_len -= sizeof(uint32_t);
2768                         switch (rscn_did.un.b.resv) {
2769                         case 0: /* Single N_Port ID effected */
2770                                 if (ns_did.un.word == rscn_did.un.word)
2771                                         return did;
2772                                 break;
2773                         case 1: /* Whole N_Port Area effected */
2774                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
2775                                     && (ns_did.un.b.area == rscn_did.un.b.area))
2776                                         return did;
2777                                 break;
2778                         case 2: /* Whole N_Port Domain effected */
2779                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
2780                                         return did;
2781                                 break;
2782                         default:
2783                                 /* Unknown Identifier in RSCN node */
2784                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2785                                                  "0217 Unknown Identifier in "
2786                                                  "RSCN payload Data: x%x\n",
2787                                                  rscn_did.un.word);
2788                         case 3: /* Whole Fabric effected */
2789                                 return did;
2790                         }
2791                 }
2792         }
2793         return 0;
2794 }
2795
2796 static int
2797 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
2798 {
2799         struct lpfc_nodelist *ndlp = NULL;
2800
2801         /* Look at all nodes effected by pending RSCNs and move
2802          * them to NPR state.
2803          */
2804
2805         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2806                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE ||
2807                     lpfc_rscn_payload_check(vport, ndlp->nlp_DID) == 0)
2808                         continue;
2809
2810                 lpfc_disc_state_machine(vport, ndlp, NULL,
2811                                                 NLP_EVT_DEVICE_RECOVERY);
2812
2813                 /*
2814                  * Make sure NLP_DELAY_TMO is NOT running after a device
2815                  * recovery event.
2816                  */
2817                 if (ndlp->nlp_flag & NLP_DELAY_TMO)
2818                         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2819         }
2820
2821         return 0;
2822 }
2823
2824 static int
2825 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
2826                   struct lpfc_nodelist *ndlp)
2827 {
2828         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2829         struct lpfc_hba  *phba = vport->phba;
2830         struct lpfc_dmabuf *pcmd;
2831         uint32_t *lp, *datap;
2832         IOCB_t *icmd;
2833         uint32_t payload_len, length, nportid, *cmd;
2834         int rscn_cnt = vport->fc_rscn_id_cnt;
2835         int rscn_id = 0, hba_id = 0;
2836         int i;
2837
2838         icmd = &cmdiocb->iocb;
2839         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2840         lp = (uint32_t *) pcmd->virt;
2841
2842         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
2843         payload_len -= sizeof(uint32_t);        /* take off word 0 */
2844         /* RSCN received */
2845         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2846                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
2847                          vport->fc_flag, payload_len, *lp, rscn_cnt);
2848         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
2849                 fc_host_post_event(shost, fc_get_event_number(),
2850                         FCH_EVT_RSCN, lp[i]);
2851
2852         /* If we are about to begin discovery, just ACC the RSCN.
2853          * Discovery processing will satisfy it.
2854          */
2855         if (vport->port_state <= LPFC_NS_QRY) {
2856                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2857                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
2858                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2859
2860                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2861                 return 0;
2862         }
2863
2864         /* If this RSCN just contains NPortIDs for other vports on this HBA,
2865          * just ACC and ignore it.
2866          */
2867         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2868                 !(vport->cfg_peer_port_login)) {
2869                 i = payload_len;
2870                 datap = lp;
2871                 while (i > 0) {
2872                         nportid = *datap++;
2873                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
2874                         i -= sizeof(uint32_t);
2875                         rscn_id++;
2876                         if (lpfc_find_vport_by_did(phba, nportid))
2877                                 hba_id++;
2878                 }
2879                 if (rscn_id == hba_id) {
2880                         /* ALL NPortIDs in RSCN are on HBA */
2881                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2882                                          "0214 Ignore RSCN "
2883                                          "Data: x%x x%x x%x x%x\n",
2884                                          vport->fc_flag, payload_len,
2885                                          *lp, rscn_cnt);
2886                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2887                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
2888                                 ndlp->nlp_DID, vport->port_state,
2889                                 ndlp->nlp_flag);
2890
2891                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
2892                                 ndlp, NULL);
2893                         return 0;
2894                 }
2895         }
2896
2897         /* If we are already processing an RSCN, save the received
2898          * RSCN payload buffer, cmdiocb->context2 to process later.
2899          */
2900         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
2901                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2902                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
2903                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2904
2905                 vport->fc_flag |= FC_RSCN_DEFERRED;
2906                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
2907                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
2908                         spin_lock_irq(shost->host_lock);
2909                         vport->fc_flag |= FC_RSCN_MODE;
2910                         spin_unlock_irq(shost->host_lock);
2911                         if (rscn_cnt) {
2912                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
2913                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
2914                         }
2915                         if ((rscn_cnt) &&
2916                             (payload_len + length <= LPFC_BPL_SIZE)) {
2917                                 *cmd &= ELS_CMD_MASK;
2918                                 *cmd |= be32_to_cpu(payload_len + length);
2919                                 memcpy(((uint8_t *)cmd) + length, lp,
2920                                        payload_len);
2921                         } else {
2922                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
2923                                 vport->fc_rscn_id_cnt++;
2924                                 /* If we zero, cmdiocb->context2, the calling
2925                                  * routine will not try to free it.
2926                                  */
2927                                 cmdiocb->context2 = NULL;
2928                         }
2929
2930                         /* Deferred RSCN */
2931                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2932                                          "0235 Deferred RSCN "
2933                                          "Data: x%x x%x x%x\n",
2934                                          vport->fc_rscn_id_cnt, vport->fc_flag,
2935                                          vport->port_state);
2936                 } else {
2937                         spin_lock_irq(shost->host_lock);
2938                         vport->fc_flag |= FC_RSCN_DISCOVERY;
2939                         spin_unlock_irq(shost->host_lock);
2940                         /* ReDiscovery RSCN */
2941                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2942                                          "0234 ReDiscovery RSCN "
2943                                          "Data: x%x x%x x%x\n",
2944                                          vport->fc_rscn_id_cnt, vport->fc_flag,
2945                                          vport->port_state);
2946                 }
2947                 /* Send back ACC */
2948                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2949
2950                 /* send RECOVERY event for ALL nodes that match RSCN payload */
2951                 lpfc_rscn_recovery_check(vport);
2952                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
2953                 return 0;
2954         }
2955
2956         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2957                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
2958                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2959
2960         spin_lock_irq(shost->host_lock);
2961         vport->fc_flag |= FC_RSCN_MODE;
2962         spin_unlock_irq(shost->host_lock);
2963         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
2964         /*
2965          * If we zero, cmdiocb->context2, the calling routine will
2966          * not try to free it.
2967          */
2968         cmdiocb->context2 = NULL;
2969
2970         lpfc_set_disctmo(vport);
2971
2972         /* Send back ACC */
2973         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2974
2975         /* send RECOVERY event for ALL nodes that match RSCN payload */
2976         lpfc_rscn_recovery_check(vport);
2977
2978         return lpfc_els_handle_rscn(vport);
2979 }
2980
2981 int
2982 lpfc_els_handle_rscn(struct lpfc_vport *vport)
2983 {
2984         struct lpfc_nodelist *ndlp;
2985         struct lpfc_hba *phba = vport->phba;
2986
2987         /* Ignore RSCN if the port is being torn down. */
2988         if (vport->load_flag & FC_UNLOADING) {
2989                 lpfc_els_flush_rscn(vport);
2990                 return 0;
2991         }
2992
2993         /* Start timer for RSCN processing */
2994         lpfc_set_disctmo(vport);
2995
2996         /* RSCN processed */
2997         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2998                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
2999                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
3000                          vport->port_state);
3001
3002         /* To process RSCN, first compare RSCN data with NameServer */
3003         vport->fc_ns_retry = 0;
3004         ndlp = lpfc_findnode_did(vport, NameServer_DID);
3005         if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
3006                 /* Good ndlp, issue CT Request to NameServer */
3007                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
3008                         /* Wait for NameServer query cmpl before we can
3009                            continue */
3010                         return 1;
3011         } else {
3012                 /* If login to NameServer does not exist, issue one */
3013                 /* Good status, issue PLOGI to NameServer */
3014                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
3015                 if (ndlp)
3016                         /* Wait for NameServer login cmpl before we can
3017                            continue */
3018                         return 1;
3019
3020                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3021                 if (!ndlp) {
3022                         lpfc_els_flush_rscn(vport);
3023                         return 0;
3024                 } else {
3025                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
3026                         ndlp->nlp_type |= NLP_FABRIC;
3027                         ndlp->nlp_prev_state = ndlp->nlp_state;
3028                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3029                         lpfc_issue_els_plogi(vport, NameServer_DID, 0);
3030                         /* Wait for NameServer login cmpl before we can
3031                            continue */
3032                         return 1;
3033                 }
3034         }
3035
3036         lpfc_els_flush_rscn(vport);
3037         return 0;
3038 }
3039
3040 static int
3041 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3042                    struct lpfc_nodelist *ndlp)
3043 {
3044         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3045         struct lpfc_hba  *phba = vport->phba;
3046         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3047         uint32_t *lp = (uint32_t *) pcmd->virt;
3048         IOCB_t *icmd = &cmdiocb->iocb;
3049         struct serv_parm *sp;
3050         LPFC_MBOXQ_t *mbox;
3051         struct ls_rjt stat;
3052         uint32_t cmd, did;
3053         int rc;
3054
3055         cmd = *lp++;
3056         sp = (struct serv_parm *) lp;
3057
3058         /* FLOGI received */
3059
3060         lpfc_set_disctmo(vport);
3061
3062         if (phba->fc_topology == TOPOLOGY_LOOP) {
3063                 /* We should never receive a FLOGI in loop mode, ignore it */
3064                 did = icmd->un.elsreq64.remoteID;
3065
3066                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
3067                    Loop Mode */
3068                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3069                                  "0113 An FLOGI ELS command x%x was "
3070                                  "received from DID x%x in Loop Mode\n",
3071                                  cmd, did);
3072                 return 1;
3073         }
3074
3075         did = Fabric_DID;
3076
3077         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
3078                 /* For a FLOGI we accept, then if our portname is greater
3079                  * then the remote portname we initiate Nport login.
3080                  */
3081
3082                 rc = memcmp(&vport->fc_portname, &sp->portName,
3083                             sizeof(struct lpfc_name));
3084
3085                 if (!rc) {
3086                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3087                         if (!mbox)
3088                                 return 1;
3089
3090                         lpfc_linkdown(phba);
3091                         lpfc_init_link(phba, mbox,
3092                                        phba->cfg_topology,
3093                                        phba->cfg_link_speed);
3094                         mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
3095                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3096                         mbox->vport = vport;
3097                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3098                         lpfc_set_loopback_flag(phba);
3099                         if (rc == MBX_NOT_FINISHED) {
3100                                 mempool_free(mbox, phba->mbox_mem_pool);
3101                         }
3102                         return 1;
3103                 } else if (rc > 0) {    /* greater than */
3104                         spin_lock_irq(shost->host_lock);
3105                         vport->fc_flag |= FC_PT2PT_PLOGI;
3106                         spin_unlock_irq(shost->host_lock);
3107                 }
3108                 spin_lock_irq(shost->host_lock);
3109                 vport->fc_flag |= FC_PT2PT;
3110                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3111                 spin_unlock_irq(shost->host_lock);
3112         } else {
3113                 /* Reject this request because invalid parameters */
3114                 stat.un.b.lsRjtRsvd0 = 0;
3115                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3116                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
3117                 stat.un.b.vendorUnique = 0;
3118                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3119                         NULL);
3120                 return 1;
3121         }
3122
3123         /* Send back ACC */
3124         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
3125
3126         return 0;
3127 }
3128
3129 static int
3130 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3131                   struct lpfc_nodelist *ndlp)
3132 {
3133         struct lpfc_dmabuf *pcmd;
3134         uint32_t *lp;
3135         IOCB_t *icmd;
3136         RNID *rn;
3137         struct ls_rjt stat;
3138         uint32_t cmd, did;
3139
3140         icmd = &cmdiocb->iocb;
3141         did = icmd->un.elsreq64.remoteID;
3142         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3143         lp = (uint32_t *) pcmd->virt;
3144
3145         cmd = *lp++;
3146         rn = (RNID *) lp;
3147
3148         /* RNID received */
3149
3150         switch (rn->Format) {
3151         case 0:
3152         case RNID_TOPOLOGY_DISC:
3153                 /* Send back ACC */
3154                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
3155                 break;
3156         default:
3157                 /* Reject this request because format not supported */
3158                 stat.un.b.lsRjtRsvd0 = 0;
3159                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3160                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3161                 stat.un.b.vendorUnique = 0;
3162                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3163                         NULL);
3164         }
3165         return 0;
3166 }
3167
3168 static int
3169 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3170                   struct lpfc_nodelist *ndlp)
3171 {
3172         struct ls_rjt stat;
3173
3174         /* For now, unconditionally reject this command */
3175         stat.un.b.lsRjtRsvd0 = 0;
3176         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3177         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3178         stat.un.b.vendorUnique = 0;
3179         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3180         return 0;
3181 }
3182
3183 static void
3184 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3185 {
3186         struct lpfc_sli *psli = &phba->sli;
3187         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3188         MAILBOX_t *mb;
3189         IOCB_t *icmd;
3190         RPS_RSP *rps_rsp;
3191         uint8_t *pcmd;
3192         struct lpfc_iocbq *elsiocb;
3193         struct lpfc_nodelist *ndlp;
3194         uint16_t xri, status;
3195         uint32_t cmdsize;
3196
3197         mb = &pmb->mb;
3198
3199         ndlp = (struct lpfc_nodelist *) pmb->context2;
3200         xri = (uint16_t) ((unsigned long)(pmb->context1));
3201         pmb->context1 = NULL;
3202         pmb->context2 = NULL;
3203
3204         if (mb->mbxStatus) {
3205                 mempool_free(pmb, phba->mbox_mem_pool);
3206                 return;
3207         }
3208
3209         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
3210         mempool_free(pmb, phba->mbox_mem_pool);
3211         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
3212                                      lpfc_max_els_tries, ndlp,
3213                                      ndlp->nlp_DID, ELS_CMD_ACC);
3214         lpfc_nlp_put(ndlp);
3215         if (!elsiocb)
3216                 return;
3217
3218         icmd = &elsiocb->iocb;
3219         icmd->ulpContext = xri;
3220
3221         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3222         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3223         pcmd += sizeof(uint32_t); /* Skip past command */
3224         rps_rsp = (RPS_RSP *)pcmd;
3225
3226         if (phba->fc_topology != TOPOLOGY_LOOP)
3227                 status = 0x10;
3228         else
3229                 status = 0x8;
3230         if (phba->pport->fc_flag & FC_FABRIC)
3231                 status |= 0x4;
3232
3233         rps_rsp->rsvd1 = 0;
3234         rps_rsp->portStatus = be16_to_cpu(status);
3235         rps_rsp->linkFailureCnt = be32_to_cpu(mb->un.varRdLnk.linkFailureCnt);
3236         rps_rsp->lossSyncCnt = be32_to_cpu(mb->un.varRdLnk.lossSyncCnt);
3237         rps_rsp->lossSignalCnt = be32_to_cpu(mb->un.varRdLnk.lossSignalCnt);
3238         rps_rsp->primSeqErrCnt = be32_to_cpu(mb->un.varRdLnk.primSeqErrCnt);
3239         rps_rsp->invalidXmitWord = be32_to_cpu(mb->un.varRdLnk.invalidXmitWord);
3240         rps_rsp->crcCnt = be32_to_cpu(mb->un.varRdLnk.crcCnt);
3241         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
3242         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
3243                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
3244                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3245                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3246                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3247                          ndlp->nlp_rpi);
3248         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3249         phba->fc_stat.elsXmitACC++;
3250         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
3251                 lpfc_els_free_iocb(phba, elsiocb);
3252         return;
3253 }
3254
3255 static int
3256 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3257                  struct lpfc_nodelist *ndlp)
3258 {
3259         struct lpfc_hba *phba = vport->phba;
3260         uint32_t *lp;
3261         uint8_t flag;
3262         LPFC_MBOXQ_t *mbox;
3263         struct lpfc_dmabuf *pcmd;
3264         RPS *rps;
3265         struct ls_rjt stat;
3266
3267         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3268             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3269                 stat.un.b.lsRjtRsvd0 = 0;
3270                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3271                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3272                 stat.un.b.vendorUnique = 0;
3273                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3274                         NULL);
3275         }
3276
3277         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3278         lp = (uint32_t *) pcmd->virt;
3279         flag = (be32_to_cpu(*lp++) & 0xf);
3280         rps = (RPS *) lp;
3281
3282         if ((flag == 0) ||
3283             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
3284             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
3285                                     sizeof(struct lpfc_name)) == 0))) {
3286
3287                 printk("Fix me....\n");
3288                 dump_stack();
3289                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
3290                 if (mbox) {
3291                         lpfc_read_lnk_stat(phba, mbox);
3292                         mbox->context1 =
3293                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
3294                         mbox->context2 = lpfc_nlp_get(ndlp);
3295                         mbox->vport = vport;
3296                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
3297                         if (lpfc_sli_issue_mbox (phba, mbox, MBX_NOWAIT)
3298                                 != MBX_NOT_FINISHED)
3299                                 /* Mbox completion will send ELS Response */
3300                                 return 0;
3301
3302                         lpfc_nlp_put(ndlp);
3303                         mempool_free(mbox, phba->mbox_mem_pool);
3304                 }
3305         }
3306         stat.un.b.lsRjtRsvd0 = 0;
3307         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3308         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3309         stat.un.b.vendorUnique = 0;
3310         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3311         return 0;
3312 }
3313
3314 static int
3315 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
3316                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3317 {
3318         struct lpfc_hba *phba = vport->phba;
3319         IOCB_t *icmd, *oldcmd;
3320         RPL_RSP rpl_rsp;
3321         struct lpfc_iocbq *elsiocb;
3322         struct lpfc_sli *psli = &phba->sli;
3323         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3324         uint8_t *pcmd;
3325
3326         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3327                                      ndlp->nlp_DID, ELS_CMD_ACC);
3328
3329         if (!elsiocb)
3330                 return 1;
3331
3332         icmd = &elsiocb->iocb;
3333         oldcmd = &oldiocb->iocb;
3334         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3335
3336         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3337         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3338         pcmd += sizeof(uint16_t);
3339         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
3340         pcmd += sizeof(uint16_t);
3341
3342         /* Setup the RPL ACC payload */
3343         rpl_rsp.listLen = be32_to_cpu(1);
3344         rpl_rsp.index = 0;
3345         rpl_rsp.port_num_blk.portNum = 0;
3346         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
3347         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
3348             sizeof(struct lpfc_name));
3349         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
3350         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
3351         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3352                          "0120 Xmit ELS RPL ACC response tag x%x "
3353                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3354                          "rpi x%x\n",
3355                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3356                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3357                          ndlp->nlp_rpi);
3358         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3359         phba->fc_stat.elsXmitACC++;
3360         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
3361                 lpfc_els_free_iocb(phba, elsiocb);
3362                 return 1;
3363         }
3364         return 0;
3365 }
3366
3367 static int
3368 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3369                  struct lpfc_nodelist *ndlp)
3370 {
3371         struct lpfc_dmabuf *pcmd;
3372         uint32_t *lp;
3373         uint32_t maxsize;
3374         uint16_t cmdsize;
3375         RPL *rpl;
3376         struct ls_rjt stat;
3377
3378         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3379             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3380                 stat.un.b.lsRjtRsvd0 = 0;
3381                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3382                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3383                 stat.un.b.vendorUnique = 0;
3384                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3385                         NULL);
3386         }
3387
3388         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3389         lp = (uint32_t *) pcmd->virt;
3390         rpl = (RPL *) (lp + 1);
3391
3392         maxsize = be32_to_cpu(rpl->maxsize);
3393
3394         /* We support only one port */
3395         if ((rpl->index == 0) &&
3396             ((maxsize == 0) ||
3397              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
3398                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
3399         } else {
3400                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
3401         }
3402         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
3403
3404         return 0;
3405 }
3406
3407 static int
3408 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3409                   struct lpfc_nodelist *ndlp)
3410 {
3411         struct lpfc_dmabuf *pcmd;
3412         uint32_t *lp;
3413         IOCB_t *icmd;
3414         FARP *fp;
3415         uint32_t cmd, cnt, did;
3416
3417         icmd = &cmdiocb->iocb;
3418         did = icmd->un.elsreq64.remoteID;
3419         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3420         lp = (uint32_t *) pcmd->virt;
3421
3422         cmd = *lp++;
3423         fp = (FARP *) lp;
3424         /* FARP-REQ received from DID <did> */
3425         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3426                          "0601 FARP-REQ received from DID x%x\n", did);
3427         /* We will only support match on WWPN or WWNN */
3428         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3429                 return 0;
3430         }
3431
3432         cnt = 0;
3433         /* If this FARP command is searching for my portname */
3434         if (fp->Mflags & FARP_MATCH_PORT) {
3435                 if (memcmp(&fp->RportName, &vport->fc_portname,
3436                            sizeof(struct lpfc_name)) == 0)
3437                         cnt = 1;
3438         }
3439
3440         /* If this FARP command is searching for my nodename */
3441         if (fp->Mflags & FARP_MATCH_NODE) {
3442                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
3443                            sizeof(struct lpfc_name)) == 0)
3444                         cnt = 1;
3445         }
3446
3447         if (cnt) {
3448                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
3449                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
3450                         /* Log back into the node before sending the FARP. */
3451                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
3452                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3453                                 lpfc_nlp_set_state(vport, ndlp,
3454                                                    NLP_STE_PLOGI_ISSUE);
3455                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3456                         }
3457
3458                         /* Send a FARP response to that node */
3459                         if (fp->Rflags & FARP_REQUEST_FARPR)
3460                                 lpfc_issue_els_farpr(vport, did, 0);
3461                 }
3462         }
3463         return 0;
3464 }
3465
3466 static int
3467 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3468                    struct lpfc_nodelist  *ndlp)
3469 {
3470         struct lpfc_dmabuf *pcmd;
3471         uint32_t *lp;
3472         IOCB_t *icmd;
3473         uint32_t cmd, did;
3474
3475         icmd = &cmdiocb->iocb;
3476         did = icmd->un.elsreq64.remoteID;
3477         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3478         lp = (uint32_t *) pcmd->virt;
3479
3480         cmd = *lp++;
3481         /* FARP-RSP received from DID <did> */
3482         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3483                          "0600 FARP-RSP received from DID x%x\n", did);
3484         /* ACCEPT the Farp resp request */
3485         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3486
3487         return 0;
3488 }
3489
3490 static int
3491 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3492                  struct lpfc_nodelist *fan_ndlp)
3493 {
3494         struct lpfc_dmabuf *pcmd;
3495         uint32_t *lp;
3496         IOCB_t *icmd;
3497         uint32_t cmd, did;
3498         FAN *fp;
3499         struct lpfc_nodelist *ndlp, *next_ndlp;
3500         struct lpfc_hba *phba = vport->phba;
3501
3502         /* FAN received */
3503         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3504                          "0265 FAN received\n");
3505         icmd = &cmdiocb->iocb;
3506         did = icmd->un.elsreq64.remoteID;
3507         pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3508         lp = (uint32_t *)pcmd->virt;
3509
3510         cmd = *lp++;
3511         fp = (FAN *) lp;
3512
3513         /* FAN received; Fan does not have a reply sequence */
3514
3515         if (phba->pport->port_state == LPFC_LOCAL_CFG_LINK) {
3516                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
3517                         sizeof(struct lpfc_name)) != 0) ||
3518                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
3519                         sizeof(struct lpfc_name)) != 0)) {
3520                         /*
3521                          * This node has switched fabrics.  FLOGI is required
3522                          * Clean up the old rpi's
3523                          */
3524
3525                         list_for_each_entry_safe(ndlp, next_ndlp,
3526                                                  &vport->fc_nodes, nlp_listp) {
3527                                 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3528                                         continue;
3529                                 if (ndlp->nlp_type & NLP_FABRIC) {
3530                                         /*
3531                                          * Clean up old Fabric, Nameserver and
3532                                          * other NLP_FABRIC logins
3533                                          */
3534                                         lpfc_drop_node(vport, ndlp);
3535
3536                                 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3537                                         /* Fail outstanding I/O now since this
3538                                          * device is marked for PLOGI
3539                                          */
3540                                         lpfc_unreg_rpi(vport, ndlp);
3541                                 }
3542                         }
3543
3544                         lpfc_initial_flogi(vport);
3545                         return 0;
3546                 }
3547                 /* Discovery not needed,
3548                  * move the nodes to their original state.
3549                  */
3550                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3551                                          nlp_listp) {
3552                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3553                                 continue;
3554
3555                         switch (ndlp->nlp_prev_state) {
3556                         case NLP_STE_UNMAPPED_NODE:
3557                                 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
3558                                 lpfc_nlp_set_state(vport, ndlp,
3559                                                    NLP_STE_UNMAPPED_NODE);
3560                                 break;
3561
3562                         case NLP_STE_MAPPED_NODE:
3563                                 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
3564                                 lpfc_nlp_set_state(vport, ndlp,
3565                                                    NLP_STE_MAPPED_NODE);
3566                                 break;
3567
3568                         default:
3569                                 break;
3570                         }
3571                 }
3572
3573                 /* Start discovery - this should just do CLEAR_LA */
3574                 lpfc_disc_start(vport);
3575         }
3576         return 0;
3577 }
3578
3579 void
3580 lpfc_els_timeout(unsigned long ptr)
3581 {
3582         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3583         struct lpfc_hba   *phba = vport->phba;
3584         unsigned long iflag;
3585
3586         spin_lock_irqsave(&vport->work_port_lock, iflag);
3587         if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
3588                 vport->work_port_events |= WORKER_ELS_TMO;
3589                 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3590
3591                 spin_lock_irqsave(&phba->hbalock, iflag);
3592                 if (phba->work_wait)
3593                         lpfc_worker_wake_up(phba);
3594                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3595         }
3596         else
3597                 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3598         return;
3599 }
3600
3601 void
3602 lpfc_els_timeout_handler(struct lpfc_vport *vport)
3603 {
3604         struct lpfc_hba  *phba = vport->phba;
3605         struct lpfc_sli_ring *pring;
3606         struct lpfc_iocbq *tmp_iocb, *piocb;
3607         IOCB_t *cmd = NULL;
3608         struct lpfc_dmabuf *pcmd;
3609         uint32_t els_command = 0;
3610         uint32_t timeout;
3611         uint32_t remote_ID = 0xffffffff;
3612
3613         /* If the timer is already canceled do nothing */
3614         if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
3615                 return;
3616         }
3617         spin_lock_irq(&phba->hbalock);
3618         timeout = (uint32_t)(phba->fc_ratov << 1);
3619
3620         pring = &phba->sli.ring[LPFC_ELS_RING];
3621
3622         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3623                 cmd = &piocb->iocb;
3624
3625                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
3626                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
3627                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
3628                         continue;
3629
3630                 if (piocb->vport != vport)
3631                         continue;
3632
3633                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
3634                 if (pcmd)
3635                         els_command = *(uint32_t *) (pcmd->virt);
3636
3637                 if (els_command == ELS_CMD_FARP ||
3638                     els_command == ELS_CMD_FARPR ||
3639                     els_command == ELS_CMD_FDISC)
3640                         continue;
3641
3642                 if (vport != piocb->vport)
3643                         continue;
3644
3645                 if (piocb->drvrTimeout > 0) {
3646                         if (piocb->drvrTimeout >= timeout)
3647                                 piocb->drvrTimeout -= timeout;
3648                         else
3649                                 piocb->drvrTimeout = 0;
3650                         continue;
3651                 }
3652
3653                 remote_ID = 0xffffffff;
3654                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
3655                         remote_ID = cmd->un.elsreq64.remoteID;
3656                 else {
3657                         struct lpfc_nodelist *ndlp;
3658                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
3659                         if (ndlp)
3660                                 remote_ID = ndlp->nlp_DID;
3661                 }
3662                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3663                                  "0127 ELS timeout Data: x%x x%x x%x "
3664                                  "x%x\n", els_command,
3665                                  remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
3666                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3667         }
3668         spin_unlock_irq(&phba->hbalock);
3669
3670         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
3671                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
3672 }
3673
3674 void
3675 lpfc_els_flush_cmd(struct lpfc_vport *vport)
3676 {
3677         LIST_HEAD(completions);
3678         struct lpfc_hba  *phba = vport->phba;
3679         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3680         struct lpfc_iocbq *tmp_iocb, *piocb;
3681         IOCB_t *cmd = NULL;
3682
3683         lpfc_fabric_abort_vport(vport);
3684
3685         spin_lock_irq(&phba->hbalock);
3686         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
3687                 cmd = &piocb->iocb;
3688
3689                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3690                         continue;
3691                 }
3692
3693                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
3694                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
3695                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
3696                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3697                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
3698                         continue;
3699
3700                 if (piocb->vport != vport)
3701                         continue;
3702
3703                 list_move_tail(&piocb->list, &completions);
3704                 pring->txq_cnt--;
3705         }
3706
3707         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3708                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3709                         continue;
3710                 }
3711
3712                 if (piocb->vport != vport)
3713                         continue;
3714
3715                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3716         }
3717         spin_unlock_irq(&phba->hbalock);
3718
3719         while (!list_empty(&completions)) {
3720                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
3721                 cmd = &piocb->iocb;
3722                 list_del_init(&piocb->list);
3723
3724                 if (!piocb->iocb_cmpl)
3725                         lpfc_sli_release_iocbq(phba, piocb);
3726                 else {
3727                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3728                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3729                         (piocb->iocb_cmpl) (phba, piocb, piocb);
3730                 }
3731         }
3732
3733         return;
3734 }
3735
3736 void
3737 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
3738 {
3739         LIST_HEAD(completions);
3740         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3741         struct lpfc_iocbq *tmp_iocb, *piocb;
3742         IOCB_t *cmd = NULL;
3743
3744         lpfc_fabric_abort_hba(phba);
3745         spin_lock_irq(&phba->hbalock);
3746         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
3747                 cmd = &piocb->iocb;
3748                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
3749                         continue;
3750                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
3751                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
3752                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
3753                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3754                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
3755                         continue;
3756                 list_move_tail(&piocb->list, &completions);
3757                 pring->txq_cnt--;
3758         }
3759         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3760                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
3761                         continue;
3762                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3763         }
3764         spin_unlock_irq(&phba->hbalock);
3765         while (!list_empty(&completions)) {
3766                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
3767                 cmd = &piocb->iocb;
3768                 list_del_init(&piocb->list);
3769                 if (!piocb->iocb_cmpl)
3770                         lpfc_sli_release_iocbq(phba, piocb);
3771                 else {
3772                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3773                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3774                         (piocb->iocb_cmpl) (phba, piocb, piocb);
3775                 }
3776         }
3777         return;
3778 }
3779
3780 static void
3781 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3782                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
3783 {
3784         struct Scsi_Host  *shost;
3785         struct lpfc_nodelist *ndlp;
3786         struct ls_rjt stat;
3787         uint32_t *payload;
3788         uint32_t cmd, did, newnode, rjt_err = 0;
3789         IOCB_t *icmd = &elsiocb->iocb;
3790
3791         if (vport == NULL || elsiocb->context2 == NULL)
3792                 goto dropit;
3793
3794         newnode = 0;
3795         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3796         cmd = *payload;
3797         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
3798                 lpfc_post_buffer(phba, pring, 1, 1);
3799
3800         did = icmd->un.rcvels.remoteID;
3801         if (icmd->ulpStatus) {
3802                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3803                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
3804                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
3805                 goto dropit;
3806         }
3807
3808         /* Check to see if link went down during discovery */
3809         if (lpfc_els_chk_latt(vport))
3810                 goto dropit;
3811
3812         /* Ignore traffic recevied during vport shutdown. */
3813         if (vport->load_flag & FC_UNLOADING)
3814                 goto dropit;
3815
3816         ndlp = lpfc_findnode_did(vport, did);
3817         if (!ndlp) {
3818                 /* Cannot find existing Fabric ndlp, so allocate a new one */
3819                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3820                 if (!ndlp)
3821                         goto dropit;
3822
3823                 lpfc_nlp_init(vport, ndlp, did);
3824                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3825                 newnode = 1;
3826                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
3827                         ndlp->nlp_type |= NLP_FABRIC;
3828                 }
3829         }
3830         else {
3831                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
3832                         /* This is simular to the new node path */
3833                         lpfc_nlp_get(ndlp);
3834                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3835                         newnode = 1;
3836                 }
3837         }
3838
3839         phba->fc_stat.elsRcvFrame++;
3840         if (elsiocb->context1)
3841                 lpfc_nlp_put(elsiocb->context1);
3842         elsiocb->context1 = lpfc_nlp_get(ndlp);
3843         elsiocb->vport = vport;
3844
3845         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
3846                 cmd &= ELS_CMD_MASK;
3847         }
3848         /* ELS command <elsCmd> received from NPORT <did> */
3849         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3850                          "0112 ELS command x%x received from NPORT x%x "
3851                          "Data: x%x\n", cmd, did, vport->port_state);
3852         switch (cmd) {
3853         case ELS_CMD_PLOGI:
3854                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3855                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
3856                         did, vport->port_state, ndlp->nlp_flag);
3857
3858                 phba->fc_stat.elsRcvPLOGI++;
3859                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
3860
3861                 if (vport->port_state < LPFC_DISC_AUTH) {
3862                         rjt_err = LSRJT_UNABLE_TPC;
3863                         break;
3864                 }
3865
3866                 shost = lpfc_shost_from_vport(vport);
3867                 spin_lock_irq(shost->host_lock);
3868                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
3869                 spin_unlock_irq(shost->host_lock);
3870
3871                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3872                                         NLP_EVT_RCV_PLOGI);
3873
3874                 break;
3875         case ELS_CMD_FLOGI:
3876                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3877                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
3878                         did, vport->port_state, ndlp->nlp_flag);
3879
3880                 phba->fc_stat.elsRcvFLOGI++;
3881                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
3882                 if (newnode)
3883                         lpfc_nlp_put(ndlp);
3884                 break;
3885         case ELS_CMD_LOGO:
3886                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3887                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
3888                         did, vport->port_state, ndlp->nlp_flag);
3889
3890                 phba->fc_stat.elsRcvLOGO++;
3891                 if (vport->port_state < LPFC_DISC_AUTH) {
3892                         rjt_err = LSRJT_UNABLE_TPC;
3893                         break;
3894                 }
3895                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
3896                 break;
3897         case ELS_CMD_PRLO:
3898                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3899                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
3900                         did, vport->port_state, ndlp->nlp_flag);
3901
3902                 phba->fc_stat.elsRcvPRLO++;
3903                 if (vport->port_state < LPFC_DISC_AUTH) {
3904                         rjt_err = LSRJT_UNABLE_TPC;
3905                         break;
3906                 }
3907                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
3908                 break;
3909         case ELS_CMD_RSCN:
3910                 phba->fc_stat.elsRcvRSCN++;
3911                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
3912                 if (newnode)
3913                         lpfc_nlp_put(ndlp);
3914                 break;
3915         case ELS_CMD_ADISC:
3916                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3917                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
3918                         did, vport->port_state, ndlp->nlp_flag);
3919
3920                 phba->fc_stat.elsRcvADISC++;
3921                 if (vport->port_state < LPFC_DISC_AUTH) {
3922                         rjt_err = LSRJT_UNABLE_TPC;
3923                         break;
3924                 }
3925                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3926                                         NLP_EVT_RCV_ADISC);
3927                 break;
3928         case ELS_CMD_PDISC:
3929                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3930                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
3931                         did, vport->port_state, ndlp->nlp_flag);
3932
3933                 phba->fc_stat.elsRcvPDISC++;
3934                 if (vport->port_state < LPFC_DISC_AUTH) {
3935                         rjt_err = LSRJT_UNABLE_TPC;
3936                         break;
3937                 }
3938                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3939                                         NLP_EVT_RCV_PDISC);
3940                 break;
3941         case ELS_CMD_FARPR:
3942                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3943                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
3944                         did, vport->port_state, ndlp->nlp_flag);
3945
3946                 phba->fc_stat.elsRcvFARPR++;
3947                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
3948                 break;
3949         case ELS_CMD_FARP:
3950                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3951                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
3952                         did, vport->port_state, ndlp->nlp_flag);
3953
3954                 phba->fc_stat.elsRcvFARP++;
3955                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
3956                 break;
3957         case ELS_CMD_FAN:
3958                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3959                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
3960                         did, vport->port_state, ndlp->nlp_flag);
3961
3962                 phba->fc_stat.elsRcvFAN++;
3963                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
3964                 break;
3965         case ELS_CMD_PRLI:
3966                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3967                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
3968                         did, vport->port_state, ndlp->nlp_flag);
3969
3970                 phba->fc_stat.elsRcvPRLI++;
3971                 if (vport->port_state < LPFC_DISC_AUTH) {
3972                         rjt_err = LSRJT_UNABLE_TPC;
3973                         break;
3974                 }
3975                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
3976                 break;
3977         case ELS_CMD_LIRR:
3978                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3979                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
3980                         did, vport->port_state, ndlp->nlp_flag);
3981
3982                 phba->fc_stat.elsRcvLIRR++;
3983                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
3984                 if (newnode)
3985                         lpfc_nlp_put(ndlp);
3986                 break;
3987         case ELS_CMD_RPS:
3988                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3989                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
3990                         did, vport->port_state, ndlp->nlp_flag);
3991
3992                 phba->fc_stat.elsRcvRPS++;
3993                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
3994                 if (newnode)
3995                         lpfc_nlp_put(ndlp);
3996                 break;
3997         case ELS_CMD_RPL:
3998                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3999                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
4000                         did, vport->port_state, ndlp->nlp_flag);
4001
4002                 phba->fc_stat.elsRcvRPL++;
4003                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
4004                 if (newnode)
4005                         lpfc_nlp_put(ndlp);
4006                 break;
4007         case ELS_CMD_RNID:
4008                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4009                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
4010                         did, vport->port_state, ndlp->nlp_flag);
4011
4012                 phba->fc_stat.elsRcvRNID++;
4013                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
4014                 if (newnode)
4015                         lpfc_nlp_put(ndlp);
4016                 break;
4017         default:
4018                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4019                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
4020                         cmd, did, vport->port_state);
4021
4022                 /* Unsupported ELS command, reject */
4023                 rjt_err = LSRJT_INVALID_CMD;
4024
4025                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
4026                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4027                                  "0115 Unknown ELS command x%x "
4028                                  "received from NPORT x%x\n", cmd, did);
4029                 if (newnode)
4030                         lpfc_nlp_put(ndlp);
4031                 break;
4032         }
4033
4034         /* check if need to LS_RJT received ELS cmd */
4035         if (rjt_err) {
4036                 memset(&stat, 0, sizeof(stat));
4037                 stat.un.b.lsRjtRsnCode = rjt_err;
4038                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
4039                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
4040                         NULL);
4041         }
4042
4043         return;
4044
4045 dropit:
4046         if (vport && !(vport->load_flag & FC_UNLOADING))
4047                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
4048                         "(%d):0111 Dropping received ELS cmd "
4049                         "Data: x%x x%x x%x\n",
4050                         vport->vpi, icmd->ulpStatus,
4051                         icmd->un.ulpWord[4], icmd->ulpTimeout);
4052         phba->fc_stat.elsRcvDrop++;
4053 }
4054
4055 static struct lpfc_vport *
4056 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
4057 {
4058         struct lpfc_vport *vport;
4059         unsigned long flags;
4060
4061         spin_lock_irqsave(&phba->hbalock, flags);
4062         list_for_each_entry(vport, &phba->port_list, listentry) {
4063                 if (vport->vpi == vpi) {
4064                         spin_unlock_irqrestore(&phba->hbalock, flags);
4065                         return vport;
4066                 }
4067         }
4068         spin_unlock_irqrestore(&phba->hbalock, flags);
4069         return NULL;
4070 }
4071
4072 void
4073 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4074                      struct lpfc_iocbq *elsiocb)
4075 {
4076         struct lpfc_vport *vport = phba->pport;
4077         IOCB_t *icmd = &elsiocb->iocb;
4078         dma_addr_t paddr;
4079         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
4080         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
4081
4082         elsiocb->context2 = NULL;
4083         elsiocb->context3 = NULL;
4084
4085         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
4086                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
4087         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
4088             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
4089                 phba->fc_stat.NoRcvBuf++;
4090                 /* Not enough posted buffers; Try posting more buffers */
4091                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
4092                         lpfc_post_buffer(phba, pring, 0, 1);
4093                 return;
4094         }
4095
4096         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4097             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
4098              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
4099                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
4100                         vport = phba->pport;
4101                 else {
4102                         uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
4103                         vport = lpfc_find_vport_by_vpid(phba, vpi);
4104                 }
4105         }
4106                                 /* If there are no BDEs associated
4107                                  * with this IOCB, there is nothing to do.
4108                                  */
4109         if (icmd->ulpBdeCount == 0)
4110                 return;
4111
4112                                 /* type of ELS cmd is first 32bit word
4113                                  * in packet
4114                                  */
4115         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4116                 elsiocb->context2 = bdeBuf1;
4117         } else {
4118                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
4119                                  icmd->un.cont64[0].addrLow);
4120                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
4121                                                              paddr);
4122         }
4123
4124         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4125         /*
4126          * The different unsolicited event handlers would tell us
4127          * if they are done with "mp" by setting context2 to NULL.
4128          */
4129         lpfc_nlp_put(elsiocb->context1);
4130         elsiocb->context1 = NULL;
4131         if (elsiocb->context2) {
4132                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
4133                 elsiocb->context2 = NULL;
4134         }
4135
4136         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
4137         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
4138             icmd->ulpBdeCount == 2) {
4139                 elsiocb->context2 = bdeBuf2;
4140                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4141                 /* free mp if we are done with it */
4142                 if (elsiocb->context2) {
4143                         lpfc_in_buf_free(phba, elsiocb->context2);
4144                         elsiocb->context2 = NULL;
4145                 }
4146         }
4147 }
4148
4149 void
4150 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4151 {
4152         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
4153
4154         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4155         if (!ndlp) {
4156                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4157                 if (!ndlp) {
4158                         if (phba->fc_topology == TOPOLOGY_LOOP) {
4159                                 lpfc_disc_start(vport);
4160                                 return;
4161                         }
4162                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4163                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4164                                          "0251 NameServer login: no memory\n");
4165                         return;
4166                 }
4167                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
4168                 ndlp->nlp_type |= NLP_FABRIC;
4169         }
4170
4171         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4172
4173         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
4174                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4175                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4176                                  "0252 Cannot issue NameServer login\n");
4177                 return;
4178         }
4179
4180         if (vport->cfg_fdmi_on) {
4181                 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
4182                                           GFP_KERNEL);
4183                 if (ndlp_fdmi) {
4184                         lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
4185                         ndlp_fdmi->nlp_type |= NLP_FABRIC;
4186                         ndlp_fdmi->nlp_state =
4187                                 NLP_STE_PLOGI_ISSUE;
4188                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
4189                                              0);
4190                 }
4191         }
4192         return;
4193 }
4194
4195 static void
4196 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4197 {
4198         struct lpfc_vport *vport = pmb->vport;
4199         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
4200         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4201         MAILBOX_t *mb = &pmb->mb;
4202
4203         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4204         lpfc_nlp_put(ndlp);
4205
4206         if (mb->mbxStatus) {
4207                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4208                                  "0915 Register VPI failed: 0x%x\n",
4209                                  mb->mbxStatus);
4210
4211                 switch (mb->mbxStatus) {
4212                 case 0x11:      /* unsupported feature */
4213                 case 0x9603:    /* max_vpi exceeded */
4214                         /* giving up on vport registration */
4215                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4216                         spin_lock_irq(shost->host_lock);
4217                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4218                         spin_unlock_irq(shost->host_lock);
4219                         lpfc_can_disctmo(vport);
4220                         break;
4221                 default:
4222                         /* Try to recover from this error */
4223                         lpfc_mbx_unreg_vpi(vport);
4224                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4225                         lpfc_initial_fdisc(vport);
4226                         break;
4227                 }
4228
4229         } else {
4230                 if (vport == phba->pport)
4231                         lpfc_issue_fabric_reglogin(vport);
4232                 else
4233                         lpfc_do_scr_ns_plogi(phba, vport);
4234         }
4235         mempool_free(pmb, phba->mbox_mem_pool);
4236         return;
4237 }
4238
4239 void
4240 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
4241                         struct lpfc_nodelist *ndlp)
4242 {
4243         LPFC_MBOXQ_t *mbox;
4244
4245         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4246         if (mbox) {
4247                 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
4248                 mbox->vport = vport;
4249                 mbox->context2 = lpfc_nlp_get(ndlp);
4250                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
4251                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4252                     == MBX_NOT_FINISHED) {
4253                         mempool_free(mbox, phba->mbox_mem_pool);
4254                         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4255
4256                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4257                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4258                                 "0253 Register VPI: Can't send mbox\n");
4259                 }
4260         } else {
4261                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4262
4263                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4264                                  "0254 Register VPI: no memory\n");
4265
4266                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4267                 lpfc_nlp_put(ndlp);
4268         }
4269 }
4270
4271 static void
4272 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4273                     struct lpfc_iocbq *rspiocb)
4274 {
4275         struct lpfc_vport *vport = cmdiocb->vport;
4276         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
4277         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4278         struct lpfc_nodelist *np;
4279         struct lpfc_nodelist *next_np;
4280         IOCB_t *irsp = &rspiocb->iocb;
4281         struct lpfc_iocbq *piocb;
4282
4283         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4284                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
4285                          irsp->ulpStatus, irsp->un.ulpWord[4],
4286                          vport->fc_prevDID);
4287         /* Since all FDISCs are being single threaded, we
4288          * must reset the discovery timer for ALL vports
4289          * waiting to send FDISC when one completes.
4290          */
4291         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
4292                 lpfc_set_disctmo(piocb->vport);
4293         }
4294
4295         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4296                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
4297                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
4298
4299         if (irsp->ulpStatus) {
4300                 /* Check for retry */
4301                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
4302                         goto out;
4303                 /* FDISC failed */
4304                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4305                                  "0124 FDISC failed. (%d/%d)\n",
4306                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
4307                 if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
4308                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4309
4310                 lpfc_nlp_put(ndlp);
4311                 /* giving up on FDISC. Cancel discovery timer */
4312                 lpfc_can_disctmo(vport);
4313         } else {
4314                 spin_lock_irq(shost->host_lock);
4315                 vport->fc_flag |= FC_FABRIC;
4316                 if (vport->phba->fc_topology == TOPOLOGY_LOOP)
4317                         vport->fc_flag |=  FC_PUBLIC_LOOP;
4318                 spin_unlock_irq(shost->host_lock);
4319
4320                 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
4321                 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
4322                 if ((vport->fc_prevDID != vport->fc_myDID) &&
4323                         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
4324                         /* If our NportID changed, we need to ensure all
4325                          * remaining NPORTs get unreg_login'ed so we can
4326                          * issue unreg_vpi.
4327                          */
4328                         list_for_each_entry_safe(np, next_np,
4329                                 &vport->fc_nodes, nlp_listp) {
4330                                 if (np->nlp_state != NLP_STE_NPR_NODE
4331                                    || !(np->nlp_flag & NLP_NPR_ADISC))
4332                                         continue;
4333                                 spin_lock_irq(shost->host_lock);
4334                                 np->nlp_flag &= ~NLP_NPR_ADISC;
4335                                 spin_unlock_irq(shost->host_lock);
4336                                 lpfc_unreg_rpi(vport, np);
4337                         }
4338                         lpfc_mbx_unreg_vpi(vport);
4339                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4340                 }
4341
4342                 if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
4343                         lpfc_register_new_vport(phba, vport, ndlp);
4344                 else
4345                         lpfc_do_scr_ns_plogi(phba, vport);
4346
4347                 lpfc_nlp_put(ndlp); /* Free Fabric ndlp for vports */
4348         }
4349
4350 out:
4351         lpfc_els_free_iocb(phba, cmdiocb);
4352 }
4353
4354 int
4355 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4356                      uint8_t retry)
4357 {
4358         struct lpfc_hba *phba = vport->phba;
4359         IOCB_t *icmd;
4360         struct lpfc_iocbq *elsiocb;
4361         struct serv_parm *sp;
4362         uint8_t *pcmd;
4363         uint16_t cmdsize;
4364         int did = ndlp->nlp_DID;
4365         int rc;
4366
4367         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
4368         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
4369                                      ELS_CMD_FDISC);
4370         if (!elsiocb) {
4371                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4372                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4373                                  "0255 Issue FDISC: no IOCB\n");
4374                 return 1;
4375         }
4376
4377         icmd = &elsiocb->iocb;
4378         icmd->un.elsreq64.myID = 0;
4379         icmd->un.elsreq64.fl = 1;
4380
4381         /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
4382         icmd->ulpCt_h = 1;
4383         icmd->ulpCt_l = 0;
4384
4385         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4386         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
4387         pcmd += sizeof(uint32_t); /* CSP Word 1 */
4388         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
4389         sp = (struct serv_parm *) pcmd;
4390         /* Setup CSPs accordingly for Fabric */
4391         sp->cmn.e_d_tov = 0;
4392         sp->cmn.w2.r_a_tov = 0;
4393         sp->cls1.classValid = 0;
4394         sp->cls2.seqDelivery = 1;
4395         sp->cls3.seqDelivery = 1;
4396
4397         pcmd += sizeof(uint32_t); /* CSP Word 2 */
4398         pcmd += sizeof(uint32_t); /* CSP Word 3 */
4399         pcmd += sizeof(uint32_t); /* CSP Word 4 */
4400         pcmd += sizeof(uint32_t); /* Port Name */
4401         memcpy(pcmd, &vport->fc_portname, 8);
4402         pcmd += sizeof(uint32_t); /* Node Name */
4403         pcmd += sizeof(uint32_t); /* Node Name */
4404         memcpy(pcmd, &vport->fc_nodename, 8);
4405
4406         lpfc_set_disctmo(vport);
4407
4408         phba->fc_stat.elsXmitFDISC++;
4409         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
4410
4411         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4412                 "Issue FDISC:     did:x%x",
4413                 did, 0, 0);
4414
4415         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
4416         if (rc == IOCB_ERROR) {
4417                 lpfc_els_free_iocb(phba, elsiocb);
4418                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4419                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4420                                  "0256 Issue FDISC: Cannot send IOCB\n");
4421                 return 1;
4422         }
4423         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
4424         vport->port_state = LPFC_FDISC;
4425         return 0;
4426 }
4427
4428 static void
4429 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4430                         struct lpfc_iocbq *rspiocb)
4431 {
4432         struct lpfc_vport *vport = cmdiocb->vport;
4433         IOCB_t *irsp;
4434
4435         irsp = &rspiocb->iocb;
4436         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4437                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
4438                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
4439
4440         lpfc_els_free_iocb(phba, cmdiocb);
4441         vport->unreg_vpi_cmpl = VPORT_ERROR;
4442 }
4443
4444 int
4445 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4446 {
4447         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4448         struct lpfc_hba  *phba = vport->phba;
4449         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4450         IOCB_t *icmd;
4451         struct lpfc_iocbq *elsiocb;
4452         uint8_t *pcmd;
4453         uint16_t cmdsize;
4454
4455         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
4456         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
4457                                      ELS_CMD_LOGO);
4458         if (!elsiocb)
4459                 return 1;
4460
4461         icmd = &elsiocb->iocb;
4462         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4463         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
4464         pcmd += sizeof(uint32_t);
4465
4466         /* Fill in LOGO payload */
4467         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
4468         pcmd += sizeof(uint32_t);
4469         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
4470
4471         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4472                 "Issue LOGO npiv  did:x%x flg:x%x",
4473                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4474
4475         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
4476         spin_lock_irq(shost->host_lock);
4477         ndlp->nlp_flag |= NLP_LOGO_SND;
4478         spin_unlock_irq(shost->host_lock);
4479         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
4480                 spin_lock_irq(shost->host_lock);
4481                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
4482                 spin_unlock_irq(shost->host_lock);
4483                 lpfc_els_free_iocb(phba, elsiocb);
4484                 return 1;
4485         }
4486         return 0;
4487 }
4488
4489 void
4490 lpfc_fabric_block_timeout(unsigned long ptr)
4491 {
4492         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4493         unsigned long iflags;
4494         uint32_t tmo_posted;
4495         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
4496         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
4497         if (!tmo_posted)
4498                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
4499         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
4500
4501         if (!tmo_posted) {
4502                 spin_lock_irqsave(&phba->hbalock, iflags);
4503                 if (phba->work_wait)
4504                         lpfc_worker_wake_up(phba);
4505                 spin_unlock_irqrestore(&phba->hbalock, iflags);
4506         }
4507 }
4508
4509 static void
4510 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
4511 {
4512         struct lpfc_iocbq *iocb;
4513         unsigned long iflags;
4514         int ret;
4515         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4516         IOCB_t *cmd;
4517
4518 repeat:
4519         iocb = NULL;
4520         spin_lock_irqsave(&phba->hbalock, iflags);
4521                                 /* Post any pending iocb to the SLI layer */
4522         if (atomic_read(&phba->fabric_iocb_count) == 0) {
4523                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
4524                                  list);
4525                 if (iocb)
4526                         atomic_inc(&phba->fabric_iocb_count);
4527         }
4528         spin_unlock_irqrestore(&phba->hbalock, iflags);
4529         if (iocb) {
4530                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
4531                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
4532                 iocb->iocb_flag |= LPFC_IO_FABRIC;
4533
4534                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
4535                         "Fabric sched1:   ste:x%x",
4536                         iocb->vport->port_state, 0, 0);
4537
4538                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
4539
4540                 if (ret == IOCB_ERROR) {
4541                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
4542                         iocb->fabric_iocb_cmpl = NULL;
4543                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
4544                         cmd = &iocb->iocb;
4545                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4546                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4547                         iocb->iocb_cmpl(phba, iocb, iocb);
4548
4549                         atomic_dec(&phba->fabric_iocb_count);
4550                         goto repeat;
4551                 }
4552         }
4553
4554         return;
4555 }
4556
4557 void
4558 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
4559 {
4560         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4561
4562         lpfc_resume_fabric_iocbs(phba);
4563         return;
4564 }
4565
4566 static void
4567 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
4568 {
4569         int blocked;
4570
4571         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4572                                 /* Start a timer to unblock fabric
4573                                  * iocbs after 100ms
4574                                  */
4575         if (!blocked)
4576                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
4577
4578         return;
4579 }
4580
4581 static void
4582 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4583         struct lpfc_iocbq *rspiocb)
4584 {
4585         struct ls_rjt stat;
4586
4587         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
4588                 BUG();
4589
4590         switch (rspiocb->iocb.ulpStatus) {
4591                 case IOSTAT_NPORT_RJT:
4592                 case IOSTAT_FABRIC_RJT:
4593                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
4594                                 lpfc_block_fabric_iocbs(phba);
4595                         }
4596                         break;
4597
4598                 case IOSTAT_NPORT_BSY:
4599                 case IOSTAT_FABRIC_BSY:
4600                         lpfc_block_fabric_iocbs(phba);
4601                         break;
4602
4603                 case IOSTAT_LS_RJT:
4604                         stat.un.lsRjtError =
4605                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
4606                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
4607                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
4608                                 lpfc_block_fabric_iocbs(phba);
4609                         break;
4610         }
4611
4612         if (atomic_read(&phba->fabric_iocb_count) == 0)
4613                 BUG();
4614
4615         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
4616         cmdiocb->fabric_iocb_cmpl = NULL;
4617         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
4618         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
4619
4620         atomic_dec(&phba->fabric_iocb_count);
4621         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
4622                                 /* Post any pending iocbs to HBA */
4623                     lpfc_resume_fabric_iocbs(phba);
4624         }
4625 }
4626
4627 int
4628 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
4629 {
4630         unsigned long iflags;
4631         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4632         int ready;
4633         int ret;
4634
4635         if (atomic_read(&phba->fabric_iocb_count) > 1)
4636                 BUG();
4637
4638         spin_lock_irqsave(&phba->hbalock, iflags);
4639         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
4640                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4641
4642         spin_unlock_irqrestore(&phba->hbalock, iflags);
4643         if (ready) {
4644                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
4645                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
4646                 iocb->iocb_flag |= LPFC_IO_FABRIC;
4647
4648                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
4649                         "Fabric sched2:   ste:x%x",
4650                         iocb->vport->port_state, 0, 0);
4651
4652                 atomic_inc(&phba->fabric_iocb_count);
4653                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
4654
4655                 if (ret == IOCB_ERROR) {
4656                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
4657                         iocb->fabric_iocb_cmpl = NULL;
4658                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
4659                         atomic_dec(&phba->fabric_iocb_count);
4660                 }
4661         } else {
4662                 spin_lock_irqsave(&phba->hbalock, iflags);
4663                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
4664                 spin_unlock_irqrestore(&phba->hbalock, iflags);
4665                 ret = IOCB_SUCCESS;
4666         }
4667         return ret;
4668 }
4669
4670
4671 void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
4672 {
4673         LIST_HEAD(completions);
4674         struct lpfc_hba  *phba = vport->phba;
4675         struct lpfc_iocbq *tmp_iocb, *piocb;
4676         IOCB_t *cmd;
4677
4678         spin_lock_irq(&phba->hbalock);
4679         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4680                                  list) {
4681
4682                 if (piocb->vport != vport)
4683                         continue;
4684
4685                 list_move_tail(&piocb->list, &completions);
4686         }
4687         spin_unlock_irq(&phba->hbalock);
4688
4689         while (!list_empty(&completions)) {
4690                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4691                 list_del_init(&piocb->list);
4692
4693                 cmd = &piocb->iocb;
4694                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4695                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4696                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4697         }
4698 }
4699
4700 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
4701 {
4702         LIST_HEAD(completions);
4703         struct lpfc_hba  *phba = ndlp->vport->phba;
4704         struct lpfc_iocbq *tmp_iocb, *piocb;
4705         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4706         IOCB_t *cmd;
4707
4708         spin_lock_irq(&phba->hbalock);
4709         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4710                                  list) {
4711                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
4712
4713                         list_move_tail(&piocb->list, &completions);
4714                 }
4715         }
4716         spin_unlock_irq(&phba->hbalock);
4717
4718         while (!list_empty(&completions)) {
4719                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4720                 list_del_init(&piocb->list);
4721
4722                 cmd = &piocb->iocb;
4723                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4724                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4725                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4726         }
4727 }
4728
4729 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
4730 {
4731         LIST_HEAD(completions);
4732         struct lpfc_iocbq *piocb;
4733         IOCB_t *cmd;
4734
4735         spin_lock_irq(&phba->hbalock);
4736         list_splice_init(&phba->fabric_iocb_list, &completions);
4737         spin_unlock_irq(&phba->hbalock);
4738
4739         while (!list_empty(&completions)) {
4740                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4741                 list_del_init(&piocb->list);
4742
4743                 cmd = &piocb->iocb;
4744                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4745                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4746                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4747         }
4748 }
4749
4750
4751 void lpfc_fabric_abort_flogi(struct lpfc_hba *phba)
4752 {
4753         LIST_HEAD(completions);
4754         struct lpfc_iocbq *tmp_iocb, *piocb;
4755         IOCB_t *cmd;
4756         struct lpfc_nodelist *ndlp;
4757
4758         spin_lock_irq(&phba->hbalock);
4759         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4760                                  list) {
4761
4762                 cmd = &piocb->iocb;
4763                 ndlp = (struct lpfc_nodelist *) piocb->context1;
4764                 if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
4765                     ndlp != NULL &&
4766                     ndlp->nlp_DID == Fabric_DID)
4767                         list_move_tail(&piocb->list, &completions);
4768         }
4769         spin_unlock_irq(&phba->hbalock);
4770
4771         while (!list_empty(&completions)) {
4772                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4773                 list_del_init(&piocb->list);
4774
4775                 cmd = &piocb->iocb;
4776                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4777                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4778                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4779         }
4780 }
4781
4782