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