xref: /OK3568_Linux_fs/kernel/drivers/ntb/test/ntb_pingpong.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   This file is provided under a dual BSD/GPLv2 license.  When using or
3*4882a593Smuzhiyun  *   redistributing this file, you may do so under either license.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *   GPL LICENSE SUMMARY
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8*4882a593Smuzhiyun  *   Copyright (C) 2017 T-Platforms. All Rights Reserved.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *   This program is free software; you can redistribute it and/or modify
11*4882a593Smuzhiyun  *   it under the terms of version 2 of the GNU General Public License as
12*4882a593Smuzhiyun  *   published by the Free Software Foundation.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *   This program is distributed in the hope that it will be useful, but
15*4882a593Smuzhiyun  *   WITHOUT ANY WARRANTY; without even the implied warranty of
16*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17*4882a593Smuzhiyun  *   General Public License for more details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *   BSD LICENSE
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22*4882a593Smuzhiyun  *   Copyright (C) 2017 T-Platforms. All Rights Reserved.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *   Redistribution and use in source and binary forms, with or without
25*4882a593Smuzhiyun  *   modification, are permitted provided that the following conditions
26*4882a593Smuzhiyun  *   are met:
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  *     * Redistributions of source code must retain the above copyright
29*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer.
30*4882a593Smuzhiyun  *     * Redistributions in binary form must reproduce the above copy
31*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer in
32*4882a593Smuzhiyun  *       the documentation and/or other materials provided with the
33*4882a593Smuzhiyun  *       distribution.
34*4882a593Smuzhiyun  *     * Neither the name of Intel Corporation nor the names of its
35*4882a593Smuzhiyun  *       contributors may be used to endorse or promote products derived
36*4882a593Smuzhiyun  *       from this software without specific prior written permission.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39*4882a593Smuzhiyun  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40*4882a593Smuzhiyun  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41*4882a593Smuzhiyun  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42*4882a593Smuzhiyun  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43*4882a593Smuzhiyun  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44*4882a593Smuzhiyun  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45*4882a593Smuzhiyun  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46*4882a593Smuzhiyun  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47*4882a593Smuzhiyun  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48*4882a593Smuzhiyun  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * PCIe NTB Pingpong Linux driver
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * How to use this tool, by example.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * Assuming $DBG_DIR is something like:
57*4882a593Smuzhiyun  * '/sys/kernel/debug/ntb_perf/0000:00:03.0'
58*4882a593Smuzhiyun  * Suppose aside from local device there is at least one remote device
59*4882a593Smuzhiyun  * connected to NTB with index 0.
60*4882a593Smuzhiyun  *-----------------------------------------------------------------------------
61*4882a593Smuzhiyun  * Eg: install driver with specified delay between doorbell event and response
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * root@self# insmod ntb_pingpong.ko delay_ms=1000
64*4882a593Smuzhiyun  *-----------------------------------------------------------------------------
65*4882a593Smuzhiyun  * Eg: get number of ping-pong cycles performed
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * root@self# cat $DBG_DIR/count
68*4882a593Smuzhiyun  */
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #include <linux/init.h>
71*4882a593Smuzhiyun #include <linux/kernel.h>
72*4882a593Smuzhiyun #include <linux/module.h>
73*4882a593Smuzhiyun #include <linux/device.h>
74*4882a593Smuzhiyun #include <linux/bitops.h>
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #include <linux/pci.h>
77*4882a593Smuzhiyun #include <linux/slab.h>
78*4882a593Smuzhiyun #include <linux/hrtimer.h>
79*4882a593Smuzhiyun #include <linux/debugfs.h>
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #include <linux/ntb.h>
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define DRIVER_NAME		"ntb_pingpong"
84*4882a593Smuzhiyun #define DRIVER_VERSION		"2.0"
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
87*4882a593Smuzhiyun MODULE_VERSION(DRIVER_VERSION);
88*4882a593Smuzhiyun MODULE_AUTHOR("Allen Hubbe <Allen.Hubbe@emc.com>");
89*4882a593Smuzhiyun MODULE_DESCRIPTION("PCIe NTB Simple Pingpong Client");
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun static unsigned int unsafe;
92*4882a593Smuzhiyun module_param(unsafe, uint, 0644);
93*4882a593Smuzhiyun MODULE_PARM_DESC(unsafe, "Run even though ntb operations may be unsafe");
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun static unsigned int delay_ms = 1000;
96*4882a593Smuzhiyun module_param(delay_ms, uint, 0644);
97*4882a593Smuzhiyun MODULE_PARM_DESC(delay_ms, "Milliseconds to delay the response to peer");
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun struct pp_ctx {
100*4882a593Smuzhiyun 	struct ntb_dev *ntb;
101*4882a593Smuzhiyun 	struct hrtimer timer;
102*4882a593Smuzhiyun 	u64 in_db;
103*4882a593Smuzhiyun 	u64 out_db;
104*4882a593Smuzhiyun 	int out_pidx;
105*4882a593Smuzhiyun 	u64 nmask;
106*4882a593Smuzhiyun 	u64 pmask;
107*4882a593Smuzhiyun 	atomic_t count;
108*4882a593Smuzhiyun 	spinlock_t lock;
109*4882a593Smuzhiyun 	struct dentry *dbgfs_dir;
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun #define to_pp_timer(__timer) \
112*4882a593Smuzhiyun 	container_of(__timer, struct pp_ctx, timer)
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun static struct dentry *pp_dbgfs_topdir;
115*4882a593Smuzhiyun 
pp_find_next_peer(struct pp_ctx * pp)116*4882a593Smuzhiyun static int pp_find_next_peer(struct pp_ctx *pp)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	u64 link, out_db;
119*4882a593Smuzhiyun 	int pidx;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	link = ntb_link_is_up(pp->ntb, NULL, NULL);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* Find next available peer */
124*4882a593Smuzhiyun 	if (link & pp->nmask)
125*4882a593Smuzhiyun 		pidx = __ffs64(link & pp->nmask);
126*4882a593Smuzhiyun 	else if (link & pp->pmask)
127*4882a593Smuzhiyun 		pidx = __ffs64(link & pp->pmask);
128*4882a593Smuzhiyun 	else
129*4882a593Smuzhiyun 		return -ENODEV;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	out_db = BIT_ULL(ntb_peer_port_number(pp->ntb, pidx));
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	spin_lock(&pp->lock);
134*4882a593Smuzhiyun 	pp->out_pidx = pidx;
135*4882a593Smuzhiyun 	pp->out_db = out_db;
136*4882a593Smuzhiyun 	spin_unlock(&pp->lock);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
pp_setup(struct pp_ctx * pp)141*4882a593Smuzhiyun static void pp_setup(struct pp_ctx *pp)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	int ret;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	ntb_db_set_mask(pp->ntb, pp->in_db);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	hrtimer_cancel(&pp->timer);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	ret = pp_find_next_peer(pp);
150*4882a593Smuzhiyun 	if (ret == -ENODEV) {
151*4882a593Smuzhiyun 		dev_dbg(&pp->ntb->dev, "Got no peers, so cancel\n");
152*4882a593Smuzhiyun 		return;
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	dev_dbg(&pp->ntb->dev, "Ping-pong started with port %d, db %#llx\n",
156*4882a593Smuzhiyun 		ntb_peer_port_number(pp->ntb, pp->out_pidx), pp->out_db);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
pp_clear(struct pp_ctx * pp)161*4882a593Smuzhiyun static void pp_clear(struct pp_ctx *pp)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	hrtimer_cancel(&pp->timer);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ntb_db_set_mask(pp->ntb, pp->in_db);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	dev_dbg(&pp->ntb->dev, "Ping-pong cancelled\n");
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
pp_ping(struct pp_ctx * pp)170*4882a593Smuzhiyun static void pp_ping(struct pp_ctx *pp)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	u32 count;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	count = atomic_read(&pp->count);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	spin_lock(&pp->lock);
177*4882a593Smuzhiyun 	ntb_peer_spad_write(pp->ntb, pp->out_pidx, 0, count);
178*4882a593Smuzhiyun 	ntb_peer_msg_write(pp->ntb, pp->out_pidx, 0, count);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	dev_dbg(&pp->ntb->dev, "Ping port %d spad %#x, msg %#x\n",
181*4882a593Smuzhiyun 		ntb_peer_port_number(pp->ntb, pp->out_pidx), count, count);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	ntb_peer_db_set(pp->ntb, pp->out_db);
184*4882a593Smuzhiyun 	ntb_db_clear_mask(pp->ntb, pp->in_db);
185*4882a593Smuzhiyun 	spin_unlock(&pp->lock);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
pp_pong(struct pp_ctx * pp)188*4882a593Smuzhiyun static void pp_pong(struct pp_ctx *pp)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	u32 msg_data = -1, spad_data = -1;
191*4882a593Smuzhiyun 	int pidx = 0;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/* Read pong data */
194*4882a593Smuzhiyun 	spad_data = ntb_spad_read(pp->ntb, 0);
195*4882a593Smuzhiyun 	msg_data = ntb_msg_read(pp->ntb, &pidx, 0);
196*4882a593Smuzhiyun 	ntb_msg_clear_sts(pp->ntb, -1);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/*
199*4882a593Smuzhiyun 	 * Scratchpad and message data may differ, since message register can't
200*4882a593Smuzhiyun 	 * be rewritten unless status is cleared. Additionally either of them
201*4882a593Smuzhiyun 	 * might be unsupported
202*4882a593Smuzhiyun 	 */
203*4882a593Smuzhiyun 	dev_dbg(&pp->ntb->dev, "Pong spad %#x, msg %#x (port %d)\n",
204*4882a593Smuzhiyun 		spad_data, msg_data, ntb_peer_port_number(pp->ntb, pidx));
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	atomic_inc(&pp->count);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	ntb_db_set_mask(pp->ntb, pp->in_db);
209*4882a593Smuzhiyun 	ntb_db_clear(pp->ntb, pp->in_db);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
pp_timer_func(struct hrtimer * t)214*4882a593Smuzhiyun static enum hrtimer_restart pp_timer_func(struct hrtimer *t)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	struct pp_ctx *pp = to_pp_timer(t);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	pp_ping(pp);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
pp_link_event(void * ctx)223*4882a593Smuzhiyun static void pp_link_event(void *ctx)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	struct pp_ctx *pp = ctx;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	pp_setup(pp);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
pp_db_event(void * ctx,int vec)230*4882a593Smuzhiyun static void pp_db_event(void *ctx, int vec)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct pp_ctx *pp = ctx;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	pp_pong(pp);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun static const struct ntb_ctx_ops pp_ops = {
238*4882a593Smuzhiyun 	.link_event = pp_link_event,
239*4882a593Smuzhiyun 	.db_event = pp_db_event
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
pp_check_ntb(struct ntb_dev * ntb)242*4882a593Smuzhiyun static int pp_check_ntb(struct ntb_dev *ntb)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	u64 pmask;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (ntb_db_is_unsafe(ntb)) {
247*4882a593Smuzhiyun 		dev_dbg(&ntb->dev, "Doorbell is unsafe\n");
248*4882a593Smuzhiyun 		if (!unsafe)
249*4882a593Smuzhiyun 			return -EINVAL;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	if (ntb_spad_is_unsafe(ntb)) {
253*4882a593Smuzhiyun 		dev_dbg(&ntb->dev, "Scratchpad is unsafe\n");
254*4882a593Smuzhiyun 		if (!unsafe)
255*4882a593Smuzhiyun 			return -EINVAL;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	pmask = GENMASK_ULL(ntb_peer_port_count(ntb), 0);
259*4882a593Smuzhiyun 	if ((ntb_db_valid_mask(ntb) & pmask) != pmask) {
260*4882a593Smuzhiyun 		dev_err(&ntb->dev, "Unsupported DB configuration\n");
261*4882a593Smuzhiyun 		return -EINVAL;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (ntb_spad_count(ntb) < 1 && ntb_msg_count(ntb) < 1) {
265*4882a593Smuzhiyun 		dev_err(&ntb->dev, "Scratchpads and messages unsupported\n");
266*4882a593Smuzhiyun 		return -EINVAL;
267*4882a593Smuzhiyun 	} else if (ntb_spad_count(ntb) < 1) {
268*4882a593Smuzhiyun 		dev_dbg(&ntb->dev, "Scratchpads unsupported\n");
269*4882a593Smuzhiyun 	} else if (ntb_msg_count(ntb) < 1) {
270*4882a593Smuzhiyun 		dev_dbg(&ntb->dev, "Messages unsupported\n");
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
pp_create_data(struct ntb_dev * ntb)276*4882a593Smuzhiyun static struct pp_ctx *pp_create_data(struct ntb_dev *ntb)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	struct pp_ctx *pp;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	pp = devm_kzalloc(&ntb->dev, sizeof(*pp), GFP_KERNEL);
281*4882a593Smuzhiyun 	if (!pp)
282*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	pp->ntb = ntb;
285*4882a593Smuzhiyun 	atomic_set(&pp->count, 0);
286*4882a593Smuzhiyun 	spin_lock_init(&pp->lock);
287*4882a593Smuzhiyun 	hrtimer_init(&pp->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
288*4882a593Smuzhiyun 	pp->timer.function = pp_timer_func;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	return pp;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
pp_init_flds(struct pp_ctx * pp)293*4882a593Smuzhiyun static void pp_init_flds(struct pp_ctx *pp)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	int pidx, lport, pcnt;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/* Find global port index */
298*4882a593Smuzhiyun 	lport = ntb_port_number(pp->ntb);
299*4882a593Smuzhiyun 	pcnt = ntb_peer_port_count(pp->ntb);
300*4882a593Smuzhiyun 	for (pidx = 0; pidx < pcnt; pidx++) {
301*4882a593Smuzhiyun 		if (lport < ntb_peer_port_number(pp->ntb, pidx))
302*4882a593Smuzhiyun 			break;
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	pp->in_db = BIT_ULL(lport);
306*4882a593Smuzhiyun 	pp->pmask = GENMASK_ULL(pidx, 0) >> 1;
307*4882a593Smuzhiyun 	pp->nmask = GENMASK_ULL(pcnt - 1, pidx);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	dev_dbg(&pp->ntb->dev, "Inbound db %#llx, prev %#llx, next %#llx\n",
310*4882a593Smuzhiyun 		pp->in_db, pp->pmask, pp->nmask);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
pp_mask_events(struct pp_ctx * pp)313*4882a593Smuzhiyun static int pp_mask_events(struct pp_ctx *pp)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	u64 db_mask, msg_mask;
316*4882a593Smuzhiyun 	int ret;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	db_mask = ntb_db_valid_mask(pp->ntb);
319*4882a593Smuzhiyun 	ret = ntb_db_set_mask(pp->ntb, db_mask);
320*4882a593Smuzhiyun 	if (ret)
321*4882a593Smuzhiyun 		return ret;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	/* Skip message events masking if unsupported */
324*4882a593Smuzhiyun 	if (ntb_msg_count(pp->ntb) < 1)
325*4882a593Smuzhiyun 		return 0;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	msg_mask = ntb_msg_outbits(pp->ntb) | ntb_msg_inbits(pp->ntb);
328*4882a593Smuzhiyun 	return ntb_msg_set_mask(pp->ntb, msg_mask);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
pp_setup_ctx(struct pp_ctx * pp)331*4882a593Smuzhiyun static int pp_setup_ctx(struct pp_ctx *pp)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	int ret;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	ret = ntb_set_ctx(pp->ntb, pp, &pp_ops);
336*4882a593Smuzhiyun 	if (ret)
337*4882a593Smuzhiyun 		return ret;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	ntb_link_enable(pp->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
340*4882a593Smuzhiyun 	/* Might be not necessary */
341*4882a593Smuzhiyun 	ntb_link_event(pp->ntb);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	return 0;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
pp_clear_ctx(struct pp_ctx * pp)346*4882a593Smuzhiyun static void pp_clear_ctx(struct pp_ctx *pp)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	ntb_link_disable(pp->ntb);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	ntb_clear_ctx(pp->ntb);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
pp_setup_dbgfs(struct pp_ctx * pp)353*4882a593Smuzhiyun static void pp_setup_dbgfs(struct pp_ctx *pp)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	struct pci_dev *pdev = pp->ntb->pdev;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	pp->dbgfs_dir = debugfs_create_dir(pci_name(pdev), pp_dbgfs_topdir);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	debugfs_create_atomic_t("count", 0600, pp->dbgfs_dir, &pp->count);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
pp_clear_dbgfs(struct pp_ctx * pp)362*4882a593Smuzhiyun static void pp_clear_dbgfs(struct pp_ctx *pp)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	debugfs_remove_recursive(pp->dbgfs_dir);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
pp_probe(struct ntb_client * client,struct ntb_dev * ntb)367*4882a593Smuzhiyun static int pp_probe(struct ntb_client *client, struct ntb_dev *ntb)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	struct pp_ctx *pp;
370*4882a593Smuzhiyun 	int ret;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	ret = pp_check_ntb(ntb);
373*4882a593Smuzhiyun 	if (ret)
374*4882a593Smuzhiyun 		return ret;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	pp = pp_create_data(ntb);
377*4882a593Smuzhiyun 	if (IS_ERR(pp))
378*4882a593Smuzhiyun 		return PTR_ERR(pp);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	pp_init_flds(pp);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	ret = pp_mask_events(pp);
383*4882a593Smuzhiyun 	if (ret)
384*4882a593Smuzhiyun 		return ret;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	ret = pp_setup_ctx(pp);
387*4882a593Smuzhiyun 	if (ret)
388*4882a593Smuzhiyun 		return ret;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	pp_setup_dbgfs(pp);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	return 0;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
pp_remove(struct ntb_client * client,struct ntb_dev * ntb)395*4882a593Smuzhiyun static void pp_remove(struct ntb_client *client, struct ntb_dev *ntb)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun 	struct pp_ctx *pp = ntb->ctx;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	pp_clear_dbgfs(pp);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	pp_clear_ctx(pp);
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	pp_clear(pp);
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun static struct ntb_client pp_client = {
407*4882a593Smuzhiyun 	.ops = {
408*4882a593Smuzhiyun 		.probe = pp_probe,
409*4882a593Smuzhiyun 		.remove = pp_remove
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun };
412*4882a593Smuzhiyun 
pp_init(void)413*4882a593Smuzhiyun static int __init pp_init(void)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	int ret;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (debugfs_initialized())
418*4882a593Smuzhiyun 		pp_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	ret = ntb_register_client(&pp_client);
421*4882a593Smuzhiyun 	if (ret)
422*4882a593Smuzhiyun 		debugfs_remove_recursive(pp_dbgfs_topdir);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	return ret;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun module_init(pp_init);
427*4882a593Smuzhiyun 
pp_exit(void)428*4882a593Smuzhiyun static void __exit pp_exit(void)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	ntb_unregister_client(&pp_client);
431*4882a593Smuzhiyun 	debugfs_remove_recursive(pp_dbgfs_topdir);
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun module_exit(pp_exit);
434