18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *   This file is provided under a dual BSD/GPLv2 license.  When using or
38c2ecf20Sopenharmony_ci *   redistributing this file, you may do so under either license.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *   GPL LICENSE SUMMARY
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
88c2ecf20Sopenharmony_ci *   Copyright (C) 2017 T-Platforms. All Rights Reserved.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *   This program is free software; you can redistribute it and/or modify
118c2ecf20Sopenharmony_ci *   it under the terms of version 2 of the GNU General Public License as
128c2ecf20Sopenharmony_ci *   published by the Free Software Foundation.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful, but
158c2ecf20Sopenharmony_ci *   WITHOUT ANY WARRANTY; without even the implied warranty of
168c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
178c2ecf20Sopenharmony_ci *   General Public License for more details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *   BSD LICENSE
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
228c2ecf20Sopenharmony_ci *   Copyright (C) 2017 T-Platforms. All Rights Reserved.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *   Redistribution and use in source and binary forms, with or without
258c2ecf20Sopenharmony_ci *   modification, are permitted provided that the following conditions
268c2ecf20Sopenharmony_ci *   are met:
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
298c2ecf20Sopenharmony_ci *       notice, this list of conditions and the following disclaimer.
308c2ecf20Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copy
318c2ecf20Sopenharmony_ci *       notice, this list of conditions and the following disclaimer in
328c2ecf20Sopenharmony_ci *       the documentation and/or other materials provided with the
338c2ecf20Sopenharmony_ci *       distribution.
348c2ecf20Sopenharmony_ci *     * Neither the name of Intel Corporation nor the names of its
358c2ecf20Sopenharmony_ci *       contributors may be used to endorse or promote products derived
368c2ecf20Sopenharmony_ci *       from this software without specific prior written permission.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
398c2ecf20Sopenharmony_ci *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
408c2ecf20Sopenharmony_ci *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
418c2ecf20Sopenharmony_ci *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
428c2ecf20Sopenharmony_ci *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
438c2ecf20Sopenharmony_ci *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
448c2ecf20Sopenharmony_ci *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
458c2ecf20Sopenharmony_ci *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
468c2ecf20Sopenharmony_ci *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
478c2ecf20Sopenharmony_ci *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
488c2ecf20Sopenharmony_ci *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * PCIe NTB Pingpong Linux driver
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * How to use this tool, by example.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * Assuming $DBG_DIR is something like:
578c2ecf20Sopenharmony_ci * '/sys/kernel/debug/ntb_perf/0000:00:03.0'
588c2ecf20Sopenharmony_ci * Suppose aside from local device there is at least one remote device
598c2ecf20Sopenharmony_ci * connected to NTB with index 0.
608c2ecf20Sopenharmony_ci *-----------------------------------------------------------------------------
618c2ecf20Sopenharmony_ci * Eg: install driver with specified delay between doorbell event and response
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * root@self# insmod ntb_pingpong.ko delay_ms=1000
648c2ecf20Sopenharmony_ci *-----------------------------------------------------------------------------
658c2ecf20Sopenharmony_ci * Eg: get number of ping-pong cycles performed
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * root@self# cat $DBG_DIR/count
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#include <linux/init.h>
718c2ecf20Sopenharmony_ci#include <linux/kernel.h>
728c2ecf20Sopenharmony_ci#include <linux/module.h>
738c2ecf20Sopenharmony_ci#include <linux/device.h>
748c2ecf20Sopenharmony_ci#include <linux/bitops.h>
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#include <linux/pci.h>
778c2ecf20Sopenharmony_ci#include <linux/slab.h>
788c2ecf20Sopenharmony_ci#include <linux/hrtimer.h>
798c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#include <linux/ntb.h>
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define DRIVER_NAME		"ntb_pingpong"
848c2ecf20Sopenharmony_ci#define DRIVER_VERSION		"2.0"
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
878c2ecf20Sopenharmony_ciMODULE_VERSION(DRIVER_VERSION);
888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Allen Hubbe <Allen.Hubbe@emc.com>");
898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PCIe NTB Simple Pingpong Client");
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic unsigned int unsafe;
928c2ecf20Sopenharmony_cimodule_param(unsafe, uint, 0644);
938c2ecf20Sopenharmony_ciMODULE_PARM_DESC(unsafe, "Run even though ntb operations may be unsafe");
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic unsigned int delay_ms = 1000;
968c2ecf20Sopenharmony_cimodule_param(delay_ms, uint, 0644);
978c2ecf20Sopenharmony_ciMODULE_PARM_DESC(delay_ms, "Milliseconds to delay the response to peer");
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistruct pp_ctx {
1008c2ecf20Sopenharmony_ci	struct ntb_dev *ntb;
1018c2ecf20Sopenharmony_ci	struct hrtimer timer;
1028c2ecf20Sopenharmony_ci	u64 in_db;
1038c2ecf20Sopenharmony_ci	u64 out_db;
1048c2ecf20Sopenharmony_ci	int out_pidx;
1058c2ecf20Sopenharmony_ci	u64 nmask;
1068c2ecf20Sopenharmony_ci	u64 pmask;
1078c2ecf20Sopenharmony_ci	atomic_t count;
1088c2ecf20Sopenharmony_ci	spinlock_t lock;
1098c2ecf20Sopenharmony_ci	struct dentry *dbgfs_dir;
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci#define to_pp_timer(__timer) \
1128c2ecf20Sopenharmony_ci	container_of(__timer, struct pp_ctx, timer)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct dentry *pp_dbgfs_topdir;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int pp_find_next_peer(struct pp_ctx *pp)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	u64 link, out_db;
1198c2ecf20Sopenharmony_ci	int pidx;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	link = ntb_link_is_up(pp->ntb, NULL, NULL);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Find next available peer */
1248c2ecf20Sopenharmony_ci	if (link & pp->nmask)
1258c2ecf20Sopenharmony_ci		pidx = __ffs64(link & pp->nmask);
1268c2ecf20Sopenharmony_ci	else if (link & pp->pmask)
1278c2ecf20Sopenharmony_ci		pidx = __ffs64(link & pp->pmask);
1288c2ecf20Sopenharmony_ci	else
1298c2ecf20Sopenharmony_ci		return -ENODEV;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	out_db = BIT_ULL(ntb_peer_port_number(pp->ntb, pidx));
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	spin_lock(&pp->lock);
1348c2ecf20Sopenharmony_ci	pp->out_pidx = pidx;
1358c2ecf20Sopenharmony_ci	pp->out_db = out_db;
1368c2ecf20Sopenharmony_ci	spin_unlock(&pp->lock);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return 0;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic void pp_setup(struct pp_ctx *pp)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	int ret;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	ntb_db_set_mask(pp->ntb, pp->in_db);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	hrtimer_cancel(&pp->timer);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	ret = pp_find_next_peer(pp);
1508c2ecf20Sopenharmony_ci	if (ret == -ENODEV) {
1518c2ecf20Sopenharmony_ci		dev_dbg(&pp->ntb->dev, "Got no peers, so cancel\n");
1528c2ecf20Sopenharmony_ci		return;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	dev_dbg(&pp->ntb->dev, "Ping-pong started with port %d, db %#llx\n",
1568c2ecf20Sopenharmony_ci		ntb_peer_port_number(pp->ntb, pp->out_pidx), pp->out_db);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void pp_clear(struct pp_ctx *pp)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	hrtimer_cancel(&pp->timer);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	ntb_db_set_mask(pp->ntb, pp->in_db);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	dev_dbg(&pp->ntb->dev, "Ping-pong cancelled\n");
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic void pp_ping(struct pp_ctx *pp)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	u32 count;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	count = atomic_read(&pp->count);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	spin_lock(&pp->lock);
1778c2ecf20Sopenharmony_ci	ntb_peer_spad_write(pp->ntb, pp->out_pidx, 0, count);
1788c2ecf20Sopenharmony_ci	ntb_peer_msg_write(pp->ntb, pp->out_pidx, 0, count);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	dev_dbg(&pp->ntb->dev, "Ping port %d spad %#x, msg %#x\n",
1818c2ecf20Sopenharmony_ci		ntb_peer_port_number(pp->ntb, pp->out_pidx), count, count);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	ntb_peer_db_set(pp->ntb, pp->out_db);
1848c2ecf20Sopenharmony_ci	ntb_db_clear_mask(pp->ntb, pp->in_db);
1858c2ecf20Sopenharmony_ci	spin_unlock(&pp->lock);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic void pp_pong(struct pp_ctx *pp)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	u32 msg_data = -1, spad_data = -1;
1918c2ecf20Sopenharmony_ci	int pidx = 0;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Read pong data */
1948c2ecf20Sopenharmony_ci	spad_data = ntb_spad_read(pp->ntb, 0);
1958c2ecf20Sopenharmony_ci	msg_data = ntb_msg_read(pp->ntb, &pidx, 0);
1968c2ecf20Sopenharmony_ci	ntb_msg_clear_sts(pp->ntb, -1);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/*
1998c2ecf20Sopenharmony_ci	 * Scratchpad and message data may differ, since message register can't
2008c2ecf20Sopenharmony_ci	 * be rewritten unless status is cleared. Additionally either of them
2018c2ecf20Sopenharmony_ci	 * might be unsupported
2028c2ecf20Sopenharmony_ci	 */
2038c2ecf20Sopenharmony_ci	dev_dbg(&pp->ntb->dev, "Pong spad %#x, msg %#x (port %d)\n",
2048c2ecf20Sopenharmony_ci		spad_data, msg_data, ntb_peer_port_number(pp->ntb, pidx));
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	atomic_inc(&pp->count);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	ntb_db_set_mask(pp->ntb, pp->in_db);
2098c2ecf20Sopenharmony_ci	ntb_db_clear(pp->ntb, pp->in_db);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic enum hrtimer_restart pp_timer_func(struct hrtimer *t)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	struct pp_ctx *pp = to_pp_timer(t);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	pp_ping(pp);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return HRTIMER_NORESTART;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic void pp_link_event(void *ctx)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct pp_ctx *pp = ctx;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	pp_setup(pp);
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic void pp_db_event(void *ctx, int vec)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct pp_ctx *pp = ctx;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	pp_pong(pp);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic const struct ntb_ctx_ops pp_ops = {
2388c2ecf20Sopenharmony_ci	.link_event = pp_link_event,
2398c2ecf20Sopenharmony_ci	.db_event = pp_db_event
2408c2ecf20Sopenharmony_ci};
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic int pp_check_ntb(struct ntb_dev *ntb)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	u64 pmask;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	if (ntb_db_is_unsafe(ntb)) {
2478c2ecf20Sopenharmony_ci		dev_dbg(&ntb->dev, "Doorbell is unsafe\n");
2488c2ecf20Sopenharmony_ci		if (!unsafe)
2498c2ecf20Sopenharmony_ci			return -EINVAL;
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	if (ntb_spad_is_unsafe(ntb)) {
2538c2ecf20Sopenharmony_ci		dev_dbg(&ntb->dev, "Scratchpad is unsafe\n");
2548c2ecf20Sopenharmony_ci		if (!unsafe)
2558c2ecf20Sopenharmony_ci			return -EINVAL;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	pmask = GENMASK_ULL(ntb_peer_port_count(ntb), 0);
2598c2ecf20Sopenharmony_ci	if ((ntb_db_valid_mask(ntb) & pmask) != pmask) {
2608c2ecf20Sopenharmony_ci		dev_err(&ntb->dev, "Unsupported DB configuration\n");
2618c2ecf20Sopenharmony_ci		return -EINVAL;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	if (ntb_spad_count(ntb) < 1 && ntb_msg_count(ntb) < 1) {
2658c2ecf20Sopenharmony_ci		dev_err(&ntb->dev, "Scratchpads and messages unsupported\n");
2668c2ecf20Sopenharmony_ci		return -EINVAL;
2678c2ecf20Sopenharmony_ci	} else if (ntb_spad_count(ntb) < 1) {
2688c2ecf20Sopenharmony_ci		dev_dbg(&ntb->dev, "Scratchpads unsupported\n");
2698c2ecf20Sopenharmony_ci	} else if (ntb_msg_count(ntb) < 1) {
2708c2ecf20Sopenharmony_ci		dev_dbg(&ntb->dev, "Messages unsupported\n");
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return 0;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic struct pp_ctx *pp_create_data(struct ntb_dev *ntb)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct pp_ctx *pp;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	pp = devm_kzalloc(&ntb->dev, sizeof(*pp), GFP_KERNEL);
2818c2ecf20Sopenharmony_ci	if (!pp)
2828c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	pp->ntb = ntb;
2858c2ecf20Sopenharmony_ci	atomic_set(&pp->count, 0);
2868c2ecf20Sopenharmony_ci	spin_lock_init(&pp->lock);
2878c2ecf20Sopenharmony_ci	hrtimer_init(&pp->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2888c2ecf20Sopenharmony_ci	pp->timer.function = pp_timer_func;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return pp;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic void pp_init_flds(struct pp_ctx *pp)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	int pidx, lport, pcnt;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* Find global port index */
2988c2ecf20Sopenharmony_ci	lport = ntb_port_number(pp->ntb);
2998c2ecf20Sopenharmony_ci	pcnt = ntb_peer_port_count(pp->ntb);
3008c2ecf20Sopenharmony_ci	for (pidx = 0; pidx < pcnt; pidx++) {
3018c2ecf20Sopenharmony_ci		if (lport < ntb_peer_port_number(pp->ntb, pidx))
3028c2ecf20Sopenharmony_ci			break;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	pp->in_db = BIT_ULL(lport);
3068c2ecf20Sopenharmony_ci	pp->pmask = GENMASK_ULL(pidx, 0) >> 1;
3078c2ecf20Sopenharmony_ci	pp->nmask = GENMASK_ULL(pcnt - 1, pidx);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	dev_dbg(&pp->ntb->dev, "Inbound db %#llx, prev %#llx, next %#llx\n",
3108c2ecf20Sopenharmony_ci		pp->in_db, pp->pmask, pp->nmask);
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int pp_mask_events(struct pp_ctx *pp)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	u64 db_mask, msg_mask;
3168c2ecf20Sopenharmony_ci	int ret;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	db_mask = ntb_db_valid_mask(pp->ntb);
3198c2ecf20Sopenharmony_ci	ret = ntb_db_set_mask(pp->ntb, db_mask);
3208c2ecf20Sopenharmony_ci	if (ret)
3218c2ecf20Sopenharmony_ci		return ret;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	/* Skip message events masking if unsupported */
3248c2ecf20Sopenharmony_ci	if (ntb_msg_count(pp->ntb) < 1)
3258c2ecf20Sopenharmony_ci		return 0;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	msg_mask = ntb_msg_outbits(pp->ntb) | ntb_msg_inbits(pp->ntb);
3288c2ecf20Sopenharmony_ci	return ntb_msg_set_mask(pp->ntb, msg_mask);
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_cistatic int pp_setup_ctx(struct pp_ctx *pp)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	int ret;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	ret = ntb_set_ctx(pp->ntb, pp, &pp_ops);
3368c2ecf20Sopenharmony_ci	if (ret)
3378c2ecf20Sopenharmony_ci		return ret;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	ntb_link_enable(pp->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
3408c2ecf20Sopenharmony_ci	/* Might be not necessary */
3418c2ecf20Sopenharmony_ci	ntb_link_event(pp->ntb);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	return 0;
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic void pp_clear_ctx(struct pp_ctx *pp)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	ntb_link_disable(pp->ntb);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	ntb_clear_ctx(pp->ntb);
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic void pp_setup_dbgfs(struct pp_ctx *pp)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	struct pci_dev *pdev = pp->ntb->pdev;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	pp->dbgfs_dir = debugfs_create_dir(pci_name(pdev), pp_dbgfs_topdir);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	debugfs_create_atomic_t("count", 0600, pp->dbgfs_dir, &pp->count);
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic void pp_clear_dbgfs(struct pp_ctx *pp)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	debugfs_remove_recursive(pp->dbgfs_dir);
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic int pp_probe(struct ntb_client *client, struct ntb_dev *ntb)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	struct pp_ctx *pp;
3708c2ecf20Sopenharmony_ci	int ret;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	ret = pp_check_ntb(ntb);
3738c2ecf20Sopenharmony_ci	if (ret)
3748c2ecf20Sopenharmony_ci		return ret;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	pp = pp_create_data(ntb);
3778c2ecf20Sopenharmony_ci	if (IS_ERR(pp))
3788c2ecf20Sopenharmony_ci		return PTR_ERR(pp);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	pp_init_flds(pp);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	ret = pp_mask_events(pp);
3838c2ecf20Sopenharmony_ci	if (ret)
3848c2ecf20Sopenharmony_ci		return ret;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	ret = pp_setup_ctx(pp);
3878c2ecf20Sopenharmony_ci	if (ret)
3888c2ecf20Sopenharmony_ci		return ret;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	pp_setup_dbgfs(pp);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return 0;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic void pp_remove(struct ntb_client *client, struct ntb_dev *ntb)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	struct pp_ctx *pp = ntb->ctx;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	pp_clear_dbgfs(pp);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	pp_clear_ctx(pp);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	pp_clear(pp);
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic struct ntb_client pp_client = {
4078c2ecf20Sopenharmony_ci	.ops = {
4088c2ecf20Sopenharmony_ci		.probe = pp_probe,
4098c2ecf20Sopenharmony_ci		.remove = pp_remove
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci};
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_cistatic int __init pp_init(void)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	int ret;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (debugfs_initialized())
4188c2ecf20Sopenharmony_ci		pp_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	ret = ntb_register_client(&pp_client);
4218c2ecf20Sopenharmony_ci	if (ret)
4228c2ecf20Sopenharmony_ci		debugfs_remove_recursive(pp_dbgfs_topdir);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return ret;
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_cimodule_init(pp_init);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic void __exit pp_exit(void)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	ntb_unregister_client(&pp_client);
4318c2ecf20Sopenharmony_ci	debugfs_remove_recursive(pp_dbgfs_topdir);
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_cimodule_exit(pp_exit);
434