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