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