xref: /openbmc/linux/drivers/ntb/test/ntb_msi_test.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1a6bed7a5SLogan Gunthorpe // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2a6bed7a5SLogan Gunthorpe 
3a6bed7a5SLogan Gunthorpe #include <linux/module.h>
4a6bed7a5SLogan Gunthorpe #include <linux/debugfs.h>
5a6bed7a5SLogan Gunthorpe #include <linux/ntb.h>
6a6bed7a5SLogan Gunthorpe #include <linux/pci.h>
7a6bed7a5SLogan Gunthorpe #include <linux/radix-tree.h>
8a6bed7a5SLogan Gunthorpe #include <linux/workqueue.h>
9a6bed7a5SLogan Gunthorpe 
10a6bed7a5SLogan Gunthorpe MODULE_LICENSE("Dual BSD/GPL");
11a6bed7a5SLogan Gunthorpe MODULE_VERSION("0.1");
12a6bed7a5SLogan Gunthorpe MODULE_AUTHOR("Logan Gunthorpe <logang@deltatee.com>");
13a6bed7a5SLogan Gunthorpe MODULE_DESCRIPTION("Test for sending MSI interrupts over an NTB memory window");
14a6bed7a5SLogan Gunthorpe 
15a6bed7a5SLogan Gunthorpe static int num_irqs = 4;
16a6bed7a5SLogan Gunthorpe module_param(num_irqs, int, 0644);
17a6bed7a5SLogan Gunthorpe MODULE_PARM_DESC(num_irqs, "number of irqs to use");
18a6bed7a5SLogan Gunthorpe 
19a6bed7a5SLogan Gunthorpe struct ntb_msit_ctx {
20a6bed7a5SLogan Gunthorpe 	struct ntb_dev *ntb;
21a6bed7a5SLogan Gunthorpe 	struct dentry *dbgfs_dir;
22a6bed7a5SLogan Gunthorpe 	struct work_struct setup_work;
23a6bed7a5SLogan Gunthorpe 
24a6bed7a5SLogan Gunthorpe 	struct ntb_msit_isr_ctx {
25a6bed7a5SLogan Gunthorpe 		int irq_idx;
26a6bed7a5SLogan Gunthorpe 		int irq_num;
27a6bed7a5SLogan Gunthorpe 		int occurrences;
28a6bed7a5SLogan Gunthorpe 		struct ntb_msit_ctx *nm;
29a6bed7a5SLogan Gunthorpe 		struct ntb_msi_desc desc;
30a6bed7a5SLogan Gunthorpe 	} *isr_ctx;
31a6bed7a5SLogan Gunthorpe 
32a6bed7a5SLogan Gunthorpe 	struct ntb_msit_peer {
33a6bed7a5SLogan Gunthorpe 		struct ntb_msit_ctx *nm;
34a6bed7a5SLogan Gunthorpe 		int pidx;
35a6bed7a5SLogan Gunthorpe 		int num_irqs;
36a6bed7a5SLogan Gunthorpe 		struct completion init_comp;
37a6bed7a5SLogan Gunthorpe 		struct ntb_msi_desc *msi_desc;
38a6bed7a5SLogan Gunthorpe 	} peers[];
39a6bed7a5SLogan Gunthorpe };
40a6bed7a5SLogan Gunthorpe 
41a6bed7a5SLogan Gunthorpe static struct dentry *ntb_msit_dbgfs_topdir;
42a6bed7a5SLogan Gunthorpe 
ntb_msit_isr(int irq,void * dev)43a6bed7a5SLogan Gunthorpe static irqreturn_t ntb_msit_isr(int irq, void *dev)
44a6bed7a5SLogan Gunthorpe {
45a6bed7a5SLogan Gunthorpe 	struct ntb_msit_isr_ctx *isr_ctx = dev;
46a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = isr_ctx->nm;
47a6bed7a5SLogan Gunthorpe 
48a6bed7a5SLogan Gunthorpe 	dev_dbg(&nm->ntb->dev, "Interrupt Occurred: %d",
49a6bed7a5SLogan Gunthorpe 		isr_ctx->irq_idx);
50a6bed7a5SLogan Gunthorpe 
51a6bed7a5SLogan Gunthorpe 	isr_ctx->occurrences++;
52a6bed7a5SLogan Gunthorpe 
53a6bed7a5SLogan Gunthorpe 	return IRQ_HANDLED;
54a6bed7a5SLogan Gunthorpe }
55a6bed7a5SLogan Gunthorpe 
ntb_msit_setup_work(struct work_struct * work)56a6bed7a5SLogan Gunthorpe static void ntb_msit_setup_work(struct work_struct *work)
57a6bed7a5SLogan Gunthorpe {
58a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = container_of(work, struct ntb_msit_ctx,
59a6bed7a5SLogan Gunthorpe 					       setup_work);
60a6bed7a5SLogan Gunthorpe 	int irq_count = 0;
61a6bed7a5SLogan Gunthorpe 	int irq;
62a6bed7a5SLogan Gunthorpe 	int ret;
63a6bed7a5SLogan Gunthorpe 	uintptr_t i;
64a6bed7a5SLogan Gunthorpe 
65a6bed7a5SLogan Gunthorpe 	ret = ntb_msi_setup_mws(nm->ntb);
66a6bed7a5SLogan Gunthorpe 	if (ret) {
67a6bed7a5SLogan Gunthorpe 		dev_err(&nm->ntb->dev, "Unable to setup MSI windows: %d\n",
68a6bed7a5SLogan Gunthorpe 			ret);
69a6bed7a5SLogan Gunthorpe 		return;
70a6bed7a5SLogan Gunthorpe 	}
71a6bed7a5SLogan Gunthorpe 
72a6bed7a5SLogan Gunthorpe 	for (i = 0; i < num_irqs; i++) {
73a6bed7a5SLogan Gunthorpe 		nm->isr_ctx[i].irq_idx = i;
74a6bed7a5SLogan Gunthorpe 		nm->isr_ctx[i].nm = nm;
75a6bed7a5SLogan Gunthorpe 
76a6bed7a5SLogan Gunthorpe 		if (!nm->isr_ctx[i].irq_num) {
77a6bed7a5SLogan Gunthorpe 			irq = ntbm_msi_request_irq(nm->ntb, ntb_msit_isr,
78a6bed7a5SLogan Gunthorpe 						   KBUILD_MODNAME,
79a6bed7a5SLogan Gunthorpe 						   &nm->isr_ctx[i],
80a6bed7a5SLogan Gunthorpe 						   &nm->isr_ctx[i].desc);
81a6bed7a5SLogan Gunthorpe 			if (irq < 0)
82a6bed7a5SLogan Gunthorpe 				break;
83a6bed7a5SLogan Gunthorpe 
84a6bed7a5SLogan Gunthorpe 			nm->isr_ctx[i].irq_num = irq;
85a6bed7a5SLogan Gunthorpe 		}
86a6bed7a5SLogan Gunthorpe 
87a6bed7a5SLogan Gunthorpe 		ret = ntb_spad_write(nm->ntb, 2 * i + 1,
88a6bed7a5SLogan Gunthorpe 				     nm->isr_ctx[i].desc.addr_offset);
89a6bed7a5SLogan Gunthorpe 		if (ret)
90a6bed7a5SLogan Gunthorpe 			break;
91a6bed7a5SLogan Gunthorpe 
92a6bed7a5SLogan Gunthorpe 		ret = ntb_spad_write(nm->ntb, 2 * i + 2,
93a6bed7a5SLogan Gunthorpe 				     nm->isr_ctx[i].desc.data);
94a6bed7a5SLogan Gunthorpe 		if (ret)
95a6bed7a5SLogan Gunthorpe 			break;
96a6bed7a5SLogan Gunthorpe 
97a6bed7a5SLogan Gunthorpe 		irq_count++;
98a6bed7a5SLogan Gunthorpe 	}
99a6bed7a5SLogan Gunthorpe 
100a6bed7a5SLogan Gunthorpe 	ntb_spad_write(nm->ntb, 0, irq_count);
101a6bed7a5SLogan Gunthorpe 	ntb_peer_db_set(nm->ntb, BIT(ntb_port_number(nm->ntb)));
102a6bed7a5SLogan Gunthorpe }
103a6bed7a5SLogan Gunthorpe 
ntb_msit_desc_changed(void * ctx)104a6bed7a5SLogan Gunthorpe static void ntb_msit_desc_changed(void *ctx)
105a6bed7a5SLogan Gunthorpe {
106a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = ctx;
107a6bed7a5SLogan Gunthorpe 	int i;
108a6bed7a5SLogan Gunthorpe 
109a6bed7a5SLogan Gunthorpe 	dev_dbg(&nm->ntb->dev, "MSI Descriptors Changed\n");
110a6bed7a5SLogan Gunthorpe 
111a6bed7a5SLogan Gunthorpe 	for (i = 0; i < num_irqs; i++) {
112a6bed7a5SLogan Gunthorpe 		ntb_spad_write(nm->ntb, 2 * i + 1,
113a6bed7a5SLogan Gunthorpe 			       nm->isr_ctx[i].desc.addr_offset);
114a6bed7a5SLogan Gunthorpe 		ntb_spad_write(nm->ntb, 2 * i + 2,
115a6bed7a5SLogan Gunthorpe 			       nm->isr_ctx[i].desc.data);
116a6bed7a5SLogan Gunthorpe 	}
117a6bed7a5SLogan Gunthorpe 
118a6bed7a5SLogan Gunthorpe 	ntb_peer_db_set(nm->ntb, BIT(ntb_port_number(nm->ntb)));
119a6bed7a5SLogan Gunthorpe }
120a6bed7a5SLogan Gunthorpe 
ntb_msit_link_event(void * ctx)121a6bed7a5SLogan Gunthorpe static void ntb_msit_link_event(void *ctx)
122a6bed7a5SLogan Gunthorpe {
123a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = ctx;
124a6bed7a5SLogan Gunthorpe 
125a6bed7a5SLogan Gunthorpe 	if (!ntb_link_is_up(nm->ntb, NULL, NULL))
126a6bed7a5SLogan Gunthorpe 		return;
127a6bed7a5SLogan Gunthorpe 
128a6bed7a5SLogan Gunthorpe 	schedule_work(&nm->setup_work);
129a6bed7a5SLogan Gunthorpe }
130a6bed7a5SLogan Gunthorpe 
ntb_msit_copy_peer_desc(struct ntb_msit_ctx * nm,int peer)131a6bed7a5SLogan Gunthorpe static void ntb_msit_copy_peer_desc(struct ntb_msit_ctx *nm, int peer)
132a6bed7a5SLogan Gunthorpe {
133a6bed7a5SLogan Gunthorpe 	int i;
134a6bed7a5SLogan Gunthorpe 	struct ntb_msi_desc *desc = nm->peers[peer].msi_desc;
135a6bed7a5SLogan Gunthorpe 	int irq_count = nm->peers[peer].num_irqs;
136a6bed7a5SLogan Gunthorpe 
137a6bed7a5SLogan Gunthorpe 	for (i = 0; i < irq_count; i++) {
138a6bed7a5SLogan Gunthorpe 		desc[i].addr_offset = ntb_peer_spad_read(nm->ntb, peer,
139a6bed7a5SLogan Gunthorpe 							 2 * i + 1);
140a6bed7a5SLogan Gunthorpe 		desc[i].data = ntb_peer_spad_read(nm->ntb, peer, 2 * i + 2);
141a6bed7a5SLogan Gunthorpe 	}
142a6bed7a5SLogan Gunthorpe 
143a6bed7a5SLogan Gunthorpe 	dev_info(&nm->ntb->dev, "Found %d interrupts on peer %d\n",
144a6bed7a5SLogan Gunthorpe 		 irq_count, peer);
145a6bed7a5SLogan Gunthorpe 
146a6bed7a5SLogan Gunthorpe 	complete_all(&nm->peers[peer].init_comp);
147a6bed7a5SLogan Gunthorpe }
148a6bed7a5SLogan Gunthorpe 
ntb_msit_db_event(void * ctx,int vec)149a6bed7a5SLogan Gunthorpe static void ntb_msit_db_event(void *ctx, int vec)
150a6bed7a5SLogan Gunthorpe {
151a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = ctx;
152a6bed7a5SLogan Gunthorpe 	struct ntb_msi_desc *desc;
153a6bed7a5SLogan Gunthorpe 	u64 peer_mask = ntb_db_read(nm->ntb);
154a6bed7a5SLogan Gunthorpe 	u32 irq_count;
155a6bed7a5SLogan Gunthorpe 	int peer;
156a6bed7a5SLogan Gunthorpe 
157a6bed7a5SLogan Gunthorpe 	ntb_db_clear(nm->ntb, peer_mask);
158a6bed7a5SLogan Gunthorpe 
159a6bed7a5SLogan Gunthorpe 	for (peer = 0; peer < sizeof(peer_mask) * 8; peer++) {
160a6bed7a5SLogan Gunthorpe 		if (!(peer_mask & BIT(peer)))
161a6bed7a5SLogan Gunthorpe 			continue;
162a6bed7a5SLogan Gunthorpe 
163a6bed7a5SLogan Gunthorpe 		irq_count = ntb_peer_spad_read(nm->ntb, peer, 0);
164a6bed7a5SLogan Gunthorpe 		if (irq_count == -1)
165a6bed7a5SLogan Gunthorpe 			continue;
166a6bed7a5SLogan Gunthorpe 
167a6bed7a5SLogan Gunthorpe 		desc = kcalloc(irq_count, sizeof(*desc), GFP_ATOMIC);
168a6bed7a5SLogan Gunthorpe 		if (!desc)
169a6bed7a5SLogan Gunthorpe 			continue;
170a6bed7a5SLogan Gunthorpe 
171a6bed7a5SLogan Gunthorpe 		kfree(nm->peers[peer].msi_desc);
172a6bed7a5SLogan Gunthorpe 		nm->peers[peer].msi_desc = desc;
173a6bed7a5SLogan Gunthorpe 		nm->peers[peer].num_irqs = irq_count;
174a6bed7a5SLogan Gunthorpe 
175a6bed7a5SLogan Gunthorpe 		ntb_msit_copy_peer_desc(nm, peer);
176a6bed7a5SLogan Gunthorpe 	}
177a6bed7a5SLogan Gunthorpe }
178a6bed7a5SLogan Gunthorpe 
179a6bed7a5SLogan Gunthorpe static const struct ntb_ctx_ops ntb_msit_ops = {
180a6bed7a5SLogan Gunthorpe 	.link_event = ntb_msit_link_event,
181a6bed7a5SLogan Gunthorpe 	.db_event = ntb_msit_db_event,
182a6bed7a5SLogan Gunthorpe };
183a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_trigger(void * data,u64 idx)184a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_trigger(void *data, u64 idx)
185a6bed7a5SLogan Gunthorpe {
186a6bed7a5SLogan Gunthorpe 	struct ntb_msit_peer *peer = data;
187a6bed7a5SLogan Gunthorpe 
188a6bed7a5SLogan Gunthorpe 	if (idx >= peer->num_irqs)
189a6bed7a5SLogan Gunthorpe 		return -EINVAL;
190a6bed7a5SLogan Gunthorpe 
191a6bed7a5SLogan Gunthorpe 	dev_dbg(&peer->nm->ntb->dev, "trigger irq %llu on peer %u\n",
192a6bed7a5SLogan Gunthorpe 		idx, peer->pidx);
193a6bed7a5SLogan Gunthorpe 
194a6bed7a5SLogan Gunthorpe 	return ntb_msi_peer_trigger(peer->nm->ntb, peer->pidx,
195a6bed7a5SLogan Gunthorpe 				    &peer->msi_desc[idx]);
196a6bed7a5SLogan Gunthorpe }
197a6bed7a5SLogan Gunthorpe 
198a6bed7a5SLogan Gunthorpe DEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_trigger_fops, NULL,
199a6bed7a5SLogan Gunthorpe 			 ntb_msit_dbgfs_trigger, "%llu\n");
200a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_port_get(void * data,u64 * port)201a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_port_get(void *data, u64 *port)
202a6bed7a5SLogan Gunthorpe {
203a6bed7a5SLogan Gunthorpe 	struct ntb_msit_peer *peer = data;
204a6bed7a5SLogan Gunthorpe 
205a6bed7a5SLogan Gunthorpe 	*port = ntb_peer_port_number(peer->nm->ntb, peer->pidx);
206a6bed7a5SLogan Gunthorpe 
207a6bed7a5SLogan Gunthorpe 	return 0;
208a6bed7a5SLogan Gunthorpe }
209a6bed7a5SLogan Gunthorpe 
210a6bed7a5SLogan Gunthorpe DEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_port_fops, ntb_msit_dbgfs_port_get,
211a6bed7a5SLogan Gunthorpe 			 NULL, "%llu\n");
212a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_count_get(void * data,u64 * count)213a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_count_get(void *data, u64 *count)
214a6bed7a5SLogan Gunthorpe {
215a6bed7a5SLogan Gunthorpe 	struct ntb_msit_peer *peer = data;
216a6bed7a5SLogan Gunthorpe 
217a6bed7a5SLogan Gunthorpe 	*count = peer->num_irqs;
218a6bed7a5SLogan Gunthorpe 
219a6bed7a5SLogan Gunthorpe 	return 0;
220a6bed7a5SLogan Gunthorpe }
221a6bed7a5SLogan Gunthorpe 
222a6bed7a5SLogan Gunthorpe DEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_count_fops, ntb_msit_dbgfs_count_get,
223a6bed7a5SLogan Gunthorpe 			 NULL, "%llu\n");
224a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_ready_get(void * data,u64 * ready)225a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_ready_get(void *data, u64 *ready)
226a6bed7a5SLogan Gunthorpe {
227a6bed7a5SLogan Gunthorpe 	struct ntb_msit_peer *peer = data;
228a6bed7a5SLogan Gunthorpe 
229a6bed7a5SLogan Gunthorpe 	*ready = try_wait_for_completion(&peer->init_comp);
230a6bed7a5SLogan Gunthorpe 
231a6bed7a5SLogan Gunthorpe 	return 0;
232a6bed7a5SLogan Gunthorpe }
233a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_ready_set(void * data,u64 ready)234a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_ready_set(void *data, u64 ready)
235a6bed7a5SLogan Gunthorpe {
236a6bed7a5SLogan Gunthorpe 	struct ntb_msit_peer *peer = data;
237a6bed7a5SLogan Gunthorpe 
238a6bed7a5SLogan Gunthorpe 	return wait_for_completion_interruptible(&peer->init_comp);
239a6bed7a5SLogan Gunthorpe }
240a6bed7a5SLogan Gunthorpe 
241a6bed7a5SLogan Gunthorpe DEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_ready_fops, ntb_msit_dbgfs_ready_get,
242a6bed7a5SLogan Gunthorpe 			 ntb_msit_dbgfs_ready_set, "%llu\n");
243a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_occurrences_get(void * data,u64 * occurrences)244a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_occurrences_get(void *data, u64 *occurrences)
245a6bed7a5SLogan Gunthorpe {
246a6bed7a5SLogan Gunthorpe 	struct ntb_msit_isr_ctx *isr_ctx = data;
247a6bed7a5SLogan Gunthorpe 
248a6bed7a5SLogan Gunthorpe 	*occurrences = isr_ctx->occurrences;
249a6bed7a5SLogan Gunthorpe 
250a6bed7a5SLogan Gunthorpe 	return 0;
251a6bed7a5SLogan Gunthorpe }
252a6bed7a5SLogan Gunthorpe 
253a6bed7a5SLogan Gunthorpe DEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_occurrences_fops,
254a6bed7a5SLogan Gunthorpe 			 ntb_msit_dbgfs_occurrences_get,
255a6bed7a5SLogan Gunthorpe 			 NULL, "%llu\n");
256a6bed7a5SLogan Gunthorpe 
ntb_msit_dbgfs_local_port_get(void * data,u64 * port)257a6bed7a5SLogan Gunthorpe static int ntb_msit_dbgfs_local_port_get(void *data, u64 *port)
258a6bed7a5SLogan Gunthorpe {
259a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = data;
260a6bed7a5SLogan Gunthorpe 
261a6bed7a5SLogan Gunthorpe 	*port = ntb_port_number(nm->ntb);
262a6bed7a5SLogan Gunthorpe 
263a6bed7a5SLogan Gunthorpe 	return 0;
264a6bed7a5SLogan Gunthorpe }
265a6bed7a5SLogan Gunthorpe 
266a6bed7a5SLogan Gunthorpe DEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_local_port_fops,
267a6bed7a5SLogan Gunthorpe 			 ntb_msit_dbgfs_local_port_get,
268a6bed7a5SLogan Gunthorpe 			 NULL, "%llu\n");
269a6bed7a5SLogan Gunthorpe 
ntb_msit_create_dbgfs(struct ntb_msit_ctx * nm)270a6bed7a5SLogan Gunthorpe static void ntb_msit_create_dbgfs(struct ntb_msit_ctx *nm)
271a6bed7a5SLogan Gunthorpe {
272a6bed7a5SLogan Gunthorpe 	struct pci_dev *pdev = nm->ntb->pdev;
273a6bed7a5SLogan Gunthorpe 	char buf[32];
274a6bed7a5SLogan Gunthorpe 	int i;
275a6bed7a5SLogan Gunthorpe 	struct dentry *peer_dir;
276a6bed7a5SLogan Gunthorpe 
277a6bed7a5SLogan Gunthorpe 	nm->dbgfs_dir = debugfs_create_dir(pci_name(pdev),
278a6bed7a5SLogan Gunthorpe 					   ntb_msit_dbgfs_topdir);
279a6bed7a5SLogan Gunthorpe 	debugfs_create_file("port", 0400, nm->dbgfs_dir, nm,
280a6bed7a5SLogan Gunthorpe 			    &ntb_msit_local_port_fops);
281a6bed7a5SLogan Gunthorpe 
282a6bed7a5SLogan Gunthorpe 	for (i = 0; i < ntb_peer_port_count(nm->ntb); i++) {
283a6bed7a5SLogan Gunthorpe 		nm->peers[i].pidx = i;
284a6bed7a5SLogan Gunthorpe 		nm->peers[i].nm = nm;
285a6bed7a5SLogan Gunthorpe 		init_completion(&nm->peers[i].init_comp);
286a6bed7a5SLogan Gunthorpe 
287a6bed7a5SLogan Gunthorpe 		snprintf(buf, sizeof(buf), "peer%d", i);
288a6bed7a5SLogan Gunthorpe 		peer_dir = debugfs_create_dir(buf, nm->dbgfs_dir);
289a6bed7a5SLogan Gunthorpe 
290a6bed7a5SLogan Gunthorpe 		debugfs_create_file_unsafe("trigger", 0200, peer_dir,
291a6bed7a5SLogan Gunthorpe 					   &nm->peers[i],
292a6bed7a5SLogan Gunthorpe 					   &ntb_msit_trigger_fops);
293a6bed7a5SLogan Gunthorpe 
294a6bed7a5SLogan Gunthorpe 		debugfs_create_file_unsafe("port", 0400, peer_dir,
295a6bed7a5SLogan Gunthorpe 					   &nm->peers[i], &ntb_msit_port_fops);
296a6bed7a5SLogan Gunthorpe 
297a6bed7a5SLogan Gunthorpe 		debugfs_create_file_unsafe("count", 0400, peer_dir,
298a6bed7a5SLogan Gunthorpe 					   &nm->peers[i],
299a6bed7a5SLogan Gunthorpe 					   &ntb_msit_count_fops);
300a6bed7a5SLogan Gunthorpe 
301a6bed7a5SLogan Gunthorpe 		debugfs_create_file_unsafe("ready", 0600, peer_dir,
302a6bed7a5SLogan Gunthorpe 					   &nm->peers[i],
303a6bed7a5SLogan Gunthorpe 					   &ntb_msit_ready_fops);
304a6bed7a5SLogan Gunthorpe 	}
305a6bed7a5SLogan Gunthorpe 
306a6bed7a5SLogan Gunthorpe 	for (i = 0; i < num_irqs; i++) {
307a6bed7a5SLogan Gunthorpe 		snprintf(buf, sizeof(buf), "irq%d_occurrences", i);
308a6bed7a5SLogan Gunthorpe 		debugfs_create_file_unsafe(buf, 0400, nm->dbgfs_dir,
309a6bed7a5SLogan Gunthorpe 					   &nm->isr_ctx[i],
310a6bed7a5SLogan Gunthorpe 					   &ntb_msit_occurrences_fops);
311a6bed7a5SLogan Gunthorpe 	}
312a6bed7a5SLogan Gunthorpe }
313a6bed7a5SLogan Gunthorpe 
ntb_msit_remove_dbgfs(struct ntb_msit_ctx * nm)314a6bed7a5SLogan Gunthorpe static void ntb_msit_remove_dbgfs(struct ntb_msit_ctx *nm)
315a6bed7a5SLogan Gunthorpe {
316a6bed7a5SLogan Gunthorpe 	debugfs_remove_recursive(nm->dbgfs_dir);
317a6bed7a5SLogan Gunthorpe }
318a6bed7a5SLogan Gunthorpe 
ntb_msit_probe(struct ntb_client * client,struct ntb_dev * ntb)319a6bed7a5SLogan Gunthorpe static int ntb_msit_probe(struct ntb_client *client, struct ntb_dev *ntb)
320a6bed7a5SLogan Gunthorpe {
321a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm;
322a6bed7a5SLogan Gunthorpe 	int peers;
323a6bed7a5SLogan Gunthorpe 	int ret;
324a6bed7a5SLogan Gunthorpe 
325a6bed7a5SLogan Gunthorpe 	peers = ntb_peer_port_count(ntb);
326a6bed7a5SLogan Gunthorpe 	if (peers <= 0)
327a6bed7a5SLogan Gunthorpe 		return -EINVAL;
328a6bed7a5SLogan Gunthorpe 
329a6bed7a5SLogan Gunthorpe 	if (ntb_spad_is_unsafe(ntb) || ntb_spad_count(ntb) < 2 * num_irqs + 1) {
330a6bed7a5SLogan Gunthorpe 		dev_err(&ntb->dev, "NTB MSI test requires at least %d spads for %d irqs\n",
331a6bed7a5SLogan Gunthorpe 			2 * num_irqs + 1, num_irqs);
332a6bed7a5SLogan Gunthorpe 		return -EFAULT;
333a6bed7a5SLogan Gunthorpe 	}
334a6bed7a5SLogan Gunthorpe 
335a6bed7a5SLogan Gunthorpe 	ret = ntb_spad_write(ntb, 0, -1);
336a6bed7a5SLogan Gunthorpe 	if (ret) {
337a6bed7a5SLogan Gunthorpe 		dev_err(&ntb->dev, "Unable to write spads: %d\n", ret);
338a6bed7a5SLogan Gunthorpe 		return ret;
339a6bed7a5SLogan Gunthorpe 	}
340a6bed7a5SLogan Gunthorpe 
341a6bed7a5SLogan Gunthorpe 	ret = ntb_db_clear_mask(ntb, GENMASK(peers - 1, 0));
342a6bed7a5SLogan Gunthorpe 	if (ret) {
343a6bed7a5SLogan Gunthorpe 		dev_err(&ntb->dev, "Unable to clear doorbell mask: %d\n", ret);
344a6bed7a5SLogan Gunthorpe 		return ret;
345a6bed7a5SLogan Gunthorpe 	}
346a6bed7a5SLogan Gunthorpe 
347a6bed7a5SLogan Gunthorpe 	ret = ntb_msi_init(ntb, ntb_msit_desc_changed);
348a6bed7a5SLogan Gunthorpe 	if (ret) {
349a6bed7a5SLogan Gunthorpe 		dev_err(&ntb->dev, "Unable to initialize MSI library: %d\n",
350a6bed7a5SLogan Gunthorpe 			ret);
351a6bed7a5SLogan Gunthorpe 		return ret;
352a6bed7a5SLogan Gunthorpe 	}
353a6bed7a5SLogan Gunthorpe 
354b8e2c8bbSGustavo A. R. Silva 	nm = devm_kzalloc(&ntb->dev, struct_size(nm, peers, peers), GFP_KERNEL);
355a6bed7a5SLogan Gunthorpe 	if (!nm)
356a6bed7a5SLogan Gunthorpe 		return -ENOMEM;
357a6bed7a5SLogan Gunthorpe 
358a6bed7a5SLogan Gunthorpe 	nm->isr_ctx = devm_kcalloc(&ntb->dev, num_irqs, sizeof(*nm->isr_ctx),
359a6bed7a5SLogan Gunthorpe 				   GFP_KERNEL);
360a6bed7a5SLogan Gunthorpe 	if (!nm->isr_ctx)
361a6bed7a5SLogan Gunthorpe 		return -ENOMEM;
362a6bed7a5SLogan Gunthorpe 
363a6bed7a5SLogan Gunthorpe 	INIT_WORK(&nm->setup_work, ntb_msit_setup_work);
364a6bed7a5SLogan Gunthorpe 	nm->ntb = ntb;
365a6bed7a5SLogan Gunthorpe 
366a6bed7a5SLogan Gunthorpe 	ntb_msit_create_dbgfs(nm);
367a6bed7a5SLogan Gunthorpe 
368a6bed7a5SLogan Gunthorpe 	ret = ntb_set_ctx(ntb, nm, &ntb_msit_ops);
369a6bed7a5SLogan Gunthorpe 	if (ret)
370a6bed7a5SLogan Gunthorpe 		goto remove_dbgfs;
371a6bed7a5SLogan Gunthorpe 
372*319f83acSYang Li 	if (!nm->isr_ctx) {
373*319f83acSYang Li 		ret = -ENOMEM;
374a6bed7a5SLogan Gunthorpe 		goto remove_dbgfs;
375*319f83acSYang Li 	}
376a6bed7a5SLogan Gunthorpe 
377a6bed7a5SLogan Gunthorpe 	ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
378a6bed7a5SLogan Gunthorpe 
379a6bed7a5SLogan Gunthorpe 	return 0;
380a6bed7a5SLogan Gunthorpe 
381a6bed7a5SLogan Gunthorpe remove_dbgfs:
382a6bed7a5SLogan Gunthorpe 	ntb_msit_remove_dbgfs(nm);
383a6bed7a5SLogan Gunthorpe 	devm_kfree(&ntb->dev, nm->isr_ctx);
384a6bed7a5SLogan Gunthorpe 	devm_kfree(&ntb->dev, nm);
385a6bed7a5SLogan Gunthorpe 	return ret;
386a6bed7a5SLogan Gunthorpe }
387a6bed7a5SLogan Gunthorpe 
ntb_msit_remove(struct ntb_client * client,struct ntb_dev * ntb)388a6bed7a5SLogan Gunthorpe static void ntb_msit_remove(struct ntb_client *client, struct ntb_dev *ntb)
389a6bed7a5SLogan Gunthorpe {
390a6bed7a5SLogan Gunthorpe 	struct ntb_msit_ctx *nm = ntb->ctx;
391a6bed7a5SLogan Gunthorpe 	int i;
392a6bed7a5SLogan Gunthorpe 
393a6bed7a5SLogan Gunthorpe 	ntb_link_disable(ntb);
394a6bed7a5SLogan Gunthorpe 	ntb_db_set_mask(ntb, ntb_db_valid_mask(ntb));
395a6bed7a5SLogan Gunthorpe 	ntb_msi_clear_mws(ntb);
396a6bed7a5SLogan Gunthorpe 
397a6bed7a5SLogan Gunthorpe 	for (i = 0; i < ntb_peer_port_count(ntb); i++)
398a6bed7a5SLogan Gunthorpe 		kfree(nm->peers[i].msi_desc);
399a6bed7a5SLogan Gunthorpe 
400a6bed7a5SLogan Gunthorpe 	ntb_clear_ctx(ntb);
401a6bed7a5SLogan Gunthorpe 	ntb_msit_remove_dbgfs(nm);
402a6bed7a5SLogan Gunthorpe }
403a6bed7a5SLogan Gunthorpe 
404a6bed7a5SLogan Gunthorpe static struct ntb_client ntb_msit_client = {
405a6bed7a5SLogan Gunthorpe 	.ops = {
406a6bed7a5SLogan Gunthorpe 		.probe = ntb_msit_probe,
407a6bed7a5SLogan Gunthorpe 		.remove = ntb_msit_remove
408a6bed7a5SLogan Gunthorpe 	}
409a6bed7a5SLogan Gunthorpe };
410a6bed7a5SLogan Gunthorpe 
ntb_msit_init(void)411a6bed7a5SLogan Gunthorpe static int __init ntb_msit_init(void)
412a6bed7a5SLogan Gunthorpe {
413a6bed7a5SLogan Gunthorpe 	int ret;
414a6bed7a5SLogan Gunthorpe 
415a6bed7a5SLogan Gunthorpe 	if (debugfs_initialized())
416a6bed7a5SLogan Gunthorpe 		ntb_msit_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME,
417a6bed7a5SLogan Gunthorpe 							   NULL);
418a6bed7a5SLogan Gunthorpe 
419a6bed7a5SLogan Gunthorpe 	ret = ntb_register_client(&ntb_msit_client);
420a6bed7a5SLogan Gunthorpe 	if (ret)
421a6bed7a5SLogan Gunthorpe 		debugfs_remove_recursive(ntb_msit_dbgfs_topdir);
422a6bed7a5SLogan Gunthorpe 
423a6bed7a5SLogan Gunthorpe 	return ret;
424a6bed7a5SLogan Gunthorpe }
425a6bed7a5SLogan Gunthorpe module_init(ntb_msit_init);
426a6bed7a5SLogan Gunthorpe 
ntb_msit_exit(void)427a6bed7a5SLogan Gunthorpe static void __exit ntb_msit_exit(void)
428a6bed7a5SLogan Gunthorpe {
429a6bed7a5SLogan Gunthorpe 	ntb_unregister_client(&ntb_msit_client);
430a6bed7a5SLogan Gunthorpe 	debugfs_remove_recursive(ntb_msit_dbgfs_topdir);
431a6bed7a5SLogan Gunthorpe }
432a6bed7a5SLogan Gunthorpe module_exit(ntb_msit_exit);
433