1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
66#define DRIVER_LICENSE "Dual BSD/GPL"
67#define DRIVER_VERSION "1.0"
68#define DRIVER_RELDATE "24 March 2015"
69#define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
70
71MODULE_LICENSE(DRIVER_LICENSE);
72MODULE_VERSION(DRIVER_VERSION);
73MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
75
76static struct bus_type ntb_bus;
77static void ntb_dev_release(struct device *dev);
78
79int __ntb_register_client(struct ntb_client *client, struct module *mod,
80 const char *mod_name)
81{
82 if (!client)
83 return -EINVAL;
84 if (!ntb_client_ops_is_valid(&client->ops))
85 return -EINVAL;
86
87 memset(&client->drv, 0, sizeof(client->drv));
88 client->drv.bus = &ntb_bus;
89 client->drv.name = mod_name;
90 client->drv.owner = mod;
91
92 return driver_register(&client->drv);
93}
94EXPORT_SYMBOL(__ntb_register_client);
95
96void ntb_unregister_client(struct ntb_client *client)
97{
98 driver_unregister(&client->drv);
99}
100EXPORT_SYMBOL(ntb_unregister_client);
101
102int ntb_register_device(struct ntb_dev *ntb)
103{
104 if (!ntb)
105 return -EINVAL;
106 if (!ntb->pdev)
107 return -EINVAL;
108 if (!ntb->ops)
109 return -EINVAL;
110 if (!ntb_dev_ops_is_valid(ntb->ops))
111 return -EINVAL;
112
113 init_completion(&ntb->released);
114
115 memset(&ntb->dev, 0, sizeof(ntb->dev));
116 ntb->dev.bus = &ntb_bus;
117 ntb->dev.parent = &ntb->pdev->dev;
118 ntb->dev.release = ntb_dev_release;
119 dev_set_name(&ntb->dev, "%s", pci_name(ntb->pdev));
120
121 ntb->ctx = NULL;
122 ntb->ctx_ops = NULL;
123 spin_lock_init(&ntb->ctx_lock);
124
125 return device_register(&ntb->dev);
126}
127EXPORT_SYMBOL(ntb_register_device);
128
129void ntb_unregister_device(struct ntb_dev *ntb)
130{
131 device_unregister(&ntb->dev);
132 wait_for_completion(&ntb->released);
133}
134EXPORT_SYMBOL(ntb_unregister_device);
135
136int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
137 const struct ntb_ctx_ops *ctx_ops)
138{
139 unsigned long irqflags;
140
141 if (!ntb_ctx_ops_is_valid(ctx_ops))
142 return -EINVAL;
143 if (ntb->ctx_ops)
144 return -EINVAL;
145
146 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
147 {
148 ntb->ctx = ctx;
149 ntb->ctx_ops = ctx_ops;
150 }
151 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
152
153 return 0;
154}
155EXPORT_SYMBOL(ntb_set_ctx);
156
157void ntb_clear_ctx(struct ntb_dev *ntb)
158{
159 unsigned long irqflags;
160
161 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
162 {
163 ntb->ctx_ops = NULL;
164 ntb->ctx = NULL;
165 }
166 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
167}
168EXPORT_SYMBOL(ntb_clear_ctx);
169
170void ntb_link_event(struct ntb_dev *ntb)
171{
172 unsigned long irqflags;
173
174 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
175 {
176 if (ntb->ctx_ops && ntb->ctx_ops->link_event)
177 ntb->ctx_ops->link_event(ntb->ctx);
178 }
179 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
180}
181EXPORT_SYMBOL(ntb_link_event);
182
183void ntb_db_event(struct ntb_dev *ntb, int vector)
184{
185 unsigned long irqflags;
186
187 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
188 {
189 if (ntb->ctx_ops && ntb->ctx_ops->db_event)
190 ntb->ctx_ops->db_event(ntb->ctx, vector);
191 }
192 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
193}
194EXPORT_SYMBOL(ntb_db_event);
195
196void ntb_msg_event(struct ntb_dev *ntb)
197{
198 unsigned long irqflags;
199
200 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
201 {
202 if (ntb->ctx_ops && ntb->ctx_ops->msg_event)
203 ntb->ctx_ops->msg_event(ntb->ctx);
204 }
205 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
206}
207EXPORT_SYMBOL(ntb_msg_event);
208
209int ntb_default_port_number(struct ntb_dev *ntb)
210{
211 switch (ntb->topo) {
212 case NTB_TOPO_PRI:
213 case NTB_TOPO_B2B_USD:
214 return NTB_PORT_PRI_USD;
215 case NTB_TOPO_SEC:
216 case NTB_TOPO_B2B_DSD:
217 return NTB_PORT_SEC_DSD;
218 default:
219 break;
220 }
221
222 return -EINVAL;
223}
224EXPORT_SYMBOL(ntb_default_port_number);
225
226int ntb_default_peer_port_count(struct ntb_dev *ntb)
227{
228 return NTB_DEF_PEER_CNT;
229}
230EXPORT_SYMBOL(ntb_default_peer_port_count);
231
232int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx)
233{
234 if (pidx != NTB_DEF_PEER_IDX)
235 return -EINVAL;
236
237 switch (ntb->topo) {
238 case NTB_TOPO_PRI:
239 case NTB_TOPO_B2B_USD:
240 return NTB_PORT_SEC_DSD;
241 case NTB_TOPO_SEC:
242 case NTB_TOPO_B2B_DSD:
243 return NTB_PORT_PRI_USD;
244 default:
245 break;
246 }
247
248 return -EINVAL;
249}
250EXPORT_SYMBOL(ntb_default_peer_port_number);
251
252int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port)
253{
254 int peer_port = ntb_default_peer_port_number(ntb, NTB_DEF_PEER_IDX);
255
256 if (peer_port == -EINVAL || port != peer_port)
257 return -EINVAL;
258
259 return 0;
260}
261EXPORT_SYMBOL(ntb_default_peer_port_idx);
262
263static int ntb_probe(struct device *dev)
264{
265 struct ntb_dev *ntb;
266 struct ntb_client *client;
267 int rc;
268
269 get_device(dev);
270 ntb = dev_ntb(dev);
271 client = drv_ntb_client(dev->driver);
272
273 rc = client->ops.probe(client, ntb);
274 if (rc)
275 put_device(dev);
276
277 return rc;
278}
279
280static int ntb_remove(struct device *dev)
281{
282 struct ntb_dev *ntb;
283 struct ntb_client *client;
284
285 if (dev->driver) {
286 ntb = dev_ntb(dev);
287 client = drv_ntb_client(dev->driver);
288
289 client->ops.remove(client, ntb);
290 put_device(dev);
291 }
292
293 return 0;
294}
295
296static void ntb_dev_release(struct device *dev)
297{
298 struct ntb_dev *ntb = dev_ntb(dev);
299
300 complete(&ntb->released);
301}
302
303static struct bus_type ntb_bus = {
304 .name = "ntb",
305 .probe = ntb_probe,
306 .remove = ntb_remove,
307};
308
309static int __init ntb_driver_init(void)
310{
311 return bus_register(&ntb_bus);
312}
313module_init(ntb_driver_init);
314
315static void __exit ntb_driver_exit(void)
316{
317 bus_unregister(&ntb_bus);
318}
319module_exit(ntb_driver_exit);
320
321