]>
Commit | Line | Data |
---|---|---|
a1bd3bae AH |
1 | /* |
2 | * This file is provided under a dual BSD/GPLv2 license. When using or | |
3 | * redistributing this file, you may do so under either license. | |
4 | * | |
5 | * GPL LICENSE SUMMARY | |
6 | * | |
7 | * Copyright (C) 2015 EMC Corporation. All Rights Reserved. | |
443b9a14 | 8 | * Copyright (C) 2016 T-Platforms. All Rights Reserved. |
a1bd3bae AH |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of version 2 of the GNU General Public License as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * BSD LICENSE | |
20 | * | |
21 | * Copyright (C) 2015 EMC Corporation. All Rights Reserved. | |
443b9a14 | 22 | * Copyright (C) 2016 T-Platforms. All Rights Reserved. |
a1bd3bae AH |
23 | * |
24 | * Redistribution and use in source and binary forms, with or without | |
25 | * modification, are permitted provided that the following conditions | |
26 | * are met: | |
27 | * | |
28 | * * Redistributions of source code must retain the above copyright | |
29 | * notice, this list of conditions and the following disclaimer. | |
30 | * * Redistributions in binary form must reproduce the above copy | |
31 | * notice, this list of conditions and the following disclaimer in | |
32 | * the documentation and/or other materials provided with the | |
33 | * distribution. | |
34 | * * Neither the name of Intel Corporation nor the names of its | |
35 | * contributors may be used to endorse or promote products derived | |
36 | * from this software without specific prior written permission. | |
37 | * | |
38 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
39 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
40 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
41 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
42 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
44 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
45 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
46 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
47 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
48 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
49 | * | |
50 | * PCIe NTB Linux driver | |
51 | * | |
52 | * Contact Information: | |
53 | * Allen Hubbe <Allen.Hubbe@emc.com> | |
54 | */ | |
55 | ||
56 | #include <linux/device.h> | |
57 | #include <linux/kernel.h> | |
58 | #include <linux/module.h> | |
59 | ||
60 | #include <linux/ntb.h> | |
61 | #include <linux/pci.h> | |
62 | ||
63 | #define DRIVER_NAME "ntb" | |
64 | #define DRIVER_DESCRIPTION "PCIe NTB Driver Framework" | |
65 | ||
a1bd3bae AH |
66 | #define DRIVER_VERSION "1.0" |
67 | #define DRIVER_RELDATE "24 March 2015" | |
68 | #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>" | |
69 | ||
0ed08f82 | 70 | MODULE_LICENSE("Dual BSD/GPL"); |
a1bd3bae AH |
71 | MODULE_VERSION(DRIVER_VERSION); |
72 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
73 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION); | |
74 | ||
75 | static struct bus_type ntb_bus; | |
76 | static void ntb_dev_release(struct device *dev); | |
77 | ||
78 | int __ntb_register_client(struct ntb_client *client, struct module *mod, | |
79 | const char *mod_name) | |
80 | { | |
81 | if (!client) | |
82 | return -EINVAL; | |
83 | if (!ntb_client_ops_is_valid(&client->ops)) | |
84 | return -EINVAL; | |
85 | ||
86 | memset(&client->drv, 0, sizeof(client->drv)); | |
87 | client->drv.bus = &ntb_bus; | |
88 | client->drv.name = mod_name; | |
89 | client->drv.owner = mod; | |
90 | ||
91 | return driver_register(&client->drv); | |
92 | } | |
93 | EXPORT_SYMBOL(__ntb_register_client); | |
94 | ||
95 | void ntb_unregister_client(struct ntb_client *client) | |
96 | { | |
97 | driver_unregister(&client->drv); | |
98 | } | |
99 | EXPORT_SYMBOL(ntb_unregister_client); | |
100 | ||
101 | int ntb_register_device(struct ntb_dev *ntb) | |
102 | { | |
103 | if (!ntb) | |
104 | return -EINVAL; | |
105 | if (!ntb->pdev) | |
106 | return -EINVAL; | |
107 | if (!ntb->ops) | |
108 | return -EINVAL; | |
109 | if (!ntb_dev_ops_is_valid(ntb->ops)) | |
110 | return -EINVAL; | |
111 | ||
112 | init_completion(&ntb->released); | |
113 | ||
114 | memset(&ntb->dev, 0, sizeof(ntb->dev)); | |
115 | ntb->dev.bus = &ntb_bus; | |
116 | ntb->dev.parent = &ntb->pdev->dev; | |
117 | ntb->dev.release = ntb_dev_release; | |
e15f9409 | 118 | dev_set_name(&ntb->dev, "%s", pci_name(ntb->pdev)); |
a1bd3bae AH |
119 | |
120 | ntb->ctx = NULL; | |
121 | ntb->ctx_ops = NULL; | |
122 | spin_lock_init(&ntb->ctx_lock); | |
123 | ||
124 | return device_register(&ntb->dev); | |
125 | } | |
126 | EXPORT_SYMBOL(ntb_register_device); | |
127 | ||
128 | void ntb_unregister_device(struct ntb_dev *ntb) | |
129 | { | |
130 | device_unregister(&ntb->dev); | |
131 | wait_for_completion(&ntb->released); | |
132 | } | |
133 | EXPORT_SYMBOL(ntb_unregister_device); | |
134 | ||
135 | int ntb_set_ctx(struct ntb_dev *ntb, void *ctx, | |
136 | const struct ntb_ctx_ops *ctx_ops) | |
137 | { | |
138 | unsigned long irqflags; | |
139 | ||
140 | if (!ntb_ctx_ops_is_valid(ctx_ops)) | |
141 | return -EINVAL; | |
142 | if (ntb->ctx_ops) | |
143 | return -EINVAL; | |
144 | ||
145 | spin_lock_irqsave(&ntb->ctx_lock, irqflags); | |
146 | { | |
147 | ntb->ctx = ctx; | |
148 | ntb->ctx_ops = ctx_ops; | |
149 | } | |
150 | spin_unlock_irqrestore(&ntb->ctx_lock, irqflags); | |
151 | ||
152 | return 0; | |
153 | } | |
154 | EXPORT_SYMBOL(ntb_set_ctx); | |
155 | ||
156 | void ntb_clear_ctx(struct ntb_dev *ntb) | |
157 | { | |
158 | unsigned long irqflags; | |
159 | ||
160 | spin_lock_irqsave(&ntb->ctx_lock, irqflags); | |
161 | { | |
162 | ntb->ctx_ops = NULL; | |
163 | ntb->ctx = NULL; | |
164 | } | |
165 | spin_unlock_irqrestore(&ntb->ctx_lock, irqflags); | |
166 | } | |
167 | EXPORT_SYMBOL(ntb_clear_ctx); | |
168 | ||
169 | void ntb_link_event(struct ntb_dev *ntb) | |
170 | { | |
171 | unsigned long irqflags; | |
172 | ||
173 | spin_lock_irqsave(&ntb->ctx_lock, irqflags); | |
174 | { | |
175 | if (ntb->ctx_ops && ntb->ctx_ops->link_event) | |
176 | ntb->ctx_ops->link_event(ntb->ctx); | |
177 | } | |
178 | spin_unlock_irqrestore(&ntb->ctx_lock, irqflags); | |
179 | } | |
180 | EXPORT_SYMBOL(ntb_link_event); | |
181 | ||
182 | void ntb_db_event(struct ntb_dev *ntb, int vector) | |
183 | { | |
184 | unsigned long irqflags; | |
185 | ||
186 | spin_lock_irqsave(&ntb->ctx_lock, irqflags); | |
187 | { | |
188 | if (ntb->ctx_ops && ntb->ctx_ops->db_event) | |
189 | ntb->ctx_ops->db_event(ntb->ctx, vector); | |
190 | } | |
191 | spin_unlock_irqrestore(&ntb->ctx_lock, irqflags); | |
192 | } | |
193 | EXPORT_SYMBOL(ntb_db_event); | |
194 | ||
bc3e49ad SS |
195 | void ntb_msg_event(struct ntb_dev *ntb) |
196 | { | |
197 | unsigned long irqflags; | |
198 | ||
199 | spin_lock_irqsave(&ntb->ctx_lock, irqflags); | |
200 | { | |
201 | if (ntb->ctx_ops && ntb->ctx_ops->msg_event) | |
202 | ntb->ctx_ops->msg_event(ntb->ctx); | |
203 | } | |
204 | spin_unlock_irqrestore(&ntb->ctx_lock, irqflags); | |
205 | } | |
206 | EXPORT_SYMBOL(ntb_msg_event); | |
207 | ||
1e530119 SS |
208 | int ntb_default_port_number(struct ntb_dev *ntb) |
209 | { | |
210 | switch (ntb->topo) { | |
211 | case NTB_TOPO_PRI: | |
212 | case NTB_TOPO_B2B_USD: | |
213 | return NTB_PORT_PRI_USD; | |
214 | case NTB_TOPO_SEC: | |
215 | case NTB_TOPO_B2B_DSD: | |
216 | return NTB_PORT_SEC_DSD; | |
217 | default: | |
218 | break; | |
219 | } | |
220 | ||
221 | return -EINVAL; | |
222 | } | |
223 | EXPORT_SYMBOL(ntb_default_port_number); | |
224 | ||
225 | int ntb_default_peer_port_count(struct ntb_dev *ntb) | |
226 | { | |
227 | return NTB_DEF_PEER_CNT; | |
228 | } | |
229 | EXPORT_SYMBOL(ntb_default_peer_port_count); | |
230 | ||
231 | int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx) | |
232 | { | |
233 | if (pidx != NTB_DEF_PEER_IDX) | |
234 | return -EINVAL; | |
235 | ||
236 | switch (ntb->topo) { | |
237 | case NTB_TOPO_PRI: | |
238 | case NTB_TOPO_B2B_USD: | |
239 | return NTB_PORT_SEC_DSD; | |
240 | case NTB_TOPO_SEC: | |
241 | case NTB_TOPO_B2B_DSD: | |
242 | return NTB_PORT_PRI_USD; | |
243 | default: | |
244 | break; | |
245 | } | |
246 | ||
247 | return -EINVAL; | |
248 | } | |
249 | EXPORT_SYMBOL(ntb_default_peer_port_number); | |
250 | ||
251 | int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port) | |
252 | { | |
253 | int peer_port = ntb_default_peer_port_number(ntb, NTB_DEF_PEER_IDX); | |
254 | ||
255 | if (peer_port == -EINVAL || port != peer_port) | |
256 | return -EINVAL; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | EXPORT_SYMBOL(ntb_default_peer_port_idx); | |
261 | ||
a1bd3bae AH |
262 | static int ntb_probe(struct device *dev) |
263 | { | |
264 | struct ntb_dev *ntb; | |
265 | struct ntb_client *client; | |
266 | int rc; | |
267 | ||
268 | get_device(dev); | |
269 | ntb = dev_ntb(dev); | |
270 | client = drv_ntb_client(dev->driver); | |
271 | ||
272 | rc = client->ops.probe(client, ntb); | |
273 | if (rc) | |
274 | put_device(dev); | |
275 | ||
276 | return rc; | |
277 | } | |
278 | ||
279 | static int ntb_remove(struct device *dev) | |
280 | { | |
281 | struct ntb_dev *ntb; | |
282 | struct ntb_client *client; | |
283 | ||
284 | if (dev->driver) { | |
285 | ntb = dev_ntb(dev); | |
286 | client = drv_ntb_client(dev->driver); | |
287 | ||
288 | client->ops.remove(client, ntb); | |
289 | put_device(dev); | |
290 | } | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
295 | static void ntb_dev_release(struct device *dev) | |
296 | { | |
297 | struct ntb_dev *ntb = dev_ntb(dev); | |
298 | ||
299 | complete(&ntb->released); | |
300 | } | |
301 | ||
302 | static struct bus_type ntb_bus = { | |
303 | .name = "ntb", | |
304 | .probe = ntb_probe, | |
305 | .remove = ntb_remove, | |
306 | }; | |
307 | ||
308 | static int __init ntb_driver_init(void) | |
309 | { | |
310 | return bus_register(&ntb_bus); | |
311 | } | |
312 | module_init(ntb_driver_init); | |
313 | ||
314 | static void __exit ntb_driver_exit(void) | |
315 | { | |
316 | bus_unregister(&ntb_bus); | |
317 | } | |
318 | module_exit(ntb_driver_exit); | |
319 |