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