Pull sn-features into release branch
[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-2005 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
39 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
40                           struct lpfc_iocbq *);
41 static int lpfc_max_els_tries = 3;
42
43 static int
44 lpfc_els_chk_latt(struct lpfc_hba * phba)
45 {
46         struct lpfc_sli *psli;
47         LPFC_MBOXQ_t *mbox;
48         uint32_t ha_copy;
49         int rc;
50
51         psli = &phba->sli;
52
53         if ((phba->hba_state >= LPFC_HBA_READY) ||
54             (phba->hba_state == LPFC_LINK_DOWN))
55                 return 0;
56
57         /* Read the HBA Host Attention Register */
58         spin_lock_irq(phba->host->host_lock);
59         ha_copy = readl(phba->HAregaddr);
60         spin_unlock_irq(phba->host->host_lock);
61
62         if (!(ha_copy & HA_LATT))
63                 return 0;
64
65         /* Pending Link Event during Discovery */
66         lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
67                         "%d:0237 Pending Link Event during "
68                         "Discovery: State x%x\n",
69                         phba->brd_no, phba->hba_state);
70
71         /* CLEAR_LA should re-enable link attention events and
72          * we should then imediately take a LATT event. The
73          * LATT processing should call lpfc_linkdown() which
74          * will cleanup any left over in-progress discovery
75          * events.
76          */
77         spin_lock_irq(phba->host->host_lock);
78         phba->fc_flag |= FC_ABORT_DISCOVERY;
79         spin_unlock_irq(phba->host->host_lock);
80
81         if (phba->hba_state != LPFC_CLEAR_LA) {
82                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
83                         phba->hba_state = LPFC_CLEAR_LA;
84                         lpfc_clear_la(phba, mbox);
85                         mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
86                         rc = lpfc_sli_issue_mbox (phba, mbox,
87                                                   (MBX_NOWAIT | MBX_STOP_IOCB));
88                         if (rc == MBX_NOT_FINISHED) {
89                                 mempool_free(mbox, phba->mbox_mem_pool);
90                                 phba->hba_state = LPFC_HBA_ERROR;
91                         }
92                 }
93         }
94
95         return (1);
96
97 }
98
99 static struct lpfc_iocbq *
100 lpfc_prep_els_iocb(struct lpfc_hba * phba,
101                    uint8_t expectRsp,
102                    uint16_t cmdSize,
103                    uint8_t retry, struct lpfc_nodelist * ndlp, uint32_t elscmd)
104 {
105         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
106         struct lpfc_sli_ring *pring;
107         struct lpfc_iocbq *elsiocb = NULL;
108         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
109         struct ulp_bde64 *bpl;
110         IOCB_t *icmd;
111
112         pring = &phba->sli.ring[LPFC_ELS_RING];
113
114         if (phba->hba_state < LPFC_LINK_UP)
115                 return  NULL;
116
117
118         /* Allocate buffer for  command iocb */
119         spin_lock_irq(phba->host->host_lock);
120         list_remove_head(lpfc_iocb_list, elsiocb, struct lpfc_iocbq, list);
121         spin_unlock_irq(phba->host->host_lock);
122
123         if (elsiocb == NULL)
124                 return NULL;
125         memset(elsiocb, 0, sizeof (struct lpfc_iocbq));
126         icmd = &elsiocb->iocb;
127
128         /* fill in BDEs for command */
129         /* Allocate buffer for command payload */
130         if (((pcmd = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL)) == 0) ||
131             ((pcmd->virt = lpfc_mbuf_alloc(phba,
132                                            MEM_PRI, &(pcmd->phys))) == 0)) {
133                 if (pcmd)
134                         kfree(pcmd);
135
136                 list_add_tail(&elsiocb->list, lpfc_iocb_list);
137                 return NULL;
138         }
139
140         INIT_LIST_HEAD(&pcmd->list);
141
142         /* Allocate buffer for response payload */
143         if (expectRsp) {
144                 prsp = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
145                 if (prsp)
146                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
147                                                      &prsp->phys);
148                 if (prsp == 0 || prsp->virt == 0) {
149                         if (prsp)
150                                 kfree(prsp);
151                         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
152                         kfree(pcmd);
153                         list_add_tail(&elsiocb->list, lpfc_iocb_list);
154                         return NULL;
155                 }
156                 INIT_LIST_HEAD(&prsp->list);
157         } else {
158                 prsp = NULL;
159         }
160
161         /* Allocate buffer for Buffer ptr list */
162         pbuflist = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
163         if (pbuflist)
164             pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
165                                              &pbuflist->phys);
166         if (pbuflist == 0 || pbuflist->virt == 0) {
167                 list_add_tail(&elsiocb->list, lpfc_iocb_list);
168                 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
169                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
170                 kfree(pcmd);
171                 kfree(prsp);
172                 if (pbuflist)
173                         kfree(pbuflist);
174                 return NULL;
175         }
176
177         INIT_LIST_HEAD(&pbuflist->list);
178
179         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
180         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
181         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BDL;
182         if (expectRsp) {
183                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
184                 icmd->un.elsreq64.remoteID = ndlp->nlp_DID;     /* DID */
185                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
186         } else {
187                 icmd->un.elsreq64.bdl.bdeSize = sizeof (struct ulp_bde64);
188                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
189         }
190
191         icmd->ulpBdeCount = 1;
192         icmd->ulpLe = 1;
193         icmd->ulpClass = CLASS3;
194
195         bpl = (struct ulp_bde64 *) pbuflist->virt;
196         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
197         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
198         bpl->tus.f.bdeSize = cmdSize;
199         bpl->tus.f.bdeFlags = 0;
200         bpl->tus.w = le32_to_cpu(bpl->tus.w);
201
202         if (expectRsp) {
203                 bpl++;
204                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
205                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
206                 bpl->tus.f.bdeSize = FCELSSIZE;
207                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
208                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
209         }
210
211         /* Save for completion so we can release these resources */
212         elsiocb->context1 = (uint8_t *) ndlp;
213         elsiocb->context2 = (uint8_t *) pcmd;
214         elsiocb->context3 = (uint8_t *) pbuflist;
215         elsiocb->retry = retry;
216         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
217
218         if (prsp) {
219                 list_add(&prsp->list, &pcmd->list);
220         }
221
222         if (expectRsp) {
223                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
224                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
225                                 "%d:0116 Xmit ELS command x%x to remote "
226                                 "NPORT x%x Data: x%x x%x\n",
227                                 phba->brd_no, elscmd,
228                                 ndlp->nlp_DID, icmd->ulpIoTag, phba->hba_state);
229         } else {
230                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
231                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
232                                 "%d:0117 Xmit ELS response x%x to remote "
233                                 "NPORT x%x Data: x%x x%x\n",
234                                 phba->brd_no, elscmd,
235                                 ndlp->nlp_DID, icmd->ulpIoTag, cmdSize);
236         }
237
238         return (elsiocb);
239 }
240
241
242 static int
243 lpfc_cmpl_els_flogi_fabric(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
244                 struct serv_parm *sp, IOCB_t *irsp)
245 {
246         LPFC_MBOXQ_t *mbox;
247         int rc;
248
249         spin_lock_irq(phba->host->host_lock);
250         phba->fc_flag |= FC_FABRIC;
251         spin_unlock_irq(phba->host->host_lock);
252
253         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
254         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
255                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
256
257         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
258
259         if (phba->fc_topology == TOPOLOGY_LOOP) {
260                 spin_lock_irq(phba->host->host_lock);
261                 phba->fc_flag |= FC_PUBLIC_LOOP;
262                 spin_unlock_irq(phba->host->host_lock);
263         } else {
264                 /*
265                  * If we are a N-port connected to a Fabric, fixup sparam's so
266                  * logins to devices on remote loops work.
267                  */
268                 phba->fc_sparam.cmn.altBbCredit = 1;
269         }
270
271         phba->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
272         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
273         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
274         ndlp->nlp_class_sup = 0;
275         if (sp->cls1.classValid)
276                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
277         if (sp->cls2.classValid)
278                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
279         if (sp->cls3.classValid)
280                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
281         if (sp->cls4.classValid)
282                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
283         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
284                                 sp->cmn.bbRcvSizeLsb;
285         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
286
287         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
288         if (!mbox)
289                 goto fail;
290
291         phba->hba_state = LPFC_FABRIC_CFG_LINK;
292         lpfc_config_link(phba, mbox);
293         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
294
295         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
296         if (rc == MBX_NOT_FINISHED)
297                 goto fail_free_mbox;
298
299         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
300         if (!mbox)
301                 goto fail;
302
303         if (lpfc_reg_login(phba, Fabric_DID, (uint8_t *) sp, mbox, 0))
304                 goto fail_free_mbox;
305
306         /*
307          * set_slim mailbox command needs to execute first,
308          * queue this command to be processed later.
309          */
310         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
311         mbox->context2 = ndlp;
312
313         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT | MBX_STOP_IOCB);
314         if (rc == MBX_NOT_FINISHED)
315                 goto fail_free_mbox;
316
317         return 0;
318
319  fail_free_mbox:
320         mempool_free(mbox, phba->mbox_mem_pool);
321  fail:
322         return -ENXIO;
323 }
324
325 /*
326  * We FLOGIed into an NPort, initiate pt2pt protocol
327  */
328 static int
329 lpfc_cmpl_els_flogi_nport(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
330                 struct serv_parm *sp)
331 {
332         LPFC_MBOXQ_t *mbox;
333         int rc;
334
335         spin_lock_irq(phba->host->host_lock);
336         phba->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
337         spin_unlock_irq(phba->host->host_lock);
338
339         phba->fc_edtov = FF_DEF_EDTOV;
340         phba->fc_ratov = FF_DEF_RATOV;
341         rc = memcmp(&phba->fc_portname, &sp->portName,
342                         sizeof(struct lpfc_name));
343         if (rc >= 0) {
344                 /* This side will initiate the PLOGI */
345                 spin_lock_irq(phba->host->host_lock);
346                 phba->fc_flag |= FC_PT2PT_PLOGI;
347                 spin_unlock_irq(phba->host->host_lock);
348
349                 /*
350                  * N_Port ID cannot be 0, set our to LocalID the other
351                  * side will be RemoteID.
352                  */
353
354                 /* not equal */
355                 if (rc)
356                         phba->fc_myDID = PT2PT_LocalID;
357
358                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
359                 if (!mbox)
360                         goto fail;
361
362                 lpfc_config_link(phba, mbox);
363
364                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
365                 rc = lpfc_sli_issue_mbox(phba, mbox,
366                                 MBX_NOWAIT | MBX_STOP_IOCB);
367                 if (rc == MBX_NOT_FINISHED) {
368                         mempool_free(mbox, phba->mbox_mem_pool);
369                         goto fail;
370                 }
371                 mempool_free(ndlp, phba->nlp_mem_pool);
372
373                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, PT2PT_RemoteID);
374                 if (!ndlp) {
375                         /*
376                          * Cannot find existing Fabric ndlp, so allocate a
377                          * new one
378                          */
379                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
380                         if (!ndlp)
381                                 goto fail;
382
383                         lpfc_nlp_init(phba, ndlp, PT2PT_RemoteID);
384                 }
385
386                 memcpy(&ndlp->nlp_portname, &sp->portName,
387                                 sizeof(struct lpfc_name));
388                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
389                                 sizeof(struct lpfc_name));
390                 ndlp->nlp_state = NLP_STE_NPR_NODE;
391                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
392                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
393         } else {
394                 /* This side will wait for the PLOGI */
395                 mempool_free( ndlp, phba->nlp_mem_pool);
396         }
397
398         spin_lock_irq(phba->host->host_lock);
399         phba->fc_flag |= FC_PT2PT;
400         spin_unlock_irq(phba->host->host_lock);
401
402         /* Start discovery - this should just do CLEAR_LA */
403         lpfc_disc_start(phba);
404         return 0;
405  fail:
406         return -ENXIO;
407 }
408
409 static void
410 lpfc_cmpl_els_flogi(struct lpfc_hba * phba,
411                     struct lpfc_iocbq * cmdiocb, struct lpfc_iocbq * rspiocb)
412 {
413         IOCB_t *irsp = &rspiocb->iocb;
414         struct lpfc_nodelist *ndlp = cmdiocb->context1;
415         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
416         struct serv_parm *sp;
417         int rc;
418
419         /* Check to see if link went down during discovery */
420         if (lpfc_els_chk_latt(phba)) {
421                 lpfc_nlp_remove(phba, ndlp);
422                 goto out;
423         }
424
425         if (irsp->ulpStatus) {
426                 /* Check for retry */
427                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
428                         /* ELS command is being retried */
429                         goto out;
430                 }
431                 /* FLOGI failed, so there is no fabric */
432                 spin_lock_irq(phba->host->host_lock);
433                 phba->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
434                 spin_unlock_irq(phba->host->host_lock);
435
436                 /* If private loop, then allow max outstandting els to be
437                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
438                  * alpa map would take too long otherwise.
439                  */
440                 if (phba->alpa_map[0] == 0) {
441                         phba->cfg_discovery_threads =
442                             LPFC_MAX_DISC_THREADS;
443                 }
444
445                 /* FLOGI failure */
446                 lpfc_printf_log(phba,
447                                 KERN_INFO,
448                                 LOG_ELS,
449                                 "%d:0100 FLOGI failure Data: x%x x%x\n",
450                                 phba->brd_no,
451                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
452                 goto flogifail;
453         }
454
455         /*
456          * The FLogI succeeded.  Sync the data for the CPU before
457          * accessing it.
458          */
459         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
460
461         sp = prsp->virt + sizeof(uint32_t);
462
463         /* FLOGI completes successfully */
464         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
465                         "%d:0101 FLOGI completes sucessfully "
466                         "Data: x%x x%x x%x x%x\n",
467                         phba->brd_no,
468                         irsp->un.ulpWord[4], sp->cmn.e_d_tov,
469                         sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
470
471         if (phba->hba_state == LPFC_FLOGI) {
472                 /*
473                  * If Common Service Parameters indicate Nport
474                  * we are point to point, if Fport we are Fabric.
475                  */
476                 if (sp->cmn.fPort)
477                         rc = lpfc_cmpl_els_flogi_fabric(phba, ndlp, sp, irsp);
478                 else
479                         rc = lpfc_cmpl_els_flogi_nport(phba, ndlp, sp);
480
481                 if (!rc)
482                         goto out;
483         }
484
485 flogifail:
486         lpfc_nlp_remove(phba, ndlp);
487
488         if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
489             (irsp->un.ulpWord[4] != IOERR_SLI_ABORTED &&
490              irsp->un.ulpWord[4] != IOERR_SLI_DOWN)) {
491                 /* FLOGI failed, so just use loop map to make discovery list */
492                 lpfc_disc_list_loopmap(phba);
493
494                 /* Start discovery */
495                 lpfc_disc_start(phba);
496         }
497
498 out:
499         lpfc_els_free_iocb(phba, cmdiocb);
500 }
501
502 static int
503 lpfc_issue_els_flogi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
504                      uint8_t retry)
505 {
506         struct serv_parm *sp;
507         IOCB_t *icmd;
508         struct lpfc_iocbq *elsiocb;
509         struct lpfc_sli_ring *pring;
510         uint8_t *pcmd;
511         uint16_t cmdsize;
512         uint32_t tmo;
513         int rc;
514
515         pring = &phba->sli.ring[LPFC_ELS_RING];
516
517         cmdsize = (sizeof (uint32_t) + sizeof (struct serv_parm));
518         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
519                                           ndlp, ELS_CMD_FLOGI)) == 0) {
520                 return (1);
521         }
522
523         icmd = &elsiocb->iocb;
524         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
525
526         /* For FLOGI request, remainder of payload is service parameters */
527         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
528         pcmd += sizeof (uint32_t);
529         memcpy(pcmd, &phba->fc_sparam, sizeof (struct serv_parm));
530         sp = (struct serv_parm *) pcmd;
531
532         /* Setup CSPs accordingly for Fabric */
533         sp->cmn.e_d_tov = 0;
534         sp->cmn.w2.r_a_tov = 0;
535         sp->cls1.classValid = 0;
536         sp->cls2.seqDelivery = 1;
537         sp->cls3.seqDelivery = 1;
538         if (sp->cmn.fcphLow < FC_PH3)
539                 sp->cmn.fcphLow = FC_PH3;
540         if (sp->cmn.fcphHigh < FC_PH3)
541                 sp->cmn.fcphHigh = FC_PH3;
542
543         tmo = phba->fc_ratov;
544         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
545         lpfc_set_disctmo(phba);
546         phba->fc_ratov = tmo;
547
548         phba->fc_stat.elsXmitFLOGI++;
549         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
550         spin_lock_irq(phba->host->host_lock);
551         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
552         spin_unlock_irq(phba->host->host_lock);
553         if (rc == IOCB_ERROR) {
554                 lpfc_els_free_iocb(phba, elsiocb);
555                 return (1);
556         }
557         return (0);
558 }
559
560 int
561 lpfc_els_abort_flogi(struct lpfc_hba * phba)
562 {
563         struct lpfc_sli_ring *pring;
564         struct lpfc_iocbq *iocb, *next_iocb;
565         struct lpfc_nodelist *ndlp;
566         IOCB_t *icmd;
567
568         /* Abort outstanding I/O on NPort <nlp_DID> */
569         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
570                         "%d:0201 Abort outstanding I/O on NPort x%x\n",
571                         phba->brd_no, Fabric_DID);
572
573         pring = &phba->sli.ring[LPFC_ELS_RING];
574
575         /*
576          * Check the txcmplq for an iocb that matches the nport the driver is
577          * searching for.
578          */
579         spin_lock_irq(phba->host->host_lock);
580         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
581                 icmd = &iocb->iocb;
582                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
583                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
584                         if (ndlp && (ndlp->nlp_DID == Fabric_DID)) {
585                                 list_del(&iocb->list);
586                                 pring->txcmplq_cnt--;
587
588                                 if ((icmd->un.elsreq64.bdl.ulpIoTag32)) {
589                                         lpfc_sli_issue_abort_iotag32
590                                                 (phba, pring, iocb);
591                                 }
592                                 if (iocb->iocb_cmpl) {
593                                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
594                                         icmd->un.ulpWord[4] =
595                                             IOERR_SLI_ABORTED;
596                                         spin_unlock_irq(phba->host->host_lock);
597                                         (iocb->iocb_cmpl) (phba, iocb, iocb);
598                                         spin_lock_irq(phba->host->host_lock);
599                                 } else {
600                                         list_add_tail(&iocb->list,
601                                                       &phba->lpfc_iocb_list);
602                                 }
603                         }
604                 }
605         }
606         spin_unlock_irq(phba->host->host_lock);
607
608         return 0;
609 }
610
611 int
612 lpfc_initial_flogi(struct lpfc_hba * phba)
613 {
614         struct lpfc_nodelist *ndlp;
615
616         /* First look for Fabric ndlp on the unmapped list */
617
618         if ((ndlp =
619              lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED,
620                                Fabric_DID)) == 0) {
621                 /* Cannot find existing Fabric ndlp, so allocate a new one */
622                 if ((ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL))
623                     == 0) {
624                         return (0);
625                 }
626                 lpfc_nlp_init(phba, ndlp, Fabric_DID);
627         }
628         else {
629                 phba->fc_unmap_cnt--;
630                 list_del(&ndlp->nlp_listp);
631                 spin_lock_irq(phba->host->host_lock);
632                 ndlp->nlp_flag &= ~NLP_LIST_MASK;
633                 spin_unlock_irq(phba->host->host_lock);
634         }
635         if (lpfc_issue_els_flogi(phba, ndlp, 0)) {
636                 mempool_free( ndlp, phba->nlp_mem_pool);
637         }
638         return (1);
639 }
640
641 static void
642 lpfc_more_plogi(struct lpfc_hba * phba)
643 {
644         int sentplogi;
645
646         if (phba->num_disc_nodes)
647                 phba->num_disc_nodes--;
648
649         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
650         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
651                         "%d:0232 Continue discovery with %d PLOGIs to go "
652                         "Data: x%x x%x x%x\n",
653                         phba->brd_no, phba->num_disc_nodes, phba->fc_plogi_cnt,
654                         phba->fc_flag, phba->hba_state);
655
656         /* Check to see if there are more PLOGIs to be sent */
657         if (phba->fc_flag & FC_NLP_MORE) {
658                 /* go thru NPR list and issue any remaining ELS PLOGIs */
659                 sentplogi = lpfc_els_disc_plogi(phba);
660         }
661         return;
662 }
663
664 static void
665 lpfc_cmpl_els_plogi(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
666                     struct lpfc_iocbq * rspiocb)
667 {
668         IOCB_t *irsp;
669         struct lpfc_sli *psli;
670         struct lpfc_nodelist *ndlp;
671         int disc, rc, did, type;
672
673         psli = &phba->sli;
674
675         /* we pass cmdiocb to state machine which needs rspiocb as well */
676         cmdiocb->context_un.rsp_iocb = rspiocb;
677
678         irsp = &rspiocb->iocb;
679         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
680         spin_lock_irq(phba->host->host_lock);
681         ndlp->nlp_flag &= ~NLP_PLOGI_SND;
682         spin_unlock_irq(phba->host->host_lock);
683
684         /* Since ndlp can be freed in the disc state machine, note if this node
685          * is being used during discovery.
686          */
687         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
688         rc   = 0;
689
690         /* PLOGI completes to NPort <nlp_DID> */
691         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
692                         "%d:0102 PLOGI completes to NPort x%x "
693                         "Data: x%x x%x x%x x%x\n",
694                         phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
695                         irsp->un.ulpWord[4], disc, phba->num_disc_nodes);
696
697         /* Check to see if link went down during discovery */
698         if (lpfc_els_chk_latt(phba)) {
699                 spin_lock_irq(phba->host->host_lock);
700                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
701                 spin_unlock_irq(phba->host->host_lock);
702                 goto out;
703         }
704
705         /* ndlp could be freed in DSM, save these values now */
706         type = ndlp->nlp_type;
707         did = ndlp->nlp_DID;
708
709         if (irsp->ulpStatus) {
710                 /* Check for retry */
711                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
712                         /* ELS command is being retried */
713                         if (disc) {
714                                 spin_lock_irq(phba->host->host_lock);
715                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
716                                 spin_unlock_irq(phba->host->host_lock);
717                         }
718                         goto out;
719                 }
720
721                 /* PLOGI failed */
722                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
723                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
724                    ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
725                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
726                         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
727                 }
728                 else {
729                         rc = lpfc_disc_state_machine(phba, ndlp, cmdiocb,
730                                         NLP_EVT_CMPL_PLOGI);
731                 }
732         } else {
733                 /* Good status, call state machine */
734                 rc = lpfc_disc_state_machine(phba, ndlp, cmdiocb,
735                                         NLP_EVT_CMPL_PLOGI);
736         }
737
738         if (type & NLP_FABRIC) {
739                 /* If we cannot login to Nameserver, kick off discovery now */
740                 if ((did == NameServer_DID) && (rc == NLP_STE_FREED_NODE)) {
741                         lpfc_disc_start(phba);
742                 }
743                 goto out;
744         }
745
746         if (disc && phba->num_disc_nodes) {
747                 /* Check to see if there are more PLOGIs to be sent */
748                 lpfc_more_plogi(phba);
749         }
750
751         if (rc != NLP_STE_FREED_NODE) {
752                 spin_lock_irq(phba->host->host_lock);
753                 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
754                 spin_unlock_irq(phba->host->host_lock);
755         }
756
757         if (phba->num_disc_nodes == 0) {
758                 if(disc) {
759                         spin_lock_irq(phba->host->host_lock);
760                         phba->fc_flag &= ~FC_NDISC_ACTIVE;
761                         spin_unlock_irq(phba->host->host_lock);
762                 }
763                 lpfc_can_disctmo(phba);
764                 if (phba->fc_flag & FC_RSCN_MODE) {
765                         /* Check to see if more RSCNs came in while we were
766                          * processing this one.
767                          */
768                         if ((phba->fc_rscn_id_cnt == 0) &&
769                             (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
770                                 spin_lock_irq(phba->host->host_lock);
771                                 phba->fc_flag &= ~FC_RSCN_MODE;
772                                 spin_unlock_irq(phba->host->host_lock);
773                         } else {
774                                 lpfc_els_handle_rscn(phba);
775                         }
776                 }
777         }
778
779 out:
780         lpfc_els_free_iocb(phba, cmdiocb);
781         return;
782 }
783
784 int
785 lpfc_issue_els_plogi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
786                      uint8_t retry)
787 {
788         struct serv_parm *sp;
789         IOCB_t *icmd;
790         struct lpfc_iocbq *elsiocb;
791         struct lpfc_sli_ring *pring;
792         struct lpfc_sli *psli;
793         uint8_t *pcmd;
794         uint16_t cmdsize;
795
796         psli = &phba->sli;
797         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
798
799         cmdsize = (sizeof (uint32_t) + sizeof (struct serv_parm));
800         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
801                                           ndlp, ELS_CMD_PLOGI)) == 0) {
802                 return (1);
803         }
804
805         icmd = &elsiocb->iocb;
806         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
807
808         /* For PLOGI request, remainder of payload is service parameters */
809         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
810         pcmd += sizeof (uint32_t);
811         memcpy(pcmd, &phba->fc_sparam, sizeof (struct serv_parm));
812         sp = (struct serv_parm *) pcmd;
813
814         if (sp->cmn.fcphLow < FC_PH_4_3)
815                 sp->cmn.fcphLow = FC_PH_4_3;
816
817         if (sp->cmn.fcphHigh < FC_PH3)
818                 sp->cmn.fcphHigh = FC_PH3;
819
820         phba->fc_stat.elsXmitPLOGI++;
821         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
822         spin_lock_irq(phba->host->host_lock);
823         ndlp->nlp_flag |= NLP_PLOGI_SND;
824         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
825                 ndlp->nlp_flag &= ~NLP_PLOGI_SND;
826                 spin_unlock_irq(phba->host->host_lock);
827                 lpfc_els_free_iocb(phba, elsiocb);
828                 return (1);
829         }
830         spin_unlock_irq(phba->host->host_lock);
831         return (0);
832 }
833
834 static void
835 lpfc_cmpl_els_prli(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
836                    struct lpfc_iocbq * rspiocb)
837 {
838         IOCB_t *irsp;
839         struct lpfc_sli *psli;
840         struct lpfc_nodelist *ndlp;
841
842         psli = &phba->sli;
843         /* we pass cmdiocb to state machine which needs rspiocb as well */
844         cmdiocb->context_un.rsp_iocb = rspiocb;
845
846         irsp = &(rspiocb->iocb);
847         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
848         spin_lock_irq(phba->host->host_lock);
849         ndlp->nlp_flag &= ~NLP_PRLI_SND;
850         spin_unlock_irq(phba->host->host_lock);
851
852         /* PRLI completes to NPort <nlp_DID> */
853         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
854                         "%d:0103 PRLI completes to NPort x%x "
855                         "Data: x%x x%x x%x\n",
856                         phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
857                         irsp->un.ulpWord[4], phba->num_disc_nodes);
858
859         phba->fc_prli_sent--;
860         /* Check to see if link went down during discovery */
861         if (lpfc_els_chk_latt(phba))
862                 goto out;
863
864         if (irsp->ulpStatus) {
865                 /* Check for retry */
866                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
867                         /* ELS command is being retried */
868                         goto out;
869                 }
870                 /* PRLI failed */
871                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
872                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
873                    ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
874                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
875                         goto out;
876                 }
877                 else {
878                         lpfc_disc_state_machine(phba, ndlp, cmdiocb,
879                                         NLP_EVT_CMPL_PRLI);
880                 }
881         } else {
882                 /* Good status, call state machine */
883                 lpfc_disc_state_machine(phba, ndlp, cmdiocb, NLP_EVT_CMPL_PRLI);
884         }
885
886 out:
887         lpfc_els_free_iocb(phba, cmdiocb);
888         return;
889 }
890
891 int
892 lpfc_issue_els_prli(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
893                     uint8_t retry)
894 {
895         PRLI *npr;
896         IOCB_t *icmd;
897         struct lpfc_iocbq *elsiocb;
898         struct lpfc_sli_ring *pring;
899         struct lpfc_sli *psli;
900         uint8_t *pcmd;
901         uint16_t cmdsize;
902
903         psli = &phba->sli;
904         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
905
906         cmdsize = (sizeof (uint32_t) + sizeof (PRLI));
907         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
908                                           ndlp, ELS_CMD_PRLI)) == 0) {
909                 return (1);
910         }
911
912         icmd = &elsiocb->iocb;
913         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
914
915         /* For PRLI request, remainder of payload is service parameters */
916         memset(pcmd, 0, (sizeof (PRLI) + sizeof (uint32_t)));
917         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
918         pcmd += sizeof (uint32_t);
919
920         /* For PRLI, remainder of payload is PRLI parameter page */
921         npr = (PRLI *) pcmd;
922         /*
923          * If our firmware version is 3.20 or later,
924          * set the following bits for FC-TAPE support.
925          */
926         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
927                 npr->ConfmComplAllowed = 1;
928                 npr->Retry = 1;
929                 npr->TaskRetryIdReq = 1;
930         }
931         npr->estabImagePair = 1;
932         npr->readXferRdyDis = 1;
933
934         /* For FCP support */
935         npr->prliType = PRLI_FCP_TYPE;
936         npr->initiatorFunc = 1;
937
938         phba->fc_stat.elsXmitPRLI++;
939         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
940         spin_lock_irq(phba->host->host_lock);
941         ndlp->nlp_flag |= NLP_PRLI_SND;
942         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
943                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
944                 spin_unlock_irq(phba->host->host_lock);
945                 lpfc_els_free_iocb(phba, elsiocb);
946                 return (1);
947         }
948         spin_unlock_irq(phba->host->host_lock);
949         phba->fc_prli_sent++;
950         return (0);
951 }
952
953 static void
954 lpfc_more_adisc(struct lpfc_hba * phba)
955 {
956         int sentadisc;
957
958         if (phba->num_disc_nodes)
959                 phba->num_disc_nodes--;
960
961         /* Continue discovery with <num_disc_nodes> ADISCs to go */
962         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
963                         "%d:0210 Continue discovery with %d ADISCs to go "
964                         "Data: x%x x%x x%x\n",
965                         phba->brd_no, phba->num_disc_nodes, phba->fc_adisc_cnt,
966                         phba->fc_flag, phba->hba_state);
967
968         /* Check to see if there are more ADISCs to be sent */
969         if (phba->fc_flag & FC_NLP_MORE) {
970                 lpfc_set_disctmo(phba);
971
972                 /* go thru NPR list and issue any remaining ELS ADISCs */
973                 sentadisc = lpfc_els_disc_adisc(phba);
974         }
975         return;
976 }
977
978 static void
979 lpfc_rscn_disc(struct lpfc_hba * phba)
980 {
981         /* RSCN discovery */
982         /* go thru NPR list and issue ELS PLOGIs */
983         if (phba->fc_npr_cnt) {
984                 if (lpfc_els_disc_plogi(phba))
985                         return;
986         }
987         if (phba->fc_flag & FC_RSCN_MODE) {
988                 /* Check to see if more RSCNs came in while we were
989                  * processing this one.
990                  */
991                 if ((phba->fc_rscn_id_cnt == 0) &&
992                     (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
993                         spin_lock_irq(phba->host->host_lock);
994                         phba->fc_flag &= ~FC_RSCN_MODE;
995                         spin_unlock_irq(phba->host->host_lock);
996                 } else {
997                         lpfc_els_handle_rscn(phba);
998                 }
999         }
1000 }
1001
1002 static void
1003 lpfc_cmpl_els_adisc(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
1004                     struct lpfc_iocbq * rspiocb)
1005 {
1006         IOCB_t *irsp;
1007         struct lpfc_sli *psli;
1008         struct lpfc_nodelist *ndlp;
1009         LPFC_MBOXQ_t *mbox;
1010         int disc, rc;
1011
1012         psli = &phba->sli;
1013
1014         /* we pass cmdiocb to state machine which needs rspiocb as well */
1015         cmdiocb->context_un.rsp_iocb = rspiocb;
1016
1017         irsp = &(rspiocb->iocb);
1018         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1019         spin_lock_irq(phba->host->host_lock);
1020         ndlp->nlp_flag &= ~NLP_ADISC_SND;
1021         spin_unlock_irq(phba->host->host_lock);
1022
1023         /* Since ndlp can be freed in the disc state machine, note if this node
1024          * is being used during discovery.
1025          */
1026         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1027
1028         /* ADISC completes to NPort <nlp_DID> */
1029         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1030                         "%d:0104 ADISC completes to NPort x%x "
1031                         "Data: x%x x%x x%x x%x\n",
1032                         phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
1033                         irsp->un.ulpWord[4], disc, phba->num_disc_nodes);
1034
1035         /* Check to see if link went down during discovery */
1036         if (lpfc_els_chk_latt(phba)) {
1037                 spin_lock_irq(phba->host->host_lock);
1038                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1039                 spin_unlock_irq(phba->host->host_lock);
1040                 goto out;
1041         }
1042
1043         if (irsp->ulpStatus) {
1044                 /* Check for retry */
1045                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1046                         /* ELS command is being retried */
1047                         if (disc) {
1048                                 spin_lock_irq(phba->host->host_lock);
1049                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1050                                 spin_unlock_irq(phba->host->host_lock);
1051                                 lpfc_set_disctmo(phba);
1052                         }
1053                         goto out;
1054                 }
1055                 /* ADISC failed */
1056                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1057                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1058                    ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
1059                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
1060                         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1061                 }
1062                 else {
1063                         lpfc_disc_state_machine(phba, ndlp, cmdiocb,
1064                                         NLP_EVT_CMPL_ADISC);
1065                 }
1066         } else {
1067                 /* Good status, call state machine */
1068                 lpfc_disc_state_machine(phba, ndlp, cmdiocb,
1069                                         NLP_EVT_CMPL_ADISC);
1070         }
1071
1072         if (disc && phba->num_disc_nodes) {
1073                 /* Check to see if there are more ADISCs to be sent */
1074                 lpfc_more_adisc(phba);
1075
1076                 /* Check to see if we are done with ADISC authentication */
1077                 if (phba->num_disc_nodes == 0) {
1078                         lpfc_can_disctmo(phba);
1079                         /* If we get here, there is nothing left to wait for */
1080                         if ((phba->hba_state < LPFC_HBA_READY) &&
1081                             (phba->hba_state != LPFC_CLEAR_LA)) {
1082                                 /* Link up discovery */
1083                                 if ((mbox = mempool_alloc(phba->mbox_mem_pool,
1084                                                           GFP_KERNEL))) {
1085                                         phba->hba_state = LPFC_CLEAR_LA;
1086                                         lpfc_clear_la(phba, mbox);
1087                                         mbox->mbox_cmpl =
1088                                             lpfc_mbx_cmpl_clear_la;
1089                                         rc = lpfc_sli_issue_mbox
1090                                                 (phba, mbox,
1091                                                  (MBX_NOWAIT | MBX_STOP_IOCB));
1092                                         if (rc == MBX_NOT_FINISHED) {
1093                                                 mempool_free(mbox,
1094                                                      phba->mbox_mem_pool);
1095                                                 lpfc_disc_flush_list(phba);
1096                                                 psli->ring[(psli->ip_ring)].
1097                                                     flag &=
1098                                                     ~LPFC_STOP_IOCB_EVENT;
1099                                                 psli->ring[(psli->fcp_ring)].
1100                                                     flag &=
1101                                                     ~LPFC_STOP_IOCB_EVENT;
1102                                                 psli->ring[(psli->next_ring)].
1103                                                     flag &=
1104                                                     ~LPFC_STOP_IOCB_EVENT;
1105                                                 phba->hba_state =
1106                                                     LPFC_HBA_READY;
1107                                         }
1108                                 }
1109                         } else {
1110                                 lpfc_rscn_disc(phba);
1111                         }
1112                 }
1113         }
1114         spin_lock_irq(phba->host->host_lock);
1115         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1116         spin_unlock_irq(phba->host->host_lock);
1117 out:
1118         lpfc_els_free_iocb(phba, cmdiocb);
1119         return;
1120 }
1121
1122 int
1123 lpfc_issue_els_adisc(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
1124                      uint8_t retry)
1125 {
1126         ADISC *ap;
1127         IOCB_t *icmd;
1128         struct lpfc_iocbq *elsiocb;
1129         struct lpfc_sli_ring *pring;
1130         struct lpfc_sli *psli;
1131         uint8_t *pcmd;
1132         uint16_t cmdsize;
1133
1134         psli = &phba->sli;
1135         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1136
1137         cmdsize = (sizeof (uint32_t) + sizeof (ADISC));
1138         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
1139                                           ndlp, ELS_CMD_ADISC)) == 0) {
1140                 return (1);
1141         }
1142
1143         icmd = &elsiocb->iocb;
1144         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1145
1146         /* For ADISC request, remainder of payload is service parameters */
1147         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1148         pcmd += sizeof (uint32_t);
1149
1150         /* Fill in ADISC payload */
1151         ap = (ADISC *) pcmd;
1152         ap->hardAL_PA = phba->fc_pref_ALPA;
1153         memcpy(&ap->portName, &phba->fc_portname, sizeof (struct lpfc_name));
1154         memcpy(&ap->nodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
1155         ap->DID = be32_to_cpu(phba->fc_myDID);
1156
1157         phba->fc_stat.elsXmitADISC++;
1158         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1159         spin_lock_irq(phba->host->host_lock);
1160         ndlp->nlp_flag |= NLP_ADISC_SND;
1161         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1162                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
1163                 spin_unlock_irq(phba->host->host_lock);
1164                 lpfc_els_free_iocb(phba, elsiocb);
1165                 return (1);
1166         }
1167         spin_unlock_irq(phba->host->host_lock);
1168         return (0);
1169 }
1170
1171 static void
1172 lpfc_cmpl_els_logo(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
1173                    struct lpfc_iocbq * rspiocb)
1174 {
1175         IOCB_t *irsp;
1176         struct lpfc_sli *psli;
1177         struct lpfc_nodelist *ndlp;
1178
1179         psli = &phba->sli;
1180         /* we pass cmdiocb to state machine which needs rspiocb as well */
1181         cmdiocb->context_un.rsp_iocb = rspiocb;
1182
1183         irsp = &(rspiocb->iocb);
1184         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1185         spin_lock_irq(phba->host->host_lock);
1186         ndlp->nlp_flag &= ~NLP_LOGO_SND;
1187         spin_unlock_irq(phba->host->host_lock);
1188
1189         /* LOGO completes to NPort <nlp_DID> */
1190         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1191                         "%d:0105 LOGO completes to NPort x%x "
1192                         "Data: x%x x%x x%x\n",
1193                         phba->brd_no, ndlp->nlp_DID, irsp->ulpStatus,
1194                         irsp->un.ulpWord[4], phba->num_disc_nodes);
1195
1196         /* Check to see if link went down during discovery */
1197         if (lpfc_els_chk_latt(phba))
1198                 goto out;
1199
1200         if (irsp->ulpStatus) {
1201                 /* Check for retry */
1202                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1203                         /* ELS command is being retried */
1204                         goto out;
1205                 }
1206                 /* LOGO failed */
1207                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1208                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1209                    ((irsp->un.ulpWord[4] == IOERR_SLI_ABORTED) ||
1210                    (irsp->un.ulpWord[4] == IOERR_SLI_DOWN))) {
1211                         goto out;
1212                 }
1213                 else {
1214                         lpfc_disc_state_machine(phba, ndlp, cmdiocb,
1215                                         NLP_EVT_CMPL_LOGO);
1216                 }
1217         } else {
1218                 /* Good status, call state machine */
1219                 lpfc_disc_state_machine(phba, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
1220
1221                 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1222                         lpfc_unreg_rpi(phba, ndlp);
1223                 }
1224         }
1225
1226 out:
1227         lpfc_els_free_iocb(phba, cmdiocb);
1228         return;
1229 }
1230
1231 int
1232 lpfc_issue_els_logo(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
1233                     uint8_t retry)
1234 {
1235         IOCB_t *icmd;
1236         struct lpfc_iocbq *elsiocb;
1237         struct lpfc_sli_ring *pring;
1238         struct lpfc_sli *psli;
1239         uint8_t *pcmd;
1240         uint16_t cmdsize;
1241
1242         psli = &phba->sli;
1243         pring = &psli->ring[LPFC_ELS_RING];
1244
1245         cmdsize = 2 * (sizeof (uint32_t) + sizeof (struct lpfc_name));
1246         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
1247                                           ndlp, ELS_CMD_LOGO)) == 0) {
1248                 return (1);
1249         }
1250
1251         icmd = &elsiocb->iocb;
1252         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1253         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1254         pcmd += sizeof (uint32_t);
1255
1256         /* Fill in LOGO payload */
1257         *((uint32_t *) (pcmd)) = be32_to_cpu(phba->fc_myDID);
1258         pcmd += sizeof (uint32_t);
1259         memcpy(pcmd, &phba->fc_portname, sizeof (struct lpfc_name));
1260
1261         phba->fc_stat.elsXmitLOGO++;
1262         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1263         spin_lock_irq(phba->host->host_lock);
1264         ndlp->nlp_flag |= NLP_LOGO_SND;
1265         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1266                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
1267                 spin_unlock_irq(phba->host->host_lock);
1268                 lpfc_els_free_iocb(phba, elsiocb);
1269                 return (1);
1270         }
1271         spin_unlock_irq(phba->host->host_lock);
1272         return (0);
1273 }
1274
1275 static void
1276 lpfc_cmpl_els_cmd(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
1277                   struct lpfc_iocbq * rspiocb)
1278 {
1279         IOCB_t *irsp;
1280
1281         irsp = &rspiocb->iocb;
1282
1283         /* ELS cmd tag <ulpIoTag> completes */
1284         lpfc_printf_log(phba,
1285                         KERN_INFO,
1286                         LOG_ELS,
1287                         "%d:0106 ELS cmd tag x%x completes Data: x%x x%x\n",
1288                         phba->brd_no,
1289                         irsp->ulpIoTag, irsp->ulpStatus, irsp->un.ulpWord[4]);
1290
1291         /* Check to see if link went down during discovery */
1292         lpfc_els_chk_latt(phba);
1293         lpfc_els_free_iocb(phba, cmdiocb);
1294         return;
1295 }
1296
1297 int
1298 lpfc_issue_els_scr(struct lpfc_hba * phba, uint32_t nportid, uint8_t retry)
1299 {
1300         IOCB_t *icmd;
1301         struct lpfc_iocbq *elsiocb;
1302         struct lpfc_sli_ring *pring;
1303         struct lpfc_sli *psli;
1304         uint8_t *pcmd;
1305         uint16_t cmdsize;
1306         struct lpfc_nodelist *ndlp;
1307
1308         psli = &phba->sli;
1309         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1310         cmdsize = (sizeof (uint32_t) + sizeof (SCR));
1311         if ((ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL)) == 0) {
1312                 return (1);
1313         }
1314
1315         lpfc_nlp_init(phba, ndlp, nportid);
1316
1317         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
1318                                           ndlp, ELS_CMD_SCR)) == 0) {
1319                 mempool_free( ndlp, phba->nlp_mem_pool);
1320                 return (1);
1321         }
1322
1323         icmd = &elsiocb->iocb;
1324         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1325
1326         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
1327         pcmd += sizeof (uint32_t);
1328
1329         /* For SCR, remainder of payload is SCR parameter page */
1330         memset(pcmd, 0, sizeof (SCR));
1331         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
1332
1333         phba->fc_stat.elsXmitSCR++;
1334         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
1335         spin_lock_irq(phba->host->host_lock);
1336         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1337                 spin_unlock_irq(phba->host->host_lock);
1338                 mempool_free( ndlp, phba->nlp_mem_pool);
1339                 lpfc_els_free_iocb(phba, elsiocb);
1340                 return (1);
1341         }
1342         spin_unlock_irq(phba->host->host_lock);
1343         mempool_free( ndlp, phba->nlp_mem_pool);
1344         return (0);
1345 }
1346
1347 static int
1348 lpfc_issue_els_farpr(struct lpfc_hba * phba, uint32_t nportid, uint8_t retry)
1349 {
1350         IOCB_t *icmd;
1351         struct lpfc_iocbq *elsiocb;
1352         struct lpfc_sli_ring *pring;
1353         struct lpfc_sli *psli;
1354         FARP *fp;
1355         uint8_t *pcmd;
1356         uint32_t *lp;
1357         uint16_t cmdsize;
1358         struct lpfc_nodelist *ondlp;
1359         struct lpfc_nodelist *ndlp;
1360
1361         psli = &phba->sli;
1362         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1363         cmdsize = (sizeof (uint32_t) + sizeof (FARP));
1364         if ((ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL)) == 0) {
1365                 return (1);
1366         }
1367         lpfc_nlp_init(phba, ndlp, nportid);
1368
1369         if ((elsiocb = lpfc_prep_els_iocb(phba, 1, cmdsize, retry,
1370                                           ndlp, ELS_CMD_RNID)) == 0) {
1371                 mempool_free( ndlp, phba->nlp_mem_pool);
1372                 return (1);
1373         }
1374
1375         icmd = &elsiocb->iocb;
1376         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1377
1378         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
1379         pcmd += sizeof (uint32_t);
1380
1381         /* Fill in FARPR payload */
1382         fp = (FARP *) (pcmd);
1383         memset(fp, 0, sizeof (FARP));
1384         lp = (uint32_t *) pcmd;
1385         *lp++ = be32_to_cpu(nportid);
1386         *lp++ = be32_to_cpu(phba->fc_myDID);
1387         fp->Rflags = 0;
1388         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
1389
1390         memcpy(&fp->RportName, &phba->fc_portname, sizeof (struct lpfc_name));
1391         memcpy(&fp->RnodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
1392         if ((ondlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, nportid))) {
1393                 memcpy(&fp->OportName, &ondlp->nlp_portname,
1394                        sizeof (struct lpfc_name));
1395                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
1396                        sizeof (struct lpfc_name));
1397         }
1398
1399         phba->fc_stat.elsXmitFARPR++;
1400         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
1401         spin_lock_irq(phba->host->host_lock);
1402         if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1403                 spin_unlock_irq(phba->host->host_lock);
1404                 mempool_free( ndlp, phba->nlp_mem_pool);
1405                 lpfc_els_free_iocb(phba, elsiocb);
1406                 return (1);
1407         }
1408         spin_unlock_irq(phba->host->host_lock);
1409         mempool_free( ndlp, phba->nlp_mem_pool);
1410         return (0);
1411 }
1412
1413 void
1414 lpfc_els_retry_delay(unsigned long ptr)
1415 {
1416         struct lpfc_nodelist *ndlp;
1417         struct lpfc_hba *phba;
1418         unsigned long iflag;
1419         struct lpfc_work_evt  *evtp;
1420
1421         ndlp = (struct lpfc_nodelist *)ptr;
1422         phba = ndlp->nlp_phba;
1423         evtp = &ndlp->els_retry_evt;
1424
1425         spin_lock_irqsave(phba->host->host_lock, iflag);
1426         if (!list_empty(&evtp->evt_listp)) {
1427                 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1428                 return;
1429         }
1430
1431         evtp->evt_arg1  = ndlp;
1432         evtp->evt       = LPFC_EVT_ELS_RETRY;
1433         list_add_tail(&evtp->evt_listp, &phba->work_list);
1434         if (phba->work_wait)
1435                 wake_up(phba->work_wait);
1436
1437         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1438         return;
1439 }
1440
1441 void
1442 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
1443 {
1444         struct lpfc_hba *phba;
1445         uint32_t cmd;
1446         uint32_t did;
1447         uint8_t retry;
1448
1449         phba = ndlp->nlp_phba;
1450         spin_lock_irq(phba->host->host_lock);
1451         did = (uint32_t) (ndlp->nlp_DID);
1452         cmd = (uint32_t) (ndlp->nlp_last_elscmd);
1453
1454         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
1455                 spin_unlock_irq(phba->host->host_lock);
1456                 return;
1457         }
1458
1459         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1460         spin_unlock_irq(phba->host->host_lock);
1461         retry = ndlp->nlp_retry;
1462
1463         switch (cmd) {
1464         case ELS_CMD_FLOGI:
1465                 lpfc_issue_els_flogi(phba, ndlp, retry);
1466                 break;
1467         case ELS_CMD_PLOGI:
1468                 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1469                 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1470                 lpfc_issue_els_plogi(phba, ndlp, retry);
1471                 break;
1472         case ELS_CMD_ADISC:
1473                 ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1474                 lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1475                 lpfc_issue_els_adisc(phba, ndlp, retry);
1476                 break;
1477         case ELS_CMD_PRLI:
1478                 ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
1479                 lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
1480                 lpfc_issue_els_prli(phba, ndlp, retry);
1481                 break;
1482         case ELS_CMD_LOGO:
1483                 ndlp->nlp_state = NLP_STE_NPR_NODE;
1484                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1485                 lpfc_issue_els_logo(phba, ndlp, retry);
1486                 break;
1487         }
1488         return;
1489 }
1490
1491 static int
1492 lpfc_els_retry(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
1493                struct lpfc_iocbq * rspiocb)
1494 {
1495         IOCB_t *irsp;
1496         struct lpfc_dmabuf *pcmd;
1497         struct lpfc_nodelist *ndlp;
1498         uint32_t *elscmd;
1499         struct ls_rjt stat;
1500         int retry, maxretry;
1501         int delay;
1502         uint32_t cmd;
1503
1504         retry = 0;
1505         delay = 0;
1506         maxretry = lpfc_max_els_tries;
1507         irsp = &rspiocb->iocb;
1508         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1509         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1510         cmd = 0;
1511         /* Note: context2 may be 0 for internal driver abort
1512          * of delays ELS command.
1513          */
1514
1515         if (pcmd && pcmd->virt) {
1516                 elscmd = (uint32_t *) (pcmd->virt);
1517                 cmd = *elscmd++;
1518         }
1519
1520         switch (irsp->ulpStatus) {
1521         case IOSTAT_FCP_RSP_ERROR:
1522         case IOSTAT_REMOTE_STOP:
1523                 break;
1524
1525         case IOSTAT_LOCAL_REJECT:
1526                 switch ((irsp->un.ulpWord[4] & 0xff)) {
1527                 case IOERR_LOOP_OPEN_FAILURE:
1528                         if (cmd == ELS_CMD_PLOGI) {
1529                                 if (cmdiocb->retry == 0) {
1530                                         delay = 1;
1531                                 }
1532                         }
1533                         retry = 1;
1534                         break;
1535
1536                 case IOERR_SEQUENCE_TIMEOUT:
1537                         retry = 1;
1538                         if ((cmd == ELS_CMD_FLOGI)
1539                             && (phba->fc_topology != TOPOLOGY_LOOP)) {
1540                                 delay = 1;
1541                                 maxretry = 48;
1542                         }
1543                         break;
1544
1545                 case IOERR_NO_RESOURCES:
1546                         if (cmd == ELS_CMD_PLOGI) {
1547                                 delay = 1;
1548                         }
1549                         retry = 1;
1550                         break;
1551
1552                 case IOERR_INVALID_RPI:
1553                         retry = 1;
1554                         break;
1555                 }
1556                 break;
1557
1558         case IOSTAT_NPORT_RJT:
1559         case IOSTAT_FABRIC_RJT:
1560                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
1561                         retry = 1;
1562                         break;
1563                 }
1564                 break;
1565
1566         case IOSTAT_NPORT_BSY:
1567         case IOSTAT_FABRIC_BSY:
1568                 retry = 1;
1569                 break;
1570
1571         case IOSTAT_LS_RJT:
1572                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
1573                 /* Added for Vendor specifc support
1574                  * Just keep retrying for these Rsn / Exp codes
1575                  */
1576                 switch (stat.un.b.lsRjtRsnCode) {
1577                 case LSRJT_UNABLE_TPC:
1578                         if (stat.un.b.lsRjtRsnCodeExp ==
1579                             LSEXP_CMD_IN_PROGRESS) {
1580                                 if (cmd == ELS_CMD_PLOGI) {
1581                                         delay = 1;
1582                                         maxretry = 48;
1583                                 }
1584                                 retry = 1;
1585                                 break;
1586                         }
1587                         if (cmd == ELS_CMD_PLOGI) {
1588                                 delay = 1;
1589                                 maxretry = lpfc_max_els_tries + 1;
1590                                 retry = 1;
1591                                 break;
1592                         }
1593                         break;
1594
1595                 case LSRJT_LOGICAL_BSY:
1596                         if (cmd == ELS_CMD_PLOGI) {
1597                                 delay = 1;
1598                                 maxretry = 48;
1599                         }
1600                         retry = 1;
1601                         break;
1602                 }
1603                 break;
1604
1605         case IOSTAT_INTERMED_RSP:
1606         case IOSTAT_BA_RJT:
1607                 break;
1608
1609         default:
1610                 break;
1611         }
1612
1613         if (ndlp->nlp_DID == FDMI_DID) {
1614                 retry = 1;
1615         }
1616
1617         if ((++cmdiocb->retry) >= maxretry) {
1618                 phba->fc_stat.elsRetryExceeded++;
1619                 retry = 0;
1620         }
1621
1622         if (retry) {
1623
1624                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
1625                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1626                                 "%d:0107 Retry ELS command x%x to remote "
1627                                 "NPORT x%x Data: x%x x%x\n",
1628                                 phba->brd_no,
1629                                 cmd, ndlp->nlp_DID, cmdiocb->retry, delay);
1630
1631                 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) {
1632                         /* If discovery / RSCN timer is running, reset it */
1633                         if (timer_pending(&phba->fc_disctmo) ||
1634                               (phba->fc_flag & FC_RSCN_MODE)) {
1635                                 lpfc_set_disctmo(phba);
1636                         }
1637                 }
1638
1639                 phba->fc_stat.elsXmitRetry++;
1640                 if (delay) {
1641                         phba->fc_stat.elsDelayRetry++;
1642                         ndlp->nlp_retry = cmdiocb->retry;
1643
1644                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
1645                         ndlp->nlp_flag |= NLP_DELAY_TMO;
1646
1647                         ndlp->nlp_state = NLP_STE_NPR_NODE;
1648                         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1649                         ndlp->nlp_last_elscmd = cmd;
1650
1651                         return (1);
1652                 }
1653                 switch (cmd) {
1654                 case ELS_CMD_FLOGI:
1655                         lpfc_issue_els_flogi(phba, ndlp, cmdiocb->retry);
1656                         return (1);
1657                 case ELS_CMD_PLOGI:
1658                         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
1659                         lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
1660                         lpfc_issue_els_plogi(phba, ndlp, cmdiocb->retry);
1661                         return (1);
1662                 case ELS_CMD_ADISC:
1663                         ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
1664                         lpfc_nlp_list(phba, ndlp, NLP_ADISC_LIST);
1665                         lpfc_issue_els_adisc(phba, ndlp, cmdiocb->retry);
1666                         return (1);
1667                 case ELS_CMD_PRLI:
1668                         ndlp->nlp_state = NLP_STE_PRLI_ISSUE;
1669                         lpfc_nlp_list(phba, ndlp, NLP_PRLI_LIST);
1670                         lpfc_issue_els_prli(phba, ndlp, cmdiocb->retry);
1671                         return (1);
1672                 case ELS_CMD_LOGO:
1673                         ndlp->nlp_state = NLP_STE_NPR_NODE;
1674                         lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1675                         lpfc_issue_els_logo(phba, ndlp, cmdiocb->retry);
1676                         return (1);
1677                 }
1678         }
1679
1680         /* No retry ELS command <elsCmd> to remote NPORT <did> */
1681         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1682                         "%d:0108 No retry ELS command x%x to remote NPORT x%x "
1683                         "Data: x%x x%x\n",
1684                         phba->brd_no,
1685                         cmd, ndlp->nlp_DID, cmdiocb->retry, ndlp->nlp_flag);
1686
1687         return (0);
1688 }
1689
1690 int
1691 lpfc_els_free_iocb(struct lpfc_hba * phba, struct lpfc_iocbq * elsiocb)
1692 {
1693         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
1694
1695         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
1696         if (elsiocb->context2) {
1697                 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
1698                 /* Free the response before processing the command.  */
1699                 if (!list_empty(&buf_ptr1->list)) {
1700                         list_remove_head(&buf_ptr1->list, buf_ptr,
1701                                          struct lpfc_dmabuf,
1702                                          list);
1703                         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1704                         kfree(buf_ptr);
1705                 }
1706                 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
1707                 kfree(buf_ptr1);
1708         }
1709
1710         if (elsiocb->context3) {
1711                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
1712                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1713                 kfree(buf_ptr);
1714         }
1715         spin_lock_irq(phba->host->host_lock);
1716         list_add_tail(&elsiocb->list, &phba->lpfc_iocb_list);
1717         spin_unlock_irq(phba->host->host_lock);
1718         return 0;
1719 }
1720
1721 static void
1722 lpfc_cmpl_els_logo_acc(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
1723                        struct lpfc_iocbq * rspiocb)
1724 {
1725         struct lpfc_nodelist *ndlp;
1726
1727         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1728
1729         /* ACC to LOGO completes to NPort <nlp_DID> */
1730         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1731                         "%d:0109 ACC to LOGO completes to NPort x%x "
1732                         "Data: x%x x%x x%x\n",
1733                         phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
1734                         ndlp->nlp_state, ndlp->nlp_rpi);
1735
1736         spin_lock_irq(phba->host->host_lock);
1737         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
1738         spin_unlock_irq(phba->host->host_lock);
1739
1740         switch (ndlp->nlp_state) {
1741         case NLP_STE_UNUSED_NODE:       /* node is just allocated */
1742                 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1743                 break;
1744         case NLP_STE_NPR_NODE:          /* NPort Recovery mode */
1745                 lpfc_unreg_rpi(phba, ndlp);
1746                 break;
1747         default:
1748                 break;
1749         }
1750         lpfc_els_free_iocb(phba, cmdiocb);
1751         return;
1752 }
1753
1754 static void
1755 lpfc_cmpl_els_acc(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
1756                   struct lpfc_iocbq * rspiocb)
1757 {
1758         struct lpfc_nodelist *ndlp;
1759         LPFC_MBOXQ_t *mbox = NULL;
1760
1761         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1762         if (cmdiocb->context_un.mbox)
1763                 mbox = cmdiocb->context_un.mbox;
1764
1765
1766         /* Check to see if link went down during discovery */
1767         if ((lpfc_els_chk_latt(phba)) || !ndlp) {
1768                 if (mbox) {
1769                         mempool_free( mbox, phba->mbox_mem_pool);
1770                 }
1771                 goto out;
1772         }
1773
1774         /* ELS response tag <ulpIoTag> completes */
1775         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1776                         "%d:0110 ELS response tag x%x completes "
1777                         "Data: x%x x%x x%x x%x x%x x%x\n",
1778                         phba->brd_no,
1779                         cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
1780                         rspiocb->iocb.un.ulpWord[4], ndlp->nlp_DID,
1781                         ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
1782
1783         if (mbox) {
1784                 if ((rspiocb->iocb.ulpStatus == 0)
1785                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
1786                         /* set_slim mailbox command needs to execute first,
1787                          * queue this command to be processed later.
1788                          */
1789                         lpfc_unreg_rpi(phba, ndlp);
1790                         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
1791                         mbox->context2 = ndlp;
1792                         ndlp->nlp_state = NLP_STE_REG_LOGIN_ISSUE;
1793                         lpfc_nlp_list(phba, ndlp, NLP_REGLOGIN_LIST);
1794                         if (lpfc_sli_issue_mbox(phba, mbox,
1795                                                 (MBX_NOWAIT | MBX_STOP_IOCB))
1796                             != MBX_NOT_FINISHED) {
1797                                 goto out;
1798                         }
1799                         /* NOTE: we should have messages for unsuccessful
1800                            reglogin */
1801                         mempool_free( mbox, phba->mbox_mem_pool);
1802                 } else {
1803                         mempool_free( mbox, phba->mbox_mem_pool);
1804                         if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1805                                 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1806                         }
1807                 }
1808         }
1809 out:
1810         if (ndlp) {
1811                 spin_lock_irq(phba->host->host_lock);
1812                 ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
1813                 spin_unlock_irq(phba->host->host_lock);
1814         }
1815         lpfc_els_free_iocb(phba, cmdiocb);
1816         return;
1817 }
1818
1819 int
1820 lpfc_els_rsp_acc(struct lpfc_hba * phba, uint32_t flag,
1821                  struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp,
1822                  LPFC_MBOXQ_t * mbox, uint8_t newnode)
1823 {
1824         IOCB_t *icmd;
1825         IOCB_t *oldcmd;
1826         struct lpfc_iocbq *elsiocb;
1827         struct lpfc_sli_ring *pring;
1828         struct lpfc_sli *psli;
1829         uint8_t *pcmd;
1830         uint16_t cmdsize;
1831         int rc;
1832
1833         psli = &phba->sli;
1834         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1835         oldcmd = &oldiocb->iocb;
1836
1837         switch (flag) {
1838         case ELS_CMD_ACC:
1839                 cmdsize = sizeof (uint32_t);
1840                 if ((elsiocb =
1841                      lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
1842                                         ndlp, ELS_CMD_ACC)) == 0) {
1843                         return (1);
1844                 }
1845                 icmd = &elsiocb->iocb;
1846                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
1847                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1848                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
1849                 pcmd += sizeof (uint32_t);
1850                 break;
1851         case ELS_CMD_PLOGI:
1852                 cmdsize = (sizeof (struct serv_parm) + sizeof (uint32_t));
1853                 if ((elsiocb =
1854                      lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
1855                                         ndlp, ELS_CMD_ACC)) == 0) {
1856                         return (1);
1857                 }
1858                 icmd = &elsiocb->iocb;
1859                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
1860                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1861
1862                 if (mbox)
1863                         elsiocb->context_un.mbox = mbox;
1864
1865                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
1866                 pcmd += sizeof (uint32_t);
1867                 memcpy(pcmd, &phba->fc_sparam, sizeof (struct serv_parm));
1868                 break;
1869         default:
1870                 return (1);
1871         }
1872
1873         if (newnode)
1874                 elsiocb->context1 = NULL;
1875
1876         /* Xmit ELS ACC response tag <ulpIoTag> */
1877         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1878                         "%d:0128 Xmit ELS ACC response tag x%x "
1879                         "Data: x%x x%x x%x x%x x%x\n",
1880                         phba->brd_no,
1881                         elsiocb->iocb.ulpIoTag,
1882                         elsiocb->iocb.ulpContext, ndlp->nlp_DID,
1883                         ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
1884
1885         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
1886                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
1887         } else {
1888                 elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
1889         }
1890
1891         phba->fc_stat.elsXmitACC++;
1892         spin_lock_irq(phba->host->host_lock);
1893         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1894         spin_unlock_irq(phba->host->host_lock);
1895         if (rc == IOCB_ERROR) {
1896                 lpfc_els_free_iocb(phba, elsiocb);
1897                 return (1);
1898         }
1899         return (0);
1900 }
1901
1902 int
1903 lpfc_els_rsp_reject(struct lpfc_hba * phba, uint32_t rejectError,
1904                     struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
1905 {
1906         IOCB_t *icmd;
1907         IOCB_t *oldcmd;
1908         struct lpfc_iocbq *elsiocb;
1909         struct lpfc_sli_ring *pring;
1910         struct lpfc_sli *psli;
1911         uint8_t *pcmd;
1912         uint16_t cmdsize;
1913         int rc;
1914
1915         psli = &phba->sli;
1916         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1917
1918         cmdsize = 2 * sizeof (uint32_t);
1919         if ((elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
1920                                           ndlp, ELS_CMD_LS_RJT)) == 0) {
1921                 return (1);
1922         }
1923
1924         icmd = &elsiocb->iocb;
1925         oldcmd = &oldiocb->iocb;
1926         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
1927         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1928
1929         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
1930         pcmd += sizeof (uint32_t);
1931         *((uint32_t *) (pcmd)) = rejectError;
1932
1933         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
1934         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1935                         "%d:0129 Xmit ELS RJT x%x response tag x%x "
1936                         "Data: x%x x%x x%x x%x x%x\n",
1937                         phba->brd_no,
1938                         rejectError, elsiocb->iocb.ulpIoTag,
1939                         elsiocb->iocb.ulpContext, ndlp->nlp_DID,
1940                         ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
1941
1942         phba->fc_stat.elsXmitLSRJT++;
1943         elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
1944         spin_lock_irq(phba->host->host_lock);
1945         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1946         spin_unlock_irq(phba->host->host_lock);
1947         if (rc == IOCB_ERROR) {
1948                 lpfc_els_free_iocb(phba, elsiocb);
1949                 return (1);
1950         }
1951         return (0);
1952 }
1953
1954 int
1955 lpfc_els_rsp_adisc_acc(struct lpfc_hba * phba,
1956                        struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
1957 {
1958         ADISC *ap;
1959         IOCB_t *icmd;
1960         IOCB_t *oldcmd;
1961         struct lpfc_iocbq *elsiocb;
1962         struct lpfc_sli_ring *pring;
1963         struct lpfc_sli *psli;
1964         uint8_t *pcmd;
1965         uint16_t cmdsize;
1966         int rc;
1967
1968         psli = &phba->sli;
1969         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
1970
1971         cmdsize = sizeof (uint32_t) + sizeof (ADISC);
1972         if ((elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
1973                                           ndlp, ELS_CMD_ACC)) == 0) {
1974                 return (1);
1975         }
1976
1977         /* Xmit ADISC ACC response tag <ulpIoTag> */
1978         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1979                         "%d:0130 Xmit ADISC ACC response tag x%x "
1980                         "Data: x%x x%x x%x x%x x%x\n",
1981                         phba->brd_no,
1982                         elsiocb->iocb.ulpIoTag,
1983                         elsiocb->iocb.ulpContext, ndlp->nlp_DID,
1984                         ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
1985
1986         icmd = &elsiocb->iocb;
1987         oldcmd = &oldiocb->iocb;
1988         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
1989         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1990
1991         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
1992         pcmd += sizeof (uint32_t);
1993
1994         ap = (ADISC *) (pcmd);
1995         ap->hardAL_PA = phba->fc_pref_ALPA;
1996         memcpy(&ap->portName, &phba->fc_portname, sizeof (struct lpfc_name));
1997         memcpy(&ap->nodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
1998         ap->DID = be32_to_cpu(phba->fc_myDID);
1999
2000         phba->fc_stat.elsXmitACC++;
2001         elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
2002         spin_lock_irq(phba->host->host_lock);
2003         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2004         spin_unlock_irq(phba->host->host_lock);
2005         if (rc == IOCB_ERROR) {
2006                 lpfc_els_free_iocb(phba, elsiocb);
2007                 return (1);
2008         }
2009         return (0);
2010 }
2011
2012 int
2013 lpfc_els_rsp_prli_acc(struct lpfc_hba * phba,
2014                       struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
2015 {
2016         PRLI *npr;
2017         lpfc_vpd_t *vpd;
2018         IOCB_t *icmd;
2019         IOCB_t *oldcmd;
2020         struct lpfc_iocbq *elsiocb;
2021         struct lpfc_sli_ring *pring;
2022         struct lpfc_sli *psli;
2023         uint8_t *pcmd;
2024         uint16_t cmdsize;
2025         int rc;
2026
2027         psli = &phba->sli;
2028         pring = &psli->ring[LPFC_ELS_RING];     /* ELS ring */
2029
2030         cmdsize = sizeof (uint32_t) + sizeof (PRLI);
2031         if ((elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
2032                                           ndlp,
2033                                           (ELS_CMD_ACC |
2034                                            (ELS_CMD_PRLI & ~ELS_RSP_MASK)))) ==
2035             0) {
2036                 return (1);
2037         }
2038
2039         /* Xmit PRLI ACC response tag <ulpIoTag> */
2040         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2041                         "%d:0131 Xmit PRLI ACC response tag x%x "
2042                         "Data: x%x x%x x%x x%x x%x\n",
2043                         phba->brd_no,
2044                         elsiocb->iocb.ulpIoTag,
2045                         elsiocb->iocb.ulpContext, ndlp->nlp_DID,
2046                         ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
2047
2048         icmd = &elsiocb->iocb;
2049         oldcmd = &oldiocb->iocb;
2050         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2051         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2052
2053         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
2054         pcmd += sizeof (uint32_t);
2055
2056         /* For PRLI, remainder of payload is PRLI parameter page */
2057         memset(pcmd, 0, sizeof (PRLI));
2058
2059         npr = (PRLI *) pcmd;
2060         vpd = &phba->vpd;
2061         /*
2062          * If our firmware version is 3.20 or later,
2063          * set the following bits for FC-TAPE support.
2064          */
2065         if (vpd->rev.feaLevelHigh >= 0x02) {
2066                 npr->ConfmComplAllowed = 1;
2067                 npr->Retry = 1;
2068                 npr->TaskRetryIdReq = 1;
2069         }
2070
2071         npr->acceptRspCode = PRLI_REQ_EXECUTED;
2072         npr->estabImagePair = 1;
2073         npr->readXferRdyDis = 1;
2074         npr->ConfmComplAllowed = 1;
2075
2076         npr->prliType = PRLI_FCP_TYPE;
2077         npr->initiatorFunc = 1;
2078
2079         phba->fc_stat.elsXmitACC++;
2080         elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
2081
2082         spin_lock_irq(phba->host->host_lock);
2083         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2084         spin_unlock_irq(phba->host->host_lock);
2085         if (rc == IOCB_ERROR) {
2086                 lpfc_els_free_iocb(phba, elsiocb);
2087                 return (1);
2088         }
2089         return (0);
2090 }
2091
2092 static int
2093 lpfc_els_rsp_rnid_acc(struct lpfc_hba * phba,
2094                       uint8_t format,
2095                       struct lpfc_iocbq * oldiocb, struct lpfc_nodelist * ndlp)
2096 {
2097         RNID *rn;
2098         IOCB_t *icmd;
2099         IOCB_t *oldcmd;
2100         struct lpfc_iocbq *elsiocb;
2101         struct lpfc_sli_ring *pring;
2102         struct lpfc_sli *psli;
2103         uint8_t *pcmd;
2104         uint16_t cmdsize;
2105         int rc;
2106
2107         psli = &phba->sli;
2108         pring = &psli->ring[LPFC_ELS_RING];
2109
2110         cmdsize = sizeof (uint32_t) + sizeof (uint32_t)
2111                 + (2 * sizeof (struct lpfc_name));
2112         if (format)
2113                 cmdsize += sizeof (RNID_TOP_DISC);
2114
2115         if ((elsiocb = lpfc_prep_els_iocb(phba, 0, cmdsize, oldiocb->retry,
2116                                           ndlp, ELS_CMD_ACC)) == 0) {
2117                 return (1);
2118         }
2119
2120         /* Xmit RNID ACC response tag <ulpIoTag> */
2121         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
2122                         "%d:0132 Xmit RNID ACC response tag x%x "
2123                         "Data: x%x\n",
2124                         phba->brd_no,
2125                         elsiocb->iocb.ulpIoTag,
2126                         elsiocb->iocb.ulpContext);
2127
2128         icmd = &elsiocb->iocb;
2129         oldcmd = &oldiocb->iocb;
2130         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
2131         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2132
2133         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
2134         pcmd += sizeof (uint32_t);
2135
2136         memset(pcmd, 0, sizeof (RNID));
2137         rn = (RNID *) (pcmd);
2138         rn->Format = format;
2139         rn->CommonLen = (2 * sizeof (struct lpfc_name));
2140         memcpy(&rn->portName, &phba->fc_portname, sizeof (struct lpfc_name));
2141         memcpy(&rn->nodeName, &phba->fc_nodename, sizeof (struct lpfc_name));
2142         switch (format) {
2143         case 0:
2144                 rn->SpecificLen = 0;
2145                 break;
2146         case RNID_TOPOLOGY_DISC:
2147                 rn->SpecificLen = sizeof (RNID_TOP_DISC);
2148                 memcpy(&rn->un.topologyDisc.portName,
2149                        &phba->fc_portname, sizeof (struct lpfc_name));
2150                 rn->un.topologyDisc.unitType = RNID_HBA;
2151                 rn->un.topologyDisc.physPort = 0;
2152                 rn->un.topologyDisc.attachedNodes = 0;
2153                 break;
2154         default:
2155                 rn->CommonLen = 0;
2156                 rn->SpecificLen = 0;
2157                 break;
2158         }
2159
2160         phba->fc_stat.elsXmitACC++;
2161         elsiocb->iocb_cmpl = lpfc_cmpl_els_acc;
2162         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
2163                                     * it could be freed */
2164
2165         spin_lock_irq(phba->host->host_lock);
2166         rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
2167         spin_unlock_irq(phba->host->host_lock);
2168         if (rc == IOCB_ERROR) {
2169                 lpfc_els_free_iocb(phba, elsiocb);
2170                 return (1);
2171         }
2172         return (0);
2173 }
2174
2175 int
2176 lpfc_els_disc_adisc(struct lpfc_hba * phba)
2177 {
2178         int sentadisc;
2179         struct lpfc_nodelist *ndlp, *next_ndlp;
2180
2181         sentadisc = 0;
2182         /* go thru NPR list and issue any remaining ELS ADISCs */
2183         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
2184                         nlp_listp) {
2185                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2186                         if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2187                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2188                                 ndlp->nlp_state = NLP_STE_ADISC_ISSUE;
2189                                 lpfc_nlp_list(phba, ndlp,
2190                                         NLP_ADISC_LIST);
2191                                 lpfc_issue_els_adisc(phba, ndlp, 0);
2192                                 sentadisc++;
2193                                 phba->num_disc_nodes++;
2194                                 if (phba->num_disc_nodes >=
2195                                     phba->cfg_discovery_threads) {
2196                                         spin_lock_irq(phba->host->host_lock);
2197                                         phba->fc_flag |= FC_NLP_MORE;
2198                                         spin_unlock_irq(phba->host->host_lock);
2199                                         break;
2200                                 }
2201                         }
2202                 }
2203         }
2204         if (sentadisc == 0) {
2205                 spin_lock_irq(phba->host->host_lock);
2206                 phba->fc_flag &= ~FC_NLP_MORE;
2207                 spin_unlock_irq(phba->host->host_lock);
2208         }
2209         return(sentadisc);
2210 }
2211
2212 int
2213 lpfc_els_disc_plogi(struct lpfc_hba * phba)
2214 {
2215         int sentplogi;
2216         struct lpfc_nodelist *ndlp, *next_ndlp;
2217
2218         sentplogi = 0;
2219         /* go thru NPR list and issue any remaining ELS PLOGIs */
2220         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
2221                                 nlp_listp) {
2222                 if ((ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
2223                    (!(ndlp->nlp_flag & NLP_DELAY_TMO))) {
2224                         if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2225                                 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
2226                                 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
2227                                 lpfc_issue_els_plogi(phba, ndlp, 0);
2228                                 sentplogi++;
2229                                 phba->num_disc_nodes++;
2230                                 if (phba->num_disc_nodes >=
2231                                     phba->cfg_discovery_threads) {
2232                                         spin_lock_irq(phba->host->host_lock);
2233                                         phba->fc_flag |= FC_NLP_MORE;
2234                                         spin_unlock_irq(phba->host->host_lock);
2235                                         break;
2236                                 }
2237                         }
2238                 }
2239         }
2240         if (sentplogi == 0) {
2241                 spin_lock_irq(phba->host->host_lock);
2242                 phba->fc_flag &= ~FC_NLP_MORE;
2243                 spin_unlock_irq(phba->host->host_lock);
2244         }
2245         return(sentplogi);
2246 }
2247
2248 int
2249 lpfc_els_flush_rscn(struct lpfc_hba * phba)
2250 {
2251         struct lpfc_dmabuf *mp;
2252         int i;
2253
2254         for (i = 0; i < phba->fc_rscn_id_cnt; i++) {
2255                 mp = phba->fc_rscn_id_list[i];
2256                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2257                 kfree(mp);
2258                 phba->fc_rscn_id_list[i] = NULL;
2259         }
2260         phba->fc_rscn_id_cnt = 0;
2261         spin_lock_irq(phba->host->host_lock);
2262         phba->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
2263         spin_unlock_irq(phba->host->host_lock);
2264         lpfc_can_disctmo(phba);
2265         return (0);
2266 }
2267
2268 int
2269 lpfc_rscn_payload_check(struct lpfc_hba * phba, uint32_t did)
2270 {
2271         D_ID ns_did;
2272         D_ID rscn_did;
2273         struct lpfc_dmabuf *mp;
2274         uint32_t *lp;
2275         uint32_t payload_len, cmd, i, match;
2276
2277         ns_did.un.word = did;
2278         match = 0;
2279
2280         /* Never match fabric nodes for RSCNs */
2281         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2282                 return(0);
2283
2284         /* If we are doing a FULL RSCN rediscovery, match everything */
2285         if (phba->fc_flag & FC_RSCN_DISCOVERY) {
2286                 return (did);
2287         }
2288
2289         for (i = 0; i < phba->fc_rscn_id_cnt; i++) {
2290                 mp = phba->fc_rscn_id_list[i];
2291                 lp = (uint32_t *) mp->virt;
2292                 cmd = *lp++;
2293                 payload_len = be32_to_cpu(cmd) & 0xffff; /* payload length */
2294                 payload_len -= sizeof (uint32_t);       /* take off word 0 */
2295                 while (payload_len) {
2296                         rscn_did.un.word = *lp++;
2297                         rscn_did.un.word = be32_to_cpu(rscn_did.un.word);
2298                         payload_len -= sizeof (uint32_t);
2299                         switch (rscn_did.un.b.resv) {
2300                         case 0: /* Single N_Port ID effected */
2301                                 if (ns_did.un.word == rscn_did.un.word) {
2302                                         match = did;
2303                                 }
2304                                 break;
2305                         case 1: /* Whole N_Port Area effected */
2306                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
2307                                     && (ns_did.un.b.area == rscn_did.un.b.area))
2308                                         {
2309                                                 match = did;
2310                                         }
2311                                 break;
2312                         case 2: /* Whole N_Port Domain effected */
2313                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
2314                                         {
2315                                                 match = did;
2316                                         }
2317                                 break;
2318                         case 3: /* Whole Fabric effected */
2319                                 match = did;
2320                                 break;
2321                         default:
2322                                 /* Unknown Identifier in RSCN list */
2323                                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2324                                                 "%d:0217 Unknown Identifier in "
2325                                                 "RSCN payload Data: x%x\n",
2326                                                 phba->brd_no, rscn_did.un.word);
2327                                 break;
2328                         }
2329                         if (match) {
2330                                 break;
2331                         }
2332                 }
2333         }
2334         return (match);
2335 }
2336
2337 static int
2338 lpfc_rscn_recovery_check(struct lpfc_hba * phba)
2339 {
2340         struct lpfc_nodelist *ndlp = NULL, *next_ndlp;
2341         struct list_head *listp;
2342         struct list_head *node_list[7];
2343         int i;
2344
2345         /* Look at all nodes effected by pending RSCNs and move
2346          * them to NPR list.
2347          */
2348         node_list[0] = &phba->fc_npr_list;  /* MUST do this list first */
2349         node_list[1] = &phba->fc_nlpmap_list;
2350         node_list[2] = &phba->fc_nlpunmap_list;
2351         node_list[3] = &phba->fc_prli_list;
2352         node_list[4] = &phba->fc_reglogin_list;
2353         node_list[5] = &phba->fc_adisc_list;
2354         node_list[6] = &phba->fc_plogi_list;
2355         for (i = 0; i < 7; i++) {
2356                 listp = node_list[i];
2357                 if (list_empty(listp))
2358                         continue;
2359
2360                 list_for_each_entry_safe(ndlp, next_ndlp, listp, nlp_listp) {
2361                         if (!(lpfc_rscn_payload_check(phba, ndlp->nlp_DID)))
2362                                 continue;
2363
2364                         lpfc_disc_state_machine(phba, ndlp, NULL,
2365                                         NLP_EVT_DEVICE_RECOVERY);
2366                         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
2367                                 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2368                                 del_timer_sync(&ndlp->nlp_delayfunc);
2369                                 if (!list_empty(&ndlp->
2370                                                 els_retry_evt.evt_listp))
2371                                         list_del_init(&ndlp->
2372                                                 els_retry_evt.evt_listp);
2373                         }
2374                 }
2375         }
2376         return (0);
2377 }
2378
2379 static int
2380 lpfc_els_rcv_rscn(struct lpfc_hba * phba,
2381                   struct lpfc_iocbq * cmdiocb,
2382                   struct lpfc_nodelist * ndlp, uint8_t newnode)
2383 {
2384         struct lpfc_dmabuf *pcmd;
2385         uint32_t *lp;
2386         IOCB_t *icmd;
2387         uint32_t payload_len, cmd;
2388
2389         icmd = &cmdiocb->iocb;
2390         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2391         lp = (uint32_t *) pcmd->virt;
2392
2393         cmd = *lp++;
2394         payload_len = be32_to_cpu(cmd) & 0xffff;        /* payload length */
2395         payload_len -= sizeof (uint32_t);       /* take off word 0 */
2396         cmd &= ELS_CMD_MASK;
2397
2398         /* RSCN received */
2399         lpfc_printf_log(phba,
2400                         KERN_INFO,
2401                         LOG_DISCOVERY,
2402                         "%d:0214 RSCN received Data: x%x x%x x%x x%x\n",
2403                         phba->brd_no,
2404                         phba->fc_flag, payload_len, *lp, phba->fc_rscn_id_cnt);
2405
2406         /* If we are about to begin discovery, just ACC the RSCN.
2407          * Discovery processing will satisfy it.
2408          */
2409         if (phba->hba_state < LPFC_NS_QRY) {
2410                 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
2411                                                                 newnode);
2412                 return (0);
2413         }
2414
2415         /* If we are already processing an RSCN, save the received
2416          * RSCN payload buffer, cmdiocb->context2 to process later.
2417          */
2418         if (phba->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
2419                 if ((phba->fc_rscn_id_cnt < FC_MAX_HOLD_RSCN) &&
2420                     !(phba->fc_flag & FC_RSCN_DISCOVERY)) {
2421                         spin_lock_irq(phba->host->host_lock);
2422                         phba->fc_flag |= FC_RSCN_MODE;
2423                         spin_unlock_irq(phba->host->host_lock);
2424                         phba->fc_rscn_id_list[phba->fc_rscn_id_cnt++] = pcmd;
2425
2426                         /* If we zero, cmdiocb->context2, the calling
2427                          * routine will not try to free it.
2428                          */
2429                         cmdiocb->context2 = NULL;
2430
2431                         /* Deferred RSCN */
2432                         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2433                                         "%d:0235 Deferred RSCN "
2434                                         "Data: x%x x%x x%x\n",
2435                                         phba->brd_no, phba->fc_rscn_id_cnt,
2436                                         phba->fc_flag, phba->hba_state);
2437                 } else {
2438                         spin_lock_irq(phba->host->host_lock);
2439                         phba->fc_flag |= FC_RSCN_DISCOVERY;
2440                         spin_unlock_irq(phba->host->host_lock);
2441                         /* ReDiscovery RSCN */
2442                         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
2443                                         "%d:0234 ReDiscovery RSCN "
2444                                         "Data: x%x x%x x%x\n",
2445                                         phba->brd_no, phba->fc_rscn_id_cnt,
2446                                         phba->fc_flag, phba->hba_state);
2447                 }
2448                 /* Send back ACC */
2449                 lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL,
2450                                                                 newnode);
2451
2452                 /* send RECOVERY event for ALL nodes that match RSCN payload */
2453                 lpfc_rscn_recovery_check(phba);
2454                 return (0);
2455         }
2456
2457         phba->fc_flag |= FC_RSCN_MODE;
2458         phba->fc_rscn_id_list[phba->fc_rscn_id_cnt++] = pcmd;
2459         /*
2460          * If we zero, cmdiocb->context2, the calling routine will
2461          * not try to free it.
2462          */
2463         cmdiocb->context2 = NULL;
2464
2465         lpfc_set_disctmo(phba);
2466
2467         /* Send back ACC */
2468         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, newnode);
2469
2470         /* send RECOVERY event for ALL nodes that match RSCN payload */
2471         lpfc_rscn_recovery_check(phba);
2472
2473         return (lpfc_els_handle_rscn(phba));
2474 }
2475
2476 int
2477 lpfc_els_handle_rscn(struct lpfc_hba * phba)
2478 {
2479         struct lpfc_nodelist *ndlp;
2480
2481         /* Start timer for RSCN processing */
2482         lpfc_set_disctmo(phba);
2483
2484         /* RSCN processed */
2485         lpfc_printf_log(phba,
2486                         KERN_INFO,
2487                         LOG_DISCOVERY,
2488                         "%d:0215 RSCN processed Data: x%x x%x x%x x%x\n",
2489                         phba->brd_no,
2490                         phba->fc_flag, 0, phba->fc_rscn_id_cnt,
2491                         phba->hba_state);
2492
2493         /* To process RSCN, first compare RSCN data with NameServer */
2494         phba->fc_ns_retry = 0;
2495         if ((ndlp = lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED,
2496                                       NameServer_DID))) {
2497                 /* Good ndlp, issue CT Request to NameServer */
2498                 if (lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT) == 0) {
2499                         /* Wait for NameServer query cmpl before we can
2500                            continue */
2501                         return (1);
2502                 }
2503         } else {
2504                 /* If login to NameServer does not exist, issue one */
2505                 /* Good status, issue PLOGI to NameServer */
2506                 if ((ndlp =
2507                      lpfc_findnode_did(phba, NLP_SEARCH_ALL, NameServer_DID))) {
2508                         /* Wait for NameServer login cmpl before we can
2509                            continue */
2510                         return (1);
2511                 }
2512                 if ((ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL))
2513                     == 0) {
2514                         lpfc_els_flush_rscn(phba);
2515                         return (0);
2516                 } else {
2517                         lpfc_nlp_init(phba, ndlp, NameServer_DID);
2518                         ndlp->nlp_type |= NLP_FABRIC;
2519                         ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
2520                         lpfc_issue_els_plogi(phba, ndlp, 0);
2521                         /* Wait for NameServer login cmpl before we can
2522                            continue */
2523                         return (1);
2524                 }
2525         }
2526
2527         lpfc_els_flush_rscn(phba);
2528         return (0);
2529 }
2530
2531 static int
2532 lpfc_els_rcv_flogi(struct lpfc_hba * phba,
2533                    struct lpfc_iocbq * cmdiocb,
2534                    struct lpfc_nodelist * ndlp, uint8_t newnode)
2535 {
2536         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2537         uint32_t *lp = (uint32_t *) pcmd->virt;
2538         IOCB_t *icmd = &cmdiocb->iocb;
2539         struct serv_parm *sp;
2540         LPFC_MBOXQ_t *mbox;
2541         struct ls_rjt stat;
2542         uint32_t cmd, did;
2543         int rc;
2544
2545         cmd = *lp++;
2546         sp = (struct serv_parm *) lp;
2547
2548         /* FLOGI received */
2549
2550         lpfc_set_disctmo(phba);
2551
2552         if (phba->fc_topology == TOPOLOGY_LOOP) {
2553                 /* We should never receive a FLOGI in loop mode, ignore it */
2554                 did = icmd->un.elsreq64.remoteID;
2555
2556                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
2557                    Loop Mode */
2558                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
2559                                 "%d:0113 An FLOGI ELS command x%x was received "
2560                                 "from DID x%x in Loop Mode\n",
2561                                 phba->brd_no, cmd, did);
2562                 return (1);
2563         }
2564
2565         did = Fabric_DID;
2566
2567         if ((lpfc_check_sparm(phba, ndlp, sp, CLASS3))) {
2568                 /* For a FLOGI we accept, then if our portname is greater
2569                  * then the remote portname we initiate Nport login.
2570                  */
2571
2572                 rc = memcmp(&phba->fc_portname, &sp->portName,
2573                             sizeof (struct lpfc_name));
2574
2575                 if (!rc) {
2576                         if ((mbox = mempool_alloc(phba->mbox_mem_pool,
2577                                                   GFP_KERNEL)) == 0) {
2578                                 return (1);
2579                         }
2580                         lpfc_linkdown(phba);
2581                         lpfc_init_link(phba, mbox,
2582                                        phba->cfg_topology,
2583                                        phba->cfg_link_speed);
2584                         mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2585                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2586                         rc = lpfc_sli_issue_mbox
2587                                 (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
2588                         if (rc == MBX_NOT_FINISHED) {
2589                                 mempool_free( mbox, phba->mbox_mem_pool);
2590                         }
2591                         return (1);
2592                 }
2593                 else if (rc > 0) {      /* greater than */
2594                         spin_lock_irq(phba->host->host_lock);
2595                         phba->fc_flag |= FC_PT2PT_PLOGI;
2596                         spin_unlock_irq(phba->host->host_lock);
2597                 }
2598                 phba->fc_flag |= FC_PT2PT;
2599                 phba->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2600         } else {
2601                 /* Reject this request because invalid parameters */
2602                 stat.un.b.lsRjtRsvd0 = 0;
2603                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2604                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
2605                 stat.un.b.vendorUnique = 0;
2606                 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
2607                 return (1);
2608         }
2609
2610         /* Send back ACC */
2611         lpfc_els_rsp_acc(phba, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL, newnode);
2612
2613         return (0);
2614 }
2615
2616 static int
2617 lpfc_els_rcv_rnid(struct lpfc_hba * phba,
2618                   struct lpfc_iocbq * cmdiocb, struct lpfc_nodelist * ndlp)
2619 {
2620         struct lpfc_dmabuf *pcmd;
2621         uint32_t *lp;
2622         IOCB_t *icmd;
2623         RNID *rn;
2624         struct ls_rjt stat;
2625         uint32_t cmd, did;
2626
2627         icmd = &cmdiocb->iocb;
2628         did = icmd->un.elsreq64.remoteID;
2629         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2630         lp = (uint32_t *) pcmd->virt;
2631
2632         cmd = *lp++;
2633         rn = (RNID *) lp;
2634
2635         /* RNID received */
2636
2637         switch (rn->Format) {
2638         case 0:
2639         case RNID_TOPOLOGY_DISC:
2640                 /* Send back ACC */
2641                 lpfc_els_rsp_rnid_acc(phba, rn->Format, cmdiocb, ndlp);
2642                 break;
2643         default:
2644                 /* Reject this request because format not supported */
2645                 stat.un.b.lsRjtRsvd0 = 0;
2646                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2647                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
2648                 stat.un.b.vendorUnique = 0;
2649                 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, cmdiocb, ndlp);
2650         }
2651         return (0);
2652 }
2653
2654 static int
2655 lpfc_els_rcv_rrq(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2656                  struct lpfc_nodelist * ndlp)
2657 {
2658         struct lpfc_dmabuf *pcmd;
2659         uint32_t *lp;
2660         IOCB_t *icmd;
2661         struct lpfc_sli_ring *pring;
2662         struct lpfc_sli *psli;
2663         RRQ *rrq;
2664         uint32_t cmd, did;
2665
2666         psli = &phba->sli;
2667         pring = &psli->ring[LPFC_FCP_RING];
2668         icmd = &cmdiocb->iocb;
2669         did = icmd->un.elsreq64.remoteID;
2670         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2671         lp = (uint32_t *) pcmd->virt;
2672
2673         cmd = *lp++;
2674         rrq = (RRQ *) lp;
2675
2676         /* RRQ received */
2677         /* Get oxid / rxid from payload and abort it */
2678         spin_lock_irq(phba->host->host_lock);
2679         if ((rrq->SID == be32_to_cpu(phba->fc_myDID))) {
2680                 lpfc_sli_abort_iocb(phba, pring, 0, 0, rrq->Oxid,
2681                                                         LPFC_CTX_CTX);
2682         } else {
2683                 lpfc_sli_abort_iocb(phba, pring, 0, 0, rrq->Rxid,
2684                                                         LPFC_CTX_CTX);
2685         }
2686
2687         spin_unlock_irq(phba->host->host_lock);
2688         /* ACCEPT the rrq request */
2689         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
2690
2691         return 0;
2692 }
2693
2694 static int
2695 lpfc_els_rcv_farp(struct lpfc_hba * phba,
2696                   struct lpfc_iocbq * cmdiocb, struct lpfc_nodelist * ndlp)
2697 {
2698         struct lpfc_dmabuf *pcmd;
2699         uint32_t *lp;
2700         IOCB_t *icmd;
2701         FARP *fp;
2702         uint32_t cmd, cnt, did;
2703
2704         icmd = &cmdiocb->iocb;
2705         did = icmd->un.elsreq64.remoteID;
2706         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2707         lp = (uint32_t *) pcmd->virt;
2708
2709         cmd = *lp++;
2710         fp = (FARP *) lp;
2711
2712         /* FARP-REQ received from DID <did> */
2713         lpfc_printf_log(phba,
2714                          KERN_INFO,
2715                          LOG_IP,
2716                          "%d:0601 FARP-REQ received from DID x%x\n",
2717                          phba->brd_no, did);
2718
2719         /* We will only support match on WWPN or WWNN */
2720         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
2721                 return (0);
2722         }
2723
2724         cnt = 0;
2725         /* If this FARP command is searching for my portname */
2726         if (fp->Mflags & FARP_MATCH_PORT) {
2727                 if (memcmp(&fp->RportName, &phba->fc_portname,
2728                            sizeof (struct lpfc_name)) == 0)
2729                         cnt = 1;
2730         }
2731
2732         /* If this FARP command is searching for my nodename */
2733         if (fp->Mflags & FARP_MATCH_NODE) {
2734                 if (memcmp(&fp->RnodeName, &phba->fc_nodename,
2735                            sizeof (struct lpfc_name)) == 0)
2736                         cnt = 1;
2737         }
2738
2739         if (cnt) {
2740                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
2741                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
2742                         /* Log back into the node before sending the FARP. */
2743                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
2744                                 ndlp->nlp_state = NLP_STE_PLOGI_ISSUE;
2745                                 lpfc_nlp_list(phba, ndlp, NLP_PLOGI_LIST);
2746                                 lpfc_issue_els_plogi(phba, ndlp, 0);
2747                         }
2748
2749                         /* Send a FARP response to that node */
2750                         if (fp->Rflags & FARP_REQUEST_FARPR) {
2751                                 lpfc_issue_els_farpr(phba, did, 0);
2752                         }
2753                 }
2754         }
2755         return (0);
2756 }
2757
2758 static int
2759 lpfc_els_rcv_farpr(struct lpfc_hba * phba,
2760                    struct lpfc_iocbq * cmdiocb, struct lpfc_nodelist * ndlp)
2761 {
2762         struct lpfc_dmabuf *pcmd;
2763         uint32_t *lp;
2764         IOCB_t *icmd;
2765         uint32_t cmd, did;
2766
2767         icmd = &cmdiocb->iocb;
2768         did = icmd->un.elsreq64.remoteID;
2769         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2770         lp = (uint32_t *) pcmd->virt;
2771
2772         cmd = *lp++;
2773         /* FARP-RSP received from DID <did> */
2774         lpfc_printf_log(phba,
2775                          KERN_INFO,
2776                          LOG_IP,
2777                          "%d:0600 FARP-RSP received from DID x%x\n",
2778                          phba->brd_no, did);
2779
2780         /* ACCEPT the Farp resp request */
2781         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
2782
2783         return 0;
2784 }
2785
2786 static int
2787 lpfc_els_rcv_fan(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
2788                  struct lpfc_nodelist * ndlp)
2789 {
2790         struct lpfc_dmabuf *pcmd;
2791         uint32_t *lp;
2792         IOCB_t *icmd;
2793         FAN *fp;
2794         uint32_t cmd, did;
2795
2796         icmd = &cmdiocb->iocb;
2797         did = icmd->un.elsreq64.remoteID;
2798         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2799         lp = (uint32_t *) pcmd->virt;
2800
2801         cmd = *lp++;
2802         fp = (FAN *) lp;
2803
2804         /* FAN received */
2805
2806         /* ACCEPT the FAN request */
2807         lpfc_els_rsp_acc(phba, ELS_CMD_ACC, cmdiocb, ndlp, NULL, 0);
2808
2809         if (phba->hba_state == LPFC_LOCAL_CFG_LINK) {
2810                 /* The discovery state machine needs to take a different
2811                  * action if this node has switched fabrics
2812                  */
2813                 if ((memcmp(&fp->FportName, &phba->fc_fabparam.portName,
2814                             sizeof (struct lpfc_name)) != 0)
2815                     ||
2816                     (memcmp(&fp->FnodeName, &phba->fc_fabparam.nodeName,
2817                             sizeof (struct lpfc_name)) != 0)) {
2818                         /* This node has switched fabrics.  An FLOGI is required
2819                          * after the timeout
2820                          */
2821                         return (0);
2822                 }
2823
2824                 /* Start discovery */
2825                 lpfc_disc_start(phba);
2826         }
2827
2828         return (0);
2829 }
2830
2831 void
2832 lpfc_els_timeout(unsigned long ptr)
2833 {
2834         struct lpfc_hba *phba;
2835         unsigned long iflag;
2836
2837         phba = (struct lpfc_hba *)ptr;
2838         if (phba == 0)
2839                 return;
2840         spin_lock_irqsave(phba->host->host_lock, iflag);
2841         if (!(phba->work_hba_events & WORKER_ELS_TMO)) {
2842                 phba->work_hba_events |= WORKER_ELS_TMO;
2843                 if (phba->work_wait)
2844                         wake_up(phba->work_wait);
2845         }
2846         spin_unlock_irqrestore(phba->host->host_lock, iflag);
2847         return;
2848 }
2849
2850 void
2851 lpfc_els_timeout_handler(struct lpfc_hba *phba)
2852 {
2853         struct lpfc_sli_ring *pring;
2854         struct lpfc_iocbq *tmp_iocb, *piocb;
2855         IOCB_t *cmd = NULL;
2856         struct lpfc_dmabuf *pcmd;
2857         struct list_head *dlp;
2858         uint32_t *elscmd;
2859         uint32_t els_command;
2860         uint32_t timeout;
2861         uint32_t remote_ID;
2862
2863         if (phba == 0)
2864                 return;
2865         spin_lock_irq(phba->host->host_lock);
2866         /* If the timer is already canceled do nothing */
2867         if (!(phba->work_hba_events & WORKER_ELS_TMO)) {
2868                 spin_unlock_irq(phba->host->host_lock);
2869                 return;
2870         }
2871         timeout = (uint32_t)(phba->fc_ratov << 1);
2872
2873         pring = &phba->sli.ring[LPFC_ELS_RING];
2874         dlp = &pring->txcmplq;
2875
2876         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
2877                 cmd = &piocb->iocb;
2878
2879                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
2880                         continue;
2881                 }
2882                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
2883                 elscmd = (uint32_t *) (pcmd->virt);
2884                 els_command = *elscmd;
2885
2886                 if ((els_command == ELS_CMD_FARP)
2887                     || (els_command == ELS_CMD_FARPR)) {
2888                         continue;
2889                 }
2890
2891                 if (piocb->drvrTimeout > 0) {
2892                         if (piocb->drvrTimeout >= timeout) {
2893                                 piocb->drvrTimeout -= timeout;
2894                         } else {
2895                                 piocb->drvrTimeout = 0;
2896                         }
2897                         continue;
2898                 }
2899
2900                 list_del(&piocb->list);
2901                 pring->txcmplq_cnt--;
2902
2903                 if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR) {
2904                         struct lpfc_nodelist *ndlp;
2905
2906                         ndlp = lpfc_findnode_rpi(phba, cmd->ulpContext);
2907                         remote_ID = ndlp->nlp_DID;
2908                         if (cmd->un.elsreq64.bdl.ulpIoTag32) {
2909                                 lpfc_sli_issue_abort_iotag32(phba,
2910                                         pring, piocb);
2911                         }
2912                 } else {
2913                         remote_ID = cmd->un.elsreq64.remoteID;
2914                 }
2915
2916                 lpfc_printf_log(phba,
2917                                 KERN_ERR,
2918                                 LOG_ELS,
2919                                 "%d:0127 ELS timeout Data: x%x x%x x%x x%x\n",
2920                                 phba->brd_no, els_command,
2921                                 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
2922
2923                 /*
2924                  * The iocb has timed out; abort it.
2925                  */
2926                 if (piocb->iocb_cmpl) {
2927                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2928                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2929                         spin_unlock_irq(phba->host->host_lock);
2930                         (piocb->iocb_cmpl) (phba, piocb, piocb);
2931                         spin_lock_irq(phba->host->host_lock);
2932                 } else {
2933                         list_add_tail(&piocb->list, &phba->lpfc_iocb_list);
2934                 }
2935         }
2936         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt) {
2937                 phba->els_tmofunc.expires = jiffies + HZ * timeout;
2938                 add_timer(&phba->els_tmofunc);
2939         }
2940         spin_unlock_irq(phba->host->host_lock);
2941 }
2942
2943 void
2944 lpfc_els_flush_cmd(struct lpfc_hba * phba)
2945 {
2946         struct lpfc_sli_ring *pring;
2947         struct lpfc_iocbq *tmp_iocb, *piocb;
2948         IOCB_t *cmd = NULL;
2949         struct lpfc_dmabuf *pcmd;
2950         uint32_t *elscmd;
2951         uint32_t els_command;
2952         uint32_t remote_ID;
2953
2954         pring = &phba->sli.ring[LPFC_ELS_RING];
2955         spin_lock_irq(phba->host->host_lock);
2956         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
2957                 cmd = &piocb->iocb;
2958
2959                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
2960                         continue;
2961                 }
2962
2963                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
2964                 if ((cmd->ulpCommand == CMD_QUE_RING_BUF_CN) ||
2965                     (cmd->ulpCommand == CMD_QUE_RING_BUF64_CN) ||
2966                     (cmd->ulpCommand == CMD_CLOSE_XRI_CN) ||
2967                     (cmd->ulpCommand == CMD_ABORT_XRI_CN)) {
2968                         continue;
2969                 }
2970
2971                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
2972                 elscmd = (uint32_t *) (pcmd->virt);
2973                 els_command = *elscmd;
2974
2975                 if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR) {
2976                         struct lpfc_nodelist *ndlp;
2977
2978                         ndlp = lpfc_findnode_rpi(phba, cmd->ulpContext);
2979                         remote_ID = ndlp->nlp_DID;
2980                         if (phba->hba_state == LPFC_HBA_READY) {
2981                                 continue;
2982                         }
2983                 } else {
2984                         remote_ID = cmd->un.elsreq64.remoteID;
2985                 }
2986
2987                 list_del(&piocb->list);
2988                 pring->txcmplq_cnt--;
2989
2990                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2991                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2992
2993                 if (piocb->iocb_cmpl) {
2994                         spin_unlock_irq(phba->host->host_lock);
2995                         (piocb->iocb_cmpl) (phba, piocb, piocb);
2996                         spin_lock_irq(phba->host->host_lock);
2997                 }
2998                 else
2999                         list_add_tail(&piocb->list, &phba->lpfc_iocb_list);
3000         }
3001
3002         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
3003                 cmd = &piocb->iocb;
3004
3005                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
3006                         continue;
3007                 }
3008                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
3009                 elscmd = (uint32_t *) (pcmd->virt);
3010                 els_command = *elscmd;
3011
3012                 if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR) {
3013                         struct lpfc_nodelist *ndlp;
3014
3015                         ndlp = lpfc_findnode_rpi(phba, cmd->ulpContext);
3016                         remote_ID = ndlp->nlp_DID;
3017                         if (phba->hba_state == LPFC_HBA_READY) {
3018                                 continue;
3019                         }
3020                 } else {
3021                         remote_ID = cmd->un.elsreq64.remoteID;
3022                 }
3023
3024                 list_del(&piocb->list);
3025                 pring->txcmplq_cnt--;
3026
3027                 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3028                 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
3029
3030                 if (piocb->iocb_cmpl) {
3031                         spin_unlock_irq(phba->host->host_lock);
3032                         (piocb->iocb_cmpl) (phba, piocb, piocb);
3033                         spin_lock_irq(phba->host->host_lock);
3034                 }
3035                 else
3036                         list_add_tail(&piocb->list, &phba->lpfc_iocb_list);
3037         }
3038         spin_unlock_irq(phba->host->host_lock);
3039         return;
3040 }
3041
3042 void
3043 lpfc_els_unsol_event(struct lpfc_hba * phba,
3044                      struct lpfc_sli_ring * pring, struct lpfc_iocbq * elsiocb)
3045 {
3046         struct lpfc_sli *psli;
3047         struct lpfc_nodelist *ndlp;
3048         struct lpfc_dmabuf *mp;
3049         uint32_t *lp;
3050         IOCB_t *icmd;
3051         struct ls_rjt stat;
3052         uint32_t cmd;
3053         uint32_t did;
3054         uint32_t newnode;
3055         uint32_t drop_cmd = 0;  /* by default do NOT drop received cmd */
3056         uint32_t rjt_err = 0;
3057
3058         psli = &phba->sli;
3059         icmd = &elsiocb->iocb;
3060
3061         if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3062                 ((icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING)) {
3063                 /* Not enough posted buffers; Try posting more buffers */
3064                 phba->fc_stat.NoRcvBuf++;
3065                 lpfc_post_buffer(phba, pring, 0, 1);
3066                 return;
3067         }
3068
3069         /* If there are no BDEs associated with this IOCB,
3070          * there is nothing to do.
3071          */
3072         if (icmd->ulpBdeCount == 0)
3073                 return;
3074
3075         /* type of ELS cmd is first 32bit word in packet */
3076         mp = lpfc_sli_ringpostbuf_get(phba, pring, getPaddr(icmd->un.
3077                                                             cont64[0].
3078                                                             addrHigh,
3079                                                             icmd->un.
3080                                                             cont64[0].addrLow));
3081         if (mp == 0) {
3082                 drop_cmd = 1;
3083                 goto dropit;
3084         }
3085
3086         newnode = 0;
3087         lp = (uint32_t *) mp->virt;
3088         cmd = *lp++;
3089         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], 1, 1);
3090
3091         if (icmd->ulpStatus) {
3092                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3093                 kfree(mp);
3094                 drop_cmd = 1;
3095                 goto dropit;
3096         }
3097
3098         /* Check to see if link went down during discovery */
3099         if (lpfc_els_chk_latt(phba)) {
3100                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3101                 kfree(mp);
3102                 drop_cmd = 1;
3103                 goto dropit;
3104         }
3105
3106         did = icmd->un.rcvels.remoteID;
3107         if ((ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, did)) == 0) {
3108                 /* Cannot find existing Fabric ndlp, so allocate a new one */
3109                 if ((ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL))
3110                     == 0) {
3111                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3112                         kfree(mp);
3113                         drop_cmd = 1;
3114                         goto dropit;
3115                 }
3116
3117                 lpfc_nlp_init(phba, ndlp, did);
3118                 newnode = 1;
3119                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK) {
3120                         ndlp->nlp_type |= NLP_FABRIC;
3121                 }
3122         }
3123
3124         phba->fc_stat.elsRcvFrame++;
3125         elsiocb->context1 = ndlp;
3126         elsiocb->context2 = mp;
3127
3128         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
3129                 cmd &= ELS_CMD_MASK;
3130         }
3131         /* ELS command <elsCmd> received from NPORT <did> */
3132         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
3133                         "%d:0112 ELS command x%x received from NPORT x%x "
3134                         "Data: x%x\n", phba->brd_no, cmd, did, phba->hba_state);
3135
3136         switch (cmd) {
3137         case ELS_CMD_PLOGI:
3138                 phba->fc_stat.elsRcvPLOGI++;
3139                 if (phba->hba_state < LPFC_DISC_AUTH) {
3140                         rjt_err = 1;
3141                         break;
3142                 }
3143                 lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PLOGI);
3144                 break;
3145         case ELS_CMD_FLOGI:
3146                 phba->fc_stat.elsRcvFLOGI++;
3147                 lpfc_els_rcv_flogi(phba, elsiocb, ndlp, newnode);
3148                 if (newnode) {
3149                         mempool_free( ndlp, phba->nlp_mem_pool);
3150                 }
3151                 break;
3152         case ELS_CMD_LOGO:
3153                 phba->fc_stat.elsRcvLOGO++;
3154                 if (phba->hba_state < LPFC_DISC_AUTH) {
3155                         rjt_err = 1;
3156                         break;
3157                 }
3158                 lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
3159                 break;
3160         case ELS_CMD_PRLO:
3161                 phba->fc_stat.elsRcvPRLO++;
3162                 if (phba->hba_state < LPFC_DISC_AUTH) {
3163                         rjt_err = 1;
3164                         break;
3165                 }
3166                 lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
3167                 break;
3168         case ELS_CMD_RSCN:
3169                 phba->fc_stat.elsRcvRSCN++;
3170                 lpfc_els_rcv_rscn(phba, elsiocb, ndlp, newnode);
3171                 if (newnode) {
3172                         mempool_free( ndlp, phba->nlp_mem_pool);
3173                 }
3174                 break;
3175         case ELS_CMD_ADISC:
3176                 phba->fc_stat.elsRcvADISC++;
3177                 if (phba->hba_state < LPFC_DISC_AUTH) {
3178                         rjt_err = 1;
3179                         break;
3180                 }
3181                 lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_ADISC);
3182                 break;
3183         case ELS_CMD_PDISC:
3184                 phba->fc_stat.elsRcvPDISC++;
3185                 if (phba->hba_state < LPFC_DISC_AUTH) {
3186                         rjt_err = 1;
3187                         break;
3188                 }
3189                 lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PDISC);
3190                 break;
3191         case ELS_CMD_FARPR:
3192                 phba->fc_stat.elsRcvFARPR++;
3193                 lpfc_els_rcv_farpr(phba, elsiocb, ndlp);
3194                 break;
3195         case ELS_CMD_FARP:
3196                 phba->fc_stat.elsRcvFARP++;
3197                 lpfc_els_rcv_farp(phba, elsiocb, ndlp);
3198                 break;
3199         case ELS_CMD_FAN:
3200                 phba->fc_stat.elsRcvFAN++;
3201                 lpfc_els_rcv_fan(phba, elsiocb, ndlp);
3202                 break;
3203         case ELS_CMD_RRQ:
3204                 phba->fc_stat.elsRcvRRQ++;
3205                 lpfc_els_rcv_rrq(phba, elsiocb, ndlp);
3206                 break;
3207         case ELS_CMD_PRLI:
3208                 phba->fc_stat.elsRcvPRLI++;
3209                 if (phba->hba_state < LPFC_DISC_AUTH) {
3210                         rjt_err = 1;
3211                         break;
3212                 }
3213                 lpfc_disc_state_machine(phba, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
3214                 break;
3215         case ELS_CMD_RNID:
3216                 phba->fc_stat.elsRcvRNID++;
3217                 lpfc_els_rcv_rnid(phba, elsiocb, ndlp);
3218                 break;
3219         default:
3220                 /* Unsupported ELS command, reject */
3221                 rjt_err = 1;
3222
3223                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
3224                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
3225                                 "%d:0115 Unknown ELS command x%x received from "
3226                                 "NPORT x%x\n", phba->brd_no, cmd, did);
3227                 if (newnode) {
3228                         mempool_free( ndlp, phba->nlp_mem_pool);
3229                 }
3230                 break;
3231         }
3232
3233         /* check if need to LS_RJT received ELS cmd */
3234         if (rjt_err) {
3235                 stat.un.b.lsRjtRsvd0 = 0;
3236                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
3237                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
3238                 stat.un.b.vendorUnique = 0;
3239                 lpfc_els_rsp_reject(phba, stat.un.lsRjtError, elsiocb, ndlp);
3240         }
3241
3242         if (elsiocb->context2) {
3243                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3244                 kfree(mp);
3245         }
3246 dropit:
3247         /* check if need to drop received ELS cmd */
3248         if (drop_cmd == 1) {
3249                 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
3250                                 "%d:0111 Dropping received ELS cmd "
3251                                 "Data: x%x x%x\n", phba->brd_no,
3252                                 icmd->ulpStatus, icmd->un.ulpWord[4]);
3253                 phba->fc_stat.elsRcvDrop++;
3254         }
3255         return;
3256 }