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