]> err.no Git - linux-2.6/blob - drivers/scsi/lpfc/lpfc_els.c
9315c3c2e6f68bfbc2d3cf527f6572b757e4786b
[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         struct lpfc_nodelist *ndlp;
2073
2074         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
2075         if (ndlp) {
2076                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
2077                         lpfc_nlp_put(ndlp);
2078
2079                         /* If the ndlp is not being used by another discovery
2080                          * thread, free it.
2081                          */
2082                         if (!lpfc_nlp_not_used(ndlp)) {
2083                                 /* If ndlp is being used by another discovery
2084                                  * thread, just clear NLP_DEFER_RM
2085                                  */
2086                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
2087                         }
2088                 }
2089                 else
2090                         lpfc_nlp_put(ndlp);
2091                 elsiocb->context1 = NULL;
2092         }
2093         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
2094         if (elsiocb->context2) {
2095                 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2096                 lpfc_els_free_data(phba, buf_ptr1);
2097         }
2098
2099         if (elsiocb->context3) {
2100                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2101                 lpfc_els_free_bpl(phba, buf_ptr);
2102         }
2103         lpfc_sli_release_iocbq(phba, elsiocb);
2104         return 0;
2105 }
2106
2107 static void
2108 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2109                        struct lpfc_iocbq *rspiocb)
2110 {
2111         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2112         struct lpfc_vport *vport = cmdiocb->vport;
2113         IOCB_t *irsp;
2114
2115         irsp = &rspiocb->iocb;
2116         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2117                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2118                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2119         /* ACC to LOGO completes to NPort <nlp_DID> */
2120         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2121                          "0109 ACC to LOGO completes to NPort x%x "
2122                          "Data: x%x x%x x%x\n",
2123                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2124                          ndlp->nlp_rpi);
2125
2126         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
2127                 /* NPort Recovery mode or node is just allocated */
2128                 if (!lpfc_nlp_not_used(ndlp)) {
2129                         /* If the ndlp is being used by another discovery
2130                          * thread, just unregister the RPI.
2131                          */
2132                         lpfc_unreg_rpi(vport, ndlp);
2133                 }
2134         }
2135         lpfc_els_free_iocb(phba, cmdiocb);
2136         return;
2137 }
2138
2139 void
2140 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2141 {
2142         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2143         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2144
2145         pmb->context1 = NULL;
2146         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2147         kfree(mp);
2148         mempool_free(pmb, phba->mbox_mem_pool);
2149         if (ndlp) {
2150                 lpfc_nlp_put(ndlp);
2151
2152                 /* This is the end of the default RPI cleanup logic for this
2153                  * ndlp. If no other discovery threads are using this ndlp.
2154                  * we should free all resources associated with it.
2155                  */
2156                 lpfc_nlp_not_used(ndlp);
2157         }
2158         return;
2159 }
2160
2161 static void
2162 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2163                   struct lpfc_iocbq *rspiocb)
2164 {
2165         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2166         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2167         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2168         IOCB_t  *irsp;
2169         uint8_t *pcmd;
2170         LPFC_MBOXQ_t *mbox = NULL;
2171         struct lpfc_dmabuf *mp = NULL;
2172         uint32_t ls_rjt = 0;
2173
2174         irsp = &rspiocb->iocb;
2175
2176         if (cmdiocb->context_un.mbox)
2177                 mbox = cmdiocb->context_un.mbox;
2178
2179         /* First determine if this is a LS_RJT cmpl */
2180         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
2181         if (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT) {
2182                 /* A LS_RJT associated with Default RPI cleanup
2183                  * has its own seperate code path.
2184                  */
2185                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2186                         ls_rjt = 1;
2187         }
2188
2189         /* Check to see if link went down during discovery */
2190         if (!ndlp || lpfc_els_chk_latt(vport)) {
2191                 if (mbox) {
2192                         mp = (struct lpfc_dmabuf *) mbox->context1;
2193                         if (mp) {
2194                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2195                                 kfree(mp);
2196                         }
2197                         mempool_free(mbox, phba->mbox_mem_pool);
2198                 }
2199                 if (ndlp && (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
2200                         if (lpfc_nlp_not_used(ndlp))
2201                                 ndlp = NULL;
2202                 goto out;
2203         }
2204
2205         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2206                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
2207                 irsp->ulpStatus, irsp->un.ulpWord[4],
2208                 cmdiocb->iocb.un.elsreq64.remoteID);
2209         /* ELS response tag <ulpIoTag> completes */
2210         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2211                          "0110 ELS response tag x%x completes "
2212                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
2213                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
2214                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
2215                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2216                          ndlp->nlp_rpi);
2217         if (mbox) {
2218                 if ((rspiocb->iocb.ulpStatus == 0)
2219                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
2220                         lpfc_unreg_rpi(vport, ndlp);
2221                         mbox->context2 = lpfc_nlp_get(ndlp);
2222                         mbox->vport = vport;
2223                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
2224                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2225                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2226                         }
2227                         else {
2228                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
2229                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2230                                 lpfc_nlp_set_state(vport, ndlp,
2231                                            NLP_STE_REG_LOGIN_ISSUE);
2232                         }
2233                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
2234                             != MBX_NOT_FINISHED) {
2235                                 goto out;
2236                         }
2237
2238                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
2239                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2240                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
2241                                 "Data: x%x x%x x%x\n",
2242                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2243                                 ndlp->nlp_rpi);
2244
2245                         if (lpfc_nlp_not_used(ndlp))
2246                                 ndlp = NULL;
2247                 } else {
2248                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
2249                         if (!lpfc_error_lost_link(irsp) &&
2250                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
2251                                 if (lpfc_nlp_not_used(ndlp))
2252                                         ndlp = NULL;
2253                         }
2254                 }
2255                 mp = (struct lpfc_dmabuf *) mbox->context1;
2256                 if (mp) {
2257                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2258                         kfree(mp);
2259                 }
2260                 mempool_free(mbox, phba->mbox_mem_pool);
2261         }
2262 out:
2263         if (ndlp) {
2264                 spin_lock_irq(shost->host_lock);
2265                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
2266                 spin_unlock_irq(shost->host_lock);
2267
2268                 /* If the node is not being used by another discovery thread,
2269                  * and we are sending a reject, we are done with it.
2270                  * Release driver reference count here and free associated
2271                  * resources.
2272                  */
2273                 if (ls_rjt)
2274                         lpfc_nlp_not_used(ndlp);
2275         }
2276
2277         lpfc_els_free_iocb(phba, cmdiocb);
2278         return;
2279 }
2280
2281 int
2282 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
2283                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2284                  LPFC_MBOXQ_t *mbox)
2285 {
2286         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2287         struct lpfc_hba  *phba = vport->phba;
2288         IOCB_t *icmd;
2289         IOCB_t *oldcmd;
2290         struct lpfc_iocbq *elsiocb;
2291         struct lpfc_sli_ring *pring;
2292         struct lpfc_sli *psli;
2293         uint8_t *pcmd;
2294         uint16_t cmdsize;
2295         int rc;
2296         ELS_PKT *els_pkt_ptr;
2297
2298         psli = &phba->sli;
2299         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2300         oldcmd = &oldiocb->iocb;
2301
2302         switch (flag) {
2303         case ELS_CMD_ACC:
2304                 cmdsize = sizeof(uint32_t);
2305                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2306                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2307                 if (!elsiocb) {
2308                         spin_lock_irq(shost->host_lock);
2309                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2310                         spin_unlock_irq(shost->host_lock);
2311                         return 1;
2312                 }
2313
2314                 icmd = &elsiocb->iocb;
2315                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2316                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2317                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2318                 pcmd += sizeof(uint32_t);
2319
2320                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2321                         "Issue ACC:       did:x%x flg:x%x",
2322                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
2323                 break;
2324         case ELS_CMD_PLOGI:
2325                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
2326                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2327                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
2328                 if (!elsiocb)
2329                         return 1;
2330
2331                 icmd = &elsiocb->iocb;
2332                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2333                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2334
2335                 if (mbox)
2336                         elsiocb->context_un.mbox = mbox;
2337
2338                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2339                 pcmd += sizeof(uint32_t);
2340                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2341
2342                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2343                         "Issue ACC PLOGI: did:x%x flg:x%x",
2344                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
2345                 break;
2346         case ELS_CMD_PRLO:
2347                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
2348                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
2349                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
2350                 if (!elsiocb)
2351                         return 1;
2352
2353                 icmd = &elsiocb->iocb;
2354                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
2355                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2356
2357                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
2358                        sizeof(uint32_t) + sizeof(PRLO));
2359                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
2360                 els_pkt_ptr = (ELS_PKT *) pcmd;
2361                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
2362
2363                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2364                         "Issue ACC PRLO:  did:x%x flg:x%x",
2365                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
2366                 break;
2367         default:
2368                 return 1;
2369         }
2370         /* Xmit ELS ACC response tag <ulpIoTag> */
2371         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2372                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
2373                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
2374                          elsiocb->iotag, elsiocb->iocb.ulpContext,
2375                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2376                          ndlp->nlp_rpi);
2377         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2378                 spin_lock_irq(shost->host_lock);
2379                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2380                 spin_unlock_irq(shost->host_lock);
2381                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
2382         } else {
2383                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2384         }
2385
2386         phba->fc_stat.elsXmitACC++;
2387         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2388         if (rc == IOCB_ERROR) {
2389                 lpfc_els_free_iocb(phba, elsiocb);
2390                 return 1;
2391         }
2392         return 0;
2393 }
2394
2395 int
2396 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
2397                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
2398                     LPFC_MBOXQ_t *mbox)
2399 {
2400         struct lpfc_hba  *phba = vport->phba;
2401         IOCB_t *icmd;
2402         IOCB_t *oldcmd;
2403         struct lpfc_iocbq *elsiocb;
2404         struct lpfc_sli_ring *pring;
2405         struct lpfc_sli *psli;
2406         uint8_t *pcmd;
2407         uint16_t cmdsize;
2408         int rc;
2409
2410         psli = &phba->sli;
2411         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2412
2413         cmdsize = 2 * sizeof(uint32_t);
2414         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2415                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
2416         if (!elsiocb)
2417                 return 1;
2418
2419         icmd = &elsiocb->iocb;
2420         oldcmd = &oldiocb->iocb;
2421         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2422         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2423
2424         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
2425         pcmd += sizeof(uint32_t);
2426         *((uint32_t *) (pcmd)) = rejectError;
2427
2428         if (mbox)
2429                 elsiocb->context_un.mbox = mbox;
2430
2431         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
2432         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2433                          "0129 Xmit ELS RJT x%x response tag x%x "
2434                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
2435                          "rpi x%x\n",
2436                          rejectError, elsiocb->iotag,
2437                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
2438                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
2439         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2440                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
2441                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
2442
2443         phba->fc_stat.elsXmitLSRJT++;
2444         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2445         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2446
2447         if (rc == IOCB_ERROR) {
2448                 lpfc_els_free_iocb(phba, elsiocb);
2449                 return 1;
2450         }
2451         return 0;
2452 }
2453
2454 int
2455 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2456                        struct lpfc_nodelist *ndlp)
2457 {
2458         struct lpfc_hba  *phba = vport->phba;
2459         struct lpfc_sli  *psli = &phba->sli;
2460         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2461         ADISC *ap;
2462         IOCB_t *icmd, *oldcmd;
2463         struct lpfc_iocbq *elsiocb;
2464         uint8_t *pcmd;
2465         uint16_t cmdsize;
2466         int rc;
2467
2468         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
2469         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2470                                      ndlp->nlp_DID, ELS_CMD_ACC);
2471         if (!elsiocb)
2472                 return 1;
2473
2474         icmd = &elsiocb->iocb;
2475         oldcmd = &oldiocb->iocb;
2476         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2477
2478         /* Xmit ADISC ACC response tag <ulpIoTag> */
2479         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2480                          "0130 Xmit ADISC ACC response iotag x%x xri: "
2481                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
2482                          elsiocb->iotag, elsiocb->iocb.ulpContext,
2483                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2484                          ndlp->nlp_rpi);
2485         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2486
2487         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2488         pcmd += sizeof(uint32_t);
2489
2490         ap = (ADISC *) (pcmd);
2491         ap->hardAL_PA = phba->fc_pref_ALPA;
2492         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2493         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2494         ap->DID = be32_to_cpu(vport->fc_myDID);
2495
2496         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2497                 "Issue ACC ADISC: did:x%x flg:x%x",
2498                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2499
2500         phba->fc_stat.elsXmitACC++;
2501         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2502         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2503         if (rc == IOCB_ERROR) {
2504                 lpfc_els_free_iocb(phba, elsiocb);
2505                 return 1;
2506         }
2507         return 0;
2508 }
2509
2510 int
2511 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
2512                       struct lpfc_nodelist *ndlp)
2513 {
2514         struct lpfc_hba  *phba = vport->phba;
2515         PRLI *npr;
2516         lpfc_vpd_t *vpd;
2517         IOCB_t *icmd;
2518         IOCB_t *oldcmd;
2519         struct lpfc_iocbq *elsiocb;
2520         struct lpfc_sli_ring *pring;
2521         struct lpfc_sli *psli;
2522         uint8_t *pcmd;
2523         uint16_t cmdsize;
2524         int rc;
2525
2526         psli = &phba->sli;
2527         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2528
2529         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
2530         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2531                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
2532         if (!elsiocb)
2533                 return 1;
2534
2535         icmd = &elsiocb->iocb;
2536         oldcmd = &oldiocb->iocb;
2537         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2538         /* Xmit PRLI ACC response tag <ulpIoTag> */
2539         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2540                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
2541                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
2542                          elsiocb->iotag, elsiocb->iocb.ulpContext,
2543                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2544                          ndlp->nlp_rpi);
2545         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2546
2547         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2548         pcmd += sizeof(uint32_t);
2549
2550         /* For PRLI, remainder of payload is PRLI parameter page */
2551         memset(pcmd, 0, sizeof(PRLI));
2552
2553         npr = (PRLI *) pcmd;
2554         vpd = &phba->vpd;
2555         /*
2556          * If our firmware version is 3.20 or later,
2557          * set the following bits for FC-TAPE support.
2558          */
2559         if (vpd->rev.feaLevelHigh >= 0x02) {
2560                 npr->ConfmComplAllowed = 1;
2561                 npr->Retry = 1;
2562                 npr->TaskRetryIdReq = 1;
2563         }
2564
2565         npr->acceptRspCode = PRLI_REQ_EXECUTED;
2566         npr->estabImagePair = 1;
2567         npr->readXferRdyDis = 1;
2568         npr->ConfmComplAllowed = 1;
2569
2570         npr->prliType = PRLI_FCP_TYPE;
2571         npr->initiatorFunc = 1;
2572
2573         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2574                 "Issue ACC PRLI:  did:x%x flg:x%x",
2575                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2576
2577         phba->fc_stat.elsXmitACC++;
2578         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2579
2580         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2581         if (rc == IOCB_ERROR) {
2582                 lpfc_els_free_iocb(phba, elsiocb);
2583                 return 1;
2584         }
2585         return 0;
2586 }
2587
2588 static int
2589 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
2590                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
2591 {
2592         struct lpfc_hba  *phba = vport->phba;
2593         RNID *rn;
2594         IOCB_t *icmd, *oldcmd;
2595         struct lpfc_iocbq *elsiocb;
2596         struct lpfc_sli_ring *pring;
2597         struct lpfc_sli *psli;
2598         uint8_t *pcmd;
2599         uint16_t cmdsize;
2600         int rc;
2601
2602         psli = &phba->sli;
2603         pring = &psli->ring[LPFC_ELS_RING];
2604
2605         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
2606                                         + (2 * sizeof(struct lpfc_name));
2607         if (format)
2608                 cmdsize += sizeof(RNID_TOP_DISC);
2609
2610         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
2611                                      ndlp->nlp_DID, ELS_CMD_ACC);
2612         if (!elsiocb)
2613                 return 1;
2614
2615         icmd = &elsiocb->iocb;
2616         oldcmd = &oldiocb->iocb;
2617         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2618         /* Xmit RNID ACC response tag <ulpIoTag> */
2619         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2620                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
2621                          elsiocb->iotag, elsiocb->iocb.ulpContext);
2622         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2623         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2624         pcmd += sizeof(uint32_t);
2625
2626         memset(pcmd, 0, sizeof(RNID));
2627         rn = (RNID *) (pcmd);
2628         rn->Format = format;
2629         rn->CommonLen = (2 * sizeof(struct lpfc_name));
2630         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2631         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2632         switch (format) {
2633         case 0:
2634                 rn->SpecificLen = 0;
2635                 break;
2636         case RNID_TOPOLOGY_DISC:
2637                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
2638                 memcpy(&rn->un.topologyDisc.portName,
2639                        &vport->fc_portname, sizeof(struct lpfc_name));
2640                 rn->un.topologyDisc.unitType = RNID_HBA;
2641                 rn->un.topologyDisc.physPort = 0;
2642                 rn->un.topologyDisc.attachedNodes = 0;
2643                 break;
2644         default:
2645                 rn->CommonLen = 0;
2646                 rn->SpecificLen = 0;
2647                 break;
2648         }
2649
2650         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2651                 "Issue ACC RNID:  did:x%x flg:x%x",
2652                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
2653
2654         phba->fc_stat.elsXmitACC++;
2655         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
2656         lpfc_nlp_put(ndlp);
2657         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
2658                                     * it could be freed */
2659
2660         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2661         if (rc == IOCB_ERROR) {
2662                 lpfc_els_free_iocb(phba, elsiocb);
2663                 return 1;
2664         }
2665         return 0;
2666 }
2667
2668 int
2669 lpfc_els_disc_adisc(struct lpfc_vport *vport)
2670 {
2671         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2672         struct lpfc_nodelist *ndlp, *next_ndlp;
2673         int sentadisc = 0;
2674
2675         /* go thru NPR nodes and issue any remaining ELS ADISCs */
2676         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2677                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2678                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
2679                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
2680                         spin_lock_irq(shost->host_lock);
2681                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2682                         spin_unlock_irq(shost->host_lock);
2683                         ndlp->nlp_prev_state = ndlp->nlp_state;
2684                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2685                         lpfc_issue_els_adisc(vport, ndlp, 0);
2686                         sentadisc++;
2687                         vport->num_disc_nodes++;
2688                         if (vport->num_disc_nodes >=
2689                             vport->cfg_discovery_threads) {
2690                                 spin_lock_irq(shost->host_lock);
2691                                 vport->fc_flag |= FC_NLP_MORE;
2692                                 spin_unlock_irq(shost->host_lock);
2693                                 break;
2694                         }
2695                 }
2696         }
2697         if (sentadisc == 0) {
2698                 spin_lock_irq(shost->host_lock);
2699                 vport->fc_flag &= ~FC_NLP_MORE;
2700                 spin_unlock_irq(shost->host_lock);
2701         }
2702         return sentadisc;
2703 }
2704
2705 int
2706 lpfc_els_disc_plogi(struct lpfc_vport *vport)
2707 {
2708         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2709         struct lpfc_nodelist *ndlp, *next_ndlp;
2710         int sentplogi = 0;
2711
2712         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
2713         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2714                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2715                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
2716                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
2717                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
2718                         ndlp->nlp_prev_state = ndlp->nlp_state;
2719                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2720                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2721                         sentplogi++;
2722                         vport->num_disc_nodes++;
2723                         if (vport->num_disc_nodes >=
2724                             vport->cfg_discovery_threads) {
2725                                 spin_lock_irq(shost->host_lock);
2726                                 vport->fc_flag |= FC_NLP_MORE;
2727                                 spin_unlock_irq(shost->host_lock);
2728                                 break;
2729                         }
2730                 }
2731         }
2732         if (sentplogi) {
2733                 lpfc_set_disctmo(vport);
2734         }
2735         else {
2736                 spin_lock_irq(shost->host_lock);
2737                 vport->fc_flag &= ~FC_NLP_MORE;
2738                 spin_unlock_irq(shost->host_lock);
2739         }
2740         return sentplogi;
2741 }
2742
2743 void
2744 lpfc_els_flush_rscn(struct lpfc_vport *vport)
2745 {
2746         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2747         struct lpfc_hba  *phba = vport->phba;
2748         int i;
2749
2750         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2751                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
2752                 vport->fc_rscn_id_list[i] = NULL;
2753         }
2754         spin_lock_irq(shost->host_lock);
2755         vport->fc_rscn_id_cnt = 0;
2756         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
2757         spin_unlock_irq(shost->host_lock);
2758         lpfc_can_disctmo(vport);
2759 }
2760
2761 int
2762 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
2763 {
2764         D_ID ns_did;
2765         D_ID rscn_did;
2766         uint32_t *lp;
2767         uint32_t payload_len, i;
2768
2769         ns_did.un.word = did;
2770
2771         /* Never match fabric nodes for RSCNs */
2772         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2773                 return 0;
2774
2775         /* If we are doing a FULL RSCN rediscovery, match everything */
2776         if (vport->fc_flag & FC_RSCN_DISCOVERY)
2777                 return did;
2778
2779         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
2780                 lp = vport->fc_rscn_id_list[i]->virt;
2781                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
2782                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
2783                 while (payload_len) {
2784                         rscn_did.un.word = be32_to_cpu(*lp++);
2785                         payload_len -= sizeof(uint32_t);
2786                         switch (rscn_did.un.b.resv) {
2787                         case 0: /* Single N_Port ID effected */
2788                                 if (ns_did.un.word == rscn_did.un.word)
2789                                         return did;
2790                                 break;
2791                         case 1: /* Whole N_Port Area effected */
2792                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
2793                                     && (ns_did.un.b.area == rscn_did.un.b.area))
2794                                         return did;
2795                                 break;
2796                         case 2: /* Whole N_Port Domain effected */
2797                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
2798                                         return did;
2799                                 break;
2800                         default:
2801                                 /* Unknown Identifier in RSCN node */
2802                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2803                                                  "0217 Unknown Identifier in "
2804                                                  "RSCN payload Data: x%x\n",
2805                                                  rscn_did.un.word);
2806                         case 3: /* Whole Fabric effected */
2807                                 return did;
2808                         }
2809                 }
2810         }
2811         return 0;
2812 }
2813
2814 static int
2815 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
2816 {
2817         struct lpfc_nodelist *ndlp = NULL;
2818
2819         /* Look at all nodes effected by pending RSCNs and move
2820          * them to NPR state.
2821          */
2822
2823         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2824                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE ||
2825                     lpfc_rscn_payload_check(vport, ndlp->nlp_DID) == 0)
2826                         continue;
2827
2828                 lpfc_disc_state_machine(vport, ndlp, NULL,
2829                                                 NLP_EVT_DEVICE_RECOVERY);
2830
2831                 /*
2832                  * Make sure NLP_DELAY_TMO is NOT running after a device
2833                  * recovery event.
2834                  */
2835                 if (ndlp->nlp_flag & NLP_DELAY_TMO)
2836                         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2837         }
2838
2839         return 0;
2840 }
2841
2842 static int
2843 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
2844                   struct lpfc_nodelist *ndlp)
2845 {
2846         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2847         struct lpfc_hba  *phba = vport->phba;
2848         struct lpfc_dmabuf *pcmd;
2849         uint32_t *lp, *datap;
2850         IOCB_t *icmd;
2851         uint32_t payload_len, length, nportid, *cmd;
2852         int rscn_cnt = vport->fc_rscn_id_cnt;
2853         int rscn_id = 0, hba_id = 0;
2854         int i;
2855
2856         icmd = &cmdiocb->iocb;
2857         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2858         lp = (uint32_t *) pcmd->virt;
2859
2860         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
2861         payload_len -= sizeof(uint32_t);        /* take off word 0 */
2862         /* RSCN received */
2863         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2864                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
2865                          vport->fc_flag, payload_len, *lp, rscn_cnt);
2866         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
2867                 fc_host_post_event(shost, fc_get_event_number(),
2868                         FCH_EVT_RSCN, lp[i]);
2869
2870         /* If we are about to begin discovery, just ACC the RSCN.
2871          * Discovery processing will satisfy it.
2872          */
2873         if (vport->port_state <= LPFC_NS_QRY) {
2874                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2875                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
2876                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2877
2878                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2879                 return 0;
2880         }
2881
2882         /* If this RSCN just contains NPortIDs for other vports on this HBA,
2883          * just ACC and ignore it.
2884          */
2885         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2886                 !(vport->cfg_peer_port_login)) {
2887                 i = payload_len;
2888                 datap = lp;
2889                 while (i > 0) {
2890                         nportid = *datap++;
2891                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
2892                         i -= sizeof(uint32_t);
2893                         rscn_id++;
2894                         if (lpfc_find_vport_by_did(phba, nportid))
2895                                 hba_id++;
2896                 }
2897                 if (rscn_id == hba_id) {
2898                         /* ALL NPortIDs in RSCN are on HBA */
2899                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2900                                          "0214 Ignore RSCN "
2901                                          "Data: x%x x%x x%x x%x\n",
2902                                          vport->fc_flag, payload_len,
2903                                          *lp, rscn_cnt);
2904                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2905                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
2906                                 ndlp->nlp_DID, vport->port_state,
2907                                 ndlp->nlp_flag);
2908
2909                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
2910                                 ndlp, NULL);
2911                         return 0;
2912                 }
2913         }
2914
2915         /* If we are already processing an RSCN, save the received
2916          * RSCN payload buffer, cmdiocb->context2 to process later.
2917          */
2918         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
2919                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2920                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
2921                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2922
2923                 vport->fc_flag |= FC_RSCN_DEFERRED;
2924                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
2925                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
2926                         spin_lock_irq(shost->host_lock);
2927                         vport->fc_flag |= FC_RSCN_MODE;
2928                         spin_unlock_irq(shost->host_lock);
2929                         if (rscn_cnt) {
2930                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
2931                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
2932                         }
2933                         if ((rscn_cnt) &&
2934                             (payload_len + length <= LPFC_BPL_SIZE)) {
2935                                 *cmd &= ELS_CMD_MASK;
2936                                 *cmd |= be32_to_cpu(payload_len + length);
2937                                 memcpy(((uint8_t *)cmd) + length, lp,
2938                                        payload_len);
2939                         } else {
2940                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
2941                                 vport->fc_rscn_id_cnt++;
2942                                 /* If we zero, cmdiocb->context2, the calling
2943                                  * routine will not try to free it.
2944                                  */
2945                                 cmdiocb->context2 = NULL;
2946                         }
2947
2948                         /* Deferred RSCN */
2949                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2950                                          "0235 Deferred RSCN "
2951                                          "Data: x%x x%x x%x\n",
2952                                          vport->fc_rscn_id_cnt, vport->fc_flag,
2953                                          vport->port_state);
2954                 } else {
2955                         spin_lock_irq(shost->host_lock);
2956                         vport->fc_flag |= FC_RSCN_DISCOVERY;
2957                         spin_unlock_irq(shost->host_lock);
2958                         /* ReDiscovery RSCN */
2959                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2960                                          "0234 ReDiscovery RSCN "
2961                                          "Data: x%x x%x x%x\n",
2962                                          vport->fc_rscn_id_cnt, vport->fc_flag,
2963                                          vport->port_state);
2964                 }
2965                 /* Send back ACC */
2966                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2967
2968                 /* send RECOVERY event for ALL nodes that match RSCN payload */
2969                 lpfc_rscn_recovery_check(vport);
2970                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
2971                 return 0;
2972         }
2973
2974         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
2975                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
2976                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
2977
2978         spin_lock_irq(shost->host_lock);
2979         vport->fc_flag |= FC_RSCN_MODE;
2980         spin_unlock_irq(shost->host_lock);
2981         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
2982         /*
2983          * If we zero, cmdiocb->context2, the calling routine will
2984          * not try to free it.
2985          */
2986         cmdiocb->context2 = NULL;
2987
2988         lpfc_set_disctmo(vport);
2989
2990         /* Send back ACC */
2991         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2992
2993         /* send RECOVERY event for ALL nodes that match RSCN payload */
2994         lpfc_rscn_recovery_check(vport);
2995
2996         return lpfc_els_handle_rscn(vport);
2997 }
2998
2999 int
3000 lpfc_els_handle_rscn(struct lpfc_vport *vport)
3001 {
3002         struct lpfc_nodelist *ndlp;
3003         struct lpfc_hba *phba = vport->phba;
3004
3005         /* Ignore RSCN if the port is being torn down. */
3006         if (vport->load_flag & FC_UNLOADING) {
3007                 lpfc_els_flush_rscn(vport);
3008                 return 0;
3009         }
3010
3011         /* Start timer for RSCN processing */
3012         lpfc_set_disctmo(vport);
3013
3014         /* RSCN processed */
3015         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3016                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
3017                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
3018                          vport->port_state);
3019
3020         /* To process RSCN, first compare RSCN data with NameServer */
3021         vport->fc_ns_retry = 0;
3022         ndlp = lpfc_findnode_did(vport, NameServer_DID);
3023         if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
3024                 /* Good ndlp, issue CT Request to NameServer */
3025                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
3026                         /* Wait for NameServer query cmpl before we can
3027                            continue */
3028                         return 1;
3029         } else {
3030                 /* If login to NameServer does not exist, issue one */
3031                 /* Good status, issue PLOGI to NameServer */
3032                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
3033                 if (ndlp)
3034                         /* Wait for NameServer login cmpl before we can
3035                            continue */
3036                         return 1;
3037
3038                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3039                 if (!ndlp) {
3040                         lpfc_els_flush_rscn(vport);
3041                         return 0;
3042                 } else {
3043                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
3044                         ndlp->nlp_type |= NLP_FABRIC;
3045                         ndlp->nlp_prev_state = ndlp->nlp_state;
3046                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3047                         lpfc_issue_els_plogi(vport, NameServer_DID, 0);
3048                         /* Wait for NameServer login cmpl before we can
3049                            continue */
3050                         return 1;
3051                 }
3052         }
3053
3054         lpfc_els_flush_rscn(vport);
3055         return 0;
3056 }
3057
3058 static int
3059 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3060                    struct lpfc_nodelist *ndlp)
3061 {
3062         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3063         struct lpfc_hba  *phba = vport->phba;
3064         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3065         uint32_t *lp = (uint32_t *) pcmd->virt;
3066         IOCB_t *icmd = &cmdiocb->iocb;
3067         struct serv_parm *sp;
3068         LPFC_MBOXQ_t *mbox;
3069         struct ls_rjt stat;
3070         uint32_t cmd, did;
3071         int rc;
3072
3073         cmd = *lp++;
3074         sp = (struct serv_parm *) lp;
3075
3076         /* FLOGI received */
3077
3078         lpfc_set_disctmo(vport);
3079
3080         if (phba->fc_topology == TOPOLOGY_LOOP) {
3081                 /* We should never receive a FLOGI in loop mode, ignore it */
3082                 did = icmd->un.elsreq64.remoteID;
3083
3084                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
3085                    Loop Mode */
3086                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3087                                  "0113 An FLOGI ELS command x%x was "
3088                                  "received from DID x%x in Loop Mode\n",
3089                                  cmd, did);
3090                 return 1;
3091         }
3092
3093         did = Fabric_DID;
3094
3095         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
3096                 /* For a FLOGI we accept, then if our portname is greater
3097                  * then the remote portname we initiate Nport login.
3098                  */
3099
3100                 rc = memcmp(&vport->fc_portname, &sp->portName,
3101                             sizeof(struct lpfc_name));
3102
3103                 if (!rc) {
3104                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3105                         if (!mbox)
3106                                 return 1;
3107
3108                         lpfc_linkdown(phba);
3109                         lpfc_init_link(phba, mbox,
3110                                        phba->cfg_topology,
3111                                        phba->cfg_link_speed);
3112                         mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
3113                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3114                         mbox->vport = vport;
3115                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3116                         lpfc_set_loopback_flag(phba);
3117                         if (rc == MBX_NOT_FINISHED) {
3118                                 mempool_free(mbox, phba->mbox_mem_pool);
3119                         }
3120                         return 1;
3121                 } else if (rc > 0) {    /* greater than */
3122                         spin_lock_irq(shost->host_lock);
3123                         vport->fc_flag |= FC_PT2PT_PLOGI;
3124                         spin_unlock_irq(shost->host_lock);
3125                 }
3126                 spin_lock_irq(shost->host_lock);
3127                 vport->fc_flag |= FC_PT2PT;
3128                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3129                 spin_unlock_irq(shost->host_lock);
3130         } else {
3131                 /* Reject this request because invalid parameters */
3132                 stat.un.b.lsRjtRsvd0 = 0;
3133                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3134                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
3135                 stat.un.b.vendorUnique = 0;
3136                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3137                         NULL);
3138                 return 1;
3139         }
3140
3141         /* Send back ACC */
3142         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
3143
3144         return 0;
3145 }
3146
3147 static int
3148 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3149                   struct lpfc_nodelist *ndlp)
3150 {
3151         struct lpfc_dmabuf *pcmd;
3152         uint32_t *lp;
3153         IOCB_t *icmd;
3154         RNID *rn;
3155         struct ls_rjt stat;
3156         uint32_t cmd, did;
3157
3158         icmd = &cmdiocb->iocb;
3159         did = icmd->un.elsreq64.remoteID;
3160         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3161         lp = (uint32_t *) pcmd->virt;
3162
3163         cmd = *lp++;
3164         rn = (RNID *) lp;
3165
3166         /* RNID received */
3167
3168         switch (rn->Format) {
3169         case 0:
3170         case RNID_TOPOLOGY_DISC:
3171                 /* Send back ACC */
3172                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
3173                 break;
3174         default:
3175                 /* Reject this request because format not supported */
3176                 stat.un.b.lsRjtRsvd0 = 0;
3177                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3178                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3179                 stat.un.b.vendorUnique = 0;
3180                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3181                         NULL);
3182         }
3183         return 0;
3184 }
3185
3186 static int
3187 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3188                   struct lpfc_nodelist *ndlp)
3189 {
3190         struct ls_rjt stat;
3191
3192         /* For now, unconditionally reject this command */
3193         stat.un.b.lsRjtRsvd0 = 0;
3194         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3195         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3196         stat.un.b.vendorUnique = 0;
3197         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3198         return 0;
3199 }
3200
3201 static void
3202 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3203 {
3204         struct lpfc_sli *psli = &phba->sli;
3205         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3206         MAILBOX_t *mb;
3207         IOCB_t *icmd;
3208         RPS_RSP *rps_rsp;
3209         uint8_t *pcmd;
3210         struct lpfc_iocbq *elsiocb;
3211         struct lpfc_nodelist *ndlp;
3212         uint16_t xri, status;
3213         uint32_t cmdsize;
3214
3215         mb = &pmb->mb;
3216
3217         ndlp = (struct lpfc_nodelist *) pmb->context2;
3218         xri = (uint16_t) ((unsigned long)(pmb->context1));
3219         pmb->context1 = NULL;
3220         pmb->context2 = NULL;
3221
3222         if (mb->mbxStatus) {
3223                 mempool_free(pmb, phba->mbox_mem_pool);
3224                 return;
3225         }
3226
3227         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
3228         mempool_free(pmb, phba->mbox_mem_pool);
3229         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
3230                                      lpfc_max_els_tries, ndlp,
3231                                      ndlp->nlp_DID, ELS_CMD_ACC);
3232         lpfc_nlp_put(ndlp);
3233         if (!elsiocb)
3234                 return;
3235
3236         icmd = &elsiocb->iocb;
3237         icmd->ulpContext = xri;
3238
3239         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3240         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3241         pcmd += sizeof(uint32_t); /* Skip past command */
3242         rps_rsp = (RPS_RSP *)pcmd;
3243
3244         if (phba->fc_topology != TOPOLOGY_LOOP)
3245                 status = 0x10;
3246         else
3247                 status = 0x8;
3248         if (phba->pport->fc_flag & FC_FABRIC)
3249                 status |= 0x4;
3250
3251         rps_rsp->rsvd1 = 0;
3252         rps_rsp->portStatus = be16_to_cpu(status);
3253         rps_rsp->linkFailureCnt = be32_to_cpu(mb->un.varRdLnk.linkFailureCnt);
3254         rps_rsp->lossSyncCnt = be32_to_cpu(mb->un.varRdLnk.lossSyncCnt);
3255         rps_rsp->lossSignalCnt = be32_to_cpu(mb->un.varRdLnk.lossSignalCnt);
3256         rps_rsp->primSeqErrCnt = be32_to_cpu(mb->un.varRdLnk.primSeqErrCnt);
3257         rps_rsp->invalidXmitWord = be32_to_cpu(mb->un.varRdLnk.invalidXmitWord);
3258         rps_rsp->crcCnt = be32_to_cpu(mb->un.varRdLnk.crcCnt);
3259         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
3260         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
3261                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
3262                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3263                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3264                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3265                          ndlp->nlp_rpi);
3266         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3267         phba->fc_stat.elsXmitACC++;
3268         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
3269                 lpfc_els_free_iocb(phba, elsiocb);
3270         return;
3271 }
3272
3273 static int
3274 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3275                  struct lpfc_nodelist *ndlp)
3276 {
3277         struct lpfc_hba *phba = vport->phba;
3278         uint32_t *lp;
3279         uint8_t flag;
3280         LPFC_MBOXQ_t *mbox;
3281         struct lpfc_dmabuf *pcmd;
3282         RPS *rps;
3283         struct ls_rjt stat;
3284
3285         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3286             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3287                 stat.un.b.lsRjtRsvd0 = 0;
3288                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3289                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3290                 stat.un.b.vendorUnique = 0;
3291                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3292                         NULL);
3293         }
3294
3295         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3296         lp = (uint32_t *) pcmd->virt;
3297         flag = (be32_to_cpu(*lp++) & 0xf);
3298         rps = (RPS *) lp;
3299
3300         if ((flag == 0) ||
3301             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
3302             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
3303                                     sizeof(struct lpfc_name)) == 0))) {
3304
3305                 printk("Fix me....\n");
3306                 dump_stack();
3307                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
3308                 if (mbox) {
3309                         lpfc_read_lnk_stat(phba, mbox);
3310                         mbox->context1 =
3311                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
3312                         mbox->context2 = lpfc_nlp_get(ndlp);
3313                         mbox->vport = vport;
3314                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
3315                         if (lpfc_sli_issue_mbox (phba, mbox, MBX_NOWAIT)
3316                                 != MBX_NOT_FINISHED)
3317                                 /* Mbox completion will send ELS Response */
3318                                 return 0;
3319
3320                         lpfc_nlp_put(ndlp);
3321                         mempool_free(mbox, phba->mbox_mem_pool);
3322                 }
3323         }
3324         stat.un.b.lsRjtRsvd0 = 0;
3325         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3326         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3327         stat.un.b.vendorUnique = 0;
3328         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
3329         return 0;
3330 }
3331
3332 static int
3333 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
3334                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3335 {
3336         struct lpfc_hba *phba = vport->phba;
3337         IOCB_t *icmd, *oldcmd;
3338         RPL_RSP rpl_rsp;
3339         struct lpfc_iocbq *elsiocb;
3340         struct lpfc_sli *psli = &phba->sli;
3341         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3342         uint8_t *pcmd;
3343
3344         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3345                                      ndlp->nlp_DID, ELS_CMD_ACC);
3346
3347         if (!elsiocb)
3348                 return 1;
3349
3350         icmd = &elsiocb->iocb;
3351         oldcmd = &oldiocb->iocb;
3352         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3353
3354         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3355         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3356         pcmd += sizeof(uint16_t);
3357         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
3358         pcmd += sizeof(uint16_t);
3359
3360         /* Setup the RPL ACC payload */
3361         rpl_rsp.listLen = be32_to_cpu(1);
3362         rpl_rsp.index = 0;
3363         rpl_rsp.port_num_blk.portNum = 0;
3364         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
3365         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
3366             sizeof(struct lpfc_name));
3367         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
3368         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
3369         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3370                          "0120 Xmit ELS RPL ACC response tag x%x "
3371                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3372                          "rpi x%x\n",
3373                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3374                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3375                          ndlp->nlp_rpi);
3376         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3377         phba->fc_stat.elsXmitACC++;
3378         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
3379                 lpfc_els_free_iocb(phba, elsiocb);
3380                 return 1;
3381         }
3382         return 0;
3383 }
3384
3385 static int
3386 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3387                  struct lpfc_nodelist *ndlp)
3388 {
3389         struct lpfc_dmabuf *pcmd;
3390         uint32_t *lp;
3391         uint32_t maxsize;
3392         uint16_t cmdsize;
3393         RPL *rpl;
3394         struct ls_rjt stat;
3395
3396         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3397             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
3398                 stat.un.b.lsRjtRsvd0 = 0;
3399                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3400                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
3401                 stat.un.b.vendorUnique = 0;
3402                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
3403                         NULL);
3404         }
3405
3406         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3407         lp = (uint32_t *) pcmd->virt;
3408         rpl = (RPL *) (lp + 1);
3409
3410         maxsize = be32_to_cpu(rpl->maxsize);
3411
3412         /* We support only one port */
3413         if ((rpl->index == 0) &&
3414             ((maxsize == 0) ||
3415              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
3416                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
3417         } else {
3418                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
3419         }
3420         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
3421
3422         return 0;
3423 }
3424
3425 static int
3426 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3427                   struct lpfc_nodelist *ndlp)
3428 {
3429         struct lpfc_dmabuf *pcmd;
3430         uint32_t *lp;
3431         IOCB_t *icmd;
3432         FARP *fp;
3433         uint32_t cmd, cnt, did;
3434
3435         icmd = &cmdiocb->iocb;
3436         did = icmd->un.elsreq64.remoteID;
3437         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3438         lp = (uint32_t *) pcmd->virt;
3439
3440         cmd = *lp++;
3441         fp = (FARP *) lp;
3442         /* FARP-REQ received from DID <did> */
3443         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3444                          "0601 FARP-REQ received from DID x%x\n", did);
3445         /* We will only support match on WWPN or WWNN */
3446         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
3447                 return 0;
3448         }
3449
3450         cnt = 0;
3451         /* If this FARP command is searching for my portname */
3452         if (fp->Mflags & FARP_MATCH_PORT) {
3453                 if (memcmp(&fp->RportName, &vport->fc_portname,
3454                            sizeof(struct lpfc_name)) == 0)
3455                         cnt = 1;
3456         }
3457
3458         /* If this FARP command is searching for my nodename */
3459         if (fp->Mflags & FARP_MATCH_NODE) {
3460                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
3461                            sizeof(struct lpfc_name)) == 0)
3462                         cnt = 1;
3463         }
3464
3465         if (cnt) {
3466                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
3467                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
3468                         /* Log back into the node before sending the FARP. */
3469                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
3470                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3471                                 lpfc_nlp_set_state(vport, ndlp,
3472                                                    NLP_STE_PLOGI_ISSUE);
3473                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3474                         }
3475
3476                         /* Send a FARP response to that node */
3477                         if (fp->Rflags & FARP_REQUEST_FARPR)
3478                                 lpfc_issue_els_farpr(vport, did, 0);
3479                 }
3480         }
3481         return 0;
3482 }
3483
3484 static int
3485 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3486                    struct lpfc_nodelist  *ndlp)
3487 {
3488         struct lpfc_dmabuf *pcmd;
3489         uint32_t *lp;
3490         IOCB_t *icmd;
3491         uint32_t cmd, did;
3492
3493         icmd = &cmdiocb->iocb;
3494         did = icmd->un.elsreq64.remoteID;
3495         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3496         lp = (uint32_t *) pcmd->virt;
3497
3498         cmd = *lp++;
3499         /* FARP-RSP received from DID <did> */
3500         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3501                          "0600 FARP-RSP received from DID x%x\n", did);
3502         /* ACCEPT the Farp resp request */
3503         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
3504
3505         return 0;
3506 }
3507
3508 static int
3509 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3510                  struct lpfc_nodelist *fan_ndlp)
3511 {
3512         struct lpfc_dmabuf *pcmd;
3513         uint32_t *lp;
3514         IOCB_t *icmd;
3515         uint32_t cmd, did;
3516         FAN *fp;
3517         struct lpfc_nodelist *ndlp, *next_ndlp;
3518         struct lpfc_hba *phba = vport->phba;
3519
3520         /* FAN received */
3521         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3522                          "0265 FAN received\n");
3523         icmd = &cmdiocb->iocb;
3524         did = icmd->un.elsreq64.remoteID;
3525         pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3526         lp = (uint32_t *)pcmd->virt;
3527
3528         cmd = *lp++;
3529         fp = (FAN *) lp;
3530
3531         /* FAN received; Fan does not have a reply sequence */
3532
3533         if (phba->pport->port_state == LPFC_LOCAL_CFG_LINK) {
3534                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
3535                         sizeof(struct lpfc_name)) != 0) ||
3536                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
3537                         sizeof(struct lpfc_name)) != 0)) {
3538                         /*
3539                          * This node has switched fabrics.  FLOGI is required
3540                          * Clean up the old rpi's
3541                          */
3542
3543                         list_for_each_entry_safe(ndlp, next_ndlp,
3544                                                  &vport->fc_nodes, nlp_listp) {
3545                                 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3546                                         continue;
3547                                 if (ndlp->nlp_type & NLP_FABRIC) {
3548                                         /*
3549                                          * Clean up old Fabric, Nameserver and
3550                                          * other NLP_FABRIC logins
3551                                          */
3552                                         lpfc_drop_node(vport, ndlp);
3553
3554                                 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3555                                         /* Fail outstanding I/O now since this
3556                                          * device is marked for PLOGI
3557                                          */
3558                                         lpfc_unreg_rpi(vport, ndlp);
3559                                 }
3560                         }
3561
3562                         lpfc_initial_flogi(vport);
3563                         return 0;
3564                 }
3565                 /* Discovery not needed,
3566                  * move the nodes to their original state.
3567                  */
3568                 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3569                                          nlp_listp) {
3570                         if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3571                                 continue;
3572
3573                         switch (ndlp->nlp_prev_state) {
3574                         case NLP_STE_UNMAPPED_NODE:
3575                                 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
3576                                 lpfc_nlp_set_state(vport, ndlp,
3577                                                    NLP_STE_UNMAPPED_NODE);
3578                                 break;
3579
3580                         case NLP_STE_MAPPED_NODE:
3581                                 ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
3582                                 lpfc_nlp_set_state(vport, ndlp,
3583                                                    NLP_STE_MAPPED_NODE);
3584                                 break;
3585
3586                         default:
3587                                 break;
3588                         }
3589                 }
3590
3591                 /* Start discovery - this should just do CLEAR_LA */
3592                 lpfc_disc_start(vport);
3593         }
3594         return 0;
3595 }
3596
3597 void
3598 lpfc_els_timeout(unsigned long ptr)
3599 {
3600         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3601         struct lpfc_hba   *phba = vport->phba;
3602         unsigned long iflag;
3603
3604         spin_lock_irqsave(&vport->work_port_lock, iflag);
3605         if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
3606                 vport->work_port_events |= WORKER_ELS_TMO;
3607                 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3608
3609                 spin_lock_irqsave(&phba->hbalock, iflag);
3610                 if (phba->work_wait)
3611                         lpfc_worker_wake_up(phba);
3612                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3613         }
3614         else
3615                 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3616         return;
3617 }
3618
3619 void
3620 lpfc_els_timeout_handler(struct lpfc_vport *vport)
3621 {
3622         struct lpfc_hba  *phba = vport->phba;
3623         struct lpfc_sli_ring *pring;
3624         struct lpfc_iocbq *tmp_iocb, *piocb;
3625         IOCB_t *cmd = NULL;
3626         struct lpfc_dmabuf *pcmd;
3627         uint32_t els_command = 0;
3628         uint32_t timeout;
3629         uint32_t remote_ID = 0xffffffff;
3630
3631         /* If the timer is already canceled do nothing */
3632         if ((vport->work_port_events & WORKER_ELS_TMO) == 0) {
3633                 return;
3634         }
3635         spin_lock_irq(&phba->hbalock);
3636         timeout = (uint32_t)(phba->fc_ratov << 1);
3637
3638         pring = &phba->sli.ring[LPFC_ELS_RING];
3639
3640         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3641                 cmd = &piocb->iocb;
3642
3643                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
3644                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
3645                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
3646                         continue;
3647
3648                 if (piocb->vport != vport)
3649                         continue;
3650
3651                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
3652                 if (pcmd)
3653                         els_command = *(uint32_t *) (pcmd->virt);
3654
3655                 if (els_command == ELS_CMD_FARP ||
3656                     els_command == ELS_CMD_FARPR ||
3657                     els_command == ELS_CMD_FDISC)
3658                         continue;
3659
3660                 if (vport != piocb->vport)
3661                         continue;
3662
3663                 if (piocb->drvrTimeout > 0) {
3664                         if (piocb->drvrTimeout >= timeout)
3665                                 piocb->drvrTimeout -= timeout;
3666                         else
3667                                 piocb->drvrTimeout = 0;
3668                         continue;
3669                 }
3670
3671                 remote_ID = 0xffffffff;
3672                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
3673                         remote_ID = cmd->un.elsreq64.remoteID;
3674                 else {
3675                         struct lpfc_nodelist *ndlp;
3676                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
3677                         if (ndlp)
3678                                 remote_ID = ndlp->nlp_DID;
3679                 }
3680                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3681                                  "0127 ELS timeout Data: x%x x%x x%x "
3682                                  "x%x\n", els_command,
3683                                  remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
3684                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3685         }
3686         spin_unlock_irq(&phba->hbalock);
3687
3688         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
3689                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
3690 }
3691
3692 void
3693 lpfc_els_flush_cmd(struct lpfc_vport *vport)
3694 {
3695         LIST_HEAD(completions);
3696         struct lpfc_hba  *phba = vport->phba;
3697         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3698         struct lpfc_iocbq *tmp_iocb, *piocb;
3699         IOCB_t *cmd = NULL;
3700
3701         lpfc_fabric_abort_vport(vport);
3702
3703         spin_lock_irq(&phba->hbalock);
3704         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
3705                 cmd = &piocb->iocb;
3706
3707                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3708                         continue;
3709                 }
3710
3711                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
3712                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
3713                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
3714                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3715                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
3716                         continue;
3717
3718                 if (piocb->vport != vport)
3719                         continue;
3720
3721                 list_move_tail(&piocb->list, &completions);
3722                 pring->txq_cnt--;
3723         }
3724
3725         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3726                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3727                         continue;
3728                 }
3729
3730                 if (piocb->vport != vport)
3731                         continue;
3732
3733                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3734         }
3735         spin_unlock_irq(&phba->hbalock);
3736
3737         while (!list_empty(&completions)) {
3738                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
3739                 cmd = &piocb->iocb;
3740                 list_del_init(&piocb->list);
3741
3742                 if (!piocb->iocb_cmpl)
3743                         lpfc_sli_release_iocbq(phba, piocb);
3744                 else {
3745                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3746                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3747                         (piocb->iocb_cmpl) (phba, piocb, piocb);
3748                 }
3749         }
3750
3751         return;
3752 }
3753
3754 void
3755 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
3756 {
3757         LIST_HEAD(completions);
3758         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3759         struct lpfc_iocbq *tmp_iocb, *piocb;
3760         IOCB_t *cmd = NULL;
3761
3762         lpfc_fabric_abort_hba(phba);
3763         spin_lock_irq(&phba->hbalock);
3764         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
3765                 cmd = &piocb->iocb;
3766                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
3767                         continue;
3768                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
3769                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
3770                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
3771                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3772                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
3773                         continue;
3774                 list_move_tail(&piocb->list, &completions);
3775                 pring->txq_cnt--;
3776         }
3777         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3778                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
3779                         continue;
3780                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
3781         }
3782         spin_unlock_irq(&phba->hbalock);
3783         while (!list_empty(&completions)) {
3784                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
3785                 cmd = &piocb->iocb;
3786                 list_del_init(&piocb->list);
3787                 if (!piocb->iocb_cmpl)
3788                         lpfc_sli_release_iocbq(phba, piocb);
3789                 else {
3790                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3791                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3792                         (piocb->iocb_cmpl) (phba, piocb, piocb);
3793                 }
3794         }
3795         return;
3796 }
3797
3798 static void
3799 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3800                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
3801 {
3802         struct Scsi_Host  *shost;
3803         struct lpfc_nodelist *ndlp;
3804         struct ls_rjt stat;
3805         uint32_t *payload;
3806         uint32_t cmd, did, newnode, rjt_err = 0;
3807         IOCB_t *icmd = &elsiocb->iocb;
3808
3809         if (vport == NULL || elsiocb->context2 == NULL)
3810                 goto dropit;
3811
3812         newnode = 0;
3813         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3814         cmd = *payload;
3815         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
3816                 lpfc_post_buffer(phba, pring, 1, 1);
3817
3818         did = icmd->un.rcvels.remoteID;
3819         if (icmd->ulpStatus) {
3820                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3821                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
3822                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
3823                 goto dropit;
3824         }
3825
3826         /* Check to see if link went down during discovery */
3827         if (lpfc_els_chk_latt(vport))
3828                 goto dropit;
3829
3830         /* Ignore traffic recevied during vport shutdown. */
3831         if (vport->load_flag & FC_UNLOADING)
3832                 goto dropit;
3833
3834         ndlp = lpfc_findnode_did(vport, did);
3835         if (!ndlp) {
3836                 /* Cannot find existing Fabric ndlp, so allocate a new one */
3837                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3838                 if (!ndlp)
3839                         goto dropit;
3840
3841                 lpfc_nlp_init(vport, ndlp, did);
3842                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3843                 newnode = 1;
3844                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
3845                         ndlp->nlp_type |= NLP_FABRIC;
3846                 }
3847         }
3848         else {
3849                 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
3850                         /* This is simular to the new node path */
3851                         lpfc_nlp_get(ndlp);
3852                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3853                         newnode = 1;
3854                 }
3855         }
3856
3857         phba->fc_stat.elsRcvFrame++;
3858         if (elsiocb->context1)
3859                 lpfc_nlp_put(elsiocb->context1);
3860         elsiocb->context1 = lpfc_nlp_get(ndlp);
3861         elsiocb->vport = vport;
3862
3863         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
3864                 cmd &= ELS_CMD_MASK;
3865         }
3866         /* ELS command <elsCmd> received from NPORT <did> */
3867         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3868                          "0112 ELS command x%x received from NPORT x%x "
3869                          "Data: x%x\n", cmd, did, vport->port_state);
3870         switch (cmd) {
3871         case ELS_CMD_PLOGI:
3872                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3873                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
3874                         did, vport->port_state, ndlp->nlp_flag);
3875
3876                 phba->fc_stat.elsRcvPLOGI++;
3877                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
3878
3879                 if (vport->port_state < LPFC_DISC_AUTH) {
3880                         rjt_err = LSRJT_UNABLE_TPC;
3881                         break;
3882                 }
3883
3884                 shost = lpfc_shost_from_vport(vport);
3885                 spin_lock_irq(shost->host_lock);
3886                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
3887                 spin_unlock_irq(shost->host_lock);
3888
3889                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3890                                         NLP_EVT_RCV_PLOGI);
3891
3892                 break;
3893         case ELS_CMD_FLOGI:
3894                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3895                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
3896                         did, vport->port_state, ndlp->nlp_flag);
3897
3898                 phba->fc_stat.elsRcvFLOGI++;
3899                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
3900                 if (newnode)
3901                         lpfc_nlp_put(ndlp);
3902                 break;
3903         case ELS_CMD_LOGO:
3904                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3905                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
3906                         did, vport->port_state, ndlp->nlp_flag);
3907
3908                 phba->fc_stat.elsRcvLOGO++;
3909                 if (vport->port_state < LPFC_DISC_AUTH) {
3910                         rjt_err = LSRJT_UNABLE_TPC;
3911                         break;
3912                 }
3913                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
3914                 break;
3915         case ELS_CMD_PRLO:
3916                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3917                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
3918                         did, vport->port_state, ndlp->nlp_flag);
3919
3920                 phba->fc_stat.elsRcvPRLO++;
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, NLP_EVT_RCV_PRLO);
3926                 break;
3927         case ELS_CMD_RSCN:
3928                 phba->fc_stat.elsRcvRSCN++;
3929                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
3930                 if (newnode)
3931                         lpfc_nlp_put(ndlp);
3932                 break;
3933         case ELS_CMD_ADISC:
3934                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3935                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
3936                         did, vport->port_state, ndlp->nlp_flag);
3937
3938                 phba->fc_stat.elsRcvADISC++;
3939                 if (vport->port_state < LPFC_DISC_AUTH) {
3940                         rjt_err = LSRJT_UNABLE_TPC;
3941                         break;
3942                 }
3943                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3944                                         NLP_EVT_RCV_ADISC);
3945                 break;
3946         case ELS_CMD_PDISC:
3947                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3948                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
3949                         did, vport->port_state, ndlp->nlp_flag);
3950
3951                 phba->fc_stat.elsRcvPDISC++;
3952                 if (vport->port_state < LPFC_DISC_AUTH) {
3953                         rjt_err = LSRJT_UNABLE_TPC;
3954                         break;
3955                 }
3956                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
3957                                         NLP_EVT_RCV_PDISC);
3958                 break;
3959         case ELS_CMD_FARPR:
3960                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3961                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
3962                         did, vport->port_state, ndlp->nlp_flag);
3963
3964                 phba->fc_stat.elsRcvFARPR++;
3965                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
3966                 break;
3967         case ELS_CMD_FARP:
3968                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3969                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
3970                         did, vport->port_state, ndlp->nlp_flag);
3971
3972                 phba->fc_stat.elsRcvFARP++;
3973                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
3974                 break;
3975         case ELS_CMD_FAN:
3976                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3977                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
3978                         did, vport->port_state, ndlp->nlp_flag);
3979
3980                 phba->fc_stat.elsRcvFAN++;
3981                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
3982                 break;
3983         case ELS_CMD_PRLI:
3984                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3985                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
3986                         did, vport->port_state, ndlp->nlp_flag);
3987
3988                 phba->fc_stat.elsRcvPRLI++;
3989                 if (vport->port_state < LPFC_DISC_AUTH) {
3990                         rjt_err = LSRJT_UNABLE_TPC;
3991                         break;
3992                 }
3993                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
3994                 break;
3995         case ELS_CMD_LIRR:
3996                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
3997                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
3998                         did, vport->port_state, ndlp->nlp_flag);
3999
4000                 phba->fc_stat.elsRcvLIRR++;
4001                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
4002                 if (newnode)
4003                         lpfc_nlp_put(ndlp);
4004                 break;
4005         case ELS_CMD_RPS:
4006                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4007                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
4008                         did, vport->port_state, ndlp->nlp_flag);
4009
4010                 phba->fc_stat.elsRcvRPS++;
4011                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
4012                 if (newnode)
4013                         lpfc_nlp_put(ndlp);
4014                 break;
4015         case ELS_CMD_RPL:
4016                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4017                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
4018                         did, vport->port_state, ndlp->nlp_flag);
4019
4020                 phba->fc_stat.elsRcvRPL++;
4021                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
4022                 if (newnode)
4023                         lpfc_nlp_put(ndlp);
4024                 break;
4025         case ELS_CMD_RNID:
4026                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4027                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
4028                         did, vport->port_state, ndlp->nlp_flag);
4029
4030                 phba->fc_stat.elsRcvRNID++;
4031                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
4032                 if (newnode)
4033                         lpfc_nlp_put(ndlp);
4034                 break;
4035         default:
4036                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4037                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
4038                         cmd, did, vport->port_state);
4039
4040                 /* Unsupported ELS command, reject */
4041                 rjt_err = LSRJT_INVALID_CMD;
4042
4043                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
4044                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4045                                  "0115 Unknown ELS command x%x "
4046                                  "received from NPORT x%x\n", cmd, did);
4047                 if (newnode)
4048                         lpfc_nlp_put(ndlp);
4049                 break;
4050         }
4051
4052         /* check if need to LS_RJT received ELS cmd */
4053         if (rjt_err) {
4054                 memset(&stat, 0, sizeof(stat));
4055                 stat.un.b.lsRjtRsnCode = rjt_err;
4056                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
4057                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
4058                         NULL);
4059         }
4060
4061         return;
4062
4063 dropit:
4064         if (vport && !(vport->load_flag & FC_UNLOADING))
4065                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
4066                         "(%d):0111 Dropping received ELS cmd "
4067                         "Data: x%x x%x x%x\n",
4068                         vport->vpi, icmd->ulpStatus,
4069                         icmd->un.ulpWord[4], icmd->ulpTimeout);
4070         phba->fc_stat.elsRcvDrop++;
4071 }
4072
4073 static struct lpfc_vport *
4074 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
4075 {
4076         struct lpfc_vport *vport;
4077         unsigned long flags;
4078
4079         spin_lock_irqsave(&phba->hbalock, flags);
4080         list_for_each_entry(vport, &phba->port_list, listentry) {
4081                 if (vport->vpi == vpi) {
4082                         spin_unlock_irqrestore(&phba->hbalock, flags);
4083                         return vport;
4084                 }
4085         }
4086         spin_unlock_irqrestore(&phba->hbalock, flags);
4087         return NULL;
4088 }
4089
4090 void
4091 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4092                      struct lpfc_iocbq *elsiocb)
4093 {
4094         struct lpfc_vport *vport = phba->pport;
4095         IOCB_t *icmd = &elsiocb->iocb;
4096         dma_addr_t paddr;
4097         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
4098         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
4099
4100         elsiocb->context2 = NULL;
4101         elsiocb->context3 = NULL;
4102
4103         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
4104                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
4105         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
4106             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
4107                 phba->fc_stat.NoRcvBuf++;
4108                 /* Not enough posted buffers; Try posting more buffers */
4109                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
4110                         lpfc_post_buffer(phba, pring, 0, 1);
4111                 return;
4112         }
4113
4114         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4115             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
4116              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
4117                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
4118                         vport = phba->pport;
4119                 else {
4120                         uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
4121                         vport = lpfc_find_vport_by_vpid(phba, vpi);
4122                 }
4123         }
4124                                 /* If there are no BDEs associated
4125                                  * with this IOCB, there is nothing to do.
4126                                  */
4127         if (icmd->ulpBdeCount == 0)
4128                 return;
4129
4130                                 /* type of ELS cmd is first 32bit word
4131                                  * in packet
4132                                  */
4133         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4134                 elsiocb->context2 = bdeBuf1;
4135         } else {
4136                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
4137                                  icmd->un.cont64[0].addrLow);
4138                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
4139                                                              paddr);
4140         }
4141
4142         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4143         /*
4144          * The different unsolicited event handlers would tell us
4145          * if they are done with "mp" by setting context2 to NULL.
4146          */
4147         lpfc_nlp_put(elsiocb->context1);
4148         elsiocb->context1 = NULL;
4149         if (elsiocb->context2) {
4150                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
4151                 elsiocb->context2 = NULL;
4152         }
4153
4154         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
4155         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
4156             icmd->ulpBdeCount == 2) {
4157                 elsiocb->context2 = bdeBuf2;
4158                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
4159                 /* free mp if we are done with it */
4160                 if (elsiocb->context2) {
4161                         lpfc_in_buf_free(phba, elsiocb->context2);
4162                         elsiocb->context2 = NULL;
4163                 }
4164         }
4165 }
4166
4167 void
4168 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4169 {
4170         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
4171
4172         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4173         if (!ndlp) {
4174                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4175                 if (!ndlp) {
4176                         if (phba->fc_topology == TOPOLOGY_LOOP) {
4177                                 lpfc_disc_start(vport);
4178                                 return;
4179                         }
4180                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4181                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4182                                          "0251 NameServer login: no memory\n");
4183                         return;
4184                 }
4185                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
4186                 ndlp->nlp_type |= NLP_FABRIC;
4187         }
4188
4189         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4190
4191         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
4192                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4193                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4194                                  "0252 Cannot issue NameServer login\n");
4195                 return;
4196         }
4197
4198         if (vport->cfg_fdmi_on) {
4199                 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
4200                                           GFP_KERNEL);
4201                 if (ndlp_fdmi) {
4202                         lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
4203                         ndlp_fdmi->nlp_type |= NLP_FABRIC;
4204                         ndlp_fdmi->nlp_state =
4205                                 NLP_STE_PLOGI_ISSUE;
4206                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
4207                                              0);
4208                 }
4209         }
4210         return;
4211 }
4212
4213 static void
4214 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4215 {
4216         struct lpfc_vport *vport = pmb->vport;
4217         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
4218         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4219         MAILBOX_t *mb = &pmb->mb;
4220
4221         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4222         lpfc_nlp_put(ndlp);
4223
4224         if (mb->mbxStatus) {
4225                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4226                                  "0915 Register VPI failed: 0x%x\n",
4227                                  mb->mbxStatus);
4228
4229                 switch (mb->mbxStatus) {
4230                 case 0x11:      /* unsupported feature */
4231                 case 0x9603:    /* max_vpi exceeded */
4232                         /* giving up on vport registration */
4233                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4234                         spin_lock_irq(shost->host_lock);
4235                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4236                         spin_unlock_irq(shost->host_lock);
4237                         lpfc_can_disctmo(vport);
4238                         break;
4239                 default:
4240                         /* Try to recover from this error */
4241                         lpfc_mbx_unreg_vpi(vport);
4242                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4243                         lpfc_initial_fdisc(vport);
4244                         break;
4245                 }
4246
4247         } else {
4248                 if (vport == phba->pport)
4249                         lpfc_issue_fabric_reglogin(vport);
4250                 else
4251                         lpfc_do_scr_ns_plogi(phba, vport);
4252         }
4253         mempool_free(pmb, phba->mbox_mem_pool);
4254         return;
4255 }
4256
4257 void
4258 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
4259                         struct lpfc_nodelist *ndlp)
4260 {
4261         LPFC_MBOXQ_t *mbox;
4262
4263         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4264         if (mbox) {
4265                 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
4266                 mbox->vport = vport;
4267                 mbox->context2 = lpfc_nlp_get(ndlp);
4268                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
4269                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4270                     == MBX_NOT_FINISHED) {
4271                         mempool_free(mbox, phba->mbox_mem_pool);
4272                         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4273
4274                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4275                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4276                                 "0253 Register VPI: Can't send mbox\n");
4277                 }
4278         } else {
4279                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4280
4281                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
4282                                  "0254 Register VPI: no memory\n");
4283
4284                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4285                 lpfc_nlp_put(ndlp);
4286         }
4287 }
4288
4289 static void
4290 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4291                     struct lpfc_iocbq *rspiocb)
4292 {
4293         struct lpfc_vport *vport = cmdiocb->vport;
4294         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
4295         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4296         struct lpfc_nodelist *np;
4297         struct lpfc_nodelist *next_np;
4298         IOCB_t *irsp = &rspiocb->iocb;
4299         struct lpfc_iocbq *piocb;
4300
4301         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4302                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
4303                          irsp->ulpStatus, irsp->un.ulpWord[4],
4304                          vport->fc_prevDID);
4305         /* Since all FDISCs are being single threaded, we
4306          * must reset the discovery timer for ALL vports
4307          * waiting to send FDISC when one completes.
4308          */
4309         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
4310                 lpfc_set_disctmo(piocb->vport);
4311         }
4312
4313         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4314                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
4315                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
4316
4317         if (irsp->ulpStatus) {
4318                 /* Check for retry */
4319                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
4320                         goto out;
4321                 /* FDISC failed */
4322                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4323                                  "0124 FDISC failed. (%d/%d)\n",
4324                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
4325                 if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
4326                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4327
4328                 lpfc_nlp_put(ndlp);
4329                 /* giving up on FDISC. Cancel discovery timer */
4330                 lpfc_can_disctmo(vport);
4331         } else {
4332                 spin_lock_irq(shost->host_lock);
4333                 vport->fc_flag |= FC_FABRIC;
4334                 if (vport->phba->fc_topology == TOPOLOGY_LOOP)
4335                         vport->fc_flag |=  FC_PUBLIC_LOOP;
4336                 spin_unlock_irq(shost->host_lock);
4337
4338                 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
4339                 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
4340                 if ((vport->fc_prevDID != vport->fc_myDID) &&
4341                         !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
4342                         /* If our NportID changed, we need to ensure all
4343                          * remaining NPORTs get unreg_login'ed so we can
4344                          * issue unreg_vpi.
4345                          */
4346                         list_for_each_entry_safe(np, next_np,
4347                                 &vport->fc_nodes, nlp_listp) {
4348                                 if (np->nlp_state != NLP_STE_NPR_NODE
4349                                    || !(np->nlp_flag & NLP_NPR_ADISC))
4350                                         continue;
4351                                 spin_lock_irq(shost->host_lock);
4352                                 np->nlp_flag &= ~NLP_NPR_ADISC;
4353                                 spin_unlock_irq(shost->host_lock);
4354                                 lpfc_unreg_rpi(vport, np);
4355                         }
4356                         lpfc_mbx_unreg_vpi(vport);
4357                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
4358                 }
4359
4360                 if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
4361                         lpfc_register_new_vport(phba, vport, ndlp);
4362                 else
4363                         lpfc_do_scr_ns_plogi(phba, vport);
4364
4365                 lpfc_nlp_put(ndlp); /* Free Fabric ndlp for vports */
4366         }
4367
4368 out:
4369         lpfc_els_free_iocb(phba, cmdiocb);
4370 }
4371
4372 int
4373 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4374                      uint8_t retry)
4375 {
4376         struct lpfc_hba *phba = vport->phba;
4377         IOCB_t *icmd;
4378         struct lpfc_iocbq *elsiocb;
4379         struct serv_parm *sp;
4380         uint8_t *pcmd;
4381         uint16_t cmdsize;
4382         int did = ndlp->nlp_DID;
4383         int rc;
4384
4385         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
4386         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
4387                                      ELS_CMD_FDISC);
4388         if (!elsiocb) {
4389                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4390                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4391                                  "0255 Issue FDISC: no IOCB\n");
4392                 return 1;
4393         }
4394
4395         icmd = &elsiocb->iocb;
4396         icmd->un.elsreq64.myID = 0;
4397         icmd->un.elsreq64.fl = 1;
4398
4399         /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
4400         icmd->ulpCt_h = 1;
4401         icmd->ulpCt_l = 0;
4402
4403         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4404         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
4405         pcmd += sizeof(uint32_t); /* CSP Word 1 */
4406         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
4407         sp = (struct serv_parm *) pcmd;
4408         /* Setup CSPs accordingly for Fabric */
4409         sp->cmn.e_d_tov = 0;
4410         sp->cmn.w2.r_a_tov = 0;
4411         sp->cls1.classValid = 0;
4412         sp->cls2.seqDelivery = 1;
4413         sp->cls3.seqDelivery = 1;
4414
4415         pcmd += sizeof(uint32_t); /* CSP Word 2 */
4416         pcmd += sizeof(uint32_t); /* CSP Word 3 */
4417         pcmd += sizeof(uint32_t); /* CSP Word 4 */
4418         pcmd += sizeof(uint32_t); /* Port Name */
4419         memcpy(pcmd, &vport->fc_portname, 8);
4420         pcmd += sizeof(uint32_t); /* Node Name */
4421         pcmd += sizeof(uint32_t); /* Node Name */
4422         memcpy(pcmd, &vport->fc_nodename, 8);
4423
4424         lpfc_set_disctmo(vport);
4425
4426         phba->fc_stat.elsXmitFDISC++;
4427         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
4428
4429         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4430                 "Issue FDISC:     did:x%x",
4431                 did, 0, 0);
4432
4433         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
4434         if (rc == IOCB_ERROR) {
4435                 lpfc_els_free_iocb(phba, elsiocb);
4436                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4437                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4438                                  "0256 Issue FDISC: Cannot send IOCB\n");
4439                 return 1;
4440         }
4441         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
4442         vport->port_state = LPFC_FDISC;
4443         return 0;
4444 }
4445
4446 static void
4447 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4448                         struct lpfc_iocbq *rspiocb)
4449 {
4450         struct lpfc_vport *vport = cmdiocb->vport;
4451         IOCB_t *irsp;
4452
4453         irsp = &rspiocb->iocb;
4454         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4455                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
4456                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
4457
4458         lpfc_els_free_iocb(phba, cmdiocb);
4459         vport->unreg_vpi_cmpl = VPORT_ERROR;
4460 }
4461
4462 int
4463 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4464 {
4465         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4466         struct lpfc_hba  *phba = vport->phba;
4467         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4468         IOCB_t *icmd;
4469         struct lpfc_iocbq *elsiocb;
4470         uint8_t *pcmd;
4471         uint16_t cmdsize;
4472
4473         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
4474         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
4475                                      ELS_CMD_LOGO);
4476         if (!elsiocb)
4477                 return 1;
4478
4479         icmd = &elsiocb->iocb;
4480         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4481         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
4482         pcmd += sizeof(uint32_t);
4483
4484         /* Fill in LOGO payload */
4485         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
4486         pcmd += sizeof(uint32_t);
4487         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
4488
4489         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4490                 "Issue LOGO npiv  did:x%x flg:x%x",
4491                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4492
4493         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
4494         spin_lock_irq(shost->host_lock);
4495         ndlp->nlp_flag |= NLP_LOGO_SND;
4496         spin_unlock_irq(shost->host_lock);
4497         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
4498                 spin_lock_irq(shost->host_lock);
4499                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
4500                 spin_unlock_irq(shost->host_lock);
4501                 lpfc_els_free_iocb(phba, elsiocb);
4502                 return 1;
4503         }
4504         return 0;
4505 }
4506
4507 void
4508 lpfc_fabric_block_timeout(unsigned long ptr)
4509 {
4510         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4511         unsigned long iflags;
4512         uint32_t tmo_posted;
4513         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
4514         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
4515         if (!tmo_posted)
4516                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
4517         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
4518
4519         if (!tmo_posted) {
4520                 spin_lock_irqsave(&phba->hbalock, iflags);
4521                 if (phba->work_wait)
4522                         lpfc_worker_wake_up(phba);
4523                 spin_unlock_irqrestore(&phba->hbalock, iflags);
4524         }
4525 }
4526
4527 static void
4528 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
4529 {
4530         struct lpfc_iocbq *iocb;
4531         unsigned long iflags;
4532         int ret;
4533         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4534         IOCB_t *cmd;
4535
4536 repeat:
4537         iocb = NULL;
4538         spin_lock_irqsave(&phba->hbalock, iflags);
4539                                 /* Post any pending iocb to the SLI layer */
4540         if (atomic_read(&phba->fabric_iocb_count) == 0) {
4541                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
4542                                  list);
4543                 if (iocb)
4544                         atomic_inc(&phba->fabric_iocb_count);
4545         }
4546         spin_unlock_irqrestore(&phba->hbalock, iflags);
4547         if (iocb) {
4548                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
4549                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
4550                 iocb->iocb_flag |= LPFC_IO_FABRIC;
4551
4552                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
4553                         "Fabric sched1:   ste:x%x",
4554                         iocb->vport->port_state, 0, 0);
4555
4556                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
4557
4558                 if (ret == IOCB_ERROR) {
4559                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
4560                         iocb->fabric_iocb_cmpl = NULL;
4561                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
4562                         cmd = &iocb->iocb;
4563                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4564                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4565                         iocb->iocb_cmpl(phba, iocb, iocb);
4566
4567                         atomic_dec(&phba->fabric_iocb_count);
4568                         goto repeat;
4569                 }
4570         }
4571
4572         return;
4573 }
4574
4575 void
4576 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
4577 {
4578         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4579
4580         lpfc_resume_fabric_iocbs(phba);
4581         return;
4582 }
4583
4584 static void
4585 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
4586 {
4587         int blocked;
4588
4589         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4590                                 /* Start a timer to unblock fabric
4591                                  * iocbs after 100ms
4592                                  */
4593         if (!blocked)
4594                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
4595
4596         return;
4597 }
4598
4599 static void
4600 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4601         struct lpfc_iocbq *rspiocb)
4602 {
4603         struct ls_rjt stat;
4604
4605         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
4606                 BUG();
4607
4608         switch (rspiocb->iocb.ulpStatus) {
4609                 case IOSTAT_NPORT_RJT:
4610                 case IOSTAT_FABRIC_RJT:
4611                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
4612                                 lpfc_block_fabric_iocbs(phba);
4613                         }
4614                         break;
4615
4616                 case IOSTAT_NPORT_BSY:
4617                 case IOSTAT_FABRIC_BSY:
4618                         lpfc_block_fabric_iocbs(phba);
4619                         break;
4620
4621                 case IOSTAT_LS_RJT:
4622                         stat.un.lsRjtError =
4623                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
4624                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
4625                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
4626                                 lpfc_block_fabric_iocbs(phba);
4627                         break;
4628         }
4629
4630         if (atomic_read(&phba->fabric_iocb_count) == 0)
4631                 BUG();
4632
4633         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
4634         cmdiocb->fabric_iocb_cmpl = NULL;
4635         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
4636         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
4637
4638         atomic_dec(&phba->fabric_iocb_count);
4639         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
4640                                 /* Post any pending iocbs to HBA */
4641                     lpfc_resume_fabric_iocbs(phba);
4642         }
4643 }
4644
4645 int
4646 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
4647 {
4648         unsigned long iflags;
4649         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4650         int ready;
4651         int ret;
4652
4653         if (atomic_read(&phba->fabric_iocb_count) > 1)
4654                 BUG();
4655
4656         spin_lock_irqsave(&phba->hbalock, iflags);
4657         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
4658                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
4659
4660         spin_unlock_irqrestore(&phba->hbalock, iflags);
4661         if (ready) {
4662                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
4663                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
4664                 iocb->iocb_flag |= LPFC_IO_FABRIC;
4665
4666                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
4667                         "Fabric sched2:   ste:x%x",
4668                         iocb->vport->port_state, 0, 0);
4669
4670                 atomic_inc(&phba->fabric_iocb_count);
4671                 ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
4672
4673                 if (ret == IOCB_ERROR) {
4674                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
4675                         iocb->fabric_iocb_cmpl = NULL;
4676                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
4677                         atomic_dec(&phba->fabric_iocb_count);
4678                 }
4679         } else {
4680                 spin_lock_irqsave(&phba->hbalock, iflags);
4681                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
4682                 spin_unlock_irqrestore(&phba->hbalock, iflags);
4683                 ret = IOCB_SUCCESS;
4684         }
4685         return ret;
4686 }
4687
4688
4689 void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
4690 {
4691         LIST_HEAD(completions);
4692         struct lpfc_hba  *phba = vport->phba;
4693         struct lpfc_iocbq *tmp_iocb, *piocb;
4694         IOCB_t *cmd;
4695
4696         spin_lock_irq(&phba->hbalock);
4697         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4698                                  list) {
4699
4700                 if (piocb->vport != vport)
4701                         continue;
4702
4703                 list_move_tail(&piocb->list, &completions);
4704         }
4705         spin_unlock_irq(&phba->hbalock);
4706
4707         while (!list_empty(&completions)) {
4708                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4709                 list_del_init(&piocb->list);
4710
4711                 cmd = &piocb->iocb;
4712                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4713                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4714                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4715         }
4716 }
4717
4718 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
4719 {
4720         LIST_HEAD(completions);
4721         struct lpfc_hba  *phba = ndlp->vport->phba;
4722         struct lpfc_iocbq *tmp_iocb, *piocb;
4723         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4724         IOCB_t *cmd;
4725
4726         spin_lock_irq(&phba->hbalock);
4727         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4728                                  list) {
4729                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
4730
4731                         list_move_tail(&piocb->list, &completions);
4732                 }
4733         }
4734         spin_unlock_irq(&phba->hbalock);
4735
4736         while (!list_empty(&completions)) {
4737                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4738                 list_del_init(&piocb->list);
4739
4740                 cmd = &piocb->iocb;
4741                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4742                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4743                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4744         }
4745 }
4746
4747 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
4748 {
4749         LIST_HEAD(completions);
4750         struct lpfc_iocbq *piocb;
4751         IOCB_t *cmd;
4752
4753         spin_lock_irq(&phba->hbalock);
4754         list_splice_init(&phba->fabric_iocb_list, &completions);
4755         spin_unlock_irq(&phba->hbalock);
4756
4757         while (!list_empty(&completions)) {
4758                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4759                 list_del_init(&piocb->list);
4760
4761                 cmd = &piocb->iocb;
4762                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4763                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4764                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4765         }
4766 }
4767
4768
4769 void lpfc_fabric_abort_flogi(struct lpfc_hba *phba)
4770 {
4771         LIST_HEAD(completions);
4772         struct lpfc_iocbq *tmp_iocb, *piocb;
4773         IOCB_t *cmd;
4774         struct lpfc_nodelist *ndlp;
4775
4776         spin_lock_irq(&phba->hbalock);
4777         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
4778                                  list) {
4779
4780                 cmd = &piocb->iocb;
4781                 ndlp = (struct lpfc_nodelist *) piocb->context1;
4782                 if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
4783                     ndlp != NULL &&
4784                     ndlp->nlp_DID == Fabric_DID)
4785                         list_move_tail(&piocb->list, &completions);
4786         }
4787         spin_unlock_irq(&phba->hbalock);
4788
4789         while (!list_empty(&completions)) {
4790                 piocb = list_get_first(&completions, struct lpfc_iocbq, list);
4791                 list_del_init(&piocb->list);
4792
4793                 cmd = &piocb->iocb;
4794                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4795                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
4796                 (piocb->iocb_cmpl) (phba, piocb, piocb);
4797         }
4798 }
4799
4800