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