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