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