xref: /freebsd/sys/dev/mlx5/mlx5_fpga_tools/mlx5fpga_tools_main.c (revision c93b6e5fa24ba172ab271432c6692f9cc604e15a)
1 /*-
2  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  * $FreeBSD$
33  */
34 
35 #include <linux/module.h>
36 #include <dev/mlx5/mlx5_fpga_tools/tools.h>
37 #include <dev/mlx5/mlx5_fpga_tools/tools_char.h>
38 
39 #if (__FreeBSD_version >= 1100000)
40 MODULE_DEPEND(mlx5fpga_tools, linuxkpi, 1, 1, 1);
41 #endif
42 MODULE_DEPEND(mlx5fpga_tools, mlx5, 1, 1, 1);
43 MODULE_DEPEND(mlx5fpga_tools, mlx5fpga, 1, 1, 1);
44 MODULE_VERSION(mlx5fpga_tools, 1);
45 
46 static void mlx5_fpga_tools_create(struct mlx5_fpga_device *fdev);
47 static int mlx5_fpga_tools_add(struct mlx5_fpga_device *fdev, u32 vid, u16 pid);
48 static void mlx5_fpga_tools_remove(struct mlx5_fpga_device *fdev);
49 static void mlx5_fpga_tools_destroy(struct mlx5_fpga_device *fdev);
50 
51 struct mlx5_fpga_tools_dev *mlx5_fpga_tools_alloc(struct mlx5_fpga_device *fdev);
52 void mlx5_fpga_tools_free(struct mlx5_fpga_tools_dev *tdev);
53 
54 static struct mlx5_fpga_client mlx5_fpga_tools_client = {
55 	.name = MLX5_FPGA_TOOLS_DRIVER_NAME,
56 	.create = mlx5_fpga_tools_create,
57 	.add = mlx5_fpga_tools_add,
58 	.remove = mlx5_fpga_tools_remove,
59 	.destroy = mlx5_fpga_tools_destroy,
60 };
61 
62 struct mlx5_fpga_tools_dev *mlx5_fpga_tools_alloc(struct mlx5_fpga_device *fdev)
63 {
64 	int ret;
65 	struct mlx5_fpga_tools_dev *tdev;
66 
67 	tdev = kzalloc(sizeof(*tdev), GFP_KERNEL);
68 	if (!tdev)
69 		goto out;
70 
71 	tdev->fdev = fdev;
72 	sx_init(&tdev->lock, "mlx5fpgat");
73 	ret = mlx5_fpga_tools_char_add_one(tdev);
74 	if (ret)
75 		goto err_free;
76 
77 	goto out;
78 
79 err_free:
80 	kfree(tdev);
81 	tdev = NULL;
82 
83 out:
84 	return tdev;
85 }
86 
87 void mlx5_fpga_tools_free(struct mlx5_fpga_tools_dev *tdev)
88 {
89 	mlx5_fpga_tools_char_remove_one(tdev);
90 	kfree(tdev);
91 }
92 
93 static void mlx5_fpga_tools_create(struct mlx5_fpga_device *fdev)
94 {
95 	struct mlx5_fpga_tools_dev *dev = NULL;
96 
97 	dev_dbg(mlx5_fpga_dev(fdev), "tools_create\n");
98 
99 	dev = mlx5_fpga_tools_alloc(fdev);
100 	if (!dev)
101 		return;
102 
103 	mlx5_fpga_client_data_set(fdev, &mlx5_fpga_tools_client, dev);
104 }
105 
106 static int mlx5_fpga_tools_add(struct mlx5_fpga_device *fdev, u32 vid, u16 pid)
107 {
108 	return 0;
109 }
110 
111 static void mlx5_fpga_tools_remove(struct mlx5_fpga_device *fdev)
112 {
113 }
114 
115 static void mlx5_fpga_tools_destroy(struct mlx5_fpga_device *fdev)
116 {
117 	struct mlx5_fpga_tools_dev *dev;
118 
119 	dev_dbg(mlx5_fpga_dev(fdev), "tools_destroy\n");
120 
121 	dev = mlx5_fpga_client_data_get(fdev, &mlx5_fpga_tools_client);
122 	if (dev)
123 		mlx5_fpga_tools_free(dev);
124 }
125 
126 static int __init mlx5_fpga_tools_init(void)
127 {
128 	int ret = mlx5_fpga_tools_char_init();
129 
130 	if (ret)
131 		return ret;
132 	mlx5_fpga_client_register(&mlx5_fpga_tools_client);
133 	return 0;
134 }
135 
136 static void __exit mlx5_fpga_tools_exit(void)
137 {
138 	mlx5_fpga_client_unregister(&mlx5_fpga_tools_client);
139 	mlx5_fpga_tools_char_deinit();
140 }
141 
142 module_init(mlx5_fpga_tools_init);
143 module_exit(mlx5_fpga_tools_exit);
144