18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <linux/module.h>
48c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
58c2ecf20Sopenharmony_ci#include <linux/ntb.h>
68c2ecf20Sopenharmony_ci#include <linux/pci.h>
78c2ecf20Sopenharmony_ci#include <linux/radix-tree.h>
88c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
118c2ecf20Sopenharmony_ciMODULE_VERSION("0.1");
128c2ecf20Sopenharmony_ciMODULE_AUTHOR("Logan Gunthorpe <logang@deltatee.com>");
138c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Test for sending MSI interrupts over an NTB memory window");
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic int num_irqs = 4;
168c2ecf20Sopenharmony_cimodule_param(num_irqs, int, 0644);
178c2ecf20Sopenharmony_ciMODULE_PARM_DESC(num_irqs, "number of irqs to use");
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct ntb_msit_ctx {
208c2ecf20Sopenharmony_ci	struct ntb_dev *ntb;
218c2ecf20Sopenharmony_ci	struct dentry *dbgfs_dir;
228c2ecf20Sopenharmony_ci	struct work_struct setup_work;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	struct ntb_msit_isr_ctx {
258c2ecf20Sopenharmony_ci		int irq_idx;
268c2ecf20Sopenharmony_ci		int irq_num;
278c2ecf20Sopenharmony_ci		int occurrences;
288c2ecf20Sopenharmony_ci		struct ntb_msit_ctx *nm;
298c2ecf20Sopenharmony_ci		struct ntb_msi_desc desc;
308c2ecf20Sopenharmony_ci	} *isr_ctx;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	struct ntb_msit_peer {
338c2ecf20Sopenharmony_ci		struct ntb_msit_ctx *nm;
348c2ecf20Sopenharmony_ci		int pidx;
358c2ecf20Sopenharmony_ci		int num_irqs;
368c2ecf20Sopenharmony_ci		struct completion init_comp;
378c2ecf20Sopenharmony_ci		struct ntb_msi_desc *msi_desc;
388c2ecf20Sopenharmony_ci	} peers[];
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic struct dentry *ntb_msit_dbgfs_topdir;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic irqreturn_t ntb_msit_isr(int irq, void *dev)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct ntb_msit_isr_ctx *isr_ctx = dev;
468c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = isr_ctx->nm;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	dev_dbg(&nm->ntb->dev, "Interrupt Occurred: %d",
498c2ecf20Sopenharmony_ci		isr_ctx->irq_idx);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	isr_ctx->occurrences++;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void ntb_msit_setup_work(struct work_struct *work)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = container_of(work, struct ntb_msit_ctx,
598c2ecf20Sopenharmony_ci					       setup_work);
608c2ecf20Sopenharmony_ci	int irq_count = 0;
618c2ecf20Sopenharmony_ci	int irq;
628c2ecf20Sopenharmony_ci	int ret;
638c2ecf20Sopenharmony_ci	uintptr_t i;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = ntb_msi_setup_mws(nm->ntb);
668c2ecf20Sopenharmony_ci	if (ret) {
678c2ecf20Sopenharmony_ci		dev_err(&nm->ntb->dev, "Unable to setup MSI windows: %d\n",
688c2ecf20Sopenharmony_ci			ret);
698c2ecf20Sopenharmony_ci		return;
708c2ecf20Sopenharmony_ci	}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	for (i = 0; i < num_irqs; i++) {
738c2ecf20Sopenharmony_ci		nm->isr_ctx[i].irq_idx = i;
748c2ecf20Sopenharmony_ci		nm->isr_ctx[i].nm = nm;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		if (!nm->isr_ctx[i].irq_num) {
778c2ecf20Sopenharmony_ci			irq = ntbm_msi_request_irq(nm->ntb, ntb_msit_isr,
788c2ecf20Sopenharmony_ci						   KBUILD_MODNAME,
798c2ecf20Sopenharmony_ci						   &nm->isr_ctx[i],
808c2ecf20Sopenharmony_ci						   &nm->isr_ctx[i].desc);
818c2ecf20Sopenharmony_ci			if (irq < 0)
828c2ecf20Sopenharmony_ci				break;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci			nm->isr_ctx[i].irq_num = irq;
858c2ecf20Sopenharmony_ci		}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci		ret = ntb_spad_write(nm->ntb, 2 * i + 1,
888c2ecf20Sopenharmony_ci				     nm->isr_ctx[i].desc.addr_offset);
898c2ecf20Sopenharmony_ci		if (ret)
908c2ecf20Sopenharmony_ci			break;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		ret = ntb_spad_write(nm->ntb, 2 * i + 2,
938c2ecf20Sopenharmony_ci				     nm->isr_ctx[i].desc.data);
948c2ecf20Sopenharmony_ci		if (ret)
958c2ecf20Sopenharmony_ci			break;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		irq_count++;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	ntb_spad_write(nm->ntb, 0, irq_count);
1018c2ecf20Sopenharmony_ci	ntb_peer_db_set(nm->ntb, BIT(ntb_port_number(nm->ntb)));
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic void ntb_msit_desc_changed(void *ctx)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = ctx;
1078c2ecf20Sopenharmony_ci	int i;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	dev_dbg(&nm->ntb->dev, "MSI Descriptors Changed\n");
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	for (i = 0; i < num_irqs; i++) {
1128c2ecf20Sopenharmony_ci		ntb_spad_write(nm->ntb, 2 * i + 1,
1138c2ecf20Sopenharmony_ci			       nm->isr_ctx[i].desc.addr_offset);
1148c2ecf20Sopenharmony_ci		ntb_spad_write(nm->ntb, 2 * i + 2,
1158c2ecf20Sopenharmony_ci			       nm->isr_ctx[i].desc.data);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	ntb_peer_db_set(nm->ntb, BIT(ntb_port_number(nm->ntb)));
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void ntb_msit_link_event(void *ctx)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = ctx;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (!ntb_link_is_up(nm->ntb, NULL, NULL))
1268c2ecf20Sopenharmony_ci		return;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	schedule_work(&nm->setup_work);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void ntb_msit_copy_peer_desc(struct ntb_msit_ctx *nm, int peer)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	int i;
1348c2ecf20Sopenharmony_ci	struct ntb_msi_desc *desc = nm->peers[peer].msi_desc;
1358c2ecf20Sopenharmony_ci	int irq_count = nm->peers[peer].num_irqs;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	for (i = 0; i < irq_count; i++) {
1388c2ecf20Sopenharmony_ci		desc[i].addr_offset = ntb_peer_spad_read(nm->ntb, peer,
1398c2ecf20Sopenharmony_ci							 2 * i + 1);
1408c2ecf20Sopenharmony_ci		desc[i].data = ntb_peer_spad_read(nm->ntb, peer, 2 * i + 2);
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	dev_info(&nm->ntb->dev, "Found %d interrupts on peer %d\n",
1448c2ecf20Sopenharmony_ci		 irq_count, peer);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	complete_all(&nm->peers[peer].init_comp);
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic void ntb_msit_db_event(void *ctx, int vec)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = ctx;
1528c2ecf20Sopenharmony_ci	struct ntb_msi_desc *desc;
1538c2ecf20Sopenharmony_ci	u64 peer_mask = ntb_db_read(nm->ntb);
1548c2ecf20Sopenharmony_ci	u32 irq_count;
1558c2ecf20Sopenharmony_ci	int peer;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	ntb_db_clear(nm->ntb, peer_mask);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	for (peer = 0; peer < sizeof(peer_mask) * 8; peer++) {
1608c2ecf20Sopenharmony_ci		if (!(peer_mask & BIT(peer)))
1618c2ecf20Sopenharmony_ci			continue;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		irq_count = ntb_peer_spad_read(nm->ntb, peer, 0);
1648c2ecf20Sopenharmony_ci		if (irq_count == -1)
1658c2ecf20Sopenharmony_ci			continue;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		desc = kcalloc(irq_count, sizeof(*desc), GFP_ATOMIC);
1688c2ecf20Sopenharmony_ci		if (!desc)
1698c2ecf20Sopenharmony_ci			continue;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		kfree(nm->peers[peer].msi_desc);
1728c2ecf20Sopenharmony_ci		nm->peers[peer].msi_desc = desc;
1738c2ecf20Sopenharmony_ci		nm->peers[peer].num_irqs = irq_count;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		ntb_msit_copy_peer_desc(nm, peer);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic const struct ntb_ctx_ops ntb_msit_ops = {
1808c2ecf20Sopenharmony_ci	.link_event = ntb_msit_link_event,
1818c2ecf20Sopenharmony_ci	.db_event = ntb_msit_db_event,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_trigger(void *data, u64 idx)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct ntb_msit_peer *peer = data;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (idx >= peer->num_irqs)
1898c2ecf20Sopenharmony_ci		return -EINVAL;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	dev_dbg(&peer->nm->ntb->dev, "trigger irq %llu on peer %u\n",
1928c2ecf20Sopenharmony_ci		idx, peer->pidx);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return ntb_msi_peer_trigger(peer->nm->ntb, peer->pidx,
1958c2ecf20Sopenharmony_ci				    &peer->msi_desc[idx]);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_trigger_fops, NULL,
1998c2ecf20Sopenharmony_ci			 ntb_msit_dbgfs_trigger, "%llu\n");
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_port_get(void *data, u64 *port)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct ntb_msit_peer *peer = data;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	*port = ntb_peer_port_number(peer->nm->ntb, peer->pidx);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return 0;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_port_fops, ntb_msit_dbgfs_port_get,
2118c2ecf20Sopenharmony_ci			 NULL, "%llu\n");
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_count_get(void *data, u64 *count)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct ntb_msit_peer *peer = data;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	*count = peer->num_irqs;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	return 0;
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_count_fops, ntb_msit_dbgfs_count_get,
2238c2ecf20Sopenharmony_ci			 NULL, "%llu\n");
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_ready_get(void *data, u64 *ready)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct ntb_msit_peer *peer = data;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	*ready = try_wait_for_completion(&peer->init_comp);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return 0;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_ready_set(void *data, u64 ready)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct ntb_msit_peer *peer = data;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return wait_for_completion_interruptible(&peer->init_comp);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_ready_fops, ntb_msit_dbgfs_ready_get,
2428c2ecf20Sopenharmony_ci			 ntb_msit_dbgfs_ready_set, "%llu\n");
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_occurrences_get(void *data, u64 *occurrences)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct ntb_msit_isr_ctx *isr_ctx = data;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	*occurrences = isr_ctx->occurrences;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	return 0;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_occurrences_fops,
2548c2ecf20Sopenharmony_ci			 ntb_msit_dbgfs_occurrences_get,
2558c2ecf20Sopenharmony_ci			 NULL, "%llu\n");
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int ntb_msit_dbgfs_local_port_get(void *data, u64 *port)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = data;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	*port = ntb_port_number(nm->ntb);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return 0;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ciDEFINE_DEBUGFS_ATTRIBUTE(ntb_msit_local_port_fops,
2678c2ecf20Sopenharmony_ci			 ntb_msit_dbgfs_local_port_get,
2688c2ecf20Sopenharmony_ci			 NULL, "%llu\n");
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic void ntb_msit_create_dbgfs(struct ntb_msit_ctx *nm)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	struct pci_dev *pdev = nm->ntb->pdev;
2738c2ecf20Sopenharmony_ci	char buf[32];
2748c2ecf20Sopenharmony_ci	int i;
2758c2ecf20Sopenharmony_ci	struct dentry *peer_dir;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	nm->dbgfs_dir = debugfs_create_dir(pci_name(pdev),
2788c2ecf20Sopenharmony_ci					   ntb_msit_dbgfs_topdir);
2798c2ecf20Sopenharmony_ci	debugfs_create_file("port", 0400, nm->dbgfs_dir, nm,
2808c2ecf20Sopenharmony_ci			    &ntb_msit_local_port_fops);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	for (i = 0; i < ntb_peer_port_count(nm->ntb); i++) {
2838c2ecf20Sopenharmony_ci		nm->peers[i].pidx = i;
2848c2ecf20Sopenharmony_ci		nm->peers[i].nm = nm;
2858c2ecf20Sopenharmony_ci		init_completion(&nm->peers[i].init_comp);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci		snprintf(buf, sizeof(buf), "peer%d", i);
2888c2ecf20Sopenharmony_ci		peer_dir = debugfs_create_dir(buf, nm->dbgfs_dir);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe("trigger", 0200, peer_dir,
2918c2ecf20Sopenharmony_ci					   &nm->peers[i],
2928c2ecf20Sopenharmony_ci					   &ntb_msit_trigger_fops);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe("port", 0400, peer_dir,
2958c2ecf20Sopenharmony_ci					   &nm->peers[i], &ntb_msit_port_fops);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe("count", 0400, peer_dir,
2988c2ecf20Sopenharmony_ci					   &nm->peers[i],
2998c2ecf20Sopenharmony_ci					   &ntb_msit_count_fops);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe("ready", 0600, peer_dir,
3028c2ecf20Sopenharmony_ci					   &nm->peers[i],
3038c2ecf20Sopenharmony_ci					   &ntb_msit_ready_fops);
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	for (i = 0; i < num_irqs; i++) {
3078c2ecf20Sopenharmony_ci		snprintf(buf, sizeof(buf), "irq%d_occurrences", i);
3088c2ecf20Sopenharmony_ci		debugfs_create_file_unsafe(buf, 0400, nm->dbgfs_dir,
3098c2ecf20Sopenharmony_ci					   &nm->isr_ctx[i],
3108c2ecf20Sopenharmony_ci					   &ntb_msit_occurrences_fops);
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic void ntb_msit_remove_dbgfs(struct ntb_msit_ctx *nm)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	debugfs_remove_recursive(nm->dbgfs_dir);
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int ntb_msit_probe(struct ntb_client *client, struct ntb_dev *ntb)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm;
3228c2ecf20Sopenharmony_ci	int peers;
3238c2ecf20Sopenharmony_ci	int ret;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	peers = ntb_peer_port_count(ntb);
3268c2ecf20Sopenharmony_ci	if (peers <= 0)
3278c2ecf20Sopenharmony_ci		return -EINVAL;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (ntb_spad_is_unsafe(ntb) || ntb_spad_count(ntb) < 2 * num_irqs + 1) {
3308c2ecf20Sopenharmony_ci		dev_err(&ntb->dev, "NTB MSI test requires at least %d spads for %d irqs\n",
3318c2ecf20Sopenharmony_ci			2 * num_irqs + 1, num_irqs);
3328c2ecf20Sopenharmony_ci		return -EFAULT;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	ret = ntb_spad_write(ntb, 0, -1);
3368c2ecf20Sopenharmony_ci	if (ret) {
3378c2ecf20Sopenharmony_ci		dev_err(&ntb->dev, "Unable to write spads: %d\n", ret);
3388c2ecf20Sopenharmony_ci		return ret;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	ret = ntb_db_clear_mask(ntb, GENMASK(peers - 1, 0));
3428c2ecf20Sopenharmony_ci	if (ret) {
3438c2ecf20Sopenharmony_ci		dev_err(&ntb->dev, "Unable to clear doorbell mask: %d\n", ret);
3448c2ecf20Sopenharmony_ci		return ret;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	ret = ntb_msi_init(ntb, ntb_msit_desc_changed);
3488c2ecf20Sopenharmony_ci	if (ret) {
3498c2ecf20Sopenharmony_ci		dev_err(&ntb->dev, "Unable to initialize MSI library: %d\n",
3508c2ecf20Sopenharmony_ci			ret);
3518c2ecf20Sopenharmony_ci		return ret;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	nm = devm_kzalloc(&ntb->dev, struct_size(nm, peers, peers), GFP_KERNEL);
3558c2ecf20Sopenharmony_ci	if (!nm)
3568c2ecf20Sopenharmony_ci		return -ENOMEM;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	nm->isr_ctx = devm_kcalloc(&ntb->dev, num_irqs, sizeof(*nm->isr_ctx),
3598c2ecf20Sopenharmony_ci				   GFP_KERNEL);
3608c2ecf20Sopenharmony_ci	if (!nm->isr_ctx)
3618c2ecf20Sopenharmony_ci		return -ENOMEM;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	INIT_WORK(&nm->setup_work, ntb_msit_setup_work);
3648c2ecf20Sopenharmony_ci	nm->ntb = ntb;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	ntb_msit_create_dbgfs(nm);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	ret = ntb_set_ctx(ntb, nm, &ntb_msit_ops);
3698c2ecf20Sopenharmony_ci	if (ret)
3708c2ecf20Sopenharmony_ci		goto remove_dbgfs;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	if (!nm->isr_ctx) {
3738c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3748c2ecf20Sopenharmony_ci		goto remove_dbgfs;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return 0;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ciremove_dbgfs:
3828c2ecf20Sopenharmony_ci	ntb_msit_remove_dbgfs(nm);
3838c2ecf20Sopenharmony_ci	devm_kfree(&ntb->dev, nm->isr_ctx);
3848c2ecf20Sopenharmony_ci	devm_kfree(&ntb->dev, nm);
3858c2ecf20Sopenharmony_ci	return ret;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic void ntb_msit_remove(struct ntb_client *client, struct ntb_dev *ntb)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	struct ntb_msit_ctx *nm = ntb->ctx;
3918c2ecf20Sopenharmony_ci	int i;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	ntb_link_disable(ntb);
3948c2ecf20Sopenharmony_ci	ntb_db_set_mask(ntb, ntb_db_valid_mask(ntb));
3958c2ecf20Sopenharmony_ci	ntb_msi_clear_mws(ntb);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	for (i = 0; i < ntb_peer_port_count(ntb); i++)
3988c2ecf20Sopenharmony_ci		kfree(nm->peers[i].msi_desc);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	ntb_clear_ctx(ntb);
4018c2ecf20Sopenharmony_ci	ntb_msit_remove_dbgfs(nm);
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic struct ntb_client ntb_msit_client = {
4058c2ecf20Sopenharmony_ci	.ops = {
4068c2ecf20Sopenharmony_ci		.probe = ntb_msit_probe,
4078c2ecf20Sopenharmony_ci		.remove = ntb_msit_remove
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic int __init ntb_msit_init(void)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	int ret;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	if (debugfs_initialized())
4168c2ecf20Sopenharmony_ci		ntb_msit_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME,
4178c2ecf20Sopenharmony_ci							   NULL);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	ret = ntb_register_client(&ntb_msit_client);
4208c2ecf20Sopenharmony_ci	if (ret)
4218c2ecf20Sopenharmony_ci		debugfs_remove_recursive(ntb_msit_dbgfs_topdir);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	return ret;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_cimodule_init(ntb_msit_init);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void __exit ntb_msit_exit(void)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	ntb_unregister_client(&ntb_msit_client);
4308c2ecf20Sopenharmony_ci	debugfs_remove_recursive(ntb_msit_dbgfs_topdir);
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_cimodule_exit(ntb_msit_exit);
433