]> Pileus Git - ~andy/linux/blob - drivers/scsi/lpfc/lpfc_els.c
Merge branch 'x86-hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~andy/linux] / 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_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
521
522         if (phba->fc_topology == TOPOLOGY_LOOP) {
523                 spin_lock_irq(shost->host_lock);
524                 vport->fc_flag |= FC_PUBLIC_LOOP;
525                 spin_unlock_irq(shost->host_lock);
526         } else {
527                 /*
528                  * If we are a N-port connected to a Fabric, fixup sparam's so
529                  * logins to devices on remote loops work.
530                  */
531                 vport->fc_sparam.cmn.altBbCredit = 1;
532         }
533
534         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
535         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
536         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
537         ndlp->nlp_class_sup = 0;
538         if (sp->cls1.classValid)
539                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
540         if (sp->cls2.classValid)
541                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
542         if (sp->cls3.classValid)
543                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
544         if (sp->cls4.classValid)
545                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
546         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
547                                 sp->cmn.bbRcvSizeLsb;
548         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
549
550         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
551                 if (sp->cmn.response_multiple_NPort) {
552                         lpfc_printf_vlog(vport, KERN_WARNING,
553                                          LOG_ELS | LOG_VPORT,
554                                          "1816 FLOGI NPIV supported, "
555                                          "response data 0x%x\n",
556                                          sp->cmn.response_multiple_NPort);
557                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
558                 } else {
559                         /* Because we asked f/w for NPIV it still expects us
560                         to call reg_vnpid atleast for the physcial host */
561                         lpfc_printf_vlog(vport, KERN_WARNING,
562                                          LOG_ELS | LOG_VPORT,
563                                          "1817 Fabric does not support NPIV "
564                                          "- configuring single port mode.\n");
565                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
566                 }
567         }
568
569         if ((vport->fc_prevDID != vport->fc_myDID) &&
570                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
571
572                 /* If our NportID changed, we need to ensure all
573                  * remaining NPORTs get unreg_login'ed.
574                  */
575                 list_for_each_entry_safe(np, next_np,
576                                         &vport->fc_nodes, nlp_listp) {
577                         if (!NLP_CHK_NODE_ACT(np))
578                                 continue;
579                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
580                                    !(np->nlp_flag & NLP_NPR_ADISC))
581                                 continue;
582                         spin_lock_irq(shost->host_lock);
583                         np->nlp_flag &= ~NLP_NPR_ADISC;
584                         spin_unlock_irq(shost->host_lock);
585                         lpfc_unreg_rpi(vport, np);
586                 }
587                 lpfc_cleanup_pending_mbox(vport);
588                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
589                         lpfc_mbx_unreg_vpi(vport);
590                         spin_lock_irq(shost->host_lock);
591                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
592                         spin_unlock_irq(shost->host_lock);
593                 }
594                 /*
595                  * If VPI is unreged, driver need to do INIT_VPI
596                  * before re-registering
597                  */
598                 if (phba->sli_rev == LPFC_SLI_REV4) {
599                         spin_lock_irq(shost->host_lock);
600                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
601                         spin_unlock_irq(shost->host_lock);
602                 }
603         }
604
605         if (phba->sli_rev < LPFC_SLI_REV4) {
606                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
607                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
608                     vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
609                         lpfc_register_new_vport(phba, vport, ndlp);
610                 else
611                         lpfc_issue_fabric_reglogin(vport);
612         } else {
613                 ndlp->nlp_type |= NLP_FABRIC;
614                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
615                 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
616                         (vport->vpi_state & LPFC_VPI_REGISTERED)) {
617                         lpfc_start_fdiscs(phba);
618                         lpfc_do_scr_ns_plogi(phba, vport);
619                 } else if (vport->fc_flag & FC_VFI_REGISTERED)
620                         lpfc_issue_init_vpi(vport);
621                 else
622                         lpfc_issue_reg_vfi(vport);
623         }
624         return 0;
625 }
626 /**
627  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
628  * @vport: pointer to a host virtual N_Port data structure.
629  * @ndlp: pointer to a node-list data structure.
630  * @sp: pointer to service parameter data structure.
631  *
632  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
633  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
634  * in a point-to-point topology. First, the @vport's N_Port Name is compared
635  * with the received N_Port Name: if the @vport's N_Port Name is greater than
636  * the received N_Port Name lexicographically, this node shall assign local
637  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
638  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
639  * this node shall just wait for the remote node to issue PLOGI and assign
640  * N_Port IDs.
641  *
642  * Return code
643  *   0 - Success
644  *   -ENXIO - Fail
645  **/
646 static int
647 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
648                           struct serv_parm *sp)
649 {
650         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
651         struct lpfc_hba  *phba = vport->phba;
652         LPFC_MBOXQ_t *mbox;
653         int rc;
654
655         spin_lock_irq(shost->host_lock);
656         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
657         spin_unlock_irq(shost->host_lock);
658
659         phba->fc_edtov = FF_DEF_EDTOV;
660         phba->fc_ratov = FF_DEF_RATOV;
661         rc = memcmp(&vport->fc_portname, &sp->portName,
662                     sizeof(vport->fc_portname));
663         if (rc >= 0) {
664                 /* This side will initiate the PLOGI */
665                 spin_lock_irq(shost->host_lock);
666                 vport->fc_flag |= FC_PT2PT_PLOGI;
667                 spin_unlock_irq(shost->host_lock);
668
669                 /*
670                  * N_Port ID cannot be 0, set our to LocalID the other
671                  * side will be RemoteID.
672                  */
673
674                 /* not equal */
675                 if (rc)
676                         vport->fc_myDID = PT2PT_LocalID;
677
678                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
679                 if (!mbox)
680                         goto fail;
681
682                 lpfc_config_link(phba, mbox);
683
684                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
685                 mbox->vport = vport;
686                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
687                 if (rc == MBX_NOT_FINISHED) {
688                         mempool_free(mbox, phba->mbox_mem_pool);
689                         goto fail;
690                 }
691                 /* Decrement ndlp reference count indicating that ndlp can be
692                  * safely released when other references to it are done.
693                  */
694                 lpfc_nlp_put(ndlp);
695
696                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
697                 if (!ndlp) {
698                         /*
699                          * Cannot find existing Fabric ndlp, so allocate a
700                          * new one
701                          */
702                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
703                         if (!ndlp)
704                                 goto fail;
705                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
706                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
707                         ndlp = lpfc_enable_node(vport, ndlp,
708                                                 NLP_STE_UNUSED_NODE);
709                         if(!ndlp)
710                                 goto fail;
711                 }
712
713                 memcpy(&ndlp->nlp_portname, &sp->portName,
714                        sizeof(struct lpfc_name));
715                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
716                        sizeof(struct lpfc_name));
717                 /* Set state will put ndlp onto node list if not already done */
718                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
719                 spin_lock_irq(shost->host_lock);
720                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
721                 spin_unlock_irq(shost->host_lock);
722         } else
723                 /* This side will wait for the PLOGI, decrement ndlp reference
724                  * count indicating that ndlp can be released when other
725                  * references to it are done.
726                  */
727                 lpfc_nlp_put(ndlp);
728
729         /* If we are pt2pt with another NPort, force NPIV off! */
730         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
731
732         spin_lock_irq(shost->host_lock);
733         vport->fc_flag |= FC_PT2PT;
734         spin_unlock_irq(shost->host_lock);
735
736         /* Start discovery - this should just do CLEAR_LA */
737         lpfc_disc_start(vport);
738         return 0;
739 fail:
740         return -ENXIO;
741 }
742
743 /**
744  * lpfc_cmpl_els_flogi - Completion callback function for flogi
745  * @phba: pointer to lpfc hba data structure.
746  * @cmdiocb: pointer to lpfc command iocb data structure.
747  * @rspiocb: pointer to lpfc response iocb data structure.
748  *
749  * This routine is the top-level completion callback function for issuing
750  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
751  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
752  * retry has been made (either immediately or delayed with lpfc_els_retry()
753  * returning 1), the command IOCB will be released and function returned.
754  * If the retry attempt has been given up (possibly reach the maximum
755  * number of retries), one additional decrement of ndlp reference shall be
756  * invoked before going out after releasing the command IOCB. This will
757  * actually release the remote node (Note, lpfc_els_free_iocb() will also
758  * invoke one decrement of ndlp reference count). If no error reported in
759  * the IOCB status, the command Port ID field is used to determine whether
760  * this is a point-to-point topology or a fabric topology: if the Port ID
761  * field is assigned, it is a fabric topology; otherwise, it is a
762  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
763  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
764  * specific topology completion conditions.
765  **/
766 static void
767 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
768                     struct lpfc_iocbq *rspiocb)
769 {
770         struct lpfc_vport *vport = cmdiocb->vport;
771         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
772         IOCB_t *irsp = &rspiocb->iocb;
773         struct lpfc_nodelist *ndlp = cmdiocb->context1;
774         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
775         struct serv_parm *sp;
776         uint16_t fcf_index;
777         int rc;
778
779         /* Check to see if link went down during discovery */
780         if (lpfc_els_chk_latt(vport)) {
781                 /* One additional decrement on node reference count to
782                  * trigger the release of the node
783                  */
784                 lpfc_nlp_put(ndlp);
785                 goto out;
786         }
787
788         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
789                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
790                 irsp->ulpStatus, irsp->un.ulpWord[4],
791                 vport->port_state);
792
793         if (irsp->ulpStatus) {
794                 /*
795                  * In case of FIP mode, perform round robin FCF failover
796                  * due to new FCF discovery
797                  */
798                 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
799                     (phba->fcf.fcf_flag & FCF_DISCOVERY) &&
800                     (irsp->ulpStatus != IOSTAT_LOCAL_REJECT) &&
801                     (irsp->un.ulpWord[4] != IOERR_SLI_ABORTED)) {
802                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
803                                         "2611 FLOGI failed on registered "
804                                         "FCF record fcf_index:%d, trying "
805                                         "to perform round robin failover\n",
806                                         phba->fcf.current_rec.fcf_indx);
807                         fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
808                         if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
809                                 /*
810                                  * Exhausted the eligible FCF record list,
811                                  * fail through to retry FLOGI on current
812                                  * FCF record.
813                                  */
814                                 lpfc_printf_log(phba, KERN_WARNING,
815                                                 LOG_FIP | LOG_ELS,
816                                                 "2760 Completed one round "
817                                                 "of FLOGI FCF round robin "
818                                                 "failover list, retry FLOGI "
819                                                 "on currently registered "
820                                                 "FCF index:%d\n",
821                                                 phba->fcf.current_rec.fcf_indx);
822                         } else {
823                                 lpfc_printf_log(phba, KERN_INFO,
824                                                 LOG_FIP | LOG_ELS,
825                                                 "2794 FLOGI FCF round robin "
826                                                 "failover to FCF index x%x\n",
827                                                 fcf_index);
828                                 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba,
829                                                                    fcf_index);
830                                 if (rc)
831                                         lpfc_printf_log(phba, KERN_WARNING,
832                                                         LOG_FIP | LOG_ELS,
833                                                         "2761 FLOGI round "
834                                                         "robin FCF failover "
835                                                         "read FCF failed "
836                                                         "rc:x%x, fcf_index:"
837                                                         "%d\n", rc,
838                                                 phba->fcf.current_rec.fcf_indx);
839                                 else
840                                         goto out;
841                         }
842                 }
843
844                 /* Check for retry */
845                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
846                         goto out;
847
848                 /* FLOGI failed, so there is no fabric */
849                 spin_lock_irq(shost->host_lock);
850                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
851                 spin_unlock_irq(shost->host_lock);
852
853                 /* If private loop, then allow max outstanding els to be
854                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
855                  * alpa map would take too long otherwise.
856                  */
857                 if (phba->alpa_map[0] == 0) {
858                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
859                 }
860
861                 /* FLOGI failure */
862                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
863                                  "0100 FLOGI failure Status:x%x/x%x TMO:x%x\n",
864                                  irsp->ulpStatus, irsp->un.ulpWord[4],
865                                  irsp->ulpTimeout);
866                 goto flogifail;
867         }
868         spin_lock_irq(shost->host_lock);
869         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
870         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
871         spin_unlock_irq(shost->host_lock);
872
873         /*
874          * The FLogI succeeded.  Sync the data for the CPU before
875          * accessing it.
876          */
877         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
878
879         sp = prsp->virt + sizeof(uint32_t);
880
881         /* FLOGI completes successfully */
882         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
883                          "0101 FLOGI completes successfully "
884                          "Data: x%x x%x x%x x%x\n",
885                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
886                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
887
888         if (vport->port_state == LPFC_FLOGI) {
889                 /*
890                  * If Common Service Parameters indicate Nport
891                  * we are point to point, if Fport we are Fabric.
892                  */
893                 if (sp->cmn.fPort)
894                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
895                 else if (!(phba->hba_flag & HBA_FCOE_SUPPORT))
896                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
897                 else {
898                         lpfc_printf_vlog(vport, KERN_ERR,
899                                 LOG_FIP | LOG_ELS,
900                                 "2831 FLOGI response with cleared Fabric "
901                                 "bit fcf_index 0x%x "
902                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
903                                 "Fabric Name "
904                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
905                                 phba->fcf.current_rec.fcf_indx,
906                                 phba->fcf.current_rec.switch_name[0],
907                                 phba->fcf.current_rec.switch_name[1],
908                                 phba->fcf.current_rec.switch_name[2],
909                                 phba->fcf.current_rec.switch_name[3],
910                                 phba->fcf.current_rec.switch_name[4],
911                                 phba->fcf.current_rec.switch_name[5],
912                                 phba->fcf.current_rec.switch_name[6],
913                                 phba->fcf.current_rec.switch_name[7],
914                                 phba->fcf.current_rec.fabric_name[0],
915                                 phba->fcf.current_rec.fabric_name[1],
916                                 phba->fcf.current_rec.fabric_name[2],
917                                 phba->fcf.current_rec.fabric_name[3],
918                                 phba->fcf.current_rec.fabric_name[4],
919                                 phba->fcf.current_rec.fabric_name[5],
920                                 phba->fcf.current_rec.fabric_name[6],
921                                 phba->fcf.current_rec.fabric_name[7]);
922                         lpfc_nlp_put(ndlp);
923                         spin_lock_irq(&phba->hbalock);
924                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
925                         spin_unlock_irq(&phba->hbalock);
926                         goto out;
927                 }
928                 if (!rc) {
929                         /* Mark the FCF discovery process done */
930                         if (phba->hba_flag & HBA_FIP_SUPPORT)
931                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
932                                                 LOG_ELS,
933                                                 "2769 FLOGI successful on FCF "
934                                                 "record: current_fcf_index:"
935                                                 "x%x, terminate FCF round "
936                                                 "robin failover process\n",
937                                                 phba->fcf.current_rec.fcf_indx);
938                         spin_lock_irq(&phba->hbalock);
939                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
940                         spin_unlock_irq(&phba->hbalock);
941                         goto out;
942                 }
943         }
944
945 flogifail:
946         lpfc_nlp_put(ndlp);
947
948         if (!lpfc_error_lost_link(irsp)) {
949                 /* FLOGI failed, so just use loop map to make discovery list */
950                 lpfc_disc_list_loopmap(vport);
951
952                 /* Start discovery */
953                 lpfc_disc_start(vport);
954         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
955                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
956                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
957                         (phba->link_state != LPFC_CLEAR_LA)) {
958                 /* If FLOGI failed enable link interrupt. */
959                 lpfc_issue_clear_la(phba, vport);
960         }
961 out:
962         lpfc_els_free_iocb(phba, cmdiocb);
963 }
964
965 /**
966  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
967  * @vport: pointer to a host virtual N_Port data structure.
968  * @ndlp: pointer to a node-list data structure.
969  * @retry: number of retries to the command IOCB.
970  *
971  * This routine issues a Fabric Login (FLOGI) Request ELS command
972  * for a @vport. The initiator service parameters are put into the payload
973  * of the FLOGI Request IOCB and the top-level callback function pointer
974  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
975  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
976  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
977  *
978  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
979  * will be incremented by 1 for holding the ndlp and the reference to ndlp
980  * will be stored into the context1 field of the IOCB for the completion
981  * callback function to the FLOGI ELS command.
982  *
983  * Return code
984  *   0 - successfully issued flogi iocb for @vport
985  *   1 - failed to issue flogi iocb for @vport
986  **/
987 static int
988 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
989                      uint8_t retry)
990 {
991         struct lpfc_hba  *phba = vport->phba;
992         struct serv_parm *sp;
993         IOCB_t *icmd;
994         struct lpfc_iocbq *elsiocb;
995         struct lpfc_sli_ring *pring;
996         uint8_t *pcmd;
997         uint16_t cmdsize;
998         uint32_t tmo;
999         int rc;
1000
1001         pring = &phba->sli.ring[LPFC_ELS_RING];
1002
1003         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1004         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1005                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1006
1007         if (!elsiocb)
1008                 return 1;
1009
1010         icmd = &elsiocb->iocb;
1011         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1012
1013         /* For FLOGI request, remainder of payload is service parameters */
1014         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1015         pcmd += sizeof(uint32_t);
1016         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1017         sp = (struct serv_parm *) pcmd;
1018
1019         /* Setup CSPs accordingly for Fabric */
1020         sp->cmn.e_d_tov = 0;
1021         sp->cmn.w2.r_a_tov = 0;
1022         sp->cls1.classValid = 0;
1023         sp->cls2.seqDelivery = 1;
1024         sp->cls3.seqDelivery = 1;
1025         if (sp->cmn.fcphLow < FC_PH3)
1026                 sp->cmn.fcphLow = FC_PH3;
1027         if (sp->cmn.fcphHigh < FC_PH3)
1028                 sp->cmn.fcphHigh = FC_PH3;
1029
1030         if  (phba->sli_rev == LPFC_SLI_REV4) {
1031                 elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1032                 elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1033                 /* FLOGI needs to be 3 for WQE FCFI */
1034                 /* Set the fcfi to the fcfi we registered with */
1035                 elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1036         } else if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1037                 sp->cmn.request_multiple_Nport = 1;
1038                 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1039                 icmd->ulpCt_h = 1;
1040                 icmd->ulpCt_l = 0;
1041         }
1042
1043         if (phba->fc_topology != TOPOLOGY_LOOP) {
1044                 icmd->un.elsreq64.myID = 0;
1045                 icmd->un.elsreq64.fl = 1;
1046         }
1047
1048         tmo = phba->fc_ratov;
1049         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1050         lpfc_set_disctmo(vport);
1051         phba->fc_ratov = tmo;
1052
1053         phba->fc_stat.elsXmitFLOGI++;
1054         elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1055
1056         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1057                 "Issue FLOGI:     opt:x%x",
1058                 phba->sli3_options, 0, 0);
1059
1060         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1061         if (rc == IOCB_ERROR) {
1062                 lpfc_els_free_iocb(phba, elsiocb);
1063                 return 1;
1064         }
1065         return 0;
1066 }
1067
1068 /**
1069  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1070  * @phba: pointer to lpfc hba data structure.
1071  *
1072  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1073  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1074  * list and issues an abort IOCB commond on each outstanding IOCB that
1075  * contains a active Fabric_DID ndlp. Note that this function is to issue
1076  * the abort IOCB command on all the outstanding IOCBs, thus when this
1077  * function returns, it does not guarantee all the IOCBs are actually aborted.
1078  *
1079  * Return code
1080  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1081  **/
1082 int
1083 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1084 {
1085         struct lpfc_sli_ring *pring;
1086         struct lpfc_iocbq *iocb, *next_iocb;
1087         struct lpfc_nodelist *ndlp;
1088         IOCB_t *icmd;
1089
1090         /* Abort outstanding I/O on NPort <nlp_DID> */
1091         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1092                         "0201 Abort outstanding I/O on NPort x%x\n",
1093                         Fabric_DID);
1094
1095         pring = &phba->sli.ring[LPFC_ELS_RING];
1096
1097         /*
1098          * Check the txcmplq for an iocb that matches the nport the driver is
1099          * searching for.
1100          */
1101         spin_lock_irq(&phba->hbalock);
1102         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1103                 icmd = &iocb->iocb;
1104                 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
1105                     icmd->un.elsreq64.bdl.ulpIoTag32) {
1106                         ndlp = (struct lpfc_nodelist *)(iocb->context1);
1107                         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1108                             (ndlp->nlp_DID == Fabric_DID))
1109                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1110                 }
1111         }
1112         spin_unlock_irq(&phba->hbalock);
1113
1114         return 0;
1115 }
1116
1117 /**
1118  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1119  * @vport: pointer to a host virtual N_Port data structure.
1120  *
1121  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1122  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1123  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1124  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1125  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1126  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1127  * @vport.
1128  *
1129  * Return code
1130  *   0 - failed to issue initial flogi for @vport
1131  *   1 - successfully issued initial flogi for @vport
1132  **/
1133 int
1134 lpfc_initial_flogi(struct lpfc_vport *vport)
1135 {
1136         struct lpfc_hba *phba = vport->phba;
1137         struct lpfc_nodelist *ndlp;
1138
1139         vport->port_state = LPFC_FLOGI;
1140         lpfc_set_disctmo(vport);
1141
1142         /* First look for the Fabric ndlp */
1143         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1144         if (!ndlp) {
1145                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1146                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1147                 if (!ndlp)
1148                         return 0;
1149                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1150                 /* Set the node type */
1151                 ndlp->nlp_type |= NLP_FABRIC;
1152                 /* Put ndlp onto node list */
1153                 lpfc_enqueue_node(vport, ndlp);
1154         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1155                 /* re-setup ndlp without removing from node list */
1156                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1157                 if (!ndlp)
1158                         return 0;
1159         }
1160
1161         if (lpfc_issue_els_flogi(vport, ndlp, 0))
1162                 /* This decrement of reference count to node shall kick off
1163                  * the release of the node.
1164                  */
1165                 lpfc_nlp_put(ndlp);
1166
1167         return 1;
1168 }
1169
1170 /**
1171  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1172  * @vport: pointer to a host virtual N_Port data structure.
1173  *
1174  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1175  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1176  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1177  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1178  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1179  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1180  * @vport.
1181  *
1182  * Return code
1183  *   0 - failed to issue initial fdisc for @vport
1184  *   1 - successfully issued initial fdisc for @vport
1185  **/
1186 int
1187 lpfc_initial_fdisc(struct lpfc_vport *vport)
1188 {
1189         struct lpfc_hba *phba = vport->phba;
1190         struct lpfc_nodelist *ndlp;
1191
1192         /* First look for the Fabric ndlp */
1193         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1194         if (!ndlp) {
1195                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1196                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1197                 if (!ndlp)
1198                         return 0;
1199                 lpfc_nlp_init(vport, ndlp, Fabric_DID);
1200                 /* Put ndlp onto node list */
1201                 lpfc_enqueue_node(vport, ndlp);
1202         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1203                 /* re-setup ndlp without removing from node list */
1204                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1205                 if (!ndlp)
1206                         return 0;
1207         }
1208
1209         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1210                 /* decrement node reference count to trigger the release of
1211                  * the node.
1212                  */
1213                 lpfc_nlp_put(ndlp);
1214                 return 0;
1215         }
1216         return 1;
1217 }
1218
1219 /**
1220  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1221  * @vport: pointer to a host virtual N_Port data structure.
1222  *
1223  * This routine checks whether there are more remaining Port Logins
1224  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1225  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1226  * to issue ELS PLOGIs up to the configured discover threads with the
1227  * @vport (@vport->cfg_discovery_threads). The function also decrement
1228  * the @vport's num_disc_node by 1 if it is not already 0.
1229  **/
1230 void
1231 lpfc_more_plogi(struct lpfc_vport *vport)
1232 {
1233         int sentplogi;
1234
1235         if (vport->num_disc_nodes)
1236                 vport->num_disc_nodes--;
1237
1238         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1239         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1240                          "0232 Continue discovery with %d PLOGIs to go "
1241                          "Data: x%x x%x x%x\n",
1242                          vport->num_disc_nodes, vport->fc_plogi_cnt,
1243                          vport->fc_flag, vport->port_state);
1244         /* Check to see if there are more PLOGIs to be sent */
1245         if (vport->fc_flag & FC_NLP_MORE)
1246                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1247                 sentplogi = lpfc_els_disc_plogi(vport);
1248
1249         return;
1250 }
1251
1252 /**
1253  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1254  * @phba: pointer to lpfc hba data structure.
1255  * @prsp: pointer to response IOCB payload.
1256  * @ndlp: pointer to a node-list data structure.
1257  *
1258  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1259  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1260  * The following cases are considered N_Port confirmed:
1261  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1262  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1263  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1264  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1265  * 1) if there is a node on vport list other than the @ndlp with the same
1266  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1267  * on that node to release the RPI associated with the node; 2) if there is
1268  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1269  * into, a new node shall be allocated (or activated). In either case, the
1270  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1271  * be released and the new_ndlp shall be put on to the vport node list and
1272  * its pointer returned as the confirmed node.
1273  *
1274  * Note that before the @ndlp got "released", the keepDID from not-matching
1275  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1276  * of the @ndlp. This is because the release of @ndlp is actually to put it
1277  * into an inactive state on the vport node list and the vport node list
1278  * management algorithm does not allow two node with a same DID.
1279  *
1280  * Return code
1281  *   pointer to the PLOGI N_Port @ndlp
1282  **/
1283 static struct lpfc_nodelist *
1284 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1285                          struct lpfc_nodelist *ndlp)
1286 {
1287         struct lpfc_vport    *vport = ndlp->vport;
1288         struct lpfc_nodelist *new_ndlp;
1289         struct lpfc_rport_data *rdata;
1290         struct fc_rport *rport;
1291         struct serv_parm *sp;
1292         uint8_t  name[sizeof(struct lpfc_name)];
1293         uint32_t rc, keepDID = 0;
1294
1295         /* Fabric nodes can have the same WWPN so we don't bother searching
1296          * by WWPN.  Just return the ndlp that was given to us.
1297          */
1298         if (ndlp->nlp_type & NLP_FABRIC)
1299                 return ndlp;
1300
1301         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1302         memset(name, 0, sizeof(struct lpfc_name));
1303
1304         /* Now we find out if the NPort we are logging into, matches the WWPN
1305          * we have for that ndlp. If not, we have some work to do.
1306          */
1307         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1308
1309         if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1310                 return ndlp;
1311
1312         if (!new_ndlp) {
1313                 rc = memcmp(&ndlp->nlp_portname, name,
1314                             sizeof(struct lpfc_name));
1315                 if (!rc)
1316                         return ndlp;
1317                 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1318                 if (!new_ndlp)
1319                         return ndlp;
1320                 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1321         } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1322                 rc = memcmp(&ndlp->nlp_portname, name,
1323                             sizeof(struct lpfc_name));
1324                 if (!rc)
1325                         return ndlp;
1326                 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1327                                                 NLP_STE_UNUSED_NODE);
1328                 if (!new_ndlp)
1329                         return ndlp;
1330                 keepDID = new_ndlp->nlp_DID;
1331         } else
1332                 keepDID = new_ndlp->nlp_DID;
1333
1334         lpfc_unreg_rpi(vport, new_ndlp);
1335         new_ndlp->nlp_DID = ndlp->nlp_DID;
1336         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1337
1338         if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1339                 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1340         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1341
1342         /* Set state will put new_ndlp on to node list if not already done */
1343         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1344
1345         /* Move this back to NPR state */
1346         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1347                 /* The new_ndlp is replacing ndlp totally, so we need
1348                  * to put ndlp on UNUSED list and try to free it.
1349                  */
1350
1351                 /* Fix up the rport accordingly */
1352                 rport =  ndlp->rport;
1353                 if (rport) {
1354                         rdata = rport->dd_data;
1355                         if (rdata->pnode == ndlp) {
1356                                 lpfc_nlp_put(ndlp);
1357                                 ndlp->rport = NULL;
1358                                 rdata->pnode = lpfc_nlp_get(new_ndlp);
1359                                 new_ndlp->rport = rport;
1360                         }
1361                         new_ndlp->nlp_type = ndlp->nlp_type;
1362                 }
1363                 /* We shall actually free the ndlp with both nlp_DID and
1364                  * nlp_portname fields equals 0 to avoid any ndlp on the
1365                  * nodelist never to be used.
1366                  */
1367                 if (ndlp->nlp_DID == 0) {
1368                         spin_lock_irq(&phba->ndlp_lock);
1369                         NLP_SET_FREE_REQ(ndlp);
1370                         spin_unlock_irq(&phba->ndlp_lock);
1371                 }
1372
1373                 /* Two ndlps cannot have the same did on the nodelist */
1374                 ndlp->nlp_DID = keepDID;
1375                 lpfc_drop_node(vport, ndlp);
1376         }
1377         else {
1378                 lpfc_unreg_rpi(vport, ndlp);
1379                 /* Two ndlps cannot have the same did */
1380                 ndlp->nlp_DID = keepDID;
1381                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1382         }
1383         return new_ndlp;
1384 }
1385
1386 /**
1387  * lpfc_end_rscn - Check and handle more rscn for a vport
1388  * @vport: pointer to a host virtual N_Port data structure.
1389  *
1390  * This routine checks whether more Registration State Change
1391  * Notifications (RSCNs) came in while the discovery state machine was in
1392  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1393  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1394  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1395  * handling the RSCNs.
1396  **/
1397 void
1398 lpfc_end_rscn(struct lpfc_vport *vport)
1399 {
1400         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1401
1402         if (vport->fc_flag & FC_RSCN_MODE) {
1403                 /*
1404                  * Check to see if more RSCNs came in while we were
1405                  * processing this one.
1406                  */
1407                 if (vport->fc_rscn_id_cnt ||
1408                     (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1409                         lpfc_els_handle_rscn(vport);
1410                 else {
1411                         spin_lock_irq(shost->host_lock);
1412                         vport->fc_flag &= ~FC_RSCN_MODE;
1413                         spin_unlock_irq(shost->host_lock);
1414                 }
1415         }
1416 }
1417
1418 /**
1419  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1420  * @phba: pointer to lpfc hba data structure.
1421  * @cmdiocb: pointer to lpfc command iocb data structure.
1422  * @rspiocb: pointer to lpfc response iocb data structure.
1423  *
1424  * This routine is the completion callback function for issuing the Port
1425  * Login (PLOGI) command. For PLOGI completion, there must be an active
1426  * ndlp on the vport node list that matches the remote node ID from the
1427  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1428  * ignored and command IOCB released. The PLOGI response IOCB status is
1429  * checked for error conditons. If there is error status reported, PLOGI
1430  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1431  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1432  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1433  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1434  * there are additional N_Port nodes with the vport that need to perform
1435  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1436  * PLOGIs.
1437  **/
1438 static void
1439 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1440                     struct lpfc_iocbq *rspiocb)
1441 {
1442         struct lpfc_vport *vport = cmdiocb->vport;
1443         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1444         IOCB_t *irsp;
1445         struct lpfc_nodelist *ndlp;
1446         struct lpfc_dmabuf *prsp;
1447         int disc, rc, did, type;
1448
1449         /* we pass cmdiocb to state machine which needs rspiocb as well */
1450         cmdiocb->context_un.rsp_iocb = rspiocb;
1451
1452         irsp = &rspiocb->iocb;
1453         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1454                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
1455                 irsp->ulpStatus, irsp->un.ulpWord[4],
1456                 irsp->un.elsreq64.remoteID);
1457
1458         ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1459         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1460                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1461                                  "0136 PLOGI completes to NPort x%x "
1462                                  "with no ndlp. Data: x%x x%x x%x\n",
1463                                  irsp->un.elsreq64.remoteID,
1464                                  irsp->ulpStatus, irsp->un.ulpWord[4],
1465                                  irsp->ulpIoTag);
1466                 goto out;
1467         }
1468
1469         /* Since ndlp can be freed in the disc state machine, note if this node
1470          * is being used during discovery.
1471          */
1472         spin_lock_irq(shost->host_lock);
1473         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1474         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1475         spin_unlock_irq(shost->host_lock);
1476         rc   = 0;
1477
1478         /* PLOGI completes to NPort <nlp_DID> */
1479         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1480                          "0102 PLOGI completes to NPort x%x "
1481                          "Data: x%x x%x x%x x%x x%x\n",
1482                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1483                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1484         /* Check to see if link went down during discovery */
1485         if (lpfc_els_chk_latt(vport)) {
1486                 spin_lock_irq(shost->host_lock);
1487                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1488                 spin_unlock_irq(shost->host_lock);
1489                 goto out;
1490         }
1491
1492         /* ndlp could be freed in DSM, save these values now */
1493         type = ndlp->nlp_type;
1494         did = ndlp->nlp_DID;
1495
1496         if (irsp->ulpStatus) {
1497                 /* Check for retry */
1498                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1499                         /* ELS command is being retried */
1500                         if (disc) {
1501                                 spin_lock_irq(shost->host_lock);
1502                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1503                                 spin_unlock_irq(shost->host_lock);
1504                         }
1505                         goto out;
1506                 }
1507                 /* PLOGI failed Don't print the vport to vport rjts */
1508                 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1509                         (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1510                         ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1511                         (phba)->pport->cfg_log_verbose & LOG_ELS)
1512                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1513                                  "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1514                                  ndlp->nlp_DID, irsp->ulpStatus,
1515                                  irsp->un.ulpWord[4]);
1516                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1517                 if (lpfc_error_lost_link(irsp))
1518                         rc = NLP_STE_FREED_NODE;
1519                 else
1520                         rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1521                                                      NLP_EVT_CMPL_PLOGI);
1522         } else {
1523                 /* Good status, call state machine */
1524                 prsp = list_entry(((struct lpfc_dmabuf *)
1525                                    cmdiocb->context2)->list.next,
1526                                   struct lpfc_dmabuf, list);
1527                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1528                 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1529                                              NLP_EVT_CMPL_PLOGI);
1530         }
1531
1532         if (disc && vport->num_disc_nodes) {
1533                 /* Check to see if there are more PLOGIs to be sent */
1534                 lpfc_more_plogi(vport);
1535
1536                 if (vport->num_disc_nodes == 0) {
1537                         spin_lock_irq(shost->host_lock);
1538                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
1539                         spin_unlock_irq(shost->host_lock);
1540
1541                         lpfc_can_disctmo(vport);
1542                         lpfc_end_rscn(vport);
1543                 }
1544         }
1545
1546 out:
1547         lpfc_els_free_iocb(phba, cmdiocb);
1548         return;
1549 }
1550
1551 /**
1552  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1553  * @vport: pointer to a host virtual N_Port data structure.
1554  * @did: destination port identifier.
1555  * @retry: number of retries to the command IOCB.
1556  *
1557  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1558  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1559  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1560  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1561  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1562  *
1563  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1564  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1565  * will be stored into the context1 field of the IOCB for the completion
1566  * callback function to the PLOGI ELS command.
1567  *
1568  * Return code
1569  *   0 - Successfully issued a plogi for @vport
1570  *   1 - failed to issue a plogi for @vport
1571  **/
1572 int
1573 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1574 {
1575         struct lpfc_hba  *phba = vport->phba;
1576         struct serv_parm *sp;
1577         IOCB_t *icmd;
1578         struct lpfc_nodelist *ndlp;
1579         struct lpfc_iocbq *elsiocb;
1580         struct lpfc_sli *psli;
1581         uint8_t *pcmd;
1582         uint16_t cmdsize;
1583         int ret;
1584
1585         psli = &phba->sli;
1586
1587         ndlp = lpfc_findnode_did(vport, did);
1588         if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1589                 ndlp = NULL;
1590
1591         /* If ndlp is not NULL, we will bump the reference count on it */
1592         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1593         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1594                                      ELS_CMD_PLOGI);
1595         if (!elsiocb)
1596                 return 1;
1597
1598         icmd = &elsiocb->iocb;
1599         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1600
1601         /* For PLOGI request, remainder of payload is service parameters */
1602         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1603         pcmd += sizeof(uint32_t);
1604         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1605         sp = (struct serv_parm *) pcmd;
1606
1607         if (sp->cmn.fcphLow < FC_PH_4_3)
1608                 sp->cmn.fcphLow = FC_PH_4_3;
1609
1610         if (sp->cmn.fcphHigh < FC_PH3)
1611                 sp->cmn.fcphHigh = FC_PH3;
1612
1613         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1614                 "Issue PLOGI:     did:x%x",
1615                 did, 0, 0);
1616
1617         phba->fc_stat.elsXmitPLOGI++;
1618         elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1619         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
1620
1621         if (ret == IOCB_ERROR) {
1622                 lpfc_els_free_iocb(phba, elsiocb);
1623                 return 1;
1624         }
1625         return 0;
1626 }
1627
1628 /**
1629  * lpfc_cmpl_els_prli - Completion callback function for prli
1630  * @phba: pointer to lpfc hba data structure.
1631  * @cmdiocb: pointer to lpfc command iocb data structure.
1632  * @rspiocb: pointer to lpfc response iocb data structure.
1633  *
1634  * This routine is the completion callback function for a Process Login
1635  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1636  * status. If there is error status reported, PRLI retry shall be attempted
1637  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1638  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1639  * ndlp to mark the PRLI completion.
1640  **/
1641 static void
1642 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1643                    struct lpfc_iocbq *rspiocb)
1644 {
1645         struct lpfc_vport *vport = cmdiocb->vport;
1646         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1647         IOCB_t *irsp;
1648         struct lpfc_sli *psli;
1649         struct lpfc_nodelist *ndlp;
1650
1651         psli = &phba->sli;
1652         /* we pass cmdiocb to state machine which needs rspiocb as well */
1653         cmdiocb->context_un.rsp_iocb = rspiocb;
1654
1655         irsp = &(rspiocb->iocb);
1656         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1657         spin_lock_irq(shost->host_lock);
1658         ndlp->nlp_flag &= ~NLP_PRLI_SND;
1659         spin_unlock_irq(shost->host_lock);
1660
1661         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1662                 "PRLI cmpl:       status:x%x/x%x did:x%x",
1663                 irsp->ulpStatus, irsp->un.ulpWord[4],
1664                 ndlp->nlp_DID);
1665         /* PRLI completes to NPort <nlp_DID> */
1666         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1667                          "0103 PRLI completes to NPort x%x "
1668                          "Data: x%x x%x x%x x%x\n",
1669                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1670                          irsp->ulpTimeout, vport->num_disc_nodes);
1671
1672         vport->fc_prli_sent--;
1673         /* Check to see if link went down during discovery */
1674         if (lpfc_els_chk_latt(vport))
1675                 goto out;
1676
1677         if (irsp->ulpStatus) {
1678                 /* Check for retry */
1679                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1680                         /* ELS command is being retried */
1681                         goto out;
1682                 }
1683                 /* PRLI failed */
1684                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1685                                  "2754 PRLI failure DID:%06X Status:x%x/x%x\n",
1686                                  ndlp->nlp_DID, irsp->ulpStatus,
1687                                  irsp->un.ulpWord[4]);
1688                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1689                 if (lpfc_error_lost_link(irsp))
1690                         goto out;
1691                 else
1692                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1693                                                 NLP_EVT_CMPL_PRLI);
1694         } else
1695                 /* Good status, call state machine */
1696                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1697                                         NLP_EVT_CMPL_PRLI);
1698 out:
1699         lpfc_els_free_iocb(phba, cmdiocb);
1700         return;
1701 }
1702
1703 /**
1704  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
1705  * @vport: pointer to a host virtual N_Port data structure.
1706  * @ndlp: pointer to a node-list data structure.
1707  * @retry: number of retries to the command IOCB.
1708  *
1709  * This routine issues a Process Login (PRLI) ELS command for the
1710  * @vport. The PRLI service parameters are set up in the payload of the
1711  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1712  * is put to the IOCB completion callback func field before invoking the
1713  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1714  *
1715  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1716  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1717  * will be stored into the context1 field of the IOCB for the completion
1718  * callback function to the PRLI ELS command.
1719  *
1720  * Return code
1721  *   0 - successfully issued prli iocb command for @vport
1722  *   1 - failed to issue prli iocb command for @vport
1723  **/
1724 int
1725 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1726                     uint8_t retry)
1727 {
1728         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1729         struct lpfc_hba *phba = vport->phba;
1730         PRLI *npr;
1731         IOCB_t *icmd;
1732         struct lpfc_iocbq *elsiocb;
1733         uint8_t *pcmd;
1734         uint16_t cmdsize;
1735
1736         cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1737         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1738                                      ndlp->nlp_DID, ELS_CMD_PRLI);
1739         if (!elsiocb)
1740                 return 1;
1741
1742         icmd = &elsiocb->iocb;
1743         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1744
1745         /* For PRLI request, remainder of payload is service parameters */
1746         memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1747         *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1748         pcmd += sizeof(uint32_t);
1749
1750         /* For PRLI, remainder of payload is PRLI parameter page */
1751         npr = (PRLI *) pcmd;
1752         /*
1753          * If our firmware version is 3.20 or later,
1754          * set the following bits for FC-TAPE support.
1755          */
1756         if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1757                 npr->ConfmComplAllowed = 1;
1758                 npr->Retry = 1;
1759                 npr->TaskRetryIdReq = 1;
1760         }
1761         npr->estabImagePair = 1;
1762         npr->readXferRdyDis = 1;
1763
1764         /* For FCP support */
1765         npr->prliType = PRLI_FCP_TYPE;
1766         npr->initiatorFunc = 1;
1767
1768         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1769                 "Issue PRLI:      did:x%x",
1770                 ndlp->nlp_DID, 0, 0);
1771
1772         phba->fc_stat.elsXmitPRLI++;
1773         elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1774         spin_lock_irq(shost->host_lock);
1775         ndlp->nlp_flag |= NLP_PRLI_SND;
1776         spin_unlock_irq(shost->host_lock);
1777         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
1778             IOCB_ERROR) {
1779                 spin_lock_irq(shost->host_lock);
1780                 ndlp->nlp_flag &= ~NLP_PRLI_SND;
1781                 spin_unlock_irq(shost->host_lock);
1782                 lpfc_els_free_iocb(phba, elsiocb);
1783                 return 1;
1784         }
1785         vport->fc_prli_sent++;
1786         return 0;
1787 }
1788
1789 /**
1790  * lpfc_rscn_disc - Perform rscn discovery for a vport
1791  * @vport: pointer to a host virtual N_Port data structure.
1792  *
1793  * This routine performs Registration State Change Notification (RSCN)
1794  * discovery for a @vport. If the @vport's node port recovery count is not
1795  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1796  * the nodes that need recovery. If none of the PLOGI were needed through
1797  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1798  * invoked to check and handle possible more RSCN came in during the period
1799  * of processing the current ones.
1800  **/
1801 static void
1802 lpfc_rscn_disc(struct lpfc_vport *vport)
1803 {
1804         lpfc_can_disctmo(vport);
1805
1806         /* RSCN discovery */
1807         /* go thru NPR nodes and issue ELS PLOGIs */
1808         if (vport->fc_npr_cnt)
1809                 if (lpfc_els_disc_plogi(vport))
1810                         return;
1811
1812         lpfc_end_rscn(vport);
1813 }
1814
1815 /**
1816  * lpfc_adisc_done - Complete the adisc phase of discovery
1817  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1818  *
1819  * This function is called when the final ADISC is completed during discovery.
1820  * This function handles clearing link attention or issuing reg_vpi depending
1821  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1822  * discovery.
1823  * This function is called with no locks held.
1824  **/
1825 static void
1826 lpfc_adisc_done(struct lpfc_vport *vport)
1827 {
1828         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1829         struct lpfc_hba   *phba = vport->phba;
1830
1831         /*
1832          * For NPIV, cmpl_reg_vpi will set port_state to READY,
1833          * and continue discovery.
1834          */
1835         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1836             !(vport->fc_flag & FC_RSCN_MODE) &&
1837             (phba->sli_rev < LPFC_SLI_REV4)) {
1838                 lpfc_issue_reg_vpi(phba, vport);
1839                 return;
1840         }
1841         /*
1842         * For SLI2, we need to set port_state to READY
1843         * and continue discovery.
1844         */
1845         if (vport->port_state < LPFC_VPORT_READY) {
1846                 /* If we get here, there is nothing to ADISC */
1847                 if (vport->port_type == LPFC_PHYSICAL_PORT)
1848                         lpfc_issue_clear_la(phba, vport);
1849                 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1850                         vport->num_disc_nodes = 0;
1851                         /* go thru NPR list, issue ELS PLOGIs */
1852                         if (vport->fc_npr_cnt)
1853                                 lpfc_els_disc_plogi(vport);
1854                         if (!vport->num_disc_nodes) {
1855                                 spin_lock_irq(shost->host_lock);
1856                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1857                                 spin_unlock_irq(shost->host_lock);
1858                                 lpfc_can_disctmo(vport);
1859                                 lpfc_end_rscn(vport);
1860                         }
1861                 }
1862                 vport->port_state = LPFC_VPORT_READY;
1863         } else
1864                 lpfc_rscn_disc(vport);
1865 }
1866
1867 /**
1868  * lpfc_more_adisc - Issue more adisc as needed
1869  * @vport: pointer to a host virtual N_Port data structure.
1870  *
1871  * This routine determines whether there are more ndlps on a @vport
1872  * node list need to have Address Discover (ADISC) issued. If so, it will
1873  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1874  * remaining nodes which need to have ADISC sent.
1875  **/
1876 void
1877 lpfc_more_adisc(struct lpfc_vport *vport)
1878 {
1879         int sentadisc;
1880
1881         if (vport->num_disc_nodes)
1882                 vport->num_disc_nodes--;
1883         /* Continue discovery with <num_disc_nodes> ADISCs to go */
1884         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1885                          "0210 Continue discovery with %d ADISCs to go "
1886                          "Data: x%x x%x x%x\n",
1887                          vport->num_disc_nodes, vport->fc_adisc_cnt,
1888                          vport->fc_flag, vport->port_state);
1889         /* Check to see if there are more ADISCs to be sent */
1890         if (vport->fc_flag & FC_NLP_MORE) {
1891                 lpfc_set_disctmo(vport);
1892                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
1893                 sentadisc = lpfc_els_disc_adisc(vport);
1894         }
1895         if (!vport->num_disc_nodes)
1896                 lpfc_adisc_done(vport);
1897         return;
1898 }
1899
1900 /**
1901  * lpfc_cmpl_els_adisc - Completion callback function for adisc
1902  * @phba: pointer to lpfc hba data structure.
1903  * @cmdiocb: pointer to lpfc command iocb data structure.
1904  * @rspiocb: pointer to lpfc response iocb data structure.
1905  *
1906  * This routine is the completion function for issuing the Address Discover
1907  * (ADISC) command. It first checks to see whether link went down during
1908  * the discovery process. If so, the node will be marked as node port
1909  * recovery for issuing discover IOCB by the link attention handler and
1910  * exit. Otherwise, the response status is checked. If error was reported
1911  * in the response status, the ADISC command shall be retried by invoking
1912  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1913  * the response status, the state machine is invoked to set transition
1914  * with respect to NLP_EVT_CMPL_ADISC event.
1915  **/
1916 static void
1917 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1918                     struct lpfc_iocbq *rspiocb)
1919 {
1920         struct lpfc_vport *vport = cmdiocb->vport;
1921         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1922         IOCB_t *irsp;
1923         struct lpfc_nodelist *ndlp;
1924         int  disc;
1925
1926         /* we pass cmdiocb to state machine which needs rspiocb as well */
1927         cmdiocb->context_un.rsp_iocb = rspiocb;
1928
1929         irsp = &(rspiocb->iocb);
1930         ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1931
1932         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1933                 "ADISC cmpl:      status:x%x/x%x did:x%x",
1934                 irsp->ulpStatus, irsp->un.ulpWord[4],
1935                 ndlp->nlp_DID);
1936
1937         /* Since ndlp can be freed in the disc state machine, note if this node
1938          * is being used during discovery.
1939          */
1940         spin_lock_irq(shost->host_lock);
1941         disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1942         ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1943         spin_unlock_irq(shost->host_lock);
1944         /* ADISC completes to NPort <nlp_DID> */
1945         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1946                          "0104 ADISC completes to NPort x%x "
1947                          "Data: x%x x%x x%x x%x x%x\n",
1948                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1949                          irsp->ulpTimeout, disc, vport->num_disc_nodes);
1950         /* Check to see if link went down during discovery */
1951         if (lpfc_els_chk_latt(vport)) {
1952                 spin_lock_irq(shost->host_lock);
1953                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1954                 spin_unlock_irq(shost->host_lock);
1955                 goto out;
1956         }
1957
1958         if (irsp->ulpStatus) {
1959                 /* Check for retry */
1960                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1961                         /* ELS command is being retried */
1962                         if (disc) {
1963                                 spin_lock_irq(shost->host_lock);
1964                                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1965                                 spin_unlock_irq(shost->host_lock);
1966                                 lpfc_set_disctmo(vport);
1967                         }
1968                         goto out;
1969                 }
1970                 /* ADISC failed */
1971                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1972                                  "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
1973                                  ndlp->nlp_DID, irsp->ulpStatus,
1974                                  irsp->un.ulpWord[4]);
1975                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1976                 if (!lpfc_error_lost_link(irsp))
1977                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1978                                                 NLP_EVT_CMPL_ADISC);
1979         } else
1980                 /* Good status, call state machine */
1981                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1982                                         NLP_EVT_CMPL_ADISC);
1983
1984         /* Check to see if there are more ADISCs to be sent */
1985         if (disc && vport->num_disc_nodes)
1986                 lpfc_more_adisc(vport);
1987 out:
1988         lpfc_els_free_iocb(phba, cmdiocb);
1989         return;
1990 }
1991
1992 /**
1993  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
1994  * @vport: pointer to a virtual N_Port data structure.
1995  * @ndlp: pointer to a node-list data structure.
1996  * @retry: number of retries to the command IOCB.
1997  *
1998  * This routine issues an Address Discover (ADISC) for an @ndlp on a
1999  * @vport. It prepares the payload of the ADISC ELS command, updates the
2000  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2001  * to issue the ADISC ELS command.
2002  *
2003  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2004  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2005  * will be stored into the context1 field of the IOCB for the completion
2006  * callback function to the ADISC ELS command.
2007  *
2008  * Return code
2009  *   0 - successfully issued adisc
2010  *   1 - failed to issue adisc
2011  **/
2012 int
2013 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2014                      uint8_t retry)
2015 {
2016         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2017         struct lpfc_hba  *phba = vport->phba;
2018         ADISC *ap;
2019         IOCB_t *icmd;
2020         struct lpfc_iocbq *elsiocb;
2021         uint8_t *pcmd;
2022         uint16_t cmdsize;
2023
2024         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2025         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2026                                      ndlp->nlp_DID, ELS_CMD_ADISC);
2027         if (!elsiocb)
2028                 return 1;
2029
2030         icmd = &elsiocb->iocb;
2031         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2032
2033         /* For ADISC request, remainder of payload is service parameters */
2034         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2035         pcmd += sizeof(uint32_t);
2036
2037         /* Fill in ADISC payload */
2038         ap = (ADISC *) pcmd;
2039         ap->hardAL_PA = phba->fc_pref_ALPA;
2040         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2041         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2042         ap->DID = be32_to_cpu(vport->fc_myDID);
2043
2044         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2045                 "Issue ADISC:     did:x%x",
2046                 ndlp->nlp_DID, 0, 0);
2047
2048         phba->fc_stat.elsXmitADISC++;
2049         elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2050         spin_lock_irq(shost->host_lock);
2051         ndlp->nlp_flag |= NLP_ADISC_SND;
2052         spin_unlock_irq(shost->host_lock);
2053         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2054             IOCB_ERROR) {
2055                 spin_lock_irq(shost->host_lock);
2056                 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2057                 spin_unlock_irq(shost->host_lock);
2058                 lpfc_els_free_iocb(phba, elsiocb);
2059                 return 1;
2060         }
2061         return 0;
2062 }
2063
2064 /**
2065  * lpfc_cmpl_els_logo - Completion callback function for logo
2066  * @phba: pointer to lpfc hba data structure.
2067  * @cmdiocb: pointer to lpfc command iocb data structure.
2068  * @rspiocb: pointer to lpfc response iocb data structure.
2069  *
2070  * This routine is the completion function for issuing the ELS Logout (LOGO)
2071  * command. If no error status was reported from the LOGO response, the
2072  * state machine of the associated ndlp shall be invoked for transition with
2073  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2074  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2075  **/
2076 static void
2077 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2078                    struct lpfc_iocbq *rspiocb)
2079 {
2080         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2081         struct lpfc_vport *vport = ndlp->vport;
2082         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2083         IOCB_t *irsp;
2084         struct lpfc_sli *psli;
2085
2086         psli = &phba->sli;
2087         /* we pass cmdiocb to state machine which needs rspiocb as well */
2088         cmdiocb->context_un.rsp_iocb = rspiocb;
2089
2090         irsp = &(rspiocb->iocb);
2091         spin_lock_irq(shost->host_lock);
2092         ndlp->nlp_flag &= ~NLP_LOGO_SND;
2093         spin_unlock_irq(shost->host_lock);
2094
2095         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2096                 "LOGO cmpl:       status:x%x/x%x did:x%x",
2097                 irsp->ulpStatus, irsp->un.ulpWord[4],
2098                 ndlp->nlp_DID);
2099         /* LOGO completes to NPort <nlp_DID> */
2100         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2101                          "0105 LOGO completes to NPort x%x "
2102                          "Data: x%x x%x x%x x%x\n",
2103                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2104                          irsp->ulpTimeout, vport->num_disc_nodes);
2105         /* Check to see if link went down during discovery */
2106         if (lpfc_els_chk_latt(vport))
2107                 goto out;
2108
2109         if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2110                 /* NLP_EVT_DEVICE_RM should unregister the RPI
2111                  * which should abort all outstanding IOs.
2112                  */
2113                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2114                                         NLP_EVT_DEVICE_RM);
2115                 goto out;
2116         }
2117
2118         if (irsp->ulpStatus) {
2119                 /* Check for retry */
2120                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
2121                         /* ELS command is being retried */
2122                         goto out;
2123                 /* LOGO failed */
2124                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2125                                  "2756 LOGO failure DID:%06X Status:x%x/x%x\n",
2126                                  ndlp->nlp_DID, irsp->ulpStatus,
2127                                  irsp->un.ulpWord[4]);
2128                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2129                 if (lpfc_error_lost_link(irsp))
2130                         goto out;
2131                 else
2132                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2133                                                 NLP_EVT_CMPL_LOGO);
2134         } else
2135                 /* Good status, call state machine.
2136                  * This will unregister the rpi if needed.
2137                  */
2138                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2139                                         NLP_EVT_CMPL_LOGO);
2140 out:
2141         lpfc_els_free_iocb(phba, cmdiocb);
2142         return;
2143 }
2144
2145 /**
2146  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2147  * @vport: pointer to a virtual N_Port data structure.
2148  * @ndlp: pointer to a node-list data structure.
2149  * @retry: number of retries to the command IOCB.
2150  *
2151  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2152  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2153  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2154  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2155  *
2156  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2157  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2158  * will be stored into the context1 field of the IOCB for the completion
2159  * callback function to the LOGO ELS command.
2160  *
2161  * Return code
2162  *   0 - successfully issued logo
2163  *   1 - failed to issue logo
2164  **/
2165 int
2166 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2167                     uint8_t retry)
2168 {
2169         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2170         struct lpfc_hba  *phba = vport->phba;
2171         IOCB_t *icmd;
2172         struct lpfc_iocbq *elsiocb;
2173         uint8_t *pcmd;
2174         uint16_t cmdsize;
2175         int rc;
2176
2177         spin_lock_irq(shost->host_lock);
2178         if (ndlp->nlp_flag & NLP_LOGO_SND) {
2179                 spin_unlock_irq(shost->host_lock);
2180                 return 0;
2181         }
2182         spin_unlock_irq(shost->host_lock);
2183
2184         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2185         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2186                                      ndlp->nlp_DID, ELS_CMD_LOGO);
2187         if (!elsiocb)
2188                 return 1;
2189
2190         icmd = &elsiocb->iocb;
2191         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2192         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2193         pcmd += sizeof(uint32_t);
2194
2195         /* Fill in LOGO payload */
2196         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2197         pcmd += sizeof(uint32_t);
2198         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2199
2200         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2201                 "Issue LOGO:      did:x%x",
2202                 ndlp->nlp_DID, 0, 0);
2203
2204         phba->fc_stat.elsXmitLOGO++;
2205         elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2206         spin_lock_irq(shost->host_lock);
2207         ndlp->nlp_flag |= NLP_LOGO_SND;
2208         spin_unlock_irq(shost->host_lock);
2209         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2210
2211         if (rc == IOCB_ERROR) {
2212                 spin_lock_irq(shost->host_lock);
2213                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2214                 spin_unlock_irq(shost->host_lock);
2215                 lpfc_els_free_iocb(phba, elsiocb);
2216                 return 1;
2217         }
2218         return 0;
2219 }
2220
2221 /**
2222  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
2223  * @phba: pointer to lpfc hba data structure.
2224  * @cmdiocb: pointer to lpfc command iocb data structure.
2225  * @rspiocb: pointer to lpfc response iocb data structure.
2226  *
2227  * This routine is a generic completion callback function for ELS commands.
2228  * Specifically, it is the callback function which does not need to perform
2229  * any command specific operations. It is currently used by the ELS command
2230  * issuing routines for the ELS State Change  Request (SCR),
2231  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2232  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2233  * certain debug loggings, this callback function simply invokes the
2234  * lpfc_els_chk_latt() routine to check whether link went down during the
2235  * discovery process.
2236  **/
2237 static void
2238 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2239                   struct lpfc_iocbq *rspiocb)
2240 {
2241         struct lpfc_vport *vport = cmdiocb->vport;
2242         IOCB_t *irsp;
2243
2244         irsp = &rspiocb->iocb;
2245
2246         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2247                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
2248                 irsp->ulpStatus, irsp->un.ulpWord[4],
2249                 irsp->un.elsreq64.remoteID);
2250         /* ELS cmd tag <ulpIoTag> completes */
2251         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2252                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2253                          irsp->ulpIoTag, irsp->ulpStatus,
2254                          irsp->un.ulpWord[4], irsp->ulpTimeout);
2255         /* Check to see if link went down during discovery */
2256         lpfc_els_chk_latt(vport);
2257         lpfc_els_free_iocb(phba, cmdiocb);
2258         return;
2259 }
2260
2261 /**
2262  * lpfc_issue_els_scr - Issue a scr to an node on a vport
2263  * @vport: pointer to a host virtual N_Port data structure.
2264  * @nportid: N_Port identifier to the remote node.
2265  * @retry: number of retries to the command IOCB.
2266  *
2267  * This routine issues a State Change Request (SCR) to a fabric node
2268  * on a @vport. The remote node @nportid is passed into the function. It
2269  * first search the @vport node list to find the matching ndlp. If no such
2270  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2271  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2272  * routine is invoked to send the SCR IOCB.
2273  *
2274  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2275  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2276  * will be stored into the context1 field of the IOCB for the completion
2277  * callback function to the SCR ELS command.
2278  *
2279  * Return code
2280  *   0 - Successfully issued scr command
2281  *   1 - Failed to issue scr command
2282  **/
2283 int
2284 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2285 {
2286         struct lpfc_hba  *phba = vport->phba;
2287         IOCB_t *icmd;
2288         struct lpfc_iocbq *elsiocb;
2289         struct lpfc_sli *psli;
2290         uint8_t *pcmd;
2291         uint16_t cmdsize;
2292         struct lpfc_nodelist *ndlp;
2293
2294         psli = &phba->sli;
2295         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2296
2297         ndlp = lpfc_findnode_did(vport, nportid);
2298         if (!ndlp) {
2299                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2300                 if (!ndlp)
2301                         return 1;
2302                 lpfc_nlp_init(vport, ndlp, nportid);
2303                 lpfc_enqueue_node(vport, ndlp);
2304         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2305                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2306                 if (!ndlp)
2307                         return 1;
2308         }
2309
2310         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2311                                      ndlp->nlp_DID, ELS_CMD_SCR);
2312
2313         if (!elsiocb) {
2314                 /* This will trigger the release of the node just
2315                  * allocated
2316                  */
2317                 lpfc_nlp_put(ndlp);
2318                 return 1;
2319         }
2320
2321         icmd = &elsiocb->iocb;
2322         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2323
2324         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2325         pcmd += sizeof(uint32_t);
2326
2327         /* For SCR, remainder of payload is SCR parameter page */
2328         memset(pcmd, 0, sizeof(SCR));
2329         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2330
2331         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2332                 "Issue SCR:       did:x%x",
2333                 ndlp->nlp_DID, 0, 0);
2334
2335         phba->fc_stat.elsXmitSCR++;
2336         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2337         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2338             IOCB_ERROR) {
2339                 /* The additional lpfc_nlp_put will cause the following
2340                  * lpfc_els_free_iocb routine to trigger the rlease of
2341                  * the node.
2342                  */
2343                 lpfc_nlp_put(ndlp);
2344                 lpfc_els_free_iocb(phba, elsiocb);
2345                 return 1;
2346         }
2347         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2348          * trigger the release of node.
2349          */
2350         lpfc_nlp_put(ndlp);
2351         return 0;
2352 }
2353
2354 /**
2355  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2356  * @vport: pointer to a host virtual N_Port data structure.
2357  * @nportid: N_Port identifier to the remote node.
2358  * @retry: number of retries to the command IOCB.
2359  *
2360  * This routine issues a Fibre Channel Address Resolution Response
2361  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2362  * is passed into the function. It first search the @vport node list to find
2363  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2364  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2365  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2366  *
2367  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2368  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2369  * will be stored into the context1 field of the IOCB for the completion
2370  * callback function to the PARPR ELS command.
2371  *
2372  * Return code
2373  *   0 - Successfully issued farpr command
2374  *   1 - Failed to issue farpr command
2375  **/
2376 static int
2377 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2378 {
2379         struct lpfc_hba  *phba = vport->phba;
2380         IOCB_t *icmd;
2381         struct lpfc_iocbq *elsiocb;
2382         struct lpfc_sli *psli;
2383         FARP *fp;
2384         uint8_t *pcmd;
2385         uint32_t *lp;
2386         uint16_t cmdsize;
2387         struct lpfc_nodelist *ondlp;
2388         struct lpfc_nodelist *ndlp;
2389
2390         psli = &phba->sli;
2391         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2392
2393         ndlp = lpfc_findnode_did(vport, nportid);
2394         if (!ndlp) {
2395                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2396                 if (!ndlp)
2397                         return 1;
2398                 lpfc_nlp_init(vport, ndlp, nportid);
2399                 lpfc_enqueue_node(vport, ndlp);
2400         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2401                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2402                 if (!ndlp)
2403                         return 1;
2404         }
2405
2406         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2407                                      ndlp->nlp_DID, ELS_CMD_RNID);
2408         if (!elsiocb) {
2409                 /* This will trigger the release of the node just
2410                  * allocated
2411                  */
2412                 lpfc_nlp_put(ndlp);
2413                 return 1;
2414         }
2415
2416         icmd = &elsiocb->iocb;
2417         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2418
2419         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2420         pcmd += sizeof(uint32_t);
2421
2422         /* Fill in FARPR payload */
2423         fp = (FARP *) (pcmd);
2424         memset(fp, 0, sizeof(FARP));
2425         lp = (uint32_t *) pcmd;
2426         *lp++ = be32_to_cpu(nportid);
2427         *lp++ = be32_to_cpu(vport->fc_myDID);
2428         fp->Rflags = 0;
2429         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2430
2431         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2432         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2433         ondlp = lpfc_findnode_did(vport, nportid);
2434         if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2435                 memcpy(&fp->OportName, &ondlp->nlp_portname,
2436                        sizeof(struct lpfc_name));
2437                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2438                        sizeof(struct lpfc_name));
2439         }
2440
2441         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2442                 "Issue FARPR:     did:x%x",
2443                 ndlp->nlp_DID, 0, 0);
2444
2445         phba->fc_stat.elsXmitFARPR++;
2446         elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2447         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2448             IOCB_ERROR) {
2449                 /* The additional lpfc_nlp_put will cause the following
2450                  * lpfc_els_free_iocb routine to trigger the release of
2451                  * the node.
2452                  */
2453                 lpfc_nlp_put(ndlp);
2454                 lpfc_els_free_iocb(phba, elsiocb);
2455                 return 1;
2456         }
2457         /* This will cause the callback-function lpfc_cmpl_els_cmd to
2458          * trigger the release of the node.
2459          */
2460         lpfc_nlp_put(ndlp);
2461         return 0;
2462 }
2463
2464 /**
2465  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
2466  * @vport: pointer to a host virtual N_Port data structure.
2467  * @nlp: pointer to a node-list data structure.
2468  *
2469  * This routine cancels the timer with a delayed IOCB-command retry for
2470  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2471  * removes the ELS retry event if it presents. In addition, if the
2472  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2473  * commands are sent for the @vport's nodes that require issuing discovery
2474  * ADISC.
2475  **/
2476 void
2477 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2478 {
2479         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2480         struct lpfc_work_evt *evtp;
2481
2482         if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2483                 return;
2484         spin_lock_irq(shost->host_lock);
2485         nlp->nlp_flag &= ~NLP_DELAY_TMO;
2486         spin_unlock_irq(shost->host_lock);
2487         del_timer_sync(&nlp->nlp_delayfunc);
2488         nlp->nlp_last_elscmd = 0;
2489         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2490                 list_del_init(&nlp->els_retry_evt.evt_listp);
2491                 /* Decrement nlp reference count held for the delayed retry */
2492                 evtp = &nlp->els_retry_evt;
2493                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2494         }
2495         if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2496                 spin_lock_irq(shost->host_lock);
2497                 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2498                 spin_unlock_irq(shost->host_lock);
2499                 if (vport->num_disc_nodes) {
2500                         if (vport->port_state < LPFC_VPORT_READY) {
2501                                 /* Check if there are more ADISCs to be sent */
2502                                 lpfc_more_adisc(vport);
2503                         } else {
2504                                 /* Check if there are more PLOGIs to be sent */
2505                                 lpfc_more_plogi(vport);
2506                                 if (vport->num_disc_nodes == 0) {
2507                                         spin_lock_irq(shost->host_lock);
2508                                         vport->fc_flag &= ~FC_NDISC_ACTIVE;
2509                                         spin_unlock_irq(shost->host_lock);
2510                                         lpfc_can_disctmo(vport);
2511                                         lpfc_end_rscn(vport);
2512                                 }
2513                         }
2514                 }
2515         }
2516         return;
2517 }
2518
2519 /**
2520  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
2521  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2522  *
2523  * This routine is invoked by the ndlp delayed-function timer to check
2524  * whether there is any pending ELS retry event(s) with the node. If not, it
2525  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2526  * adds the delayed events to the HBA work list and invokes the
2527  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2528  * event. Note that lpfc_nlp_get() is called before posting the event to
2529  * the work list to hold reference count of ndlp so that it guarantees the
2530  * reference to ndlp will still be available when the worker thread gets
2531  * to the event associated with the ndlp.
2532  **/
2533 void
2534 lpfc_els_retry_delay(unsigned long ptr)
2535 {
2536         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2537         struct lpfc_vport *vport = ndlp->vport;
2538         struct lpfc_hba   *phba = vport->phba;
2539         unsigned long flags;
2540         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2541
2542         spin_lock_irqsave(&phba->hbalock, flags);
2543         if (!list_empty(&evtp->evt_listp)) {
2544                 spin_unlock_irqrestore(&phba->hbalock, flags);
2545                 return;
2546         }
2547
2548         /* We need to hold the node by incrementing the reference
2549          * count until the queued work is done
2550          */
2551         evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2552         if (evtp->evt_arg1) {
2553                 evtp->evt = LPFC_EVT_ELS_RETRY;
2554                 list_add_tail(&evtp->evt_listp, &phba->work_list);
2555                 lpfc_worker_wake_up(phba);
2556         }
2557         spin_unlock_irqrestore(&phba->hbalock, flags);
2558         return;
2559 }
2560
2561 /**
2562  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
2563  * @ndlp: pointer to a node-list data structure.
2564  *
2565  * This routine is the worker-thread handler for processing the @ndlp delayed
2566  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2567  * the last ELS command from the associated ndlp and invokes the proper ELS
2568  * function according to the delayed ELS command to retry the command.
2569  **/
2570 void
2571 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2572 {
2573         struct lpfc_vport *vport = ndlp->vport;
2574         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2575         uint32_t cmd, did, retry;
2576
2577         spin_lock_irq(shost->host_lock);
2578         did = ndlp->nlp_DID;
2579         cmd = ndlp->nlp_last_elscmd;
2580         ndlp->nlp_last_elscmd = 0;
2581
2582         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2583                 spin_unlock_irq(shost->host_lock);
2584                 return;
2585         }
2586
2587         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2588         spin_unlock_irq(shost->host_lock);
2589         /*
2590          * If a discovery event readded nlp_delayfunc after timer
2591          * firing and before processing the timer, cancel the
2592          * nlp_delayfunc.
2593          */
2594         del_timer_sync(&ndlp->nlp_delayfunc);
2595         retry = ndlp->nlp_retry;
2596         ndlp->nlp_retry = 0;
2597
2598         switch (cmd) {
2599         case ELS_CMD_FLOGI:
2600                 lpfc_issue_els_flogi(vport, ndlp, retry);
2601                 break;
2602         case ELS_CMD_PLOGI:
2603                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2604                         ndlp->nlp_prev_state = ndlp->nlp_state;
2605                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2606                 }
2607                 break;
2608         case ELS_CMD_ADISC:
2609                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2610                         ndlp->nlp_prev_state = ndlp->nlp_state;
2611                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2612                 }
2613                 break;
2614         case ELS_CMD_PRLI:
2615                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2616                         ndlp->nlp_prev_state = ndlp->nlp_state;
2617                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2618                 }
2619                 break;
2620         case ELS_CMD_LOGO:
2621                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2622                         ndlp->nlp_prev_state = ndlp->nlp_state;
2623                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2624                 }
2625                 break;
2626         case ELS_CMD_FDISC:
2627                 lpfc_issue_els_fdisc(vport, ndlp, retry);
2628                 break;
2629         }
2630         return;
2631 }
2632
2633 /**
2634  * lpfc_els_retry - Make retry decision on an els command iocb
2635  * @phba: pointer to lpfc hba data structure.
2636  * @cmdiocb: pointer to lpfc command iocb data structure.
2637  * @rspiocb: pointer to lpfc response iocb data structure.
2638  *
2639  * This routine makes a retry decision on an ELS command IOCB, which has
2640  * failed. The following ELS IOCBs use this function for retrying the command
2641  * when previously issued command responsed with error status: FLOGI, PLOGI,
2642  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2643  * returned error status, it makes the decision whether a retry shall be
2644  * issued for the command, and whether a retry shall be made immediately or
2645  * delayed. In the former case, the corresponding ELS command issuing-function
2646  * is called to retry the command. In the later case, the ELS command shall
2647  * be posted to the ndlp delayed event and delayed function timer set to the
2648  * ndlp for the delayed command issusing.
2649  *
2650  * Return code
2651  *   0 - No retry of els command is made
2652  *   1 - Immediate or delayed retry of els command is made
2653  **/
2654 static int
2655 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2656                struct lpfc_iocbq *rspiocb)
2657 {
2658         struct lpfc_vport *vport = cmdiocb->vport;
2659         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2660         IOCB_t *irsp = &rspiocb->iocb;
2661         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2662         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2663         uint32_t *elscmd;
2664         struct ls_rjt stat;
2665         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2666         int logerr = 0;
2667         uint32_t cmd = 0;
2668         uint32_t did;
2669
2670
2671         /* Note: context2 may be 0 for internal driver abort
2672          * of delays ELS command.
2673          */
2674
2675         if (pcmd && pcmd->virt) {
2676                 elscmd = (uint32_t *) (pcmd->virt);
2677                 cmd = *elscmd++;
2678         }
2679
2680         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2681                 did = ndlp->nlp_DID;
2682         else {
2683                 /* We should only hit this case for retrying PLOGI */
2684                 did = irsp->un.elsreq64.remoteID;
2685                 ndlp = lpfc_findnode_did(vport, did);
2686                 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2687                     && (cmd != ELS_CMD_PLOGI))
2688                         return 1;
2689         }
2690
2691         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2692                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2693                 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2694
2695         switch (irsp->ulpStatus) {
2696         case IOSTAT_FCP_RSP_ERROR:
2697         case IOSTAT_REMOTE_STOP:
2698                 break;
2699
2700         case IOSTAT_LOCAL_REJECT:
2701                 switch ((irsp->un.ulpWord[4] & 0xff)) {
2702                 case IOERR_LOOP_OPEN_FAILURE:
2703                         if (cmd == ELS_CMD_FLOGI) {
2704                                 if (PCI_DEVICE_ID_HORNET ==
2705                                         phba->pcidev->device) {
2706                                         phba->fc_topology = TOPOLOGY_LOOP;
2707                                         phba->pport->fc_myDID = 0;
2708                                         phba->alpa_map[0] = 0;
2709                                         phba->alpa_map[1] = 0;
2710                                 }
2711                         }
2712                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2713                                 delay = 1000;
2714                         retry = 1;
2715                         break;
2716
2717                 case IOERR_ILLEGAL_COMMAND:
2718                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2719                                          "0124 Retry illegal cmd x%x "
2720                                          "retry:x%x delay:x%x\n",
2721                                          cmd, cmdiocb->retry, delay);
2722                         retry = 1;
2723                         /* All command's retry policy */
2724                         maxretry = 8;
2725                         if (cmdiocb->retry > 2)
2726                                 delay = 1000;
2727                         break;
2728
2729                 case IOERR_NO_RESOURCES:
2730                         logerr = 1; /* HBA out of resources */
2731                         retry = 1;
2732                         if (cmdiocb->retry > 100)
2733                                 delay = 100;
2734                         maxretry = 250;
2735                         break;
2736
2737                 case IOERR_ILLEGAL_FRAME:
2738                         delay = 100;
2739                         retry = 1;
2740                         break;
2741
2742                 case IOERR_SEQUENCE_TIMEOUT:
2743                 case IOERR_INVALID_RPI:
2744                         retry = 1;
2745                         break;
2746                 }
2747                 break;
2748
2749         case IOSTAT_NPORT_RJT:
2750         case IOSTAT_FABRIC_RJT:
2751                 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2752                         retry = 1;
2753                         break;
2754                 }
2755                 break;
2756
2757         case IOSTAT_NPORT_BSY:
2758         case IOSTAT_FABRIC_BSY:
2759                 logerr = 1; /* Fabric / Remote NPort out of resources */
2760                 retry = 1;
2761                 break;
2762
2763         case IOSTAT_LS_RJT:
2764                 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2765                 /* Added for Vendor specifc support
2766                  * Just keep retrying for these Rsn / Exp codes
2767                  */
2768                 switch (stat.un.b.lsRjtRsnCode) {
2769                 case LSRJT_UNABLE_TPC:
2770                         if (stat.un.b.lsRjtRsnCodeExp ==
2771                             LSEXP_CMD_IN_PROGRESS) {
2772                                 if (cmd == ELS_CMD_PLOGI) {
2773                                         delay = 1000;
2774                                         maxretry = 48;
2775                                 }
2776                                 retry = 1;
2777                                 break;
2778                         }
2779                         if (stat.un.b.lsRjtRsnCodeExp ==
2780                             LSEXP_CANT_GIVE_DATA) {
2781                                 if (cmd == ELS_CMD_PLOGI) {
2782                                         delay = 1000;
2783                                         maxretry = 48;
2784                                 }
2785                                 retry = 1;
2786                                 break;
2787                         }
2788                         if (cmd == ELS_CMD_PLOGI) {
2789                                 delay = 1000;
2790                                 maxretry = lpfc_max_els_tries + 1;
2791                                 retry = 1;
2792                                 break;
2793                         }
2794                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2795                           (cmd == ELS_CMD_FDISC) &&
2796                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2797                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2798                                                  "0125 FDISC Failed (x%x). "
2799                                                  "Fabric out of resources\n",
2800                                                  stat.un.lsRjtError);
2801                                 lpfc_vport_set_state(vport,
2802                                                      FC_VPORT_NO_FABRIC_RSCS);
2803                         }
2804                         break;
2805
2806                 case LSRJT_LOGICAL_BSY:
2807                         if ((cmd == ELS_CMD_PLOGI) ||
2808                             (cmd == ELS_CMD_PRLI)) {
2809                                 delay = 1000;
2810                                 maxretry = 48;
2811                         } else if (cmd == ELS_CMD_FDISC) {
2812                                 /* FDISC retry policy */
2813                                 maxretry = 48;
2814                                 if (cmdiocb->retry >= 32)
2815                                         delay = 1000;
2816                         }
2817                         retry = 1;
2818                         break;
2819
2820                 case LSRJT_LOGICAL_ERR:
2821                         /* There are some cases where switches return this
2822                          * error when they are not ready and should be returning
2823                          * Logical Busy. We should delay every time.
2824                          */
2825                         if (cmd == ELS_CMD_FDISC &&
2826                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2827                                 maxretry = 3;
2828                                 delay = 1000;
2829                                 retry = 1;
2830                                 break;
2831                         }
2832                 case LSRJT_PROTOCOL_ERR:
2833                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2834                           (cmd == ELS_CMD_FDISC) &&
2835                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2836                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2837                           ) {
2838                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2839                                                  "0122 FDISC Failed (x%x). "
2840                                                  "Fabric Detected Bad WWN\n",
2841                                                  stat.un.lsRjtError);
2842                                 lpfc_vport_set_state(vport,
2843                                                      FC_VPORT_FABRIC_REJ_WWN);
2844                         }
2845                         break;
2846                 }
2847                 break;
2848
2849         case IOSTAT_INTERMED_RSP:
2850         case IOSTAT_BA_RJT:
2851                 break;
2852
2853         default:
2854                 break;
2855         }
2856
2857         if (did == FDMI_DID)
2858                 retry = 1;
2859
2860         if (((cmd == ELS_CMD_FLOGI) || (cmd == ELS_CMD_FDISC)) &&
2861             (phba->fc_topology != TOPOLOGY_LOOP) &&
2862             !lpfc_error_lost_link(irsp)) {
2863                 /* FLOGI retry policy */
2864                 retry = 1;
2865                 /* retry forever */
2866                 maxretry = 0;
2867                 if (cmdiocb->retry >= 100)
2868                         delay = 5000;
2869                 else if (cmdiocb->retry >= 32)
2870                         delay = 1000;
2871         }
2872
2873         cmdiocb->retry++;
2874         if (maxretry && (cmdiocb->retry >= maxretry)) {
2875                 phba->fc_stat.elsRetryExceeded++;
2876                 retry = 0;
2877         }
2878
2879         if ((vport->load_flag & FC_UNLOADING) != 0)
2880                 retry = 0;
2881
2882         if (retry) {
2883
2884                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
2885                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2886                                  "0107 Retry ELS command x%x to remote "
2887                                  "NPORT x%x Data: x%x x%x\n",
2888                                  cmd, did, cmdiocb->retry, delay);
2889
2890                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2891                         ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2892                         ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2893                         /* Don't reset timer for no resources */
2894
2895                         /* If discovery / RSCN timer is running, reset it */
2896                         if (timer_pending(&vport->fc_disctmo) ||
2897                             (vport->fc_flag & FC_RSCN_MODE))
2898                                 lpfc_set_disctmo(vport);
2899                 }
2900
2901                 phba->fc_stat.elsXmitRetry++;
2902                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2903                         phba->fc_stat.elsDelayRetry++;
2904                         ndlp->nlp_retry = cmdiocb->retry;
2905
2906                         /* delay is specified in milliseconds */
2907                         mod_timer(&ndlp->nlp_delayfunc,
2908                                 jiffies + msecs_to_jiffies(delay));
2909                         spin_lock_irq(shost->host_lock);
2910                         ndlp->nlp_flag |= NLP_DELAY_TMO;
2911                         spin_unlock_irq(shost->host_lock);
2912
2913                         ndlp->nlp_prev_state = ndlp->nlp_state;
2914                         if (cmd == ELS_CMD_PRLI)
2915                                 lpfc_nlp_set_state(vport, ndlp,
2916                                         NLP_STE_REG_LOGIN_ISSUE);
2917                         else
2918                                 lpfc_nlp_set_state(vport, ndlp,
2919                                         NLP_STE_NPR_NODE);
2920                         ndlp->nlp_last_elscmd = cmd;
2921
2922                         return 1;
2923                 }
2924                 switch (cmd) {
2925                 case ELS_CMD_FLOGI:
2926                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2927                         return 1;
2928                 case ELS_CMD_FDISC:
2929                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2930                         return 1;
2931                 case ELS_CMD_PLOGI:
2932                         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2933                                 ndlp->nlp_prev_state = ndlp->nlp_state;
2934                                 lpfc_nlp_set_state(vport, ndlp,
2935                                                    NLP_STE_PLOGI_ISSUE);
2936                         }
2937                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2938                         return 1;
2939                 case ELS_CMD_ADISC:
2940                         ndlp->nlp_prev_state = ndlp->nlp_state;
2941                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2942                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2943                         return 1;
2944                 case ELS_CMD_PRLI:
2945                         ndlp->nlp_prev_state = ndlp->nlp_state;
2946                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2947                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2948                         return 1;
2949                 case ELS_CMD_LOGO:
2950                         ndlp->nlp_prev_state = ndlp->nlp_state;
2951                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2952                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2953                         return 1;
2954                 }
2955         }
2956         /* No retry ELS command <elsCmd> to remote NPORT <did> */
2957         if (logerr) {
2958                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2959                          "0137 No retry ELS command x%x to remote "
2960                          "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2961                          cmd, did, irsp->ulpStatus,
2962                          irsp->un.ulpWord[4]);
2963         }
2964         else {
2965                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2966                          "0108 No retry ELS command x%x to remote "
2967                          "NPORT x%x Retried:%d Error:x%x/%x\n",
2968                          cmd, did, cmdiocb->retry, irsp->ulpStatus,
2969                          irsp->un.ulpWord[4]);
2970         }
2971         return 0;
2972 }
2973
2974 /**
2975  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
2976  * @phba: pointer to lpfc hba data structure.
2977  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
2978  *
2979  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
2980  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
2981  * checks to see whether there is a lpfc DMA buffer associated with the
2982  * response of the command IOCB. If so, it will be released before releasing
2983  * the lpfc DMA buffer associated with the IOCB itself.
2984  *
2985  * Return code
2986  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2987  **/
2988 static int
2989 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2990 {
2991         struct lpfc_dmabuf *buf_ptr;
2992
2993         /* Free the response before processing the command. */
2994         if (!list_empty(&buf_ptr1->list)) {
2995                 list_remove_head(&buf_ptr1->list, buf_ptr,
2996                                  struct lpfc_dmabuf,
2997                                  list);
2998                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2999                 kfree(buf_ptr);
3000         }
3001         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3002         kfree(buf_ptr1);
3003         return 0;
3004 }
3005
3006 /**
3007  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
3008  * @phba: pointer to lpfc hba data structure.
3009  * @buf_ptr: pointer to the lpfc dma buffer data structure.
3010  *
3011  * This routine releases the lpfc Direct Memory Access (DMA) buffer
3012  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3013  * pool.
3014  *
3015  * Return code
3016  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3017  **/
3018 static int
3019 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3020 {
3021         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3022         kfree(buf_ptr);
3023         return 0;
3024 }
3025
3026 /**
3027  * lpfc_els_free_iocb - Free a command iocb and its associated resources
3028  * @phba: pointer to lpfc hba data structure.
3029  * @elsiocb: pointer to lpfc els command iocb data structure.
3030  *
3031  * This routine frees a command IOCB and its associated resources. The
3032  * command IOCB data structure contains the reference to various associated
3033  * resources, these fields must be set to NULL if the associated reference
3034  * not present:
3035  *   context1 - reference to ndlp
3036  *   context2 - reference to cmd
3037  *   context2->next - reference to rsp
3038  *   context3 - reference to bpl
3039  *
3040  * It first properly decrements the reference count held on ndlp for the
3041  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3042  * set, it invokes the lpfc_els_free_data() routine to release the Direct
3043  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3044  * adds the DMA buffer the @phba data structure for the delayed release.
3045  * If reference to the Buffer Pointer List (BPL) is present, the
3046  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3047  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3048  * invoked to release the IOCB data structure back to @phba IOCBQ list.
3049  *
3050  * Return code
3051  *   0 - Success (currently, always return 0)
3052  **/
3053 int
3054 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
3055 {
3056         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
3057         struct lpfc_nodelist *ndlp;
3058
3059         ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3060         if (ndlp) {
3061                 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3062                         lpfc_nlp_put(ndlp);
3063
3064                         /* If the ndlp is not being used by another discovery
3065                          * thread, free it.
3066                          */
3067                         if (!lpfc_nlp_not_used(ndlp)) {
3068                                 /* If ndlp is being used by another discovery
3069                                  * thread, just clear NLP_DEFER_RM
3070                                  */
3071                                 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3072                         }
3073                 }
3074                 else
3075                         lpfc_nlp_put(ndlp);
3076                 elsiocb->context1 = NULL;
3077         }
3078         /* context2  = cmd,  context2->next = rsp, context3 = bpl */
3079         if (elsiocb->context2) {
3080                 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3081                         /* Firmware could still be in progress of DMAing
3082                          * payload, so don't free data buffer till after
3083                          * a hbeat.
3084                          */
3085                         elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3086                         buf_ptr = elsiocb->context2;
3087                         elsiocb->context2 = NULL;
3088                         if (buf_ptr) {
3089                                 buf_ptr1 = NULL;
3090                                 spin_lock_irq(&phba->hbalock);
3091                                 if (!list_empty(&buf_ptr->list)) {
3092                                         list_remove_head(&buf_ptr->list,
3093                                                 buf_ptr1, struct lpfc_dmabuf,
3094                                                 list);
3095                                         INIT_LIST_HEAD(&buf_ptr1->list);
3096                                         list_add_tail(&buf_ptr1->list,
3097                                                 &phba->elsbuf);
3098                                         phba->elsbuf_cnt++;
3099                                 }
3100                                 INIT_LIST_HEAD(&buf_ptr->list);
3101                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3102                                 phba->elsbuf_cnt++;
3103                                 spin_unlock_irq(&phba->hbalock);
3104                         }
3105                 } else {
3106                         buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3107                         lpfc_els_free_data(phba, buf_ptr1);
3108                 }
3109         }
3110
3111         if (elsiocb->context3) {
3112                 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
3113                 lpfc_els_free_bpl(phba, buf_ptr);
3114         }
3115         lpfc_sli_release_iocbq(phba, elsiocb);
3116         return 0;
3117 }
3118
3119 /**
3120  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
3121  * @phba: pointer to lpfc hba data structure.
3122  * @cmdiocb: pointer to lpfc command iocb data structure.
3123  * @rspiocb: pointer to lpfc response iocb data structure.
3124  *
3125  * This routine is the completion callback function to the Logout (LOGO)
3126  * Accept (ACC) Response ELS command. This routine is invoked to indicate
3127  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3128  * release the ndlp if it has the last reference remaining (reference count
3129  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3130  * field to NULL to inform the following lpfc_els_free_iocb() routine no
3131  * ndlp reference count needs to be decremented. Otherwise, the ndlp
3132  * reference use-count shall be decremented by the lpfc_els_free_iocb()
3133  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3134  * IOCB data structure.
3135  **/
3136 static void
3137 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3138                        struct lpfc_iocbq *rspiocb)
3139 {
3140         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3141         struct lpfc_vport *vport = cmdiocb->vport;
3142         IOCB_t *irsp;
3143
3144         irsp = &rspiocb->iocb;
3145         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3146                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
3147                 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
3148         /* ACC to LOGO completes to NPort <nlp_DID> */
3149         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3150                          "0109 ACC to LOGO completes to NPort x%x "
3151                          "Data: x%x x%x x%x\n",
3152                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3153                          ndlp->nlp_rpi);
3154
3155         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3156                 /* NPort Recovery mode or node is just allocated */
3157                 if (!lpfc_nlp_not_used(ndlp)) {
3158                         /* If the ndlp is being used by another discovery
3159                          * thread, just unregister the RPI.
3160                          */
3161                         lpfc_unreg_rpi(vport, ndlp);
3162                 } else {
3163                         /* Indicate the node has already released, should
3164                          * not reference to it from within lpfc_els_free_iocb.
3165                          */
3166                         cmdiocb->context1 = NULL;
3167                 }
3168         }
3169         lpfc_els_free_iocb(phba, cmdiocb);
3170         return;
3171 }
3172
3173 /**
3174  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
3175  * @phba: pointer to lpfc hba data structure.
3176  * @pmb: pointer to the driver internal queue element for mailbox command.
3177  *
3178  * This routine is the completion callback function for unregister default
3179  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3180  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3181  * decrements the ndlp reference count held for this completion callback
3182  * function. After that, it invokes the lpfc_nlp_not_used() to check
3183  * whether there is only one reference left on the ndlp. If so, it will
3184  * perform one more decrement and trigger the release of the ndlp.
3185  **/
3186 void
3187 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3188 {
3189         struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3190         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3191
3192         /*
3193          * This routine is used to register and unregister in previous SLI
3194          * modes.
3195          */
3196         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
3197             (phba->sli_rev == LPFC_SLI_REV4))
3198                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
3199
3200         pmb->context1 = NULL;
3201         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3202         kfree(mp);
3203         mempool_free(pmb, phba->mbox_mem_pool);
3204         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3205                 lpfc_nlp_put(ndlp);
3206                 /* This is the end of the default RPI cleanup logic for this
3207                  * ndlp. If no other discovery threads are using this ndlp.
3208                  * we should free all resources associated with it.
3209                  */
3210                 lpfc_nlp_not_used(ndlp);
3211         }
3212
3213         return;
3214 }
3215
3216 /**
3217  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
3218  * @phba: pointer to lpfc hba data structure.
3219  * @cmdiocb: pointer to lpfc command iocb data structure.
3220  * @rspiocb: pointer to lpfc response iocb data structure.
3221  *
3222  * This routine is the completion callback function for ELS Response IOCB
3223  * command. In normal case, this callback function just properly sets the
3224  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3225  * field in the command IOCB is not NULL, the referred mailbox command will
3226  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3227  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3228  * link down event occurred during the discovery, the lpfc_nlp_not_used()
3229  * routine shall be invoked trying to release the ndlp if no other threads
3230  * are currently referring it.
3231  **/
3232 static void
3233 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3234                   struct lpfc_iocbq *rspiocb)
3235 {
3236         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3237         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3238         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
3239         IOCB_t  *irsp;
3240         uint8_t *pcmd;
3241         LPFC_MBOXQ_t *mbox = NULL;
3242         struct lpfc_dmabuf *mp = NULL;
3243         uint32_t ls_rjt = 0;
3244
3245         irsp = &rspiocb->iocb;
3246
3247         if (cmdiocb->context_un.mbox)
3248                 mbox = cmdiocb->context_un.mbox;
3249
3250         /* First determine if this is a LS_RJT cmpl. Note, this callback
3251          * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3252          */
3253         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3254         if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3255             (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3256                 /* A LS_RJT associated with Default RPI cleanup has its own
3257                  * separate code path.
3258                  */
3259                 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3260                         ls_rjt = 1;
3261         }
3262
3263         /* Check to see if link went down during discovery */
3264         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3265                 if (mbox) {
3266                         mp = (struct lpfc_dmabuf *) mbox->context1;
3267                         if (mp) {
3268                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3269                                 kfree(mp);
3270                         }
3271                         mempool_free(mbox, phba->mbox_mem_pool);
3272                 }
3273                 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3274                     (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3275                         if (lpfc_nlp_not_used(ndlp)) {
3276                                 ndlp = NULL;
3277                                 /* Indicate the node has already released,
3278                                  * should not reference to it from within
3279                                  * the routine lpfc_els_free_iocb.
3280                                  */
3281                                 cmdiocb->context1 = NULL;
3282                         }
3283                 goto out;
3284         }
3285
3286         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3287                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
3288                 irsp->ulpStatus, irsp->un.ulpWord[4],
3289                 cmdiocb->iocb.un.elsreq64.remoteID);
3290         /* ELS response tag <ulpIoTag> completes */
3291         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3292                          "0110 ELS response tag x%x completes "
3293                          "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3294                          cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3295                          rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3296                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3297                          ndlp->nlp_rpi);
3298         if (mbox) {
3299                 if ((rspiocb->iocb.ulpStatus == 0)
3300                     && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3301                         lpfc_unreg_rpi(vport, ndlp);
3302                         /* Increment reference count to ndlp to hold the
3303                          * reference to ndlp for the callback function.
3304                          */
3305                         mbox->context2 = lpfc_nlp_get(ndlp);
3306                         mbox->vport = vport;
3307                         if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3308                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3309                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3310                         }
3311                         else {
3312                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3313                                 ndlp->nlp_prev_state = ndlp->nlp_state;
3314                                 lpfc_nlp_set_state(vport, ndlp,
3315                                            NLP_STE_REG_LOGIN_ISSUE);
3316                         }
3317                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3318                             != MBX_NOT_FINISHED)
3319                                 goto out;
3320                         else
3321                                 /* Decrement the ndlp reference count we
3322                                  * set for this failed mailbox command.
3323                                  */
3324                                 lpfc_nlp_put(ndlp);
3325
3326                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
3327                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3328                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
3329                                 "Data: x%x x%x x%x\n",
3330                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3331                                 ndlp->nlp_rpi);
3332
3333                         if (lpfc_nlp_not_used(ndlp)) {
3334                                 ndlp = NULL;
3335                                 /* Indicate node has already been released,
3336                                  * should not reference to it from within
3337                                  * the routine lpfc_els_free_iocb.
3338                                  */
3339                                 cmdiocb->context1 = NULL;
3340                         }
3341                 } else {
3342                         /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3343                         if (!lpfc_error_lost_link(irsp) &&
3344                             ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3345                                 if (lpfc_nlp_not_used(ndlp)) {
3346                                         ndlp = NULL;
3347                                         /* Indicate node has already been
3348                                          * released, should not reference
3349                                          * to it from within the routine
3350                                          * lpfc_els_free_iocb.
3351                                          */
3352                                         cmdiocb->context1 = NULL;
3353                                 }
3354                         }
3355                 }
3356                 mp = (struct lpfc_dmabuf *) mbox->context1;
3357                 if (mp) {
3358                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
3359                         kfree(mp);
3360                 }
3361                 mempool_free(mbox, phba->mbox_mem_pool);
3362         }
3363 out:
3364         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3365                 spin_lock_irq(shost->host_lock);
3366                 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3367                 spin_unlock_irq(shost->host_lock);
3368
3369                 /* If the node is not being used by another discovery thread,
3370                  * and we are sending a reject, we are done with it.
3371                  * Release driver reference count here and free associated
3372                  * resources.
3373                  */
3374                 if (ls_rjt)
3375                         if (lpfc_nlp_not_used(ndlp))
3376                                 /* Indicate node has already been released,
3377                                  * should not reference to it from within
3378                                  * the routine lpfc_els_free_iocb.
3379                                  */
3380                                 cmdiocb->context1 = NULL;
3381         }
3382
3383         lpfc_els_free_iocb(phba, cmdiocb);
3384         return;
3385 }
3386
3387 /**
3388  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
3389  * @vport: pointer to a host virtual N_Port data structure.
3390  * @flag: the els command code to be accepted.
3391  * @oldiocb: pointer to the original lpfc command iocb data structure.
3392  * @ndlp: pointer to a node-list data structure.
3393  * @mbox: pointer to the driver internal queue element for mailbox command.
3394  *
3395  * This routine prepares and issues an Accept (ACC) response IOCB
3396  * command. It uses the @flag to properly set up the IOCB field for the
3397  * specific ACC response command to be issued and invokes the
3398  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3399  * @mbox pointer is passed in, it will be put into the context_un.mbox
3400  * field of the IOCB for the completion callback function to issue the
3401  * mailbox command to the HBA later when callback is invoked.
3402  *
3403  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3404  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3405  * will be stored into the context1 field of the IOCB for the completion
3406  * callback function to the corresponding response ELS IOCB command.
3407  *
3408  * Return code
3409  *   0 - Successfully issued acc response
3410  *   1 - Failed to issue acc response
3411  **/
3412 int
3413 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3414                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3415                  LPFC_MBOXQ_t *mbox)
3416 {
3417         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3418         struct lpfc_hba  *phba = vport->phba;
3419         IOCB_t *icmd;
3420         IOCB_t *oldcmd;
3421         struct lpfc_iocbq *elsiocb;
3422         struct lpfc_sli *psli;
3423         uint8_t *pcmd;
3424         uint16_t cmdsize;
3425         int rc;
3426         ELS_PKT *els_pkt_ptr;
3427
3428         psli = &phba->sli;
3429         oldcmd = &oldiocb->iocb;
3430
3431         switch (flag) {
3432         case ELS_CMD_ACC:
3433                 cmdsize = sizeof(uint32_t);
3434                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3435                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3436                 if (!elsiocb) {
3437                         spin_lock_irq(shost->host_lock);
3438                         ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3439                         spin_unlock_irq(shost->host_lock);
3440                         return 1;
3441                 }
3442
3443                 icmd = &elsiocb->iocb;
3444                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3445                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3446                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3447                 pcmd += sizeof(uint32_t);
3448
3449                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3450                         "Issue ACC:       did:x%x flg:x%x",
3451                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3452                 break;
3453         case ELS_CMD_PLOGI:
3454                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3455                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3456                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3457                 if (!elsiocb)
3458                         return 1;
3459
3460                 icmd = &elsiocb->iocb;
3461                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3462                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3463
3464                 if (mbox)
3465                         elsiocb->context_un.mbox = mbox;
3466
3467                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3468                 pcmd += sizeof(uint32_t);
3469                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3470
3471                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3472                         "Issue ACC PLOGI: did:x%x flg:x%x",
3473                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3474                 break;
3475         case ELS_CMD_PRLO:
3476                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3477                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3478                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3479                 if (!elsiocb)
3480                         return 1;
3481
3482                 icmd = &elsiocb->iocb;
3483                 icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3484                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3485
3486                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3487                        sizeof(uint32_t) + sizeof(PRLO));
3488                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3489                 els_pkt_ptr = (ELS_PKT *) pcmd;
3490                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3491
3492                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3493                         "Issue ACC PRLO:  did:x%x flg:x%x",
3494                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3495                 break;
3496         default:
3497                 return 1;
3498         }
3499         /* Xmit ELS ACC response tag <ulpIoTag> */
3500         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3501                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3502                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3503                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3504                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3505                          ndlp->nlp_rpi);
3506         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3507                 spin_lock_irq(shost->host_lock);
3508                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3509                 spin_unlock_irq(shost->host_lock);
3510                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3511         } else {
3512                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3513         }
3514
3515         phba->fc_stat.elsXmitACC++;
3516         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3517         if (rc == IOCB_ERROR) {
3518                 lpfc_els_free_iocb(phba, elsiocb);
3519                 return 1;
3520         }
3521         return 0;
3522 }
3523
3524 /**
3525  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
3526  * @vport: pointer to a virtual N_Port data structure.
3527  * @rejectError:
3528  * @oldiocb: pointer to the original lpfc command iocb data structure.
3529  * @ndlp: pointer to a node-list data structure.
3530  * @mbox: pointer to the driver internal queue element for mailbox command.
3531  *
3532  * This routine prepares and issue an Reject (RJT) response IOCB
3533  * command. If a @mbox pointer is passed in, it will be put into the
3534  * context_un.mbox field of the IOCB for the completion callback function
3535  * to issue to the HBA later.
3536  *
3537  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3538  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3539  * will be stored into the context1 field of the IOCB for the completion
3540  * callback function to the reject response ELS IOCB command.
3541  *
3542  * Return code
3543  *   0 - Successfully issued reject response
3544  *   1 - Failed to issue reject response
3545  **/
3546 int
3547 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3548                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3549                     LPFC_MBOXQ_t *mbox)
3550 {
3551         struct lpfc_hba  *phba = vport->phba;
3552         IOCB_t *icmd;
3553         IOCB_t *oldcmd;
3554         struct lpfc_iocbq *elsiocb;
3555         struct lpfc_sli *psli;
3556         uint8_t *pcmd;
3557         uint16_t cmdsize;
3558         int rc;
3559
3560         psli = &phba->sli;
3561         cmdsize = 2 * sizeof(uint32_t);
3562         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3563                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
3564         if (!elsiocb)
3565                 return 1;
3566
3567         icmd = &elsiocb->iocb;
3568         oldcmd = &oldiocb->iocb;
3569         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3570         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3571
3572         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3573         pcmd += sizeof(uint32_t);
3574         *((uint32_t *) (pcmd)) = rejectError;
3575
3576         if (mbox)
3577                 elsiocb->context_un.mbox = mbox;
3578
3579         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
3580         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3581                          "0129 Xmit ELS RJT x%x response tag x%x "
3582                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3583                          "rpi x%x\n",
3584                          rejectError, elsiocb->iotag,
3585                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3586                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3587         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3588                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3589                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3590
3591         phba->fc_stat.elsXmitLSRJT++;
3592         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3593         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3594
3595         if (rc == IOCB_ERROR) {
3596                 lpfc_els_free_iocb(phba, elsiocb);
3597                 return 1;
3598         }
3599         return 0;
3600 }
3601
3602 /**
3603  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
3604  * @vport: pointer to a virtual N_Port data structure.
3605  * @oldiocb: pointer to the original lpfc command iocb data structure.
3606  * @ndlp: pointer to a node-list data structure.
3607  *
3608  * This routine prepares and issues an Accept (ACC) response to Address
3609  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3610  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3611  *
3612  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3613  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3614  * will be stored into the context1 field of the IOCB for the completion
3615  * callback function to the ADISC Accept response ELS IOCB command.
3616  *
3617  * Return code
3618  *   0 - Successfully issued acc adisc response
3619  *   1 - Failed to issue adisc acc response
3620  **/
3621 int
3622 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3623                        struct lpfc_nodelist *ndlp)
3624 {
3625         struct lpfc_hba  *phba = vport->phba;
3626         ADISC *ap;
3627         IOCB_t *icmd, *oldcmd;
3628         struct lpfc_iocbq *elsiocb;
3629         uint8_t *pcmd;
3630         uint16_t cmdsize;
3631         int rc;
3632
3633         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3634         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3635                                      ndlp->nlp_DID, ELS_CMD_ACC);
3636         if (!elsiocb)
3637                 return 1;
3638
3639         icmd = &elsiocb->iocb;
3640         oldcmd = &oldiocb->iocb;
3641         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3642
3643         /* Xmit ADISC ACC response tag <ulpIoTag> */
3644         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3645                          "0130 Xmit ADISC ACC response iotag x%x xri: "
3646                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3647                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3648                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3649                          ndlp->nlp_rpi);
3650         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3651
3652         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3653         pcmd += sizeof(uint32_t);
3654
3655         ap = (ADISC *) (pcmd);
3656         ap->hardAL_PA = phba->fc_pref_ALPA;
3657         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3658         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3659         ap->DID = be32_to_cpu(vport->fc_myDID);
3660
3661         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3662                 "Issue ACC ADISC: did:x%x flg:x%x",
3663                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3664
3665         phba->fc_stat.elsXmitACC++;
3666         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3667         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3668         if (rc == IOCB_ERROR) {
3669                 lpfc_els_free_iocb(phba, elsiocb);
3670                 return 1;
3671         }
3672         return 0;
3673 }
3674
3675 /**
3676  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
3677  * @vport: pointer to a virtual N_Port data structure.
3678  * @oldiocb: pointer to the original lpfc command iocb data structure.
3679  * @ndlp: pointer to a node-list data structure.
3680  *
3681  * This routine prepares and issues an Accept (ACC) response to Process
3682  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3683  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3684  *
3685  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3686  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3687  * will be stored into the context1 field of the IOCB for the completion
3688  * callback function to the PRLI Accept response ELS IOCB command.
3689  *
3690  * Return code
3691  *   0 - Successfully issued acc prli response
3692  *   1 - Failed to issue acc prli response
3693  **/
3694 int
3695 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3696                       struct lpfc_nodelist *ndlp)
3697 {
3698         struct lpfc_hba  *phba = vport->phba;
3699         PRLI *npr;
3700         lpfc_vpd_t *vpd;
3701         IOCB_t *icmd;
3702         IOCB_t *oldcmd;
3703         struct lpfc_iocbq *elsiocb;
3704         struct lpfc_sli *psli;
3705         uint8_t *pcmd;
3706         uint16_t cmdsize;
3707         int rc;
3708
3709         psli = &phba->sli;
3710
3711         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3712         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3713                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3714         if (!elsiocb)
3715                 return 1;
3716
3717         icmd = &elsiocb->iocb;
3718         oldcmd = &oldiocb->iocb;
3719         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3720         /* Xmit PRLI ACC response tag <ulpIoTag> */
3721         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3722                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3723                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3724                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3725                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3726                          ndlp->nlp_rpi);
3727         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3728
3729         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3730         pcmd += sizeof(uint32_t);
3731
3732         /* For PRLI, remainder of payload is PRLI parameter page */
3733         memset(pcmd, 0, sizeof(PRLI));
3734
3735         npr = (PRLI *) pcmd;
3736         vpd = &phba->vpd;
3737         /*
3738          * If the remote port is a target and our firmware version is 3.20 or
3739          * later, set the following bits for FC-TAPE support.
3740          */
3741         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3742             (vpd->rev.feaLevelHigh >= 0x02)) {
3743                 npr->ConfmComplAllowed = 1;
3744                 npr->Retry = 1;
3745                 npr->TaskRetryIdReq = 1;
3746         }
3747
3748         npr->acceptRspCode = PRLI_REQ_EXECUTED;
3749         npr->estabImagePair = 1;
3750         npr->readXferRdyDis = 1;
3751         npr->ConfmComplAllowed = 1;
3752
3753         npr->prliType = PRLI_FCP_TYPE;
3754         npr->initiatorFunc = 1;
3755
3756         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3757                 "Issue ACC PRLI:  did:x%x flg:x%x",
3758                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3759
3760         phba->fc_stat.elsXmitACC++;
3761         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3762
3763         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3764         if (rc == IOCB_ERROR) {
3765                 lpfc_els_free_iocb(phba, elsiocb);
3766                 return 1;
3767         }
3768         return 0;
3769 }
3770
3771 /**
3772  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
3773  * @vport: pointer to a virtual N_Port data structure.
3774  * @format: rnid command format.
3775  * @oldiocb: pointer to the original lpfc command iocb data structure.
3776  * @ndlp: pointer to a node-list data structure.
3777  *
3778  * This routine issues a Request Node Identification Data (RNID) Accept
3779  * (ACC) response. It constructs the RNID ACC response command according to
3780  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3781  * issue the response. Note that this command does not need to hold the ndlp
3782  * reference count for the callback. So, the ndlp reference count taken by
3783  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3784  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3785  * there is no ndlp reference available.
3786  *
3787  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3788  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3789  * will be stored into the context1 field of the IOCB for the completion
3790  * callback function. However, for the RNID Accept Response ELS command,
3791  * this is undone later by this routine after the IOCB is allocated.
3792  *
3793  * Return code
3794  *   0 - Successfully issued acc rnid response
3795  *   1 - Failed to issue acc rnid response
3796  **/
3797 static int
3798 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3799                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3800 {
3801         struct lpfc_hba  *phba = vport->phba;
3802         RNID *rn;
3803         IOCB_t *icmd, *oldcmd;
3804         struct lpfc_iocbq *elsiocb;
3805         struct lpfc_sli *psli;
3806         uint8_t *pcmd;
3807         uint16_t cmdsize;
3808         int rc;
3809
3810         psli = &phba->sli;
3811         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3812                                         + (2 * sizeof(struct lpfc_name));
3813         if (format)
3814                 cmdsize += sizeof(RNID_TOP_DISC);
3815
3816         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3817                                      ndlp->nlp_DID, ELS_CMD_ACC);
3818         if (!elsiocb)
3819                 return 1;
3820
3821         icmd = &elsiocb->iocb;
3822         oldcmd = &oldiocb->iocb;
3823         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
3824         /* Xmit RNID ACC response tag <ulpIoTag> */
3825         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3826                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3827                          elsiocb->iotag, elsiocb->iocb.ulpContext);
3828         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3829         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3830         pcmd += sizeof(uint32_t);
3831
3832         memset(pcmd, 0, sizeof(RNID));
3833         rn = (RNID *) (pcmd);
3834         rn->Format = format;
3835         rn->CommonLen = (2 * sizeof(struct lpfc_name));
3836         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3837         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3838         switch (format) {
3839         case 0:
3840                 rn->SpecificLen = 0;
3841                 break;
3842         case RNID_TOPOLOGY_DISC:
3843                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
3844                 memcpy(&rn->un.topologyDisc.portName,
3845                        &vport->fc_portname, sizeof(struct lpfc_name));
3846                 rn->un.topologyDisc.unitType = RNID_HBA;
3847                 rn->un.topologyDisc.physPort = 0;
3848                 rn->un.topologyDisc.attachedNodes = 0;
3849                 break;
3850         default:
3851                 rn->CommonLen = 0;
3852                 rn->SpecificLen = 0;
3853                 break;
3854         }
3855
3856         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3857                 "Issue ACC RNID:  did:x%x flg:x%x",
3858                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3859
3860         phba->fc_stat.elsXmitACC++;
3861         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3862         lpfc_nlp_put(ndlp);
3863         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3864                                     * it could be freed */
3865
3866         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3867         if (rc == IOCB_ERROR) {
3868                 lpfc_els_free_iocb(phba, elsiocb);
3869                 return 1;
3870         }
3871         return 0;
3872 }
3873
3874 /**
3875  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
3876  * @vport: pointer to a host virtual N_Port data structure.
3877  *
3878  * This routine issues Address Discover (ADISC) ELS commands to those
3879  * N_Ports which are in node port recovery state and ADISC has not been issued
3880  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3881  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3882  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3883  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3884  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
3885  * IOCBs quit for later pick up. On the other hand, after walking through
3886  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
3887  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
3888  * no more ADISC need to be sent.
3889  *
3890  * Return code
3891  *    The number of N_Ports with adisc issued.
3892  **/
3893 int
3894 lpfc_els_disc_adisc(struct lpfc_vport *vport)
3895 {
3896         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3897         struct lpfc_nodelist *ndlp, *next_ndlp;
3898         int sentadisc = 0;
3899
3900         /* go thru NPR nodes and issue any remaining ELS ADISCs */
3901         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3902                 if (!NLP_CHK_NODE_ACT(ndlp))
3903                         continue;
3904                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3905                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3906                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
3907                         spin_lock_irq(shost->host_lock);
3908                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3909                         spin_unlock_irq(shost->host_lock);
3910                         ndlp->nlp_prev_state = ndlp->nlp_state;
3911                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3912                         lpfc_issue_els_adisc(vport, ndlp, 0);
3913                         sentadisc++;
3914                         vport->num_disc_nodes++;
3915                         if (vport->num_disc_nodes >=
3916                             vport->cfg_discovery_threads) {
3917                                 spin_lock_irq(shost->host_lock);
3918                                 vport->fc_flag |= FC_NLP_MORE;
3919                                 spin_unlock_irq(shost->host_lock);
3920                                 break;
3921                         }
3922                 }
3923         }
3924         if (sentadisc == 0) {
3925                 spin_lock_irq(shost->host_lock);
3926                 vport->fc_flag &= ~FC_NLP_MORE;
3927                 spin_unlock_irq(shost->host_lock);
3928         }
3929         return sentadisc;
3930 }
3931
3932 /**
3933  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
3934  * @vport: pointer to a host virtual N_Port data structure.
3935  *
3936  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
3937  * which are in node port recovery state, with a @vport. Each time an ELS
3938  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
3939  * the per @vport number of discover count (num_disc_nodes) shall be
3940  * incremented. If the num_disc_nodes reaches a pre-configured threshold
3941  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
3942  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
3943  * later pick up. On the other hand, after walking through all the ndlps with
3944  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
3945  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
3946  * PLOGI need to be sent.
3947  *
3948  * Return code
3949  *   The number of N_Ports with plogi issued.
3950  **/
3951 int
3952 lpfc_els_disc_plogi(struct lpfc_vport *vport)
3953 {
3954         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3955         struct lpfc_nodelist *ndlp, *next_ndlp;
3956         int sentplogi = 0;
3957
3958         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
3959         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3960                 if (!NLP_CHK_NODE_ACT(ndlp))
3961                         continue;
3962                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3963                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3964                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
3965                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
3966                         ndlp->nlp_prev_state = ndlp->nlp_state;
3967                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3968                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3969                         sentplogi++;
3970                         vport->num_disc_nodes++;
3971                         if (vport->num_disc_nodes >=
3972                             vport->cfg_discovery_threads) {
3973                                 spin_lock_irq(shost->host_lock);
3974                                 vport->fc_flag |= FC_NLP_MORE;
3975                                 spin_unlock_irq(shost->host_lock);
3976                                 break;
3977                         }
3978                 }
3979         }
3980         if (sentplogi) {
3981                 lpfc_set_disctmo(vport);
3982         }
3983         else {
3984                 spin_lock_irq(shost->host_lock);
3985                 vport->fc_flag &= ~FC_NLP_MORE;
3986                 spin_unlock_irq(shost->host_lock);
3987         }
3988         return sentplogi;
3989 }
3990
3991 /**
3992  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
3993  * @vport: pointer to a host virtual N_Port data structure.
3994  *
3995  * This routine cleans up any Registration State Change Notification
3996  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
3997  * @vport together with the host_lock is used to prevent multiple thread
3998  * trying to access the RSCN array on a same @vport at the same time.
3999  **/
4000 void
4001 lpfc_els_flush_rscn(struct lpfc_vport *vport)
4002 {
4003         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4004         struct lpfc_hba  *phba = vport->phba;
4005         int i;
4006
4007         spin_lock_irq(shost->host_lock);
4008         if (vport->fc_rscn_flush) {
4009                 /* Another thread is walking fc_rscn_id_list on this vport */
4010                 spin_unlock_irq(shost->host_lock);
4011                 return;
4012         }
4013         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
4014         vport->fc_rscn_flush = 1;
4015         spin_unlock_irq(shost->host_lock);
4016
4017         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4018                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
4019                 vport->fc_rscn_id_list[i] = NULL;
4020         }
4021         spin_lock_irq(shost->host_lock);
4022         vport->fc_rscn_id_cnt = 0;
4023         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
4024         spin_unlock_irq(shost->host_lock);
4025         lpfc_can_disctmo(vport);
4026         /* Indicate we are done walking this fc_rscn_id_list */
4027         vport->fc_rscn_flush = 0;
4028 }
4029
4030 /**
4031  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
4032  * @vport: pointer to a host virtual N_Port data structure.
4033  * @did: remote destination port identifier.
4034  *
4035  * This routine checks whether there is any pending Registration State
4036  * Configuration Notification (RSCN) to a @did on @vport.
4037  *
4038  * Return code
4039  *   None zero - The @did matched with a pending rscn
4040  *   0 - not able to match @did with a pending rscn
4041  **/
4042 int
4043 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
4044 {
4045         D_ID ns_did;
4046         D_ID rscn_did;
4047         uint32_t *lp;
4048         uint32_t payload_len, i;
4049         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4050
4051         ns_did.un.word = did;
4052
4053         /* Never match fabric nodes for RSCNs */
4054         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
4055                 return 0;
4056
4057         /* If we are doing a FULL RSCN rediscovery, match everything */
4058         if (vport->fc_flag & FC_RSCN_DISCOVERY)
4059                 return did;
4060
4061         spin_lock_irq(shost->host_lock);
4062         if (vport->fc_rscn_flush) {
4063                 /* Another thread is walking fc_rscn_id_list on this vport */
4064                 spin_unlock_irq(shost->host_lock);
4065                 return 0;
4066         }
4067         /* Indicate we are walking fc_rscn_id_list on this vport */
4068         vport->fc_rscn_flush = 1;
4069         spin_unlock_irq(shost->host_lock);
4070         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4071                 lp = vport->fc_rscn_id_list[i]->virt;
4072                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4073                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
4074                 while (payload_len) {
4075                         rscn_did.un.word = be32_to_cpu(*lp++);
4076                         payload_len -= sizeof(uint32_t);
4077                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
4078                         case RSCN_ADDRESS_FORMAT_PORT:
4079                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4080                                     && (ns_did.un.b.area == rscn_did.un.b.area)
4081                                     && (ns_did.un.b.id == rscn_did.un.b.id))
4082                                         goto return_did_out;
4083                                 break;
4084                         case RSCN_ADDRESS_FORMAT_AREA:
4085                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4086                                     && (ns_did.un.b.area == rscn_did.un.b.area))
4087                                         goto return_did_out;
4088                                 break;
4089                         case RSCN_ADDRESS_FORMAT_DOMAIN:
4090                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
4091                                         goto return_did_out;
4092                                 break;
4093                         case RSCN_ADDRESS_FORMAT_FABRIC:
4094                                 goto return_did_out;
4095                         }
4096                 }
4097         }
4098         /* Indicate we are done with walking fc_rscn_id_list on this vport */
4099         vport->fc_rscn_flush = 0;
4100         return 0;
4101 return_did_out:
4102         /* Indicate we are done with walking fc_rscn_id_list on this vport */
4103         vport->fc_rscn_flush = 0;
4104         return did;
4105 }
4106
4107 /**
4108  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
4109  * @vport: pointer to a host virtual N_Port data structure.
4110  *
4111  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
4112  * state machine for a @vport's nodes that are with pending RSCN (Registration
4113  * State Change Notification).
4114  *
4115  * Return code
4116  *   0 - Successful (currently alway return 0)
4117  **/
4118 static int
4119 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
4120 {
4121         struct lpfc_nodelist *ndlp = NULL;
4122
4123         /* Move all affected nodes by pending RSCNs to NPR state. */
4124         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4125                 if (!NLP_CHK_NODE_ACT(ndlp) ||
4126                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
4127                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
4128                         continue;
4129                 lpfc_disc_state_machine(vport, ndlp, NULL,
4130                                         NLP_EVT_DEVICE_RECOVERY);
4131                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
4132         }
4133         return 0;
4134 }
4135
4136 /**
4137  * lpfc_send_rscn_event - Send an RSCN event to management application
4138  * @vport: pointer to a host virtual N_Port data structure.
4139  * @cmdiocb: pointer to lpfc command iocb data structure.
4140  *
4141  * lpfc_send_rscn_event sends an RSCN netlink event to management
4142  * applications.
4143  */
4144 static void
4145 lpfc_send_rscn_event(struct lpfc_vport *vport,
4146                 struct lpfc_iocbq *cmdiocb)
4147 {
4148         struct lpfc_dmabuf *pcmd;
4149         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4150         uint32_t *payload_ptr;
4151         uint32_t payload_len;
4152         struct lpfc_rscn_event_header *rscn_event_data;
4153
4154         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4155         payload_ptr = (uint32_t *) pcmd->virt;
4156         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4157
4158         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4159                 payload_len, GFP_KERNEL);
4160         if (!rscn_event_data) {
4161                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4162                         "0147 Failed to allocate memory for RSCN event\n");
4163                 return;
4164         }
4165         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4166         rscn_event_data->payload_length = payload_len;
4167         memcpy(rscn_event_data->rscn_payload, payload_ptr,
4168                 payload_len);
4169
4170         fc_host_post_vendor_event(shost,
4171                 fc_get_event_number(),
4172                 sizeof(struct lpfc_els_event_header) + payload_len,
4173                 (char *)rscn_event_data,
4174                 LPFC_NL_VENDOR_ID);
4175
4176         kfree(rscn_event_data);
4177 }
4178
4179 /**
4180  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
4181  * @vport: pointer to a host virtual N_Port data structure.
4182  * @cmdiocb: pointer to lpfc command iocb data structure.
4183  * @ndlp: pointer to a node-list data structure.
4184  *
4185  * This routine processes an unsolicited RSCN (Registration State Change
4186  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4187  * to invoke fc_host_post_event() routine to the FC transport layer. If the
4188  * discover state machine is about to begin discovery, it just accepts the
4189  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4190  * contains N_Port IDs for other vports on this HBA, it just accepts the
4191  * RSCN and ignore processing it. If the state machine is in the recovery
4192  * state, the fc_rscn_id_list of this @vport is walked and the
4193  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4194  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4195  * routine is invoked to handle the RSCN event.
4196  *
4197  * Return code
4198  *   0 - Just sent the acc response
4199  *   1 - Sent the acc response and waited for name server completion
4200  **/
4201 static int
4202 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4203                   struct lpfc_nodelist *ndlp)
4204 {
4205         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4206         struct lpfc_hba  *phba = vport->phba;
4207         struct lpfc_dmabuf *pcmd;
4208         uint32_t *lp, *datap;
4209         IOCB_t *icmd;
4210         uint32_t payload_len, length, nportid, *cmd;
4211         int rscn_cnt;
4212         int rscn_id = 0, hba_id = 0;
4213         int i;
4214
4215         icmd = &cmdiocb->iocb;
4216         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4217         lp = (uint32_t *) pcmd->virt;
4218
4219         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4220         payload_len -= sizeof(uint32_t);        /* take off word 0 */
4221         /* RSCN received */
4222         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4223                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
4224                          vport->fc_flag, payload_len, *lp,
4225                          vport->fc_rscn_id_cnt);
4226
4227         /* Send an RSCN event to the management application */
4228         lpfc_send_rscn_event(vport, cmdiocb);
4229
4230         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
4231                 fc_host_post_event(shost, fc_get_event_number(),
4232                         FCH_EVT_RSCN, lp[i]);
4233
4234         /* If we are about to begin discovery, just ACC the RSCN.
4235          * Discovery processing will satisfy it.
4236          */
4237         if (vport->port_state <= LPFC_NS_QRY) {
4238                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4239                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4240                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4241
4242                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4243                 return 0;
4244         }
4245
4246         /* If this RSCN just contains NPortIDs for other vports on this HBA,
4247          * just ACC and ignore it.
4248          */
4249         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4250                 !(vport->cfg_peer_port_login)) {
4251                 i = payload_len;
4252                 datap = lp;
4253                 while (i > 0) {
4254                         nportid = *datap++;
4255                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4256                         i -= sizeof(uint32_t);
4257                         rscn_id++;
4258                         if (lpfc_find_vport_by_did(phba, nportid))
4259                                 hba_id++;
4260                 }
4261                 if (rscn_id == hba_id) {
4262                         /* ALL NPortIDs in RSCN are on HBA */
4263                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4264                                          "0219 Ignore RSCN "
4265                                          "Data: x%x x%x x%x x%x\n",
4266                                          vport->fc_flag, payload_len,
4267                                          *lp, vport->fc_rscn_id_cnt);
4268                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4269                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
4270                                 ndlp->nlp_DID, vport->port_state,
4271                                 ndlp->nlp_flag);
4272
4273                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
4274                                 ndlp, NULL);
4275                         return 0;
4276                 }
4277         }
4278
4279         spin_lock_irq(shost->host_lock);
4280         if (vport->fc_rscn_flush) {
4281                 /* Another thread is walking fc_rscn_id_list on this vport */
4282                 vport->fc_flag |= FC_RSCN_DISCOVERY;
4283                 spin_unlock_irq(shost->host_lock);
4284                 /* Send back ACC */
4285                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4286                 return 0;
4287         }
4288         /* Indicate we are walking fc_rscn_id_list on this vport */
4289         vport->fc_rscn_flush = 1;
4290         spin_unlock_irq(shost->host_lock);
4291         /* Get the array count after successfully have the token */
4292         rscn_cnt = vport->fc_rscn_id_cnt;
4293         /* If we are already processing an RSCN, save the received
4294          * RSCN payload buffer, cmdiocb->context2 to process later.
4295          */
4296         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4297                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4298                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4299                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4300
4301                 spin_lock_irq(shost->host_lock);
4302                 vport->fc_flag |= FC_RSCN_DEFERRED;
4303                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4304                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4305                         vport->fc_flag |= FC_RSCN_MODE;
4306                         spin_unlock_irq(shost->host_lock);
4307                         if (rscn_cnt) {
4308                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4309                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4310                         }
4311                         if ((rscn_cnt) &&
4312                             (payload_len + length <= LPFC_BPL_SIZE)) {
4313                                 *cmd &= ELS_CMD_MASK;
4314                                 *cmd |= cpu_to_be32(payload_len + length);
4315                                 memcpy(((uint8_t *)cmd) + length, lp,
4316                                        payload_len);
4317                         } else {
4318                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4319                                 vport->fc_rscn_id_cnt++;
4320                                 /* If we zero, cmdiocb->context2, the calling
4321                                  * routine will not try to free it.
4322                                  */
4323                                 cmdiocb->context2 = NULL;
4324                         }
4325                         /* Deferred RSCN */
4326                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4327                                          "0235 Deferred RSCN "
4328                                          "Data: x%x x%x x%x\n",
4329                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4330                                          vport->port_state);
4331                 } else {
4332                         vport->fc_flag |= FC_RSCN_DISCOVERY;
4333                         spin_unlock_irq(shost->host_lock);
4334                         /* ReDiscovery RSCN */
4335                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4336                                          "0234 ReDiscovery RSCN "
4337                                          "Data: x%x x%x x%x\n",
4338                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4339                                          vport->port_state);
4340                 }
4341                 /* Indicate we are done walking fc_rscn_id_list on this vport */
4342                 vport->fc_rscn_flush = 0;
4343                 /* Send back ACC */
4344                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4345                 /* send RECOVERY event for ALL nodes that match RSCN payload */
4346                 lpfc_rscn_recovery_check(vport);
4347                 spin_lock_irq(shost->host_lock);
4348                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
4349                 spin_unlock_irq(shost->host_lock);
4350                 return 0;
4351         }
4352         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4353                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4354                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4355
4356         spin_lock_irq(shost->host_lock);
4357         vport->fc_flag |= FC_RSCN_MODE;
4358         spin_unlock_irq(shost->host_lock);
4359         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4360         /* Indicate we are done walking fc_rscn_id_list on this vport */
4361         vport->fc_rscn_flush = 0;
4362         /*
4363          * If we zero, cmdiocb->context2, the calling routine will
4364          * not try to free it.
4365          */
4366         cmdiocb->context2 = NULL;
4367         lpfc_set_disctmo(vport);
4368         /* Send back ACC */
4369         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4370         /* send RECOVERY event for ALL nodes that match RSCN payload */
4371         lpfc_rscn_recovery_check(vport);
4372         return lpfc_els_handle_rscn(vport);
4373 }
4374
4375 /**
4376  * lpfc_els_handle_rscn - Handle rscn for a vport
4377  * @vport: pointer to a host virtual N_Port data structure.
4378  *
4379  * This routine handles the Registration State Configuration Notification
4380  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4381  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4382  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4383  * NameServer shall be issued. If CT command to the NameServer fails to be
4384  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4385  * RSCN activities with the @vport.
4386  *
4387  * Return code
4388  *   0 - Cleaned up rscn on the @vport
4389  *   1 - Wait for plogi to name server before proceed
4390  **/
4391 int
4392 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4393 {
4394         struct lpfc_nodelist *ndlp;
4395         struct lpfc_hba *phba = vport->phba;
4396
4397         /* Ignore RSCN if the port is being torn down. */
4398         if (vport->load_flag & FC_UNLOADING) {
4399                 lpfc_els_flush_rscn(vport);
4400                 return 0;
4401         }
4402
4403         /* Start timer for RSCN processing */
4404         lpfc_set_disctmo(vport);
4405
4406         /* RSCN processed */
4407         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4408                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4409                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4410                          vport->port_state);
4411
4412         /* To process RSCN, first compare RSCN data with NameServer */
4413         vport->fc_ns_retry = 0;
4414         vport->num_disc_nodes = 0;
4415
4416         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4417         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4418             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4419                 /* Good ndlp, issue CT Request to NameServer */
4420                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4421                         /* Wait for NameServer query cmpl before we can
4422                            continue */
4423                         return 1;
4424         } else {
4425                 /* If login to NameServer does not exist, issue one */
4426                 /* Good status, issue PLOGI to NameServer */
4427                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4428                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4429                         /* Wait for NameServer login cmpl before we can
4430                            continue */
4431                         return 1;
4432
4433                 if (ndlp) {
4434                         ndlp = lpfc_enable_node(vport, ndlp,
4435                                                 NLP_STE_PLOGI_ISSUE);
4436                         if (!ndlp) {
4437                                 lpfc_els_flush_rscn(vport);
4438                                 return 0;
4439                         }
4440                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4441                 } else {
4442                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4443                         if (!ndlp) {
4444                                 lpfc_els_flush_rscn(vport);
4445                                 return 0;
4446                         }
4447                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
4448                         ndlp->nlp_prev_state = ndlp->nlp_state;
4449                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4450                 }
4451                 ndlp->nlp_type |= NLP_FABRIC;
4452                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4453                 /* Wait for NameServer login cmpl before we can
4454                  * continue
4455                  */
4456                 return 1;
4457         }
4458
4459         lpfc_els_flush_rscn(vport);
4460         return 0;
4461 }
4462
4463 /**
4464  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
4465  * @vport: pointer to a host virtual N_Port data structure.
4466  * @cmdiocb: pointer to lpfc command iocb data structure.
4467  * @ndlp: pointer to a node-list data structure.
4468  *
4469  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4470  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4471  * point topology. As an unsolicited FLOGI should not be received in a loop
4472  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4473  * lpfc_check_sparm() routine is invoked to check the parameters in the
4474  * unsolicited FLOGI. If parameters validation failed, the routine
4475  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4476  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4477  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4478  * will initiate PLOGI. The higher lexicographical value party shall has
4479  * higher priority (as the winning port) and will initiate PLOGI and
4480  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4481  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4482  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4483  *
4484  * Return code
4485  *   0 - Successfully processed the unsolicited flogi
4486  *   1 - Failed to process the unsolicited flogi
4487  **/
4488 static int
4489 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4490                    struct lpfc_nodelist *ndlp)
4491 {
4492         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4493         struct lpfc_hba  *phba = vport->phba;
4494         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4495         uint32_t *lp = (uint32_t *) pcmd->virt;
4496         IOCB_t *icmd = &cmdiocb->iocb;
4497         struct serv_parm *sp;
4498         LPFC_MBOXQ_t *mbox;
4499         struct ls_rjt stat;
4500         uint32_t cmd, did;
4501         int rc;
4502
4503         cmd = *lp++;
4504         sp = (struct serv_parm *) lp;
4505
4506         /* FLOGI received */
4507
4508         lpfc_set_disctmo(vport);
4509
4510         if (phba->fc_topology == TOPOLOGY_LOOP) {
4511                 /* We should never receive a FLOGI in loop mode, ignore it */
4512                 did = icmd->un.elsreq64.remoteID;
4513
4514                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4515                    Loop Mode */
4516                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4517                                  "0113 An FLOGI ELS command x%x was "
4518                                  "received from DID x%x in Loop Mode\n",
4519                                  cmd, did);
4520                 return 1;
4521         }
4522
4523         did = Fabric_DID;
4524
4525         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
4526                 /* For a FLOGI we accept, then if our portname is greater
4527                  * then the remote portname we initiate Nport login.
4528                  */
4529
4530                 rc = memcmp(&vport->fc_portname, &sp->portName,
4531                             sizeof(struct lpfc_name));
4532
4533                 if (!rc) {
4534                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4535                         if (!mbox)
4536                                 return 1;
4537
4538                         lpfc_linkdown(phba);
4539                         lpfc_init_link(phba, mbox,
4540                                        phba->cfg_topology,
4541                                        phba->cfg_link_speed);
4542                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
4543                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4544                         mbox->vport = vport;
4545                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4546                         lpfc_set_loopback_flag(phba);
4547                         if (rc == MBX_NOT_FINISHED) {
4548                                 mempool_free(mbox, phba->mbox_mem_pool);
4549                         }
4550                         return 1;
4551                 } else if (rc > 0) {    /* greater than */
4552                         spin_lock_irq(shost->host_lock);
4553                         vport->fc_flag |= FC_PT2PT_PLOGI;
4554                         spin_unlock_irq(shost->host_lock);
4555                 }
4556                 spin_lock_irq(shost->host_lock);
4557                 vport->fc_flag |= FC_PT2PT;
4558                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4559                 spin_unlock_irq(shost->host_lock);
4560         } else {
4561                 /* Reject this request because invalid parameters */
4562                 stat.un.b.lsRjtRsvd0 = 0;
4563                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4564                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4565                 stat.un.b.vendorUnique = 0;
4566                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4567                         NULL);
4568                 return 1;
4569         }
4570
4571         /* Send back ACC */
4572         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4573
4574         return 0;
4575 }
4576
4577 /**
4578  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
4579  * @vport: pointer to a host virtual N_Port data structure.
4580  * @cmdiocb: pointer to lpfc command iocb data structure.
4581  * @ndlp: pointer to a node-list data structure.
4582  *
4583  * This routine processes Request Node Identification Data (RNID) IOCB
4584  * received as an ELS unsolicited event. Only when the RNID specified format
4585  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4586  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4587  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4588  * rejected by invoking the lpfc_els_rsp_reject() routine.
4589  *
4590  * Return code
4591  *   0 - Successfully processed rnid iocb (currently always return 0)
4592  **/
4593 static int
4594 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4595                   struct lpfc_nodelist *ndlp)
4596 {
4597         struct lpfc_dmabuf *pcmd;
4598         uint32_t *lp;
4599         IOCB_t *icmd;
4600         RNID *rn;
4601         struct ls_rjt stat;
4602         uint32_t cmd, did;
4603
4604         icmd = &cmdiocb->iocb;
4605         did = icmd->un.elsreq64.remoteID;
4606         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4607         lp = (uint32_t *) pcmd->virt;
4608
4609         cmd = *lp++;
4610         rn = (RNID *) lp;
4611
4612         /* RNID received */
4613
4614         switch (rn->Format) {
4615         case 0:
4616         case RNID_TOPOLOGY_DISC:
4617                 /* Send back ACC */
4618                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4619                 break;
4620         default:
4621                 /* Reject this request because format not supported */
4622                 stat.un.b.lsRjtRsvd0 = 0;
4623                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4624                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4625                 stat.un.b.vendorUnique = 0;
4626                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4627                         NULL);
4628         }
4629         return 0;
4630 }
4631
4632 /**
4633  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
4634  * @vport: pointer to a host virtual N_Port data structure.
4635  * @cmdiocb: pointer to lpfc command iocb data structure.
4636  * @ndlp: pointer to a node-list data structure.
4637  *
4638  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4639  * received as an ELS unsolicited event. Currently, this function just invokes
4640  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4641  *
4642  * Return code
4643  *   0 - Successfully processed lirr iocb (currently always return 0)
4644  **/
4645 static int
4646 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4647                   struct lpfc_nodelist *ndlp)
4648 {
4649         struct ls_rjt stat;
4650
4651         /* For now, unconditionally reject this command */
4652         stat.un.b.lsRjtRsvd0 = 0;
4653         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4654         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4655         stat.un.b.vendorUnique = 0;
4656         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4657         return 0;
4658 }
4659
4660 /**
4661  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
4662  * @vport: pointer to a host virtual N_Port data structure.
4663  * @cmdiocb: pointer to lpfc command iocb data structure.
4664  * @ndlp: pointer to a node-list data structure.
4665  *
4666  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
4667  * received as an ELS unsolicited event. A request to RRQ shall only
4668  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
4669  * Nx_Port N_Port_ID of the target Exchange is the same as the
4670  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
4671  * not accepted, an LS_RJT with reason code "Unable to perform
4672  * command request" and reason code explanation "Invalid Originator
4673  * S_ID" shall be returned. For now, we just unconditionally accept
4674  * RRQ from the target.
4675  **/
4676 static void
4677 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4678                  struct lpfc_nodelist *ndlp)
4679 {
4680         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4681 }
4682
4683 /**
4684  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
4685  * @phba: pointer to lpfc hba data structure.
4686  * @pmb: pointer to the driver internal queue element for mailbox command.
4687  *
4688  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4689  * mailbox command. This callback function is to actually send the Accept
4690  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4691  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4692  * mailbox command, constructs the RPS response with the link statistics
4693  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4694  * response to the RPS.
4695  *
4696  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4697  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4698  * will be stored into the context1 field of the IOCB for the completion
4699  * callback function to the RPS Accept Response ELS IOCB command.
4700  *
4701  **/
4702 static void
4703 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4704 {
4705         MAILBOX_t *mb;
4706         IOCB_t *icmd;
4707         RPS_RSP *rps_rsp;
4708         uint8_t *pcmd;
4709         struct lpfc_iocbq *elsiocb;
4710         struct lpfc_nodelist *ndlp;
4711         uint16_t xri, status;
4712         uint32_t cmdsize;
4713
4714         mb = &pmb->u.mb;
4715
4716         ndlp = (struct lpfc_nodelist *) pmb->context2;
4717         xri = (uint16_t) ((unsigned long)(pmb->context1));
4718         pmb->context1 = NULL;
4719         pmb->context2 = NULL;
4720
4721         if (mb->mbxStatus) {
4722                 mempool_free(pmb, phba->mbox_mem_pool);
4723                 return;
4724         }
4725
4726         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4727         mempool_free(pmb, phba->mbox_mem_pool);
4728         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4729                                      lpfc_max_els_tries, ndlp,
4730                                      ndlp->nlp_DID, ELS_CMD_ACC);
4731
4732         /* Decrement the ndlp reference count from previous mbox command */
4733         lpfc_nlp_put(ndlp);
4734
4735         if (!elsiocb)
4736                 return;
4737
4738         icmd = &elsiocb->iocb;
4739         icmd->ulpContext = xri;
4740
4741         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4742         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4743         pcmd += sizeof(uint32_t); /* Skip past command */
4744         rps_rsp = (RPS_RSP *)pcmd;
4745
4746         if (phba->fc_topology != TOPOLOGY_LOOP)
4747                 status = 0x10;
4748         else
4749                 status = 0x8;
4750         if (phba->pport->fc_flag & FC_FABRIC)
4751                 status |= 0x4;
4752
4753         rps_rsp->rsvd1 = 0;
4754         rps_rsp->portStatus = cpu_to_be16(status);
4755         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4756         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4757         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4758         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4759         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4760         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4761         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
4762         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4763                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4764                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4765                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4766                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4767                          ndlp->nlp_rpi);
4768         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4769         phba->fc_stat.elsXmitACC++;
4770         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
4771                 lpfc_els_free_iocb(phba, elsiocb);
4772         return;
4773 }
4774
4775 /**
4776  * lpfc_els_rcv_rps - Process an unsolicited rps iocb
4777  * @vport: pointer to a host virtual N_Port data structure.
4778  * @cmdiocb: pointer to lpfc command iocb data structure.
4779  * @ndlp: pointer to a node-list data structure.
4780  *
4781  * This routine processes Read Port Status (RPS) IOCB received as an
4782  * ELS unsolicited event. It first checks the remote port state. If the
4783  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
4784  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
4785  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
4786  * for reading the HBA link statistics. It is for the callback function,
4787  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
4788  * to actually sending out RPS Accept (ACC) response.
4789  *
4790  * Return codes
4791  *   0 - Successfully processed rps iocb (currently always return 0)
4792  **/
4793 static int
4794 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4795                  struct lpfc_nodelist *ndlp)
4796 {
4797         struct lpfc_hba *phba = vport->phba;
4798         uint32_t *lp;
4799         uint8_t flag;
4800         LPFC_MBOXQ_t *mbox;
4801         struct lpfc_dmabuf *pcmd;
4802         RPS *rps;
4803         struct ls_rjt stat;
4804
4805         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4806             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
4807                 /* reject the unsolicited RPS request and done with it */
4808                 goto reject_out;
4809
4810         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4811         lp = (uint32_t *) pcmd->virt;
4812         flag = (be32_to_cpu(*lp++) & 0xf);
4813         rps = (RPS *) lp;
4814
4815         if ((flag == 0) ||
4816             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
4817             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
4818                                     sizeof(struct lpfc_name)) == 0))) {
4819
4820                 printk("Fix me....\n");
4821                 dump_stack();
4822                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
4823                 if (mbox) {
4824                         lpfc_read_lnk_stat(phba, mbox);
4825                         mbox->context1 =
4826                             (void *)((unsigned long) cmdiocb->iocb.ulpContext);
4827                         mbox->context2 = lpfc_nlp_get(ndlp);
4828                         mbox->vport = vport;
4829                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
4830                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4831                                 != MBX_NOT_FINISHED)
4832                                 /* Mbox completion will send ELS Response */
4833                                 return 0;
4834                         /* Decrement reference count used for the failed mbox
4835                          * command.
4836                          */
4837                         lpfc_nlp_put(ndlp);
4838                         mempool_free(mbox, phba->mbox_mem_pool);
4839                 }
4840         }
4841
4842 reject_out:
4843         /* issue rejection response */
4844         stat.un.b.lsRjtRsvd0 = 0;
4845         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4846         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4847         stat.un.b.vendorUnique = 0;
4848         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4849         return 0;
4850 }
4851
4852 /**
4853  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
4854  * @vport: pointer to a host virtual N_Port data structure.
4855  * @cmdsize: size of the ELS command.
4856  * @oldiocb: pointer to the original lpfc command iocb data structure.
4857  * @ndlp: pointer to a node-list data structure.
4858  *
4859  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
4860  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
4861  *
4862  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4863  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4864  * will be stored into the context1 field of the IOCB for the completion
4865  * callback function to the RPL Accept Response ELS command.
4866  *
4867  * Return code
4868  *   0 - Successfully issued ACC RPL ELS command
4869  *   1 - Failed to issue ACC RPL ELS command
4870  **/
4871 static int
4872 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
4873                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4874 {
4875         struct lpfc_hba *phba = vport->phba;
4876         IOCB_t *icmd, *oldcmd;
4877         RPL_RSP rpl_rsp;
4878         struct lpfc_iocbq *elsiocb;
4879         uint8_t *pcmd;
4880
4881         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4882                                      ndlp->nlp_DID, ELS_CMD_ACC);
4883
4884         if (!elsiocb)
4885                 return 1;
4886
4887         icmd = &elsiocb->iocb;
4888         oldcmd = &oldiocb->iocb;
4889         icmd->ulpContext = oldcmd->ulpContext;  /* Xri */
4890
4891         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4892         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4893         pcmd += sizeof(uint16_t);
4894         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
4895         pcmd += sizeof(uint16_t);
4896
4897         /* Setup the RPL ACC payload */
4898         rpl_rsp.listLen = be32_to_cpu(1);
4899         rpl_rsp.index = 0;
4900         rpl_rsp.port_num_blk.portNum = 0;
4901         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
4902         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
4903             sizeof(struct lpfc_name));
4904         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
4905         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
4906         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4907                          "0120 Xmit ELS RPL ACC response tag x%x "
4908                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4909                          "rpi x%x\n",
4910                          elsiocb->iotag, elsiocb->iocb.ulpContext,
4911                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4912                          ndlp->nlp_rpi);
4913         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4914         phba->fc_stat.elsXmitACC++;
4915         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
4916             IOCB_ERROR) {
4917                 lpfc_els_free_iocb(phba, elsiocb);
4918                 return 1;
4919         }
4920         return 0;
4921 }
4922
4923 /**
4924  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
4925  * @vport: pointer to a host virtual N_Port data structure.
4926  * @cmdiocb: pointer to lpfc command iocb data structure.
4927  * @ndlp: pointer to a node-list data structure.
4928  *
4929  * This routine processes Read Port List (RPL) IOCB received as an ELS
4930  * unsolicited event. It first checks the remote port state. If the remote
4931  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
4932  * invokes the lpfc_els_rsp_reject() routine to send reject response.
4933  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
4934  * to accept the RPL.
4935  *
4936  * Return code
4937  *   0 - Successfully processed rpl iocb (currently always return 0)
4938  **/
4939 static int
4940 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4941                  struct lpfc_nodelist *ndlp)
4942 {
4943         struct lpfc_dmabuf *pcmd;
4944         uint32_t *lp;
4945         uint32_t maxsize;
4946         uint16_t cmdsize;
4947         RPL *rpl;
4948         struct ls_rjt stat;
4949
4950         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4951             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
4952                 /* issue rejection response */
4953                 stat.un.b.lsRjtRsvd0 = 0;
4954                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4955                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4956                 stat.un.b.vendorUnique = 0;
4957                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4958                         NULL);
4959                 /* rejected the unsolicited RPL request and done with it */
4960                 return 0;
4961         }
4962
4963         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4964         lp = (uint32_t *) pcmd->virt;
4965         rpl = (RPL *) (lp + 1);
4966
4967         maxsize = be32_to_cpu(rpl->maxsize);
4968
4969         /* We support only one port */
4970         if ((rpl->index == 0) &&
4971             ((maxsize == 0) ||
4972              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
4973                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
4974         } else {
4975                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
4976         }
4977         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
4978
4979         return 0;
4980 }
4981
4982 /**
4983  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
4984  * @vport: pointer to a virtual N_Port data structure.
4985  * @cmdiocb: pointer to lpfc command iocb data structure.
4986  * @ndlp: pointer to a node-list data structure.
4987  *
4988  * This routine processes Fibre Channel Address Resolution Protocol
4989  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
4990  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
4991  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
4992  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
4993  * remote PortName is compared against the FC PortName stored in the @vport
4994  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
4995  * compared against the FC NodeName stored in the @vport data structure.
4996  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
4997  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
4998  * invoked to send out FARP Response to the remote node. Before sending the
4999  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
5000  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
5001  * routine is invoked to log into the remote port first.
5002  *
5003  * Return code
5004  *   0 - Either the FARP Match Mode not supported or successfully processed
5005  **/
5006 static int
5007 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5008                   struct lpfc_nodelist *ndlp)
5009 {
5010         struct lpfc_dmabuf *pcmd;
5011         uint32_t *lp;
5012         IOCB_t *icmd;
5013         FARP *fp;
5014         uint32_t cmd, cnt, did;
5015
5016         icmd = &cmdiocb->iocb;
5017         did = icmd->un.elsreq64.remoteID;
5018         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5019         lp = (uint32_t *) pcmd->virt;
5020
5021         cmd = *lp++;
5022         fp = (FARP *) lp;
5023         /* FARP-REQ received from DID <did> */
5024         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5025                          "0601 FARP-REQ received from DID x%x\n", did);
5026         /* We will only support match on WWPN or WWNN */
5027         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
5028                 return 0;
5029         }
5030
5031         cnt = 0;
5032         /* If this FARP command is searching for my portname */
5033         if (fp->Mflags & FARP_MATCH_PORT) {
5034                 if (memcmp(&fp->RportName, &vport->fc_portname,
5035                            sizeof(struct lpfc_name)) == 0)
5036                         cnt = 1;
5037         }
5038
5039         /* If this FARP command is searching for my nodename */
5040         if (fp->Mflags & FARP_MATCH_NODE) {
5041                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
5042                            sizeof(struct lpfc_name)) == 0)
5043                         cnt = 1;
5044         }
5045
5046         if (cnt) {
5047                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
5048                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
5049                         /* Log back into the node before sending the FARP. */
5050                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
5051                                 ndlp->nlp_prev_state = ndlp->nlp_state;
5052                                 lpfc_nlp_set_state(vport, ndlp,
5053                                                    NLP_STE_PLOGI_ISSUE);
5054                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5055                         }
5056
5057                         /* Send a FARP response to that node */
5058                         if (fp->Rflags & FARP_REQUEST_FARPR)
5059                                 lpfc_issue_els_farpr(vport, did, 0);
5060                 }
5061         }
5062         return 0;
5063 }
5064
5065 /**
5066  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
5067  * @vport: pointer to a host virtual N_Port data structure.
5068  * @cmdiocb: pointer to lpfc command iocb data structure.
5069  * @ndlp: pointer to a node-list data structure.
5070  *
5071  * This routine processes Fibre Channel Address Resolution Protocol
5072  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
5073  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
5074  * the FARP response request.
5075  *
5076  * Return code
5077  *   0 - Successfully processed FARPR IOCB (currently always return 0)
5078  **/
5079 static int
5080 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5081                    struct lpfc_nodelist  *ndlp)
5082 {
5083         struct lpfc_dmabuf *pcmd;
5084         uint32_t *lp;
5085         IOCB_t *icmd;
5086         uint32_t cmd, did;
5087
5088         icmd = &cmdiocb->iocb;
5089         did = icmd->un.elsreq64.remoteID;
5090         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5091         lp = (uint32_t *) pcmd->virt;
5092
5093         cmd = *lp++;
5094         /* FARP-RSP received from DID <did> */
5095         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5096                          "0600 FARP-RSP received from DID x%x\n", did);
5097         /* ACCEPT the Farp resp request */
5098         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5099
5100         return 0;
5101 }
5102
5103 /**
5104  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
5105  * @vport: pointer to a host virtual N_Port data structure.
5106  * @cmdiocb: pointer to lpfc command iocb data structure.
5107  * @fan_ndlp: pointer to a node-list data structure.
5108  *
5109  * This routine processes a Fabric Address Notification (FAN) IOCB
5110  * command received as an ELS unsolicited event. The FAN ELS command will
5111  * only be processed on a physical port (i.e., the @vport represents the
5112  * physical port). The fabric NodeName and PortName from the FAN IOCB are
5113  * compared against those in the phba data structure. If any of those is
5114  * different, the lpfc_initial_flogi() routine is invoked to initialize
5115  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
5116  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
5117  * is invoked to register login to the fabric.
5118  *
5119  * Return code
5120  *   0 - Successfully processed fan iocb (currently always return 0).
5121  **/
5122 static int
5123 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5124                  struct lpfc_nodelist *fan_ndlp)
5125 {
5126         struct lpfc_hba *phba = vport->phba;
5127         uint32_t *lp;
5128         FAN *fp;
5129
5130         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
5131         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
5132         fp = (FAN *) ++lp;
5133         /* FAN received; Fan does not have a reply sequence */
5134         if ((vport == phba->pport) &&
5135             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5136                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
5137                             sizeof(struct lpfc_name))) ||
5138                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
5139                             sizeof(struct lpfc_name)))) {
5140                         /* This port has switched fabrics. FLOGI is required */
5141                         lpfc_initial_flogi(vport);
5142                 } else {
5143                         /* FAN verified - skip FLOGI */
5144                         vport->fc_myDID = vport->fc_prevDID;
5145                         if (phba->sli_rev < LPFC_SLI_REV4)
5146                                 lpfc_issue_fabric_reglogin(vport);
5147                         else
5148                                 lpfc_issue_reg_vfi(vport);
5149                 }
5150         }
5151         return 0;
5152 }
5153
5154 /**
5155  * lpfc_els_timeout - Handler funciton to the els timer
5156  * @ptr: holder for the timer function associated data.
5157  *
5158  * This routine is invoked by the ELS timer after timeout. It posts the ELS
5159  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
5160  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
5161  * up the worker thread. It is for the worker thread to invoke the routine
5162  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
5163  **/
5164 void
5165 lpfc_els_timeout(unsigned long ptr)
5166 {
5167         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5168         struct lpfc_hba   *phba = vport->phba;
5169         uint32_t tmo_posted;
5170         unsigned long iflag;
5171
5172         spin_lock_irqsave(&vport->work_port_lock, iflag);
5173         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
5174         if (!tmo_posted)
5175                 vport->work_port_events |= WORKER_ELS_TMO;
5176         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
5177
5178         if (!tmo_posted)
5179                 lpfc_worker_wake_up(phba);
5180         return;
5181 }
5182
5183
5184 /**
5185  * lpfc_els_timeout_handler - Process an els timeout event
5186  * @vport: pointer to a virtual N_Port data structure.
5187  *
5188  * This routine is the actual handler function that processes an ELS timeout
5189  * event. It walks the ELS ring to get and abort all the IOCBs (except the
5190  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
5191  * invoking the lpfc_sli_issue_abort_iotag() routine.
5192  **/
5193 void
5194 lpfc_els_timeout_handler(struct lpfc_vport *vport)
5195 {
5196         struct lpfc_hba  *phba = vport->phba;
5197         struct lpfc_sli_ring *pring;
5198         struct lpfc_iocbq *tmp_iocb, *piocb;
5199         IOCB_t *cmd = NULL;
5200         struct lpfc_dmabuf *pcmd;
5201         uint32_t els_command = 0;
5202         uint32_t timeout;
5203         uint32_t remote_ID = 0xffffffff;
5204         LIST_HEAD(txcmplq_completions);
5205         LIST_HEAD(abort_list);
5206
5207
5208         timeout = (uint32_t)(phba->fc_ratov << 1);
5209
5210         pring = &phba->sli.ring[LPFC_ELS_RING];
5211
5212         spin_lock_irq(&phba->hbalock);
5213         list_splice_init(&pring->txcmplq, &txcmplq_completions);
5214         spin_unlock_irq(&phba->hbalock);
5215
5216         list_for_each_entry_safe(piocb, tmp_iocb, &txcmplq_completions, list) {
5217                 cmd = &piocb->iocb;
5218
5219                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
5220                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
5221                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5222                         continue;
5223
5224                 if (piocb->vport != vport)
5225                         continue;
5226
5227                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
5228                 if (pcmd)
5229                         els_command = *(uint32_t *) (pcmd->virt);
5230
5231                 if (els_command == ELS_CMD_FARP ||
5232                     els_command == ELS_CMD_FARPR ||
5233                     els_command == ELS_CMD_FDISC)
5234                         continue;
5235
5236                 if (piocb->drvrTimeout > 0) {
5237                         if (piocb->drvrTimeout >= timeout)
5238                                 piocb->drvrTimeout -= timeout;
5239                         else
5240                                 piocb->drvrTimeout = 0;
5241                         continue;
5242                 }
5243
5244                 remote_ID = 0xffffffff;
5245                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
5246                         remote_ID = cmd->un.elsreq64.remoteID;
5247                 else {
5248                         struct lpfc_nodelist *ndlp;
5249                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
5250                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5251                                 remote_ID = ndlp->nlp_DID;
5252                 }
5253                 list_add_tail(&piocb->dlist, &abort_list);
5254         }
5255         spin_lock_irq(&phba->hbalock);
5256         list_splice(&txcmplq_completions, &pring->txcmplq);
5257         spin_unlock_irq(&phba->hbalock);
5258
5259         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
5260                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5261                          "0127 ELS timeout Data: x%x x%x x%x "
5262                          "x%x\n", els_command,
5263                          remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
5264                 spin_lock_irq(&phba->hbalock);
5265                 list_del_init(&piocb->dlist);
5266                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5267                 spin_unlock_irq(&phba->hbalock);
5268         }
5269
5270         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
5271                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
5272 }
5273
5274 /**
5275  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
5276  * @vport: pointer to a host virtual N_Port data structure.
5277  *
5278  * This routine is used to clean up all the outstanding ELS commands on a
5279  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
5280  * routine. After that, it walks the ELS transmit queue to remove all the
5281  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
5282  * the IOCBs with a non-NULL completion callback function, the callback
5283  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5284  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
5285  * callback function, the IOCB will simply be released. Finally, it walks
5286  * the ELS transmit completion queue to issue an abort IOCB to any transmit
5287  * completion queue IOCB that is associated with the @vport and is not
5288  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
5289  * part of the discovery state machine) out to HBA by invoking the
5290  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
5291  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
5292  * the IOCBs are aborted when this function returns.
5293  **/
5294 void
5295 lpfc_els_flush_cmd(struct lpfc_vport *vport)
5296 {
5297         LIST_HEAD(completions);
5298         struct lpfc_hba  *phba = vport->phba;
5299         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5300         struct lpfc_iocbq *tmp_iocb, *piocb;
5301         IOCB_t *cmd = NULL;
5302
5303         lpfc_fabric_abort_vport(vport);
5304
5305         spin_lock_irq(&phba->hbalock);
5306         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5307                 cmd = &piocb->iocb;
5308
5309                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5310                         continue;
5311                 }
5312
5313                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5314                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5315                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5316                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5317                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
5318                         continue;
5319
5320                 if (piocb->vport != vport)
5321                         continue;
5322
5323                 list_move_tail(&piocb->list, &completions);
5324                 pring->txq_cnt--;
5325         }
5326
5327         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5328                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5329                         continue;
5330                 }
5331
5332                 if (piocb->vport != vport)
5333                         continue;
5334
5335                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5336         }
5337         spin_unlock_irq(&phba->hbalock);
5338
5339         /* Cancell all the IOCBs from the completions list */
5340         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5341                               IOERR_SLI_ABORTED);
5342
5343         return;
5344 }
5345
5346 /**
5347  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
5348  * @phba: pointer to lpfc hba data structure.
5349  *
5350  * This routine is used to clean up all the outstanding ELS commands on a
5351  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
5352  * routine. After that, it walks the ELS transmit queue to remove all the
5353  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
5354  * the IOCBs with the completion callback function associated, the callback
5355  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5356  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
5357  * callback function associated, the IOCB will simply be released. Finally,
5358  * it walks the ELS transmit completion queue to issue an abort IOCB to any
5359  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
5360  * management plane IOCBs that are not part of the discovery state machine)
5361  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
5362  **/
5363 void
5364 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
5365 {
5366         LIST_HEAD(completions);
5367         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5368         struct lpfc_iocbq *tmp_iocb, *piocb;
5369         IOCB_t *cmd = NULL;
5370
5371         lpfc_fabric_abort_hba(phba);
5372         spin_lock_irq(&phba->hbalock);
5373         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5374                 cmd = &piocb->iocb;
5375                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5376                         continue;
5377                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5378                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5379                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5380                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5381                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
5382                         continue;
5383                 list_move_tail(&piocb->list, &completions);
5384                 pring->txq_cnt--;
5385         }
5386         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5387                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5388                         continue;
5389                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5390         }
5391         spin_unlock_irq(&phba->hbalock);
5392
5393         /* Cancel all the IOCBs from the completions list */
5394         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5395                               IOERR_SLI_ABORTED);
5396
5397         return;
5398 }
5399
5400 /**
5401  * lpfc_send_els_failure_event - Posts an ELS command failure event
5402  * @phba: Pointer to hba context object.
5403  * @cmdiocbp: Pointer to command iocb which reported error.
5404  * @rspiocbp: Pointer to response iocb which reported error.
5405  *
5406  * This function sends an event when there is an ELS command
5407  * failure.
5408  **/
5409 void
5410 lpfc_send_els_failure_event(struct lpfc_hba *phba,
5411                         struct lpfc_iocbq *cmdiocbp,
5412                         struct lpfc_iocbq *rspiocbp)
5413 {
5414         struct lpfc_vport *vport = cmdiocbp->vport;
5415         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5416         struct lpfc_lsrjt_event lsrjt_event;
5417         struct lpfc_fabric_event_header fabric_event;
5418         struct ls_rjt stat;
5419         struct lpfc_nodelist *ndlp;
5420         uint32_t *pcmd;
5421
5422         ndlp = cmdiocbp->context1;
5423         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5424                 return;
5425
5426         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
5427                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
5428                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
5429                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
5430                         sizeof(struct lpfc_name));
5431                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
5432                         sizeof(struct lpfc_name));
5433                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
5434                         cmdiocbp->context2)->virt);
5435                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
5436                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
5437                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
5438                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
5439                 fc_host_post_vendor_event(shost,
5440                         fc_get_event_number(),
5441                         sizeof(lsrjt_event),
5442                         (char *)&lsrjt_event,
5443                         LPFC_NL_VENDOR_ID);
5444                 return;
5445         }
5446         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
5447                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
5448                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
5449                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
5450                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
5451                 else
5452                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
5453                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
5454                         sizeof(struct lpfc_name));
5455                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
5456                         sizeof(struct lpfc_name));
5457                 fc_host_post_vendor_event(shost,
5458                         fc_get_event_number(),
5459                         sizeof(fabric_event),
5460                         (char *)&fabric_event,
5461                         LPFC_NL_VENDOR_ID);
5462                 return;
5463         }
5464
5465 }
5466
5467 /**
5468  * lpfc_send_els_event - Posts unsolicited els event
5469  * @vport: Pointer to vport object.
5470  * @ndlp: Pointer FC node object.
5471  * @cmd: ELS command code.
5472  *
5473  * This function posts an event when there is an incoming
5474  * unsolicited ELS command.
5475  **/
5476 static void
5477 lpfc_send_els_event(struct lpfc_vport *vport,
5478                     struct lpfc_nodelist *ndlp,
5479                     uint32_t *payload)
5480 {
5481         struct lpfc_els_event_header *els_data = NULL;
5482         struct lpfc_logo_event *logo_data = NULL;
5483         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5484
5485         if (*payload == ELS_CMD_LOGO) {
5486                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
5487                 if (!logo_data) {
5488                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5489                                 "0148 Failed to allocate memory "
5490                                 "for LOGO event\n");
5491                         return;
5492                 }
5493                 els_data = &logo_data->header;
5494         } else {
5495                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
5496                         GFP_KERNEL);
5497                 if (!els_data) {
5498                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5499                                 "0149 Failed to allocate memory "
5500                                 "for ELS event\n");
5501                         return;
5502                 }
5503         }
5504         els_data->event_type = FC_REG_ELS_EVENT;
5505         switch (*payload) {
5506         case ELS_CMD_PLOGI:
5507                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
5508                 break;
5509         case ELS_CMD_PRLO:
5510                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
5511                 break;
5512         case ELS_CMD_ADISC:
5513                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
5514                 break;
5515         case ELS_CMD_LOGO:
5516                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
5517                 /* Copy the WWPN in the LOGO payload */
5518                 memcpy(logo_data->logo_wwpn, &payload[2],
5519                         sizeof(struct lpfc_name));
5520                 break;
5521         default:
5522                 kfree(els_data);
5523                 return;
5524         }
5525         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
5526         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
5527         if (*payload == ELS_CMD_LOGO) {
5528                 fc_host_post_vendor_event(shost,
5529                         fc_get_event_number(),
5530                         sizeof(struct lpfc_logo_event),
5531                         (char *)logo_data,
5532                         LPFC_NL_VENDOR_ID);
5533                 kfree(logo_data);
5534         } else {
5535                 fc_host_post_vendor_event(shost,
5536                         fc_get_event_number(),
5537                         sizeof(struct lpfc_els_event_header),
5538                         (char *)els_data,
5539                         LPFC_NL_VENDOR_ID);
5540                 kfree(els_data);
5541         }
5542
5543         return;
5544 }
5545
5546
5547 /**
5548  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
5549  * @phba: pointer to lpfc hba data structure.
5550  * @pring: pointer to a SLI ring.
5551  * @vport: pointer to a host virtual N_Port data structure.
5552  * @elsiocb: pointer to lpfc els command iocb data structure.
5553  *
5554  * This routine is used for processing the IOCB associated with a unsolicited
5555  * event. It first determines whether there is an existing ndlp that matches
5556  * the DID from the unsolicited IOCB. If not, it will create a new one with
5557  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
5558  * IOCB is then used to invoke the proper routine and to set up proper state
5559  * of the discovery state machine.
5560  **/
5561 static void
5562 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5563                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
5564 {
5565         struct Scsi_Host  *shost;
5566         struct lpfc_nodelist *ndlp;
5567         struct ls_rjt stat;
5568         uint32_t *payload;
5569         uint32_t cmd, did, newnode, rjt_err = 0;
5570         IOCB_t *icmd = &elsiocb->iocb;
5571
5572         if (!vport || !(elsiocb->context2))
5573                 goto dropit;
5574
5575         newnode = 0;
5576         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
5577         cmd = *payload;
5578         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
5579                 lpfc_post_buffer(phba, pring, 1);
5580
5581         did = icmd->un.rcvels.remoteID;
5582         if (icmd->ulpStatus) {
5583                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5584                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
5585                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
5586                 goto dropit;
5587         }
5588
5589         /* Check to see if link went down during discovery */
5590         if (lpfc_els_chk_latt(vport))
5591                 goto dropit;
5592
5593         /* Ignore traffic received during vport shutdown. */
5594         if (vport->load_flag & FC_UNLOADING)
5595                 goto dropit;
5596
5597         ndlp = lpfc_findnode_did(vport, did);
5598         if (!ndlp) {
5599                 /* Cannot find existing Fabric ndlp, so allocate a new one */
5600                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5601                 if (!ndlp)
5602                         goto dropit;
5603
5604                 lpfc_nlp_init(vport, ndlp, did);
5605                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5606                 newnode = 1;
5607                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5608                         ndlp->nlp_type |= NLP_FABRIC;
5609         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5610                 ndlp = lpfc_enable_node(vport, ndlp,
5611                                         NLP_STE_UNUSED_NODE);
5612                 if (!ndlp)
5613                         goto dropit;
5614                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5615                 newnode = 1;
5616                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5617                         ndlp->nlp_type |= NLP_FABRIC;
5618         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
5619                 /* This is similar to the new node path */
5620                 ndlp = lpfc_nlp_get(ndlp);
5621                 if (!ndlp)
5622                         goto dropit;
5623                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5624                 newnode = 1;
5625         }
5626
5627         phba->fc_stat.elsRcvFrame++;
5628
5629         elsiocb->context1 = lpfc_nlp_get(ndlp);
5630         elsiocb->vport = vport;
5631
5632         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
5633                 cmd &= ELS_CMD_MASK;
5634         }
5635         /* ELS command <elsCmd> received from NPORT <did> */
5636         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5637                          "0112 ELS command x%x received from NPORT x%x "
5638                          "Data: x%x\n", cmd, did, vport->port_state);
5639         switch (cmd) {
5640         case ELS_CMD_PLOGI:
5641                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5642                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
5643                         did, vport->port_state, ndlp->nlp_flag);
5644
5645                 phba->fc_stat.elsRcvPLOGI++;
5646                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
5647
5648                 lpfc_send_els_event(vport, ndlp, payload);
5649                 if (vport->port_state < LPFC_DISC_AUTH) {
5650                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
5651                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
5652                                 rjt_err = LSRJT_UNABLE_TPC;
5653                                 break;
5654                         }
5655                         /* We get here, and drop thru, if we are PT2PT with
5656                          * another NPort and the other side has initiated
5657                          * the PLOGI before responding to our FLOGI.
5658                          */
5659                 }
5660
5661                 shost = lpfc_shost_from_vport(vport);
5662                 spin_lock_irq(shost->host_lock);
5663                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
5664                 spin_unlock_irq(shost->host_lock);
5665
5666                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5667                                         NLP_EVT_RCV_PLOGI);
5668
5669                 break;
5670         case ELS_CMD_FLOGI:
5671                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5672                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
5673                         did, vport->port_state, ndlp->nlp_flag);
5674
5675                 phba->fc_stat.elsRcvFLOGI++;
5676                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
5677                 if (newnode)
5678                         lpfc_nlp_put(ndlp);
5679                 break;
5680         case ELS_CMD_LOGO:
5681                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5682                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
5683                         did, vport->port_state, ndlp->nlp_flag);
5684
5685                 phba->fc_stat.elsRcvLOGO++;
5686                 lpfc_send_els_event(vport, ndlp, payload);
5687                 if (vport->port_state < LPFC_DISC_AUTH) {
5688                         rjt_err = LSRJT_UNABLE_TPC;
5689                         break;
5690                 }
5691                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
5692                 break;
5693         case ELS_CMD_PRLO:
5694                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5695                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
5696                         did, vport->port_state, ndlp->nlp_flag);
5697
5698                 phba->fc_stat.elsRcvPRLO++;
5699                 lpfc_send_els_event(vport, ndlp, payload);
5700                 if (vport->port_state < LPFC_DISC_AUTH) {
5701                         rjt_err = LSRJT_UNABLE_TPC;
5702                         break;
5703                 }
5704                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
5705                 break;
5706         case ELS_CMD_RSCN:
5707                 phba->fc_stat.elsRcvRSCN++;
5708                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
5709                 if (newnode)
5710                         lpfc_nlp_put(ndlp);
5711                 break;
5712         case ELS_CMD_ADISC:
5713                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5714                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
5715                         did, vport->port_state, ndlp->nlp_flag);
5716
5717                 lpfc_send_els_event(vport, ndlp, payload);
5718                 phba->fc_stat.elsRcvADISC++;
5719                 if (vport->port_state < LPFC_DISC_AUTH) {
5720                         rjt_err = LSRJT_UNABLE_TPC;
5721                         break;
5722                 }
5723                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5724                                         NLP_EVT_RCV_ADISC);
5725                 break;
5726         case ELS_CMD_PDISC:
5727                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5728                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
5729                         did, vport->port_state, ndlp->nlp_flag);
5730
5731                 phba->fc_stat.elsRcvPDISC++;
5732                 if (vport->port_state < LPFC_DISC_AUTH) {
5733                         rjt_err = LSRJT_UNABLE_TPC;
5734                         break;
5735                 }
5736                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
5737                                         NLP_EVT_RCV_PDISC);
5738                 break;
5739         case ELS_CMD_FARPR:
5740                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5741                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
5742                         did, vport->port_state, ndlp->nlp_flag);
5743
5744                 phba->fc_stat.elsRcvFARPR++;
5745                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
5746                 break;
5747         case ELS_CMD_FARP:
5748                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5749                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
5750                         did, vport->port_state, ndlp->nlp_flag);
5751
5752                 phba->fc_stat.elsRcvFARP++;
5753                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
5754                 break;
5755         case ELS_CMD_FAN:
5756                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5757                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
5758                         did, vport->port_state, ndlp->nlp_flag);
5759
5760                 phba->fc_stat.elsRcvFAN++;
5761                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
5762                 break;
5763         case ELS_CMD_PRLI:
5764                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5765                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
5766                         did, vport->port_state, ndlp->nlp_flag);
5767
5768                 phba->fc_stat.elsRcvPRLI++;
5769                 if (vport->port_state < LPFC_DISC_AUTH) {
5770                         rjt_err = LSRJT_UNABLE_TPC;
5771                         break;
5772                 }
5773                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
5774                 break;
5775         case ELS_CMD_LIRR:
5776                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5777                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
5778                         did, vport->port_state, ndlp->nlp_flag);
5779
5780                 phba->fc_stat.elsRcvLIRR++;
5781                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
5782                 if (newnode)
5783                         lpfc_nlp_put(ndlp);
5784                 break;
5785         case ELS_CMD_RPS:
5786                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5787                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
5788                         did, vport->port_state, ndlp->nlp_flag);
5789
5790                 phba->fc_stat.elsRcvRPS++;
5791                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
5792                 if (newnode)
5793                         lpfc_nlp_put(ndlp);
5794                 break;
5795         case ELS_CMD_RPL:
5796                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5797                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
5798                         did, vport->port_state, ndlp->nlp_flag);
5799
5800                 phba->fc_stat.elsRcvRPL++;
5801                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
5802                 if (newnode)
5803                         lpfc_nlp_put(ndlp);
5804                 break;
5805         case ELS_CMD_RNID:
5806                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5807                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
5808                         did, vport->port_state, ndlp->nlp_flag);
5809
5810                 phba->fc_stat.elsRcvRNID++;
5811                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
5812                 if (newnode)
5813                         lpfc_nlp_put(ndlp);
5814                 break;
5815         case ELS_CMD_RRQ:
5816                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5817                         "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
5818                         did, vport->port_state, ndlp->nlp_flag);
5819
5820                 phba->fc_stat.elsRcvRRQ++;
5821                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
5822                 if (newnode)
5823                         lpfc_nlp_put(ndlp);
5824                 break;
5825         default:
5826                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5827                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
5828                         cmd, did, vport->port_state);
5829
5830                 /* Unsupported ELS command, reject */
5831                 rjt_err = LSRJT_INVALID_CMD;
5832
5833                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
5834                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5835                                  "0115 Unknown ELS command x%x "
5836                                  "received from NPORT x%x\n", cmd, did);
5837                 if (newnode)
5838                         lpfc_nlp_put(ndlp);
5839                 break;
5840         }
5841
5842         /* check if need to LS_RJT received ELS cmd */
5843         if (rjt_err) {
5844                 memset(&stat, 0, sizeof(stat));
5845                 stat.un.b.lsRjtRsnCode = rjt_err;
5846                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
5847                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
5848                         NULL);
5849         }
5850
5851         lpfc_nlp_put(elsiocb->context1);
5852         elsiocb->context1 = NULL;
5853         return;
5854
5855 dropit:
5856         if (vport && !(vport->load_flag & FC_UNLOADING))
5857                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5858                         "0111 Dropping received ELS cmd "
5859                         "Data: x%x x%x x%x\n",
5860                         icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
5861         phba->fc_stat.elsRcvDrop++;
5862 }
5863
5864 /**
5865  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5866  * @phba: pointer to lpfc hba data structure.
5867  * @vpi: host virtual N_Port identifier.
5868  *
5869  * This routine finds a vport on a HBA (referred by @phba) through a
5870  * @vpi. The function walks the HBA's vport list and returns the address
5871  * of the vport with the matching @vpi.
5872  *
5873  * Return code
5874  *    NULL - No vport with the matching @vpi found
5875  *    Otherwise - Address to the vport with the matching @vpi.
5876  **/
5877 struct lpfc_vport *
5878 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5879 {
5880         struct lpfc_vport *vport;
5881         unsigned long flags;
5882
5883         spin_lock_irqsave(&phba->hbalock, flags);
5884         list_for_each_entry(vport, &phba->port_list, listentry) {
5885                 if (vport->vpi == vpi) {
5886                         spin_unlock_irqrestore(&phba->hbalock, flags);
5887                         return vport;
5888                 }
5889         }
5890         spin_unlock_irqrestore(&phba->hbalock, flags);
5891         return NULL;
5892 }
5893
5894 /**
5895  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
5896  * @phba: pointer to lpfc hba data structure.
5897  * @pring: pointer to a SLI ring.
5898  * @elsiocb: pointer to lpfc els iocb data structure.
5899  *
5900  * This routine is used to process an unsolicited event received from a SLI
5901  * (Service Level Interface) ring. The actual processing of the data buffer
5902  * associated with the unsolicited event is done by invoking the routine
5903  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
5904  * SLI ring on which the unsolicited event was received.
5905  **/
5906 void
5907 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5908                      struct lpfc_iocbq *elsiocb)
5909 {
5910         struct lpfc_vport *vport = phba->pport;
5911         IOCB_t *icmd = &elsiocb->iocb;
5912         dma_addr_t paddr;
5913         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
5914         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
5915
5916         elsiocb->context1 = NULL;
5917         elsiocb->context2 = NULL;
5918         elsiocb->context3 = NULL;
5919
5920         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
5921                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
5922         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
5923             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
5924                 phba->fc_stat.NoRcvBuf++;
5925                 /* Not enough posted buffers; Try posting more buffers */
5926                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
5927                         lpfc_post_buffer(phba, pring, 0);
5928                 return;
5929         }
5930
5931         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5932             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
5933              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
5934                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
5935                         vport = phba->pport;
5936                 else
5937                         vport = lpfc_find_vport_by_vpid(phba,
5938                                 icmd->unsli3.rcvsli3.vpi - phba->vpi_base);
5939         }
5940         /* If there are no BDEs associated
5941          * with this IOCB, there is nothing to do.
5942          */
5943         if (icmd->ulpBdeCount == 0)
5944                 return;
5945
5946         /* type of ELS cmd is first 32bit word
5947          * in packet
5948          */
5949         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5950                 elsiocb->context2 = bdeBuf1;
5951         } else {
5952                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
5953                                  icmd->un.cont64[0].addrLow);
5954                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
5955                                                              paddr);
5956         }
5957
5958         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5959         /*
5960          * The different unsolicited event handlers would tell us
5961          * if they are done with "mp" by setting context2 to NULL.
5962          */
5963         if (elsiocb->context2) {
5964                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
5965                 elsiocb->context2 = NULL;
5966         }
5967
5968         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
5969         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
5970             icmd->ulpBdeCount == 2) {
5971                 elsiocb->context2 = bdeBuf2;
5972                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5973                 /* free mp if we are done with it */
5974                 if (elsiocb->context2) {
5975                         lpfc_in_buf_free(phba, elsiocb->context2);
5976                         elsiocb->context2 = NULL;
5977                 }
5978         }
5979 }
5980
5981 /**
5982  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
5983  * @phba: pointer to lpfc hba data structure.
5984  * @vport: pointer to a virtual N_Port data structure.
5985  *
5986  * This routine issues a Port Login (PLOGI) to the Name Server with
5987  * State Change Request (SCR) for a @vport. This routine will create an
5988  * ndlp for the Name Server associated to the @vport if such node does
5989  * not already exist. The PLOGI to Name Server is issued by invoking the
5990  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
5991  * (FDMI) is configured to the @vport, a FDMI node will be created and
5992  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
5993  **/
5994 void
5995 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5996 {
5997         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
5998
5999         ndlp = lpfc_findnode_did(vport, NameServer_DID);
6000         if (!ndlp) {
6001                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
6002                 if (!ndlp) {
6003                         if (phba->fc_topology == TOPOLOGY_LOOP) {
6004                                 lpfc_disc_start(vport);
6005                                 return;
6006                         }
6007                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6008                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6009                                          "0251 NameServer login: no memory\n");
6010                         return;
6011                 }
6012                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
6013         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
6014                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
6015                 if (!ndlp) {
6016                         if (phba->fc_topology == TOPOLOGY_LOOP) {
6017                                 lpfc_disc_start(vport);
6018                                 return;
6019                         }
6020                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6021                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6022                                         "0348 NameServer login: node freed\n");
6023                         return;
6024                 }
6025         }
6026         ndlp->nlp_type |= NLP_FABRIC;
6027
6028         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6029
6030         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
6031                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6032                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6033                                  "0252 Cannot issue NameServer login\n");
6034                 return;
6035         }
6036
6037         if (vport->cfg_fdmi_on) {
6038                 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
6039                                           GFP_KERNEL);
6040                 if (ndlp_fdmi) {
6041                         lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
6042                         ndlp_fdmi->nlp_type |= NLP_FABRIC;
6043                         lpfc_nlp_set_state(vport, ndlp_fdmi,
6044                                 NLP_STE_PLOGI_ISSUE);
6045                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
6046                                              0);
6047                 }
6048         }
6049         return;
6050 }
6051
6052 /**
6053  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
6054  * @phba: pointer to lpfc hba data structure.
6055  * @pmb: pointer to the driver internal queue element for mailbox command.
6056  *
6057  * This routine is the completion callback function to register new vport
6058  * mailbox command. If the new vport mailbox command completes successfully,
6059  * the fabric registration login shall be performed on physical port (the
6060  * new vport created is actually a physical port, with VPI 0) or the port
6061  * login to Name Server for State Change Request (SCR) will be performed
6062  * on virtual port (real virtual port, with VPI greater than 0).
6063  **/
6064 static void
6065 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6066 {
6067         struct lpfc_vport *vport = pmb->vport;
6068         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6069         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
6070         MAILBOX_t *mb = &pmb->u.mb;
6071         int rc;
6072
6073         spin_lock_irq(shost->host_lock);
6074         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6075         spin_unlock_irq(shost->host_lock);
6076
6077         if (mb->mbxStatus) {
6078                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6079                                  "0915 Register VPI failed: 0x%x\n",
6080                                  mb->mbxStatus);
6081
6082                 switch (mb->mbxStatus) {
6083                 case 0x11:      /* unsupported feature */
6084                 case 0x9603:    /* max_vpi exceeded */
6085                 case 0x9602:    /* Link event since CLEAR_LA */
6086                         /* giving up on vport registration */
6087                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6088                         spin_lock_irq(shost->host_lock);
6089                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6090                         spin_unlock_irq(shost->host_lock);
6091                         lpfc_can_disctmo(vport);
6092                         break;
6093                 /* If reg_vpi fail with invalid VPI status, re-init VPI */
6094                 case 0x20:
6095                         spin_lock_irq(shost->host_lock);
6096                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6097                         spin_unlock_irq(shost->host_lock);
6098                         lpfc_init_vpi(phba, pmb, vport->vpi);
6099                         pmb->vport = vport;
6100                         pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
6101                         rc = lpfc_sli_issue_mbox(phba, pmb,
6102                                 MBX_NOWAIT);
6103                         if (rc == MBX_NOT_FINISHED) {
6104                                 lpfc_printf_vlog(vport,
6105                                         KERN_ERR, LOG_MBOX,
6106                                         "2732 Failed to issue INIT_VPI"
6107                                         " mailbox command\n");
6108                         } else {
6109                                 lpfc_nlp_put(ndlp);
6110                                 return;
6111                         }
6112
6113                 default:
6114                         /* Try to recover from this error */
6115                         lpfc_mbx_unreg_vpi(vport);
6116                         spin_lock_irq(shost->host_lock);
6117                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6118                         spin_unlock_irq(shost->host_lock);
6119                         if (vport->port_type == LPFC_PHYSICAL_PORT
6120                                 && !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
6121                                 lpfc_initial_flogi(vport);
6122                         else
6123                                 lpfc_initial_fdisc(vport);
6124                         break;
6125                 }
6126         } else {
6127                 spin_lock_irq(shost->host_lock);
6128                 vport->vpi_state |= LPFC_VPI_REGISTERED;
6129                 spin_unlock_irq(shost->host_lock);
6130                 if (vport == phba->pport) {
6131                         if (phba->sli_rev < LPFC_SLI_REV4)
6132                                 lpfc_issue_fabric_reglogin(vport);
6133                         else {
6134                                 /*
6135                                  * If the physical port is instantiated using
6136                                  * FDISC, do not start vport discovery.
6137                                  */
6138                                 if (vport->port_state != LPFC_FDISC)
6139                                         lpfc_start_fdiscs(phba);
6140                                 lpfc_do_scr_ns_plogi(phba, vport);
6141                         }
6142                 } else
6143                         lpfc_do_scr_ns_plogi(phba, vport);
6144         }
6145
6146         /* Now, we decrement the ndlp reference count held for this
6147          * callback function
6148          */
6149         lpfc_nlp_put(ndlp);
6150
6151         mempool_free(pmb, phba->mbox_mem_pool);
6152         return;
6153 }
6154
6155 /**
6156  * lpfc_register_new_vport - Register a new vport with a HBA
6157  * @phba: pointer to lpfc hba data structure.
6158  * @vport: pointer to a host virtual N_Port data structure.
6159  * @ndlp: pointer to a node-list data structure.
6160  *
6161  * This routine registers the @vport as a new virtual port with a HBA.
6162  * It is done through a registering vpi mailbox command.
6163  **/
6164 void
6165 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
6166                         struct lpfc_nodelist *ndlp)
6167 {
6168         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6169         LPFC_MBOXQ_t *mbox;
6170
6171         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6172         if (mbox) {
6173                 lpfc_reg_vpi(vport, mbox);
6174                 mbox->vport = vport;
6175                 mbox->context2 = lpfc_nlp_get(ndlp);
6176                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
6177                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6178                     == MBX_NOT_FINISHED) {
6179                         /* mailbox command not success, decrement ndlp
6180                          * reference count for this command
6181                          */
6182                         lpfc_nlp_put(ndlp);
6183                         mempool_free(mbox, phba->mbox_mem_pool);
6184
6185                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6186                                 "0253 Register VPI: Can't send mbox\n");
6187                         goto mbox_err_exit;
6188                 }
6189         } else {
6190                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6191                                  "0254 Register VPI: no memory\n");
6192                 goto mbox_err_exit;
6193         }
6194         return;
6195
6196 mbox_err_exit:
6197         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6198         spin_lock_irq(shost->host_lock);
6199         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6200         spin_unlock_irq(shost->host_lock);
6201         return;
6202 }
6203
6204 /**
6205  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
6206  * @phba: pointer to lpfc hba data structure.
6207  *
6208  * This routine cancels the retry delay timers to all the vports.
6209  **/
6210 void
6211 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
6212 {
6213         struct lpfc_vport **vports;
6214         struct lpfc_nodelist *ndlp;
6215         uint32_t link_state;
6216         int i;
6217
6218         /* Treat this failure as linkdown for all vports */
6219         link_state = phba->link_state;
6220         lpfc_linkdown(phba);
6221         phba->link_state = link_state;
6222
6223         vports = lpfc_create_vport_work_array(phba);
6224
6225         if (vports) {
6226                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6227                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6228                         if (ndlp)
6229                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6230                         lpfc_els_flush_cmd(vports[i]);
6231                 }
6232                 lpfc_destroy_vport_work_array(phba, vports);
6233         }
6234 }
6235
6236 /**
6237  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
6238  * @phba: pointer to lpfc hba data structure.
6239  *
6240  * This routine abort all pending discovery commands and
6241  * start a timer to retry FLOGI for the physical port
6242  * discovery.
6243  **/
6244 void
6245 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
6246 {
6247         struct lpfc_nodelist *ndlp;
6248         struct Scsi_Host  *shost;
6249
6250         /* Cancel the all vports retry delay retry timers */
6251         lpfc_cancel_all_vport_retry_delay_timer(phba);
6252
6253         /* If fabric require FLOGI, then re-instantiate physical login */
6254         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6255         if (!ndlp)
6256                 return;
6257
6258         shost = lpfc_shost_from_vport(phba->pport);
6259         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
6260         spin_lock_irq(shost->host_lock);
6261         ndlp->nlp_flag |= NLP_DELAY_TMO;
6262         spin_unlock_irq(shost->host_lock);
6263         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
6264         phba->pport->port_state = LPFC_FLOGI;
6265         return;
6266 }
6267
6268 /**
6269  * lpfc_fabric_login_reqd - Check if FLOGI required.
6270  * @phba: pointer to lpfc hba data structure.
6271  * @cmdiocb: pointer to FDISC command iocb.
6272  * @rspiocb: pointer to FDISC response iocb.
6273  *
6274  * This routine checks if a FLOGI is reguired for FDISC
6275  * to succeed.
6276  **/
6277 static int
6278 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
6279                 struct lpfc_iocbq *cmdiocb,
6280                 struct lpfc_iocbq *rspiocb)
6281 {
6282
6283         if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
6284                 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
6285                 return 0;
6286         else
6287                 return 1;
6288 }
6289
6290 /**
6291  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
6292  * @phba: pointer to lpfc hba data structure.
6293  * @cmdiocb: pointer to lpfc command iocb data structure.
6294  * @rspiocb: pointer to lpfc response iocb data structure.
6295  *
6296  * This routine is the completion callback function to a Fabric Discover
6297  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
6298  * single threaded, each FDISC completion callback function will reset
6299  * the discovery timer for all vports such that the timers will not get
6300  * unnecessary timeout. The function checks the FDISC IOCB status. If error
6301  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
6302  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
6303  * assigned to the vport has been changed with the completion of the FDISC
6304  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
6305  * are unregistered from the HBA, and then the lpfc_register_new_vport()
6306  * routine is invoked to register new vport with the HBA. Otherwise, the
6307  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
6308  * Server for State Change Request (SCR).
6309  **/
6310 static void
6311 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6312                     struct lpfc_iocbq *rspiocb)
6313 {
6314         struct lpfc_vport *vport = cmdiocb->vport;
6315         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6316         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
6317         struct lpfc_nodelist *np;
6318         struct lpfc_nodelist *next_np;
6319         IOCB_t *irsp = &rspiocb->iocb;
6320         struct lpfc_iocbq *piocb;
6321
6322         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6323                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
6324                          irsp->ulpStatus, irsp->un.ulpWord[4],
6325                          vport->fc_prevDID);
6326         /* Since all FDISCs are being single threaded, we
6327          * must reset the discovery timer for ALL vports
6328          * waiting to send FDISC when one completes.
6329          */
6330         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
6331                 lpfc_set_disctmo(piocb->vport);
6332         }
6333
6334         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6335                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
6336                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
6337
6338         if (irsp->ulpStatus) {
6339
6340                 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
6341                         lpfc_retry_pport_discovery(phba);
6342                         goto out;
6343                 }
6344
6345                 /* Check for retry */
6346                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
6347                         goto out;
6348                 /* FDISC failed */
6349                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6350                                  "0126 FDISC failed. (%d/%d)\n",
6351                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
6352                 goto fdisc_failed;
6353         }
6354         spin_lock_irq(shost->host_lock);
6355         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
6356         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
6357         vport->fc_flag |= FC_FABRIC;
6358         if (vport->phba->fc_topology == TOPOLOGY_LOOP)
6359                 vport->fc_flag |=  FC_PUBLIC_LOOP;
6360         spin_unlock_irq(shost->host_lock);
6361
6362         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
6363         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
6364         if ((vport->fc_prevDID != vport->fc_myDID) &&
6365                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
6366                 /* If our NportID changed, we need to ensure all
6367                  * remaining NPORTs get unreg_login'ed so we can
6368                  * issue unreg_vpi.
6369                  */
6370                 list_for_each_entry_safe(np, next_np,
6371                         &vport->fc_nodes, nlp_listp) {
6372                         if (!NLP_CHK_NODE_ACT(ndlp) ||
6373                             (np->nlp_state != NLP_STE_NPR_NODE) ||
6374                             !(np->nlp_flag & NLP_NPR_ADISC))
6375                                 continue;
6376                         spin_lock_irq(shost->host_lock);
6377                         np->nlp_flag &= ~NLP_NPR_ADISC;
6378                         spin_unlock_irq(shost->host_lock);
6379                         lpfc_unreg_rpi(vport, np);
6380                 }
6381                 lpfc_cleanup_pending_mbox(vport);
6382                 lpfc_mbx_unreg_vpi(vport);
6383                 spin_lock_irq(shost->host_lock);
6384                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6385                 if (phba->sli_rev == LPFC_SLI_REV4)
6386                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6387                 else
6388                         vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
6389                 spin_unlock_irq(shost->host_lock);
6390         }
6391
6392         if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
6393                 lpfc_issue_init_vpi(vport);
6394         else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
6395                 lpfc_register_new_vport(phba, vport, ndlp);
6396         else
6397                 lpfc_do_scr_ns_plogi(phba, vport);
6398         goto out;
6399 fdisc_failed:
6400         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6401         /* Cancel discovery timer */
6402         lpfc_can_disctmo(vport);
6403         lpfc_nlp_put(ndlp);
6404 out:
6405         lpfc_els_free_iocb(phba, cmdiocb);
6406 }
6407
6408 /**
6409  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
6410  * @vport: pointer to a virtual N_Port data structure.
6411  * @ndlp: pointer to a node-list data structure.
6412  * @retry: number of retries to the command IOCB.
6413  *
6414  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
6415  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
6416  * routine to issue the IOCB, which makes sure only one outstanding fabric
6417  * IOCB will be sent off HBA at any given time.
6418  *
6419  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6420  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6421  * will be stored into the context1 field of the IOCB for the completion
6422  * callback function to the FDISC ELS command.
6423  *
6424  * Return code
6425  *   0 - Successfully issued fdisc iocb command
6426  *   1 - Failed to issue fdisc iocb command
6427  **/
6428 static int
6429 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
6430                      uint8_t retry)
6431 {
6432         struct lpfc_hba *phba = vport->phba;
6433         IOCB_t *icmd;
6434         struct lpfc_iocbq *elsiocb;
6435         struct serv_parm *sp;
6436         uint8_t *pcmd;
6437         uint16_t cmdsize;
6438         int did = ndlp->nlp_DID;
6439         int rc;
6440
6441         vport->port_state = LPFC_FDISC;
6442         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
6443         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
6444                                      ELS_CMD_FDISC);
6445         if (!elsiocb) {
6446                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6447                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6448                                  "0255 Issue FDISC: no IOCB\n");
6449                 return 1;
6450         }
6451
6452         icmd = &elsiocb->iocb;
6453         icmd->un.elsreq64.myID = 0;
6454         icmd->un.elsreq64.fl = 1;
6455
6456         if  (phba->sli_rev == LPFC_SLI_REV4) {
6457                 /* FDISC needs to be 1 for WQE VPI */
6458                 elsiocb->iocb.ulpCt_h = (SLI4_CT_VPI >> 1) & 1;
6459                 elsiocb->iocb.ulpCt_l = SLI4_CT_VPI & 1 ;
6460                 /* Set the ulpContext to the vpi */
6461                 elsiocb->iocb.ulpContext = vport->vpi + phba->vpi_base;
6462         } else {
6463                 /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
6464                 icmd->ulpCt_h = 1;
6465                 icmd->ulpCt_l = 0;
6466         }
6467
6468         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6469         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
6470         pcmd += sizeof(uint32_t); /* CSP Word 1 */
6471         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
6472         sp = (struct serv_parm *) pcmd;
6473         /* Setup CSPs accordingly for Fabric */
6474         sp->cmn.e_d_tov = 0;
6475         sp->cmn.w2.r_a_tov = 0;
6476         sp->cls1.classValid = 0;
6477         sp->cls2.seqDelivery = 1;
6478         sp->cls3.seqDelivery = 1;
6479
6480         pcmd += sizeof(uint32_t); /* CSP Word 2 */
6481         pcmd += sizeof(uint32_t); /* CSP Word 3 */
6482         pcmd += sizeof(uint32_t); /* CSP Word 4 */
6483         pcmd += sizeof(uint32_t); /* Port Name */
6484         memcpy(pcmd, &vport->fc_portname, 8);
6485         pcmd += sizeof(uint32_t); /* Node Name */
6486         pcmd += sizeof(uint32_t); /* Node Name */
6487         memcpy(pcmd, &vport->fc_nodename, 8);
6488
6489         lpfc_set_disctmo(vport);
6490
6491         phba->fc_stat.elsXmitFDISC++;
6492         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
6493
6494         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6495                 "Issue FDISC:     did:x%x",
6496                 did, 0, 0);
6497
6498         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
6499         if (rc == IOCB_ERROR) {
6500                 lpfc_els_free_iocb(phba, elsiocb);
6501                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6502                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6503                                  "0256 Issue FDISC: Cannot send IOCB\n");
6504                 return 1;
6505         }
6506         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
6507         return 0;
6508 }
6509
6510 /**
6511  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
6512  * @phba: pointer to lpfc hba data structure.
6513  * @cmdiocb: pointer to lpfc command iocb data structure.
6514  * @rspiocb: pointer to lpfc response iocb data structure.
6515  *
6516  * This routine is the completion callback function to the issuing of a LOGO
6517  * ELS command off a vport. It frees the command IOCB and then decrement the
6518  * reference count held on ndlp for this completion function, indicating that
6519  * the reference to the ndlp is no long needed. Note that the
6520  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
6521  * callback function and an additional explicit ndlp reference decrementation
6522  * will trigger the actual release of the ndlp.
6523  **/
6524 static void
6525 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6526                         struct lpfc_iocbq *rspiocb)
6527 {
6528         struct lpfc_vport *vport = cmdiocb->vport;
6529         IOCB_t *irsp;
6530         struct lpfc_nodelist *ndlp;
6531         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
6532
6533         irsp = &rspiocb->iocb;
6534         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6535                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
6536                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
6537
6538         lpfc_els_free_iocb(phba, cmdiocb);
6539         vport->unreg_vpi_cmpl = VPORT_ERROR;
6540
6541         /* Trigger the release of the ndlp after logo */
6542         lpfc_nlp_put(ndlp);
6543 }
6544
6545 /**
6546  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
6547  * @vport: pointer to a virtual N_Port data structure.
6548  * @ndlp: pointer to a node-list data structure.
6549  *
6550  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
6551  *
6552  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6553  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6554  * will be stored into the context1 field of the IOCB for the completion
6555  * callback function to the LOGO ELS command.
6556  *
6557  * Return codes
6558  *   0 - Successfully issued logo off the @vport
6559  *   1 - Failed to issue logo off the @vport
6560  **/
6561 int
6562 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6563 {
6564         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6565         struct lpfc_hba  *phba = vport->phba;
6566         IOCB_t *icmd;
6567         struct lpfc_iocbq *elsiocb;
6568         uint8_t *pcmd;
6569         uint16_t cmdsize;
6570
6571         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
6572         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
6573                                      ELS_CMD_LOGO);
6574         if (!elsiocb)
6575                 return 1;
6576
6577         icmd = &elsiocb->iocb;
6578         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6579         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
6580         pcmd += sizeof(uint32_t);
6581
6582         /* Fill in LOGO payload */
6583         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
6584         pcmd += sizeof(uint32_t);
6585         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
6586
6587         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6588                 "Issue LOGO npiv  did:x%x flg:x%x",
6589                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
6590
6591         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
6592         spin_lock_irq(shost->host_lock);
6593         ndlp->nlp_flag |= NLP_LOGO_SND;
6594         spin_unlock_irq(shost->host_lock);
6595         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
6596             IOCB_ERROR) {
6597                 spin_lock_irq(shost->host_lock);
6598                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
6599                 spin_unlock_irq(shost->host_lock);
6600                 lpfc_els_free_iocb(phba, elsiocb);
6601                 return 1;
6602         }
6603         return 0;
6604 }
6605
6606 /**
6607  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
6608  * @ptr: holder for the timer function associated data.
6609  *
6610  * This routine is invoked by the fabric iocb block timer after
6611  * timeout. It posts the fabric iocb block timeout event by setting the
6612  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
6613  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
6614  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
6615  * posted event WORKER_FABRIC_BLOCK_TMO.
6616  **/
6617 void
6618 lpfc_fabric_block_timeout(unsigned long ptr)
6619 {
6620         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6621         unsigned long iflags;
6622         uint32_t tmo_posted;
6623
6624         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
6625         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
6626         if (!tmo_posted)
6627                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
6628         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
6629
6630         if (!tmo_posted)
6631                 lpfc_worker_wake_up(phba);
6632         return;
6633 }
6634
6635 /**
6636  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
6637  * @phba: pointer to lpfc hba data structure.
6638  *
6639  * This routine issues one fabric iocb from the driver internal list to
6640  * the HBA. It first checks whether it's ready to issue one fabric iocb to
6641  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
6642  * remove one pending fabric iocb from the driver internal list and invokes
6643  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
6644  **/
6645 static void
6646 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
6647 {
6648         struct lpfc_iocbq *iocb;
6649         unsigned long iflags;
6650         int ret;
6651         IOCB_t *cmd;
6652
6653 repeat:
6654         iocb = NULL;
6655         spin_lock_irqsave(&phba->hbalock, iflags);
6656         /* Post any pending iocb to the SLI layer */
6657         if (atomic_read(&phba->fabric_iocb_count) == 0) {
6658                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
6659                                  list);
6660                 if (iocb)
6661                         /* Increment fabric iocb count to hold the position */
6662                         atomic_inc(&phba->fabric_iocb_count);
6663         }
6664         spin_unlock_irqrestore(&phba->hbalock, iflags);
6665         if (iocb) {
6666                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6667                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6668                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6669
6670                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6671                         "Fabric sched1:   ste:x%x",
6672                         iocb->vport->port_state, 0, 0);
6673
6674                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
6675
6676                 if (ret == IOCB_ERROR) {
6677                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6678                         iocb->fabric_iocb_cmpl = NULL;
6679                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6680                         cmd = &iocb->iocb;
6681                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6682                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6683                         iocb->iocb_cmpl(phba, iocb, iocb);
6684
6685                         atomic_dec(&phba->fabric_iocb_count);
6686                         goto repeat;
6687                 }
6688         }
6689
6690         return;
6691 }
6692
6693 /**
6694  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
6695  * @phba: pointer to lpfc hba data structure.
6696  *
6697  * This routine unblocks the  issuing fabric iocb command. The function
6698  * will clear the fabric iocb block bit and then invoke the routine
6699  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
6700  * from the driver internal fabric iocb list.
6701  **/
6702 void
6703 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
6704 {
6705         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6706
6707         lpfc_resume_fabric_iocbs(phba);
6708         return;
6709 }
6710
6711 /**
6712  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
6713  * @phba: pointer to lpfc hba data structure.
6714  *
6715  * This routine blocks the issuing fabric iocb for a specified amount of
6716  * time (currently 100 ms). This is done by set the fabric iocb block bit
6717  * and set up a timeout timer for 100ms. When the block bit is set, no more
6718  * fabric iocb will be issued out of the HBA.
6719  **/
6720 static void
6721 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
6722 {
6723         int blocked;
6724
6725         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6726         /* Start a timer to unblock fabric iocbs after 100ms */
6727         if (!blocked)
6728                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
6729
6730         return;
6731 }
6732
6733 /**
6734  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
6735  * @phba: pointer to lpfc hba data structure.
6736  * @cmdiocb: pointer to lpfc command iocb data structure.
6737  * @rspiocb: pointer to lpfc response iocb data structure.
6738  *
6739  * This routine is the callback function that is put to the fabric iocb's
6740  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
6741  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
6742  * function first restores and invokes the original iocb's callback function
6743  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
6744  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
6745  **/
6746 static void
6747 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6748         struct lpfc_iocbq *rspiocb)
6749 {
6750         struct ls_rjt stat;
6751
6752         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
6753                 BUG();
6754
6755         switch (rspiocb->iocb.ulpStatus) {
6756                 case IOSTAT_NPORT_RJT:
6757                 case IOSTAT_FABRIC_RJT:
6758                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
6759                                 lpfc_block_fabric_iocbs(phba);
6760                         }
6761                         break;
6762
6763                 case IOSTAT_NPORT_BSY:
6764                 case IOSTAT_FABRIC_BSY:
6765                         lpfc_block_fabric_iocbs(phba);
6766                         break;
6767
6768                 case IOSTAT_LS_RJT:
6769                         stat.un.lsRjtError =
6770                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
6771                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
6772                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
6773                                 lpfc_block_fabric_iocbs(phba);
6774                         break;
6775         }
6776
6777         if (atomic_read(&phba->fabric_iocb_count) == 0)
6778                 BUG();
6779
6780         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
6781         cmdiocb->fabric_iocb_cmpl = NULL;
6782         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
6783         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
6784
6785         atomic_dec(&phba->fabric_iocb_count);
6786         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
6787                 /* Post any pending iocbs to HBA */
6788                 lpfc_resume_fabric_iocbs(phba);
6789         }
6790 }
6791
6792 /**
6793  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
6794  * @phba: pointer to lpfc hba data structure.
6795  * @iocb: pointer to lpfc command iocb data structure.
6796  *
6797  * This routine is used as the top-level API for issuing a fabric iocb command
6798  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
6799  * function makes sure that only one fabric bound iocb will be outstanding at
6800  * any given time. As such, this function will first check to see whether there
6801  * is already an outstanding fabric iocb on the wire. If so, it will put the
6802  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
6803  * issued later. Otherwise, it will issue the iocb on the wire and update the
6804  * fabric iocb count it indicate that there is one fabric iocb on the wire.
6805  *
6806  * Note, this implementation has a potential sending out fabric IOCBs out of
6807  * order. The problem is caused by the construction of the "ready" boolen does
6808  * not include the condition that the internal fabric IOCB list is empty. As
6809  * such, it is possible a fabric IOCB issued by this routine might be "jump"
6810  * ahead of the fabric IOCBs in the internal list.
6811  *
6812  * Return code
6813  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
6814  *   IOCB_ERROR - failed to issue fabric iocb
6815  **/
6816 static int
6817 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
6818 {
6819         unsigned long iflags;
6820         int ready;
6821         int ret;
6822
6823         if (atomic_read(&phba->fabric_iocb_count) > 1)
6824                 BUG();
6825
6826         spin_lock_irqsave(&phba->hbalock, iflags);
6827         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
6828                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6829
6830         if (ready)
6831                 /* Increment fabric iocb count to hold the position */
6832                 atomic_inc(&phba->fabric_iocb_count);
6833         spin_unlock_irqrestore(&phba->hbalock, iflags);
6834         if (ready) {
6835                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6836                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6837                 iocb->iocb_flag |= LPFC_IO_FABRIC;
6838
6839                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6840                         "Fabric sched2:   ste:x%x",
6841                         iocb->vport->port_state, 0, 0);
6842
6843                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
6844
6845                 if (ret == IOCB_ERROR) {
6846                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6847                         iocb->fabric_iocb_cmpl = NULL;
6848                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6849                         atomic_dec(&phba->fabric_iocb_count);
6850                 }
6851         } else {
6852                 spin_lock_irqsave(&phba->hbalock, iflags);
6853                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
6854                 spin_unlock_irqrestore(&phba->hbalock, iflags);
6855                 ret = IOCB_SUCCESS;
6856         }
6857         return ret;
6858 }
6859
6860 /**
6861  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
6862  * @vport: pointer to a virtual N_Port data structure.
6863  *
6864  * This routine aborts all the IOCBs associated with a @vport from the
6865  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6866  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6867  * list, removes each IOCB associated with the @vport off the list, set the
6868  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6869  * associated with the IOCB.
6870  **/
6871 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
6872 {
6873         LIST_HEAD(completions);
6874         struct lpfc_hba  *phba = vport->phba;
6875         struct lpfc_iocbq *tmp_iocb, *piocb;
6876
6877         spin_lock_irq(&phba->hbalock);
6878         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6879                                  list) {
6880
6881                 if (piocb->vport != vport)
6882                         continue;
6883
6884                 list_move_tail(&piocb->list, &completions);
6885         }
6886         spin_unlock_irq(&phba->hbalock);
6887
6888         /* Cancel all the IOCBs from the completions list */
6889         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6890                               IOERR_SLI_ABORTED);
6891 }
6892
6893 /**
6894  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
6895  * @ndlp: pointer to a node-list data structure.
6896  *
6897  * This routine aborts all the IOCBs associated with an @ndlp from the
6898  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6899  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6900  * list, removes each IOCB associated with the @ndlp off the list, set the
6901  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6902  * associated with the IOCB.
6903  **/
6904 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
6905 {
6906         LIST_HEAD(completions);
6907         struct lpfc_hba  *phba = ndlp->phba;
6908         struct lpfc_iocbq *tmp_iocb, *piocb;
6909         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6910
6911         spin_lock_irq(&phba->hbalock);
6912         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6913                                  list) {
6914                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
6915
6916                         list_move_tail(&piocb->list, &completions);
6917                 }
6918         }
6919         spin_unlock_irq(&phba->hbalock);
6920
6921         /* Cancel all the IOCBs from the completions list */
6922         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6923                               IOERR_SLI_ABORTED);
6924 }
6925
6926 /**
6927  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
6928  * @phba: pointer to lpfc hba data structure.
6929  *
6930  * This routine aborts all the IOCBs currently on the driver internal
6931  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
6932  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
6933  * list, removes IOCBs off the list, set the status feild to
6934  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
6935  * the IOCB.
6936  **/
6937 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
6938 {
6939         LIST_HEAD(completions);
6940
6941         spin_lock_irq(&phba->hbalock);
6942         list_splice_init(&phba->fabric_iocb_list, &completions);
6943         spin_unlock_irq(&phba->hbalock);
6944
6945         /* Cancel all the IOCBs from the completions list */
6946         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6947                               IOERR_SLI_ABORTED);
6948 }
6949
6950 /**
6951  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
6952  * @phba: pointer to lpfc hba data structure.
6953  * @axri: pointer to the els xri abort wcqe structure.
6954  *
6955  * This routine is invoked by the worker thread to process a SLI4 slow-path
6956  * ELS aborted xri.
6957  **/
6958 void
6959 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
6960                           struct sli4_wcqe_xri_aborted *axri)
6961 {
6962         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
6963         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6964         unsigned long iflag = 0;
6965         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6966
6967         spin_lock_irqsave(&phba->hbalock, iflag);
6968         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
6969         list_for_each_entry_safe(sglq_entry, sglq_next,
6970                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
6971                 if (sglq_entry->sli4_xritag == xri) {
6972                         list_del(&sglq_entry->list);
6973                         list_add_tail(&sglq_entry->list,
6974                                 &phba->sli4_hba.lpfc_sgl_list);
6975                         sglq_entry->state = SGL_FREED;
6976                         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
6977                         spin_unlock_irqrestore(&phba->hbalock, iflag);
6978
6979                         /* Check if TXQ queue needs to be serviced */
6980                         if (pring->txq_cnt)
6981                                 lpfc_worker_wake_up(phba);
6982                         return;
6983                 }
6984         }
6985         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
6986         sglq_entry = __lpfc_get_active_sglq(phba, xri);
6987         if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
6988                 spin_unlock_irqrestore(&phba->hbalock, iflag);
6989                 return;
6990         }
6991         sglq_entry->state = SGL_XRI_ABORTED;
6992         spin_unlock_irqrestore(&phba->hbalock, iflag);
6993         return;
6994 }