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