162306a36Sopenharmony_ci/*******************************************************************
262306a36Sopenharmony_ci * This file is part of the Emulex Linux Device Driver for         *
362306a36Sopenharmony_ci * Fibre Channel Host Bus Adapters.                                *
462306a36Sopenharmony_ci * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
562306a36Sopenharmony_ci * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
662306a36Sopenharmony_ci * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
762306a36Sopenharmony_ci * EMULEX and SLI are trademarks of Emulex.                        *
862306a36Sopenharmony_ci * www.broadcom.com                                                *
962306a36Sopenharmony_ci * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
1062306a36Sopenharmony_ci *                                                                 *
1162306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or   *
1262306a36Sopenharmony_ci * modify it under the terms of version 2 of the GNU General       *
1362306a36Sopenharmony_ci * Public License as published by the Free Software Foundation.    *
1462306a36Sopenharmony_ci * This program is distributed in the hope that it will be useful. *
1562306a36Sopenharmony_ci * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
1662306a36Sopenharmony_ci * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
1762306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
1862306a36Sopenharmony_ci * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
1962306a36Sopenharmony_ci * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
2062306a36Sopenharmony_ci * more details, a copy of which can be found in the file COPYING  *
2162306a36Sopenharmony_ci * included with this package.                                     *
2262306a36Sopenharmony_ci *******************************************************************/
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include <linux/blkdev.h>
2562306a36Sopenharmony_ci#include <linux/pci.h>
2662306a36Sopenharmony_ci#include <linux/slab.h>
2762306a36Sopenharmony_ci#include <linux/interrupt.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <scsi/scsi_device.h>
3062306a36Sopenharmony_ci#include <scsi/scsi_transport_fc.h>
3162306a36Sopenharmony_ci#include <scsi/scsi.h>
3262306a36Sopenharmony_ci#include <scsi/fc/fc_fs.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include "lpfc_hw4.h"
3562306a36Sopenharmony_ci#include "lpfc_hw.h"
3662306a36Sopenharmony_ci#include "lpfc_sli.h"
3762306a36Sopenharmony_ci#include "lpfc_sli4.h"
3862306a36Sopenharmony_ci#include "lpfc_nl.h"
3962306a36Sopenharmony_ci#include "lpfc_disc.h"
4062306a36Sopenharmony_ci#include "lpfc_scsi.h"
4162306a36Sopenharmony_ci#include "lpfc.h"
4262306a36Sopenharmony_ci#include "lpfc_logmsg.h"
4362306a36Sopenharmony_ci#include "lpfc_crtn.h"
4462306a36Sopenharmony_ci#include "lpfc_compat.h"
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/**
4762306a36Sopenharmony_ci * lpfc_mbox_rsrc_prep - Prepare a mailbox with DMA buffer memory.
4862306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
4962306a36Sopenharmony_ci * @mbox: pointer to the driver internal queue element for mailbox command.
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * A mailbox command consists of the pool memory for the command, @mbox, and
5262306a36Sopenharmony_ci * one or more DMA buffers for the data transfer.  This routine provides
5362306a36Sopenharmony_ci * a standard framework for allocating the dma buffer and assigning to the
5462306a36Sopenharmony_ci * @mbox.  Callers should cleanup the mbox with a call to
5562306a36Sopenharmony_ci * lpfc_mbox_rsrc_cleanup.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * The lpfc_mbuf_alloc routine acquires the hbalock so the caller is
5862306a36Sopenharmony_ci * responsible to ensure the hbalock is released.  Also note that the
5962306a36Sopenharmony_ci * driver design is a single dmabuf/mbuf per mbox in the ctx_buf.
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci **/
6262306a36Sopenharmony_ciint
6362306a36Sopenharmony_cilpfc_mbox_rsrc_prep(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	struct lpfc_dmabuf *mp;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	mp = kmalloc(sizeof(*mp), GFP_KERNEL);
6862306a36Sopenharmony_ci	if (!mp)
6962306a36Sopenharmony_ci		return -ENOMEM;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
7262306a36Sopenharmony_ci	if (!mp->virt) {
7362306a36Sopenharmony_ci		kfree(mp);
7462306a36Sopenharmony_ci		return -ENOMEM;
7562306a36Sopenharmony_ci	}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	memset(mp->virt, 0, LPFC_BPL_SIZE);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* Initialization only.  Driver does not use a list of dmabufs. */
8062306a36Sopenharmony_ci	INIT_LIST_HEAD(&mp->list);
8162306a36Sopenharmony_ci	mbox->ctx_buf = mp;
8262306a36Sopenharmony_ci	return 0;
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/**
8662306a36Sopenharmony_ci * lpfc_mbox_rsrc_cleanup - Free the mailbox DMA buffer and virtual memory.
8762306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
8862306a36Sopenharmony_ci * @mbox: pointer to the driver internal queue element for mailbox command.
8962306a36Sopenharmony_ci * @locked: value that indicates if the hbalock is held (1) or not (0).
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * A mailbox command consists of the pool memory for the command, @mbox, and
9262306a36Sopenharmony_ci * possibly a DMA buffer for the data transfer.  This routine provides
9362306a36Sopenharmony_ci * a standard framework for releasing any dma buffers and freeing all
9462306a36Sopenharmony_ci * memory resources in it as well as releasing the @mbox back to the @phba pool.
9562306a36Sopenharmony_ci * Callers should use this routine for cleanup for all mailboxes prepped with
9662306a36Sopenharmony_ci * lpfc_mbox_rsrc_prep.
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci **/
9962306a36Sopenharmony_civoid
10062306a36Sopenharmony_cilpfc_mbox_rsrc_cleanup(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
10162306a36Sopenharmony_ci		       enum lpfc_mbox_ctx locked)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	struct lpfc_dmabuf *mp;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
10662306a36Sopenharmony_ci	mbox->ctx_buf = NULL;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	/* Release the generic BPL buffer memory.  */
10962306a36Sopenharmony_ci	if (mp) {
11062306a36Sopenharmony_ci		if (locked == MBOX_THD_LOCKED)
11162306a36Sopenharmony_ci			__lpfc_mbuf_free(phba, mp->virt, mp->phys);
11262306a36Sopenharmony_ci		else
11362306a36Sopenharmony_ci			lpfc_mbuf_free(phba, mp->virt, mp->phys);
11462306a36Sopenharmony_ci		kfree(mp);
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	mempool_free(mbox, phba->mbox_mem_pool);
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/**
12162306a36Sopenharmony_ci * lpfc_dump_static_vport - Dump HBA's static vport information.
12262306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
12362306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
12462306a36Sopenharmony_ci * @offset: offset for dumping vport info.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * The dump mailbox command provides a method for the device driver to obtain
12762306a36Sopenharmony_ci * various types of information from the HBA device.
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci * This routine prepares the mailbox command for dumping list of static
13062306a36Sopenharmony_ci * vports to be created.
13162306a36Sopenharmony_ci **/
13262306a36Sopenharmony_ciint
13362306a36Sopenharmony_cilpfc_dump_static_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
13462306a36Sopenharmony_ci		uint16_t offset)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	MAILBOX_t *mb;
13762306a36Sopenharmony_ci	struct lpfc_dmabuf *mp;
13862306a36Sopenharmony_ci	int rc;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	mb = &pmb->u.mb;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	/* Setup to dump vport info region */
14362306a36Sopenharmony_ci	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
14462306a36Sopenharmony_ci	mb->mbxCommand = MBX_DUMP_MEMORY;
14562306a36Sopenharmony_ci	mb->un.varDmp.type = DMP_NV_PARAMS;
14662306a36Sopenharmony_ci	mb->un.varDmp.entry_index = offset;
14762306a36Sopenharmony_ci	mb->un.varDmp.region_id = DMP_REGION_VPORT;
14862306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	/* For SLI3 HBAs data is embedded in mailbox */
15162306a36Sopenharmony_ci	if (phba->sli_rev != LPFC_SLI_REV4) {
15262306a36Sopenharmony_ci		mb->un.varDmp.cv = 1;
15362306a36Sopenharmony_ci		mb->un.varDmp.word_cnt = DMP_RSP_SIZE/sizeof(uint32_t);
15462306a36Sopenharmony_ci		return 0;
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	rc = lpfc_mbox_rsrc_prep(phba, pmb);
15862306a36Sopenharmony_ci	if (rc) {
15962306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
16062306a36Sopenharmony_ci				"2605 %s: memory allocation failed\n",
16162306a36Sopenharmony_ci				__func__);
16262306a36Sopenharmony_ci		return 1;
16362306a36Sopenharmony_ci	}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	mp = pmb->ctx_buf;
16662306a36Sopenharmony_ci	mb->un.varWords[3] = putPaddrLow(mp->phys);
16762306a36Sopenharmony_ci	mb->un.varWords[4] = putPaddrHigh(mp->phys);
16862306a36Sopenharmony_ci	mb->un.varDmp.sli4_length = sizeof(struct static_vport_info);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	return 0;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/**
17462306a36Sopenharmony_ci * lpfc_down_link - Bring down HBAs link.
17562306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
17662306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
17762306a36Sopenharmony_ci *
17862306a36Sopenharmony_ci * This routine prepares a mailbox command to bring down HBA link.
17962306a36Sopenharmony_ci **/
18062306a36Sopenharmony_civoid
18162306a36Sopenharmony_cilpfc_down_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	MAILBOX_t *mb;
18462306a36Sopenharmony_ci	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
18562306a36Sopenharmony_ci	mb = &pmb->u.mb;
18662306a36Sopenharmony_ci	mb->mbxCommand = MBX_DOWN_LINK;
18762306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
18862306a36Sopenharmony_ci}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci/**
19162306a36Sopenharmony_ci * lpfc_dump_mem - Prepare a mailbox command for reading a region.
19262306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
19362306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
19462306a36Sopenharmony_ci * @offset: offset into the region.
19562306a36Sopenharmony_ci * @region_id: config region id.
19662306a36Sopenharmony_ci *
19762306a36Sopenharmony_ci * The dump mailbox command provides a method for the device driver to obtain
19862306a36Sopenharmony_ci * various types of information from the HBA device.
19962306a36Sopenharmony_ci *
20062306a36Sopenharmony_ci * This routine prepares the mailbox command for dumping HBA's config region.
20162306a36Sopenharmony_ci **/
20262306a36Sopenharmony_civoid
20362306a36Sopenharmony_cilpfc_dump_mem(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, uint16_t offset,
20462306a36Sopenharmony_ci		uint16_t region_id)
20562306a36Sopenharmony_ci{
20662306a36Sopenharmony_ci	MAILBOX_t *mb;
20762306a36Sopenharmony_ci	void *ctx;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	mb = &pmb->u.mb;
21062306a36Sopenharmony_ci	ctx = pmb->ctx_buf;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	/* Setup to dump VPD region */
21362306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
21462306a36Sopenharmony_ci	mb->mbxCommand = MBX_DUMP_MEMORY;
21562306a36Sopenharmony_ci	mb->un.varDmp.cv = 1;
21662306a36Sopenharmony_ci	mb->un.varDmp.type = DMP_NV_PARAMS;
21762306a36Sopenharmony_ci	mb->un.varDmp.entry_index = offset;
21862306a36Sopenharmony_ci	mb->un.varDmp.region_id = region_id;
21962306a36Sopenharmony_ci	mb->un.varDmp.word_cnt = (DMP_RSP_SIZE / sizeof (uint32_t));
22062306a36Sopenharmony_ci	mb->un.varDmp.co = 0;
22162306a36Sopenharmony_ci	mb->un.varDmp.resp_offset = 0;
22262306a36Sopenharmony_ci	pmb->ctx_buf = ctx;
22362306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
22462306a36Sopenharmony_ci	return;
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci/**
22862306a36Sopenharmony_ci * lpfc_dump_wakeup_param - Prepare mailbox command for retrieving wakeup params
22962306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
23062306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
23162306a36Sopenharmony_ci *
23262306a36Sopenharmony_ci * This function create a dump memory mailbox command to dump wake up
23362306a36Sopenharmony_ci * parameters.
23462306a36Sopenharmony_ci */
23562306a36Sopenharmony_civoid
23662306a36Sopenharmony_cilpfc_dump_wakeup_param(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
23762306a36Sopenharmony_ci{
23862306a36Sopenharmony_ci	MAILBOX_t *mb;
23962306a36Sopenharmony_ci	void *ctx;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	mb = &pmb->u.mb;
24262306a36Sopenharmony_ci	/* Save context so that we can restore after memset */
24362306a36Sopenharmony_ci	ctx = pmb->ctx_buf;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* Setup to dump VPD region */
24662306a36Sopenharmony_ci	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
24762306a36Sopenharmony_ci	mb->mbxCommand = MBX_DUMP_MEMORY;
24862306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
24962306a36Sopenharmony_ci	mb->un.varDmp.cv = 1;
25062306a36Sopenharmony_ci	mb->un.varDmp.type = DMP_NV_PARAMS;
25162306a36Sopenharmony_ci	if (phba->sli_rev < LPFC_SLI_REV4)
25262306a36Sopenharmony_ci		mb->un.varDmp.entry_index = 0;
25362306a36Sopenharmony_ci	mb->un.varDmp.region_id = WAKE_UP_PARMS_REGION_ID;
25462306a36Sopenharmony_ci	mb->un.varDmp.word_cnt = WAKE_UP_PARMS_WORD_SIZE;
25562306a36Sopenharmony_ci	mb->un.varDmp.co = 0;
25662306a36Sopenharmony_ci	mb->un.varDmp.resp_offset = 0;
25762306a36Sopenharmony_ci	pmb->ctx_buf = ctx;
25862306a36Sopenharmony_ci	return;
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci/**
26262306a36Sopenharmony_ci * lpfc_read_nv - Prepare a mailbox command for reading HBA's NVRAM param
26362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
26462306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
26562306a36Sopenharmony_ci *
26662306a36Sopenharmony_ci * The read NVRAM mailbox command returns the HBA's non-volatile parameters
26762306a36Sopenharmony_ci * that are used as defaults when the Fibre Channel link is brought on-line.
26862306a36Sopenharmony_ci *
26962306a36Sopenharmony_ci * This routine prepares the mailbox command for reading information stored
27062306a36Sopenharmony_ci * in the HBA's NVRAM. Specifically, the HBA's WWNN and WWPN.
27162306a36Sopenharmony_ci **/
27262306a36Sopenharmony_civoid
27362306a36Sopenharmony_cilpfc_read_nv(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
27462306a36Sopenharmony_ci{
27562306a36Sopenharmony_ci	MAILBOX_t *mb;
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	mb = &pmb->u.mb;
27862306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
27962306a36Sopenharmony_ci	mb->mbxCommand = MBX_READ_NV;
28062306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
28162306a36Sopenharmony_ci	return;
28262306a36Sopenharmony_ci}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci/**
28562306a36Sopenharmony_ci * lpfc_config_async - Prepare a mailbox command for enabling HBA async event
28662306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
28762306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
28862306a36Sopenharmony_ci * @ring: ring number for the asynchronous event to be configured.
28962306a36Sopenharmony_ci *
29062306a36Sopenharmony_ci * The asynchronous event enable mailbox command is used to enable the
29162306a36Sopenharmony_ci * asynchronous event posting via the ASYNC_STATUS_CN IOCB response and
29262306a36Sopenharmony_ci * specifies the default ring to which events are posted.
29362306a36Sopenharmony_ci *
29462306a36Sopenharmony_ci * This routine prepares the mailbox command for enabling HBA asynchronous
29562306a36Sopenharmony_ci * event support on a IOCB ring.
29662306a36Sopenharmony_ci **/
29762306a36Sopenharmony_civoid
29862306a36Sopenharmony_cilpfc_config_async(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb,
29962306a36Sopenharmony_ci		uint32_t ring)
30062306a36Sopenharmony_ci{
30162306a36Sopenharmony_ci	MAILBOX_t *mb;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	mb = &pmb->u.mb;
30462306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
30562306a36Sopenharmony_ci	mb->mbxCommand = MBX_ASYNCEVT_ENABLE;
30662306a36Sopenharmony_ci	mb->un.varCfgAsyncEvent.ring = ring;
30762306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
30862306a36Sopenharmony_ci	return;
30962306a36Sopenharmony_ci}
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci/**
31262306a36Sopenharmony_ci * lpfc_heart_beat - Prepare a mailbox command for heart beat
31362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
31462306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
31562306a36Sopenharmony_ci *
31662306a36Sopenharmony_ci * The heart beat mailbox command is used to detect an unresponsive HBA, which
31762306a36Sopenharmony_ci * is defined as any device where no error attention is sent and both mailbox
31862306a36Sopenharmony_ci * and rings are not processed.
31962306a36Sopenharmony_ci *
32062306a36Sopenharmony_ci * This routine prepares the mailbox command for issuing a heart beat in the
32162306a36Sopenharmony_ci * form of mailbox command to the HBA. The timely completion of the heart
32262306a36Sopenharmony_ci * beat mailbox command indicates the health of the HBA.
32362306a36Sopenharmony_ci **/
32462306a36Sopenharmony_civoid
32562306a36Sopenharmony_cilpfc_heart_beat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
32662306a36Sopenharmony_ci{
32762306a36Sopenharmony_ci	MAILBOX_t *mb;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	mb = &pmb->u.mb;
33062306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
33162306a36Sopenharmony_ci	mb->mbxCommand = MBX_HEARTBEAT;
33262306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
33362306a36Sopenharmony_ci	return;
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci/**
33762306a36Sopenharmony_ci * lpfc_read_topology - Prepare a mailbox command for reading HBA topology
33862306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
33962306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
34062306a36Sopenharmony_ci * @mp: DMA buffer memory for reading the link attention information into.
34162306a36Sopenharmony_ci *
34262306a36Sopenharmony_ci * The read topology mailbox command is issued to read the link topology
34362306a36Sopenharmony_ci * information indicated by the HBA port when the Link Event bit of the Host
34462306a36Sopenharmony_ci * Attention (HSTATT) register is set to 1 (For SLI-3) or when an FC Link
34562306a36Sopenharmony_ci * Attention ACQE is received from the port (For SLI-4). A Link Event
34662306a36Sopenharmony_ci * Attention occurs based on an exception detected at the Fibre Channel link
34762306a36Sopenharmony_ci * interface.
34862306a36Sopenharmony_ci *
34962306a36Sopenharmony_ci * This routine prepares the mailbox command for reading HBA link topology
35062306a36Sopenharmony_ci * information. A DMA memory has been set aside and address passed to the
35162306a36Sopenharmony_ci * HBA through @mp for the HBA to DMA link attention information into the
35262306a36Sopenharmony_ci * memory as part of the execution of the mailbox command.
35362306a36Sopenharmony_ci *
35462306a36Sopenharmony_ci * Return codes
35562306a36Sopenharmony_ci *    0 - Success (currently always return 0)
35662306a36Sopenharmony_ci **/
35762306a36Sopenharmony_ciint
35862306a36Sopenharmony_cilpfc_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb,
35962306a36Sopenharmony_ci		   struct lpfc_dmabuf *mp)
36062306a36Sopenharmony_ci{
36162306a36Sopenharmony_ci	MAILBOX_t *mb;
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	mb = &pmb->u.mb;
36462306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	INIT_LIST_HEAD(&mp->list);
36762306a36Sopenharmony_ci	mb->mbxCommand = MBX_READ_TOPOLOGY;
36862306a36Sopenharmony_ci	mb->un.varReadTop.lilpBde64.tus.f.bdeSize = LPFC_ALPA_MAP_SIZE;
36962306a36Sopenharmony_ci	mb->un.varReadTop.lilpBde64.addrHigh = putPaddrHigh(mp->phys);
37062306a36Sopenharmony_ci	mb->un.varReadTop.lilpBde64.addrLow = putPaddrLow(mp->phys);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	/* Save address for later completion and set the owner to host so that
37362306a36Sopenharmony_ci	 * the FW knows this mailbox is available for processing.
37462306a36Sopenharmony_ci	 */
37562306a36Sopenharmony_ci	pmb->ctx_buf = (uint8_t *)mp;
37662306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
37762306a36Sopenharmony_ci	return (0);
37862306a36Sopenharmony_ci}
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci/**
38162306a36Sopenharmony_ci * lpfc_clear_la - Prepare a mailbox command for clearing HBA link attention
38262306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
38362306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
38462306a36Sopenharmony_ci *
38562306a36Sopenharmony_ci * The clear link attention mailbox command is issued to clear the link event
38662306a36Sopenharmony_ci * attention condition indicated by the Link Event bit of the Host Attention
38762306a36Sopenharmony_ci * (HSTATT) register. The link event attention condition is cleared only if
38862306a36Sopenharmony_ci * the event tag specified matches that of the current link event counter.
38962306a36Sopenharmony_ci * The current event tag is read using the read link attention event mailbox
39062306a36Sopenharmony_ci * command.
39162306a36Sopenharmony_ci *
39262306a36Sopenharmony_ci * This routine prepares the mailbox command for clearing HBA link attention
39362306a36Sopenharmony_ci * information.
39462306a36Sopenharmony_ci **/
39562306a36Sopenharmony_civoid
39662306a36Sopenharmony_cilpfc_clear_la(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
39762306a36Sopenharmony_ci{
39862306a36Sopenharmony_ci	MAILBOX_t *mb;
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci	mb = &pmb->u.mb;
40162306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	mb->un.varClearLA.eventTag = phba->fc_eventTag;
40462306a36Sopenharmony_ci	mb->mbxCommand = MBX_CLEAR_LA;
40562306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
40662306a36Sopenharmony_ci	return;
40762306a36Sopenharmony_ci}
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci/**
41062306a36Sopenharmony_ci * lpfc_config_link - Prepare a mailbox command for configuring link on a HBA
41162306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
41262306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
41362306a36Sopenharmony_ci *
41462306a36Sopenharmony_ci * The configure link mailbox command is used before the initialize link
41562306a36Sopenharmony_ci * mailbox command to override default value and to configure link-oriented
41662306a36Sopenharmony_ci * parameters such as DID address and various timers. Typically, this
41762306a36Sopenharmony_ci * command would be used after an F_Port login to set the returned DID address
41862306a36Sopenharmony_ci * and the fabric timeout values. This command is not valid before a configure
41962306a36Sopenharmony_ci * port command has configured the HBA port.
42062306a36Sopenharmony_ci *
42162306a36Sopenharmony_ci * This routine prepares the mailbox command for configuring link on a HBA.
42262306a36Sopenharmony_ci **/
42362306a36Sopenharmony_civoid
42462306a36Sopenharmony_cilpfc_config_link(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
42562306a36Sopenharmony_ci{
42662306a36Sopenharmony_ci	struct lpfc_vport  *vport = phba->pport;
42762306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
42862306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci	/* NEW_FEATURE
43162306a36Sopenharmony_ci	 * SLI-2, Coalescing Response Feature.
43262306a36Sopenharmony_ci	 */
43362306a36Sopenharmony_ci	if (phba->cfg_cr_delay && (phba->sli_rev < LPFC_SLI_REV4)) {
43462306a36Sopenharmony_ci		mb->un.varCfgLnk.cr = 1;
43562306a36Sopenharmony_ci		mb->un.varCfgLnk.ci = 1;
43662306a36Sopenharmony_ci		mb->un.varCfgLnk.cr_delay = phba->cfg_cr_delay;
43762306a36Sopenharmony_ci		mb->un.varCfgLnk.cr_count = phba->cfg_cr_count;
43862306a36Sopenharmony_ci	}
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci	mb->un.varCfgLnk.myId = vport->fc_myDID;
44162306a36Sopenharmony_ci	mb->un.varCfgLnk.edtov = phba->fc_edtov;
44262306a36Sopenharmony_ci	mb->un.varCfgLnk.arbtov = phba->fc_arbtov;
44362306a36Sopenharmony_ci	mb->un.varCfgLnk.ratov = phba->fc_ratov;
44462306a36Sopenharmony_ci	mb->un.varCfgLnk.rttov = phba->fc_rttov;
44562306a36Sopenharmony_ci	mb->un.varCfgLnk.altov = phba->fc_altov;
44662306a36Sopenharmony_ci	mb->un.varCfgLnk.crtov = phba->fc_crtov;
44762306a36Sopenharmony_ci	mb->un.varCfgLnk.cscn = 0;
44862306a36Sopenharmony_ci	if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
44962306a36Sopenharmony_ci		mb->un.varCfgLnk.cscn = 1;
45062306a36Sopenharmony_ci		mb->un.varCfgLnk.bbscn = bf_get(lpfc_bbscn_def,
45162306a36Sopenharmony_ci						 &phba->sli4_hba.bbscn_params);
45262306a36Sopenharmony_ci	}
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci	if (phba->cfg_ack0 && (phba->sli_rev < LPFC_SLI_REV4))
45562306a36Sopenharmony_ci		mb->un.varCfgLnk.ack0_enable = 1;
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	mb->mbxCommand = MBX_CONFIG_LINK;
45862306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
45962306a36Sopenharmony_ci	return;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci/**
46362306a36Sopenharmony_ci * lpfc_config_msi - Prepare a mailbox command for configuring msi-x
46462306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
46562306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
46662306a36Sopenharmony_ci *
46762306a36Sopenharmony_ci * The configure MSI-X mailbox command is used to configure the HBA's SLI-3
46862306a36Sopenharmony_ci * MSI-X multi-message interrupt vector association to interrupt attention
46962306a36Sopenharmony_ci * conditions.
47062306a36Sopenharmony_ci *
47162306a36Sopenharmony_ci * Return codes
47262306a36Sopenharmony_ci *    0 - Success
47362306a36Sopenharmony_ci *    -EINVAL - Failure
47462306a36Sopenharmony_ci **/
47562306a36Sopenharmony_ciint
47662306a36Sopenharmony_cilpfc_config_msi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
47762306a36Sopenharmony_ci{
47862306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
47962306a36Sopenharmony_ci	uint32_t attentionConditions[2];
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	/* Sanity check */
48262306a36Sopenharmony_ci	if (phba->cfg_use_msi != 2) {
48362306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
48462306a36Sopenharmony_ci				"0475 Not configured for supporting MSI-X "
48562306a36Sopenharmony_ci				"cfg_use_msi: 0x%x\n", phba->cfg_use_msi);
48662306a36Sopenharmony_ci		return -EINVAL;
48762306a36Sopenharmony_ci	}
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	if (phba->sli_rev < 3) {
49062306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
49162306a36Sopenharmony_ci				"0476 HBA not supporting SLI-3 or later "
49262306a36Sopenharmony_ci				"SLI Revision: 0x%x\n", phba->sli_rev);
49362306a36Sopenharmony_ci		return -EINVAL;
49462306a36Sopenharmony_ci	}
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	/* Clear mailbox command fields */
49762306a36Sopenharmony_ci	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci	/*
50062306a36Sopenharmony_ci	 * SLI-3, Message Signaled Interrupt Feature.
50162306a36Sopenharmony_ci	 */
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	/* Multi-message attention configuration */
50462306a36Sopenharmony_ci	attentionConditions[0] = (HA_R0ATT | HA_R1ATT | HA_R2ATT | HA_ERATT |
50562306a36Sopenharmony_ci				  HA_LATT | HA_MBATT);
50662306a36Sopenharmony_ci	attentionConditions[1] = 0;
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	mb->un.varCfgMSI.attentionConditions[0] = attentionConditions[0];
50962306a36Sopenharmony_ci	mb->un.varCfgMSI.attentionConditions[1] = attentionConditions[1];
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	/*
51262306a36Sopenharmony_ci	 * Set up message number to HA bit association
51362306a36Sopenharmony_ci	 */
51462306a36Sopenharmony_ci#ifdef __BIG_ENDIAN_BITFIELD
51562306a36Sopenharmony_ci	/* RA0 (FCP Ring) */
51662306a36Sopenharmony_ci	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS] = 1;
51762306a36Sopenharmony_ci	/* RA1 (Other Protocol Extra Ring) */
51862306a36Sopenharmony_ci	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS] = 1;
51962306a36Sopenharmony_ci#else   /*  __LITTLE_ENDIAN_BITFIELD */
52062306a36Sopenharmony_ci	/* RA0 (FCP Ring) */
52162306a36Sopenharmony_ci	mb->un.varCfgMSI.messageNumberByHA[HA_R0_POS^3] = 1;
52262306a36Sopenharmony_ci	/* RA1 (Other Protocol Extra Ring) */
52362306a36Sopenharmony_ci	mb->un.varCfgMSI.messageNumberByHA[HA_R1_POS^3] = 1;
52462306a36Sopenharmony_ci#endif
52562306a36Sopenharmony_ci	/* Multi-message interrupt autoclear configuration*/
52662306a36Sopenharmony_ci	mb->un.varCfgMSI.autoClearHA[0] = attentionConditions[0];
52762306a36Sopenharmony_ci	mb->un.varCfgMSI.autoClearHA[1] = attentionConditions[1];
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci	/* For now, HBA autoclear does not work reliably, disable it */
53062306a36Sopenharmony_ci	mb->un.varCfgMSI.autoClearHA[0] = 0;
53162306a36Sopenharmony_ci	mb->un.varCfgMSI.autoClearHA[1] = 0;
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	/* Set command and owner bit */
53462306a36Sopenharmony_ci	mb->mbxCommand = MBX_CONFIG_MSI;
53562306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	return 0;
53862306a36Sopenharmony_ci}
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci/**
54162306a36Sopenharmony_ci * lpfc_init_link - Prepare a mailbox command for initialize link on a HBA
54262306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
54362306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
54462306a36Sopenharmony_ci * @topology: the link topology for the link to be initialized to.
54562306a36Sopenharmony_ci * @linkspeed: the link speed for the link to be initialized to.
54662306a36Sopenharmony_ci *
54762306a36Sopenharmony_ci * The initialize link mailbox command is used to initialize the Fibre
54862306a36Sopenharmony_ci * Channel link. This command must follow a configure port command that
54962306a36Sopenharmony_ci * establishes the mode of operation.
55062306a36Sopenharmony_ci *
55162306a36Sopenharmony_ci * This routine prepares the mailbox command for initializing link on a HBA
55262306a36Sopenharmony_ci * with the specified link topology and speed.
55362306a36Sopenharmony_ci **/
55462306a36Sopenharmony_civoid
55562306a36Sopenharmony_cilpfc_init_link(struct lpfc_hba * phba,
55662306a36Sopenharmony_ci	       LPFC_MBOXQ_t * pmb, uint32_t topology, uint32_t linkspeed)
55762306a36Sopenharmony_ci{
55862306a36Sopenharmony_ci	lpfc_vpd_t *vpd;
55962306a36Sopenharmony_ci	MAILBOX_t *mb;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	mb = &pmb->u.mb;
56262306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	switch (topology) {
56562306a36Sopenharmony_ci	case FLAGS_TOPOLOGY_MODE_LOOP_PT:
56662306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
56762306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
56862306a36Sopenharmony_ci		break;
56962306a36Sopenharmony_ci	case FLAGS_TOPOLOGY_MODE_PT_PT:
57062306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
57162306a36Sopenharmony_ci		break;
57262306a36Sopenharmony_ci	case FLAGS_TOPOLOGY_MODE_LOOP:
57362306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_LOOP;
57462306a36Sopenharmony_ci		break;
57562306a36Sopenharmony_ci	case FLAGS_TOPOLOGY_MODE_PT_LOOP:
57662306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
57762306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags |= FLAGS_TOPOLOGY_FAILOVER;
57862306a36Sopenharmony_ci		break;
57962306a36Sopenharmony_ci	case FLAGS_LOCAL_LB:
58062306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
58162306a36Sopenharmony_ci		break;
58262306a36Sopenharmony_ci	}
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	/* Topology handling for ASIC_GEN_NUM 0xC and later */
58562306a36Sopenharmony_ci	if ((phba->sli4_hba.pc_sli4_params.sli_family == LPFC_SLI_INTF_FAMILY_G6 ||
58662306a36Sopenharmony_ci	     phba->sli4_hba.pc_sli4_params.if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
58762306a36Sopenharmony_ci	    !(phba->sli4_hba.pc_sli4_params.pls) &&
58862306a36Sopenharmony_ci	    mb->un.varInitLnk.link_flags & FLAGS_TOPOLOGY_MODE_LOOP) {
58962306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags = FLAGS_TOPOLOGY_MODE_PT_PT;
59062306a36Sopenharmony_ci		phba->cfg_topology = FLAGS_TOPOLOGY_MODE_PT_PT;
59162306a36Sopenharmony_ci	}
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci	/* Enable asynchronous ABTS responses from firmware */
59462306a36Sopenharmony_ci	if (phba->sli_rev == LPFC_SLI_REV3 && !phba->cfg_fcp_wait_abts_rsp)
59562306a36Sopenharmony_ci		mb->un.varInitLnk.link_flags |= FLAGS_IMED_ABORT;
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci	/* NEW_FEATURE
59862306a36Sopenharmony_ci	 * Setting up the link speed
59962306a36Sopenharmony_ci	 */
60062306a36Sopenharmony_ci	vpd = &phba->vpd;
60162306a36Sopenharmony_ci	if (vpd->rev.feaLevelHigh >= 0x02){
60262306a36Sopenharmony_ci		switch(linkspeed){
60362306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_1G:
60462306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
60562306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_1G;
60662306a36Sopenharmony_ci			break;
60762306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_2G:
60862306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
60962306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_2G;
61062306a36Sopenharmony_ci			break;
61162306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_4G:
61262306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
61362306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_4G;
61462306a36Sopenharmony_ci			break;
61562306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_8G:
61662306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
61762306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_8G;
61862306a36Sopenharmony_ci			break;
61962306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_10G:
62062306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
62162306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_10G;
62262306a36Sopenharmony_ci			break;
62362306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_16G:
62462306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |=	FLAGS_LINK_SPEED;
62562306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_16G;
62662306a36Sopenharmony_ci			break;
62762306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_32G:
62862306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
62962306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_32G;
63062306a36Sopenharmony_ci			break;
63162306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_64G:
63262306a36Sopenharmony_ci			mb->un.varInitLnk.link_flags |= FLAGS_LINK_SPEED;
63362306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_64G;
63462306a36Sopenharmony_ci			break;
63562306a36Sopenharmony_ci		case LPFC_USER_LINK_SPEED_AUTO:
63662306a36Sopenharmony_ci		default:
63762306a36Sopenharmony_ci			mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
63862306a36Sopenharmony_ci			break;
63962306a36Sopenharmony_ci		}
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	}
64262306a36Sopenharmony_ci	else
64362306a36Sopenharmony_ci		mb->un.varInitLnk.link_speed = LINK_SPEED_AUTO;
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	mb->mbxCommand = (volatile uint8_t)MBX_INIT_LINK;
64662306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
64762306a36Sopenharmony_ci	mb->un.varInitLnk.fabric_AL_PA = phba->fc_pref_ALPA;
64862306a36Sopenharmony_ci	return;
64962306a36Sopenharmony_ci}
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci/**
65262306a36Sopenharmony_ci * lpfc_read_sparam - Prepare a mailbox command for reading HBA parameters
65362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
65462306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
65562306a36Sopenharmony_ci * @vpi: virtual N_Port identifier.
65662306a36Sopenharmony_ci *
65762306a36Sopenharmony_ci * The read service parameter mailbox command is used to read the HBA port
65862306a36Sopenharmony_ci * service parameters. The service parameters are read into the buffer
65962306a36Sopenharmony_ci * specified directly by a BDE in the mailbox command. These service
66062306a36Sopenharmony_ci * parameters may then be used to build the payload of an N_Port/F_POrt
66162306a36Sopenharmony_ci * login request and reply (LOGI/ACC).
66262306a36Sopenharmony_ci *
66362306a36Sopenharmony_ci * This routine prepares the mailbox command for reading HBA port service
66462306a36Sopenharmony_ci * parameters. The DMA memory is allocated in this function and the addresses
66562306a36Sopenharmony_ci * are populated into the mailbox command for the HBA to DMA the service
66662306a36Sopenharmony_ci * parameters into.
66762306a36Sopenharmony_ci *
66862306a36Sopenharmony_ci * Return codes
66962306a36Sopenharmony_ci *    0 - Success
67062306a36Sopenharmony_ci *    1 - DMA memory allocation failed
67162306a36Sopenharmony_ci **/
67262306a36Sopenharmony_ciint
67362306a36Sopenharmony_cilpfc_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb, int vpi)
67462306a36Sopenharmony_ci{
67562306a36Sopenharmony_ci	struct lpfc_dmabuf *mp;
67662306a36Sopenharmony_ci	MAILBOX_t *mb;
67762306a36Sopenharmony_ci	int rc;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_ci	/* Get a buffer to hold the HBAs Service Parameters */
68262306a36Sopenharmony_ci	rc = lpfc_mbox_rsrc_prep(phba, pmb);
68362306a36Sopenharmony_ci	if (rc) {
68462306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
68562306a36Sopenharmony_ci			        "0301 READ_SPARAM: no buffers\n");
68662306a36Sopenharmony_ci		return 1;
68762306a36Sopenharmony_ci	}
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci	mp = pmb->ctx_buf;
69062306a36Sopenharmony_ci	mb = &pmb->u.mb;
69162306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
69262306a36Sopenharmony_ci	mb->mbxCommand = MBX_READ_SPARM64;
69362306a36Sopenharmony_ci	mb->un.varRdSparm.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
69462306a36Sopenharmony_ci	mb->un.varRdSparm.un.sp64.addrHigh = putPaddrHigh(mp->phys);
69562306a36Sopenharmony_ci	mb->un.varRdSparm.un.sp64.addrLow = putPaddrLow(mp->phys);
69662306a36Sopenharmony_ci	if (phba->sli_rev >= LPFC_SLI_REV3)
69762306a36Sopenharmony_ci		mb->un.varRdSparm.vpi = phba->vpi_ids[vpi];
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	return (0);
70062306a36Sopenharmony_ci}
70162306a36Sopenharmony_ci
70262306a36Sopenharmony_ci/**
70362306a36Sopenharmony_ci * lpfc_unreg_did - Prepare a mailbox command for unregistering DID
70462306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
70562306a36Sopenharmony_ci * @vpi: virtual N_Port identifier.
70662306a36Sopenharmony_ci * @did: remote port identifier.
70762306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
70862306a36Sopenharmony_ci *
70962306a36Sopenharmony_ci * The unregister DID mailbox command is used to unregister an N_Port/F_Port
71062306a36Sopenharmony_ci * login for an unknown RPI by specifying the DID of a remote port. This
71162306a36Sopenharmony_ci * command frees an RPI context in the HBA port. This has the effect of
71262306a36Sopenharmony_ci * performing an implicit N_Port/F_Port logout.
71362306a36Sopenharmony_ci *
71462306a36Sopenharmony_ci * This routine prepares the mailbox command for unregistering a remote
71562306a36Sopenharmony_ci * N_Port/F_Port (DID) login.
71662306a36Sopenharmony_ci **/
71762306a36Sopenharmony_civoid
71862306a36Sopenharmony_cilpfc_unreg_did(struct lpfc_hba * phba, uint16_t vpi, uint32_t did,
71962306a36Sopenharmony_ci	       LPFC_MBOXQ_t * pmb)
72062306a36Sopenharmony_ci{
72162306a36Sopenharmony_ci	MAILBOX_t *mb;
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci	mb = &pmb->u.mb;
72462306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci	mb->un.varUnregDID.did = did;
72762306a36Sopenharmony_ci	mb->un.varUnregDID.vpi = vpi;
72862306a36Sopenharmony_ci	if ((vpi != 0xffff) &&
72962306a36Sopenharmony_ci	    (phba->sli_rev == LPFC_SLI_REV4))
73062306a36Sopenharmony_ci		mb->un.varUnregDID.vpi = phba->vpi_ids[vpi];
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_ci	mb->mbxCommand = MBX_UNREG_D_ID;
73362306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
73462306a36Sopenharmony_ci	return;
73562306a36Sopenharmony_ci}
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci/**
73862306a36Sopenharmony_ci * lpfc_read_config - Prepare a mailbox command for reading HBA configuration
73962306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
74062306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
74162306a36Sopenharmony_ci *
74262306a36Sopenharmony_ci * The read configuration mailbox command is used to read the HBA port
74362306a36Sopenharmony_ci * configuration parameters. This mailbox command provides a method for
74462306a36Sopenharmony_ci * seeing any parameters that may have changed via various configuration
74562306a36Sopenharmony_ci * mailbox commands.
74662306a36Sopenharmony_ci *
74762306a36Sopenharmony_ci * This routine prepares the mailbox command for reading out HBA configuration
74862306a36Sopenharmony_ci * parameters.
74962306a36Sopenharmony_ci **/
75062306a36Sopenharmony_civoid
75162306a36Sopenharmony_cilpfc_read_config(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
75262306a36Sopenharmony_ci{
75362306a36Sopenharmony_ci	MAILBOX_t *mb;
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci	mb = &pmb->u.mb;
75662306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci	mb->mbxCommand = MBX_READ_CONFIG;
75962306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
76062306a36Sopenharmony_ci	return;
76162306a36Sopenharmony_ci}
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ci/**
76462306a36Sopenharmony_ci * lpfc_read_lnk_stat - Prepare a mailbox command for reading HBA link stats
76562306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
76662306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
76762306a36Sopenharmony_ci *
76862306a36Sopenharmony_ci * The read link status mailbox command is used to read the link status from
76962306a36Sopenharmony_ci * the HBA. Link status includes all link-related error counters. These
77062306a36Sopenharmony_ci * counters are maintained by the HBA and originated in the link hardware
77162306a36Sopenharmony_ci * unit. Note that all of these counters wrap.
77262306a36Sopenharmony_ci *
77362306a36Sopenharmony_ci * This routine prepares the mailbox command for reading out HBA link status.
77462306a36Sopenharmony_ci **/
77562306a36Sopenharmony_civoid
77662306a36Sopenharmony_cilpfc_read_lnk_stat(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
77762306a36Sopenharmony_ci{
77862306a36Sopenharmony_ci	MAILBOX_t *mb;
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci	mb = &pmb->u.mb;
78162306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	mb->mbxCommand = MBX_READ_LNK_STAT;
78462306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
78562306a36Sopenharmony_ci	return;
78662306a36Sopenharmony_ci}
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_ci/**
78962306a36Sopenharmony_ci * lpfc_reg_rpi - Prepare a mailbox command for registering remote login
79062306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
79162306a36Sopenharmony_ci * @vpi: virtual N_Port identifier.
79262306a36Sopenharmony_ci * @did: remote port identifier.
79362306a36Sopenharmony_ci * @param: pointer to memory holding the server parameters.
79462306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
79562306a36Sopenharmony_ci * @rpi: the rpi to use in the registration (usually only used for SLI4.
79662306a36Sopenharmony_ci *
79762306a36Sopenharmony_ci * The registration login mailbox command is used to register an N_Port or
79862306a36Sopenharmony_ci * F_Port login. This registration allows the HBA to cache the remote N_Port
79962306a36Sopenharmony_ci * service parameters internally and thereby make the appropriate FC-2
80062306a36Sopenharmony_ci * decisions. The remote port service parameters are handed off by the driver
80162306a36Sopenharmony_ci * to the HBA using a descriptor entry that directly identifies a buffer in
80262306a36Sopenharmony_ci * host memory. In exchange, the HBA returns an RPI identifier.
80362306a36Sopenharmony_ci *
80462306a36Sopenharmony_ci * This routine prepares the mailbox command for registering remote port login.
80562306a36Sopenharmony_ci * The function allocates DMA buffer for passing the service parameters to the
80662306a36Sopenharmony_ci * HBA with the mailbox command.
80762306a36Sopenharmony_ci *
80862306a36Sopenharmony_ci * Return codes
80962306a36Sopenharmony_ci *    0 - Success
81062306a36Sopenharmony_ci *    1 - DMA memory allocation failed
81162306a36Sopenharmony_ci **/
81262306a36Sopenharmony_ciint
81362306a36Sopenharmony_cilpfc_reg_rpi(struct lpfc_hba *phba, uint16_t vpi, uint32_t did,
81462306a36Sopenharmony_ci	     uint8_t *param, LPFC_MBOXQ_t *pmb, uint16_t rpi)
81562306a36Sopenharmony_ci{
81662306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
81762306a36Sopenharmony_ci	uint8_t *sparam;
81862306a36Sopenharmony_ci	struct lpfc_dmabuf *mp;
81962306a36Sopenharmony_ci	int rc;
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	mb->un.varRegLogin.rpi = 0;
82462306a36Sopenharmony_ci	if (phba->sli_rev == LPFC_SLI_REV4)
82562306a36Sopenharmony_ci		mb->un.varRegLogin.rpi = phba->sli4_hba.rpi_ids[rpi];
82662306a36Sopenharmony_ci	if (phba->sli_rev >= LPFC_SLI_REV3)
82762306a36Sopenharmony_ci		mb->un.varRegLogin.vpi = phba->vpi_ids[vpi];
82862306a36Sopenharmony_ci	mb->un.varRegLogin.did = did;
82962306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	/* Get a buffer to hold NPorts Service Parameters */
83262306a36Sopenharmony_ci	rc = lpfc_mbox_rsrc_prep(phba, pmb);
83362306a36Sopenharmony_ci	if (rc) {
83462306a36Sopenharmony_ci		mb->mbxCommand = MBX_REG_LOGIN64;
83562306a36Sopenharmony_ci		/* REG_LOGIN: no buffers */
83662306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
83762306a36Sopenharmony_ci				"0302 REG_LOGIN: no buffers, VPI:%d DID:x%x, "
83862306a36Sopenharmony_ci				"rpi x%x\n", vpi, did, rpi);
83962306a36Sopenharmony_ci		return 1;
84062306a36Sopenharmony_ci	}
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci	/* Copy param's into a new buffer */
84362306a36Sopenharmony_ci	mp = pmb->ctx_buf;
84462306a36Sopenharmony_ci	sparam = mp->virt;
84562306a36Sopenharmony_ci	memcpy(sparam, param, sizeof (struct serv_parm));
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	/* Finish initializing the mailbox. */
84862306a36Sopenharmony_ci	mb->mbxCommand = MBX_REG_LOGIN64;
84962306a36Sopenharmony_ci	mb->un.varRegLogin.un.sp64.tus.f.bdeSize = sizeof (struct serv_parm);
85062306a36Sopenharmony_ci	mb->un.varRegLogin.un.sp64.addrHigh = putPaddrHigh(mp->phys);
85162306a36Sopenharmony_ci	mb->un.varRegLogin.un.sp64.addrLow = putPaddrLow(mp->phys);
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci	return 0;
85462306a36Sopenharmony_ci}
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci/**
85762306a36Sopenharmony_ci * lpfc_unreg_login - Prepare a mailbox command for unregistering remote login
85862306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
85962306a36Sopenharmony_ci * @vpi: virtual N_Port identifier.
86062306a36Sopenharmony_ci * @rpi: remote port identifier
86162306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
86262306a36Sopenharmony_ci *
86362306a36Sopenharmony_ci * The unregistration login mailbox command is used to unregister an N_Port
86462306a36Sopenharmony_ci * or F_Port login. This command frees an RPI context in the HBA. It has the
86562306a36Sopenharmony_ci * effect of performing an implicit N_Port/F_Port logout.
86662306a36Sopenharmony_ci *
86762306a36Sopenharmony_ci * This routine prepares the mailbox command for unregistering remote port
86862306a36Sopenharmony_ci * login.
86962306a36Sopenharmony_ci *
87062306a36Sopenharmony_ci * For SLI4 ports, the rpi passed to this function must be the physical
87162306a36Sopenharmony_ci * rpi value, not the logical index.
87262306a36Sopenharmony_ci **/
87362306a36Sopenharmony_civoid
87462306a36Sopenharmony_cilpfc_unreg_login(struct lpfc_hba *phba, uint16_t vpi, uint32_t rpi,
87562306a36Sopenharmony_ci		 LPFC_MBOXQ_t * pmb)
87662306a36Sopenharmony_ci{
87762306a36Sopenharmony_ci	MAILBOX_t *mb;
87862306a36Sopenharmony_ci
87962306a36Sopenharmony_ci	mb = &pmb->u.mb;
88062306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
88162306a36Sopenharmony_ci
88262306a36Sopenharmony_ci	mb->un.varUnregLogin.rpi = rpi;
88362306a36Sopenharmony_ci	mb->un.varUnregLogin.rsvd1 = 0;
88462306a36Sopenharmony_ci	if (phba->sli_rev >= LPFC_SLI_REV3)
88562306a36Sopenharmony_ci		mb->un.varUnregLogin.vpi = phba->vpi_ids[vpi];
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ci	mb->mbxCommand = MBX_UNREG_LOGIN;
88862306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci	return;
89162306a36Sopenharmony_ci}
89262306a36Sopenharmony_ci
89362306a36Sopenharmony_ci/**
89462306a36Sopenharmony_ci * lpfc_sli4_unreg_all_rpis - unregister all RPIs for a vport on SLI4 HBA.
89562306a36Sopenharmony_ci * @vport: pointer to a vport object.
89662306a36Sopenharmony_ci *
89762306a36Sopenharmony_ci * This routine sends mailbox command to unregister all active RPIs for
89862306a36Sopenharmony_ci * a vport.
89962306a36Sopenharmony_ci **/
90062306a36Sopenharmony_civoid
90162306a36Sopenharmony_cilpfc_sli4_unreg_all_rpis(struct lpfc_vport *vport)
90262306a36Sopenharmony_ci{
90362306a36Sopenharmony_ci	struct lpfc_hba  *phba  = vport->phba;
90462306a36Sopenharmony_ci	LPFC_MBOXQ_t     *mbox;
90562306a36Sopenharmony_ci	int rc;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
90862306a36Sopenharmony_ci	if (mbox) {
90962306a36Sopenharmony_ci		/*
91062306a36Sopenharmony_ci		 * For SLI4 functions, the rpi field is overloaded for
91162306a36Sopenharmony_ci		 * the vport context unreg all.  This routine passes
91262306a36Sopenharmony_ci		 * 0 for the rpi field in lpfc_unreg_login for compatibility
91362306a36Sopenharmony_ci		 * with SLI3 and then overrides the rpi field with the
91462306a36Sopenharmony_ci		 * expected value for SLI4.
91562306a36Sopenharmony_ci		 */
91662306a36Sopenharmony_ci		lpfc_unreg_login(phba, vport->vpi, phba->vpi_ids[vport->vpi],
91762306a36Sopenharmony_ci				 mbox);
91862306a36Sopenharmony_ci		mbox->u.mb.un.varUnregLogin.rsvd1 = 0x4000;
91962306a36Sopenharmony_ci		mbox->vport = vport;
92062306a36Sopenharmony_ci		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
92162306a36Sopenharmony_ci		mbox->ctx_ndlp = NULL;
92262306a36Sopenharmony_ci		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
92362306a36Sopenharmony_ci		if (rc == MBX_NOT_FINISHED)
92462306a36Sopenharmony_ci			mempool_free(mbox, phba->mbox_mem_pool);
92562306a36Sopenharmony_ci	}
92662306a36Sopenharmony_ci}
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci/**
92962306a36Sopenharmony_ci * lpfc_reg_vpi - Prepare a mailbox command for registering vport identifier
93062306a36Sopenharmony_ci * @vport: pointer to a vport object.
93162306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
93262306a36Sopenharmony_ci *
93362306a36Sopenharmony_ci * The registration vport identifier mailbox command is used to activate a
93462306a36Sopenharmony_ci * virtual N_Port after it has acquired an N_Port_ID. The HBA validates the
93562306a36Sopenharmony_ci * N_Port_ID against the information in the selected virtual N_Port context
93662306a36Sopenharmony_ci * block and marks it active to allow normal processing of IOCB commands and
93762306a36Sopenharmony_ci * received unsolicited exchanges.
93862306a36Sopenharmony_ci *
93962306a36Sopenharmony_ci * This routine prepares the mailbox command for registering a virtual N_Port.
94062306a36Sopenharmony_ci **/
94162306a36Sopenharmony_civoid
94262306a36Sopenharmony_cilpfc_reg_vpi(struct lpfc_vport *vport, LPFC_MBOXQ_t *pmb)
94362306a36Sopenharmony_ci{
94462306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
94562306a36Sopenharmony_ci	struct lpfc_hba *phba = vport->phba;
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
94862306a36Sopenharmony_ci	/*
94962306a36Sopenharmony_ci	 * Set the re-reg VPI bit for f/w to update the MAC address.
95062306a36Sopenharmony_ci	 */
95162306a36Sopenharmony_ci	if ((phba->sli_rev == LPFC_SLI_REV4) &&
95262306a36Sopenharmony_ci		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI))
95362306a36Sopenharmony_ci		mb->un.varRegVpi.upd = 1;
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci	mb->un.varRegVpi.vpi = phba->vpi_ids[vport->vpi];
95662306a36Sopenharmony_ci	mb->un.varRegVpi.sid = vport->fc_myDID;
95762306a36Sopenharmony_ci	if (phba->sli_rev == LPFC_SLI_REV4)
95862306a36Sopenharmony_ci		mb->un.varRegVpi.vfi = phba->sli4_hba.vfi_ids[vport->vfi];
95962306a36Sopenharmony_ci	else
96062306a36Sopenharmony_ci		mb->un.varRegVpi.vfi = vport->vfi + vport->phba->vfi_base;
96162306a36Sopenharmony_ci	memcpy(mb->un.varRegVpi.wwn, &vport->fc_portname,
96262306a36Sopenharmony_ci	       sizeof(struct lpfc_name));
96362306a36Sopenharmony_ci	mb->un.varRegVpi.wwn[0] = cpu_to_le32(mb->un.varRegVpi.wwn[0]);
96462306a36Sopenharmony_ci	mb->un.varRegVpi.wwn[1] = cpu_to_le32(mb->un.varRegVpi.wwn[1]);
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_ci	mb->mbxCommand = MBX_REG_VPI;
96762306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
96862306a36Sopenharmony_ci	return;
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci}
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci/**
97362306a36Sopenharmony_ci * lpfc_unreg_vpi - Prepare a mailbox command for unregistering vport id
97462306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
97562306a36Sopenharmony_ci * @vpi: virtual N_Port identifier.
97662306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
97762306a36Sopenharmony_ci *
97862306a36Sopenharmony_ci * The unregistration vport identifier mailbox command is used to inactivate
97962306a36Sopenharmony_ci * a virtual N_Port. The driver must have logged out and unregistered all
98062306a36Sopenharmony_ci * remote N_Ports to abort any activity on the virtual N_Port. The HBA will
98162306a36Sopenharmony_ci * unregisters any default RPIs associated with the specified vpi, aborting
98262306a36Sopenharmony_ci * any active exchanges. The HBA will post the mailbox response after making
98362306a36Sopenharmony_ci * the virtual N_Port inactive.
98462306a36Sopenharmony_ci *
98562306a36Sopenharmony_ci * This routine prepares the mailbox command for unregistering a virtual
98662306a36Sopenharmony_ci * N_Port.
98762306a36Sopenharmony_ci **/
98862306a36Sopenharmony_civoid
98962306a36Sopenharmony_cilpfc_unreg_vpi(struct lpfc_hba *phba, uint16_t vpi, LPFC_MBOXQ_t *pmb)
99062306a36Sopenharmony_ci{
99162306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
99262306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci	if (phba->sli_rev == LPFC_SLI_REV3)
99562306a36Sopenharmony_ci		mb->un.varUnregVpi.vpi = phba->vpi_ids[vpi];
99662306a36Sopenharmony_ci	else if (phba->sli_rev >= LPFC_SLI_REV4)
99762306a36Sopenharmony_ci		mb->un.varUnregVpi.sli4_vpi = phba->vpi_ids[vpi];
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ci	mb->mbxCommand = MBX_UNREG_VPI;
100062306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
100162306a36Sopenharmony_ci	return;
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci}
100462306a36Sopenharmony_ci
100562306a36Sopenharmony_ci/**
100662306a36Sopenharmony_ci * lpfc_config_pcb_setup - Set up IOCB rings in the Port Control Block (PCB)
100762306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
100862306a36Sopenharmony_ci *
100962306a36Sopenharmony_ci * This routine sets up and initializes the IOCB rings in the Port Control
101062306a36Sopenharmony_ci * Block (PCB).
101162306a36Sopenharmony_ci **/
101262306a36Sopenharmony_cistatic void
101362306a36Sopenharmony_cilpfc_config_pcb_setup(struct lpfc_hba * phba)
101462306a36Sopenharmony_ci{
101562306a36Sopenharmony_ci	struct lpfc_sli *psli = &phba->sli;
101662306a36Sopenharmony_ci	struct lpfc_sli_ring *pring;
101762306a36Sopenharmony_ci	PCB_t *pcbp = phba->pcb;
101862306a36Sopenharmony_ci	dma_addr_t pdma_addr;
101962306a36Sopenharmony_ci	uint32_t offset;
102062306a36Sopenharmony_ci	uint32_t iocbCnt = 0;
102162306a36Sopenharmony_ci	int i;
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci	pcbp->maxRing = (psli->num_rings - 1);
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_ci	for (i = 0; i < psli->num_rings; i++) {
102662306a36Sopenharmony_ci		pring = &psli->sli3_ring[i];
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci		pring->sli.sli3.sizeCiocb =
102962306a36Sopenharmony_ci			phba->sli_rev == 3 ? SLI3_IOCB_CMD_SIZE :
103062306a36Sopenharmony_ci							SLI2_IOCB_CMD_SIZE;
103162306a36Sopenharmony_ci		pring->sli.sli3.sizeRiocb =
103262306a36Sopenharmony_ci			phba->sli_rev == 3 ? SLI3_IOCB_RSP_SIZE :
103362306a36Sopenharmony_ci							SLI2_IOCB_RSP_SIZE;
103462306a36Sopenharmony_ci		/* A ring MUST have both cmd and rsp entries defined to be
103562306a36Sopenharmony_ci		   valid */
103662306a36Sopenharmony_ci		if ((pring->sli.sli3.numCiocb == 0) ||
103762306a36Sopenharmony_ci			(pring->sli.sli3.numRiocb == 0)) {
103862306a36Sopenharmony_ci			pcbp->rdsc[i].cmdEntries = 0;
103962306a36Sopenharmony_ci			pcbp->rdsc[i].rspEntries = 0;
104062306a36Sopenharmony_ci			pcbp->rdsc[i].cmdAddrHigh = 0;
104162306a36Sopenharmony_ci			pcbp->rdsc[i].rspAddrHigh = 0;
104262306a36Sopenharmony_ci			pcbp->rdsc[i].cmdAddrLow = 0;
104362306a36Sopenharmony_ci			pcbp->rdsc[i].rspAddrLow = 0;
104462306a36Sopenharmony_ci			pring->sli.sli3.cmdringaddr = NULL;
104562306a36Sopenharmony_ci			pring->sli.sli3.rspringaddr = NULL;
104662306a36Sopenharmony_ci			continue;
104762306a36Sopenharmony_ci		}
104862306a36Sopenharmony_ci		/* Command ring setup for ring */
104962306a36Sopenharmony_ci		pring->sli.sli3.cmdringaddr = (void *)&phba->IOCBs[iocbCnt];
105062306a36Sopenharmony_ci		pcbp->rdsc[i].cmdEntries = pring->sli.sli3.numCiocb;
105162306a36Sopenharmony_ci
105262306a36Sopenharmony_ci		offset = (uint8_t *) &phba->IOCBs[iocbCnt] -
105362306a36Sopenharmony_ci			 (uint8_t *) phba->slim2p.virt;
105462306a36Sopenharmony_ci		pdma_addr = phba->slim2p.phys + offset;
105562306a36Sopenharmony_ci		pcbp->rdsc[i].cmdAddrHigh = putPaddrHigh(pdma_addr);
105662306a36Sopenharmony_ci		pcbp->rdsc[i].cmdAddrLow = putPaddrLow(pdma_addr);
105762306a36Sopenharmony_ci		iocbCnt += pring->sli.sli3.numCiocb;
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci		/* Response ring setup for ring */
106062306a36Sopenharmony_ci		pring->sli.sli3.rspringaddr = (void *) &phba->IOCBs[iocbCnt];
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci		pcbp->rdsc[i].rspEntries = pring->sli.sli3.numRiocb;
106362306a36Sopenharmony_ci		offset = (uint8_t *)&phba->IOCBs[iocbCnt] -
106462306a36Sopenharmony_ci			 (uint8_t *)phba->slim2p.virt;
106562306a36Sopenharmony_ci		pdma_addr = phba->slim2p.phys + offset;
106662306a36Sopenharmony_ci		pcbp->rdsc[i].rspAddrHigh = putPaddrHigh(pdma_addr);
106762306a36Sopenharmony_ci		pcbp->rdsc[i].rspAddrLow = putPaddrLow(pdma_addr);
106862306a36Sopenharmony_ci		iocbCnt += pring->sli.sli3.numRiocb;
106962306a36Sopenharmony_ci	}
107062306a36Sopenharmony_ci}
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_ci/**
107362306a36Sopenharmony_ci * lpfc_read_rev - Prepare a mailbox command for reading HBA revision
107462306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
107562306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
107662306a36Sopenharmony_ci *
107762306a36Sopenharmony_ci * The read revision mailbox command is used to read the revision levels of
107862306a36Sopenharmony_ci * the HBA components. These components include hardware units, resident
107962306a36Sopenharmony_ci * firmware, and available firmware. HBAs that supports SLI-3 mode of
108062306a36Sopenharmony_ci * operation provide different response information depending on the version
108162306a36Sopenharmony_ci * requested by the driver.
108262306a36Sopenharmony_ci *
108362306a36Sopenharmony_ci * This routine prepares the mailbox command for reading HBA revision
108462306a36Sopenharmony_ci * information.
108562306a36Sopenharmony_ci **/
108662306a36Sopenharmony_civoid
108762306a36Sopenharmony_cilpfc_read_rev(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
108862306a36Sopenharmony_ci{
108962306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
109062306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
109162306a36Sopenharmony_ci	mb->un.varRdRev.cv = 1;
109262306a36Sopenharmony_ci	mb->un.varRdRev.v3req = 1; /* Request SLI3 info */
109362306a36Sopenharmony_ci	mb->mbxCommand = MBX_READ_REV;
109462306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
109562306a36Sopenharmony_ci	return;
109662306a36Sopenharmony_ci}
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_civoid
109962306a36Sopenharmony_cilpfc_sli4_swap_str(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
110062306a36Sopenharmony_ci{
110162306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
110262306a36Sopenharmony_ci	struct lpfc_mqe *mqe;
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci	switch (mb->mbxCommand) {
110562306a36Sopenharmony_ci	case  MBX_READ_REV:
110662306a36Sopenharmony_ci		 mqe = &pmb->u.mqe;
110762306a36Sopenharmony_ci		lpfc_sli_pcimem_bcopy(mqe->un.read_rev.fw_name,
110862306a36Sopenharmony_ci				 mqe->un.read_rev.fw_name, 16);
110962306a36Sopenharmony_ci		lpfc_sli_pcimem_bcopy(mqe->un.read_rev.ulp_fw_name,
111062306a36Sopenharmony_ci				 mqe->un.read_rev.ulp_fw_name, 16);
111162306a36Sopenharmony_ci		break;
111262306a36Sopenharmony_ci	default:
111362306a36Sopenharmony_ci		break;
111462306a36Sopenharmony_ci	}
111562306a36Sopenharmony_ci	return;
111662306a36Sopenharmony_ci}
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci/**
111962306a36Sopenharmony_ci * lpfc_build_hbq_profile2 - Set up the HBQ Selection Profile 2
112062306a36Sopenharmony_ci * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
112162306a36Sopenharmony_ci * @hbq_desc: pointer to the HBQ selection profile descriptor.
112262306a36Sopenharmony_ci *
112362306a36Sopenharmony_ci * The Host Buffer Queue (HBQ) Selection Profile 2 specifies that the HBA
112462306a36Sopenharmony_ci * tests the incoming frames' R_CTL/TYPE fields with works 10:15 and performs
112562306a36Sopenharmony_ci * the Sequence Length Test using the fields in the Selection Profile 2
112662306a36Sopenharmony_ci * extension in words 20:31.
112762306a36Sopenharmony_ci **/
112862306a36Sopenharmony_cistatic void
112962306a36Sopenharmony_cilpfc_build_hbq_profile2(struct config_hbq_var *hbqmb,
113062306a36Sopenharmony_ci			struct lpfc_hbq_init  *hbq_desc)
113162306a36Sopenharmony_ci{
113262306a36Sopenharmony_ci	hbqmb->profiles.profile2.seqlenbcnt = hbq_desc->seqlenbcnt;
113362306a36Sopenharmony_ci	hbqmb->profiles.profile2.maxlen     = hbq_desc->maxlen;
113462306a36Sopenharmony_ci	hbqmb->profiles.profile2.seqlenoff  = hbq_desc->seqlenoff;
113562306a36Sopenharmony_ci}
113662306a36Sopenharmony_ci
113762306a36Sopenharmony_ci/**
113862306a36Sopenharmony_ci * lpfc_build_hbq_profile3 - Set up the HBQ Selection Profile 3
113962306a36Sopenharmony_ci * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
114062306a36Sopenharmony_ci * @hbq_desc: pointer to the HBQ selection profile descriptor.
114162306a36Sopenharmony_ci *
114262306a36Sopenharmony_ci * The Host Buffer Queue (HBQ) Selection Profile 3 specifies that the HBA
114362306a36Sopenharmony_ci * tests the incoming frame's R_CTL/TYPE fields with words 10:15 and performs
114462306a36Sopenharmony_ci * the Sequence Length Test and Byte Field Test using the fields in the
114562306a36Sopenharmony_ci * Selection Profile 3 extension in words 20:31.
114662306a36Sopenharmony_ci **/
114762306a36Sopenharmony_cistatic void
114862306a36Sopenharmony_cilpfc_build_hbq_profile3(struct config_hbq_var *hbqmb,
114962306a36Sopenharmony_ci			struct lpfc_hbq_init  *hbq_desc)
115062306a36Sopenharmony_ci{
115162306a36Sopenharmony_ci	hbqmb->profiles.profile3.seqlenbcnt = hbq_desc->seqlenbcnt;
115262306a36Sopenharmony_ci	hbqmb->profiles.profile3.maxlen     = hbq_desc->maxlen;
115362306a36Sopenharmony_ci	hbqmb->profiles.profile3.cmdcodeoff = hbq_desc->cmdcodeoff;
115462306a36Sopenharmony_ci	hbqmb->profiles.profile3.seqlenoff  = hbq_desc->seqlenoff;
115562306a36Sopenharmony_ci	memcpy(&hbqmb->profiles.profile3.cmdmatch, hbq_desc->cmdmatch,
115662306a36Sopenharmony_ci	       sizeof(hbqmb->profiles.profile3.cmdmatch));
115762306a36Sopenharmony_ci}
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci/**
116062306a36Sopenharmony_ci * lpfc_build_hbq_profile5 - Set up the HBQ Selection Profile 5
116162306a36Sopenharmony_ci * @hbqmb: pointer to the HBQ configuration data structure in mailbox command.
116262306a36Sopenharmony_ci * @hbq_desc: pointer to the HBQ selection profile descriptor.
116362306a36Sopenharmony_ci *
116462306a36Sopenharmony_ci * The Host Buffer Queue (HBQ) Selection Profile 5 specifies a header HBQ. The
116562306a36Sopenharmony_ci * HBA tests the initial frame of an incoming sequence using the frame's
116662306a36Sopenharmony_ci * R_CTL/TYPE fields with words 10:15 and performs the Sequence Length Test
116762306a36Sopenharmony_ci * and Byte Field Test using the fields in the Selection Profile 5 extension
116862306a36Sopenharmony_ci * words 20:31.
116962306a36Sopenharmony_ci **/
117062306a36Sopenharmony_cistatic void
117162306a36Sopenharmony_cilpfc_build_hbq_profile5(struct config_hbq_var *hbqmb,
117262306a36Sopenharmony_ci			struct lpfc_hbq_init  *hbq_desc)
117362306a36Sopenharmony_ci{
117462306a36Sopenharmony_ci	hbqmb->profiles.profile5.seqlenbcnt = hbq_desc->seqlenbcnt;
117562306a36Sopenharmony_ci	hbqmb->profiles.profile5.maxlen     = hbq_desc->maxlen;
117662306a36Sopenharmony_ci	hbqmb->profiles.profile5.cmdcodeoff = hbq_desc->cmdcodeoff;
117762306a36Sopenharmony_ci	hbqmb->profiles.profile5.seqlenoff  = hbq_desc->seqlenoff;
117862306a36Sopenharmony_ci	memcpy(&hbqmb->profiles.profile5.cmdmatch, hbq_desc->cmdmatch,
117962306a36Sopenharmony_ci	       sizeof(hbqmb->profiles.profile5.cmdmatch));
118062306a36Sopenharmony_ci}
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci/**
118362306a36Sopenharmony_ci * lpfc_config_hbq - Prepare a mailbox command for configuring an HBQ
118462306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
118562306a36Sopenharmony_ci * @id: HBQ identifier.
118662306a36Sopenharmony_ci * @hbq_desc: pointer to the HBA descriptor data structure.
118762306a36Sopenharmony_ci * @hbq_entry_index: index of the HBQ entry data structures.
118862306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
118962306a36Sopenharmony_ci *
119062306a36Sopenharmony_ci * The configure HBQ (Host Buffer Queue) mailbox command is used to configure
119162306a36Sopenharmony_ci * an HBQ. The configuration binds events that require buffers to a particular
119262306a36Sopenharmony_ci * ring and HBQ based on a selection profile.
119362306a36Sopenharmony_ci *
119462306a36Sopenharmony_ci * This routine prepares the mailbox command for configuring an HBQ.
119562306a36Sopenharmony_ci **/
119662306a36Sopenharmony_civoid
119762306a36Sopenharmony_cilpfc_config_hbq(struct lpfc_hba *phba, uint32_t id,
119862306a36Sopenharmony_ci		 struct lpfc_hbq_init *hbq_desc,
119962306a36Sopenharmony_ci		uint32_t hbq_entry_index, LPFC_MBOXQ_t *pmb)
120062306a36Sopenharmony_ci{
120162306a36Sopenharmony_ci	int i;
120262306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
120362306a36Sopenharmony_ci	struct config_hbq_var *hbqmb = &mb->un.varCfgHbq;
120462306a36Sopenharmony_ci
120562306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
120662306a36Sopenharmony_ci	hbqmb->hbqId = id;
120762306a36Sopenharmony_ci	hbqmb->entry_count = hbq_desc->entry_count;   /* # entries in HBQ */
120862306a36Sopenharmony_ci	hbqmb->recvNotify = hbq_desc->rn;             /* Receive
120962306a36Sopenharmony_ci						       * Notification */
121062306a36Sopenharmony_ci	hbqmb->numMask    = hbq_desc->mask_count;     /* # R_CTL/TYPE masks
121162306a36Sopenharmony_ci						       * # in words 0-19 */
121262306a36Sopenharmony_ci	hbqmb->profile    = hbq_desc->profile;	      /* Selection profile:
121362306a36Sopenharmony_ci						       * 0 = all,
121462306a36Sopenharmony_ci						       * 7 = logentry */
121562306a36Sopenharmony_ci	hbqmb->ringMask   = hbq_desc->ring_mask;      /* Binds HBQ to a ring
121662306a36Sopenharmony_ci						       * e.g. Ring0=b0001,
121762306a36Sopenharmony_ci						       * ring2=b0100 */
121862306a36Sopenharmony_ci	hbqmb->headerLen  = hbq_desc->headerLen;      /* 0 if not profile 4
121962306a36Sopenharmony_ci						       * or 5 */
122062306a36Sopenharmony_ci	hbqmb->logEntry   = hbq_desc->logEntry;       /* Set to 1 if this
122162306a36Sopenharmony_ci						       * HBQ will be used
122262306a36Sopenharmony_ci						       * for LogEntry
122362306a36Sopenharmony_ci						       * buffers */
122462306a36Sopenharmony_ci	hbqmb->hbqaddrLow = putPaddrLow(phba->hbqslimp.phys) +
122562306a36Sopenharmony_ci		hbq_entry_index * sizeof(struct lpfc_hbq_entry);
122662306a36Sopenharmony_ci	hbqmb->hbqaddrHigh = putPaddrHigh(phba->hbqslimp.phys);
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci	mb->mbxCommand = MBX_CONFIG_HBQ;
122962306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci				/* Copy info for profiles 2,3,5. Other
123262306a36Sopenharmony_ci				 * profiles this area is reserved
123362306a36Sopenharmony_ci				 */
123462306a36Sopenharmony_ci	if (hbq_desc->profile == 2)
123562306a36Sopenharmony_ci		lpfc_build_hbq_profile2(hbqmb, hbq_desc);
123662306a36Sopenharmony_ci	else if (hbq_desc->profile == 3)
123762306a36Sopenharmony_ci		lpfc_build_hbq_profile3(hbqmb, hbq_desc);
123862306a36Sopenharmony_ci	else if (hbq_desc->profile == 5)
123962306a36Sopenharmony_ci		lpfc_build_hbq_profile5(hbqmb, hbq_desc);
124062306a36Sopenharmony_ci
124162306a36Sopenharmony_ci	/* Return if no rctl / type masks for this HBQ */
124262306a36Sopenharmony_ci	if (!hbq_desc->mask_count)
124362306a36Sopenharmony_ci		return;
124462306a36Sopenharmony_ci
124562306a36Sopenharmony_ci	/* Otherwise we setup specific rctl / type masks for this HBQ */
124662306a36Sopenharmony_ci	for (i = 0; i < hbq_desc->mask_count; i++) {
124762306a36Sopenharmony_ci		hbqmb->hbqMasks[i].tmatch = hbq_desc->hbqMasks[i].tmatch;
124862306a36Sopenharmony_ci		hbqmb->hbqMasks[i].tmask  = hbq_desc->hbqMasks[i].tmask;
124962306a36Sopenharmony_ci		hbqmb->hbqMasks[i].rctlmatch = hbq_desc->hbqMasks[i].rctlmatch;
125062306a36Sopenharmony_ci		hbqmb->hbqMasks[i].rctlmask  = hbq_desc->hbqMasks[i].rctlmask;
125162306a36Sopenharmony_ci	}
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	return;
125462306a36Sopenharmony_ci}
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci/**
125762306a36Sopenharmony_ci * lpfc_config_ring - Prepare a mailbox command for configuring an IOCB ring
125862306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
125962306a36Sopenharmony_ci * @ring: ring number/index
126062306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
126162306a36Sopenharmony_ci *
126262306a36Sopenharmony_ci * The configure ring mailbox command is used to configure an IOCB ring. This
126362306a36Sopenharmony_ci * configuration binds from one to six of HBA RC_CTL/TYPE mask entries to the
126462306a36Sopenharmony_ci * ring. This is used to map incoming sequences to a particular ring whose
126562306a36Sopenharmony_ci * RC_CTL/TYPE mask entry matches that of the sequence. The driver should not
126662306a36Sopenharmony_ci * attempt to configure a ring whose number is greater than the number
126762306a36Sopenharmony_ci * specified in the Port Control Block (PCB). It is an error to issue the
126862306a36Sopenharmony_ci * configure ring command more than once with the same ring number. The HBA
126962306a36Sopenharmony_ci * returns an error if the driver attempts this.
127062306a36Sopenharmony_ci *
127162306a36Sopenharmony_ci * This routine prepares the mailbox command for configuring IOCB ring.
127262306a36Sopenharmony_ci **/
127362306a36Sopenharmony_civoid
127462306a36Sopenharmony_cilpfc_config_ring(struct lpfc_hba * phba, int ring, LPFC_MBOXQ_t * pmb)
127562306a36Sopenharmony_ci{
127662306a36Sopenharmony_ci	int i;
127762306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
127862306a36Sopenharmony_ci	struct lpfc_sli *psli;
127962306a36Sopenharmony_ci	struct lpfc_sli_ring *pring;
128062306a36Sopenharmony_ci
128162306a36Sopenharmony_ci	memset(pmb, 0, sizeof (LPFC_MBOXQ_t));
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci	mb->un.varCfgRing.ring = ring;
128462306a36Sopenharmony_ci	mb->un.varCfgRing.maxOrigXchg = 0;
128562306a36Sopenharmony_ci	mb->un.varCfgRing.maxRespXchg = 0;
128662306a36Sopenharmony_ci	mb->un.varCfgRing.recvNotify = 1;
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci	psli = &phba->sli;
128962306a36Sopenharmony_ci	pring = &psli->sli3_ring[ring];
129062306a36Sopenharmony_ci	mb->un.varCfgRing.numMask = pring->num_mask;
129162306a36Sopenharmony_ci	mb->mbxCommand = MBX_CONFIG_RING;
129262306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
129362306a36Sopenharmony_ci
129462306a36Sopenharmony_ci	/* Is this ring configured for a specific profile */
129562306a36Sopenharmony_ci	if (pring->prt[0].profile) {
129662306a36Sopenharmony_ci		mb->un.varCfgRing.profile = pring->prt[0].profile;
129762306a36Sopenharmony_ci		return;
129862306a36Sopenharmony_ci	}
129962306a36Sopenharmony_ci
130062306a36Sopenharmony_ci	/* Otherwise we setup specific rctl / type masks for this ring */
130162306a36Sopenharmony_ci	for (i = 0; i < pring->num_mask; i++) {
130262306a36Sopenharmony_ci		mb->un.varCfgRing.rrRegs[i].rval = pring->prt[i].rctl;
130362306a36Sopenharmony_ci		if (mb->un.varCfgRing.rrRegs[i].rval != FC_RCTL_ELS_REQ)
130462306a36Sopenharmony_ci			mb->un.varCfgRing.rrRegs[i].rmask = 0xff;
130562306a36Sopenharmony_ci		else
130662306a36Sopenharmony_ci			mb->un.varCfgRing.rrRegs[i].rmask = 0xfe;
130762306a36Sopenharmony_ci		mb->un.varCfgRing.rrRegs[i].tval = pring->prt[i].type;
130862306a36Sopenharmony_ci		mb->un.varCfgRing.rrRegs[i].tmask = 0xff;
130962306a36Sopenharmony_ci	}
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ci	return;
131262306a36Sopenharmony_ci}
131362306a36Sopenharmony_ci
131462306a36Sopenharmony_ci/**
131562306a36Sopenharmony_ci * lpfc_config_port - Prepare a mailbox command for configuring port
131662306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
131762306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
131862306a36Sopenharmony_ci *
131962306a36Sopenharmony_ci * The configure port mailbox command is used to identify the Port Control
132062306a36Sopenharmony_ci * Block (PCB) in the driver memory. After this command is issued, the
132162306a36Sopenharmony_ci * driver must not access the mailbox in the HBA without first resetting
132262306a36Sopenharmony_ci * the HBA. The HBA may copy the PCB information to internal storage for
132362306a36Sopenharmony_ci * subsequent use; the driver can not change the PCB information unless it
132462306a36Sopenharmony_ci * resets the HBA.
132562306a36Sopenharmony_ci *
132662306a36Sopenharmony_ci * This routine prepares the mailbox command for configuring port.
132762306a36Sopenharmony_ci **/
132862306a36Sopenharmony_civoid
132962306a36Sopenharmony_cilpfc_config_port(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
133062306a36Sopenharmony_ci{
133162306a36Sopenharmony_ci	MAILBOX_t __iomem *mb_slim = (MAILBOX_t __iomem *) phba->MBslimaddr;
133262306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
133362306a36Sopenharmony_ci	dma_addr_t pdma_addr;
133462306a36Sopenharmony_ci	uint32_t bar_low, bar_high;
133562306a36Sopenharmony_ci	size_t offset;
133662306a36Sopenharmony_ci	struct lpfc_hgp hgp;
133762306a36Sopenharmony_ci	int i;
133862306a36Sopenharmony_ci	uint32_t pgp_offset;
133962306a36Sopenharmony_ci
134062306a36Sopenharmony_ci	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
134162306a36Sopenharmony_ci	mb->mbxCommand = MBX_CONFIG_PORT;
134262306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
134362306a36Sopenharmony_ci
134462306a36Sopenharmony_ci	mb->un.varCfgPort.pcbLen = sizeof(PCB_t);
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci	offset = (uint8_t *)phba->pcb - (uint8_t *)phba->slim2p.virt;
134762306a36Sopenharmony_ci	pdma_addr = phba->slim2p.phys + offset;
134862306a36Sopenharmony_ci	mb->un.varCfgPort.pcbLow = putPaddrLow(pdma_addr);
134962306a36Sopenharmony_ci	mb->un.varCfgPort.pcbHigh = putPaddrHigh(pdma_addr);
135062306a36Sopenharmony_ci
135162306a36Sopenharmony_ci	/* Always Host Group Pointer is in SLIM */
135262306a36Sopenharmony_ci	mb->un.varCfgPort.hps = 1;
135362306a36Sopenharmony_ci
135462306a36Sopenharmony_ci	/* If HBA supports SLI=3 ask for it */
135562306a36Sopenharmony_ci
135662306a36Sopenharmony_ci	if (phba->sli_rev == LPFC_SLI_REV3 && phba->vpd.sli3Feat.cerbm) {
135762306a36Sopenharmony_ci		if (phba->cfg_enable_bg)
135862306a36Sopenharmony_ci			mb->un.varCfgPort.cbg = 1; /* configure BlockGuard */
135962306a36Sopenharmony_ci		mb->un.varCfgPort.cerbm = 1; /* Request HBQs */
136062306a36Sopenharmony_ci		mb->un.varCfgPort.ccrp = 1; /* Command Ring Polling */
136162306a36Sopenharmony_ci		mb->un.varCfgPort.max_hbq = lpfc_sli_hbq_count();
136262306a36Sopenharmony_ci		if (phba->max_vpi && phba->cfg_enable_npiv &&
136362306a36Sopenharmony_ci		    phba->vpd.sli3Feat.cmv) {
136462306a36Sopenharmony_ci			mb->un.varCfgPort.max_vpi = LPFC_MAX_VPI;
136562306a36Sopenharmony_ci			mb->un.varCfgPort.cmv = 1;
136662306a36Sopenharmony_ci		} else
136762306a36Sopenharmony_ci			mb->un.varCfgPort.max_vpi = phba->max_vpi = 0;
136862306a36Sopenharmony_ci	} else
136962306a36Sopenharmony_ci		phba->sli_rev = LPFC_SLI_REV2;
137062306a36Sopenharmony_ci	mb->un.varCfgPort.sli_mode = phba->sli_rev;
137162306a36Sopenharmony_ci
137262306a36Sopenharmony_ci	/* If this is an SLI3 port, configure async status notification. */
137362306a36Sopenharmony_ci	if (phba->sli_rev == LPFC_SLI_REV3)
137462306a36Sopenharmony_ci		mb->un.varCfgPort.casabt = 1;
137562306a36Sopenharmony_ci
137662306a36Sopenharmony_ci	/* Now setup pcb */
137762306a36Sopenharmony_ci	phba->pcb->type = TYPE_NATIVE_SLI2;
137862306a36Sopenharmony_ci	phba->pcb->feature = FEATURE_INITIAL_SLI2;
137962306a36Sopenharmony_ci
138062306a36Sopenharmony_ci	/* Setup Mailbox pointers */
138162306a36Sopenharmony_ci	phba->pcb->mailBoxSize = sizeof(MAILBOX_t) + MAILBOX_EXT_SIZE;
138262306a36Sopenharmony_ci	offset = (uint8_t *)phba->mbox - (uint8_t *)phba->slim2p.virt;
138362306a36Sopenharmony_ci	pdma_addr = phba->slim2p.phys + offset;
138462306a36Sopenharmony_ci	phba->pcb->mbAddrHigh = putPaddrHigh(pdma_addr);
138562306a36Sopenharmony_ci	phba->pcb->mbAddrLow = putPaddrLow(pdma_addr);
138662306a36Sopenharmony_ci
138762306a36Sopenharmony_ci	/*
138862306a36Sopenharmony_ci	 * Setup Host Group ring pointer.
138962306a36Sopenharmony_ci	 *
139062306a36Sopenharmony_ci	 * For efficiency reasons, the ring get/put pointers can be
139162306a36Sopenharmony_ci	 * placed in adapter memory (SLIM) rather than in host memory.
139262306a36Sopenharmony_ci	 * This allows firmware to avoid PCI reads/writes when updating
139362306a36Sopenharmony_ci	 * and checking pointers.
139462306a36Sopenharmony_ci	 *
139562306a36Sopenharmony_ci	 * The firmware recognizes the use of SLIM memory by comparing
139662306a36Sopenharmony_ci	 * the address of the get/put pointers structure with that of
139762306a36Sopenharmony_ci	 * the SLIM BAR (BAR0).
139862306a36Sopenharmony_ci	 *
139962306a36Sopenharmony_ci	 * Caution: be sure to use the PCI config space value of BAR0/BAR1
140062306a36Sopenharmony_ci	 * (the hardware's view of the base address), not the OS's
140162306a36Sopenharmony_ci	 * value of pci_resource_start() as the OS value may be a cookie
140262306a36Sopenharmony_ci	 * for ioremap/iomap.
140362306a36Sopenharmony_ci	 */
140462306a36Sopenharmony_ci
140562306a36Sopenharmony_ci
140662306a36Sopenharmony_ci	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_0, &bar_low);
140762306a36Sopenharmony_ci	pci_read_config_dword(phba->pcidev, PCI_BASE_ADDRESS_1, &bar_high);
140862306a36Sopenharmony_ci
140962306a36Sopenharmony_ci	/*
141062306a36Sopenharmony_ci	 * Set up HGP - Port Memory
141162306a36Sopenharmony_ci	 *
141262306a36Sopenharmony_ci	 * The port expects the host get/put pointers to reside in memory
141362306a36Sopenharmony_ci	 * following the "non-diagnostic" mode mailbox (32 words, 0x80 bytes)
141462306a36Sopenharmony_ci	 * area of SLIM.  In SLI-2 mode, there's an additional 16 reserved
141562306a36Sopenharmony_ci	 * words (0x40 bytes).  This area is not reserved if HBQs are
141662306a36Sopenharmony_ci	 * configured in SLI-3.
141762306a36Sopenharmony_ci	 *
141862306a36Sopenharmony_ci	 * CR0Put    - SLI2(no HBQs) = 0xc0, With HBQs = 0x80
141962306a36Sopenharmony_ci	 * RR0Get                      0xc4              0x84
142062306a36Sopenharmony_ci	 * CR1Put                      0xc8              0x88
142162306a36Sopenharmony_ci	 * RR1Get                      0xcc              0x8c
142262306a36Sopenharmony_ci	 * CR2Put                      0xd0              0x90
142362306a36Sopenharmony_ci	 * RR2Get                      0xd4              0x94
142462306a36Sopenharmony_ci	 * CR3Put                      0xd8              0x98
142562306a36Sopenharmony_ci	 * RR3Get                      0xdc              0x9c
142662306a36Sopenharmony_ci	 *
142762306a36Sopenharmony_ci	 * Reserved                    0xa0-0xbf
142862306a36Sopenharmony_ci	 *    If HBQs configured:
142962306a36Sopenharmony_ci	 *                         HBQ 0 Put ptr  0xc0
143062306a36Sopenharmony_ci	 *                         HBQ 1 Put ptr  0xc4
143162306a36Sopenharmony_ci	 *                         HBQ 2 Put ptr  0xc8
143262306a36Sopenharmony_ci	 *                         ......
143362306a36Sopenharmony_ci	 *                         HBQ(M-1)Put Pointer 0xc0+(M-1)*4
143462306a36Sopenharmony_ci	 *
143562306a36Sopenharmony_ci	 */
143662306a36Sopenharmony_ci
143762306a36Sopenharmony_ci	if (phba->cfg_hostmem_hgp && phba->sli_rev != 3) {
143862306a36Sopenharmony_ci		phba->host_gp = (struct lpfc_hgp __iomem *)
143962306a36Sopenharmony_ci				 &phba->mbox->us.s2.host[0];
144062306a36Sopenharmony_ci		phba->hbq_put = NULL;
144162306a36Sopenharmony_ci		offset = (uint8_t *)&phba->mbox->us.s2.host -
144262306a36Sopenharmony_ci			(uint8_t *)phba->slim2p.virt;
144362306a36Sopenharmony_ci		pdma_addr = phba->slim2p.phys + offset;
144462306a36Sopenharmony_ci		phba->pcb->hgpAddrHigh = putPaddrHigh(pdma_addr);
144562306a36Sopenharmony_ci		phba->pcb->hgpAddrLow = putPaddrLow(pdma_addr);
144662306a36Sopenharmony_ci	} else {
144762306a36Sopenharmony_ci		/* Always Host Group Pointer is in SLIM */
144862306a36Sopenharmony_ci		mb->un.varCfgPort.hps = 1;
144962306a36Sopenharmony_ci
145062306a36Sopenharmony_ci		if (phba->sli_rev == 3) {
145162306a36Sopenharmony_ci			phba->host_gp = &mb_slim->us.s3.host[0];
145262306a36Sopenharmony_ci			phba->hbq_put = &mb_slim->us.s3.hbq_put[0];
145362306a36Sopenharmony_ci		} else {
145462306a36Sopenharmony_ci			phba->host_gp = &mb_slim->us.s2.host[0];
145562306a36Sopenharmony_ci			phba->hbq_put = NULL;
145662306a36Sopenharmony_ci		}
145762306a36Sopenharmony_ci
145862306a36Sopenharmony_ci		/* mask off BAR0's flag bits 0 - 3 */
145962306a36Sopenharmony_ci		phba->pcb->hgpAddrLow = (bar_low & PCI_BASE_ADDRESS_MEM_MASK) +
146062306a36Sopenharmony_ci			(void __iomem *)phba->host_gp -
146162306a36Sopenharmony_ci			(void __iomem *)phba->MBslimaddr;
146262306a36Sopenharmony_ci		if (bar_low & PCI_BASE_ADDRESS_MEM_TYPE_64)
146362306a36Sopenharmony_ci			phba->pcb->hgpAddrHigh = bar_high;
146462306a36Sopenharmony_ci		else
146562306a36Sopenharmony_ci			phba->pcb->hgpAddrHigh = 0;
146662306a36Sopenharmony_ci		/* write HGP data to SLIM at the required longword offset */
146762306a36Sopenharmony_ci		memset(&hgp, 0, sizeof(struct lpfc_hgp));
146862306a36Sopenharmony_ci
146962306a36Sopenharmony_ci		for (i = 0; i < phba->sli.num_rings; i++) {
147062306a36Sopenharmony_ci			lpfc_memcpy_to_slim(phba->host_gp + i, &hgp,
147162306a36Sopenharmony_ci				    sizeof(*phba->host_gp));
147262306a36Sopenharmony_ci		}
147362306a36Sopenharmony_ci	}
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci	/* Setup Port Group offset */
147662306a36Sopenharmony_ci	if (phba->sli_rev == 3)
147762306a36Sopenharmony_ci		pgp_offset = offsetof(struct lpfc_sli2_slim,
147862306a36Sopenharmony_ci				      mbx.us.s3_pgp.port);
147962306a36Sopenharmony_ci	else
148062306a36Sopenharmony_ci		pgp_offset = offsetof(struct lpfc_sli2_slim, mbx.us.s2.port);
148162306a36Sopenharmony_ci	pdma_addr = phba->slim2p.phys + pgp_offset;
148262306a36Sopenharmony_ci	phba->pcb->pgpAddrHigh = putPaddrHigh(pdma_addr);
148362306a36Sopenharmony_ci	phba->pcb->pgpAddrLow = putPaddrLow(pdma_addr);
148462306a36Sopenharmony_ci
148562306a36Sopenharmony_ci	/* Use callback routine to setp rings in the pcb */
148662306a36Sopenharmony_ci	lpfc_config_pcb_setup(phba);
148762306a36Sopenharmony_ci
148862306a36Sopenharmony_ci	/* special handling for LC HBAs */
148962306a36Sopenharmony_ci	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
149062306a36Sopenharmony_ci		uint32_t hbainit[5];
149162306a36Sopenharmony_ci
149262306a36Sopenharmony_ci		lpfc_hba_init(phba, hbainit);
149362306a36Sopenharmony_ci
149462306a36Sopenharmony_ci		memcpy(&mb->un.varCfgPort.hbainit, hbainit, 20);
149562306a36Sopenharmony_ci	}
149662306a36Sopenharmony_ci
149762306a36Sopenharmony_ci	/* Swap PCB if needed */
149862306a36Sopenharmony_ci	lpfc_sli_pcimem_bcopy(phba->pcb, phba->pcb, sizeof(PCB_t));
149962306a36Sopenharmony_ci}
150062306a36Sopenharmony_ci
150162306a36Sopenharmony_ci/**
150262306a36Sopenharmony_ci * lpfc_kill_board - Prepare a mailbox command for killing board
150362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
150462306a36Sopenharmony_ci * @pmb: pointer to the driver internal queue element for mailbox command.
150562306a36Sopenharmony_ci *
150662306a36Sopenharmony_ci * The kill board mailbox command is used to tell firmware to perform a
150762306a36Sopenharmony_ci * graceful shutdown of a channel on a specified board to prepare for reset.
150862306a36Sopenharmony_ci * When the kill board mailbox command is received, the ER3 bit is set to 1
150962306a36Sopenharmony_ci * in the Host Status register and the ER Attention bit is set to 1 in the
151062306a36Sopenharmony_ci * Host Attention register of the HBA function that received the kill board
151162306a36Sopenharmony_ci * command.
151262306a36Sopenharmony_ci *
151362306a36Sopenharmony_ci * This routine prepares the mailbox command for killing the board in
151462306a36Sopenharmony_ci * preparation for a graceful shutdown.
151562306a36Sopenharmony_ci **/
151662306a36Sopenharmony_civoid
151762306a36Sopenharmony_cilpfc_kill_board(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
151862306a36Sopenharmony_ci{
151962306a36Sopenharmony_ci	MAILBOX_t *mb = &pmb->u.mb;
152062306a36Sopenharmony_ci
152162306a36Sopenharmony_ci	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
152262306a36Sopenharmony_ci	mb->mbxCommand = MBX_KILL_BOARD;
152362306a36Sopenharmony_ci	mb->mbxOwner = OWN_HOST;
152462306a36Sopenharmony_ci	return;
152562306a36Sopenharmony_ci}
152662306a36Sopenharmony_ci
152762306a36Sopenharmony_ci/**
152862306a36Sopenharmony_ci * lpfc_mbox_put - Put a mailbox cmd into the tail of driver's mailbox queue
152962306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
153062306a36Sopenharmony_ci * @mbq: pointer to the driver internal queue element for mailbox command.
153162306a36Sopenharmony_ci *
153262306a36Sopenharmony_ci * Driver maintains a internal mailbox command queue implemented as a linked
153362306a36Sopenharmony_ci * list. When a mailbox command is issued, it shall be put into the mailbox
153462306a36Sopenharmony_ci * command queue such that they shall be processed orderly as HBA can process
153562306a36Sopenharmony_ci * one mailbox command at a time.
153662306a36Sopenharmony_ci **/
153762306a36Sopenharmony_civoid
153862306a36Sopenharmony_cilpfc_mbox_put(struct lpfc_hba * phba, LPFC_MBOXQ_t * mbq)
153962306a36Sopenharmony_ci{
154062306a36Sopenharmony_ci	struct lpfc_sli *psli;
154162306a36Sopenharmony_ci
154262306a36Sopenharmony_ci	psli = &phba->sli;
154362306a36Sopenharmony_ci
154462306a36Sopenharmony_ci	list_add_tail(&mbq->list, &psli->mboxq);
154562306a36Sopenharmony_ci
154662306a36Sopenharmony_ci	psli->mboxq_cnt++;
154762306a36Sopenharmony_ci
154862306a36Sopenharmony_ci	return;
154962306a36Sopenharmony_ci}
155062306a36Sopenharmony_ci
155162306a36Sopenharmony_ci/**
155262306a36Sopenharmony_ci * lpfc_mbox_get - Remove a mailbox cmd from the head of driver's mailbox queue
155362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
155462306a36Sopenharmony_ci *
155562306a36Sopenharmony_ci * Driver maintains a internal mailbox command queue implemented as a linked
155662306a36Sopenharmony_ci * list. When a mailbox command is issued, it shall be put into the mailbox
155762306a36Sopenharmony_ci * command queue such that they shall be processed orderly as HBA can process
155862306a36Sopenharmony_ci * one mailbox command at a time. After HBA finished processing a mailbox
155962306a36Sopenharmony_ci * command, the driver will remove a pending mailbox command from the head of
156062306a36Sopenharmony_ci * the mailbox command queue and send to the HBA for processing.
156162306a36Sopenharmony_ci *
156262306a36Sopenharmony_ci * Return codes
156362306a36Sopenharmony_ci *    pointer to the driver internal queue element for mailbox command.
156462306a36Sopenharmony_ci **/
156562306a36Sopenharmony_ciLPFC_MBOXQ_t *
156662306a36Sopenharmony_cilpfc_mbox_get(struct lpfc_hba * phba)
156762306a36Sopenharmony_ci{
156862306a36Sopenharmony_ci	LPFC_MBOXQ_t *mbq = NULL;
156962306a36Sopenharmony_ci	struct lpfc_sli *psli = &phba->sli;
157062306a36Sopenharmony_ci
157162306a36Sopenharmony_ci	list_remove_head((&psli->mboxq), mbq, LPFC_MBOXQ_t, list);
157262306a36Sopenharmony_ci	if (mbq)
157362306a36Sopenharmony_ci		psli->mboxq_cnt--;
157462306a36Sopenharmony_ci
157562306a36Sopenharmony_ci	return mbq;
157662306a36Sopenharmony_ci}
157762306a36Sopenharmony_ci
157862306a36Sopenharmony_ci/**
157962306a36Sopenharmony_ci * __lpfc_mbox_cmpl_put - Put mailbox cmd into mailbox cmd complete list
158062306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
158162306a36Sopenharmony_ci * @mbq: pointer to the driver internal queue element for mailbox command.
158262306a36Sopenharmony_ci *
158362306a36Sopenharmony_ci * This routine put the completed mailbox command into the mailbox command
158462306a36Sopenharmony_ci * complete list. This is the unlocked version of the routine. The mailbox
158562306a36Sopenharmony_ci * complete list is used by the driver worker thread to process mailbox
158662306a36Sopenharmony_ci * complete callback functions outside the driver interrupt handler.
158762306a36Sopenharmony_ci **/
158862306a36Sopenharmony_civoid
158962306a36Sopenharmony_ci__lpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
159062306a36Sopenharmony_ci{
159162306a36Sopenharmony_ci	list_add_tail(&mbq->list, &phba->sli.mboxq_cmpl);
159262306a36Sopenharmony_ci}
159362306a36Sopenharmony_ci
159462306a36Sopenharmony_ci/**
159562306a36Sopenharmony_ci * lpfc_mbox_cmpl_put - Put mailbox command into mailbox command complete list
159662306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
159762306a36Sopenharmony_ci * @mbq: pointer to the driver internal queue element for mailbox command.
159862306a36Sopenharmony_ci *
159962306a36Sopenharmony_ci * This routine put the completed mailbox command into the mailbox command
160062306a36Sopenharmony_ci * complete list. This is the locked version of the routine. The mailbox
160162306a36Sopenharmony_ci * complete list is used by the driver worker thread to process mailbox
160262306a36Sopenharmony_ci * complete callback functions outside the driver interrupt handler.
160362306a36Sopenharmony_ci **/
160462306a36Sopenharmony_civoid
160562306a36Sopenharmony_cilpfc_mbox_cmpl_put(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbq)
160662306a36Sopenharmony_ci{
160762306a36Sopenharmony_ci	unsigned long iflag;
160862306a36Sopenharmony_ci
160962306a36Sopenharmony_ci	/* This function expects to be called from interrupt context */
161062306a36Sopenharmony_ci	spin_lock_irqsave(&phba->hbalock, iflag);
161162306a36Sopenharmony_ci	__lpfc_mbox_cmpl_put(phba, mbq);
161262306a36Sopenharmony_ci	spin_unlock_irqrestore(&phba->hbalock, iflag);
161362306a36Sopenharmony_ci	return;
161462306a36Sopenharmony_ci}
161562306a36Sopenharmony_ci
161662306a36Sopenharmony_ci/**
161762306a36Sopenharmony_ci * lpfc_mbox_cmd_check - Check the validality of a mailbox command
161862306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
161962306a36Sopenharmony_ci * @mboxq: pointer to the driver internal queue element for mailbox command.
162062306a36Sopenharmony_ci *
162162306a36Sopenharmony_ci * This routine is to check whether a mailbox command is valid to be issued.
162262306a36Sopenharmony_ci * This check will be performed by both the mailbox issue API when a client
162362306a36Sopenharmony_ci * is to issue a mailbox command to the mailbox transport.
162462306a36Sopenharmony_ci *
162562306a36Sopenharmony_ci * Return 0 - pass the check, -ENODEV - fail the check
162662306a36Sopenharmony_ci **/
162762306a36Sopenharmony_ciint
162862306a36Sopenharmony_cilpfc_mbox_cmd_check(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
162962306a36Sopenharmony_ci{
163062306a36Sopenharmony_ci	/* Mailbox command that have a completion handler must also have a
163162306a36Sopenharmony_ci	 * vport specified.
163262306a36Sopenharmony_ci	 */
163362306a36Sopenharmony_ci	if (mboxq->mbox_cmpl && mboxq->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
163462306a36Sopenharmony_ci	    mboxq->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
163562306a36Sopenharmony_ci		if (!mboxq->vport) {
163662306a36Sopenharmony_ci			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
163762306a36Sopenharmony_ci					"1814 Mbox x%x failed, no vport\n",
163862306a36Sopenharmony_ci					mboxq->u.mb.mbxCommand);
163962306a36Sopenharmony_ci			dump_stack();
164062306a36Sopenharmony_ci			return -ENODEV;
164162306a36Sopenharmony_ci		}
164262306a36Sopenharmony_ci	}
164362306a36Sopenharmony_ci	return 0;
164462306a36Sopenharmony_ci}
164562306a36Sopenharmony_ci
164662306a36Sopenharmony_ci/**
164762306a36Sopenharmony_ci * lpfc_mbox_dev_check - Check the device state for issuing a mailbox command
164862306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
164962306a36Sopenharmony_ci *
165062306a36Sopenharmony_ci * This routine is to check whether the HBA device is ready for posting a
165162306a36Sopenharmony_ci * mailbox command. It is used by the mailbox transport API at the time the
165262306a36Sopenharmony_ci * to post a mailbox command to the device.
165362306a36Sopenharmony_ci *
165462306a36Sopenharmony_ci * Return 0 - pass the check, -ENODEV - fail the check
165562306a36Sopenharmony_ci **/
165662306a36Sopenharmony_ciint
165762306a36Sopenharmony_cilpfc_mbox_dev_check(struct lpfc_hba *phba)
165862306a36Sopenharmony_ci{
165962306a36Sopenharmony_ci	/* If the PCI channel is in offline state, do not issue mbox */
166062306a36Sopenharmony_ci	if (unlikely(pci_channel_offline(phba->pcidev)))
166162306a36Sopenharmony_ci		return -ENODEV;
166262306a36Sopenharmony_ci
166362306a36Sopenharmony_ci	/* If the HBA is in error state, do not issue mbox */
166462306a36Sopenharmony_ci	if (phba->link_state == LPFC_HBA_ERROR)
166562306a36Sopenharmony_ci		return -ENODEV;
166662306a36Sopenharmony_ci
166762306a36Sopenharmony_ci	return 0;
166862306a36Sopenharmony_ci}
166962306a36Sopenharmony_ci
167062306a36Sopenharmony_ci/**
167162306a36Sopenharmony_ci * lpfc_mbox_tmo_val - Retrieve mailbox command timeout value
167262306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
167362306a36Sopenharmony_ci * @mboxq: pointer to the driver internal queue element for mailbox command.
167462306a36Sopenharmony_ci *
167562306a36Sopenharmony_ci * This routine retrieves the proper timeout value according to the mailbox
167662306a36Sopenharmony_ci * command code.
167762306a36Sopenharmony_ci *
167862306a36Sopenharmony_ci * Return codes
167962306a36Sopenharmony_ci *    Timeout value to be used for the given mailbox command
168062306a36Sopenharmony_ci **/
168162306a36Sopenharmony_ciint
168262306a36Sopenharmony_cilpfc_mbox_tmo_val(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
168362306a36Sopenharmony_ci{
168462306a36Sopenharmony_ci	MAILBOX_t *mbox = &mboxq->u.mb;
168562306a36Sopenharmony_ci	uint8_t subsys, opcode;
168662306a36Sopenharmony_ci
168762306a36Sopenharmony_ci	switch (mbox->mbxCommand) {
168862306a36Sopenharmony_ci	case MBX_WRITE_NV:	/* 0x03 */
168962306a36Sopenharmony_ci	case MBX_DUMP_MEMORY:	/* 0x17 */
169062306a36Sopenharmony_ci	case MBX_UPDATE_CFG:	/* 0x1B */
169162306a36Sopenharmony_ci	case MBX_DOWN_LOAD:	/* 0x1C */
169262306a36Sopenharmony_ci	case MBX_DEL_LD_ENTRY:	/* 0x1D */
169362306a36Sopenharmony_ci	case MBX_WRITE_VPARMS:	/* 0x32 */
169462306a36Sopenharmony_ci	case MBX_LOAD_AREA:	/* 0x81 */
169562306a36Sopenharmony_ci	case MBX_WRITE_WWN:     /* 0x98 */
169662306a36Sopenharmony_ci	case MBX_LOAD_EXP_ROM:	/* 0x9C */
169762306a36Sopenharmony_ci	case MBX_ACCESS_VDATA:	/* 0xA5 */
169862306a36Sopenharmony_ci		return LPFC_MBOX_TMO_FLASH_CMD;
169962306a36Sopenharmony_ci	case MBX_SLI4_CONFIG:	/* 0x9b */
170062306a36Sopenharmony_ci		subsys = lpfc_sli_config_mbox_subsys_get(phba, mboxq);
170162306a36Sopenharmony_ci		opcode = lpfc_sli_config_mbox_opcode_get(phba, mboxq);
170262306a36Sopenharmony_ci		if (subsys == LPFC_MBOX_SUBSYSTEM_COMMON) {
170362306a36Sopenharmony_ci			switch (opcode) {
170462306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_READ_OBJECT:
170562306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_WRITE_OBJECT:
170662306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_READ_OBJECT_LIST:
170762306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_DELETE_OBJECT:
170862306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_GET_PROFILE_LIST:
170962306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_SET_ACT_PROFILE:
171062306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_GET_PROFILE_CONFIG:
171162306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_SET_PROFILE_CONFIG:
171262306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_GET_FACTORY_PROFILE_CONFIG:
171362306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_GET_PROFILE_CAPACITIES:
171462306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_SEND_ACTIVATION:
171562306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_RESET_LICENSES:
171662306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_SET_BOOT_CONFIG:
171762306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_GET_VPD_DATA:
171862306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_SET_PHYSICAL_LINK_CONFIG:
171962306a36Sopenharmony_ci				return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
172062306a36Sopenharmony_ci			}
172162306a36Sopenharmony_ci		}
172262306a36Sopenharmony_ci		if (subsys == LPFC_MBOX_SUBSYSTEM_FCOE) {
172362306a36Sopenharmony_ci			switch (opcode) {
172462306a36Sopenharmony_ci			case LPFC_MBOX_OPCODE_FCOE_SET_FCLINK_SETTINGS:
172562306a36Sopenharmony_ci				return LPFC_MBOX_SLI4_CONFIG_EXTENDED_TMO;
172662306a36Sopenharmony_ci			}
172762306a36Sopenharmony_ci		}
172862306a36Sopenharmony_ci		return LPFC_MBOX_SLI4_CONFIG_TMO;
172962306a36Sopenharmony_ci	}
173062306a36Sopenharmony_ci	return LPFC_MBOX_TMO;
173162306a36Sopenharmony_ci}
173262306a36Sopenharmony_ci
173362306a36Sopenharmony_ci/**
173462306a36Sopenharmony_ci * lpfc_sli4_mbx_sge_set - Set a sge entry in non-embedded mailbox command
173562306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command.
173662306a36Sopenharmony_ci * @sgentry: sge entry index.
173762306a36Sopenharmony_ci * @phyaddr: physical address for the sge
173862306a36Sopenharmony_ci * @length: Length of the sge.
173962306a36Sopenharmony_ci *
174062306a36Sopenharmony_ci * This routine sets up an entry in the non-embedded mailbox command at the sge
174162306a36Sopenharmony_ci * index location.
174262306a36Sopenharmony_ci **/
174362306a36Sopenharmony_civoid
174462306a36Sopenharmony_cilpfc_sli4_mbx_sge_set(struct lpfcMboxq *mbox, uint32_t sgentry,
174562306a36Sopenharmony_ci		      dma_addr_t phyaddr, uint32_t length)
174662306a36Sopenharmony_ci{
174762306a36Sopenharmony_ci	struct lpfc_mbx_nembed_cmd *nembed_sge;
174862306a36Sopenharmony_ci
174962306a36Sopenharmony_ci	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
175062306a36Sopenharmony_ci				&mbox->u.mqe.un.nembed_cmd;
175162306a36Sopenharmony_ci	nembed_sge->sge[sgentry].pa_lo = putPaddrLow(phyaddr);
175262306a36Sopenharmony_ci	nembed_sge->sge[sgentry].pa_hi = putPaddrHigh(phyaddr);
175362306a36Sopenharmony_ci	nembed_sge->sge[sgentry].length = length;
175462306a36Sopenharmony_ci}
175562306a36Sopenharmony_ci
175662306a36Sopenharmony_ci/**
175762306a36Sopenharmony_ci * lpfc_sli4_mbx_sge_get - Get a sge entry from non-embedded mailbox command
175862306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command.
175962306a36Sopenharmony_ci * @sgentry: sge entry index.
176062306a36Sopenharmony_ci * @sge: pointer to lpfc mailbox sge to load into.
176162306a36Sopenharmony_ci *
176262306a36Sopenharmony_ci * This routine gets an entry from the non-embedded mailbox command at the sge
176362306a36Sopenharmony_ci * index location.
176462306a36Sopenharmony_ci **/
176562306a36Sopenharmony_civoid
176662306a36Sopenharmony_cilpfc_sli4_mbx_sge_get(struct lpfcMboxq *mbox, uint32_t sgentry,
176762306a36Sopenharmony_ci		      struct lpfc_mbx_sge *sge)
176862306a36Sopenharmony_ci{
176962306a36Sopenharmony_ci	struct lpfc_mbx_nembed_cmd *nembed_sge;
177062306a36Sopenharmony_ci
177162306a36Sopenharmony_ci	nembed_sge = (struct lpfc_mbx_nembed_cmd *)
177262306a36Sopenharmony_ci				&mbox->u.mqe.un.nembed_cmd;
177362306a36Sopenharmony_ci	sge->pa_lo = nembed_sge->sge[sgentry].pa_lo;
177462306a36Sopenharmony_ci	sge->pa_hi = nembed_sge->sge[sgentry].pa_hi;
177562306a36Sopenharmony_ci	sge->length = nembed_sge->sge[sgentry].length;
177662306a36Sopenharmony_ci}
177762306a36Sopenharmony_ci
177862306a36Sopenharmony_ci/**
177962306a36Sopenharmony_ci * lpfc_sli4_mbox_cmd_free - Free a sli4 mailbox command
178062306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
178162306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command.
178262306a36Sopenharmony_ci *
178362306a36Sopenharmony_ci * This routine cleans up and releases an SLI4 mailbox command that was
178462306a36Sopenharmony_ci * configured using lpfc_sli4_config.  It accounts for the embedded and
178562306a36Sopenharmony_ci * non-embedded config types.
178662306a36Sopenharmony_ci **/
178762306a36Sopenharmony_civoid
178862306a36Sopenharmony_cilpfc_sli4_mbox_cmd_free(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
178962306a36Sopenharmony_ci{
179062306a36Sopenharmony_ci	struct lpfc_mbx_sli4_config *sli4_cfg;
179162306a36Sopenharmony_ci	struct lpfc_mbx_sge sge;
179262306a36Sopenharmony_ci	dma_addr_t phyaddr;
179362306a36Sopenharmony_ci	uint32_t sgecount, sgentry;
179462306a36Sopenharmony_ci
179562306a36Sopenharmony_ci	sli4_cfg = &mbox->u.mqe.un.sli4_config;
179662306a36Sopenharmony_ci
179762306a36Sopenharmony_ci	/* For embedded mbox command, just free the mbox command */
179862306a36Sopenharmony_ci	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
179962306a36Sopenharmony_ci		mempool_free(mbox, phba->mbox_mem_pool);
180062306a36Sopenharmony_ci		return;
180162306a36Sopenharmony_ci	}
180262306a36Sopenharmony_ci
180362306a36Sopenharmony_ci	/* For non-embedded mbox command, we need to free the pages first */
180462306a36Sopenharmony_ci	sgecount = bf_get(lpfc_mbox_hdr_sge_cnt, &sli4_cfg->header.cfg_mhdr);
180562306a36Sopenharmony_ci	/* There is nothing we can do if there is no sge address array */
180662306a36Sopenharmony_ci	if (unlikely(!mbox->sge_array)) {
180762306a36Sopenharmony_ci		mempool_free(mbox, phba->mbox_mem_pool);
180862306a36Sopenharmony_ci		return;
180962306a36Sopenharmony_ci	}
181062306a36Sopenharmony_ci	/* Each non-embedded DMA memory was allocated in the length of a page */
181162306a36Sopenharmony_ci	for (sgentry = 0; sgentry < sgecount; sgentry++) {
181262306a36Sopenharmony_ci		lpfc_sli4_mbx_sge_get(mbox, sgentry, &sge);
181362306a36Sopenharmony_ci		phyaddr = getPaddr(sge.pa_hi, sge.pa_lo);
181462306a36Sopenharmony_ci		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
181562306a36Sopenharmony_ci				  mbox->sge_array->addr[sgentry], phyaddr);
181662306a36Sopenharmony_ci	}
181762306a36Sopenharmony_ci	/* Free the sge address array memory */
181862306a36Sopenharmony_ci	kfree(mbox->sge_array);
181962306a36Sopenharmony_ci	/* Finally, free the mailbox command itself */
182062306a36Sopenharmony_ci	mempool_free(mbox, phba->mbox_mem_pool);
182162306a36Sopenharmony_ci}
182262306a36Sopenharmony_ci
182362306a36Sopenharmony_ci/**
182462306a36Sopenharmony_ci * lpfc_sli4_config - Initialize the  SLI4 Config Mailbox command
182562306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
182662306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command.
182762306a36Sopenharmony_ci * @subsystem: The sli4 config sub mailbox subsystem.
182862306a36Sopenharmony_ci * @opcode: The sli4 config sub mailbox command opcode.
182962306a36Sopenharmony_ci * @length: Length of the sli4 config mailbox command (including sub-header).
183062306a36Sopenharmony_ci * @emb: True if embedded mbox command should be setup.
183162306a36Sopenharmony_ci *
183262306a36Sopenharmony_ci * This routine sets up the header fields of SLI4 specific mailbox command
183362306a36Sopenharmony_ci * for sending IOCTL command.
183462306a36Sopenharmony_ci *
183562306a36Sopenharmony_ci * Return: the actual length of the mbox command allocated (mostly useful
183662306a36Sopenharmony_ci *         for none embedded mailbox command).
183762306a36Sopenharmony_ci **/
183862306a36Sopenharmony_ciint
183962306a36Sopenharmony_cilpfc_sli4_config(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
184062306a36Sopenharmony_ci		 uint8_t subsystem, uint8_t opcode, uint32_t length, bool emb)
184162306a36Sopenharmony_ci{
184262306a36Sopenharmony_ci	struct lpfc_mbx_sli4_config *sli4_config;
184362306a36Sopenharmony_ci	union lpfc_sli4_cfg_shdr *cfg_shdr = NULL;
184462306a36Sopenharmony_ci	uint32_t alloc_len;
184562306a36Sopenharmony_ci	uint32_t resid_len;
184662306a36Sopenharmony_ci	uint32_t pagen, pcount;
184762306a36Sopenharmony_ci	void *viraddr;
184862306a36Sopenharmony_ci	dma_addr_t phyaddr;
184962306a36Sopenharmony_ci
185062306a36Sopenharmony_ci	/* Set up SLI4 mailbox command header fields */
185162306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
185262306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_SLI4_CONFIG);
185362306a36Sopenharmony_ci
185462306a36Sopenharmony_ci	/* Set up SLI4 ioctl command header fields */
185562306a36Sopenharmony_ci	sli4_config = &mbox->u.mqe.un.sli4_config;
185662306a36Sopenharmony_ci
185762306a36Sopenharmony_ci	/* Setup for the embedded mbox command */
185862306a36Sopenharmony_ci	if (emb) {
185962306a36Sopenharmony_ci		/* Set up main header fields */
186062306a36Sopenharmony_ci		bf_set(lpfc_mbox_hdr_emb, &sli4_config->header.cfg_mhdr, 1);
186162306a36Sopenharmony_ci		sli4_config->header.cfg_mhdr.payload_length = length;
186262306a36Sopenharmony_ci		/* Set up sub-header fields following main header */
186362306a36Sopenharmony_ci		bf_set(lpfc_mbox_hdr_opcode,
186462306a36Sopenharmony_ci			&sli4_config->header.cfg_shdr.request, opcode);
186562306a36Sopenharmony_ci		bf_set(lpfc_mbox_hdr_subsystem,
186662306a36Sopenharmony_ci			&sli4_config->header.cfg_shdr.request, subsystem);
186762306a36Sopenharmony_ci		sli4_config->header.cfg_shdr.request.request_length =
186862306a36Sopenharmony_ci			length - LPFC_MBX_CMD_HDR_LENGTH;
186962306a36Sopenharmony_ci		return length;
187062306a36Sopenharmony_ci	}
187162306a36Sopenharmony_ci
187262306a36Sopenharmony_ci	/* Setup for the non-embedded mbox command */
187362306a36Sopenharmony_ci	pcount = (SLI4_PAGE_ALIGN(length))/SLI4_PAGE_SIZE;
187462306a36Sopenharmony_ci	pcount = (pcount > LPFC_SLI4_MBX_SGE_MAX_PAGES) ?
187562306a36Sopenharmony_ci				LPFC_SLI4_MBX_SGE_MAX_PAGES : pcount;
187662306a36Sopenharmony_ci	/* Allocate record for keeping SGE virtual addresses */
187762306a36Sopenharmony_ci	mbox->sge_array = kzalloc(sizeof(struct lpfc_mbx_nembed_sge_virt),
187862306a36Sopenharmony_ci				  GFP_KERNEL);
187962306a36Sopenharmony_ci	if (!mbox->sge_array) {
188062306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
188162306a36Sopenharmony_ci				"2527 Failed to allocate non-embedded SGE "
188262306a36Sopenharmony_ci				"array.\n");
188362306a36Sopenharmony_ci		return 0;
188462306a36Sopenharmony_ci	}
188562306a36Sopenharmony_ci	for (pagen = 0, alloc_len = 0; pagen < pcount; pagen++) {
188662306a36Sopenharmony_ci		/* The DMA memory is always allocated in the length of a
188762306a36Sopenharmony_ci		 * page even though the last SGE might not fill up to a
188862306a36Sopenharmony_ci		 * page, this is used as a priori size of SLI4_PAGE_SIZE for
188962306a36Sopenharmony_ci		 * the later DMA memory free.
189062306a36Sopenharmony_ci		 */
189162306a36Sopenharmony_ci		viraddr = dma_alloc_coherent(&phba->pcidev->dev,
189262306a36Sopenharmony_ci					     SLI4_PAGE_SIZE, &phyaddr,
189362306a36Sopenharmony_ci					     GFP_KERNEL);
189462306a36Sopenharmony_ci		/* In case of malloc fails, proceed with whatever we have */
189562306a36Sopenharmony_ci		if (!viraddr)
189662306a36Sopenharmony_ci			break;
189762306a36Sopenharmony_ci		mbox->sge_array->addr[pagen] = viraddr;
189862306a36Sopenharmony_ci		/* Keep the first page for later sub-header construction */
189962306a36Sopenharmony_ci		if (pagen == 0)
190062306a36Sopenharmony_ci			cfg_shdr = (union lpfc_sli4_cfg_shdr *)viraddr;
190162306a36Sopenharmony_ci		resid_len = length - alloc_len;
190262306a36Sopenharmony_ci		if (resid_len > SLI4_PAGE_SIZE) {
190362306a36Sopenharmony_ci			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
190462306a36Sopenharmony_ci					      SLI4_PAGE_SIZE);
190562306a36Sopenharmony_ci			alloc_len += SLI4_PAGE_SIZE;
190662306a36Sopenharmony_ci		} else {
190762306a36Sopenharmony_ci			lpfc_sli4_mbx_sge_set(mbox, pagen, phyaddr,
190862306a36Sopenharmony_ci					      resid_len);
190962306a36Sopenharmony_ci			alloc_len = length;
191062306a36Sopenharmony_ci		}
191162306a36Sopenharmony_ci	}
191262306a36Sopenharmony_ci
191362306a36Sopenharmony_ci	/* Set up main header fields in mailbox command */
191462306a36Sopenharmony_ci	sli4_config->header.cfg_mhdr.payload_length = alloc_len;
191562306a36Sopenharmony_ci	bf_set(lpfc_mbox_hdr_sge_cnt, &sli4_config->header.cfg_mhdr, pagen);
191662306a36Sopenharmony_ci
191762306a36Sopenharmony_ci	/* Set up sub-header fields into the first page */
191862306a36Sopenharmony_ci	if (pagen > 0) {
191962306a36Sopenharmony_ci		bf_set(lpfc_mbox_hdr_opcode, &cfg_shdr->request, opcode);
192062306a36Sopenharmony_ci		bf_set(lpfc_mbox_hdr_subsystem, &cfg_shdr->request, subsystem);
192162306a36Sopenharmony_ci		cfg_shdr->request.request_length =
192262306a36Sopenharmony_ci				alloc_len - sizeof(union  lpfc_sli4_cfg_shdr);
192362306a36Sopenharmony_ci	}
192462306a36Sopenharmony_ci	/* The sub-header is in DMA memory, which needs endian converstion */
192562306a36Sopenharmony_ci	if (cfg_shdr)
192662306a36Sopenharmony_ci		lpfc_sli_pcimem_bcopy(cfg_shdr, cfg_shdr,
192762306a36Sopenharmony_ci				      sizeof(union  lpfc_sli4_cfg_shdr));
192862306a36Sopenharmony_ci	return alloc_len;
192962306a36Sopenharmony_ci}
193062306a36Sopenharmony_ci
193162306a36Sopenharmony_ci/**
193262306a36Sopenharmony_ci * lpfc_sli4_mbox_rsrc_extent - Initialize the opcode resource extent.
193362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
193462306a36Sopenharmony_ci * @mbox: pointer to an allocated lpfc mbox resource.
193562306a36Sopenharmony_ci * @exts_count: the number of extents, if required, to allocate.
193662306a36Sopenharmony_ci * @rsrc_type: the resource extent type.
193762306a36Sopenharmony_ci * @emb: true if LPFC_SLI4_MBX_EMBED. false if LPFC_SLI4_MBX_NEMBED.
193862306a36Sopenharmony_ci *
193962306a36Sopenharmony_ci * This routine completes the subcommand header for SLI4 resource extent
194062306a36Sopenharmony_ci * mailbox commands.  It is called after lpfc_sli4_config.  The caller must
194162306a36Sopenharmony_ci * pass an allocated mailbox and the attributes required to initialize the
194262306a36Sopenharmony_ci * mailbox correctly.
194362306a36Sopenharmony_ci *
194462306a36Sopenharmony_ci * Return: the actual length of the mbox command allocated.
194562306a36Sopenharmony_ci **/
194662306a36Sopenharmony_ciint
194762306a36Sopenharmony_cilpfc_sli4_mbox_rsrc_extent(struct lpfc_hba *phba, struct lpfcMboxq *mbox,
194862306a36Sopenharmony_ci			   uint16_t exts_count, uint16_t rsrc_type, bool emb)
194962306a36Sopenharmony_ci{
195062306a36Sopenharmony_ci	uint8_t opcode = 0;
195162306a36Sopenharmony_ci	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc_extnt = NULL;
195262306a36Sopenharmony_ci	void *virtaddr = NULL;
195362306a36Sopenharmony_ci
195462306a36Sopenharmony_ci	/* Set up SLI4 ioctl command header fields */
195562306a36Sopenharmony_ci	if (emb == LPFC_SLI4_MBX_NEMBED) {
195662306a36Sopenharmony_ci		/* Get the first SGE entry from the non-embedded DMA memory */
195762306a36Sopenharmony_ci		virtaddr = mbox->sge_array->addr[0];
195862306a36Sopenharmony_ci		if (virtaddr == NULL)
195962306a36Sopenharmony_ci			return 1;
196062306a36Sopenharmony_ci		n_rsrc_extnt = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
196162306a36Sopenharmony_ci	}
196262306a36Sopenharmony_ci
196362306a36Sopenharmony_ci	/*
196462306a36Sopenharmony_ci	 * The resource type is common to all extent Opcodes and resides in the
196562306a36Sopenharmony_ci	 * same position.
196662306a36Sopenharmony_ci	 */
196762306a36Sopenharmony_ci	if (emb == LPFC_SLI4_MBX_EMBED)
196862306a36Sopenharmony_ci		bf_set(lpfc_mbx_alloc_rsrc_extents_type,
196962306a36Sopenharmony_ci		       &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
197062306a36Sopenharmony_ci		       rsrc_type);
197162306a36Sopenharmony_ci	else {
197262306a36Sopenharmony_ci		/* This is DMA data.  Byteswap is required. */
197362306a36Sopenharmony_ci		bf_set(lpfc_mbx_alloc_rsrc_extents_type,
197462306a36Sopenharmony_ci		       n_rsrc_extnt, rsrc_type);
197562306a36Sopenharmony_ci		lpfc_sli_pcimem_bcopy(&n_rsrc_extnt->word4,
197662306a36Sopenharmony_ci				      &n_rsrc_extnt->word4,
197762306a36Sopenharmony_ci				      sizeof(uint32_t));
197862306a36Sopenharmony_ci	}
197962306a36Sopenharmony_ci
198062306a36Sopenharmony_ci	/* Complete the initialization for the particular Opcode. */
198162306a36Sopenharmony_ci	opcode = lpfc_sli_config_mbox_opcode_get(phba, mbox);
198262306a36Sopenharmony_ci	switch (opcode) {
198362306a36Sopenharmony_ci	case LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT:
198462306a36Sopenharmony_ci		if (emb == LPFC_SLI4_MBX_EMBED)
198562306a36Sopenharmony_ci			bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
198662306a36Sopenharmony_ci			       &mbox->u.mqe.un.alloc_rsrc_extents.u.req,
198762306a36Sopenharmony_ci			       exts_count);
198862306a36Sopenharmony_ci		else
198962306a36Sopenharmony_ci			bf_set(lpfc_mbx_alloc_rsrc_extents_cnt,
199062306a36Sopenharmony_ci			       n_rsrc_extnt, exts_count);
199162306a36Sopenharmony_ci		break;
199262306a36Sopenharmony_ci	case LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT:
199362306a36Sopenharmony_ci	case LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO:
199462306a36Sopenharmony_ci	case LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT:
199562306a36Sopenharmony_ci		/* Initialization is complete.*/
199662306a36Sopenharmony_ci		break;
199762306a36Sopenharmony_ci	default:
199862306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
199962306a36Sopenharmony_ci				"2929 Resource Extent Opcode x%x is "
200062306a36Sopenharmony_ci				"unsupported\n", opcode);
200162306a36Sopenharmony_ci		return 1;
200262306a36Sopenharmony_ci	}
200362306a36Sopenharmony_ci
200462306a36Sopenharmony_ci	return 0;
200562306a36Sopenharmony_ci}
200662306a36Sopenharmony_ci
200762306a36Sopenharmony_ci/**
200862306a36Sopenharmony_ci * lpfc_sli_config_mbox_subsys_get - Get subsystem from a sli_config mbox cmd
200962306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
201062306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command queue entry.
201162306a36Sopenharmony_ci *
201262306a36Sopenharmony_ci * This routine gets the subsystem from a SLI4 specific SLI_CONFIG mailbox
201362306a36Sopenharmony_ci * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if the
201462306a36Sopenharmony_ci * sub-header is not present, subsystem LPFC_MBOX_SUBSYSTEM_NA (0x0) shall
201562306a36Sopenharmony_ci * be returned.
201662306a36Sopenharmony_ci **/
201762306a36Sopenharmony_ciuint8_t
201862306a36Sopenharmony_cilpfc_sli_config_mbox_subsys_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
201962306a36Sopenharmony_ci{
202062306a36Sopenharmony_ci	struct lpfc_mbx_sli4_config *sli4_cfg;
202162306a36Sopenharmony_ci	union lpfc_sli4_cfg_shdr *cfg_shdr;
202262306a36Sopenharmony_ci
202362306a36Sopenharmony_ci	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
202462306a36Sopenharmony_ci		return LPFC_MBOX_SUBSYSTEM_NA;
202562306a36Sopenharmony_ci	sli4_cfg = &mbox->u.mqe.un.sli4_config;
202662306a36Sopenharmony_ci
202762306a36Sopenharmony_ci	/* For embedded mbox command, get opcode from embedded sub-header*/
202862306a36Sopenharmony_ci	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
202962306a36Sopenharmony_ci		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
203062306a36Sopenharmony_ci		return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
203162306a36Sopenharmony_ci	}
203262306a36Sopenharmony_ci
203362306a36Sopenharmony_ci	/* For non-embedded mbox command, get opcode from first dma page */
203462306a36Sopenharmony_ci	if (unlikely(!mbox->sge_array))
203562306a36Sopenharmony_ci		return LPFC_MBOX_SUBSYSTEM_NA;
203662306a36Sopenharmony_ci	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
203762306a36Sopenharmony_ci	return bf_get(lpfc_mbox_hdr_subsystem, &cfg_shdr->request);
203862306a36Sopenharmony_ci}
203962306a36Sopenharmony_ci
204062306a36Sopenharmony_ci/**
204162306a36Sopenharmony_ci * lpfc_sli_config_mbox_opcode_get - Get opcode from a sli_config mbox cmd
204262306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
204362306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command queue entry.
204462306a36Sopenharmony_ci *
204562306a36Sopenharmony_ci * This routine gets the opcode from a SLI4 specific SLI_CONFIG mailbox
204662306a36Sopenharmony_ci * command. If the mailbox command is not MBX_SLI4_CONFIG (0x9B) or if
204762306a36Sopenharmony_ci * the sub-header is not present, opcode LPFC_MBOX_OPCODE_NA (0x0) be
204862306a36Sopenharmony_ci * returned.
204962306a36Sopenharmony_ci **/
205062306a36Sopenharmony_ciuint8_t
205162306a36Sopenharmony_cilpfc_sli_config_mbox_opcode_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
205262306a36Sopenharmony_ci{
205362306a36Sopenharmony_ci	struct lpfc_mbx_sli4_config *sli4_cfg;
205462306a36Sopenharmony_ci	union lpfc_sli4_cfg_shdr *cfg_shdr;
205562306a36Sopenharmony_ci
205662306a36Sopenharmony_ci	if (mbox->u.mb.mbxCommand != MBX_SLI4_CONFIG)
205762306a36Sopenharmony_ci		return LPFC_MBOX_OPCODE_NA;
205862306a36Sopenharmony_ci	sli4_cfg = &mbox->u.mqe.un.sli4_config;
205962306a36Sopenharmony_ci
206062306a36Sopenharmony_ci	/* For embedded mbox command, get opcode from embedded sub-header*/
206162306a36Sopenharmony_ci	if (bf_get(lpfc_mbox_hdr_emb, &sli4_cfg->header.cfg_mhdr)) {
206262306a36Sopenharmony_ci		cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
206362306a36Sopenharmony_ci		return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
206462306a36Sopenharmony_ci	}
206562306a36Sopenharmony_ci
206662306a36Sopenharmony_ci	/* For non-embedded mbox command, get opcode from first dma page */
206762306a36Sopenharmony_ci	if (unlikely(!mbox->sge_array))
206862306a36Sopenharmony_ci		return LPFC_MBOX_OPCODE_NA;
206962306a36Sopenharmony_ci	cfg_shdr = (union lpfc_sli4_cfg_shdr *)mbox->sge_array->addr[0];
207062306a36Sopenharmony_ci	return bf_get(lpfc_mbox_hdr_opcode, &cfg_shdr->request);
207162306a36Sopenharmony_ci}
207262306a36Sopenharmony_ci
207362306a36Sopenharmony_ci/**
207462306a36Sopenharmony_ci * lpfc_sli4_mbx_read_fcf_rec - Allocate and construct read fcf mbox cmd
207562306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
207662306a36Sopenharmony_ci * @mboxq: pointer to lpfc mbox command.
207762306a36Sopenharmony_ci * @fcf_index: index to fcf table.
207862306a36Sopenharmony_ci *
207962306a36Sopenharmony_ci * This routine routine allocates and constructs non-embedded mailbox command
208062306a36Sopenharmony_ci * for reading a FCF table entry referred by @fcf_index.
208162306a36Sopenharmony_ci *
208262306a36Sopenharmony_ci * Return: pointer to the mailbox command constructed if successful, otherwise
208362306a36Sopenharmony_ci * NULL.
208462306a36Sopenharmony_ci **/
208562306a36Sopenharmony_ciint
208662306a36Sopenharmony_cilpfc_sli4_mbx_read_fcf_rec(struct lpfc_hba *phba,
208762306a36Sopenharmony_ci			   struct lpfcMboxq *mboxq,
208862306a36Sopenharmony_ci			   uint16_t fcf_index)
208962306a36Sopenharmony_ci{
209062306a36Sopenharmony_ci	void *virt_addr;
209162306a36Sopenharmony_ci	uint8_t *bytep;
209262306a36Sopenharmony_ci	struct lpfc_mbx_sge sge;
209362306a36Sopenharmony_ci	uint32_t alloc_len, req_len;
209462306a36Sopenharmony_ci	struct lpfc_mbx_read_fcf_tbl *read_fcf;
209562306a36Sopenharmony_ci
209662306a36Sopenharmony_ci	if (!mboxq)
209762306a36Sopenharmony_ci		return -ENOMEM;
209862306a36Sopenharmony_ci
209962306a36Sopenharmony_ci	req_len = sizeof(struct fcf_record) +
210062306a36Sopenharmony_ci		  sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
210162306a36Sopenharmony_ci
210262306a36Sopenharmony_ci	/* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
210362306a36Sopenharmony_ci	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
210462306a36Sopenharmony_ci			LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
210562306a36Sopenharmony_ci			LPFC_SLI4_MBX_NEMBED);
210662306a36Sopenharmony_ci
210762306a36Sopenharmony_ci	if (alloc_len < req_len) {
210862306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
210962306a36Sopenharmony_ci				"0291 Allocated DMA memory size (x%x) is "
211062306a36Sopenharmony_ci				"less than the requested DMA memory "
211162306a36Sopenharmony_ci				"size (x%x)\n", alloc_len, req_len);
211262306a36Sopenharmony_ci		return -ENOMEM;
211362306a36Sopenharmony_ci	}
211462306a36Sopenharmony_ci
211562306a36Sopenharmony_ci	/* Get the first SGE entry from the non-embedded DMA memory. This
211662306a36Sopenharmony_ci	 * routine only uses a single SGE.
211762306a36Sopenharmony_ci	 */
211862306a36Sopenharmony_ci	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
211962306a36Sopenharmony_ci	virt_addr = mboxq->sge_array->addr[0];
212062306a36Sopenharmony_ci	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
212162306a36Sopenharmony_ci
212262306a36Sopenharmony_ci	/* Set up command fields */
212362306a36Sopenharmony_ci	bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
212462306a36Sopenharmony_ci	/* Perform necessary endian conversion */
212562306a36Sopenharmony_ci	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
212662306a36Sopenharmony_ci	lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
212762306a36Sopenharmony_ci
212862306a36Sopenharmony_ci	return 0;
212962306a36Sopenharmony_ci}
213062306a36Sopenharmony_ci
213162306a36Sopenharmony_ci/**
213262306a36Sopenharmony_ci * lpfc_request_features: Configure SLI4 REQUEST_FEATURES mailbox
213362306a36Sopenharmony_ci * @phba: pointer to lpfc hba data structure.
213462306a36Sopenharmony_ci * @mboxq: pointer to lpfc mbox command.
213562306a36Sopenharmony_ci *
213662306a36Sopenharmony_ci * This routine sets up the mailbox for an SLI4 REQUEST_FEATURES
213762306a36Sopenharmony_ci * mailbox command.
213862306a36Sopenharmony_ci **/
213962306a36Sopenharmony_civoid
214062306a36Sopenharmony_cilpfc_request_features(struct lpfc_hba *phba, struct lpfcMboxq *mboxq)
214162306a36Sopenharmony_ci{
214262306a36Sopenharmony_ci	/* Set up SLI4 mailbox command header fields */
214362306a36Sopenharmony_ci	memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
214462306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mboxq->u.mqe, MBX_SLI4_REQ_FTRS);
214562306a36Sopenharmony_ci
214662306a36Sopenharmony_ci	/* Set up host requested features. */
214762306a36Sopenharmony_ci	bf_set(lpfc_mbx_rq_ftr_rq_fcpi, &mboxq->u.mqe.un.req_ftrs, 1);
214862306a36Sopenharmony_ci	bf_set(lpfc_mbx_rq_ftr_rq_perfh, &mboxq->u.mqe.un.req_ftrs, 1);
214962306a36Sopenharmony_ci
215062306a36Sopenharmony_ci	/* Enable DIF (block guard) only if configured to do so. */
215162306a36Sopenharmony_ci	if (phba->cfg_enable_bg)
215262306a36Sopenharmony_ci		bf_set(lpfc_mbx_rq_ftr_rq_dif, &mboxq->u.mqe.un.req_ftrs, 1);
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci	/* Enable NPIV only if configured to do so. */
215562306a36Sopenharmony_ci	if (phba->max_vpi && phba->cfg_enable_npiv)
215662306a36Sopenharmony_ci		bf_set(lpfc_mbx_rq_ftr_rq_npiv, &mboxq->u.mqe.un.req_ftrs, 1);
215762306a36Sopenharmony_ci
215862306a36Sopenharmony_ci	if (phba->nvmet_support) {
215962306a36Sopenharmony_ci		bf_set(lpfc_mbx_rq_ftr_rq_mrqp, &mboxq->u.mqe.un.req_ftrs, 1);
216062306a36Sopenharmony_ci		/* iaab/iaar NOT set for now */
216162306a36Sopenharmony_ci		bf_set(lpfc_mbx_rq_ftr_rq_iaab, &mboxq->u.mqe.un.req_ftrs, 0);
216262306a36Sopenharmony_ci		bf_set(lpfc_mbx_rq_ftr_rq_iaar, &mboxq->u.mqe.un.req_ftrs, 0);
216362306a36Sopenharmony_ci	}
216462306a36Sopenharmony_ci
216562306a36Sopenharmony_ci	/* Enable Application Services Header for appheader VMID */
216662306a36Sopenharmony_ci	if (phba->cfg_vmid_app_header) {
216762306a36Sopenharmony_ci		bf_set(lpfc_mbx_rq_ftr_rq_ashdr, &mboxq->u.mqe.un.req_ftrs, 1);
216862306a36Sopenharmony_ci		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 1);
216962306a36Sopenharmony_ci	}
217062306a36Sopenharmony_ci	return;
217162306a36Sopenharmony_ci}
217262306a36Sopenharmony_ci
217362306a36Sopenharmony_ci/**
217462306a36Sopenharmony_ci * lpfc_init_vfi - Initialize the INIT_VFI mailbox command
217562306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
217662306a36Sopenharmony_ci * @vport: Vport associated with the VF.
217762306a36Sopenharmony_ci *
217862306a36Sopenharmony_ci * This routine initializes @mbox to all zeros and then fills in the mailbox
217962306a36Sopenharmony_ci * fields from @vport. INIT_VFI configures virtual fabrics identified by VFI
218062306a36Sopenharmony_ci * in the context of an FCF. The driver issues this command to setup a VFI
218162306a36Sopenharmony_ci * before issuing a FLOGI to login to the VSAN. The driver should also issue a
218262306a36Sopenharmony_ci * REG_VFI after a successful VSAN login.
218362306a36Sopenharmony_ci **/
218462306a36Sopenharmony_civoid
218562306a36Sopenharmony_cilpfc_init_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
218662306a36Sopenharmony_ci{
218762306a36Sopenharmony_ci	struct lpfc_mbx_init_vfi *init_vfi;
218862306a36Sopenharmony_ci
218962306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
219062306a36Sopenharmony_ci	mbox->vport = vport;
219162306a36Sopenharmony_ci	init_vfi = &mbox->u.mqe.un.init_vfi;
219262306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VFI);
219362306a36Sopenharmony_ci	bf_set(lpfc_init_vfi_vr, init_vfi, 1);
219462306a36Sopenharmony_ci	bf_set(lpfc_init_vfi_vt, init_vfi, 1);
219562306a36Sopenharmony_ci	bf_set(lpfc_init_vfi_vp, init_vfi, 1);
219662306a36Sopenharmony_ci	bf_set(lpfc_init_vfi_vfi, init_vfi,
219762306a36Sopenharmony_ci	       vport->phba->sli4_hba.vfi_ids[vport->vfi]);
219862306a36Sopenharmony_ci	bf_set(lpfc_init_vfi_vpi, init_vfi,
219962306a36Sopenharmony_ci	       vport->phba->vpi_ids[vport->vpi]);
220062306a36Sopenharmony_ci	bf_set(lpfc_init_vfi_fcfi, init_vfi,
220162306a36Sopenharmony_ci	       vport->phba->fcf.fcfi);
220262306a36Sopenharmony_ci}
220362306a36Sopenharmony_ci
220462306a36Sopenharmony_ci/**
220562306a36Sopenharmony_ci * lpfc_reg_vfi - Initialize the REG_VFI mailbox command
220662306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
220762306a36Sopenharmony_ci * @vport: vport associated with the VF.
220862306a36Sopenharmony_ci * @phys: BDE DMA bus address used to send the service parameters to the HBA.
220962306a36Sopenharmony_ci *
221062306a36Sopenharmony_ci * This routine initializes @mbox to all zeros and then fills in the mailbox
221162306a36Sopenharmony_ci * fields from @vport, and uses @buf as a DMAable buffer to send the vport's
221262306a36Sopenharmony_ci * fc service parameters to the HBA for this VFI. REG_VFI configures virtual
221362306a36Sopenharmony_ci * fabrics identified by VFI in the context of an FCF.
221462306a36Sopenharmony_ci **/
221562306a36Sopenharmony_civoid
221662306a36Sopenharmony_cilpfc_reg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport, dma_addr_t phys)
221762306a36Sopenharmony_ci{
221862306a36Sopenharmony_ci	struct lpfc_mbx_reg_vfi *reg_vfi;
221962306a36Sopenharmony_ci	struct lpfc_hba *phba = vport->phba;
222062306a36Sopenharmony_ci	uint8_t bbscn_fabric = 0, bbscn_max = 0, bbscn_def = 0;
222162306a36Sopenharmony_ci
222262306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
222362306a36Sopenharmony_ci	reg_vfi = &mbox->u.mqe.un.reg_vfi;
222462306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_VFI);
222562306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_vp, reg_vfi, 1);
222662306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_vfi, reg_vfi,
222762306a36Sopenharmony_ci	       phba->sli4_hba.vfi_ids[vport->vfi]);
222862306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_fcfi, reg_vfi, phba->fcf.fcfi);
222962306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_vpi, reg_vfi, phba->vpi_ids[vport->vpi]);
223062306a36Sopenharmony_ci	memcpy(reg_vfi->wwn, &vport->fc_portname, sizeof(struct lpfc_name));
223162306a36Sopenharmony_ci	reg_vfi->wwn[0] = cpu_to_le32(reg_vfi->wwn[0]);
223262306a36Sopenharmony_ci	reg_vfi->wwn[1] = cpu_to_le32(reg_vfi->wwn[1]);
223362306a36Sopenharmony_ci	reg_vfi->e_d_tov = phba->fc_edtov;
223462306a36Sopenharmony_ci	reg_vfi->r_a_tov = phba->fc_ratov;
223562306a36Sopenharmony_ci	if (phys) {
223662306a36Sopenharmony_ci		reg_vfi->bde.addrHigh = putPaddrHigh(phys);
223762306a36Sopenharmony_ci		reg_vfi->bde.addrLow = putPaddrLow(phys);
223862306a36Sopenharmony_ci		reg_vfi->bde.tus.f.bdeSize = sizeof(vport->fc_sparam);
223962306a36Sopenharmony_ci		reg_vfi->bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
224062306a36Sopenharmony_ci	}
224162306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_nport_id, reg_vfi, vport->fc_myDID);
224262306a36Sopenharmony_ci
224362306a36Sopenharmony_ci	/* Only FC supports upd bit */
224462306a36Sopenharmony_ci	if ((phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC) &&
224562306a36Sopenharmony_ci	    (vport->fc_flag & FC_VFI_REGISTERED) &&
224662306a36Sopenharmony_ci	    (!phba->fc_topology_changed))
224762306a36Sopenharmony_ci		bf_set(lpfc_reg_vfi_upd, reg_vfi, 1);
224862306a36Sopenharmony_ci
224962306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 0);
225062306a36Sopenharmony_ci	bf_set(lpfc_reg_vfi_bbscn, reg_vfi, 0);
225162306a36Sopenharmony_ci	bbscn_fabric = (phba->fc_fabparam.cmn.bbRcvSizeMsb >> 4) & 0xF;
225262306a36Sopenharmony_ci
225362306a36Sopenharmony_ci	if (phba->bbcredit_support && phba->cfg_enable_bbcr  &&
225462306a36Sopenharmony_ci	    bbscn_fabric != 0) {
225562306a36Sopenharmony_ci		bbscn_max = bf_get(lpfc_bbscn_max,
225662306a36Sopenharmony_ci				   &phba->sli4_hba.bbscn_params);
225762306a36Sopenharmony_ci		if (bbscn_fabric <= bbscn_max) {
225862306a36Sopenharmony_ci			bbscn_def = bf_get(lpfc_bbscn_def,
225962306a36Sopenharmony_ci					   &phba->sli4_hba.bbscn_params);
226062306a36Sopenharmony_ci
226162306a36Sopenharmony_ci			if (bbscn_fabric > bbscn_def)
226262306a36Sopenharmony_ci				bf_set(lpfc_reg_vfi_bbscn, reg_vfi,
226362306a36Sopenharmony_ci				       bbscn_fabric);
226462306a36Sopenharmony_ci			else
226562306a36Sopenharmony_ci				bf_set(lpfc_reg_vfi_bbscn, reg_vfi, bbscn_def);
226662306a36Sopenharmony_ci
226762306a36Sopenharmony_ci			bf_set(lpfc_reg_vfi_bbcr, reg_vfi, 1);
226862306a36Sopenharmony_ci		}
226962306a36Sopenharmony_ci	}
227062306a36Sopenharmony_ci	lpfc_printf_vlog(vport, KERN_INFO, LOG_MBOX,
227162306a36Sopenharmony_ci			"3134 Register VFI, mydid:x%x, fcfi:%d, "
227262306a36Sopenharmony_ci			" vfi:%d, vpi:%d, fc_pname:%x%x fc_flag:x%x"
227362306a36Sopenharmony_ci			" port_state:x%x topology chg:%d bbscn_fabric :%d\n",
227462306a36Sopenharmony_ci			vport->fc_myDID,
227562306a36Sopenharmony_ci			phba->fcf.fcfi,
227662306a36Sopenharmony_ci			phba->sli4_hba.vfi_ids[vport->vfi],
227762306a36Sopenharmony_ci			phba->vpi_ids[vport->vpi],
227862306a36Sopenharmony_ci			reg_vfi->wwn[0], reg_vfi->wwn[1], vport->fc_flag,
227962306a36Sopenharmony_ci			vport->port_state, phba->fc_topology_changed,
228062306a36Sopenharmony_ci			bbscn_fabric);
228162306a36Sopenharmony_ci}
228262306a36Sopenharmony_ci
228362306a36Sopenharmony_ci/**
228462306a36Sopenharmony_ci * lpfc_init_vpi - Initialize the INIT_VPI mailbox command
228562306a36Sopenharmony_ci * @phba: pointer to the hba structure to init the VPI for.
228662306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
228762306a36Sopenharmony_ci * @vpi: VPI to be initialized.
228862306a36Sopenharmony_ci *
228962306a36Sopenharmony_ci * The INIT_VPI mailbox command supports virtual N_Ports. The driver uses the
229062306a36Sopenharmony_ci * command to activate a virtual N_Port. The HBA assigns a MAC address to use
229162306a36Sopenharmony_ci * with the virtual N Port.  The SLI Host issues this command before issuing a
229262306a36Sopenharmony_ci * FDISC to connect to the Fabric. The SLI Host should issue a REG_VPI after a
229362306a36Sopenharmony_ci * successful virtual NPort login.
229462306a36Sopenharmony_ci **/
229562306a36Sopenharmony_civoid
229662306a36Sopenharmony_cilpfc_init_vpi(struct lpfc_hba *phba, struct lpfcMboxq *mbox, uint16_t vpi)
229762306a36Sopenharmony_ci{
229862306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
229962306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_INIT_VPI);
230062306a36Sopenharmony_ci	bf_set(lpfc_init_vpi_vpi, &mbox->u.mqe.un.init_vpi,
230162306a36Sopenharmony_ci	       phba->vpi_ids[vpi]);
230262306a36Sopenharmony_ci	bf_set(lpfc_init_vpi_vfi, &mbox->u.mqe.un.init_vpi,
230362306a36Sopenharmony_ci	       phba->sli4_hba.vfi_ids[phba->pport->vfi]);
230462306a36Sopenharmony_ci}
230562306a36Sopenharmony_ci
230662306a36Sopenharmony_ci/**
230762306a36Sopenharmony_ci * lpfc_unreg_vfi - Initialize the UNREG_VFI mailbox command
230862306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
230962306a36Sopenharmony_ci * @vport: vport associated with the VF.
231062306a36Sopenharmony_ci *
231162306a36Sopenharmony_ci * The UNREG_VFI mailbox command causes the SLI Host to put a virtual fabric
231262306a36Sopenharmony_ci * (logical NPort) into the inactive state. The SLI Host must have logged out
231362306a36Sopenharmony_ci * and unregistered all remote N_Ports to abort any activity on the virtual
231462306a36Sopenharmony_ci * fabric. The SLI Port posts the mailbox response after marking the virtual
231562306a36Sopenharmony_ci * fabric inactive.
231662306a36Sopenharmony_ci **/
231762306a36Sopenharmony_civoid
231862306a36Sopenharmony_cilpfc_unreg_vfi(struct lpfcMboxq *mbox, struct lpfc_vport *vport)
231962306a36Sopenharmony_ci{
232062306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
232162306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_VFI);
232262306a36Sopenharmony_ci	bf_set(lpfc_unreg_vfi_vfi, &mbox->u.mqe.un.unreg_vfi,
232362306a36Sopenharmony_ci	       vport->phba->sli4_hba.vfi_ids[vport->vfi]);
232462306a36Sopenharmony_ci}
232562306a36Sopenharmony_ci
232662306a36Sopenharmony_ci/**
232762306a36Sopenharmony_ci * lpfc_sli4_dump_cfg_rg23 - Dump sli4 port config region 23
232862306a36Sopenharmony_ci * @phba: pointer to the hba structure containing.
232962306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
233062306a36Sopenharmony_ci *
233162306a36Sopenharmony_ci * This function create a SLI4 dump mailbox command to dump configure
233262306a36Sopenharmony_ci * region 23.
233362306a36Sopenharmony_ci **/
233462306a36Sopenharmony_ciint
233562306a36Sopenharmony_cilpfc_sli4_dump_cfg_rg23(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
233662306a36Sopenharmony_ci{
233762306a36Sopenharmony_ci	struct lpfc_dmabuf *mp = NULL;
233862306a36Sopenharmony_ci	MAILBOX_t *mb;
233962306a36Sopenharmony_ci	int rc;
234062306a36Sopenharmony_ci
234162306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
234262306a36Sopenharmony_ci	mb = &mbox->u.mb;
234362306a36Sopenharmony_ci
234462306a36Sopenharmony_ci	rc = lpfc_mbox_rsrc_prep(phba, mbox);
234562306a36Sopenharmony_ci	if (rc) {
234662306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
234762306a36Sopenharmony_ci				"2569 %s: memory allocation failed\n",
234862306a36Sopenharmony_ci				__func__);
234962306a36Sopenharmony_ci		return 1;
235062306a36Sopenharmony_ci	}
235162306a36Sopenharmony_ci
235262306a36Sopenharmony_ci	mb->mbxCommand = MBX_DUMP_MEMORY;
235362306a36Sopenharmony_ci	mb->un.varDmp.type = DMP_NV_PARAMS;
235462306a36Sopenharmony_ci	mb->un.varDmp.region_id = DMP_REGION_23;
235562306a36Sopenharmony_ci	mb->un.varDmp.sli4_length = DMP_RGN23_SIZE;
235662306a36Sopenharmony_ci	mp = mbox->ctx_buf;
235762306a36Sopenharmony_ci	mb->un.varWords[3] = putPaddrLow(mp->phys);
235862306a36Sopenharmony_ci	mb->un.varWords[4] = putPaddrHigh(mp->phys);
235962306a36Sopenharmony_ci	return 0;
236062306a36Sopenharmony_ci}
236162306a36Sopenharmony_ci
236262306a36Sopenharmony_cistatic void
236362306a36Sopenharmony_cilpfc_mbx_cmpl_rdp_link_stat(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
236462306a36Sopenharmony_ci{
236562306a36Sopenharmony_ci	MAILBOX_t *mb;
236662306a36Sopenharmony_ci	int rc = FAILURE;
236762306a36Sopenharmony_ci	struct lpfc_rdp_context *rdp_context =
236862306a36Sopenharmony_ci			(struct lpfc_rdp_context *)(mboxq->ctx_ndlp);
236962306a36Sopenharmony_ci
237062306a36Sopenharmony_ci	mb = &mboxq->u.mb;
237162306a36Sopenharmony_ci	if (mb->mbxStatus)
237262306a36Sopenharmony_ci		goto mbx_failed;
237362306a36Sopenharmony_ci
237462306a36Sopenharmony_ci	memcpy(&rdp_context->link_stat, &mb->un.varRdLnk, sizeof(READ_LNK_VAR));
237562306a36Sopenharmony_ci
237662306a36Sopenharmony_ci	rc = SUCCESS;
237762306a36Sopenharmony_ci
237862306a36Sopenharmony_cimbx_failed:
237962306a36Sopenharmony_ci	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
238062306a36Sopenharmony_ci	rdp_context->cmpl(phba, rdp_context, rc);
238162306a36Sopenharmony_ci}
238262306a36Sopenharmony_ci
238362306a36Sopenharmony_cistatic void
238462306a36Sopenharmony_cilpfc_mbx_cmpl_rdp_page_a2(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
238562306a36Sopenharmony_ci{
238662306a36Sopenharmony_ci	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
238762306a36Sopenharmony_ci	struct lpfc_rdp_context *rdp_context =
238862306a36Sopenharmony_ci			(struct lpfc_rdp_context *)(mbox->ctx_ndlp);
238962306a36Sopenharmony_ci
239062306a36Sopenharmony_ci	if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
239162306a36Sopenharmony_ci		goto error_mbox_free;
239262306a36Sopenharmony_ci
239362306a36Sopenharmony_ci	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
239462306a36Sopenharmony_ci				DMP_SFF_PAGE_A2_SIZE);
239562306a36Sopenharmony_ci
239662306a36Sopenharmony_ci	lpfc_read_lnk_stat(phba, mbox);
239762306a36Sopenharmony_ci	mbox->vport = rdp_context->ndlp->vport;
239862306a36Sopenharmony_ci
239962306a36Sopenharmony_ci	/* Save the dma buffer for cleanup in the final completion. */
240062306a36Sopenharmony_ci	mbox->ctx_buf = mp;
240162306a36Sopenharmony_ci	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_link_stat;
240262306a36Sopenharmony_ci	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
240362306a36Sopenharmony_ci	if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) == MBX_NOT_FINISHED)
240462306a36Sopenharmony_ci		goto error_mbox_free;
240562306a36Sopenharmony_ci
240662306a36Sopenharmony_ci	return;
240762306a36Sopenharmony_ci
240862306a36Sopenharmony_cierror_mbox_free:
240962306a36Sopenharmony_ci	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
241062306a36Sopenharmony_ci	rdp_context->cmpl(phba, rdp_context, FAILURE);
241162306a36Sopenharmony_ci}
241262306a36Sopenharmony_ci
241362306a36Sopenharmony_civoid
241462306a36Sopenharmony_cilpfc_mbx_cmpl_rdp_page_a0(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
241562306a36Sopenharmony_ci{
241662306a36Sopenharmony_ci	int rc;
241762306a36Sopenharmony_ci	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(mbox->ctx_buf);
241862306a36Sopenharmony_ci	struct lpfc_rdp_context *rdp_context =
241962306a36Sopenharmony_ci			(struct lpfc_rdp_context *)(mbox->ctx_ndlp);
242062306a36Sopenharmony_ci
242162306a36Sopenharmony_ci	if (bf_get(lpfc_mqe_status, &mbox->u.mqe))
242262306a36Sopenharmony_ci		goto error;
242362306a36Sopenharmony_ci
242462306a36Sopenharmony_ci	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
242562306a36Sopenharmony_ci				DMP_SFF_PAGE_A0_SIZE);
242662306a36Sopenharmony_ci
242762306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
242862306a36Sopenharmony_ci
242962306a36Sopenharmony_ci	memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
243062306a36Sopenharmony_ci	INIT_LIST_HEAD(&mp->list);
243162306a36Sopenharmony_ci
243262306a36Sopenharmony_ci	/* save address for completion */
243362306a36Sopenharmony_ci	mbox->ctx_buf = mp;
243462306a36Sopenharmony_ci	mbox->vport = rdp_context->ndlp->vport;
243562306a36Sopenharmony_ci
243662306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
243762306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_type,
243862306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
243962306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_link,
244062306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
244162306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_page_no,
244262306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
244362306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_length,
244462306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
244562306a36Sopenharmony_ci	mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
244662306a36Sopenharmony_ci	mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
244762306a36Sopenharmony_ci
244862306a36Sopenharmony_ci	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a2;
244962306a36Sopenharmony_ci	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
245062306a36Sopenharmony_ci	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
245162306a36Sopenharmony_ci	if (rc == MBX_NOT_FINISHED)
245262306a36Sopenharmony_ci		goto error;
245362306a36Sopenharmony_ci
245462306a36Sopenharmony_ci	return;
245562306a36Sopenharmony_ci
245662306a36Sopenharmony_cierror:
245762306a36Sopenharmony_ci	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
245862306a36Sopenharmony_ci	rdp_context->cmpl(phba, rdp_context, FAILURE);
245962306a36Sopenharmony_ci}
246062306a36Sopenharmony_ci
246162306a36Sopenharmony_ci
246262306a36Sopenharmony_ci/*
246362306a36Sopenharmony_ci * lpfc_sli4_dump_page_a0 - Dump sli4 read SFP Diagnostic.
246462306a36Sopenharmony_ci * @phba: pointer to the hba structure containing.
246562306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
246662306a36Sopenharmony_ci *
246762306a36Sopenharmony_ci * This function create a SLI4 dump mailbox command to dump configure
246862306a36Sopenharmony_ci * type 3 page 0xA0.
246962306a36Sopenharmony_ci */
247062306a36Sopenharmony_ciint
247162306a36Sopenharmony_cilpfc_sli4_dump_page_a0(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
247262306a36Sopenharmony_ci{
247362306a36Sopenharmony_ci	int rc;
247462306a36Sopenharmony_ci	struct lpfc_dmabuf *mp = NULL;
247562306a36Sopenharmony_ci
247662306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
247762306a36Sopenharmony_ci
247862306a36Sopenharmony_ci	rc = lpfc_mbox_rsrc_prep(phba, mbox);
247962306a36Sopenharmony_ci	if (rc) {
248062306a36Sopenharmony_ci		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
248162306a36Sopenharmony_ci			"3569 dump type 3 page 0xA0 allocation failed\n");
248262306a36Sopenharmony_ci		return 1;
248362306a36Sopenharmony_ci	}
248462306a36Sopenharmony_ci
248562306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
248662306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_type,
248762306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
248862306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_link,
248962306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
249062306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_page_no,
249162306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A0);
249262306a36Sopenharmony_ci	bf_set(lpfc_mbx_memory_dump_type3_length,
249362306a36Sopenharmony_ci		&mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
249462306a36Sopenharmony_ci
249562306a36Sopenharmony_ci	mp = mbox->ctx_buf;
249662306a36Sopenharmony_ci	mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
249762306a36Sopenharmony_ci	mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
249862306a36Sopenharmony_ci
249962306a36Sopenharmony_ci	return 0;
250062306a36Sopenharmony_ci}
250162306a36Sopenharmony_ci
250262306a36Sopenharmony_ci/**
250362306a36Sopenharmony_ci * lpfc_reg_fcfi - Initialize the REG_FCFI mailbox command
250462306a36Sopenharmony_ci * @phba: pointer to the hba structure containing the FCF index and RQ ID.
250562306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
250662306a36Sopenharmony_ci *
250762306a36Sopenharmony_ci * The REG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs). The
250862306a36Sopenharmony_ci * SLI Host uses the command to activate an FCF after it has acquired FCF
250962306a36Sopenharmony_ci * information via a READ_FCF mailbox command. This mailbox command also is used
251062306a36Sopenharmony_ci * to indicate where received unsolicited frames from this FCF will be sent. By
251162306a36Sopenharmony_ci * default this routine will set up the FCF to forward all unsolicited frames
251262306a36Sopenharmony_ci * to the RQ ID passed in the @phba. This can be overridden by the caller for
251362306a36Sopenharmony_ci * more complicated setups.
251462306a36Sopenharmony_ci **/
251562306a36Sopenharmony_civoid
251662306a36Sopenharmony_cilpfc_reg_fcfi(struct lpfc_hba *phba, struct lpfcMboxq *mbox)
251762306a36Sopenharmony_ci{
251862306a36Sopenharmony_ci	struct lpfc_mbx_reg_fcfi *reg_fcfi;
251962306a36Sopenharmony_ci
252062306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
252162306a36Sopenharmony_ci	reg_fcfi = &mbox->u.mqe.un.reg_fcfi;
252262306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI);
252362306a36Sopenharmony_ci	if (phba->nvmet_support == 0) {
252462306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
252562306a36Sopenharmony_ci		       phba->sli4_hba.hdr_rq->queue_id);
252662306a36Sopenharmony_ci		/* Match everything - rq_id0 */
252762306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, 0);
252862306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0);
252962306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi, 0);
253062306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rctl_mask0, reg_fcfi, 0);
253162306a36Sopenharmony_ci
253262306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi, REG_FCF_INVALID_QID);
253362306a36Sopenharmony_ci
253462306a36Sopenharmony_ci		/* addr mode is bit wise inverted value of fcf addr_mode */
253562306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_mam, reg_fcfi,
253662306a36Sopenharmony_ci		       (~phba->fcf.addr_mode) & 0x3);
253762306a36Sopenharmony_ci	} else {
253862306a36Sopenharmony_ci		/* This is ONLY for NVMET MRQ == 1 */
253962306a36Sopenharmony_ci		if (phba->cfg_nvmet_mrq != 1)
254062306a36Sopenharmony_ci			return;
254162306a36Sopenharmony_ci
254262306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rq_id0, reg_fcfi,
254362306a36Sopenharmony_ci		       phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
254462306a36Sopenharmony_ci		/* Match type FCP - rq_id0 */
254562306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_type_match0, reg_fcfi, FC_TYPE_FCP);
254662306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_type_mask0, reg_fcfi, 0xff);
254762306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rctl_match0, reg_fcfi,
254862306a36Sopenharmony_ci		       FC_RCTL_DD_UNSOL_CMD);
254962306a36Sopenharmony_ci
255062306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rq_id1, reg_fcfi,
255162306a36Sopenharmony_ci		       phba->sli4_hba.hdr_rq->queue_id);
255262306a36Sopenharmony_ci		/* Match everything else - rq_id1 */
255362306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_type_match1, reg_fcfi, 0);
255462306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_type_mask1, reg_fcfi, 0);
255562306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rctl_match1, reg_fcfi, 0);
255662306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_rctl_mask1, reg_fcfi, 0);
255762306a36Sopenharmony_ci	}
255862306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
255962306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
256062306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_info_index, reg_fcfi,
256162306a36Sopenharmony_ci	       phba->fcf.current_rec.fcf_indx);
256262306a36Sopenharmony_ci	if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
256362306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_vv, reg_fcfi, 1);
256462306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_vlan_tag, reg_fcfi,
256562306a36Sopenharmony_ci		       phba->fcf.current_rec.vlan_id);
256662306a36Sopenharmony_ci	}
256762306a36Sopenharmony_ci}
256862306a36Sopenharmony_ci
256962306a36Sopenharmony_ci/**
257062306a36Sopenharmony_ci * lpfc_reg_fcfi_mrq - Initialize the REG_FCFI_MRQ mailbox command
257162306a36Sopenharmony_ci * @phba: pointer to the hba structure containing the FCF index and RQ ID.
257262306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
257362306a36Sopenharmony_ci * @mode: 0 to register FCFI, 1 to register MRQs
257462306a36Sopenharmony_ci *
257562306a36Sopenharmony_ci * The REG_FCFI_MRQ mailbox command supports Fibre Channel Forwarders (FCFs).
257662306a36Sopenharmony_ci * The SLI Host uses the command to activate an FCF after it has acquired FCF
257762306a36Sopenharmony_ci * information via a READ_FCF mailbox command. This mailbox command also is used
257862306a36Sopenharmony_ci * to indicate where received unsolicited frames from this FCF will be sent. By
257962306a36Sopenharmony_ci * default this routine will set up the FCF to forward all unsolicited frames
258062306a36Sopenharmony_ci * to the RQ ID passed in the @phba. This can be overridden by the caller for
258162306a36Sopenharmony_ci * more complicated setups.
258262306a36Sopenharmony_ci **/
258362306a36Sopenharmony_civoid
258462306a36Sopenharmony_cilpfc_reg_fcfi_mrq(struct lpfc_hba *phba, struct lpfcMboxq *mbox, int mode)
258562306a36Sopenharmony_ci{
258662306a36Sopenharmony_ci	struct lpfc_mbx_reg_fcfi_mrq *reg_fcfi;
258762306a36Sopenharmony_ci
258862306a36Sopenharmony_ci	/* This is ONLY for MRQ */
258962306a36Sopenharmony_ci	if (phba->cfg_nvmet_mrq <= 1)
259062306a36Sopenharmony_ci		return;
259162306a36Sopenharmony_ci
259262306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
259362306a36Sopenharmony_ci	reg_fcfi = &mbox->u.mqe.un.reg_fcfi_mrq;
259462306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_REG_FCFI_MRQ);
259562306a36Sopenharmony_ci	if (mode == 0) {
259662306a36Sopenharmony_ci		bf_set(lpfc_reg_fcfi_mrq_info_index, reg_fcfi,
259762306a36Sopenharmony_ci		       phba->fcf.current_rec.fcf_indx);
259862306a36Sopenharmony_ci		if (phba->fcf.current_rec.vlan_id != LPFC_FCOE_NULL_VID) {
259962306a36Sopenharmony_ci			bf_set(lpfc_reg_fcfi_mrq_vv, reg_fcfi, 1);
260062306a36Sopenharmony_ci			bf_set(lpfc_reg_fcfi_mrq_vlan_tag, reg_fcfi,
260162306a36Sopenharmony_ci			       phba->fcf.current_rec.vlan_id);
260262306a36Sopenharmony_ci		}
260362306a36Sopenharmony_ci		return;
260462306a36Sopenharmony_ci	}
260562306a36Sopenharmony_ci
260662306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rq_id0, reg_fcfi,
260762306a36Sopenharmony_ci	       phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id);
260862306a36Sopenharmony_ci	/* Match NVME frames of type FCP (protocol NVME) - rq_id0 */
260962306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_type_match0, reg_fcfi, FC_TYPE_FCP);
261062306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_type_mask0, reg_fcfi, 0xff);
261162306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rctl_match0, reg_fcfi, FC_RCTL_DD_UNSOL_CMD);
261262306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rctl_mask0, reg_fcfi, 0xff);
261362306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_ptc0, reg_fcfi, 1);
261462306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_pt0, reg_fcfi, 1);
261562306a36Sopenharmony_ci
261662306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_policy, reg_fcfi, 3); /* NVME connection id */
261762306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_mode, reg_fcfi, 1);
261862306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_filter, reg_fcfi, 1); /* rq_id0 */
261962306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_npairs, reg_fcfi, phba->cfg_nvmet_mrq);
262062306a36Sopenharmony_ci
262162306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rq_id1, reg_fcfi,
262262306a36Sopenharmony_ci	       phba->sli4_hba.hdr_rq->queue_id);
262362306a36Sopenharmony_ci	/* Match everything - rq_id1 */
262462306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_type_match1, reg_fcfi, 0);
262562306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_type_mask1, reg_fcfi, 0);
262662306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rctl_match1, reg_fcfi, 0);
262762306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rctl_mask1, reg_fcfi, 0);
262862306a36Sopenharmony_ci
262962306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rq_id2, reg_fcfi, REG_FCF_INVALID_QID);
263062306a36Sopenharmony_ci	bf_set(lpfc_reg_fcfi_mrq_rq_id3, reg_fcfi, REG_FCF_INVALID_QID);
263162306a36Sopenharmony_ci}
263262306a36Sopenharmony_ci
263362306a36Sopenharmony_ci/**
263462306a36Sopenharmony_ci * lpfc_unreg_fcfi - Initialize the UNREG_FCFI mailbox command
263562306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
263662306a36Sopenharmony_ci * @fcfi: FCFI to be unregistered.
263762306a36Sopenharmony_ci *
263862306a36Sopenharmony_ci * The UNREG_FCFI mailbox command supports Fibre Channel Forwarders (FCFs).
263962306a36Sopenharmony_ci * The SLI Host uses the command to inactivate an FCFI.
264062306a36Sopenharmony_ci **/
264162306a36Sopenharmony_civoid
264262306a36Sopenharmony_cilpfc_unreg_fcfi(struct lpfcMboxq *mbox, uint16_t fcfi)
264362306a36Sopenharmony_ci{
264462306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
264562306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_UNREG_FCFI);
264662306a36Sopenharmony_ci	bf_set(lpfc_unreg_fcfi, &mbox->u.mqe.un.unreg_fcfi, fcfi);
264762306a36Sopenharmony_ci}
264862306a36Sopenharmony_ci
264962306a36Sopenharmony_ci/**
265062306a36Sopenharmony_ci * lpfc_resume_rpi - Initialize the RESUME_RPI mailbox command
265162306a36Sopenharmony_ci * @mbox: pointer to lpfc mbox command to initialize.
265262306a36Sopenharmony_ci * @ndlp: The nodelist structure that describes the RPI to resume.
265362306a36Sopenharmony_ci *
265462306a36Sopenharmony_ci * The RESUME_RPI mailbox command is used to restart I/O to an RPI after a
265562306a36Sopenharmony_ci * link event.
265662306a36Sopenharmony_ci **/
265762306a36Sopenharmony_civoid
265862306a36Sopenharmony_cilpfc_resume_rpi(struct lpfcMboxq *mbox, struct lpfc_nodelist *ndlp)
265962306a36Sopenharmony_ci{
266062306a36Sopenharmony_ci	struct lpfc_hba *phba = ndlp->phba;
266162306a36Sopenharmony_ci	struct lpfc_mbx_resume_rpi *resume_rpi;
266262306a36Sopenharmony_ci
266362306a36Sopenharmony_ci	memset(mbox, 0, sizeof(*mbox));
266462306a36Sopenharmony_ci	resume_rpi = &mbox->u.mqe.un.resume_rpi;
266562306a36Sopenharmony_ci	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_RESUME_RPI);
266662306a36Sopenharmony_ci	bf_set(lpfc_resume_rpi_index, resume_rpi,
266762306a36Sopenharmony_ci	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
266862306a36Sopenharmony_ci	bf_set(lpfc_resume_rpi_ii, resume_rpi, RESUME_INDEX_RPI);
266962306a36Sopenharmony_ci	resume_rpi->event_tag = ndlp->phba->fc_eventTag;
267062306a36Sopenharmony_ci}
267162306a36Sopenharmony_ci
2672