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