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