]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/lpfc/lpfc_els.c
1725b81770e92b5a99fe24afa7661082f3cf68d2
[karo-tx-linux.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 = phba->vpi_ids[vport->vpi];
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
458         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
459         if (!mboxq) {
460                 rc = -ENOMEM;
461                 goto fail_free_coherent;
462         }
463         vport->port_state = LPFC_FABRIC_CFG_LINK;
464         memcpy(dmabuf->virt, &phba->fc_fabparam, sizeof(vport->fc_sparam));
465         lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
466         mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
467         mboxq->vport = vport;
468         mboxq->context1 = dmabuf;
469         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
470         if (rc == MBX_NOT_FINISHED) {
471                 rc = -ENXIO;
472                 goto fail_free_mbox;
473         }
474         return 0;
475
476 fail_free_mbox:
477         mempool_free(mboxq, phba->mbox_mem_pool);
478 fail_free_coherent:
479         lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
480 fail_free_dmabuf:
481         kfree(dmabuf);
482 fail:
483         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
484         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
485                 "0289 Issue Register VFI failed: Err %d\n", rc);
486         return rc;
487 }
488
489 /**
490  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
491  * @vport: pointer to a host virtual N_Port data structure.
492  * @sp: pointer to service parameter data structure.
493  *
494  * This routine is called from FLOGI/FDISC completion handler functions.
495  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
496  * node nodename is changed in the completion service parameter else return
497  * 0. This function also set flag in the vport data structure to delay
498  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
499  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
500  * node nodename is changed in the completion service parameter.
501  *
502  * Return code
503  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
504  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
505  *
506  **/
507 static uint8_t
508 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
509                 struct serv_parm *sp)
510 {
511         uint8_t fabric_param_changed = 0;
512         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
513
514         if ((vport->fc_prevDID != vport->fc_myDID) ||
515                 memcmp(&vport->fabric_portname, &sp->portName,
516                         sizeof(struct lpfc_name)) ||
517                 memcmp(&vport->fabric_nodename, &sp->nodeName,
518                         sizeof(struct lpfc_name)))
519                 fabric_param_changed = 1;
520
521         /*
522          * Word 1 Bit 31 in common service parameter is overloaded.
523          * Word 1 Bit 31 in FLOGI request is multiple NPort request
524          * Word 1 Bit 31 in FLOGI response is clean address bit
525          *
526          * If fabric parameter is changed and clean address bit is
527          * cleared delay nport discovery if
528          * - vport->fc_prevDID != 0 (not initial discovery) OR
529          * - lpfc_delay_discovery module parameter is set.
530          */
531         if (fabric_param_changed && !sp->cmn.clean_address_bit &&
532             (vport->fc_prevDID || lpfc_delay_discovery)) {
533                 spin_lock_irq(shost->host_lock);
534                 vport->fc_flag |= FC_DISC_DELAYED;
535                 spin_unlock_irq(shost->host_lock);
536         }
537
538         return fabric_param_changed;
539 }
540
541
542 /**
543  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
544  * @vport: pointer to a host virtual N_Port data structure.
545  * @ndlp: pointer to a node-list data structure.
546  * @sp: pointer to service parameter data structure.
547  * @irsp: pointer to the IOCB within the lpfc response IOCB.
548  *
549  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
550  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
551  * port in a fabric topology. It properly sets up the parameters to the @ndlp
552  * from the IOCB response. It also check the newly assigned N_Port ID to the
553  * @vport against the previously assigned N_Port ID. If it is different from
554  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
555  * is invoked on all the remaining nodes with the @vport to unregister the
556  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
557  * is invoked to register login to the fabric.
558  *
559  * Return code
560  *   0 - Success (currently, always return 0)
561  **/
562 static int
563 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
564                            struct serv_parm *sp, IOCB_t *irsp)
565 {
566         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
567         struct lpfc_hba  *phba = vport->phba;
568         struct lpfc_nodelist *np;
569         struct lpfc_nodelist *next_np;
570         uint8_t fabric_param_changed;
571
572         spin_lock_irq(shost->host_lock);
573         vport->fc_flag |= FC_FABRIC;
574         spin_unlock_irq(shost->host_lock);
575
576         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
577         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
578                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
579
580         phba->fc_edtovResol = sp->cmn.edtovResolution;
581         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
582
583         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
584                 spin_lock_irq(shost->host_lock);
585                 vport->fc_flag |= FC_PUBLIC_LOOP;
586                 spin_unlock_irq(shost->host_lock);
587         }
588
589         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
590         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
591         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
592         ndlp->nlp_class_sup = 0;
593         if (sp->cls1.classValid)
594                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
595         if (sp->cls2.classValid)
596                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
597         if (sp->cls3.classValid)
598                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
599         if (sp->cls4.classValid)
600                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
601         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
602                                 sp->cmn.bbRcvSizeLsb;
603
604         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
605         memcpy(&vport->fabric_portname, &sp->portName,
606                         sizeof(struct lpfc_name));
607         memcpy(&vport->fabric_nodename, &sp->nodeName,
608                         sizeof(struct lpfc_name));
609         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
610
611         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
612                 if (sp->cmn.response_multiple_NPort) {
613                         lpfc_printf_vlog(vport, KERN_WARNING,
614                                          LOG_ELS | LOG_VPORT,
615                                          "1816 FLOGI NPIV supported, "
616                                          "response data 0x%x\n",
617                                          sp->cmn.response_multiple_NPort);
618                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
619                 } else {
620                         /* Because we asked f/w for NPIV it still expects us
621                         to call reg_vnpid atleast for the physcial host */
622                         lpfc_printf_vlog(vport, KERN_WARNING,
623                                          LOG_ELS | LOG_VPORT,
624                                          "1817 Fabric does not support NPIV "
625                                          "- configuring single port mode.\n");
626                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
627                 }
628         }
629
630         if (fabric_param_changed &&
631                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
632
633                 /* If our NportID changed, we need to ensure all
634                  * remaining NPORTs get unreg_login'ed.
635                  */
636                 list_for_each_entry_safe(np, next_np,
637                                         &vport->fc_nodes, nlp_listp) {
638                         if (!NLP_CHK_NODE_ACT(np))
639                                 continue;
640                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
641                                    !(np->nlp_flag & NLP_NPR_ADISC))
642                                 continue;
643                         spin_lock_irq(shost->host_lock);
644                         np->nlp_flag &= ~NLP_NPR_ADISC;
645                         spin_unlock_irq(shost->host_lock);
646                         lpfc_unreg_rpi(vport, np);
647                 }
648                 lpfc_cleanup_pending_mbox(vport);
649
650                 if (phba->sli_rev == LPFC_SLI_REV4) {
651                         lpfc_sli4_unreg_all_rpis(vport);
652                         lpfc_mbx_unreg_vpi(vport);
653                         spin_lock_irq(shost->host_lock);
654                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
655                         /*
656                         * If VPI is unreged, driver need to do INIT_VPI
657                         * before re-registering
658                         */
659                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
660                         spin_unlock_irq(shost->host_lock);
661                 }
662         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
663                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
664                         /*
665                          * Driver needs to re-reg VPI in order for f/w
666                          * to update the MAC address.
667                          */
668                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
669                         lpfc_register_new_vport(phba, vport, ndlp);
670                         return 0;
671         }
672
673         if (phba->sli_rev < LPFC_SLI_REV4) {
674                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
675                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
676                     vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
677                         lpfc_register_new_vport(phba, vport, ndlp);
678                 else
679                         lpfc_issue_fabric_reglogin(vport);
680         } else {
681                 ndlp->nlp_type |= NLP_FABRIC;
682                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
683                 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
684                         (vport->vpi_state & LPFC_VPI_REGISTERED)) {
685                         lpfc_start_fdiscs(phba);
686                         lpfc_do_scr_ns_plogi(phba, vport);
687                 } else if (vport->fc_flag & FC_VFI_REGISTERED)
688                         lpfc_issue_init_vpi(vport);
689                 else
690                         lpfc_issue_reg_vfi(vport);
691         }
692         return 0;
693 }
694 /**
695  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
696  * @vport: pointer to a host virtual N_Port data structure.
697  * @ndlp: pointer to a node-list data structure.
698  * @sp: pointer to service parameter data structure.
699  *
700  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
701  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
702  * in a point-to-point topology. First, the @vport's N_Port Name is compared
703  * with the received N_Port Name: if the @vport's N_Port Name is greater than
704  * the received N_Port Name lexicographically, this node shall assign local
705  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
706  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
707  * this node shall just wait for the remote node to issue PLOGI and assign
708  * N_Port IDs.
709  *
710  * Return code
711  *   0 - Success
712  *   -ENXIO - Fail
713  **/
714 static int
715 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
716                           struct serv_parm *sp)
717 {
718         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
719         struct lpfc_hba  *phba = vport->phba;
720         LPFC_MBOXQ_t *mbox;
721         int rc;
722
723         spin_lock_irq(shost->host_lock);
724         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
725         spin_unlock_irq(shost->host_lock);
726
727         phba->fc_edtov = FF_DEF_EDTOV;
728         phba->fc_ratov = FF_DEF_RATOV;
729         rc = memcmp(&vport->fc_portname, &sp->portName,
730                     sizeof(vport->fc_portname));
731         if (rc >= 0) {
732                 /* This side will initiate the PLOGI */
733                 spin_lock_irq(shost->host_lock);
734                 vport->fc_flag |= FC_PT2PT_PLOGI;
735                 spin_unlock_irq(shost->host_lock);
736
737                 /*
738                  * N_Port ID cannot be 0, set our to LocalID the other
739                  * side will be RemoteID.
740                  */
741
742                 /* not equal */
743                 if (rc)
744                         vport->fc_myDID = PT2PT_LocalID;
745
746                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
747                 if (!mbox)
748                         goto fail;
749
750                 lpfc_config_link(phba, mbox);
751
752                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
753                 mbox->vport = vport;
754                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
755                 if (rc == MBX_NOT_FINISHED) {
756                         mempool_free(mbox, phba->mbox_mem_pool);
757                         goto fail;
758                 }
759                 /* Decrement ndlp reference count indicating that ndlp can be
760                  * safely released when other references to it are done.
761                  */
762                 lpfc_nlp_put(ndlp);
763
764                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
765                 if (!ndlp) {
766                         /*
767                          * Cannot find existing Fabric ndlp, so allocate a
768                          * new one
769                          */
770                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
771                         if (!ndlp)
772                                 goto fail;
773                         lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
774                 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
775                         ndlp = lpfc_enable_node(vport, ndlp,
776                                                 NLP_STE_UNUSED_NODE);
777                         if(!ndlp)
778                                 goto fail;
779                 }
780
781                 memcpy(&ndlp->nlp_portname, &sp->portName,
782                        sizeof(struct lpfc_name));
783                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
784                        sizeof(struct lpfc_name));
785                 /* Set state will put ndlp onto node list if not already done */
786                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
787                 spin_lock_irq(shost->host_lock);
788                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
789                 spin_unlock_irq(shost->host_lock);
790         } else
791                 /* This side will wait for the PLOGI, decrement ndlp reference
792                  * count indicating that ndlp can be released when other
793                  * references to it are done.
794                  */
795                 lpfc_nlp_put(ndlp);
796
797         /* If we are pt2pt with another NPort, force NPIV off! */
798         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
799
800         spin_lock_irq(shost->host_lock);
801         vport->fc_flag |= FC_PT2PT;
802         spin_unlock_irq(shost->host_lock);
803
804         /* Start discovery - this should just do CLEAR_LA */
805         lpfc_disc_start(vport);
806         return 0;
807 fail:
808         return -ENXIO;
809 }
810
811 /**
812  * lpfc_cmpl_els_flogi - Completion callback function for flogi
813  * @phba: pointer to lpfc hba data structure.
814  * @cmdiocb: pointer to lpfc command iocb data structure.
815  * @rspiocb: pointer to lpfc response iocb data structure.
816  *
817  * This routine is the top-level completion callback function for issuing
818  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
819  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
820  * retry has been made (either immediately or delayed with lpfc_els_retry()
821  * returning 1), the command IOCB will be released and function returned.
822  * If the retry attempt has been given up (possibly reach the maximum
823  * number of retries), one additional decrement of ndlp reference shall be
824  * invoked before going out after releasing the command IOCB. This will
825  * actually release the remote node (Note, lpfc_els_free_iocb() will also
826  * invoke one decrement of ndlp reference count). If no error reported in
827  * the IOCB status, the command Port ID field is used to determine whether
828  * this is a point-to-point topology or a fabric topology: if the Port ID
829  * field is assigned, it is a fabric topology; otherwise, it is a
830  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
831  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
832  * specific topology completion conditions.
833  **/
834 static void
835 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
836                     struct lpfc_iocbq *rspiocb)
837 {
838         struct lpfc_vport *vport = cmdiocb->vport;
839         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
840         IOCB_t *irsp = &rspiocb->iocb;
841         struct lpfc_nodelist *ndlp = cmdiocb->context1;
842         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
843         struct serv_parm *sp;
844         uint16_t fcf_index;
845         int rc;
846
847         /* Check to see if link went down during discovery */
848         if (lpfc_els_chk_latt(vport)) {
849                 /* One additional decrement on node reference count to
850                  * trigger the release of the node
851                  */
852                 lpfc_nlp_put(ndlp);
853                 goto out;
854         }
855
856         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
857                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
858                 irsp->ulpStatus, irsp->un.ulpWord[4],
859                 vport->port_state);
860
861         if (irsp->ulpStatus) {
862                 /*
863                  * In case of FIP mode, perform roundrobin FCF failover
864                  * due to new FCF discovery
865                  */
866                 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
867                     (phba->fcf.fcf_flag & FCF_DISCOVERY) &&
868                     !((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
869                      (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED))) {
870                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
871                                         "2611 FLOGI failed on FCF (x%x), "
872                                         "status:x%x/x%x, tmo:x%x, perform "
873                                         "roundrobin FCF failover\n",
874                                         phba->fcf.current_rec.fcf_indx,
875                                         irsp->ulpStatus, irsp->un.ulpWord[4],
876                                         irsp->ulpTimeout);
877                         fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
878                         rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
879                         if (rc)
880                                 goto out;
881                 }
882
883                 /* FLOGI failure */
884                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
885                                 "2858 FLOGI failure Status:x%x/x%x TMO:x%x\n",
886                                 irsp->ulpStatus, irsp->un.ulpWord[4],
887                                 irsp->ulpTimeout);
888
889                 /* Check for retry */
890                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
891                         goto out;
892
893                 /* FLOGI failure */
894                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
895                                  "0100 FLOGI failure Status:x%x/x%x TMO:x%x\n",
896                                  irsp->ulpStatus, irsp->un.ulpWord[4],
897                                  irsp->ulpTimeout);
898
899                 /* FLOGI failed, so there is no fabric */
900                 spin_lock_irq(shost->host_lock);
901                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
902                 spin_unlock_irq(shost->host_lock);
903
904                 /* If private loop, then allow max outstanding els to be
905                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
906                  * alpa map would take too long otherwise.
907                  */
908                 if (phba->alpa_map[0] == 0) {
909                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
910                         if ((phba->sli_rev == LPFC_SLI_REV4) &&
911                             (!(vport->fc_flag & FC_VFI_REGISTERED) ||
912                              (vport->fc_prevDID != vport->fc_myDID))) {
913                                 if (vport->fc_flag & FC_VFI_REGISTERED)
914                                         lpfc_sli4_unreg_all_rpis(vport);
915                                 lpfc_issue_reg_vfi(vport);
916                                 lpfc_nlp_put(ndlp);
917                                 goto out;
918                         }
919                 }
920                 goto flogifail;
921         }
922         spin_lock_irq(shost->host_lock);
923         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
924         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
925         spin_unlock_irq(shost->host_lock);
926
927         /*
928          * The FLogI succeeded.  Sync the data for the CPU before
929          * accessing it.
930          */
931         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
932
933         sp = prsp->virt + sizeof(uint32_t);
934
935         /* FLOGI completes successfully */
936         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
937                          "0101 FLOGI completes successfully "
938                          "Data: x%x x%x x%x x%x\n",
939                          irsp->un.ulpWord[4], sp->cmn.e_d_tov,
940                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
941
942         if (vport->port_state == LPFC_FLOGI) {
943                 /*
944                  * If Common Service Parameters indicate Nport
945                  * we are point to point, if Fport we are Fabric.
946                  */
947                 if (sp->cmn.fPort)
948                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
949                 else if (!(phba->hba_flag & HBA_FCOE_MODE))
950                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
951                 else {
952                         lpfc_printf_vlog(vport, KERN_ERR,
953                                 LOG_FIP | LOG_ELS,
954                                 "2831 FLOGI response with cleared Fabric "
955                                 "bit fcf_index 0x%x "
956                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
957                                 "Fabric Name "
958                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
959                                 phba->fcf.current_rec.fcf_indx,
960                                 phba->fcf.current_rec.switch_name[0],
961                                 phba->fcf.current_rec.switch_name[1],
962                                 phba->fcf.current_rec.switch_name[2],
963                                 phba->fcf.current_rec.switch_name[3],
964                                 phba->fcf.current_rec.switch_name[4],
965                                 phba->fcf.current_rec.switch_name[5],
966                                 phba->fcf.current_rec.switch_name[6],
967                                 phba->fcf.current_rec.switch_name[7],
968                                 phba->fcf.current_rec.fabric_name[0],
969                                 phba->fcf.current_rec.fabric_name[1],
970                                 phba->fcf.current_rec.fabric_name[2],
971                                 phba->fcf.current_rec.fabric_name[3],
972                                 phba->fcf.current_rec.fabric_name[4],
973                                 phba->fcf.current_rec.fabric_name[5],
974                                 phba->fcf.current_rec.fabric_name[6],
975                                 phba->fcf.current_rec.fabric_name[7]);
976                         lpfc_nlp_put(ndlp);
977                         spin_lock_irq(&phba->hbalock);
978                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
979                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
980                         spin_unlock_irq(&phba->hbalock);
981                         goto out;
982                 }
983                 if (!rc) {
984                         /* Mark the FCF discovery process done */
985                         if (phba->hba_flag & HBA_FIP_SUPPORT)
986                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
987                                                 LOG_ELS,
988                                                 "2769 FLOGI to FCF (x%x) "
989                                                 "completed successfully\n",
990                                                 phba->fcf.current_rec.fcf_indx);
991                         spin_lock_irq(&phba->hbalock);
992                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
993                         phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
994                         spin_unlock_irq(&phba->hbalock);
995                         goto out;
996                 }
997         }
998
999 flogifail:
1000         lpfc_nlp_put(ndlp);
1001
1002         if (!lpfc_error_lost_link(irsp)) {
1003                 /* FLOGI failed, so just use loop map to make discovery list */
1004                 lpfc_disc_list_loopmap(vport);
1005
1006                 /* Start discovery */
1007                 lpfc_disc_start(vport);
1008         } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1009                         ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
1010                         (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
1011                         (phba->link_state != LPFC_CLEAR_LA)) {
1012                 /* If FLOGI failed enable link interrupt. */
1013                 lpfc_issue_clear_la(phba, vport);
1014         }
1015 out:
1016         lpfc_els_free_iocb(phba, cmdiocb);
1017 }
1018
1019 /**
1020  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1021  * @vport: pointer to a host virtual N_Port data structure.
1022  * @ndlp: pointer to a node-list data structure.
1023  * @retry: number of retries to the command IOCB.
1024  *
1025  * This routine issues a Fabric Login (FLOGI) Request ELS command
1026  * for a @vport. The initiator service parameters are put into the payload
1027  * of the FLOGI Request IOCB and the top-level callback function pointer
1028  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1029  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1030  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1031  *
1032  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1033  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1034  * will be stored into the context1 field of the IOCB for the completion
1035  * callback function to the FLOGI ELS command.
1036  *
1037  * Return code
1038  *   0 - successfully issued flogi iocb for @vport
1039  *   1 - failed to issue flogi iocb for @vport
1040  **/
1041 static int
1042 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1043                      uint8_t retry)
1044 {
1045         struct lpfc_hba  *phba = vport->phba;
1046         struct serv_parm *sp;
1047         IOCB_t *icmd;
1048         struct lpfc_iocbq *elsiocb;
1049         struct lpfc_sli_ring *pring;
1050         uint8_t *pcmd;
1051         uint16_t cmdsize;
1052         uint32_t tmo;
1053         int rc;
1054
1055         pring = &phba->sli.ring[LPFC_ELS_RING];
1056
1057         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1058         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1059                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1060
1061         if (!elsiocb)
1062                 return 1;
1063
1064         icmd = &elsiocb->iocb;
1065         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1066
1067         /* For FLOGI request, remainder of payload is service parameters */
1068         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1069         pcmd += sizeof(uint32_t);
1070         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1071         sp = (struct serv_parm *) pcmd;
1072
1073         /* Setup CSPs accordingly for Fabric */
1074         sp->cmn.e_d_tov = 0;
1075         sp->cmn.w2.r_a_tov = 0;
1076         sp->cls1.classValid = 0;
1077         sp->cls2.seqDelivery = 1;
1078         sp->cls3.seqDelivery = 1;
1079         if (sp->cmn.fcphLow < FC_PH3)
1080                 sp->cmn.fcphLow = FC_PH3;
1081         if (sp->cmn.fcphHigh < FC_PH3)
1082                 sp->cmn.fcphHigh = FC_PH3;
1083
1084         if  (phba->sli_rev == LPFC_SLI_REV4) {
1085                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1086                     LPFC_SLI_INTF_IF_TYPE_0) {
1087                         elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1088                         elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1089                         /* FLOGI needs to be 3 for WQE FCFI */
1090                         /* Set the fcfi to the fcfi we registered with */
1091                         elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1092                 }
1093         } else {
1094                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1095                         sp->cmn.request_multiple_Nport = 1;
1096                         /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1097                         icmd->ulpCt_h = 1;
1098                         icmd->ulpCt_l = 0;
1099                 } else
1100                         sp->cmn.request_multiple_Nport = 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 / rx_id */
3657                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3658                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3659                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3660                 pcmd += sizeof(uint32_t);
3661
3662                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3663                         "Issue ACC:       did:x%x flg:x%x",
3664                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3665                 break;
3666         case ELS_CMD_PLOGI:
3667                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3668                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3669                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3670                 if (!elsiocb)
3671                         return 1;
3672
3673                 icmd = &elsiocb->iocb;
3674                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
3675                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3676                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3677
3678                 if (mbox)
3679                         elsiocb->context_un.mbox = mbox;
3680
3681                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3682                 pcmd += sizeof(uint32_t);
3683                 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3684
3685                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3686                         "Issue ACC PLOGI: did:x%x flg:x%x",
3687                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3688                 break;
3689         case ELS_CMD_PRLO:
3690                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3691                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3692                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3693                 if (!elsiocb)
3694                         return 1;
3695
3696                 icmd = &elsiocb->iocb;
3697                 icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
3698                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3699                 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3700
3701                 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3702                        sizeof(uint32_t) + sizeof(PRLO));
3703                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3704                 els_pkt_ptr = (ELS_PKT *) pcmd;
3705                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3706
3707                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3708                         "Issue ACC PRLO:  did:x%x flg:x%x",
3709                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
3710                 break;
3711         default:
3712                 return 1;
3713         }
3714         /* Xmit ELS ACC response tag <ulpIoTag> */
3715         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3716                          "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3717                          "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3718                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3719                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3720                          ndlp->nlp_rpi);
3721         if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3722                 spin_lock_irq(shost->host_lock);
3723                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3724                 spin_unlock_irq(shost->host_lock);
3725                 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3726         } else {
3727                 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3728         }
3729
3730         phba->fc_stat.elsXmitACC++;
3731         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3732         if (rc == IOCB_ERROR) {
3733                 lpfc_els_free_iocb(phba, elsiocb);
3734                 return 1;
3735         }
3736         return 0;
3737 }
3738
3739 /**
3740  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
3741  * @vport: pointer to a virtual N_Port data structure.
3742  * @rejectError:
3743  * @oldiocb: pointer to the original lpfc command iocb data structure.
3744  * @ndlp: pointer to a node-list data structure.
3745  * @mbox: pointer to the driver internal queue element for mailbox command.
3746  *
3747  * This routine prepares and issue an Reject (RJT) response IOCB
3748  * command. If a @mbox pointer is passed in, it will be put into the
3749  * context_un.mbox field of the IOCB for the completion callback function
3750  * to issue to the HBA later.
3751  *
3752  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3753  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3754  * will be stored into the context1 field of the IOCB for the completion
3755  * callback function to the reject response ELS IOCB command.
3756  *
3757  * Return code
3758  *   0 - Successfully issued reject response
3759  *   1 - Failed to issue reject response
3760  **/
3761 int
3762 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3763                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3764                     LPFC_MBOXQ_t *mbox)
3765 {
3766         struct lpfc_hba  *phba = vport->phba;
3767         IOCB_t *icmd;
3768         IOCB_t *oldcmd;
3769         struct lpfc_iocbq *elsiocb;
3770         struct lpfc_sli *psli;
3771         uint8_t *pcmd;
3772         uint16_t cmdsize;
3773         int rc;
3774
3775         psli = &phba->sli;
3776         cmdsize = 2 * sizeof(uint32_t);
3777         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3778                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
3779         if (!elsiocb)
3780                 return 1;
3781
3782         icmd = &elsiocb->iocb;
3783         oldcmd = &oldiocb->iocb;
3784         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
3785         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3786         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3787
3788         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3789         pcmd += sizeof(uint32_t);
3790         *((uint32_t *) (pcmd)) = rejectError;
3791
3792         if (mbox)
3793                 elsiocb->context_un.mbox = mbox;
3794
3795         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
3796         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3797                          "0129 Xmit ELS RJT x%x response tag x%x "
3798                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3799                          "rpi x%x\n",
3800                          rejectError, elsiocb->iotag,
3801                          elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3802                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3803         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3804                 "Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3805                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3806
3807         phba->fc_stat.elsXmitLSRJT++;
3808         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3809         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3810
3811         if (rc == IOCB_ERROR) {
3812                 lpfc_els_free_iocb(phba, elsiocb);
3813                 return 1;
3814         }
3815         return 0;
3816 }
3817
3818 /**
3819  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
3820  * @vport: pointer to a virtual N_Port data structure.
3821  * @oldiocb: pointer to the original lpfc command iocb data structure.
3822  * @ndlp: pointer to a node-list data structure.
3823  *
3824  * This routine prepares and issues an Accept (ACC) response to Address
3825  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3826  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3827  *
3828  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3829  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3830  * will be stored into the context1 field of the IOCB for the completion
3831  * callback function to the ADISC Accept response ELS IOCB command.
3832  *
3833  * Return code
3834  *   0 - Successfully issued acc adisc response
3835  *   1 - Failed to issue adisc acc response
3836  **/
3837 int
3838 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3839                        struct lpfc_nodelist *ndlp)
3840 {
3841         struct lpfc_hba  *phba = vport->phba;
3842         ADISC *ap;
3843         IOCB_t *icmd, *oldcmd;
3844         struct lpfc_iocbq *elsiocb;
3845         uint8_t *pcmd;
3846         uint16_t cmdsize;
3847         int rc;
3848
3849         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3850         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3851                                      ndlp->nlp_DID, ELS_CMD_ACC);
3852         if (!elsiocb)
3853                 return 1;
3854
3855         icmd = &elsiocb->iocb;
3856         oldcmd = &oldiocb->iocb;
3857         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
3858         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3859
3860         /* Xmit ADISC ACC response tag <ulpIoTag> */
3861         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3862                          "0130 Xmit ADISC ACC response iotag x%x xri: "
3863                          "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3864                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3865                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3866                          ndlp->nlp_rpi);
3867         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3868
3869         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3870         pcmd += sizeof(uint32_t);
3871
3872         ap = (ADISC *) (pcmd);
3873         ap->hardAL_PA = phba->fc_pref_ALPA;
3874         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3875         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3876         ap->DID = be32_to_cpu(vport->fc_myDID);
3877
3878         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3879                 "Issue ACC ADISC: did:x%x flg:x%x",
3880                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3881
3882         phba->fc_stat.elsXmitACC++;
3883         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3884         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3885         if (rc == IOCB_ERROR) {
3886                 lpfc_els_free_iocb(phba, elsiocb);
3887                 return 1;
3888         }
3889         return 0;
3890 }
3891
3892 /**
3893  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
3894  * @vport: pointer to a virtual N_Port data structure.
3895  * @oldiocb: pointer to the original lpfc command iocb data structure.
3896  * @ndlp: pointer to a node-list data structure.
3897  *
3898  * This routine prepares and issues an Accept (ACC) response to Process
3899  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3900  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3901  *
3902  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3903  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3904  * will be stored into the context1 field of the IOCB for the completion
3905  * callback function to the PRLI Accept response ELS IOCB command.
3906  *
3907  * Return code
3908  *   0 - Successfully issued acc prli response
3909  *   1 - Failed to issue acc prli response
3910  **/
3911 int
3912 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3913                       struct lpfc_nodelist *ndlp)
3914 {
3915         struct lpfc_hba  *phba = vport->phba;
3916         PRLI *npr;
3917         lpfc_vpd_t *vpd;
3918         IOCB_t *icmd;
3919         IOCB_t *oldcmd;
3920         struct lpfc_iocbq *elsiocb;
3921         struct lpfc_sli *psli;
3922         uint8_t *pcmd;
3923         uint16_t cmdsize;
3924         int rc;
3925
3926         psli = &phba->sli;
3927
3928         cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3929         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3930                 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3931         if (!elsiocb)
3932                 return 1;
3933
3934         icmd = &elsiocb->iocb;
3935         oldcmd = &oldiocb->iocb;
3936         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
3937         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3938
3939         /* Xmit PRLI ACC response tag <ulpIoTag> */
3940         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3941                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3942                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3943                          elsiocb->iotag, elsiocb->iocb.ulpContext,
3944                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3945                          ndlp->nlp_rpi);
3946         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3947
3948         *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3949         pcmd += sizeof(uint32_t);
3950
3951         /* For PRLI, remainder of payload is PRLI parameter page */
3952         memset(pcmd, 0, sizeof(PRLI));
3953
3954         npr = (PRLI *) pcmd;
3955         vpd = &phba->vpd;
3956         /*
3957          * If the remote port is a target and our firmware version is 3.20 or
3958          * later, set the following bits for FC-TAPE support.
3959          */
3960         if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3961             (vpd->rev.feaLevelHigh >= 0x02)) {
3962                 npr->ConfmComplAllowed = 1;
3963                 npr->Retry = 1;
3964                 npr->TaskRetryIdReq = 1;
3965         }
3966
3967         npr->acceptRspCode = PRLI_REQ_EXECUTED;
3968         npr->estabImagePair = 1;
3969         npr->readXferRdyDis = 1;
3970         npr->ConfmComplAllowed = 1;
3971
3972         npr->prliType = PRLI_FCP_TYPE;
3973         npr->initiatorFunc = 1;
3974
3975         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3976                 "Issue ACC PRLI:  did:x%x flg:x%x",
3977                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3978
3979         phba->fc_stat.elsXmitACC++;
3980         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3981
3982         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3983         if (rc == IOCB_ERROR) {
3984                 lpfc_els_free_iocb(phba, elsiocb);
3985                 return 1;
3986         }
3987         return 0;
3988 }
3989
3990 /**
3991  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
3992  * @vport: pointer to a virtual N_Port data structure.
3993  * @format: rnid command format.
3994  * @oldiocb: pointer to the original lpfc command iocb data structure.
3995  * @ndlp: pointer to a node-list data structure.
3996  *
3997  * This routine issues a Request Node Identification Data (RNID) Accept
3998  * (ACC) response. It constructs the RNID ACC response command according to
3999  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4000  * issue the response. Note that this command does not need to hold the ndlp
4001  * reference count for the callback. So, the ndlp reference count taken by
4002  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4003  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4004  * there is no ndlp reference available.
4005  *
4006  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4007  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4008  * will be stored into the context1 field of the IOCB for the completion
4009  * callback function. However, for the RNID Accept Response ELS command,
4010  * this is undone later by this routine after the IOCB is allocated.
4011  *
4012  * Return code
4013  *   0 - Successfully issued acc rnid response
4014  *   1 - Failed to issue acc rnid response
4015  **/
4016 static int
4017 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
4018                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4019 {
4020         struct lpfc_hba  *phba = vport->phba;
4021         RNID *rn;
4022         IOCB_t *icmd, *oldcmd;
4023         struct lpfc_iocbq *elsiocb;
4024         struct lpfc_sli *psli;
4025         uint8_t *pcmd;
4026         uint16_t cmdsize;
4027         int rc;
4028
4029         psli = &phba->sli;
4030         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4031                                         + (2 * sizeof(struct lpfc_name));
4032         if (format)
4033                 cmdsize += sizeof(RNID_TOP_DISC);
4034
4035         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4036                                      ndlp->nlp_DID, ELS_CMD_ACC);
4037         if (!elsiocb)
4038                 return 1;
4039
4040         icmd = &elsiocb->iocb;
4041         oldcmd = &oldiocb->iocb;
4042         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
4043         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4044
4045         /* Xmit RNID ACC response tag <ulpIoTag> */
4046         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4047                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
4048                          elsiocb->iotag, elsiocb->iocb.ulpContext);
4049         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4050         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4051         pcmd += sizeof(uint32_t);
4052
4053         memset(pcmd, 0, sizeof(RNID));
4054         rn = (RNID *) (pcmd);
4055         rn->Format = format;
4056         rn->CommonLen = (2 * sizeof(struct lpfc_name));
4057         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4058         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
4059         switch (format) {
4060         case 0:
4061                 rn->SpecificLen = 0;
4062                 break;
4063         case RNID_TOPOLOGY_DISC:
4064                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
4065                 memcpy(&rn->un.topologyDisc.portName,
4066                        &vport->fc_portname, sizeof(struct lpfc_name));
4067                 rn->un.topologyDisc.unitType = RNID_HBA;
4068                 rn->un.topologyDisc.physPort = 0;
4069                 rn->un.topologyDisc.attachedNodes = 0;
4070                 break;
4071         default:
4072                 rn->CommonLen = 0;
4073                 rn->SpecificLen = 0;
4074                 break;
4075         }
4076
4077         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4078                 "Issue ACC RNID:  did:x%x flg:x%x",
4079                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4080
4081         phba->fc_stat.elsXmitACC++;
4082         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4083         lpfc_nlp_put(ndlp);
4084         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
4085                                     * it could be freed */
4086
4087         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4088         if (rc == IOCB_ERROR) {
4089                 lpfc_els_free_iocb(phba, elsiocb);
4090                 return 1;
4091         }
4092         return 0;
4093 }
4094
4095 /**
4096  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
4097  * @vport: pointer to a virtual N_Port data structure.
4098  * @iocb: pointer to the lpfc command iocb data structure.
4099  * @ndlp: pointer to a node-list data structure.
4100  *
4101  * Return
4102  **/
4103 static void
4104 lpfc_els_clear_rrq(struct lpfc_vport *vport,
4105       struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
4106 {
4107         struct lpfc_hba  *phba = vport->phba;
4108         uint8_t *pcmd;
4109         struct RRQ *rrq;
4110         uint16_t rxid;
4111         uint16_t xri;
4112         struct lpfc_node_rrq *prrq;
4113
4114
4115         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
4116         pcmd += sizeof(uint32_t);
4117         rrq = (struct RRQ *)pcmd;
4118         rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
4119         rxid = bf_get(rrq_rxid, rrq);
4120
4121         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4122                         "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
4123                         " x%x x%x\n",
4124                         be32_to_cpu(bf_get(rrq_did, rrq)),
4125                         bf_get(rrq_oxid, rrq),
4126                         rxid,
4127                         iocb->iotag, iocb->iocb.ulpContext);
4128
4129         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4130                 "Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
4131                 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
4132         if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
4133                 xri = bf_get(rrq_oxid, rrq);
4134         else
4135                 xri = rxid;
4136         prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
4137         if (prrq)
4138                 lpfc_clr_rrq_active(phba, xri, prrq);
4139         return;
4140 }
4141
4142 /**
4143  * lpfc_els_rsp_echo_acc - Issue echo acc response
4144  * @vport: pointer to a virtual N_Port data structure.
4145  * @data: pointer to echo data to return in the accept.
4146  * @oldiocb: pointer to the original lpfc command iocb data structure.
4147  * @ndlp: pointer to a node-list data structure.
4148  *
4149  * Return code
4150  *   0 - Successfully issued acc echo response
4151  *   1 - Failed to issue acc echo response
4152  **/
4153 static int
4154 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
4155                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4156 {
4157         struct lpfc_hba  *phba = vport->phba;
4158         struct lpfc_iocbq *elsiocb;
4159         struct lpfc_sli *psli;
4160         uint8_t *pcmd;
4161         uint16_t cmdsize;
4162         int rc;
4163
4164         psli = &phba->sli;
4165         cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
4166
4167         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4168                                      ndlp->nlp_DID, ELS_CMD_ACC);
4169         if (!elsiocb)
4170                 return 1;
4171
4172         elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
4173         elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
4174
4175         /* Xmit ECHO ACC response tag <ulpIoTag> */
4176         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4177                          "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
4178                          elsiocb->iotag, elsiocb->iocb.ulpContext);
4179         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4180         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4181         pcmd += sizeof(uint32_t);
4182         memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
4183
4184         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4185                 "Issue ACC ECHO:  did:x%x flg:x%x",
4186                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4187
4188         phba->fc_stat.elsXmitACC++;
4189         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4190         lpfc_nlp_put(ndlp);
4191         elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
4192                                     * it could be freed */
4193
4194         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4195         if (rc == IOCB_ERROR) {
4196                 lpfc_els_free_iocb(phba, elsiocb);
4197                 return 1;
4198         }
4199         return 0;
4200 }
4201
4202 /**
4203  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
4204  * @vport: pointer to a host virtual N_Port data structure.
4205  *
4206  * This routine issues Address Discover (ADISC) ELS commands to those
4207  * N_Ports which are in node port recovery state and ADISC has not been issued
4208  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
4209  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
4210  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
4211  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
4212  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4213  * IOCBs quit for later pick up. On the other hand, after walking through
4214  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4215  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4216  * no more ADISC need to be sent.
4217  *
4218  * Return code
4219  *    The number of N_Ports with adisc issued.
4220  **/
4221 int
4222 lpfc_els_disc_adisc(struct lpfc_vport *vport)
4223 {
4224         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4225         struct lpfc_nodelist *ndlp, *next_ndlp;
4226         int sentadisc = 0;
4227
4228         /* go thru NPR nodes and issue any remaining ELS ADISCs */
4229         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4230                 if (!NLP_CHK_NODE_ACT(ndlp))
4231                         continue;
4232                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4233                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4234                     (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
4235                         spin_lock_irq(shost->host_lock);
4236                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4237                         spin_unlock_irq(shost->host_lock);
4238                         ndlp->nlp_prev_state = ndlp->nlp_state;
4239                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4240                         lpfc_issue_els_adisc(vport, ndlp, 0);
4241                         sentadisc++;
4242                         vport->num_disc_nodes++;
4243                         if (vport->num_disc_nodes >=
4244                             vport->cfg_discovery_threads) {
4245                                 spin_lock_irq(shost->host_lock);
4246                                 vport->fc_flag |= FC_NLP_MORE;
4247                                 spin_unlock_irq(shost->host_lock);
4248                                 break;
4249                         }
4250                 }
4251         }
4252         if (sentadisc == 0) {
4253                 spin_lock_irq(shost->host_lock);
4254                 vport->fc_flag &= ~FC_NLP_MORE;
4255                 spin_unlock_irq(shost->host_lock);
4256         }
4257         return sentadisc;
4258 }
4259
4260 /**
4261  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
4262  * @vport: pointer to a host virtual N_Port data structure.
4263  *
4264  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4265  * which are in node port recovery state, with a @vport. Each time an ELS
4266  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4267  * the per @vport number of discover count (num_disc_nodes) shall be
4268  * incremented. If the num_disc_nodes reaches a pre-configured threshold
4269  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4270  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4271  * later pick up. On the other hand, after walking through all the ndlps with
4272  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4273  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4274  * PLOGI need to be sent.
4275  *
4276  * Return code
4277  *   The number of N_Ports with plogi issued.
4278  **/
4279 int
4280 lpfc_els_disc_plogi(struct lpfc_vport *vport)
4281 {
4282         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4283         struct lpfc_nodelist *ndlp, *next_ndlp;
4284         int sentplogi = 0;
4285
4286         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
4287         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4288                 if (!NLP_CHK_NODE_ACT(ndlp))
4289                         continue;
4290                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4291                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4292                     (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4293                     (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4294                         ndlp->nlp_prev_state = ndlp->nlp_state;
4295                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4296                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4297                         sentplogi++;
4298                         vport->num_disc_nodes++;
4299                         if (vport->num_disc_nodes >=
4300                             vport->cfg_discovery_threads) {
4301                                 spin_lock_irq(shost->host_lock);
4302                                 vport->fc_flag |= FC_NLP_MORE;
4303                                 spin_unlock_irq(shost->host_lock);
4304                                 break;
4305                         }
4306                 }
4307         }
4308         if (sentplogi) {
4309                 lpfc_set_disctmo(vport);
4310         }
4311         else {
4312                 spin_lock_irq(shost->host_lock);
4313                 vport->fc_flag &= ~FC_NLP_MORE;
4314                 spin_unlock_irq(shost->host_lock);
4315         }
4316         return sentplogi;
4317 }
4318
4319 /**
4320  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
4321  * @vport: pointer to a host virtual N_Port data structure.
4322  *
4323  * This routine cleans up any Registration State Change Notification
4324  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
4325  * @vport together with the host_lock is used to prevent multiple thread
4326  * trying to access the RSCN array on a same @vport at the same time.
4327  **/
4328 void
4329 lpfc_els_flush_rscn(struct lpfc_vport *vport)
4330 {
4331         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4332         struct lpfc_hba  *phba = vport->phba;
4333         int i;
4334
4335         spin_lock_irq(shost->host_lock);
4336         if (vport->fc_rscn_flush) {
4337                 /* Another thread is walking fc_rscn_id_list on this vport */
4338                 spin_unlock_irq(shost->host_lock);
4339                 return;
4340         }
4341         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
4342         vport->fc_rscn_flush = 1;
4343         spin_unlock_irq(shost->host_lock);
4344
4345         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4346                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
4347                 vport->fc_rscn_id_list[i] = NULL;
4348         }
4349         spin_lock_irq(shost->host_lock);
4350         vport->fc_rscn_id_cnt = 0;
4351         vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
4352         spin_unlock_irq(shost->host_lock);
4353         lpfc_can_disctmo(vport);
4354         /* Indicate we are done walking this fc_rscn_id_list */
4355         vport->fc_rscn_flush = 0;
4356 }
4357
4358 /**
4359  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
4360  * @vport: pointer to a host virtual N_Port data structure.
4361  * @did: remote destination port identifier.
4362  *
4363  * This routine checks whether there is any pending Registration State
4364  * Configuration Notification (RSCN) to a @did on @vport.
4365  *
4366  * Return code
4367  *   None zero - The @did matched with a pending rscn
4368  *   0 - not able to match @did with a pending rscn
4369  **/
4370 int
4371 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
4372 {
4373         D_ID ns_did;
4374         D_ID rscn_did;
4375         uint32_t *lp;
4376         uint32_t payload_len, i;
4377         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4378
4379         ns_did.un.word = did;
4380
4381         /* Never match fabric nodes for RSCNs */
4382         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
4383                 return 0;
4384
4385         /* If we are doing a FULL RSCN rediscovery, match everything */
4386         if (vport->fc_flag & FC_RSCN_DISCOVERY)
4387                 return did;
4388
4389         spin_lock_irq(shost->host_lock);
4390         if (vport->fc_rscn_flush) {
4391                 /* Another thread is walking fc_rscn_id_list on this vport */
4392                 spin_unlock_irq(shost->host_lock);
4393                 return 0;
4394         }
4395         /* Indicate we are walking fc_rscn_id_list on this vport */
4396         vport->fc_rscn_flush = 1;
4397         spin_unlock_irq(shost->host_lock);
4398         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4399                 lp = vport->fc_rscn_id_list[i]->virt;
4400                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4401                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
4402                 while (payload_len) {
4403                         rscn_did.un.word = be32_to_cpu(*lp++);
4404                         payload_len -= sizeof(uint32_t);
4405                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
4406                         case RSCN_ADDRESS_FORMAT_PORT:
4407                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4408                                     && (ns_did.un.b.area == rscn_did.un.b.area)
4409                                     && (ns_did.un.b.id == rscn_did.un.b.id))
4410                                         goto return_did_out;
4411                                 break;
4412                         case RSCN_ADDRESS_FORMAT_AREA:
4413                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4414                                     && (ns_did.un.b.area == rscn_did.un.b.area))
4415                                         goto return_did_out;
4416                                 break;
4417                         case RSCN_ADDRESS_FORMAT_DOMAIN:
4418                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
4419                                         goto return_did_out;
4420                                 break;
4421                         case RSCN_ADDRESS_FORMAT_FABRIC:
4422                                 goto return_did_out;
4423                         }
4424                 }
4425         }
4426         /* Indicate we are done with walking fc_rscn_id_list on this vport */
4427         vport->fc_rscn_flush = 0;
4428         return 0;
4429 return_did_out:
4430         /* Indicate we are done with walking fc_rscn_id_list on this vport */
4431         vport->fc_rscn_flush = 0;
4432         return did;
4433 }
4434
4435 /**
4436  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
4437  * @vport: pointer to a host virtual N_Port data structure.
4438  *
4439  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
4440  * state machine for a @vport's nodes that are with pending RSCN (Registration
4441  * State Change Notification).
4442  *
4443  * Return code
4444  *   0 - Successful (currently alway return 0)
4445  **/
4446 static int
4447 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
4448 {
4449         struct lpfc_nodelist *ndlp = NULL;
4450
4451         /* Move all affected nodes by pending RSCNs to NPR state. */
4452         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4453                 if (!NLP_CHK_NODE_ACT(ndlp) ||
4454                     (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
4455                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
4456                         continue;
4457                 lpfc_disc_state_machine(vport, ndlp, NULL,
4458                                         NLP_EVT_DEVICE_RECOVERY);
4459                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
4460         }
4461         return 0;
4462 }
4463
4464 /**
4465  * lpfc_send_rscn_event - Send an RSCN event to management application
4466  * @vport: pointer to a host virtual N_Port data structure.
4467  * @cmdiocb: pointer to lpfc command iocb data structure.
4468  *
4469  * lpfc_send_rscn_event sends an RSCN netlink event to management
4470  * applications.
4471  */
4472 static void
4473 lpfc_send_rscn_event(struct lpfc_vport *vport,
4474                 struct lpfc_iocbq *cmdiocb)
4475 {
4476         struct lpfc_dmabuf *pcmd;
4477         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4478         uint32_t *payload_ptr;
4479         uint32_t payload_len;
4480         struct lpfc_rscn_event_header *rscn_event_data;
4481
4482         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4483         payload_ptr = (uint32_t *) pcmd->virt;
4484         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4485
4486         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4487                 payload_len, GFP_KERNEL);
4488         if (!rscn_event_data) {
4489                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4490                         "0147 Failed to allocate memory for RSCN event\n");
4491                 return;
4492         }
4493         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4494         rscn_event_data->payload_length = payload_len;
4495         memcpy(rscn_event_data->rscn_payload, payload_ptr,
4496                 payload_len);
4497
4498         fc_host_post_vendor_event(shost,
4499                 fc_get_event_number(),
4500                 sizeof(struct lpfc_els_event_header) + payload_len,
4501                 (char *)rscn_event_data,
4502                 LPFC_NL_VENDOR_ID);
4503
4504         kfree(rscn_event_data);
4505 }
4506
4507 /**
4508  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
4509  * @vport: pointer to a host virtual N_Port data structure.
4510  * @cmdiocb: pointer to lpfc command iocb data structure.
4511  * @ndlp: pointer to a node-list data structure.
4512  *
4513  * This routine processes an unsolicited RSCN (Registration State Change
4514  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4515  * to invoke fc_host_post_event() routine to the FC transport layer. If the
4516  * discover state machine is about to begin discovery, it just accepts the
4517  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4518  * contains N_Port IDs for other vports on this HBA, it just accepts the
4519  * RSCN and ignore processing it. If the state machine is in the recovery
4520  * state, the fc_rscn_id_list of this @vport is walked and the
4521  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4522  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4523  * routine is invoked to handle the RSCN event.
4524  *
4525  * Return code
4526  *   0 - Just sent the acc response
4527  *   1 - Sent the acc response and waited for name server completion
4528  **/
4529 static int
4530 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4531                   struct lpfc_nodelist *ndlp)
4532 {
4533         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4534         struct lpfc_hba  *phba = vport->phba;
4535         struct lpfc_dmabuf *pcmd;
4536         uint32_t *lp, *datap;
4537         IOCB_t *icmd;
4538         uint32_t payload_len, length, nportid, *cmd;
4539         int rscn_cnt;
4540         int rscn_id = 0, hba_id = 0;
4541         int i;
4542
4543         icmd = &cmdiocb->iocb;
4544         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4545         lp = (uint32_t *) pcmd->virt;
4546
4547         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4548         payload_len -= sizeof(uint32_t);        /* take off word 0 */
4549         /* RSCN received */
4550         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4551                          "0214 RSCN received Data: x%x x%x x%x x%x\n",
4552                          vport->fc_flag, payload_len, *lp,
4553                          vport->fc_rscn_id_cnt);
4554
4555         /* Send an RSCN event to the management application */
4556         lpfc_send_rscn_event(vport, cmdiocb);
4557
4558         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
4559                 fc_host_post_event(shost, fc_get_event_number(),
4560                         FCH_EVT_RSCN, lp[i]);
4561
4562         /* If we are about to begin discovery, just ACC the RSCN.
4563          * Discovery processing will satisfy it.
4564          */
4565         if (vport->port_state <= LPFC_NS_QRY) {
4566                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4567                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4568                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4569
4570                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4571                 return 0;
4572         }
4573
4574         /* If this RSCN just contains NPortIDs for other vports on this HBA,
4575          * just ACC and ignore it.
4576          */
4577         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4578                 !(vport->cfg_peer_port_login)) {
4579                 i = payload_len;
4580                 datap = lp;
4581                 while (i > 0) {
4582                         nportid = *datap++;
4583                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4584                         i -= sizeof(uint32_t);
4585                         rscn_id++;
4586                         if (lpfc_find_vport_by_did(phba, nportid))
4587                                 hba_id++;
4588                 }
4589                 if (rscn_id == hba_id) {
4590                         /* ALL NPortIDs in RSCN are on HBA */
4591                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4592                                          "0219 Ignore RSCN "
4593                                          "Data: x%x x%x x%x x%x\n",
4594                                          vport->fc_flag, payload_len,
4595                                          *lp, vport->fc_rscn_id_cnt);
4596                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4597                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
4598                                 ndlp->nlp_DID, vport->port_state,
4599                                 ndlp->nlp_flag);
4600
4601                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
4602                                 ndlp, NULL);
4603                         return 0;
4604                 }
4605         }
4606
4607         spin_lock_irq(shost->host_lock);
4608         if (vport->fc_rscn_flush) {
4609                 /* Another thread is walking fc_rscn_id_list on this vport */
4610                 vport->fc_flag |= FC_RSCN_DISCOVERY;
4611                 spin_unlock_irq(shost->host_lock);
4612                 /* Send back ACC */
4613                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4614                 return 0;
4615         }
4616         /* Indicate we are walking fc_rscn_id_list on this vport */
4617         vport->fc_rscn_flush = 1;
4618         spin_unlock_irq(shost->host_lock);
4619         /* Get the array count after successfully have the token */
4620         rscn_cnt = vport->fc_rscn_id_cnt;
4621         /* If we are already processing an RSCN, save the received
4622          * RSCN payload buffer, cmdiocb->context2 to process later.
4623          */
4624         if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4625                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4626                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4627                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4628
4629                 spin_lock_irq(shost->host_lock);
4630                 vport->fc_flag |= FC_RSCN_DEFERRED;
4631                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4632                     !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4633                         vport->fc_flag |= FC_RSCN_MODE;
4634                         spin_unlock_irq(shost->host_lock);
4635                         if (rscn_cnt) {
4636                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4637                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4638                         }
4639                         if ((rscn_cnt) &&
4640                             (payload_len + length <= LPFC_BPL_SIZE)) {
4641                                 *cmd &= ELS_CMD_MASK;
4642                                 *cmd |= cpu_to_be32(payload_len + length);
4643                                 memcpy(((uint8_t *)cmd) + length, lp,
4644                                        payload_len);
4645                         } else {
4646                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4647                                 vport->fc_rscn_id_cnt++;
4648                                 /* If we zero, cmdiocb->context2, the calling
4649                                  * routine will not try to free it.
4650                                  */
4651                                 cmdiocb->context2 = NULL;
4652                         }
4653                         /* Deferred RSCN */
4654                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4655                                          "0235 Deferred RSCN "
4656                                          "Data: x%x x%x x%x\n",
4657                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4658                                          vport->port_state);
4659                 } else {
4660                         vport->fc_flag |= FC_RSCN_DISCOVERY;
4661                         spin_unlock_irq(shost->host_lock);
4662                         /* ReDiscovery RSCN */
4663                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4664                                          "0234 ReDiscovery RSCN "
4665                                          "Data: x%x x%x x%x\n",
4666                                          vport->fc_rscn_id_cnt, vport->fc_flag,
4667                                          vport->port_state);
4668                 }
4669                 /* Indicate we are done walking fc_rscn_id_list on this vport */
4670                 vport->fc_rscn_flush = 0;
4671                 /* Send back ACC */
4672                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4673                 /* send RECOVERY event for ALL nodes that match RSCN payload */
4674                 lpfc_rscn_recovery_check(vport);
4675                 spin_lock_irq(shost->host_lock);
4676                 vport->fc_flag &= ~FC_RSCN_DEFERRED;
4677                 spin_unlock_irq(shost->host_lock);
4678                 return 0;
4679         }
4680         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4681                 "RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4682                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4683
4684         spin_lock_irq(shost->host_lock);
4685         vport->fc_flag |= FC_RSCN_MODE;
4686         spin_unlock_irq(shost->host_lock);
4687         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4688         /* Indicate we are done walking fc_rscn_id_list on this vport */
4689         vport->fc_rscn_flush = 0;
4690         /*
4691          * If we zero, cmdiocb->context2, the calling routine will
4692          * not try to free it.
4693          */
4694         cmdiocb->context2 = NULL;
4695         lpfc_set_disctmo(vport);
4696         /* Send back ACC */
4697         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4698         /* send RECOVERY event for ALL nodes that match RSCN payload */
4699         lpfc_rscn_recovery_check(vport);
4700         return lpfc_els_handle_rscn(vport);
4701 }
4702
4703 /**
4704  * lpfc_els_handle_rscn - Handle rscn for a vport
4705  * @vport: pointer to a host virtual N_Port data structure.
4706  *
4707  * This routine handles the Registration State Configuration Notification
4708  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4709  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4710  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4711  * NameServer shall be issued. If CT command to the NameServer fails to be
4712  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4713  * RSCN activities with the @vport.
4714  *
4715  * Return code
4716  *   0 - Cleaned up rscn on the @vport
4717  *   1 - Wait for plogi to name server before proceed
4718  **/
4719 int
4720 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4721 {
4722         struct lpfc_nodelist *ndlp;
4723         struct lpfc_hba *phba = vport->phba;
4724
4725         /* Ignore RSCN if the port is being torn down. */
4726         if (vport->load_flag & FC_UNLOADING) {
4727                 lpfc_els_flush_rscn(vport);
4728                 return 0;
4729         }
4730
4731         /* Start timer for RSCN processing */
4732         lpfc_set_disctmo(vport);
4733
4734         /* RSCN processed */
4735         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4736                          "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4737                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4738                          vport->port_state);
4739
4740         /* To process RSCN, first compare RSCN data with NameServer */
4741         vport->fc_ns_retry = 0;
4742         vport->num_disc_nodes = 0;
4743
4744         ndlp = lpfc_findnode_did(vport, NameServer_DID);
4745         if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4746             && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4747                 /* Good ndlp, issue CT Request to NameServer */
4748                 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4749                         /* Wait for NameServer query cmpl before we can
4750                            continue */
4751                         return 1;
4752         } else {
4753                 /* If login to NameServer does not exist, issue one */
4754                 /* Good status, issue PLOGI to NameServer */
4755                 ndlp = lpfc_findnode_did(vport, NameServer_DID);
4756                 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4757                         /* Wait for NameServer login cmpl before we can
4758                            continue */
4759                         return 1;
4760
4761                 if (ndlp) {
4762                         ndlp = lpfc_enable_node(vport, ndlp,
4763                                                 NLP_STE_PLOGI_ISSUE);
4764                         if (!ndlp) {
4765                                 lpfc_els_flush_rscn(vport);
4766                                 return 0;
4767                         }
4768                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4769                 } else {
4770                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4771                         if (!ndlp) {
4772                                 lpfc_els_flush_rscn(vport);
4773                                 return 0;
4774                         }
4775                         lpfc_nlp_init(vport, ndlp, NameServer_DID);
4776                         ndlp->nlp_prev_state = ndlp->nlp_state;
4777                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4778                 }
4779                 ndlp->nlp_type |= NLP_FABRIC;
4780                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4781                 /* Wait for NameServer login cmpl before we can
4782                  * continue
4783                  */
4784                 return 1;
4785         }
4786
4787         lpfc_els_flush_rscn(vport);
4788         return 0;
4789 }
4790
4791 /**
4792  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
4793  * @vport: pointer to a host virtual N_Port data structure.
4794  * @cmdiocb: pointer to lpfc command iocb data structure.
4795  * @ndlp: pointer to a node-list data structure.
4796  *
4797  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4798  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4799  * point topology. As an unsolicited FLOGI should not be received in a loop
4800  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4801  * lpfc_check_sparm() routine is invoked to check the parameters in the
4802  * unsolicited FLOGI. If parameters validation failed, the routine
4803  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4804  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4805  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4806  * will initiate PLOGI. The higher lexicographical value party shall has
4807  * higher priority (as the winning port) and will initiate PLOGI and
4808  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4809  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4810  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4811  *
4812  * Return code
4813  *   0 - Successfully processed the unsolicited flogi
4814  *   1 - Failed to process the unsolicited flogi
4815  **/
4816 static int
4817 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4818                    struct lpfc_nodelist *ndlp)
4819 {
4820         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4821         struct lpfc_hba  *phba = vport->phba;
4822         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4823         uint32_t *lp = (uint32_t *) pcmd->virt;
4824         IOCB_t *icmd = &cmdiocb->iocb;
4825         struct serv_parm *sp;
4826         LPFC_MBOXQ_t *mbox;
4827         struct ls_rjt stat;
4828         uint32_t cmd, did;
4829         int rc;
4830
4831         cmd = *lp++;
4832         sp = (struct serv_parm *) lp;
4833
4834         /* FLOGI received */
4835
4836         lpfc_set_disctmo(vport);
4837
4838         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4839                 /* We should never receive a FLOGI in loop mode, ignore it */
4840                 did = icmd->un.elsreq64.remoteID;
4841
4842                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4843                    Loop Mode */
4844                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4845                                  "0113 An FLOGI ELS command x%x was "
4846                                  "received from DID x%x in Loop Mode\n",
4847                                  cmd, did);
4848                 return 1;
4849         }
4850
4851         did = Fabric_DID;
4852
4853         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
4854                 /* For a FLOGI we accept, then if our portname is greater
4855                  * then the remote portname we initiate Nport login.
4856                  */
4857
4858                 rc = memcmp(&vport->fc_portname, &sp->portName,
4859                             sizeof(struct lpfc_name));
4860
4861                 if (!rc) {
4862                         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4863                         if (!mbox)
4864                                 return 1;
4865
4866                         lpfc_linkdown(phba);
4867                         lpfc_init_link(phba, mbox,
4868                                        phba->cfg_topology,
4869                                        phba->cfg_link_speed);
4870                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
4871                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4872                         mbox->vport = vport;
4873                         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4874                         lpfc_set_loopback_flag(phba);
4875                         if (rc == MBX_NOT_FINISHED) {
4876                                 mempool_free(mbox, phba->mbox_mem_pool);
4877                         }
4878                         return 1;
4879                 } else if (rc > 0) {    /* greater than */
4880                         spin_lock_irq(shost->host_lock);
4881                         vport->fc_flag |= FC_PT2PT_PLOGI;
4882                         spin_unlock_irq(shost->host_lock);
4883                 }
4884                 spin_lock_irq(shost->host_lock);
4885                 vport->fc_flag |= FC_PT2PT;
4886                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4887                 spin_unlock_irq(shost->host_lock);
4888         } else {
4889                 /* Reject this request because invalid parameters */
4890                 stat.un.b.lsRjtRsvd0 = 0;
4891                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4892                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4893                 stat.un.b.vendorUnique = 0;
4894                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4895                         NULL);
4896                 return 1;
4897         }
4898
4899         /* Send back ACC */
4900         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4901
4902         return 0;
4903 }
4904
4905 /**
4906  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
4907  * @vport: pointer to a host virtual N_Port data structure.
4908  * @cmdiocb: pointer to lpfc command iocb data structure.
4909  * @ndlp: pointer to a node-list data structure.
4910  *
4911  * This routine processes Request Node Identification Data (RNID) IOCB
4912  * received as an ELS unsolicited event. Only when the RNID specified format
4913  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4914  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4915  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4916  * rejected by invoking the lpfc_els_rsp_reject() routine.
4917  *
4918  * Return code
4919  *   0 - Successfully processed rnid iocb (currently always return 0)
4920  **/
4921 static int
4922 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4923                   struct lpfc_nodelist *ndlp)
4924 {
4925         struct lpfc_dmabuf *pcmd;
4926         uint32_t *lp;
4927         IOCB_t *icmd;
4928         RNID *rn;
4929         struct ls_rjt stat;
4930         uint32_t cmd, did;
4931
4932         icmd = &cmdiocb->iocb;
4933         did = icmd->un.elsreq64.remoteID;
4934         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4935         lp = (uint32_t *) pcmd->virt;
4936
4937         cmd = *lp++;
4938         rn = (RNID *) lp;
4939
4940         /* RNID received */
4941
4942         switch (rn->Format) {
4943         case 0:
4944         case RNID_TOPOLOGY_DISC:
4945                 /* Send back ACC */
4946                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4947                 break;
4948         default:
4949                 /* Reject this request because format not supported */
4950                 stat.un.b.lsRjtRsvd0 = 0;
4951                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4952                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4953                 stat.un.b.vendorUnique = 0;
4954                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4955                         NULL);
4956         }
4957         return 0;
4958 }
4959
4960 /**
4961  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
4962  * @vport: pointer to a host virtual N_Port data structure.
4963  * @cmdiocb: pointer to lpfc command iocb data structure.
4964  * @ndlp: pointer to a node-list data structure.
4965  *
4966  * Return code
4967  *   0 - Successfully processed echo iocb (currently always return 0)
4968  **/
4969 static int
4970 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4971                   struct lpfc_nodelist *ndlp)
4972 {
4973         uint8_t *pcmd;
4974
4975         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4976
4977         /* skip over first word of echo command to find echo data */
4978         pcmd += sizeof(uint32_t);
4979
4980         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
4981         return 0;
4982 }
4983
4984 /**
4985  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
4986  * @vport: pointer to a host virtual N_Port data structure.
4987  * @cmdiocb: pointer to lpfc command iocb data structure.
4988  * @ndlp: pointer to a node-list data structure.
4989  *
4990  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4991  * received as an ELS unsolicited event. Currently, this function just invokes
4992  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4993  *
4994  * Return code
4995  *   0 - Successfully processed lirr iocb (currently always return 0)
4996  **/
4997 static int
4998 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4999                   struct lpfc_nodelist *ndlp)
5000 {
5001         struct ls_rjt stat;
5002
5003         /* For now, unconditionally reject this command */
5004         stat.un.b.lsRjtRsvd0 = 0;
5005         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5006         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5007         stat.un.b.vendorUnique = 0;
5008         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5009         return 0;
5010 }
5011
5012 /**
5013  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
5014  * @vport: pointer to a host virtual N_Port data structure.
5015  * @cmdiocb: pointer to lpfc command iocb data structure.
5016  * @ndlp: pointer to a node-list data structure.
5017  *
5018  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
5019  * received as an ELS unsolicited event. A request to RRQ shall only
5020  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
5021  * Nx_Port N_Port_ID of the target Exchange is the same as the
5022  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
5023  * not accepted, an LS_RJT with reason code "Unable to perform
5024  * command request" and reason code explanation "Invalid Originator
5025  * S_ID" shall be returned. For now, we just unconditionally accept
5026  * RRQ from the target.
5027  **/
5028 static void
5029 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5030                  struct lpfc_nodelist *ndlp)
5031 {
5032         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5033         if (vport->phba->sli_rev == LPFC_SLI_REV4)
5034                 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
5035 }
5036
5037 /**
5038  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
5039  * @phba: pointer to lpfc hba data structure.
5040  * @pmb: pointer to the driver internal queue element for mailbox command.
5041  *
5042  * This routine is the completion callback function for the MBX_READ_LNK_STAT
5043  * mailbox command. This callback function is to actually send the Accept
5044  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
5045  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
5046  * mailbox command, constructs the RPS response with the link statistics
5047  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
5048  * response to the RPS.
5049  *
5050  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5051  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5052  * will be stored into the context1 field of the IOCB for the completion
5053  * callback function to the RPS Accept Response ELS IOCB command.
5054  *
5055  **/
5056 static void
5057 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5058 {
5059         MAILBOX_t *mb;
5060         IOCB_t *icmd;
5061         struct RLS_RSP *rls_rsp;
5062         uint8_t *pcmd;
5063         struct lpfc_iocbq *elsiocb;
5064         struct lpfc_nodelist *ndlp;
5065         uint16_t oxid;
5066         uint16_t rxid;
5067         uint32_t cmdsize;
5068
5069         mb = &pmb->u.mb;
5070
5071         ndlp = (struct lpfc_nodelist *) pmb->context2;
5072         rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
5073         oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
5074         pmb->context1 = NULL;
5075         pmb->context2 = NULL;
5076
5077         if (mb->mbxStatus) {
5078                 mempool_free(pmb, phba->mbox_mem_pool);
5079                 return;
5080         }
5081
5082         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
5083         mempool_free(pmb, phba->mbox_mem_pool);
5084         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5085                                      lpfc_max_els_tries, ndlp,
5086                                      ndlp->nlp_DID, ELS_CMD_ACC);
5087
5088         /* Decrement the ndlp reference count from previous mbox command */
5089         lpfc_nlp_put(ndlp);
5090
5091         if (!elsiocb)
5092                 return;
5093
5094         icmd = &elsiocb->iocb;
5095         icmd->ulpContext = rxid;
5096         icmd->unsli3.rcvsli3.ox_id = oxid;
5097
5098         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5099         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5100         pcmd += sizeof(uint32_t); /* Skip past command */
5101         rls_rsp = (struct RLS_RSP *)pcmd;
5102
5103         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
5104         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
5105         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
5106         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
5107         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
5108         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
5109
5110         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5111         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5112                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
5113                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5114                          elsiocb->iotag, elsiocb->iocb.ulpContext,
5115                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5116                          ndlp->nlp_rpi);
5117         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5118         phba->fc_stat.elsXmitACC++;
5119         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5120                 lpfc_els_free_iocb(phba, elsiocb);
5121 }
5122
5123 /**
5124  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
5125  * @phba: pointer to lpfc hba data structure.
5126  * @pmb: pointer to the driver internal queue element for mailbox command.
5127  *
5128  * This routine is the completion callback function for the MBX_READ_LNK_STAT
5129  * mailbox command. This callback function is to actually send the Accept
5130  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
5131  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
5132  * mailbox command, constructs the RPS response with the link statistics
5133  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
5134  * response to the RPS.
5135  *
5136  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5137  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5138  * will be stored into the context1 field of the IOCB for the completion
5139  * callback function to the RPS Accept Response ELS IOCB command.
5140  *
5141  **/
5142 static void
5143 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5144 {
5145         MAILBOX_t *mb;
5146         IOCB_t *icmd;
5147         RPS_RSP *rps_rsp;
5148         uint8_t *pcmd;
5149         struct lpfc_iocbq *elsiocb;
5150         struct lpfc_nodelist *ndlp;
5151         uint16_t status;
5152         uint16_t oxid;
5153         uint16_t rxid;
5154         uint32_t cmdsize;
5155
5156         mb = &pmb->u.mb;
5157
5158         ndlp = (struct lpfc_nodelist *) pmb->context2;
5159         rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
5160         oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
5161         pmb->context1 = NULL;
5162         pmb->context2 = NULL;
5163
5164         if (mb->mbxStatus) {
5165                 mempool_free(pmb, phba->mbox_mem_pool);
5166                 return;
5167         }
5168
5169         cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
5170         mempool_free(pmb, phba->mbox_mem_pool);
5171         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5172                                      lpfc_max_els_tries, ndlp,
5173                                      ndlp->nlp_DID, ELS_CMD_ACC);
5174
5175         /* Decrement the ndlp reference count from previous mbox command */
5176         lpfc_nlp_put(ndlp);
5177
5178         if (!elsiocb)
5179                 return;
5180
5181         icmd = &elsiocb->iocb;
5182         icmd->ulpContext = rxid;
5183         icmd->unsli3.rcvsli3.ox_id = oxid;
5184
5185         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5186         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5187         pcmd += sizeof(uint32_t); /* Skip past command */
5188         rps_rsp = (RPS_RSP *)pcmd;
5189
5190         if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5191                 status = 0x10;
5192         else
5193                 status = 0x8;
5194         if (phba->pport->fc_flag & FC_FABRIC)
5195                 status |= 0x4;
5196
5197         rps_rsp->rsvd1 = 0;
5198         rps_rsp->portStatus = cpu_to_be16(status);
5199         rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
5200         rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
5201         rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
5202         rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
5203         rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
5204         rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
5205         /* Xmit ELS RPS ACC response tag <ulpIoTag> */
5206         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5207                          "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
5208                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5209                          elsiocb->iotag, elsiocb->iocb.ulpContext,
5210                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5211                          ndlp->nlp_rpi);
5212         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5213         phba->fc_stat.elsXmitACC++;
5214         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5215                 lpfc_els_free_iocb(phba, elsiocb);
5216         return;
5217 }
5218
5219 /**
5220  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
5221  * @vport: pointer to a host virtual N_Port data structure.
5222  * @cmdiocb: pointer to lpfc command iocb data structure.
5223  * @ndlp: pointer to a node-list data structure.
5224  *
5225  * This routine processes Read Port Status (RPL) IOCB received as an
5226  * ELS unsolicited event. It first checks the remote port state. If the
5227  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5228  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5229  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5230  * for reading the HBA link statistics. It is for the callback function,
5231  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
5232  * to actually sending out RPL Accept (ACC) response.
5233  *
5234  * Return codes
5235  *   0 - Successfully processed rls iocb (currently always return 0)
5236  **/
5237 static int
5238 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5239                  struct lpfc_nodelist *ndlp)
5240 {
5241         struct lpfc_hba *phba = vport->phba;
5242         LPFC_MBOXQ_t *mbox;
5243         struct lpfc_dmabuf *pcmd;
5244         struct ls_rjt stat;
5245
5246         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5247             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5248                 /* reject the unsolicited RPS request and done with it */
5249                 goto reject_out;
5250
5251         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5252
5253         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5254         if (mbox) {
5255                 lpfc_read_lnk_stat(phba, mbox);
5256                 mbox->context1 = (void *)((unsigned long)
5257                         ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
5258                         cmdiocb->iocb.ulpContext)); /* rx_id */
5259                 mbox->context2 = lpfc_nlp_get(ndlp);
5260                 mbox->vport = vport;
5261                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
5262                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5263                         != MBX_NOT_FINISHED)
5264                         /* Mbox completion will send ELS Response */
5265                         return 0;
5266                 /* Decrement reference count used for the failed mbox
5267                  * command.
5268                  */
5269                 lpfc_nlp_put(ndlp);
5270                 mempool_free(mbox, phba->mbox_mem_pool);
5271         }
5272 reject_out:
5273         /* issue rejection response */
5274         stat.un.b.lsRjtRsvd0 = 0;
5275         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5276         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5277         stat.un.b.vendorUnique = 0;
5278         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5279         return 0;
5280 }
5281
5282 /**
5283  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
5284  * @vport: pointer to a host virtual N_Port data structure.
5285  * @cmdiocb: pointer to lpfc command iocb data structure.
5286  * @ndlp: pointer to a node-list data structure.
5287  *
5288  * This routine processes Read Timout Value (RTV) IOCB received as an
5289  * ELS unsolicited event. It first checks the remote port state. If the
5290  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5291  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5292  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
5293  * Value (RTV) unsolicited IOCB event.
5294  *
5295  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5296  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5297  * will be stored into the context1 field of the IOCB for the completion
5298  * callback function to the RPS Accept Response ELS IOCB command.
5299  *
5300  * Return codes
5301  *   0 - Successfully processed rtv iocb (currently always return 0)
5302  **/
5303 static int
5304 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5305                  struct lpfc_nodelist *ndlp)
5306 {
5307         struct lpfc_hba *phba = vport->phba;
5308         struct ls_rjt stat;
5309         struct RTV_RSP *rtv_rsp;
5310         uint8_t *pcmd;
5311         struct lpfc_iocbq *elsiocb;
5312         uint32_t cmdsize;
5313
5314
5315         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5316             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5317                 /* reject the unsolicited RPS request and done with it */
5318                 goto reject_out;
5319
5320         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
5321         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5322                                      lpfc_max_els_tries, ndlp,
5323                                      ndlp->nlp_DID, ELS_CMD_ACC);
5324
5325         if (!elsiocb)
5326                 return 1;
5327
5328         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5329                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5330         pcmd += sizeof(uint32_t); /* Skip past command */
5331
5332         /* use the command's xri in the response */
5333         elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
5334         elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
5335
5336         rtv_rsp = (struct RTV_RSP *)pcmd;
5337
5338         /* populate RTV payload */
5339         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
5340         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
5341         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
5342         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
5343         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
5344
5345         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5346         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5347                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
5348                          "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
5349                          "Data: x%x x%x x%x\n",
5350                          elsiocb->iotag, elsiocb->iocb.ulpContext,
5351                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5352                          ndlp->nlp_rpi,
5353                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
5354         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5355         phba->fc_stat.elsXmitACC++;
5356         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5357                 lpfc_els_free_iocb(phba, elsiocb);
5358         return 0;
5359
5360 reject_out:
5361         /* issue rejection response */
5362         stat.un.b.lsRjtRsvd0 = 0;
5363         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5364         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5365         stat.un.b.vendorUnique = 0;
5366         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5367         return 0;
5368 }
5369
5370 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
5371  * @vport: pointer to a host virtual N_Port data structure.
5372  * @cmdiocb: pointer to lpfc command iocb data structure.
5373  * @ndlp: pointer to a node-list data structure.
5374  *
5375  * This routine processes Read Port Status (RPS) IOCB received as an
5376  * ELS unsolicited event. It first checks the remote port state. If the
5377  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5378  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
5379  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5380  * for reading the HBA link statistics. It is for the callback function,
5381  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
5382  * to actually sending out RPS Accept (ACC) response.
5383  *
5384  * Return codes
5385  *   0 - Successfully processed rps iocb (currently always return 0)
5386  **/
5387 static int
5388 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5389                  struct lpfc_nodelist *ndlp)
5390 {
5391         struct lpfc_hba *phba = vport->phba;
5392         uint32_t *lp;
5393         uint8_t flag;
5394         LPFC_MBOXQ_t *mbox;
5395         struct lpfc_dmabuf *pcmd;
5396         RPS *rps;
5397         struct ls_rjt stat;
5398
5399         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5400             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5401                 /* reject the unsolicited RPS request and done with it */
5402                 goto reject_out;
5403
5404         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5405         lp = (uint32_t *) pcmd->virt;
5406         flag = (be32_to_cpu(*lp++) & 0xf);
5407         rps = (RPS *) lp;
5408
5409         if ((flag == 0) ||
5410             ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
5411             ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
5412                                     sizeof(struct lpfc_name)) == 0))) {
5413
5414                 printk("Fix me....\n");
5415                 dump_stack();
5416                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5417                 if (mbox) {
5418                         lpfc_read_lnk_stat(phba, mbox);
5419                         mbox->context1 = (void *)((unsigned long)
5420                                 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
5421                                 cmdiocb->iocb.ulpContext)); /* rx_id */
5422                         mbox->context2 = lpfc_nlp_get(ndlp);
5423                         mbox->vport = vport;
5424                         mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
5425                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5426                                 != MBX_NOT_FINISHED)
5427                                 /* Mbox completion will send ELS Response */
5428                                 return 0;
5429                         /* Decrement reference count used for the failed mbox
5430                          * command.
5431                          */
5432                         lpfc_nlp_put(ndlp);
5433                         mempool_free(mbox, phba->mbox_mem_pool);
5434                 }
5435         }
5436
5437 reject_out:
5438         /* issue rejection response */
5439         stat.un.b.lsRjtRsvd0 = 0;
5440         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5441         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5442         stat.un.b.vendorUnique = 0;
5443         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5444         return 0;
5445 }
5446
5447 /* lpfc_issue_els_rrq - Process an unsolicited rps iocb
5448  * @vport: pointer to a host virtual N_Port data structure.
5449  * @ndlp: pointer to a node-list data structure.
5450  * @did: DID of the target.
5451  * @rrq: Pointer to the rrq struct.
5452  *
5453  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
5454  * Successful the the completion handler will clear the RRQ.
5455  *
5456  * Return codes
5457  *   0 - Successfully sent rrq els iocb.
5458  *   1 - Failed to send rrq els iocb.
5459  **/
5460 static int
5461 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5462                         uint32_t did, struct lpfc_node_rrq *rrq)
5463 {
5464         struct lpfc_hba  *phba = vport->phba;
5465         struct RRQ *els_rrq;
5466         IOCB_t *icmd;
5467         struct lpfc_iocbq *elsiocb;
5468         uint8_t *pcmd;
5469         uint16_t cmdsize;
5470         int ret;
5471
5472
5473         if (ndlp != rrq->ndlp)
5474                 ndlp = rrq->ndlp;
5475         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5476                 return 1;
5477
5478         /* If ndlp is not NULL, we will bump the reference count on it */
5479         cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
5480         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
5481                                      ELS_CMD_RRQ);
5482         if (!elsiocb)
5483                 return 1;
5484
5485         icmd = &elsiocb->iocb;
5486         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5487
5488         /* For RRQ request, remainder of payload is Exchange IDs */
5489         *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
5490         pcmd += sizeof(uint32_t);
5491         els_rrq = (struct RRQ *) pcmd;
5492
5493         bf_set(rrq_oxid, els_rrq, rrq->xritag);
5494         bf_set(rrq_rxid, els_rrq, rrq->rxid);
5495         bf_set(rrq_did, els_rrq, vport->fc_myDID);
5496         els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
5497         els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
5498
5499
5500         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5501                 "Issue RRQ:     did:x%x",
5502                 did, rrq->xritag, rrq->rxid);
5503         elsiocb->context_un.rrq = rrq;
5504         elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
5505         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5506
5507         if (ret == IOCB_ERROR) {
5508                 lpfc_els_free_iocb(phba, elsiocb);
5509                 return 1;
5510         }
5511         return 0;
5512 }
5513
5514 /**
5515  * lpfc_send_rrq - Sends ELS RRQ if needed.
5516  * @phba: pointer to lpfc hba data structure.
5517  * @rrq: pointer to the active rrq.
5518  *
5519  * This routine will call the lpfc_issue_els_rrq if the rrq is
5520  * still active for the xri. If this function returns a failure then
5521  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
5522  *
5523  * Returns 0 Success.
5524  *         1 Failure.
5525  **/
5526 int
5527 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
5528 {
5529         struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
5530                                                         rrq->nlp_DID);
5531         if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
5532                 return lpfc_issue_els_rrq(rrq->vport, ndlp,
5533                                          rrq->nlp_DID, rrq);
5534         else
5535                 return 1;
5536 }
5537
5538 /**
5539  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
5540  * @vport: pointer to a host virtual N_Port data structure.
5541  * @cmdsize: size of the ELS command.
5542  * @oldiocb: pointer to the original lpfc command iocb data structure.
5543  * @ndlp: pointer to a node-list data structure.
5544  *
5545  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
5546  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
5547  *
5548  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5549  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5550  * will be stored into the context1 field of the IOCB for the completion
5551  * callback function to the RPL Accept Response ELS command.
5552  *
5553  * Return code
5554  *   0 - Successfully issued ACC RPL ELS command
5555  *   1 - Failed to issue ACC RPL ELS command
5556  **/
5557 static int
5558 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
5559                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5560 {
5561         struct lpfc_hba *phba = vport->phba;
5562         IOCB_t *icmd, *oldcmd;
5563         RPL_RSP rpl_rsp;
5564         struct lpfc_iocbq *elsiocb;
5565         uint8_t *pcmd;
5566
5567         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5568                                      ndlp->nlp_DID, ELS_CMD_ACC);
5569
5570         if (!elsiocb)
5571                 return 1;
5572
5573         icmd = &elsiocb->iocb;
5574         oldcmd = &oldiocb->iocb;
5575         icmd->ulpContext = oldcmd->ulpContext;  /* Xri / rx_id */
5576         icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5577
5578         pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5579         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5580         pcmd += sizeof(uint16_t);
5581         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
5582         pcmd += sizeof(uint16_t);
5583
5584         /* Setup the RPL ACC payload */
5585         rpl_rsp.listLen = be32_to_cpu(1);
5586         rpl_rsp.index = 0;
5587         rpl_rsp.port_num_blk.portNum = 0;
5588         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
5589         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
5590             sizeof(struct lpfc_name));
5591         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
5592         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
5593         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5594                          "0120 Xmit ELS RPL ACC response tag x%x "
5595                          "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5596                          "rpi x%x\n",
5597                          elsiocb->iotag, elsiocb->iocb.ulpContext,
5598                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5599                          ndlp->nlp_rpi);
5600         elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5601         phba->fc_stat.elsXmitACC++;
5602         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
5603             IOCB_ERROR) {
5604                 lpfc_els_free_iocb(phba, elsiocb);
5605                 return 1;
5606         }
5607         return 0;
5608 }
5609
5610 /**
5611  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
5612  * @vport: pointer to a host virtual N_Port data structure.
5613  * @cmdiocb: pointer to lpfc command iocb data structure.
5614  * @ndlp: pointer to a node-list data structure.
5615  *
5616  * This routine processes Read Port List (RPL) IOCB received as an ELS
5617  * unsolicited event. It first checks the remote port state. If the remote
5618  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
5619  * invokes the lpfc_els_rsp_reject() routine to send reject response.
5620  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
5621  * to accept the RPL.
5622  *
5623  * Return code
5624  *   0 - Successfully processed rpl iocb (currently always return 0)
5625  **/
5626 static int
5627 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5628                  struct lpfc_nodelist *ndlp)
5629 {
5630         struct lpfc_dmabuf *pcmd;
5631         uint32_t *lp;
5632         uint32_t maxsize;
5633         uint16_t cmdsize;
5634         RPL *rpl;
5635         struct ls_rjt stat;
5636
5637         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5638             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
5639                 /* issue rejection response */
5640                 stat.un.b.lsRjtRsvd0 = 0;
5641                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5642                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5643                 stat.un.b.vendorUnique = 0;
5644                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5645                         NULL);
5646                 /* rejected the unsolicited RPL request and done with it */
5647                 return 0;
5648         }
5649
5650         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5651         lp = (uint32_t *) pcmd->virt;
5652         rpl = (RPL *) (lp + 1);
5653         maxsize = be32_to_cpu(rpl->maxsize);
5654
5655         /* We support only one port */
5656         if ((rpl->index == 0) &&
5657             ((maxsize == 0) ||
5658              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
5659                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
5660         } else {
5661                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
5662         }
5663         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
5664
5665         return 0;
5666 }
5667
5668 /**
5669  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
5670  * @vport: pointer to a virtual N_Port data structure.
5671  * @cmdiocb: pointer to lpfc command iocb data structure.
5672  * @ndlp: pointer to a node-list data structure.
5673  *
5674  * This routine processes Fibre Channel Address Resolution Protocol
5675  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
5676  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
5677  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
5678  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
5679  * remote PortName is compared against the FC PortName stored in the @vport
5680  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
5681  * compared against the FC NodeName stored in the @vport data structure.
5682  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
5683  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
5684  * invoked to send out FARP Response to the remote node. Before sending the
5685  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
5686  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
5687  * routine is invoked to log into the remote port first.
5688  *
5689  * Return code
5690  *   0 - Either the FARP Match Mode not supported or successfully processed
5691  **/
5692 static int
5693 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5694                   struct lpfc_nodelist *ndlp)
5695 {
5696         struct lpfc_dmabuf *pcmd;
5697         uint32_t *lp;
5698         IOCB_t *icmd;
5699         FARP *fp;
5700         uint32_t cmd, cnt, did;
5701
5702         icmd = &cmdiocb->iocb;
5703         did = icmd->un.elsreq64.remoteID;
5704         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5705         lp = (uint32_t *) pcmd->virt;
5706
5707         cmd = *lp++;
5708         fp = (FARP *) lp;
5709         /* FARP-REQ received from DID <did> */
5710         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5711                          "0601 FARP-REQ received from DID x%x\n", did);
5712         /* We will only support match on WWPN or WWNN */
5713         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
5714                 return 0;
5715         }
5716
5717         cnt = 0;
5718         /* If this FARP command is searching for my portname */
5719         if (fp->Mflags & FARP_MATCH_PORT) {
5720                 if (memcmp(&fp->RportName, &vport->fc_portname,
5721                            sizeof(struct lpfc_name)) == 0)
5722                         cnt = 1;
5723         }
5724
5725         /* If this FARP command is searching for my nodename */
5726         if (fp->Mflags & FARP_MATCH_NODE) {
5727                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
5728                            sizeof(struct lpfc_name)) == 0)
5729                         cnt = 1;
5730         }
5731
5732         if (cnt) {
5733                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
5734                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
5735                         /* Log back into the node before sending the FARP. */
5736                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
5737                                 ndlp->nlp_prev_state = ndlp->nlp_state;
5738                                 lpfc_nlp_set_state(vport, ndlp,
5739                                                    NLP_STE_PLOGI_ISSUE);
5740                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5741                         }
5742
5743                         /* Send a FARP response to that node */
5744                         if (fp->Rflags & FARP_REQUEST_FARPR)
5745                                 lpfc_issue_els_farpr(vport, did, 0);
5746                 }
5747         }
5748         return 0;
5749 }
5750
5751 /**
5752  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
5753  * @vport: pointer to a host virtual N_Port data structure.
5754  * @cmdiocb: pointer to lpfc command iocb data structure.
5755  * @ndlp: pointer to a node-list data structure.
5756  *
5757  * This routine processes Fibre Channel Address Resolution Protocol
5758  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
5759  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
5760  * the FARP response request.
5761  *
5762  * Return code
5763  *   0 - Successfully processed FARPR IOCB (currently always return 0)
5764  **/
5765 static int
5766 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5767                    struct lpfc_nodelist  *ndlp)
5768 {
5769         struct lpfc_dmabuf *pcmd;
5770         uint32_t *lp;
5771         IOCB_t *icmd;
5772         uint32_t cmd, did;
5773
5774         icmd = &cmdiocb->iocb;
5775         did = icmd->un.elsreq64.remoteID;
5776         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5777         lp = (uint32_t *) pcmd->virt;
5778
5779         cmd = *lp++;
5780         /* FARP-RSP received from DID <did> */
5781         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5782                          "0600 FARP-RSP received from DID x%x\n", did);
5783         /* ACCEPT the Farp resp request */
5784         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5785
5786         return 0;
5787 }
5788
5789 /**
5790  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
5791  * @vport: pointer to a host virtual N_Port data structure.
5792  * @cmdiocb: pointer to lpfc command iocb data structure.
5793  * @fan_ndlp: pointer to a node-list data structure.
5794  *
5795  * This routine processes a Fabric Address Notification (FAN) IOCB
5796  * command received as an ELS unsolicited event. The FAN ELS command will
5797  * only be processed on a physical port (i.e., the @vport represents the
5798  * physical port). The fabric NodeName and PortName from the FAN IOCB are
5799  * compared against those in the phba data structure. If any of those is
5800  * different, the lpfc_initial_flogi() routine is invoked to initialize
5801  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
5802  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
5803  * is invoked to register login to the fabric.
5804  *
5805  * Return code
5806  *   0 - Successfully processed fan iocb (currently always return 0).
5807  **/
5808 static int
5809 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5810                  struct lpfc_nodelist *fan_ndlp)
5811 {
5812         struct lpfc_hba *phba = vport->phba;
5813         uint32_t *lp;
5814         FAN *fp;
5815
5816         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
5817         lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
5818         fp = (FAN *) ++lp;
5819         /* FAN received; Fan does not have a reply sequence */
5820         if ((vport == phba->pport) &&
5821             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5822                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
5823                             sizeof(struct lpfc_name))) ||
5824                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
5825                             sizeof(struct lpfc_name)))) {
5826                         /* This port has switched fabrics. FLOGI is required */
5827                         lpfc_issue_init_vfi(vport);
5828                 } else {
5829                         /* FAN verified - skip FLOGI */
5830                         vport->fc_myDID = vport->fc_prevDID;
5831                         if (phba->sli_rev < LPFC_SLI_REV4)
5832                                 lpfc_issue_fabric_reglogin(vport);
5833                         else
5834                                 lpfc_issue_reg_vfi(vport);
5835                 }
5836         }
5837         return 0;
5838 }
5839
5840 /**
5841  * lpfc_els_timeout - Handler funciton to the els timer
5842  * @ptr: holder for the timer function associated data.
5843  *
5844  * This routine is invoked by the ELS timer after timeout. It posts the ELS
5845  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
5846  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
5847  * up the worker thread. It is for the worker thread to invoke the routine
5848  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
5849  **/
5850 void
5851 lpfc_els_timeout(unsigned long ptr)
5852 {
5853         struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5854         struct lpfc_hba   *phba = vport->phba;
5855         uint32_t tmo_posted;
5856         unsigned long iflag;
5857
5858         spin_lock_irqsave(&vport->work_port_lock, iflag);
5859         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
5860         if (!tmo_posted)
5861                 vport->work_port_events |= WORKER_ELS_TMO;
5862         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
5863
5864         if (!tmo_posted)
5865                 lpfc_worker_wake_up(phba);
5866         return;
5867 }
5868
5869
5870 /**
5871  * lpfc_els_timeout_handler - Process an els timeout event
5872  * @vport: pointer to a virtual N_Port data structure.
5873  *
5874  * This routine is the actual handler function that processes an ELS timeout
5875  * event. It walks the ELS ring to get and abort all the IOCBs (except the
5876  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
5877  * invoking the lpfc_sli_issue_abort_iotag() routine.
5878  **/
5879 void
5880 lpfc_els_timeout_handler(struct lpfc_vport *vport)
5881 {
5882         struct lpfc_hba  *phba = vport->phba;
5883         struct lpfc_sli_ring *pring;
5884         struct lpfc_iocbq *tmp_iocb, *piocb;
5885         IOCB_t *cmd = NULL;
5886         struct lpfc_dmabuf *pcmd;
5887         uint32_t els_command = 0;
5888         uint32_t timeout;
5889         uint32_t remote_ID = 0xffffffff;
5890         LIST_HEAD(txcmplq_completions);
5891         LIST_HEAD(abort_list);
5892
5893
5894         timeout = (uint32_t)(phba->fc_ratov << 1);
5895
5896         pring = &phba->sli.ring[LPFC_ELS_RING];
5897
5898         spin_lock_irq(&phba->hbalock);
5899         list_splice_init(&pring->txcmplq, &txcmplq_completions);
5900         spin_unlock_irq(&phba->hbalock);
5901
5902         list_for_each_entry_safe(piocb, tmp_iocb, &txcmplq_completions, list) {
5903                 cmd = &piocb->iocb;
5904
5905                 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
5906                     piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
5907                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5908                         continue;
5909
5910                 if (piocb->vport != vport)
5911                         continue;
5912
5913                 pcmd = (struct lpfc_dmabuf *) piocb->context2;
5914                 if (pcmd)
5915                         els_command = *(uint32_t *) (pcmd->virt);
5916
5917                 if (els_command == ELS_CMD_FARP ||
5918                     els_command == ELS_CMD_FARPR ||
5919                     els_command == ELS_CMD_FDISC)
5920                         continue;
5921
5922                 if (piocb->drvrTimeout > 0) {
5923                         if (piocb->drvrTimeout >= timeout)
5924                                 piocb->drvrTimeout -= timeout;
5925                         else
5926                                 piocb->drvrTimeout = 0;
5927                         continue;
5928                 }
5929
5930                 remote_ID = 0xffffffff;
5931                 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
5932                         remote_ID = cmd->un.elsreq64.remoteID;
5933                 else {
5934                         struct lpfc_nodelist *ndlp;
5935                         ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
5936                         if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5937                                 remote_ID = ndlp->nlp_DID;
5938                 }
5939                 list_add_tail(&piocb->dlist, &abort_list);
5940         }
5941         spin_lock_irq(&phba->hbalock);
5942         list_splice(&txcmplq_completions, &pring->txcmplq);
5943         spin_unlock_irq(&phba->hbalock);
5944
5945         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
5946                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5947                          "0127 ELS timeout Data: x%x x%x x%x "
5948                          "x%x\n", els_command,
5949                          remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
5950                 spin_lock_irq(&phba->hbalock);
5951                 list_del_init(&piocb->dlist);
5952                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5953                 spin_unlock_irq(&phba->hbalock);
5954         }
5955
5956         if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
5957                 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
5958 }
5959
5960 /**
5961  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
5962  * @vport: pointer to a host virtual N_Port data structure.
5963  *
5964  * This routine is used to clean up all the outstanding ELS commands on a
5965  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
5966  * routine. After that, it walks the ELS transmit queue to remove all the
5967  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
5968  * the IOCBs with a non-NULL completion callback function, the callback
5969  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5970  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
5971  * callback function, the IOCB will simply be released. Finally, it walks
5972  * the ELS transmit completion queue to issue an abort IOCB to any transmit
5973  * completion queue IOCB that is associated with the @vport and is not
5974  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
5975  * part of the discovery state machine) out to HBA by invoking the
5976  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
5977  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
5978  * the IOCBs are aborted when this function returns.
5979  **/
5980 void
5981 lpfc_els_flush_cmd(struct lpfc_vport *vport)
5982 {
5983         LIST_HEAD(completions);
5984         struct lpfc_hba  *phba = vport->phba;
5985         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5986         struct lpfc_iocbq *tmp_iocb, *piocb;
5987         IOCB_t *cmd = NULL;
5988
5989         lpfc_fabric_abort_vport(vport);
5990
5991         spin_lock_irq(&phba->hbalock);
5992         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5993                 cmd = &piocb->iocb;
5994
5995                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5996                         continue;
5997                 }
5998
5999                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
6000                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
6001                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
6002                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
6003                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
6004                         continue;
6005
6006                 if (piocb->vport != vport)
6007                         continue;
6008
6009                 list_move_tail(&piocb->list, &completions);
6010                 pring->txq_cnt--;
6011         }
6012
6013         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6014                 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
6015                         continue;
6016                 }
6017
6018                 if (piocb->vport != vport)
6019                         continue;
6020
6021                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6022         }
6023         spin_unlock_irq(&phba->hbalock);
6024
6025         /* Cancell all the IOCBs from the completions list */
6026         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6027                               IOERR_SLI_ABORTED);
6028
6029         return;
6030 }
6031
6032 /**
6033  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
6034  * @phba: pointer to lpfc hba data structure.
6035  *
6036  * This routine is used to clean up all the outstanding ELS commands on a
6037  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
6038  * routine. After that, it walks the ELS transmit queue to remove all the
6039  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
6040  * the IOCBs with the completion callback function associated, the callback
6041  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
6042  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
6043  * callback function associated, the IOCB will simply be released. Finally,
6044  * it walks the ELS transmit completion queue to issue an abort IOCB to any
6045  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
6046  * management plane IOCBs that are not part of the discovery state machine)
6047  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
6048  **/
6049 void
6050 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
6051 {
6052         LIST_HEAD(completions);
6053         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6054         struct lpfc_iocbq *tmp_iocb, *piocb;
6055         IOCB_t *cmd = NULL;
6056
6057         lpfc_fabric_abort_hba(phba);
6058         spin_lock_irq(&phba->hbalock);
6059         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
6060                 cmd = &piocb->iocb;
6061                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
6062                         continue;
6063                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
6064                 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
6065                     cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
6066                     cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
6067                     cmd->ulpCommand == CMD_ABORT_XRI_CN)
6068                         continue;
6069                 list_move_tail(&piocb->list, &completions);
6070                 pring->txq_cnt--;
6071         }
6072         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6073                 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
6074                         continue;
6075                 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6076         }
6077         spin_unlock_irq(&phba->hbalock);
6078
6079         /* Cancel all the IOCBs from the completions list */
6080         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6081                               IOERR_SLI_ABORTED);
6082
6083         return;
6084 }
6085
6086 /**
6087  * lpfc_send_els_failure_event - Posts an ELS command failure event
6088  * @phba: Pointer to hba context object.
6089  * @cmdiocbp: Pointer to command iocb which reported error.
6090  * @rspiocbp: Pointer to response iocb which reported error.
6091  *
6092  * This function sends an event when there is an ELS command
6093  * failure.
6094  **/
6095 void
6096 lpfc_send_els_failure_event(struct lpfc_hba *phba,
6097                         struct lpfc_iocbq *cmdiocbp,
6098                         struct lpfc_iocbq *rspiocbp)
6099 {
6100         struct lpfc_vport *vport = cmdiocbp->vport;
6101         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6102         struct lpfc_lsrjt_event lsrjt_event;
6103         struct lpfc_fabric_event_header fabric_event;
6104         struct ls_rjt stat;
6105         struct lpfc_nodelist *ndlp;
6106         uint32_t *pcmd;
6107
6108         ndlp = cmdiocbp->context1;
6109         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
6110                 return;
6111
6112         if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
6113                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
6114                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
6115                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
6116                         sizeof(struct lpfc_name));
6117                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
6118                         sizeof(struct lpfc_name));
6119                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
6120                         cmdiocbp->context2)->virt);
6121                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
6122                 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
6123                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
6124                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
6125                 fc_host_post_vendor_event(shost,
6126                         fc_get_event_number(),
6127                         sizeof(lsrjt_event),
6128                         (char *)&lsrjt_event,
6129                         LPFC_NL_VENDOR_ID);
6130                 return;
6131         }
6132         if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
6133                 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
6134                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
6135                 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
6136                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
6137                 else
6138                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
6139                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
6140                         sizeof(struct lpfc_name));
6141                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
6142                         sizeof(struct lpfc_name));
6143                 fc_host_post_vendor_event(shost,
6144                         fc_get_event_number(),
6145                         sizeof(fabric_event),
6146                         (char *)&fabric_event,
6147                         LPFC_NL_VENDOR_ID);
6148                 return;
6149         }
6150
6151 }
6152
6153 /**
6154  * lpfc_send_els_event - Posts unsolicited els event
6155  * @vport: Pointer to vport object.
6156  * @ndlp: Pointer FC node object.
6157  * @cmd: ELS command code.
6158  *
6159  * This function posts an event when there is an incoming
6160  * unsolicited ELS command.
6161  **/
6162 static void
6163 lpfc_send_els_event(struct lpfc_vport *vport,
6164                     struct lpfc_nodelist *ndlp,
6165                     uint32_t *payload)
6166 {
6167         struct lpfc_els_event_header *els_data = NULL;
6168         struct lpfc_logo_event *logo_data = NULL;
6169         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6170
6171         if (*payload == ELS_CMD_LOGO) {
6172                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
6173                 if (!logo_data) {
6174                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6175                                 "0148 Failed to allocate memory "
6176                                 "for LOGO event\n");
6177                         return;
6178                 }
6179                 els_data = &logo_data->header;
6180         } else {
6181                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
6182                         GFP_KERNEL);
6183                 if (!els_data) {
6184                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6185                                 "0149 Failed to allocate memory "
6186                                 "for ELS event\n");
6187                         return;
6188                 }
6189         }
6190         els_data->event_type = FC_REG_ELS_EVENT;
6191         switch (*payload) {
6192         case ELS_CMD_PLOGI:
6193                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
6194                 break;
6195         case ELS_CMD_PRLO:
6196                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
6197                 break;
6198         case ELS_CMD_ADISC:
6199                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
6200                 break;
6201         case ELS_CMD_LOGO:
6202                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
6203                 /* Copy the WWPN in the LOGO payload */
6204                 memcpy(logo_data->logo_wwpn, &payload[2],
6205                         sizeof(struct lpfc_name));
6206                 break;
6207         default:
6208                 kfree(els_data);
6209                 return;
6210         }
6211         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
6212         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
6213         if (*payload == ELS_CMD_LOGO) {
6214                 fc_host_post_vendor_event(shost,
6215                         fc_get_event_number(),
6216                         sizeof(struct lpfc_logo_event),
6217                         (char *)logo_data,
6218                         LPFC_NL_VENDOR_ID);
6219                 kfree(logo_data);
6220         } else {
6221                 fc_host_post_vendor_event(shost,
6222                         fc_get_event_number(),
6223                         sizeof(struct lpfc_els_event_header),
6224                         (char *)els_data,
6225                         LPFC_NL_VENDOR_ID);
6226                 kfree(els_data);
6227         }
6228
6229         return;
6230 }
6231
6232
6233 /**
6234  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
6235  * @phba: pointer to lpfc hba data structure.
6236  * @pring: pointer to a SLI ring.
6237  * @vport: pointer to a host virtual N_Port data structure.
6238  * @elsiocb: pointer to lpfc els command iocb data structure.
6239  *
6240  * This routine is used for processing the IOCB associated with a unsolicited
6241  * event. It first determines whether there is an existing ndlp that matches
6242  * the DID from the unsolicited IOCB. If not, it will create a new one with
6243  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
6244  * IOCB is then used to invoke the proper routine and to set up proper state
6245  * of the discovery state machine.
6246  **/
6247 static void
6248 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6249                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
6250 {
6251         struct Scsi_Host  *shost;
6252         struct lpfc_nodelist *ndlp;
6253         struct ls_rjt stat;
6254         uint32_t *payload;
6255         uint32_t cmd, did, newnode, rjt_err = 0;
6256         IOCB_t *icmd = &elsiocb->iocb;
6257
6258         if (!vport || !(elsiocb->context2))
6259                 goto dropit;
6260
6261         newnode = 0;
6262         payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
6263         cmd = *payload;
6264         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
6265                 lpfc_post_buffer(phba, pring, 1);
6266
6267         did = icmd->un.rcvels.remoteID;
6268         if (icmd->ulpStatus) {
6269                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6270                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
6271                         icmd->ulpStatus, icmd->un.ulpWord[4], did);
6272                 goto dropit;
6273         }
6274
6275         /* Check to see if link went down during discovery */
6276         if (lpfc_els_chk_latt(vport))
6277                 goto dropit;
6278
6279         /* Ignore traffic received during vport shutdown. */
6280         if (vport->load_flag & FC_UNLOADING)
6281                 goto dropit;
6282
6283         /* If NPort discovery is delayed drop incoming ELS */
6284         if ((vport->fc_flag & FC_DISC_DELAYED) &&
6285                         (cmd != ELS_CMD_PLOGI))
6286                 goto dropit;
6287
6288         ndlp = lpfc_findnode_did(vport, did);
6289         if (!ndlp) {
6290                 /* Cannot find existing Fabric ndlp, so allocate a new one */
6291                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
6292                 if (!ndlp)
6293                         goto dropit;
6294
6295                 lpfc_nlp_init(vport, ndlp, did);
6296                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6297                 newnode = 1;
6298                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6299                         ndlp->nlp_type |= NLP_FABRIC;
6300         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
6301                 ndlp = lpfc_enable_node(vport, ndlp,
6302                                         NLP_STE_UNUSED_NODE);
6303                 if (!ndlp)
6304                         goto dropit;
6305                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6306                 newnode = 1;
6307                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6308                         ndlp->nlp_type |= NLP_FABRIC;
6309         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
6310                 /* This is similar to the new node path */
6311                 ndlp = lpfc_nlp_get(ndlp);
6312                 if (!ndlp)
6313                         goto dropit;
6314                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6315                 newnode = 1;
6316         }
6317
6318         phba->fc_stat.elsRcvFrame++;
6319
6320         elsiocb->context1 = lpfc_nlp_get(ndlp);
6321         elsiocb->vport = vport;
6322
6323         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
6324                 cmd &= ELS_CMD_MASK;
6325         }
6326         /* ELS command <elsCmd> received from NPORT <did> */
6327         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6328                          "0112 ELS command x%x received from NPORT x%x "
6329                          "Data: x%x\n", cmd, did, vport->port_state);
6330         switch (cmd) {
6331         case ELS_CMD_PLOGI:
6332                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6333                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
6334                         did, vport->port_state, ndlp->nlp_flag);
6335
6336                 phba->fc_stat.elsRcvPLOGI++;
6337                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
6338
6339                 lpfc_send_els_event(vport, ndlp, payload);
6340
6341                 /* If Nport discovery is delayed, reject PLOGIs */
6342                 if (vport->fc_flag & FC_DISC_DELAYED) {
6343                         rjt_err = LSRJT_UNABLE_TPC;
6344                         break;
6345                 }
6346                 if (vport->port_state < LPFC_DISC_AUTH) {
6347                         if (!(phba->pport->fc_flag & FC_PT2PT) ||
6348                                 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
6349                                 rjt_err = LSRJT_UNABLE_TPC;
6350                                 break;
6351                         }
6352                         /* We get here, and drop thru, if we are PT2PT with
6353                          * another NPort and the other side has initiated
6354                          * the PLOGI before responding to our FLOGI.
6355                          */
6356                 }
6357
6358                 shost = lpfc_shost_from_vport(vport);
6359                 spin_lock_irq(shost->host_lock);
6360                 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
6361                 spin_unlock_irq(shost->host_lock);
6362
6363                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6364                                         NLP_EVT_RCV_PLOGI);
6365
6366                 break;
6367         case ELS_CMD_FLOGI:
6368                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6369                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
6370                         did, vport->port_state, ndlp->nlp_flag);
6371
6372                 phba->fc_stat.elsRcvFLOGI++;
6373                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
6374                 if (newnode)
6375                         lpfc_nlp_put(ndlp);
6376                 break;
6377         case ELS_CMD_LOGO:
6378                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6379                         "RCV LOGO:        did:x%x/ste:x%x flg:x%x",
6380                         did, vport->port_state, ndlp->nlp_flag);
6381
6382                 phba->fc_stat.elsRcvLOGO++;
6383                 lpfc_send_els_event(vport, ndlp, payload);
6384                 if (vport->port_state < LPFC_DISC_AUTH) {
6385                         rjt_err = LSRJT_UNABLE_TPC;
6386                         break;
6387                 }
6388                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
6389                 break;
6390         case ELS_CMD_PRLO:
6391                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6392                         "RCV PRLO:        did:x%x/ste:x%x flg:x%x",
6393                         did, vport->port_state, ndlp->nlp_flag);
6394
6395                 phba->fc_stat.elsRcvPRLO++;
6396                 lpfc_send_els_event(vport, ndlp, payload);
6397                 if (vport->port_state < LPFC_DISC_AUTH) {
6398                         rjt_err = LSRJT_UNABLE_TPC;
6399                         break;
6400                 }
6401                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
6402                 break;
6403         case ELS_CMD_RSCN:
6404                 phba->fc_stat.elsRcvRSCN++;
6405                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
6406                 if (newnode)
6407                         lpfc_nlp_put(ndlp);
6408                 break;
6409         case ELS_CMD_ADISC:
6410                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6411                         "RCV ADISC:       did:x%x/ste:x%x flg:x%x",
6412                         did, vport->port_state, ndlp->nlp_flag);
6413
6414                 lpfc_send_els_event(vport, ndlp, payload);
6415                 phba->fc_stat.elsRcvADISC++;
6416                 if (vport->port_state < LPFC_DISC_AUTH) {
6417                         rjt_err = LSRJT_UNABLE_TPC;
6418                         break;
6419                 }
6420                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6421                                         NLP_EVT_RCV_ADISC);
6422                 break;
6423         case ELS_CMD_PDISC:
6424                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6425                         "RCV PDISC:       did:x%x/ste:x%x flg:x%x",
6426                         did, vport->port_state, ndlp->nlp_flag);
6427
6428                 phba->fc_stat.elsRcvPDISC++;
6429                 if (vport->port_state < LPFC_DISC_AUTH) {
6430                         rjt_err = LSRJT_UNABLE_TPC;
6431                         break;
6432                 }
6433                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6434                                         NLP_EVT_RCV_PDISC);
6435                 break;
6436         case ELS_CMD_FARPR:
6437                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6438                         "RCV FARPR:       did:x%x/ste:x%x flg:x%x",
6439                         did, vport->port_state, ndlp->nlp_flag);
6440
6441                 phba->fc_stat.elsRcvFARPR++;
6442                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
6443                 break;
6444         case ELS_CMD_FARP:
6445                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6446                         "RCV FARP:        did:x%x/ste:x%x flg:x%x",
6447                         did, vport->port_state, ndlp->nlp_flag);
6448
6449                 phba->fc_stat.elsRcvFARP++;
6450                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
6451                 break;
6452         case ELS_CMD_FAN:
6453                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6454                         "RCV FAN:         did:x%x/ste:x%x flg:x%x",
6455                         did, vport->port_state, ndlp->nlp_flag);
6456
6457                 phba->fc_stat.elsRcvFAN++;
6458                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
6459                 break;
6460         case ELS_CMD_PRLI:
6461                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6462                         "RCV PRLI:        did:x%x/ste:x%x flg:x%x",
6463                         did, vport->port_state, ndlp->nlp_flag);
6464
6465                 phba->fc_stat.elsRcvPRLI++;
6466                 if (vport->port_state < LPFC_DISC_AUTH) {
6467                         rjt_err = LSRJT_UNABLE_TPC;
6468                         break;
6469                 }
6470                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
6471                 break;
6472         case ELS_CMD_LIRR:
6473                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6474                         "RCV LIRR:        did:x%x/ste:x%x flg:x%x",
6475                         did, vport->port_state, ndlp->nlp_flag);
6476
6477                 phba->fc_stat.elsRcvLIRR++;
6478                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
6479                 if (newnode)
6480                         lpfc_nlp_put(ndlp);
6481                 break;
6482         case ELS_CMD_RLS:
6483                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6484                         "RCV RLS:         did:x%x/ste:x%x flg:x%x",
6485                         did, vport->port_state, ndlp->nlp_flag);
6486
6487                 phba->fc_stat.elsRcvRLS++;
6488                 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
6489                 if (newnode)
6490                         lpfc_nlp_put(ndlp);
6491                 break;
6492         case ELS_CMD_RPS:
6493                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6494                         "RCV RPS:         did:x%x/ste:x%x flg:x%x",
6495                         did, vport->port_state, ndlp->nlp_flag);
6496
6497                 phba->fc_stat.elsRcvRPS++;
6498                 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
6499                 if (newnode)
6500                         lpfc_nlp_put(ndlp);
6501                 break;
6502         case ELS_CMD_RPL:
6503                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6504                         "RCV RPL:         did:x%x/ste:x%x flg:x%x",
6505                         did, vport->port_state, ndlp->nlp_flag);
6506
6507                 phba->fc_stat.elsRcvRPL++;
6508                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
6509                 if (newnode)
6510                         lpfc_nlp_put(ndlp);
6511                 break;
6512         case ELS_CMD_RNID:
6513                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6514                         "RCV RNID:        did:x%x/ste:x%x flg:x%x",
6515                         did, vport->port_state, ndlp->nlp_flag);
6516
6517                 phba->fc_stat.elsRcvRNID++;
6518                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
6519                 if (newnode)
6520                         lpfc_nlp_put(ndlp);
6521                 break;
6522         case ELS_CMD_RTV:
6523                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6524                         "RCV RTV:        did:x%x/ste:x%x flg:x%x",
6525                         did, vport->port_state, ndlp->nlp_flag);
6526                 phba->fc_stat.elsRcvRTV++;
6527                 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
6528                 if (newnode)
6529                         lpfc_nlp_put(ndlp);
6530                 break;
6531         case ELS_CMD_RRQ:
6532                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6533                         "RCV RRQ:         did:x%x/ste:x%x flg:x%x",
6534                         did, vport->port_state, ndlp->nlp_flag);
6535
6536                 phba->fc_stat.elsRcvRRQ++;
6537                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
6538                 if (newnode)
6539                         lpfc_nlp_put(ndlp);
6540                 break;
6541         case ELS_CMD_ECHO:
6542                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6543                         "RCV ECHO:        did:x%x/ste:x%x flg:x%x",
6544                         did, vport->port_state, ndlp->nlp_flag);
6545
6546                 phba->fc_stat.elsRcvECHO++;
6547                 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
6548                 if (newnode)
6549                         lpfc_nlp_put(ndlp);
6550                 break;
6551         default:
6552                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6553                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
6554                         cmd, did, vport->port_state);
6555
6556                 /* Unsupported ELS command, reject */
6557                 rjt_err = LSRJT_CMD_UNSUPPORTED;
6558
6559                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
6560                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6561                                  "0115 Unknown ELS command x%x "
6562                                  "received from NPORT x%x\n", cmd, did);
6563                 if (newnode)
6564                         lpfc_nlp_put(ndlp);
6565                 break;
6566         }
6567
6568         /* check if need to LS_RJT received ELS cmd */
6569         if (rjt_err) {
6570                 memset(&stat, 0, sizeof(stat));
6571                 stat.un.b.lsRjtRsnCode = rjt_err;
6572                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
6573                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
6574                         NULL);
6575         }
6576
6577         lpfc_nlp_put(elsiocb->context1);
6578         elsiocb->context1 = NULL;
6579         return;
6580
6581 dropit:
6582         if (vport && !(vport->load_flag & FC_UNLOADING))
6583                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6584                         "0111 Dropping received ELS cmd "
6585                         "Data: x%x x%x x%x\n",
6586                         icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
6587         phba->fc_stat.elsRcvDrop++;
6588 }
6589
6590 /**
6591  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6592  * @phba: pointer to lpfc hba data structure.
6593  * @vpi: host virtual N_Port identifier.
6594  *
6595  * This routine finds a vport on a HBA (referred by @phba) through a
6596  * @vpi. The function walks the HBA's vport list and returns the address
6597  * of the vport with the matching @vpi.
6598  *
6599  * Return code
6600  *    NULL - No vport with the matching @vpi found
6601  *    Otherwise - Address to the vport with the matching @vpi.
6602  **/
6603 struct lpfc_vport *
6604 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6605 {
6606         struct lpfc_vport *vport;
6607         unsigned long flags;
6608         int i = 0;
6609
6610         /* The physical ports are always vpi 0 - translate is unnecessary. */
6611         if (vpi > 0) {
6612                 /*
6613                  * Translate the physical vpi to the logical vpi.  The
6614                  * vport stores the logical vpi.
6615                  */
6616                 for (i = 0; i < phba->max_vpi; i++) {
6617                         if (vpi == phba->vpi_ids[i])
6618                                 break;
6619                 }
6620
6621                 if (i >= phba->max_vpi) {
6622                         lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
6623                                          "2936 Could not find Vport mapped "
6624                                          "to vpi %d\n", vpi);
6625                         return NULL;
6626                 }
6627         }
6628
6629         spin_lock_irqsave(&phba->hbalock, flags);
6630         list_for_each_entry(vport, &phba->port_list, listentry) {
6631                 if (vport->vpi == i) {
6632                         spin_unlock_irqrestore(&phba->hbalock, flags);
6633                         return vport;
6634                 }
6635         }
6636         spin_unlock_irqrestore(&phba->hbalock, flags);
6637         return NULL;
6638 }
6639
6640 /**
6641  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
6642  * @phba: pointer to lpfc hba data structure.
6643  * @pring: pointer to a SLI ring.
6644  * @elsiocb: pointer to lpfc els iocb data structure.
6645  *
6646  * This routine is used to process an unsolicited event received from a SLI
6647  * (Service Level Interface) ring. The actual processing of the data buffer
6648  * associated with the unsolicited event is done by invoking the routine
6649  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
6650  * SLI ring on which the unsolicited event was received.
6651  **/
6652 void
6653 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6654                      struct lpfc_iocbq *elsiocb)
6655 {
6656         struct lpfc_vport *vport = phba->pport;
6657         IOCB_t *icmd = &elsiocb->iocb;
6658         dma_addr_t paddr;
6659         struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
6660         struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
6661
6662         elsiocb->context1 = NULL;
6663         elsiocb->context2 = NULL;
6664         elsiocb->context3 = NULL;
6665
6666         if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
6667                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
6668         } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
6669             (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
6670                 phba->fc_stat.NoRcvBuf++;
6671                 /* Not enough posted buffers; Try posting more buffers */
6672                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
6673                         lpfc_post_buffer(phba, pring, 0);
6674                 return;
6675         }
6676
6677         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6678             (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
6679              icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6680                 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
6681                         vport = phba->pport;
6682                 else
6683                         vport = lpfc_find_vport_by_vpid(phba,
6684                                                 icmd->unsli3.rcvsli3.vpi);
6685         }
6686
6687         /* If there are no BDEs associated
6688          * with this IOCB, there is nothing to do.
6689          */
6690         if (icmd->ulpBdeCount == 0)
6691                 return;
6692
6693         /* type of ELS cmd is first 32bit word
6694          * in packet
6695          */
6696         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
6697                 elsiocb->context2 = bdeBuf1;
6698         } else {
6699                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
6700                                  icmd->un.cont64[0].addrLow);
6701                 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
6702                                                              paddr);
6703         }
6704
6705         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
6706         /*
6707          * The different unsolicited event handlers would tell us
6708          * if they are done with "mp" by setting context2 to NULL.
6709          */
6710         if (elsiocb->context2) {
6711                 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
6712                 elsiocb->context2 = NULL;
6713         }
6714
6715         /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
6716         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
6717             icmd->ulpBdeCount == 2) {
6718                 elsiocb->context2 = bdeBuf2;
6719                 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
6720                 /* free mp if we are done with it */
6721                 if (elsiocb->context2) {
6722                         lpfc_in_buf_free(phba, elsiocb->context2);
6723                         elsiocb->context2 = NULL;
6724                 }
6725         }
6726 }
6727
6728 /**
6729  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
6730  * @phba: pointer to lpfc hba data structure.
6731  * @vport: pointer to a virtual N_Port data structure.
6732  *
6733  * This routine issues a Port Login (PLOGI) to the Name Server with
6734  * State Change Request (SCR) for a @vport. This routine will create an
6735  * ndlp for the Name Server associated to the @vport if such node does
6736  * not already exist. The PLOGI to Name Server is issued by invoking the
6737  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
6738  * (FDMI) is configured to the @vport, a FDMI node will be created and
6739  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
6740  **/
6741 void
6742 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
6743 {
6744         struct lpfc_nodelist *ndlp, *ndlp_fdmi;
6745         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6746
6747         /*
6748          * If lpfc_delay_discovery parameter is set and the clean address
6749          * bit is cleared and fc fabric parameters chenged, delay FC NPort
6750          * discovery.
6751          */
6752         spin_lock_irq(shost->host_lock);
6753         if (vport->fc_flag & FC_DISC_DELAYED) {
6754                 spin_unlock_irq(shost->host_lock);
6755                 mod_timer(&vport->delayed_disc_tmo,
6756                         jiffies + HZ * phba->fc_ratov);
6757                 return;
6758         }
6759         spin_unlock_irq(shost->host_lock);
6760
6761         ndlp = lpfc_findnode_did(vport, NameServer_DID);
6762         if (!ndlp) {
6763                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
6764                 if (!ndlp) {
6765                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6766                                 lpfc_disc_start(vport);
6767                                 return;
6768                         }
6769                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6770                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6771                                          "0251 NameServer login: no memory\n");
6772                         return;
6773                 }
6774                 lpfc_nlp_init(vport, ndlp, NameServer_DID);
6775         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
6776                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
6777                 if (!ndlp) {
6778                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6779                                 lpfc_disc_start(vport);
6780                                 return;
6781                         }
6782                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6783                         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6784                                         "0348 NameServer login: node freed\n");
6785                         return;
6786                 }
6787         }
6788         ndlp->nlp_type |= NLP_FABRIC;
6789
6790         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6791
6792         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
6793                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6794                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6795                                  "0252 Cannot issue NameServer login\n");
6796                 return;
6797         }
6798
6799         if (vport->cfg_fdmi_on) {
6800                 /* If this is the first time, allocate an ndlp and initialize
6801                  * it. Otherwise, make sure the node is enabled and then do the
6802                  * login.
6803                  */
6804                 ndlp_fdmi = lpfc_findnode_did(vport, FDMI_DID);
6805                 if (!ndlp_fdmi) {
6806                         ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
6807                                                   GFP_KERNEL);
6808                         if (ndlp_fdmi) {
6809                                 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
6810                                 ndlp_fdmi->nlp_type |= NLP_FABRIC;
6811                         } else
6812                                 return;
6813                 }
6814                 if (!NLP_CHK_NODE_ACT(ndlp_fdmi))
6815                         ndlp_fdmi = lpfc_enable_node(vport,
6816                                                      ndlp_fdmi,
6817                                                      NLP_STE_NPR_NODE);
6818
6819                 if (ndlp_fdmi) {
6820                         lpfc_nlp_set_state(vport, ndlp_fdmi,
6821                                            NLP_STE_PLOGI_ISSUE);
6822                         lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID, 0);
6823                 }
6824         }
6825 }
6826
6827 /**
6828  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
6829  * @phba: pointer to lpfc hba data structure.
6830  * @pmb: pointer to the driver internal queue element for mailbox command.
6831  *
6832  * This routine is the completion callback function to register new vport
6833  * mailbox command. If the new vport mailbox command completes successfully,
6834  * the fabric registration login shall be performed on physical port (the
6835  * new vport created is actually a physical port, with VPI 0) or the port
6836  * login to Name Server for State Change Request (SCR) will be performed
6837  * on virtual port (real virtual port, with VPI greater than 0).
6838  **/
6839 static void
6840 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6841 {
6842         struct lpfc_vport *vport = pmb->vport;
6843         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6844         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
6845         MAILBOX_t *mb = &pmb->u.mb;
6846         int rc;
6847
6848         spin_lock_irq(shost->host_lock);
6849         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6850         spin_unlock_irq(shost->host_lock);
6851
6852         if (mb->mbxStatus) {
6853                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6854                                 "0915 Register VPI failed : Status: x%x"
6855                                 " upd bit: x%x \n", mb->mbxStatus,
6856                                  mb->un.varRegVpi.upd);
6857                 if (phba->sli_rev == LPFC_SLI_REV4 &&
6858                         mb->un.varRegVpi.upd)
6859                         goto mbox_err_exit ;
6860
6861                 switch (mb->mbxStatus) {
6862                 case 0x11:      /* unsupported feature */
6863                 case 0x9603:    /* max_vpi exceeded */
6864                 case 0x9602:    /* Link event since CLEAR_LA */
6865                         /* giving up on vport registration */
6866                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6867                         spin_lock_irq(shost->host_lock);
6868                         vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6869                         spin_unlock_irq(shost->host_lock);
6870                         lpfc_can_disctmo(vport);
6871                         break;
6872                 /* If reg_vpi fail with invalid VPI status, re-init VPI */
6873                 case 0x20:
6874                         spin_lock_irq(shost->host_lock);
6875                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6876                         spin_unlock_irq(shost->host_lock);
6877                         lpfc_init_vpi(phba, pmb, vport->vpi);
6878                         pmb->vport = vport;
6879                         pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
6880                         rc = lpfc_sli_issue_mbox(phba, pmb,
6881                                 MBX_NOWAIT);
6882                         if (rc == MBX_NOT_FINISHED) {
6883                                 lpfc_printf_vlog(vport,
6884                                         KERN_ERR, LOG_MBOX,
6885                                         "2732 Failed to issue INIT_VPI"
6886                                         " mailbox command\n");
6887                         } else {
6888                                 lpfc_nlp_put(ndlp);
6889                                 return;
6890                         }
6891
6892                 default:
6893                         /* Try to recover from this error */
6894                         if (phba->sli_rev == LPFC_SLI_REV4)
6895                                 lpfc_sli4_unreg_all_rpis(vport);
6896                         lpfc_mbx_unreg_vpi(vport);
6897                         spin_lock_irq(shost->host_lock);
6898                         vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6899                         spin_unlock_irq(shost->host_lock);
6900                         if (vport->port_type == LPFC_PHYSICAL_PORT
6901                                 && !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
6902                                 lpfc_issue_init_vfi(vport);
6903                         else
6904                                 lpfc_initial_fdisc(vport);
6905                         break;
6906                 }
6907         } else {
6908                 spin_lock_irq(shost->host_lock);
6909                 vport->vpi_state |= LPFC_VPI_REGISTERED;
6910                 spin_unlock_irq(shost->host_lock);
6911                 if (vport == phba->pport) {
6912                         if (phba->sli_rev < LPFC_SLI_REV4)
6913                                 lpfc_issue_fabric_reglogin(vport);
6914                         else {
6915                                 /*
6916                                  * If the physical port is instantiated using
6917                                  * FDISC, do not start vport discovery.
6918                                  */
6919                                 if (vport->port_state != LPFC_FDISC)
6920                                         lpfc_start_fdiscs(phba);
6921                                 lpfc_do_scr_ns_plogi(phba, vport);
6922                         }
6923                 } else
6924                         lpfc_do_scr_ns_plogi(phba, vport);
6925         }
6926 mbox_err_exit:
6927         /* Now, we decrement the ndlp reference count held for this
6928          * callback function
6929          */
6930         lpfc_nlp_put(ndlp);
6931
6932         mempool_free(pmb, phba->mbox_mem_pool);
6933         return;
6934 }
6935
6936 /**
6937  * lpfc_register_new_vport - Register a new vport with a HBA
6938  * @phba: pointer to lpfc hba data structure.
6939  * @vport: pointer to a host virtual N_Port data structure.
6940  * @ndlp: pointer to a node-list data structure.
6941  *
6942  * This routine registers the @vport as a new virtual port with a HBA.
6943  * It is done through a registering vpi mailbox command.
6944  **/
6945 void
6946 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
6947                         struct lpfc_nodelist *ndlp)
6948 {
6949         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6950         LPFC_MBOXQ_t *mbox;
6951
6952         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6953         if (mbox) {
6954                 lpfc_reg_vpi(vport, mbox);
6955                 mbox->vport = vport;
6956                 mbox->context2 = lpfc_nlp_get(ndlp);
6957                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
6958                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6959                     == MBX_NOT_FINISHED) {
6960                         /* mailbox command not success, decrement ndlp
6961                          * reference count for this command
6962                          */
6963                         lpfc_nlp_put(ndlp);
6964                         mempool_free(mbox, phba->mbox_mem_pool);
6965
6966                         lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6967                                 "0253 Register VPI: Can't send mbox\n");
6968                         goto mbox_err_exit;
6969                 }
6970         } else {
6971                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6972                                  "0254 Register VPI: no memory\n");
6973                 goto mbox_err_exit;
6974         }
6975         return;
6976
6977 mbox_err_exit:
6978         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6979         spin_lock_irq(shost->host_lock);
6980         vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6981         spin_unlock_irq(shost->host_lock);
6982         return;
6983 }
6984
6985 /**
6986  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
6987  * @phba: pointer to lpfc hba data structure.
6988  *
6989  * This routine cancels the retry delay timers to all the vports.
6990  **/
6991 void
6992 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
6993 {
6994         struct lpfc_vport **vports;
6995         struct lpfc_nodelist *ndlp;
6996         uint32_t link_state;
6997         int i;
6998
6999         /* Treat this failure as linkdown for all vports */
7000         link_state = phba->link_state;
7001         lpfc_linkdown(phba);
7002         phba->link_state = link_state;
7003
7004         vports = lpfc_create_vport_work_array(phba);
7005
7006         if (vports) {
7007                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
7008                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
7009                         if (ndlp)
7010                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
7011                         lpfc_els_flush_cmd(vports[i]);
7012                 }
7013                 lpfc_destroy_vport_work_array(phba, vports);
7014         }
7015 }
7016
7017 /**
7018  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
7019  * @phba: pointer to lpfc hba data structure.
7020  *
7021  * This routine abort all pending discovery commands and
7022  * start a timer to retry FLOGI for the physical port
7023  * discovery.
7024  **/
7025 void
7026 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
7027 {
7028         struct lpfc_nodelist *ndlp;
7029         struct Scsi_Host  *shost;
7030
7031         /* Cancel the all vports retry delay retry timers */
7032         lpfc_cancel_all_vport_retry_delay_timer(phba);
7033
7034         /* If fabric require FLOGI, then re-instantiate physical login */
7035         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
7036         if (!ndlp)
7037                 return;
7038
7039         shost = lpfc_shost_from_vport(phba->pport);
7040         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
7041         spin_lock_irq(shost->host_lock);
7042         ndlp->nlp_flag |= NLP_DELAY_TMO;
7043         spin_unlock_irq(shost->host_lock);
7044         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
7045         phba->pport->port_state = LPFC_FLOGI;
7046         return;
7047 }
7048
7049 /**
7050  * lpfc_fabric_login_reqd - Check if FLOGI required.
7051  * @phba: pointer to lpfc hba data structure.
7052  * @cmdiocb: pointer to FDISC command iocb.
7053  * @rspiocb: pointer to FDISC response iocb.
7054  *
7055  * This routine checks if a FLOGI is reguired for FDISC
7056  * to succeed.
7057  **/
7058 static int
7059 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
7060                 struct lpfc_iocbq *cmdiocb,
7061                 struct lpfc_iocbq *rspiocb)
7062 {
7063
7064         if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
7065                 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
7066                 return 0;
7067         else
7068                 return 1;
7069 }
7070
7071 /**
7072  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
7073  * @phba: pointer to lpfc hba data structure.
7074  * @cmdiocb: pointer to lpfc command iocb data structure.
7075  * @rspiocb: pointer to lpfc response iocb data structure.
7076  *
7077  * This routine is the completion callback function to a Fabric Discover
7078  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
7079  * single threaded, each FDISC completion callback function will reset
7080  * the discovery timer for all vports such that the timers will not get
7081  * unnecessary timeout. The function checks the FDISC IOCB status. If error
7082  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
7083  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
7084  * assigned to the vport has been changed with the completion of the FDISC
7085  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
7086  * are unregistered from the HBA, and then the lpfc_register_new_vport()
7087  * routine is invoked to register new vport with the HBA. Otherwise, the
7088  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
7089  * Server for State Change Request (SCR).
7090  **/
7091 static void
7092 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7093                     struct lpfc_iocbq *rspiocb)
7094 {
7095         struct lpfc_vport *vport = cmdiocb->vport;
7096         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
7097         struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
7098         struct lpfc_nodelist *np;
7099         struct lpfc_nodelist *next_np;
7100         IOCB_t *irsp = &rspiocb->iocb;
7101         struct lpfc_iocbq *piocb;
7102         struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
7103         struct serv_parm *sp;
7104         uint8_t fabric_param_changed;
7105
7106         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7107                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
7108                          irsp->ulpStatus, irsp->un.ulpWord[4],
7109                          vport->fc_prevDID);
7110         /* Since all FDISCs are being single threaded, we
7111          * must reset the discovery timer for ALL vports
7112          * waiting to send FDISC when one completes.
7113          */
7114         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
7115                 lpfc_set_disctmo(piocb->vport);
7116         }
7117
7118         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7119                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
7120                 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
7121
7122         if (irsp->ulpStatus) {
7123
7124                 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
7125                         lpfc_retry_pport_discovery(phba);
7126                         goto out;
7127                 }
7128
7129                 /* Check for retry */
7130                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
7131                         goto out;
7132                 /* FDISC failed */
7133                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7134                                  "0126 FDISC failed. (%d/%d)\n",
7135                                  irsp->ulpStatus, irsp->un.ulpWord[4]);
7136                 goto fdisc_failed;
7137         }
7138         spin_lock_irq(shost->host_lock);
7139         vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
7140         vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
7141         vport->fc_flag |= FC_FABRIC;
7142         if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
7143                 vport->fc_flag |=  FC_PUBLIC_LOOP;
7144         spin_unlock_irq(shost->host_lock);
7145
7146         vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
7147         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
7148         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
7149         sp = prsp->virt + sizeof(uint32_t);
7150         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
7151         memcpy(&vport->fabric_portname, &sp->portName,
7152                 sizeof(struct lpfc_name));
7153         memcpy(&vport->fabric_nodename, &sp->nodeName,
7154                 sizeof(struct lpfc_name));
7155         if (fabric_param_changed &&
7156                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
7157                 /* If our NportID changed, we need to ensure all
7158                  * remaining NPORTs get unreg_login'ed so we can
7159                  * issue unreg_vpi.
7160                  */
7161                 list_for_each_entry_safe(np, next_np,
7162                         &vport->fc_nodes, nlp_listp) {
7163                         if (!NLP_CHK_NODE_ACT(ndlp) ||
7164                             (np->nlp_state != NLP_STE_NPR_NODE) ||
7165                             !(np->nlp_flag & NLP_NPR_ADISC))
7166                                 continue;
7167                         spin_lock_irq(shost->host_lock);
7168                         np->nlp_flag &= ~NLP_NPR_ADISC;
7169                         spin_unlock_irq(shost->host_lock);
7170                         lpfc_unreg_rpi(vport, np);
7171                 }
7172                 lpfc_cleanup_pending_mbox(vport);
7173
7174                 if (phba->sli_rev == LPFC_SLI_REV4)
7175                         lpfc_sli4_unreg_all_rpis(vport);
7176
7177                 lpfc_mbx_unreg_vpi(vport);
7178                 spin_lock_irq(shost->host_lock);
7179                 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7180                 if (phba->sli_rev == LPFC_SLI_REV4)
7181                         vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
7182                 else
7183                         vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
7184                 spin_unlock_irq(shost->host_lock);
7185         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
7186                 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
7187                 /*
7188                  * Driver needs to re-reg VPI in order for f/w
7189                  * to update the MAC address.
7190                  */
7191                 lpfc_register_new_vport(phba, vport, ndlp);
7192                 goto out;
7193         }
7194
7195         if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
7196                 lpfc_issue_init_vpi(vport);
7197         else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
7198                 lpfc_register_new_vport(phba, vport, ndlp);
7199         else
7200                 lpfc_do_scr_ns_plogi(phba, vport);
7201         goto out;
7202 fdisc_failed:
7203         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7204         /* Cancel discovery timer */
7205         lpfc_can_disctmo(vport);
7206         lpfc_nlp_put(ndlp);
7207 out:
7208         lpfc_els_free_iocb(phba, cmdiocb);
7209 }
7210
7211 /**
7212  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
7213  * @vport: pointer to a virtual N_Port data structure.
7214  * @ndlp: pointer to a node-list data structure.
7215  * @retry: number of retries to the command IOCB.
7216  *
7217  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
7218  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
7219  * routine to issue the IOCB, which makes sure only one outstanding fabric
7220  * IOCB will be sent off HBA at any given time.
7221  *
7222  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7223  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7224  * will be stored into the context1 field of the IOCB for the completion
7225  * callback function to the FDISC ELS command.
7226  *
7227  * Return code
7228  *   0 - Successfully issued fdisc iocb command
7229  *   1 - Failed to issue fdisc iocb command
7230  **/
7231 static int
7232 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7233                      uint8_t retry)
7234 {
7235         struct lpfc_hba *phba = vport->phba;
7236         IOCB_t *icmd;
7237         struct lpfc_iocbq *elsiocb;
7238         struct serv_parm *sp;
7239         uint8_t *pcmd;
7240         uint16_t cmdsize;
7241         int did = ndlp->nlp_DID;
7242         int rc;
7243
7244         vport->port_state = LPFC_FDISC;
7245         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
7246         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
7247                                      ELS_CMD_FDISC);
7248         if (!elsiocb) {
7249                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7250                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7251                                  "0255 Issue FDISC: no IOCB\n");
7252                 return 1;
7253         }
7254
7255         icmd = &elsiocb->iocb;
7256         icmd->un.elsreq64.myID = 0;
7257         icmd->un.elsreq64.fl = 1;
7258
7259         if  ((phba->sli_rev == LPFC_SLI_REV4) &&
7260              (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7261               LPFC_SLI_INTF_IF_TYPE_0)) {
7262                 /* FDISC needs to be 1 for WQE VPI */
7263                 elsiocb->iocb.ulpCt_h = (SLI4_CT_VPI >> 1) & 1;
7264                 elsiocb->iocb.ulpCt_l = SLI4_CT_VPI & 1 ;
7265                 /* Set the ulpContext to the vpi */
7266                 elsiocb->iocb.ulpContext = phba->vpi_ids[vport->vpi];
7267         } else {
7268                 /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
7269                 icmd->ulpCt_h = 1;
7270                 icmd->ulpCt_l = 0;
7271         }
7272
7273         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7274         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
7275         pcmd += sizeof(uint32_t); /* CSP Word 1 */
7276         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
7277         sp = (struct serv_parm *) pcmd;
7278         /* Setup CSPs accordingly for Fabric */
7279         sp->cmn.e_d_tov = 0;
7280         sp->cmn.w2.r_a_tov = 0;
7281         sp->cls1.classValid = 0;
7282         sp->cls2.seqDelivery = 1;
7283         sp->cls3.seqDelivery = 1;
7284
7285         pcmd += sizeof(uint32_t); /* CSP Word 2 */
7286         pcmd += sizeof(uint32_t); /* CSP Word 3 */
7287         pcmd += sizeof(uint32_t); /* CSP Word 4 */
7288         pcmd += sizeof(uint32_t); /* Port Name */
7289         memcpy(pcmd, &vport->fc_portname, 8);
7290         pcmd += sizeof(uint32_t); /* Node Name */
7291         pcmd += sizeof(uint32_t); /* Node Name */
7292         memcpy(pcmd, &vport->fc_nodename, 8);
7293
7294         lpfc_set_disctmo(vport);
7295
7296         phba->fc_stat.elsXmitFDISC++;
7297         elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
7298
7299         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7300                 "Issue FDISC:     did:x%x",
7301                 did, 0, 0);
7302
7303         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
7304         if (rc == IOCB_ERROR) {
7305                 lpfc_els_free_iocb(phba, elsiocb);
7306                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7307                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7308                                  "0256 Issue FDISC: Cannot send IOCB\n");
7309                 return 1;
7310         }
7311         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
7312         return 0;
7313 }
7314
7315 /**
7316  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
7317  * @phba: pointer to lpfc hba data structure.
7318  * @cmdiocb: pointer to lpfc command iocb data structure.
7319  * @rspiocb: pointer to lpfc response iocb data structure.
7320  *
7321  * This routine is the completion callback function to the issuing of a LOGO
7322  * ELS command off a vport. It frees the command IOCB and then decrement the
7323  * reference count held on ndlp for this completion function, indicating that
7324  * the reference to the ndlp is no long needed. Note that the
7325  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
7326  * callback function and an additional explicit ndlp reference decrementation
7327  * will trigger the actual release of the ndlp.
7328  **/
7329 static void
7330 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7331                         struct lpfc_iocbq *rspiocb)
7332 {
7333         struct lpfc_vport *vport = cmdiocb->vport;
7334         IOCB_t *irsp;
7335         struct lpfc_nodelist *ndlp;
7336         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7337
7338         ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
7339         irsp = &rspiocb->iocb;
7340         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7341                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
7342                 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
7343
7344         lpfc_els_free_iocb(phba, cmdiocb);
7345         vport->unreg_vpi_cmpl = VPORT_ERROR;
7346
7347         /* Trigger the release of the ndlp after logo */
7348         lpfc_nlp_put(ndlp);
7349
7350         /* NPIV LOGO completes to NPort <nlp_DID> */
7351         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7352                          "2928 NPIV LOGO completes to NPort x%x "
7353                          "Data: x%x x%x x%x x%x\n",
7354                          ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
7355                          irsp->ulpTimeout, vport->num_disc_nodes);
7356
7357         if (irsp->ulpStatus == IOSTAT_SUCCESS) {
7358                 spin_lock_irq(shost->host_lock);
7359                 vport->fc_flag &= ~FC_FABRIC;
7360                 spin_unlock_irq(shost->host_lock);
7361         }
7362 }
7363
7364 /**
7365  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
7366  * @vport: pointer to a virtual N_Port data structure.
7367  * @ndlp: pointer to a node-list data structure.
7368  *
7369  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
7370  *
7371  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7372  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7373  * will be stored into the context1 field of the IOCB for the completion
7374  * callback function to the LOGO ELS command.
7375  *
7376  * Return codes
7377  *   0 - Successfully issued logo off the @vport
7378  *   1 - Failed to issue logo off the @vport
7379  **/
7380 int
7381 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
7382 {
7383         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7384         struct lpfc_hba  *phba = vport->phba;
7385         IOCB_t *icmd;
7386         struct lpfc_iocbq *elsiocb;
7387         uint8_t *pcmd;
7388         uint16_t cmdsize;
7389
7390         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
7391         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
7392                                      ELS_CMD_LOGO);
7393         if (!elsiocb)
7394                 return 1;
7395
7396         icmd = &elsiocb->iocb;
7397         pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7398         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
7399         pcmd += sizeof(uint32_t);
7400
7401         /* Fill in LOGO payload */
7402         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
7403         pcmd += sizeof(uint32_t);
7404         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
7405
7406         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7407                 "Issue LOGO npiv  did:x%x flg:x%x",
7408                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
7409
7410         elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
7411         spin_lock_irq(shost->host_lock);
7412         ndlp->nlp_flag |= NLP_LOGO_SND;
7413         spin_unlock_irq(shost->host_lock);
7414         if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7415             IOCB_ERROR) {
7416                 spin_lock_irq(shost->host_lock);
7417                 ndlp->nlp_flag &= ~NLP_LOGO_SND;
7418                 spin_unlock_irq(shost->host_lock);
7419                 lpfc_els_free_iocb(phba, elsiocb);
7420                 return 1;
7421         }
7422         return 0;
7423 }
7424
7425 /**
7426  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
7427  * @ptr: holder for the timer function associated data.
7428  *
7429  * This routine is invoked by the fabric iocb block timer after
7430  * timeout. It posts the fabric iocb block timeout event by setting the
7431  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
7432  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
7433  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
7434  * posted event WORKER_FABRIC_BLOCK_TMO.
7435  **/
7436 void
7437 lpfc_fabric_block_timeout(unsigned long ptr)
7438 {
7439         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
7440         unsigned long iflags;
7441         uint32_t tmo_posted;
7442
7443         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
7444         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
7445         if (!tmo_posted)
7446                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
7447         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
7448
7449         if (!tmo_posted)
7450                 lpfc_worker_wake_up(phba);
7451         return;
7452 }
7453
7454 /**
7455  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
7456  * @phba: pointer to lpfc hba data structure.
7457  *
7458  * This routine issues one fabric iocb from the driver internal list to
7459  * the HBA. It first checks whether it's ready to issue one fabric iocb to
7460  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
7461  * remove one pending fabric iocb from the driver internal list and invokes
7462  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
7463  **/
7464 static void
7465 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
7466 {
7467         struct lpfc_iocbq *iocb;
7468         unsigned long iflags;
7469         int ret;
7470         IOCB_t *cmd;
7471
7472 repeat:
7473         iocb = NULL;
7474         spin_lock_irqsave(&phba->hbalock, iflags);
7475         /* Post any pending iocb to the SLI layer */
7476         if (atomic_read(&phba->fabric_iocb_count) == 0) {
7477                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
7478                                  list);
7479                 if (iocb)
7480                         /* Increment fabric iocb count to hold the position */
7481                         atomic_inc(&phba->fabric_iocb_count);
7482         }
7483         spin_unlock_irqrestore(&phba->hbalock, iflags);
7484         if (iocb) {
7485                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7486                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7487                 iocb->iocb_flag |= LPFC_IO_FABRIC;
7488
7489                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7490                         "Fabric sched1:   ste:x%x",
7491                         iocb->vport->port_state, 0, 0);
7492
7493                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
7494
7495                 if (ret == IOCB_ERROR) {
7496                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7497                         iocb->fabric_iocb_cmpl = NULL;
7498                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7499                         cmd = &iocb->iocb;
7500                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
7501                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
7502                         iocb->iocb_cmpl(phba, iocb, iocb);
7503
7504                         atomic_dec(&phba->fabric_iocb_count);
7505                         goto repeat;
7506                 }
7507         }
7508
7509         return;
7510 }
7511
7512 /**
7513  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
7514  * @phba: pointer to lpfc hba data structure.
7515  *
7516  * This routine unblocks the  issuing fabric iocb command. The function
7517  * will clear the fabric iocb block bit and then invoke the routine
7518  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
7519  * from the driver internal fabric iocb list.
7520  **/
7521 void
7522 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
7523 {
7524         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7525
7526         lpfc_resume_fabric_iocbs(phba);
7527         return;
7528 }
7529
7530 /**
7531  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
7532  * @phba: pointer to lpfc hba data structure.
7533  *
7534  * This routine blocks the issuing fabric iocb for a specified amount of
7535  * time (currently 100 ms). This is done by set the fabric iocb block bit
7536  * and set up a timeout timer for 100ms. When the block bit is set, no more
7537  * fabric iocb will be issued out of the HBA.
7538  **/
7539 static void
7540 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
7541 {
7542         int blocked;
7543
7544         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7545         /* Start a timer to unblock fabric iocbs after 100ms */
7546         if (!blocked)
7547                 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
7548
7549         return;
7550 }
7551
7552 /**
7553  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
7554  * @phba: pointer to lpfc hba data structure.
7555  * @cmdiocb: pointer to lpfc command iocb data structure.
7556  * @rspiocb: pointer to lpfc response iocb data structure.
7557  *
7558  * This routine is the callback function that is put to the fabric iocb's
7559  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
7560  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
7561  * function first restores and invokes the original iocb's callback function
7562  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
7563  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
7564  **/
7565 static void
7566 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7567         struct lpfc_iocbq *rspiocb)
7568 {
7569         struct ls_rjt stat;
7570
7571         if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
7572                 BUG();
7573
7574         switch (rspiocb->iocb.ulpStatus) {
7575                 case IOSTAT_NPORT_RJT:
7576                 case IOSTAT_FABRIC_RJT:
7577                         if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
7578                                 lpfc_block_fabric_iocbs(phba);
7579                         }
7580                         break;
7581
7582                 case IOSTAT_NPORT_BSY:
7583                 case IOSTAT_FABRIC_BSY:
7584                         lpfc_block_fabric_iocbs(phba);
7585                         break;
7586
7587                 case IOSTAT_LS_RJT:
7588                         stat.un.lsRjtError =
7589                                 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
7590                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
7591                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
7592                                 lpfc_block_fabric_iocbs(phba);
7593                         break;
7594         }
7595
7596         if (atomic_read(&phba->fabric_iocb_count) == 0)
7597                 BUG();
7598
7599         cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
7600         cmdiocb->fabric_iocb_cmpl = NULL;
7601         cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
7602         cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
7603
7604         atomic_dec(&phba->fabric_iocb_count);
7605         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
7606                 /* Post any pending iocbs to HBA */
7607                 lpfc_resume_fabric_iocbs(phba);
7608         }
7609 }
7610
7611 /**
7612  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
7613  * @phba: pointer to lpfc hba data structure.
7614  * @iocb: pointer to lpfc command iocb data structure.
7615  *
7616  * This routine is used as the top-level API for issuing a fabric iocb command
7617  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
7618  * function makes sure that only one fabric bound iocb will be outstanding at
7619  * any given time. As such, this function will first check to see whether there
7620  * is already an outstanding fabric iocb on the wire. If so, it will put the
7621  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
7622  * issued later. Otherwise, it will issue the iocb on the wire and update the
7623  * fabric iocb count it indicate that there is one fabric iocb on the wire.
7624  *
7625  * Note, this implementation has a potential sending out fabric IOCBs out of
7626  * order. The problem is caused by the construction of the "ready" boolen does
7627  * not include the condition that the internal fabric IOCB list is empty. As
7628  * such, it is possible a fabric IOCB issued by this routine might be "jump"
7629  * ahead of the fabric IOCBs in the internal list.
7630  *
7631  * Return code
7632  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
7633  *   IOCB_ERROR - failed to issue fabric iocb
7634  **/
7635 static int
7636 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
7637 {
7638         unsigned long iflags;
7639         int ready;
7640         int ret;
7641
7642         if (atomic_read(&phba->fabric_iocb_count) > 1)
7643                 BUG();
7644
7645         spin_lock_irqsave(&phba->hbalock, iflags);
7646         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
7647                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7648
7649         if (ready)
7650                 /* Increment fabric iocb count to hold the position */
7651                 atomic_inc(&phba->fabric_iocb_count);
7652         spin_unlock_irqrestore(&phba->hbalock, iflags);
7653         if (ready) {
7654                 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7655                 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7656                 iocb->iocb_flag |= LPFC_IO_FABRIC;
7657
7658                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7659                         "Fabric sched2:   ste:x%x",
7660                         iocb->vport->port_state, 0, 0);
7661
7662                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
7663
7664                 if (ret == IOCB_ERROR) {
7665                         iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7666                         iocb->fabric_iocb_cmpl = NULL;
7667                         iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7668                         atomic_dec(&phba->fabric_iocb_count);
7669                 }
7670         } else {
7671                 spin_lock_irqsave(&phba->hbalock, iflags);
7672                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
7673                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7674                 ret = IOCB_SUCCESS;
7675         }
7676         return ret;
7677 }
7678
7679 /**
7680  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
7681  * @vport: pointer to a virtual N_Port data structure.
7682  *
7683  * This routine aborts all the IOCBs associated with a @vport from the
7684  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
7685  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
7686  * list, removes each IOCB associated with the @vport off the list, set the
7687  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
7688  * associated with the IOCB.
7689  **/
7690 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
7691 {
7692         LIST_HEAD(completions);
7693         struct lpfc_hba  *phba = vport->phba;
7694         struct lpfc_iocbq *tmp_iocb, *piocb;
7695
7696         spin_lock_irq(&phba->hbalock);
7697         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
7698                                  list) {
7699
7700                 if (piocb->vport != vport)
7701                         continue;
7702
7703                 list_move_tail(&piocb->list, &completions);
7704         }
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_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
7714  * @ndlp: pointer to a node-list data structure.
7715  *
7716  * This routine aborts all the IOCBs associated with an @ndlp from the
7717  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
7718  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
7719  * list, removes each IOCB associated with the @ndlp off the list, set the
7720  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
7721  * associated with the IOCB.
7722  **/
7723 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
7724 {
7725         LIST_HEAD(completions);
7726         struct lpfc_hba  *phba = ndlp->phba;
7727         struct lpfc_iocbq *tmp_iocb, *piocb;
7728         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7729
7730         spin_lock_irq(&phba->hbalock);
7731         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
7732                                  list) {
7733                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
7734
7735                         list_move_tail(&piocb->list, &completions);
7736                 }
7737         }
7738         spin_unlock_irq(&phba->hbalock);
7739
7740         /* Cancel all the IOCBs from the completions list */
7741         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7742                               IOERR_SLI_ABORTED);
7743 }
7744
7745 /**
7746  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
7747  * @phba: pointer to lpfc hba data structure.
7748  *
7749  * This routine aborts all the IOCBs currently on the driver internal
7750  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
7751  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
7752  * list, removes IOCBs off the list, set the status feild to
7753  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
7754  * the IOCB.
7755  **/
7756 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
7757 {
7758         LIST_HEAD(completions);
7759
7760         spin_lock_irq(&phba->hbalock);
7761         list_splice_init(&phba->fabric_iocb_list, &completions);
7762         spin_unlock_irq(&phba->hbalock);
7763
7764         /* Cancel all the IOCBs from the completions list */
7765         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7766                               IOERR_SLI_ABORTED);
7767 }
7768
7769 /**
7770  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
7771  * @vport: pointer to lpfc vport data structure.
7772  *
7773  * This routine is invoked by the vport cleanup for deletions and the cleanup
7774  * for an ndlp on removal.
7775  **/
7776 void
7777 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
7778 {
7779         struct lpfc_hba *phba = vport->phba;
7780         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7781         unsigned long iflag = 0;
7782
7783         spin_lock_irqsave(&phba->hbalock, iflag);
7784         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
7785         list_for_each_entry_safe(sglq_entry, sglq_next,
7786                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
7787                 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
7788                         sglq_entry->ndlp = NULL;
7789         }
7790         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7791         spin_unlock_irqrestore(&phba->hbalock, iflag);
7792         return;
7793 }
7794
7795 /**
7796  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
7797  * @phba: pointer to lpfc hba data structure.
7798  * @axri: pointer to the els xri abort wcqe structure.
7799  *
7800  * This routine is invoked by the worker thread to process a SLI4 slow-path
7801  * ELS aborted xri.
7802  **/
7803 void
7804 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
7805                           struct sli4_wcqe_xri_aborted *axri)
7806 {
7807         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
7808         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
7809         uint16_t lxri = 0;
7810
7811         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7812         unsigned long iflag = 0;
7813         struct lpfc_nodelist *ndlp;
7814         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7815
7816         spin_lock_irqsave(&phba->hbalock, iflag);
7817         spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
7818         list_for_each_entry_safe(sglq_entry, sglq_next,
7819                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
7820                 if (sglq_entry->sli4_xritag == xri) {
7821                         list_del(&sglq_entry->list);
7822                         ndlp = sglq_entry->ndlp;
7823                         sglq_entry->ndlp = NULL;
7824                         list_add_tail(&sglq_entry->list,
7825                                 &phba->sli4_hba.lpfc_sgl_list);
7826                         sglq_entry->state = SGL_FREED;
7827                         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7828                         spin_unlock_irqrestore(&phba->hbalock, iflag);
7829                         lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
7830
7831                         /* Check if TXQ queue needs to be serviced */
7832                         if (pring->txq_cnt)
7833                                 lpfc_worker_wake_up(phba);
7834                         return;
7835                 }
7836         }
7837         spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7838         lxri = lpfc_sli4_xri_inrange(phba, xri);
7839         if (lxri == NO_XRI) {
7840                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7841                 return;
7842         }
7843         sglq_entry = __lpfc_get_active_sglq(phba, lxri);
7844         if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
7845                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7846                 return;
7847         }
7848         sglq_entry->state = SGL_XRI_ABORTED;
7849         spin_unlock_irqrestore(&phba->hbalock, iflag);
7850         return;
7851 }