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