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