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