]> Git Repo - J-linux.git/blob - drivers/scsi/lpfc/lpfc_els.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-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) 2017-2024 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 /* See Fibre Channel protocol T11 FC-LS for details */
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <uapi/scsi/fc/fc_fs.h>
35 #include <uapi/scsi/fc/fc_els.h>
36
37 #include "lpfc_hw4.h"
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_sli4.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_crtn.h"
47 #include "lpfc_vport.h"
48 #include "lpfc_debugfs.h"
49
50 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
51                           struct lpfc_iocbq *);
52 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
53                         struct lpfc_iocbq *);
54 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
55 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
56                                 struct lpfc_nodelist *ndlp, uint8_t retry);
57 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
58                                   struct lpfc_iocbq *iocb);
59 static void lpfc_cmpl_els_edc(struct lpfc_hba *phba,
60                               struct lpfc_iocbq *cmdiocb,
61                               struct lpfc_iocbq *rspiocb);
62 static void lpfc_cmpl_els_uvem(struct lpfc_hba *, struct lpfc_iocbq *,
63                                struct lpfc_iocbq *);
64
65 static int lpfc_max_els_tries = 3;
66
67 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport);
68 static void lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max);
69 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid);
70
71 /**
72  * lpfc_els_chk_latt - Check host link attention event for a vport
73  * @vport: pointer to a host virtual N_Port data structure.
74  *
75  * This routine checks whether there is an outstanding host link
76  * attention event during the discovery process with the @vport. It is done
77  * by reading the HBA's Host Attention (HA) register. If there is any host
78  * link attention events during this @vport's discovery process, the @vport
79  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
80  * be issued if the link state is not already in host link cleared state,
81  * and a return code shall indicate whether the host link attention event
82  * had happened.
83  *
84  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
85  * state in LPFC_VPORT_READY, the request for checking host link attention
86  * event will be ignored and a return code shall indicate no host link
87  * attention event had happened.
88  *
89  * Return codes
90  *   0 - no host link attention event happened
91  *   1 - host link attention event happened
92  **/
93 int
94 lpfc_els_chk_latt(struct lpfc_vport *vport)
95 {
96         struct lpfc_hba  *phba = vport->phba;
97         uint32_t ha_copy;
98
99         if (vport->port_state >= LPFC_VPORT_READY ||
100             phba->link_state == LPFC_LINK_DOWN ||
101             phba->sli_rev > LPFC_SLI_REV3)
102                 return 0;
103
104         /* Read the HBA Host Attention Register */
105         if (lpfc_readl(phba->HAregaddr, &ha_copy))
106                 return 1;
107
108         if (!(ha_copy & HA_LATT))
109                 return 0;
110
111         /* Pending Link Event during Discovery */
112         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
113                          "0237 Pending Link Event during "
114                          "Discovery: State x%x\n",
115                          phba->pport->port_state);
116
117         /* CLEAR_LA should re-enable link attention events and
118          * we should then immediately take a LATT event. The
119          * LATT processing should call lpfc_linkdown() which
120          * will cleanup any left over in-progress discovery
121          * events.
122          */
123         set_bit(FC_ABORT_DISCOVERY, &vport->fc_flag);
124
125         if (phba->link_state != LPFC_CLEAR_LA)
126                 lpfc_issue_clear_la(phba, vport);
127
128         return 1;
129 }
130
131 static bool lpfc_is_els_acc_rsp(struct lpfc_dmabuf *buf)
132 {
133         struct fc_els_ls_acc *rsp = buf->virt;
134
135         if (rsp && rsp->la_cmd == ELS_LS_ACC)
136                 return true;
137         return false;
138 }
139
140 /**
141  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
142  * @vport: pointer to a host virtual N_Port data structure.
143  * @expect_rsp: flag indicating whether response is expected.
144  * @cmd_size: size of the ELS command.
145  * @retry: number of retries to the command when it fails.
146  * @ndlp: pointer to a node-list data structure.
147  * @did: destination identifier.
148  * @elscmd: the ELS command code.
149  *
150  * This routine is used for allocating a lpfc-IOCB data structure from
151  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
152  * passed into the routine for discovery state machine to issue an Extended
153  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
154  * and preparation routine that is used by all the discovery state machine
155  * routines and the ELS command-specific fields will be later set up by
156  * the individual discovery machine routines after calling this routine
157  * allocating and preparing a generic IOCB data structure. It fills in the
158  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
159  * payload and response payload (if expected). The reference count on the
160  * ndlp is incremented by 1 and the reference to the ndlp is put into
161  * ndlp of the IOCB data structure for this IOCB to hold the ndlp
162  * reference for the command's callback function to access later.
163  *
164  * Return code
165  *   Pointer to the newly allocated/prepared els iocb data structure
166  *   NULL - when els iocb data structure allocation/preparation failed
167  **/
168 struct lpfc_iocbq *
169 lpfc_prep_els_iocb(struct lpfc_vport *vport, u8 expect_rsp,
170                    u16 cmd_size, u8 retry,
171                    struct lpfc_nodelist *ndlp, u32 did,
172                    u32 elscmd)
173 {
174         struct lpfc_hba  *phba = vport->phba;
175         struct lpfc_iocbq *elsiocb;
176         struct lpfc_dmabuf *pcmd, *prsp, *pbuflist, *bmp;
177         struct ulp_bde64_le *bpl;
178         u32 timeout = 0;
179
180         if (!lpfc_is_link_up(phba))
181                 return NULL;
182
183         /* Allocate buffer for  command iocb */
184         elsiocb = lpfc_sli_get_iocbq(phba);
185         if (!elsiocb)
186                 return NULL;
187
188         /*
189          * If this command is for fabric controller and HBA running
190          * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
191          */
192         if (did == Fabric_DID &&
193             test_bit(HBA_FIP_SUPPORT, &phba->hba_flag) &&
194             (elscmd == ELS_CMD_FLOGI ||
195              elscmd == ELS_CMD_FDISC ||
196              elscmd == ELS_CMD_LOGO))
197                 switch (elscmd) {
198                 case ELS_CMD_FLOGI:
199                         elsiocb->cmd_flag |=
200                                 ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
201                                  & LPFC_FIP_ELS_ID_MASK);
202                         break;
203                 case ELS_CMD_FDISC:
204                         elsiocb->cmd_flag |=
205                                 ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
206                                  & LPFC_FIP_ELS_ID_MASK);
207                         break;
208                 case ELS_CMD_LOGO:
209                         elsiocb->cmd_flag |=
210                                 ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
211                                  & LPFC_FIP_ELS_ID_MASK);
212                         break;
213                 }
214         else
215                 elsiocb->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
216
217         /* fill in BDEs for command */
218         /* Allocate buffer for command payload */
219         pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
220         if (pcmd)
221                 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
222         if (!pcmd || !pcmd->virt)
223                 goto els_iocb_free_pcmb_exit;
224
225         INIT_LIST_HEAD(&pcmd->list);
226
227         /* Allocate buffer for response payload */
228         if (expect_rsp) {
229                 prsp = kmalloc(sizeof(*prsp), GFP_KERNEL);
230                 if (prsp)
231                         prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
232                                                      &prsp->phys);
233                 if (!prsp || !prsp->virt)
234                         goto els_iocb_free_prsp_exit;
235                 INIT_LIST_HEAD(&prsp->list);
236         } else {
237                 prsp = NULL;
238         }
239
240         /* Allocate buffer for Buffer ptr list */
241         pbuflist = kmalloc(sizeof(*pbuflist), GFP_KERNEL);
242         if (pbuflist)
243                 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
244                                                  &pbuflist->phys);
245         if (!pbuflist || !pbuflist->virt)
246                 goto els_iocb_free_pbuf_exit;
247
248         INIT_LIST_HEAD(&pbuflist->list);
249
250         if (expect_rsp) {
251                 switch (elscmd) {
252                 case ELS_CMD_FLOGI:
253                         timeout = FF_DEF_RATOV * 2;
254                         break;
255                 case ELS_CMD_LOGO:
256                         timeout = phba->fc_ratov;
257                         break;
258                 default:
259                         timeout = phba->fc_ratov * 2;
260                 }
261
262                 /* Fill SGE for the num bde count */
263                 elsiocb->num_bdes = 2;
264         }
265
266         if (phba->sli_rev == LPFC_SLI_REV4)
267                 bmp = pcmd;
268         else
269                 bmp = pbuflist;
270
271         lpfc_sli_prep_els_req_rsp(phba, elsiocb, vport, bmp, cmd_size, did,
272                                   elscmd, timeout, expect_rsp);
273
274         bpl = (struct ulp_bde64_le *)pbuflist->virt;
275         bpl->addr_low = cpu_to_le32(putPaddrLow(pcmd->phys));
276         bpl->addr_high = cpu_to_le32(putPaddrHigh(pcmd->phys));
277         bpl->type_size = cpu_to_le32(cmd_size);
278         bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
279
280         if (expect_rsp) {
281                 bpl++;
282                 bpl->addr_low = cpu_to_le32(putPaddrLow(prsp->phys));
283                 bpl->addr_high = cpu_to_le32(putPaddrHigh(prsp->phys));
284                 bpl->type_size = cpu_to_le32(FCELSSIZE);
285                 bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
286         }
287
288         elsiocb->cmd_dmabuf = pcmd;
289         elsiocb->bpl_dmabuf = pbuflist;
290         elsiocb->retry = retry;
291         elsiocb->vport = vport;
292         elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
293
294         if (prsp)
295                 list_add(&prsp->list, &pcmd->list);
296         if (expect_rsp) {
297                 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
298                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
299                                  "0116 Xmit ELS command x%x to remote "
300                                  "NPORT x%x I/O tag: x%x, port state:x%x "
301                                  "rpi x%x fc_flag:x%lx\n",
302                                  elscmd, did, elsiocb->iotag,
303                                  vport->port_state, ndlp->nlp_rpi,
304                                  vport->fc_flag);
305         } else {
306                 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
307                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
308                                  "0117 Xmit ELS response x%x to remote "
309                                  "NPORT x%x I/O tag: x%x, size: x%x "
310                                  "port_state x%x  rpi x%x fc_flag x%lx\n",
311                                  elscmd, ndlp->nlp_DID, elsiocb->iotag,
312                                  cmd_size, vport->port_state,
313                                  ndlp->nlp_rpi, vport->fc_flag);
314         }
315
316         return elsiocb;
317
318 els_iocb_free_pbuf_exit:
319         if (expect_rsp)
320                 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
321         kfree(pbuflist);
322
323 els_iocb_free_prsp_exit:
324         lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
325         kfree(prsp);
326
327 els_iocb_free_pcmb_exit:
328         kfree(pcmd);
329         lpfc_sli_release_iocbq(phba, elsiocb);
330         return NULL;
331 }
332
333 /**
334  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
335  * @vport: pointer to a host virtual N_Port data structure.
336  *
337  * This routine issues a fabric registration login for a @vport. An
338  * active ndlp node with Fabric_DID must already exist for this @vport.
339  * The routine invokes two mailbox commands to carry out fabric registration
340  * login through the HBA firmware: the first mailbox command requests the
341  * HBA to perform link configuration for the @vport; and the second mailbox
342  * command requests the HBA to perform the actual fabric registration login
343  * with the @vport.
344  *
345  * Return code
346  *   0 - successfully issued fabric registration login for @vport
347  *   -ENXIO -- failed to issue fabric registration login for @vport
348  **/
349 int
350 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
351 {
352         struct lpfc_hba  *phba = vport->phba;
353         LPFC_MBOXQ_t *mbox;
354         struct lpfc_nodelist *ndlp;
355         struct serv_parm *sp;
356         int rc;
357         int err = 0;
358
359         sp = &phba->fc_fabparam;
360         ndlp = lpfc_findnode_did(vport, Fabric_DID);
361         if (!ndlp) {
362                 err = 1;
363                 goto fail;
364         }
365
366         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
367         if (!mbox) {
368                 err = 2;
369                 goto fail;
370         }
371
372         vport->port_state = LPFC_FABRIC_CFG_LINK;
373         lpfc_config_link(phba, mbox);
374         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
375         mbox->vport = vport;
376
377         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
378         if (rc == MBX_NOT_FINISHED) {
379                 err = 3;
380                 goto fail_free_mbox;
381         }
382
383         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
384         if (!mbox) {
385                 err = 4;
386                 goto fail;
387         }
388         rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
389                           ndlp->nlp_rpi);
390         if (rc) {
391                 err = 5;
392                 goto fail_free_mbox;
393         }
394
395         mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
396         mbox->vport = vport;
397         /* increment the reference count on ndlp to hold reference
398          * for the callback routine.
399          */
400         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
401         if (!mbox->ctx_ndlp) {
402                 err = 6;
403                 goto fail_free_mbox;
404         }
405
406         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
407         if (rc == MBX_NOT_FINISHED) {
408                 err = 7;
409                 goto fail_issue_reg_login;
410         }
411
412         return 0;
413
414 fail_issue_reg_login:
415         /* decrement the reference count on ndlp just incremented
416          * for the failed mbox command.
417          */
418         lpfc_nlp_put(ndlp);
419 fail_free_mbox:
420         lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
421 fail:
422         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
423         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
424                          "0249 Cannot issue Register Fabric login: Err %d\n",
425                          err);
426         return -ENXIO;
427 }
428
429 /**
430  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
431  * @vport: pointer to a host virtual N_Port data structure.
432  *
433  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
434  * the @vport. This mailbox command is necessary for SLI4 port only.
435  *
436  * Return code
437  *   0 - successfully issued REG_VFI for @vport
438  *   A failure code otherwise.
439  **/
440 int
441 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
442 {
443         struct lpfc_hba  *phba = vport->phba;
444         LPFC_MBOXQ_t *mboxq = NULL;
445         struct lpfc_nodelist *ndlp;
446         struct lpfc_dmabuf *dmabuf = NULL;
447         int rc = 0;
448
449         /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
450         if ((phba->sli_rev == LPFC_SLI_REV4) &&
451             !(phba->link_flag & LS_LOOPBACK_MODE) &&
452             !test_bit(FC_PT2PT, &vport->fc_flag)) {
453                 ndlp = lpfc_findnode_did(vport, Fabric_DID);
454                 if (!ndlp) {
455                         rc = -ENODEV;
456                         goto fail;
457                 }
458         }
459
460         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
461         if (!mboxq) {
462                 rc = -ENOMEM;
463                 goto fail;
464         }
465
466         /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
467         if (test_bit(FC_FABRIC, &vport->fc_flag) ||
468             test_bit(FC_PT2PT, &vport->fc_flag)) {
469                 rc = lpfc_mbox_rsrc_prep(phba, mboxq);
470                 if (rc) {
471                         rc = -ENOMEM;
472                         goto fail_mbox;
473                 }
474                 dmabuf = mboxq->ctx_buf;
475                 memcpy(dmabuf->virt, &phba->fc_fabparam,
476                        sizeof(struct serv_parm));
477         }
478
479         vport->port_state = LPFC_FABRIC_CFG_LINK;
480         if (dmabuf) {
481                 lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
482                 /* lpfc_reg_vfi memsets the mailbox.  Restore the ctx_buf. */
483                 mboxq->ctx_buf = dmabuf;
484         } else {
485                 lpfc_reg_vfi(mboxq, vport, 0);
486         }
487
488         mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
489         mboxq->vport = vport;
490         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
491         if (rc == MBX_NOT_FINISHED) {
492                 rc = -ENXIO;
493                 goto fail_mbox;
494         }
495         return 0;
496
497 fail_mbox:
498         lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
499 fail:
500         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
501         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
502                          "0289 Issue Register VFI failed: Err %d\n", rc);
503         return rc;
504 }
505
506 /**
507  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
508  * @vport: pointer to a host virtual N_Port data structure.
509  *
510  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
511  * the @vport. This mailbox command is necessary for SLI4 port only.
512  *
513  * Return code
514  *   0 - successfully issued REG_VFI for @vport
515  *   A failure code otherwise.
516  **/
517 int
518 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
519 {
520         struct lpfc_hba *phba = vport->phba;
521         LPFC_MBOXQ_t *mboxq;
522         int rc;
523
524         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
525         if (!mboxq) {
526                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
527                                 "2556 UNREG_VFI mbox allocation failed"
528                                 "HBA state x%x\n", phba->pport->port_state);
529                 return -ENOMEM;
530         }
531
532         lpfc_unreg_vfi(mboxq, vport);
533         mboxq->vport = vport;
534         mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
535
536         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
537         if (rc == MBX_NOT_FINISHED) {
538                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
539                                 "2557 UNREG_VFI issue mbox failed rc x%x "
540                                 "HBA state x%x\n",
541                                 rc, phba->pport->port_state);
542                 mempool_free(mboxq, phba->mbox_mem_pool);
543                 return -EIO;
544         }
545
546         clear_bit(FC_VFI_REGISTERED, &vport->fc_flag);
547         return 0;
548 }
549
550 /**
551  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
552  * @vport: pointer to a host virtual N_Port data structure.
553  * @sp: pointer to service parameter data structure.
554  *
555  * This routine is called from FLOGI/FDISC completion handler functions.
556  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
557  * node nodename is changed in the completion service parameter else return
558  * 0. This function also set flag in the vport data structure to delay
559  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
560  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
561  * node nodename is changed in the completion service parameter.
562  *
563  * Return code
564  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
565  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
566  *
567  **/
568 static uint8_t
569 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
570                 struct serv_parm *sp)
571 {
572         struct lpfc_hba *phba = vport->phba;
573         uint8_t fabric_param_changed = 0;
574
575         if ((vport->fc_prevDID != vport->fc_myDID) ||
576                 memcmp(&vport->fabric_portname, &sp->portName,
577                         sizeof(struct lpfc_name)) ||
578                 memcmp(&vport->fabric_nodename, &sp->nodeName,
579                         sizeof(struct lpfc_name)) ||
580                 (vport->vport_flag & FAWWPN_PARAM_CHG)) {
581                 fabric_param_changed = 1;
582                 vport->vport_flag &= ~FAWWPN_PARAM_CHG;
583         }
584         /*
585          * Word 1 Bit 31 in common service parameter is overloaded.
586          * Word 1 Bit 31 in FLOGI request is multiple NPort request
587          * Word 1 Bit 31 in FLOGI response is clean address bit
588          *
589          * If fabric parameter is changed and clean address bit is
590          * cleared delay nport discovery if
591          * - vport->fc_prevDID != 0 (not initial discovery) OR
592          * - lpfc_delay_discovery module parameter is set.
593          */
594         if (fabric_param_changed && !sp->cmn.clean_address_bit &&
595             (vport->fc_prevDID || phba->cfg_delay_discovery))
596                 set_bit(FC_DISC_DELAYED, &vport->fc_flag);
597
598         return fabric_param_changed;
599 }
600
601
602 /**
603  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
604  * @vport: pointer to a host virtual N_Port data structure.
605  * @ndlp: pointer to a node-list data structure.
606  * @sp: pointer to service parameter data structure.
607  * @ulp_word4: command response value
608  *
609  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
610  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
611  * port in a fabric topology. It properly sets up the parameters to the @ndlp
612  * from the IOCB response. It also check the newly assigned N_Port ID to the
613  * @vport against the previously assigned N_Port ID. If it is different from
614  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
615  * is invoked on all the remaining nodes with the @vport to unregister the
616  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
617  * is invoked to register login to the fabric.
618  *
619  * Return code
620  *   0 - Success (currently, always return 0)
621  **/
622 static int
623 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
624                            struct serv_parm *sp, uint32_t ulp_word4)
625 {
626         struct lpfc_hba  *phba = vport->phba;
627         struct lpfc_nodelist *np;
628         struct lpfc_nodelist *next_np;
629         uint8_t fabric_param_changed;
630
631         set_bit(FC_FABRIC, &vport->fc_flag);
632
633         phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
634         if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
635                 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
636
637         phba->fc_edtovResol = sp->cmn.edtovResolution;
638         phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
639
640         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
641                 set_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
642
643         vport->fc_myDID = ulp_word4 & Mask_DID;
644         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
645         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
646         ndlp->nlp_class_sup = 0;
647         if (sp->cls1.classValid)
648                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
649         if (sp->cls2.classValid)
650                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
651         if (sp->cls3.classValid)
652                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
653         if (sp->cls4.classValid)
654                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
655         ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
656                                 sp->cmn.bbRcvSizeLsb;
657
658         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
659         if (fabric_param_changed) {
660                 /* Reset FDMI attribute masks based on config parameter */
661                 if (phba->cfg_enable_SmartSAN ||
662                     (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
663                         /* Setup appropriate attribute masks */
664                         vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
665                         if (phba->cfg_enable_SmartSAN)
666                                 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
667                         else
668                                 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
669                 } else {
670                         vport->fdmi_hba_mask = 0;
671                         vport->fdmi_port_mask = 0;
672                 }
673
674         }
675         memcpy(&vport->fabric_portname, &sp->portName,
676                         sizeof(struct lpfc_name));
677         memcpy(&vport->fabric_nodename, &sp->nodeName,
678                         sizeof(struct lpfc_name));
679         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
680
681         if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
682                 if (sp->cmn.response_multiple_NPort) {
683                         lpfc_printf_vlog(vport, KERN_WARNING,
684                                          LOG_ELS | LOG_VPORT,
685                                          "1816 FLOGI NPIV supported, "
686                                          "response data 0x%x\n",
687                                          sp->cmn.response_multiple_NPort);
688                         spin_lock_irq(&phba->hbalock);
689                         phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
690                         spin_unlock_irq(&phba->hbalock);
691                 } else {
692                         /* Because we asked f/w for NPIV it still expects us
693                         to call reg_vnpid at least for the physical host */
694                         lpfc_printf_vlog(vport, KERN_WARNING,
695                                          LOG_ELS | LOG_VPORT,
696                                          "1817 Fabric does not support NPIV "
697                                          "- configuring single port mode.\n");
698                         spin_lock_irq(&phba->hbalock);
699                         phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
700                         spin_unlock_irq(&phba->hbalock);
701                 }
702         }
703
704         /*
705          * For FC we need to do some special processing because of the SLI
706          * Port's default settings of the Common Service Parameters.
707          */
708         if ((phba->sli_rev == LPFC_SLI_REV4) &&
709             (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
710                 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
711                 if (fabric_param_changed)
712                         lpfc_unregister_fcf_prep(phba);
713
714                 /* This should just update the VFI CSPs*/
715                 if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
716                         lpfc_issue_reg_vfi(vport);
717         }
718
719         if (fabric_param_changed &&
720                 !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
721
722                 /* If our NportID changed, we need to ensure all
723                  * remaining NPORTs get unreg_login'ed.
724                  */
725                 list_for_each_entry_safe(np, next_np,
726                                         &vport->fc_nodes, nlp_listp) {
727                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
728                             !test_bit(NLP_NPR_ADISC, &np->nlp_flag))
729                                 continue;
730                         clear_bit(NLP_NPR_ADISC, &np->nlp_flag);
731                         lpfc_unreg_rpi(vport, np);
732                 }
733                 lpfc_cleanup_pending_mbox(vport);
734
735                 if (phba->sli_rev == LPFC_SLI_REV4) {
736                         lpfc_sli4_unreg_all_rpis(vport);
737                         lpfc_mbx_unreg_vpi(vport);
738                         set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
739                 }
740
741                 /*
742                  * For SLI3 and SLI4, the VPI needs to be reregistered in
743                  * response to this fabric parameter change event.
744                  */
745                 set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
746         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
747                    !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
748                         /*
749                          * Driver needs to re-reg VPI in order for f/w
750                          * to update the MAC address.
751                          */
752                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
753                         lpfc_register_new_vport(phba, vport, ndlp);
754                         return 0;
755         }
756
757         if (phba->sli_rev < LPFC_SLI_REV4) {
758                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
759                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
760                     test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
761                         lpfc_register_new_vport(phba, vport, ndlp);
762                 else
763                         lpfc_issue_fabric_reglogin(vport);
764         } else {
765                 ndlp->nlp_type |= NLP_FABRIC;
766                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
767                 if ((!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) &&
768                     (vport->vpi_state & LPFC_VPI_REGISTERED)) {
769                         lpfc_start_fdiscs(phba);
770                         lpfc_do_scr_ns_plogi(phba, vport);
771                 } else if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
772                         lpfc_issue_init_vpi(vport);
773                 else {
774                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
775                                         "3135 Need register VFI: (x%x/%x)\n",
776                                         vport->fc_prevDID, vport->fc_myDID);
777                         lpfc_issue_reg_vfi(vport);
778                 }
779         }
780         return 0;
781 }
782
783 /**
784  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
785  * @vport: pointer to a host virtual N_Port data structure.
786  * @ndlp: pointer to a node-list data structure.
787  * @sp: pointer to service parameter data structure.
788  *
789  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
790  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
791  * in a point-to-point topology. First, the @vport's N_Port Name is compared
792  * with the received N_Port Name: if the @vport's N_Port Name is greater than
793  * the received N_Port Name lexicographically, this node shall assign local
794  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
795  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
796  * this node shall just wait for the remote node to issue PLOGI and assign
797  * N_Port IDs.
798  *
799  * Return code
800  *   0 - Success
801  *   -ENXIO - Fail
802  **/
803 static int
804 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
805                           struct serv_parm *sp)
806 {
807         struct lpfc_hba  *phba = vport->phba;
808         LPFC_MBOXQ_t *mbox;
809         int rc;
810
811         clear_bit(FC_FABRIC, &vport->fc_flag);
812         clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
813         set_bit(FC_PT2PT, &vport->fc_flag);
814
815         /* If we are pt2pt with another NPort, force NPIV off! */
816         phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
817
818         /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
819         if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
820                 lpfc_unregister_fcf_prep(phba);
821                 clear_bit(FC_VFI_REGISTERED, &vport->fc_flag);
822                 phba->fc_topology_changed = 0;
823         }
824
825         rc = memcmp(&vport->fc_portname, &sp->portName,
826                     sizeof(vport->fc_portname));
827
828         if (rc >= 0) {
829                 /* This side will initiate the PLOGI */
830                 set_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
831
832                 /*
833                  * N_Port ID cannot be 0, set our Id to LocalID
834                  * the other side will be RemoteID.
835                  */
836
837                 /* not equal */
838                 if (rc)
839                         vport->fc_myDID = PT2PT_LocalID;
840
841                 /* If not registered with a transport, decrement ndlp reference
842                  * count indicating that ndlp can be safely released when other
843                  * references are removed.
844                  */
845                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
846                         lpfc_nlp_put(ndlp);
847
848                 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
849                 if (!ndlp) {
850                         /*
851                          * Cannot find existing Fabric ndlp, so allocate a
852                          * new one
853                          */
854                         ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
855                         if (!ndlp)
856                                 goto fail;
857                 }
858
859                 memcpy(&ndlp->nlp_portname, &sp->portName,
860                        sizeof(struct lpfc_name));
861                 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
862                        sizeof(struct lpfc_name));
863                 /* Set state will put ndlp onto node list if not already done */
864                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
865                 set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
866
867                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
868                 if (!mbox)
869                         goto fail;
870
871                 lpfc_config_link(phba, mbox);
872
873                 mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
874                 mbox->vport = vport;
875                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
876                 if (rc == MBX_NOT_FINISHED) {
877                         mempool_free(mbox, phba->mbox_mem_pool);
878                         goto fail;
879                 }
880         } else {
881                 /* This side will wait for the PLOGI. If not registered with
882                  * a transport, decrement node reference count indicating that
883                  * ndlp can be released when other references are removed.
884                  */
885                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
886                         lpfc_nlp_put(ndlp);
887
888                 /* Start discovery - this should just do CLEAR_LA */
889                 lpfc_disc_start(vport);
890         }
891
892         return 0;
893 fail:
894         return -ENXIO;
895 }
896
897 /**
898  * lpfc_cmpl_els_flogi - Completion callback function for flogi
899  * @phba: pointer to lpfc hba data structure.
900  * @cmdiocb: pointer to lpfc command iocb data structure.
901  * @rspiocb: pointer to lpfc response iocb data structure.
902  *
903  * This routine is the top-level completion callback function for issuing
904  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
905  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
906  * retry has been made (either immediately or delayed with lpfc_els_retry()
907  * returning 1), the command IOCB will be released and function returned.
908  * If the retry attempt has been given up (possibly reach the maximum
909  * number of retries), one additional decrement of ndlp reference shall be
910  * invoked before going out after releasing the command IOCB. This will
911  * actually release the remote node (Note, lpfc_els_free_iocb() will also
912  * invoke one decrement of ndlp reference count). If no error reported in
913  * the IOCB status, the command Port ID field is used to determine whether
914  * this is a point-to-point topology or a fabric topology: if the Port ID
915  * field is assigned, it is a fabric topology; otherwise, it is a
916  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
917  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
918  * specific topology completion conditions.
919  **/
920 static void
921 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
922                     struct lpfc_iocbq *rspiocb)
923 {
924         struct lpfc_vport *vport = cmdiocb->vport;
925         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
926         IOCB_t *irsp;
927         struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
928         struct serv_parm *sp;
929         uint16_t fcf_index;
930         int rc;
931         u32 ulp_status, ulp_word4, tmo;
932         bool flogi_in_retry = false;
933
934         /* Check to see if link went down during discovery */
935         if (lpfc_els_chk_latt(vport)) {
936                 /* One additional decrement on node reference count to
937                  * trigger the release of the node
938                  */
939                 if (!(ndlp->fc4_xpt_flags & SCSI_XPT_REGD))
940                         lpfc_nlp_put(ndlp);
941                 goto out;
942         }
943
944         ulp_status = get_job_ulpstatus(phba, rspiocb);
945         ulp_word4 = get_job_word4(phba, rspiocb);
946
947         if (phba->sli_rev == LPFC_SLI_REV4) {
948                 tmo = get_wqe_tmo(cmdiocb);
949         } else {
950                 irsp = &rspiocb->iocb;
951                 tmo = irsp->ulpTimeout;
952         }
953
954         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
955                 "FLOGI cmpl:      status:x%x/x%x state:x%x",
956                 ulp_status, ulp_word4,
957                 vport->port_state);
958
959         if (ulp_status) {
960                 /*
961                  * In case of FIP mode, perform roundrobin FCF failover
962                  * due to new FCF discovery
963                  */
964                 if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag) &&
965                     (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
966                         if (phba->link_state < LPFC_LINK_UP)
967                                 goto stop_rr_fcf_flogi;
968                         if ((phba->fcoe_cvl_eventtag_attn ==
969                              phba->fcoe_cvl_eventtag) &&
970                             (ulp_status == IOSTAT_LOCAL_REJECT) &&
971                             ((ulp_word4 & IOERR_PARAM_MASK) ==
972                             IOERR_SLI_ABORTED))
973                                 goto stop_rr_fcf_flogi;
974                         else
975                                 phba->fcoe_cvl_eventtag_attn =
976                                         phba->fcoe_cvl_eventtag;
977                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
978                                         "2611 FLOGI FCF (x%x), "
979                                         "status:x%x/x%x, tmo:x%x, perform "
980                                         "roundrobin FCF failover\n",
981                                         phba->fcf.current_rec.fcf_indx,
982                                         ulp_status, ulp_word4, tmo);
983                         lpfc_sli4_set_fcf_flogi_fail(phba,
984                                         phba->fcf.current_rec.fcf_indx);
985                         fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
986                         rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
987                         if (rc)
988                                 goto out;
989                 }
990
991 stop_rr_fcf_flogi:
992                 /* FLOGI failure */
993                 if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
994                       ((ulp_word4 & IOERR_PARAM_MASK) ==
995                                         IOERR_LOOP_OPEN_FAILURE)))
996                         lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
997                                       "2858 FLOGI Status:x%x/x%x TMO"
998                                       ":x%x Data x%lx x%x\n",
999                                       ulp_status, ulp_word4, tmo,
1000                                       phba->hba_flag, phba->fcf.fcf_flag);
1001
1002                 /* Check for retry */
1003                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1004                         /* Address a timing race with dev_loss.  If dev_loss
1005                          * is active on this FPort node, put the initial ref
1006                          * count back to stop premature node release actions.
1007                          */
1008                         lpfc_check_nlp_post_devloss(vport, ndlp);
1009                         flogi_in_retry = true;
1010                         goto out;
1011                 }
1012
1013                 /* The FLOGI will not be retried.  If the FPort node is not
1014                  * registered with the SCSI transport, remove the initial
1015                  * reference to trigger node release.
1016                  */
1017                 if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag) &&
1018                     !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD))
1019                         lpfc_nlp_put(ndlp);
1020
1021                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
1022                                  "0150 FLOGI Status:x%x/x%x "
1023                                  "xri x%x TMO:x%x refcnt %d\n",
1024                                  ulp_status, ulp_word4, cmdiocb->sli4_xritag,
1025                                  tmo, kref_read(&ndlp->kref));
1026
1027                 /* If this is not a loop open failure, bail out */
1028                 if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
1029                       ((ulp_word4 & IOERR_PARAM_MASK) ==
1030                                         IOERR_LOOP_OPEN_FAILURE))) {
1031                         /* Warn FLOGI status */
1032                         lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
1033                                       "0100 FLOGI Status:x%x/x%x "
1034                                       "TMO:x%x\n",
1035                                       ulp_status, ulp_word4, tmo);
1036                         goto flogifail;
1037                 }
1038
1039                 /* FLOGI failed, so there is no fabric */
1040                 clear_bit(FC_FABRIC, &vport->fc_flag);
1041                 clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
1042                 clear_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
1043
1044                 /* If private loop, then allow max outstanding els to be
1045                  * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1046                  * alpa map would take too long otherwise.
1047                  */
1048                 if (phba->alpa_map[0] == 0)
1049                         vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1050                 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1051                     (!test_bit(FC_VFI_REGISTERED, &vport->fc_flag) ||
1052                      (vport->fc_prevDID != vport->fc_myDID) ||
1053                         phba->fc_topology_changed)) {
1054                         if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag)) {
1055                                 if (phba->fc_topology_changed) {
1056                                         lpfc_unregister_fcf_prep(phba);
1057                                         clear_bit(FC_VFI_REGISTERED,
1058                                                   &vport->fc_flag);
1059                                         phba->fc_topology_changed = 0;
1060                                 } else {
1061                                         lpfc_sli4_unreg_all_rpis(vport);
1062                                 }
1063                         }
1064
1065                         /* Do not register VFI if the driver aborted FLOGI */
1066                         if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
1067                                 lpfc_issue_reg_vfi(vport);
1068
1069                         goto out;
1070                 }
1071                 goto flogifail;
1072         }
1073         clear_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
1074         clear_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
1075
1076         /*
1077          * The FLOGI succeeded.  Sync the data for the CPU before
1078          * accessing it.
1079          */
1080         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1081         if (!prsp)
1082                 goto out;
1083         if (!lpfc_is_els_acc_rsp(prsp))
1084                 goto out;
1085         sp = prsp->virt + sizeof(uint32_t);
1086
1087         /* FLOGI completes successfully */
1088         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1089                          "0101 FLOGI completes successfully, I/O tag:x%x "
1090                          "xri x%x Data: x%x x%x x%x x%x x%x x%lx x%x %d\n",
1091                          cmdiocb->iotag, cmdiocb->sli4_xritag,
1092                          ulp_word4, sp->cmn.e_d_tov,
1093                          sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1094                          vport->port_state, vport->fc_flag,
1095                          sp->cmn.priority_tagging, kref_read(&ndlp->kref));
1096
1097         /* reinitialize the VMID datastructure before returning */
1098         if (lpfc_is_vmid_enabled(phba)) {
1099                 lpfc_reinit_vmid(vport);
1100                 vport->vmid_flag = 0;
1101         }
1102         if (sp->cmn.priority_tagging)
1103                 vport->phba->pport->vmid_flag |= (LPFC_VMID_ISSUE_QFPA |
1104                                                   LPFC_VMID_TYPE_PRIO);
1105
1106         /*
1107          * Address a timing race with dev_loss.  If dev_loss is active on
1108          * this FPort node, put the initial ref count back to stop premature
1109          * node release actions.
1110          */
1111         lpfc_check_nlp_post_devloss(vport, ndlp);
1112         if (vport->port_state == LPFC_FLOGI) {
1113                 /*
1114                  * If Common Service Parameters indicate Nport
1115                  * we are point to point, if Fport we are Fabric.
1116                  */
1117                 if (sp->cmn.fPort)
1118                         rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp,
1119                                                         ulp_word4);
1120                 else if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag))
1121                         rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1122                 else {
1123                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1124                                 "2831 FLOGI response with cleared Fabric "
1125                                 "bit fcf_index 0x%x "
1126                                 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1127                                 "Fabric Name "
1128                                 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1129                                 phba->fcf.current_rec.fcf_indx,
1130                                 phba->fcf.current_rec.switch_name[0],
1131                                 phba->fcf.current_rec.switch_name[1],
1132                                 phba->fcf.current_rec.switch_name[2],
1133                                 phba->fcf.current_rec.switch_name[3],
1134                                 phba->fcf.current_rec.switch_name[4],
1135                                 phba->fcf.current_rec.switch_name[5],
1136                                 phba->fcf.current_rec.switch_name[6],
1137                                 phba->fcf.current_rec.switch_name[7],
1138                                 phba->fcf.current_rec.fabric_name[0],
1139                                 phba->fcf.current_rec.fabric_name[1],
1140                                 phba->fcf.current_rec.fabric_name[2],
1141                                 phba->fcf.current_rec.fabric_name[3],
1142                                 phba->fcf.current_rec.fabric_name[4],
1143                                 phba->fcf.current_rec.fabric_name[5],
1144                                 phba->fcf.current_rec.fabric_name[6],
1145                                 phba->fcf.current_rec.fabric_name[7]);
1146
1147                         lpfc_nlp_put(ndlp);
1148                         spin_lock_irq(&phba->hbalock);
1149                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1150                         spin_unlock_irq(&phba->hbalock);
1151                         clear_bit(FCF_RR_INPROG, &phba->hba_flag);
1152                         clear_bit(HBA_DEVLOSS_TMO, &phba->hba_flag);
1153                         phba->fcf.fcf_redisc_attempted = 0; /* reset */
1154                         goto out;
1155                 }
1156                 if (!rc) {
1157                         /* Mark the FCF discovery process done */
1158                         if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag))
1159                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1160                                                 LOG_ELS,
1161                                                 "2769 FLOGI to FCF (x%x) "
1162                                                 "completed successfully\n",
1163                                                 phba->fcf.current_rec.fcf_indx);
1164                         spin_lock_irq(&phba->hbalock);
1165                         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1166                         spin_unlock_irq(&phba->hbalock);
1167                         clear_bit(FCF_RR_INPROG, &phba->hba_flag);
1168                         clear_bit(HBA_DEVLOSS_TMO, &phba->hba_flag);
1169                         phba->fcf.fcf_redisc_attempted = 0; /* reset */
1170                         goto out;
1171                 }
1172         } else if (vport->port_state > LPFC_FLOGI &&
1173                    test_bit(FC_PT2PT, &vport->fc_flag)) {
1174                 /*
1175                  * In a p2p topology, it is possible that discovery has
1176                  * already progressed, and this completion can be ignored.
1177                  * Recheck the indicated topology.
1178                  */
1179                 if (!sp->cmn.fPort)
1180                         goto out;
1181         }
1182
1183 flogifail:
1184         spin_lock_irq(&phba->hbalock);
1185         phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1186         spin_unlock_irq(&phba->hbalock);
1187
1188         if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
1189                 /* FLOGI failed, so just use loop map to make discovery list */
1190                 lpfc_disc_list_loopmap(vport);
1191
1192                 /* Start discovery */
1193                 lpfc_disc_start(vport);
1194         } else if (((ulp_status != IOSTAT_LOCAL_REJECT) ||
1195                         (((ulp_word4 & IOERR_PARAM_MASK) !=
1196                          IOERR_SLI_ABORTED) &&
1197                         ((ulp_word4 & IOERR_PARAM_MASK) !=
1198                          IOERR_SLI_DOWN))) &&
1199                         (phba->link_state != LPFC_CLEAR_LA)) {
1200                 /* If FLOGI failed enable link interrupt. */
1201                 lpfc_issue_clear_la(phba, vport);
1202         }
1203 out:
1204         if (!flogi_in_retry)
1205                 clear_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
1206
1207         lpfc_els_free_iocb(phba, cmdiocb);
1208         lpfc_nlp_put(ndlp);
1209 }
1210
1211 /**
1212  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1213  *                           aborted during a link down
1214  * @phba: pointer to lpfc hba data structure.
1215  * @cmdiocb: pointer to lpfc command iocb data structure.
1216  * @rspiocb: pointer to lpfc response iocb data structure.
1217  *
1218  */
1219 static void
1220 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1221                         struct lpfc_iocbq *rspiocb)
1222 {
1223         uint32_t *pcmd;
1224         uint32_t cmd;
1225         u32 ulp_status, ulp_word4;
1226
1227         pcmd = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
1228         cmd = *pcmd;
1229
1230         ulp_status = get_job_ulpstatus(phba, rspiocb);
1231         ulp_word4 = get_job_word4(phba, rspiocb);
1232
1233         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1234                         "6445 ELS completes after LINK_DOWN: "
1235                         " Status %x/%x cmd x%x flg x%x iotag x%x\n",
1236                         ulp_status, ulp_word4, cmd,
1237                         cmdiocb->cmd_flag, cmdiocb->iotag);
1238
1239         if (cmdiocb->cmd_flag & LPFC_IO_FABRIC) {
1240                 cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
1241                 atomic_dec(&phba->fabric_iocb_count);
1242         }
1243         lpfc_els_free_iocb(phba, cmdiocb);
1244 }
1245
1246 /**
1247  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1248  * @vport: pointer to a host virtual N_Port data structure.
1249  * @ndlp: pointer to a node-list data structure.
1250  * @retry: number of retries to the command IOCB.
1251  *
1252  * This routine issues a Fabric Login (FLOGI) Request ELS command
1253  * for a @vport. The initiator service parameters are put into the payload
1254  * of the FLOGI Request IOCB and the top-level callback function pointer
1255  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1256  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1257  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1258  *
1259  * Note that the ndlp reference count will be incremented by 1 for holding the
1260  * ndlp and the reference to ndlp will be stored into the ndlp field of
1261  * the IOCB for the completion callback function to the FLOGI ELS command.
1262  *
1263  * Return code
1264  *   0 - successfully issued flogi iocb for @vport
1265  *   1 - failed to issue flogi iocb for @vport
1266  **/
1267 static int
1268 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1269                      uint8_t retry)
1270 {
1271         struct lpfc_hba  *phba = vport->phba;
1272         struct serv_parm *sp;
1273         union lpfc_wqe128 *wqe = NULL;
1274         IOCB_t *icmd = NULL;
1275         struct lpfc_iocbq *elsiocb;
1276         struct lpfc_iocbq defer_flogi_acc;
1277         u8 *pcmd, ct;
1278         uint16_t cmdsize;
1279         uint32_t tmo, did;
1280         int rc;
1281
1282         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1283         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1284                                      ndlp->nlp_DID, ELS_CMD_FLOGI);
1285
1286         if (!elsiocb)
1287                 return 1;
1288
1289         wqe = &elsiocb->wqe;
1290         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
1291         icmd = &elsiocb->iocb;
1292
1293         /* For FLOGI request, remainder of payload is service parameters */
1294         *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1295         pcmd += sizeof(uint32_t);
1296         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1297         sp = (struct serv_parm *) pcmd;
1298
1299         /* Setup CSPs accordingly for Fabric */
1300         sp->cmn.e_d_tov = 0;
1301         sp->cmn.w2.r_a_tov = 0;
1302         sp->cmn.virtual_fabric_support = 0;
1303         sp->cls1.classValid = 0;
1304         if (sp->cmn.fcphLow < FC_PH3)
1305                 sp->cmn.fcphLow = FC_PH3;
1306         if (sp->cmn.fcphHigh < FC_PH3)
1307                 sp->cmn.fcphHigh = FC_PH3;
1308
1309         /* Determine if switch supports priority tagging */
1310         if (phba->cfg_vmid_priority_tagging) {
1311                 sp->cmn.priority_tagging = 1;
1312                 /* lpfc_vmid_host_uuid is combination of wwpn and wwnn */
1313                 if (!memchr_inv(vport->lpfc_vmid_host_uuid, 0,
1314                                 sizeof(vport->lpfc_vmid_host_uuid))) {
1315                         memcpy(vport->lpfc_vmid_host_uuid, phba->wwpn,
1316                                sizeof(phba->wwpn));
1317                         memcpy(&vport->lpfc_vmid_host_uuid[8], phba->wwnn,
1318                                sizeof(phba->wwnn));
1319                 }
1320         }
1321
1322         if  (phba->sli_rev == LPFC_SLI_REV4) {
1323                 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1324                     LPFC_SLI_INTF_IF_TYPE_0) {
1325                         /* FLOGI needs to be 3 for WQE FCFI */
1326                         ct = SLI4_CT_FCFI;
1327                         bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
1328
1329                         /* Set the fcfi to the fcfi we registered with */
1330                         bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
1331                                phba->fcf.fcfi);
1332                 }
1333
1334                 /* Can't do SLI4 class2 without support sequence coalescing */
1335                 sp->cls2.classValid = 0;
1336                 sp->cls2.seqDelivery = 0;
1337         } else {
1338                 /* Historical, setting sequential-delivery bit for SLI3 */
1339                 sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1340                 sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1341                 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1342                         sp->cmn.request_multiple_Nport = 1;
1343                         /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1344                         icmd->ulpCt_h = 1;
1345                         icmd->ulpCt_l = 0;
1346                 } else {
1347                         sp->cmn.request_multiple_Nport = 0;
1348                 }
1349
1350                 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1351                         icmd->un.elsreq64.myID = 0;
1352                         icmd->un.elsreq64.fl = 1;
1353                 }
1354         }
1355
1356         tmo = phba->fc_ratov;
1357         phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1358         lpfc_set_disctmo(vport);
1359         phba->fc_ratov = tmo;
1360
1361         phba->fc_stat.elsXmitFLOGI++;
1362         elsiocb->cmd_cmpl = lpfc_cmpl_els_flogi;
1363
1364         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1365                 "Issue FLOGI:     opt:x%x",
1366                 phba->sli3_options, 0, 0);
1367
1368         elsiocb->ndlp = lpfc_nlp_get(ndlp);
1369         if (!elsiocb->ndlp) {
1370                 lpfc_els_free_iocb(phba, elsiocb);
1371                 return 1;
1372         }
1373
1374         /* Avoid race with FLOGI completion and hba_flags. */
1375         set_bit(HBA_FLOGI_ISSUED, &phba->hba_flag);
1376         set_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
1377
1378         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1379         if (rc == IOCB_ERROR) {
1380                 clear_bit(HBA_FLOGI_ISSUED, &phba->hba_flag);
1381                 clear_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
1382                 lpfc_els_free_iocb(phba, elsiocb);
1383                 lpfc_nlp_put(ndlp);
1384                 return 1;
1385         }
1386
1387         /* Clear external loopback plug detected flag */
1388         phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
1389
1390         /* Check for a deferred FLOGI ACC condition */
1391         if (phba->defer_flogi_acc.flag) {
1392                 /* lookup ndlp for received FLOGI */
1393                 ndlp = lpfc_findnode_did(vport, 0);
1394                 if (!ndlp)
1395                         return 0;
1396
1397                 did = vport->fc_myDID;
1398                 vport->fc_myDID = Fabric_DID;
1399
1400                 memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1401
1402                 if (phba->sli_rev == LPFC_SLI_REV4) {
1403                         bf_set(wqe_ctxt_tag,
1404                                &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
1405                                phba->defer_flogi_acc.rx_id);
1406                         bf_set(wqe_rcvoxid,
1407                                &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
1408                                phba->defer_flogi_acc.ox_id);
1409                 } else {
1410                         icmd = &defer_flogi_acc.iocb;
1411                         icmd->ulpContext = phba->defer_flogi_acc.rx_id;
1412                         icmd->unsli3.rcvsli3.ox_id =
1413                                 phba->defer_flogi_acc.ox_id;
1414                 }
1415
1416                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1417                                  "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1418                                  " ox_id: x%x, hba_flag x%lx\n",
1419                                  phba->defer_flogi_acc.rx_id,
1420                                  phba->defer_flogi_acc.ox_id, phba->hba_flag);
1421
1422                 /* Send deferred FLOGI ACC */
1423                 lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1424                                  ndlp, NULL);
1425
1426                 phba->defer_flogi_acc.flag = false;
1427
1428                 /* Decrement the held ndlp that was incremented when the
1429                  * deferred flogi acc flag was set.
1430                  */
1431                 if (phba->defer_flogi_acc.ndlp) {
1432                         lpfc_nlp_put(phba->defer_flogi_acc.ndlp);
1433                         phba->defer_flogi_acc.ndlp = NULL;
1434                 }
1435
1436                 vport->fc_myDID = did;
1437         }
1438
1439         return 0;
1440 }
1441
1442 /**
1443  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1444  * @phba: pointer to lpfc hba data structure.
1445  *
1446  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1447  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1448  * list and issues an abort IOCB commond on each outstanding IOCB that
1449  * contains a active Fabric_DID ndlp. Note that this function is to issue
1450  * the abort IOCB command on all the outstanding IOCBs, thus when this
1451  * function returns, it does not guarantee all the IOCBs are actually aborted.
1452  *
1453  * Return code
1454  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1455  **/
1456 int
1457 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1458 {
1459         struct lpfc_sli_ring *pring;
1460         struct lpfc_iocbq *iocb, *next_iocb;
1461         struct lpfc_nodelist *ndlp;
1462         u32 ulp_command;
1463
1464         /* Abort outstanding I/O on NPort <nlp_DID> */
1465         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1466                         "0201 Abort outstanding I/O on NPort x%x\n",
1467                         Fabric_DID);
1468
1469         pring = lpfc_phba_elsring(phba);
1470         if (unlikely(!pring))
1471                 return -EIO;
1472
1473         /*
1474          * Check the txcmplq for an iocb that matches the nport the driver is
1475          * searching for.
1476          */
1477         spin_lock_irq(&phba->hbalock);
1478         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1479                 ulp_command = get_job_cmnd(phba, iocb);
1480                 if (ulp_command == CMD_ELS_REQUEST64_CR) {
1481                         ndlp = iocb->ndlp;
1482                         if (ndlp && ndlp->nlp_DID == Fabric_DID) {
1483                                 if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
1484                                     !test_bit(FC_PT2PT_PLOGI,
1485                                               &phba->pport->fc_flag))
1486                                         iocb->fabric_cmd_cmpl =
1487                                                 lpfc_ignore_els_cmpl;
1488                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
1489                                                            NULL);
1490                         }
1491                 }
1492         }
1493         /* Make sure HBA is alive */
1494         lpfc_issue_hb_tmo(phba);
1495
1496         spin_unlock_irq(&phba->hbalock);
1497
1498         return 0;
1499 }
1500
1501 /**
1502  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1503  * @vport: pointer to a host virtual N_Port data structure.
1504  *
1505  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1506  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1507  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1508  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1509  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1510  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1511  * @vport.
1512  *
1513  * Return code
1514  *   0 - failed to issue initial flogi for @vport
1515  *   1 - successfully issued initial flogi for @vport
1516  **/
1517 int
1518 lpfc_initial_flogi(struct lpfc_vport *vport)
1519 {
1520         struct lpfc_nodelist *ndlp;
1521
1522         vport->port_state = LPFC_FLOGI;
1523         lpfc_set_disctmo(vport);
1524
1525         /* First look for the Fabric ndlp */
1526         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1527         if (!ndlp) {
1528                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1529                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1530                 if (!ndlp)
1531                         return 0;
1532                 /* Set the node type */
1533                 ndlp->nlp_type |= NLP_FABRIC;
1534
1535                 /* Put ndlp onto node list */
1536                 lpfc_enqueue_node(vport, ndlp);
1537         }
1538
1539         /* Reset the Fabric flag, topology change may have happened */
1540         clear_bit(FC_FABRIC, &vport->fc_flag);
1541         if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1542                 /* A node reference should be retained while registered with a
1543                  * transport or dev-loss-evt work is pending.
1544                  * Otherwise, decrement node reference to trigger release.
1545                  */
1546                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
1547                     !test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
1548                         lpfc_nlp_put(ndlp);
1549                 return 0;
1550         }
1551         return 1;
1552 }
1553
1554 /**
1555  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1556  * @vport: pointer to a host virtual N_Port data structure.
1557  *
1558  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1559  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1560  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1561  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1562  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1563  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1564  * @vport.
1565  *
1566  * Return code
1567  *   0 - failed to issue initial fdisc for @vport
1568  *   1 - successfully issued initial fdisc for @vport
1569  **/
1570 int
1571 lpfc_initial_fdisc(struct lpfc_vport *vport)
1572 {
1573         struct lpfc_nodelist *ndlp;
1574
1575         /* First look for the Fabric ndlp */
1576         ndlp = lpfc_findnode_did(vport, Fabric_DID);
1577         if (!ndlp) {
1578                 /* Cannot find existing Fabric ndlp, so allocate a new one */
1579                 ndlp = lpfc_nlp_init(vport, Fabric_DID);
1580                 if (!ndlp)
1581                         return 0;
1582
1583                 /* NPIV is only supported in Fabrics. */
1584                 ndlp->nlp_type |= NLP_FABRIC;
1585
1586                 /* Put ndlp onto node list */
1587                 lpfc_enqueue_node(vport, ndlp);
1588         }
1589
1590         if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1591                 /* A node reference should be retained while registered with a
1592                  * transport or dev-loss-evt work is pending.
1593                  * Otherwise, decrement node reference to trigger release.
1594                  */
1595                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
1596                     !test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
1597                         lpfc_nlp_put(ndlp);
1598                 return 0;
1599         }
1600         return 1;
1601 }
1602
1603 /**
1604  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1605  * @vport: pointer to a host virtual N_Port data structure.
1606  *
1607  * This routine checks whether there are more remaining Port Logins
1608  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1609  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1610  * to issue ELS PLOGIs up to the configured discover threads with the
1611  * @vport (@vport->cfg_discovery_threads). The function also decrement
1612  * the @vport's num_disc_node by 1 if it is not already 0.
1613  **/
1614 void
1615 lpfc_more_plogi(struct lpfc_vport *vport)
1616 {
1617         if (vport->num_disc_nodes)
1618                 vport->num_disc_nodes--;
1619
1620         /* Continue discovery with <num_disc_nodes> PLOGIs to go */
1621         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1622                          "0232 Continue discovery with %d PLOGIs to go "
1623                          "Data: x%x x%lx x%x\n",
1624                          vport->num_disc_nodes,
1625                          atomic_read(&vport->fc_plogi_cnt),
1626                          vport->fc_flag, vport->port_state);
1627         /* Check to see if there are more PLOGIs to be sent */
1628         if (test_bit(FC_NLP_MORE, &vport->fc_flag))
1629                 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1630                 lpfc_els_disc_plogi(vport);
1631
1632         return;
1633 }
1634
1635 /**
1636  * lpfc_plogi_confirm_nport - Confirm plogi wwpn matches stored ndlp
1637  * @phba: pointer to lpfc hba data structure.
1638  * @prsp: pointer to response IOCB payload.
1639  * @ndlp: pointer to a node-list data structure.
1640  *
1641  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1642  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1643  * The following cases are considered N_Port confirmed:
1644  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1645  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1646  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1647  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1648  * 1) if there is a node on vport list other than the @ndlp with the same
1649  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1650  * on that node to release the RPI associated with the node; 2) if there is
1651  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1652  * into, a new node shall be allocated (or activated). In either case, the
1653  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1654  * be released and the new_ndlp shall be put on to the vport node list and
1655  * its pointer returned as the confirmed node.
1656  *
1657  * Note that before the @ndlp got "released", the keepDID from not-matching
1658  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1659  * of the @ndlp. This is because the release of @ndlp is actually to put it
1660  * into an inactive state on the vport node list and the vport node list
1661  * management algorithm does not allow two node with a same DID.
1662  *
1663  * Return code
1664  *   pointer to the PLOGI N_Port @ndlp
1665  **/
1666 static struct lpfc_nodelist *
1667 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1668                          struct lpfc_nodelist *ndlp)
1669 {
1670         struct lpfc_vport *vport = ndlp->vport;
1671         struct lpfc_nodelist *new_ndlp;
1672         struct serv_parm *sp;
1673         uint8_t  name[sizeof(struct lpfc_name)];
1674         uint32_t keepDID = 0;
1675         int rc;
1676         unsigned long keep_nlp_flag = 0, keep_new_nlp_flag = 0;
1677         uint16_t keep_nlp_state;
1678         u32 keep_nlp_fc4_type = 0;
1679         struct lpfc_nvme_rport *keep_nrport = NULL;
1680         unsigned long *active_rrqs_xri_bitmap = NULL;
1681
1682         sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1683         memset(name, 0, sizeof(struct lpfc_name));
1684
1685         /* Now we find out if the NPort we are logging into, matches the WWPN
1686          * we have for that ndlp. If not, we have some work to do.
1687          */
1688         new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1689
1690         /* return immediately if the WWPN matches ndlp */
1691         if (new_ndlp == ndlp)
1692                 return ndlp;
1693
1694         if (phba->sli_rev == LPFC_SLI_REV4) {
1695                 active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1696                                                        GFP_KERNEL);
1697                 if (active_rrqs_xri_bitmap)
1698                         memset(active_rrqs_xri_bitmap, 0,
1699                                phba->cfg_rrq_xri_bitmap_sz);
1700         }
1701
1702         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1703                          "3178 PLOGI confirm: ndlp x%x x%lx x%x: "
1704                          "new_ndlp x%x x%lx x%x\n",
1705                          ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1706                          (new_ndlp ? new_ndlp->nlp_DID : 0),
1707                          (new_ndlp ? new_ndlp->nlp_flag : 0),
1708                          (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1709
1710         if (!new_ndlp) {
1711                 rc = memcmp(&ndlp->nlp_portname, name,
1712                             sizeof(struct lpfc_name));
1713                 if (!rc) {
1714                         if (active_rrqs_xri_bitmap)
1715                                 mempool_free(active_rrqs_xri_bitmap,
1716                                              phba->active_rrq_pool);
1717                         return ndlp;
1718                 }
1719                 new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
1720                 if (!new_ndlp) {
1721                         if (active_rrqs_xri_bitmap)
1722                                 mempool_free(active_rrqs_xri_bitmap,
1723                                              phba->active_rrq_pool);
1724                         return ndlp;
1725                 }
1726         } else {
1727                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1728                     active_rrqs_xri_bitmap)
1729                         memcpy(active_rrqs_xri_bitmap,
1730                                new_ndlp->active_rrqs_xri_bitmap,
1731                                phba->cfg_rrq_xri_bitmap_sz);
1732
1733                 /*
1734                  * Unregister from backend if not done yet. Could have been
1735                  * skipped due to ADISC
1736                  */
1737                 lpfc_nlp_unreg_node(vport, new_ndlp);
1738         }
1739
1740         keepDID = new_ndlp->nlp_DID;
1741
1742         /* At this point in this routine, we know new_ndlp will be
1743          * returned. however, any previous GID_FTs that were done
1744          * would have updated nlp_fc4_type in ndlp, so we must ensure
1745          * new_ndlp has the right value.
1746          */
1747         if (test_bit(FC_FABRIC, &vport->fc_flag)) {
1748                 keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1749                 new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1750         }
1751
1752         lpfc_unreg_rpi(vport, new_ndlp);
1753         new_ndlp->nlp_DID = ndlp->nlp_DID;
1754         new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1755         if (phba->sli_rev == LPFC_SLI_REV4)
1756                 memcpy(new_ndlp->active_rrqs_xri_bitmap,
1757                        ndlp->active_rrqs_xri_bitmap,
1758                        phba->cfg_rrq_xri_bitmap_sz);
1759
1760         /* Lock both ndlps */
1761         spin_lock_irq(&ndlp->lock);
1762         spin_lock_irq(&new_ndlp->lock);
1763         keep_new_nlp_flag = new_ndlp->nlp_flag;
1764         keep_nlp_flag = ndlp->nlp_flag;
1765         new_ndlp->nlp_flag = ndlp->nlp_flag;
1766
1767         /* if new_ndlp had NLP_UNREG_INP set, keep it */
1768         if (test_bit(NLP_UNREG_INP, &keep_new_nlp_flag))
1769                 set_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
1770         else
1771                 clear_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
1772
1773         /* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1774         if (test_bit(NLP_RPI_REGISTERED, &keep_new_nlp_flag))
1775                 set_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
1776         else
1777                 clear_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
1778
1779         /*
1780          * Retain the DROPPED flag. This will take care of the init
1781          * refcount when affecting the state change
1782          */
1783         if (test_bit(NLP_DROPPED, &keep_new_nlp_flag))
1784                 set_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
1785         else
1786                 clear_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
1787
1788         ndlp->nlp_flag = keep_new_nlp_flag;
1789
1790         /* if ndlp had NLP_UNREG_INP set, keep it */
1791         if (test_bit(NLP_UNREG_INP, &keep_nlp_flag))
1792                 set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
1793         else
1794                 clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
1795
1796         /* if ndlp had NLP_RPI_REGISTERED set, keep it */
1797         if (test_bit(NLP_RPI_REGISTERED, &keep_nlp_flag))
1798                 set_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
1799         else
1800                 clear_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
1801
1802         /*
1803          * Retain the DROPPED flag. This will take care of the init
1804          * refcount when affecting the state change
1805          */
1806         if (test_bit(NLP_DROPPED, &keep_nlp_flag))
1807                 set_bit(NLP_DROPPED, &ndlp->nlp_flag);
1808         else
1809                 clear_bit(NLP_DROPPED, &ndlp->nlp_flag);
1810
1811         spin_unlock_irq(&new_ndlp->lock);
1812         spin_unlock_irq(&ndlp->lock);
1813
1814         /* Set nlp_states accordingly */
1815         keep_nlp_state = new_ndlp->nlp_state;
1816         lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1817
1818         /* interchange the nvme remoteport structs */
1819         keep_nrport = new_ndlp->nrport;
1820         new_ndlp->nrport = ndlp->nrport;
1821
1822         /* Move this back to NPR state */
1823         if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1824                 /* The ndlp doesn't have a portname yet, but does have an
1825                  * NPort ID.  The new_ndlp portname matches the Rport's
1826                  * portname.  Reinstantiate the new_ndlp and reset the ndlp.
1827                  */
1828                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1829                          "3179 PLOGI confirm NEW: %x %x\n",
1830                          new_ndlp->nlp_DID, keepDID);
1831
1832                 /* Two ndlps cannot have the same did on the nodelist.
1833                  * The KeepDID and keep_nlp_fc4_type need to be swapped
1834                  * because ndlp is inflight with no WWPN.
1835                  */
1836                 ndlp->nlp_DID = keepDID;
1837                 ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1838                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1839                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1840                     active_rrqs_xri_bitmap)
1841                         memcpy(ndlp->active_rrqs_xri_bitmap,
1842                                active_rrqs_xri_bitmap,
1843                                phba->cfg_rrq_xri_bitmap_sz);
1844
1845         } else {
1846                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1847                          "3180 PLOGI confirm SWAP: %x %x\n",
1848                          new_ndlp->nlp_DID, keepDID);
1849
1850                 lpfc_unreg_rpi(vport, ndlp);
1851
1852                 /* The ndlp and new_ndlp both have WWPNs but are swapping
1853                  * NPort Ids and attributes.
1854                  */
1855                 ndlp->nlp_DID = keepDID;
1856                 ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1857
1858                 if (phba->sli_rev == LPFC_SLI_REV4 &&
1859                     active_rrqs_xri_bitmap)
1860                         memcpy(ndlp->active_rrqs_xri_bitmap,
1861                                active_rrqs_xri_bitmap,
1862                                phba->cfg_rrq_xri_bitmap_sz);
1863
1864                 /* Since we are switching over to the new_ndlp,
1865                  * reset the old ndlp state
1866                  */
1867                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1868                     (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1869                         keep_nlp_state = NLP_STE_NPR_NODE;
1870                 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1871                 ndlp->nrport = keep_nrport;
1872         }
1873
1874         /*
1875          * If ndlp is not associated with any rport we can drop it here else
1876          * let dev_loss_tmo_callbk trigger DEVICE_RM event
1877          */
1878         if (!ndlp->rport && (ndlp->nlp_state == NLP_STE_NPR_NODE))
1879                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1880
1881         if (phba->sli_rev == LPFC_SLI_REV4 &&
1882             active_rrqs_xri_bitmap)
1883                 mempool_free(active_rrqs_xri_bitmap,
1884                              phba->active_rrq_pool);
1885
1886         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1887                          "3173 PLOGI confirm exit: new_ndlp x%x x%lx x%x\n",
1888                          new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1889                          new_ndlp->nlp_fc4_type);
1890
1891         return new_ndlp;
1892 }
1893
1894 /**
1895  * lpfc_end_rscn - Check and handle more rscn for a vport
1896  * @vport: pointer to a host virtual N_Port data structure.
1897  *
1898  * This routine checks whether more Registration State Change
1899  * Notifications (RSCNs) came in while the discovery state machine was in
1900  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1901  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1902  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1903  * handling the RSCNs.
1904  **/
1905 void
1906 lpfc_end_rscn(struct lpfc_vport *vport)
1907 {
1908
1909         if (test_bit(FC_RSCN_MODE, &vport->fc_flag)) {
1910                 /*
1911                  * Check to see if more RSCNs came in while we were
1912                  * processing this one.
1913                  */
1914                 if (vport->fc_rscn_id_cnt ||
1915                     test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag))
1916                         lpfc_els_handle_rscn(vport);
1917                 else
1918                         clear_bit(FC_RSCN_MODE, &vport->fc_flag);
1919         }
1920 }
1921
1922 /**
1923  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1924  * @phba: pointer to lpfc hba data structure.
1925  * @cmdiocb: pointer to lpfc command iocb data structure.
1926  * @rspiocb: pointer to lpfc response iocb data structure.
1927  *
1928  * This routine will call the clear rrq function to free the rrq and
1929  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1930  * exist then the clear_rrq is still called because the rrq needs to
1931  * be freed.
1932  **/
1933
1934 static void
1935 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1936                   struct lpfc_iocbq *rspiocb)
1937 {
1938         struct lpfc_vport *vport = cmdiocb->vport;
1939         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
1940         struct lpfc_node_rrq *rrq;
1941         u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
1942         u32 ulp_word4 = get_job_word4(phba, rspiocb);
1943
1944         /* we pass cmdiocb to state machine which needs rspiocb as well */
1945         rrq = cmdiocb->context_un.rrq;
1946         cmdiocb->rsp_iocb = rspiocb;
1947
1948         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1949                 "RRQ cmpl:      status:x%x/x%x did:x%x",
1950                 ulp_status, ulp_word4,
1951                 get_job_els_rsp64_did(phba, cmdiocb));
1952
1953
1954         /* rrq completes to NPort <nlp_DID> */
1955         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1956                          "2880 RRQ completes to DID x%x "
1957                          "Data: x%x x%x x%x x%x x%x\n",
1958                          ndlp->nlp_DID, ulp_status, ulp_word4,
1959                          get_wqe_tmo(cmdiocb), rrq->xritag, rrq->rxid);
1960
1961         if (ulp_status) {
1962                 /* Check for retry */
1963                 /* Warn RRQ status Don't print the vport to vport rjts */
1964                 if (ulp_status != IOSTAT_LS_RJT ||
1965                     (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
1966                      ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
1967                     (phba)->pport->cfg_log_verbose & LOG_ELS)
1968                         lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
1969                                       "2881 RRQ DID:%06X Status:"
1970                                       "x%x/x%x\n",
1971                                       ndlp->nlp_DID, ulp_status,
1972                                       ulp_word4);
1973         }
1974
1975         lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1976         lpfc_els_free_iocb(phba, cmdiocb);
1977         lpfc_nlp_put(ndlp);
1978         return;
1979 }
1980 /**
1981  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1982  * @phba: pointer to lpfc hba data structure.
1983  * @cmdiocb: pointer to lpfc command iocb data structure.
1984  * @rspiocb: pointer to lpfc response iocb data structure.
1985  *
1986  * This routine is the completion callback function for issuing the Port
1987  * Login (PLOGI) command. For PLOGI completion, there must be an active
1988  * ndlp on the vport node list that matches the remote node ID from the
1989  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1990  * ignored and command IOCB released. The PLOGI response IOCB status is
1991  * checked for error conditions. If there is error status reported, PLOGI
1992  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1993  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1994  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1995  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1996  * there are additional N_Port nodes with the vport that need to perform
1997  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1998  * PLOGIs.
1999  **/
2000 static void
2001 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2002                     struct lpfc_iocbq *rspiocb)
2003 {
2004         struct lpfc_vport *vport = cmdiocb->vport;
2005         IOCB_t *irsp;
2006         struct lpfc_nodelist *ndlp, *free_ndlp;
2007         struct lpfc_dmabuf *prsp;
2008         bool disc;
2009         struct serv_parm *sp = NULL;
2010         u32 ulp_status, ulp_word4, did, iotag;
2011         bool release_node = false;
2012
2013         /* we pass cmdiocb to state machine which needs rspiocb as well */
2014         cmdiocb->rsp_iocb = rspiocb;
2015
2016         ulp_status = get_job_ulpstatus(phba, rspiocb);
2017         ulp_word4 = get_job_word4(phba, rspiocb);
2018         did = get_job_els_rsp64_did(phba, cmdiocb);
2019
2020         if (phba->sli_rev == LPFC_SLI_REV4) {
2021                 iotag = get_wqe_reqtag(cmdiocb);
2022         } else {
2023                 irsp = &rspiocb->iocb;
2024                 iotag = irsp->ulpIoTag;
2025         }
2026
2027         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2028                 "PLOGI cmpl:      status:x%x/x%x did:x%x",
2029                 ulp_status, ulp_word4, did);
2030
2031         ndlp = lpfc_findnode_did(vport, did);
2032         if (!ndlp) {
2033                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2034                                  "0136 PLOGI completes to NPort x%x "
2035                                  "with no ndlp. Data: x%x x%x x%x\n",
2036                                  did, ulp_status, ulp_word4, iotag);
2037                 goto out_freeiocb;
2038         }
2039
2040         /* Since ndlp can be freed in the disc state machine, note if this node
2041          * is being used during discovery.
2042          */
2043         disc = test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2044
2045         /* PLOGI completes to NPort <nlp_DID> */
2046         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2047                          "0102 PLOGI completes to NPort x%06x "
2048                          "IoTag x%x Data: x%x x%x x%x x%x x%x\n",
2049                          ndlp->nlp_DID, iotag,
2050                          ndlp->nlp_fc4_type,
2051                          ulp_status, ulp_word4,
2052                          disc, vport->num_disc_nodes);
2053
2054         /* Check to see if link went down during discovery */
2055         if (lpfc_els_chk_latt(vport)) {
2056                 set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2057                 goto out;
2058         }
2059
2060         if (ulp_status) {
2061                 /* Check for retry */
2062                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2063                         /* ELS command is being retried */
2064                         if (disc)
2065                                 set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2066                         goto out;
2067                 }
2068                 /* Warn PLOGI status Don't print the vport to vport rjts */
2069                 if (ulp_status != IOSTAT_LS_RJT ||
2070                     (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
2071                      ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
2072                     (phba)->pport->cfg_log_verbose & LOG_ELS)
2073                         lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
2074                                       "2753 PLOGI DID:%06X "
2075                                       "Status:x%x/x%x\n",
2076                                       ndlp->nlp_DID, ulp_status,
2077                                       ulp_word4);
2078
2079                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2080                 if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
2081                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2082                                                 NLP_EVT_CMPL_PLOGI);
2083
2084                 /* If a PLOGI collision occurred, the node needs to continue
2085                  * with the reglogin process.
2086                  */
2087                 spin_lock_irq(&ndlp->lock);
2088                 if ((test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag) ||
2089                      test_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag)) &&
2090                     ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
2091                         spin_unlock_irq(&ndlp->lock);
2092                         goto out;
2093                 }
2094
2095                 /* No PLOGI collision and the node is not registered with the
2096                  * scsi or nvme transport. It is no longer an active node. Just
2097                  * start the device remove process.
2098                  */
2099                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2100                         clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2101                         if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
2102                                 release_node = true;
2103                 }
2104                 spin_unlock_irq(&ndlp->lock);
2105
2106                 if (release_node)
2107                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2108                                                 NLP_EVT_DEVICE_RM);
2109         } else {
2110                 /* Good status, call state machine */
2111                 prsp = list_get_first(&cmdiocb->cmd_dmabuf->list,
2112                                       struct lpfc_dmabuf, list);
2113                 if (!prsp)
2114                         goto out;
2115                 if (!lpfc_is_els_acc_rsp(prsp))
2116                         goto out;
2117                 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2118
2119                 sp = (struct serv_parm *)((u8 *)prsp->virt +
2120                                           sizeof(u32));
2121
2122                 ndlp->vmid_support = 0;
2123                 if ((phba->cfg_vmid_app_header && sp->cmn.app_hdr_support) ||
2124                     (phba->cfg_vmid_priority_tagging &&
2125                      sp->cmn.priority_tagging)) {
2126                         lpfc_printf_log(phba, KERN_DEBUG, LOG_ELS,
2127                                         "4018 app_hdr_support %d tagging %d DID x%x\n",
2128                                         sp->cmn.app_hdr_support,
2129                                         sp->cmn.priority_tagging,
2130                                         ndlp->nlp_DID);
2131                         /* if the dest port supports VMID, mark it in ndlp */
2132                         ndlp->vmid_support = 1;
2133                 }
2134
2135                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2136                                         NLP_EVT_CMPL_PLOGI);
2137         }
2138
2139         if (disc && vport->num_disc_nodes) {
2140                 /* Check to see if there are more PLOGIs to be sent */
2141                 lpfc_more_plogi(vport);
2142
2143                 if (vport->num_disc_nodes == 0) {
2144                         clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
2145
2146                         lpfc_can_disctmo(vport);
2147                         lpfc_end_rscn(vport);
2148                 }
2149         }
2150
2151 out:
2152         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2153                               "PLOGI Cmpl PUT:     did:x%x refcnt %d",
2154                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2155
2156 out_freeiocb:
2157         /* Release the reference on the original I/O request. */
2158         free_ndlp = cmdiocb->ndlp;
2159
2160         lpfc_els_free_iocb(phba, cmdiocb);
2161         lpfc_nlp_put(free_ndlp);
2162         return;
2163 }
2164
2165 /**
2166  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2167  * @vport: pointer to a host virtual N_Port data structure.
2168  * @did: destination port identifier.
2169  * @retry: number of retries to the command IOCB.
2170  *
2171  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2172  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2173  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2174  * This routine constructs the proper fields of the PLOGI IOCB and invokes
2175  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2176  *
2177  * Note that the ndlp reference count will be incremented by 1 for holding
2178  * the ndlp and the reference to ndlp will be stored into the ndlp field
2179  * of the IOCB for the completion callback function to the PLOGI ELS command.
2180  *
2181  * Return code
2182  *   0 - Successfully issued a plogi for @vport
2183  *   1 - failed to issue a plogi for @vport
2184  **/
2185 int
2186 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2187 {
2188         struct lpfc_hba  *phba = vport->phba;
2189         struct serv_parm *sp;
2190         struct lpfc_nodelist *ndlp;
2191         struct lpfc_iocbq *elsiocb;
2192         uint8_t *pcmd;
2193         uint16_t cmdsize;
2194         int ret;
2195
2196         ndlp = lpfc_findnode_did(vport, did);
2197         if (!ndlp)
2198                 return 1;
2199
2200         /* Defer the processing of the issue PLOGI until after the
2201          * outstanding UNREG_RPI mbox command completes, unless we
2202          * are going offline. This logic does not apply for Fabric DIDs
2203          */
2204         if ((test_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag) ||
2205              test_bit(NLP_UNREG_INP, &ndlp->nlp_flag)) &&
2206             ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2207             !test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
2208                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2209                                  "4110 Issue PLOGI x%x deferred "
2210                                  "on NPort x%x rpi x%x flg x%lx Data:"
2211                                  " x%px\n",
2212                                  ndlp->nlp_defer_did, ndlp->nlp_DID,
2213                                  ndlp->nlp_rpi, ndlp->nlp_flag, ndlp);
2214
2215                 /* We can only defer 1st PLOGI */
2216                 if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2217                         ndlp->nlp_defer_did = did;
2218                 return 0;
2219         }
2220
2221         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2222         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2223                                      ELS_CMD_PLOGI);
2224         if (!elsiocb)
2225                 return 1;
2226
2227         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2228
2229         /* For PLOGI request, remainder of payload is service parameters */
2230         *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2231         pcmd += sizeof(uint32_t);
2232         memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2233         sp = (struct serv_parm *) pcmd;
2234
2235         /*
2236          * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2237          * to device on remote loops work.
2238          */
2239         if (test_bit(FC_FABRIC, &vport->fc_flag) &&
2240             !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
2241                 sp->cmn.altBbCredit = 1;
2242
2243         if (sp->cmn.fcphLow < FC_PH_4_3)
2244                 sp->cmn.fcphLow = FC_PH_4_3;
2245
2246         if (sp->cmn.fcphHigh < FC_PH3)
2247                 sp->cmn.fcphHigh = FC_PH3;
2248
2249         sp->cmn.valid_vendor_ver_level = 0;
2250         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2251         sp->cmn.bbRcvSizeMsb &= 0xF;
2252
2253         /* Check if the destination port supports VMID */
2254         ndlp->vmid_support = 0;
2255         if (vport->vmid_priority_tagging)
2256                 sp->cmn.priority_tagging = 1;
2257         else if (phba->cfg_vmid_app_header &&
2258                  bf_get(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags))
2259                 sp->cmn.app_hdr_support = 1;
2260
2261         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2262                 "Issue PLOGI:     did:x%x",
2263                 did, 0, 0);
2264
2265         /* If our firmware supports this feature, convey that
2266          * information to the target using the vendor specific field.
2267          */
2268         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2269                 sp->cmn.valid_vendor_ver_level = 1;
2270                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2271                 sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2272         }
2273
2274         phba->fc_stat.elsXmitPLOGI++;
2275         elsiocb->cmd_cmpl = lpfc_cmpl_els_plogi;
2276
2277         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2278                               "Issue PLOGI:     did:x%x refcnt %d",
2279                               did, kref_read(&ndlp->kref), 0);
2280         elsiocb->ndlp = lpfc_nlp_get(ndlp);
2281         if (!elsiocb->ndlp) {
2282                 lpfc_els_free_iocb(phba, elsiocb);
2283                 return 1;
2284         }
2285
2286         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2287         if (ret) {
2288                 lpfc_els_free_iocb(phba, elsiocb);
2289                 lpfc_nlp_put(ndlp);
2290                 return 1;
2291         }
2292
2293         return 0;
2294 }
2295
2296 /**
2297  * lpfc_cmpl_els_prli - Completion callback function for prli
2298  * @phba: pointer to lpfc hba data structure.
2299  * @cmdiocb: pointer to lpfc command iocb data structure.
2300  * @rspiocb: pointer to lpfc response iocb data structure.
2301  *
2302  * This routine is the completion callback function for a Process Login
2303  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2304  * status. If there is error status reported, PRLI retry shall be attempted
2305  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2306  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2307  * ndlp to mark the PRLI completion.
2308  **/
2309 static void
2310 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2311                    struct lpfc_iocbq *rspiocb)
2312 {
2313         struct lpfc_vport *vport = cmdiocb->vport;
2314         struct lpfc_nodelist *ndlp;
2315         char *mode;
2316         u32 ulp_status;
2317         u32 ulp_word4;
2318         bool release_node = false;
2319
2320         /* we pass cmdiocb to state machine which needs rspiocb as well */
2321         cmdiocb->rsp_iocb = rspiocb;
2322
2323         ndlp = cmdiocb->ndlp;
2324
2325         ulp_status = get_job_ulpstatus(phba, rspiocb);
2326         ulp_word4 = get_job_word4(phba, rspiocb);
2327
2328         clear_bit(NLP_PRLI_SND, &ndlp->nlp_flag);
2329
2330         /* Driver supports multiple FC4 types.  Counters matter. */
2331         spin_lock_irq(&ndlp->lock);
2332         vport->fc_prli_sent--;
2333         ndlp->fc4_prli_sent--;
2334         spin_unlock_irq(&ndlp->lock);
2335
2336         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2337                 "PRLI cmpl:       status:x%x/x%x did:x%x",
2338                 ulp_status, ulp_word4,
2339                 ndlp->nlp_DID);
2340
2341         /* PRLI completes to NPort <nlp_DID> */
2342         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2343                          "0103 PRLI completes to NPort x%06x "
2344                          "Data: x%x x%x x%x x%x x%x\n",
2345                          ndlp->nlp_DID, ulp_status, ulp_word4,
2346                          vport->num_disc_nodes, ndlp->fc4_prli_sent,
2347                          ndlp->fc4_xpt_flags);
2348
2349         /* Check to see if link went down during discovery */
2350         if (lpfc_els_chk_latt(vport))
2351                 goto out;
2352
2353         if (ulp_status) {
2354                 /* Check for retry */
2355                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2356                         /* ELS command is being retried */
2357                         goto out;
2358                 }
2359
2360                 /* If we don't send GFT_ID to Fabric, a PRLI error
2361                  * could be expected.
2362                  */
2363                 if (test_bit(FC_FABRIC, &vport->fc_flag) ||
2364                     vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)
2365                         mode = KERN_WARNING;
2366                 else
2367                         mode = KERN_INFO;
2368
2369                 /* Warn PRLI status */
2370                 lpfc_printf_vlog(vport, mode, LOG_ELS,
2371                                  "2754 PRLI DID:%06X Status:x%x/x%x, "
2372                                  "data: x%x x%x x%lx\n",
2373                                  ndlp->nlp_DID, ulp_status,
2374                                  ulp_word4, ndlp->nlp_state,
2375                                  ndlp->fc4_prli_sent, ndlp->nlp_flag);
2376
2377                 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2378                 if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
2379                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2380                                                 NLP_EVT_CMPL_PRLI);
2381
2382                 /* The following condition catches an inflight transition
2383                  * mismatch typically caused by an RSCN. Skip any
2384                  * processing to allow recovery.
2385                  */
2386                 if ((ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
2387                      ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE) ||
2388                     (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2389                      test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag))) {
2390                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2391                                          "2784 PRLI cmpl: Allow Node recovery "
2392                                          "DID x%06x nstate x%x nflag x%lx\n",
2393                                          ndlp->nlp_DID, ndlp->nlp_state,
2394                                          ndlp->nlp_flag);
2395                         goto out;
2396                 }
2397
2398                 /*
2399                  * For P2P topology, retain the node so that PLOGI can be
2400                  * attempted on it again.
2401                  */
2402                 if (test_bit(FC_PT2PT, &vport->fc_flag))
2403                         goto out;
2404
2405                 /* As long as this node is not registered with the SCSI
2406                  * or NVMe transport and no other PRLIs are outstanding,
2407                  * it is no longer an active node.  Otherwise devloss
2408                  * handles the final cleanup.
2409                  */
2410                 spin_lock_irq(&ndlp->lock);
2411                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
2412                     !ndlp->fc4_prli_sent) {
2413                         clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2414                         if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
2415                                 release_node = true;
2416                 }
2417                 spin_unlock_irq(&ndlp->lock);
2418
2419                 if (release_node)
2420                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2421                                                 NLP_EVT_DEVICE_RM);
2422         } else {
2423                 /* Good status, call state machine.  However, if another
2424                  * PRLI is outstanding, don't call the state machine
2425                  * because final disposition to Mapped or Unmapped is
2426                  * completed there.
2427                  */
2428                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2429                                         NLP_EVT_CMPL_PRLI);
2430         }
2431
2432 out:
2433         lpfc_els_free_iocb(phba, cmdiocb);
2434         lpfc_nlp_put(ndlp);
2435         return;
2436 }
2437
2438 /**
2439  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2440  * @vport: pointer to a host virtual N_Port data structure.
2441  * @ndlp: pointer to a node-list data structure.
2442  * @retry: number of retries to the command IOCB.
2443  *
2444  * This routine issues a Process Login (PRLI) ELS command for the
2445  * @vport. The PRLI service parameters are set up in the payload of the
2446  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2447  * is put to the IOCB completion callback func field before invoking the
2448  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2449  *
2450  * Note that the ndlp reference count will be incremented by 1 for holding the
2451  * ndlp and the reference to ndlp will be stored into the ndlp field of
2452  * the IOCB for the completion callback function to the PRLI ELS command.
2453  *
2454  * Return code
2455  *   0 - successfully issued prli iocb command for @vport
2456  *   1 - failed to issue prli iocb command for @vport
2457  **/
2458 int
2459 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2460                     uint8_t retry)
2461 {
2462         int rc = 0;
2463         struct lpfc_hba *phba = vport->phba;
2464         PRLI *npr;
2465         struct lpfc_nvme_prli *npr_nvme;
2466         struct lpfc_iocbq *elsiocb;
2467         uint8_t *pcmd;
2468         uint16_t cmdsize;
2469         u32 local_nlp_type, elscmd;
2470
2471         /*
2472          * If we are in RSCN mode, the FC4 types supported from a
2473          * previous GFT_ID command may not be accurate. So, if we
2474          * are a NVME Initiator, always look for the possibility of
2475          * the remote NPort beng a NVME Target.
2476          */
2477         if (phba->sli_rev == LPFC_SLI_REV4 &&
2478             test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
2479             vport->nvmei_support)
2480                 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2481         local_nlp_type = ndlp->nlp_fc4_type;
2482
2483         /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2484          * fields here before any of them can complete.
2485          */
2486         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2487         ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2488         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2489         clear_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
2490         clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2491         ndlp->nvme_fb_size = 0;
2492
2493  send_next_prli:
2494         if (local_nlp_type & NLP_FC4_FCP) {
2495                 /* Payload is 4 + 16 = 20 x14 bytes. */
2496                 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2497                 elscmd = ELS_CMD_PRLI;
2498         } else if (local_nlp_type & NLP_FC4_NVME) {
2499                 /* Payload is 4 + 20 = 24 x18 bytes. */
2500                 cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2501                 elscmd = ELS_CMD_NVMEPRLI;
2502         } else {
2503                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2504                                  "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2505                                  ndlp->nlp_fc4_type, ndlp->nlp_DID);
2506                 return 1;
2507         }
2508
2509         /* SLI3 ports don't support NVME.  If this rport is a strict NVME
2510          * FC4 type, implicitly LOGO.
2511          */
2512         if (phba->sli_rev == LPFC_SLI_REV3 &&
2513             ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2514                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2515                                  "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2516                                  ndlp->nlp_type);
2517                 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2518                 return 1;
2519         }
2520
2521         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2522                                      ndlp->nlp_DID, elscmd);
2523         if (!elsiocb)
2524                 return 1;
2525
2526         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2527
2528         /* For PRLI request, remainder of payload is service parameters */
2529         memset(pcmd, 0, cmdsize);
2530
2531         if (local_nlp_type & NLP_FC4_FCP) {
2532                 /* Remainder of payload is FCP PRLI parameter page.
2533                  * Note: this data structure is defined as
2534                  * BE/LE in the structure definition so no
2535                  * byte swap call is made.
2536                  */
2537                 *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2538                 pcmd += sizeof(uint32_t);
2539                 npr = (PRLI *)pcmd;
2540
2541                 /*
2542                  * If our firmware version is 3.20 or later,
2543                  * set the following bits for FC-TAPE support.
2544                  */
2545                 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2546                         npr->ConfmComplAllowed = 1;
2547                         npr->Retry = 1;
2548                         npr->TaskRetryIdReq = 1;
2549                 }
2550                 npr->estabImagePair = 1;
2551                 npr->readXferRdyDis = 1;
2552                 if (vport->cfg_first_burst_size)
2553                         npr->writeXferRdyDis = 1;
2554
2555                 /* For FCP support */
2556                 npr->prliType = PRLI_FCP_TYPE;
2557                 npr->initiatorFunc = 1;
2558                 elsiocb->cmd_flag |= LPFC_PRLI_FCP_REQ;
2559
2560                 /* Remove FCP type - processed. */
2561                 local_nlp_type &= ~NLP_FC4_FCP;
2562         } else if (local_nlp_type & NLP_FC4_NVME) {
2563                 /* Remainder of payload is NVME PRLI parameter page.
2564                  * This data structure is the newer definition that
2565                  * uses bf macros so a byte swap is required.
2566                  */
2567                 *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2568                 pcmd += sizeof(uint32_t);
2569                 npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2570                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2571                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2572                 if (phba->nsler) {
2573                         bf_set(prli_nsler, npr_nvme, 1);
2574                         bf_set(prli_conf, npr_nvme, 1);
2575                 }
2576
2577                 /* Only initiators request first burst. */
2578                 if ((phba->cfg_nvme_enable_fb) &&
2579                     !phba->nvmet_support)
2580                         bf_set(prli_fba, npr_nvme, 1);
2581
2582                 if (phba->nvmet_support) {
2583                         bf_set(prli_tgt, npr_nvme, 1);
2584                         bf_set(prli_disc, npr_nvme, 1);
2585                 } else {
2586                         bf_set(prli_init, npr_nvme, 1);
2587                         bf_set(prli_conf, npr_nvme, 1);
2588                 }
2589
2590                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2591                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2592                 elsiocb->cmd_flag |= LPFC_PRLI_NVME_REQ;
2593
2594                 /* Remove NVME type - processed. */
2595                 local_nlp_type &= ~NLP_FC4_NVME;
2596         }
2597
2598         phba->fc_stat.elsXmitPRLI++;
2599         elsiocb->cmd_cmpl = lpfc_cmpl_els_prli;
2600
2601         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2602                               "Issue PRLI:  did:x%x refcnt %d",
2603                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2604         elsiocb->ndlp = lpfc_nlp_get(ndlp);
2605         if (!elsiocb->ndlp) {
2606                 lpfc_els_free_iocb(phba, elsiocb);
2607                 return 1;
2608         }
2609
2610         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2611         if (rc == IOCB_ERROR) {
2612                 lpfc_els_free_iocb(phba, elsiocb);
2613                 lpfc_nlp_put(ndlp);
2614                 return 1;
2615         }
2616
2617         /* The vport counters are used for lpfc_scan_finished, but
2618          * the ndlp is used to track outstanding PRLIs for different
2619          * FC4 types.
2620          */
2621         set_bit(NLP_PRLI_SND, &ndlp->nlp_flag);
2622         spin_lock_irq(&ndlp->lock);
2623         vport->fc_prli_sent++;
2624         ndlp->fc4_prli_sent++;
2625         spin_unlock_irq(&ndlp->lock);
2626
2627         /* The driver supports 2 FC4 types.  Make sure
2628          * a PRLI is issued for all types before exiting.
2629          */
2630         if (phba->sli_rev == LPFC_SLI_REV4 &&
2631             local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2632                 goto send_next_prli;
2633         else
2634                 return 0;
2635 }
2636
2637 /**
2638  * lpfc_rscn_disc - Perform rscn discovery for a vport
2639  * @vport: pointer to a host virtual N_Port data structure.
2640  *
2641  * This routine performs Registration State Change Notification (RSCN)
2642  * discovery for a @vport. If the @vport's node port recovery count is not
2643  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2644  * the nodes that need recovery. If none of the PLOGI were needed through
2645  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2646  * invoked to check and handle possible more RSCN came in during the period
2647  * of processing the current ones.
2648  **/
2649 static void
2650 lpfc_rscn_disc(struct lpfc_vport *vport)
2651 {
2652         lpfc_can_disctmo(vport);
2653
2654         /* RSCN discovery */
2655         /* go thru NPR nodes and issue ELS PLOGIs */
2656         if (atomic_read(&vport->fc_npr_cnt))
2657                 if (lpfc_els_disc_plogi(vport))
2658                         return;
2659
2660         lpfc_end_rscn(vport);
2661 }
2662
2663 /**
2664  * lpfc_adisc_done - Complete the adisc phase of discovery
2665  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2666  *
2667  * This function is called when the final ADISC is completed during discovery.
2668  * This function handles clearing link attention or issuing reg_vpi depending
2669  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2670  * discovery.
2671  * This function is called with no locks held.
2672  **/
2673 static void
2674 lpfc_adisc_done(struct lpfc_vport *vport)
2675 {
2676         struct lpfc_hba   *phba = vport->phba;
2677
2678         /*
2679          * For NPIV, cmpl_reg_vpi will set port_state to READY,
2680          * and continue discovery.
2681          */
2682         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2683             !test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
2684             (phba->sli_rev < LPFC_SLI_REV4)) {
2685
2686                 /*
2687                  * If link is down, clear_la and reg_vpi will be done after
2688                  * flogi following a link up event
2689                  */
2690                 if (!lpfc_is_link_up(phba))
2691                         return;
2692
2693                 /* The ADISCs are complete.  Doesn't matter if they
2694                  * succeeded or failed because the ADISC completion
2695                  * routine guarantees to call the state machine and
2696                  * the RPI is either unregistered (failed ADISC response)
2697                  * or the RPI is still valid and the node is marked
2698                  * mapped for a target.  The exchanges should be in the
2699                  * correct state. This code is specific to SLI3.
2700                  */
2701                 lpfc_issue_clear_la(phba, vport);
2702                 lpfc_issue_reg_vpi(phba, vport);
2703                 return;
2704         }
2705         /*
2706         * For SLI2, we need to set port_state to READY
2707         * and continue discovery.
2708         */
2709         if (vport->port_state < LPFC_VPORT_READY) {
2710                 /* If we get here, there is nothing to ADISC */
2711                 lpfc_issue_clear_la(phba, vport);
2712                 if (!test_bit(FC_ABORT_DISCOVERY, &vport->fc_flag)) {
2713                         vport->num_disc_nodes = 0;
2714                         /* go thru NPR list, issue ELS PLOGIs */
2715                         if (atomic_read(&vport->fc_npr_cnt))
2716                                 lpfc_els_disc_plogi(vport);
2717                         if (!vport->num_disc_nodes) {
2718                                 clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
2719                                 lpfc_can_disctmo(vport);
2720                                 lpfc_end_rscn(vport);
2721                         }
2722                 }
2723                 vport->port_state = LPFC_VPORT_READY;
2724         } else
2725                 lpfc_rscn_disc(vport);
2726 }
2727
2728 /**
2729  * lpfc_more_adisc - Issue more adisc as needed
2730  * @vport: pointer to a host virtual N_Port data structure.
2731  *
2732  * This routine determines whether there are more ndlps on a @vport
2733  * node list need to have Address Discover (ADISC) issued. If so, it will
2734  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2735  * remaining nodes which need to have ADISC sent.
2736  **/
2737 void
2738 lpfc_more_adisc(struct lpfc_vport *vport)
2739 {
2740         if (vport->num_disc_nodes)
2741                 vport->num_disc_nodes--;
2742         /* Continue discovery with <num_disc_nodes> ADISCs to go */
2743         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2744                          "0210 Continue discovery with %d ADISCs to go "
2745                          "Data: x%x x%lx x%x\n",
2746                          vport->num_disc_nodes,
2747                          atomic_read(&vport->fc_adisc_cnt),
2748                          vport->fc_flag, vport->port_state);
2749         /* Check to see if there are more ADISCs to be sent */
2750         if (test_bit(FC_NLP_MORE, &vport->fc_flag)) {
2751                 lpfc_set_disctmo(vport);
2752                 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2753                 lpfc_els_disc_adisc(vport);
2754         }
2755         if (!vport->num_disc_nodes)
2756                 lpfc_adisc_done(vport);
2757         return;
2758 }
2759
2760 /**
2761  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2762  * @phba: pointer to lpfc hba data structure.
2763  * @cmdiocb: pointer to lpfc command iocb data structure.
2764  * @rspiocb: pointer to lpfc response iocb data structure.
2765  *
2766  * This routine is the completion function for issuing the Address Discover
2767  * (ADISC) command. It first checks to see whether link went down during
2768  * the discovery process. If so, the node will be marked as node port
2769  * recovery for issuing discover IOCB by the link attention handler and
2770  * exit. Otherwise, the response status is checked. If error was reported
2771  * in the response status, the ADISC command shall be retried by invoking
2772  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2773  * the response status, the state machine is invoked to set transition
2774  * with respect to NLP_EVT_CMPL_ADISC event.
2775  **/
2776 static void
2777 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2778                     struct lpfc_iocbq *rspiocb)
2779 {
2780         struct lpfc_vport *vport = cmdiocb->vport;
2781         IOCB_t *irsp;
2782         struct lpfc_nodelist *ndlp;
2783         bool  disc;
2784         u32 ulp_status, ulp_word4, tmo, iotag;
2785         bool release_node = false;
2786
2787         /* we pass cmdiocb to state machine which needs rspiocb as well */
2788         cmdiocb->rsp_iocb = rspiocb;
2789
2790         ndlp = cmdiocb->ndlp;
2791
2792         ulp_status = get_job_ulpstatus(phba, rspiocb);
2793         ulp_word4 = get_job_word4(phba, rspiocb);
2794
2795         if (phba->sli_rev == LPFC_SLI_REV4) {
2796                 tmo = get_wqe_tmo(cmdiocb);
2797                 iotag = get_wqe_reqtag(cmdiocb);
2798         } else {
2799                 irsp = &rspiocb->iocb;
2800                 tmo = irsp->ulpTimeout;
2801                 iotag = irsp->ulpIoTag;
2802         }
2803
2804         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2805                 "ADISC cmpl:      status:x%x/x%x did:x%x",
2806                 ulp_status, ulp_word4,
2807                 ndlp->nlp_DID);
2808
2809         /* Since ndlp can be freed in the disc state machine, note if this node
2810          * is being used during discovery.
2811          */
2812         disc = test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2813         clear_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
2814         /* ADISC completes to NPort <nlp_DID> */
2815         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2816                          "0104 ADISC completes to NPort x%x "
2817                          "IoTag x%x Data: x%x x%x x%x x%x x%x\n",
2818                          ndlp->nlp_DID, iotag,
2819                          ulp_status, ulp_word4,
2820                          tmo, disc, vport->num_disc_nodes);
2821
2822         /* Check to see if link went down during discovery */
2823         if (lpfc_els_chk_latt(vport)) {
2824                 set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2825                 goto out;
2826         }
2827
2828         if (ulp_status) {
2829                 /* Check for retry */
2830                 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2831                         /* ELS command is being retried */
2832                         if (disc) {
2833                                 set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2834                                 lpfc_set_disctmo(vport);
2835                         }
2836                         goto out;
2837                 }
2838                 /* Warn ADISC status */
2839                 lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
2840                               "2755 ADISC DID:%06X Status:x%x/x%x\n",
2841                               ndlp->nlp_DID, ulp_status,
2842                               ulp_word4);
2843                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2844                                         NLP_EVT_CMPL_ADISC);
2845
2846                 /* As long as this node is not registered with the SCSI or NVMe
2847                  * transport, it is no longer an active node. Otherwise
2848                  * devloss handles the final cleanup.
2849                  */
2850                 spin_lock_irq(&ndlp->lock);
2851                 if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2852                         clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2853                         if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
2854                                 release_node = true;
2855                 }
2856                 spin_unlock_irq(&ndlp->lock);
2857
2858                 if (release_node)
2859                         lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2860                                                 NLP_EVT_DEVICE_RM);
2861         } else
2862                 /* Good status, call state machine */
2863                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2864                                         NLP_EVT_CMPL_ADISC);
2865
2866         /* Check to see if there are more ADISCs to be sent */
2867         if (disc && vport->num_disc_nodes)
2868                 lpfc_more_adisc(vport);
2869 out:
2870         lpfc_els_free_iocb(phba, cmdiocb);
2871         lpfc_nlp_put(ndlp);
2872         return;
2873 }
2874
2875 /**
2876  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2877  * @vport: pointer to a virtual N_Port data structure.
2878  * @ndlp: pointer to a node-list data structure.
2879  * @retry: number of retries to the command IOCB.
2880  *
2881  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2882  * @vport. It prepares the payload of the ADISC ELS command, updates the
2883  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2884  * to issue the ADISC ELS command.
2885  *
2886  * Note that the ndlp reference count will be incremented by 1 for holding the
2887  * ndlp and the reference to ndlp will be stored into the ndlp field of
2888  * the IOCB for the completion callback function to the ADISC ELS command.
2889  *
2890  * Return code
2891  *   0 - successfully issued adisc
2892  *   1 - failed to issue adisc
2893  **/
2894 int
2895 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2896                      uint8_t retry)
2897 {
2898         int rc = 0;
2899         struct lpfc_hba  *phba = vport->phba;
2900         ADISC *ap;
2901         struct lpfc_iocbq *elsiocb;
2902         uint8_t *pcmd;
2903         uint16_t cmdsize;
2904
2905         cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2906         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2907                                      ndlp->nlp_DID, ELS_CMD_ADISC);
2908         if (!elsiocb)
2909                 return 1;
2910
2911         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2912
2913         /* For ADISC request, remainder of payload is service parameters */
2914         *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2915         pcmd += sizeof(uint32_t);
2916
2917         /* Fill in ADISC payload */
2918         ap = (ADISC *) pcmd;
2919         ap->hardAL_PA = phba->fc_pref_ALPA;
2920         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2921         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2922         ap->DID = be32_to_cpu(vport->fc_myDID);
2923
2924         phba->fc_stat.elsXmitADISC++;
2925         elsiocb->cmd_cmpl = lpfc_cmpl_els_adisc;
2926         set_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
2927         elsiocb->ndlp = lpfc_nlp_get(ndlp);
2928         if (!elsiocb->ndlp) {
2929                 lpfc_els_free_iocb(phba, elsiocb);
2930                 goto err;
2931         }
2932
2933         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2934                               "Issue ADISC:   did:x%x refcnt %d",
2935                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2936
2937         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2938         if (rc == IOCB_ERROR) {
2939                 lpfc_els_free_iocb(phba, elsiocb);
2940                 lpfc_nlp_put(ndlp);
2941                 goto err;
2942         }
2943
2944         return 0;
2945
2946 err:
2947         clear_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
2948         return 1;
2949 }
2950
2951 /**
2952  * lpfc_cmpl_els_logo - Completion callback function for logo
2953  * @phba: pointer to lpfc hba data structure.
2954  * @cmdiocb: pointer to lpfc command iocb data structure.
2955  * @rspiocb: pointer to lpfc response iocb data structure.
2956  *
2957  * This routine is the completion function for issuing the ELS Logout (LOGO)
2958  * command. If no error status was reported from the LOGO response, the
2959  * state machine of the associated ndlp shall be invoked for transition with
2960  * respect to NLP_EVT_CMPL_LOGO event.
2961  **/
2962 static void
2963 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2964                    struct lpfc_iocbq *rspiocb)
2965 {
2966         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
2967         struct lpfc_vport *vport = ndlp->vport;
2968         IOCB_t *irsp;
2969         uint32_t skip_recovery = 0;
2970         int wake_up_waiter = 0;
2971         u32 ulp_status;
2972         u32 ulp_word4;
2973         u32 tmo, iotag;
2974
2975         /* we pass cmdiocb to state machine which needs rspiocb as well */
2976         cmdiocb->rsp_iocb = rspiocb;
2977
2978         ulp_status = get_job_ulpstatus(phba, rspiocb);
2979         ulp_word4 = get_job_word4(phba, rspiocb);
2980
2981         if (phba->sli_rev == LPFC_SLI_REV4) {
2982                 tmo = get_wqe_tmo(cmdiocb);
2983                 iotag = get_wqe_reqtag(cmdiocb);
2984         } else {
2985                 irsp = &rspiocb->iocb;
2986                 tmo = irsp->ulpTimeout;
2987                 iotag = irsp->ulpIoTag;
2988         }
2989
2990         clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
2991         spin_lock_irq(&ndlp->lock);
2992         if (ndlp->save_flags & NLP_WAIT_FOR_LOGO) {
2993                 wake_up_waiter = 1;
2994                 ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
2995         }
2996         spin_unlock_irq(&ndlp->lock);
2997
2998         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2999                 "LOGO cmpl:       status:x%x/x%x did:x%x",
3000                 ulp_status, ulp_word4,
3001                 ndlp->nlp_DID);
3002
3003         /* LOGO completes to NPort <nlp_DID> */
3004         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3005                          "0105 LOGO completes to NPort x%x "
3006                          "IoTag x%x refcnt %d nflags x%lx xflags x%x "
3007                          "Data: x%x x%x x%x x%x\n",
3008                          ndlp->nlp_DID, iotag,
3009                          kref_read(&ndlp->kref), ndlp->nlp_flag,
3010                          ndlp->fc4_xpt_flags, ulp_status, ulp_word4,
3011                          tmo, vport->num_disc_nodes);
3012
3013         if (lpfc_els_chk_latt(vport)) {
3014                 skip_recovery = 1;
3015                 goto out;
3016         }
3017
3018         /* The LOGO will not be retried on failure.  A LOGO was
3019          * issued to the remote rport and a ACC or RJT or no Answer are
3020          * all acceptable.  Note the failure and move forward with
3021          * discovery.  The PLOGI will retry.
3022          */
3023         if (ulp_status) {
3024                 /* Warn LOGO status */
3025                 lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
3026                               "2756 LOGO, No Retry DID:%06X "
3027                               "Status:x%x/x%x\n",
3028                               ndlp->nlp_DID, ulp_status,
3029                               ulp_word4);
3030
3031                 if (lpfc_error_lost_link(vport, ulp_status, ulp_word4))
3032                         skip_recovery = 1;
3033         }
3034
3035         /* Call state machine. This will unregister the rpi if needed. */
3036         lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
3037
3038         if (skip_recovery)
3039                 goto out;
3040
3041         /* The driver sets this flag for an NPIV instance that doesn't want to
3042          * log into the remote port.
3043          */
3044         if (test_bit(NLP_TARGET_REMOVE, &ndlp->nlp_flag)) {
3045                 clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
3046                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3047                                         NLP_EVT_DEVICE_RM);
3048                 goto out_rsrc_free;
3049         }
3050
3051 out:
3052         /* At this point, the LOGO processing is complete. NOTE: For a
3053          * pt2pt topology, we are assuming the NPortID will only change
3054          * on link up processing. For a LOGO / PLOGI initiated by the
3055          * Initiator, we are assuming the NPortID is not going to change.
3056          */
3057
3058         if (wake_up_waiter && ndlp->logo_waitq)
3059                 wake_up(ndlp->logo_waitq);
3060         /*
3061          * If the node is a target, the handling attempts to recover the port.
3062          * For any other port type, the rpi is unregistered as an implicit
3063          * LOGO.
3064          */
3065         if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
3066             skip_recovery == 0) {
3067                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
3068                 set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
3069
3070                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3071                                  "3187 LOGO completes to NPort x%x: Start "
3072                                  "Recovery Data: x%x x%x x%x x%x\n",
3073                                  ndlp->nlp_DID, ulp_status,
3074                                  ulp_word4, tmo,
3075                                  vport->num_disc_nodes);
3076
3077                 lpfc_els_free_iocb(phba, cmdiocb);
3078                 lpfc_nlp_put(ndlp);
3079
3080                 lpfc_disc_start(vport);
3081                 return;
3082         }
3083
3084         /* Cleanup path for failed REG_RPI handling. If REG_RPI fails, the
3085          * driver sends a LOGO to the rport to cleanup.  For fabric and
3086          * initiator ports cleanup the node as long as it the node is not
3087          * register with the transport.
3088          */
3089         if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
3090                 clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
3091                 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3092                                         NLP_EVT_DEVICE_RM);
3093         }
3094 out_rsrc_free:
3095         /* Driver is done with the I/O. */
3096         lpfc_els_free_iocb(phba, cmdiocb);
3097         lpfc_nlp_put(ndlp);
3098 }
3099
3100 /**
3101  * lpfc_issue_els_logo - Issue a logo to an node on a vport
3102  * @vport: pointer to a virtual N_Port data structure.
3103  * @ndlp: pointer to a node-list data structure.
3104  * @retry: number of retries to the command IOCB.
3105  *
3106  * This routine constructs and issues an ELS Logout (LOGO) iocb command
3107  * to a remote node, referred by an @ndlp on a @vport. It constructs the
3108  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
3109  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
3110  *
3111  * Note that the ndlp reference count will be incremented by 1 for holding the
3112  * ndlp and the reference to ndlp will be stored into the ndlp field of
3113  * the IOCB for the completion callback function to the LOGO ELS command.
3114  *
3115  * Callers of this routine are expected to unregister the RPI first
3116  *
3117  * Return code
3118  *   0 - successfully issued logo
3119  *   1 - failed to issue logo
3120  **/
3121 int
3122 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3123                     uint8_t retry)
3124 {
3125         struct lpfc_hba  *phba = vport->phba;
3126         struct lpfc_iocbq *elsiocb;
3127         uint8_t *pcmd;
3128         uint16_t cmdsize;
3129         int rc;
3130
3131         if (test_bit(NLP_LOGO_SND, &ndlp->nlp_flag))
3132                 return 0;
3133
3134         cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
3135         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3136                                      ndlp->nlp_DID, ELS_CMD_LOGO);
3137         if (!elsiocb)
3138                 return 1;
3139
3140         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3141         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
3142         pcmd += sizeof(uint32_t);
3143
3144         /* Fill in LOGO payload */
3145         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
3146         pcmd += sizeof(uint32_t);
3147         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
3148
3149         phba->fc_stat.elsXmitLOGO++;
3150         elsiocb->cmd_cmpl = lpfc_cmpl_els_logo;
3151         set_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
3152         clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
3153         elsiocb->ndlp = lpfc_nlp_get(ndlp);
3154         if (!elsiocb->ndlp) {
3155                 lpfc_els_free_iocb(phba, elsiocb);
3156                 goto err;
3157         }
3158
3159         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3160                               "Issue LOGO:      did:x%x refcnt %d",
3161                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3162
3163         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3164         if (rc == IOCB_ERROR) {
3165                 lpfc_els_free_iocb(phba, elsiocb);
3166                 lpfc_nlp_put(ndlp);
3167                 goto err;
3168         }
3169
3170         spin_lock_irq(&ndlp->lock);
3171         ndlp->nlp_prev_state = ndlp->nlp_state;
3172         spin_unlock_irq(&ndlp->lock);
3173         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3174         return 0;
3175
3176 err:
3177         clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
3178         return 1;
3179 }
3180
3181 /**
3182  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3183  * @phba: pointer to lpfc hba data structure.
3184  * @cmdiocb: pointer to lpfc command iocb data structure.
3185  * @rspiocb: pointer to lpfc response iocb data structure.
3186  *
3187  * This routine is a generic completion callback function for ELS commands.
3188  * Specifically, it is the callback function which does not need to perform
3189  * any command specific operations. It is currently used by the ELS command
3190  * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
3191  * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
3192  * Other than certain debug loggings, this callback function simply invokes the
3193  * lpfc_els_chk_latt() routine to check whether link went down during the
3194  * discovery process.
3195  **/
3196 static void
3197 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3198                   struct lpfc_iocbq *rspiocb)
3199 {
3200         struct lpfc_vport *vport = cmdiocb->vport;
3201         struct lpfc_nodelist *free_ndlp;
3202         IOCB_t *irsp;
3203         u32 ulp_status, ulp_word4, tmo, did, iotag;
3204
3205         ulp_status = get_job_ulpstatus(phba, rspiocb);
3206         ulp_word4 = get_job_word4(phba, rspiocb);
3207         did = get_job_els_rsp64_did(phba, cmdiocb);
3208
3209         if (phba->sli_rev == LPFC_SLI_REV4) {
3210                 tmo = get_wqe_tmo(cmdiocb);
3211                 iotag = get_wqe_reqtag(cmdiocb);
3212         } else {
3213                 irsp = &rspiocb->iocb;
3214                 tmo = irsp->ulpTimeout;
3215                 iotag = irsp->ulpIoTag;
3216         }
3217
3218         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3219                               "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3220                               ulp_status, ulp_word4, did);
3221
3222         /* ELS cmd tag <ulpIoTag> completes */
3223         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3224                          "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3225                          iotag, ulp_status, ulp_word4, tmo);
3226
3227         /* Check to see if link went down during discovery */
3228         lpfc_els_chk_latt(vport);
3229
3230         free_ndlp = cmdiocb->ndlp;
3231
3232         lpfc_els_free_iocb(phba, cmdiocb);
3233         lpfc_nlp_put(free_ndlp);
3234 }
3235
3236 /**
3237  * lpfc_reg_fab_ctrl_node - RPI register the fabric controller node.
3238  * @vport: pointer to lpfc_vport data structure.
3239  * @fc_ndlp: pointer to the fabric controller (0xfffffd) node.
3240  *
3241  * This routine registers the rpi assigned to the fabric controller
3242  * NPort_ID (0xfffffd) with the port and moves the node to UNMAPPED
3243  * state triggering a registration with the SCSI transport.
3244  *
3245  * This routine is single out because the fabric controller node
3246  * does not receive a PLOGI.  This routine is consumed by the
3247  * SCR and RDF ELS commands.  Callers are expected to qualify
3248  * with SLI4 first.
3249  **/
3250 static int
3251 lpfc_reg_fab_ctrl_node(struct lpfc_vport *vport, struct lpfc_nodelist *fc_ndlp)
3252 {
3253         int rc;
3254         struct lpfc_hba *phba = vport->phba;
3255         struct lpfc_nodelist *ns_ndlp;
3256         LPFC_MBOXQ_t *mbox;
3257
3258         if (test_bit(NLP_RPI_REGISTERED, &fc_ndlp->nlp_flag))
3259                 return 0;
3260
3261         ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
3262         if (!ns_ndlp)
3263                 return -ENODEV;
3264
3265         lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3266                          "0935 %s: Reg FC RPI x%x on FC DID x%x NSSte: x%x\n",
3267                          __func__, fc_ndlp->nlp_rpi, fc_ndlp->nlp_DID,
3268                          ns_ndlp->nlp_state);
3269         if (ns_ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3270                 return -ENODEV;
3271
3272         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3273         if (!mbox) {
3274                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3275                                  "0936 %s: no memory for reg_login "
3276                                  "Data: x%x x%x x%lx x%x\n", __func__,
3277                                  fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3278                                  fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3279                 return -ENOMEM;
3280         }
3281         rc = lpfc_reg_rpi(phba, vport->vpi, fc_ndlp->nlp_DID,
3282                           (u8 *)&vport->fc_sparam, mbox, fc_ndlp->nlp_rpi);
3283         if (rc) {
3284                 rc = -EACCES;
3285                 goto out;
3286         }
3287
3288         set_bit(NLP_REG_LOGIN_SEND, &fc_ndlp->nlp_flag);
3289         mbox->mbox_cmpl = lpfc_mbx_cmpl_fc_reg_login;
3290         mbox->ctx_ndlp = lpfc_nlp_get(fc_ndlp);
3291         if (!mbox->ctx_ndlp) {
3292                 rc = -ENOMEM;
3293                 goto out;
3294         }
3295
3296         mbox->vport = vport;
3297         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3298         if (rc == MBX_NOT_FINISHED) {
3299                 rc = -ENODEV;
3300                 lpfc_nlp_put(fc_ndlp);
3301                 goto out;
3302         }
3303         /* Success path. Exit. */
3304         lpfc_nlp_set_state(vport, fc_ndlp,
3305                            NLP_STE_REG_LOGIN_ISSUE);
3306         return 0;
3307
3308  out:
3309         lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
3310         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3311                          "0938 %s: failed to format reg_login "
3312                          "Data: x%x x%x x%lx x%x\n", __func__,
3313                          fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3314                          fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3315         return rc;
3316 }
3317
3318 /**
3319  * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
3320  * @phba: pointer to lpfc hba data structure.
3321  * @cmdiocb: pointer to lpfc command iocb data structure.
3322  * @rspiocb: pointer to lpfc response iocb data structure.
3323  *
3324  * This routine is a generic completion callback function for Discovery ELS cmd.
3325  * Currently used by the ELS command issuing routines for the ELS State Change
3326  * Request (SCR), lpfc_issue_els_scr() and the ELS RDF, lpfc_issue_els_rdf().
3327  * These commands will be retried once only for ELS timeout errors.
3328  **/
3329 static void
3330 lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3331                        struct lpfc_iocbq *rspiocb)
3332 {
3333         struct lpfc_vport *vport = cmdiocb->vport;
3334         IOCB_t *irsp;
3335         struct lpfc_els_rdf_rsp *prdf;
3336         struct lpfc_dmabuf *pcmd, *prsp;
3337         u32 *pdata;
3338         u32 cmd;
3339         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
3340         u32 ulp_status, ulp_word4, tmo, did, iotag;
3341
3342         ulp_status = get_job_ulpstatus(phba, rspiocb);
3343         ulp_word4 = get_job_word4(phba, rspiocb);
3344         did = get_job_els_rsp64_did(phba, cmdiocb);
3345
3346         if (phba->sli_rev == LPFC_SLI_REV4) {
3347                 tmo = get_wqe_tmo(cmdiocb);
3348                 iotag = get_wqe_reqtag(cmdiocb);
3349         } else {
3350                 irsp = &rspiocb->iocb;
3351                 tmo = irsp->ulpTimeout;
3352                 iotag = irsp->ulpIoTag;
3353         }
3354
3355         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3356                 "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3357                 ulp_status, ulp_word4, did);
3358
3359         /* ELS cmd tag <ulpIoTag> completes */
3360         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3361                          "0217 ELS cmd tag x%x completes Data: x%x x%x x%x x%x\n",
3362                          iotag, ulp_status, ulp_word4, tmo, cmdiocb->retry);
3363
3364         pcmd = cmdiocb->cmd_dmabuf;
3365         if (!pcmd)
3366                 goto out;
3367
3368         pdata = (u32 *)pcmd->virt;
3369         if (!pdata)
3370                 goto out;
3371         cmd = *pdata;
3372
3373         /* Only 1 retry for ELS Timeout only */
3374         if (ulp_status == IOSTAT_LOCAL_REJECT &&
3375             ((ulp_word4 & IOERR_PARAM_MASK) ==
3376             IOERR_SEQUENCE_TIMEOUT)) {
3377                 cmdiocb->retry++;
3378                 if (cmdiocb->retry <= 1) {
3379                         switch (cmd) {
3380                         case ELS_CMD_SCR:
3381                                 lpfc_issue_els_scr(vport, cmdiocb->retry);
3382                                 break;
3383                         case ELS_CMD_EDC:
3384                                 lpfc_issue_els_edc(vport, cmdiocb->retry);
3385                                 break;
3386                         case ELS_CMD_RDF:
3387                                 lpfc_issue_els_rdf(vport, cmdiocb->retry);
3388                                 break;
3389                         }
3390                         goto out;
3391                 }
3392                 phba->fc_stat.elsRetryExceeded++;
3393         }
3394         if (cmd == ELS_CMD_EDC) {
3395                 /* must be called before checking uplStatus and returning */
3396                 lpfc_cmpl_els_edc(phba, cmdiocb, rspiocb);
3397                 return;
3398         }
3399         if (ulp_status) {
3400                 /* ELS discovery cmd completes with error */
3401                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
3402                                  "4203 ELS cmd x%x error: x%x x%X\n", cmd,
3403                                  ulp_status, ulp_word4);
3404                 goto out;
3405         }
3406
3407         /* The RDF response doesn't have any impact on the running driver
3408          * but the notification descriptors are dumped here for support.
3409          */
3410         if (cmd == ELS_CMD_RDF) {
3411                 int i;
3412
3413                 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3414                 if (!prsp)
3415                         goto out;
3416
3417                 prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
3418                 if (!prdf)
3419                         goto out;
3420                 if (!lpfc_is_els_acc_rsp(prsp))
3421                         goto out;
3422
3423                 for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
3424                             i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
3425                         lpfc_printf_vlog(vport, KERN_INFO,
3426                                          LOG_ELS | LOG_CGN_MGMT,
3427                                          "4677 Fabric RDF Notification Grant "
3428                                          "Data: 0x%08x Reg: %x %x\n",
3429                                          be32_to_cpu(
3430                                                  prdf->reg_d1.desc_tags[i]),
3431                                          phba->cgn_reg_signal,
3432                                          phba->cgn_reg_fpin);
3433         }
3434
3435 out:
3436         /* Check to see if link went down during discovery */
3437         lpfc_els_chk_latt(vport);
3438         lpfc_els_free_iocb(phba, cmdiocb);
3439         lpfc_nlp_put(ndlp);
3440         return;
3441 }
3442
3443 /**
3444  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3445  * @vport: pointer to a host virtual N_Port data structure.
3446  * @retry: retry counter for the command IOCB.
3447  *
3448  * This routine issues a State Change Request (SCR) to a fabric node
3449  * on a @vport. The remote node is Fabric Controller (0xfffffd). It
3450  * first search the @vport node list to find the matching ndlp. If no such
3451  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3452  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3453  * routine is invoked to send the SCR IOCB.
3454  *
3455  * Note that the ndlp reference count will be incremented by 1 for holding the
3456  * ndlp and the reference to ndlp will be stored into the ndlp field of
3457  * the IOCB for the completion callback function to the SCR ELS command.
3458  *
3459  * Return code
3460  *   0 - Successfully issued scr command
3461  *   1 - Failed to issue scr command
3462  **/
3463 int
3464 lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
3465 {
3466         int rc = 0;
3467         struct lpfc_hba  *phba = vport->phba;
3468         struct lpfc_iocbq *elsiocb;
3469         uint8_t *pcmd;
3470         uint16_t cmdsize;
3471         struct lpfc_nodelist *ndlp;
3472
3473         cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3474
3475         ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3476         if (!ndlp) {
3477                 ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3478                 if (!ndlp)
3479                         return 1;
3480                 lpfc_enqueue_node(vport, ndlp);
3481         }
3482
3483         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3484                                      ndlp->nlp_DID, ELS_CMD_SCR);
3485         if (!elsiocb)
3486                 return 1;
3487
3488         if (phba->sli_rev == LPFC_SLI_REV4) {
3489                 rc = lpfc_reg_fab_ctrl_node(vport, ndlp);
3490                 if (rc) {
3491                         lpfc_els_free_iocb(phba, elsiocb);
3492                         lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3493                                          "0937 %s: Failed to reg fc node, rc %d\n",
3494                                          __func__, rc);
3495                         return 1;
3496                 }
3497         }
3498         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3499
3500         *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3501         pcmd += sizeof(uint32_t);
3502
3503         /* For SCR, remainder of payload is SCR parameter page */
3504         memset(pcmd, 0, sizeof(SCR));
3505         ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3506
3507         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3508                 "Issue SCR:       did:x%x",
3509                 ndlp->nlp_DID, 0, 0);
3510
3511         phba->fc_stat.elsXmitSCR++;
3512         elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
3513         elsiocb->ndlp = lpfc_nlp_get(ndlp);
3514         if (!elsiocb->ndlp) {
3515                 lpfc_els_free_iocb(phba, elsiocb);
3516                 return 1;
3517         }
3518
3519         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3520                               "Issue SCR:     did:x%x refcnt %d",
3521                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3522
3523         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3524         if (rc == IOCB_ERROR) {
3525                 lpfc_els_free_iocb(phba, elsiocb);
3526                 lpfc_nlp_put(ndlp);
3527                 return 1;
3528         }
3529
3530         return 0;
3531 }
3532
3533 /**
3534  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3535  *   or the other nport (pt2pt).
3536  * @vport: pointer to a host virtual N_Port data structure.
3537  * @retry: number of retries to the command IOCB.
3538  *
3539  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3540  *  when connected to a fabric, or to the remote port when connected
3541  *  in point-to-point mode. When sent to the Fabric Controller, it will
3542  *  replay the RSCN to registered recipients.
3543  *
3544  * Note that the ndlp reference count will be incremented by 1 for holding the
3545  * ndlp and the reference to ndlp will be stored into the ndlp field of
3546  * the IOCB for the completion callback function to the RSCN ELS command.
3547  *
3548  * Return code
3549  *   0 - Successfully issued RSCN command
3550  *   1 - Failed to issue RSCN command
3551  **/
3552 int
3553 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3554 {
3555         int rc = 0;
3556         struct lpfc_hba *phba = vport->phba;
3557         struct lpfc_iocbq *elsiocb;
3558         struct lpfc_nodelist *ndlp;
3559         struct {
3560                 struct fc_els_rscn rscn;
3561                 struct fc_els_rscn_page portid;
3562         } *event;
3563         uint32_t nportid;
3564         uint16_t cmdsize = sizeof(*event);
3565
3566         /* Not supported for private loop */
3567         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3568             !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
3569                 return 1;
3570
3571         if (test_bit(FC_PT2PT, &vport->fc_flag)) {
3572                 /* find any mapped nport - that would be the other nport */
3573                 ndlp = lpfc_findnode_mapped(vport);
3574                 if (!ndlp)
3575                         return 1;
3576         } else {
3577                 nportid = FC_FID_FCTRL;
3578                 /* find the fabric controller node */
3579                 ndlp = lpfc_findnode_did(vport, nportid);
3580                 if (!ndlp) {
3581                         /* if one didn't exist, make one */
3582                         ndlp = lpfc_nlp_init(vport, nportid);
3583                         if (!ndlp)
3584                                 return 1;
3585                         lpfc_enqueue_node(vport, ndlp);
3586                 }
3587         }
3588
3589         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3590                                      ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3591
3592         if (!elsiocb)
3593                 return 1;
3594
3595         event = elsiocb->cmd_dmabuf->virt;
3596
3597         event->rscn.rscn_cmd = ELS_RSCN;
3598         event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3599         event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3600
3601         nportid = vport->fc_myDID;
3602         /* appears that page flags must be 0 for fabric to broadcast RSCN */
3603         event->portid.rscn_page_flags = 0;
3604         event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3605         event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3606         event->portid.rscn_fid[2] = nportid & 0x000000FF;
3607
3608         phba->fc_stat.elsXmitRSCN++;
3609         elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
3610         elsiocb->ndlp = lpfc_nlp_get(ndlp);
3611         if (!elsiocb->ndlp) {
3612                 lpfc_els_free_iocb(phba, elsiocb);
3613                 return 1;
3614         }
3615
3616         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3617                               "Issue RSCN:       did:x%x",
3618                               ndlp->nlp_DID, 0, 0);
3619
3620         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3621         if (rc == IOCB_ERROR) {
3622                 lpfc_els_free_iocb(phba, elsiocb);
3623                 lpfc_nlp_put(ndlp);
3624                 return 1;
3625         }
3626
3627         return 0;
3628 }
3629
3630 /**
3631  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3632  * @vport: pointer to a host virtual N_Port data structure.
3633  * @nportid: N_Port identifier to the remote node.
3634  * @retry: number of retries to the command IOCB.
3635  *
3636  * This routine issues a Fibre Channel Address Resolution Response
3637  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3638  * is passed into the function. It first search the @vport node list to find
3639  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3640  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3641  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3642  *
3643  * Note that the ndlp reference count will be incremented by 1 for holding the
3644  * ndlp and the reference to ndlp will be stored into the ndlp field of
3645  * the IOCB for the completion callback function to the FARPR ELS command.
3646  *
3647  * Return code
3648  *   0 - Successfully issued farpr command
3649  *   1 - Failed to issue farpr command
3650  **/
3651 static int
3652 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3653 {
3654         int rc = 0;
3655         struct lpfc_hba  *phba = vport->phba;
3656         struct lpfc_iocbq *elsiocb;
3657         FARP *fp;
3658         uint8_t *pcmd;
3659         uint32_t *lp;
3660         uint16_t cmdsize;
3661         struct lpfc_nodelist *ondlp;
3662         struct lpfc_nodelist *ndlp;
3663
3664         cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3665
3666         ndlp = lpfc_findnode_did(vport, nportid);
3667         if (!ndlp) {
3668                 ndlp = lpfc_nlp_init(vport, nportid);
3669                 if (!ndlp)
3670                         return 1;
3671                 lpfc_enqueue_node(vport, ndlp);
3672         }
3673
3674         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3675                                      ndlp->nlp_DID, ELS_CMD_FARPR);
3676         if (!elsiocb)
3677                 return 1;
3678
3679         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3680
3681         *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3682         pcmd += sizeof(uint32_t);
3683
3684         /* Fill in FARPR payload */
3685         fp = (FARP *) (pcmd);
3686         memset(fp, 0, sizeof(FARP));
3687         lp = (uint32_t *) pcmd;
3688         *lp++ = be32_to_cpu(nportid);
3689         *lp++ = be32_to_cpu(vport->fc_myDID);
3690         fp->Rflags = 0;
3691         fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3692
3693         memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3694         memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3695         ondlp = lpfc_findnode_did(vport, nportid);
3696         if (ondlp) {
3697                 memcpy(&fp->OportName, &ondlp->nlp_portname,
3698                        sizeof(struct lpfc_name));
3699                 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3700                        sizeof(struct lpfc_name));
3701         }
3702
3703         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3704                 "Issue FARPR:     did:x%x",
3705                 ndlp->nlp_DID, 0, 0);
3706
3707         phba->fc_stat.elsXmitFARPR++;
3708         elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
3709         elsiocb->ndlp = lpfc_nlp_get(ndlp);
3710         if (!elsiocb->ndlp) {
3711                 lpfc_els_free_iocb(phba, elsiocb);
3712                 return 1;
3713         }
3714
3715         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3716         if (rc == IOCB_ERROR) {
3717                 /* The additional lpfc_nlp_put will cause the following
3718                  * lpfc_els_free_iocb routine to trigger the release of
3719                  * the node.
3720                  */
3721                 lpfc_els_free_iocb(phba, elsiocb);
3722                 lpfc_nlp_put(ndlp);
3723                 return 1;
3724         }
3725         /* This will cause the callback-function lpfc_cmpl_els_cmd to
3726          * trigger the release of the node.
3727          */
3728         /* Don't release reference count as RDF is likely outstanding */
3729         return 0;
3730 }
3731
3732 /**
3733  * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
3734  * @vport: pointer to a host virtual N_Port data structure.
3735  * @retry: retry counter for the command IOCB.
3736  *
3737  * This routine issues an ELS RDF to the Fabric Controller to register
3738  * for diagnostic functions.
3739  *
3740  * Note that the ndlp reference count will be incremented by 1 for holding the
3741  * ndlp and the reference to ndlp will be stored into the ndlp field of
3742  * the IOCB for the completion callback function to the RDF ELS command.
3743  *
3744  * Return code
3745  *   0 - Successfully issued rdf command
3746  *   1 - Failed to issue rdf command
3747  **/
3748 int
3749 lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
3750 {
3751         struct lpfc_hba *phba = vport->phba;
3752         struct lpfc_iocbq *elsiocb;
3753         struct lpfc_els_rdf_req *prdf;
3754         struct lpfc_nodelist *ndlp;
3755         uint16_t cmdsize;
3756         int rc;
3757
3758         cmdsize = sizeof(*prdf);
3759
3760         ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3761         if (!ndlp) {
3762                 ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3763                 if (!ndlp)
3764                         return -ENODEV;
3765                 lpfc_enqueue_node(vport, ndlp);
3766         }
3767
3768         /* RDF ELS is not required on an NPIV VN_Port. */
3769         if (vport->port_type == LPFC_NPIV_PORT)
3770                 return -EACCES;
3771
3772         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3773                                      ndlp->nlp_DID, ELS_CMD_RDF);
3774         if (!elsiocb)
3775                 return -ENOMEM;
3776
3777         /* Configure the payload for the supported FPIN events. */
3778         prdf = (struct lpfc_els_rdf_req *)elsiocb->cmd_dmabuf->virt;
3779         memset(prdf, 0, cmdsize);
3780         prdf->rdf.fpin_cmd = ELS_RDF;
3781         prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
3782                                          sizeof(struct fc_els_rdf));
3783         prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
3784         prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
3785                                 FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
3786         prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
3787         prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
3788         prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
3789         prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
3790         prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
3791
3792         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3793                          "6444 Xmit RDF to remote NPORT x%x Reg: %x %x\n",
3794                          ndlp->nlp_DID, phba->cgn_reg_signal,
3795                          phba->cgn_reg_fpin);
3796
3797         phba->cgn_fpin_frequency = LPFC_FPIN_INIT_FREQ;
3798         elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
3799         elsiocb->ndlp = lpfc_nlp_get(ndlp);
3800         if (!elsiocb->ndlp) {
3801                 lpfc_els_free_iocb(phba, elsiocb);
3802                 return -EIO;
3803         }
3804
3805         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3806                               "Issue RDF:     did:x%x refcnt %d",
3807                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3808
3809         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3810         if (rc == IOCB_ERROR) {
3811                 lpfc_els_free_iocb(phba, elsiocb);
3812                 lpfc_nlp_put(ndlp);
3813                 return -EIO;
3814         }
3815         return 0;
3816 }
3817
3818  /**
3819   * lpfc_els_rcv_rdf - Receive RDF ELS request from the fabric.
3820   * @vport: pointer to a host virtual N_Port data structure.
3821   * @cmdiocb: pointer to lpfc command iocb data structure.
3822   * @ndlp: pointer to a node-list data structure.
3823   *
3824   * A received RDF implies a possible change to fabric supported diagnostic
3825   * functions.  This routine sends LS_ACC and then has the Nx_Port issue a new
3826   * RDF request to reregister for supported diagnostic functions.
3827   *
3828   * Return code
3829   *   0 - Success
3830   *   -EIO - Failed to process received RDF
3831   **/
3832 static int
3833 lpfc_els_rcv_rdf(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3834                  struct lpfc_nodelist *ndlp)
3835 {
3836         /* Send LS_ACC */
3837         if (lpfc_els_rsp_acc(vport, ELS_CMD_RDF, cmdiocb, ndlp, NULL)) {
3838                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3839                                  "1623 Failed to RDF_ACC from x%x for x%x\n",
3840                                  ndlp->nlp_DID, vport->fc_myDID);
3841                 return -EIO;
3842         }
3843
3844         /* Issue new RDF for reregistering */
3845         if (lpfc_issue_els_rdf(vport, 0)) {
3846                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3847                                  "2623 Failed to re register RDF for x%x\n",
3848                                  vport->fc_myDID);
3849                 return -EIO;
3850         }
3851
3852         return 0;
3853 }
3854
3855 /**
3856  * lpfc_least_capable_settings - helper function for EDC rsp processing
3857  * @phba: pointer to lpfc hba data structure.
3858  * @pcgd: pointer to congestion detection descriptor in EDC rsp.
3859  *
3860  * This helper routine determines the least capable setting for
3861  * congestion signals, signal freq, including scale, from the
3862  * congestion detection descriptor in the EDC rsp.  The routine
3863  * sets @phba values in preparation for a set_featues mailbox.
3864  **/
3865 static void
3866 lpfc_least_capable_settings(struct lpfc_hba *phba,
3867                             struct fc_diag_cg_sig_desc *pcgd)
3868 {
3869         u32 rsp_sig_cap = 0, drv_sig_cap = 0;
3870         u32 rsp_sig_freq_cyc = 0, rsp_sig_freq_scale = 0;
3871
3872         /* Get rsp signal and frequency capabilities.  */
3873         rsp_sig_cap = be32_to_cpu(pcgd->xmt_signal_capability);
3874         rsp_sig_freq_cyc = be16_to_cpu(pcgd->xmt_signal_frequency.count);
3875         rsp_sig_freq_scale = be16_to_cpu(pcgd->xmt_signal_frequency.units);
3876
3877         /* If the Fport does not support signals. Set FPIN only */
3878         if (rsp_sig_cap == EDC_CG_SIG_NOTSUPPORTED)
3879                 goto out_no_support;
3880
3881         /* Apply the xmt scale to the xmt cycle to get the correct frequency.
3882          * Adapter default is 100 millisSeconds.  Convert all xmt cycle values
3883          * to milliSeconds.
3884          */
3885         switch (rsp_sig_freq_scale) {
3886         case EDC_CG_SIGFREQ_SEC:
3887                 rsp_sig_freq_cyc *= MSEC_PER_SEC;
3888                 break;
3889         case EDC_CG_SIGFREQ_MSEC:
3890                 rsp_sig_freq_cyc = 1;
3891                 break;
3892         default:
3893                 goto out_no_support;
3894         }
3895
3896         /* Convenient shorthand. */
3897         drv_sig_cap = phba->cgn_reg_signal;
3898
3899         /* Choose the least capable frequency. */
3900         if (rsp_sig_freq_cyc > phba->cgn_sig_freq)
3901                 phba->cgn_sig_freq = rsp_sig_freq_cyc;
3902
3903         /* Should be some common signals support. Settle on least capable
3904          * signal and adjust FPIN values. Initialize defaults to ease the
3905          * decision.
3906          */
3907         phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
3908         phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
3909         if (rsp_sig_cap == EDC_CG_SIG_WARN_ONLY &&
3910             (drv_sig_cap == EDC_CG_SIG_WARN_ONLY ||
3911              drv_sig_cap == EDC_CG_SIG_WARN_ALARM)) {
3912                 phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
3913                 phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
3914         }
3915         if (rsp_sig_cap == EDC_CG_SIG_WARN_ALARM) {
3916                 if (drv_sig_cap == EDC_CG_SIG_WARN_ALARM) {
3917                         phba->cgn_reg_signal = EDC_CG_SIG_WARN_ALARM;
3918                         phba->cgn_reg_fpin = LPFC_CGN_FPIN_NONE;
3919                 }
3920                 if (drv_sig_cap == EDC_CG_SIG_WARN_ONLY) {
3921                         phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
3922                         phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
3923                 }
3924         }
3925
3926         /* We are NOT recording signal frequency in congestion info buffer */
3927         return;
3928
3929 out_no_support:
3930         phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
3931         phba->cgn_sig_freq = 0;
3932         phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
3933 }
3934
3935 DECLARE_ENUM2STR_LOOKUP(lpfc_get_tlv_dtag_nm, fc_ls_tlv_dtag,
3936                         FC_LS_TLV_DTAG_INIT);
3937
3938 /**
3939  * lpfc_cmpl_els_edc - Completion callback function for EDC
3940  * @phba: pointer to lpfc hba data structure.
3941  * @cmdiocb: pointer to lpfc command iocb data structure.
3942  * @rspiocb: pointer to lpfc response iocb data structure.
3943  *
3944  * This routine is the completion callback function for issuing the Exchange
3945  * Diagnostic Capabilities (EDC) command. The driver issues an EDC to
3946  * notify the FPort of its Congestion and Link Fault capabilities.  This
3947  * routine parses the FPort's response and decides on the least common
3948  * values applicable to both FPort and NPort for Warnings and Alarms that
3949  * are communicated via hardware signals.
3950  **/
3951 static void
3952 lpfc_cmpl_els_edc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3953                   struct lpfc_iocbq *rspiocb)
3954 {
3955         IOCB_t *irsp_iocb;
3956         struct fc_els_edc_resp *edc_rsp;
3957         struct fc_tlv_desc *tlv;
3958         struct fc_diag_cg_sig_desc *pcgd;
3959         struct fc_diag_lnkflt_desc *plnkflt;
3960         struct lpfc_dmabuf *pcmd, *prsp;
3961         const char *dtag_nm;
3962         u32 *pdata, dtag;
3963         int desc_cnt = 0, bytes_remain;
3964         bool rcv_cap_desc = false;
3965         struct lpfc_nodelist *ndlp;
3966         u32 ulp_status, ulp_word4, tmo, did, iotag;
3967
3968         ndlp = cmdiocb->ndlp;
3969
3970         ulp_status = get_job_ulpstatus(phba, rspiocb);
3971         ulp_word4 = get_job_word4(phba, rspiocb);
3972         did = get_job_els_rsp64_did(phba, rspiocb);
3973
3974         if (phba->sli_rev == LPFC_SLI_REV4) {
3975                 tmo = get_wqe_tmo(rspiocb);
3976                 iotag = get_wqe_reqtag(rspiocb);
3977         } else {
3978                 irsp_iocb = &rspiocb->iocb;
3979                 tmo = irsp_iocb->ulpTimeout;
3980                 iotag = irsp_iocb->ulpIoTag;
3981         }
3982
3983         lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
3984                               "EDC cmpl:    status:x%x/x%x did:x%x",
3985                               ulp_status, ulp_word4, did);
3986
3987         /* ELS cmd tag <ulpIoTag> completes */
3988         lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3989                         "4201 EDC cmd tag x%x completes Data: x%x x%x x%x\n",
3990                         iotag, ulp_status, ulp_word4, tmo);
3991
3992         pcmd = cmdiocb->cmd_dmabuf;
3993         if (!pcmd)
3994                 goto out;
3995
3996         pdata = (u32 *)pcmd->virt;
3997         if (!pdata)
3998                 goto out;
3999
4000         /* Need to clear signal values, send features MB and RDF with FPIN. */
4001         if (ulp_status)
4002                 goto out;
4003
4004         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
4005         if (!prsp)
4006                 goto out;
4007
4008         edc_rsp = prsp->virt;
4009         if (!edc_rsp)
4010                 goto out;
4011
4012         /* ELS cmd tag <ulpIoTag> completes */
4013         lpfc_printf_log(phba, KERN_INFO,
4014                         LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
4015                         "4676 Fabric EDC Rsp: "
4016                         "0x%02x, 0x%08x\n",
4017                         edc_rsp->acc_hdr.la_cmd,
4018                         be32_to_cpu(edc_rsp->desc_list_len));
4019
4020         if (!lpfc_is_els_acc_rsp(prsp))
4021                 goto out;
4022
4023         /*
4024          * Payload length in bytes is the response descriptor list
4025          * length minus the 12 bytes of Link Service Request
4026          * Information descriptor in the reply.
4027          */
4028         bytes_remain = be32_to_cpu(edc_rsp->desc_list_len) -
4029                                    sizeof(struct fc_els_lsri_desc);
4030         if (bytes_remain <= 0)
4031                 goto out;
4032
4033         tlv = edc_rsp->desc;
4034
4035         /*
4036          * cycle through EDC diagnostic descriptors to find the
4037          * congestion signaling capability descriptor
4038          */
4039         while (bytes_remain) {
4040                 if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
4041                         lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4042                                         "6461 Truncated TLV hdr on "
4043                                         "Diagnostic descriptor[%d]\n",
4044                                         desc_cnt);
4045                         goto out;
4046                 }
4047
4048                 dtag = be32_to_cpu(tlv->desc_tag);
4049                 switch (dtag) {
4050                 case ELS_DTAG_LNK_FAULT_CAP:
4051                         if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4052                             FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4053                                         sizeof(struct fc_diag_lnkflt_desc)) {
4054                                 lpfc_printf_log(phba, KERN_WARNING,
4055                                         LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
4056                                         "6462 Truncated Link Fault Diagnostic "
4057                                         "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4058                                         desc_cnt, bytes_remain,
4059                                         FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4060                                         sizeof(struct fc_diag_lnkflt_desc));
4061                                 goto out;
4062                         }
4063                         plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
4064                         lpfc_printf_log(phba, KERN_INFO,
4065                                 LOG_ELS | LOG_LDS_EVENT,
4066                                 "4617 Link Fault Desc Data: 0x%08x 0x%08x "
4067                                 "0x%08x 0x%08x 0x%08x\n",
4068                                 be32_to_cpu(plnkflt->desc_tag),
4069                                 be32_to_cpu(plnkflt->desc_len),
4070                                 be32_to_cpu(
4071                                         plnkflt->degrade_activate_threshold),
4072                                 be32_to_cpu(
4073                                         plnkflt->degrade_deactivate_threshold),
4074                                 be32_to_cpu(plnkflt->fec_degrade_interval));
4075                         break;
4076                 case ELS_DTAG_CG_SIGNAL_CAP:
4077                         if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4078                             FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4079                                         sizeof(struct fc_diag_cg_sig_desc)) {
4080                                 lpfc_printf_log(
4081                                         phba, KERN_WARNING, LOG_CGN_MGMT,
4082                                         "6463 Truncated Cgn Signal Diagnostic "
4083                                         "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4084                                         desc_cnt, bytes_remain,
4085                                         FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4086                                         sizeof(struct fc_diag_cg_sig_desc));
4087                                 goto out;
4088                         }
4089
4090                         pcgd = (struct fc_diag_cg_sig_desc *)tlv;
4091                         lpfc_printf_log(
4092                                 phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4093                                 "4616 CGN Desc Data: 0x%08x 0x%08x "
4094                                 "0x%08x 0x%04x 0x%04x 0x%08x 0x%04x 0x%04x\n",
4095                                 be32_to_cpu(pcgd->desc_tag),
4096                                 be32_to_cpu(pcgd->desc_len),
4097                                 be32_to_cpu(pcgd->xmt_signal_capability),
4098                                 be16_to_cpu(pcgd->xmt_signal_frequency.count),
4099                                 be16_to_cpu(pcgd->xmt_signal_frequency.units),
4100                                 be32_to_cpu(pcgd->rcv_signal_capability),
4101                                 be16_to_cpu(pcgd->rcv_signal_frequency.count),
4102                                 be16_to_cpu(pcgd->rcv_signal_frequency.units));
4103
4104                         /* Compare driver and Fport capabilities and choose
4105                          * least common.
4106                          */
4107                         lpfc_least_capable_settings(phba, pcgd);
4108                         rcv_cap_desc = true;
4109                         break;
4110                 default:
4111                         dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
4112                         lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4113                                         "4919 unknown Diagnostic "
4114                                         "Descriptor[%d]: tag x%x (%s)\n",
4115                                         desc_cnt, dtag, dtag_nm);
4116                 }
4117
4118                 bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
4119                 tlv = fc_tlv_next_desc(tlv);
4120                 desc_cnt++;
4121         }
4122
4123 out:
4124         if (!rcv_cap_desc) {
4125                 phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
4126                 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4127                 phba->cgn_sig_freq = 0;
4128                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
4129                                 "4202 EDC rsp error - sending RDF "
4130                                 "for FPIN only.\n");
4131         }
4132
4133         lpfc_config_cgn_signal(phba);
4134
4135         /* Check to see if link went down during discovery */
4136         lpfc_els_chk_latt(phba->pport);
4137         lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
4138                               "EDC Cmpl:     did:x%x refcnt %d",
4139                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4140         lpfc_els_free_iocb(phba, cmdiocb);
4141         lpfc_nlp_put(ndlp);
4142 }
4143
4144 static void
4145 lpfc_format_edc_lft_desc(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
4146 {
4147         struct fc_diag_lnkflt_desc *lft = (struct fc_diag_lnkflt_desc *)tlv;
4148
4149         lft->desc_tag = cpu_to_be32(ELS_DTAG_LNK_FAULT_CAP);
4150         lft->desc_len = cpu_to_be32(
4151                 FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_lnkflt_desc));
4152
4153         lft->degrade_activate_threshold =
4154                 cpu_to_be32(phba->degrade_activate_threshold);
4155         lft->degrade_deactivate_threshold =
4156                 cpu_to_be32(phba->degrade_deactivate_threshold);
4157         lft->fec_degrade_interval = cpu_to_be32(phba->fec_degrade_interval);
4158 }
4159
4160 static void
4161 lpfc_format_edc_cgn_desc(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
4162 {
4163         struct fc_diag_cg_sig_desc *cgd = (struct fc_diag_cg_sig_desc *)tlv;
4164
4165         /* We are assuming cgd was zero'ed before calling this routine */
4166
4167         /* Configure the congestion detection capability */
4168         cgd->desc_tag = cpu_to_be32(ELS_DTAG_CG_SIGNAL_CAP);
4169
4170         /* Descriptor len doesn't include the tag or len fields. */
4171         cgd->desc_len = cpu_to_be32(
4172                 FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_cg_sig_desc));
4173
4174         /* xmt_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4175          * xmt_signal_frequency.count already set to 0.
4176          * xmt_signal_frequency.units already set to 0.
4177          */
4178
4179         if (phba->cmf_active_mode == LPFC_CFG_OFF) {
4180                 /* rcv_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4181                  * rcv_signal_frequency.count already set to 0.
4182                  * rcv_signal_frequency.units already set to 0.
4183                  */
4184                 phba->cgn_sig_freq = 0;
4185                 return;
4186         }
4187         switch (phba->cgn_reg_signal) {
4188         case EDC_CG_SIG_WARN_ONLY:
4189                 cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ONLY);
4190                 break;
4191         case EDC_CG_SIG_WARN_ALARM:
4192                 cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ALARM);
4193                 break;
4194         default:
4195                 /* rcv_signal_capability left 0 thus no support */
4196                 break;
4197         }
4198
4199         /* We start negotiation with lpfc_fabric_cgn_frequency, after
4200          * the completion we settle on the higher frequency.
4201          */
4202         cgd->rcv_signal_frequency.count =
4203                 cpu_to_be16(lpfc_fabric_cgn_frequency);
4204         cgd->rcv_signal_frequency.units =
4205                 cpu_to_be16(EDC_CG_SIGFREQ_MSEC);
4206 }
4207
4208 static bool
4209 lpfc_link_is_lds_capable(struct lpfc_hba *phba)
4210 {
4211         if (!(phba->lmt & LMT_64Gb))
4212                 return false;
4213         if (phba->sli_rev != LPFC_SLI_REV4)
4214                 return false;
4215
4216         if (phba->sli4_hba.conf_trunk) {
4217                 if (phba->trunk_link.phy_lnk_speed == LPFC_USER_LINK_SPEED_64G)
4218                         return true;
4219         } else if (phba->fc_linkspeed == LPFC_LINK_SPEED_64GHZ) {
4220                 return true;
4221         }
4222         return false;
4223 }
4224
4225  /**
4226   * lpfc_issue_els_edc - Exchange Diagnostic Capabilities with the fabric.
4227   * @vport: pointer to a host virtual N_Port data structure.
4228   * @retry: retry counter for the command iocb.
4229   *
4230   * This routine issues an ELS EDC to the F-Port Controller to communicate
4231   * this N_Port's support of hardware signals in its Congestion
4232   * Capabilities Descriptor.
4233   *
4234   * Note: This routine does not check if one or more signals are
4235   * set in the cgn_reg_signal parameter.  The caller makes the
4236   * decision to enforce cgn_reg_signal as nonzero or zero depending
4237   * on the conditions.  During Fabric requests, the driver
4238   * requires cgn_reg_signals to be nonzero.  But a dynamic request
4239   * to set the congestion mode to OFF from Monitor or Manage
4240   * would correctly issue an EDC with no signals enabled to
4241   * turn off switch functionality and then update the FW.
4242   *
4243   * Return code
4244   *   0 - Successfully issued edc command
4245   *   1 - Failed to issue edc command
4246   **/
4247 int
4248 lpfc_issue_els_edc(struct lpfc_vport *vport, uint8_t retry)
4249 {
4250         struct lpfc_hba  *phba = vport->phba;
4251         struct lpfc_iocbq *elsiocb;
4252         struct fc_els_edc *edc_req;
4253         struct fc_tlv_desc *tlv;
4254         u16 cmdsize;
4255         struct lpfc_nodelist *ndlp;
4256         u8 *pcmd = NULL;
4257         u32 cgn_desc_size, lft_desc_size;
4258         int rc;
4259
4260         if (vport->port_type == LPFC_NPIV_PORT)
4261                 return -EACCES;
4262
4263         ndlp = lpfc_findnode_did(vport, Fabric_DID);
4264         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
4265                 return -ENODEV;
4266
4267         cgn_desc_size = (phba->cgn_init_reg_signal) ?
4268                                 sizeof(struct fc_diag_cg_sig_desc) : 0;
4269         lft_desc_size = (lpfc_link_is_lds_capable(phba)) ?
4270                                 sizeof(struct fc_diag_lnkflt_desc) : 0;
4271         cmdsize = cgn_desc_size + lft_desc_size;
4272
4273         /* Skip EDC if no applicable descriptors */
4274         if (!cmdsize)
4275                 goto try_rdf;
4276
4277         cmdsize += sizeof(struct fc_els_edc);
4278         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
4279                                      ndlp->nlp_DID, ELS_CMD_EDC);
4280         if (!elsiocb)
4281                 goto try_rdf;
4282
4283         /* Configure the payload for the supported Diagnostics capabilities. */
4284         pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
4285         memset(pcmd, 0, cmdsize);
4286         edc_req = (struct fc_els_edc *)pcmd;
4287         edc_req->desc_len = cpu_to_be32(cgn_desc_size + lft_desc_size);
4288         edc_req->edc_cmd = ELS_EDC;
4289         tlv = edc_req->desc;
4290
4291         if (cgn_desc_size) {
4292                 lpfc_format_edc_cgn_desc(phba, tlv);
4293                 phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
4294                 tlv = fc_tlv_next_desc(tlv);
4295         }
4296
4297         if (lft_desc_size)
4298                 lpfc_format_edc_lft_desc(phba, tlv);
4299
4300         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4301                          "4623 Xmit EDC to remote "
4302                          "NPORT x%x reg_sig x%x reg_fpin:x%x\n",
4303                          ndlp->nlp_DID, phba->cgn_reg_signal,
4304                          phba->cgn_reg_fpin);
4305
4306         elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
4307         elsiocb->ndlp = lpfc_nlp_get(ndlp);
4308         if (!elsiocb->ndlp) {
4309                 lpfc_els_free_iocb(phba, elsiocb);
4310                 return -EIO;
4311         }
4312
4313         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4314                               "Issue EDC:     did:x%x refcnt %d",
4315                               ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4316         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4317         if (rc == IOCB_ERROR) {
4318                 /* The additional lpfc_nlp_put will cause the following
4319                  * lpfc_els_free_iocb routine to trigger the rlease of
4320                  * the node.
4321                  */
4322                 lpfc_els_free_iocb(phba, elsiocb);
4323                 lpfc_nlp_put(ndlp);
4324                 goto try_rdf;
4325         }
4326         return 0;
4327 try_rdf:
4328         phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
4329         phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4330         rc = lpfc_issue_els_rdf(vport, 0);
4331         return rc;
4332 }
4333
4334 /**
4335  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
4336  * @vport: pointer to a host virtual N_Port data structure.
4337  * @nlp: pointer to a node-list data structure.
4338  *
4339  * This routine cancels the timer with a delayed IOCB-command retry for
4340  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
4341  * removes the ELS retry event if it presents. In addition, if the
4342  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
4343  * commands are sent for the @vport's nodes that require issuing discovery
4344  * ADISC.
4345  **/
4346 void
4347 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
4348 {
4349         struct lpfc_work_evt *evtp;
4350
4351         if (!test_and_clear_bit(NLP_DELAY_TMO, &nlp->nlp_flag))
4352                 return;
4353         del_timer_sync(&nlp->nlp_delayfunc);
4354         nlp->nlp_last_elscmd = 0;
4355         if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
4356                 list_del_init(&nlp->els_retry_evt.evt_listp);
4357                 /* Decrement nlp reference count held for the delayed retry */
4358                 evtp = &nlp->els_retry_evt;
4359                 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
4360         }
4361         if (test_and_clear_bit(NLP_NPR_2B_DISC, &nlp->nlp_flag)) {
4362                 if (vport->num_disc_nodes) {
4363                         if (vport->port_state < LPFC_VPORT_READY) {
4364                                 /* Check if there are more ADISCs to be sent */
4365                                 lpfc_more_adisc(vport);
4366                         } else {
4367                                 /* Check if there are more PLOGIs to be sent */
4368                                 lpfc_more_plogi(vport);
4369                                 if (vport->num_disc_nodes == 0) {
4370                                         clear_bit(FC_NDISC_ACTIVE,
4371                                                   &vport->fc_flag);
4372                                         lpfc_can_disctmo(vport);
4373                                         lpfc_end_rscn(vport);
4374                                 }
4375                         }
4376                 }
4377         }
4378         return;
4379 }
4380
4381 /**
4382  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
4383  * @t: pointer to the timer function associated data (ndlp).
4384  *
4385  * This routine is invoked by the ndlp delayed-function timer to check
4386  * whether there is any pending ELS retry event(s) with the node. If not, it
4387  * simply returns. Otherwise, if there is at least one ELS delayed event, it
4388  * adds the delayed events to the HBA work list and invokes the
4389  * lpfc_worker_wake_up() routine to wake up worker thread to process the
4390  * event. Note that lpfc_nlp_get() is called before posting the event to
4391  * the work list to hold reference count of ndlp so that it guarantees the
4392  * reference to ndlp will still be available when the worker thread gets
4393  * to the event associated with the ndlp.
4394  **/
4395 void
4396 lpfc_els_retry_delay(struct timer_list *t)
4397 {
4398         struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
4399         struct lpfc_vport *vport = ndlp->vport;
4400         struct lpfc_hba   *phba = vport->phba;
4401         unsigned long flags;
4402         struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
4403
4404         /* Hold a node reference for outstanding queued work */
4405         if (!lpfc_nlp_get(ndlp))
4406                 return;
4407
4408         spin_lock_irqsave(&phba->hbalock, flags);
4409         if (!list_empty(&evtp->evt_listp)) {
4410                 spin_unlock_irqrestore(&phba->hbalock, flags);
4411                 lpfc_nlp_put(ndlp);
4412                 return;
4413         }
4414
4415         evtp->evt_arg1 = ndlp;
4416         evtp->evt = LPFC_EVT_ELS_RETRY;
4417         list_add_tail(&evtp->evt_listp, &phba->work_list);
4418         spin_unlock_irqrestore(&phba->hbalock, flags);
4419
4420         lpfc_worker_wake_up(phba);
4421 }
4422
4423 /**
4424  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
4425  * @ndlp: pointer to a node-list data structure.
4426  *
4427  * This routine is the worker-thread handler for processing the @ndlp delayed
4428  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
4429  * the last ELS command from the associated ndlp and invokes the proper ELS
4430  * function according to the delayed ELS command to retry the command.
4431  **/
4432 void
4433 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
4434 {
4435         struct lpfc_vport *vport = ndlp->vport;
4436         uint32_t cmd, retry;
4437
4438         spin_lock_irq(&ndlp->lock);
4439         cmd = ndlp->nlp_last_elscmd;
4440         ndlp->nlp_last_elscmd = 0;
4441         spin_unlock_irq(&ndlp->lock);
4442
4443         if (!test_and_clear_bit(NLP_DELAY_TMO, &ndlp->nlp_flag))
4444                 return;
4445
4446         /*
4447          * If a discovery event readded nlp_delayfunc after timer
4448          * firing and before processing the timer, cancel the
4449          * nlp_delayfunc.
4450          */
4451         del_timer_sync(&ndlp->nlp_delayfunc);
4452         retry = ndlp->nlp_retry;
4453         ndlp->nlp_retry = 0;
4454
4455         switch (cmd) {
4456         case ELS_CMD_FLOGI:
4457                 lpfc_issue_els_flogi(vport, ndlp, retry);
4458                 break;
4459         case ELS_CMD_PLOGI:
4460                 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
4461                         ndlp->nlp_prev_state = ndlp->nlp_state;
4462                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4463                 }
4464                 break;
4465         case ELS_CMD_ADISC:
4466                 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
4467                         ndlp->nlp_prev_state = ndlp->nlp_state;
4468                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4469                 }
4470                 break;
4471         case ELS_CMD_PRLI:
4472         case ELS_CMD_NVMEPRLI:
4473                 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
4474                         ndlp->nlp_prev_state = ndlp->nlp_state;
4475                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4476                 }
4477                 break;
4478         case ELS_CMD_LOGO:
4479                 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
4480                         ndlp->nlp_prev_state = ndlp->nlp_state;
4481                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4482                 }
4483                 break;
4484         case ELS_CMD_FDISC:
4485                 if (!test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag))
4486                         lpfc_issue_els_fdisc(vport, ndlp, retry);
4487                 break;
4488         }
4489         return;
4490 }
4491
4492 /**
4493  * lpfc_link_reset - Issue link reset
4494  * @vport: pointer to a virtual N_Port data structure.
4495  *
4496  * This routine performs link reset by sending INIT_LINK mailbox command.
4497  * For SLI-3 adapter, link attention interrupt is enabled before issuing
4498  * INIT_LINK mailbox command.
4499  *
4500  * Return code
4501  *   0 - Link reset initiated successfully
4502  *   1 - Failed to initiate link reset
4503  **/
4504 int
4505 lpfc_link_reset(struct lpfc_vport *vport)
4506 {
4507         struct lpfc_hba *phba = vport->phba;
4508         LPFC_MBOXQ_t *mbox;
4509         uint32_t control;
4510         int rc;
4511
4512         lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4513                          "2851 Attempt link reset\n");
4514         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4515         if (!mbox) {
4516                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4517                                 "2852 Failed to allocate mbox memory");
4518                 return 1;
4519         }
4520
4521         /* Enable Link attention interrupts */
4522         if (phba->sli_rev <= LPFC_SLI_REV3) {
4523                 spin_lock_irq(&phba->hbalock);
4524                 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4525                 control = readl(phba->HCregaddr);
4526                 control |= HC_LAINT_ENA;
4527                 writel(control, phba->HCregaddr);
4528                 readl(phba->HCregaddr); /* flush */
4529                 spin_unlock_irq(&phba->hbalock);
4530         }
4531
4532         lpfc_init_link(phba, mbox, phba->cfg_topology,
4533                        phba->cfg_link_speed);
4534         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4535         mbox->vport = vport;
4536         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4537         if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
4538                 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4539                                 "2853 Failed to issue INIT_LINK "
4540                                 "mbox command, rc:x%x\n", rc);
4541                 mempool_free(mbox, phba->mbox_mem_pool);
4542                 return 1;
4543         }
4544
4545         return 0;
4546 }
4547
4548 /**
4549  * lpfc_els_retry - Make retry decision on an els command iocb
4550  * @phba: pointer to lpfc hba data structure.
4551  * @cmdiocb: pointer to lpfc command iocb data structure.
4552  * @rspiocb: pointer to lpfc response iocb data structure.
4553  *
4554  * This routine makes a retry decision on an ELS command IOCB, which has
4555  * failed. The following ELS IOCBs use this function for retrying the command
4556  * when previously issued command responsed with error status: FLOGI, PLOGI,
4557  * PRLI, ADISC and FDISC. Based on the ELS command type and the
4558  * returned error status, it makes the decision whether a retry shall be
4559  * issued for the command, and whether a retry shall be made immediately or
4560  * delayed. In the former case, the corresponding ELS command issuing-function
4561  * is called to retry the command. In the later case, the ELS command shall
4562  * be posted to the ndlp delayed event and delayed function timer set to the
4563  * ndlp for the delayed command issusing.
4564  *
4565  * Return code
4566  *   0 - No retry of els command is made
4567  *   1 - Immediate or delayed retry of els command is made
4568  **/
4569 static int
4570 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4571                struct lpfc_iocbq *rspiocb)
4572 {
4573         struct lpfc_vport *vport = cmdiocb->vport;
4574         union lpfc_wqe128 *irsp = &rspiocb->wqe;
4575         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
4576         struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
4577         uint32_t *elscmd;
4578         struct ls_rjt stat;
4579         int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
4580         int logerr = 0;
4581         uint32_t cmd = 0;
4582         uint32_t did;
4583         int link_reset = 0, rc;
4584         u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
4585         u32 ulp_word4 = get_job_word4(phba, rspiocb);
4586
4587
4588         /* Note: cmd_dmabuf may be 0 for internal driver abort
4589          * of delays ELS command.
4590          */
4591
4592         if (pcmd && pcmd->virt) {
4593                 elscmd = (uint32_t *) (pcmd->virt);
4594                 cmd = *elscmd++;
4595         }
4596
4597         if (ndlp)
4598                 did = ndlp->nlp_DID;
4599         else {
4600                 /* We should only hit this case for retrying PLOGI */
4601                 did = get_job_els_rsp64_did(phba, rspiocb);
4602                 ndlp = lpfc_findnode_did(vport, did);
4603                 if (!ndlp && (cmd != ELS_CMD_PLOGI))
4604                         return 0;
4605         }
4606
4607         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4608                 "Retry ELS:       wd7:x%x wd4:x%x did:x%x",
4609                 *(((uint32_t *)irsp) + 7), ulp_word4, did);
4610
4611         switch (ulp_status) {
4612         case IOSTAT_FCP_RSP_ERROR:
4613                 break;
4614         case IOSTAT_REMOTE_STOP:
4615                 if (phba->sli_rev == LPFC_SLI_REV4) {
4616                         /* This IO was aborted by the target, we don't
4617                          * know the rxid and because we did not send the
4618                          * ABTS we cannot generate and RRQ.
4619                          */
4620                         lpfc_set_rrq_active(phba, ndlp,
4621                                          cmdiocb->sli4_lxritag, 0, 0);
4622                 }
4623                 break;
4624         case IOSTAT_LOCAL_REJECT:
4625                 switch ((ulp_word4 & IOERR_PARAM_MASK)) {
4626                 case IOERR_LOOP_OPEN_FAILURE:
4627                         if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
4628                                 delay = 1000;
4629                         retry = 1;
4630                         break;
4631
4632                 case IOERR_ILLEGAL_COMMAND:
4633                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4634                                          "0124 Retry illegal cmd x%x "
4635                                          "retry:x%x delay:x%x\n",
4636                                          cmd, cmdiocb->retry, delay);
4637                         retry = 1;
4638                         /* All command's retry policy */
4639                         maxretry = 8;
4640                         if (cmdiocb->retry > 2)
4641                                 delay = 1000;
4642                         break;
4643
4644                 case IOERR_NO_RESOURCES:
4645                         logerr = 1; /* HBA out of resources */
4646                         retry = 1;
4647                         if (cmdiocb->retry > 100)
4648                                 delay = 100;
4649                         maxretry = 250;
4650                         break;
4651
4652                 case IOERR_ILLEGAL_FRAME:
4653                         delay = 100;
4654                         retry = 1;
4655                         break;
4656
4657                 case IOERR_INVALID_RPI:
4658                         if (cmd == ELS_CMD_PLOGI &&
4659                             did == NameServer_DID) {
4660                                 /* Continue forever if plogi to */
4661                                 /* the nameserver fails */
4662                                 maxretry = 0;
4663                                 delay = 100;
4664                         } else if (cmd == ELS_CMD_PRLI &&
4665                                    ndlp->nlp_state != NLP_STE_PRLI_ISSUE) {
4666                                 /* State-command disagreement.  The PRLI was
4667                                  * failed with an invalid rpi meaning there
4668                                  * some unexpected state change.  Don't retry.
4669                                  */
4670                                 maxretry = 0;
4671                                 retry = 0;
4672                                 break;
4673                         }
4674                         retry = 1;
4675                         break;
4676
4677                 case IOERR_SEQUENCE_TIMEOUT:
4678                         if (cmd == ELS_CMD_PLOGI &&
4679                             did == NameServer_DID &&
4680                             (cmdiocb->retry + 1) == maxretry) {
4681                                 /* Reset the Link */
4682                                 link_reset = 1;
4683                                 break;
4684                         }
4685                         retry = 1;
4686                         delay = 100;
4687                         break;
4688                 case IOERR_SLI_ABORTED:
4689                         /* Retry ELS PLOGI command?
4690                          * Possibly the rport just wasn't ready.
4691                          */
4692                         if (cmd == ELS_CMD_PLOGI) {
4693                                 /* No retry if state change */
4694                                 if (ndlp &&
4695                                     ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
4696                                         goto out_retry;
4697                                 retry = 1;
4698                                 maxretry = 2;
4699                         }
4700                         break;
4701                 }
4702                 break;
4703
4704         case IOSTAT_NPORT_RJT:
4705         case IOSTAT_FABRIC_RJT:
4706                 if (ulp_word4 & RJT_UNAVAIL_TEMP) {
4707                         retry = 1;
4708                         break;
4709                 }
4710                 break;
4711
4712         case IOSTAT_NPORT_BSY:
4713         case IOSTAT_FABRIC_BSY:
4714                 logerr = 1; /* Fabric / Remote NPort out of resources */
4715                 retry = 1;
4716                 break;
4717
4718         case IOSTAT_LS_RJT:
4719                 stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
4720                 /* Added for Vendor specifc support
4721                  * Just keep retrying for these Rsn / Exp codes
4722                  */
4723                 if (test_bit(FC_PT2PT, &vport->fc_flag) &&
4724                     cmd == ELS_CMD_NVMEPRLI) {
4725                         switch (stat.un.b.lsRjtRsnCode) {
4726                         case LSRJT_UNABLE_TPC:
4727                         case LSRJT_INVALID_CMD:
4728                         case LSRJT_LOGICAL_ERR:
4729                         case LSRJT_CMD_UNSUPPORTED:
4730                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
4731                                                  "0168 NVME PRLI LS_RJT "
4732                                                  "reason %x port doesn't "
4733                                                  "support NVME, disabling NVME\n",
4734                                                  stat.un.b.lsRjtRsnCode);
4735                                 retry = 0;
4736                                 set_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
4737                                 goto out_retry;
4738                         }
4739                 }
4740                 switch (stat.un.b.lsRjtRsnCode) {
4741                 case LSRJT_UNABLE_TPC:
4742                         /* Special case for PRLI LS_RJTs. Recall that lpfc
4743                          * uses a single routine to issue both PRLI FC4 types.
4744                          * If the PRLI is rejected because that FC4 type
4745                          * isn't really supported, don't retry and cause
4746                          * multiple transport registrations.  Otherwise, parse
4747                          * the reason code/reason code explanation and take the
4748                          * appropriate action.
4749                          */
4750                         lpfc_printf_vlog(vport, KERN_INFO,
4751                                          LOG_DISCOVERY | LOG_ELS | LOG_NODE,
4752                                          "0153 ELS cmd x%x LS_RJT by x%x. "
4753                                          "RsnCode x%x RsnCodeExp x%x\n",
4754                                          cmd, did, stat.un.b.lsRjtRsnCode,
4755                                          stat.un.b.lsRjtRsnCodeExp);
4756
4757                         switch (stat.un.b.lsRjtRsnCodeExp) {
4758                         case LSEXP_CANT_GIVE_DATA:
4759                         case LSEXP_CMD_IN_PROGRESS:
4760                                 if (cmd == ELS_CMD_PLOGI) {
4761                                         delay = 1000;
4762                                         maxretry = 48;
4763                                 }
4764                                 retry = 1;
4765                                 break;
4766                         case LSEXP_REQ_UNSUPPORTED:
4767                         case LSEXP_NO_RSRC_ASSIGN:
4768                                 /* These explanation codes get no retry. */
4769                                 if (cmd == ELS_CMD_PRLI ||
4770                                     cmd == ELS_CMD_NVMEPRLI)
4771                                         break;
4772                                 fallthrough;
4773                         default:
4774                                 /* Limit the delay and retry action to a limited
4775                                  * cmd set.  There are other ELS commands where
4776                                  * a retry is not expected.
4777                                  */
4778                                 if (cmd == ELS_CMD_PLOGI ||
4779                                     cmd == ELS_CMD_PRLI ||
4780                                     cmd == ELS_CMD_NVMEPRLI) {
4781                                         delay = 1000;
4782                                         maxretry = lpfc_max_els_tries + 1;
4783                                         retry = 1;
4784                                 }
4785                                 break;
4786                         }
4787
4788                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4789                           (cmd == ELS_CMD_FDISC) &&
4790                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
4791                                 lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
4792                                               "0125 FDISC (x%x). "
4793                                               "Fabric out of resources\n",
4794                                               stat.un.lsRjtError);
4795                                 lpfc_vport_set_state(vport,
4796                                                      FC_VPORT_NO_FABRIC_RSCS);
4797                         }
4798                         break;
4799
4800                 case LSRJT_LOGICAL_BSY:
4801                         if ((cmd == ELS_CMD_PLOGI) ||
4802                             (cmd == ELS_CMD_PRLI) ||
4803                             (cmd == ELS_CMD_NVMEPRLI)) {
4804                                 delay = 1000;
4805                                 maxretry = 48;
4806                         } else if (cmd == ELS_CMD_FDISC) {
4807                                 /* FDISC retry policy */
4808                                 maxretry = 48;
4809                                 if (cmdiocb->retry >= 32)
4810                                         delay = 1000;
4811                         }
4812                         retry = 1;
4813                         break;
4814
4815                 case LSRJT_LOGICAL_ERR:
4816                         /* There are some cases where switches return this
4817                          * error when they are not ready and should be returning
4818                          * Logical Busy. We should delay every time.
4819                          */
4820                         if (cmd == ELS_CMD_FDISC &&
4821                             stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4822                                 maxretry = 3;
4823                                 delay = 1000;
4824                                 retry = 1;
4825                         } else if (cmd == ELS_CMD_FLOGI &&
4826                                    stat.un.b.lsRjtRsnCodeExp ==
4827                                                 LSEXP_NOTHING_MORE) {
4828                                 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4829                                 retry = 1;
4830                                 lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
4831                                               "0820 FLOGI (x%x). "
4832                                               "BBCredit Not Supported\n",
4833                                               stat.un.lsRjtError);
4834                         }
4835                         break;
4836
4837                 case LSRJT_PROTOCOL_ERR:
4838                         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4839                           (cmd == ELS_CMD_FDISC) &&
4840                           ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4841                           (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4842                           ) {
4843                                 lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
4844                                               "0122 FDISC (x%x). "
4845                                               "Fabric Detected Bad WWN\n",
4846                                               stat.un.lsRjtError);
4847                                 lpfc_vport_set_state(vport,
4848                                                      FC_VPORT_FABRIC_REJ_WWN);
4849                         }
4850                         break;
4851                 case LSRJT_VENDOR_UNIQUE:
4852                         if ((stat.un.b.vendorUnique == 0x45) &&
4853                             (cmd == ELS_CMD_FLOGI)) {
4854                                 goto out_retry;
4855                         }
4856                         break;
4857                 case LSRJT_CMD_UNSUPPORTED:
4858                         /* lpfc nvmet returns this type of LS_RJT when it
4859                          * receives an FCP PRLI because lpfc nvmet only
4860                          * support NVME.  ELS request is terminated for FCP4
4861                          * on this rport.
4862                          */
4863                         if (stat.un.b.lsRjtRsnCodeExp ==
4864                             LSEXP_REQ_UNSUPPORTED) {
4865                                 if (cmd == ELS_CMD_PRLI)
4866                                         goto out_retry;
4867                         }
4868                         break;
4869                 }
4870                 break;
4871
4872         case IOSTAT_INTERMED_RSP:
4873         case IOSTAT_BA_RJT:
4874                 break;
4875
4876         default:
4877                 break;
4878         }
4879
4880         if (link_reset) {
4881                 rc = lpfc_link_reset(vport);
4882                 if (rc) {
4883                         /* Do not give up. Retry PLOGI one more time and attempt
4884                          * link reset if PLOGI fails again.
4885                          */
4886                         retry = 1;
4887                         delay = 100;
4888                         goto out_retry;
4889                 }
4890                 return 1;
4891         }
4892
4893         if (did == FDMI_DID)
4894                 retry = 1;
4895
4896         if ((cmd == ELS_CMD_FLOGI) &&
4897             (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
4898             !lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
4899                 /* FLOGI retry policy */
4900                 retry = 1;
4901                 /* retry FLOGI forever */
4902                 if (phba->link_flag != LS_LOOPBACK_MODE)
4903                         maxretry = 0;
4904                 else
4905                         maxretry = 2;
4906
4907                 if (cmdiocb->retry >= 100)
4908                         delay = 5000;
4909                 else if (cmdiocb->retry >= 32)
4910                         delay = 1000;
4911         } else if ((cmd == ELS_CMD_FDISC) &&
4912             !lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
4913                 /* retry FDISCs every second up to devloss */
4914                 retry = 1;
4915                 maxretry = vport->cfg_devloss_tmo;
4916                 delay = 1000;
4917         }
4918
4919         cmdiocb->retry++;
4920         if (maxretry && (cmdiocb->retry >= maxretry)) {
4921                 phba->fc_stat.elsRetryExceeded++;
4922                 retry = 0;
4923         }
4924
4925         if (test_bit(FC_UNLOADING, &vport->load_flag))
4926                 retry = 0;
4927
4928 out_retry:
4929         if (retry) {
4930                 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
4931                         /* Stop retrying PLOGI and FDISC if in FCF discovery */
4932                         if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4933                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4934                                                  "2849 Stop retry ELS command "
4935                                                  "x%x to remote NPORT x%x, "
4936                                                  "Data: x%x x%x\n", cmd, did,
4937                                                  cmdiocb->retry, delay);
4938                                 return 0;
4939                         }
4940                 }
4941
4942                 /* Retry ELS command <elsCmd> to remote NPORT <did> */
4943                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4944                                  "0107 Retry ELS command x%x to remote "
4945                                  "NPORT x%x Data: x%x x%x\n",
4946                                  cmd, did, cmdiocb->retry, delay);
4947
4948                 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
4949                         ((ulp_status != IOSTAT_LOCAL_REJECT) ||
4950                         ((ulp_word4 & IOERR_PARAM_MASK) !=
4951                         IOERR_NO_RESOURCES))) {
4952                         /* Don't reset timer for no resources */
4953
4954                         /* If discovery / RSCN timer is running, reset it */
4955                         if (timer_pending(&vport->fc_disctmo) ||
4956                             test_bit(FC_RSCN_MODE, &vport->fc_flag))
4957                                 lpfc_set_disctmo(vport);
4958                 }
4959
4960                 phba->fc_stat.elsXmitRetry++;
4961                 if (ndlp && delay) {
4962                         phba->fc_stat.elsDelayRetry++;
4963                         ndlp->nlp_retry = cmdiocb->retry;
4964
4965                         /* delay is specified in milliseconds */
4966                         mod_timer(&ndlp->nlp_delayfunc,
4967                                 jiffies + msecs_to_jiffies(delay));
4968                         set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
4969
4970                         ndlp->nlp_prev_state = ndlp->nlp_state;
4971                         if ((cmd == ELS_CMD_PRLI) ||
4972                             (cmd == ELS_CMD_NVMEPRLI))
4973                                 lpfc_nlp_set_state(vport, ndlp,
4974                                         NLP_STE_PRLI_ISSUE);
4975                         else if (cmd != ELS_CMD_ADISC)
4976                                 lpfc_nlp_set_state(vport, ndlp,
4977                                         NLP_STE_NPR_NODE);
4978                         ndlp->nlp_last_elscmd = cmd;
4979
4980                         return 1;
4981                 }
4982                 switch (cmd) {
4983                 case ELS_CMD_FLOGI:
4984                         lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
4985                         return 1;
4986                 case ELS_CMD_FDISC:
4987                         lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
4988                         return 1;
4989                 case ELS_CMD_PLOGI:
4990                         if (ndlp) {
4991                                 ndlp->nlp_prev_state = ndlp->nlp_state;
4992                                 lpfc_nlp_set_state(vport, ndlp,
4993                                                    NLP_STE_PLOGI_ISSUE);
4994                         }
4995                         lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
4996                         return 1;
4997                 case ELS_CMD_ADISC:
4998                         ndlp->nlp_prev_state = ndlp->nlp_state;
4999                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
5000                         lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
5001                         return 1;
5002                 case ELS_CMD_PRLI:
5003                 case ELS_CMD_NVMEPRLI:
5004                         ndlp->nlp_prev_state = ndlp->nlp_state;
5005                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
5006                         lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
5007                         return 1;
5008                 case ELS_CMD_LOGO:
5009                         ndlp->nlp_prev_state = ndlp->nlp_state;
5010                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
5011                         lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
5012                         return 1;
5013                 }
5014         }
5015         /* No retry ELS command <elsCmd> to remote NPORT <did> */
5016         if (logerr) {
5017                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5018                          "0137 No retry ELS command x%x to remote "
5019                          "NPORT x%x: Out of Resources: Error:x%x/%x "
5020                          "IoTag x%x\n",
5021                          cmd, did, ulp_status, ulp_word4,
5022                          cmdiocb->iotag);
5023         }
5024         else {
5025                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5026                                  "0108 No retry ELS command x%x to remote "
5027                                  "NPORT x%x Retried:%d Error:x%x/%x "
5028                                  "IoTag x%x nflags x%lx\n",
5029                                  cmd, did, cmdiocb->retry, ulp_status,
5030                                  ulp_word4, cmdiocb->iotag,
5031                                  (ndlp ? ndlp->nlp_flag : 0));
5032         }
5033         return 0;
5034 }
5035
5036 /**
5037  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
5038  * @phba: pointer to lpfc hba data structure.
5039  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
5040  *
5041  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
5042  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
5043  * checks to see whether there is a lpfc DMA buffer associated with the
5044  * response of the command IOCB. If so, it will be released before releasing
5045  * the lpfc DMA buffer associated with the IOCB itself.
5046  *
5047  * Return code
5048  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
5049  **/
5050 static int
5051 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
5052 {
5053         struct lpfc_dmabuf *buf_ptr;
5054
5055         /* Free the response before processing the command. */
5056         if (!list_empty(&buf_ptr1->list)) {
5057                 list_remove_head(&buf_ptr1->list, buf_ptr,
5058                                  struct lpfc_dmabuf,
5059                                  list);
5060                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5061                 kfree(buf_ptr);
5062         }
5063         lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
5064         kfree(buf_ptr1);
5065         return 0;
5066 }
5067
5068 /**
5069  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
5070  * @phba: pointer to lpfc hba data structure.
5071  * @buf_ptr: pointer to the lpfc dma buffer data structure.
5072  *
5073  * This routine releases the lpfc Direct Memory Access (DMA) buffer
5074  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
5075  * pool.
5076  *
5077  * Return code
5078  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
5079  **/
5080 static int
5081 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
5082 {
5083         lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5084         kfree(buf_ptr);
5085         return 0;
5086 }
5087
5088 /**
5089  * lpfc_els_free_iocb - Free a command iocb and its associated resources
5090  * @phba: pointer to lpfc hba data structure.
5091  * @elsiocb: pointer to lpfc els command iocb data structure.
5092  *
5093  * This routine frees a command IOCB and its associated resources. The
5094  * command IOCB data structure contains the reference to various associated
5095  * resources, these fields must be set to NULL if the associated reference
5096  * not present:
5097  *   cmd_dmabuf - reference to cmd.
5098  *   cmd_dmabuf->next - reference to rsp
5099  *   rsp_dmabuf - unused
5100  *   bpl_dmabuf - reference to bpl
5101  *
5102  * It first properly decrements the reference count held on ndlp for the
5103  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
5104  * set, it invokes the lpfc_els_free_data() routine to release the Direct
5105  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
5106  * adds the DMA buffer the @phba data structure for the delayed release.
5107  * If reference to the Buffer Pointer List (BPL) is present, the
5108  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
5109  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
5110  * invoked to release the IOCB data structure back to @phba IOCBQ list.
5111  *
5112  * Return code
5113  *   0 - Success (currently, always return 0)
5114  **/
5115 int
5116 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
5117 {
5118         struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
5119
5120         /* The I/O iocb is complete.  Clear the node and first dmbuf */
5121         elsiocb->ndlp = NULL;
5122
5123         /* cmd_dmabuf = cmd,  cmd_dmabuf->next = rsp, bpl_dmabuf = bpl */
5124         if (elsiocb->cmd_dmabuf) {
5125                 if (elsiocb->cmd_flag & LPFC_DELAY_MEM_FREE) {
5126                         /* Firmware could still be in progress of DMAing
5127                          * payload, so don't free data buffer till after
5128                          * a hbeat.
5129                          */
5130                         elsiocb->cmd_flag &= ~LPFC_DELAY_MEM_FREE;
5131                         buf_ptr = elsiocb->cmd_dmabuf;
5132                         elsiocb->cmd_dmabuf = NULL;
5133                         if (buf_ptr) {
5134                                 buf_ptr1 = NULL;
5135                                 spin_lock_irq(&phba->hbalock);
5136                                 if (!list_empty(&buf_ptr->list)) {
5137                                         list_remove_head(&buf_ptr->list,
5138                                                 buf_ptr1, struct lpfc_dmabuf,
5139                                                 list);
5140                                         INIT_LIST_HEAD(&buf_ptr1->list);
5141                                         list_add_tail(&buf_ptr1->list,
5142                                                 &phba->elsbuf);
5143                                         phba->elsbuf_cnt++;
5144                                 }
5145                                 INIT_LIST_HEAD(&buf_ptr->list);
5146                                 list_add_tail(&buf_ptr->list, &phba->elsbuf);
5147                                 phba->elsbuf_cnt++;
5148                                 spin_unlock_irq(&phba->hbalock);
5149                         }
5150                 } else {
5151                         buf_ptr1 = elsiocb->cmd_dmabuf;
5152                         lpfc_els_free_data(phba, buf_ptr1);
5153                         elsiocb->cmd_dmabuf = NULL;
5154                 }
5155         }
5156
5157         if (elsiocb->bpl_dmabuf) {
5158                 buf_ptr = elsiocb->bpl_dmabuf;
5159                 lpfc_els_free_bpl(phba, buf_ptr);
5160                 elsiocb->bpl_dmabuf = NULL;
5161         }
5162         lpfc_sli_release_iocbq(phba, elsiocb);
5163         return 0;
5164 }
5165
5166 /**
5167  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
5168  * @phba: pointer to lpfc hba data structure.
5169  * @cmdiocb: pointer to lpfc command iocb data structure.
5170  * @rspiocb: pointer to lpfc response iocb data structure.
5171  *
5172  * This routine is the completion callback function to the Logout (LOGO)
5173  * Accept (ACC) Response ELS command. This routine is invoked to indicate
5174  * the completion of the LOGO process. If the node has transitioned to NPR,
5175  * this routine unregisters the RPI if it is still registered. The
5176  * lpfc_els_free_iocb() is invoked to release the IOCB data structure.
5177  **/
5178 static void
5179 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5180                        struct lpfc_iocbq *rspiocb)
5181 {
5182         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
5183         struct lpfc_vport *vport = cmdiocb->vport;
5184         u32 ulp_status, ulp_word4;
5185
5186         ulp_status = get_job_ulpstatus(phba, rspiocb);
5187         ulp_word4 = get_job_word4(phba, rspiocb);
5188
5189         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5190                 "ACC LOGO cmpl:   status:x%x/x%x did:x%x",
5191                 ulp_status, ulp_word4, ndlp->nlp_DID);
5192         /* ACC to LOGO completes to NPort <nlp_DID> */
5193         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5194                          "0109 ACC to LOGO completes to NPort x%x refcnt %d "
5195                          "last els x%x Data: x%lx x%x x%x\n",
5196                          ndlp->nlp_DID, kref_read(&ndlp->kref),
5197                          ndlp->nlp_last_elscmd, ndlp->nlp_flag, ndlp->nlp_state,
5198                          ndlp->nlp_rpi);
5199
5200         /* This clause allows the LOGO ACC to complete and free resources
5201          * for the Fabric Domain Controller.  It does deliberately skip
5202          * the unreg_rpi and release rpi because some fabrics send RDP
5203          * requests after logging out from the initiator.
5204          */
5205         if (ndlp->nlp_type & NLP_FABRIC &&
5206             ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
5207                 goto out;
5208
5209         if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
5210                 if (test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag))
5211                         lpfc_unreg_rpi(vport, ndlp);
5212
5213                 /* If came from PRLO, then PRLO_ACC is done.
5214                  * Start rediscovery now.
5215                  */
5216                 if (ndlp->nlp_last_elscmd == ELS_CMD_PRLO) {
5217                         set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
5218                         ndlp->nlp_prev_state = ndlp->nlp_state;
5219                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5220                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5221                 }
5222         }
5223
5224  out:
5225         /*
5226          * The driver received a LOGO from the rport and has ACK'd it.
5227          * At this point, the driver is done so release the IOCB
5228          */
5229         lpfc_els_free_iocb(phba, cmdiocb);
5230         lpfc_nlp_put(ndlp);
5231 }
5232
5233 /**
5234  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
5235  * @phba: pointer to lpfc hba data structure.
5236  * @pmb: pointer to the driver internal queue element for mailbox command.
5237  *
5238  * This routine is the completion callback function for unregister default
5239  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
5240  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
5241  * decrements the ndlp reference count held for this completion callback
5242  * function. After that, it invokes the lpfc_drop_node to check
5243  * whether it is appropriate to release the node.
5244  **/
5245 void
5246 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5247 {
5248         struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
5249         u32 mbx_flag = pmb->mbox_flag;
5250         u32 mbx_cmd = pmb->u.mb.mbxCommand;
5251
5252         if (ndlp) {
5253                 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5254                                  "0006 rpi x%x DID:%x flg:%lx %d x%px "
5255                                  "mbx_cmd x%x mbx_flag x%x x%px\n",
5256                                  ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5257                                  kref_read(&ndlp->kref), ndlp, mbx_cmd,
5258                                  mbx_flag, pmb);
5259
5260                 /* This ends the default/temporary RPI cleanup logic for this
5261                  * ndlp and the node and rpi needs to be released. Free the rpi
5262                  * first on an UNREG_LOGIN and then release the final
5263                  * references.
5264                  */
5265                 clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
5266                 if (mbx_cmd == MBX_UNREG_LOGIN)
5267                         clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
5268                 lpfc_nlp_put(ndlp);
5269                 lpfc_drop_node(ndlp->vport, ndlp);
5270         }
5271
5272         lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
5273 }
5274
5275 /**
5276  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
5277  * @phba: pointer to lpfc hba data structure.
5278  * @cmdiocb: pointer to lpfc command iocb data structure.
5279  * @rspiocb: pointer to lpfc response iocb data structure.
5280  *
5281  * This routine is the completion callback function for ELS Response IOCB
5282  * command. In normal case, this callback function just properly sets the
5283  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
5284  * field in the command IOCB is not NULL, the referred mailbox command will
5285  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
5286  * the IOCB.
5287  **/
5288 static void
5289 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5290                   struct lpfc_iocbq *rspiocb)
5291 {
5292         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
5293         struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
5294         struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
5295         IOCB_t  *irsp;
5296         LPFC_MBOXQ_t *mbox = NULL;
5297         u32 ulp_status, ulp_word4, tmo, did, iotag;
5298
5299         if (!vport) {
5300                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
5301                                 "3177 null vport in ELS rsp\n");
5302                 goto out;
5303         }
5304         if (cmdiocb->context_un.mbox)
5305                 mbox = cmdiocb->context_un.mbox;
5306
5307         ulp_status = get_job_ulpstatus(phba, rspiocb);
5308         ulp_word4 = get_job_word4(phba, rspiocb);
5309         did = get_job_els_rsp64_did(phba, cmdiocb);
5310
5311         if (phba->sli_rev == LPFC_SLI_REV4) {
5312                 tmo = get_wqe_tmo(cmdiocb);
5313                 iotag = get_wqe_reqtag(cmdiocb);
5314         } else {
5315                 irsp = &rspiocb->iocb;
5316                 tmo = irsp->ulpTimeout;
5317                 iotag = irsp->ulpIoTag;
5318         }
5319
5320         /* Check to see if link went down during discovery */
5321         if (!ndlp || lpfc_els_chk_latt(vport)) {
5322                 if (mbox)
5323                         lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
5324                 goto out;
5325         }
5326
5327         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5328                 "ELS rsp cmpl:    status:x%x/x%x did:x%x",
5329                 ulp_status, ulp_word4, did);
5330         /* ELS response tag <ulpIoTag> completes */
5331         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5332                          "0110 ELS response tag x%x completes "
5333                          "Data: x%x x%x x%x x%x x%lx x%x x%x x%x %p %p\n",
5334                          iotag, ulp_status, ulp_word4, tmo,
5335                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5336                          ndlp->nlp_rpi, kref_read(&ndlp->kref), mbox, ndlp);
5337         if (mbox) {
5338                 if (ulp_status == 0 &&
5339                     test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag)) {
5340                         if (!lpfc_unreg_rpi(vport, ndlp) &&
5341                             !test_bit(FC_PT2PT, &vport->fc_flag)) {
5342                                 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5343                                     ndlp->nlp_state ==
5344                                      NLP_STE_REG_LOGIN_ISSUE) {
5345                                         lpfc_printf_vlog(vport, KERN_INFO,
5346                                                          LOG_DISCOVERY,
5347                                                          "0314 PLOGI recov "
5348                                                          "DID x%x "
5349                                                          "Data: x%x x%x x%lx\n",
5350                                                          ndlp->nlp_DID,
5351                                                          ndlp->nlp_state,
5352                                                          ndlp->nlp_rpi,
5353                                                          ndlp->nlp_flag);
5354                                         goto out_free_mbox;
5355                                 }
5356                         }
5357
5358                         /* Increment reference count to ndlp to hold the
5359                          * reference to ndlp for the callback function.
5360                          */
5361                         mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5362                         if (!mbox->ctx_ndlp)
5363                                 goto out_free_mbox;
5364
5365                         mbox->vport = vport;
5366                         if (test_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag)) {
5367                                 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5368                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5369                         } else {
5370                                 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
5371                                 ndlp->nlp_prev_state = ndlp->nlp_state;
5372                                 lpfc_nlp_set_state(vport, ndlp,
5373                                            NLP_STE_REG_LOGIN_ISSUE);
5374                         }
5375
5376                         set_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
5377                         if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5378                             != MBX_NOT_FINISHED)
5379                                 goto out;
5380
5381                         /* Decrement the ndlp reference count we
5382                          * set for this failed mailbox command.
5383                          */
5384                         lpfc_nlp_put(ndlp);
5385                         clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
5386
5387                         /* ELS rsp: Cannot issue reg_login for <NPortid> */
5388                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5389                                 "0138 ELS rsp: Cannot issue reg_login for x%x "
5390                                 "Data: x%lx x%x x%x\n",
5391                                 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5392                                 ndlp->nlp_rpi);
5393                 }
5394 out_free_mbox:
5395                 lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
5396         }
5397 out:
5398         if (ndlp && shost) {
5399                 if (mbox)
5400                         clear_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
5401                 clear_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag);
5402         }
5403
5404         /* An SLI4 NPIV instance wants to drop the node at this point under
5405          * these conditions because it doesn't need the login.
5406          */
5407         if (phba->sli_rev == LPFC_SLI_REV4 &&
5408             vport && vport->port_type == LPFC_NPIV_PORT &&
5409             !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
5410                 if (ndlp->nlp_state != NLP_STE_PLOGI_ISSUE &&
5411                     ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE &&
5412                     ndlp->nlp_state != NLP_STE_PRLI_ISSUE) {
5413                         /* Drop ndlp if there is no planned or outstanding
5414                          * issued PRLI.
5415                          *
5416                          * In cases when the ndlp is acting as both an initiator
5417                          * and target function, let our issued PRLI determine
5418                          * the final ndlp kref drop.
5419                          */
5420                         lpfc_drop_node(vport, ndlp);
5421                 }
5422         }
5423
5424         /* Release the originating I/O reference. */
5425         lpfc_els_free_iocb(phba, cmdiocb);
5426         lpfc_nlp_put(ndlp);
5427         return;
5428 }
5429
5430 /**
5431  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
5432  * @vport: pointer to a host virtual N_Port data structure.
5433  * @flag: the els command code to be accepted.
5434  * @oldiocb: pointer to the original lpfc command iocb data structure.
5435  * @ndlp: pointer to a node-list data structure.
5436  * @mbox: pointer to the driver internal queue element for mailbox command.
5437  *
5438  * This routine prepares and issues an Accept (ACC) response IOCB
5439  * command. It uses the @flag to properly set up the IOCB field for the
5440  * specific ACC response command to be issued and invokes the
5441  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
5442  * @mbox pointer is passed in, it will be put into the context_un.mbox
5443  * field of the IOCB for the completion callback function to issue the
5444  * mailbox command to the HBA later when callback is invoked.
5445  *
5446  * Note that the ndlp reference count will be incremented by 1 for holding the
5447  * ndlp and the reference to ndlp will be stored into the ndlp field of
5448  * the IOCB for the completion callback function to the corresponding
5449  * response ELS IOCB command.
5450  *
5451  * Return code
5452  *   0 - Successfully issued acc response
5453  *   1 - Failed to issue acc response
5454  **/
5455 int
5456 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
5457                  struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5458                  LPFC_MBOXQ_t *mbox)
5459 {
5460         struct lpfc_hba  *phba = vport->phba;
5461         IOCB_t *icmd;
5462         IOCB_t *oldcmd;
5463         union lpfc_wqe128 *wqe;
5464         union lpfc_wqe128 *oldwqe = &oldiocb->wqe;
5465         struct lpfc_iocbq *elsiocb;
5466         uint8_t *pcmd;
5467         struct serv_parm *sp;
5468         uint16_t cmdsize;
5469         int rc;
5470         ELS_PKT *els_pkt_ptr;
5471         struct fc_els_rdf_resp *rdf_resp;
5472
5473         switch (flag) {
5474         case ELS_CMD_ACC:
5475                 cmdsize = sizeof(uint32_t);
5476                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5477                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5478                 if (!elsiocb) {
5479                         clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
5480                         return 1;
5481                 }
5482
5483                 if (phba->sli_rev == LPFC_SLI_REV4) {
5484                         wqe = &elsiocb->wqe;
5485                         /* XRI / rx_id */
5486                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5487                                bf_get(wqe_ctxt_tag,
5488                                       &oldwqe->xmit_els_rsp.wqe_com));
5489
5490                         /* oxid */
5491                         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5492                                bf_get(wqe_rcvoxid,
5493                                       &oldwqe->xmit_els_rsp.wqe_com));
5494                 } else {
5495                         icmd = &elsiocb->iocb;
5496                         oldcmd = &oldiocb->iocb;
5497                         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5498                         icmd->unsli3.rcvsli3.ox_id =
5499                                 oldcmd->unsli3.rcvsli3.ox_id;
5500                 }
5501
5502                 pcmd = elsiocb->cmd_dmabuf->virt;
5503                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5504                 pcmd += sizeof(uint32_t);
5505
5506                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5507                         "Issue ACC:       did:x%x flg:x%lx",
5508                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
5509                 break;
5510         case ELS_CMD_FLOGI:
5511         case ELS_CMD_PLOGI:
5512                 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
5513                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5514                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5515                 if (!elsiocb)
5516                         return 1;
5517
5518                 if (phba->sli_rev == LPFC_SLI_REV4) {
5519                         wqe = &elsiocb->wqe;
5520                         /* XRI / rx_id */
5521                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5522                                bf_get(wqe_ctxt_tag,
5523                                       &oldwqe->xmit_els_rsp.wqe_com));
5524
5525                         /* oxid */
5526                         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5527                                bf_get(wqe_rcvoxid,
5528                                       &oldwqe->xmit_els_rsp.wqe_com));
5529                 } else {
5530                         icmd = &elsiocb->iocb;
5531                         oldcmd = &oldiocb->iocb;
5532                         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5533                         icmd->unsli3.rcvsli3.ox_id =
5534                                 oldcmd->unsli3.rcvsli3.ox_id;
5535                 }
5536
5537                 pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
5538
5539                 if (mbox)
5540                         elsiocb->context_un.mbox = mbox;
5541
5542                 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5543                 pcmd += sizeof(uint32_t);
5544                 sp = (struct serv_parm *)pcmd;
5545
5546                 if (flag == ELS_CMD_FLOGI) {
5547                         /* Copy the received service parameters back */
5548                         memcpy(sp, &phba->fc_fabparam,
5549                                sizeof(struct serv_parm));
5550
5551                         /* Clear the F_Port bit */
5552                         sp->cmn.fPort = 0;
5553
5554                         /* Mark all class service parameters as invalid */
5555                         sp->cls1.classValid = 0;
5556                         sp->cls2.classValid = 0;
5557                         sp->cls3.classValid = 0;
5558                         sp->cls4.classValid = 0;
5559
5560                         /* Copy our worldwide names */
5561                         memcpy(&sp->portName, &vport->fc_sparam.portName,
5562                                sizeof(struct lpfc_name));
5563                         memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
5564                                sizeof(struct lpfc_name));
5565                 } else {
5566                         memcpy(pcmd, &vport->fc_sparam,
5567                                sizeof(struct serv_parm));
5568
5569                         sp->cmn.valid_vendor_ver_level = 0;
5570                         memset(sp->un.vendorVersion, 0,
5571                                sizeof(sp->un.vendorVersion));
5572                         sp->cmn.bbRcvSizeMsb &= 0xF;
5573
5574                         /* If our firmware supports this feature, convey that
5575                          * info to the target using the vendor specific field.
5576                          */
5577                         if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
5578                                 sp->cmn.valid_vendor_ver_level = 1;
5579                                 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
5580                                 sp->un.vv.flags =
5581                                         cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
5582                         }
5583                 }
5584
5585                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5586                         "Issue ACC FLOGI/PLOGI: did:x%x flg:x%lx",
5587                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
5588                 break;
5589         case ELS_CMD_PRLO:
5590                 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
5591                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5592                                              ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
5593                 if (!elsiocb)
5594                         return 1;
5595
5596                 if (phba->sli_rev == LPFC_SLI_REV4) {
5597                         wqe = &elsiocb->wqe;
5598                         /* XRI / rx_id */
5599                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5600                                bf_get(wqe_ctxt_tag,
5601                                       &oldwqe->xmit_els_rsp.wqe_com));
5602
5603                         /* oxid */
5604                         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5605                                bf_get(wqe_rcvoxid,
5606                                       &oldwqe->xmit_els_rsp.wqe_com));
5607                 } else {
5608                         icmd = &elsiocb->iocb;
5609                         oldcmd = &oldiocb->iocb;
5610                         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5611                         icmd->unsli3.rcvsli3.ox_id =
5612                                 oldcmd->unsli3.rcvsli3.ox_id;
5613                 }
5614
5615                 pcmd = (u8 *) elsiocb->cmd_dmabuf->virt;
5616
5617                 memcpy(pcmd, oldiocb->cmd_dmabuf->virt,
5618                        sizeof(uint32_t) + sizeof(PRLO));
5619                 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
5620                 els_pkt_ptr = (ELS_PKT *) pcmd;
5621                 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
5622
5623                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5624                         "Issue ACC PRLO:  did:x%x flg:x%lx",
5625                         ndlp->nlp_DID, ndlp->nlp_flag, 0);
5626                 break;
5627         case ELS_CMD_RDF:
5628                 cmdsize = sizeof(*rdf_resp);
5629                 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5630                                              ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5631                 if (!elsiocb)
5632                         return 1;
5633
5634                 if (phba->sli_rev == LPFC_SLI_REV4) {
5635                         wqe = &elsiocb->wqe;
5636                         /* XRI / rx_id */
5637                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5638                                bf_get(wqe_ctxt_tag,
5639                                       &oldwqe->xmit_els_rsp.wqe_com));
5640
5641                         /* oxid */
5642                         bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5643                                bf_get(wqe_rcvoxid,
5644                                       &oldwqe->xmit_els_rsp.wqe_com));
5645                 } else {
5646                         icmd = &elsiocb->iocb;
5647                         oldcmd = &oldiocb->iocb;
5648                         icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5649                         icmd->unsli3.rcvsli3.ox_id =
5650                                 oldcmd->unsli3.rcvsli3.ox_id;
5651                 }
5652
5653                 pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
5654                 rdf_resp = (struct fc_els_rdf_resp *)pcmd;
5655                 memset(rdf_resp, 0, sizeof(*rdf_resp));
5656                 rdf_resp->acc_hdr.la_cmd = ELS_LS_ACC;
5657
5658                 /* FC-LS-5 specifies desc_list_len shall be set to 12 */
5659                 rdf_resp->desc_list_len = cpu_to_be32(12);
5660
5661                 /* FC-LS-5 specifies LS REQ Information descriptor */
5662                 rdf_resp->lsri.desc_tag = cpu_to_be32(1);
5663                 rdf_resp->lsri.desc_len = cpu_to_be32(sizeof(u32));
5664                 rdf_resp->lsri.rqst_w0.cmd = ELS_RDF;
5665                 break;
5666         default:
5667                 return 1;
5668         }
5669         if (test_bit(NLP_LOGO_ACC, &ndlp->nlp_flag)) {
5670                 if (!test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag) &&
5671                     !test_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag))
5672                         clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
5673                 elsiocb->cmd_cmpl = lpfc_cmpl_els_logo_acc;
5674         } else {
5675                 elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5676         }
5677
5678         phba->fc_stat.elsXmitACC++;
5679         elsiocb->ndlp = lpfc_nlp_get(ndlp);
5680         if (!elsiocb->ndlp) {
5681                 lpfc_els_free_iocb(phba, elsiocb);
5682                 return 1;
5683         }
5684
5685         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5686         if (rc == IOCB_ERROR) {
5687                 lpfc_els_free_iocb(phba, elsiocb);
5688                 lpfc_nlp_put(ndlp);
5689                 return 1;
5690         }
5691
5692         /* Xmit ELS ACC response tag <ulpIoTag> */
5693         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5694                          "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5695                          "XRI: x%x, DID: x%x, nlp_flag: x%lx nlp_state: x%x "
5696                          "RPI: x%x, fc_flag x%lx refcnt %d\n",
5697                          rc, elsiocb->iotag, elsiocb->sli4_xritag,
5698                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5699                          ndlp->nlp_rpi, vport->fc_flag, kref_read(&ndlp->kref));
5700         return 0;
5701 }
5702
5703 /**
5704  * lpfc_els_rsp_reject - Prepare and issue a rjt response iocb command
5705  * @vport: pointer to a virtual N_Port data structure.
5706  * @rejectError: reject response to issue
5707  * @oldiocb: pointer to the original lpfc command iocb data structure.
5708  * @ndlp: pointer to a node-list data structure.
5709  * @mbox: pointer to the driver internal queue element for mailbox command.
5710  *
5711  * This routine prepares and issue an Reject (RJT) response IOCB
5712  * command. If a @mbox pointer is passed in, it will be put into the
5713  * context_un.mbox field of the IOCB for the completion callback function
5714  * to issue to the HBA later.
5715  *
5716  * Note that the ndlp reference count will be incremented by 1 for holding the
5717  * ndlp and the reference to ndlp will be stored into the ndlp field of
5718  * the IOCB for the completion callback function to the reject response
5719  * ELS IOCB command.
5720  *
5721  * Return code
5722  *   0 - Successfully issued reject response
5723  *   1 - Failed to issue reject response
5724  **/
5725 int
5726 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
5727                     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5728                     LPFC_MBOXQ_t *mbox)
5729 {
5730         int rc;
5731         struct lpfc_hba  *phba = vport->phba;
5732         IOCB_t *icmd;
5733         IOCB_t *oldcmd;
5734         union lpfc_wqe128 *wqe;
5735         struct lpfc_iocbq *elsiocb;
5736         uint8_t *pcmd;
5737         uint16_t cmdsize;
5738
5739         cmdsize = 2 * sizeof(uint32_t);
5740         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5741                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
5742         if (!elsiocb)
5743                 return 1;
5744
5745         if (phba->sli_rev == LPFC_SLI_REV4) {
5746                 wqe = &elsiocb->wqe;
5747                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5748                        get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
5749                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5750                        get_job_rcvoxid(phba, oldiocb));
5751         } else {
5752                 icmd = &elsiocb->iocb;
5753                 oldcmd = &oldiocb->iocb;
5754                 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5755                 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5756         }
5757
5758         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
5759
5760         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5761         pcmd += sizeof(uint32_t);
5762         *((uint32_t *) (pcmd)) = rejectError;
5763
5764         if (mbox)
5765                 elsiocb->context_un.mbox = mbox;
5766
5767         /* Xmit ELS RJT <err> response tag <ulpIoTag> */
5768         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5769                          "0129 Xmit ELS RJT x%x response tag x%x "
5770                          "xri x%x, did x%x, nlp_flag x%lx, nlp_state x%x, "
5771                          "rpi x%x\n",
5772                          rejectError, elsiocb->iotag,
5773                          get_job_ulpcontext(phba, elsiocb), ndlp->nlp_DID,
5774                          ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
5775         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5776                 "Issue LS_RJT:    did:x%x flg:x%lx err:x%x",
5777                 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
5778
5779         phba->fc_stat.elsXmitLSRJT++;
5780         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5781         elsiocb->ndlp = lpfc_nlp_get(ndlp);
5782         if (!elsiocb->ndlp) {
5783                 lpfc_els_free_iocb(phba, elsiocb);
5784                 return 1;
5785         }
5786
5787         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5788         if (rc == IOCB_ERROR) {
5789                 lpfc_els_free_iocb(phba, elsiocb);
5790                 lpfc_nlp_put(ndlp);
5791                 return 1;
5792         }
5793
5794         return 0;
5795 }
5796
5797  /**
5798   * lpfc_issue_els_edc_rsp - Exchange Diagnostic Capabilities with the fabric.
5799   * @vport: pointer to a host virtual N_Port data structure.
5800   * @cmdiocb: pointer to the original lpfc command iocb data structure.
5801   * @ndlp: NPort to where rsp is directed
5802   *
5803   * This routine issues an EDC ACC RSP to the F-Port Controller to communicate
5804   * this N_Port's support of hardware signals in its Congestion
5805   * Capabilities Descriptor.
5806   *
5807   * Return code
5808   *   0 - Successfully issued edc rsp command
5809   *   1 - Failed to issue edc rsp command
5810   **/
5811 static int
5812 lpfc_issue_els_edc_rsp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5813                        struct lpfc_nodelist *ndlp)
5814 {
5815         struct lpfc_hba  *phba = vport->phba;
5816         struct fc_els_edc_resp *edc_rsp;
5817         struct fc_tlv_desc *tlv;
5818         struct lpfc_iocbq *elsiocb;
5819         IOCB_t *icmd, *cmd;
5820         union lpfc_wqe128 *wqe;
5821         u32 cgn_desc_size, lft_desc_size;
5822         u16 cmdsize;
5823         uint8_t *pcmd;
5824         int rc;
5825
5826         cmdsize = sizeof(struct fc_els_edc_resp);
5827         cgn_desc_size = sizeof(struct fc_diag_cg_sig_desc);
5828         lft_desc_size = (lpfc_link_is_lds_capable(phba)) ?
5829                                 sizeof(struct fc_diag_lnkflt_desc) : 0;
5830         cmdsize += cgn_desc_size + lft_desc_size;
5831         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, cmdiocb->retry,
5832                                      ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5833         if (!elsiocb)
5834                 return 1;
5835
5836         if (phba->sli_rev == LPFC_SLI_REV4) {
5837                 wqe = &elsiocb->wqe;
5838                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5839                        get_job_ulpcontext(phba, cmdiocb)); /* Xri / rx_id */
5840                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5841                        get_job_rcvoxid(phba, cmdiocb));
5842         } else {
5843                 icmd = &elsiocb->iocb;
5844                 cmd = &cmdiocb->iocb;
5845                 icmd->ulpContext = cmd->ulpContext; /* Xri / rx_id */
5846                 icmd->unsli3.rcvsli3.ox_id = cmd->unsli3.rcvsli3.ox_id;
5847         }
5848
5849         pcmd = elsiocb->cmd_dmabuf->virt;
5850         memset(pcmd, 0, cmdsize);
5851
5852         edc_rsp = (struct fc_els_edc_resp *)pcmd;
5853         edc_rsp->acc_hdr.la_cmd = ELS_LS_ACC;
5854         edc_rsp->desc_list_len = cpu_to_be32(sizeof(struct fc_els_lsri_desc) +
5855                                                 cgn_desc_size + lft_desc_size);
5856         edc_rsp->lsri.desc_tag = cpu_to_be32(ELS_DTAG_LS_REQ_INFO);
5857         edc_rsp->lsri.desc_len = cpu_to_be32(
5858                 FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_els_lsri_desc));
5859         edc_rsp->lsri.rqst_w0.cmd = ELS_EDC;
5860         tlv = edc_rsp->desc;
5861         lpfc_format_edc_cgn_desc(phba, tlv);
5862         tlv = fc_tlv_next_desc(tlv);
5863         if (lft_desc_size)
5864                 lpfc_format_edc_lft_desc(phba, tlv);
5865
5866         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5867                               "Issue EDC ACC:      did:x%x flg:x%lx refcnt %d",
5868                               ndlp->nlp_DID, ndlp->nlp_flag,
5869                               kref_read(&ndlp->kref));
5870         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5871
5872         phba->fc_stat.elsXmitACC++;
5873         elsiocb->ndlp = lpfc_nlp_get(ndlp);
5874         if (!elsiocb->ndlp) {
5875                 lpfc_els_free_iocb(phba, elsiocb);
5876                 return 1;
5877         }
5878
5879         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5880         if (rc == IOCB_ERROR) {
5881                 lpfc_els_free_iocb(phba, elsiocb);
5882                 lpfc_nlp_put(ndlp);
5883                 return 1;
5884         }
5885
5886         /* Xmit ELS ACC response tag <ulpIoTag> */
5887         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5888                          "0152 Xmit EDC ACC response Status: x%x, IoTag: x%x, "
5889                          "XRI: x%x, DID: x%x, nlp_flag: x%lx nlp_state: x%x "
5890                          "RPI: x%x, fc_flag x%lx\n",
5891                          rc, elsiocb->iotag, elsiocb->sli4_xritag,
5892                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5893                          ndlp->nlp_rpi, vport->fc_flag);
5894
5895         return 0;
5896 }
5897
5898 /**
5899  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
5900  * @vport: pointer to a virtual N_Port data structure.
5901  * @oldiocb: pointer to the original lpfc command iocb data structure.
5902  * @ndlp: pointer to a node-list data structure.
5903  *
5904  * This routine prepares and issues an Accept (ACC) response to Address
5905  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
5906  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5907  *
5908  * Note that the ndlp reference count will be incremented by 1 for holding the
5909  * ndlp and the reference to ndlp will be stored into the ndlp field of
5910  * the IOCB for the completion callback function to the ADISC Accept response
5911  * ELS IOCB command.
5912  *
5913  * Return code
5914  *   0 - Successfully issued acc adisc response
5915  *   1 - Failed to issue adisc acc response
5916  **/
5917 int
5918 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5919                        struct lpfc_nodelist *ndlp)
5920 {
5921         struct lpfc_hba  *phba = vport->phba;
5922         ADISC *ap;
5923         IOCB_t *icmd, *oldcmd;
5924         union lpfc_wqe128 *wqe;
5925         struct lpfc_iocbq *elsiocb;
5926         uint8_t *pcmd;
5927         uint16_t cmdsize;
5928         int rc;
5929         u32 ulp_context;
5930
5931         cmdsize = sizeof(uint32_t) + sizeof(ADISC);
5932         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5933                                      ndlp->nlp_DID, ELS_CMD_ACC);
5934         if (!elsiocb)
5935                 return 1;
5936
5937         if (phba->sli_rev == LPFC_SLI_REV4) {
5938                 wqe = &elsiocb->wqe;
5939                 /* XRI / rx_id */
5940                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5941                        get_job_ulpcontext(phba, oldiocb));
5942                 ulp_context = get_job_ulpcontext(phba, elsiocb);
5943                 /* oxid */
5944                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5945                        get_job_rcvoxid(phba, oldiocb));
5946         } else {
5947                 icmd = &elsiocb->iocb;
5948                 oldcmd = &oldiocb->iocb;
5949                 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5950                 ulp_context = elsiocb->iocb.ulpContext;
5951                 icmd->unsli3.rcvsli3.ox_id =
5952                         oldcmd->unsli3.rcvsli3.ox_id;
5953         }
5954
5955         /* Xmit ADISC ACC response tag <ulpIoTag> */
5956         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5957                          "0130 Xmit ADISC ACC response iotag x%x xri: "
5958                          "x%x, did x%x, nlp_flag x%lx, nlp_state x%x rpi x%x\n",
5959                          elsiocb->iotag, ulp_context,
5960                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5961                          ndlp->nlp_rpi);
5962         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
5963
5964         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5965         pcmd += sizeof(uint32_t);
5966
5967         ap = (ADISC *) (pcmd);
5968         ap->hardAL_PA = phba->fc_pref_ALPA;
5969         memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5970         memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5971         ap->DID = be32_to_cpu(vport->fc_myDID);
5972
5973         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5974                       "Issue ACC ADISC: did:x%x flg:x%lx refcnt %d",
5975                       ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
5976
5977         phba->fc_stat.elsXmitACC++;
5978         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5979         elsiocb->ndlp = lpfc_nlp_get(ndlp);
5980         if (!elsiocb->ndlp) {
5981                 lpfc_els_free_iocb(phba, elsiocb);
5982                 return 1;
5983         }
5984
5985         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5986         if (rc == IOCB_ERROR) {
5987                 lpfc_els_free_iocb(phba, elsiocb);
5988                 lpfc_nlp_put(ndlp);
5989                 return 1;
5990         }
5991
5992         return 0;
5993 }
5994
5995 /**
5996  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
5997  * @vport: pointer to a virtual N_Port data structure.
5998  * @oldiocb: pointer to the original lpfc command iocb data structure.
5999  * @ndlp: pointer to a node-list data structure.
6000  *
6001  * This routine prepares and issues an Accept (ACC) response to Process
6002  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
6003  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
6004  *
6005  * Note that the ndlp reference count will be incremented by 1 for holding the
6006  * ndlp and the reference to ndlp will be stored into the ndlp field of
6007  * the IOCB for the completion callback function to the PRLI Accept response
6008  * ELS IOCB command.
6009  *
6010  * Return code
6011  *   0 - Successfully issued acc prli response
6012  *   1 - Failed to issue acc prli response
6013  **/
6014 int
6015 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
6016                       struct lpfc_nodelist *ndlp)
6017 {
6018         struct lpfc_hba  *phba = vport->phba;
6019         PRLI *npr;
6020         struct lpfc_nvme_prli *npr_nvme;
6021         lpfc_vpd_t *vpd;
6022         IOCB_t *icmd;
6023         IOCB_t *oldcmd;
6024         union lpfc_wqe128 *wqe;
6025         struct lpfc_iocbq *elsiocb;
6026         uint8_t *pcmd;
6027         uint16_t cmdsize;
6028         uint32_t prli_fc4_req, *req_payload;
6029         struct lpfc_dmabuf *req_buf;
6030         int rc;
6031         u32 elsrspcmd, ulp_context;
6032
6033         /* Need the incoming PRLI payload to determine if the ACC is for an
6034          * FC4 or NVME PRLI type.  The PRLI type is at word 1.
6035          */
6036         req_buf = oldiocb->cmd_dmabuf;
6037         req_payload = (((uint32_t *)req_buf->virt) + 1);
6038
6039         /* PRLI type payload is at byte 3 for FCP or NVME. */
6040         prli_fc4_req = be32_to_cpu(*req_payload);
6041         prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
6042         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6043                          "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
6044                          prli_fc4_req, *((uint32_t *)req_payload));
6045
6046         if (prli_fc4_req == PRLI_FCP_TYPE) {
6047                 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
6048                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
6049         } else if (prli_fc4_req == PRLI_NVME_TYPE) {
6050                 cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
6051                 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
6052         } else {
6053                 return 1;
6054         }
6055
6056         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6057                                      ndlp->nlp_DID, elsrspcmd);
6058         if (!elsiocb)
6059                 return 1;
6060
6061         if (phba->sli_rev == LPFC_SLI_REV4) {
6062                 wqe = &elsiocb->wqe;
6063                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6064                        get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6065                 ulp_context = get_job_ulpcontext(phba, elsiocb);
6066                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6067                        get_job_rcvoxid(phba, oldiocb));
6068         } else {
6069                 icmd = &elsiocb->iocb;
6070                 oldcmd = &oldiocb->iocb;
6071                 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6072                 ulp_context = elsiocb->iocb.ulpContext;
6073                 icmd->unsli3.rcvsli3.ox_id =
6074                         oldcmd->unsli3.rcvsli3.ox_id;
6075         }
6076
6077         /* Xmit PRLI ACC response tag <ulpIoTag> */
6078         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6079                          "0131 Xmit PRLI ACC response tag x%x xri x%x, "
6080                          "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x\n",
6081                          elsiocb->iotag, ulp_context,
6082                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6083                          ndlp->nlp_rpi);
6084         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6085         memset(pcmd, 0, cmdsize);
6086
6087         *((uint32_t *)(pcmd)) = elsrspcmd;
6088         pcmd += sizeof(uint32_t);
6089
6090         /* For PRLI, remainder of payload is PRLI parameter page */
6091         vpd = &phba->vpd;
6092
6093         if (prli_fc4_req == PRLI_FCP_TYPE) {
6094                 /*
6095                  * If the remote port is a target and our firmware version
6096                  * is 3.20 or later, set the following bits for FC-TAPE
6097                  * support.
6098                  */
6099                 npr = (PRLI *) pcmd;
6100                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
6101                     (vpd->rev.feaLevelHigh >= 0x02)) {
6102                         npr->ConfmComplAllowed = 1;
6103                         npr->Retry = 1;
6104                         npr->TaskRetryIdReq = 1;
6105                 }
6106                 npr->acceptRspCode = PRLI_REQ_EXECUTED;
6107
6108                 /* Set image pair for complementary pairs only. */
6109                 if (ndlp->nlp_type & NLP_FCP_TARGET)
6110                         npr->estabImagePair = 1;
6111                 else
6112                         npr->estabImagePair = 0;
6113                 npr->readXferRdyDis = 1;
6114                 npr->ConfmComplAllowed = 1;
6115                 npr->prliType = PRLI_FCP_TYPE;
6116                 npr->initiatorFunc = 1;
6117
6118                 /* Xmit PRLI ACC response tag <ulpIoTag> */
6119                 lpfc_printf_vlog(vport, KERN_INFO,
6120                                  LOG_ELS | LOG_NODE | LOG_DISCOVERY,
6121                                  "6014 FCP issue PRLI ACC imgpair %d "
6122                                  "retry %d task %d\n",
6123                                  npr->estabImagePair,
6124                                  npr->Retry, npr->TaskRetryIdReq);
6125
6126         } else if (prli_fc4_req == PRLI_NVME_TYPE) {
6127                 /* Respond with an NVME PRLI Type */
6128                 npr_nvme = (struct lpfc_nvme_prli *) pcmd;
6129                 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
6130                 bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
6131                 bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
6132                 if (phba->nvmet_support) {
6133                         bf_set(prli_tgt, npr_nvme, 1);
6134                         bf_set(prli_disc, npr_nvme, 1);
6135                         if (phba->cfg_nvme_enable_fb) {
6136                                 bf_set(prli_fba, npr_nvme, 1);
6137
6138                                 /* TBD.  Target mode needs to post buffers
6139                                  * that support the configured first burst
6140                                  * byte size.
6141                                  */
6142                                 bf_set(prli_fb_sz, npr_nvme,
6143                                        phba->cfg_nvmet_fb_size);
6144                         }
6145                 } else {
6146                         bf_set(prli_init, npr_nvme, 1);
6147                 }
6148
6149                 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6150                                  "6015 NVME issue PRLI ACC word1 x%08x "
6151                                  "word4 x%08x word5 x%08x flag x%lx, "
6152                                  "fcp_info x%x nlp_type x%x\n",
6153                                  npr_nvme->word1, npr_nvme->word4,
6154                                  npr_nvme->word5, ndlp->nlp_flag,
6155                                  ndlp->nlp_fcp_info, ndlp->nlp_type);
6156                 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
6157                 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
6158                 npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
6159         } else
6160                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6161                                  "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
6162                                  prli_fc4_req, ndlp->nlp_fc4_type,
6163                                  ndlp->nlp_DID);
6164
6165         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6166                       "Issue ACC PRLI:  did:x%x flg:x%lx",
6167                       ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6168
6169         phba->fc_stat.elsXmitACC++;
6170         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6171         elsiocb->ndlp =  lpfc_nlp_get(ndlp);
6172         if (!elsiocb->ndlp) {
6173                 lpfc_els_free_iocb(phba, elsiocb);
6174                 return 1;
6175         }
6176
6177         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6178         if (rc == IOCB_ERROR) {
6179                 lpfc_els_free_iocb(phba, elsiocb);
6180                 lpfc_nlp_put(ndlp);
6181                 return 1;
6182         }
6183
6184         return 0;
6185 }
6186
6187 /**
6188  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
6189  * @vport: pointer to a virtual N_Port data structure.
6190  * @format: rnid command format.
6191  * @oldiocb: pointer to the original lpfc command iocb data structure.
6192  * @ndlp: pointer to a node-list data structure.
6193  *
6194  * This routine issues a Request Node Identification Data (RNID) Accept
6195  * (ACC) response. It constructs the RNID ACC response command according to
6196  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
6197  * issue the response.
6198  *
6199  * Note that the ndlp reference count will be incremented by 1 for holding the
6200  * ndlp and the reference to ndlp will be stored into the ndlp field of
6201  * the IOCB for the completion callback function.
6202  *
6203  * Return code
6204  *   0 - Successfully issued acc rnid response
6205  *   1 - Failed to issue acc rnid response
6206  **/
6207 static int
6208 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
6209                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6210 {
6211         struct lpfc_hba  *phba = vport->phba;
6212         RNID *rn;
6213         IOCB_t *icmd, *oldcmd;
6214         union lpfc_wqe128 *wqe;
6215         struct lpfc_iocbq *elsiocb;
6216         uint8_t *pcmd;
6217         uint16_t cmdsize;
6218         int rc;
6219         u32 ulp_context;
6220
6221         cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
6222                                         + (2 * sizeof(struct lpfc_name));
6223         if (format)
6224                 cmdsize += sizeof(RNID_TOP_DISC);
6225
6226         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6227                                      ndlp->nlp_DID, ELS_CMD_ACC);
6228         if (!elsiocb)
6229                 return 1;
6230
6231         if (phba->sli_rev == LPFC_SLI_REV4) {
6232                 wqe = &elsiocb->wqe;
6233                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6234                        get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6235                 ulp_context = get_job_ulpcontext(phba, elsiocb);
6236                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6237                        get_job_rcvoxid(phba, oldiocb));
6238         } else {
6239                 icmd = &elsiocb->iocb;
6240                 oldcmd = &oldiocb->iocb;
6241                 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6242                 ulp_context = elsiocb->iocb.ulpContext;
6243                 icmd->unsli3.rcvsli3.ox_id =
6244                         oldcmd->unsli3.rcvsli3.ox_id;
6245         }
6246
6247         /* Xmit RNID ACC response tag <ulpIoTag> */
6248         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6249                          "0132 Xmit RNID ACC response tag x%x xri x%x\n",
6250                          elsiocb->iotag, ulp_context);
6251         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6252         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6253         pcmd += sizeof(uint32_t);
6254
6255         memset(pcmd, 0, sizeof(RNID));
6256         rn = (RNID *) (pcmd);
6257         rn->Format = format;
6258         rn->CommonLen = (2 * sizeof(struct lpfc_name));
6259         memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
6260         memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
6261         switch (format) {
6262         case 0:
6263                 rn->SpecificLen = 0;
6264                 break;
6265         case RNID_TOPOLOGY_DISC:
6266                 rn->SpecificLen = sizeof(RNID_TOP_DISC);
6267                 memcpy(&rn->un.topologyDisc.portName,
6268                        &vport->fc_portname, sizeof(struct lpfc_name));
6269                 rn->un.topologyDisc.unitType = RNID_HBA;
6270                 rn->un.topologyDisc.physPort = 0;
6271                 rn->un.topologyDisc.attachedNodes = 0;
6272                 break;
6273         default:
6274                 rn->CommonLen = 0;
6275                 rn->SpecificLen = 0;
6276                 break;
6277         }
6278
6279         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6280                       "Issue ACC RNID:  did:x%x flg:x%lx refcnt %d",
6281                       ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6282
6283         phba->fc_stat.elsXmitACC++;
6284         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6285         elsiocb->ndlp = lpfc_nlp_get(ndlp);
6286         if (!elsiocb->ndlp) {
6287                 lpfc_els_free_iocb(phba, elsiocb);
6288                 return 1;
6289         }
6290
6291         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6292         if (rc == IOCB_ERROR) {
6293                 lpfc_els_free_iocb(phba, elsiocb);
6294                 lpfc_nlp_put(ndlp);
6295                 return 1;
6296         }
6297
6298         return 0;
6299 }
6300
6301 /**
6302  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
6303  * @vport: pointer to a virtual N_Port data structure.
6304  * @iocb: pointer to the lpfc command iocb data structure.
6305  * @ndlp: pointer to a node-list data structure.
6306  *
6307  * Return
6308  **/
6309 static void
6310 lpfc_els_clear_rrq(struct lpfc_vport *vport,
6311                    struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
6312 {
6313         struct lpfc_hba  *phba = vport->phba;
6314         uint8_t *pcmd;
6315         struct RRQ *rrq;
6316         uint16_t rxid;
6317         uint16_t xri;
6318         struct lpfc_node_rrq *prrq;
6319
6320
6321         pcmd = (uint8_t *)iocb->cmd_dmabuf->virt;
6322         pcmd += sizeof(uint32_t);
6323         rrq = (struct RRQ *)pcmd;
6324         rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
6325         rxid = bf_get(rrq_rxid, rrq);
6326
6327         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6328                         "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
6329                         " x%x x%x\n",
6330                         be32_to_cpu(bf_get(rrq_did, rrq)),
6331                         bf_get(rrq_oxid, rrq),
6332                         rxid,
6333                         get_wqe_reqtag(iocb),
6334                         get_job_ulpcontext(phba, iocb));
6335
6336         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6337                 "Clear RRQ:  did:x%x flg:x%lx exchg:x%.08x",
6338                 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
6339         if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
6340                 xri = bf_get(rrq_oxid, rrq);
6341         else
6342                 xri = rxid;
6343         prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
6344         if (prrq)
6345                 lpfc_clr_rrq_active(phba, xri, prrq);
6346         return;
6347 }
6348
6349 /**
6350  * lpfc_els_rsp_echo_acc - Issue echo acc response
6351  * @vport: pointer to a virtual N_Port data structure.
6352  * @data: pointer to echo data to return in the accept.
6353  * @oldiocb: pointer to the original lpfc command iocb data structure.
6354  * @ndlp: pointer to a node-list data structure.
6355  *
6356  * Return code
6357  *   0 - Successfully issued acc echo response
6358  *   1 - Failed to issue acc echo response
6359  **/
6360 static int
6361 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
6362                       struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6363 {
6364         struct lpfc_hba  *phba = vport->phba;
6365         IOCB_t *icmd, *oldcmd;
6366         union lpfc_wqe128 *wqe;
6367         struct lpfc_iocbq *elsiocb;
6368         uint8_t *pcmd;
6369         uint16_t cmdsize;
6370         int rc;
6371         u32 ulp_context;
6372
6373         if (phba->sli_rev == LPFC_SLI_REV4)
6374                 cmdsize = oldiocb->wcqe_cmpl.total_data_placed;
6375         else
6376                 cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
6377
6378         /* The accumulated length can exceed the BPL_SIZE.  For
6379          * now, use this as the limit
6380          */
6381         if (cmdsize > LPFC_BPL_SIZE)
6382                 cmdsize = LPFC_BPL_SIZE;
6383         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6384                                      ndlp->nlp_DID, ELS_CMD_ACC);
6385         if (!elsiocb)
6386                 return 1;
6387
6388         if (phba->sli_rev == LPFC_SLI_REV4) {
6389                 wqe = &elsiocb->wqe;
6390                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6391                        get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6392                 ulp_context = get_job_ulpcontext(phba, elsiocb);
6393                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6394                        get_job_rcvoxid(phba, oldiocb));
6395         } else {
6396                 icmd = &elsiocb->iocb;
6397                 oldcmd = &oldiocb->iocb;
6398                 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6399                 ulp_context = elsiocb->iocb.ulpContext;
6400                 icmd->unsli3.rcvsli3.ox_id =
6401                         oldcmd->unsli3.rcvsli3.ox_id;
6402         }
6403
6404         /* Xmit ECHO ACC response tag <ulpIoTag> */
6405         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6406                          "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
6407                          elsiocb->iotag, ulp_context);
6408         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6409         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6410         pcmd += sizeof(uint32_t);
6411         memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
6412
6413         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6414                       "Issue ACC ECHO:  did:x%x flg:x%lx refcnt %d",
6415                       ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6416
6417         phba->fc_stat.elsXmitACC++;
6418         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6419         elsiocb->ndlp =  lpfc_nlp_get(ndlp);
6420         if (!elsiocb->ndlp) {
6421                 lpfc_els_free_iocb(phba, elsiocb);
6422                 return 1;
6423         }
6424
6425         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6426         if (rc == IOCB_ERROR) {
6427                 lpfc_els_free_iocb(phba, elsiocb);
6428                 lpfc_nlp_put(ndlp);
6429                 return 1;
6430         }
6431
6432         return 0;
6433 }
6434
6435 /**
6436  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
6437  * @vport: pointer to a host virtual N_Port data structure.
6438  *
6439  * This routine issues Address Discover (ADISC) ELS commands to those
6440  * N_Ports which are in node port recovery state and ADISC has not been issued
6441  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
6442  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
6443  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
6444  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
6445  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
6446  * IOCBs quit for later pick up. On the other hand, after walking through
6447  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
6448  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
6449  * no more ADISC need to be sent.
6450  *
6451  * Return code
6452  *    The number of N_Ports with adisc issued.
6453  **/
6454 int
6455 lpfc_els_disc_adisc(struct lpfc_vport *vport)
6456 {
6457         struct lpfc_nodelist *ndlp, *next_ndlp;
6458         int sentadisc = 0;
6459
6460         /* go thru NPR nodes and issue any remaining ELS ADISCs */
6461         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6462
6463                 if (ndlp->nlp_state != NLP_STE_NPR_NODE ||
6464                     !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag))
6465                         continue;
6466
6467                 clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
6468
6469                 if (!test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
6470                         /* This node was marked for ADISC but was not picked
6471                          * for discovery. This is possible if the node was
6472                          * missing in gidft response.
6473                          *
6474                          * At time of marking node for ADISC, we skipped unreg
6475                          * from backend
6476                          */
6477                         lpfc_nlp_unreg_node(vport, ndlp);
6478                         lpfc_unreg_rpi(vport, ndlp);
6479                         continue;
6480                 }
6481
6482                 ndlp->nlp_prev_state = ndlp->nlp_state;
6483                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6484                 lpfc_issue_els_adisc(vport, ndlp, 0);
6485                 sentadisc++;
6486                 vport->num_disc_nodes++;
6487                 if (vport->num_disc_nodes >=
6488                                 vport->cfg_discovery_threads) {
6489                         set_bit(FC_NLP_MORE, &vport->fc_flag);
6490                         break;
6491                 }
6492
6493         }
6494         if (sentadisc == 0)
6495                 clear_bit(FC_NLP_MORE, &vport->fc_flag);
6496         return sentadisc;
6497 }
6498
6499 /**
6500  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
6501  * @vport: pointer to a host virtual N_Port data structure.
6502  *
6503  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
6504  * which are in node port recovery state, with a @vport. Each time an ELS
6505  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
6506  * the per @vport number of discover count (num_disc_nodes) shall be
6507  * incremented. If the num_disc_nodes reaches a pre-configured threshold
6508  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
6509  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
6510  * later pick up. On the other hand, after walking through all the ndlps with
6511  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
6512  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
6513  * PLOGI need to be sent.
6514  *
6515  * Return code
6516  *   The number of N_Ports with plogi issued.
6517  **/
6518 int
6519 lpfc_els_disc_plogi(struct lpfc_vport *vport)
6520 {
6521         struct lpfc_nodelist *ndlp, *next_ndlp;
6522         int sentplogi = 0;
6523
6524         /* go thru NPR nodes and issue any remaining ELS PLOGIs */
6525         list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6526                 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
6527                     test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag) &&
6528                     !test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag) &&
6529                     !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag)) {
6530                         ndlp->nlp_prev_state = ndlp->nlp_state;
6531                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6532                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6533                         sentplogi++;
6534                         vport->num_disc_nodes++;
6535                         if (vport->num_disc_nodes >=
6536                                         vport->cfg_discovery_threads) {
6537                                 set_bit(FC_NLP_MORE, &vport->fc_flag);
6538                                 break;
6539                         }
6540                 }
6541         }
6542
6543         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6544                          "6452 Discover PLOGI %d flag x%lx\n",
6545                          sentplogi, vport->fc_flag);
6546
6547         if (sentplogi)
6548                 lpfc_set_disctmo(vport);
6549         else
6550                 clear_bit(FC_NLP_MORE, &vport->fc_flag);
6551         return sentplogi;
6552 }
6553
6554 static uint32_t
6555 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
6556                 uint32_t word0)
6557 {
6558
6559         desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
6560         desc->payload.els_req = word0;
6561         desc->length = cpu_to_be32(sizeof(desc->payload));
6562
6563         return sizeof(struct fc_rdp_link_service_desc);
6564 }
6565
6566 static uint32_t
6567 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
6568                 uint8_t *page_a0, uint8_t *page_a2)
6569 {
6570         uint16_t wavelength;
6571         uint16_t temperature;
6572         uint16_t rx_power;
6573         uint16_t tx_bias;
6574         uint16_t tx_power;
6575         uint16_t vcc;
6576         uint16_t flag = 0;
6577         struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
6578         struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
6579
6580         desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
6581
6582         trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
6583                         &page_a0[SSF_TRANSCEIVER_CODE_B4];
6584         trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
6585                         &page_a0[SSF_TRANSCEIVER_CODE_B5];
6586
6587         if ((trasn_code_byte4->fc_sw_laser) ||
6588             (trasn_code_byte5->fc_sw_laser_sl) ||
6589             (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
6590                 flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
6591         } else if (trasn_code_byte4->fc_lw_laser) {
6592                 wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
6593                         page_a0[SSF_WAVELENGTH_B0];
6594                 if (wavelength == SFP_WAVELENGTH_LC1310)
6595                         flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
6596                 if (wavelength == SFP_WAVELENGTH_LL1550)
6597                         flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
6598         }
6599         /* check if its SFP+ */
6600         flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
6601                         SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
6602                                         << SFP_FLAG_CT_SHIFT;
6603
6604         /* check if its OPTICAL */
6605         flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
6606                         SFP_FLAG_IS_OPTICAL_PORT : 0)
6607                                         << SFP_FLAG_IS_OPTICAL_SHIFT;
6608
6609         temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
6610                 page_a2[SFF_TEMPERATURE_B0]);
6611         vcc = (page_a2[SFF_VCC_B1] << 8 |
6612                 page_a2[SFF_VCC_B0]);
6613         tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
6614                 page_a2[SFF_TXPOWER_B0]);
6615         tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
6616                 page_a2[SFF_TX_BIAS_CURRENT_B0]);
6617         rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
6618                 page_a2[SFF_RXPOWER_B0]);
6619         desc->sfp_info.temperature = cpu_to_be16(temperature);
6620         desc->sfp_info.rx_power = cpu_to_be16(rx_power);
6621         desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
6622         desc->sfp_info.tx_power = cpu_to_be16(tx_power);
6623         desc->sfp_info.vcc = cpu_to_be16(vcc);
6624
6625         desc->sfp_info.flags = cpu_to_be16(flag);
6626         desc->length = cpu_to_be32(sizeof(desc->sfp_info));
6627
6628         return sizeof(struct fc_rdp_sfp_desc);
6629 }
6630
6631 static uint32_t
6632 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
6633                 READ_LNK_VAR *stat)
6634 {
6635         uint32_t type;
6636
6637         desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
6638
6639         type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
6640
6641         desc->info.port_type = cpu_to_be32(type);
6642
6643         desc->info.link_status.link_failure_cnt =
6644                 cpu_to_be32(stat->linkFailureCnt);
6645         desc->info.link_status.loss_of_synch_cnt =
6646                 cpu_to_be32(stat->lossSyncCnt);
6647         desc->info.link_status.loss_of_signal_cnt =
6648                 cpu_to_be32(stat->lossSignalCnt);
6649         desc->info.link_status.primitive_seq_proto_err =
6650                 cpu_to_be32(stat->primSeqErrCnt);
6651         desc->info.link_status.invalid_trans_word =
6652                 cpu_to_be32(stat->invalidXmitWord);
6653         desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
6654
6655         desc->length = cpu_to_be32(sizeof(desc->info));
6656
6657         return sizeof(struct fc_rdp_link_error_status_desc);
6658 }
6659
6660 static uint32_t
6661 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
6662                       struct lpfc_vport *vport)
6663 {
6664         uint32_t bbCredit;
6665
6666         desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
6667
6668         bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
6669                         (vport->fc_sparam.cmn.bbCreditMsb << 8);
6670         desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
6671         if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
6672                 bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
6673                         (vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
6674                 desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
6675         } else {
6676                 desc->bbc_info.attached_port_bbc = 0;
6677         }
6678
6679         desc->bbc_info.rtt = 0;
6680         desc->length = cpu_to_be32(sizeof(desc->bbc_info));
6681
6682         return sizeof(struct fc_rdp_bbc_desc);
6683 }
6684
6685 static uint32_t
6686 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
6687                            struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
6688 {
6689         uint32_t flags = 0;
6690
6691         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6692
6693         desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
6694         desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
6695         desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
6696         desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
6697
6698         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6699                 flags |= RDP_OET_HIGH_ALARM;
6700         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6701                 flags |= RDP_OET_LOW_ALARM;
6702         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6703                 flags |= RDP_OET_HIGH_WARNING;
6704         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6705                 flags |= RDP_OET_LOW_WARNING;
6706
6707         flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
6708         desc->oed_info.function_flags = cpu_to_be32(flags);
6709         desc->length = cpu_to_be32(sizeof(desc->oed_info));
6710         return sizeof(struct fc_rdp_oed_sfp_desc);
6711 }
6712
6713 static uint32_t
6714 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
6715                               struct fc_rdp_oed_sfp_desc *desc,
6716                               uint8_t *page_a2)
6717 {
6718         uint32_t flags = 0;
6719
6720         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6721
6722         desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
6723         desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
6724         desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
6725         desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
6726
6727         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6728                 flags |= RDP_OET_HIGH_ALARM;
6729         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6730                 flags |= RDP_OET_LOW_ALARM;
6731         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6732                 flags |= RDP_OET_HIGH_WARNING;
6733         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6734                 flags |= RDP_OET_LOW_WARNING;
6735
6736         flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
6737         desc->oed_info.function_flags = cpu_to_be32(flags);
6738         desc->length = cpu_to_be32(sizeof(desc->oed_info));
6739         return sizeof(struct fc_rdp_oed_sfp_desc);
6740 }
6741
6742 static uint32_t
6743 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
6744                              struct fc_rdp_oed_sfp_desc *desc,
6745                              uint8_t *page_a2)
6746 {
6747         uint32_t flags = 0;
6748
6749         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6750
6751         desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
6752         desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
6753         desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
6754         desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
6755
6756         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6757                 flags |= RDP_OET_HIGH_ALARM;
6758         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
6759                 flags |= RDP_OET_LOW_ALARM;
6760         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6761                 flags |= RDP_OET_HIGH_WARNING;
6762         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
6763                 flags |= RDP_OET_LOW_WARNING;
6764
6765         flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
6766         desc->oed_info.function_flags = cpu_to_be32(flags);
6767         desc->length = cpu_to_be32(sizeof(desc->oed_info));
6768         return sizeof(struct fc_rdp_oed_sfp_desc);
6769 }
6770
6771 static uint32_t
6772 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
6773                               struct fc_rdp_oed_sfp_desc *desc,
6774                               uint8_t *page_a2)
6775 {
6776         uint32_t flags = 0;
6777
6778         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6779
6780         desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
6781         desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
6782         desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
6783         desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
6784
6785         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6786                 flags |= RDP_OET_HIGH_ALARM;
6787         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
6788                 flags |= RDP_OET_LOW_ALARM;
6789         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6790                 flags |= RDP_OET_HIGH_WARNING;
6791         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
6792                 flags |= RDP_OET_LOW_WARNING;
6793
6794         flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
6795         desc->oed_info.function_flags = cpu_to_be32(flags);
6796         desc->length = cpu_to_be32(sizeof(desc->oed_info));
6797         return sizeof(struct fc_rdp_oed_sfp_desc);
6798 }
6799
6800
6801 static uint32_t
6802 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
6803                               struct fc_rdp_oed_sfp_desc *desc,
6804                               uint8_t *page_a2)
6805 {
6806         uint32_t flags = 0;
6807
6808         desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6809
6810         desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
6811         desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
6812         desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
6813         desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
6814
6815         if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6816                 flags |= RDP_OET_HIGH_ALARM;
6817         if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
6818                 flags |= RDP_OET_LOW_ALARM;
6819         if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6820                 flags |= RDP_OET_HIGH_WARNING;
6821         if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
6822                 flags |= RDP_OET_LOW_WARNING;
6823
6824         flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
6825         desc->oed_info.function_flags = cpu_to_be32(flags);
6826         desc->length = cpu_to_be32(sizeof(desc->oed_info));
6827         return sizeof(struct fc_rdp_oed_sfp_desc);
6828 }
6829
6830 static uint32_t
6831 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
6832                       uint8_t *page_a0, struct lpfc_vport *vport)
6833 {
6834         desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
6835         memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
6836         memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
6837         memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
6838         memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
6839         memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
6840         desc->length = cpu_to_be32(sizeof(desc->opd_info));
6841         return sizeof(struct fc_rdp_opd_sfp_desc);
6842 }
6843
6844 static uint32_t
6845 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
6846 {
6847         if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
6848                 return 0;
6849         desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
6850
6851         desc->info.CorrectedBlocks =
6852                 cpu_to_be32(stat->fecCorrBlkCount);
6853         desc->info.UncorrectableBlocks =
6854                 cpu_to_be32(stat->fecUncorrBlkCount);
6855
6856         desc->length = cpu_to_be32(sizeof(desc->info));
6857
6858         return sizeof(struct fc_fec_rdp_desc);
6859 }
6860
6861 static uint32_t
6862 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
6863 {
6864         uint16_t rdp_cap = 0;
6865         uint16_t rdp_speed;
6866
6867         desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
6868
6869         switch (phba->fc_linkspeed) {
6870         case LPFC_LINK_SPEED_1GHZ:
6871                 rdp_speed = RDP_PS_1GB;
6872                 break;
6873         case LPFC_LINK_SPEED_2GHZ:
6874                 rdp_speed = RDP_PS_2GB;
6875                 break;
6876         case LPFC_LINK_SPEED_4GHZ:
6877                 rdp_speed = RDP_PS_4GB;
6878                 break;
6879         case LPFC_LINK_SPEED_8GHZ:
6880                 rdp_speed = RDP_PS_8GB;
6881                 break;
6882         case LPFC_LINK_SPEED_10GHZ:
6883                 rdp_speed = RDP_PS_10GB;
6884                 break;
6885         case LPFC_LINK_SPEED_16GHZ:
6886                 rdp_speed = RDP_PS_16GB;
6887                 break;
6888         case LPFC_LINK_SPEED_32GHZ:
6889                 rdp_speed = RDP_PS_32GB;
6890                 break;
6891         case LPFC_LINK_SPEED_64GHZ:
6892                 rdp_speed = RDP_PS_64GB;
6893                 break;
6894         case LPFC_LINK_SPEED_128GHZ:
6895                 rdp_speed = RDP_PS_128GB;
6896                 break;
6897         case LPFC_LINK_SPEED_256GHZ:
6898                 rdp_speed = RDP_PS_256GB;
6899                 break;
6900         default:
6901                 rdp_speed = RDP_PS_UNKNOWN;
6902                 break;
6903         }
6904
6905         desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
6906
6907         if (phba->lmt & LMT_256Gb)
6908                 rdp_cap |= RDP_PS_256GB;
6909         if (phba->lmt & LMT_128Gb)
6910                 rdp_cap |= RDP_PS_128GB;
6911         if (phba->lmt & LMT_64Gb)
6912                 rdp_cap |= RDP_PS_64GB;
6913         if (phba->lmt & LMT_32Gb)
6914                 rdp_cap |= RDP_PS_32GB;
6915         if (phba->lmt & LMT_16Gb)
6916                 rdp_cap |= RDP_PS_16GB;
6917         if (phba->lmt & LMT_10Gb)
6918                 rdp_cap |= RDP_PS_10GB;
6919         if (phba->lmt & LMT_8Gb)
6920                 rdp_cap |= RDP_PS_8GB;
6921         if (phba->lmt & LMT_4Gb)
6922                 rdp_cap |= RDP_PS_4GB;
6923         if (phba->lmt & LMT_2Gb)
6924                 rdp_cap |= RDP_PS_2GB;
6925         if (phba->lmt & LMT_1Gb)
6926                 rdp_cap |= RDP_PS_1GB;
6927
6928         if (rdp_cap == 0)
6929                 rdp_cap = RDP_CAP_UNKNOWN;
6930         if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
6931                 rdp_cap |= RDP_CAP_USER_CONFIGURED;
6932
6933         desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
6934         desc->length = cpu_to_be32(sizeof(desc->info));
6935         return sizeof(struct fc_rdp_port_speed_desc);
6936 }
6937
6938 static uint32_t
6939 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
6940                 struct lpfc_vport *vport)
6941 {
6942
6943         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6944
6945         memcpy(desc->port_names.wwnn, &vport->fc_nodename,
6946                         sizeof(desc->port_names.wwnn));
6947
6948         memcpy(desc->port_names.wwpn, &vport->fc_portname,
6949                         sizeof(desc->port_names.wwpn));
6950
6951         desc->length = cpu_to_be32(sizeof(desc->port_names));
6952         return sizeof(struct fc_rdp_port_name_desc);
6953 }
6954
6955 static uint32_t
6956 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
6957                 struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6958 {
6959
6960         desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6961         if (test_bit(FC_FABRIC, &vport->fc_flag)) {
6962                 memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
6963                        sizeof(desc->port_names.wwnn));
6964
6965                 memcpy(desc->port_names.wwpn, &vport->fabric_portname,
6966                        sizeof(desc->port_names.wwpn));
6967         } else {  /* Point to Point */
6968                 memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
6969                        sizeof(desc->port_names.wwnn));
6970
6971                 memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
6972                        sizeof(desc->port_names.wwpn));
6973         }
6974
6975         desc->length = cpu_to_be32(sizeof(desc->port_names));
6976         return sizeof(struct fc_rdp_port_name_desc);
6977 }
6978
6979 static void
6980 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
6981                 int status)
6982 {
6983         struct lpfc_nodelist *ndlp = rdp_context->ndlp;
6984         struct lpfc_vport *vport = ndlp->vport;
6985         struct lpfc_iocbq *elsiocb;
6986         struct ulp_bde64 *bpl;
6987         IOCB_t *icmd;
6988         union lpfc_wqe128 *wqe;
6989         uint8_t *pcmd;
6990         struct ls_rjt *stat;
6991         struct fc_rdp_res_frame *rdp_res;
6992         uint32_t cmdsize, len;
6993         uint16_t *flag_ptr;
6994         int rc;
6995         u32 ulp_context;
6996
6997         if (status != SUCCESS)
6998                 goto error;
6999
7000         /* This will change once we know the true size of the RDP payload */
7001         cmdsize = sizeof(struct fc_rdp_res_frame);
7002
7003         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
7004                                 lpfc_max_els_tries, rdp_context->ndlp,
7005                                 rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
7006         if (!elsiocb)
7007                 goto free_rdp_context;
7008
7009         ulp_context = get_job_ulpcontext(phba, elsiocb);
7010         if (phba->sli_rev == LPFC_SLI_REV4) {
7011                 wqe = &elsiocb->wqe;
7012                 /* ox-id of the frame */
7013                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7014                        rdp_context->ox_id);
7015                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
7016                        rdp_context->rx_id);
7017         } else {
7018                 icmd = &elsiocb->iocb;
7019                 icmd->ulpContext = rdp_context->rx_id;
7020                 icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
7021         }
7022
7023         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7024                         "2171 Xmit RDP response tag x%x xri x%x, "
7025                         "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x",
7026                         elsiocb->iotag, ulp_context,
7027                         ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7028                         ndlp->nlp_rpi);
7029         rdp_res = (struct fc_rdp_res_frame *)elsiocb->cmd_dmabuf->virt;
7030         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7031         memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
7032         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7033
7034         /* Update Alarm and Warning */
7035         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
7036         phba->sfp_alarm |= *flag_ptr;
7037         flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
7038         phba->sfp_warning |= *flag_ptr;
7039
7040         /* For RDP payload */
7041         len = 8;
7042         len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
7043                                          (len + pcmd), ELS_CMD_RDP);
7044
7045         len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
7046                         rdp_context->page_a0, rdp_context->page_a2);
7047         len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
7048                                   phba);
7049         len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
7050                                        (len + pcmd), &rdp_context->link_stat);
7051         len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
7052                                              (len + pcmd), vport);
7053         len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
7054                                         (len + pcmd), vport, ndlp);
7055         len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
7056                         &rdp_context->link_stat);
7057         len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
7058                                      &rdp_context->link_stat, vport);
7059         len += lpfc_rdp_res_oed_temp_desc(phba,
7060                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7061                                 rdp_context->page_a2);
7062         len += lpfc_rdp_res_oed_voltage_desc(phba,
7063                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7064                                 rdp_context->page_a2);
7065         len += lpfc_rdp_res_oed_txbias_desc(phba,
7066                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7067                                 rdp_context->page_a2);
7068         len += lpfc_rdp_res_oed_txpower_desc(phba,
7069                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7070                                 rdp_context->page_a2);
7071         len += lpfc_rdp_res_oed_rxpower_desc(phba,
7072                                 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7073                                 rdp_context->page_a2);
7074         len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
7075                                      rdp_context->page_a0, vport);
7076
7077         rdp_res->length = cpu_to_be32(len - 8);
7078         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7079
7080         /* Now that we know the true size of the payload, update the BPL */
7081         bpl = (struct ulp_bde64 *)elsiocb->bpl_dmabuf->virt;
7082         bpl->tus.f.bdeSize = len;
7083         bpl->tus.f.bdeFlags = 0;
7084         bpl->tus.w = le32_to_cpu(bpl->tus.w);
7085
7086         phba->fc_stat.elsXmitACC++;
7087         elsiocb->ndlp = lpfc_nlp_get(ndlp);
7088         if (!elsiocb->ndlp) {
7089                 lpfc_els_free_iocb(phba, elsiocb);
7090                 goto free_rdp_context;
7091         }
7092
7093         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7094         if (rc == IOCB_ERROR) {
7095                 lpfc_els_free_iocb(phba, elsiocb);
7096                 lpfc_nlp_put(ndlp);
7097         }
7098
7099         goto free_rdp_context;
7100
7101 error:
7102         cmdsize = 2 * sizeof(uint32_t);
7103         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
7104                         ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
7105         if (!elsiocb)
7106                 goto free_rdp_context;
7107
7108         if (phba->sli_rev == LPFC_SLI_REV4) {
7109                 wqe = &elsiocb->wqe;
7110                 /* ox-id of the frame */
7111                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7112                        rdp_context->ox_id);
7113                 bf_set(wqe_ctxt_tag,
7114                        &wqe->xmit_els_rsp.wqe_com,
7115                        rdp_context->rx_id);
7116         } else {
7117                 icmd = &elsiocb->iocb;
7118                 icmd->ulpContext = rdp_context->rx_id;
7119                 icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
7120         }
7121
7122         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7123
7124         *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
7125         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7126         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7127
7128         phba->fc_stat.elsXmitLSRJT++;
7129         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7130         elsiocb->ndlp = lpfc_nlp_get(ndlp);
7131         if (!elsiocb->ndlp) {
7132                 lpfc_els_free_iocb(phba, elsiocb);
7133                 goto free_rdp_context;
7134         }
7135
7136         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7137         if (rc == IOCB_ERROR) {
7138                 lpfc_els_free_iocb(phba, elsiocb);
7139                 lpfc_nlp_put(ndlp);
7140         }
7141
7142 free_rdp_context:
7143         /* This reference put is for the original unsolicited RDP. If the
7144          * prep failed, there is no reference to remove.
7145          */
7146         lpfc_nlp_put(ndlp);
7147         kfree(rdp_context);
7148 }
7149
7150 static int
7151 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
7152 {
7153         LPFC_MBOXQ_t *mbox = NULL;
7154         int rc;
7155
7156         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7157         if (!mbox) {
7158                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
7159                                 "7105 failed to allocate mailbox memory");
7160                 return 1;
7161         }
7162
7163         if (lpfc_sli4_dump_page_a0(phba, mbox))
7164                 goto rdp_fail;
7165         mbox->vport = rdp_context->ndlp->vport;
7166         mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
7167         mbox->ctx_u.rdp = rdp_context;
7168         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7169         if (rc == MBX_NOT_FINISHED) {
7170                 lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
7171                 return 1;
7172         }
7173
7174         return 0;
7175
7176 rdp_fail:
7177         mempool_free(mbox, phba->mbox_mem_pool);
7178         return 1;
7179 }
7180
7181 int lpfc_get_sfp_info_wait(struct lpfc_hba *phba,
7182                            struct lpfc_rdp_context *rdp_context)
7183 {
7184         LPFC_MBOXQ_t *mbox = NULL;
7185         int rc;
7186         struct lpfc_dmabuf *mp;
7187         struct lpfc_dmabuf *mpsave;
7188         void *virt;
7189         MAILBOX_t *mb;
7190
7191         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7192         if (!mbox) {
7193                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
7194                                 "7205 failed to allocate mailbox memory");
7195                 return 1;
7196         }
7197
7198         if (lpfc_sli4_dump_page_a0(phba, mbox))
7199                 goto sfp_fail;
7200         mp = mbox->ctx_buf;
7201         mpsave = mp;
7202         virt = mp->virt;
7203         if (phba->sli_rev < LPFC_SLI_REV4) {
7204                 mb = &mbox->u.mb;
7205                 mb->un.varDmp.cv = 1;
7206                 mb->un.varDmp.co = 1;
7207                 mb->un.varWords[2] = 0;
7208                 mb->un.varWords[3] = DMP_SFF_PAGE_A0_SIZE / 4;
7209                 mb->un.varWords[4] = 0;
7210                 mb->un.varWords[5] = 0;
7211                 mb->un.varWords[6] = 0;
7212                 mb->un.varWords[7] = 0;
7213                 mb->un.varWords[8] = 0;
7214                 mb->un.varWords[9] = 0;
7215                 mb->un.varWords[10] = 0;
7216                 mbox->in_ext_byte_len = DMP_SFF_PAGE_A0_SIZE;
7217                 mbox->out_ext_byte_len = DMP_SFF_PAGE_A0_SIZE;
7218                 mbox->mbox_offset_word = 5;
7219                 mbox->ext_buf = virt;
7220         } else {
7221                 bf_set(lpfc_mbx_memory_dump_type3_length,
7222                        &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
7223                 mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
7224                 mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
7225         }
7226         mbox->vport = phba->pport;
7227         rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_SLI4_CONFIG_TMO);
7228         if (rc == MBX_NOT_FINISHED) {
7229                 rc = 1;
7230                 goto error;
7231         }
7232         if (rc == MBX_TIMEOUT)
7233                 goto error;
7234         if (phba->sli_rev == LPFC_SLI_REV4)
7235                 mp = mbox->ctx_buf;
7236         else
7237                 mp = mpsave;
7238
7239         if (bf_get(lpfc_mqe_status, &mbox->u.mqe)) {
7240                 rc = 1;
7241                 goto error;
7242         }
7243
7244         lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
7245                              DMP_SFF_PAGE_A0_SIZE);
7246
7247         memset(mbox, 0, sizeof(*mbox));
7248         memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
7249         INIT_LIST_HEAD(&mp->list);
7250
7251         /* save address for completion */
7252         mbox->ctx_buf = mp;
7253         mbox->vport = phba->pport;
7254
7255         bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
7256         bf_set(lpfc_mbx_memory_dump_type3_type,
7257                &mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
7258         bf_set(lpfc_mbx_memory_dump_type3_link,
7259                &mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
7260         bf_set(lpfc_mbx_memory_dump_type3_page_no,
7261                &mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
7262         if (phba->sli_rev < LPFC_SLI_REV4) {
7263                 mb = &mbox->u.mb;
7264                 mb->un.varDmp.cv = 1;
7265                 mb->un.varDmp.co = 1;
7266                 mb->un.varWords[2] = 0;
7267                 mb->un.varWords[3] = DMP_SFF_PAGE_A2_SIZE / 4;
7268                 mb->un.varWords[4] = 0;
7269                 mb->un.varWords[5] = 0;
7270                 mb->un.varWords[6] = 0;
7271                 mb->un.varWords[7] = 0;
7272                 mb->un.varWords[8] = 0;
7273                 mb->un.varWords[9] = 0;
7274                 mb->un.varWords[10] = 0;
7275                 mbox->in_ext_byte_len = DMP_SFF_PAGE_A2_SIZE;
7276                 mbox->out_ext_byte_len = DMP_SFF_PAGE_A2_SIZE;
7277                 mbox->mbox_offset_word = 5;
7278                 mbox->ext_buf = virt;
7279         } else {
7280                 bf_set(lpfc_mbx_memory_dump_type3_length,
7281                        &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
7282                 mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
7283                 mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
7284         }
7285
7286         rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_SLI4_CONFIG_TMO);
7287
7288         if (rc == MBX_TIMEOUT)
7289                 goto error;
7290         if (bf_get(lpfc_mqe_status, &mbox->u.mqe)) {
7291                 rc = 1;
7292                 goto error;
7293         }
7294         rc = 0;
7295
7296         lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
7297                              DMP_SFF_PAGE_A2_SIZE);
7298
7299 error:
7300         if (mbox->mbox_flag & LPFC_MBX_WAKE) {
7301                 mbox->ctx_buf = mpsave;
7302                 lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
7303         }
7304
7305         return rc;
7306
7307 sfp_fail:
7308         mempool_free(mbox, phba->mbox_mem_pool);
7309         return 1;
7310 }
7311
7312 /*
7313  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
7314  * @vport: pointer to a host virtual N_Port data structure.
7315  * @cmdiocb: pointer to lpfc command iocb data structure.
7316  * @ndlp: pointer to a node-list data structure.
7317  *
7318  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
7319  * IOCB. First, the payload of the unsolicited RDP is checked.
7320  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
7321  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
7322  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
7323  * gather all data and send RDP response.
7324  *
7325  * Return code
7326  *   0 - Sent the acc response
7327  *   1 - Sent the reject response.
7328  */
7329 static int
7330 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7331                 struct lpfc_nodelist *ndlp)
7332 {
7333         struct lpfc_hba *phba = vport->phba;
7334         struct lpfc_dmabuf *pcmd;
7335         uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
7336         struct fc_rdp_req_frame *rdp_req;
7337         struct lpfc_rdp_context *rdp_context;
7338         union lpfc_wqe128 *cmd = NULL;
7339         struct ls_rjt stat;
7340
7341         if (phba->sli_rev < LPFC_SLI_REV4 ||
7342             bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7343                                                 LPFC_SLI_INTF_IF_TYPE_2) {
7344                 rjt_err = LSRJT_UNABLE_TPC;
7345                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
7346                 goto error;
7347         }
7348
7349         if (phba->sli_rev < LPFC_SLI_REV4 ||
7350             test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
7351                 rjt_err = LSRJT_UNABLE_TPC;
7352                 rjt_expl = LSEXP_REQ_UNSUPPORTED;
7353                 goto error;
7354         }
7355
7356         pcmd = cmdiocb->cmd_dmabuf;
7357         rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
7358
7359         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7360                          "2422 ELS RDP Request "
7361                          "dec len %d tag x%x port_id %d len %d\n",
7362                          be32_to_cpu(rdp_req->rdp_des_length),
7363                          be32_to_cpu(rdp_req->nport_id_desc.tag),
7364                          be32_to_cpu(rdp_req->nport_id_desc.nport_id),
7365                          be32_to_cpu(rdp_req->nport_id_desc.length));
7366
7367         if (sizeof(struct fc_rdp_nport_desc) !=
7368                         be32_to_cpu(rdp_req->rdp_des_length))
7369                 goto rjt_logerr;
7370         if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
7371                 goto rjt_logerr;
7372         if (RDP_NPORT_ID_SIZE !=
7373                         be32_to_cpu(rdp_req->nport_id_desc.length))
7374                 goto rjt_logerr;
7375         rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
7376         if (!rdp_context) {
7377                 rjt_err = LSRJT_UNABLE_TPC;
7378                 goto error;
7379         }
7380
7381         cmd = &cmdiocb->wqe;
7382         rdp_context->ndlp = lpfc_nlp_get(ndlp);
7383         if (!rdp_context->ndlp) {
7384                 kfree(rdp_context);
7385                 rjt_err = LSRJT_UNABLE_TPC;
7386                 goto error;
7387         }
7388         rdp_context->ox_id = bf_get(wqe_rcvoxid,
7389                                     &cmd->xmit_els_rsp.wqe_com);
7390         rdp_context->rx_id = bf_get(wqe_ctxt_tag,
7391                                     &cmd->xmit_els_rsp.wqe_com);
7392         rdp_context->cmpl = lpfc_els_rdp_cmpl;
7393         if (lpfc_get_rdp_info(phba, rdp_context)) {
7394                 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
7395                                  "2423 Unable to send mailbox");
7396                 kfree(rdp_context);
7397                 rjt_err = LSRJT_UNABLE_TPC;
7398                 lpfc_nlp_put(ndlp);
7399                 goto error;
7400         }
7401
7402         return 0;
7403
7404 rjt_logerr:
7405         rjt_err = LSRJT_LOGICAL_ERR;
7406
7407 error:
7408         memset(&stat, 0, sizeof(stat));
7409         stat.un.b.lsRjtRsnCode = rjt_err;
7410         stat.un.b.lsRjtRsnCodeExp = rjt_expl;
7411         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7412         return 1;
7413 }
7414
7415
7416 static void
7417 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7418 {
7419         MAILBOX_t *mb;
7420         IOCB_t *icmd;
7421         union lpfc_wqe128 *wqe;
7422         uint8_t *pcmd;
7423         struct lpfc_iocbq *elsiocb;
7424         struct lpfc_nodelist *ndlp;
7425         struct ls_rjt *stat;
7426         union lpfc_sli4_cfg_shdr *shdr;
7427         struct lpfc_lcb_context *lcb_context;
7428         struct fc_lcb_res_frame *lcb_res;
7429         uint32_t cmdsize, shdr_status, shdr_add_status;
7430         int rc;
7431
7432         mb = &pmb->u.mb;
7433         lcb_context = pmb->ctx_u.lcb;
7434         ndlp = lcb_context->ndlp;
7435         memset(&pmb->ctx_u, 0, sizeof(pmb->ctx_u));
7436         pmb->ctx_buf = NULL;
7437
7438         shdr = (union lpfc_sli4_cfg_shdr *)
7439                         &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
7440         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7441         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7442
7443         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
7444                                 "0194 SET_BEACON_CONFIG mailbox "
7445                                 "completed with status x%x add_status x%x,"
7446                                 " mbx status x%x\n",
7447                                 shdr_status, shdr_add_status, mb->mbxStatus);
7448
7449         if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
7450             (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
7451             (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
7452                 mempool_free(pmb, phba->mbox_mem_pool);
7453                 goto error;
7454         }
7455
7456         mempool_free(pmb, phba->mbox_mem_pool);
7457         cmdsize = sizeof(struct fc_lcb_res_frame);
7458         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7459                         lpfc_max_els_tries, ndlp,
7460                         ndlp->nlp_DID, ELS_CMD_ACC);
7461
7462         /* Decrement the ndlp reference count from previous mbox command */
7463         lpfc_nlp_put(ndlp);
7464
7465         if (!elsiocb)
7466                 goto free_lcb_context;
7467
7468         lcb_res = (struct fc_lcb_res_frame *)elsiocb->cmd_dmabuf->virt;
7469
7470         memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
7471
7472         if (phba->sli_rev == LPFC_SLI_REV4) {
7473                 wqe = &elsiocb->wqe;
7474                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
7475                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7476                        lcb_context->ox_id);
7477         } else {
7478                 icmd = &elsiocb->iocb;
7479                 icmd->ulpContext = lcb_context->rx_id;
7480                 icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7481         }
7482
7483         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7484         *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
7485         lcb_res->lcb_sub_command = lcb_context->sub_command;
7486         lcb_res->lcb_type = lcb_context->type;
7487         lcb_res->capability = lcb_context->capability;
7488         lcb_res->lcb_frequency = lcb_context->frequency;
7489         lcb_res->lcb_duration = lcb_context->duration;
7490         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7491         phba->fc_stat.elsXmitACC++;
7492
7493         elsiocb->ndlp = lpfc_nlp_get(ndlp);
7494         if (!elsiocb->ndlp) {
7495                 lpfc_els_free_iocb(phba, elsiocb);
7496                 goto out;
7497         }
7498
7499         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7500         if (rc == IOCB_ERROR) {
7501                 lpfc_els_free_iocb(phba, elsiocb);
7502                 lpfc_nlp_put(ndlp);
7503         }
7504  out:
7505         kfree(lcb_context);
7506         return;
7507
7508 error:
7509         cmdsize = sizeof(struct fc_lcb_res_frame);
7510         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7511                                      lpfc_max_els_tries, ndlp,
7512                                      ndlp->nlp_DID, ELS_CMD_LS_RJT);
7513         lpfc_nlp_put(ndlp);
7514         if (!elsiocb)
7515                 goto free_lcb_context;
7516
7517         if (phba->sli_rev == LPFC_SLI_REV4) {
7518                 wqe = &elsiocb->wqe;
7519                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
7520                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7521                        lcb_context->ox_id);
7522         } else {
7523                 icmd = &elsiocb->iocb;
7524                 icmd->ulpContext = lcb_context->rx_id;
7525                 icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7526         }
7527
7528         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7529
7530         *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
7531         stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7532         stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7533
7534         if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
7535                 stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
7536
7537         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7538         phba->fc_stat.elsXmitLSRJT++;
7539         elsiocb->ndlp = lpfc_nlp_get(ndlp);
7540         if (!elsiocb->ndlp) {
7541                 lpfc_els_free_iocb(phba, elsiocb);
7542                 goto free_lcb_context;
7543         }
7544
7545         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7546         if (rc == IOCB_ERROR) {
7547                 lpfc_els_free_iocb(phba, elsiocb);
7548                 lpfc_nlp_put(ndlp);
7549         }
7550 free_lcb_context:
7551         kfree(lcb_context);
7552 }
7553
7554 static int
7555 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
7556                      struct lpfc_lcb_context *lcb_context,
7557                      uint32_t beacon_state)
7558 {
7559         struct lpfc_hba *phba = vport->phba;
7560         union lpfc_sli4_cfg_shdr *cfg_shdr;
7561         LPFC_MBOXQ_t *mbox = NULL;
7562         uint32_t len;
7563         int rc;
7564
7565         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7566         if (!mbox)
7567                 return 1;
7568
7569         cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
7570         len = sizeof(struct lpfc_mbx_set_beacon_config) -
7571                 sizeof(struct lpfc_sli4_cfg_mhdr);
7572         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7573                          LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
7574                          LPFC_SLI4_MBX_EMBED);
7575         mbox->ctx_u.lcb = lcb_context;
7576         mbox->vport = phba->pport;
7577         mbox->mbox_cmpl = lpfc_els_lcb_rsp;
7578         bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
7579                phba->sli4_hba.physical_port);
7580         bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
7581                beacon_state);
7582         mbox->u.mqe.un.beacon_config.word5 = 0;         /* Reserved */
7583
7584         /*
7585          *      Check bv1s bit before issuing the mailbox
7586          *      if bv1s == 1, LCB V1 supported
7587          *      else, LCB V0 supported
7588          */
7589
7590         if (phba->sli4_hba.pc_sli4_params.bv1s) {
7591                 /* COMMON_SET_BEACON_CONFIG_V1 */
7592                 cfg_shdr->request.word9 = BEACON_VERSION_V1;
7593                 lcb_context->capability |= LCB_CAPABILITY_DURATION;
7594                 bf_set(lpfc_mbx_set_beacon_port_type,
7595                        &mbox->u.mqe.un.beacon_config, 0);
7596                 bf_set(lpfc_mbx_set_beacon_duration_v1,
7597                        &mbox->u.mqe.un.beacon_config,
7598                        be16_to_cpu(lcb_context->duration));
7599         } else {
7600                 /* COMMON_SET_BEACON_CONFIG_V0 */
7601                 if (be16_to_cpu(lcb_context->duration) != 0) {
7602                         mempool_free(mbox, phba->mbox_mem_pool);
7603                         return 1;
7604                 }
7605                 cfg_shdr->request.word9 = BEACON_VERSION_V0;
7606                 lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
7607                 bf_set(lpfc_mbx_set_beacon_state,
7608                        &mbox->u.mqe.un.beacon_config, beacon_state);
7609                 bf_set(lpfc_mbx_set_beacon_port_type,
7610                        &mbox->u.mqe.un.beacon_config, 1);
7611                 bf_set(lpfc_mbx_set_beacon_duration,
7612                        &mbox->u.mqe.un.beacon_config,
7613                        be16_to_cpu(lcb_context->duration));
7614         }
7615
7616         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7617         if (rc == MBX_NOT_FINISHED) {
7618                 mempool_free(mbox, phba->mbox_mem_pool);
7619                 return 1;
7620         }
7621
7622         return 0;
7623 }
7624
7625
7626 /**
7627  * lpfc_els_rcv_lcb - Process an unsolicited LCB
7628  * @vport: pointer to a host virtual N_Port data structure.
7629  * @cmdiocb: pointer to lpfc command iocb data structure.
7630  * @ndlp: pointer to a node-list data structure.
7631  *
7632  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
7633  * First, the payload of the unsolicited LCB is checked.
7634  * Then based on Subcommand beacon will either turn on or off.
7635  *
7636  * Return code
7637  * 0 - Sent the acc response
7638  * 1 - Sent the reject response.
7639  **/
7640 static int
7641 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7642                  struct lpfc_nodelist *ndlp)
7643 {
7644         struct lpfc_hba *phba = vport->phba;
7645         struct lpfc_dmabuf *pcmd;
7646         uint8_t *lp;
7647         struct fc_lcb_request_frame *beacon;
7648         struct lpfc_lcb_context *lcb_context;
7649         u8 state, rjt_err = 0;
7650         struct ls_rjt stat;
7651
7652         pcmd = cmdiocb->cmd_dmabuf;
7653         lp = (uint8_t *)pcmd->virt;
7654         beacon = (struct fc_lcb_request_frame *)pcmd->virt;
7655
7656         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7657                         "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
7658                         "type x%x frequency %x duration x%x\n",
7659                         lp[0], lp[1], lp[2],
7660                         beacon->lcb_command,
7661                         beacon->lcb_sub_command,
7662                         beacon->lcb_type,
7663                         beacon->lcb_frequency,
7664                         be16_to_cpu(beacon->lcb_duration));
7665
7666         if (beacon->lcb_sub_command != LPFC_LCB_ON &&
7667             beacon->lcb_sub_command != LPFC_LCB_OFF) {
7668                 rjt_err = LSRJT_CMD_UNSUPPORTED;
7669                 goto rjt;
7670         }
7671
7672         if (phba->sli_rev < LPFC_SLI_REV4  ||
7673             test_bit(HBA_FCOE_MODE, &phba->hba_flag) ||
7674             (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7675             LPFC_SLI_INTF_IF_TYPE_2)) {
7676                 rjt_err = LSRJT_CMD_UNSUPPORTED;
7677                 goto rjt;
7678         }
7679
7680         lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
7681         if (!lcb_context) {
7682                 rjt_err = LSRJT_UNABLE_TPC;
7683                 goto rjt;
7684         }
7685
7686         state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
7687         lcb_context->sub_command = beacon->lcb_sub_command;
7688         lcb_context->capability = 0;
7689         lcb_context->type = beacon->lcb_type;
7690         lcb_context->frequency = beacon->lcb_frequency;
7691         lcb_context->duration = beacon->lcb_duration;
7692         lcb_context->ox_id = get_job_rcvoxid(phba, cmdiocb);
7693         lcb_context->rx_id = get_job_ulpcontext(phba, cmdiocb);
7694         lcb_context->ndlp = lpfc_nlp_get(ndlp);
7695         if (!lcb_context->ndlp) {
7696                 rjt_err = LSRJT_UNABLE_TPC;
7697                 goto rjt_free;
7698         }
7699
7700         if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
7701                 lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
7702                                  "0193 failed to send mail box");
7703                 lpfc_nlp_put(ndlp);
7704                 rjt_err = LSRJT_UNABLE_TPC;
7705                 goto rjt_free;
7706         }
7707         return 0;
7708
7709 rjt_free:
7710         kfree(lcb_context);
7711 rjt:
7712         memset(&stat, 0, sizeof(stat));
7713         stat.un.b.lsRjtRsnCode = rjt_err;
7714         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7715         return 1;
7716 }
7717
7718
7719 /**
7720  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
7721  * @vport: pointer to a host virtual N_Port data structure.
7722  *
7723  * This routine cleans up any Registration State Change Notification
7724  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
7725  * @vport together with the host_lock is used to prevent multiple thread
7726  * trying to access the RSCN array on a same @vport at the same time.
7727  **/
7728 void
7729 lpfc_els_flush_rscn(struct lpfc_vport *vport)
7730 {
7731         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7732         struct lpfc_hba  *phba = vport->phba;
7733         int i;
7734
7735         spin_lock_irq(shost->host_lock);
7736         if (vport->fc_rscn_flush) {
7737                 /* Another thread is walking fc_rscn_id_list on this vport */
7738                 spin_unlock_irq(shost->host_lock);
7739                 return;
7740         }
7741         /* Indicate we are walking lpfc_els_flush_rscn on this vport */
7742         vport->fc_rscn_flush = 1;
7743         spin_unlock_irq(shost->host_lock);
7744
7745         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7746                 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
7747                 vport->fc_rscn_id_list[i] = NULL;
7748         }
7749         clear_bit(FC_RSCN_MODE, &vport->fc_flag);
7750         clear_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
7751         spin_lock_irq(shost->host_lock);
7752         vport->fc_rscn_id_cnt = 0;
7753         spin_unlock_irq(shost->host_lock);
7754         lpfc_can_disctmo(vport);
7755         /* Indicate we are done walking this fc_rscn_id_list */
7756         vport->fc_rscn_flush = 0;
7757 }
7758
7759 /**
7760  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
7761  * @vport: pointer to a host virtual N_Port data structure.
7762  * @did: remote destination port identifier.
7763  *
7764  * This routine checks whether there is any pending Registration State
7765  * Configuration Notification (RSCN) to a @did on @vport.
7766  *
7767  * Return code
7768  *   None zero - The @did matched with a pending rscn
7769  *   0 - not able to match @did with a pending rscn
7770  **/
7771 int
7772 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
7773 {
7774         D_ID ns_did;
7775         D_ID rscn_did;
7776         uint32_t *lp;
7777         uint32_t payload_len, i;
7778         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7779
7780         ns_did.un.word = did;
7781
7782         /* Never match fabric nodes for RSCNs */
7783         if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7784                 return 0;
7785
7786         /* If we are doing a FULL RSCN rediscovery, match everything */
7787         if (test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag))
7788                 return did;
7789
7790         spin_lock_irq(shost->host_lock);
7791         if (vport->fc_rscn_flush) {
7792                 /* Another thread is walking fc_rscn_id_list on this vport */
7793                 spin_unlock_irq(shost->host_lock);
7794                 return 0;
7795         }
7796         /* Indicate we are walking fc_rscn_id_list on this vport */
7797         vport->fc_rscn_flush = 1;
7798         spin_unlock_irq(shost->host_lock);
7799         for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7800                 lp = vport->fc_rscn_id_list[i]->virt;
7801                 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7802                 payload_len -= sizeof(uint32_t);        /* take off word 0 */
7803                 while (payload_len) {
7804                         rscn_did.un.word = be32_to_cpu(*lp++);
7805                         payload_len -= sizeof(uint32_t);
7806                         switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
7807                         case RSCN_ADDRESS_FORMAT_PORT:
7808                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7809                                     && (ns_did.un.b.area == rscn_did.un.b.area)
7810                                     && (ns_did.un.b.id == rscn_did.un.b.id))
7811                                         goto return_did_out;
7812                                 break;
7813                         case RSCN_ADDRESS_FORMAT_AREA:
7814                                 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7815                                     && (ns_did.un.b.area == rscn_did.un.b.area))
7816                                         goto return_did_out;
7817                                 break;
7818                         case RSCN_ADDRESS_FORMAT_DOMAIN:
7819                                 if (ns_did.un.b.domain == rscn_did.un.b.domain)
7820                                         goto return_did_out;
7821                                 break;
7822                         case RSCN_ADDRESS_FORMAT_FABRIC:
7823                                 goto return_did_out;
7824                         }
7825                 }
7826         }
7827         /* Indicate we are done with walking fc_rscn_id_list on this vport */
7828         vport->fc_rscn_flush = 0;
7829         return 0;
7830 return_did_out:
7831         /* Indicate we are done with walking fc_rscn_id_list on this vport */
7832         vport->fc_rscn_flush = 0;
7833         return did;
7834 }
7835
7836 /**
7837  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
7838  * @vport: pointer to a host virtual N_Port data structure.
7839  *
7840  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
7841  * state machine for a @vport's nodes that are with pending RSCN (Registration
7842  * State Change Notification).
7843  *
7844  * Return code
7845  *   0 - Successful (currently alway return 0)
7846  **/
7847 static int
7848 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
7849 {
7850         struct lpfc_nodelist *ndlp = NULL, *n;
7851
7852         /* Move all affected nodes by pending RSCNs to NPR state. */
7853         list_for_each_entry_safe(ndlp, n, &vport->fc_nodes, nlp_listp) {
7854                 if ((ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
7855                     !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
7856                         continue;
7857
7858                 /* NVME Target mode does not do RSCN Recovery. */
7859                 if (vport->phba->nvmet_support)
7860                         continue;
7861
7862                 /* If we are in the process of doing discovery on this
7863                  * NPort, let it continue on its own.
7864                  */
7865                 switch (ndlp->nlp_state) {
7866                 case  NLP_STE_PLOGI_ISSUE:
7867                 case  NLP_STE_ADISC_ISSUE:
7868                 case  NLP_STE_REG_LOGIN_ISSUE:
7869                 case  NLP_STE_PRLI_ISSUE:
7870                 case  NLP_STE_LOGO_ISSUE:
7871                         continue;
7872                 }
7873
7874                 lpfc_disc_state_machine(vport, ndlp, NULL,
7875                                         NLP_EVT_DEVICE_RECOVERY);
7876                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
7877         }
7878         return 0;
7879 }
7880
7881 /**
7882  * lpfc_send_rscn_event - Send an RSCN event to management application
7883  * @vport: pointer to a host virtual N_Port data structure.
7884  * @cmdiocb: pointer to lpfc command iocb data structure.
7885  *
7886  * lpfc_send_rscn_event sends an RSCN netlink event to management
7887  * applications.
7888  */
7889 static void
7890 lpfc_send_rscn_event(struct lpfc_vport *vport,
7891                 struct lpfc_iocbq *cmdiocb)
7892 {
7893         struct lpfc_dmabuf *pcmd;
7894         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7895         uint32_t *payload_ptr;
7896         uint32_t payload_len;
7897         struct lpfc_rscn_event_header *rscn_event_data;
7898
7899         pcmd = cmdiocb->cmd_dmabuf;
7900         payload_ptr = (uint32_t *) pcmd->virt;
7901         payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
7902
7903         rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
7904                 payload_len, GFP_KERNEL);
7905         if (!rscn_event_data) {
7906                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7907                         "0147 Failed to allocate memory for RSCN event\n");
7908                 return;
7909         }
7910         rscn_event_data->event_type = FC_REG_RSCN_EVENT;
7911         rscn_event_data->payload_length = payload_len;
7912         memcpy(rscn_event_data->rscn_payload, payload_ptr,
7913                 payload_len);
7914
7915         fc_host_post_vendor_event(shost,
7916                 fc_get_event_number(),
7917                 sizeof(struct lpfc_rscn_event_header) + payload_len,
7918                 (char *)rscn_event_data,
7919                 LPFC_NL_VENDOR_ID);
7920
7921         kfree(rscn_event_data);
7922 }
7923
7924 /**
7925  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
7926  * @vport: pointer to a host virtual N_Port data structure.
7927  * @cmdiocb: pointer to lpfc command iocb data structure.
7928  * @ndlp: pointer to a node-list data structure.
7929  *
7930  * This routine processes an unsolicited RSCN (Registration State Change
7931  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
7932  * to invoke fc_host_post_event() routine to the FC transport layer. If the
7933  * discover state machine is about to begin discovery, it just accepts the
7934  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
7935  * contains N_Port IDs for other vports on this HBA, it just accepts the
7936  * RSCN and ignore processing it. If the state machine is in the recovery
7937  * state, the fc_rscn_id_list of this @vport is walked and the
7938  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
7939  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
7940  * routine is invoked to handle the RSCN event.
7941  *
7942  * Return code
7943  *   0 - Just sent the acc response
7944  *   1 - Sent the acc response and waited for name server completion
7945  **/
7946 static int
7947 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7948                   struct lpfc_nodelist *ndlp)
7949 {
7950         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7951         struct lpfc_hba  *phba = vport->phba;
7952         struct lpfc_dmabuf *pcmd;
7953         uint32_t *lp, *datap;
7954         uint32_t payload_len, length, nportid, *cmd;
7955         int rscn_cnt;
7956         int rscn_id = 0, hba_id = 0;
7957         int i, tmo;
7958
7959         pcmd = cmdiocb->cmd_dmabuf;
7960         lp = (uint32_t *) pcmd->virt;
7961
7962         payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7963         payload_len -= sizeof(uint32_t);        /* take off word 0 */
7964         /* RSCN received */
7965         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7966                          "0214 RSCN received Data: x%lx x%x x%x x%x\n",
7967                          vport->fc_flag, payload_len, *lp,
7968                          vport->fc_rscn_id_cnt);
7969
7970         /* Send an RSCN event to the management application */
7971         lpfc_send_rscn_event(vport, cmdiocb);
7972
7973         for (i = 0; i < payload_len/sizeof(uint32_t); i++)
7974                 fc_host_post_event(shost, fc_get_event_number(),
7975                         FCH_EVT_RSCN, lp[i]);
7976
7977         /* Check if RSCN is coming from a direct-connected remote NPort */
7978         if (test_bit(FC_PT2PT, &vport->fc_flag)) {
7979                 /* If so, just ACC it, no other action needed for now */
7980                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7981                                  "2024 pt2pt RSCN %08x Data: x%lx x%x\n",
7982                                  *lp, vport->fc_flag, payload_len);
7983                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7984
7985                 /* Check to see if we need to NVME rescan this target
7986                  * remoteport.
7987                  */
7988                 if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
7989                     ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
7990                         lpfc_nvme_rescan_port(vport, ndlp);
7991                 return 0;
7992         }
7993
7994         /* If we are about to begin discovery, just ACC the RSCN.
7995          * Discovery processing will satisfy it.
7996          */
7997         if (vport->port_state <= LPFC_NS_QRY) {
7998                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7999                         "RCV RSCN ignore: did:x%x/ste:x%x flg:x%lx",
8000                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
8001
8002                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8003                 return 0;
8004         }
8005
8006         /* If this RSCN just contains NPortIDs for other vports on this HBA,
8007          * just ACC and ignore it.
8008          */
8009         if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
8010                 !(vport->cfg_peer_port_login)) {
8011                 i = payload_len;
8012                 datap = lp;
8013                 while (i > 0) {
8014                         nportid = *datap++;
8015                         nportid = ((be32_to_cpu(nportid)) & Mask_DID);
8016                         i -= sizeof(uint32_t);
8017                         rscn_id++;
8018                         if (lpfc_find_vport_by_did(phba, nportid))
8019                                 hba_id++;
8020                 }
8021                 if (rscn_id == hba_id) {
8022                         /* ALL NPortIDs in RSCN are on HBA */
8023                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8024                                          "0219 Ignore RSCN "
8025                                          "Data: x%lx x%x x%x x%x\n",
8026                                          vport->fc_flag, payload_len,
8027                                          *lp, vport->fc_rscn_id_cnt);
8028                         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8029                                 "RCV RSCN vport:  did:x%x/ste:x%x flg:x%lx",
8030                                 ndlp->nlp_DID, vport->port_state,
8031                                 ndlp->nlp_flag);
8032
8033                         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
8034                                 ndlp, NULL);
8035                         /* Restart disctmo if its already running */
8036                         if (test_bit(FC_DISC_TMO, &vport->fc_flag)) {
8037                                 tmo = ((phba->fc_ratov * 3) + 3);
8038                                 mod_timer(&vport->fc_disctmo,
8039                                           jiffies +
8040                                           msecs_to_jiffies(1000 * tmo));
8041                         }
8042                         return 0;
8043                 }
8044         }
8045
8046         spin_lock_irq(shost->host_lock);
8047         if (vport->fc_rscn_flush) {
8048                 /* Another thread is walking fc_rscn_id_list on this vport */
8049                 spin_unlock_irq(shost->host_lock);
8050                 set_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
8051                 /* Send back ACC */
8052                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8053                 return 0;
8054         }
8055         /* Indicate we are walking fc_rscn_id_list on this vport */
8056         vport->fc_rscn_flush = 1;
8057         spin_unlock_irq(shost->host_lock);
8058         /* Get the array count after successfully have the token */
8059         rscn_cnt = vport->fc_rscn_id_cnt;
8060         /* If we are already processing an RSCN, save the received
8061          * RSCN payload buffer, cmdiocb->cmd_dmabuf to process later.
8062          */
8063         if (test_bit(FC_RSCN_MODE, &vport->fc_flag) ||
8064             test_bit(FC_NDISC_ACTIVE, &vport->fc_flag)) {
8065                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8066                         "RCV RSCN defer:  did:x%x/ste:x%x flg:x%lx",
8067                         ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
8068
8069                 set_bit(FC_RSCN_DEFERRED, &vport->fc_flag);
8070
8071                 /* Restart disctmo if its already running */
8072                 if (test_bit(FC_DISC_TMO, &vport->fc_flag)) {
8073                         tmo = ((phba->fc_ratov * 3) + 3);
8074                         mod_timer(&vport->fc_disctmo,
8075                                   jiffies + msecs_to_jiffies(1000 * tmo));
8076                 }
8077                 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
8078                     !test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag)) {
8079                         set_bit(FC_RSCN_MODE, &vport->fc_flag);
8080                         if (rscn_cnt) {
8081                                 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
8082                                 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
8083                         }
8084                         if ((rscn_cnt) &&
8085                             (payload_len + length <= LPFC_BPL_SIZE)) {
8086                                 *cmd &= ELS_CMD_MASK;
8087                                 *cmd |= cpu_to_be32(payload_len + length);
8088                                 memcpy(((uint8_t *)cmd) + length, lp,
8089                                        payload_len);
8090                         } else {
8091                                 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
8092                                 vport->fc_rscn_id_cnt++;
8093                                 /* If we zero, cmdiocb->cmd_dmabuf, the calling
8094                                  * routine will not try to free it.
8095                                  */
8096                                 cmdiocb->cmd_dmabuf = NULL;
8097                         }
8098                         /* Deferred RSCN */
8099                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8100                                          "0235 Deferred RSCN "
8101                                          "Data: x%x x%lx x%x\n",
8102                                          vport->fc_rscn_id_cnt, vport->fc_flag,
8103                                          vport->port_state);
8104                 } else {
8105                         set_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
8106                         /* ReDiscovery RSCN */
8107                         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8108                                          "0234 ReDiscovery RSCN "
8109                                          "Data: x%x x%lx x%x\n",
8110                                          vport->fc_rscn_id_cnt, vport->fc_flag,
8111                                          vport->port_state);
8112                 }
8113                 /* Indicate we are done walking fc_rscn_id_list on this vport */
8114                 vport->fc_rscn_flush = 0;
8115                 /* Send back ACC */
8116                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8117                 /* send RECOVERY event for ALL nodes that match RSCN payload */
8118                 lpfc_rscn_recovery_check(vport);
8119                 return 0;
8120         }
8121         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8122                 "RCV RSCN:        did:x%x/ste:x%x flg:x%lx",
8123                 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
8124
8125         set_bit(FC_RSCN_MODE, &vport->fc_flag);
8126         vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
8127         /* Indicate we are done walking fc_rscn_id_list on this vport */
8128         vport->fc_rscn_flush = 0;
8129         /*
8130          * If we zero, cmdiocb->cmd_dmabuf, the calling routine will
8131          * not try to free it.
8132          */
8133         cmdiocb->cmd_dmabuf = NULL;
8134         lpfc_set_disctmo(vport);
8135         /* Send back ACC */
8136         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8137         /* send RECOVERY event for ALL nodes that match RSCN payload */
8138         lpfc_rscn_recovery_check(vport);
8139         return lpfc_els_handle_rscn(vport);
8140 }
8141
8142 /**
8143  * lpfc_els_handle_rscn - Handle rscn for a vport
8144  * @vport: pointer to a host virtual N_Port data structure.
8145  *
8146  * This routine handles the Registration State Configuration Notification
8147  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
8148  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
8149  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
8150  * NameServer shall be issued. If CT command to the NameServer fails to be
8151  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
8152  * RSCN activities with the @vport.
8153  *
8154  * Return code
8155  *   0 - Cleaned up rscn on the @vport
8156  *   1 - Wait for plogi to name server before proceed
8157  **/
8158 int
8159 lpfc_els_handle_rscn(struct lpfc_vport *vport)
8160 {
8161         struct lpfc_nodelist *ndlp;
8162         struct lpfc_hba  *phba = vport->phba;
8163
8164         /* Ignore RSCN if the port is being torn down. */
8165         if (test_bit(FC_UNLOADING, &vport->load_flag)) {
8166                 lpfc_els_flush_rscn(vport);
8167                 return 0;
8168         }
8169
8170         /* Start timer for RSCN processing */
8171         lpfc_set_disctmo(vport);
8172
8173         /* RSCN processed */
8174         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8175                          "0215 RSCN processed Data: x%lx x%x x%x x%x x%x x%x\n",
8176                          vport->fc_flag, 0, vport->fc_rscn_id_cnt,
8177                          vport->port_state, vport->num_disc_nodes,
8178                          vport->gidft_inp);
8179
8180         /* To process RSCN, first compare RSCN data with NameServer */
8181         vport->fc_ns_retry = 0;
8182         vport->num_disc_nodes = 0;
8183
8184         ndlp = lpfc_findnode_did(vport, NameServer_DID);
8185         if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
8186                 /* Good ndlp, issue CT Request to NameServer.  Need to
8187                  * know how many gidfts were issued.  If none, then just
8188                  * flush the RSCN.  Otherwise, the outstanding requests
8189                  * need to complete.
8190                  */
8191                 if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
8192                         if (lpfc_issue_gidft(vport) > 0)
8193                                 return 1;
8194                 } else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
8195                         if (lpfc_issue_gidpt(vport) > 0)
8196                                 return 1;
8197                 } else {
8198                         return 1;
8199                 }
8200         } else {
8201                 /* Nameserver login in question.  Revalidate. */
8202                 if (ndlp) {
8203                         ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
8204                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8205                 } else {
8206                         ndlp = lpfc_nlp_init(vport, NameServer_DID);
8207                         if (!ndlp) {
8208                                 lpfc_els_flush_rscn(vport);
8209                                 return 0;
8210                         }
8211                         ndlp->nlp_prev_state = ndlp->nlp_state;
8212                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8213                 }
8214                 ndlp->nlp_type |= NLP_FABRIC;
8215                 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
8216                 /* Wait for NameServer login cmpl before we can
8217                  * continue
8218                  */
8219                 return 1;
8220         }
8221
8222         lpfc_els_flush_rscn(vport);
8223         return 0;
8224 }
8225
8226 /**
8227  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
8228  * @vport: pointer to a host virtual N_Port data structure.
8229  * @cmdiocb: pointer to lpfc command iocb data structure.
8230  * @ndlp: pointer to a node-list data structure.
8231  *
8232  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
8233  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
8234  * point topology. As an unsolicited FLOGI should not be received in a loop
8235  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
8236  * lpfc_check_sparm() routine is invoked to check the parameters in the
8237  * unsolicited FLOGI. If parameters validation failed, the routine
8238  * lpfc_els_rsp_reject() shall be called with reject reason code set to
8239  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
8240  * FLOGI shall be compared with the Port WWN of the @vport to determine who
8241  * will initiate PLOGI. The higher lexicographical value party shall has
8242  * higher priority (as the winning port) and will initiate PLOGI and
8243  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
8244  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
8245  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
8246  *
8247  * Return code
8248  *   0 - Successfully processed the unsolicited flogi
8249  *   1 - Failed to process the unsolicited flogi
8250  **/
8251 static int
8252 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8253                    struct lpfc_nodelist *ndlp)
8254 {
8255         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8256         struct lpfc_hba  *phba = vport->phba;
8257         struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
8258         uint32_t *lp = (uint32_t *) pcmd->virt;
8259         union lpfc_wqe128 *wqe = &cmdiocb->wqe;
8260         struct serv_parm *sp;
8261         LPFC_MBOXQ_t *mbox;
8262         uint32_t cmd, did;
8263         int rc;
8264         unsigned long fc_flag = 0;
8265         uint32_t port_state = 0;
8266
8267         /* Clear external loopback plug detected flag */
8268         phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
8269
8270         cmd = *lp++;
8271         sp = (struct serv_parm *) lp;
8272
8273         /* FLOGI received */
8274
8275         lpfc_set_disctmo(vport);
8276
8277         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8278                 /* We should never receive a FLOGI in loop mode, ignore it */
8279                 did =  bf_get(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest);
8280
8281                 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
8282                    Loop Mode */
8283                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8284                                  "0113 An FLOGI ELS command x%x was "
8285                                  "received from DID x%x in Loop Mode\n",
8286                                  cmd, did);
8287                 return 1;
8288         }
8289
8290         (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
8291
8292         /*
8293          * If our portname is greater than the remote portname,
8294          * then we initiate Nport login.
8295          */
8296
8297         rc = memcmp(&vport->fc_portname, &sp->portName,
8298                     sizeof(struct lpfc_name));
8299
8300         if (!rc) {
8301                 if (phba->sli_rev < LPFC_SLI_REV4) {
8302                         mbox = mempool_alloc(phba->mbox_mem_pool,
8303                                              GFP_KERNEL);
8304                         if (!mbox)
8305                                 return 1;
8306                         lpfc_linkdown(phba);
8307                         lpfc_init_link(phba, mbox,
8308                                        phba->cfg_topology,
8309                                        phba->cfg_link_speed);
8310                         mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8311                         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8312                         mbox->vport = vport;
8313                         rc = lpfc_sli_issue_mbox(phba, mbox,
8314                                                  MBX_NOWAIT);
8315                         lpfc_set_loopback_flag(phba);
8316                         if (rc == MBX_NOT_FINISHED)
8317                                 mempool_free(mbox, phba->mbox_mem_pool);
8318                         return 1;
8319                 }
8320
8321                 /* External loopback plug insertion detected */
8322                 phba->link_flag |= LS_EXTERNAL_LOOPBACK;
8323
8324                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_LIBDFC,
8325                                  "1119 External Loopback plug detected\n");
8326
8327                 /* abort the flogi coming back to ourselves
8328                  * due to external loopback on the port.
8329                  */
8330                 lpfc_els_abort_flogi(phba);
8331                 return 0;
8332
8333         } else if (rc > 0) {    /* greater than */
8334                 set_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
8335
8336                 /* If we have the high WWPN we can assign our own
8337                  * myDID; otherwise, we have to WAIT for a PLOGI
8338                  * from the remote NPort to find out what it
8339                  * will be.
8340                  */
8341                 vport->fc_myDID = PT2PT_LocalID;
8342         } else {
8343                 vport->fc_myDID = PT2PT_RemoteID;
8344         }
8345
8346         /*
8347          * The vport state should go to LPFC_FLOGI only
8348          * AFTER we issue a FLOGI, not receive one.
8349          */
8350         spin_lock_irq(shost->host_lock);
8351         fc_flag = vport->fc_flag;
8352         port_state = vport->port_state;
8353         /* Acking an unsol FLOGI.  Count 1 for link bounce
8354          * work-around.
8355          */
8356         vport->rcv_flogi_cnt++;
8357         spin_unlock_irq(shost->host_lock);
8358         set_bit(FC_PT2PT, &vport->fc_flag);
8359         clear_bit(FC_FABRIC, &vport->fc_flag);
8360         clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
8361         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8362                          "3311 Rcv Flogi PS x%x new PS x%x "
8363                          "fc_flag x%lx new fc_flag x%lx\n",
8364                          port_state, vport->port_state,
8365                          fc_flag, vport->fc_flag);
8366
8367         /*
8368          * We temporarily set fc_myDID to make it look like we are
8369          * a Fabric. This is done just so we end up with the right
8370          * did / sid on the FLOGI ACC rsp.
8371          */
8372         did = vport->fc_myDID;
8373         vport->fc_myDID = Fabric_DID;
8374
8375         memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
8376
8377         /* Defer ACC response until AFTER we issue a FLOGI */
8378         if (!test_bit(HBA_FLOGI_ISSUED, &phba->hba_flag)) {
8379                 phba->defer_flogi_acc.rx_id = bf_get(wqe_ctxt_tag,
8380                                                      &wqe->xmit_els_rsp.wqe_com);
8381                 phba->defer_flogi_acc.ox_id = bf_get(wqe_rcvoxid,
8382                                                      &wqe->xmit_els_rsp.wqe_com);
8383
8384                 vport->fc_myDID = did;
8385
8386                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8387                                  "3344 Deferring FLOGI ACC: rx_id: x%x,"
8388                                  " ox_id: x%x, hba_flag x%lx\n",
8389                                  phba->defer_flogi_acc.rx_id,
8390                                  phba->defer_flogi_acc.ox_id, phba->hba_flag);
8391
8392                 phba->defer_flogi_acc.flag = true;
8393
8394                 /* This nlp_get is paired with nlp_puts that reset the
8395                  * defer_flogi_acc.flag back to false.  We need to retain
8396                  * a kref on the ndlp until the deferred FLOGI ACC is
8397                  * processed or cancelled.
8398                  */
8399                 phba->defer_flogi_acc.ndlp = lpfc_nlp_get(ndlp);
8400                 return 0;
8401         }
8402
8403         /* Send back ACC */
8404         lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
8405
8406         /* Now lets put fc_myDID back to what its supposed to be */
8407         vport->fc_myDID = did;
8408
8409         return 0;
8410 }
8411
8412 /**
8413  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
8414  * @vport: pointer to a host virtual N_Port data structure.
8415  * @cmdiocb: pointer to lpfc command iocb data structure.
8416  * @ndlp: pointer to a node-list data structure.
8417  *
8418  * This routine processes Request Node Identification Data (RNID) IOCB
8419  * received as an ELS unsolicited event. Only when the RNID specified format
8420  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
8421  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
8422  * Accept (ACC) the RNID ELS command. All the other RNID formats are
8423  * rejected by invoking the lpfc_els_rsp_reject() routine.
8424  *
8425  * Return code
8426  *   0 - Successfully processed rnid iocb (currently always return 0)
8427  **/
8428 static int
8429 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8430                   struct lpfc_nodelist *ndlp)
8431 {
8432         struct lpfc_dmabuf *pcmd;
8433         uint32_t *lp;
8434         RNID *rn;
8435         struct ls_rjt stat;
8436
8437         pcmd = cmdiocb->cmd_dmabuf;
8438         lp = (uint32_t *) pcmd->virt;
8439
8440         lp++;
8441         rn = (RNID *) lp;
8442
8443         /* RNID received */
8444
8445         switch (rn->Format) {
8446         case 0:
8447         case RNID_TOPOLOGY_DISC:
8448                 /* Send back ACC */
8449                 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
8450                 break;
8451         default:
8452                 /* Reject this request because format not supported */
8453                 stat.un.b.lsRjtRsvd0 = 0;
8454                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8455                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8456                 stat.un.b.vendorUnique = 0;
8457                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8458                         NULL);
8459         }
8460         return 0;
8461 }
8462
8463 /**
8464  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
8465  * @vport: pointer to a host virtual N_Port data structure.
8466  * @cmdiocb: pointer to lpfc command iocb data structure.
8467  * @ndlp: pointer to a node-list data structure.
8468  *
8469  * Return code
8470  *   0 - Successfully processed echo iocb (currently always return 0)
8471  **/
8472 static int
8473 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8474                   struct lpfc_nodelist *ndlp)
8475 {
8476         uint8_t *pcmd;
8477
8478         pcmd = (uint8_t *)cmdiocb->cmd_dmabuf->virt;
8479
8480         /* skip over first word of echo command to find echo data */
8481         pcmd += sizeof(uint32_t);
8482
8483         lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
8484         return 0;
8485 }
8486
8487 /**
8488  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
8489  * @vport: pointer to a host virtual N_Port data structure.
8490  * @cmdiocb: pointer to lpfc command iocb data structure.
8491  * @ndlp: pointer to a node-list data structure.
8492  *
8493  * This routine processes a Link Incident Report Registration(LIRR) IOCB
8494  * received as an ELS unsolicited event. Currently, this function just invokes
8495  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
8496  *
8497  * Return code
8498  *   0 - Successfully processed lirr iocb (currently always return 0)
8499  **/
8500 static int
8501 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8502                   struct lpfc_nodelist *ndlp)
8503 {
8504         struct ls_rjt stat;
8505
8506         /* For now, unconditionally reject this command */
8507         stat.un.b.lsRjtRsvd0 = 0;
8508         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8509         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8510         stat.un.b.vendorUnique = 0;
8511         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8512         return 0;
8513 }
8514
8515 /**
8516  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
8517  * @vport: pointer to a host virtual N_Port data structure.
8518  * @cmdiocb: pointer to lpfc command iocb data structure.
8519  * @ndlp: pointer to a node-list data structure.
8520  *
8521  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
8522  * received as an ELS unsolicited event. A request to RRQ shall only
8523  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
8524  * Nx_Port N_Port_ID of the target Exchange is the same as the
8525  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
8526  * not accepted, an LS_RJT with reason code "Unable to perform
8527  * command request" and reason code explanation "Invalid Originator
8528  * S_ID" shall be returned. For now, we just unconditionally accept
8529  * RRQ from the target.
8530  **/
8531 static void
8532 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8533                  struct lpfc_nodelist *ndlp)
8534 {
8535         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8536         if (vport->phba->sli_rev == LPFC_SLI_REV4)
8537                 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
8538 }
8539
8540 /**
8541  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
8542  * @phba: pointer to lpfc hba data structure.
8543  * @pmb: pointer to the driver internal queue element for mailbox command.
8544  *
8545  * This routine is the completion callback function for the MBX_READ_LNK_STAT
8546  * mailbox command. This callback function is to actually send the Accept
8547  * (ACC) response to a Read Link Status (RLS) unsolicited IOCB event. It
8548  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
8549  * mailbox command, constructs the RLS response with the link statistics
8550  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
8551  * response to the RLS.
8552  *
8553  * Note that the ndlp reference count will be incremented by 1 for holding the
8554  * ndlp and the reference to ndlp will be stored into the ndlp field of
8555  * the IOCB for the completion callback function to the RLS Accept Response
8556  * ELS IOCB command.
8557  *
8558  **/
8559 static void
8560 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8561 {
8562         int rc = 0;
8563         MAILBOX_t *mb;
8564         IOCB_t *icmd;
8565         union lpfc_wqe128 *wqe;
8566         struct RLS_RSP *rls_rsp;
8567         uint8_t *pcmd;
8568         struct lpfc_iocbq *elsiocb;
8569         struct lpfc_nodelist *ndlp;
8570         uint16_t oxid;
8571         uint16_t rxid;
8572         uint32_t cmdsize;
8573         u32 ulp_context;
8574
8575         mb = &pmb->u.mb;
8576
8577         ndlp = pmb->ctx_ndlp;
8578         rxid = (uint16_t)(pmb->ctx_u.ox_rx_id & 0xffff);
8579         oxid = (uint16_t)((pmb->ctx_u.ox_rx_id >> 16) & 0xffff);
8580         memset(&pmb->ctx_u, 0, sizeof(pmb->ctx_u));
8581         pmb->ctx_ndlp = NULL;
8582
8583         if (mb->mbxStatus) {
8584                 mempool_free(pmb, phba->mbox_mem_pool);
8585                 return;
8586         }
8587
8588         cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
8589         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8590                                      lpfc_max_els_tries, ndlp,
8591                                      ndlp->nlp_DID, ELS_CMD_ACC);
8592
8593         /* Decrement the ndlp reference count from previous mbox command */
8594         lpfc_nlp_put(ndlp);
8595
8596         if (!elsiocb) {
8597                 mempool_free(pmb, phba->mbox_mem_pool);
8598                 return;
8599         }
8600
8601         ulp_context = get_job_ulpcontext(phba, elsiocb);
8602         if (phba->sli_rev == LPFC_SLI_REV4) {
8603                 wqe = &elsiocb->wqe;
8604                 /* Xri / rx_id */
8605                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, rxid);
8606                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com, oxid);
8607         } else {
8608                 icmd = &elsiocb->iocb;
8609                 icmd->ulpContext = rxid;
8610                 icmd->unsli3.rcvsli3.ox_id = oxid;
8611         }
8612
8613         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8614         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8615         pcmd += sizeof(uint32_t); /* Skip past command */
8616         rls_rsp = (struct RLS_RSP *)pcmd;
8617
8618         rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
8619         rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
8620         rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
8621         rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
8622         rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
8623         rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
8624         mempool_free(pmb, phba->mbox_mem_pool);
8625         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
8626         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8627                          "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
8628                          "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x\n",
8629                          elsiocb->iotag, ulp_context,
8630                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8631                          ndlp->nlp_rpi);
8632         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8633         phba->fc_stat.elsXmitACC++;
8634         elsiocb->ndlp = lpfc_nlp_get(ndlp);
8635         if (!elsiocb->ndlp) {
8636                 lpfc_els_free_iocb(phba, elsiocb);
8637                 return;
8638         }
8639
8640         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8641         if (rc == IOCB_ERROR) {
8642                 lpfc_els_free_iocb(phba, elsiocb);
8643                 lpfc_nlp_put(ndlp);
8644         }
8645         return;
8646 }
8647
8648 /**
8649  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
8650  * @vport: pointer to a host virtual N_Port data structure.
8651  * @cmdiocb: pointer to lpfc command iocb data structure.
8652  * @ndlp: pointer to a node-list data structure.
8653  *
8654  * This routine processes Read Link Status (RLS) IOCB received as an
8655  * ELS unsolicited event. It first checks the remote port state. If the
8656  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8657  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8658  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
8659  * for reading the HBA link statistics. It is for the callback function,
8660  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
8661  * to actually sending out RPL Accept (ACC) response.
8662  *
8663  * Return codes
8664  *   0 - Successfully processed rls iocb (currently always return 0)
8665  **/
8666 static int
8667 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8668                  struct lpfc_nodelist *ndlp)
8669 {
8670         struct lpfc_hba *phba = vport->phba;
8671         LPFC_MBOXQ_t *mbox;
8672         struct ls_rjt stat;
8673         u32 ctx = get_job_ulpcontext(phba, cmdiocb);
8674         u32 ox_id = get_job_rcvoxid(phba, cmdiocb);
8675
8676         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8677             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8678                 /* reject the unsolicited RLS request and done with it */
8679                 goto reject_out;
8680
8681         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
8682         if (mbox) {
8683                 lpfc_read_lnk_stat(phba, mbox);
8684                 mbox->ctx_u.ox_rx_id = ox_id << 16 | ctx;
8685                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
8686                 if (!mbox->ctx_ndlp)
8687                         goto node_err;
8688                 mbox->vport = vport;
8689                 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
8690                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
8691                         != MBX_NOT_FINISHED)
8692                         /* Mbox completion will send ELS Response */
8693                         return 0;
8694                 /* Decrement reference count used for the failed mbox
8695                  * command.
8696                  */
8697                 lpfc_nlp_put(ndlp);
8698 node_err:
8699                 mempool_free(mbox, phba->mbox_mem_pool);
8700         }
8701 reject_out:
8702         /* issue rejection response */
8703         stat.un.b.lsRjtRsvd0 = 0;
8704         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8705         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8706         stat.un.b.vendorUnique = 0;
8707         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8708         return 0;
8709 }
8710
8711 /**
8712  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
8713  * @vport: pointer to a host virtual N_Port data structure.
8714  * @cmdiocb: pointer to lpfc command iocb data structure.
8715  * @ndlp: pointer to a node-list data structure.
8716  *
8717  * This routine processes Read Timout Value (RTV) IOCB received as an
8718  * ELS unsolicited event. It first checks the remote port state. If the
8719  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8720  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8721  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
8722  * Value (RTV) unsolicited IOCB event.
8723  *
8724  * Note that the ndlp reference count will be incremented by 1 for holding the
8725  * ndlp and the reference to ndlp will be stored into the ndlp field of
8726  * the IOCB for the completion callback function to the RTV Accept Response
8727  * ELS IOCB command.
8728  *
8729  * Return codes
8730  *   0 - Successfully processed rtv iocb (currently always return 0)
8731  **/
8732 static int
8733 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8734                  struct lpfc_nodelist *ndlp)
8735 {
8736         int rc = 0;
8737         IOCB_t *icmd;
8738         union lpfc_wqe128 *wqe;
8739         struct lpfc_hba *phba = vport->phba;
8740         struct ls_rjt stat;
8741         struct RTV_RSP *rtv_rsp;
8742         uint8_t *pcmd;
8743         struct lpfc_iocbq *elsiocb;
8744         uint32_t cmdsize;
8745         u32 ulp_context;
8746
8747         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8748             (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8749                 /* reject the unsolicited RTV request and done with it */
8750                 goto reject_out;
8751
8752         cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
8753         elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8754                                      lpfc_max_els_tries, ndlp,
8755                                      ndlp->nlp_DID, ELS_CMD_ACC);
8756
8757         if (!elsiocb)
8758                 return 1;
8759
8760         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8761         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8762         pcmd += sizeof(uint32_t); /* Skip past command */
8763
8764         ulp_context = get_job_ulpcontext(phba, elsiocb);
8765         /* use the command's xri in the response */
8766         if (phba->sli_rev == LPFC_SLI_REV4) {
8767                 wqe = &elsiocb->wqe;
8768                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
8769                        get_job_ulpcontext(phba, cmdiocb));
8770                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8771                        get_job_rcvoxid(phba, cmdiocb));
8772         } else {
8773                 icmd = &elsiocb->iocb;
8774                 icmd->ulpContext = get_job_ulpcontext(phba, cmdiocb);
8775                 icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, cmdiocb);
8776         }
8777
8778         rtv_rsp = (struct RTV_RSP *)pcmd;
8779
8780         /* populate RTV payload */
8781         rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
8782         rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
8783         bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
8784         bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
8785         rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
8786
8787         /* Xmit ELS RLS ACC response tag <ulpIoTag> */
8788         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8789                          "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
8790                          "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x, "
8791                          "Data: x%x x%x x%x\n",
8792                          elsiocb->iotag, ulp_context,
8793                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8794                          ndlp->nlp_rpi,
8795                         rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
8796         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8797         phba->fc_stat.elsXmitACC++;
8798         elsiocb->ndlp = lpfc_nlp_get(ndlp);
8799         if (!elsiocb->ndlp) {
8800                 lpfc_els_free_iocb(phba, elsiocb);
8801                 return 0;
8802         }
8803
8804         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8805         if (rc == IOCB_ERROR) {
8806                 lpfc_els_free_iocb(phba, elsiocb);
8807                 lpfc_nlp_put(ndlp);
8808         }
8809         return 0;
8810
8811 reject_out:
8812         /* issue rejection response */
8813         stat.un.b.lsRjtRsvd0 = 0;
8814         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8815         stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8816         stat.un.b.vendorUnique = 0;
8817         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8818         return 0;
8819 }
8820
8821 /* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
8822  * @vport: pointer to a host virtual N_Port data structure.
8823  * @ndlp: pointer to a node-list data structure.
8824  * @did: DID of the target.
8825  * @rrq: Pointer to the rrq struct.
8826  *
8827  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
8828  * successful, the completion handler will clear the RRQ.
8829  *
8830  * Return codes
8831  *   0 - Successfully sent rrq els iocb.
8832  *   1 - Failed to send rrq els iocb.
8833  **/
8834 static int
8835 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
8836                         uint32_t did, struct lpfc_node_rrq *rrq)
8837 {
8838         struct lpfc_hba  *phba = vport->phba;
8839         struct RRQ *els_rrq;
8840         struct lpfc_iocbq *elsiocb;
8841         uint8_t *pcmd;
8842         uint16_t cmdsize;
8843         int ret;
8844
8845         if (!ndlp)
8846                 return 1;
8847
8848         /* If ndlp is not NULL, we will bump the reference count on it */
8849         cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
8850         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
8851                                      ELS_CMD_RRQ);
8852         if (!elsiocb)
8853                 return 1;
8854
8855         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8856
8857         /* For RRQ request, remainder of payload is Exchange IDs */
8858         *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
8859         pcmd += sizeof(uint32_t);
8860         els_rrq = (struct RRQ *) pcmd;
8861
8862         bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
8863         bf_set(rrq_rxid, els_rrq, rrq->rxid);
8864         bf_set(rrq_did, els_rrq, vport->fc_myDID);
8865         els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
8866         els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
8867
8868
8869         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8870                 "Issue RRQ:     did:x%x",
8871                 did, rrq->xritag, rrq->rxid);
8872         elsiocb->context_un.rrq = rrq;
8873         elsiocb->cmd_cmpl = lpfc_cmpl_els_rrq;
8874
8875         elsiocb->ndlp = lpfc_nlp_get(ndlp);
8876         if (!elsiocb->ndlp)
8877                 goto io_err;
8878
8879         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8880         if (ret == IOCB_ERROR) {
8881                 lpfc_nlp_put(ndlp);
8882                 goto io_err;
8883         }
8884         return 0;
8885
8886  io_err:
8887         lpfc_els_free_iocb(phba, elsiocb);
8888         return 1;
8889 }
8890
8891 /**
8892  * lpfc_send_rrq - Sends ELS RRQ if needed.
8893  * @phba: pointer to lpfc hba data structure.
8894  * @rrq: pointer to the active rrq.
8895  *
8896  * This routine will call the lpfc_issue_els_rrq if the rrq is
8897  * still active for the xri. If this function returns a failure then
8898  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
8899  *
8900  * Returns 0 Success.
8901  *         1 Failure.
8902  **/
8903 int
8904 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
8905 {
8906         struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
8907                                                        rrq->nlp_DID);
8908         if (!ndlp)
8909                 return 1;
8910
8911         if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
8912                 return lpfc_issue_els_rrq(rrq->vport, ndlp,
8913                                          rrq->nlp_DID, rrq);
8914         else
8915                 return 1;
8916 }
8917
8918 /**
8919  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
8920  * @vport: pointer to a host virtual N_Port data structure.
8921  * @cmdsize: size of the ELS command.
8922  * @oldiocb: pointer to the original lpfc command iocb data structure.
8923  * @ndlp: pointer to a node-list data structure.
8924  *
8925  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
8926  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
8927  *
8928  * Note that the ndlp reference count will be incremented by 1 for holding the
8929  * ndlp and the reference to ndlp will be stored into the ndlp field of
8930  * the IOCB for the completion callback function to the RPL Accept Response
8931  * ELS command.
8932  *
8933  * Return code
8934  *   0 - Successfully issued ACC RPL ELS command
8935  *   1 - Failed to issue ACC RPL ELS command
8936  **/
8937 static int
8938 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
8939                      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
8940 {
8941         int rc = 0;
8942         struct lpfc_hba *phba = vport->phba;
8943         IOCB_t *icmd;
8944         union lpfc_wqe128 *wqe;
8945         RPL_RSP rpl_rsp;
8946         struct lpfc_iocbq *elsiocb;
8947         uint8_t *pcmd;
8948         u32 ulp_context;
8949
8950         elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
8951                                      ndlp->nlp_DID, ELS_CMD_ACC);
8952
8953         if (!elsiocb)
8954                 return 1;
8955
8956         ulp_context = get_job_ulpcontext(phba, elsiocb);
8957         if (phba->sli_rev == LPFC_SLI_REV4) {
8958                 wqe = &elsiocb->wqe;
8959                 /* Xri / rx_id */
8960                 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
8961                        get_job_ulpcontext(phba, oldiocb));
8962                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8963                        get_job_rcvoxid(phba, oldiocb));
8964         } else {
8965                 icmd = &elsiocb->iocb;
8966                 icmd->ulpContext = get_job_ulpcontext(phba, oldiocb);
8967                 icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, oldiocb);
8968         }
8969
8970         pcmd = elsiocb->cmd_dmabuf->virt;
8971         *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8972         pcmd += sizeof(uint16_t);
8973         *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
8974         pcmd += sizeof(uint16_t);
8975
8976         /* Setup the RPL ACC payload */
8977         rpl_rsp.listLen = be32_to_cpu(1);
8978         rpl_rsp.index = 0;
8979         rpl_rsp.port_num_blk.portNum = 0;
8980         rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
8981         memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
8982             sizeof(struct lpfc_name));
8983         memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
8984         /* Xmit ELS RPL ACC response tag <ulpIoTag> */
8985         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8986                          "0120 Xmit ELS RPL ACC response tag x%x "
8987                          "xri x%x, did x%x, nlp_flag x%lx, nlp_state x%x, "
8988                          "rpi x%x\n",
8989                          elsiocb->iotag, ulp_context,
8990                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8991                          ndlp->nlp_rpi);
8992         elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8993         phba->fc_stat.elsXmitACC++;
8994         elsiocb->ndlp = lpfc_nlp_get(ndlp);
8995         if (!elsiocb->ndlp) {
8996                 lpfc_els_free_iocb(phba, elsiocb);
8997                 return 1;
8998         }
8999
9000         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
9001         if (rc == IOCB_ERROR) {
9002                 lpfc_els_free_iocb(phba, elsiocb);
9003                 lpfc_nlp_put(ndlp);
9004                 return 1;
9005         }
9006
9007         return 0;
9008 }
9009
9010 /**
9011  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
9012  * @vport: pointer to a host virtual N_Port data structure.
9013  * @cmdiocb: pointer to lpfc command iocb data structure.
9014  * @ndlp: pointer to a node-list data structure.
9015  *
9016  * This routine processes Read Port List (RPL) IOCB received as an ELS
9017  * unsolicited event. It first checks the remote port state. If the remote
9018  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
9019  * invokes the lpfc_els_rsp_reject() routine to send reject response.
9020  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
9021  * to accept the RPL.
9022  *
9023  * Return code
9024  *   0 - Successfully processed rpl iocb (currently always return 0)
9025  **/
9026 static int
9027 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9028                  struct lpfc_nodelist *ndlp)
9029 {
9030         struct lpfc_dmabuf *pcmd;
9031         uint32_t *lp;
9032         uint32_t maxsize;
9033         uint16_t cmdsize;
9034         RPL *rpl;
9035         struct ls_rjt stat;
9036
9037         if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
9038             (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
9039                 /* issue rejection response */
9040                 stat.un.b.lsRjtRsvd0 = 0;
9041                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
9042                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
9043                 stat.un.b.vendorUnique = 0;
9044                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
9045                         NULL);
9046                 /* rejected the unsolicited RPL request and done with it */
9047                 return 0;
9048         }
9049
9050         pcmd = cmdiocb->cmd_dmabuf;
9051         lp = (uint32_t *) pcmd->virt;
9052         rpl = (RPL *) (lp + 1);
9053         maxsize = be32_to_cpu(rpl->maxsize);
9054
9055         /* We support only one port */
9056         if ((rpl->index == 0) &&
9057             ((maxsize == 0) ||
9058              ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
9059                 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
9060         } else {
9061                 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
9062         }
9063         lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
9064
9065         return 0;
9066 }
9067
9068 /**
9069  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
9070  * @vport: pointer to a virtual N_Port data structure.
9071  * @cmdiocb: pointer to lpfc command iocb data structure.
9072  * @ndlp: pointer to a node-list data structure.
9073  *
9074  * This routine processes Fibre Channel Address Resolution Protocol
9075  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
9076  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
9077  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
9078  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
9079  * remote PortName is compared against the FC PortName stored in the @vport
9080  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
9081  * compared against the FC NodeName stored in the @vport data structure.
9082  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
9083  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
9084  * invoked to send out FARP Response to the remote node. Before sending the
9085  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
9086  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
9087  * routine is invoked to log into the remote port first.
9088  *
9089  * Return code
9090  *   0 - Either the FARP Match Mode not supported or successfully processed
9091  **/
9092 static int
9093 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9094                   struct lpfc_nodelist *ndlp)
9095 {
9096         struct lpfc_dmabuf *pcmd;
9097         uint32_t *lp;
9098         FARP *fp;
9099         uint32_t cnt, did;
9100
9101         did = get_job_els_rsp64_did(vport->phba, cmdiocb);
9102         pcmd = cmdiocb->cmd_dmabuf;
9103         lp = (uint32_t *) pcmd->virt;
9104
9105         lp++;
9106         fp = (FARP *) lp;
9107         /* FARP-REQ received from DID <did> */
9108         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9109                          "0601 FARP-REQ received from DID x%x\n", did);
9110         /* We will only support match on WWPN or WWNN */
9111         if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
9112                 return 0;
9113         }
9114
9115         cnt = 0;
9116         /* If this FARP command is searching for my portname */
9117         if (fp->Mflags & FARP_MATCH_PORT) {
9118                 if (memcmp(&fp->RportName, &vport->fc_portname,
9119                            sizeof(struct lpfc_name)) == 0)
9120                         cnt = 1;
9121         }
9122
9123         /* If this FARP command is searching for my nodename */
9124         if (fp->Mflags & FARP_MATCH_NODE) {
9125                 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
9126                            sizeof(struct lpfc_name)) == 0)
9127                         cnt = 1;
9128         }
9129
9130         if (cnt) {
9131                 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
9132                    (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
9133                         /* Log back into the node before sending the FARP. */
9134                         if (fp->Rflags & FARP_REQUEST_PLOGI) {
9135                                 ndlp->nlp_prev_state = ndlp->nlp_state;
9136                                 lpfc_nlp_set_state(vport, ndlp,
9137                                                    NLP_STE_PLOGI_ISSUE);
9138                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
9139                         }
9140
9141                         /* Send a FARP response to that node */
9142                         if (fp->Rflags & FARP_REQUEST_FARPR)
9143                                 lpfc_issue_els_farpr(vport, did, 0);
9144                 }
9145         }
9146         return 0;
9147 }
9148
9149 /**
9150  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
9151  * @vport: pointer to a host virtual N_Port data structure.
9152  * @cmdiocb: pointer to lpfc command iocb data structure.
9153  * @ndlp: pointer to a node-list data structure.
9154  *
9155  * This routine processes Fibre Channel Address Resolution Protocol
9156  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
9157  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
9158  * the FARP response request.
9159  *
9160  * Return code
9161  *   0 - Successfully processed FARPR IOCB (currently always return 0)
9162  **/
9163 static int
9164 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9165                    struct lpfc_nodelist  *ndlp)
9166 {
9167         uint32_t did;
9168
9169         did = get_job_els_rsp64_did(vport->phba, cmdiocb);
9170
9171         /* FARP-RSP received from DID <did> */
9172         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9173                          "0600 FARP-RSP received from DID x%x\n", did);
9174         /* ACCEPT the Farp resp request */
9175         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
9176
9177         return 0;
9178 }
9179
9180 /**
9181  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
9182  * @vport: pointer to a host virtual N_Port data structure.
9183  * @cmdiocb: pointer to lpfc command iocb data structure.
9184  * @fan_ndlp: pointer to a node-list data structure.
9185  *
9186  * This routine processes a Fabric Address Notification (FAN) IOCB
9187  * command received as an ELS unsolicited event. The FAN ELS command will
9188  * only be processed on a physical port (i.e., the @vport represents the
9189  * physical port). The fabric NodeName and PortName from the FAN IOCB are
9190  * compared against those in the phba data structure. If any of those is
9191  * different, the lpfc_initial_flogi() routine is invoked to initialize
9192  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
9193  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
9194  * is invoked to register login to the fabric.
9195  *
9196  * Return code
9197  *   0 - Successfully processed fan iocb (currently always return 0).
9198  **/
9199 static int
9200 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9201                  struct lpfc_nodelist *fan_ndlp)
9202 {
9203         struct lpfc_hba *phba = vport->phba;
9204         uint32_t *lp;
9205         FAN *fp;
9206
9207         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
9208         lp = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
9209         fp = (FAN *) ++lp;
9210         /* FAN received; Fan does not have a reply sequence */
9211         if ((vport == phba->pport) &&
9212             (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
9213                 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
9214                             sizeof(struct lpfc_name))) ||
9215                     (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
9216                             sizeof(struct lpfc_name)))) {
9217                         /* This port has switched fabrics. FLOGI is required */
9218                         lpfc_issue_init_vfi(vport);
9219                 } else {
9220                         /* FAN verified - skip FLOGI */
9221                         vport->fc_myDID = vport->fc_prevDID;
9222                         if (phba->sli_rev < LPFC_SLI_REV4)
9223                                 lpfc_issue_fabric_reglogin(vport);
9224                         else {
9225                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9226                                         "3138 Need register VFI: (x%x/%x)\n",
9227                                         vport->fc_prevDID, vport->fc_myDID);
9228                                 lpfc_issue_reg_vfi(vport);
9229                         }
9230                 }
9231         }
9232         return 0;
9233 }
9234
9235 /**
9236  * lpfc_els_rcv_edc - Process an unsolicited EDC iocb
9237  * @vport: pointer to a host virtual N_Port data structure.
9238  * @cmdiocb: pointer to lpfc command iocb data structure.
9239  * @ndlp: pointer to a node-list data structure.
9240  *
9241  * Return code
9242  *   0 - Successfully processed echo iocb (currently always return 0)
9243  **/
9244 static int
9245 lpfc_els_rcv_edc(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9246                  struct lpfc_nodelist *ndlp)
9247 {
9248         struct lpfc_hba  *phba = vport->phba;
9249         struct fc_els_edc *edc_req;
9250         struct fc_tlv_desc *tlv;
9251         uint8_t *payload;
9252         uint32_t *ptr, dtag;
9253         const char *dtag_nm;
9254         int desc_cnt = 0, bytes_remain;
9255         struct fc_diag_lnkflt_desc *plnkflt;
9256
9257         payload = cmdiocb->cmd_dmabuf->virt;
9258
9259         edc_req = (struct fc_els_edc *)payload;
9260         bytes_remain = be32_to_cpu(edc_req->desc_len);
9261
9262         ptr = (uint32_t *)payload;
9263         lpfc_printf_vlog(vport, KERN_INFO,
9264                          LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9265                          "3319 Rcv EDC payload len %d: x%x x%x x%x\n",
9266                          bytes_remain, be32_to_cpu(*ptr),
9267                          be32_to_cpu(*(ptr + 1)), be32_to_cpu(*(ptr + 2)));
9268
9269         /* No signal support unless there is a congestion descriptor */
9270         phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
9271         phba->cgn_sig_freq = 0;
9272         phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
9273
9274         if (bytes_remain <= 0)
9275                 goto out;
9276
9277         tlv = edc_req->desc;
9278
9279         /*
9280          * cycle through EDC diagnostic descriptors to find the
9281          * congestion signaling capability descriptor
9282          */
9283         while (bytes_remain) {
9284                 if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
9285                         lpfc_printf_log(phba, KERN_WARNING,
9286                                         LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9287                                         "6464 Truncated TLV hdr on "
9288                                         "Diagnostic descriptor[%d]\n",
9289                                         desc_cnt);
9290                         goto out;
9291                 }
9292
9293                 dtag = be32_to_cpu(tlv->desc_tag);
9294                 switch (dtag) {
9295                 case ELS_DTAG_LNK_FAULT_CAP:
9296                         if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
9297                             FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
9298                                 sizeof(struct fc_diag_lnkflt_desc)) {
9299                                 lpfc_printf_log(phba, KERN_WARNING,
9300                                         LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9301                                         "6465 Truncated Link Fault Diagnostic "
9302                                         "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
9303                                         desc_cnt, bytes_remain,
9304                                         FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
9305                                         sizeof(struct fc_diag_lnkflt_desc));
9306                                 goto out;
9307                         }
9308                         plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
9309                         lpfc_printf_log(phba, KERN_INFO,
9310                                 LOG_ELS | LOG_LDS_EVENT,
9311                                 "4626 Link Fault Desc Data: x%08x len x%x "
9312                                 "da x%x dd x%x interval x%x\n",
9313                                 be32_to_cpu(plnkflt->desc_tag),
9314                                 be32_to_cpu(plnkflt->desc_len),
9315                                 be32_to_cpu(
9316                                         plnkflt->degrade_activate_threshold),
9317                                 be32_to_cpu(
9318                                         plnkflt->degrade_deactivate_threshold),
9319                                 be32_to_cpu(plnkflt->fec_degrade_interval));
9320                         break;
9321                 case ELS_DTAG_CG_SIGNAL_CAP:
9322                         if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
9323                             FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
9324                                 sizeof(struct fc_diag_cg_sig_desc)) {
9325                                 lpfc_printf_log(
9326                                         phba, KERN_WARNING, LOG_CGN_MGMT,
9327                                         "6466 Truncated cgn signal Diagnostic "
9328                                         "descriptor[%d]: %d vs 0x%zx 0x%zx\n",
9329                                         desc_cnt, bytes_remain,
9330                                         FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
9331                                         sizeof(struct fc_diag_cg_sig_desc));
9332                                 goto out;
9333                         }
9334
9335                         phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
9336                         phba->cgn_reg_signal = phba->cgn_init_reg_signal;
9337
9338                         /* We start negotiation with lpfc_fabric_cgn_frequency.
9339                          * When we process the EDC, we will settle on the
9340                          * higher frequency.
9341                          */
9342                         phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
9343
9344                         lpfc_least_capable_settings(
9345                                 phba, (struct fc_diag_cg_sig_desc *)tlv);
9346                         break;
9347                 default:
9348                         dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9349                         lpfc_printf_log(phba, KERN_WARNING,
9350                                         LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9351                                         "6467 unknown Diagnostic "
9352                                         "Descriptor[%d]: tag x%x (%s)\n",
9353                                         desc_cnt, dtag, dtag_nm);
9354                 }
9355                 bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
9356                 tlv = fc_tlv_next_desc(tlv);
9357                 desc_cnt++;
9358         }
9359 out:
9360         /* Need to send back an ACC */
9361         lpfc_issue_els_edc_rsp(vport, cmdiocb, ndlp);
9362
9363         lpfc_config_cgn_signal(phba);
9364         return 0;
9365 }
9366
9367 /**
9368  * lpfc_els_timeout - Handler funciton to the els timer
9369  * @t: timer context used to obtain the vport.
9370  *
9371  * This routine is invoked by the ELS timer after timeout. It posts the ELS
9372  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
9373  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
9374  * up the worker thread. It is for the worker thread to invoke the routine
9375  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
9376  **/
9377 void
9378 lpfc_els_timeout(struct timer_list *t)
9379 {
9380         struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
9381         struct lpfc_hba   *phba = vport->phba;
9382         uint32_t tmo_posted;
9383         unsigned long iflag;
9384
9385         spin_lock_irqsave(&vport->work_port_lock, iflag);
9386         tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
9387         if (!tmo_posted && !test_bit(FC_UNLOADING, &vport->load_flag))
9388                 vport->work_port_events |= WORKER_ELS_TMO;
9389         spin_unlock_irqrestore(&vport->work_port_lock, iflag);
9390
9391         if (!tmo_posted && !test_bit(FC_UNLOADING, &vport->load_flag))
9392                 lpfc_worker_wake_up(phba);
9393         return;
9394 }
9395
9396
9397 /**
9398  * lpfc_els_timeout_handler - Process an els timeout event
9399  * @vport: pointer to a virtual N_Port data structure.
9400  *
9401  * This routine is the actual handler function that processes an ELS timeout
9402  * event. It walks the ELS ring to get and abort all the IOCBs (except the
9403  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
9404  * invoking the lpfc_sli_issue_abort_iotag() routine.
9405  **/
9406 void
9407 lpfc_els_timeout_handler(struct lpfc_vport *vport)
9408 {
9409         struct lpfc_hba  *phba = vport->phba;
9410         struct lpfc_sli_ring *pring;
9411         struct lpfc_iocbq *tmp_iocb, *piocb;
9412         IOCB_t *cmd = NULL;
9413         struct lpfc_dmabuf *pcmd;
9414         uint32_t els_command = 0;
9415         uint32_t timeout;
9416         uint32_t remote_ID = 0xffffffff;
9417         LIST_HEAD(abort_list);
9418         u32 ulp_command = 0, ulp_context = 0, did = 0, iotag = 0;
9419
9420
9421         timeout = (uint32_t)(phba->fc_ratov << 1);
9422
9423         pring = lpfc_phba_elsring(phba);
9424         if (unlikely(!pring))
9425                 return;
9426
9427         if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
9428                 return;
9429
9430         spin_lock_irq(&phba->hbalock);
9431         if (phba->sli_rev == LPFC_SLI_REV4)
9432                 spin_lock(&pring->ring_lock);
9433
9434         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9435                 ulp_command = get_job_cmnd(phba, piocb);
9436                 ulp_context = get_job_ulpcontext(phba, piocb);
9437                 did = get_job_els_rsp64_did(phba, piocb);
9438
9439                 if (phba->sli_rev == LPFC_SLI_REV4) {
9440                         iotag = get_wqe_reqtag(piocb);
9441                 } else {
9442                         cmd = &piocb->iocb;
9443                         iotag = cmd->ulpIoTag;
9444                 }
9445
9446                 if ((piocb->cmd_flag & LPFC_IO_LIBDFC) != 0 ||
9447                     ulp_command == CMD_ABORT_XRI_CX ||
9448                     ulp_command == CMD_ABORT_XRI_CN ||
9449                     ulp_command == CMD_CLOSE_XRI_CN)
9450                         continue;
9451
9452                 if (piocb->vport != vport)
9453                         continue;
9454
9455                 pcmd = piocb->cmd_dmabuf;
9456                 if (pcmd)
9457                         els_command = *(uint32_t *) (pcmd->virt);
9458
9459                 if (els_command == ELS_CMD_FARP ||
9460                     els_command == ELS_CMD_FARPR ||
9461                     els_command == ELS_CMD_FDISC)
9462                         continue;
9463
9464                 if (piocb->drvrTimeout > 0) {
9465                         if (piocb->drvrTimeout >= timeout)
9466                                 piocb->drvrTimeout -= timeout;
9467                         else
9468                                 piocb->drvrTimeout = 0;
9469                         continue;
9470                 }
9471
9472                 remote_ID = 0xffffffff;
9473                 if (ulp_command != CMD_GEN_REQUEST64_CR) {
9474                         remote_ID = did;
9475                 } else {
9476                         struct lpfc_nodelist *ndlp;
9477                         ndlp = __lpfc_findnode_rpi(vport, ulp_context);
9478                         if (ndlp)
9479                                 remote_ID = ndlp->nlp_DID;
9480                 }
9481                 list_add_tail(&piocb->dlist, &abort_list);
9482         }
9483         if (phba->sli_rev == LPFC_SLI_REV4)
9484                 spin_unlock(&pring->ring_lock);
9485         spin_unlock_irq(&phba->hbalock);
9486
9487         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9488                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9489                          "0127 ELS timeout Data: x%x x%x x%x "
9490                          "x%x\n", els_command,
9491                          remote_ID, ulp_command, iotag);
9492
9493                 spin_lock_irq(&phba->hbalock);
9494                 list_del_init(&piocb->dlist);
9495                 lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9496                 spin_unlock_irq(&phba->hbalock);
9497         }
9498
9499         /* Make sure HBA is alive */
9500         lpfc_issue_hb_tmo(phba);
9501
9502         if (!list_empty(&pring->txcmplq))
9503                 if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
9504                         mod_timer(&vport->els_tmofunc,
9505                                   jiffies + msecs_to_jiffies(1000 * timeout));
9506 }
9507
9508 /**
9509  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
9510  * @vport: pointer to a host virtual N_Port data structure.
9511  *
9512  * This routine is used to clean up all the outstanding ELS commands on a
9513  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
9514  * routine. After that, it walks the ELS transmit queue to remove all the
9515  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
9516  * the IOCBs with a non-NULL completion callback function, the callback
9517  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9518  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
9519  * callback function, the IOCB will simply be released. Finally, it walks
9520  * the ELS transmit completion queue to issue an abort IOCB to any transmit
9521  * completion queue IOCB that is associated with the @vport and is not
9522  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
9523  * part of the discovery state machine) out to HBA by invoking the
9524  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
9525  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
9526  * the IOCBs are aborted when this function returns.
9527  **/
9528 void
9529 lpfc_els_flush_cmd(struct lpfc_vport *vport)
9530 {
9531         LIST_HEAD(abort_list);
9532         LIST_HEAD(cancel_list);
9533         struct lpfc_hba  *phba = vport->phba;
9534         struct lpfc_sli_ring *pring;
9535         struct lpfc_iocbq *tmp_iocb, *piocb;
9536         u32 ulp_command;
9537         unsigned long iflags = 0;
9538         bool mbx_tmo_err;
9539
9540         lpfc_fabric_abort_vport(vport);
9541
9542         /*
9543          * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
9544          * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
9545          * ultimately grabs the ring_lock, the driver must splice the list into
9546          * a working list and release the locks before calling the abort.
9547          */
9548         spin_lock_irqsave(&phba->hbalock, iflags);
9549         pring = lpfc_phba_elsring(phba);
9550
9551         /* Bail out if we've no ELS wq, like in PCI error recovery case. */
9552         if (unlikely(!pring)) {
9553                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9554                 return;
9555         }
9556
9557         if (phba->sli_rev == LPFC_SLI_REV4)
9558                 spin_lock(&pring->ring_lock);
9559
9560         mbx_tmo_err = test_bit(MBX_TMO_ERR, &phba->bit_flags);
9561         /* First we need to issue aborts to outstanding cmds on txcmpl */
9562         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9563                 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9564                                  "2243 iotag = 0x%x cmd_flag = 0x%x "
9565                                  "ulp_command = 0x%x this_vport %x "
9566                                  "sli_flag = 0x%x\n",
9567                                  piocb->iotag, piocb->cmd_flag,
9568                                  get_job_cmnd(phba, piocb),
9569                                  (piocb->vport == vport),
9570                                  phba->sli.sli_flag);
9571
9572                 if (piocb->vport != vport)
9573                         continue;
9574
9575                 if ((phba->sli.sli_flag & LPFC_SLI_ACTIVE) && !mbx_tmo_err) {
9576                         if (piocb->cmd_flag & LPFC_IO_LIBDFC)
9577                                 continue;
9578                         if (piocb->cmd_flag & LPFC_DRIVER_ABORTED)
9579                                 continue;
9580                 }
9581
9582                 /* On the ELS ring we can have ELS_REQUESTs, ELS_RSPs,
9583                  * or GEN_REQUESTs waiting for a CQE response.
9584                  */
9585                 ulp_command = get_job_cmnd(phba, piocb);
9586                 if (ulp_command == CMD_ELS_REQUEST64_WQE ||
9587                     ulp_command == CMD_XMIT_ELS_RSP64_WQE) {
9588                         list_add_tail(&piocb->dlist, &abort_list);
9589
9590                         /* If the link is down when flushing ELS commands
9591                          * the firmware will not complete them till after
9592                          * the link comes back up. This may confuse
9593                          * discovery for the new link up, so we need to
9594                          * change the compl routine to just clean up the iocb
9595                          * and avoid any retry logic.
9596                          */
9597                         if (phba->link_state == LPFC_LINK_DOWN)
9598                                 piocb->cmd_cmpl = lpfc_cmpl_els_link_down;
9599                 } else if (ulp_command == CMD_GEN_REQUEST64_CR ||
9600                            mbx_tmo_err)
9601                         list_add_tail(&piocb->dlist, &abort_list);
9602         }
9603
9604         if (phba->sli_rev == LPFC_SLI_REV4)
9605                 spin_unlock(&pring->ring_lock);
9606         spin_unlock_irqrestore(&phba->hbalock, iflags);
9607
9608         /* Abort each txcmpl iocb on aborted list and remove the dlist links. */
9609         list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9610                 spin_lock_irqsave(&phba->hbalock, iflags);
9611                 list_del_init(&piocb->dlist);
9612                 if (mbx_tmo_err || !(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
9613                         list_move_tail(&piocb->list, &cancel_list);
9614                 else
9615                         lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9616
9617                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9618         }
9619         if (!list_empty(&cancel_list))
9620                 lpfc_sli_cancel_iocbs(phba, &cancel_list, IOSTAT_LOCAL_REJECT,
9621                                       IOERR_SLI_ABORTED);
9622         else
9623                 /* Make sure HBA is alive */
9624                 lpfc_issue_hb_tmo(phba);
9625
9626         if (!list_empty(&abort_list))
9627                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9628                                  "3387 abort list for txq not empty\n");
9629         INIT_LIST_HEAD(&abort_list);
9630
9631         spin_lock_irqsave(&phba->hbalock, iflags);
9632         if (phba->sli_rev == LPFC_SLI_REV4)
9633                 spin_lock(&pring->ring_lock);
9634
9635         /* No need to abort the txq list,
9636          * just queue them up for lpfc_sli_cancel_iocbs
9637          */
9638         list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
9639                 ulp_command = get_job_cmnd(phba, piocb);
9640
9641                 if (piocb->cmd_flag & LPFC_IO_LIBDFC)
9642                         continue;
9643
9644                 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
9645                 if (ulp_command == CMD_QUE_RING_BUF_CN ||
9646                     ulp_command == CMD_QUE_RING_BUF64_CN ||
9647                     ulp_command == CMD_CLOSE_XRI_CN ||
9648                     ulp_command == CMD_ABORT_XRI_CN ||
9649                     ulp_command == CMD_ABORT_XRI_CX)
9650                         continue;
9651
9652                 if (piocb->vport != vport)
9653                         continue;
9654
9655                 list_del_init(&piocb->list);
9656                 list_add_tail(&piocb->list, &abort_list);
9657         }
9658
9659         /* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
9660         if (vport == phba->pport) {
9661                 list_for_each_entry_safe(piocb, tmp_iocb,
9662                                          &phba->fabric_iocb_list, list) {
9663                         list_del_init(&piocb->list);
9664                         list_add_tail(&piocb->list, &abort_list);
9665                 }
9666         }
9667
9668         if (phba->sli_rev == LPFC_SLI_REV4)
9669                 spin_unlock(&pring->ring_lock);
9670         spin_unlock_irqrestore(&phba->hbalock, iflags);
9671
9672         /* Cancel all the IOCBs from the completions list */
9673         lpfc_sli_cancel_iocbs(phba, &abort_list,
9674                               IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
9675
9676         return;
9677 }
9678
9679 /**
9680  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
9681  * @phba: pointer to lpfc hba data structure.
9682  *
9683  * This routine is used to clean up all the outstanding ELS commands on a
9684  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
9685  * routine. After that, it walks the ELS transmit queue to remove all the
9686  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
9687  * the IOCBs with the completion callback function associated, the callback
9688  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9689  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
9690  * callback function associated, the IOCB will simply be released. Finally,
9691  * it walks the ELS transmit completion queue to issue an abort IOCB to any
9692  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
9693  * management plane IOCBs that are not part of the discovery state machine)
9694  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
9695  **/
9696 void
9697 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
9698 {
9699         struct lpfc_vport *vport;
9700
9701         spin_lock_irq(&phba->port_list_lock);
9702         list_for_each_entry(vport, &phba->port_list, listentry)
9703                 lpfc_els_flush_cmd(vport);
9704         spin_unlock_irq(&phba->port_list_lock);
9705
9706         return;
9707 }
9708
9709 /**
9710  * lpfc_send_els_failure_event - Posts an ELS command failure event
9711  * @phba: Pointer to hba context object.
9712  * @cmdiocbp: Pointer to command iocb which reported error.
9713  * @rspiocbp: Pointer to response iocb which reported error.
9714  *
9715  * This function sends an event when there is an ELS command
9716  * failure.
9717  **/
9718 void
9719 lpfc_send_els_failure_event(struct lpfc_hba *phba,
9720                         struct lpfc_iocbq *cmdiocbp,
9721                         struct lpfc_iocbq *rspiocbp)
9722 {
9723         struct lpfc_vport *vport = cmdiocbp->vport;
9724         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9725         struct lpfc_lsrjt_event lsrjt_event;
9726         struct lpfc_fabric_event_header fabric_event;
9727         struct ls_rjt stat;
9728         struct lpfc_nodelist *ndlp;
9729         uint32_t *pcmd;
9730         u32 ulp_status, ulp_word4;
9731
9732         ndlp = cmdiocbp->ndlp;
9733         if (!ndlp)
9734                 return;
9735
9736         ulp_status = get_job_ulpstatus(phba, rspiocbp);
9737         ulp_word4 = get_job_word4(phba, rspiocbp);
9738
9739         if (ulp_status == IOSTAT_LS_RJT) {
9740                 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
9741                 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
9742                 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
9743                         sizeof(struct lpfc_name));
9744                 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
9745                         sizeof(struct lpfc_name));
9746                 pcmd = (uint32_t *)cmdiocbp->cmd_dmabuf->virt;
9747                 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
9748                 stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
9749                 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
9750                 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
9751                 fc_host_post_vendor_event(shost,
9752                         fc_get_event_number(),
9753                         sizeof(lsrjt_event),
9754                         (char *)&lsrjt_event,
9755                         LPFC_NL_VENDOR_ID);
9756                 return;
9757         }
9758         if (ulp_status == IOSTAT_NPORT_BSY ||
9759             ulp_status == IOSTAT_FABRIC_BSY) {
9760                 fabric_event.event_type = FC_REG_FABRIC_EVENT;
9761                 if (ulp_status == IOSTAT_NPORT_BSY)
9762                         fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
9763                 else
9764                         fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
9765                 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
9766                         sizeof(struct lpfc_name));
9767                 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
9768                         sizeof(struct lpfc_name));
9769                 fc_host_post_vendor_event(shost,
9770                         fc_get_event_number(),
9771                         sizeof(fabric_event),
9772                         (char *)&fabric_event,
9773                         LPFC_NL_VENDOR_ID);
9774                 return;
9775         }
9776
9777 }
9778
9779 /**
9780  * lpfc_send_els_event - Posts unsolicited els event
9781  * @vport: Pointer to vport object.
9782  * @ndlp: Pointer FC node object.
9783  * @payload: ELS command code type.
9784  *
9785  * This function posts an event when there is an incoming
9786  * unsolicited ELS command.
9787  **/
9788 static void
9789 lpfc_send_els_event(struct lpfc_vport *vport,
9790                     struct lpfc_nodelist *ndlp,
9791                     uint32_t *payload)
9792 {
9793         struct lpfc_els_event_header *els_data = NULL;
9794         struct lpfc_logo_event *logo_data = NULL;
9795         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9796
9797         if (*payload == ELS_CMD_LOGO) {
9798                 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
9799                 if (!logo_data) {
9800                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9801                                 "0148 Failed to allocate memory "
9802                                 "for LOGO event\n");
9803                         return;
9804                 }
9805                 els_data = &logo_data->header;
9806         } else {
9807                 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
9808                         GFP_KERNEL);
9809                 if (!els_data) {
9810                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9811                                 "0149 Failed to allocate memory "
9812                                 "for ELS event\n");
9813                         return;
9814                 }
9815         }
9816         els_data->event_type = FC_REG_ELS_EVENT;
9817         switch (*payload) {
9818         case ELS_CMD_PLOGI:
9819                 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
9820                 break;
9821         case ELS_CMD_PRLO:
9822                 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
9823                 break;
9824         case ELS_CMD_ADISC:
9825                 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
9826                 break;
9827         case ELS_CMD_LOGO:
9828                 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
9829                 /* Copy the WWPN in the LOGO payload */
9830                 memcpy(logo_data->logo_wwpn, &payload[2],
9831                         sizeof(struct lpfc_name));
9832                 break;
9833         default:
9834                 kfree(els_data);
9835                 return;
9836         }
9837         memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
9838         memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
9839         if (*payload == ELS_CMD_LOGO) {
9840                 fc_host_post_vendor_event(shost,
9841                         fc_get_event_number(),
9842                         sizeof(struct lpfc_logo_event),
9843                         (char *)logo_data,
9844                         LPFC_NL_VENDOR_ID);
9845                 kfree(logo_data);
9846         } else {
9847                 fc_host_post_vendor_event(shost,
9848                         fc_get_event_number(),
9849                         sizeof(struct lpfc_els_event_header),
9850                         (char *)els_data,
9851                         LPFC_NL_VENDOR_ID);
9852                 kfree(els_data);
9853         }
9854
9855         return;
9856 }
9857
9858
9859 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
9860                         FC_FPIN_LI_EVT_TYPES_INIT);
9861
9862 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_deli_event_nm, fc_fpin_deli_event_types,
9863                         FC_FPIN_DELI_EVT_TYPES_INIT);
9864
9865 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_event_nm, fc_fpin_congn_event_types,
9866                         FC_FPIN_CONGN_EVT_TYPES_INIT);
9867
9868 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_severity_nm,
9869                         fc_fpin_congn_severity_types,
9870                         FC_FPIN_CONGN_SEVERITY_INIT);
9871
9872
9873 /**
9874  * lpfc_display_fpin_wwpn - Display WWPNs accessible by the attached port
9875  * @phba: Pointer to phba object.
9876  * @wwnlist: Pointer to list of WWPNs in FPIN payload
9877  * @cnt: count of WWPNs in FPIN payload
9878  *
9879  * This routine is called by LI and PC descriptors.
9880  * Limit the number of WWPNs displayed to 6 log messages, 6 per log message
9881  */
9882 static void
9883 lpfc_display_fpin_wwpn(struct lpfc_hba *phba, __be64 *wwnlist, u32 cnt)
9884 {
9885         char buf[LPFC_FPIN_WWPN_LINE_SZ];
9886         __be64 wwn;
9887         u64 wwpn;
9888         int i, len;
9889         int line = 0;
9890         int wcnt = 0;
9891         bool endit = false;
9892
9893         len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ, "Accessible WWPNs:");
9894         for (i = 0; i < cnt; i++) {
9895                 /* Are we on the last WWPN */
9896                 if (i == (cnt - 1))
9897                         endit = true;
9898
9899                 /* Extract the next WWPN from the payload */
9900                 wwn = *wwnlist++;
9901                 wwpn = be64_to_cpu(wwn);
9902                 len += scnprintf(buf + len, LPFC_FPIN_WWPN_LINE_SZ - len,
9903                                  " %016llx", wwpn);
9904
9905                 /* Log a message if we are on the last WWPN
9906                  * or if we hit the max allowed per message.
9907                  */
9908                 wcnt++;
9909                 if (wcnt == LPFC_FPIN_WWPN_LINE_CNT || endit) {
9910                         buf[len] = 0;
9911                         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9912                                         "4686 %s\n", buf);
9913
9914                         /* Check if we reached the last WWPN */
9915                         if (endit)
9916                                 return;
9917
9918                         /* Limit the number of log message displayed per FPIN */
9919                         line++;
9920                         if (line == LPFC_FPIN_WWPN_NUM_LINE) {
9921                                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9922                                                 "4687 %d WWPNs Truncated\n",
9923                                                 cnt - i - 1);
9924                                 return;
9925                         }
9926
9927                         /* Start over with next log message */
9928                         wcnt = 0;
9929                         len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ,
9930                                         "Additional WWPNs:");
9931                 }
9932         }
9933 }
9934
9935 /**
9936  * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
9937  * @phba: Pointer to phba object.
9938  * @tlv:  Pointer to the Link Integrity Notification Descriptor.
9939  *
9940  * This function processes a Link Integrity FPIN event by logging a message.
9941  **/
9942 static void
9943 lpfc_els_rcv_fpin_li(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9944 {
9945         struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
9946         const char *li_evt_str;
9947         u32 li_evt, cnt;
9948
9949         li_evt = be16_to_cpu(li->event_type);
9950         li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
9951         cnt = be32_to_cpu(li->pname_count);
9952
9953         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9954                         "4680 FPIN Link Integrity %s (x%x) "
9955                         "Detecting PN x%016llx Attached PN x%016llx "
9956                         "Duration %d mSecs Count %d Port Cnt %d\n",
9957                         li_evt_str, li_evt,
9958                         be64_to_cpu(li->detecting_wwpn),
9959                         be64_to_cpu(li->attached_wwpn),
9960                         be32_to_cpu(li->event_threshold),
9961                         be32_to_cpu(li->event_count), cnt);
9962
9963         lpfc_display_fpin_wwpn(phba, (__be64 *)&li->pname_list, cnt);
9964 }
9965
9966 /**
9967  * lpfc_els_rcv_fpin_del - Process an FPIN Delivery Event.
9968  * @phba: Pointer to hba object.
9969  * @tlv:  Pointer to the Delivery Notification Descriptor TLV
9970  *
9971  * This function processes a Delivery FPIN event by logging a message.
9972  **/
9973 static void
9974 lpfc_els_rcv_fpin_del(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9975 {
9976         struct fc_fn_deli_desc *del = (struct fc_fn_deli_desc *)tlv;
9977         const char *del_rsn_str;
9978         u32 del_rsn;
9979         __be32 *frame;
9980
9981         del_rsn = be16_to_cpu(del->deli_reason_code);
9982         del_rsn_str = lpfc_get_fpin_deli_event_nm(del_rsn);
9983
9984         /* Skip over desc_tag/desc_len header to payload */
9985         frame = (__be32 *)(del + 1);
9986
9987         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9988                         "4681 FPIN Delivery %s (x%x) "
9989                         "Detecting PN x%016llx Attached PN x%016llx "
9990                         "DiscHdr0  x%08x "
9991                         "DiscHdr1 x%08x DiscHdr2 x%08x DiscHdr3 x%08x "
9992                         "DiscHdr4 x%08x DiscHdr5 x%08x\n",
9993                         del_rsn_str, del_rsn,
9994                         be64_to_cpu(del->detecting_wwpn),
9995                         be64_to_cpu(del->attached_wwpn),
9996                         be32_to_cpu(frame[0]),
9997                         be32_to_cpu(frame[1]),
9998                         be32_to_cpu(frame[2]),
9999                         be32_to_cpu(frame[3]),
10000                         be32_to_cpu(frame[4]),
10001                         be32_to_cpu(frame[5]));
10002 }
10003
10004 /**
10005  * lpfc_els_rcv_fpin_peer_cgn - Process a FPIN Peer Congestion Event.
10006  * @phba: Pointer to hba object.
10007  * @tlv:  Pointer to the Peer Congestion Notification Descriptor TLV
10008  *
10009  * This function processes a Peer Congestion FPIN event by logging a message.
10010  **/
10011 static void
10012 lpfc_els_rcv_fpin_peer_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10013 {
10014         struct fc_fn_peer_congn_desc *pc = (struct fc_fn_peer_congn_desc *)tlv;
10015         const char *pc_evt_str;
10016         u32 pc_evt, cnt;
10017
10018         pc_evt = be16_to_cpu(pc->event_type);
10019         pc_evt_str = lpfc_get_fpin_congn_event_nm(pc_evt);
10020         cnt = be32_to_cpu(pc->pname_count);
10021
10022         /* Capture FPIN frequency */
10023         phba->cgn_fpin_frequency = be32_to_cpu(pc->event_period);
10024
10025         lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_ELS,
10026                         "4684 FPIN Peer Congestion %s (x%x) "
10027                         "Duration %d mSecs "
10028                         "Detecting PN x%016llx Attached PN x%016llx "
10029                         "Impacted Port Cnt %d\n",
10030                         pc_evt_str, pc_evt,
10031                         be32_to_cpu(pc->event_period),
10032                         be64_to_cpu(pc->detecting_wwpn),
10033                         be64_to_cpu(pc->attached_wwpn),
10034                         cnt);
10035
10036         lpfc_display_fpin_wwpn(phba, (__be64 *)&pc->pname_list, cnt);
10037 }
10038
10039 /**
10040  * lpfc_els_rcv_fpin_cgn - Process an FPIN Congestion notification
10041  * @phba: Pointer to hba object.
10042  * @tlv:  Pointer to the Congestion Notification Descriptor TLV
10043  *
10044  * This function processes an FPIN Congestion Notifiction.  The notification
10045  * could be an Alarm or Warning.  This routine feeds that data into driver's
10046  * running congestion algorithm. It also processes the FPIN by
10047  * logging a message. It returns 1 to indicate deliver this message
10048  * to the upper layer or 0 to indicate don't deliver it.
10049  **/
10050 static int
10051 lpfc_els_rcv_fpin_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10052 {
10053         struct lpfc_cgn_info *cp;
10054         struct fc_fn_congn_desc *cgn = (struct fc_fn_congn_desc *)tlv;
10055         const char *cgn_evt_str;
10056         u32 cgn_evt;
10057         const char *cgn_sev_str;
10058         u32 cgn_sev;
10059         uint16_t value;
10060         u32 crc;
10061         bool nm_log = false;
10062         int rc = 1;
10063
10064         cgn_evt = be16_to_cpu(cgn->event_type);
10065         cgn_evt_str = lpfc_get_fpin_congn_event_nm(cgn_evt);
10066         cgn_sev = cgn->severity;
10067         cgn_sev_str = lpfc_get_fpin_congn_severity_nm(cgn_sev);
10068
10069         /* The driver only takes action on a Credit Stall or Oversubscription
10070          * event type to engage the IO algorithm.  The driver prints an
10071          * unmaskable message only for Lost Credit and Credit Stall.
10072          * TODO: Still need to have definition of host action on clear,
10073          *       lost credit and device specific event types.
10074          */
10075         switch (cgn_evt) {
10076         case FPIN_CONGN_LOST_CREDIT:
10077                 nm_log = true;
10078                 break;
10079         case FPIN_CONGN_CREDIT_STALL:
10080                 nm_log = true;
10081                 fallthrough;
10082         case FPIN_CONGN_OVERSUBSCRIPTION:
10083                 if (cgn_evt == FPIN_CONGN_OVERSUBSCRIPTION)
10084                         nm_log = false;
10085                 switch (cgn_sev) {
10086                 case FPIN_CONGN_SEVERITY_ERROR:
10087                         /* Take action here for an Alarm event */
10088                         if (phba->cmf_active_mode != LPFC_CFG_OFF) {
10089                                 if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) {
10090                                         /* Track of alarm cnt for SYNC_WQE */
10091                                         atomic_inc(&phba->cgn_sync_alarm_cnt);
10092                                 }
10093                                 /* Track alarm cnt for cgn_info regardless
10094                                  * of whether CMF is configured for Signals
10095                                  * or FPINs.
10096                                  */
10097                                 atomic_inc(&phba->cgn_fabric_alarm_cnt);
10098                                 goto cleanup;
10099                         }
10100                         break;
10101                 case FPIN_CONGN_SEVERITY_WARNING:
10102                         /* Take action here for a Warning event */
10103                         if (phba->cmf_active_mode != LPFC_CFG_OFF) {
10104                                 if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) {
10105                                         /* Track of warning cnt for SYNC_WQE */
10106                                         atomic_inc(&phba->cgn_sync_warn_cnt);
10107                                 }
10108                                 /* Track warning cnt and freq for cgn_info
10109                                  * regardless of whether CMF is configured for
10110                                  * Signals or FPINs.
10111                                  */
10112                                 atomic_inc(&phba->cgn_fabric_warn_cnt);
10113 cleanup:
10114                                 /* Save frequency in ms */
10115                                 phba->cgn_fpin_frequency =
10116                                         be32_to_cpu(cgn->event_period);
10117                                 value = phba->cgn_fpin_frequency;
10118                                 if (phba->cgn_i) {
10119                                         cp = (struct lpfc_cgn_info *)
10120                                                 phba->cgn_i->virt;
10121                                         cp->cgn_alarm_freq =
10122                                                 cpu_to_le16(value);
10123                                         cp->cgn_warn_freq =
10124                                                 cpu_to_le16(value);
10125                                         crc = lpfc_cgn_calc_crc32
10126                                                 (cp,
10127                                                 LPFC_CGN_INFO_SZ,
10128                                                 LPFC_CGN_CRC32_SEED);
10129                                         cp->cgn_info_crc = cpu_to_le32(crc);
10130                                 }
10131
10132                                 /* Don't deliver to upper layer since
10133                                  * driver took action on this tlv.
10134                                  */
10135                                 rc = 0;
10136                         }
10137                         break;
10138                 }
10139                 break;
10140         }
10141
10142         /* Change the log level to unmaskable for the following event types. */
10143         lpfc_printf_log(phba, (nm_log ? KERN_WARNING : KERN_INFO),
10144                         LOG_CGN_MGMT | LOG_ELS,
10145                         "4683 FPIN CONGESTION %s type %s (x%x) Event "
10146                         "Duration %d mSecs\n",
10147                         cgn_sev_str, cgn_evt_str, cgn_evt,
10148                         be32_to_cpu(cgn->event_period));
10149         return rc;
10150 }
10151
10152 void
10153 lpfc_els_rcv_fpin(struct lpfc_vport *vport, void *p, u32 fpin_length)
10154 {
10155         struct lpfc_hba *phba = vport->phba;
10156         struct fc_els_fpin *fpin = (struct fc_els_fpin *)p;
10157         struct fc_tlv_desc *tlv, *first_tlv, *current_tlv;
10158         const char *dtag_nm;
10159         int desc_cnt = 0, bytes_remain, cnt;
10160         u32 dtag, deliver = 0;
10161         int len;
10162
10163         /* FPINs handled only if we are in the right discovery state */
10164         if (vport->port_state < LPFC_DISC_AUTH)
10165                 return;
10166
10167         /* make sure there is the full fpin header */
10168         if (fpin_length < sizeof(struct fc_els_fpin))
10169                 return;
10170
10171         /* Sanity check descriptor length. The desc_len value does not
10172          * include space for the ELS command and the desc_len fields.
10173          */
10174         len = be32_to_cpu(fpin->desc_len);
10175         if (fpin_length < len + sizeof(struct fc_els_fpin)) {
10176                 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10177                                 "4671 Bad ELS FPIN length %d: %d\n",
10178                                 len, fpin_length);
10179                 return;
10180         }
10181
10182         tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
10183         first_tlv = tlv;
10184         bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
10185         bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
10186
10187         /* process each descriptor separately */
10188         while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
10189                bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
10190                 dtag = be32_to_cpu(tlv->desc_tag);
10191                 switch (dtag) {
10192                 case ELS_DTAG_LNK_INTEGRITY:
10193                         lpfc_els_rcv_fpin_li(phba, tlv);
10194                         deliver = 1;
10195                         break;
10196                 case ELS_DTAG_DELIVERY:
10197                         lpfc_els_rcv_fpin_del(phba, tlv);
10198                         deliver = 1;
10199                         break;
10200                 case ELS_DTAG_PEER_CONGEST:
10201                         lpfc_els_rcv_fpin_peer_cgn(phba, tlv);
10202                         deliver = 1;
10203                         break;
10204                 case ELS_DTAG_CONGESTION:
10205                         deliver = lpfc_els_rcv_fpin_cgn(phba, tlv);
10206                         break;
10207                 default:
10208                         dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10209                         lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10210                                         "4678 unknown FPIN descriptor[%d]: "
10211                                         "tag x%x (%s)\n",
10212                                         desc_cnt, dtag, dtag_nm);
10213
10214                         /* If descriptor is bad, drop the rest of the data */
10215                         return;
10216                 }
10217                 lpfc_cgn_update_stat(phba, dtag);
10218                 cnt = be32_to_cpu(tlv->desc_len);
10219
10220                 /* Sanity check descriptor length. The desc_len value does not
10221                  * include space for the desc_tag and the desc_len fields.
10222                  */
10223                 len -= (cnt + sizeof(struct fc_tlv_desc));
10224                 if (len < 0) {
10225                         dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10226                         lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10227                                         "4672 Bad FPIN descriptor TLV length "
10228                                         "%d: %d %d %s\n",
10229                                         cnt, len, fpin_length, dtag_nm);
10230                         return;
10231                 }
10232
10233                 current_tlv = tlv;
10234                 bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
10235                 tlv = fc_tlv_next_desc(tlv);
10236
10237                 /* Format payload such that the FPIN delivered to the
10238                  * upper layer is a single descriptor FPIN.
10239                  */
10240                 if (desc_cnt)
10241                         memcpy(first_tlv, current_tlv,
10242                                (cnt + sizeof(struct fc_els_fpin)));
10243
10244                 /* Adjust the length so that it only reflects a
10245                  * single descriptor FPIN.
10246                  */
10247                 fpin_length = cnt + sizeof(struct fc_els_fpin);
10248                 fpin->desc_len = cpu_to_be32(fpin_length);
10249                 fpin_length += sizeof(struct fc_els_fpin); /* the entire FPIN */
10250
10251                 /* Send every descriptor individually to the upper layer */
10252                 if (deliver)
10253                         fc_host_fpin_rcv(lpfc_shost_from_vport(vport),
10254                                          fpin_length, (char *)fpin, 0);
10255                 desc_cnt++;
10256         }
10257 }
10258
10259 /**
10260  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
10261  * @phba: pointer to lpfc hba data structure.
10262  * @pring: pointer to a SLI ring.
10263  * @vport: pointer to a host virtual N_Port data structure.
10264  * @elsiocb: pointer to lpfc els command iocb data structure.
10265  *
10266  * This routine is used for processing the IOCB associated with a unsolicited
10267  * event. It first determines whether there is an existing ndlp that matches
10268  * the DID from the unsolicited IOCB. If not, it will create a new one with
10269  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
10270  * IOCB is then used to invoke the proper routine and to set up proper state
10271  * of the discovery state machine.
10272  **/
10273 static void
10274 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10275                       struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
10276 {
10277         struct lpfc_nodelist *ndlp;
10278         struct ls_rjt stat;
10279         u32 *payload, payload_len;
10280         u32 cmd = 0, did = 0, newnode, status = 0;
10281         uint8_t rjt_exp, rjt_err = 0, init_link = 0;
10282         struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10283         LPFC_MBOXQ_t *mbox;
10284
10285         if (!vport || !elsiocb->cmd_dmabuf)
10286                 goto dropit;
10287
10288         newnode = 0;
10289         wcqe_cmpl = &elsiocb->wcqe_cmpl;
10290         payload = elsiocb->cmd_dmabuf->virt;
10291         if (phba->sli_rev == LPFC_SLI_REV4)
10292                 payload_len = wcqe_cmpl->total_data_placed;
10293         else
10294                 payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
10295         status = get_job_ulpstatus(phba, elsiocb);
10296         cmd = *payload;
10297         if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
10298                 lpfc_sli3_post_buffer(phba, pring, 1);
10299
10300         did = get_job_els_rsp64_did(phba, elsiocb);
10301         if (status) {
10302                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10303                         "RCV Unsol ELS:  status:x%x/x%x did:x%x",
10304                         status, get_job_word4(phba, elsiocb), did);
10305                 goto dropit;
10306         }
10307
10308         /* Check to see if link went down during discovery */
10309         if (lpfc_els_chk_latt(vport))
10310                 goto dropit;
10311
10312         /* Ignore traffic received during vport shutdown. */
10313         if (test_bit(FC_UNLOADING, &vport->load_flag))
10314                 goto dropit;
10315
10316         /* If NPort discovery is delayed drop incoming ELS */
10317         if (test_bit(FC_DISC_DELAYED, &vport->fc_flag) &&
10318             cmd != ELS_CMD_PLOGI)
10319                 goto dropit;
10320
10321         ndlp = lpfc_findnode_did(vport, did);
10322         if (!ndlp) {
10323                 /* Cannot find existing Fabric ndlp, so allocate a new one */
10324                 ndlp = lpfc_nlp_init(vport, did);
10325                 if (!ndlp)
10326                         goto dropit;
10327                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10328                 newnode = 1;
10329                 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
10330                         ndlp->nlp_type |= NLP_FABRIC;
10331         } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
10332                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10333                 newnode = 1;
10334         }
10335
10336         phba->fc_stat.elsRcvFrame++;
10337
10338         /*
10339          * Do not process any unsolicited ELS commands
10340          * if the ndlp is in DEV_LOSS
10341          */
10342         if (test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag)) {
10343                 if (newnode)
10344                         lpfc_nlp_put(ndlp);
10345                 goto dropit;
10346         }
10347
10348         elsiocb->ndlp = lpfc_nlp_get(ndlp);
10349         if (!elsiocb->ndlp)
10350                 goto dropit;
10351         elsiocb->vport = vport;
10352
10353         if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
10354                 cmd &= ELS_CMD_MASK;
10355         }
10356         /* ELS command <elsCmd> received from NPORT <did> */
10357         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10358                          "0112 ELS command x%x received from NPORT x%x "
10359                          "refcnt %d Data: x%x x%lx x%x x%x\n",
10360                          cmd, did, kref_read(&ndlp->kref), vport->port_state,
10361                          vport->fc_flag, vport->fc_myDID, vport->fc_prevDID);
10362
10363         /* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
10364         if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
10365             (cmd != ELS_CMD_FLOGI) &&
10366             !((cmd == ELS_CMD_PLOGI) && test_bit(FC_PT2PT, &vport->fc_flag))) {
10367                 rjt_err = LSRJT_LOGICAL_BSY;
10368                 rjt_exp = LSEXP_NOTHING_MORE;
10369                 goto lsrjt;
10370         }
10371
10372         switch (cmd) {
10373         case ELS_CMD_PLOGI:
10374                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10375                         "RCV PLOGI:       did:x%x/ste:x%x flg:x%lx",
10376                         did, vport->port_state, ndlp->nlp_flag);
10377
10378                 phba->fc_stat.elsRcvPLOGI++;
10379                 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
10380                 if (phba->sli_rev == LPFC_SLI_REV4 &&
10381                     test_bit(FC_PT2PT, &phba->pport->fc_flag)) {
10382                         vport->fc_prevDID = vport->fc_myDID;
10383                         /* Our DID needs to be updated before registering
10384                          * the vfi. This is done in lpfc_rcv_plogi but
10385                          * that is called after the reg_vfi.
10386                          */
10387                         vport->fc_myDID =
10388                                 bf_get(els_rsp64_sid,
10389                                        &elsiocb->wqe.xmit_els_rsp);
10390                         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10391                                          "3312 Remote port assigned DID x%x "
10392                                          "%x\n", vport->fc_myDID,
10393                                          vport->fc_prevDID);
10394                 }
10395
10396                 lpfc_send_els_event(vport, ndlp, payload);
10397
10398                 /* If Nport discovery is delayed, reject PLOGIs */
10399                 if (test_bit(FC_DISC_DELAYED, &vport->fc_flag)) {
10400                         rjt_err = LSRJT_UNABLE_TPC;
10401                         rjt_exp = LSEXP_NOTHING_MORE;
10402                         break;
10403                 }
10404
10405                 if (vport->port_state < LPFC_DISC_AUTH) {
10406                         if (!test_bit(FC_PT2PT, &phba->pport->fc_flag) ||
10407                             test_bit(FC_PT2PT_PLOGI, &phba->pport->fc_flag)) {
10408                                 rjt_err = LSRJT_UNABLE_TPC;
10409                                 rjt_exp = LSEXP_NOTHING_MORE;
10410                                 break;
10411                         }
10412                 }
10413
10414                 clear_bit(NLP_TARGET_REMOVE, &ndlp->nlp_flag);
10415
10416                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
10417                                         NLP_EVT_RCV_PLOGI);
10418
10419                 break;
10420         case ELS_CMD_FLOGI:
10421                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10422                         "RCV FLOGI:       did:x%x/ste:x%x flg:x%lx",
10423                         did, vport->port_state, ndlp->nlp_flag);
10424
10425                 phba->fc_stat.elsRcvFLOGI++;
10426
10427                 /* If the driver believes fabric discovery is done and is ready,
10428                  * bounce the link.  There is some descrepancy.
10429                  */
10430                 if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
10431                     test_bit(FC_PT2PT, &vport->fc_flag) &&
10432                     vport->rcv_flogi_cnt >= 1) {
10433                         rjt_err = LSRJT_LOGICAL_BSY;
10434                         rjt_exp = LSEXP_NOTHING_MORE;
10435                         init_link++;
10436                         goto lsrjt;
10437                 }
10438
10439                 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
10440                 /* retain node if our response is deferred */
10441                 if (phba->defer_flogi_acc.flag)
10442                         break;
10443                 if (newnode)
10444                         lpfc_disc_state_machine(vport, ndlp, NULL,
10445                                         NLP_EVT_DEVICE_RM);
10446                 break;
10447         case ELS_CMD_LOGO:
10448                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10449                         "RCV LOGO:        did:x%x/ste:x%x flg:x%lx",
10450                         did, vport->port_state, ndlp->nlp_flag);
10451
10452                 phba->fc_stat.elsRcvLOGO++;
10453                 lpfc_send_els_event(vport, ndlp, payload);
10454                 if (vport->port_state < LPFC_DISC_AUTH) {
10455                         rjt_err = LSRJT_UNABLE_TPC;
10456                         rjt_exp = LSEXP_NOTHING_MORE;
10457                         break;
10458                 }
10459                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
10460                 if (newnode)
10461                         lpfc_disc_state_machine(vport, ndlp, NULL,
10462                                         NLP_EVT_DEVICE_RM);
10463                 break;
10464         case ELS_CMD_PRLO:
10465                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10466                         "RCV PRLO:        did:x%x/ste:x%x flg:x%lx",
10467                         did, vport->port_state, ndlp->nlp_flag);
10468
10469                 phba->fc_stat.elsRcvPRLO++;
10470                 lpfc_send_els_event(vport, ndlp, payload);
10471                 if (vport->port_state < LPFC_DISC_AUTH) {
10472                         rjt_err = LSRJT_UNABLE_TPC;
10473                         rjt_exp = LSEXP_NOTHING_MORE;
10474                         break;
10475                 }
10476                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
10477                 break;
10478         case ELS_CMD_LCB:
10479                 phba->fc_stat.elsRcvLCB++;
10480                 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
10481                 break;
10482         case ELS_CMD_RDP:
10483                 phba->fc_stat.elsRcvRDP++;
10484                 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
10485                 break;
10486         case ELS_CMD_RSCN:
10487                 phba->fc_stat.elsRcvRSCN++;
10488                 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
10489                 if (newnode)
10490                         lpfc_disc_state_machine(vport, ndlp, NULL,
10491                                         NLP_EVT_DEVICE_RM);
10492                 break;
10493         case ELS_CMD_ADISC:
10494                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10495                         "RCV ADISC:       did:x%x/ste:x%x flg:x%lx",
10496                         did, vport->port_state, ndlp->nlp_flag);
10497
10498                 lpfc_send_els_event(vport, ndlp, payload);
10499                 phba->fc_stat.elsRcvADISC++;
10500                 if (vport->port_state < LPFC_DISC_AUTH) {
10501                         rjt_err = LSRJT_UNABLE_TPC;
10502                         rjt_exp = LSEXP_NOTHING_MORE;
10503                         break;
10504                 }
10505                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
10506                                         NLP_EVT_RCV_ADISC);
10507                 break;
10508         case ELS_CMD_PDISC:
10509                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10510                         "RCV PDISC:       did:x%x/ste:x%x flg:x%lx",
10511                         did, vport->port_state, ndlp->nlp_flag);
10512
10513                 phba->fc_stat.elsRcvPDISC++;
10514                 if (vport->port_state < LPFC_DISC_AUTH) {
10515                         rjt_err = LSRJT_UNABLE_TPC;
10516                         rjt_exp = LSEXP_NOTHING_MORE;
10517                         break;
10518                 }
10519                 lpfc_disc_state_machine(vport, ndlp, elsiocb,
10520                                         NLP_EVT_RCV_PDISC);
10521                 break;
10522         case ELS_CMD_FARPR:
10523                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10524                         "RCV FARPR:       did:x%x/ste:x%x flg:x%lx",
10525                         did, vport->port_state, ndlp->nlp_flag);
10526
10527                 phba->fc_stat.elsRcvFARPR++;
10528                 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
10529                 break;
10530         case ELS_CMD_FARP:
10531                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10532                         "RCV FARP:        did:x%x/ste:x%x flg:x%lx",
10533                         did, vport->port_state, ndlp->nlp_flag);
10534
10535                 phba->fc_stat.elsRcvFARP++;
10536                 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
10537                 break;
10538         case ELS_CMD_FAN:
10539                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10540                         "RCV FAN:         did:x%x/ste:x%x flg:x%lx",
10541                         did, vport->port_state, ndlp->nlp_flag);
10542
10543                 phba->fc_stat.elsRcvFAN++;
10544                 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
10545                 break;
10546         case ELS_CMD_PRLI:
10547         case ELS_CMD_NVMEPRLI:
10548                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10549                         "RCV PRLI:        did:x%x/ste:x%x flg:x%lx",
10550                         did, vport->port_state, ndlp->nlp_flag);
10551
10552                 phba->fc_stat.elsRcvPRLI++;
10553                 if ((vport->port_state < LPFC_DISC_AUTH) &&
10554                     test_bit(FC_FABRIC, &vport->fc_flag)) {
10555                         rjt_err = LSRJT_UNABLE_TPC;
10556                         rjt_exp = LSEXP_NOTHING_MORE;
10557                         break;
10558                 }
10559                 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
10560                 break;
10561         case ELS_CMD_LIRR:
10562                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10563                         "RCV LIRR:        did:x%x/ste:x%x flg:x%lx",
10564                         did, vport->port_state, ndlp->nlp_flag);
10565
10566                 phba->fc_stat.elsRcvLIRR++;
10567                 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
10568                 if (newnode)
10569                         lpfc_disc_state_machine(vport, ndlp, NULL,
10570                                         NLP_EVT_DEVICE_RM);
10571                 break;
10572         case ELS_CMD_RLS:
10573                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10574                         "RCV RLS:         did:x%x/ste:x%x flg:x%lx",
10575                         did, vport->port_state, ndlp->nlp_flag);
10576
10577                 phba->fc_stat.elsRcvRLS++;
10578                 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
10579                 if (newnode)
10580                         lpfc_disc_state_machine(vport, ndlp, NULL,
10581                                         NLP_EVT_DEVICE_RM);
10582                 break;
10583         case ELS_CMD_RPL:
10584                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10585                         "RCV RPL:         did:x%x/ste:x%x flg:x%lx",
10586                         did, vport->port_state, ndlp->nlp_flag);
10587
10588                 phba->fc_stat.elsRcvRPL++;
10589                 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
10590                 if (newnode)
10591                         lpfc_disc_state_machine(vport, ndlp, NULL,
10592                                         NLP_EVT_DEVICE_RM);
10593                 break;
10594         case ELS_CMD_RNID:
10595                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10596                         "RCV RNID:        did:x%x/ste:x%x flg:x%lx",
10597                         did, vport->port_state, ndlp->nlp_flag);
10598
10599                 phba->fc_stat.elsRcvRNID++;
10600                 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
10601                 if (newnode)
10602                         lpfc_disc_state_machine(vport, ndlp, NULL,
10603                                         NLP_EVT_DEVICE_RM);
10604                 break;
10605         case ELS_CMD_RTV:
10606                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10607                         "RCV RTV:        did:x%x/ste:x%x flg:x%lx",
10608                         did, vport->port_state, ndlp->nlp_flag);
10609                 phba->fc_stat.elsRcvRTV++;
10610                 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
10611                 if (newnode)
10612                         lpfc_disc_state_machine(vport, ndlp, NULL,
10613                                         NLP_EVT_DEVICE_RM);
10614                 break;
10615         case ELS_CMD_RRQ:
10616                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10617                         "RCV RRQ:         did:x%x/ste:x%x flg:x%lx",
10618                         did, vport->port_state, ndlp->nlp_flag);
10619
10620                 phba->fc_stat.elsRcvRRQ++;
10621                 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
10622                 if (newnode)
10623                         lpfc_disc_state_machine(vport, ndlp, NULL,
10624                                         NLP_EVT_DEVICE_RM);
10625                 break;
10626         case ELS_CMD_ECHO:
10627                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10628                         "RCV ECHO:        did:x%x/ste:x%x flg:x%lx",
10629                         did, vport->port_state, ndlp->nlp_flag);
10630
10631                 phba->fc_stat.elsRcvECHO++;
10632                 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
10633                 if (newnode)
10634                         lpfc_disc_state_machine(vport, ndlp, NULL,
10635                                         NLP_EVT_DEVICE_RM);
10636                 break;
10637         case ELS_CMD_REC:
10638                 /* receive this due to exchange closed */
10639                 rjt_err = LSRJT_UNABLE_TPC;
10640                 rjt_exp = LSEXP_INVALID_OX_RX;
10641                 break;
10642         case ELS_CMD_FPIN:
10643                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10644                                       "RCV FPIN:       did:x%x/ste:x%x "
10645                                       "flg:x%lx",
10646                                       did, vport->port_state, ndlp->nlp_flag);
10647
10648                 lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
10649                                   payload_len);
10650
10651                 /* There are no replies, so no rjt codes */
10652                 break;
10653         case ELS_CMD_EDC:
10654                 lpfc_els_rcv_edc(vport, elsiocb, ndlp);
10655                 break;
10656         case ELS_CMD_RDF:
10657                 phba->fc_stat.elsRcvRDF++;
10658                 /* Accept RDF only from fabric controller */
10659                 if (did != Fabric_Cntl_DID) {
10660                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
10661                                          "1115 Received RDF from invalid DID "
10662                                          "x%x\n", did);
10663                         rjt_err = LSRJT_PROTOCOL_ERR;
10664                         rjt_exp = LSEXP_NOTHING_MORE;
10665                         goto lsrjt;
10666                 }
10667
10668                 lpfc_els_rcv_rdf(vport, elsiocb, ndlp);
10669                 break;
10670         default:
10671                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10672                         "RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
10673                         cmd, did, vport->port_state);
10674
10675                 /* Unsupported ELS command, reject */
10676                 rjt_err = LSRJT_CMD_UNSUPPORTED;
10677                 rjt_exp = LSEXP_NOTHING_MORE;
10678
10679                 /* Unknown ELS command <elsCmd> received from NPORT <did> */
10680                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
10681                                  "0115 Unknown ELS command x%x "
10682                                  "received from NPORT x%x\n", cmd, did);
10683                 if (newnode)
10684                         lpfc_disc_state_machine(vport, ndlp, NULL,
10685                                         NLP_EVT_DEVICE_RM);
10686                 break;
10687         }
10688
10689 lsrjt:
10690         /* check if need to LS_RJT received ELS cmd */
10691         if (rjt_err) {
10692                 memset(&stat, 0, sizeof(stat));
10693                 stat.un.b.lsRjtRsnCode = rjt_err;
10694                 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
10695                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
10696                                     NULL);
10697                 /* Remove the reference from above for new nodes. */
10698                 if (newnode)
10699                         lpfc_disc_state_machine(vport, ndlp, NULL,
10700                                         NLP_EVT_DEVICE_RM);
10701         }
10702
10703         /* Release the reference on this elsiocb, not the ndlp. */
10704         lpfc_nlp_put(elsiocb->ndlp);
10705         elsiocb->ndlp = NULL;
10706
10707         /* Special case.  Driver received an unsolicited command that
10708          * unsupportable given the driver's current state.  Reset the
10709          * link and start over.
10710          */
10711         if (init_link) {
10712                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10713                 if (!mbox)
10714                         return;
10715                 lpfc_linkdown(phba);
10716                 lpfc_init_link(phba, mbox,
10717                                phba->cfg_topology,
10718                                phba->cfg_link_speed);
10719                 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
10720                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10721                 mbox->vport = vport;
10722                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
10723                     MBX_NOT_FINISHED)
10724                         mempool_free(mbox, phba->mbox_mem_pool);
10725         }
10726
10727         return;
10728
10729 dropit:
10730         if (vport && !test_bit(FC_UNLOADING, &vport->load_flag))
10731                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10732                         "0111 Dropping received ELS cmd "
10733                         "Data: x%x x%x x%x x%x\n",
10734                         cmd, status, get_job_word4(phba, elsiocb), did);
10735
10736         phba->fc_stat.elsRcvDrop++;
10737 }
10738
10739 /**
10740  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
10741  * @phba: pointer to lpfc hba data structure.
10742  * @pring: pointer to a SLI ring.
10743  * @elsiocb: pointer to lpfc els iocb data structure.
10744  *
10745  * This routine is used to process an unsolicited event received from a SLI
10746  * (Service Level Interface) ring. The actual processing of the data buffer
10747  * associated with the unsolicited event is done by invoking the routine
10748  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
10749  * SLI ring on which the unsolicited event was received.
10750  **/
10751 void
10752 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10753                      struct lpfc_iocbq *elsiocb)
10754 {
10755         struct lpfc_vport *vport = elsiocb->vport;
10756         u32 ulp_command, status, parameter, bde_count = 0;
10757         IOCB_t *icmd;
10758         struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10759         struct lpfc_dmabuf *bdeBuf1 = elsiocb->cmd_dmabuf;
10760         struct lpfc_dmabuf *bdeBuf2 = elsiocb->bpl_dmabuf;
10761         dma_addr_t paddr;
10762
10763         elsiocb->cmd_dmabuf = NULL;
10764         elsiocb->rsp_dmabuf = NULL;
10765         elsiocb->bpl_dmabuf = NULL;
10766
10767         wcqe_cmpl = &elsiocb->wcqe_cmpl;
10768         ulp_command = get_job_cmnd(phba, elsiocb);
10769         status = get_job_ulpstatus(phba, elsiocb);
10770         parameter = get_job_word4(phba, elsiocb);
10771         if (phba->sli_rev == LPFC_SLI_REV4)
10772                 bde_count = wcqe_cmpl->word3;
10773         else
10774                 bde_count = elsiocb->iocb.ulpBdeCount;
10775
10776         if (status == IOSTAT_NEED_BUFFER) {
10777                 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
10778         } else if (status == IOSTAT_LOCAL_REJECT &&
10779                    (parameter & IOERR_PARAM_MASK) ==
10780                    IOERR_RCV_BUFFER_WAITING) {
10781                 phba->fc_stat.NoRcvBuf++;
10782                 /* Not enough posted buffers; Try posting more buffers */
10783                 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
10784                         lpfc_sli3_post_buffer(phba, pring, 0);
10785                 return;
10786         }
10787
10788         if (phba->sli_rev == LPFC_SLI_REV3) {
10789                 icmd = &elsiocb->iocb;
10790                 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
10791                     (ulp_command == CMD_IOCB_RCV_ELS64_CX ||
10792                      ulp_command == CMD_IOCB_RCV_SEQ64_CX)) {
10793                         if (icmd->unsli3.rcvsli3.vpi == 0xffff)
10794                                 vport = phba->pport;
10795                         else
10796                                 vport = lpfc_find_vport_by_vpid(phba,
10797                                                 icmd->unsli3.rcvsli3.vpi);
10798                 }
10799         }
10800
10801         /* If there are no BDEs associated
10802          * with this IOCB, there is nothing to do.
10803          */
10804         if (bde_count == 0)
10805                 return;
10806
10807         /* Account for SLI2 or SLI3 and later unsolicited buffering */
10808         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
10809                 elsiocb->cmd_dmabuf = bdeBuf1;
10810                 if (bde_count == 2)
10811                         elsiocb->bpl_dmabuf = bdeBuf2;
10812         } else {
10813                 icmd = &elsiocb->iocb;
10814                 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
10815                                  icmd->un.cont64[0].addrLow);
10816                 elsiocb->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
10817                                                                paddr);
10818                 if (bde_count == 2) {
10819                         paddr = getPaddr(icmd->un.cont64[1].addrHigh,
10820                                          icmd->un.cont64[1].addrLow);
10821                         elsiocb->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
10822                                                                         pring,
10823                                                                         paddr);
10824                 }
10825         }
10826
10827         lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10828         /*
10829          * The different unsolicited event handlers would tell us
10830          * if they are done with "mp" by setting cmd_dmabuf to NULL.
10831          */
10832         if (elsiocb->cmd_dmabuf) {
10833                 lpfc_in_buf_free(phba, elsiocb->cmd_dmabuf);
10834                 elsiocb->cmd_dmabuf = NULL;
10835         }
10836
10837         if (elsiocb->bpl_dmabuf) {
10838                 lpfc_in_buf_free(phba, elsiocb->bpl_dmabuf);
10839                 elsiocb->bpl_dmabuf = NULL;
10840         }
10841
10842 }
10843
10844 static void
10845 lpfc_start_fdmi(struct lpfc_vport *vport)
10846 {
10847         struct lpfc_nodelist *ndlp;
10848
10849         /* If this is the first time, allocate an ndlp and initialize
10850          * it. Otherwise, make sure the node is enabled and then do the
10851          * login.
10852          */
10853         ndlp = lpfc_findnode_did(vport, FDMI_DID);
10854         if (!ndlp) {
10855                 ndlp = lpfc_nlp_init(vport, FDMI_DID);
10856                 if (ndlp) {
10857                         ndlp->nlp_type |= NLP_FABRIC;
10858                 } else {
10859                         return;
10860                 }
10861         }
10862
10863         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10864         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
10865 }
10866
10867 /**
10868  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
10869  * @phba: pointer to lpfc hba data structure.
10870  * @vport: pointer to a virtual N_Port data structure.
10871  *
10872  * This routine issues a Port Login (PLOGI) to the Name Server with
10873  * State Change Request (SCR) for a @vport. This routine will create an
10874  * ndlp for the Name Server associated to the @vport if such node does
10875  * not already exist. The PLOGI to Name Server is issued by invoking the
10876  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
10877  * (FDMI) is configured to the @vport, a FDMI node will be created and
10878  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
10879  **/
10880 void
10881 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
10882 {
10883         struct lpfc_nodelist *ndlp;
10884
10885         /*
10886          * If lpfc_delay_discovery parameter is set and the clean address
10887          * bit is cleared and fc fabric parameters chenged, delay FC NPort
10888          * discovery.
10889          */
10890         if (test_bit(FC_DISC_DELAYED, &vport->fc_flag)) {
10891                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10892                                  "3334 Delay fc port discovery for %d secs\n",
10893                                  phba->fc_ratov);
10894                 mod_timer(&vport->delayed_disc_tmo,
10895                         jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
10896                 return;
10897         }
10898
10899         ndlp = lpfc_findnode_did(vport, NameServer_DID);
10900         if (!ndlp) {
10901                 ndlp = lpfc_nlp_init(vport, NameServer_DID);
10902                 if (!ndlp) {
10903                         if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10904                                 lpfc_disc_start(vport);
10905                                 return;
10906                         }
10907                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10908                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10909                                          "0251 NameServer login: no memory\n");
10910                         return;
10911                 }
10912         }
10913
10914         ndlp->nlp_type |= NLP_FABRIC;
10915
10916         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10917
10918         if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
10919                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10920                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10921                                  "0252 Cannot issue NameServer login\n");
10922                 return;
10923         }
10924
10925         if ((phba->cfg_enable_SmartSAN ||
10926              phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT) &&
10927             test_bit(FC_ALLOW_FDMI, &vport->load_flag))
10928                 lpfc_start_fdmi(vport);
10929 }
10930
10931 /**
10932  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
10933  * @phba: pointer to lpfc hba data structure.
10934  * @pmb: pointer to the driver internal queue element for mailbox command.
10935  *
10936  * This routine is the completion callback function to register new vport
10937  * mailbox command. If the new vport mailbox command completes successfully,
10938  * the fabric registration login shall be performed on physical port (the
10939  * new vport created is actually a physical port, with VPI 0) or the port
10940  * login to Name Server for State Change Request (SCR) will be performed
10941  * on virtual port (real virtual port, with VPI greater than 0).
10942  **/
10943 static void
10944 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
10945 {
10946         struct lpfc_vport *vport = pmb->vport;
10947         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10948         struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
10949         MAILBOX_t *mb = &pmb->u.mb;
10950         int rc;
10951
10952         clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
10953
10954         if (mb->mbxStatus) {
10955                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10956                                 "0915 Register VPI failed : Status: x%x"
10957                                 " upd bit: x%x \n", mb->mbxStatus,
10958                                  mb->un.varRegVpi.upd);
10959                 if (phba->sli_rev == LPFC_SLI_REV4 &&
10960                         mb->un.varRegVpi.upd)
10961                         goto mbox_err_exit ;
10962
10963                 switch (mb->mbxStatus) {
10964                 case 0x11:      /* unsupported feature */
10965                 case 0x9603:    /* max_vpi exceeded */
10966                 case 0x9602:    /* Link event since CLEAR_LA */
10967                         /* giving up on vport registration */
10968                         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10969                         clear_bit(FC_FABRIC, &vport->fc_flag);
10970                         clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
10971                         lpfc_can_disctmo(vport);
10972                         break;
10973                 /* If reg_vpi fail with invalid VPI status, re-init VPI */
10974                 case 0x20:
10975                         set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
10976                         lpfc_init_vpi(phba, pmb, vport->vpi);
10977                         pmb->vport = vport;
10978                         pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
10979                         rc = lpfc_sli_issue_mbox(phba, pmb,
10980                                 MBX_NOWAIT);
10981                         if (rc == MBX_NOT_FINISHED) {
10982                                 lpfc_printf_vlog(vport, KERN_ERR,
10983                                                  LOG_TRACE_EVENT,
10984                                         "2732 Failed to issue INIT_VPI"
10985                                         " mailbox command\n");
10986                         } else {
10987                                 lpfc_nlp_put(ndlp);
10988                                 return;
10989                         }
10990                         fallthrough;
10991                 default:
10992                         /* Try to recover from this error */
10993                         if (phba->sli_rev == LPFC_SLI_REV4)
10994                                 lpfc_sli4_unreg_all_rpis(vport);
10995                         lpfc_mbx_unreg_vpi(vport);
10996                         set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
10997                         if (mb->mbxStatus == MBX_NOT_FINISHED)
10998                                 break;
10999                         if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
11000                             !test_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag)) {
11001                                 if (phba->sli_rev == LPFC_SLI_REV4)
11002                                         lpfc_issue_init_vfi(vport);
11003                                 else
11004                                         lpfc_initial_flogi(vport);
11005                         } else {
11006                                 lpfc_initial_fdisc(vport);
11007                         }
11008                         break;
11009                 }
11010         } else {
11011                 spin_lock_irq(shost->host_lock);
11012                 vport->vpi_state |= LPFC_VPI_REGISTERED;
11013                 spin_unlock_irq(shost->host_lock);
11014                 if (vport == phba->pport) {
11015                         if (phba->sli_rev < LPFC_SLI_REV4)
11016                                 lpfc_issue_fabric_reglogin(vport);
11017                         else {
11018                                 /*
11019                                  * If the physical port is instantiated using
11020                                  * FDISC, do not start vport discovery.
11021                                  */
11022                                 if (vport->port_state != LPFC_FDISC)
11023                                         lpfc_start_fdiscs(phba);
11024                                 lpfc_do_scr_ns_plogi(phba, vport);
11025                         }
11026                 } else {
11027                         lpfc_do_scr_ns_plogi(phba, vport);
11028                 }
11029         }
11030 mbox_err_exit:
11031         /* Now, we decrement the ndlp reference count held for this
11032          * callback function
11033          */
11034         lpfc_nlp_put(ndlp);
11035
11036         mempool_free(pmb, phba->mbox_mem_pool);
11037
11038         /* reinitialize the VMID datastructure before returning.
11039          * this is specifically for vport
11040          */
11041         if (lpfc_is_vmid_enabled(phba))
11042                 lpfc_reinit_vmid(vport);
11043         vport->vmid_flag = vport->phba->pport->vmid_flag;
11044
11045         return;
11046 }
11047
11048 /**
11049  * lpfc_register_new_vport - Register a new vport with a HBA
11050  * @phba: pointer to lpfc hba data structure.
11051  * @vport: pointer to a host virtual N_Port data structure.
11052  * @ndlp: pointer to a node-list data structure.
11053  *
11054  * This routine registers the @vport as a new virtual port with a HBA.
11055  * It is done through a registering vpi mailbox command.
11056  **/
11057 void
11058 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
11059                         struct lpfc_nodelist *ndlp)
11060 {
11061         LPFC_MBOXQ_t *mbox;
11062
11063         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11064         if (mbox) {
11065                 lpfc_reg_vpi(vport, mbox);
11066                 mbox->vport = vport;
11067                 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
11068                 if (!mbox->ctx_ndlp) {
11069                         mempool_free(mbox, phba->mbox_mem_pool);
11070                         goto mbox_err_exit;
11071                 }
11072
11073                 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
11074                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
11075                     == MBX_NOT_FINISHED) {
11076                         /* mailbox command not success, decrement ndlp
11077                          * reference count for this command
11078                          */
11079                         lpfc_nlp_put(ndlp);
11080                         mempool_free(mbox, phba->mbox_mem_pool);
11081
11082                         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11083                                 "0253 Register VPI: Can't send mbox\n");
11084                         goto mbox_err_exit;
11085                 }
11086         } else {
11087                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11088                                  "0254 Register VPI: no memory\n");
11089                 goto mbox_err_exit;
11090         }
11091         return;
11092
11093 mbox_err_exit:
11094         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11095         clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11096         return;
11097 }
11098
11099 /**
11100  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
11101  * @phba: pointer to lpfc hba data structure.
11102  *
11103  * This routine cancels the retry delay timers to all the vports.
11104  **/
11105 void
11106 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
11107 {
11108         struct lpfc_vport **vports;
11109         struct lpfc_nodelist *ndlp;
11110         uint32_t link_state;
11111         int i;
11112
11113         /* Treat this failure as linkdown for all vports */
11114         link_state = phba->link_state;
11115         lpfc_linkdown(phba);
11116         phba->link_state = link_state;
11117
11118         vports = lpfc_create_vport_work_array(phba);
11119
11120         if (vports) {
11121                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11122                         ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
11123                         if (ndlp)
11124                                 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
11125                         lpfc_els_flush_cmd(vports[i]);
11126                 }
11127                 lpfc_destroy_vport_work_array(phba, vports);
11128         }
11129 }
11130
11131 /**
11132  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
11133  * @phba: pointer to lpfc hba data structure.
11134  *
11135  * This routine abort all pending discovery commands and
11136  * start a timer to retry FLOGI for the physical port
11137  * discovery.
11138  **/
11139 void
11140 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
11141 {
11142         struct lpfc_nodelist *ndlp;
11143
11144         /* Cancel the all vports retry delay retry timers */
11145         lpfc_cancel_all_vport_retry_delay_timer(phba);
11146
11147         /* If fabric require FLOGI, then re-instantiate physical login */
11148         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
11149         if (!ndlp)
11150                 return;
11151
11152         mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
11153         set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
11154         ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
11155         phba->pport->port_state = LPFC_FLOGI;
11156         return;
11157 }
11158
11159 /**
11160  * lpfc_fabric_login_reqd - Check if FLOGI required.
11161  * @phba: pointer to lpfc hba data structure.
11162  * @cmdiocb: pointer to FDISC command iocb.
11163  * @rspiocb: pointer to FDISC response iocb.
11164  *
11165  * This routine checks if a FLOGI is reguired for FDISC
11166  * to succeed.
11167  **/
11168 static int
11169 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
11170                 struct lpfc_iocbq *cmdiocb,
11171                 struct lpfc_iocbq *rspiocb)
11172 {
11173         u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11174         u32 ulp_word4 = get_job_word4(phba, rspiocb);
11175
11176         if (ulp_status != IOSTAT_FABRIC_RJT ||
11177             ulp_word4 != RJT_LOGIN_REQUIRED)
11178                 return 0;
11179         else
11180                 return 1;
11181 }
11182
11183 /**
11184  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
11185  * @phba: pointer to lpfc hba data structure.
11186  * @cmdiocb: pointer to lpfc command iocb data structure.
11187  * @rspiocb: pointer to lpfc response iocb data structure.
11188  *
11189  * This routine is the completion callback function to a Fabric Discover
11190  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
11191  * single threaded, each FDISC completion callback function will reset
11192  * the discovery timer for all vports such that the timers will not get
11193  * unnecessary timeout. The function checks the FDISC IOCB status. If error
11194  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
11195  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
11196  * assigned to the vport has been changed with the completion of the FDISC
11197  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
11198  * are unregistered from the HBA, and then the lpfc_register_new_vport()
11199  * routine is invoked to register new vport with the HBA. Otherwise, the
11200  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
11201  * Server for State Change Request (SCR).
11202  **/
11203 static void
11204 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11205                     struct lpfc_iocbq *rspiocb)
11206 {
11207         struct lpfc_vport *vport = cmdiocb->vport;
11208         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
11209         struct lpfc_nodelist *np;
11210         struct lpfc_nodelist *next_np;
11211         struct lpfc_iocbq *piocb;
11212         struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
11213         struct serv_parm *sp;
11214         uint8_t fabric_param_changed;
11215         u32 ulp_status, ulp_word4;
11216
11217         ulp_status = get_job_ulpstatus(phba, rspiocb);
11218         ulp_word4 = get_job_word4(phba, rspiocb);
11219
11220         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11221                          "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
11222                          ulp_status, ulp_word4,
11223                          vport->fc_prevDID);
11224         /* Since all FDISCs are being single threaded, we
11225          * must reset the discovery timer for ALL vports
11226          * waiting to send FDISC when one completes.
11227          */
11228         list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
11229                 lpfc_set_disctmo(piocb->vport);
11230         }
11231
11232         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11233                 "FDISC cmpl:      status:x%x/x%x prevdid:x%x",
11234                 ulp_status, ulp_word4, vport->fc_prevDID);
11235
11236         if (ulp_status) {
11237
11238                 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
11239                         lpfc_retry_pport_discovery(phba);
11240                         goto out;
11241                 }
11242
11243                 /* Check for retry */
11244                 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
11245                         goto out;
11246                 /* Warn FDISC status */
11247                 lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
11248                               "0126 FDISC cmpl status: x%x/x%x)\n",
11249                               ulp_status, ulp_word4);
11250                 goto fdisc_failed;
11251         }
11252
11253         lpfc_check_nlp_post_devloss(vport, ndlp);
11254
11255         clear_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
11256         clear_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
11257         set_bit(FC_FABRIC, &vport->fc_flag);
11258         if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
11259                 set_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
11260
11261         vport->fc_myDID = ulp_word4 & Mask_DID;
11262         lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
11263         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
11264         if (!prsp)
11265                 goto out;
11266         if (!lpfc_is_els_acc_rsp(prsp))
11267                 goto out;
11268
11269         sp = prsp->virt + sizeof(uint32_t);
11270         fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
11271         memcpy(&vport->fabric_portname, &sp->portName,
11272                 sizeof(struct lpfc_name));
11273         memcpy(&vport->fabric_nodename, &sp->nodeName,
11274                 sizeof(struct lpfc_name));
11275         if (fabric_param_changed &&
11276                 !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
11277                 /* If our NportID changed, we need to ensure all
11278                  * remaining NPORTs get unreg_login'ed so we can
11279                  * issue unreg_vpi.
11280                  */
11281                 list_for_each_entry_safe(np, next_np,
11282                         &vport->fc_nodes, nlp_listp) {
11283                         if ((np->nlp_state != NLP_STE_NPR_NODE) ||
11284                             !test_bit(NLP_NPR_ADISC, &np->nlp_flag))
11285                                 continue;
11286                         clear_bit(NLP_NPR_ADISC, &np->nlp_flag);
11287                         lpfc_unreg_rpi(vport, np);
11288                 }
11289                 lpfc_cleanup_pending_mbox(vport);
11290
11291                 if (phba->sli_rev == LPFC_SLI_REV4)
11292                         lpfc_sli4_unreg_all_rpis(vport);
11293
11294                 lpfc_mbx_unreg_vpi(vport);
11295                 set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11296                 if (phba->sli_rev == LPFC_SLI_REV4)
11297                         set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
11298                 else
11299                         set_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag);
11300         } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
11301                    !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
11302                 /*
11303                  * Driver needs to re-reg VPI in order for f/w
11304                  * to update the MAC address.
11305                  */
11306                 lpfc_register_new_vport(phba, vport, ndlp);
11307                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11308                 goto out;
11309         }
11310
11311         if (test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag))
11312                 lpfc_issue_init_vpi(vport);
11313         else if (test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
11314                 lpfc_register_new_vport(phba, vport, ndlp);
11315         else
11316                 lpfc_do_scr_ns_plogi(phba, vport);
11317
11318         /* The FDISC completed successfully. Move the fabric ndlp to
11319          * UNMAPPED state and register with the transport.
11320          */
11321         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11322         goto out;
11323
11324 fdisc_failed:
11325         if (vport->fc_vport &&
11326             (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
11327                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11328         /* Cancel discovery timer */
11329         lpfc_can_disctmo(vport);
11330 out:
11331         lpfc_els_free_iocb(phba, cmdiocb);
11332         lpfc_nlp_put(ndlp);
11333 }
11334
11335 /**
11336  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
11337  * @vport: pointer to a virtual N_Port data structure.
11338  * @ndlp: pointer to a node-list data structure.
11339  * @retry: number of retries to the command IOCB.
11340  *
11341  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
11342  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
11343  * routine to issue the IOCB, which makes sure only one outstanding fabric
11344  * IOCB will be sent off HBA at any given time.
11345  *
11346  * Note that the ndlp reference count will be incremented by 1 for holding the
11347  * ndlp and the reference to ndlp will be stored into the ndlp field of
11348  * the IOCB for the completion callback function to the FDISC ELS command.
11349  *
11350  * Return code
11351  *   0 - Successfully issued fdisc iocb command
11352  *   1 - Failed to issue fdisc iocb command
11353  **/
11354 static int
11355 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
11356                      uint8_t retry)
11357 {
11358         struct lpfc_hba *phba = vport->phba;
11359         IOCB_t *icmd;
11360         union lpfc_wqe128 *wqe = NULL;
11361         struct lpfc_iocbq *elsiocb;
11362         struct serv_parm *sp;
11363         uint8_t *pcmd;
11364         uint16_t cmdsize;
11365         int did = ndlp->nlp_DID;
11366         int rc;
11367
11368         vport->port_state = LPFC_FDISC;
11369         vport->fc_myDID = 0;
11370         cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
11371         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
11372                                      ELS_CMD_FDISC);
11373         if (!elsiocb) {
11374                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11375                 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11376                                  "0255 Issue FDISC: no IOCB\n");
11377                 return 1;
11378         }
11379
11380         if (phba->sli_rev == LPFC_SLI_REV4) {
11381                 wqe = &elsiocb->wqe;
11382                 bf_set(els_req64_sid, &wqe->els_req, 0);
11383                 bf_set(els_req64_sp, &wqe->els_req, 1);
11384         } else {
11385                 icmd = &elsiocb->iocb;
11386                 icmd->un.elsreq64.myID = 0;
11387                 icmd->un.elsreq64.fl = 1;
11388                 icmd->ulpCt_h = 1;
11389                 icmd->ulpCt_l = 0;
11390         }
11391
11392         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11393         *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
11394         pcmd += sizeof(uint32_t); /* CSP Word 1 */
11395         memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
11396         sp = (struct serv_parm *) pcmd;
11397         /* Setup CSPs accordingly for Fabric */
11398         sp->cmn.e_d_tov = 0;
11399         sp->cmn.w2.r_a_tov = 0;
11400         sp->cmn.virtual_fabric_support = 0;
11401         sp->cls1.classValid = 0;
11402         sp->cls2.seqDelivery = 1;
11403         sp->cls3.seqDelivery = 1;
11404
11405         pcmd += sizeof(uint32_t); /* CSP Word 2 */
11406         pcmd += sizeof(uint32_t); /* CSP Word 3 */
11407         pcmd += sizeof(uint32_t); /* CSP Word 4 */
11408         pcmd += sizeof(uint32_t); /* Port Name */
11409         memcpy(pcmd, &vport->fc_portname, 8);
11410         pcmd += sizeof(uint32_t); /* Node Name */
11411         pcmd += sizeof(uint32_t); /* Node Name */
11412         memcpy(pcmd, &vport->fc_nodename, 8);
11413         sp->cmn.valid_vendor_ver_level = 0;
11414         memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
11415         lpfc_set_disctmo(vport);
11416
11417         phba->fc_stat.elsXmitFDISC++;
11418         elsiocb->cmd_cmpl = lpfc_cmpl_els_fdisc;
11419
11420         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11421                 "Issue FDISC:     did:x%x",
11422                 did, 0, 0);
11423
11424         elsiocb->ndlp = lpfc_nlp_get(ndlp);
11425         if (!elsiocb->ndlp)
11426                 goto err_out;
11427
11428         rc = lpfc_issue_fabric_iocb(phba, elsiocb);
11429         if (rc == IOCB_ERROR) {
11430                 lpfc_nlp_put(ndlp);
11431                 goto err_out;
11432         }
11433
11434         lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
11435         return 0;
11436
11437  err_out:
11438         lpfc_els_free_iocb(phba, elsiocb);
11439         lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11440         lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11441                          "0256 Issue FDISC: Cannot send IOCB\n");
11442         return 1;
11443 }
11444
11445 /**
11446  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
11447  * @phba: pointer to lpfc hba data structure.
11448  * @cmdiocb: pointer to lpfc command iocb data structure.
11449  * @rspiocb: pointer to lpfc response iocb data structure.
11450  *
11451  * This routine is the completion callback function to the issuing of a LOGO
11452  * ELS command off a vport. It frees the command IOCB and then decrement the
11453  * reference count held on ndlp for this completion function, indicating that
11454  * the reference to the ndlp is no long needed. Note that the
11455  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
11456  * callback function and an additional explicit ndlp reference decrementation
11457  * will trigger the actual release of the ndlp.
11458  **/
11459 static void
11460 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11461                         struct lpfc_iocbq *rspiocb)
11462 {
11463         struct lpfc_vport *vport = cmdiocb->vport;
11464         IOCB_t *irsp;
11465         struct lpfc_nodelist *ndlp;
11466         u32 ulp_status, ulp_word4, did, tmo;
11467
11468         ndlp = cmdiocb->ndlp;
11469
11470         ulp_status = get_job_ulpstatus(phba, rspiocb);
11471         ulp_word4 = get_job_word4(phba, rspiocb);
11472
11473         if (phba->sli_rev == LPFC_SLI_REV4) {
11474                 did = get_job_els_rsp64_did(phba, cmdiocb);
11475                 tmo = get_wqe_tmo(cmdiocb);
11476         } else {
11477                 irsp = &rspiocb->iocb;
11478                 did = get_job_els_rsp64_did(phba, rspiocb);
11479                 tmo = irsp->ulpTimeout;
11480         }
11481
11482         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11483                 "LOGO npiv cmpl:  status:x%x/x%x did:x%x",
11484                 ulp_status, ulp_word4, did);
11485
11486         /* NPIV LOGO completes to NPort <nlp_DID> */
11487         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11488                          "2928 NPIV LOGO completes to NPort x%x "
11489                          "Data: x%x x%x x%x x%x x%x x%lx x%x\n",
11490                          ndlp->nlp_DID, ulp_status, ulp_word4,
11491                          tmo, vport->num_disc_nodes,
11492                          kref_read(&ndlp->kref), ndlp->nlp_flag,
11493                          ndlp->fc4_xpt_flags);
11494
11495         if (ulp_status == IOSTAT_SUCCESS) {
11496                 clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
11497                 clear_bit(FC_FABRIC, &vport->fc_flag);
11498                 lpfc_can_disctmo(vport);
11499         }
11500
11501         if (ndlp->save_flags & NLP_WAIT_FOR_LOGO) {
11502                 /* Wake up lpfc_vport_delete if waiting...*/
11503                 if (ndlp->logo_waitq)
11504                         wake_up(ndlp->logo_waitq);
11505                 clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
11506                 clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11507                 spin_lock_irq(&ndlp->lock);
11508                 ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
11509                 spin_unlock_irq(&ndlp->lock);
11510         }
11511
11512         /* Safe to release resources now. */
11513         lpfc_els_free_iocb(phba, cmdiocb);
11514         lpfc_nlp_put(ndlp);
11515 }
11516
11517 /**
11518  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
11519  * @vport: pointer to a virtual N_Port data structure.
11520  * @ndlp: pointer to a node-list data structure.
11521  *
11522  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
11523  *
11524  * Note that the ndlp reference count will be incremented by 1 for holding the
11525  * ndlp and the reference to ndlp will be stored into the ndlp field of
11526  * the IOCB for the completion callback function to the LOGO ELS command.
11527  *
11528  * Return codes
11529  *   0 - Successfully issued logo off the @vport
11530  *   1 - Failed to issue logo off the @vport
11531  **/
11532 int
11533 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
11534 {
11535         int rc = 0;
11536         struct lpfc_hba  *phba = vport->phba;
11537         struct lpfc_iocbq *elsiocb;
11538         uint8_t *pcmd;
11539         uint16_t cmdsize;
11540
11541         cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
11542         elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
11543                                      ELS_CMD_LOGO);
11544         if (!elsiocb)
11545                 return 1;
11546
11547         pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11548         *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
11549         pcmd += sizeof(uint32_t);
11550
11551         /* Fill in LOGO payload */
11552         *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
11553         pcmd += sizeof(uint32_t);
11554         memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
11555
11556         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11557                 "Issue LOGO npiv  did:x%x flg:x%lx",
11558                 ndlp->nlp_DID, ndlp->nlp_flag, 0);
11559
11560         elsiocb->cmd_cmpl = lpfc_cmpl_els_npiv_logo;
11561         set_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11562         elsiocb->ndlp = lpfc_nlp_get(ndlp);
11563         if (!elsiocb->ndlp) {
11564                 lpfc_els_free_iocb(phba, elsiocb);
11565                 goto err;
11566         }
11567
11568         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
11569         if (rc == IOCB_ERROR) {
11570                 lpfc_els_free_iocb(phba, elsiocb);
11571                 lpfc_nlp_put(ndlp);
11572                 goto err;
11573         }
11574         return 0;
11575
11576 err:
11577         clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11578         return 1;
11579 }
11580
11581 /**
11582  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
11583  * @t: timer context used to obtain the lpfc hba.
11584  *
11585  * This routine is invoked by the fabric iocb block timer after
11586  * timeout. It posts the fabric iocb block timeout event by setting the
11587  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
11588  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
11589  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
11590  * posted event WORKER_FABRIC_BLOCK_TMO.
11591  **/
11592 void
11593 lpfc_fabric_block_timeout(struct timer_list *t)
11594 {
11595         struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
11596         unsigned long iflags;
11597         uint32_t tmo_posted;
11598
11599         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11600         tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
11601         if (!tmo_posted)
11602                 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
11603         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11604
11605         if (!tmo_posted)
11606                 lpfc_worker_wake_up(phba);
11607         return;
11608 }
11609
11610 /**
11611  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
11612  * @phba: pointer to lpfc hba data structure.
11613  *
11614  * This routine issues one fabric iocb from the driver internal list to
11615  * the HBA. It first checks whether it's ready to issue one fabric iocb to
11616  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
11617  * remove one pending fabric iocb from the driver internal list and invokes
11618  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
11619  **/
11620 static void
11621 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
11622 {
11623         struct lpfc_iocbq *iocb;
11624         unsigned long iflags;
11625         int ret;
11626
11627 repeat:
11628         iocb = NULL;
11629         spin_lock_irqsave(&phba->hbalock, iflags);
11630         /* Post any pending iocb to the SLI layer */
11631         if (atomic_read(&phba->fabric_iocb_count) == 0) {
11632                 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
11633                                  list);
11634                 if (iocb)
11635                         /* Increment fabric iocb count to hold the position */
11636                         atomic_inc(&phba->fabric_iocb_count);
11637         }
11638         spin_unlock_irqrestore(&phba->hbalock, iflags);
11639         if (iocb) {
11640                 iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11641                 iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11642                 iocb->cmd_flag |= LPFC_IO_FABRIC;
11643
11644                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11645                                       "Fabric sched1:   ste:x%x",
11646                                       iocb->vport->port_state, 0, 0);
11647
11648                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11649
11650                 if (ret == IOCB_ERROR) {
11651                         iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11652                         iocb->fabric_cmd_cmpl = NULL;
11653                         iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11654                         set_job_ulpstatus(iocb, IOSTAT_LOCAL_REJECT);
11655                         iocb->wcqe_cmpl.parameter = IOERR_SLI_ABORTED;
11656                         iocb->cmd_cmpl(phba, iocb, iocb);
11657
11658                         atomic_dec(&phba->fabric_iocb_count);
11659                         goto repeat;
11660                 }
11661         }
11662 }
11663
11664 /**
11665  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
11666  * @phba: pointer to lpfc hba data structure.
11667  *
11668  * This routine unblocks the  issuing fabric iocb command. The function
11669  * will clear the fabric iocb block bit and then invoke the routine
11670  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
11671  * from the driver internal fabric iocb list.
11672  **/
11673 void
11674 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
11675 {
11676         clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11677
11678         lpfc_resume_fabric_iocbs(phba);
11679         return;
11680 }
11681
11682 /**
11683  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
11684  * @phba: pointer to lpfc hba data structure.
11685  *
11686  * This routine blocks the issuing fabric iocb for a specified amount of
11687  * time (currently 100 ms). This is done by set the fabric iocb block bit
11688  * and set up a timeout timer for 100ms. When the block bit is set, no more
11689  * fabric iocb will be issued out of the HBA.
11690  **/
11691 static void
11692 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
11693 {
11694         int blocked;
11695
11696         blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11697         /* Start a timer to unblock fabric iocbs after 100ms */
11698         if (!blocked)
11699                 mod_timer(&phba->fabric_block_timer,
11700                           jiffies + msecs_to_jiffies(100));
11701
11702         return;
11703 }
11704
11705 /**
11706  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
11707  * @phba: pointer to lpfc hba data structure.
11708  * @cmdiocb: pointer to lpfc command iocb data structure.
11709  * @rspiocb: pointer to lpfc response iocb data structure.
11710  *
11711  * This routine is the callback function that is put to the fabric iocb's
11712  * callback function pointer (iocb->cmd_cmpl). The original iocb's callback
11713  * function pointer has been stored in iocb->fabric_cmd_cmpl. This callback
11714  * function first restores and invokes the original iocb's callback function
11715  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
11716  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
11717  **/
11718 static void
11719 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11720                       struct lpfc_iocbq *rspiocb)
11721 {
11722         struct ls_rjt stat;
11723         u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11724         u32 ulp_word4 = get_job_word4(phba, rspiocb);
11725
11726         WARN_ON((cmdiocb->cmd_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
11727
11728         switch (ulp_status) {
11729                 case IOSTAT_NPORT_RJT:
11730                 case IOSTAT_FABRIC_RJT:
11731                         if (ulp_word4 & RJT_UNAVAIL_TEMP)
11732                                 lpfc_block_fabric_iocbs(phba);
11733                         break;
11734
11735                 case IOSTAT_NPORT_BSY:
11736                 case IOSTAT_FABRIC_BSY:
11737                         lpfc_block_fabric_iocbs(phba);
11738                         break;
11739
11740                 case IOSTAT_LS_RJT:
11741                         stat.un.ls_rjt_error_be =
11742                                 cpu_to_be32(ulp_word4);
11743                         if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
11744                                 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
11745                                 lpfc_block_fabric_iocbs(phba);
11746                         break;
11747         }
11748
11749         BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
11750
11751         cmdiocb->cmd_cmpl = cmdiocb->fabric_cmd_cmpl;
11752         cmdiocb->fabric_cmd_cmpl = NULL;
11753         cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
11754         cmdiocb->cmd_cmpl(phba, cmdiocb, rspiocb);
11755
11756         atomic_dec(&phba->fabric_iocb_count);
11757         if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
11758                 /* Post any pending iocbs to HBA */
11759                 lpfc_resume_fabric_iocbs(phba);
11760         }
11761 }
11762
11763 /**
11764  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
11765  * @phba: pointer to lpfc hba data structure.
11766  * @iocb: pointer to lpfc command iocb data structure.
11767  *
11768  * This routine is used as the top-level API for issuing a fabric iocb command
11769  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
11770  * function makes sure that only one fabric bound iocb will be outstanding at
11771  * any given time. As such, this function will first check to see whether there
11772  * is already an outstanding fabric iocb on the wire. If so, it will put the
11773  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
11774  * issued later. Otherwise, it will issue the iocb on the wire and update the
11775  * fabric iocb count it indicate that there is one fabric iocb on the wire.
11776  *
11777  * Note, this implementation has a potential sending out fabric IOCBs out of
11778  * order. The problem is caused by the construction of the "ready" boolen does
11779  * not include the condition that the internal fabric IOCB list is empty. As
11780  * such, it is possible a fabric IOCB issued by this routine might be "jump"
11781  * ahead of the fabric IOCBs in the internal list.
11782  *
11783  * Return code
11784  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
11785  *   IOCB_ERROR - failed to issue fabric iocb
11786  **/
11787 static int
11788 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
11789 {
11790         unsigned long iflags;
11791         int ready;
11792         int ret;
11793
11794         BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
11795
11796         spin_lock_irqsave(&phba->hbalock, iflags);
11797         ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
11798                 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11799
11800         if (ready)
11801                 /* Increment fabric iocb count to hold the position */
11802                 atomic_inc(&phba->fabric_iocb_count);
11803         spin_unlock_irqrestore(&phba->hbalock, iflags);
11804         if (ready) {
11805                 iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11806                 iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11807                 iocb->cmd_flag |= LPFC_IO_FABRIC;
11808
11809                 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11810                                       "Fabric sched2:   ste:x%x",
11811                                       iocb->vport->port_state, 0, 0);
11812
11813                 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11814
11815                 if (ret == IOCB_ERROR) {
11816                         iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11817                         iocb->fabric_cmd_cmpl = NULL;
11818                         iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11819                         atomic_dec(&phba->fabric_iocb_count);
11820                 }
11821         } else {
11822                 spin_lock_irqsave(&phba->hbalock, iflags);
11823                 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
11824                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11825                 ret = IOCB_SUCCESS;
11826         }
11827         return ret;
11828 }
11829
11830 /**
11831  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
11832  * @vport: pointer to a virtual N_Port data structure.
11833  *
11834  * This routine aborts all the IOCBs associated with a @vport from the
11835  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11836  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11837  * list, removes each IOCB associated with the @vport off the list, set the
11838  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11839  * associated with the IOCB.
11840  **/
11841 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
11842 {
11843         LIST_HEAD(completions);
11844         struct lpfc_hba  *phba = vport->phba;
11845         struct lpfc_iocbq *tmp_iocb, *piocb;
11846
11847         spin_lock_irq(&phba->hbalock);
11848         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11849                                  list) {
11850
11851                 if (piocb->vport != vport)
11852                         continue;
11853
11854                 list_move_tail(&piocb->list, &completions);
11855         }
11856         spin_unlock_irq(&phba->hbalock);
11857
11858         /* Cancel all the IOCBs from the completions list */
11859         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11860                               IOERR_SLI_ABORTED);
11861 }
11862
11863 /**
11864  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
11865  * @ndlp: pointer to a node-list data structure.
11866  *
11867  * This routine aborts all the IOCBs associated with an @ndlp from the
11868  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11869  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11870  * list, removes each IOCB associated with the @ndlp off the list, set the
11871  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11872  * associated with the IOCB.
11873  **/
11874 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
11875 {
11876         LIST_HEAD(completions);
11877         struct lpfc_hba  *phba = ndlp->phba;
11878         struct lpfc_iocbq *tmp_iocb, *piocb;
11879         struct lpfc_sli_ring *pring;
11880
11881         pring = lpfc_phba_elsring(phba);
11882
11883         if (unlikely(!pring))
11884                 return;
11885
11886         spin_lock_irq(&phba->hbalock);
11887         list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11888                                  list) {
11889                 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
11890
11891                         list_move_tail(&piocb->list, &completions);
11892                 }
11893         }
11894         spin_unlock_irq(&phba->hbalock);
11895
11896         /* Cancel all the IOCBs from the completions list */
11897         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11898                               IOERR_SLI_ABORTED);
11899 }
11900
11901 /**
11902  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
11903  * @phba: pointer to lpfc hba data structure.
11904  *
11905  * This routine aborts all the IOCBs currently on the driver internal
11906  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
11907  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
11908  * list, removes IOCBs off the list, set the status field to
11909  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
11910  * the IOCB.
11911  **/
11912 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
11913 {
11914         LIST_HEAD(completions);
11915
11916         spin_lock_irq(&phba->hbalock);
11917         list_splice_init(&phba->fabric_iocb_list, &completions);
11918         spin_unlock_irq(&phba->hbalock);
11919
11920         /* Cancel all the IOCBs from the completions list */
11921         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11922                               IOERR_SLI_ABORTED);
11923 }
11924
11925 /**
11926  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
11927  * @vport: pointer to lpfc vport data structure.
11928  *
11929  * This routine is invoked by the vport cleanup for deletions and the cleanup
11930  * for an ndlp on removal.
11931  **/
11932 void
11933 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
11934 {
11935         struct lpfc_hba *phba = vport->phba;
11936         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11937         struct lpfc_nodelist *ndlp = NULL;
11938         unsigned long iflag = 0;
11939
11940         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11941         list_for_each_entry_safe(sglq_entry, sglq_next,
11942                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11943                 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport) {
11944                         lpfc_nlp_put(sglq_entry->ndlp);
11945                         ndlp = sglq_entry->ndlp;
11946                         sglq_entry->ndlp = NULL;
11947
11948                         /* If the xri on the abts_els_sgl list is for the Fport
11949                          * node and the vport is unloading, the xri aborted wcqe
11950                          * likely isn't coming back.  Just release the sgl.
11951                          */
11952                         if (test_bit(FC_UNLOADING, &vport->load_flag) &&
11953                             ndlp->nlp_DID == Fabric_DID) {
11954                                 list_del(&sglq_entry->list);
11955                                 sglq_entry->state = SGL_FREED;
11956                                 list_add_tail(&sglq_entry->list,
11957                                         &phba->sli4_hba.lpfc_els_sgl_list);
11958                         }
11959                 }
11960         }
11961         spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11962         return;
11963 }
11964
11965 /**
11966  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
11967  * @phba: pointer to lpfc hba data structure.
11968  * @axri: pointer to the els xri abort wcqe structure.
11969  *
11970  * This routine is invoked by the worker thread to process a SLI4 slow-path
11971  * ELS aborted xri.
11972  **/
11973 void
11974 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
11975                           struct sli4_wcqe_xri_aborted *axri)
11976 {
11977         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
11978         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
11979         uint16_t lxri = 0;
11980
11981         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11982         unsigned long iflag = 0;
11983         struct lpfc_nodelist *ndlp;
11984         struct lpfc_sli_ring *pring;
11985
11986         pring = lpfc_phba_elsring(phba);
11987
11988         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11989         list_for_each_entry_safe(sglq_entry, sglq_next,
11990                         &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11991                 if (sglq_entry->sli4_xritag == xri) {
11992                         list_del(&sglq_entry->list);
11993                         ndlp = sglq_entry->ndlp;
11994                         sglq_entry->ndlp = NULL;
11995                         list_add_tail(&sglq_entry->list,
11996                                 &phba->sli4_hba.lpfc_els_sgl_list);
11997                         sglq_entry->state = SGL_FREED;
11998                         spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock,
11999                                                iflag);
12000
12001                         if (ndlp) {
12002                                 lpfc_set_rrq_active(phba, ndlp,
12003                                         sglq_entry->sli4_lxritag,
12004                                         rxid, 1);
12005                                 lpfc_nlp_put(ndlp);
12006                         }
12007
12008                         /* Check if TXQ queue needs to be serviced */
12009                         if (pring && !list_empty(&pring->txq))
12010                                 lpfc_worker_wake_up(phba);
12011                         return;
12012                 }
12013         }
12014         spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
12015         lxri = lpfc_sli4_xri_inrange(phba, xri);
12016         if (lxri == NO_XRI)
12017                 return;
12018
12019         spin_lock_irqsave(&phba->hbalock, iflag);
12020         sglq_entry = __lpfc_get_active_sglq(phba, lxri);
12021         if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
12022                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12023                 return;
12024         }
12025         sglq_entry->state = SGL_XRI_ABORTED;
12026         spin_unlock_irqrestore(&phba->hbalock, iflag);
12027         return;
12028 }
12029
12030 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
12031  * @vport: pointer to virtual port object.
12032  * @ndlp: nodelist pointer for the impacted node.
12033  *
12034  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
12035  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
12036  * the driver is required to send a LOGO to the remote node before it
12037  * attempts to recover its login to the remote node.
12038  */
12039 void
12040 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
12041                            struct lpfc_nodelist *ndlp)
12042 {
12043         struct Scsi_Host *shost;
12044         struct lpfc_hba *phba;
12045         unsigned long flags = 0;
12046
12047         shost = lpfc_shost_from_vport(vport);
12048         phba = vport->phba;
12049         if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
12050                 lpfc_printf_log(phba, KERN_INFO,
12051                                 LOG_SLI, "3093 No rport recovery needed. "
12052                                 "rport in state 0x%x\n", ndlp->nlp_state);
12053                 return;
12054         }
12055         lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12056                         "3094 Start rport recovery on shost id 0x%x "
12057                         "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
12058                         "flag 0x%lx\n",
12059                         shost->host_no, ndlp->nlp_DID,
12060                         vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
12061                         ndlp->nlp_flag);
12062         /*
12063          * The rport is not responding.  Remove the FCP-2 flag to prevent
12064          * an ADISC in the follow-up recovery code.
12065          */
12066         spin_lock_irqsave(&ndlp->lock, flags);
12067         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
12068         spin_unlock_irqrestore(&ndlp->lock, flags);
12069         set_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
12070         lpfc_unreg_rpi(vport, ndlp);
12071 }
12072
12073 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport)
12074 {
12075         bitmap_zero(vport->vmid_priority_range, LPFC_VMID_MAX_PRIORITY_RANGE);
12076 }
12077
12078 static void
12079 lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max)
12080 {
12081         u32 i;
12082
12083         if ((min > max) || (max > LPFC_VMID_MAX_PRIORITY_RANGE))
12084                 return;
12085
12086         for (i = min; i <= max; i++)
12087                 set_bit(i, vport->vmid_priority_range);
12088 }
12089
12090 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid)
12091 {
12092         set_bit(ctcl_vmid, vport->vmid_priority_range);
12093 }
12094
12095 u32 lpfc_vmid_get_cs_ctl(struct lpfc_vport *vport)
12096 {
12097         u32 i;
12098
12099         i = find_first_bit(vport->vmid_priority_range,
12100                            LPFC_VMID_MAX_PRIORITY_RANGE);
12101
12102         if (i == LPFC_VMID_MAX_PRIORITY_RANGE)
12103                 return 0;
12104
12105         clear_bit(i, vport->vmid_priority_range);
12106         return i;
12107 }
12108
12109 #define MAX_PRIORITY_DESC       255
12110
12111 static void
12112 lpfc_cmpl_els_qfpa(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12113                    struct lpfc_iocbq *rspiocb)
12114 {
12115         struct lpfc_vport *vport = cmdiocb->vport;
12116         struct priority_range_desc *desc;
12117         struct lpfc_dmabuf *prsp = NULL;
12118         struct lpfc_vmid_priority_range *vmid_range = NULL;
12119         u32 *data;
12120         struct lpfc_dmabuf *dmabuf = cmdiocb->cmd_dmabuf;
12121         u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12122         u32 ulp_word4 = get_job_word4(phba, rspiocb);
12123         u8 *pcmd, max_desc;
12124         u32 len, i;
12125         struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12126
12127         prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12128         if (!prsp)
12129                 goto out;
12130
12131         pcmd = prsp->virt;
12132         data = (u32 *)pcmd;
12133         if (data[0] == ELS_CMD_LS_RJT) {
12134                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12135                                  "3277 QFPA LS_RJT x%x  x%x\n",
12136                                  data[0], data[1]);
12137                 goto out;
12138         }
12139         if (ulp_status) {
12140                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
12141                                  "6529 QFPA failed with status x%x  x%x\n",
12142                                  ulp_status, ulp_word4);
12143                 goto out;
12144         }
12145
12146         if (!vport->qfpa_res) {
12147                 max_desc = FCELSSIZE / sizeof(*vport->qfpa_res);
12148                 vport->qfpa_res = kcalloc(max_desc, sizeof(*vport->qfpa_res),
12149                                           GFP_KERNEL);
12150                 if (!vport->qfpa_res)
12151                         goto out;
12152         }
12153
12154         len = *((u32 *)(pcmd + 4));
12155         len = be32_to_cpu(len);
12156         memcpy(vport->qfpa_res, pcmd, len + 8);
12157         len = len / LPFC_PRIORITY_RANGE_DESC_SIZE;
12158
12159         desc = (struct priority_range_desc *)(pcmd + 8);
12160         vmid_range = vport->vmid_priority.vmid_range;
12161         if (!vmid_range) {
12162                 vmid_range = kcalloc(MAX_PRIORITY_DESC, sizeof(*vmid_range),
12163                                      GFP_KERNEL);
12164                 if (!vmid_range) {
12165                         kfree(vport->qfpa_res);
12166                         goto out;
12167                 }
12168                 vport->vmid_priority.vmid_range = vmid_range;
12169         }
12170         vport->vmid_priority.num_descriptors = len;
12171
12172         for (i = 0; i < len; i++, vmid_range++, desc++) {
12173                 lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12174                                  "6539 vmid values low=%d, high=%d, qos=%d, "
12175                                  "local ve id=%d\n", desc->lo_range,
12176                                  desc->hi_range, desc->qos_priority,
12177                                  desc->local_ve_id);
12178
12179                 vmid_range->low = desc->lo_range << 1;
12180                 if (desc->local_ve_id == QFPA_ODD_ONLY)
12181                         vmid_range->low++;
12182                 if (desc->qos_priority)
12183                         vport->vmid_flag |= LPFC_VMID_QOS_ENABLED;
12184                 vmid_range->qos = desc->qos_priority;
12185
12186                 vmid_range->high = desc->hi_range << 1;
12187                 if ((desc->local_ve_id == QFPA_ODD_ONLY) ||
12188                     (desc->local_ve_id == QFPA_EVEN_ODD))
12189                         vmid_range->high++;
12190         }
12191         lpfc_init_cs_ctl_bitmap(vport);
12192         for (i = 0; i < vport->vmid_priority.num_descriptors; i++) {
12193                 lpfc_vmid_set_cs_ctl_range(vport,
12194                                 vport->vmid_priority.vmid_range[i].low,
12195                                 vport->vmid_priority.vmid_range[i].high);
12196         }
12197
12198         vport->vmid_flag |= LPFC_VMID_QFPA_CMPL;
12199  out:
12200         lpfc_els_free_iocb(phba, cmdiocb);
12201         lpfc_nlp_put(ndlp);
12202 }
12203
12204 int lpfc_issue_els_qfpa(struct lpfc_vport *vport)
12205 {
12206         struct lpfc_hba *phba = vport->phba;
12207         struct lpfc_nodelist *ndlp;
12208         struct lpfc_iocbq *elsiocb;
12209         u8 *pcmd;
12210         int ret;
12211
12212         ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
12213         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12214                 return -ENXIO;
12215
12216         elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_QFPA_SIZE, 2, ndlp,
12217                                      ndlp->nlp_DID, ELS_CMD_QFPA);
12218         if (!elsiocb)
12219                 return -ENOMEM;
12220
12221         pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12222
12223         *((u32 *)(pcmd)) = ELS_CMD_QFPA;
12224         pcmd += 4;
12225
12226         elsiocb->cmd_cmpl = lpfc_cmpl_els_qfpa;
12227
12228         elsiocb->ndlp = lpfc_nlp_get(ndlp);
12229         if (!elsiocb->ndlp) {
12230                 lpfc_els_free_iocb(vport->phba, elsiocb);
12231                 return -ENXIO;
12232         }
12233
12234         ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 2);
12235         if (ret != IOCB_SUCCESS) {
12236                 lpfc_els_free_iocb(phba, elsiocb);
12237                 lpfc_nlp_put(ndlp);
12238                 return -EIO;
12239         }
12240         vport->vmid_flag &= ~LPFC_VMID_QOS_ENABLED;
12241         return 0;
12242 }
12243
12244 int
12245 lpfc_vmid_uvem(struct lpfc_vport *vport,
12246                struct lpfc_vmid *vmid, bool instantiated)
12247 {
12248         struct lpfc_vem_id_desc *vem_id_desc;
12249         struct lpfc_nodelist *ndlp;
12250         struct lpfc_iocbq *elsiocb;
12251         struct instantiated_ve_desc *inst_desc;
12252         struct lpfc_vmid_context *vmid_context;
12253         u8 *pcmd;
12254         u32 *len;
12255         int ret = 0;
12256
12257         ndlp = lpfc_findnode_did(vport, Fabric_DID);
12258         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12259                 return -ENXIO;
12260
12261         vmid_context = kmalloc(sizeof(*vmid_context), GFP_KERNEL);
12262         if (!vmid_context)
12263                 return -ENOMEM;
12264         elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_UVEM_SIZE, 2,
12265                                      ndlp, Fabric_DID, ELS_CMD_UVEM);
12266         if (!elsiocb)
12267                 goto out;
12268
12269         lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12270                          "3427 Host vmid %s %d\n",
12271                          vmid->host_vmid, instantiated);
12272         vmid_context->vmp = vmid;
12273         vmid_context->nlp = ndlp;
12274         vmid_context->instantiated = instantiated;
12275         elsiocb->vmid_tag.vmid_context = vmid_context;
12276         pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12277
12278         if (!memchr_inv(vport->lpfc_vmid_host_uuid, 0,
12279                         sizeof(vport->lpfc_vmid_host_uuid)))
12280                 memcpy(vport->lpfc_vmid_host_uuid, vmid->host_vmid,
12281                        sizeof(vport->lpfc_vmid_host_uuid));
12282
12283         *((u32 *)(pcmd)) = ELS_CMD_UVEM;
12284         len = (u32 *)(pcmd + 4);
12285         *len = cpu_to_be32(LPFC_UVEM_SIZE - 8);
12286
12287         vem_id_desc = (struct lpfc_vem_id_desc *)(pcmd + 8);
12288         vem_id_desc->tag = be32_to_cpu(VEM_ID_DESC_TAG);
12289         vem_id_desc->length = be32_to_cpu(LPFC_UVEM_VEM_ID_DESC_SIZE);
12290         memcpy(vem_id_desc->vem_id, vport->lpfc_vmid_host_uuid,
12291                sizeof(vem_id_desc->vem_id));
12292
12293         inst_desc = (struct instantiated_ve_desc *)(pcmd + 32);
12294         inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12295         inst_desc->length = be32_to_cpu(LPFC_UVEM_VE_MAP_DESC_SIZE);
12296         memcpy(inst_desc->global_vem_id, vmid->host_vmid,
12297                sizeof(inst_desc->global_vem_id));
12298
12299         bf_set(lpfc_instantiated_nport_id, inst_desc, vport->fc_myDID);
12300         bf_set(lpfc_instantiated_local_id, inst_desc,
12301                vmid->un.cs_ctl_vmid);
12302         if (instantiated) {
12303                 inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12304         } else {
12305                 inst_desc->tag = be32_to_cpu(DEINSTANTIATED_VE_DESC_TAG);
12306                 lpfc_vmid_put_cs_ctl(vport, vmid->un.cs_ctl_vmid);
12307         }
12308         inst_desc->word6 = cpu_to_be32(inst_desc->word6);
12309
12310         elsiocb->cmd_cmpl = lpfc_cmpl_els_uvem;
12311
12312         elsiocb->ndlp = lpfc_nlp_get(ndlp);
12313         if (!elsiocb->ndlp) {
12314                 lpfc_els_free_iocb(vport->phba, elsiocb);
12315                 goto out;
12316         }
12317
12318         ret = lpfc_sli_issue_iocb(vport->phba, LPFC_ELS_RING, elsiocb, 0);
12319         if (ret != IOCB_SUCCESS) {
12320                 lpfc_els_free_iocb(vport->phba, elsiocb);
12321                 lpfc_nlp_put(ndlp);
12322                 goto out;
12323         }
12324
12325         return 0;
12326  out:
12327         kfree(vmid_context);
12328         return -EIO;
12329 }
12330
12331 static void
12332 lpfc_cmpl_els_uvem(struct lpfc_hba *phba, struct lpfc_iocbq *icmdiocb,
12333                    struct lpfc_iocbq *rspiocb)
12334 {
12335         struct lpfc_vport *vport = icmdiocb->vport;
12336         struct lpfc_dmabuf *prsp = NULL;
12337         struct lpfc_vmid_context *vmid_context =
12338             icmdiocb->vmid_tag.vmid_context;
12339         struct lpfc_nodelist *ndlp = icmdiocb->ndlp;
12340         u8 *pcmd;
12341         u32 *data;
12342         u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12343         u32 ulp_word4 = get_job_word4(phba, rspiocb);
12344         struct lpfc_dmabuf *dmabuf = icmdiocb->cmd_dmabuf;
12345         struct lpfc_vmid *vmid;
12346
12347         vmid = vmid_context->vmp;
12348         if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12349                 ndlp = NULL;
12350
12351         prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12352         if (!prsp)
12353                 goto out;
12354         pcmd = prsp->virt;
12355         data = (u32 *)pcmd;
12356         if (data[0] == ELS_CMD_LS_RJT) {
12357                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12358                                  "4532 UVEM LS_RJT %x %x\n", data[0], data[1]);
12359                 goto out;
12360         }
12361         if (ulp_status) {
12362                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12363                                  "4533 UVEM error status %x: %x\n",
12364                                  ulp_status, ulp_word4);
12365                 goto out;
12366         }
12367         spin_lock(&phba->hbalock);
12368         /* Set IN USE flag */
12369         vport->vmid_flag |= LPFC_VMID_IN_USE;
12370         phba->pport->vmid_flag |= LPFC_VMID_IN_USE;
12371         spin_unlock(&phba->hbalock);
12372
12373         if (vmid_context->instantiated) {
12374                 write_lock(&vport->vmid_lock);
12375                 vmid->flag |= LPFC_VMID_REGISTERED;
12376                 vmid->flag &= ~LPFC_VMID_REQ_REGISTER;
12377                 write_unlock(&vport->vmid_lock);
12378         }
12379
12380  out:
12381         kfree(vmid_context);
12382         lpfc_els_free_iocb(phba, icmdiocb);
12383         lpfc_nlp_put(ndlp);
12384 }
This page took 0.763155 seconds and 4 git commands to generate.