162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
362306a36Sopenharmony_ci *   redistributing this file, you may do so under either license.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *   GPL LICENSE SUMMARY
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *   Copyright(c) 2012 Intel Corporation. All rights reserved.
862306a36Sopenharmony_ci *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *   This program is free software; you can redistribute it and/or modify
1162306a36Sopenharmony_ci *   it under the terms of version 2 of the GNU General Public License as
1262306a36Sopenharmony_ci *   published by the Free Software Foundation.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci *   BSD LICENSE
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *   Copyright(c) 2012 Intel Corporation. All rights reserved.
1762306a36Sopenharmony_ci *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *   Redistribution and use in source and binary forms, with or without
2062306a36Sopenharmony_ci *   modification, are permitted provided that the following conditions
2162306a36Sopenharmony_ci *   are met:
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
2462306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer.
2562306a36Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copy
2662306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer in
2762306a36Sopenharmony_ci *       the documentation and/or other materials provided with the
2862306a36Sopenharmony_ci *       distribution.
2962306a36Sopenharmony_ci *     * Neither the name of Intel Corporation nor the names of its
3062306a36Sopenharmony_ci *       contributors may be used to endorse or promote products derived
3162306a36Sopenharmony_ci *       from this software without specific prior written permission.
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3462306a36Sopenharmony_ci *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3562306a36Sopenharmony_ci *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3662306a36Sopenharmony_ci *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3762306a36Sopenharmony_ci *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3862306a36Sopenharmony_ci *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3962306a36Sopenharmony_ci *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4062306a36Sopenharmony_ci *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4162306a36Sopenharmony_ci *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4262306a36Sopenharmony_ci *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4362306a36Sopenharmony_ci *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * Intel PCIe NTB Linux driver
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#ifndef NTB_HW_INTEL_H
4962306a36Sopenharmony_ci#define NTB_HW_INTEL_H
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#include <linux/ntb.h>
5262306a36Sopenharmony_ci#include <linux/pci.h>
5362306a36Sopenharmony_ci#include <linux/io-64-nonatomic-lo-hi.h>
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* PCI device IDs */
5662306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_JSF	0x3725
5762306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_PS_JSF	0x3726
5862306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_SS_JSF	0x3727
5962306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_SNB	0x3C0D
6062306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_PS_SNB	0x3C0E
6162306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_SS_SNB	0x3C0F
6262306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_IVT	0x0E0D
6362306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_PS_IVT	0x0E0E
6462306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_SS_IVT	0x0E0F
6562306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_HSX	0x2F0D
6662306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_PS_HSX	0x2F0E
6762306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_SS_HSX	0x2F0F
6862306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_BDX	0x6F0D
6962306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_PS_BDX	0x6F0E
7062306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_SS_BDX	0x6F0F
7162306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_SKX	0x201C
7262306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_ICX	0x347e
7362306a36Sopenharmony_ci#define PCI_DEVICE_ID_INTEL_NTB_B2B_GNR	0x0db4
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/* Ntb control and link status */
7662306a36Sopenharmony_ci#define NTB_CTL_CFG_LOCK		BIT(0)
7762306a36Sopenharmony_ci#define NTB_CTL_DISABLE			BIT(1)
7862306a36Sopenharmony_ci#define NTB_CTL_S2P_BAR2_SNOOP		BIT(2)
7962306a36Sopenharmony_ci#define NTB_CTL_P2S_BAR2_SNOOP		BIT(4)
8062306a36Sopenharmony_ci#define NTB_CTL_S2P_BAR4_SNOOP		BIT(6)
8162306a36Sopenharmony_ci#define NTB_CTL_P2S_BAR4_SNOOP		BIT(8)
8262306a36Sopenharmony_ci#define NTB_CTL_S2P_BAR5_SNOOP		BIT(12)
8362306a36Sopenharmony_ci#define NTB_CTL_P2S_BAR5_SNOOP		BIT(14)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define NTB_LNK_STA_ACTIVE_BIT		0x2000
8662306a36Sopenharmony_ci#define NTB_LNK_STA_SPEED_MASK		0x000f
8762306a36Sopenharmony_ci#define NTB_LNK_STA_WIDTH_MASK		0x03f0
8862306a36Sopenharmony_ci#define NTB_LNK_STA_ACTIVE(x)		(!!((x) & NTB_LNK_STA_ACTIVE_BIT))
8962306a36Sopenharmony_ci#define NTB_LNK_STA_SPEED(x)		((x) & NTB_LNK_STA_SPEED_MASK)
9062306a36Sopenharmony_ci#define NTB_LNK_STA_WIDTH(x)		(((x) & NTB_LNK_STA_WIDTH_MASK) >> 4)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/* flags to indicate unsafe api */
9362306a36Sopenharmony_ci#define NTB_UNSAFE_DB			BIT_ULL(0)
9462306a36Sopenharmony_ci#define NTB_UNSAFE_SPAD			BIT_ULL(1)
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci#define NTB_BAR_MASK_64			~(0xfull)
9762306a36Sopenharmony_ci#define NTB_BAR_MASK_32			~(0xfu)
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistruct intel_ntb_dev;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistruct intel_ntb_reg {
10262306a36Sopenharmony_ci	int (*poll_link)(struct intel_ntb_dev *ndev);
10362306a36Sopenharmony_ci	int (*link_is_up)(struct intel_ntb_dev *ndev);
10462306a36Sopenharmony_ci	u64 (*db_ioread)(const void __iomem *mmio);
10562306a36Sopenharmony_ci	void (*db_iowrite)(u64 db_bits, void __iomem *mmio);
10662306a36Sopenharmony_ci	unsigned long			ntb_ctl;
10762306a36Sopenharmony_ci	resource_size_t			db_size;
10862306a36Sopenharmony_ci	int				mw_bar[];
10962306a36Sopenharmony_ci};
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistruct intel_ntb_alt_reg {
11262306a36Sopenharmony_ci	unsigned long			db_bell;
11362306a36Sopenharmony_ci	unsigned long			db_mask;
11462306a36Sopenharmony_ci	unsigned long			db_clear;
11562306a36Sopenharmony_ci	unsigned long			spad;
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistruct intel_ntb_xlat_reg {
11962306a36Sopenharmony_ci	unsigned long			bar0_base;
12062306a36Sopenharmony_ci	unsigned long			bar2_xlat;
12162306a36Sopenharmony_ci	unsigned long			bar2_limit;
12262306a36Sopenharmony_ci	unsigned short			bar2_idx;
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistruct intel_b2b_addr {
12662306a36Sopenharmony_ci	phys_addr_t			bar0_addr;
12762306a36Sopenharmony_ci	phys_addr_t			bar2_addr64;
12862306a36Sopenharmony_ci	phys_addr_t			bar4_addr64;
12962306a36Sopenharmony_ci	phys_addr_t			bar4_addr32;
13062306a36Sopenharmony_ci	phys_addr_t			bar5_addr32;
13162306a36Sopenharmony_ci};
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistruct intel_ntb_vec {
13462306a36Sopenharmony_ci	struct intel_ntb_dev		*ndev;
13562306a36Sopenharmony_ci	int				num;
13662306a36Sopenharmony_ci};
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistruct intel_ntb_dev {
13962306a36Sopenharmony_ci	struct ntb_dev			ntb;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	/* offset of peer bar0 in b2b bar */
14262306a36Sopenharmony_ci	unsigned long			b2b_off;
14362306a36Sopenharmony_ci	/* mw idx used to access peer bar0 */
14462306a36Sopenharmony_ci	unsigned int			b2b_idx;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/* BAR45 is split into BAR4 and BAR5 */
14762306a36Sopenharmony_ci	bool				bar4_split;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	u32				ntb_ctl;
15062306a36Sopenharmony_ci	u32				lnk_sta;
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	unsigned char			mw_count;
15362306a36Sopenharmony_ci	unsigned char			spad_count;
15462306a36Sopenharmony_ci	unsigned char			db_count;
15562306a36Sopenharmony_ci	unsigned char			db_vec_count;
15662306a36Sopenharmony_ci	unsigned char			db_vec_shift;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	u64				db_valid_mask;
15962306a36Sopenharmony_ci	u64				db_link_mask;
16062306a36Sopenharmony_ci	u64				db_mask;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	/* synchronize rmw access of db_mask and hw reg */
16362306a36Sopenharmony_ci	spinlock_t			db_mask_lock;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	struct msix_entry		*msix;
16662306a36Sopenharmony_ci	struct intel_ntb_vec		*vec;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	const struct intel_ntb_reg	*reg;
16962306a36Sopenharmony_ci	const struct intel_ntb_alt_reg	*self_reg;
17062306a36Sopenharmony_ci	const struct intel_ntb_alt_reg	*peer_reg;
17162306a36Sopenharmony_ci	const struct intel_ntb_xlat_reg	*xlat_reg;
17262306a36Sopenharmony_ci	void				__iomem *self_mmio;
17362306a36Sopenharmony_ci	void				__iomem *peer_mmio;
17462306a36Sopenharmony_ci	phys_addr_t			peer_addr;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	unsigned long			last_ts;
17762306a36Sopenharmony_ci	struct delayed_work		hb_timer;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	unsigned long			hwerr_flags;
18062306a36Sopenharmony_ci	unsigned long			unsafe_flags;
18162306a36Sopenharmony_ci	unsigned long			unsafe_flags_ignore;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	struct dentry			*debugfs_dir;
18462306a36Sopenharmony_ci	struct dentry			*debugfs_info;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	/* gen4 entries */
18762306a36Sopenharmony_ci	int				dev_up;
18862306a36Sopenharmony_ci};
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci#define ntb_ndev(__ntb) container_of(__ntb, struct intel_ntb_dev, ntb)
19162306a36Sopenharmony_ci#define hb_ndev(__work) container_of(__work, struct intel_ntb_dev, \
19262306a36Sopenharmony_ci				     hb_timer.work)
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_cistatic inline int pdev_is_gen1(struct pci_dev *pdev)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	switch (pdev->device) {
19762306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
19862306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
19962306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
20062306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
20162306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
20262306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
20362306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
20462306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
20562306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
20662306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
20762306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
20862306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
20962306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
21062306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
21162306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
21262306a36Sopenharmony_ci		return 1;
21362306a36Sopenharmony_ci	}
21462306a36Sopenharmony_ci	return 0;
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic inline int pdev_is_gen3(struct pci_dev *pdev)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	if (pdev->device == PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)
22062306a36Sopenharmony_ci		return 1;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	return 0;
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic inline int pdev_is_gen4(struct pci_dev *pdev)
22662306a36Sopenharmony_ci{
22762306a36Sopenharmony_ci	if (pdev->device == PCI_DEVICE_ID_INTEL_NTB_B2B_ICX)
22862306a36Sopenharmony_ci		return 1;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	return 0;
23162306a36Sopenharmony_ci}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistatic inline int pdev_is_gen5(struct pci_dev *pdev)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	return pdev->device == PCI_DEVICE_ID_INTEL_NTB_B2B_GNR;
23662306a36Sopenharmony_ci}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci#endif
239