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