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