[SCSI] lpfc 8.3.18: Add support of received ELS commands
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_els.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 /* See Fibre Channel protocol T11 FC-LS for details */
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_sli4.h"
36 #include "lpfc_nl.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
46                           struct lpfc_iocbq *);
47 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
48                         struct lpfc_iocbq *);
49 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
50 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
51                                 struct lpfc_nodelist *ndlp, uint8_t retry);
52 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
53                                   struct lpfc_iocbq *iocb);
54
55 static int lpfc_max_els_tries = 3;
56
57 /**
58  * lpfc_els_chk_latt - Check host link attention event for a vport
59  * @vport: pointer to a host virtual N_Port data structure.
60  *
61  * This routine checks whether there is an outstanding host link
62  * attention event during the discovery process with the @vport. It is done
63  * by reading the HBA's Host Attention (HA) register. If there is any host
64  * link attention events during this @vport's discovery process, the @vport
65  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
66  * be issued if the link state is not already in host link cleared state,
67  * and a return code shall indicate whether the host link attention event
68  * had happened.
69  *
70  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
71  * state in LPFC_VPORT_READY, the request for checking host link attention
72  * event will be ignored and a return code shall indicate no host link
73  * attention event had happened.
74  *
75  * Return codes
76  *   0 - no host link attention event happened
77  *   1 - host link attention event happened
78  **/
79 int
80 lpfc_els_chk_latt(struct lpfc_vport *vport)
81 {
82         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
83         struct lpfc_hba  *phba = vport->phba;
84         uint32_t ha_copy;
85
86         if (vport->port_state >= LPFC_VPORT_READY ||
87             phba->link_state == LPFC_LINK_DOWN ||
88             phba->sli_rev > LPFC_SLI_REV3)
89                 return 0;
90
91         /* Read the HBA Host Attention Register */
92         ha_copy = readl(phba->HAregaddr);
93
94         if (!(ha_copy & HA_LATT))
95                 return 0;
96
97         /* Pending Link Event during Discovery */
98         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
99                          "0237 Pending Link Event during "
100                          "Discovery: State x%x\n",
101                          phba->pport->port_state);
102
103         /* CLEAR_LA should re-enable link attention events and
104          * we should then imediately take a LATT event. The
105          * LATT processing should call lpfc_linkdown() which
106          * will cleanup any left over in-progress discovery
107          * events.
108          */
109         spin_lock_irq(shost->host_lock);
110         vport->fc_flag |= FC_ABORT_DISCOVERY;
111         spin_unlock_irq(shost->host_lock);
112
113         if (phba->link_state != LPFC_CLEAR_LA)
114                 lpfc_issue_clear_la(phba, vport);
115
116         return 1;
117 }
118
119 /**
120  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
121  * @vport: pointer to a host virtual N_Port data structure.
122  * @expectRsp: flag indicating whether response is expected.
123  * @cmdSize: size of the ELS command.
124  * @retry: number of retries to the command IOCB when it fails.
125  * @ndlp: pointer to a node-list data structure.
126  * @did: destination identifier.
127  * @elscmd: the ELS command code.
128  *
129  * This routine is used for allocating a lpfc-IOCB data structure from
130  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
131  * passed into the routine for discovery state machine to issue an Extended
132  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
133  * and preparation routine that is used by all the discovery state machine
134  * routines and the ELS command-specific fields will be later set up by
135  * the individual discovery machine routines after calling this routine
136  * allocating and preparing a generic IOCB data structure. It fills in the
137  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
138  * payload and response payload (if expected). The reference count on the
139  * ndlp is incremented by 1 and the reference to the ndlp is put into
140  * context1 of the IOCB data structure for this IOCB to hold the ndlp
141  * reference for the command's callback function to access later.
142  *
143  * Return code
144  *   Pointer to the newly allocated/prepared els iocb data structure
145  *   NULL - when els iocb data structure allocation/preparation failed
146  **/
147 struct lpfc_iocbq *
148 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
149                    uint16_t cmdSize, uint8_t retry,
150                    struct lpfc_nodelist *ndlp, uint32_t did,
151                    uint32_t elscmd)
152 {
153         struct lpfc_hba  *phba = vport->phba;
154         struct lpfc_iocbq *elsiocb;
155         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
156         struct ulp_bde64 *bpl;
157         IOCB_t *icmd;
158
159
160         if (!lpfc_is_link_up(phba))
161                 return NULL;
162
163         /* Allocate buffer for  command iocb */
164         elsiocb = lpfc_sli_get_iocbq(phba);
165
166         if (elsiocb == NULL)
167                 return NULL;
168
169         /*
170          * If this command is for fabric controller and HBA running
171          * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
172          */
173         if ((did == Fabric_DID) &&
174                 (phba->hba_flag & HBA_FIP_SUPPORT) &&
175                 ((elscmd == ELS_CMD_FLOGI) ||
176                  (elscmd == ELS_CMD_FDISC) ||
177                  (elscmd == ELS_CMD_LOGO)))
178                 switch (elscmd) {
179                 case ELS_CMD_FLOGI:
180                 elsiocb->iocb_flag |= ((ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
181                                         & LPFC_FIP_ELS_ID_MASK);
182                 break;
183                 case ELS_CMD_FDISC:
184                 elsiocb->iocb_flag |= ((ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
185                                         & LPFC_FIP_ELS_ID_MASK);
186                 break;
187                 case ELS_CMD_LOGO:
188                 elsiocb->iocb_flag |= ((ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
189                                         & LPFC_FIP_ELS_ID_MASK);
190                 break;
191                 }
192         else
193                 elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
194
195         icmd = &elsiocb->iocb;
196
197         /* fill in BDEs for command */
198         /* Allocate buffer for command payload */
199         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
200         if (pcmd)
201                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
202         if (!pcmd || !pcmd->virt)
203                 goto els_iocb_free_pcmb_exit;
204
205         INIT_LIST_HEAD(&pcmd->list);
206
207         /* Allocate buffer for response payload */
208         if (expectRsp) {
209                 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
210                 if (prsp)
211                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
212                                                      &prsp->phys);
213                 if (!prsp || !prsp->virt)
214                         goto els_iocb_free_prsp_exit;
215                 INIT_LIST_HEAD(&prsp->list);
216         } else
217                 prsp = NULL;
218
219         /* Allocate buffer for Buffer ptr list */
220         pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
221         if (pbuflist)
222                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
223                                                  &pbuflist->phys);
224         if (!pbuflist || !pbuflist->virt)
225                 goto els_iocb_free_pbuf_exit;
226
227         INIT_LIST_HEAD(&pbuflist->list);
228
229         icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
230         icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
231         icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
232         icmd->un.elsreq64.remoteID = did;       /* DID */
233         if (expectRsp) {
234                 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
235                 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
236                 icmd->ulpTimeout = phba->fc_ratov * 2;
237         } else {
238                 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
239                 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
240         }
241         icmd->ulpBdeCount = 1;
242         icmd->ulpLe = 1;
243         icmd->ulpClass = CLASS3;
244
245         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
246                 icmd->un.elsreq64.myID = vport->fc_myDID;
247
248                 /* For ELS_REQUEST64_CR, use the VPI by default */
249                 icmd->ulpContext = vport->vpi + phba->vpi_base;
250                 icmd->ulpCt_h = 0;
251                 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
252                 if (elscmd == ELS_CMD_ECHO)
253                         icmd->ulpCt_l = 0; /* context = invalid RPI */
254                 else
255                         icmd->ulpCt_l = 1; /* context = VPI */
256         }
257
258         bpl = (struct ulp_bde64 *) pbuflist->virt;
259         bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
260         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
261         bpl->tus.f.bdeSize = cmdSize;
262         bpl->tus.f.bdeFlags = 0;
263         bpl->tus.w = le32_to_cpu(bpl->tus.w);
264
265         if (expectRsp) {
266                 bpl++;
267                 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
268                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
269                 bpl->tus.f.bdeSize = FCELSSIZE;
270                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
271                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
272         }
273
274         /* prevent preparing iocb with NULL ndlp reference */
275         elsiocb->context1 = lpfc_nlp_get(ndlp);
276         if (!elsiocb->context1)
277                 goto els_iocb_free_pbuf_exit;
278         elsiocb->context2 = pcmd;
279         elsiocb->context3 = pbuflist;
280         elsiocb->retry = retry;
281         elsiocb->vport = vport;
282         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
283
284         if (prsp) {
285                 list_add(&prsp->list, &pcmd->list);
286         }
287         if (expectRsp) {
288                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
289                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
290                                  "0116 Xmit ELS command x%x to remote "
291                                  "NPORT x%x I/O tag: x%x, port state: x%x\n",
292                                  elscmd, did, elsiocb->iotag,
293                                  vport->port_state);
294         } else {
295                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
296                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
297                                  "0117 Xmit ELS response x%x to remote "
298                                  "NPORT x%x I/O tag: x%x, size: x%x\n",
299                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
300                                  cmdSize);
301         }
302         return elsiocb;
303
304 els_iocb_free_pbuf_exit:
305         if (expectRsp)
306                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
307         kfree(pbuflist);
308
309 els_iocb_free_prsp_exit:
310         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
311         kfree(prsp);
312
313 els_iocb_free_pcmb_exit:
314         kfree(pcmd);
315         lpfc_sli_release_iocbq(phba, elsiocb);
316         return NULL;
317 }
318
319 /**
320  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
321  * @vport: pointer to a host virtual N_Port data structure.
322  *
323  * This routine issues a fabric registration login for a @vport. An
324  * active ndlp node with Fabric_DID must already exist for this @vport.
325  * The routine invokes two mailbox commands to carry out fabric registration
326  * login through the HBA firmware: the first mailbox command requests the
327  * HBA to perform link configuration for the @vport; and the second mailbox
328  * command requests the HBA to perform the actual fabric registration login
329  * with the @vport.
330  *
331  * Return code
332  *   0 - successfully issued fabric registration login for @vport
333  *   -ENXIO -- failed to issue fabric registration login for @vport
334  **/
335 int
336 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
337 {
338         struct lpfc_hba  *phba = vport->phba;
339         LPFC_MBOXQ_t *mbox;
340         struct lpfc_dmabuf *mp;
341         struct lpfc_nodelist *ndlp;
342         struct serv_parm *sp;
343         int rc;
344         int err = 0;
345
346         sp = &phba->fc_fabparam;
347         ndlp = lpfc_findnode_did(vport, Fabric_DID);
348         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
349                 err = 1;
350                 goto fail;
351         }
352
353         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
354         if (!mbox) {
355                 err = 2;
356                 goto fail;
357         }
358
359         vport->port_state = LPFC_FABRIC_CFG_LINK;
360         lpfc_config_link(phba, mbox);
361         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
362         mbox->vport = vport;
363
364         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
365         if (rc == MBX_NOT_FINISHED) {
366                 err = 3;
367                 goto fail_free_mbox;
368         }
369
370         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
371         if (!mbox) {
372                 err = 4;
373                 goto fail;
374         }
375         rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox, 0);
376         if (rc) {
377                 err = 5;
378                 goto fail_free_mbox;
379         }
380
381         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
382         mbox->vport = vport;
383         /* increment the reference count on ndlp to hold reference
384          * for the callback routine.
385          */
386         mbox->context2 = lpfc_nlp_get(ndlp);
387
388         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
389         if (rc == MBX_NOT_FINISHED) {
390                 err = 6;
391                 goto fail_issue_reg_login;
392         }
393
394         return 0;
395
396 fail_issue_reg_login:
397         /* decrement the reference count on ndlp just incremented
398          * for the failed mbox command.
399          */
400         lpfc_nlp_put(ndlp);
401         mp = (struct lpfc_dmabuf *) mbox->context1;
402         lpfc_mbuf_free(phba, mp->virt, mp->phys);
403         kfree(mp);
404 fail_free_mbox:
405         mempool_free(mbox, phba->mbox_mem_pool);
406
407 fail:
408         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
409         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
410                 "0249 Cannot issue Register Fabric login: Err %d\n", err);
411         return -ENXIO;
412 }
413
414 /**
415  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
416  * @vport: pointer to a host virtual N_Port data structure.
417  *
418  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
419  * the @vport. This mailbox command is necessary for FCoE only.
420  *
421  * Return code
422  *   0 - successfully issued REG_VFI for @vport
423  *   A failure code otherwise.
424  **/
425 static int
426 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
427 {
428         struct lpfc_hba  *phba = vport->phba;
429         LPFC_MBOXQ_t *mboxq;
430         struct lpfc_nodelist *ndlp;
431         struct serv_parm *sp;
432         struct lpfc_dmabuf *dmabuf;
433         int rc = 0;
434
435         sp = &phba->fc_fabparam;
436         ndlp = lpfc_findnode_did(vport, Fabric_DID);
437         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
438                 rc = -ENODEV;
439                 goto fail;
440         }
441
442         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
443         if (!dmabuf) {
444                 rc = -ENOMEM;
445                 goto fail;
446         }
447         dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
448         if (!dmabuf->virt) {
449                 rc = -ENOMEM;
450                 goto fail_free_dmabuf;
451         }
452         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
453         if (!mboxq) {
454                 rc = -ENOMEM;
455                 goto fail_free_coherent;
456         }
457         vport->port_state = LPFC_FABRIC_CFG_LINK;
458         memcpy(dmabuf->virt, &phba->fc_fabparam, sizeof(vport->fc_sparam));
459         lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
460         mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
461         mboxq->vport = vport;
462         mboxq->context1 = dmabuf;
463         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
464         if (rc == MBX_NOT_FINISHED) {
465                 rc = -ENXIO;
466                 goto fail_free_mbox;
467         }
468         return 0;
469
470 fail_free_mbox:
471         mempool_free(mboxq, phba->mbox_mem_pool);
472 fail_free_coherent:
473         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
474 fail_free_dmabuf:
475         kfree(dmabuf);
476 fail:
477         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
478         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
479                 "0289 Issue Register VFI failed: Err %d\n", rc);
480         return rc;
481 }
482
483 /**
484  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
485  * @vport: pointer to a host virtual N_Port data structure.
486  * @ndlp: pointer to a node-list data structure.
487  * @sp: pointer to service parameter data structure.
488  * @irsp: pointer to the IOCB within the lpfc response IOCB.
489  *
490  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
491  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
492  * port in a fabric topology. It properly sets up the parameters to the @ndlp
493  * from the IOCB response. It also check the newly assigned N_Port ID to the
494  * @vport against the previously assigned N_Port ID. If it is different from
495  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
496  * is invoked on all the remaining nodes with the @vport to unregister the
497  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
498  * is invoked to register login to the fabric.
499  *
500  * Return code
501  *   0 - Success (currently, always return 0)
502  **/
503 static int
504 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
505                            struct serv_parm *sp, IOCB_t *irsp)
506 {
507         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
508         struct lpfc_hba  *phba = vport->phba;
509         struct lpfc_nodelist *np;
510         struct lpfc_nodelist *next_np;
511
512         spin_lock_irq(shost->host_lock);
513         vport->fc_flag |= FC_FABRIC;
514         spin_unlock_irq(shost->host_lock);
515
516         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
517         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
518                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
519
520         phba->fc_edtovResol = sp->cmn.edtovResolution;
521         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
522
523         if (phba->fc_topology == TOPOLOGY_LOOP) {
524                 spin_lock_irq(shost->host_lock);
525                 vport->fc_flag |= FC_PUBLIC_LOOP;
526                 spin_unlock_irq(shost->host_lock);
527         }
528
529         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
530         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
531         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
532         ndlp->nlp_class_sup = 0;
533         if (sp->cls1.classValid)
534                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
535         if (sp->cls2.classValid)
536                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
537         if (sp->cls3.classValid)
538                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
539         if (sp->cls4.classValid)
540                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
541         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
542                                 sp->cmn.bbRcvSizeLsb;
543         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
544
545         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
546                 if (sp->cmn.response_multiple_NPort) {
547                         lpfc_printf_vlog(vport, KERN_WARNING,
548                                          LOG_ELS | LOG_VPORT,
549                                          "1816 FLOGI NPIV supported, "
550                                          "response data 0x%x\n",
551                                          sp->cmn.response_multiple_NPort);
552                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
553                 } else {
554                         /* Because we asked f/w for NPIV it still expects us
555                         to call reg_vnpid atleast for the physcial host */
556                         lpfc_printf_vlog(vport, KERN_WARNING,
557                                          LOG_ELS | LOG_VPORT,
558                                          "1817 Fabric does not support NPIV "
559                                          "- configuring single port mode.\n");
560                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
561                 }
562         }
563
564         if ((vport->fc_prevDID != vport->fc_myDID) &&
565                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
566
567                 /* If our NportID changed, we need to ensure all
568                  * remaining NPORTs get unreg_login'ed.
569                  */
570                 list_for_each_entry_safe(np, next_np,
571                                         &vport->fc_nodes, nlp_listp) {
572                         if (!NLP_CHK_NODE_ACT(np))
573                                 continue;
574                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
575                                    !(np->nlp_flag & NLP_NPR_ADISC))
576                                 continue;
577                         spin_lock_irq(shost->host_lock);
578                         np->nlp_flag &= ~NLP_NPR_ADISC;
579                         spin_unlock_irq(shost->host_lock);
580                         lpfc_unreg_rpi(vport, np);
581                 }
582                 lpfc_cleanup_pending_mbox(vport);
583                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
584                         lpfc_mbx_unreg_vpi(vport);
585                         spin_lock_irq(shost->host_lock);
586                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
587                         spin_unlock_irq(shost->host_lock);
588                 }
589                 /*
590                  * If VPI is unreged, driver need to do INIT_VPI
591                  * before re-registering
592                  */
593                 if (phba->sli_rev == LPFC_SLI_REV4) {
594                         spin_lock_irq(shost->host_lock);
595                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
596                         spin_unlock_irq(shost->host_lock);
597                 }
598         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
599                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
600                         /*
601                          * Driver needs to re-reg VPI in order for f/w
602                          * to update the MAC address.
603                          */
604                         lpfc_register_new_vport(phba, vport, ndlp);
605                         return 0;
606         }
607
608         if (phba->sli_rev < LPFC_SLI_REV4) {
609                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
610                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
611                     vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
612                         lpfc_register_new_vport(phba, vport, ndlp);
613                 else
614                         lpfc_issue_fabric_reglogin(vport);
615         } else {
616                 ndlp->nlp_type |= NLP_FABRIC;
617                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
618                 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
619                         (vport->vpi_state & LPFC_VPI_REGISTERED)) {
620                         lpfc_start_fdiscs(phba);
621                         lpfc_do_scr_ns_plogi(phba, vport);
622                 } else if (vport->fc_flag & FC_VFI_REGISTERED)
623                         lpfc_issue_init_vpi(vport);
624                 else
625                         lpfc_issue_reg_vfi(vport);
626         }
627         return 0;
628 }
629 /**
630  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
631  * @vport: pointer to a host virtual N_Port data structure.
632  * @ndlp: pointer to a node-list data structure.
633  * @sp: pointer to service parameter data structure.
634  *
635  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
636  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
637  * in a point-to-point topology. First, the @vport's N_Port Name is compared
638  * with the received N_Port Name: if the @vport's N_Port Name is greater than
639  * the received N_Port Name lexicographically, this node shall assign local
640  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
641  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
642  * this node shall just wait for the remote node to issue PLOGI and assign
643  * N_Port IDs.
644  *
645  * Return code
646  *   0 - Success
647  *   -ENXIO - Fail
648  **/
649 static int
650 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
651                           struct serv_parm *sp)
652 {
653         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
654         struct lpfc_hba  *phba = vport->phba;
655         LPFC_MBOXQ_t *mbox;
656         int rc;
657
658         spin_lock_irq(shost->host_lock);
659         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
660         spin_unlock_irq(shost->host_lock);
661
662         phba->fc_edtov = FF_DEF_EDTOV;
663         phba->fc_ratov = FF_DEF_RATOV;
664         rc = memcmp(&vport->fc_portname, &sp->portName,
665                     sizeof(vport->fc_portname));
666         if (rc >= 0) {
667                 /* This side will initiate the PLOGI */
668                 spin_lock_irq(shost->host_lock);
669                 vport->fc_flag |= FC_PT2PT_PLOGI;
670                 spin_unlock_irq(shost->host_lock);
671
672                 /*
673                  * N_Port ID cannot be 0, set our to LocalID the other
674                  * side will be RemoteID.
675                  */
676
677                 /* not equal */
678                 if (rc)
679                         vport->fc_myDID = PT2PT_LocalID;
680
681                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
682                 if (!mbox)
683                         goto fail;
684
685                 lpfc_config_link(phba, mbox);
686
687                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
688                 mbox->vport = vport;
689                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
690                 if (rc == MBX_NOT_FINISHED) {
691                         mempool_free(mbox, phba->mbox_mem_pool);
692                         goto fail;
693                 }
694                 /* Decrement ndlp reference count indicating that ndlp can be
695                  * safely released when other references to it are done.
696                  */
697                 lpfc_nlp_put(ndlp);
698
699                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
700                 if (!ndlp) {
701                         /*
702                          * Cannot find existing Fabric ndlp, so allocate a
703                          * new one
704                          */
705                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
706                         if (!ndlp)
707                                 goto fail;
708                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
709                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
710                         ndlp = lpfc_enable_node(vport, ndlp,
711                                                 NLP_STE_UNUSED_NODE);
712                         if(!ndlp)
713                                 goto fail;
714                 }
715
716                 memcpy(&ndlp->nlp_portname, &sp->portName,
717                        sizeof(struct lpfc_name));
718                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
719                        sizeof(struct lpfc_name));
720                 /* Set state will put ndlp onto node list if not already done */
721                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
722                 spin_lock_irq(shost->host_lock);
723                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
724                 spin_unlock_irq(shost->host_lock);
725         } else
726                 /* This side will wait for the PLOGI, decrement ndlp reference
727                  * count indicating that ndlp can be released when other
728                  * references to it are done.
729                  */
730                 lpfc_nlp_put(ndlp);
731
732         /* If we are pt2pt with another NPort, force NPIV off! */
733         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
734
735         spin_lock_irq(shost->host_lock);
736         vport->fc_flag |= FC_PT2PT;
737         spin_unlock_irq(shost->host_lock);
738
739         /* Start discovery - this should just do CLEAR_LA */
740         lpfc_disc_start(vport);
741         return 0;
742 fail:
743         return -ENXIO;
744 }
745
746 /**
747  * lpfc_cmpl_els_flogi - Completion callback function for flogi
748  * @phba: pointer to lpfc hba data structure.
749  * @cmdiocb: pointer to lpfc command iocb data structure.
750  * @rspiocb: pointer to lpfc response iocb data structure.
751  *
752  * This routine is the top-level completion callback function for issuing
753  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
754  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
755  * retry has been made (either immediately or delayed with lpfc_els_retry()
756  * returning 1), the command IOCB will be released and function returned.
757  * If the retry attempt has been given up (possibly reach the maximum
758  * number of retries), one additional decrement of ndlp reference shall be
759  * invoked before going out after releasing the command IOCB. This will
760  * actually release the remote node (Note, lpfc_els_free_iocb() will also
761  * invoke one decrement of ndlp reference count). If no error reported in
762  * the IOCB status, the command Port ID field is used to determine whether
763  * this is a point-to-point topology or a fabric topology: if the Port ID
764  * field is assigned, it is a fabric topology; otherwise, it is a
765  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
766  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
767  * specific topology completion conditions.
768  **/
769 static void
770 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
771                     struct lpfc_iocbq *rspiocb)
772 {
773         struct lpfc_vport *vport = cmdiocb->vport;
774         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
775         IOCB_t *irsp = &rspiocb->iocb;
776         struct lpfc_nodelist *ndlp = cmdiocb->context1;
777         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
778         struct serv_parm *sp;
779         uint16_t fcf_index;
780         int rc;
781
782         /* Check to see if link went down during discovery */
783         if (lpfc_els_chk_latt(vport)) {
784                 /* One additional decrement on node reference count to
785                  * trigger the release of the node
786                  */
787                 lpfc_nlp_put(ndlp);
788                 goto out;
789         }
790
791         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
792                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
793                 irsp->ulpStatus, irsp->un.ulpWord[4],
794                 vport->port_state);
795
796         if (irsp->ulpStatus) {
797                 /*
798                  * In case of FIP mode, perform round robin FCF failover
799                  * due to new FCF discovery
800                  */
801                 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
802                     (phba->fcf.fcf_flag & FCF_DISCOVERY) &&
803                     (irsp->ulpStatus != IOSTAT_LOCAL_REJECT) &&
804                     (irsp->un.ulpWord[4] != IOERR_SLI_ABORTED)) {
805                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
806                                         "2611 FLOGI failed on registered "
807                                         "FCF record fcf_index(%d), status: "
808                                         "x%x/x%x, tmo:x%x, trying to perform "
809                                         "round robin failover\n",
810                                         phba->fcf.current_rec.fcf_indx,
811                                         irsp->ulpStatus, irsp->un.ulpWord[4],
812                                         irsp->ulpTimeout);
813                         fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
814                         if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
815                                 /*
816                                  * Exhausted the eligible FCF record list,
817                                  * fail through to retry FLOGI on current
818                                  * FCF record.
819                                  */
820                                 lpfc_printf_log(phba, KERN_WARNING,
821                                                 LOG_FIP | LOG_ELS,
822                                                 "2760 Completed one round "
823                                                 "of FLOGI FCF round robin "
824                                                 "failover list, retry FLOGI "
825                                                 "on currently registered "
826                                                 "FCF index:%d\n",
827                                                 phba->fcf.current_rec.fcf_indx);
828                         } else {
829                                 lpfc_printf_log(phba, KERN_INFO,
830                                                 LOG_FIP | LOG_ELS,
831                                                 "2794 FLOGI FCF round robin "
832                                                 "failover to FCF index x%x\n",
833                                                 fcf_index);
834                                 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba,
835                                                                    fcf_index);
836                                 if (rc)
837                                         lpfc_printf_log(phba, KERN_WARNING,
838                                                         LOG_FIP | LOG_ELS,
839                                                         "2761 FLOGI round "
840                                                         "robin FCF failover "
841                                                         "read FCF failed "
842                                                         "rc:x%x, fcf_index:"
843                                                         "%d\n", rc,
844                                                 phba->fcf.current_rec.fcf_indx);
845                                 else
846                                         goto out;
847                         }
848                 }
849
850                 /* FLOGI failure */
851                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
852                                 "2858 FLOGI failure Status:x%x/x%x TMO:x%x\n",
853                                 irsp->ulpStatus, irsp->un.ulpWord[4],
854                                 irsp->ulpTimeout);
855
856                 /* Check for retry */
857                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
858                         goto out;
859
860                 /* FLOGI failed, so there is no fabric */
861                 spin_lock_irq(shost->host_lock);
862                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
863                 spin_unlock_irq(shost->host_lock);
864
865                 /* If private loop, then allow max outstanding els to be
866                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
867                  * alpa map would take too long otherwise.
868                  */
869                 if (phba->alpa_map[0] == 0) {
870                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
871                 }
872
873                 /* FLOGI failure */
874                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
875                                  "0100 FLOGI failure Status:x%x/x%x TMO:x%x\n",
876                                  irsp->ulpStatus, irsp->un.ulpWord[4],
877                                  irsp->ulpTimeout);
878                 goto flogifail;
879         }
880         spin_lock_irq(shost->host_lock);
881         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
882         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
883         spin_unlock_irq(shost->host_lock);
884
885         /*
886          * The FLogI succeeded.  Sync the data for the CPU before
887          * accessing it.
888          */
889         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
890
891         sp = prsp->virt + sizeof(uint32_t);
892
893         /* FLOGI completes successfully */
894         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
895                          "0101 FLOGI completes successfully "
896                          "Data: x%x x%x x%x x%x\n",
897                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
898                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
899
900         if (vport->port_state == LPFC_FLOGI) {
901                 /*
902                  * If Common Service Parameters indicate Nport
903                  * we are point to point, if Fport we are Fabric.
904                  */
905                 if (sp->cmn.fPort)
906                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
907                 else if (!(phba->hba_flag & HBA_FCOE_SUPPORT))
908                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
909                 else {
910                         lpfc_printf_vlog(vport, KERN_ERR,
911                                 LOG_FIP | LOG_ELS,
912                                 "2831 FLOGI response with cleared Fabric "
913                                 "bit fcf_index 0x%x "
914                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
915                                 "Fabric Name "
916                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
917                                 phba->fcf.current_rec.fcf_indx,
918                                 phba->fcf.current_rec.switch_name[0],
919                                 phba->fcf.current_rec.switch_name[1],
920                                 phba->fcf.current_rec.switch_name[2],
921                                 phba->fcf.current_rec.switch_name[3],
922                                 phba->fcf.current_rec.switch_name[4],
923                                 phba->fcf.current_rec.switch_name[5],
924                                 phba->fcf.current_rec.switch_name[6],
925                                 phba->fcf.current_rec.switch_name[7],
926                                 phba->fcf.current_rec.fabric_name[0],
927                                 phba->fcf.current_rec.fabric_name[1],
928                                 phba->fcf.current_rec.fabric_name[2],
929                                 phba->fcf.current_rec.fabric_name[3],
930                                 phba->fcf.current_rec.fabric_name[4],
931                                 phba->fcf.current_rec.fabric_name[5],
932                                 phba->fcf.current_rec.fabric_name[6],
933                                 phba->fcf.current_rec.fabric_name[7]);
934                         lpfc_nlp_put(ndlp);
935                         spin_lock_irq(&phba->hbalock);
936                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
937                         spin_unlock_irq(&phba->hbalock);
938                         goto out;
939                 }
940                 if (!rc) {
941                         /* Mark the FCF discovery process done */
942                         if (phba->hba_flag & HBA_FIP_SUPPORT)
943                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
944                                                 LOG_ELS,
945                                                 "2769 FLOGI successful on FCF "
946                                                 "record: current_fcf_index:"
947                                                 "x%x, terminate FCF round "
948                                                 "robin failover process\n",
949                                                 phba->fcf.current_rec.fcf_indx);
950                         spin_lock_irq(&phba->hbalock);
951                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
952                         spin_unlock_irq(&phba->hbalock);
953                         goto out;
954                 }
955         }
956
957 flogifail:
958         lpfc_nlp_put(ndlp);
959
960         if (!lpfc_error_lost_link(irsp)) {
961                 /* FLOGI failed, so just use loop map to make discovery list */
962                 lpfc_disc_list_loopmap(vport);
963
964                 /* Start discovery */
965                 lpfc_disc_start(vport);
966         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
967                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
968                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
969                         (phba->link_state != LPFC_CLEAR_LA)) {
970                 /* If FLOGI failed enable link interrupt. */
971                 lpfc_issue_clear_la(phba, vport);
972         }
973 out:
974         lpfc_els_free_iocb(phba, cmdiocb);
975 }
976
977 /**
978  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
979  * @vport: pointer to a host virtual N_Port data structure.
980  * @ndlp: pointer to a node-list data structure.
981  * @retry: number of retries to the command IOCB.
982  *
983  * This routine issues a Fabric Login (FLOGI) Request ELS command
984  * for a @vport. The initiator service parameters are put into the payload
985  * of the FLOGI Request IOCB and the top-level callback function pointer
986  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
987  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
988  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
989  *
990  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
991  * will be incremented by 1 for holding the ndlp and the reference to ndlp
992  * will be stored into the context1 field of the IOCB for the completion
993  * callback function to the FLOGI ELS command.
994  *
995  * Return code
996  *   0 - successfully issued flogi iocb for @vport
997  *   1 - failed to issue flogi iocb for @vport
998  **/
999 static int
1000 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1001                      uint8_t retry)
1002 {
1003         struct lpfc_hba  *phba = vport->phba;
1004         struct serv_parm *sp;
1005         IOCB_t *icmd;
1006         struct lpfc_iocbq *elsiocb;
1007         struct lpfc_sli_ring *pring;
1008         uint8_t *pcmd;
1009         uint16_t cmdsize;
1010         uint32_t tmo;
1011         int rc;
1012
1013         pring = &phba->sli.ring[LPFC_ELS_RING];
1014
1015         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1016         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1017                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1018
1019         if (!elsiocb)
1020                 return 1;
1021
1022         icmd = &elsiocb->iocb;
1023         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1024
1025         /* For FLOGI request, remainder of payload is service parameters */
1026         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1027         pcmd += sizeof(uint32_t);
1028         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1029         sp = (struct serv_parm *) pcmd;
1030
1031         /* Setup CSPs accordingly for Fabric */
1032         sp->cmn.e_d_tov = 0;
1033         sp->cmn.w2.r_a_tov = 0;
1034         sp->cls1.classValid = 0;
1035         sp->cls2.seqDelivery = 1;
1036         sp->cls3.seqDelivery = 1;
1037         if (sp->cmn.fcphLow < FC_PH3)
1038                 sp->cmn.fcphLow = FC_PH3;
1039         if (sp->cmn.fcphHigh < FC_PH3)
1040                 sp->cmn.fcphHigh = FC_PH3;
1041
1042         if  (phba->sli_rev == LPFC_SLI_REV4) {
1043                 elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1044                 elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1045                 /* FLOGI needs to be 3 for WQE FCFI */
1046                 /* Set the fcfi to the fcfi we registered with */
1047                 elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1048         } else if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1049                 sp->cmn.request_multiple_Nport = 1;
1050                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1051                 icmd->ulpCt_h = 1;
1052                 icmd->ulpCt_l = 0;
1053         }
1054
1055         if (phba->fc_topology != TOPOLOGY_LOOP) {
1056                 icmd->un.elsreq64.myID = 0;
1057                 icmd->un.elsreq64.fl = 1;
1058         }
1059
1060         tmo = phba->fc_ratov;
1061         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1062         lpfc_set_disctmo(vport);
1063         phba->fc_ratov = tmo;
1064
1065         phba->fc_stat.elsXmitFLOGI++;
1066         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1067
1068         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1069                 "Issue FLOGI:     opt:x%x",
1070                 phba->sli3_options, 0, 0);
1071
1072         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1073         if (rc == IOCB_ERROR) {
1074                 lpfc_els_free_iocb(phba, elsiocb);
1075                 return 1;
1076         }
1077         return 0;
1078 }
1079
1080 /**
1081  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1082  * @phba: pointer to lpfc hba data structure.
1083  *
1084  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1085  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1086  * list and issues an abort IOCB commond on each outstanding IOCB that
1087  * contains a active Fabric_DID ndlp. Note that this function is to issue
1088  * the abort IOCB command on all the outstanding IOCBs, thus when this
1089  * function returns, it does not guarantee all the IOCBs are actually aborted.
1090  *
1091  * Return code
1092  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1093  **/
1094 int
1095 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1096 {
1097         struct lpfc_sli_ring *pring;
1098         struct lpfc_iocbq *iocb, *next_iocb;
1099         struct lpfc_nodelist *ndlp;
1100         IOCB_t *icmd;
1101
1102         /* Abort outstanding I/O on NPort <nlp_DID> */
1103         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1104                         "0201 Abort outstanding I/O on NPort x%x\n",
1105                         Fabric_DID);
1106
1107         pring = &phba->sli.ring[LPFC_ELS_RING];
1108
1109         /*
1110          * Check the txcmplq for an iocb that matches the nport the driver is
1111          * searching for.
1112          */
1113         spin_lock_irq(&phba->hbalock);
1114         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1115                 icmd = &iocb->iocb;
1116                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
1117                     icmd->un.elsreq64.bdl.ulpIoTag32) {
1118                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
1119                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1120                             (ndlp->nlp_DID == Fabric_DID))
1121                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1122                 }
1123         }
1124         spin_unlock_irq(&phba->hbalock);
1125
1126         return 0;
1127 }
1128
1129 /**
1130  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1131  * @vport: pointer to a host virtual N_Port data structure.
1132  *
1133  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1134  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1135  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1136  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1137  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1138  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1139  * @vport.
1140  *
1141  * Return code
1142  *   0 - failed to issue initial flogi for @vport
1143  *   1 - successfully issued initial flogi for @vport
1144  **/
1145 int
1146 lpfc_initial_flogi(struct lpfc_vport *vport)
1147 {
1148         struct lpfc_hba *phba = vport->phba;
1149         struct lpfc_nodelist *ndlp;
1150
1151         vport->port_state = LPFC_FLOGI;
1152         lpfc_set_disctmo(vport);
1153
1154         /* First look for the Fabric ndlp */
1155         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1156         if (!ndlp) {
1157                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1158                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1159                 if (!ndlp)
1160                         return 0;
1161                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1162                 /* Set the node type */
1163                 ndlp->nlp_type |= NLP_FABRIC;
1164                 /* Put ndlp onto node list */
1165                 lpfc_enqueue_node(vport, ndlp);
1166         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1167                 /* re-setup ndlp without removing from node list */
1168                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1169                 if (!ndlp)
1170                         return 0;
1171         }
1172
1173         if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1174                 /* This decrement of reference count to node shall kick off
1175                  * the release of the node.
1176                  */
1177                 lpfc_nlp_put(ndlp);
1178                 return 0;
1179         }
1180         return 1;
1181 }
1182
1183 /**
1184  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1185  * @vport: pointer to a host virtual N_Port data structure.
1186  *
1187  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1188  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1189  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1190  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1191  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1192  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1193  * @vport.
1194  *
1195  * Return code
1196  *   0 - failed to issue initial fdisc for @vport
1197  *   1 - successfully issued initial fdisc for @vport
1198  **/
1199 int
1200 lpfc_initial_fdisc(struct lpfc_vport *vport)
1201 {
1202         struct lpfc_hba *phba = vport->phba;
1203         struct lpfc_nodelist *ndlp;
1204
1205         /* First look for the Fabric ndlp */
1206         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1207         if (!ndlp) {
1208                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1209                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1210                 if (!ndlp)
1211                         return 0;
1212                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1213                 /* Put ndlp onto node list */
1214                 lpfc_enqueue_node(vport, ndlp);
1215         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1216                 /* re-setup ndlp without removing from node list */
1217                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1218                 if (!ndlp)
1219                         return 0;
1220         }
1221
1222         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1223                 /* decrement node reference count to trigger the release of
1224                  * the node.
1225                  */
1226                 lpfc_nlp_put(ndlp);
1227                 return 0;
1228         }
1229         return 1;
1230 }
1231
1232 /**
1233  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1234  * @vport: pointer to a host virtual N_Port data structure.
1235  *
1236  * This routine checks whether there are more remaining Port Logins
1237  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1238  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1239  * to issue ELS PLOGIs up to the configured discover threads with the
1240  * @vport (@vport->cfg_discovery_threads). The function also decrement
1241  * the @vport's num_disc_node by 1 if it is not already 0.
1242  **/
1243 void
1244 lpfc_more_plogi(struct lpfc_vport *vport)
1245 {
1246         int sentplogi;
1247
1248         if (vport->num_disc_nodes)
1249                 vport->num_disc_nodes--;
1250
1251         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1252         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1253                          "0232 Continue discovery with %d PLOGIs to go "
1254                          "Data: x%x x%x x%x\n",
1255                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1256                          vport->fc_flag, vport->port_state);
1257         /* Check to see if there are more PLOGIs to be sent */
1258         if (vport->fc_flag & FC_NLP_MORE)
1259                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1260                 sentplogi = lpfc_els_disc_plogi(vport);
1261
1262         return;
1263 }
1264
1265 /**
1266  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1267  * @phba: pointer to lpfc hba data structure.
1268  * @prsp: pointer to response IOCB payload.
1269  * @ndlp: pointer to a node-list data structure.
1270  *
1271  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1272  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1273  * The following cases are considered N_Port confirmed:
1274  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1275  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1276  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1277  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1278  * 1) if there is a node on vport list other than the @ndlp with the same
1279  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1280  * on that node to release the RPI associated with the node; 2) if there is
1281  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1282  * into, a new node shall be allocated (or activated). In either case, the
1283  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1284  * be released and the new_ndlp shall be put on to the vport node list and
1285  * its pointer returned as the confirmed node.
1286  *
1287  * Note that before the @ndlp got "released", the keepDID from not-matching
1288  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1289  * of the @ndlp. This is because the release of @ndlp is actually to put it
1290  * into an inactive state on the vport node list and the vport node list
1291  * management algorithm does not allow two node with a same DID.
1292  *
1293  * Return code
1294  *   pointer to the PLOGI N_Port @ndlp
1295  **/
1296 static struct lpfc_nodelist *
1297 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1298                          struct lpfc_nodelist *ndlp)
1299 {
1300         struct lpfc_vport    *vport = ndlp->vport;
1301         struct lpfc_nodelist *new_ndlp;
1302         struct lpfc_rport_data *rdata;
1303         struct fc_rport *rport;
1304         struct serv_parm *sp;
1305         uint8_t  name[sizeof(struct lpfc_name)];
1306         uint32_t rc, keepDID = 0;
1307         int  put_node;
1308         int  put_rport;
1309
1310         /* Fabric nodes can have the same WWPN so we don't bother searching
1311          * by WWPN.  Just return the ndlp that was given to us.
1312          */
1313         if (ndlp->nlp_type & NLP_FABRIC)
1314                 return ndlp;
1315
1316         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1317         memset(name, 0, sizeof(struct lpfc_name));
1318
1319         /* Now we find out if the NPort we are logging into, matches the WWPN
1320          * we have for that ndlp. If not, we have some work to do.
1321          */
1322         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1323
1324         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1325                 return ndlp;
1326
1327         if (!new_ndlp) {
1328                 rc = memcmp(&ndlp->nlp_portname, name,
1329                             sizeof(struct lpfc_name));
1330                 if (!rc)
1331                         return ndlp;
1332                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1333                 if (!new_ndlp)
1334                         return ndlp;
1335                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1336         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1337                 rc = memcmp(&ndlp->nlp_portname, name,
1338                             sizeof(struct lpfc_name));
1339                 if (!rc)
1340                         return ndlp;
1341                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1342                                                 NLP_STE_UNUSED_NODE);
1343                 if (!new_ndlp)
1344                         return ndlp;
1345                 keepDID = new_ndlp->nlp_DID;
1346         } else
1347                 keepDID = new_ndlp->nlp_DID;
1348
1349         lpfc_unreg_rpi(vport, new_ndlp);
1350         new_ndlp->nlp_DID = ndlp->nlp_DID;
1351         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1352
1353         if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1354                 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1355         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1356
1357         /* Set state will put new_ndlp on to node list if not already done */
1358         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1359
1360         /* Move this back to NPR state */
1361         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1362                 /* The new_ndlp is replacing ndlp totally, so we need
1363                  * to put ndlp on UNUSED list and try to free it.
1364                  */
1365
1366                 /* Fix up the rport accordingly */
1367                 rport =  ndlp->rport;
1368                 if (rport) {
1369                         rdata = rport->dd_data;
1370                         if (rdata->pnode == ndlp) {
1371                                 lpfc_nlp_put(ndlp);
1372                                 ndlp->rport = NULL;
1373                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1374                                 new_ndlp->rport = rport;
1375                         }
1376                         new_ndlp->nlp_type = ndlp->nlp_type;
1377                 }
1378                 /* We shall actually free the ndlp with both nlp_DID and
1379                  * nlp_portname fields equals 0 to avoid any ndlp on the
1380                  * nodelist never to be used.
1381                  */
1382                 if (ndlp->nlp_DID == 0) {
1383                         spin_lock_irq(&phba->ndlp_lock);
1384                         NLP_SET_FREE_REQ(ndlp);
1385                         spin_unlock_irq(&phba->ndlp_lock);
1386                 }
1387
1388                 /* Two ndlps cannot have the same did on the nodelist */
1389                 ndlp->nlp_DID = keepDID;
1390                 lpfc_drop_node(vport, ndlp);
1391         }
1392         else {
1393                 lpfc_unreg_rpi(vport, ndlp);
1394                 /* Two ndlps cannot have the same did */
1395                 ndlp->nlp_DID = keepDID;
1396                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1397                 /* Since we are swapping the ndlp passed in with the new one
1398                  * and the did has already been swapped, copy over the
1399                  * state and names.
1400                  */
1401                 memcpy(&new_ndlp->nlp_portname, &ndlp->nlp_portname,
1402                         sizeof(struct lpfc_name));
1403                 memcpy(&new_ndlp->nlp_nodename, &ndlp->nlp_nodename,
1404                         sizeof(struct lpfc_name));
1405                 new_ndlp->nlp_state = ndlp->nlp_state;
1406                 /* Fix up the rport accordingly */
1407                 rport = ndlp->rport;
1408                 if (rport) {
1409                         rdata = rport->dd_data;
1410                         put_node = rdata->pnode != NULL;
1411                         put_rport = ndlp->rport != NULL;
1412                         rdata->pnode = NULL;
1413                         ndlp->rport = NULL;
1414                         if (put_node)
1415                                 lpfc_nlp_put(ndlp);
1416                         if (put_rport)
1417                                 put_device(&rport->dev);
1418                 }
1419         }
1420         return new_ndlp;
1421 }
1422
1423 /**
1424  * lpfc_end_rscn - Check and handle more rscn for a vport
1425  * @vport: pointer to a host virtual N_Port data structure.
1426  *
1427  * This routine checks whether more Registration State Change
1428  * Notifications (RSCNs) came in while the discovery state machine was in
1429  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1430  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1431  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1432  * handling the RSCNs.
1433  **/
1434 void
1435 lpfc_end_rscn(struct lpfc_vport *vport)
1436 {
1437         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1438
1439         if (vport->fc_flag & FC_RSCN_MODE) {
1440                 /*
1441                  * Check to see if more RSCNs came in while we were
1442                  * processing this one.
1443                  */
1444                 if (vport->fc_rscn_id_cnt ||
1445                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1446                         lpfc_els_handle_rscn(vport);
1447                 else {
1448                         spin_lock_irq(shost->host_lock);
1449                         vport->fc_flag &= ~FC_RSCN_MODE;
1450                         spin_unlock_irq(shost->host_lock);
1451                 }
1452         }
1453 }
1454
1455 /**
1456  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1457  * @phba: pointer to lpfc hba data structure.
1458  * @cmdiocb: pointer to lpfc command iocb data structure.
1459  * @rspiocb: pointer to lpfc response iocb data structure.
1460  *
1461  * This routine is the completion callback function for issuing the Port
1462  * Login (PLOGI) command. For PLOGI completion, there must be an active
1463  * ndlp on the vport node list that matches the remote node ID from the
1464  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1465  * ignored and command IOCB released. The PLOGI response IOCB status is
1466  * checked for error conditons. If there is error status reported, PLOGI
1467  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1468  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1469  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1470  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1471  * there are additional N_Port nodes with the vport that need to perform
1472  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1473  * PLOGIs.
1474  **/
1475 static void
1476 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1477                     struct lpfc_iocbq *rspiocb)
1478 {
1479         struct lpfc_vport *vport = cmdiocb->vport;
1480         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1481         IOCB_t *irsp;
1482         struct lpfc_nodelist *ndlp;
1483         struct lpfc_dmabuf *prsp;
1484         int disc, rc, did, type;
1485
1486         /* we pass cmdiocb to state machine which needs rspiocb as well */
1487         cmdiocb->context_un.rsp_iocb = rspiocb;
1488
1489         irsp = &rspiocb->iocb;
1490         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1491                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1492                 irsp->ulpStatus, irsp->un.ulpWord[4],
1493                 irsp->un.elsreq64.remoteID);
1494
1495         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1496         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1497                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1498                                  "0136 PLOGI completes to NPort x%x "
1499                                  "with no ndlp. Data: x%x x%x x%x\n",
1500                                  irsp->un.elsreq64.remoteID,
1501                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1502                                  irsp->ulpIoTag);
1503                 goto out;
1504         }
1505
1506         /* Since ndlp can be freed in the disc state machine, note if this node
1507          * is being used during discovery.
1508          */
1509         spin_lock_irq(shost->host_lock);
1510         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1511         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1512         spin_unlock_irq(shost->host_lock);
1513         rc   = 0;
1514
1515         /* PLOGI completes to NPort <nlp_DID> */
1516         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1517                          "0102 PLOGI completes to NPort x%x "
1518                          "Data: x%x x%x x%x x%x x%x\n",
1519                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1520                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1521         /* Check to see if link went down during discovery */
1522         if (lpfc_els_chk_latt(vport)) {
1523                 spin_lock_irq(shost->host_lock);
1524                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1525                 spin_unlock_irq(shost->host_lock);
1526                 goto out;
1527         }
1528
1529         /* ndlp could be freed in DSM, save these values now */
1530         type = ndlp->nlp_type;
1531         did = ndlp->nlp_DID;
1532
1533         if (irsp->ulpStatus) {
1534                 /* Check for retry */
1535                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1536                         /* ELS command is being retried */
1537                         if (disc) {
1538                                 spin_lock_irq(shost->host_lock);
1539                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1540                                 spin_unlock_irq(shost->host_lock);
1541                         }
1542                         goto out;
1543                 }
1544                 /* PLOGI failed Don't print the vport to vport rjts */
1545                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1546                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1547                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1548                         (phba)->pport->cfg_log_verbose & LOG_ELS)
1549                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1550                                  "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1551                                  ndlp->nlp_DID, irsp->ulpStatus,
1552                                  irsp->un.ulpWord[4]);
1553                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1554                 if (lpfc_error_lost_link(irsp))
1555                         rc = NLP_STE_FREED_NODE;
1556                 else
1557                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1558                                                      NLP_EVT_CMPL_PLOGI);
1559         } else {
1560                 /* Good status, call state machine */
1561                 prsp = list_entry(((struct lpfc_dmabuf *)
1562                                    cmdiocb->context2)->list.next,
1563                                   struct lpfc_dmabuf, list);
1564                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1565                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1566                                              NLP_EVT_CMPL_PLOGI);
1567         }
1568
1569         if (disc && vport->num_disc_nodes) {
1570                 /* Check to see if there are more PLOGIs to be sent */
1571                 lpfc_more_plogi(vport);
1572
1573                 if (vport->num_disc_nodes == 0) {
1574                         spin_lock_irq(shost->host_lock);
1575                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1576                         spin_unlock_irq(shost->host_lock);
1577
1578                         lpfc_can_disctmo(vport);
1579                         lpfc_end_rscn(vport);
1580                 }
1581         }
1582
1583 out:
1584         lpfc_els_free_iocb(phba, cmdiocb);
1585         return;
1586 }
1587
1588 /**
1589  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1590  * @vport: pointer to a host virtual N_Port data structure.
1591  * @did: destination port identifier.
1592  * @retry: number of retries to the command IOCB.
1593  *
1594  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1595  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1596  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1597  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1598  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1599  *
1600  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1601  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1602  * will be stored into the context1 field of the IOCB for the completion
1603  * callback function to the PLOGI ELS command.
1604  *
1605  * Return code
1606  *   0 - Successfully issued a plogi for @vport
1607  *   1 - failed to issue a plogi for @vport
1608  **/
1609 int
1610 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1611 {
1612         struct lpfc_hba  *phba = vport->phba;
1613         struct serv_parm *sp;
1614         IOCB_t *icmd;
1615         struct lpfc_nodelist *ndlp;
1616         struct lpfc_iocbq *elsiocb;
1617         struct lpfc_sli *psli;
1618         uint8_t *pcmd;
1619         uint16_t cmdsize;
1620         int ret;
1621
1622         psli = &phba->sli;
1623
1624         ndlp = lpfc_findnode_did(vport, did);
1625         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1626                 ndlp = NULL;
1627
1628         /* If ndlp is not NULL, we will bump the reference count on it */
1629         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1630         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1631                                      ELS_CMD_PLOGI);
1632         if (!elsiocb)
1633                 return 1;
1634
1635         icmd = &elsiocb->iocb;
1636         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1637
1638         /* For PLOGI request, remainder of payload is service parameters */
1639         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1640         pcmd += sizeof(uint32_t);
1641         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1642         sp = (struct serv_parm *) pcmd;
1643
1644         /*
1645          * If we are a N-port connected to a Fabric, fix-up paramm's so logins
1646          * to device on remote loops work.
1647          */
1648         if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
1649                 sp->cmn.altBbCredit = 1;
1650
1651         if (sp->cmn.fcphLow < FC_PH_4_3)
1652                 sp->cmn.fcphLow = FC_PH_4_3;
1653
1654         if (sp->cmn.fcphHigh < FC_PH3)
1655                 sp->cmn.fcphHigh = FC_PH3;
1656
1657         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1658                 "Issue PLOGI:     did:x%x",
1659                 did, 0, 0);
1660
1661         phba->fc_stat.elsXmitPLOGI++;
1662         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1663         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
1664
1665         if (ret == IOCB_ERROR) {
1666                 lpfc_els_free_iocb(phba, elsiocb);
1667                 return 1;
1668         }
1669         return 0;
1670 }
1671
1672 /**
1673  * lpfc_cmpl_els_prli - Completion callback function for prli
1674  * @phba: pointer to lpfc hba data structure.
1675  * @cmdiocb: pointer to lpfc command iocb data structure.
1676  * @rspiocb: pointer to lpfc response iocb data structure.
1677  *
1678  * This routine is the completion callback function for a Process Login
1679  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1680  * status. If there is error status reported, PRLI retry shall be attempted
1681  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1682  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1683  * ndlp to mark the PRLI completion.
1684  **/
1685 static void
1686 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1687                    struct lpfc_iocbq *rspiocb)
1688 {
1689         struct lpfc_vport *vport = cmdiocb->vport;
1690         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1691         IOCB_t *irsp;
1692         struct lpfc_sli *psli;
1693         struct lpfc_nodelist *ndlp;
1694
1695         psli = &phba->sli;
1696         /* we pass cmdiocb to state machine which needs rspiocb as well */
1697         cmdiocb->context_un.rsp_iocb = rspiocb;
1698
1699         irsp = &(rspiocb->iocb);
1700         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1701         spin_lock_irq(shost->host_lock);
1702         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1703         spin_unlock_irq(shost->host_lock);
1704
1705         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1706                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1707                 irsp->ulpStatus, irsp->un.ulpWord[4],
1708                 ndlp->nlp_DID);
1709         /* PRLI completes to NPort <nlp_DID> */
1710         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1711                          "0103 PRLI completes to NPort x%x "
1712                          "Data: x%x x%x x%x x%x\n",
1713                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1714                          irsp->ulpTimeout, vport->num_disc_nodes);
1715
1716         vport->fc_prli_sent--;
1717         /* Check to see if link went down during discovery */
1718         if (lpfc_els_chk_latt(vport))
1719                 goto out;
1720
1721         if (irsp->ulpStatus) {
1722                 /* Check for retry */
1723                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1724                         /* ELS command is being retried */
1725                         goto out;
1726                 }
1727                 /* PRLI failed */
1728                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1729                                  "2754 PRLI failure DID:%06X Status:x%x/x%x\n",
1730                                  ndlp->nlp_DID, irsp->ulpStatus,
1731                                  irsp->un.ulpWord[4]);
1732                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1733                 if (lpfc_error_lost_link(irsp))
1734                         goto out;
1735                 else
1736                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1737                                                 NLP_EVT_CMPL_PRLI);
1738         } else
1739                 /* Good status, call state machine */
1740                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1741                                         NLP_EVT_CMPL_PRLI);
1742 out:
1743         lpfc_els_free_iocb(phba, cmdiocb);
1744         return;
1745 }
1746
1747 /**
1748  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
1749  * @vport: pointer to a host virtual N_Port data structure.
1750  * @ndlp: pointer to a node-list data structure.
1751  * @retry: number of retries to the command IOCB.
1752  *
1753  * This routine issues a Process Login (PRLI) ELS command for the
1754  * @vport. The PRLI service parameters are set up in the payload of the
1755  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1756  * is put to the IOCB completion callback func field before invoking the
1757  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1758  *
1759  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1760  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1761  * will be stored into the context1 field of the IOCB for the completion
1762  * callback function to the PRLI ELS command.
1763  *
1764  * Return code
1765  *   0 - successfully issued prli iocb command for @vport
1766  *   1 - failed to issue prli iocb command for @vport
1767  **/
1768 int
1769 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1770                     uint8_t retry)
1771 {
1772         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1773         struct lpfc_hba *phba = vport->phba;
1774         PRLI *npr;
1775         IOCB_t *icmd;
1776         struct lpfc_iocbq *elsiocb;
1777         uint8_t *pcmd;
1778         uint16_t cmdsize;
1779
1780         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1781         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1782                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1783         if (!elsiocb)
1784                 return 1;
1785
1786         icmd = &elsiocb->iocb;
1787         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1788
1789         /* For PRLI request, remainder of payload is service parameters */
1790         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1791         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1792         pcmd += sizeof(uint32_t);
1793
1794         /* For PRLI, remainder of payload is PRLI parameter page */
1795         npr = (PRLI *) pcmd;
1796         /*
1797          * If our firmware version is 3.20 or later,
1798          * set the following bits for FC-TAPE support.
1799          */
1800         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1801                 npr->ConfmComplAllowed = 1;
1802                 npr->Retry = 1;
1803                 npr->TaskRetryIdReq = 1;
1804         }
1805         npr->estabImagePair = 1;
1806         npr->readXferRdyDis = 1;
1807
1808         /* For FCP support */
1809         npr->prliType = PRLI_FCP_TYPE;
1810         npr->initiatorFunc = 1;
1811
1812         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1813                 "Issue PRLI:      did:x%x",
1814                 ndlp->nlp_DID, 0, 0);
1815
1816         phba->fc_stat.elsXmitPRLI++;
1817         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1818         spin_lock_irq(shost->host_lock);
1819         ndlp->nlp_flag |= NLP_PRLI_SND;
1820         spin_unlock_irq(shost->host_lock);
1821         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
1822             IOCB_ERROR) {
1823                 spin_lock_irq(shost->host_lock);
1824                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1825                 spin_unlock_irq(shost->host_lock);
1826                 lpfc_els_free_iocb(phba, elsiocb);
1827                 return 1;
1828         }
1829         vport->fc_prli_sent++;
1830         return 0;
1831 }
1832
1833 /**
1834  * lpfc_rscn_disc - Perform rscn discovery for a vport
1835  * @vport: pointer to a host virtual N_Port data structure.
1836  *
1837  * This routine performs Registration State Change Notification (RSCN)
1838  * discovery for a @vport. If the @vport's node port recovery count is not
1839  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1840  * the nodes that need recovery. If none of the PLOGI were needed through
1841  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1842  * invoked to check and handle possible more RSCN came in during the period
1843  * of processing the current ones.
1844  **/
1845 static void
1846 lpfc_rscn_disc(struct lpfc_vport *vport)
1847 {
1848         lpfc_can_disctmo(vport);
1849
1850         /* RSCN discovery */
1851         /* go thru NPR nodes and issue ELS PLOGIs */
1852         if (vport->fc_npr_cnt)
1853                 if (lpfc_els_disc_plogi(vport))
1854                         return;
1855
1856         lpfc_end_rscn(vport);
1857 }
1858
1859 /**
1860  * lpfc_adisc_done - Complete the adisc phase of discovery
1861  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1862  *
1863  * This function is called when the final ADISC is completed during discovery.
1864  * This function handles clearing link attention or issuing reg_vpi depending
1865  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1866  * discovery.
1867  * This function is called with no locks held.
1868  **/
1869 static void
1870 lpfc_adisc_done(struct lpfc_vport *vport)
1871 {
1872         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1873         struct lpfc_hba   *phba = vport->phba;
1874
1875         /*
1876          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1877          * and continue discovery.
1878          */
1879         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1880             !(vport->fc_flag & FC_RSCN_MODE) &&
1881             (phba->sli_rev < LPFC_SLI_REV4)) {
1882                 lpfc_issue_reg_vpi(phba, vport);
1883                 return;
1884         }
1885         /*
1886         * For SLI2, we need to set port_state to READY
1887         * and continue discovery.
1888         */
1889         if (vport->port_state < LPFC_VPORT_READY) {
1890                 /* If we get here, there is nothing to ADISC */
1891                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1892                         lpfc_issue_clear_la(phba, vport);
1893                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1894                         vport->num_disc_nodes = 0;
1895                         /* go thru NPR list, issue ELS PLOGIs */
1896                         if (vport->fc_npr_cnt)
1897                                 lpfc_els_disc_plogi(vport);
1898                         if (!vport->num_disc_nodes) {
1899                                 spin_lock_irq(shost->host_lock);
1900                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1901                                 spin_unlock_irq(shost->host_lock);
1902                                 lpfc_can_disctmo(vport);
1903                                 lpfc_end_rscn(vport);
1904                         }
1905                 }
1906                 vport->port_state = LPFC_VPORT_READY;
1907         } else
1908                 lpfc_rscn_disc(vport);
1909 }
1910
1911 /**
1912  * lpfc_more_adisc - Issue more adisc as needed
1913  * @vport: pointer to a host virtual N_Port data structure.
1914  *
1915  * This routine determines whether there are more ndlps on a @vport
1916  * node list need to have Address Discover (ADISC) issued. If so, it will
1917  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1918  * remaining nodes which need to have ADISC sent.
1919  **/
1920 void
1921 lpfc_more_adisc(struct lpfc_vport *vport)
1922 {
1923         int sentadisc;
1924
1925         if (vport->num_disc_nodes)
1926                 vport->num_disc_nodes--;
1927         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1928         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1929                          "0210 Continue discovery with %d ADISCs to go "
1930                          "Data: x%x x%x x%x\n",
1931                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1932                          vport->fc_flag, vport->port_state);
1933         /* Check to see if there are more ADISCs to be sent */
1934         if (vport->fc_flag & FC_NLP_MORE) {
1935                 lpfc_set_disctmo(vport);
1936                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1937                 sentadisc = lpfc_els_disc_adisc(vport);
1938         }
1939         if (!vport->num_disc_nodes)
1940                 lpfc_adisc_done(vport);
1941         return;
1942 }
1943
1944 /**
1945  * lpfc_cmpl_els_adisc - Completion callback function for adisc
1946  * @phba: pointer to lpfc hba data structure.
1947  * @cmdiocb: pointer to lpfc command iocb data structure.
1948  * @rspiocb: pointer to lpfc response iocb data structure.
1949  *
1950  * This routine is the completion function for issuing the Address Discover
1951  * (ADISC) command. It first checks to see whether link went down during
1952  * the discovery process. If so, the node will be marked as node port
1953  * recovery for issuing discover IOCB by the link attention handler and
1954  * exit. Otherwise, the response status is checked. If error was reported
1955  * in the response status, the ADISC command shall be retried by invoking
1956  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1957  * the response status, the state machine is invoked to set transition
1958  * with respect to NLP_EVT_CMPL_ADISC event.
1959  **/
1960 static void
1961 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1962                     struct lpfc_iocbq *rspiocb)
1963 {
1964         struct lpfc_vport *vport = cmdiocb->vport;
1965         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1966         IOCB_t *irsp;
1967         struct lpfc_nodelist *ndlp;
1968         int  disc;
1969
1970         /* we pass cmdiocb to state machine which needs rspiocb as well */
1971         cmdiocb->context_un.rsp_iocb = rspiocb;
1972
1973         irsp = &(rspiocb->iocb);
1974         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1975
1976         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1977                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1978                 irsp->ulpStatus, irsp->un.ulpWord[4],
1979                 ndlp->nlp_DID);
1980
1981         /* Since ndlp can be freed in the disc state machine, note if this node
1982          * is being used during discovery.
1983          */
1984         spin_lock_irq(shost->host_lock);
1985         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1986         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1987         spin_unlock_irq(shost->host_lock);
1988         /* ADISC completes to NPort <nlp_DID> */
1989         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1990                          "0104 ADISC completes to NPort x%x "
1991                          "Data: x%x x%x x%x x%x x%x\n",
1992                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1993                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1994         /* Check to see if link went down during discovery */
1995         if (lpfc_els_chk_latt(vport)) {
1996                 spin_lock_irq(shost->host_lock);
1997                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1998                 spin_unlock_irq(shost->host_lock);
1999                 goto out;
2000         }
2001
2002         if (irsp->ulpStatus) {
2003                 /* Check for retry */
2004                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2005                         /* ELS command is being retried */
2006                         if (disc) {
2007                                 spin_lock_irq(shost->host_lock);
2008                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2009                                 spin_unlock_irq(shost->host_lock);
2010                                 lpfc_set_disctmo(vport);
2011                         }
2012                         goto out;
2013                 }
2014                 /* ADISC failed */
2015                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2016                                  "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2017                                  ndlp->nlp_DID, irsp->ulpStatus,
2018                                  irsp->un.ulpWord[4]);
2019                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2020                 if (!lpfc_error_lost_link(irsp))
2021                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2022                                                 NLP_EVT_CMPL_ADISC);
2023         } else
2024                 /* Good status, call state machine */
2025                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2026                                         NLP_EVT_CMPL_ADISC);
2027
2028         /* Check to see if there are more ADISCs to be sent */
2029         if (disc && vport->num_disc_nodes)
2030                 lpfc_more_adisc(vport);
2031 out:
2032         lpfc_els_free_iocb(phba, cmdiocb);
2033         return;
2034 }
2035
2036 /**
2037  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2038  * @vport: pointer to a virtual N_Port data structure.
2039  * @ndlp: pointer to a node-list data structure.
2040  * @retry: number of retries to the command IOCB.
2041  *
2042  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2043  * @vport. It prepares the payload of the ADISC ELS command, updates the
2044  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2045  * to issue the ADISC ELS command.
2046  *
2047  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2048  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2049  * will be stored into the context1 field of the IOCB for the completion
2050  * callback function to the ADISC ELS command.
2051  *
2052  * Return code
2053  *   0 - successfully issued adisc
2054  *   1 - failed to issue adisc
2055  **/
2056 int
2057 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2058                      uint8_t retry)
2059 {
2060         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2061         struct lpfc_hba  *phba = vport->phba;
2062         ADISC *ap;
2063         IOCB_t *icmd;
2064         struct lpfc_iocbq *elsiocb;
2065         uint8_t *pcmd;
2066         uint16_t cmdsize;
2067
2068         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2069         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2070                                      ndlp->nlp_DID, ELS_CMD_ADISC);
2071         if (!elsiocb)
2072                 return 1;
2073
2074         icmd = &elsiocb->iocb;
2075         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2076
2077         /* For ADISC request, remainder of payload is service parameters */
2078         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2079         pcmd += sizeof(uint32_t);
2080
2081         /* Fill in ADISC payload */
2082         ap = (ADISC *) pcmd;
2083         ap->hardAL_PA = phba->fc_pref_ALPA;
2084         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2085         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2086         ap->DID = be32_to_cpu(vport->fc_myDID);
2087
2088         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2089                 "Issue ADISC:     did:x%x",
2090                 ndlp->nlp_DID, 0, 0);
2091
2092         phba->fc_stat.elsXmitADISC++;
2093         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2094         spin_lock_irq(shost->host_lock);
2095         ndlp->nlp_flag |= NLP_ADISC_SND;
2096         spin_unlock_irq(shost->host_lock);
2097         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2098             IOCB_ERROR) {
2099                 spin_lock_irq(shost->host_lock);
2100                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2101                 spin_unlock_irq(shost->host_lock);
2102                 lpfc_els_free_iocb(phba, elsiocb);
2103                 return 1;
2104         }
2105         return 0;
2106 }
2107
2108 /**
2109  * lpfc_cmpl_els_logo - Completion callback function for logo
2110  * @phba: pointer to lpfc hba data structure.
2111  * @cmdiocb: pointer to lpfc command iocb data structure.
2112  * @rspiocb: pointer to lpfc response iocb data structure.
2113  *
2114  * This routine is the completion function for issuing the ELS Logout (LOGO)
2115  * command. If no error status was reported from the LOGO response, the
2116  * state machine of the associated ndlp shall be invoked for transition with
2117  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2118  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2119  **/
2120 static void
2121 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2122                    struct lpfc_iocbq *rspiocb)
2123 {
2124         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2125         struct lpfc_vport *vport = ndlp->vport;
2126         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2127         IOCB_t *irsp;
2128         struct lpfc_sli *psli;
2129
2130         psli = &phba->sli;
2131         /* we pass cmdiocb to state machine which needs rspiocb as well */
2132         cmdiocb->context_un.rsp_iocb = rspiocb;
2133
2134         irsp = &(rspiocb->iocb);
2135         spin_lock_irq(shost->host_lock);
2136         ndlp->nlp_flag &= ~NLP_LOGO_SND;
2137         spin_unlock_irq(shost->host_lock);
2138
2139         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2140                 "LOGO cmpl:       status:x%x/x%x did:x%x",
2141                 irsp->ulpStatus, irsp->un.ulpWord[4],
2142                 ndlp->nlp_DID);
2143         /* LOGO completes to NPort <nlp_DID> */
2144         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2145                          "0105 LOGO completes to NPort x%x "
2146                          "Data: x%x x%x x%x x%x\n",
2147                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2148                          irsp->ulpTimeout, vport->num_disc_nodes);
2149         /* Check to see if link went down during discovery */
2150         if (lpfc_els_chk_latt(vport))
2151                 goto out;
2152
2153         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2154                 /* NLP_EVT_DEVICE_RM should unregister the RPI
2155                  * which should abort all outstanding IOs.
2156                  */
2157                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2158                                         NLP_EVT_DEVICE_RM);
2159                 goto out;
2160         }
2161
2162         if (irsp->ulpStatus) {
2163                 /* Check for retry */
2164                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
2165                         /* ELS command is being retried */
2166                         goto out;
2167                 /* LOGO failed */
2168                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2169                                  "2756 LOGO failure DID:%06X Status:x%x/x%x\n",
2170                                  ndlp->nlp_DID, irsp->ulpStatus,
2171                                  irsp->un.ulpWord[4]);
2172                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2173                 if (lpfc_error_lost_link(irsp))
2174                         goto out;
2175                 else
2176                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2177                                                 NLP_EVT_CMPL_LOGO);
2178         } else
2179                 /* Good status, call state machine.
2180                  * This will unregister the rpi if needed.
2181                  */
2182                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2183                                         NLP_EVT_CMPL_LOGO);
2184 out:
2185         lpfc_els_free_iocb(phba, cmdiocb);
2186         return;
2187 }
2188
2189 /**
2190  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2191  * @vport: pointer to a virtual N_Port data structure.
2192  * @ndlp: pointer to a node-list data structure.
2193  * @retry: number of retries to the command IOCB.
2194  *
2195  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2196  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2197  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2198  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2199  *
2200  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2201  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2202  * will be stored into the context1 field of the IOCB for the completion
2203  * callback function to the LOGO ELS command.
2204  *
2205  * Return code
2206  *   0 - successfully issued logo
2207  *   1 - failed to issue logo
2208  **/
2209 int
2210 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2211                     uint8_t retry)
2212 {
2213         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2214         struct lpfc_hba  *phba = vport->phba;
2215         IOCB_t *icmd;
2216         struct lpfc_iocbq *elsiocb;
2217         uint8_t *pcmd;
2218         uint16_t cmdsize;
2219         int rc;
2220
2221         spin_lock_irq(shost->host_lock);
2222         if (ndlp->nlp_flag & NLP_LOGO_SND) {
2223                 spin_unlock_irq(shost->host_lock);
2224                 return 0;
2225         }
2226         spin_unlock_irq(shost->host_lock);
2227
2228         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2229         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2230                                      ndlp->nlp_DID, ELS_CMD_LOGO);
2231         if (!elsiocb)
2232                 return 1;
2233
2234         icmd = &elsiocb->iocb;
2235         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2236         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2237         pcmd += sizeof(uint32_t);
2238
2239         /* Fill in LOGO payload */
2240         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2241         pcmd += sizeof(uint32_t);
2242         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2243
2244         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2245                 "Issue LOGO:      did:x%x",
2246                 ndlp->nlp_DID, 0, 0);
2247
2248         phba->fc_stat.elsXmitLOGO++;
2249         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2250         spin_lock_irq(shost->host_lock);
2251         ndlp->nlp_flag |= NLP_LOGO_SND;
2252         spin_unlock_irq(shost->host_lock);
2253         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2254
2255         if (rc == IOCB_ERROR) {
2256                 spin_lock_irq(shost->host_lock);
2257                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2258                 spin_unlock_irq(shost->host_lock);
2259                 lpfc_els_free_iocb(phba, elsiocb);
2260                 return 1;
2261         }
2262         return 0;
2263 }
2264
2265 /**
2266  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
2267  * @phba: pointer to lpfc hba data structure.
2268  * @cmdiocb: pointer to lpfc command iocb data structure.
2269  * @rspiocb: pointer to lpfc response iocb data structure.
2270  *
2271  * This routine is a generic completion callback function for ELS commands.
2272  * Specifically, it is the callback function which does not need to perform
2273  * any command specific operations. It is currently used by the ELS command
2274  * issuing routines for the ELS State Change  Request (SCR),
2275  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2276  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2277  * certain debug loggings, this callback function simply invokes the
2278  * lpfc_els_chk_latt() routine to check whether link went down during the
2279  * discovery process.
2280  **/
2281 static void
2282 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2283                   struct lpfc_iocbq *rspiocb)
2284 {
2285         struct lpfc_vport *vport = cmdiocb->vport;
2286         IOCB_t *irsp;
2287
2288         irsp = &rspiocb->iocb;
2289
2290         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2291                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2292                 irsp->ulpStatus, irsp->un.ulpWord[4],
2293                 irsp->un.elsreq64.remoteID);
2294         /* ELS cmd tag <ulpIoTag> completes */
2295         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2296                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2297                          irsp->ulpIoTag, irsp->ulpStatus,
2298                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2299         /* Check to see if link went down during discovery */
2300         lpfc_els_chk_latt(vport);
2301         lpfc_els_free_iocb(phba, cmdiocb);
2302         return;
2303 }
2304
2305 /**
2306  * lpfc_issue_els_scr - Issue a scr to an node on a vport
2307  * @vport: pointer to a host virtual N_Port data structure.
2308  * @nportid: N_Port identifier to the remote node.
2309  * @retry: number of retries to the command IOCB.
2310  *
2311  * This routine issues a State Change Request (SCR) to a fabric node
2312  * on a @vport. The remote node @nportid is passed into the function. It
2313  * first search the @vport node list to find the matching ndlp. If no such
2314  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2315  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2316  * routine is invoked to send the SCR IOCB.
2317  *
2318  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2319  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2320  * will be stored into the context1 field of the IOCB for the completion
2321  * callback function to the SCR ELS command.
2322  *
2323  * Return code
2324  *   0 - Successfully issued scr command
2325  *   1 - Failed to issue scr command
2326  **/
2327 int
2328 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2329 {
2330         struct lpfc_hba  *phba = vport->phba;
2331         IOCB_t *icmd;
2332         struct lpfc_iocbq *elsiocb;
2333         struct lpfc_sli *psli;
2334         uint8_t *pcmd;
2335         uint16_t cmdsize;
2336         struct lpfc_nodelist *ndlp;
2337
2338         psli = &phba->sli;
2339         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2340
2341         ndlp = lpfc_findnode_did(vport, nportid);
2342         if (!ndlp) {
2343                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2344                 if (!ndlp)
2345                         return 1;
2346                 lpfc_nlp_init(vport, ndlp, nportid);
2347                 lpfc_enqueue_node(vport, ndlp);
2348         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2349                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2350                 if (!ndlp)
2351                         return 1;
2352         }
2353
2354         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2355                                      ndlp->nlp_DID, ELS_CMD_SCR);
2356
2357         if (!elsiocb) {
2358                 /* This will trigger the release of the node just
2359                  * allocated
2360                  */
2361                 lpfc_nlp_put(ndlp);
2362                 return 1;
2363         }
2364
2365         icmd = &elsiocb->iocb;
2366         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2367
2368         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2369         pcmd += sizeof(uint32_t);
2370
2371         /* For SCR, remainder of payload is SCR parameter page */
2372         memset(pcmd, 0, sizeof(SCR));
2373         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2374
2375         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2376                 "Issue SCR:       did:x%x",
2377                 ndlp->nlp_DID, 0, 0);
2378
2379         phba->fc_stat.elsXmitSCR++;
2380         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2381         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2382             IOCB_ERROR) {
2383                 /* The additional lpfc_nlp_put will cause the following
2384                  * lpfc_els_free_iocb routine to trigger the rlease of
2385                  * the node.
2386                  */
2387                 lpfc_nlp_put(ndlp);
2388                 lpfc_els_free_iocb(phba, elsiocb);
2389                 return 1;
2390         }
2391         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2392          * trigger the release of node.
2393          */
2394         lpfc_nlp_put(ndlp);
2395         return 0;
2396 }
2397
2398 /**
2399  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2400  * @vport: pointer to a host virtual N_Port data structure.
2401  * @nportid: N_Port identifier to the remote node.
2402  * @retry: number of retries to the command IOCB.
2403  *
2404  * This routine issues a Fibre Channel Address Resolution Response
2405  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2406  * is passed into the function. It first search the @vport node list to find
2407  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2408  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2409  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2410  *
2411  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2412  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2413  * will be stored into the context1 field of the IOCB for the completion
2414  * callback function to the PARPR ELS command.
2415  *
2416  * Return code
2417  *   0 - Successfully issued farpr command
2418  *   1 - Failed to issue farpr command
2419  **/
2420 static int
2421 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2422 {
2423         struct lpfc_hba  *phba = vport->phba;
2424         IOCB_t *icmd;
2425         struct lpfc_iocbq *elsiocb;
2426         struct lpfc_sli *psli;
2427         FARP *fp;
2428         uint8_t *pcmd;
2429         uint32_t *lp;
2430         uint16_t cmdsize;
2431         struct lpfc_nodelist *ondlp;
2432         struct lpfc_nodelist *ndlp;
2433
2434         psli = &phba->sli;
2435         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2436
2437         ndlp = lpfc_findnode_did(vport, nportid);
2438         if (!ndlp) {
2439                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2440                 if (!ndlp)
2441                         return 1;
2442                 lpfc_nlp_init(vport, ndlp, nportid);
2443                 lpfc_enqueue_node(vport, ndlp);
2444         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2445                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2446                 if (!ndlp)
2447                         return 1;
2448         }
2449
2450         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2451                                      ndlp->nlp_DID, ELS_CMD_RNID);
2452         if (!elsiocb) {
2453                 /* This will trigger the release of the node just
2454                  * allocated
2455                  */
2456                 lpfc_nlp_put(ndlp);
2457                 return 1;
2458         }
2459
2460         icmd = &elsiocb->iocb;
2461         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2462
2463         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2464         pcmd += sizeof(uint32_t);
2465
2466         /* Fill in FARPR payload */
2467         fp = (FARP *) (pcmd);
2468         memset(fp, 0, sizeof(FARP));
2469         lp = (uint32_t *) pcmd;
2470         *lp++ = be32_to_cpu(nportid);
2471         *lp++ = be32_to_cpu(vport->fc_myDID);
2472         fp->Rflags = 0;
2473         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2474
2475         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2476         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2477         ondlp = lpfc_findnode_did(vport, nportid);
2478         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2479                 memcpy(&fp->OportName, &ondlp->nlp_portname,
2480                        sizeof(struct lpfc_name));
2481                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2482                        sizeof(struct lpfc_name));
2483         }
2484
2485         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2486                 "Issue FARPR:     did:x%x",
2487                 ndlp->nlp_DID, 0, 0);
2488
2489         phba->fc_stat.elsXmitFARPR++;
2490         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2491         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2492             IOCB_ERROR) {
2493                 /* The additional lpfc_nlp_put will cause the following
2494                  * lpfc_els_free_iocb routine to trigger the release of
2495                  * the node.
2496                  */
2497                 lpfc_nlp_put(ndlp);
2498                 lpfc_els_free_iocb(phba, elsiocb);
2499                 return 1;
2500         }
2501         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2502          * trigger the release of the node.
2503          */
2504         lpfc_nlp_put(ndlp);
2505         return 0;
2506 }
2507
2508 /**
2509  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
2510  * @vport: pointer to a host virtual N_Port data structure.
2511  * @nlp: pointer to a node-list data structure.
2512  *
2513  * This routine cancels the timer with a delayed IOCB-command retry for
2514  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2515  * removes the ELS retry event if it presents. In addition, if the
2516  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2517  * commands are sent for the @vport's nodes that require issuing discovery
2518  * ADISC.
2519  **/
2520 void
2521 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2522 {
2523         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2524         struct lpfc_work_evt *evtp;
2525
2526         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2527                 return;
2528         spin_lock_irq(shost->host_lock);
2529         nlp->nlp_flag &= ~NLP_DELAY_TMO;
2530         spin_unlock_irq(shost->host_lock);
2531         del_timer_sync(&nlp->nlp_delayfunc);
2532         nlp->nlp_last_elscmd = 0;
2533         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2534                 list_del_init(&nlp->els_retry_evt.evt_listp);
2535                 /* Decrement nlp reference count held for the delayed retry */
2536                 evtp = &nlp->els_retry_evt;
2537                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2538         }
2539         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2540                 spin_lock_irq(shost->host_lock);
2541                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2542                 spin_unlock_irq(shost->host_lock);
2543                 if (vport->num_disc_nodes) {
2544                         if (vport->port_state < LPFC_VPORT_READY) {
2545                                 /* Check if there are more ADISCs to be sent */
2546                                 lpfc_more_adisc(vport);
2547                         } else {
2548                                 /* Check if there are more PLOGIs to be sent */
2549                                 lpfc_more_plogi(vport);
2550                                 if (vport->num_disc_nodes == 0) {
2551                                         spin_lock_irq(shost->host_lock);
2552                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2553                                         spin_unlock_irq(shost->host_lock);
2554                                         lpfc_can_disctmo(vport);
2555                                         lpfc_end_rscn(vport);
2556                                 }
2557                         }
2558                 }
2559         }
2560         return;
2561 }
2562
2563 /**
2564  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
2565  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2566  *
2567  * This routine is invoked by the ndlp delayed-function timer to check
2568  * whether there is any pending ELS retry event(s) with the node. If not, it
2569  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2570  * adds the delayed events to the HBA work list and invokes the
2571  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2572  * event. Note that lpfc_nlp_get() is called before posting the event to
2573  * the work list to hold reference count of ndlp so that it guarantees the
2574  * reference to ndlp will still be available when the worker thread gets
2575  * to the event associated with the ndlp.
2576  **/
2577 void
2578 lpfc_els_retry_delay(unsigned long ptr)
2579 {
2580         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2581         struct lpfc_vport *vport = ndlp->vport;
2582         struct lpfc_hba   *phba = vport->phba;
2583         unsigned long flags;
2584         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2585
2586         spin_lock_irqsave(&phba->hbalock, flags);
2587         if (!list_empty(&evtp->evt_listp)) {
2588                 spin_unlock_irqrestore(&phba->hbalock, flags);
2589                 return;
2590         }
2591
2592         /* We need to hold the node by incrementing the reference
2593          * count until the queued work is done
2594          */
2595         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2596         if (evtp->evt_arg1) {
2597                 evtp->evt = LPFC_EVT_ELS_RETRY;
2598                 list_add_tail(&evtp->evt_listp, &phba->work_list);
2599                 lpfc_worker_wake_up(phba);
2600         }
2601         spin_unlock_irqrestore(&phba->hbalock, flags);
2602         return;
2603 }
2604
2605 /**
2606  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
2607  * @ndlp: pointer to a node-list data structure.
2608  *
2609  * This routine is the worker-thread handler for processing the @ndlp delayed
2610  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2611  * the last ELS command from the associated ndlp and invokes the proper ELS
2612  * function according to the delayed ELS command to retry the command.
2613  **/
2614 void
2615 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2616 {
2617         struct lpfc_vport *vport = ndlp->vport;
2618         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2619         uint32_t cmd, did, retry;
2620
2621         spin_lock_irq(shost->host_lock);
2622         did = ndlp->nlp_DID;
2623         cmd = ndlp->nlp_last_elscmd;
2624         ndlp->nlp_last_elscmd = 0;
2625
2626         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2627                 spin_unlock_irq(shost->host_lock);
2628                 return;
2629         }
2630
2631         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2632         spin_unlock_irq(shost->host_lock);
2633         /*
2634          * If a discovery event readded nlp_delayfunc after timer
2635          * firing and before processing the timer, cancel the
2636          * nlp_delayfunc.
2637          */
2638         del_timer_sync(&ndlp->nlp_delayfunc);
2639         retry = ndlp->nlp_retry;
2640         ndlp->nlp_retry = 0;
2641
2642         switch (cmd) {
2643         case ELS_CMD_FLOGI:
2644                 lpfc_issue_els_flogi(vport, ndlp, retry);
2645                 break;
2646         case ELS_CMD_PLOGI:
2647                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2648                         ndlp->nlp_prev_state = ndlp->nlp_state;
2649                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2650                 }
2651                 break;
2652         case ELS_CMD_ADISC:
2653                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2654                         ndlp->nlp_prev_state = ndlp->nlp_state;
2655                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2656                 }
2657                 break;
2658         case ELS_CMD_PRLI:
2659                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2660                         ndlp->nlp_prev_state = ndlp->nlp_state;
2661                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2662                 }
2663                 break;
2664         case ELS_CMD_LOGO:
2665                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2666                         ndlp->nlp_prev_state = ndlp->nlp_state;
2667                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2668                 }
2669                 break;
2670         case ELS_CMD_FDISC:
2671                 lpfc_issue_els_fdisc(vport, ndlp, retry);
2672                 break;
2673         }
2674         return;
2675 }
2676
2677 /**
2678  * lpfc_els_retry - Make retry decision on an els command iocb
2679  * @phba: pointer to lpfc hba data structure.
2680  * @cmdiocb: pointer to lpfc command iocb data structure.
2681  * @rspiocb: pointer to lpfc response iocb data structure.
2682  *
2683  * This routine makes a retry decision on an ELS command IOCB, which has
2684  * failed. The following ELS IOCBs use this function for retrying the command
2685  * when previously issued command responsed with error status: FLOGI, PLOGI,
2686  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2687  * returned error status, it makes the decision whether a retry shall be
2688  * issued for the command, and whether a retry shall be made immediately or
2689  * delayed. In the former case, the corresponding ELS command issuing-function
2690  * is called to retry the command. In the later case, the ELS command shall
2691  * be posted to the ndlp delayed event and delayed function timer set to the
2692  * ndlp for the delayed command issusing.
2693  *
2694  * Return code
2695  *   0 - No retry of els command is made
2696  *   1 - Immediate or delayed retry of els command is made
2697  **/
2698 static int
2699 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2700                struct lpfc_iocbq *rspiocb)
2701 {
2702         struct lpfc_vport *vport = cmdiocb->vport;
2703         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2704         IOCB_t *irsp = &rspiocb->iocb;
2705         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2706         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2707         uint32_t *elscmd;
2708         struct ls_rjt stat;
2709         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2710         int logerr = 0;
2711         uint32_t cmd = 0;
2712         uint32_t did;
2713
2714
2715         /* Note: context2 may be 0 for internal driver abort
2716          * of delays ELS command.
2717          */
2718
2719         if (pcmd && pcmd->virt) {
2720                 elscmd = (uint32_t *) (pcmd->virt);
2721                 cmd = *elscmd++;
2722         }
2723
2724         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2725                 did = ndlp->nlp_DID;
2726         else {
2727                 /* We should only hit this case for retrying PLOGI */
2728                 did = irsp->un.elsreq64.remoteID;
2729                 ndlp = lpfc_findnode_did(vport, did);
2730                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2731                     && (cmd != ELS_CMD_PLOGI))
2732                         return 1;
2733         }
2734
2735         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2736                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2737                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2738
2739         switch (irsp->ulpStatus) {
2740         case IOSTAT_FCP_RSP_ERROR:
2741         case IOSTAT_REMOTE_STOP:
2742                 break;
2743
2744         case IOSTAT_LOCAL_REJECT:
2745                 switch ((irsp->un.ulpWord[4] & 0xff)) {
2746                 case IOERR_LOOP_OPEN_FAILURE:
2747                         if (cmd == ELS_CMD_FLOGI) {
2748                                 if (PCI_DEVICE_ID_HORNET ==
2749                                         phba->pcidev->device) {
2750                                         phba->fc_topology = TOPOLOGY_LOOP;
2751                                         phba->pport->fc_myDID = 0;
2752                                         phba->alpa_map[0] = 0;
2753                                         phba->alpa_map[1] = 0;
2754                                 }
2755                         }
2756                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2757                                 delay = 1000;
2758                         retry = 1;
2759                         break;
2760
2761                 case IOERR_ILLEGAL_COMMAND:
2762                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2763                                          "0124 Retry illegal cmd x%x "
2764                                          "retry:x%x delay:x%x\n",
2765                                          cmd, cmdiocb->retry, delay);
2766                         retry = 1;
2767                         /* All command's retry policy */
2768                         maxretry = 8;
2769                         if (cmdiocb->retry > 2)
2770                                 delay = 1000;
2771                         break;
2772
2773                 case IOERR_NO_RESOURCES:
2774                         logerr = 1; /* HBA out of resources */
2775                         retry = 1;
2776                         if (cmdiocb->retry > 100)
2777                                 delay = 100;
2778                         maxretry = 250;
2779                         break;
2780
2781                 case IOERR_ILLEGAL_FRAME:
2782                         delay = 100;
2783                         retry = 1;
2784                         break;
2785
2786                 case IOERR_SEQUENCE_TIMEOUT:
2787                 case IOERR_INVALID_RPI:
2788                         retry = 1;
2789                         break;
2790                 }
2791                 break;
2792
2793         case IOSTAT_NPORT_RJT:
2794         case IOSTAT_FABRIC_RJT:
2795                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2796                         retry = 1;
2797                         break;
2798                 }
2799                 break;
2800
2801         case IOSTAT_NPORT_BSY:
2802         case IOSTAT_FABRIC_BSY:
2803                 logerr = 1; /* Fabric / Remote NPort out of resources */
2804                 retry = 1;
2805                 break;
2806
2807         case IOSTAT_LS_RJT:
2808                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2809                 /* Added for Vendor specifc support
2810                  * Just keep retrying for these Rsn / Exp codes
2811                  */
2812                 switch (stat.un.b.lsRjtRsnCode) {
2813                 case LSRJT_UNABLE_TPC:
2814                         if (stat.un.b.lsRjtRsnCodeExp ==
2815                             LSEXP_CMD_IN_PROGRESS) {
2816                                 if (cmd == ELS_CMD_PLOGI) {
2817                                         delay = 1000;
2818                                         maxretry = 48;
2819                                 }
2820                                 retry = 1;
2821                                 break;
2822                         }
2823                         if (stat.un.b.lsRjtRsnCodeExp ==
2824                             LSEXP_CANT_GIVE_DATA) {
2825                                 if (cmd == ELS_CMD_PLOGI) {
2826                                         delay = 1000;
2827                                         maxretry = 48;
2828                                 }
2829                                 retry = 1;
2830                                 break;
2831                         }
2832                         if (cmd == ELS_CMD_PLOGI) {
2833                                 delay = 1000;
2834                                 maxretry = lpfc_max_els_tries + 1;
2835                                 retry = 1;
2836                                 break;
2837                         }
2838                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2839                           (cmd == ELS_CMD_FDISC) &&
2840                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2841                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2842                                                  "0125 FDISC Failed (x%x). "
2843                                                  "Fabric out of resources\n",
2844                                                  stat.un.lsRjtError);
2845                                 lpfc_vport_set_state(vport,
2846                                                      FC_VPORT_NO_FABRIC_RSCS);
2847                         }
2848                         break;
2849
2850                 case LSRJT_LOGICAL_BSY:
2851                         if ((cmd == ELS_CMD_PLOGI) ||
2852                             (cmd == ELS_CMD_PRLI)) {
2853                                 delay = 1000;
2854                                 maxretry = 48;
2855                         } else if (cmd == ELS_CMD_FDISC) {
2856                                 /* FDISC retry policy */
2857                                 maxretry = 48;
2858                                 if (cmdiocb->retry >= 32)
2859                                         delay = 1000;
2860                         }
2861                         retry = 1;
2862                         break;
2863
2864                 case LSRJT_LOGICAL_ERR:
2865                         /* There are some cases where switches return this
2866                          * error when they are not ready and should be returning
2867                          * Logical Busy. We should delay every time.
2868                          */
2869                         if (cmd == ELS_CMD_FDISC &&
2870                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2871                                 maxretry = 3;
2872                                 delay = 1000;
2873                                 retry = 1;
2874                                 break;
2875                         }
2876                 case LSRJT_PROTOCOL_ERR:
2877                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2878                           (cmd == ELS_CMD_FDISC) &&
2879                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2880                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2881                           ) {
2882                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2883                                                  "0122 FDISC Failed (x%x). "
2884                                                  "Fabric Detected Bad WWN\n",
2885                                                  stat.un.lsRjtError);
2886                                 lpfc_vport_set_state(vport,
2887                                                      FC_VPORT_FABRIC_REJ_WWN);
2888                         }
2889                         break;
2890                 }
2891                 break;
2892
2893         case IOSTAT_INTERMED_RSP:
2894         case IOSTAT_BA_RJT:
2895                 break;
2896
2897         default:
2898                 break;
2899         }
2900
2901         if (did == FDMI_DID)
2902                 retry = 1;
2903
2904         if (((cmd == ELS_CMD_FLOGI) || (cmd == ELS_CMD_FDISC)) &&
2905             (phba->fc_topology != TOPOLOGY_LOOP) &&
2906             !lpfc_error_lost_link(irsp)) {
2907                 /* FLOGI retry policy */
2908                 retry = 1;
2909                 /* retry forever */
2910                 maxretry = 0;
2911                 if (cmdiocb->retry >= 100)
2912                         delay = 5000;
2913                 else if (cmdiocb->retry >= 32)
2914                         delay = 1000;
2915         }
2916
2917         cmdiocb->retry++;
2918         if (maxretry && (cmdiocb->retry >= maxretry)) {
2919                 phba->fc_stat.elsRetryExceeded++;
2920                 retry = 0;
2921         }
2922
2923         if ((vport->load_flag & FC_UNLOADING) != 0)
2924                 retry = 0;
2925
2926         if (retry) {
2927                 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
2928                         /* Stop retrying PLOGI and FDISC if in FCF discovery */
2929                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
2930                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2931                                                  "2849 Stop retry ELS command "
2932                                                  "x%x to remote NPORT x%x, "
2933                                                  "Data: x%x x%x\n", cmd, did,
2934                                                  cmdiocb->retry, delay);
2935                                 return 0;
2936                         }
2937                 }
2938
2939                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
2940                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2941                                  "0107 Retry ELS command x%x to remote "
2942                                  "NPORT x%x Data: x%x x%x\n",
2943                                  cmd, did, cmdiocb->retry, delay);
2944
2945                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2946                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2947                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2948                         /* Don't reset timer for no resources */
2949
2950                         /* If discovery / RSCN timer is running, reset it */
2951                         if (timer_pending(&vport->fc_disctmo) ||
2952                             (vport->fc_flag & FC_RSCN_MODE))
2953                                 lpfc_set_disctmo(vport);
2954                 }
2955
2956                 phba->fc_stat.elsXmitRetry++;
2957                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2958                         phba->fc_stat.elsDelayRetry++;
2959                         ndlp->nlp_retry = cmdiocb->retry;
2960
2961                         /* delay is specified in milliseconds */
2962                         mod_timer(&ndlp->nlp_delayfunc,
2963                                 jiffies + msecs_to_jiffies(delay));
2964                         spin_lock_irq(shost->host_lock);
2965                         ndlp->nlp_flag |= NLP_DELAY_TMO;
2966                         spin_unlock_irq(shost->host_lock);
2967
2968                         ndlp->nlp_prev_state = ndlp->nlp_state;
2969                         if (cmd == ELS_CMD_PRLI)
2970                                 lpfc_nlp_set_state(vport, ndlp,
2971                                         NLP_STE_REG_LOGIN_ISSUE);
2972                         else
2973                                 lpfc_nlp_set_state(vport, ndlp,
2974                                         NLP_STE_NPR_NODE);
2975                         ndlp->nlp_last_elscmd = cmd;
2976
2977                         return 1;
2978                 }
2979                 switch (cmd) {
2980                 case ELS_CMD_FLOGI:
2981                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2982                         return 1;
2983                 case ELS_CMD_FDISC:
2984                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2985                         return 1;
2986                 case ELS_CMD_PLOGI:
2987                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2988                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2989                                 lpfc_nlp_set_state(vport, ndlp,
2990                                                    NLP_STE_PLOGI_ISSUE);
2991                         }
2992                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2993                         return 1;
2994                 case ELS_CMD_ADISC:
2995                         ndlp->nlp_prev_state = ndlp->nlp_state;
2996                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2997                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2998                         return 1;
2999                 case ELS_CMD_PRLI:
3000                         ndlp->nlp_prev_state = ndlp->nlp_state;
3001                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3002                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
3003                         return 1;
3004                 case ELS_CMD_LOGO:
3005                         ndlp->nlp_prev_state = ndlp->nlp_state;
3006                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3007                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
3008                         return 1;
3009                 }
3010         }
3011         /* No retry ELS command <elsCmd> to remote NPORT <did> */
3012         if (logerr) {
3013                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3014                          "0137 No retry ELS command x%x to remote "
3015                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
3016                          cmd, did, irsp->ulpStatus,
3017                          irsp->un.ulpWord[4]);
3018         }
3019         else {
3020                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3021                          "0108 No retry ELS command x%x to remote "
3022                          "NPORT x%x Retried:%d Error:x%x/%x\n",
3023                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
3024                          irsp->un.ulpWord[4]);
3025         }
3026         return 0;
3027 }
3028
3029 /**
3030  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
3031  * @phba: pointer to lpfc hba data structure.
3032  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3033  *
3034  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3035  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3036  * checks to see whether there is a lpfc DMA buffer associated with the
3037  * response of the command IOCB. If so, it will be released before releasing
3038  * the lpfc DMA buffer associated with the IOCB itself.
3039  *
3040  * Return code
3041  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3042  **/
3043 static int
3044 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3045 {
3046         struct lpfc_dmabuf *buf_ptr;
3047
3048         /* Free the response before processing the command. */
3049         if (!list_empty(&buf_ptr1->list)) {
3050                 list_remove_head(&buf_ptr1->list, buf_ptr,
3051                                  struct lpfc_dmabuf,
3052                                  list);
3053                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3054                 kfree(buf_ptr);
3055         }
3056         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3057         kfree(buf_ptr1);
3058         return 0;
3059 }
3060
3061 /**
3062  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
3063  * @phba: pointer to lpfc hba data structure.
3064  * @buf_ptr: pointer to the lpfc dma buffer data structure.
3065  *
3066  * This routine releases the lpfc Direct Memory Access (DMA) buffer
3067  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3068  * pool.
3069  *
3070  * Return code
3071  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3072  **/
3073 static int
3074 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3075 {
3076         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3077         kfree(buf_ptr);
3078         return 0;
3079 }
3080
3081 /**
3082  * lpfc_els_free_iocb - Free a command iocb and its associated resources
3083  * @phba: pointer to lpfc hba data structure.
3084  * @elsiocb: pointer to lpfc els command iocb data structure.
3085  *
3086  * This routine frees a command IOCB and its associated resources. The
3087  * command IOCB data structure contains the reference to various associated
3088  * resources, these fields must be set to NULL if the associated reference
3089  * not present:
3090  *   context1 - reference to ndlp
3091  *   context2 - reference to cmd
3092  *   context2->next - reference to rsp
3093  *   context3 - reference to bpl
3094  *
3095  * It first properly decrements the reference count held on ndlp for the
3096  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3097  * set, it invokes the lpfc_els_free_data() routine to release the Direct
3098  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3099  * adds the DMA buffer the @phba data structure for the delayed release.
3100  * If reference to the Buffer Pointer List (BPL) is present, the
3101  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3102  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3103  * invoked to release the IOCB data structure back to @phba IOCBQ list.
3104  *
3105  * Return code
3106  *   0 - Success (currently, always return 0)
3107  **/
3108 int
3109 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
3110 {
3111         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
3112         struct lpfc_nodelist *ndlp;
3113
3114         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3115         if (ndlp) {
3116                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3117                         lpfc_nlp_put(ndlp);
3118
3119                         /* If the ndlp is not being used by another discovery
3120                          * thread, free it.
3121                          */
3122                         if (!lpfc_nlp_not_used(ndlp)) {
3123                                 /* If ndlp is being used by another discovery
3124                                  * thread, just clear NLP_DEFER_RM
3125                                  */
3126                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3127                         }
3128                 }
3129                 else
3130                         lpfc_nlp_put(ndlp);
3131                 elsiocb->context1 = NULL;
3132         }
3133         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
3134         if (elsiocb->context2) {
3135                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3136                         /* Firmware could still be in progress of DMAing
3137                          * payload, so don't free data buffer till after
3138                          * a hbeat.
3139                          */
3140                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3141                         buf_ptr = elsiocb->context2;
3142                         elsiocb->context2 = NULL;
3143                         if (buf_ptr) {
3144                                 buf_ptr1 = NULL;
3145                                 spin_lock_irq(&phba->hbalock);
3146                                 if (!list_empty(&buf_ptr->list)) {
3147                                         list_remove_head(&buf_ptr->list,
3148                                                 buf_ptr1, struct lpfc_dmabuf,
3149                                                 list);
3150                                         INIT_LIST_HEAD(&buf_ptr1->list);
3151                                         list_add_tail(&buf_ptr1->list,
3152                                                 &phba->elsbuf);
3153                                         phba->elsbuf_cnt++;
3154                                 }
3155                                 INIT_LIST_HEAD(&buf_ptr->list);
3156                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3157                                 phba->elsbuf_cnt++;
3158                                 spin_unlock_irq(&phba->hbalock);
3159                         }
3160                 } else {
3161                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3162                         lpfc_els_free_data(phba, buf_ptr1);
3163                 }
3164         }
3165
3166         if (elsiocb->context3) {
3167                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
3168                 lpfc_els_free_bpl(phba, buf_ptr);
3169         }
3170         lpfc_sli_release_iocbq(phba, elsiocb);
3171         return 0;
3172 }
3173
3174 /**
3175  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
3176  * @phba: pointer to lpfc hba data structure.
3177  * @cmdiocb: pointer to lpfc command iocb data structure.
3178  * @rspiocb: pointer to lpfc response iocb data structure.
3179  *
3180  * This routine is the completion callback function to the Logout (LOGO)
3181  * Accept (ACC) Response ELS command. This routine is invoked to indicate
3182  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3183  * release the ndlp if it has the last reference remaining (reference count
3184  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3185  * field to NULL to inform the following lpfc_els_free_iocb() routine no
3186  * ndlp reference count needs to be decremented. Otherwise, the ndlp
3187  * reference use-count shall be decremented by the lpfc_els_free_iocb()
3188  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3189  * IOCB data structure.
3190  **/
3191 static void
3192 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3193                        struct lpfc_iocbq *rspiocb)
3194 {
3195         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3196         struct lpfc_vport *vport = cmdiocb->vport;
3197         IOCB_t *irsp;
3198
3199         irsp = &rspiocb->iocb;
3200         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3201                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
3202                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
3203         /* ACC to LOGO completes to NPort <nlp_DID> */
3204         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3205                          "0109 ACC to LOGO completes to NPort x%x "
3206                          "Data: x%x x%x x%x\n",
3207                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3208                          ndlp->nlp_rpi);
3209
3210         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3211                 /* NPort Recovery mode or node is just allocated */
3212                 if (!lpfc_nlp_not_used(ndlp)) {
3213                         /* If the ndlp is being used by another discovery
3214                          * thread, just unregister the RPI.
3215                          */
3216                         lpfc_unreg_rpi(vport, ndlp);
3217                 } else {
3218                         /* Indicate the node has already released, should
3219                          * not reference to it from within lpfc_els_free_iocb.
3220                          */
3221                         cmdiocb->context1 = NULL;
3222                 }
3223         }
3224         lpfc_els_free_iocb(phba, cmdiocb);
3225         return;
3226 }
3227
3228 /**
3229  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
3230  * @phba: pointer to lpfc hba data structure.
3231  * @pmb: pointer to the driver internal queue element for mailbox command.
3232  *
3233  * This routine is the completion callback function for unregister default
3234  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3235  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3236  * decrements the ndlp reference count held for this completion callback
3237  * function. After that, it invokes the lpfc_nlp_not_used() to check
3238  * whether there is only one reference left on the ndlp. If so, it will
3239  * perform one more decrement and trigger the release of the ndlp.
3240  **/
3241 void
3242 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3243 {
3244         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3245         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3246
3247         /*
3248          * This routine is used to register and unregister in previous SLI
3249          * modes.
3250          */
3251         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
3252             (phba->sli_rev == LPFC_SLI_REV4))
3253                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
3254
3255         pmb->context1 = NULL;
3256         pmb->context2 = NULL;
3257
3258         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3259         kfree(mp);
3260         mempool_free(pmb, phba->mbox_mem_pool);
3261         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3262                 lpfc_nlp_put(ndlp);
3263                 /* This is the end of the default RPI cleanup logic for this
3264                  * ndlp. If no other discovery threads are using this ndlp.
3265                  * we should free all resources associated with it.
3266                  */
3267                 lpfc_nlp_not_used(ndlp);
3268         }
3269
3270         return;
3271 }
3272
3273 /**
3274  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
3275  * @phba: pointer to lpfc hba data structure.
3276  * @cmdiocb: pointer to lpfc command iocb data structure.
3277  * @rspiocb: pointer to lpfc response iocb data structure.
3278  *
3279  * This routine is the completion callback function for ELS Response IOCB
3280  * command. In normal case, this callback function just properly sets the
3281  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3282  * field in the command IOCB is not NULL, the referred mailbox command will
3283  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3284  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3285  * link down event occurred during the discovery, the lpfc_nlp_not_used()
3286  * routine shall be invoked trying to release the ndlp if no other threads
3287  * are currently referring it.
3288  **/
3289 static void
3290 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3291                   struct lpfc_iocbq *rspiocb)
3292 {
3293         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3294         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3295         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
3296         IOCB_t  *irsp;
3297         uint8_t *pcmd;
3298         LPFC_MBOXQ_t *mbox = NULL;
3299         struct lpfc_dmabuf *mp = NULL;
3300         uint32_t ls_rjt = 0;
3301
3302         irsp = &rspiocb->iocb;
3303
3304         if (cmdiocb->context_un.mbox)
3305                 mbox = cmdiocb->context_un.mbox;
3306
3307         /* First determine if this is a LS_RJT cmpl. Note, this callback
3308          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3309          */
3310         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3311         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3312             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3313                 /* A LS_RJT associated with Default RPI cleanup has its own
3314                  * separate code path.
3315                  */
3316                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3317                         ls_rjt = 1;
3318         }
3319
3320         /* Check to see if link went down during discovery */
3321         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3322                 if (mbox) {
3323                         mp = (struct lpfc_dmabuf *) mbox->context1;
3324                         if (mp) {
3325                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3326                                 kfree(mp);
3327                         }
3328                         mempool_free(mbox, phba->mbox_mem_pool);
3329                 }
3330                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3331                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3332                         if (lpfc_nlp_not_used(ndlp)) {
3333                                 ndlp = NULL;
3334                                 /* Indicate the node has already released,
3335                                  * should not reference to it from within
3336                                  * the routine lpfc_els_free_iocb.
3337                                  */
3338                                 cmdiocb->context1 = NULL;
3339                         }
3340                 goto out;
3341         }
3342
3343         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3344                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
3345                 irsp->ulpStatus, irsp->un.ulpWord[4],
3346                 cmdiocb->iocb.un.elsreq64.remoteID);
3347         /* ELS response tag <ulpIoTag> completes */
3348         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3349                          "0110 ELS response tag x%x completes "
3350                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3351                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3352                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3353                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3354                          ndlp->nlp_rpi);
3355         if (mbox) {
3356                 if ((rspiocb->iocb.ulpStatus == 0)
3357                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3358                         lpfc_unreg_rpi(vport, ndlp);
3359                         /* Increment reference count to ndlp to hold the
3360                          * reference to ndlp for the callback function.
3361                          */
3362                         mbox->context2 = lpfc_nlp_get(ndlp);
3363                         mbox->vport = vport;
3364                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3365                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3366                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3367                         }
3368                         else {
3369                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3370                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3371                                 lpfc_nlp_set_state(vport, ndlp,
3372                                            NLP_STE_REG_LOGIN_ISSUE);
3373                         }
3374                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3375                             != MBX_NOT_FINISHED)
3376                                 goto out;
3377                         else
3378                                 /* Decrement the ndlp reference count we
3379                                  * set for this failed mailbox command.
3380                                  */
3381                                 lpfc_nlp_put(ndlp);
3382
3383                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
3384                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3385                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
3386                                 "Data: x%x x%x x%x\n",
3387                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3388                                 ndlp->nlp_rpi);
3389
3390                         if (lpfc_nlp_not_used(ndlp)) {
3391                                 ndlp = NULL;
3392                                 /* Indicate node has already been released,
3393                                  * should not reference to it from within
3394                                  * the routine lpfc_els_free_iocb.
3395                                  */
3396                                 cmdiocb->context1 = NULL;
3397                         }
3398                 } else {
3399                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3400                         if (!lpfc_error_lost_link(irsp) &&
3401                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3402                                 if (lpfc_nlp_not_used(ndlp)) {
3403                                         ndlp = NULL;
3404                                         /* Indicate node has already been
3405                                          * released, should not reference
3406                                          * to it from within the routine
3407                                          * lpfc_els_free_iocb.
3408                                          */
3409                                         cmdiocb->context1 = NULL;
3410                                 }
3411                         }
3412                 }
3413                 mp = (struct lpfc_dmabuf *) mbox->context1;
3414                 if (mp) {
3415                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3416                         kfree(mp);
3417                 }
3418                 mempool_free(mbox, phba->mbox_mem_pool);
3419         }
3420 out:
3421         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3422                 spin_lock_irq(shost->host_lock);
3423                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3424                 spin_unlock_irq(shost->host_lock);
3425
3426                 /* If the node is not being used by another discovery thread,
3427                  * and we are sending a reject, we are done with it.
3428                  * Release driver reference count here and free associated
3429                  * resources.
3430                  */
3431                 if (ls_rjt)
3432                         if (lpfc_nlp_not_used(ndlp))
3433                                 /* Indicate node has already been released,
3434                                  * should not reference to it from within
3435                                  * the routine lpfc_els_free_iocb.
3436                                  */
3437                                 cmdiocb->context1 = NULL;
3438         }
3439
3440         lpfc_els_free_iocb(phba, cmdiocb);
3441         return;
3442 }
3443
3444 /**
3445  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
3446  * @vport: pointer to a host virtual N_Port data structure.
3447  * @flag: the els command code to be accepted.
3448  * @oldiocb: pointer to the original lpfc command iocb data structure.
3449  * @ndlp: pointer to a node-list data structure.
3450  * @mbox: pointer to the driver internal queue element for mailbox command.
3451  *
3452  * This routine prepares and issues an Accept (ACC) response IOCB
3453  * command. It uses the @flag to properly set up the IOCB field for the
3454  * specific ACC response command to be issued and invokes the
3455  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3456  * @mbox pointer is passed in, it will be put into the context_un.mbox
3457  * field of the IOCB for the completion callback function to issue the
3458  * mailbox command to the HBA later when callback is invoked.
3459  *
3460  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3461  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3462  * will be stored into the context1 field of the IOCB for the completion
3463  * callback function to the corresponding response ELS IOCB command.
3464  *
3465  * Return code
3466  *   0 - Successfully issued acc response
3467  *   1 - Failed to issue acc response
3468  **/
3469 int
3470 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3471                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3472                  LPFC_MBOXQ_t *mbox)
3473 {
3474         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3475         struct lpfc_hba  *phba = vport->phba;
3476         IOCB_t *icmd;
3477         IOCB_t *oldcmd;
3478         struct lpfc_iocbq *elsiocb;
3479         struct lpfc_sli *psli;
3480         uint8_t *pcmd;
3481         uint16_t cmdsize;
3482         int rc;
3483         ELS_PKT *els_pkt_ptr;
3484
3485         psli = &phba->sli;
3486         oldcmd = &oldiocb->iocb;
3487
3488         switch (flag) {
3489         case ELS_CMD_ACC:
3490                 cmdsize = sizeof(uint32_t);
3491                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3492                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3493                 if (!elsiocb) {
3494                         spin_lock_irq(shost->host_lock);
3495                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3496                         spin_unlock_irq(shost->host_lock);
3497                         return 1;
3498                 }
3499
3500                 icmd = &elsiocb->iocb;
3501                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3502                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3503                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3504                 pcmd += sizeof(uint32_t);
3505
3506                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3507                         "Issue ACC:       did:x%x flg:x%x",
3508                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3509                 break;
3510         case ELS_CMD_PLOGI:
3511                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3512                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3513                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3514                 if (!elsiocb)
3515                         return 1;
3516
3517                 icmd = &elsiocb->iocb;
3518                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3519                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3520
3521                 if (mbox)
3522                         elsiocb->context_un.mbox = mbox;
3523
3524                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3525                 pcmd += sizeof(uint32_t);
3526                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3527
3528                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3529                         "Issue ACC PLOGI: did:x%x flg:x%x",
3530                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3531                 break;
3532         case ELS_CMD_PRLO:
3533                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3534                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3535                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3536                 if (!elsiocb)
3537                         return 1;
3538
3539                 icmd = &elsiocb->iocb;
3540                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3541                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3542
3543                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3544                        sizeof(uint32_t) + sizeof(PRLO));
3545                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3546                 els_pkt_ptr = (ELS_PKT *) pcmd;
3547                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3548
3549                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3550                         "Issue ACC PRLO:  did:x%x flg:x%x",
3551                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3552                 break;
3553         default:
3554                 return 1;
3555         }
3556         /* Xmit ELS ACC response tag <ulpIoTag> */
3557         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3558                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3559                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3560                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3561                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3562                          ndlp->nlp_rpi);
3563         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3564                 spin_lock_irq(shost->host_lock);
3565                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3566                 spin_unlock_irq(shost->host_lock);
3567                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3568         } else {
3569                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3570         }
3571
3572         phba->fc_stat.elsXmitACC++;
3573         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3574         if (rc == IOCB_ERROR) {
3575                 lpfc_els_free_iocb(phba, elsiocb);
3576                 return 1;
3577         }
3578         return 0;
3579 }
3580
3581 /**
3582  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
3583  * @vport: pointer to a virtual N_Port data structure.
3584  * @rejectError:
3585  * @oldiocb: pointer to the original lpfc command iocb data structure.
3586  * @ndlp: pointer to a node-list data structure.
3587  * @mbox: pointer to the driver internal queue element for mailbox command.
3588  *
3589  * This routine prepares and issue an Reject (RJT) response IOCB
3590  * command. If a @mbox pointer is passed in, it will be put into the
3591  * context_un.mbox field of the IOCB for the completion callback function
3592  * to issue to the HBA later.
3593  *
3594  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3595  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3596  * will be stored into the context1 field of the IOCB for the completion
3597  * callback function to the reject response ELS IOCB command.
3598  *
3599  * Return code
3600  *   0 - Successfully issued reject response
3601  *   1 - Failed to issue reject response
3602  **/
3603 int
3604 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3605                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3606                     LPFC_MBOXQ_t *mbox)
3607 {
3608         struct lpfc_hba  *phba = vport->phba;
3609         IOCB_t *icmd;
3610         IOCB_t *oldcmd;
3611         struct lpfc_iocbq *elsiocb;
3612         struct lpfc_sli *psli;
3613         uint8_t *pcmd;
3614         uint16_t cmdsize;
3615         int rc;
3616
3617         psli = &phba->sli;
3618         cmdsize = 2 * sizeof(uint32_t);
3619         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3620                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
3621         if (!elsiocb)
3622                 return 1;
3623
3624         icmd = &elsiocb->iocb;
3625         oldcmd = &oldiocb->iocb;
3626         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3627         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3628
3629         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3630         pcmd += sizeof(uint32_t);
3631         *((uint32_t *) (pcmd)) = rejectError;
3632
3633         if (mbox)
3634                 elsiocb->context_un.mbox = mbox;
3635
3636         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
3637         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3638                          "0129 Xmit ELS RJT x%x response tag x%x "
3639                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3640                          "rpi x%x\n",
3641                          rejectError, elsiocb->iotag,
3642                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3643                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3644         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3645                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3646                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3647
3648         phba->fc_stat.elsXmitLSRJT++;
3649         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3650         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3651
3652         if (rc == IOCB_ERROR) {
3653                 lpfc_els_free_iocb(phba, elsiocb);
3654                 return 1;
3655         }
3656         return 0;
3657 }
3658
3659 /**
3660  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
3661  * @vport: pointer to a virtual N_Port data structure.
3662  * @oldiocb: pointer to the original lpfc command iocb data structure.
3663  * @ndlp: pointer to a node-list data structure.
3664  *
3665  * This routine prepares and issues an Accept (ACC) response to Address
3666  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3667  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3668  *
3669  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3670  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3671  * will be stored into the context1 field of the IOCB for the completion
3672  * callback function to the ADISC Accept response ELS IOCB command.
3673  *
3674  * Return code
3675  *   0 - Successfully issued acc adisc response
3676  *   1 - Failed to issue adisc acc response
3677  **/
3678 int
3679 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3680                        struct lpfc_nodelist *ndlp)
3681 {
3682         struct lpfc_hba  *phba = vport->phba;
3683         ADISC *ap;
3684         IOCB_t *icmd, *oldcmd;
3685         struct lpfc_iocbq *elsiocb;
3686         uint8_t *pcmd;
3687         uint16_t cmdsize;
3688         int rc;
3689
3690         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3691         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3692                                      ndlp->nlp_DID, ELS_CMD_ACC);
3693         if (!elsiocb)
3694                 return 1;
3695
3696         icmd = &elsiocb->iocb;
3697         oldcmd = &oldiocb->iocb;
3698         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3699
3700         /* Xmit ADISC ACC response tag <ulpIoTag> */
3701         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3702                          "0130 Xmit ADISC ACC response iotag x%x xri: "
3703                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3704                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3705                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3706                          ndlp->nlp_rpi);
3707         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3708
3709         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3710         pcmd += sizeof(uint32_t);
3711
3712         ap = (ADISC *) (pcmd);
3713         ap->hardAL_PA = phba->fc_pref_ALPA;
3714         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3715         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3716         ap->DID = be32_to_cpu(vport->fc_myDID);
3717
3718         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3719                 "Issue ACC ADISC: did:x%x flg:x%x",
3720                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3721
3722         phba->fc_stat.elsXmitACC++;
3723         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3724         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3725         if (rc == IOCB_ERROR) {
3726                 lpfc_els_free_iocb(phba, elsiocb);
3727                 return 1;
3728         }
3729         return 0;
3730 }
3731
3732 /**
3733  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
3734  * @vport: pointer to a virtual N_Port data structure.
3735  * @oldiocb: pointer to the original lpfc command iocb data structure.
3736  * @ndlp: pointer to a node-list data structure.
3737  *
3738  * This routine prepares and issues an Accept (ACC) response to Process
3739  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3740  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3741  *
3742  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3743  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3744  * will be stored into the context1 field of the IOCB for the completion
3745  * callback function to the PRLI Accept response ELS IOCB command.
3746  *
3747  * Return code
3748  *   0 - Successfully issued acc prli response
3749  *   1 - Failed to issue acc prli response
3750  **/
3751 int
3752 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3753                       struct lpfc_nodelist *ndlp)
3754 {
3755         struct lpfc_hba  *phba = vport->phba;
3756         PRLI *npr;
3757         lpfc_vpd_t *vpd;
3758         IOCB_t *icmd;
3759         IOCB_t *oldcmd;
3760         struct lpfc_iocbq *elsiocb;
3761         struct lpfc_sli *psli;
3762         uint8_t *pcmd;
3763         uint16_t cmdsize;
3764         int rc;
3765
3766         psli = &phba->sli;
3767
3768         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3769         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3770                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3771         if (!elsiocb)
3772                 return 1;
3773
3774         icmd = &elsiocb->iocb;
3775         oldcmd = &oldiocb->iocb;
3776         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3777         /* Xmit PRLI ACC response tag <ulpIoTag> */
3778         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3779                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3780                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3781                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3782                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3783                          ndlp->nlp_rpi);
3784         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3785
3786         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3787         pcmd += sizeof(uint32_t);
3788
3789         /* For PRLI, remainder of payload is PRLI parameter page */
3790         memset(pcmd, 0, sizeof(PRLI));
3791
3792         npr = (PRLI *) pcmd;
3793         vpd = &phba->vpd;
3794         /*
3795          * If the remote port is a target and our firmware version is 3.20 or
3796          * later, set the following bits for FC-TAPE support.
3797          */
3798         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3799             (vpd->rev.feaLevelHigh >= 0x02)) {
3800                 npr->ConfmComplAllowed = 1;
3801                 npr->Retry = 1;
3802                 npr->TaskRetryIdReq = 1;
3803         }
3804
3805         npr->acceptRspCode = PRLI_REQ_EXECUTED;
3806         npr->estabImagePair = 1;
3807         npr->readXferRdyDis = 1;
3808         npr->ConfmComplAllowed = 1;
3809
3810         npr->prliType = PRLI_FCP_TYPE;
3811         npr->initiatorFunc = 1;
3812
3813         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3814                 "Issue ACC PRLI:  did:x%x flg:x%x",
3815                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3816
3817         phba->fc_stat.elsXmitACC++;
3818         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3819
3820         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3821         if (rc == IOCB_ERROR) {
3822                 lpfc_els_free_iocb(phba, elsiocb);
3823                 return 1;
3824         }
3825         return 0;
3826 }
3827
3828 /**
3829  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
3830  * @vport: pointer to a virtual N_Port data structure.
3831  * @format: rnid command format.
3832  * @oldiocb: pointer to the original lpfc command iocb data structure.
3833  * @ndlp: pointer to a node-list data structure.
3834  *
3835  * This routine issues a Request Node Identification Data (RNID) Accept
3836  * (ACC) response. It constructs the RNID ACC response command according to
3837  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3838  * issue the response. Note that this command does not need to hold the ndlp
3839  * reference count for the callback. So, the ndlp reference count taken by
3840  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3841  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3842  * there is no ndlp reference available.
3843  *
3844  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3845  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3846  * will be stored into the context1 field of the IOCB for the completion
3847  * callback function. However, for the RNID Accept Response ELS command,
3848  * this is undone later by this routine after the IOCB is allocated.
3849  *
3850  * Return code
3851  *   0 - Successfully issued acc rnid response
3852  *   1 - Failed to issue acc rnid response
3853  **/
3854 static int
3855 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3856                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3857 {
3858         struct lpfc_hba  *phba = vport->phba;
3859         RNID *rn;
3860         IOCB_t *icmd, *oldcmd;
3861         struct lpfc_iocbq *elsiocb;
3862         struct lpfc_sli *psli;
3863         uint8_t *pcmd;
3864         uint16_t cmdsize;
3865         int rc;
3866
3867         psli = &phba->sli;
3868         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3869                                         + (2 * sizeof(struct lpfc_name));
3870         if (format)
3871                 cmdsize += sizeof(RNID_TOP_DISC);
3872
3873         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3874                                      ndlp->nlp_DID, ELS_CMD_ACC);
3875         if (!elsiocb)
3876                 return 1;
3877
3878         icmd = &elsiocb->iocb;
3879         oldcmd = &oldiocb->iocb;
3880         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3881         /* Xmit RNID ACC response tag <ulpIoTag> */
3882         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3883                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3884                          elsiocb->iotag, elsiocb->iocb.ulpContext);
3885         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3886         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3887         pcmd += sizeof(uint32_t);
3888
3889         memset(pcmd, 0, sizeof(RNID));
3890         rn = (RNID *) (pcmd);
3891         rn->Format = format;
3892         rn->CommonLen = (2 * sizeof(struct lpfc_name));
3893         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3894         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3895         switch (format) {
3896         case 0:
3897                 rn->SpecificLen = 0;
3898                 break;
3899         case RNID_TOPOLOGY_DISC:
3900                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
3901                 memcpy(&rn->un.topologyDisc.portName,
3902                        &vport->fc_portname, sizeof(struct lpfc_name));
3903                 rn->un.topologyDisc.unitType = RNID_HBA;
3904                 rn->un.topologyDisc.physPort = 0;
3905                 rn->un.topologyDisc.attachedNodes = 0;
3906                 break;
3907         default:
3908                 rn->CommonLen = 0;
3909                 rn->SpecificLen = 0;
3910                 break;
3911         }
3912
3913         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3914                 "Issue ACC RNID:  did:x%x flg:x%x",
3915                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3916
3917         phba->fc_stat.elsXmitACC++;
3918         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3919         lpfc_nlp_put(ndlp);
3920         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3921                                     * it could be freed */
3922
3923         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3924         if (rc == IOCB_ERROR) {
3925                 lpfc_els_free_iocb(phba, elsiocb);
3926                 return 1;
3927         }
3928         return 0;
3929 }
3930
3931 /**
3932  * lpfc_els_rsp_echo_acc - Issue echo acc response
3933  * @vport: pointer to a virtual N_Port data structure.
3934  * @data: pointer to echo data to return in the accept.
3935  * @oldiocb: pointer to the original lpfc command iocb data structure.
3936  * @ndlp: pointer to a node-list data structure.
3937  *
3938  * Return code
3939  *   0 - Successfully issued acc echo response
3940  *   1 - Failed to issue acc echo response
3941  **/
3942 static int
3943 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
3944                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3945 {
3946         struct lpfc_hba  *phba = vport->phba;
3947         struct lpfc_iocbq *elsiocb;
3948         struct lpfc_sli *psli;
3949         uint8_t *pcmd;
3950         uint16_t cmdsize;
3951         int rc;
3952
3953         psli = &phba->sli;
3954         cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
3955
3956         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3957                                      ndlp->nlp_DID, ELS_CMD_ACC);
3958         if (!elsiocb)
3959                 return 1;
3960
3961         elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;    /* Xri */
3962         /* Xmit ECHO ACC response tag <ulpIoTag> */
3963         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3964                          "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
3965                          elsiocb->iotag, elsiocb->iocb.ulpContext);
3966         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3967         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3968         pcmd += sizeof(uint32_t);
3969         memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
3970
3971         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3972                 "Issue ACC ECHO:  did:x%x flg:x%x",
3973                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3974
3975         phba->fc_stat.elsXmitACC++;
3976         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3977         lpfc_nlp_put(ndlp);
3978         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3979                                     * it could be freed */
3980
3981         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3982         if (rc == IOCB_ERROR) {
3983                 lpfc_els_free_iocb(phba, elsiocb);
3984                 return 1;
3985         }
3986         return 0;
3987 }
3988
3989 /**
3990  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
3991  * @vport: pointer to a host virtual N_Port data structure.
3992  *
3993  * This routine issues Address Discover (ADISC) ELS commands to those
3994  * N_Ports which are in node port recovery state and ADISC has not been issued
3995  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3996  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3997  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3998  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3999  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4000  * IOCBs quit for later pick up. On the other hand, after walking through
4001  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4002  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4003  * no more ADISC need to be sent.
4004  *
4005  * Return code
4006  *    The number of N_Ports with adisc issued.
4007  **/
4008 int
4009 lpfc_els_disc_adisc(struct lpfc_vport *vport)
4010 {
4011         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4012         struct lpfc_nodelist *ndlp, *next_ndlp;
4013         int sentadisc = 0;
4014
4015         /* go thru NPR nodes and issue any remaining ELS ADISCs */
4016         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4017                 if (!NLP_CHK_NODE_ACT(ndlp))
4018                         continue;
4019                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4020                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4021                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
4022                         spin_lock_irq(shost->host_lock);
4023                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4024                         spin_unlock_irq(shost->host_lock);
4025                         ndlp->nlp_prev_state = ndlp->nlp_state;
4026                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4027                         lpfc_issue_els_adisc(vport, ndlp, 0);
4028                         sentadisc++;
4029                         vport->num_disc_nodes++;
4030                         if (vport->num_disc_nodes >=
4031                             vport->cfg_discovery_threads) {
4032                                 spin_lock_irq(shost->host_lock);
4033                                 vport->fc_flag |= FC_NLP_MORE;
4034                                 spin_unlock_irq(shost->host_lock);
4035                                 break;
4036                         }
4037                 }
4038         }
4039         if (sentadisc == 0) {
4040                 spin_lock_irq(shost->host_lock);
4041                 vport->fc_flag &= ~FC_NLP_MORE;
4042                 spin_unlock_irq(shost->host_lock);
4043         }
4044         return sentadisc;
4045 }
4046
4047 /**
4048  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
4049  * @vport: pointer to a host virtual N_Port data structure.
4050  *
4051  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4052  * which are in node port recovery state, with a @vport. Each time an ELS
4053  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4054  * the per @vport number of discover count (num_disc_nodes) shall be
4055  * incremented. If the num_disc_nodes reaches a pre-configured threshold
4056  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4057  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4058  * later pick up. On the other hand, after walking through all the ndlps with
4059  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4060  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4061  * PLOGI need to be sent.
4062  *
4063  * Return code
4064  *   The number of N_Ports with plogi issued.
4065  **/
4066 int
4067 lpfc_els_disc_plogi(struct lpfc_vport *vport)
4068 {
4069         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4070         struct lpfc_nodelist *ndlp, *next_ndlp;
4071         int sentplogi = 0;
4072
4073         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
4074         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4075                 if (!NLP_CHK_NODE_ACT(ndlp))
4076                         continue;
4077                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4078                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4079                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4080                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4081                         ndlp->nlp_prev_state = ndlp->nlp_state;
4082                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4083                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4084                         sentplogi++;
4085                         vport->num_disc_nodes++;
4086                         if (vport->num_disc_nodes >=
4087                             vport->cfg_discovery_threads) {
4088                                 spin_lock_irq(shost->host_lock);
4089                                 vport->fc_flag |= FC_NLP_MORE;
4090                                 spin_unlock_irq(shost->host_lock);
4091                                 break;
4092                         }
4093                 }
4094         }
4095         if (sentplogi) {
4096                 lpfc_set_disctmo(vport);
4097         }
4098         else {
4099                 spin_lock_irq(shost->host_lock);
4100                 vport->fc_flag &= ~FC_NLP_MORE;
4101                 spin_unlock_irq(shost->host_lock);
4102         }
4103         return sentplogi;
4104 }
4105
4106 /**
4107  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
4108  * @vport: pointer to a host virtual N_Port data structure.
4109  *
4110  * This routine cleans up any Registration State Change Notification
4111  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
4112  * @vport together with the host_lock is used to prevent multiple thread
4113  * trying to access the RSCN array on a same @vport at the same time.
4114  **/
4115 void
4116 lpfc_els_flush_rscn(struct lpfc_vport *vport)
4117 {
4118         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4119         struct lpfc_hba  *phba = vport->phba;
4120         int i;
4121
4122         spin_lock_irq(shost->host_lock);
4123         if (vport->fc_rscn_flush) {
4124                 /* Another thread is walking fc_rscn_id_list on this vport */
4125                 spin_unlock_irq(shost->host_lock);
4126                 return;
4127         }
4128         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
4129         vport->fc_rscn_flush = 1;
4130         spin_unlock_irq(shost->host_lock);
4131
4132         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4133                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
4134                 vport->fc_rscn_id_list[i] = NULL;
4135         }
4136         spin_lock_irq(shost->host_lock);
4137         vport->fc_rscn_id_cnt = 0;
4138         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
4139         spin_unlock_irq(shost->host_lock);
4140         lpfc_can_disctmo(vport);
4141         /* Indicate we are done walking this fc_rscn_id_list */
4142         vport->fc_rscn_flush = 0;
4143 }
4144
4145 /**
4146  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
4147  * @vport: pointer to a host virtual N_Port data structure.
4148  * @did: remote destination port identifier.
4149  *
4150  * This routine checks whether there is any pending Registration State
4151  * Configuration Notification (RSCN) to a @did on @vport.
4152  *
4153  * Return code
4154  *   None zero - The @did matched with a pending rscn
4155  *   0 - not able to match @did with a pending rscn
4156  **/
4157 int
4158 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
4159 {
4160         D_ID ns_did;
4161         D_ID rscn_did;
4162         uint32_t *lp;
4163         uint32_t payload_len, i;
4164         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4165
4166         ns_did.un.word = did;
4167
4168         /* Never match fabric nodes for RSCNs */
4169         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
4170                 return 0;
4171
4172         /* If we are doing a FULL RSCN rediscovery, match everything */
4173         if (vport->fc_flag & FC_RSCN_DISCOVERY)
4174                 return did;
4175
4176         spin_lock_irq(shost->host_lock);
4177         if (vport->fc_rscn_flush) {
4178                 /* Another thread is walking fc_rscn_id_list on this vport */
4179                 spin_unlock_irq(shost->host_lock);
4180                 return 0;
4181         }
4182         /* Indicate we are walking fc_rscn_id_list on this vport */
4183         vport->fc_rscn_flush = 1;
4184         spin_unlock_irq(shost->host_lock);
4185         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4186                 lp = vport->fc_rscn_id_list[i]->virt;
4187                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4188                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
4189                 while (payload_len) {
4190                         rscn_did.un.word = be32_to_cpu(*lp++);
4191                         payload_len -= sizeof(uint32_t);
4192                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
4193                         case RSCN_ADDRESS_FORMAT_PORT:
4194                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4195                                     && (ns_did.un.b.area == rscn_did.un.b.area)
4196                                     && (ns_did.un.b.id == rscn_did.un.b.id))
4197                                         goto return_did_out;
4198                                 break;
4199                         case RSCN_ADDRESS_FORMAT_AREA:
4200                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4201                                     && (ns_did.un.b.area == rscn_did.un.b.area))
4202                                         goto return_did_out;
4203                                 break;
4204                         case RSCN_ADDRESS_FORMAT_DOMAIN:
4205                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
4206                                         goto return_did_out;
4207                                 break;
4208                         case RSCN_ADDRESS_FORMAT_FABRIC:
4209                                 goto return_did_out;
4210                         }
4211                 }
4212         }
4213         /* Indicate we are done with walking fc_rscn_id_list on this vport */
4214         vport->fc_rscn_flush = 0;
4215         return 0;
4216 return_did_out:
4217         /* Indicate we are done with walking fc_rscn_id_list on this vport */
4218         vport->fc_rscn_flush = 0;
4219         return did;
4220 }
4221
4222 /**
4223  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
4224  * @vport: pointer to a host virtual N_Port data structure.
4225  *
4226  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
4227  * state machine for a @vport's nodes that are with pending RSCN (Registration
4228  * State Change Notification).
4229  *
4230  * Return code
4231  *   0 - Successful (currently alway return 0)
4232  **/
4233 static int
4234 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
4235 {
4236         struct lpfc_nodelist *ndlp = NULL;
4237
4238         /* Move all affected nodes by pending RSCNs to NPR state. */
4239         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4240                 if (!NLP_CHK_NODE_ACT(ndlp) ||
4241                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
4242                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
4243                         continue;
4244                 lpfc_disc_state_machine(vport, ndlp, NULL,
4245                                         NLP_EVT_DEVICE_RECOVERY);
4246                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
4247         }
4248         return 0;
4249 }
4250
4251 /**
4252  * lpfc_send_rscn_event - Send an RSCN event to management application
4253  * @vport: pointer to a host virtual N_Port data structure.
4254  * @cmdiocb: pointer to lpfc command iocb data structure.
4255  *
4256  * lpfc_send_rscn_event sends an RSCN netlink event to management
4257  * applications.
4258  */
4259 static void
4260 lpfc_send_rscn_event(struct lpfc_vport *vport,
4261                 struct lpfc_iocbq *cmdiocb)
4262 {
4263         struct lpfc_dmabuf *pcmd;
4264         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4265         uint32_t *payload_ptr;
4266         uint32_t payload_len;
4267         struct lpfc_rscn_event_header *rscn_event_data;
4268
4269         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4270         payload_ptr = (uint32_t *) pcmd->virt;
4271         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4272
4273         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4274                 payload_len, GFP_KERNEL);
4275         if (!rscn_event_data) {
4276                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4277                         "0147 Failed to allocate memory for RSCN event\n");
4278                 return;
4279         }
4280         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4281         rscn_event_data->payload_length = payload_len;
4282         memcpy(rscn_event_data->rscn_payload, payload_ptr,
4283                 payload_len);
4284
4285         fc_host_post_vendor_event(shost,
4286                 fc_get_event_number(),
4287                 sizeof(struct lpfc_els_event_header) + payload_len,
4288                 (char *)rscn_event_data,
4289                 LPFC_NL_VENDOR_ID);
4290
4291         kfree(rscn_event_data);
4292 }
4293
4294 /**
4295  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
4296  * @vport: pointer to a host virtual N_Port data structure.
4297  * @cmdiocb: pointer to lpfc command iocb data structure.
4298  * @ndlp: pointer to a node-list data structure.
4299  *
4300  * This routine processes an unsolicited RSCN (Registration State Change
4301  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4302  * to invoke fc_host_post_event() routine to the FC transport layer. If the
4303  * discover state machine is about to begin discovery, it just accepts the
4304  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4305  * contains N_Port IDs for other vports on this HBA, it just accepts the
4306  * RSCN and ignore processing it. If the state machine is in the recovery
4307  * state, the fc_rscn_id_list of this @vport is walked and the
4308  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4309  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4310  * routine is invoked to handle the RSCN event.
4311  *
4312  * Return code
4313  *   0 - Just sent the acc response
4314  *   1 - Sent the acc response and waited for name server completion
4315  **/
4316 static int
4317 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4318                   struct lpfc_nodelist *ndlp)
4319 {
4320         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4321         struct lpfc_hba  *phba = vport->phba;
4322         struct lpfc_dmabuf *pcmd;
4323         uint32_t *lp, *datap;
4324         IOCB_t *icmd;
4325         uint32_t payload_len, length, nportid, *cmd;
4326         int rscn_cnt;
4327         int rscn_id = 0, hba_id = 0;
4328         int i;
4329
4330         icmd = &cmdiocb->iocb;
4331         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4332         lp = (uint32_t *) pcmd->virt;
4333
4334         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4335         payload_len -= sizeof(uint32_t);        /* take off word 0 */
4336         /* RSCN received */
4337         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4338                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
4339                          vport->fc_flag, payload_len, *lp,
4340                          vport->fc_rscn_id_cnt);
4341
4342         /* Send an RSCN event to the management application */
4343         lpfc_send_rscn_event(vport, cmdiocb);
4344
4345         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
4346                 fc_host_post_event(shost, fc_get_event_number(),
4347                         FCH_EVT_RSCN, lp[i]);
4348
4349         /* If we are about to begin discovery, just ACC the RSCN.
4350          * Discovery processing will satisfy it.
4351          */
4352         if (vport->port_state <= LPFC_NS_QRY) {
4353                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4354                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4355                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4356
4357                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4358                 return 0;
4359         }
4360
4361         /* If this RSCN just contains NPortIDs for other vports on this HBA,
4362          * just ACC and ignore it.
4363          */
4364         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4365                 !(vport->cfg_peer_port_login)) {
4366                 i = payload_len;
4367                 datap = lp;
4368                 while (i > 0) {
4369                         nportid = *datap++;
4370                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4371                         i -= sizeof(uint32_t);
4372                         rscn_id++;
4373                         if (lpfc_find_vport_by_did(phba, nportid))
4374                                 hba_id++;
4375                 }
4376                 if (rscn_id == hba_id) {
4377                         /* ALL NPortIDs in RSCN are on HBA */
4378                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4379                                          "0219 Ignore RSCN "
4380                                          "Data: x%x x%x x%x x%x\n",
4381                                          vport->fc_flag, payload_len,
4382                                          *lp, vport->fc_rscn_id_cnt);
4383                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4384                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
4385                                 ndlp->nlp_DID, vport->port_state,
4386                                 ndlp->nlp_flag);
4387
4388                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
4389                                 ndlp, NULL);
4390                         return 0;
4391                 }
4392         }
4393
4394         spin_lock_irq(shost->host_lock);
4395         if (vport->fc_rscn_flush) {
4396                 /* Another thread is walking fc_rscn_id_list on this vport */
4397                 vport->fc_flag |= FC_RSCN_DISCOVERY;
4398                 spin_unlock_irq(shost->host_lock);
4399                 /* Send back ACC */
4400                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4401                 return 0;
4402         }
4403         /* Indicate we are walking fc_rscn_id_list on this vport */
4404         vport->fc_rscn_flush = 1;
4405         spin_unlock_irq(shost->host_lock);
4406         /* Get the array count after successfully have the token */
4407         rscn_cnt = vport->fc_rscn_id_cnt;
4408         /* If we are already processing an RSCN, save the received
4409          * RSCN payload buffer, cmdiocb->context2 to process later.
4410          */
4411         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4412                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4413                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4414                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4415
4416                 spin_lock_irq(shost->host_lock);
4417                 vport->fc_flag |= FC_RSCN_DEFERRED;
4418                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4419                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4420                         vport->fc_flag |= FC_RSCN_MODE;
4421                         spin_unlock_irq(shost->host_lock);
4422                         if (rscn_cnt) {
4423                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4424                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4425                         }
4426                         if ((rscn_cnt) &&
4427                             (payload_len + length <= LPFC_BPL_SIZE)) {
4428                                 *cmd &= ELS_CMD_MASK;
4429                                 *cmd |= cpu_to_be32(payload_len + length);
4430                                 memcpy(((uint8_t *)cmd) + length, lp,
4431                                        payload_len);
4432                         } else {
4433                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4434                                 vport->fc_rscn_id_cnt++;
4435                                 /* If we zero, cmdiocb->context2, the calling
4436                                  * routine will not try to free it.
4437                                  */
4438                                 cmdiocb->context2 = NULL;
4439                         }
4440                         /* Deferred RSCN */
4441                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4442                                          "0235 Deferred RSCN "
4443                                          "Data: x%x x%x x%x\n",
4444                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4445                                          vport->port_state);
4446                 } else {
4447                         vport->fc_flag |= FC_RSCN_DISCOVERY;
4448                         spin_unlock_irq(shost->host_lock);
4449                         /* ReDiscovery RSCN */
4450                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4451                                          "0234 ReDiscovery RSCN "
4452                                          "Data: x%x x%x x%x\n",
4453                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4454                                          vport->port_state);
4455                 }
4456                 /* Indicate we are done walking fc_rscn_id_list on this vport */
4457                 vport->fc_rscn_flush = 0;
4458                 /* Send back ACC */
4459                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4460                 /* send RECOVERY event for ALL nodes that match RSCN payload */
4461                 lpfc_rscn_recovery_check(vport);
4462                 spin_lock_irq(shost->host_lock);
4463                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
4464                 spin_unlock_irq(shost->host_lock);
4465                 return 0;
4466         }
4467         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4468                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4469                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4470
4471         spin_lock_irq(shost->host_lock);
4472         vport->fc_flag |= FC_RSCN_MODE;
4473         spin_unlock_irq(shost->host_lock);
4474         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4475         /* Indicate we are done walking fc_rscn_id_list on this vport */
4476         vport->fc_rscn_flush = 0;
4477         /*
4478          * If we zero, cmdiocb->context2, the calling routine will
4479          * not try to free it.
4480          */
4481         cmdiocb->context2 = NULL;
4482         lpfc_set_disctmo(vport);
4483         /* Send back ACC */
4484         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4485         /* send RECOVERY event for ALL nodes that match RSCN payload */
4486         lpfc_rscn_recovery_check(vport);
4487         return lpfc_els_handle_rscn(vport);
4488 }
4489
4490 /**
4491  * lpfc_els_handle_rscn - Handle rscn for a vport
4492  * @vport: pointer to a host virtual N_Port data structure.
4493  *
4494  * This routine handles the Registration State Configuration Notification
4495  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4496  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4497  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4498  * NameServer shall be issued. If CT command to the NameServer fails to be
4499  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4500  * RSCN activities with the @vport.
4501  *
4502  * Return code
4503  *   0 - Cleaned up rscn on the @vport
4504  *   1 - Wait for plogi to name server before proceed
4505  **/
4506 int
4507 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4508 {
4509         struct lpfc_nodelist *ndlp;
4510         struct lpfc_hba *phba = vport->phba;
4511
4512         /* Ignore RSCN if the port is being torn down. */
4513         if (vport->load_flag & FC_UNLOADING) {
4514                 lpfc_els_flush_rscn(vport);
4515                 return 0;
4516         }
4517
4518         /* Start timer for RSCN processing */
4519         lpfc_set_disctmo(vport);
4520
4521         /* RSCN processed */
4522         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4523                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4524                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4525                          vport->port_state);
4526
4527         /* To process RSCN, first compare RSCN data with NameServer */
4528         vport->fc_ns_retry = 0;
4529         vport->num_disc_nodes = 0;
4530
4531         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4532         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4533             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4534                 /* Good ndlp, issue CT Request to NameServer */
4535                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4536                         /* Wait for NameServer query cmpl before we can
4537                            continue */
4538                         return 1;
4539         } else {
4540                 /* If login to NameServer does not exist, issue one */
4541                 /* Good status, issue PLOGI to NameServer */
4542                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4543                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4544                         /* Wait for NameServer login cmpl before we can
4545                            continue */
4546                         return 1;
4547
4548                 if (ndlp) {
4549                         ndlp = lpfc_enable_node(vport, ndlp,
4550                                                 NLP_STE_PLOGI_ISSUE);
4551                         if (!ndlp) {
4552                                 lpfc_els_flush_rscn(vport);
4553                                 return 0;
4554                         }
4555                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4556                 } else {
4557                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4558                         if (!ndlp) {
4559                                 lpfc_els_flush_rscn(vport);
4560                                 return 0;
4561                         }
4562                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
4563                         ndlp->nlp_prev_state = ndlp->nlp_state;
4564                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4565                 }
4566                 ndlp->nlp_type |= NLP_FABRIC;
4567                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4568                 /* Wait for NameServer login cmpl before we can
4569                  * continue
4570                  */
4571                 return 1;
4572         }
4573
4574         lpfc_els_flush_rscn(vport);
4575         return 0;
4576 }
4577
4578 /**
4579  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
4580  * @vport: pointer to a host virtual N_Port data structure.
4581  * @cmdiocb: pointer to lpfc command iocb data structure.
4582  * @ndlp: pointer to a node-list data structure.
4583  *
4584  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4585  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4586  * point topology. As an unsolicited FLOGI should not be received in a loop
4587  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4588  * lpfc_check_sparm() routine is invoked to check the parameters in the
4589  * unsolicited FLOGI. If parameters validation failed, the routine
4590  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4591  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4592  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4593  * will initiate PLOGI. The higher lexicographical value party shall has
4594  * higher priority (as the winning port) and will initiate PLOGI and
4595  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4596  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4597  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4598  *
4599  * Return code
4600  *   0 - Successfully processed the unsolicited flogi
4601  *   1 - Failed to process the unsolicited flogi
4602  **/
4603 static int
4604 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4605                    struct lpfc_nodelist *ndlp)
4606 {
4607         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4608         struct lpfc_hba  *phba = vport->phba;
4609         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4610         uint32_t *lp = (uint32_t *) pcmd->virt;
4611         IOCB_t *icmd = &cmdiocb->iocb;
4612         struct serv_parm *sp;
4613         LPFC_MBOXQ_t *mbox;
4614         struct ls_rjt stat;
4615         uint32_t cmd, did;
4616         int rc;
4617
4618         cmd = *lp++;
4619         sp = (struct serv_parm *) lp;
4620
4621         /* FLOGI received */
4622
4623         lpfc_set_disctmo(vport);
4624
4625         if (phba->fc_topology == TOPOLOGY_LOOP) {
4626                 /* We should never receive a FLOGI in loop mode, ignore it */
4627                 did = icmd->un.elsreq64.remoteID;
4628
4629                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4630                    Loop Mode */
4631                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4632                                  "0113 An FLOGI ELS command x%x was "
4633                                  "received from DID x%x in Loop Mode\n",
4634                                  cmd, did);
4635                 return 1;
4636         }
4637
4638         did = Fabric_DID;
4639
4640         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
4641                 /* For a FLOGI we accept, then if our portname is greater
4642                  * then the remote portname we initiate Nport login.
4643                  */
4644
4645                 rc = memcmp(&vport->fc_portname, &sp->portName,
4646                             sizeof(struct lpfc_name));
4647
4648                 if (!rc) {
4649                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4650                         if (!mbox)
4651                                 return 1;
4652
4653                         lpfc_linkdown(phba);
4654                         lpfc_init_link(phba, mbox,
4655                                        phba->cfg_topology,
4656                                        phba->cfg_link_speed);
4657                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
4658                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4659                         mbox->vport = vport;
4660                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4661                         lpfc_set_loopback_flag(phba);
4662                         if (rc == MBX_NOT_FINISHED) {
4663                                 mempool_free(mbox, phba->mbox_mem_pool);
4664                         }
4665                         return 1;
4666                 } else if (rc > 0) {    /* greater than */
4667                         spin_lock_irq(shost->host_lock);
4668                         vport->fc_flag |= FC_PT2PT_PLOGI;
4669                         spin_unlock_irq(shost->host_lock);
4670                 }
4671                 spin_lock_irq(shost->host_lock);
4672                 vport->fc_flag |= FC_PT2PT;
4673                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4674                 spin_unlock_irq(shost->host_lock);
4675         } else {
4676                 /* Reject this request because invalid parameters */
4677                 stat.un.b.lsRjtRsvd0 = 0;
4678                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4679                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4680                 stat.un.b.vendorUnique = 0;
4681                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4682                         NULL);
4683                 return 1;
4684         }
4685
4686         /* Send back ACC */
4687         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4688
4689         return 0;
4690 }
4691
4692 /**
4693  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
4694  * @vport: pointer to a host virtual N_Port data structure.
4695  * @cmdiocb: pointer to lpfc command iocb data structure.
4696  * @ndlp: pointer to a node-list data structure.
4697  *
4698  * This routine processes Request Node Identification Data (RNID) IOCB
4699  * received as an ELS unsolicited event. Only when the RNID specified format
4700  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4701  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4702  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4703  * rejected by invoking the lpfc_els_rsp_reject() routine.
4704  *
4705  * Return code
4706  *   0 - Successfully processed rnid iocb (currently always return 0)
4707  **/
4708 static int
4709 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4710                   struct lpfc_nodelist *ndlp)
4711 {
4712         struct lpfc_dmabuf *pcmd;
4713         uint32_t *lp;
4714         IOCB_t *icmd;
4715         RNID *rn;
4716         struct ls_rjt stat;
4717         uint32_t cmd, did;
4718
4719         icmd = &cmdiocb->iocb;
4720         did = icmd->un.elsreq64.remoteID;
4721         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4722         lp = (uint32_t *) pcmd->virt;
4723
4724         cmd = *lp++;
4725         rn = (RNID *) lp;
4726
4727         /* RNID received */
4728
4729         switch (rn->Format) {
4730         case 0:
4731         case RNID_TOPOLOGY_DISC:
4732                 /* Send back ACC */
4733                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4734                 break;
4735         default:
4736                 /* Reject this request because format not supported */
4737                 stat.un.b.lsRjtRsvd0 = 0;
4738                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4739                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4740                 stat.un.b.vendorUnique = 0;
4741                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4742                         NULL);
4743         }
4744         return 0;
4745 }
4746
4747 /**
4748  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
4749  * @vport: pointer to a host virtual N_Port data structure.
4750  * @cmdiocb: pointer to lpfc command iocb data structure.
4751  * @ndlp: pointer to a node-list data structure.
4752  *
4753  * Return code
4754  *   0 - Successfully processed echo iocb (currently always return 0)
4755  **/
4756 static int
4757 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4758                   struct lpfc_nodelist *ndlp)
4759 {
4760         uint8_t *pcmd;
4761
4762         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4763
4764         /* skip over first word of echo command to find echo data */
4765         pcmd += sizeof(uint32_t);
4766
4767         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
4768         return 0;
4769 }
4770
4771 /**
4772  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
4773  * @vport: pointer to a host virtual N_Port data structure.
4774  * @cmdiocb: pointer to lpfc command iocb data structure.
4775  * @ndlp: pointer to a node-list data structure.
4776  *
4777  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4778  * received as an ELS unsolicited event. Currently, this function just invokes
4779  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4780  *
4781  * Return code
4782  *   0 - Successfully processed lirr iocb (currently always return 0)
4783  **/
4784 static int
4785 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4786                   struct lpfc_nodelist *ndlp)
4787 {
4788         struct ls_rjt stat;
4789
4790         /* For now, unconditionally reject this command */
4791         stat.un.b.lsRjtRsvd0 = 0;
4792         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4793         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4794         stat.un.b.vendorUnique = 0;
4795         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4796         return 0;
4797 }
4798
4799 /**
4800  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
4801  * @vport: pointer to a host virtual N_Port data structure.
4802  * @cmdiocb: pointer to lpfc command iocb data structure.
4803  * @ndlp: pointer to a node-list data structure.
4804  *
4805  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
4806  * received as an ELS unsolicited event. A request to RRQ shall only
4807  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
4808  * Nx_Port N_Port_ID of the target Exchange is the same as the
4809  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
4810  * not accepted, an LS_RJT with reason code "Unable to perform
4811  * command request" and reason code explanation "Invalid Originator
4812  * S_ID" shall be returned. For now, we just unconditionally accept
4813  * RRQ from the target.
4814  **/
4815 static void
4816 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4817                  struct lpfc_nodelist *ndlp)
4818 {
4819         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4820 }
4821
4822 /**
4823  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
4824  * @phba: pointer to lpfc hba data structure.
4825  * @pmb: pointer to the driver internal queue element for mailbox command.
4826  *
4827  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4828  * mailbox command. This callback function is to actually send the Accept
4829  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4830  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4831  * mailbox command, constructs the RPS response with the link statistics
4832  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4833  * response to the RPS.
4834  *
4835  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4836  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4837  * will be stored into the context1 field of the IOCB for the completion
4838  * callback function to the RPS Accept Response ELS IOCB command.
4839  *
4840  **/
4841 static void
4842 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4843 {
4844         MAILBOX_t *mb;
4845         IOCB_t *icmd;
4846         struct RLS_RSP *rls_rsp;
4847         uint8_t *pcmd;
4848         struct lpfc_iocbq *elsiocb;
4849         struct lpfc_nodelist *ndlp;
4850         uint16_t xri;
4851         uint32_t cmdsize;
4852
4853         mb = &pmb->u.mb;
4854
4855         ndlp = (struct lpfc_nodelist *) pmb->context2;
4856         xri = (uint16_t) ((unsigned long)(pmb->context1));
4857         pmb->context1 = NULL;
4858         pmb->context2 = NULL;
4859
4860         if (mb->mbxStatus) {
4861                 mempool_free(pmb, phba->mbox_mem_pool);
4862                 return;
4863         }
4864
4865         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
4866         mempool_free(pmb, phba->mbox_mem_pool);
4867         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4868                                      lpfc_max_els_tries, ndlp,
4869                                      ndlp->nlp_DID, ELS_CMD_ACC);
4870
4871         /* Decrement the ndlp reference count from previous mbox command */
4872         lpfc_nlp_put(ndlp);
4873
4874         if (!elsiocb)
4875                 return;
4876
4877         icmd = &elsiocb->iocb;
4878         icmd->ulpContext = xri;
4879
4880         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4881         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4882         pcmd += sizeof(uint32_t); /* Skip past command */
4883         rls_rsp = (struct RLS_RSP *)pcmd;
4884
4885         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4886         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4887         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4888         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4889         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4890         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4891
4892         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
4893         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4894                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
4895                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4896                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4897                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4898                          ndlp->nlp_rpi);
4899         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4900         phba->fc_stat.elsXmitACC++;
4901         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
4902                 lpfc_els_free_iocb(phba, elsiocb);
4903 }
4904
4905 /**
4906  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
4907  * @phba: pointer to lpfc hba data structure.
4908  * @pmb: pointer to the driver internal queue element for mailbox command.
4909  *
4910  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4911  * mailbox command. This callback function is to actually send the Accept
4912  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4913  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4914  * mailbox command, constructs the RPS response with the link statistics
4915  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4916  * response to the RPS.
4917  *
4918  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4919  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4920  * will be stored into the context1 field of the IOCB for the completion
4921  * callback function to the RPS Accept Response ELS IOCB command.
4922  *
4923  **/
4924 static void
4925 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4926 {
4927         MAILBOX_t *mb;
4928         IOCB_t *icmd;
4929         RPS_RSP *rps_rsp;
4930         uint8_t *pcmd;
4931         struct lpfc_iocbq *elsiocb;
4932         struct lpfc_nodelist *ndlp;
4933         uint16_t xri, status;
4934         uint32_t cmdsize;
4935
4936         mb = &pmb->u.mb;
4937
4938         ndlp = (struct lpfc_nodelist *) pmb->context2;
4939         xri = (uint16_t) ((unsigned long)(pmb->context1));
4940         pmb->context1 = NULL;
4941         pmb->context2 = NULL;
4942
4943         if (mb->mbxStatus) {
4944                 mempool_free(pmb, phba->mbox_mem_pool);
4945                 return;
4946         }
4947
4948         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4949         mempool_free(pmb, phba->mbox_mem_pool);
4950         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4951                                      lpfc_max_els_tries, ndlp,
4952                                      ndlp->nlp_DID, ELS_CMD_ACC);
4953
4954         /* Decrement the ndlp reference count from previous mbox command */
4955         lpfc_nlp_put(ndlp);
4956
4957         if (!elsiocb)
4958                 return;
4959
4960         icmd = &elsiocb->iocb;
4961         icmd->ulpContext = xri;
4962
4963         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4964         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4965         pcmd += sizeof(uint32_t); /* Skip past command */
4966         rps_rsp = (RPS_RSP *)pcmd;
4967
4968         if (phba->fc_topology != TOPOLOGY_LOOP)
4969                 status = 0x10;
4970         else
4971                 status = 0x8;
4972         if (phba->pport->fc_flag & FC_FABRIC)
4973                 status |= 0x4;
4974
4975         rps_rsp->rsvd1 = 0;
4976         rps_rsp->portStatus = cpu_to_be16(status);
4977         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4978         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4979         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4980         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4981         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4982         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4983         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
4984         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4985                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4986                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4987                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4988                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4989                          ndlp->nlp_rpi);
4990         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4991         phba->fc_stat.elsXmitACC++;
4992         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
4993                 lpfc_els_free_iocb(phba, elsiocb);
4994         return;
4995 }
4996
4997 /**
4998  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
4999  * @vport: pointer to a host virtual N_Port data structure.
5000  * @cmdiocb: pointer to lpfc command iocb data structure.
5001  * @ndlp: pointer to a node-list data structure.
5002  *
5003  * This routine processes Read Port Status (RPL) IOCB received as an
5004  * ELS unsolicited event. It first checks the remote port state. If the
5005  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5006  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5007  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5008  * for reading the HBA link statistics. It is for the callback function,
5009  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
5010  * to actually sending out RPL Accept (ACC) response.
5011  *
5012  * Return codes
5013  *   0 - Successfully processed rls iocb (currently always return 0)
5014  **/
5015 static int
5016 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5017                  struct lpfc_nodelist *ndlp)
5018 {
5019         struct lpfc_hba *phba = vport->phba;
5020         LPFC_MBOXQ_t *mbox;
5021         struct lpfc_dmabuf *pcmd;
5022         struct ls_rjt stat;
5023
5024         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5025             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5026                 /* reject the unsolicited RPS request and done with it */
5027                 goto reject_out;
5028
5029         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5030
5031         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5032         if (mbox) {
5033                 lpfc_read_lnk_stat(phba, mbox);
5034                 mbox->context1 =
5035                     (void *)((unsigned long) cmdiocb->iocb.ulpContext);
5036                 mbox->context2 = lpfc_nlp_get(ndlp);
5037                 mbox->vport = vport;
5038                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
5039                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5040                         != MBX_NOT_FINISHED)
5041                         /* Mbox completion will send ELS Response */
5042                         return 0;
5043                 /* Decrement reference count used for the failed mbox
5044                  * command.
5045                  */
5046                 lpfc_nlp_put(ndlp);
5047                 mempool_free(mbox, phba->mbox_mem_pool);
5048         }
5049 reject_out:
5050         /* issue rejection response */
5051         stat.un.b.lsRjtRsvd0 = 0;
5052         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5053         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5054         stat.un.b.vendorUnique = 0;
5055         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5056         return 0;
5057 }
5058
5059 /**
5060  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
5061  * @vport: pointer to a host virtual N_Port data structure.
5062  * @cmdiocb: pointer to lpfc command iocb data structure.
5063  * @ndlp: pointer to a node-list data structure.
5064  *
5065  * This routine processes Read Timout Value (RTV) IOCB received as an
5066  * ELS unsolicited event. It first checks the remote port state. If the
5067  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5068  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5069  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
5070  * Value (RTV) unsolicited IOCB event.
5071  *
5072  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5073  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5074  * will be stored into the context1 field of the IOCB for the completion
5075  * callback function to the RPS Accept Response ELS IOCB command.
5076  *
5077  * Return codes
5078  *   0 - Successfully processed rtv iocb (currently always return 0)
5079  **/
5080 static int
5081 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5082                  struct lpfc_nodelist *ndlp)
5083 {
5084         struct lpfc_hba *phba = vport->phba;
5085         struct ls_rjt stat;
5086         struct RTV_RSP *rtv_rsp;
5087         uint8_t *pcmd;
5088         struct lpfc_iocbq *elsiocb;
5089         uint32_t cmdsize;
5090
5091
5092         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5093             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5094                 /* reject the unsolicited RPS request and done with it */
5095                 goto reject_out;
5096
5097         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
5098         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5099                                      lpfc_max_els_tries, ndlp,
5100                                      ndlp->nlp_DID, ELS_CMD_ACC);
5101
5102         if (!elsiocb)
5103                 return 1;
5104
5105         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5106                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5107         pcmd += sizeof(uint32_t); /* Skip past command */
5108
5109         /* use the command's xri in the response */
5110         elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;
5111
5112         rtv_rsp = (struct RTV_RSP *)pcmd;
5113
5114         /* populate RTV payload */
5115         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
5116         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
5117         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
5118         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
5119         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
5120
5121         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5122         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5123                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
5124                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
5125                          "Data: x%x x%x x%x\n",
5126                          elsiocb->iotag, elsiocb->iocb.ulpContext,
5127                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5128                          ndlp->nlp_rpi,
5129                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
5130         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5131         phba->fc_stat.elsXmitACC++;
5132         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5133                 lpfc_els_free_iocb(phba, elsiocb);
5134         return 0;
5135
5136 reject_out:
5137         /* issue rejection response */
5138         stat.un.b.lsRjtRsvd0 = 0;
5139         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5140         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5141         stat.un.b.vendorUnique = 0;
5142         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5143         return 0;
5144 }
5145
5146 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
5147  * @vport: pointer to a host virtual N_Port data structure.
5148  * @cmdiocb: pointer to lpfc command iocb data structure.
5149  * @ndlp: pointer to a node-list data structure.
5150  *
5151  * This routine processes Read Port Status (RPS) IOCB received as an
5152  * ELS unsolicited event. It first checks the remote port state. If the
5153  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5154  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
5155  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5156  * for reading the HBA link statistics. It is for the callback function,
5157  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
5158  * to actually sending out RPS Accept (ACC) response.
5159  *
5160  * Return codes
5161  *   0 - Successfully processed rps iocb (currently always return 0)
5162  **/
5163 static int
5164 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5165                  struct lpfc_nodelist *ndlp)
5166 {
5167         struct lpfc_hba *phba = vport->phba;
5168         uint32_t *lp;
5169         uint8_t flag;
5170         LPFC_MBOXQ_t *mbox;
5171         struct lpfc_dmabuf *pcmd;
5172         RPS *rps;
5173         struct ls_rjt stat;
5174
5175         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5176             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5177                 /* reject the unsolicited RPS request and done with it */
5178                 goto reject_out;
5179
5180         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5181         lp = (uint32_t *) pcmd->virt;
5182         flag = (be32_to_cpu(*lp++) & 0xf);
5183         rps = (RPS *) lp;
5184
5185         if ((flag == 0) ||
5186             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
5187             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
5188                                     sizeof(struct lpfc_name)) == 0))) {
5189
5190                 printk("Fix me....\n");
5191                 dump_stack();
5192                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5193                 if (mbox) {
5194                         lpfc_read_lnk_stat(phba, mbox);
5195                         mbox->context1 =
5196                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
5197                         mbox->context2 = lpfc_nlp_get(ndlp);
5198                         mbox->vport = vport;
5199                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
5200                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5201                                 != MBX_NOT_FINISHED)
5202                                 /* Mbox completion will send ELS Response */
5203                                 return 0;
5204                         /* Decrement reference count used for the failed mbox
5205                          * command.
5206                          */
5207                         lpfc_nlp_put(ndlp);
5208                         mempool_free(mbox, phba->mbox_mem_pool);
5209                 }
5210         }
5211
5212 reject_out:
5213         /* issue rejection response */
5214         stat.un.b.lsRjtRsvd0 = 0;
5215         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5216         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5217         stat.un.b.vendorUnique = 0;
5218         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5219         return 0;
5220 }
5221
5222 /**
5223  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
5224  * @vport: pointer to a host virtual N_Port data structure.
5225  * @cmdsize: size of the ELS command.
5226  * @oldiocb: pointer to the original lpfc command iocb data structure.
5227  * @ndlp: pointer to a node-list data structure.
5228  *
5229  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
5230  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
5231  *
5232  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5233  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5234  * will be stored into the context1 field of the IOCB for the completion
5235  * callback function to the RPL Accept Response ELS command.
5236  *
5237  * Return code
5238  *   0 - Successfully issued ACC RPL ELS command
5239  *   1 - Failed to issue ACC RPL ELS command
5240  **/
5241 static int
5242 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
5243                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5244 {
5245         struct lpfc_hba *phba = vport->phba;
5246         IOCB_t *icmd, *oldcmd;
5247         RPL_RSP rpl_rsp;
5248         struct lpfc_iocbq *elsiocb;
5249         uint8_t *pcmd;
5250
5251         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5252                                      ndlp->nlp_DID, ELS_CMD_ACC);
5253
5254         if (!elsiocb)
5255                 return 1;
5256
5257         icmd = &elsiocb->iocb;
5258         oldcmd = &oldiocb->iocb;
5259         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
5260
5261         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5262         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5263         pcmd += sizeof(uint16_t);
5264         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
5265         pcmd += sizeof(uint16_t);
5266
5267         /* Setup the RPL ACC payload */
5268         rpl_rsp.listLen = be32_to_cpu(1);
5269         rpl_rsp.index = 0;
5270         rpl_rsp.port_num_blk.portNum = 0;
5271         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
5272         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
5273             sizeof(struct lpfc_name));
5274         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
5275         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
5276         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5277                          "0120 Xmit ELS RPL ACC response tag x%x "
5278                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5279                          "rpi x%x\n",
5280                          elsiocb->iotag, elsiocb->iocb.ulpContext,
5281                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5282                          ndlp->nlp_rpi);
5283         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5284         phba->fc_stat.elsXmitACC++;
5285         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
5286             IOCB_ERROR) {
5287                 lpfc_els_free_iocb(phba, elsiocb);
5288                 return 1;
5289         }
5290         return 0;
5291 }
5292
5293 /**
5294  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
5295  * @vport: pointer to a host virtual N_Port data structure.
5296  * @cmdiocb: pointer to lpfc command iocb data structure.
5297  * @ndlp: pointer to a node-list data structure.
5298  *
5299  * This routine processes Read Port List (RPL) IOCB received as an ELS
5300  * unsolicited event. It first checks the remote port state. If the remote
5301  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
5302  * invokes the lpfc_els_rsp_reject() routine to send reject response.
5303  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
5304  * to accept the RPL.
5305  *
5306  * Return code
5307  *   0 - Successfully processed rpl iocb (currently always return 0)
5308  **/
5309 static int
5310 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5311                  struct lpfc_nodelist *ndlp)
5312 {
5313         struct lpfc_dmabuf *pcmd;
5314         uint32_t *lp;
5315         uint32_t maxsize;
5316         uint16_t cmdsize;
5317         RPL *rpl;
5318         struct ls_rjt stat;
5319
5320         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5321             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
5322                 /* issue rejection response */
5323                 stat.un.b.lsRjtRsvd0 = 0;
5324                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5325                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5326                 stat.un.b.vendorUnique = 0;
5327                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5328                         NULL);
5329                 /* rejected the unsolicited RPL request and done with it */
5330                 return 0;
5331         }
5332
5333         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5334         lp = (uint32_t *) pcmd->virt;
5335         rpl = (RPL *) (lp + 1);
5336         maxsize = be32_to_cpu(rpl->maxsize);
5337
5338         /* We support only one port */
5339         if ((rpl->index == 0) &&
5340             ((maxsize == 0) ||
5341              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
5342                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
5343         } else {
5344                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
5345         }
5346         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
5347
5348         return 0;
5349 }
5350
5351 /**
5352  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
5353  * @vport: pointer to a virtual N_Port data structure.
5354  * @cmdiocb: pointer to lpfc command iocb data structure.
5355  * @ndlp: pointer to a node-list data structure.
5356  *
5357  * This routine processes Fibre Channel Address Resolution Protocol
5358  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
5359  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
5360  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
5361  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
5362  * remote PortName is compared against the FC PortName stored in the @vport
5363  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
5364  * compared against the FC NodeName stored in the @vport data structure.
5365  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
5366  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
5367  * invoked to send out FARP Response to the remote node. Before sending the
5368  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
5369  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
5370  * routine is invoked to log into the remote port first.
5371  *
5372  * Return code
5373  *   0 - Either the FARP Match Mode not supported or successfully processed
5374  **/
5375 static int
5376 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5377                   struct lpfc_nodelist *ndlp)
5378 {
5379         struct lpfc_dmabuf *pcmd;
5380         uint32_t *lp;
5381         IOCB_t *icmd;
5382         FARP *fp;
5383         uint32_t cmd, cnt, did;
5384
5385         icmd = &cmdiocb->iocb;
5386         did = icmd->un.elsreq64.remoteID;
5387         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5388         lp = (uint32_t *) pcmd->virt;
5389
5390         cmd = *lp++;
5391         fp = (FARP *) lp;
5392         /* FARP-REQ received from DID <did> */
5393         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5394                          "0601 FARP-REQ received from DID x%x\n", did);
5395         /* We will only support match on WWPN or WWNN */
5396         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
5397                 return 0;
5398         }
5399
5400         cnt = 0;
5401         /* If this FARP command is searching for my portname */
5402         if (fp->Mflags & FARP_MATCH_PORT) {
5403                 if (memcmp(&fp->RportName, &vport->fc_portname,
5404                            sizeof(struct lpfc_name)) == 0)
5405                         cnt = 1;
5406         }
5407
5408         /* If this FARP command is searching for my nodename */
5409         if (fp->Mflags & FARP_MATCH_NODE) {
5410                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
5411                            sizeof(struct lpfc_name)) == 0)
5412                         cnt = 1;
5413         }
5414
5415         if (cnt) {
5416                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
5417                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
5418                         /* Log back into the node before sending the FARP. */
5419                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
5420                                 ndlp->nlp_prev_state = ndlp->nlp_state;
5421                                 lpfc_nlp_set_state(vport, ndlp,
5422                                                    NLP_STE_PLOGI_ISSUE);
5423                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5424                         }
5425
5426                         /* Send a FARP response to that node */
5427                         if (fp->Rflags & FARP_REQUEST_FARPR)
5428                                 lpfc_issue_els_farpr(vport, did, 0);
5429                 }
5430         }
5431         return 0;
5432 }
5433
5434 /**
5435  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
5436  * @vport: pointer to a host virtual N_Port data structure.
5437  * @cmdiocb: pointer to lpfc command iocb data structure.
5438  * @ndlp: pointer to a node-list data structure.
5439  *
5440  * This routine processes Fibre Channel Address Resolution Protocol
5441  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
5442  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
5443  * the FARP response request.
5444  *
5445  * Return code
5446  *   0 - Successfully processed FARPR IOCB (currently always return 0)
5447  **/
5448 static int
5449 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5450                    struct lpfc_nodelist  *ndlp)
5451 {
5452         struct lpfc_dmabuf *pcmd;
5453         uint32_t *lp;
5454         IOCB_t *icmd;
5455         uint32_t cmd, did;
5456
5457         icmd = &cmdiocb->iocb;
5458         did = icmd->un.elsreq64.remoteID;
5459         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5460         lp = (uint32_t *) pcmd->virt;
5461
5462         cmd = *lp++;
5463         /* FARP-RSP received from DID <did> */
5464         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5465                          "0600 FARP-RSP received from DID x%x\n", did);
5466         /* ACCEPT the Farp resp request */
5467         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5468
5469         return 0;
5470 }
5471
5472 /**
5473  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
5474  * @vport: pointer to a host virtual N_Port data structure.
5475  * @cmdiocb: pointer to lpfc command iocb data structure.
5476  * @fan_ndlp: pointer to a node-list data structure.
5477  *
5478  * This routine processes a Fabric Address Notification (FAN) IOCB
5479  * command received as an ELS unsolicited event. The FAN ELS command will
5480  * only be processed on a physical port (i.e., the @vport represents the
5481  * physical port). The fabric NodeName and PortName from the FAN IOCB are
5482  * compared against those in the phba data structure. If any of those is
5483  * different, the lpfc_initial_flogi() routine is invoked to initialize
5484  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
5485  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
5486  * is invoked to register login to the fabric.
5487  *
5488  * Return code
5489  *   0 - Successfully processed fan iocb (currently always return 0).
5490  **/
5491 static int
5492 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5493                  struct lpfc_nodelist *fan_ndlp)
5494 {
5495         struct lpfc_hba *phba = vport->phba;
5496         uint32_t *lp;
5497         FAN *fp;
5498
5499         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
5500         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
5501         fp = (FAN *) ++lp;
5502         /* FAN received; Fan does not have a reply sequence */
5503         if ((vport == phba->pport) &&
5504             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5505                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
5506                             sizeof(struct lpfc_name))) ||
5507                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
5508                             sizeof(struct lpfc_name)))) {
5509                         /* This port has switched fabrics. FLOGI is required */
5510                         lpfc_initial_flogi(vport);
5511                 } else {
5512                         /* FAN verified - skip FLOGI */
5513                         vport->fc_myDID = vport->fc_prevDID;
5514                         if (phba->sli_rev < LPFC_SLI_REV4)
5515                                 lpfc_issue_fabric_reglogin(vport);
5516                         else
5517                                 lpfc_issue_reg_vfi(vport);
5518                 }
5519         }
5520         return 0;
5521 }
5522
5523 /**
5524  * lpfc_els_timeout - Handler funciton to the els timer
5525  * @ptr: holder for the timer function associated data.
5526  *
5527  * This routine is invoked by the ELS timer after timeout. It posts the ELS
5528  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
5529  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
5530  * up the worker thread. It is for the worker thread to invoke the routine
5531  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
5532  **/
5533 void
5534 lpfc_els_timeout(unsigned long ptr)
5535 {
5536         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5537         struct lpfc_hba   *phba = vport->phba;
5538         uint32_t tmo_posted;
5539         unsigned long iflag;
5540
5541         spin_lock_irqsave(&vport->work_port_lock, iflag);
5542         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
5543         if (!tmo_posted)
5544                 vport->work_port_events |= WORKER_ELS_TMO;
5545         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
5546
5547         if (!tmo_posted)
5548                 lpfc_worker_wake_up(phba);
5549         return;
5550 }
5551
5552
5553 /**
5554  * lpfc_els_timeout_handler - Process an els timeout event
5555  * @vport: pointer to a virtual N_Port data structure.
5556  *
5557  * This routine is the actual handler function that processes an ELS timeout
5558  * event. It walks the ELS ring to get and abort all the IOCBs (except the
5559  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
5560  * invoking the lpfc_sli_issue_abort_iotag() routine.
5561  **/
5562 void
5563 lpfc_els_timeout_handler(struct lpfc_vport *vport)
5564 {
5565         struct lpfc_hba  *phba = vport->phba;
5566         struct lpfc_sli_ring *pring;
5567         struct lpfc_iocbq *tmp_iocb, *piocb;
5568         IOCB_t *cmd = NULL;
5569         struct lpfc_dmabuf *pcmd;
5570         uint32_t els_command = 0;
5571         uint32_t timeout;
5572         uint32_t remote_ID = 0xffffffff;
5573         LIST_HEAD(txcmplq_completions);
5574         LIST_HEAD(abort_list);
5575
5576
5577         timeout = (uint32_t)(phba->fc_ratov << 1);
5578
5579         pring = &phba->sli.ring[LPFC_ELS_RING];
5580
5581         spin_lock_irq(&phba->hbalock);
5582         list_splice_init(&pring->txcmplq, &txcmplq_completions);
5583         spin_unlock_irq(&phba->hbalock);
5584
5585         list_for_each_entry_safe(piocb, tmp_iocb, &txcmplq_completions, list) {
5586                 cmd = &piocb->iocb;
5587
5588                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
5589                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
5590                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5591                         continue;
5592