xref: /openbmc/linux/drivers/ntb/test/ntb_pingpong.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1963de473SAllen Hubbe /*
2963de473SAllen Hubbe  *   This file is provided under a dual BSD/GPLv2 license.  When using or
3963de473SAllen Hubbe  *   redistributing this file, you may do so under either license.
4963de473SAllen Hubbe  *
5963de473SAllen Hubbe  *   GPL LICENSE SUMMARY
6963de473SAllen Hubbe  *
7963de473SAllen Hubbe  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8c7aeb0afSSerge Semin  *   Copyright (C) 2017 T-Platforms. All Rights Reserved.
9963de473SAllen Hubbe  *
10963de473SAllen Hubbe  *   This program is free software; you can redistribute it and/or modify
11963de473SAllen Hubbe  *   it under the terms of version 2 of the GNU General Public License as
12963de473SAllen Hubbe  *   published by the Free Software Foundation.
13963de473SAllen Hubbe  *
14963de473SAllen Hubbe  *   This program is distributed in the hope that it will be useful, but
15963de473SAllen Hubbe  *   WITHOUT ANY WARRANTY; without even the implied warranty of
16963de473SAllen Hubbe  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17963de473SAllen Hubbe  *   General Public License for more details.
18963de473SAllen Hubbe  *
19963de473SAllen Hubbe  *   BSD LICENSE
20963de473SAllen Hubbe  *
21963de473SAllen Hubbe  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22c7aeb0afSSerge Semin  *   Copyright (C) 2017 T-Platforms. All Rights Reserved.
23963de473SAllen Hubbe  *
24963de473SAllen Hubbe  *   Redistribution and use in source and binary forms, with or without
25963de473SAllen Hubbe  *   modification, are permitted provided that the following conditions
26963de473SAllen Hubbe  *   are met:
27963de473SAllen Hubbe  *
28963de473SAllen Hubbe  *     * Redistributions of source code must retain the above copyright
29963de473SAllen Hubbe  *       notice, this list of conditions and the following disclaimer.
30963de473SAllen Hubbe  *     * Redistributions in binary form must reproduce the above copy
31963de473SAllen Hubbe  *       notice, this list of conditions and the following disclaimer in
32963de473SAllen Hubbe  *       the documentation and/or other materials provided with the
33963de473SAllen Hubbe  *       distribution.
34963de473SAllen Hubbe  *     * Neither the name of Intel Corporation nor the names of its
35963de473SAllen Hubbe  *       contributors may be used to endorse or promote products derived
36963de473SAllen Hubbe  *       from this software without specific prior written permission.
37963de473SAllen Hubbe  *
38963de473SAllen Hubbe  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39963de473SAllen Hubbe  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40963de473SAllen Hubbe  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41963de473SAllen Hubbe  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42963de473SAllen Hubbe  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43963de473SAllen Hubbe  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44963de473SAllen Hubbe  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45963de473SAllen Hubbe  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46963de473SAllen Hubbe  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47963de473SAllen Hubbe  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48963de473SAllen Hubbe  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49963de473SAllen Hubbe  *
50963de473SAllen Hubbe  * PCIe NTB Pingpong Linux driver
51963de473SAllen Hubbe  */
52963de473SAllen Hubbe 
53c7aeb0afSSerge Semin /*
54c7aeb0afSSerge Semin  * How to use this tool, by example.
55c7aeb0afSSerge Semin  *
56c7aeb0afSSerge Semin  * Assuming $DBG_DIR is something like:
57c7aeb0afSSerge Semin  * '/sys/kernel/debug/ntb_perf/0000:00:03.0'
58c7aeb0afSSerge Semin  * Suppose aside from local device there is at least one remote device
59c7aeb0afSSerge Semin  * connected to NTB with index 0.
60c7aeb0afSSerge Semin  *-----------------------------------------------------------------------------
61c7aeb0afSSerge Semin  * Eg: install driver with specified delay between doorbell event and response
62c7aeb0afSSerge Semin  *
63c7aeb0afSSerge Semin  * root@self# insmod ntb_pingpong.ko delay_ms=1000
64c7aeb0afSSerge Semin  *-----------------------------------------------------------------------------
65c7aeb0afSSerge Semin  * Eg: get number of ping-pong cycles performed
66c7aeb0afSSerge Semin  *
67c7aeb0afSSerge Semin  * root@self# cat $DBG_DIR/count
68c7aeb0afSSerge Semin  */
69963de473SAllen Hubbe 
70963de473SAllen Hubbe #include <linux/init.h>
71963de473SAllen Hubbe #include <linux/kernel.h>
72963de473SAllen Hubbe #include <linux/module.h>
73c7aeb0afSSerge Semin #include <linux/device.h>
74c7aeb0afSSerge Semin #include <linux/bitops.h>
75963de473SAllen Hubbe 
76963de473SAllen Hubbe #include <linux/pci.h>
77963de473SAllen Hubbe #include <linux/slab.h>
78c7aeb0afSSerge Semin #include <linux/hrtimer.h>
7920572ee1SLogan Gunthorpe #include <linux/debugfs.h>
80963de473SAllen Hubbe 
81963de473SAllen Hubbe #include <linux/ntb.h>
82963de473SAllen Hubbe 
83963de473SAllen Hubbe #define DRIVER_NAME		"ntb_pingpong"
84c7aeb0afSSerge Semin #define DRIVER_VERSION		"2.0"
85963de473SAllen Hubbe 
860ed08f82SGreg Kroah-Hartman MODULE_LICENSE("Dual BSD/GPL");
87963de473SAllen Hubbe MODULE_VERSION(DRIVER_VERSION);
88c7aeb0afSSerge Semin MODULE_AUTHOR("Allen Hubbe <Allen.Hubbe@emc.com>");
89c7aeb0afSSerge Semin MODULE_DESCRIPTION("PCIe NTB Simple Pingpong Client");
90963de473SAllen Hubbe 
91963de473SAllen Hubbe static unsigned int unsafe;
92963de473SAllen Hubbe module_param(unsafe, uint, 0644);
93963de473SAllen Hubbe MODULE_PARM_DESC(unsafe, "Run even though ntb operations may be unsafe");
94963de473SAllen Hubbe 
95963de473SAllen Hubbe static unsigned int delay_ms = 1000;
96963de473SAllen Hubbe module_param(delay_ms, uint, 0644);
97963de473SAllen Hubbe MODULE_PARM_DESC(delay_ms, "Milliseconds to delay the response to peer");
98963de473SAllen Hubbe 
99963de473SAllen Hubbe struct pp_ctx {
100963de473SAllen Hubbe 	struct ntb_dev *ntb;
101c7aeb0afSSerge Semin 	struct hrtimer timer;
102c7aeb0afSSerge Semin 	u64 in_db;
103c7aeb0afSSerge Semin 	u64 out_db;
104c7aeb0afSSerge Semin 	int out_pidx;
105c7aeb0afSSerge Semin 	u64 nmask;
106c7aeb0afSSerge Semin 	u64 pmask;
10720572ee1SLogan Gunthorpe 	atomic_t count;
108c7aeb0afSSerge Semin 	spinlock_t lock;
109c7aeb0afSSerge Semin 	struct dentry *dbgfs_dir;
110963de473SAllen Hubbe };
111c7aeb0afSSerge Semin #define to_pp_timer(__timer) \
112c7aeb0afSSerge Semin 	container_of(__timer, struct pp_ctx, timer)
113963de473SAllen Hubbe 
114c7aeb0afSSerge Semin static struct dentry *pp_dbgfs_topdir;
11520572ee1SLogan Gunthorpe 
pp_find_next_peer(struct pp_ctx * pp)116c7aeb0afSSerge Semin static int pp_find_next_peer(struct pp_ctx *pp)
117963de473SAllen Hubbe {
118c7aeb0afSSerge Semin 	u64 link, out_db;
119c7aeb0afSSerge Semin 	int pidx;
120963de473SAllen Hubbe 
121c7aeb0afSSerge Semin 	link = ntb_link_is_up(pp->ntb, NULL, NULL);
122963de473SAllen Hubbe 
123c7aeb0afSSerge Semin 	/* Find next available peer */
124ca93c457SLogan Gunthorpe 	if (link & pp->nmask)
125c7aeb0afSSerge Semin 		pidx = __ffs64(link & pp->nmask);
126ca93c457SLogan Gunthorpe 	else if (link & pp->pmask)
127c7aeb0afSSerge Semin 		pidx = __ffs64(link & pp->pmask);
128ca93c457SLogan Gunthorpe 	else
129c7aeb0afSSerge Semin 		return -ENODEV;
130ca93c457SLogan Gunthorpe 
131ca93c457SLogan Gunthorpe 	out_db = BIT_ULL(ntb_peer_port_number(pp->ntb, pidx));
132963de473SAllen Hubbe 
133c7aeb0afSSerge Semin 	spin_lock(&pp->lock);
134c7aeb0afSSerge Semin 	pp->out_pidx = pidx;
135c7aeb0afSSerge Semin 	pp->out_db = out_db;
136c7aeb0afSSerge Semin 	spin_unlock(&pp->lock);
137963de473SAllen Hubbe 
138c7aeb0afSSerge Semin 	return 0;
139963de473SAllen Hubbe }
140c7aeb0afSSerge Semin 
pp_setup(struct pp_ctx * pp)141c7aeb0afSSerge Semin static void pp_setup(struct pp_ctx *pp)
142c7aeb0afSSerge Semin {
143c7aeb0afSSerge Semin 	int ret;
144c7aeb0afSSerge Semin 
145c7aeb0afSSerge Semin 	ntb_db_set_mask(pp->ntb, pp->in_db);
146c7aeb0afSSerge Semin 
147c7aeb0afSSerge Semin 	hrtimer_cancel(&pp->timer);
148c7aeb0afSSerge Semin 
149c7aeb0afSSerge Semin 	ret = pp_find_next_peer(pp);
150c7aeb0afSSerge Semin 	if (ret == -ENODEV) {
151c7aeb0afSSerge Semin 		dev_dbg(&pp->ntb->dev, "Got no peers, so cancel\n");
152c7aeb0afSSerge Semin 		return;
153c7aeb0afSSerge Semin 	}
154c7aeb0afSSerge Semin 
155c7aeb0afSSerge Semin 	dev_dbg(&pp->ntb->dev, "Ping-pong started with port %d, db %#llx\n",
156c7aeb0afSSerge Semin 		ntb_peer_port_number(pp->ntb, pp->out_pidx), pp->out_db);
157c7aeb0afSSerge Semin 
158c7aeb0afSSerge Semin 	hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
159c7aeb0afSSerge Semin }
160c7aeb0afSSerge Semin 
pp_clear(struct pp_ctx * pp)161c7aeb0afSSerge Semin static void pp_clear(struct pp_ctx *pp)
162c7aeb0afSSerge Semin {
163c7aeb0afSSerge Semin 	hrtimer_cancel(&pp->timer);
164c7aeb0afSSerge Semin 
165c7aeb0afSSerge Semin 	ntb_db_set_mask(pp->ntb, pp->in_db);
166c7aeb0afSSerge Semin 
167c7aeb0afSSerge Semin 	dev_dbg(&pp->ntb->dev, "Ping-pong cancelled\n");
168c7aeb0afSSerge Semin }
169c7aeb0afSSerge Semin 
pp_ping(struct pp_ctx * pp)170c7aeb0afSSerge Semin static void pp_ping(struct pp_ctx *pp)
171c7aeb0afSSerge Semin {
172c7aeb0afSSerge Semin 	u32 count;
173c7aeb0afSSerge Semin 
174c7aeb0afSSerge Semin 	count = atomic_read(&pp->count);
175c7aeb0afSSerge Semin 
176c7aeb0afSSerge Semin 	spin_lock(&pp->lock);
177c7aeb0afSSerge Semin 	ntb_peer_spad_write(pp->ntb, pp->out_pidx, 0, count);
178c7aeb0afSSerge Semin 	ntb_peer_msg_write(pp->ntb, pp->out_pidx, 0, count);
179c7aeb0afSSerge Semin 
180c7aeb0afSSerge Semin 	dev_dbg(&pp->ntb->dev, "Ping port %d spad %#x, msg %#x\n",
181c7aeb0afSSerge Semin 		ntb_peer_port_number(pp->ntb, pp->out_pidx), count, count);
182c7aeb0afSSerge Semin 
183c7aeb0afSSerge Semin 	ntb_peer_db_set(pp->ntb, pp->out_db);
184c7aeb0afSSerge Semin 	ntb_db_clear_mask(pp->ntb, pp->in_db);
185c7aeb0afSSerge Semin 	spin_unlock(&pp->lock);
186c7aeb0afSSerge Semin }
187c7aeb0afSSerge Semin 
pp_pong(struct pp_ctx * pp)188c7aeb0afSSerge Semin static void pp_pong(struct pp_ctx *pp)
189c7aeb0afSSerge Semin {
190*e6315480SColin Ian King 	u32 msg_data, spad_data;
191c7aeb0afSSerge Semin 	int pidx = 0;
192c7aeb0afSSerge Semin 
193c7aeb0afSSerge Semin 	/* Read pong data */
194c7aeb0afSSerge Semin 	spad_data = ntb_spad_read(pp->ntb, 0);
195c7aeb0afSSerge Semin 	msg_data = ntb_msg_read(pp->ntb, &pidx, 0);
196c7aeb0afSSerge Semin 	ntb_msg_clear_sts(pp->ntb, -1);
197c7aeb0afSSerge Semin 
198c7aeb0afSSerge Semin 	/*
199c7aeb0afSSerge Semin 	 * Scratchpad and message data may differ, since message register can't
200c7aeb0afSSerge Semin 	 * be rewritten unless status is cleared. Additionally either of them
201c7aeb0afSSerge Semin 	 * might be unsupported
202c7aeb0afSSerge Semin 	 */
203c7aeb0afSSerge Semin 	dev_dbg(&pp->ntb->dev, "Pong spad %#x, msg %#x (port %d)\n",
204c7aeb0afSSerge Semin 		spad_data, msg_data, ntb_peer_port_number(pp->ntb, pidx));
205c7aeb0afSSerge Semin 
206c7aeb0afSSerge Semin 	atomic_inc(&pp->count);
207c7aeb0afSSerge Semin 
208c7aeb0afSSerge Semin 	ntb_db_set_mask(pp->ntb, pp->in_db);
209c7aeb0afSSerge Semin 	ntb_db_clear(pp->ntb, pp->in_db);
210c7aeb0afSSerge Semin 
211c7aeb0afSSerge Semin 	hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
212c7aeb0afSSerge Semin }
213c7aeb0afSSerge Semin 
pp_timer_func(struct hrtimer * t)214c7aeb0afSSerge Semin static enum hrtimer_restart pp_timer_func(struct hrtimer *t)
215c7aeb0afSSerge Semin {
216c7aeb0afSSerge Semin 	struct pp_ctx *pp = to_pp_timer(t);
217c7aeb0afSSerge Semin 
218c7aeb0afSSerge Semin 	pp_ping(pp);
219c7aeb0afSSerge Semin 
220c7aeb0afSSerge Semin 	return HRTIMER_NORESTART;
221963de473SAllen Hubbe }
222963de473SAllen Hubbe 
pp_link_event(void * ctx)223963de473SAllen Hubbe static void pp_link_event(void *ctx)
224963de473SAllen Hubbe {
225963de473SAllen Hubbe 	struct pp_ctx *pp = ctx;
226963de473SAllen Hubbe 
227c7aeb0afSSerge Semin 	pp_setup(pp);
228963de473SAllen Hubbe }
229963de473SAllen Hubbe 
pp_db_event(void * ctx,int vec)230963de473SAllen Hubbe static void pp_db_event(void *ctx, int vec)
231963de473SAllen Hubbe {
232963de473SAllen Hubbe 	struct pp_ctx *pp = ctx;
233963de473SAllen Hubbe 
234c7aeb0afSSerge Semin 	pp_pong(pp);
23520572ee1SLogan Gunthorpe }
23620572ee1SLogan Gunthorpe 
237963de473SAllen Hubbe static const struct ntb_ctx_ops pp_ops = {
238963de473SAllen Hubbe 	.link_event = pp_link_event,
239c7aeb0afSSerge Semin 	.db_event = pp_db_event
240963de473SAllen Hubbe };
241963de473SAllen Hubbe 
pp_check_ntb(struct ntb_dev * ntb)242c7aeb0afSSerge Semin static int pp_check_ntb(struct ntb_dev *ntb)
243963de473SAllen Hubbe {
244c7aeb0afSSerge Semin 	u64 pmask;
245963de473SAllen Hubbe 
246963de473SAllen Hubbe 	if (ntb_db_is_unsafe(ntb)) {
247c7aeb0afSSerge Semin 		dev_dbg(&ntb->dev, "Doorbell is unsafe\n");
248c7aeb0afSSerge Semin 		if (!unsafe)
249c7aeb0afSSerge Semin 			return -EINVAL;
250d67288a3SSerge Semin 	}
251d67288a3SSerge Semin 
252963de473SAllen Hubbe 	if (ntb_spad_is_unsafe(ntb)) {
253c7aeb0afSSerge Semin 		dev_dbg(&ntb->dev, "Scratchpad is unsafe\n");
254c7aeb0afSSerge Semin 		if (!unsafe)
255c7aeb0afSSerge Semin 			return -EINVAL;
256963de473SAllen Hubbe 	}
257963de473SAllen Hubbe 
258c7aeb0afSSerge Semin 	pmask = GENMASK_ULL(ntb_peer_port_count(ntb), 0);
259c7aeb0afSSerge Semin 	if ((ntb_db_valid_mask(ntb) & pmask) != pmask) {
260c7aeb0afSSerge Semin 		dev_err(&ntb->dev, "Unsupported DB configuration\n");
261c7aeb0afSSerge Semin 		return -EINVAL;
262963de473SAllen Hubbe 	}
263963de473SAllen Hubbe 
264c7aeb0afSSerge Semin 	if (ntb_spad_count(ntb) < 1 && ntb_msg_count(ntb) < 1) {
265c7aeb0afSSerge Semin 		dev_err(&ntb->dev, "Scratchpads and messages unsupported\n");
266c7aeb0afSSerge Semin 		return -EINVAL;
267c7aeb0afSSerge Semin 	} else if (ntb_spad_count(ntb) < 1) {
268c7aeb0afSSerge Semin 		dev_dbg(&ntb->dev, "Scratchpads unsupported\n");
269c7aeb0afSSerge Semin 	} else if (ntb_msg_count(ntb) < 1) {
270c7aeb0afSSerge Semin 		dev_dbg(&ntb->dev, "Messages unsupported\n");
271c7aeb0afSSerge Semin 	}
272963de473SAllen Hubbe 
273963de473SAllen Hubbe 	return 0;
274963de473SAllen Hubbe }
275963de473SAllen Hubbe 
pp_create_data(struct ntb_dev * ntb)276c7aeb0afSSerge Semin static struct pp_ctx *pp_create_data(struct ntb_dev *ntb)
277c7aeb0afSSerge Semin {
278c7aeb0afSSerge Semin 	struct pp_ctx *pp;
279c7aeb0afSSerge Semin 
280c7aeb0afSSerge Semin 	pp = devm_kzalloc(&ntb->dev, sizeof(*pp), GFP_KERNEL);
281c7aeb0afSSerge Semin 	if (!pp)
282c7aeb0afSSerge Semin 		return ERR_PTR(-ENOMEM);
283c7aeb0afSSerge Semin 
284c7aeb0afSSerge Semin 	pp->ntb = ntb;
285c7aeb0afSSerge Semin 	atomic_set(&pp->count, 0);
286c7aeb0afSSerge Semin 	spin_lock_init(&pp->lock);
287c7aeb0afSSerge Semin 	hrtimer_init(&pp->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
288c7aeb0afSSerge Semin 	pp->timer.function = pp_timer_func;
289c7aeb0afSSerge Semin 
290c7aeb0afSSerge Semin 	return pp;
291c7aeb0afSSerge Semin }
292c7aeb0afSSerge Semin 
pp_init_flds(struct pp_ctx * pp)293c7aeb0afSSerge Semin static void pp_init_flds(struct pp_ctx *pp)
294c7aeb0afSSerge Semin {
295c7aeb0afSSerge Semin 	int pidx, lport, pcnt;
296c7aeb0afSSerge Semin 
297c7aeb0afSSerge Semin 	/* Find global port index */
298c7aeb0afSSerge Semin 	lport = ntb_port_number(pp->ntb);
299c7aeb0afSSerge Semin 	pcnt = ntb_peer_port_count(pp->ntb);
300c7aeb0afSSerge Semin 	for (pidx = 0; pidx < pcnt; pidx++) {
301c7aeb0afSSerge Semin 		if (lport < ntb_peer_port_number(pp->ntb, pidx))
302c7aeb0afSSerge Semin 			break;
303c7aeb0afSSerge Semin 	}
304c7aeb0afSSerge Semin 
305ca93c457SLogan Gunthorpe 	pp->in_db = BIT_ULL(lport);
306c7aeb0afSSerge Semin 	pp->pmask = GENMASK_ULL(pidx, 0) >> 1;
307c7aeb0afSSerge Semin 	pp->nmask = GENMASK_ULL(pcnt - 1, pidx);
308c7aeb0afSSerge Semin 
309c7aeb0afSSerge Semin 	dev_dbg(&pp->ntb->dev, "Inbound db %#llx, prev %#llx, next %#llx\n",
310c7aeb0afSSerge Semin 		pp->in_db, pp->pmask, pp->nmask);
311c7aeb0afSSerge Semin }
312c7aeb0afSSerge Semin 
pp_mask_events(struct pp_ctx * pp)313c7aeb0afSSerge Semin static int pp_mask_events(struct pp_ctx *pp)
314c7aeb0afSSerge Semin {
315c7aeb0afSSerge Semin 	u64 db_mask, msg_mask;
316c7aeb0afSSerge Semin 	int ret;
317c7aeb0afSSerge Semin 
318c7aeb0afSSerge Semin 	db_mask = ntb_db_valid_mask(pp->ntb);
319c7aeb0afSSerge Semin 	ret = ntb_db_set_mask(pp->ntb, db_mask);
320c7aeb0afSSerge Semin 	if (ret)
321c7aeb0afSSerge Semin 		return ret;
322c7aeb0afSSerge Semin 
323c7aeb0afSSerge Semin 	/* Skip message events masking if unsupported */
324c7aeb0afSSerge Semin 	if (ntb_msg_count(pp->ntb) < 1)
325c7aeb0afSSerge Semin 		return 0;
326c7aeb0afSSerge Semin 
327c7aeb0afSSerge Semin 	msg_mask = ntb_msg_outbits(pp->ntb) | ntb_msg_inbits(pp->ntb);
328c7aeb0afSSerge Semin 	return ntb_msg_set_mask(pp->ntb, msg_mask);
329c7aeb0afSSerge Semin }
330c7aeb0afSSerge Semin 
pp_setup_ctx(struct pp_ctx * pp)331c7aeb0afSSerge Semin static int pp_setup_ctx(struct pp_ctx *pp)
332c7aeb0afSSerge Semin {
333c7aeb0afSSerge Semin 	int ret;
334c7aeb0afSSerge Semin 
335c7aeb0afSSerge Semin 	ret = ntb_set_ctx(pp->ntb, pp, &pp_ops);
336c7aeb0afSSerge Semin 	if (ret)
337c7aeb0afSSerge Semin 		return ret;
338c7aeb0afSSerge Semin 
339c7aeb0afSSerge Semin 	ntb_link_enable(pp->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
340c7aeb0afSSerge Semin 	/* Might be not necessary */
341c7aeb0afSSerge Semin 	ntb_link_event(pp->ntb);
342c7aeb0afSSerge Semin 
343c7aeb0afSSerge Semin 	return 0;
344c7aeb0afSSerge Semin }
345c7aeb0afSSerge Semin 
pp_clear_ctx(struct pp_ctx * pp)346c7aeb0afSSerge Semin static void pp_clear_ctx(struct pp_ctx *pp)
347c7aeb0afSSerge Semin {
348c7aeb0afSSerge Semin 	ntb_link_disable(pp->ntb);
349c7aeb0afSSerge Semin 
350c7aeb0afSSerge Semin 	ntb_clear_ctx(pp->ntb);
351c7aeb0afSSerge Semin }
352c7aeb0afSSerge Semin 
pp_setup_dbgfs(struct pp_ctx * pp)353c7aeb0afSSerge Semin static void pp_setup_dbgfs(struct pp_ctx *pp)
354c7aeb0afSSerge Semin {
355c7aeb0afSSerge Semin 	struct pci_dev *pdev = pp->ntb->pdev;
356c7aeb0afSSerge Semin 
357c7aeb0afSSerge Semin 	pp->dbgfs_dir = debugfs_create_dir(pci_name(pdev), pp_dbgfs_topdir);
358c7aeb0afSSerge Semin 
359be576734SGreg Kroah-Hartman 	debugfs_create_atomic_t("count", 0600, pp->dbgfs_dir, &pp->count);
360c7aeb0afSSerge Semin }
361c7aeb0afSSerge Semin 
pp_clear_dbgfs(struct pp_ctx * pp)362c7aeb0afSSerge Semin static void pp_clear_dbgfs(struct pp_ctx *pp)
363c7aeb0afSSerge Semin {
364c7aeb0afSSerge Semin 	debugfs_remove_recursive(pp->dbgfs_dir);
365c7aeb0afSSerge Semin }
366c7aeb0afSSerge Semin 
pp_probe(struct ntb_client * client,struct ntb_dev * ntb)367c7aeb0afSSerge Semin static int pp_probe(struct ntb_client *client, struct ntb_dev *ntb)
368c7aeb0afSSerge Semin {
369c7aeb0afSSerge Semin 	struct pp_ctx *pp;
370c7aeb0afSSerge Semin 	int ret;
371c7aeb0afSSerge Semin 
372c7aeb0afSSerge Semin 	ret = pp_check_ntb(ntb);
373c7aeb0afSSerge Semin 	if (ret)
374c7aeb0afSSerge Semin 		return ret;
375c7aeb0afSSerge Semin 
376c7aeb0afSSerge Semin 	pp = pp_create_data(ntb);
377c7aeb0afSSerge Semin 	if (IS_ERR(pp))
378c7aeb0afSSerge Semin 		return PTR_ERR(pp);
379c7aeb0afSSerge Semin 
380c7aeb0afSSerge Semin 	pp_init_flds(pp);
381c7aeb0afSSerge Semin 
382c7aeb0afSSerge Semin 	ret = pp_mask_events(pp);
383c7aeb0afSSerge Semin 	if (ret)
384c7aeb0afSSerge Semin 		return ret;
385c7aeb0afSSerge Semin 
386c7aeb0afSSerge Semin 	ret = pp_setup_ctx(pp);
387c7aeb0afSSerge Semin 	if (ret)
388c7aeb0afSSerge Semin 		return ret;
389c7aeb0afSSerge Semin 
390c7aeb0afSSerge Semin 	pp_setup_dbgfs(pp);
391c7aeb0afSSerge Semin 
392c7aeb0afSSerge Semin 	return 0;
393c7aeb0afSSerge Semin }
394c7aeb0afSSerge Semin 
pp_remove(struct ntb_client * client,struct ntb_dev * ntb)395c7aeb0afSSerge Semin static void pp_remove(struct ntb_client *client, struct ntb_dev *ntb)
396963de473SAllen Hubbe {
397963de473SAllen Hubbe 	struct pp_ctx *pp = ntb->ctx;
398963de473SAllen Hubbe 
399c7aeb0afSSerge Semin 	pp_clear_dbgfs(pp);
40020572ee1SLogan Gunthorpe 
401c7aeb0afSSerge Semin 	pp_clear_ctx(pp);
402963de473SAllen Hubbe 
403c7aeb0afSSerge Semin 	pp_clear(pp);
404963de473SAllen Hubbe }
405963de473SAllen Hubbe 
406963de473SAllen Hubbe static struct ntb_client pp_client = {
407963de473SAllen Hubbe 	.ops = {
408963de473SAllen Hubbe 		.probe = pp_probe,
409c7aeb0afSSerge Semin 		.remove = pp_remove
410c7aeb0afSSerge Semin 	}
411963de473SAllen Hubbe };
41220572ee1SLogan Gunthorpe 
pp_init(void)41320572ee1SLogan Gunthorpe static int __init pp_init(void)
41420572ee1SLogan Gunthorpe {
415c7aeb0afSSerge Semin 	int ret;
41620572ee1SLogan Gunthorpe 
41720572ee1SLogan Gunthorpe 	if (debugfs_initialized())
418c7aeb0afSSerge Semin 		pp_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
41920572ee1SLogan Gunthorpe 
420c7aeb0afSSerge Semin 	ret = ntb_register_client(&pp_client);
421c7aeb0afSSerge Semin 	if (ret)
422c7aeb0afSSerge Semin 		debugfs_remove_recursive(pp_dbgfs_topdir);
42320572ee1SLogan Gunthorpe 
424c7aeb0afSSerge Semin 	return ret;
42520572ee1SLogan Gunthorpe }
42620572ee1SLogan Gunthorpe module_init(pp_init);
42720572ee1SLogan Gunthorpe 
pp_exit(void)42820572ee1SLogan Gunthorpe static void __exit pp_exit(void)
42920572ee1SLogan Gunthorpe {
43020572ee1SLogan Gunthorpe 	ntb_unregister_client(&pp_client);
431c7aeb0afSSerge Semin 	debugfs_remove_recursive(pp_dbgfs_topdir);
43220572ee1SLogan Gunthorpe }
43320572ee1SLogan Gunthorpe module_exit(pp_exit);
434