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