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 MODULE_DEPEND(mlx5fpga_tools, linuxkpi, 1, 1, 1);
40 MODULE_DEPEND(mlx5fpga_tools, mlx5, 1, 1, 1);
41 MODULE_DEPEND(mlx5fpga_tools, mlx5fpga, 1, 1, 1);
42 MODULE_VERSION(mlx5fpga_tools, 1);
43 
44 static void mlx5_fpga_tools_create(struct mlx5_fpga_device *fdev);
45 static int mlx5_fpga_tools_add(struct mlx5_fpga_device *fdev, u32 vid, u16 pid);
46 static void mlx5_fpga_tools_remove(struct mlx5_fpga_device *fdev);
47 static void mlx5_fpga_tools_destroy(struct mlx5_fpga_device *fdev);
48 
49 struct mlx5_fpga_tools_dev *mlx5_fpga_tools_alloc(struct mlx5_fpga_device *fdev);
50 void mlx5_fpga_tools_free(struct mlx5_fpga_tools_dev *tdev);
51 
52 static struct mlx5_fpga_client mlx5_fpga_tools_client = {
53 	.name = MLX5_FPGA_TOOLS_DRIVER_NAME,
54 	.create = mlx5_fpga_tools_create,
55 	.add = mlx5_fpga_tools_add,
56 	.remove = mlx5_fpga_tools_remove,
57 	.destroy = mlx5_fpga_tools_destroy,
58 };
59 
60 struct mlx5_fpga_tools_dev *mlx5_fpga_tools_alloc(struct mlx5_fpga_device *fdev)
61 {
62 	int ret;
63 	struct mlx5_fpga_tools_dev *tdev;
64 
65 	tdev = kzalloc(sizeof(*tdev), GFP_KERNEL);
66 	if (!tdev)
67 		goto out;
68 
69 	tdev->fdev = fdev;
70 	sx_init(&tdev->lock, "mlx5fpgat");
71 	ret = mlx5_fpga_tools_char_add_one(tdev);
72 	if (ret)
73 		goto err_free;
74 
75 	goto out;
76 
77 err_free:
78 	kfree(tdev);
79 	tdev = NULL;
80 
81 out:
82 	return tdev;
83 }
84 
85 void mlx5_fpga_tools_free(struct mlx5_fpga_tools_dev *tdev)
86 {
87 	mlx5_fpga_tools_char_remove_one(tdev);
88 	kfree(tdev);
89 }
90 
91 static void mlx5_fpga_tools_create(struct mlx5_fpga_device *fdev)
92 {
93 	struct mlx5_fpga_tools_dev *dev = NULL;
94 
95 	dev_dbg(mlx5_fpga_dev(fdev), "tools_create\n");
96 
97 	dev = mlx5_fpga_tools_alloc(fdev);
98 	if (!dev)
99 		return;
100 
101 	mlx5_fpga_client_data_set(fdev, &mlx5_fpga_tools_client, dev);
102 }
103 
104 static int mlx5_fpga_tools_add(struct mlx5_fpga_device *fdev, u32 vid, u16 pid)
105 {
106 	return 0;
107 }
108 
109 static void mlx5_fpga_tools_remove(struct mlx5_fpga_device *fdev)
110 {
111 }
112 
113 static void mlx5_fpga_tools_destroy(struct mlx5_fpga_device *fdev)
114 {
115 	struct mlx5_fpga_tools_dev *dev;
116 
117 	dev_dbg(mlx5_fpga_dev(fdev), "tools_destroy\n");
118 
119 	dev = mlx5_fpga_client_data_get(fdev, &mlx5_fpga_tools_client);
120 	if (dev)
121 		mlx5_fpga_tools_free(dev);
122 }
123 
124 static int __init mlx5_fpga_tools_init(void)
125 {
126 	int ret = mlx5_fpga_tools_char_init();
127 
128 	if (ret)
129 		return ret;
130 	mlx5_fpga_client_register(&mlx5_fpga_tools_client);
131 	return 0;
132 }
133 
134 static void __exit mlx5_fpga_tools_exit(void)
135 {
136 	mlx5_fpga_client_unregister(&mlx5_fpga_tools_client);
137 	mlx5_fpga_tools_char_deinit();
138 }
139 
140 module_init_order(mlx5_fpga_tools_init, SI_ORDER_SECOND);
141 module_exit_order(mlx5_fpga_tools_exit, SI_ORDER_SECOND);
142