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