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