xref: /freebsd/sys/dev/ntb/ntb.c (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
1 /*-
2  * Copyright (c) 2016 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/rmlock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
38 
39 #include "ntb.h"
40 
41 devclass_t ntb_hw_devclass;
42 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW, 0, "NTB sysctls");
43 
44 struct ntb_child {
45 	device_t	dev;
46 	int		enabled;
47 	int		mwoff;
48 	int		mwcnt;
49 	int		spadoff;
50 	int		spadcnt;
51 	int		dboff;
52 	int		dbmask;
53 	void		*ctx;
54 	const struct ntb_ctx_ops *ctx_ops;
55 	struct rmlock	ctx_lock;
56 	struct ntb_child *next;
57 };
58 
59 int
60 ntb_register_device(device_t dev)
61 {
62 	struct ntb_child **cpp = device_get_softc(dev);
63 	struct ntb_child *nc;
64 	int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
65 	char cfg[128] = "";
66 	char buf[32];
67 	char *n, *np, *c, *p, *name;
68 
69 	mwu = 0;
70 	mwt = NTB_MW_COUNT(dev);
71 	spadu = 0;
72 	spadt = NTB_SPAD_COUNT(dev);
73 	dbu = 0;
74 	dbt = flsll(NTB_DB_VALID_MASK(dev));
75 
76 	device_printf(dev, "%d memory windows, %d scratchpads, "
77 	    "%d doorbells\n", mwt, spadt, dbt);
78 
79 	snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev),
80 	    device_get_unit(dev));
81 	TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg));
82 	n = cfg;
83 	i = 0;
84 	while ((c = strsep(&n, ",")) != NULL) {
85 		np = c;
86 		name = strsep(&np, ":");
87 		if (name != NULL && name[0] == 0)
88 			name = NULL;
89 		p = strsep(&np, ":");
90 		mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
91 		p = strsep(&np, ":");
92 		spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
93 		db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
94 
95 		if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
96 			device_printf(dev, "Not enough resources for config\n");
97 			break;
98 		}
99 
100 		nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
101 		nc->mwoff = mwu;
102 		nc->mwcnt = mw;
103 		nc->spadoff = spadu;
104 		nc->spadcnt = spad;
105 		nc->dboff = dbu;
106 		nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db));
107 		rm_init(&nc->ctx_lock, "ntb ctx");
108 		nc->dev = device_add_child(dev, name, -1);
109 		if (nc->dev == NULL) {
110 			ntb_unregister_device(dev);
111 			return (ENOMEM);
112 		}
113 		device_set_ivars(nc->dev, nc);
114 		*cpp = nc;
115 		cpp = &nc->next;
116 
117 		if (bootverbose) {
118 			device_printf(dev, "%d \"%s\":", i, name);
119 			if (mw > 0) {
120 				printf(" memory windows %d", mwu);
121 				if (mw > 1)
122 					printf("-%d", mwu + mw - 1);
123 			}
124 			if (spad > 0) {
125 				printf(" scratchpads %d", spadu);
126 				if (spad > 1)
127 					printf("-%d", spadu + spad - 1);
128 			}
129 			if (db > 0) {
130 				printf(" doorbells %d", dbu);
131 				if (db > 1)
132 					printf("-%d", dbu + db - 1);
133 			}
134 			printf("\n");
135 		}
136 
137 		mwu += mw;
138 		spadu += spad;
139 		dbu += db;
140 		i++;
141 	}
142 
143 	bus_generic_attach(dev);
144 	return (0);
145 }
146 
147 int
148 ntb_unregister_device(device_t dev)
149 {
150 	struct ntb_child **cpp = device_get_softc(dev);
151 	struct ntb_child *nc;
152 	int error = 0;
153 
154 	while ((nc = *cpp) != NULL) {
155 		*cpp = (*cpp)->next;
156 		error = device_delete_child(dev, nc->dev);
157 		if (error)
158 			break;
159 		rm_destroy(&nc->ctx_lock);
160 		free(nc, M_DEVBUF);
161 	}
162 	return (error);
163 }
164 
165 void
166 ntb_link_event(device_t dev)
167 {
168 	struct ntb_child **cpp = device_get_softc(dev);
169 	struct ntb_child *nc;
170 	struct rm_priotracker ctx_tracker;
171 	enum ntb_speed speed;
172 	enum ntb_width width;
173 
174 	if (NTB_LINK_IS_UP(dev, &speed, &width)) {
175 		device_printf(dev, "Link is up (PCIe %d.x / x%d)\n",
176 		    (int)speed, (int)width);
177 	} else {
178 		device_printf(dev, "Link is down\n");
179 	}
180 	for (nc = *cpp; nc != NULL; nc = nc->next) {
181 		rm_rlock(&nc->ctx_lock, &ctx_tracker);
182 		if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
183 			nc->ctx_ops->link_event(nc->ctx);
184 		rm_runlock(&nc->ctx_lock, &ctx_tracker);
185 	}
186 }
187 
188 void
189 ntb_db_event(device_t dev, uint32_t vec)
190 {
191 	struct ntb_child **cpp = device_get_softc(dev);
192 	struct ntb_child *nc;
193 	struct rm_priotracker ctx_tracker;
194 
195 	for (nc = *cpp; nc != NULL; nc = nc->next) {
196 		rm_rlock(&nc->ctx_lock, &ctx_tracker);
197 		if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
198 			nc->ctx_ops->db_event(nc->ctx, vec);
199 		rm_runlock(&nc->ctx_lock, &ctx_tracker);
200 	}
201 }
202 
203 bool
204 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
205 {
206 
207 	return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
208 }
209 
210 int
211 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
212 {
213 	struct ntb_child *nc = device_get_ivars(ntb);
214 	struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
215 	struct ntb_child *nc1;
216 
217 	for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
218 		if (nc1->enabled) {
219 			nc->enabled = 1;
220 			return (0);
221 		}
222 	}
223 	nc->enabled = 1;
224 	return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
225 }
226 
227 int
228 ntb_link_disable(device_t ntb)
229 {
230 	struct ntb_child *nc = device_get_ivars(ntb);
231 	struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
232 	struct ntb_child *nc1;
233 
234 	if (!nc->enabled)
235 		return (0);
236 	nc->enabled = 0;
237 	for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
238 		if (nc1->enabled)
239 			return (0);
240 	}
241 	return (NTB_LINK_DISABLE(device_get_parent(ntb)));
242 }
243 
244 bool
245 ntb_link_enabled(device_t ntb)
246 {
247 	struct ntb_child *nc = device_get_ivars(ntb);
248 
249 	return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
250 }
251 
252 int
253 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
254 {
255 	struct ntb_child *nc = device_get_ivars(ntb);
256 
257 	if (ctx == NULL || ctx_ops == NULL)
258 		return (EINVAL);
259 
260 	rm_wlock(&nc->ctx_lock);
261 	if (nc->ctx_ops != NULL) {
262 		rm_wunlock(&nc->ctx_lock);
263 		return (EINVAL);
264 	}
265 	nc->ctx = ctx;
266 	nc->ctx_ops = ctx_ops;
267 	rm_wunlock(&nc->ctx_lock);
268 
269 	return (0);
270 }
271 
272 void *
273 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
274 {
275 	struct ntb_child *nc = device_get_ivars(ntb);
276 
277 	KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
278 	if (ctx_ops != NULL)
279 		*ctx_ops = nc->ctx_ops;
280 	return (nc->ctx);
281 }
282 
283 void
284 ntb_clear_ctx(device_t ntb)
285 {
286 	struct ntb_child *nc = device_get_ivars(ntb);
287 
288 	rm_wlock(&nc->ctx_lock);
289 	nc->ctx = NULL;
290 	nc->ctx_ops = NULL;
291 	rm_wunlock(&nc->ctx_lock);
292 }
293 
294 uint8_t
295 ntb_mw_count(device_t ntb)
296 {
297 	struct ntb_child *nc = device_get_ivars(ntb);
298 
299 	return (nc->mwcnt);
300 }
301 
302 int
303 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
304     caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
305     bus_addr_t *plimit)
306 {
307 	struct ntb_child *nc = device_get_ivars(ntb);
308 
309 	return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
310 	    base, vbase, size, align, align_size, plimit));
311 }
312 
313 int
314 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
315 {
316 	struct ntb_child *nc = device_get_ivars(ntb);
317 
318 	return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
319 	    addr, size));
320 }
321 
322 int
323 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
324 {
325 	struct ntb_child *nc = device_get_ivars(ntb);
326 
327 	return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
328 }
329 
330 int
331 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
332 {
333 	struct ntb_child *nc = device_get_ivars(ntb);
334 
335 	return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
336 }
337 
338 int
339 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
340 {
341 	struct ntb_child *nc = device_get_ivars(ntb);
342 
343 	return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
344 }
345 
346 uint8_t
347 ntb_spad_count(device_t ntb)
348 {
349 	struct ntb_child *nc = device_get_ivars(ntb);
350 
351 	return (nc->spadcnt);
352 }
353 
354 void
355 ntb_spad_clear(device_t ntb)
356 {
357 	struct ntb_child *nc = device_get_ivars(ntb);
358 	unsigned i;
359 
360 	for (i = 0; i < nc->spadcnt; i++)
361 		NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
362 }
363 
364 int
365 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
366 {
367 	struct ntb_child *nc = device_get_ivars(ntb);
368 
369 	return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
370 }
371 
372 int
373 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
374 {
375 	struct ntb_child *nc = device_get_ivars(ntb);
376 
377 	return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
378 }
379 
380 int
381 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
382 {
383 	struct ntb_child *nc = device_get_ivars(ntb);
384 
385 	return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
386 	    val));
387 }
388 
389 int
390 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
391 {
392 	struct ntb_child *nc = device_get_ivars(ntb);
393 
394 	return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
395 	    val));
396 }
397 
398 uint64_t
399 ntb_db_valid_mask(device_t ntb)
400 {
401 	struct ntb_child *nc = device_get_ivars(ntb);
402 
403 	return (nc->dbmask);
404 }
405 
406 int
407 ntb_db_vector_count(device_t ntb)
408 {
409 
410 	return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
411 }
412 
413 uint64_t
414 ntb_db_vector_mask(device_t ntb, uint32_t vector)
415 {
416 	struct ntb_child *nc = device_get_ivars(ntb);
417 
418 	return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
419 	    >> nc->dboff) & nc->dbmask);
420 }
421 
422 int
423 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
424 {
425 
426 	return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
427 }
428 
429 void
430 ntb_db_clear(device_t ntb, uint64_t bits)
431 {
432 	struct ntb_child *nc = device_get_ivars(ntb);
433 
434 	return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
435 }
436 
437 void
438 ntb_db_clear_mask(device_t ntb, uint64_t bits)
439 {
440 	struct ntb_child *nc = device_get_ivars(ntb);
441 
442 	return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
443 }
444 
445 uint64_t
446 ntb_db_read(device_t ntb)
447 {
448 	struct ntb_child *nc = device_get_ivars(ntb);
449 
450 	return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
451 	    & nc->dbmask);
452 }
453 
454 void
455 ntb_db_set_mask(device_t ntb, uint64_t bits)
456 {
457 	struct ntb_child *nc = device_get_ivars(ntb);
458 
459 	return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
460 }
461 
462 void
463 ntb_peer_db_set(device_t ntb, uint64_t bits)
464 {
465 	struct ntb_child *nc = device_get_ivars(ntb);
466 
467 	return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
468 }
469 
470 MODULE_VERSION(ntb, 1);
471