xref: /freebsd/sys/dev/ntb/ntb.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 /*-
2  * Copyright (c) 2016-2017 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/sbuf.h>
38 #include <sys/sysctl.h>
39 
40 #include "ntb.h"
41 
42 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
43     "NTB sysctls");
44 
45 struct ntb_child {
46 	device_t	dev;
47 	int		function;
48 	int		enabled;
49 	int		mwoff;
50 	int		mwcnt;
51 	int		spadoff;
52 	int		spadcnt;
53 	int		dboff;
54 	int		dbcnt;
55 	uint64_t	dbmask;
56 	void		*ctx;
57 	const struct ntb_ctx_ops *ctx_ops;
58 	struct rmlock	ctx_lock;
59 	struct ntb_child *next;
60 };
61 
62 int
63 ntb_register_device(device_t dev)
64 {
65 	struct ntb_child **cpp = device_get_softc(dev);
66 	struct ntb_child *nc;
67 	int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
68 	char cfg[128] = "";
69 	char buf[32];
70 	char *n, *np, *c, *p, *name;
71 
72 	mwu = 0;
73 	mwt = NTB_MW_COUNT(dev);
74 	spadu = 0;
75 	spadt = NTB_SPAD_COUNT(dev);
76 	dbu = 0;
77 	dbt = flsll(NTB_DB_VALID_MASK(dev));
78 
79 	device_printf(dev, "%d memory windows, %d scratchpads, "
80 	    "%d doorbells\n", mwt, spadt, dbt);
81 
82 	snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev),
83 	    device_get_unit(dev));
84 	TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg));
85 	n = cfg;
86 	i = 0;
87 	while ((c = strsep(&n, ",")) != NULL) {
88 		np = c;
89 		name = strsep(&np, ":");
90 		if (name != NULL && name[0] == 0)
91 			name = NULL;
92 		p = strsep(&np, ":");
93 		mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
94 		p = strsep(&np, ":");
95 		spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
96 		db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
97 
98 		if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
99 			device_printf(dev, "Not enough resources for config\n");
100 			break;
101 		}
102 
103 		nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
104 		nc->function = i;
105 		nc->mwoff = mwu;
106 		nc->mwcnt = mw;
107 		nc->spadoff = spadu;
108 		nc->spadcnt = spad;
109 		nc->dboff = dbu;
110 		nc->dbcnt = db;
111 		nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db));
112 		rm_init(&nc->ctx_lock, "ntb ctx");
113 		nc->dev = device_add_child(dev, name, -1);
114 		if (nc->dev == NULL) {
115 			ntb_unregister_device(dev);
116 			return (ENOMEM);
117 		}
118 		device_set_ivars(nc->dev, nc);
119 		*cpp = nc;
120 		cpp = &nc->next;
121 
122 		if (bootverbose) {
123 			device_printf(dev, "%d \"%s\":", i, name);
124 			if (mw > 0) {
125 				printf(" memory windows %d", mwu);
126 				if (mw > 1)
127 					printf("-%d", mwu + mw - 1);
128 			}
129 			if (spad > 0) {
130 				printf(" scratchpads %d", spadu);
131 				if (spad > 1)
132 					printf("-%d", spadu + spad - 1);
133 			}
134 			if (db > 0) {
135 				printf(" doorbells %d", dbu);
136 				if (db > 1)
137 					printf("-%d", dbu + db - 1);
138 			}
139 			printf("\n");
140 		}
141 
142 		mwu += mw;
143 		spadu += spad;
144 		dbu += db;
145 		i++;
146 	}
147 
148 	bus_generic_attach(dev);
149 	return (0);
150 }
151 
152 int
153 ntb_unregister_device(device_t dev)
154 {
155 	struct ntb_child **cpp = device_get_softc(dev);
156 	struct ntb_child *nc;
157 	int error = 0;
158 
159 	while ((nc = *cpp) != NULL) {
160 		*cpp = (*cpp)->next;
161 		error = device_delete_child(dev, nc->dev);
162 		if (error)
163 			break;
164 		rm_destroy(&nc->ctx_lock);
165 		free(nc, M_DEVBUF);
166 	}
167 	return (error);
168 }
169 
170 int
171 ntb_child_location(device_t dev, device_t child, struct sbuf *sb)
172 {
173 	struct ntb_child *nc = device_get_ivars(child);
174 
175 	sbuf_printf(sb, "function=%d", nc->function);
176 	return (0);
177 }
178 
179 int
180 ntb_print_child(device_t dev, device_t child)
181 {
182 	struct ntb_child *nc = device_get_ivars(child);
183 	int retval;
184 
185 	retval = bus_print_child_header(dev, child);
186 	if (nc->mwcnt > 0) {
187 		printf(" mw %d", nc->mwoff);
188 		if (nc->mwcnt > 1)
189 			printf("-%d", nc->mwoff + nc->mwcnt - 1);
190 	}
191 	if (nc->spadcnt > 0) {
192 		printf(" spad %d", nc->spadoff);
193 		if (nc->spadcnt > 1)
194 			printf("-%d", nc->spadoff + nc->spadcnt - 1);
195 	}
196 	if (nc->dbcnt > 0) {
197 		printf(" db %d", nc->dboff);
198 		if (nc->dbcnt > 1)
199 			printf("-%d", nc->dboff + nc->dbcnt - 1);
200 	}
201 	retval += printf(" at function %d", nc->function);
202 	retval += bus_print_child_domain(dev, child);
203 	retval += bus_print_child_footer(dev, child);
204 
205 	return (retval);
206 }
207 
208 bus_dma_tag_t
209 ntb_get_dma_tag(device_t bus, device_t child)
210 {
211 
212 	return (bus_get_dma_tag(bus));
213 }
214 
215 void
216 ntb_link_event(device_t dev)
217 {
218 	struct ntb_child **cpp = device_get_softc(dev);
219 	struct ntb_child *nc;
220 	struct rm_priotracker ctx_tracker;
221 	enum ntb_speed speed;
222 	enum ntb_width width;
223 
224 	if (NTB_LINK_IS_UP(dev, &speed, &width)) {
225 		device_printf(dev, "Link is up (PCIe %d.x / x%d)\n",
226 		    (int)speed, (int)width);
227 	} else {
228 		device_printf(dev, "Link is down\n");
229 	}
230 	for (nc = *cpp; nc != NULL; nc = nc->next) {
231 		rm_rlock(&nc->ctx_lock, &ctx_tracker);
232 		if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
233 			nc->ctx_ops->link_event(nc->ctx);
234 		rm_runlock(&nc->ctx_lock, &ctx_tracker);
235 	}
236 }
237 
238 void
239 ntb_db_event(device_t dev, uint32_t vec)
240 {
241 	struct ntb_child **cpp = device_get_softc(dev);
242 	struct ntb_child *nc;
243 	struct rm_priotracker ctx_tracker;
244 
245 	for (nc = *cpp; nc != NULL; nc = nc->next) {
246 		rm_rlock(&nc->ctx_lock, &ctx_tracker);
247 		if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
248 			nc->ctx_ops->db_event(nc->ctx, vec);
249 		rm_runlock(&nc->ctx_lock, &ctx_tracker);
250 	}
251 }
252 
253 int
254 ntb_port_number(device_t ntb)
255 {
256 	return (NTB_PORT_NUMBER(device_get_parent(ntb)));
257 }
258 
259 int
260 ntb_peer_port_count(device_t ntb)
261 {
262 	return (NTB_PEER_PORT_COUNT(device_get_parent(ntb)));
263 }
264 
265 int
266 ntb_peer_port_number(device_t ntb, int pidx)
267 {
268 	return (NTB_PEER_PORT_NUMBER(device_get_parent(ntb), pidx));
269 }
270 
271 int
272 ntb_peer_port_idx(device_t ntb, int port)
273 {
274 	return (NTB_PEER_PORT_IDX(device_get_parent(ntb), port));
275 }
276 
277 bool
278 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
279 {
280 
281 	return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
282 }
283 
284 int
285 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
286 {
287 	struct ntb_child *nc = device_get_ivars(ntb);
288 	struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
289 	struct ntb_child *nc1;
290 
291 	for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
292 		if (nc1->enabled) {
293 			nc->enabled = 1;
294 			return (0);
295 		}
296 	}
297 	nc->enabled = 1;
298 	return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
299 }
300 
301 int
302 ntb_link_disable(device_t ntb)
303 {
304 	struct ntb_child *nc = device_get_ivars(ntb);
305 	struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
306 	struct ntb_child *nc1;
307 
308 	if (!nc->enabled)
309 		return (0);
310 	nc->enabled = 0;
311 	for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
312 		if (nc1->enabled)
313 			return (0);
314 	}
315 	return (NTB_LINK_DISABLE(device_get_parent(ntb)));
316 }
317 
318 bool
319 ntb_link_enabled(device_t ntb)
320 {
321 	struct ntb_child *nc = device_get_ivars(ntb);
322 
323 	return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
324 }
325 
326 int
327 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
328 {
329 	struct ntb_child *nc = device_get_ivars(ntb);
330 
331 	if (ctx == NULL || ctx_ops == NULL)
332 		return (EINVAL);
333 
334 	rm_wlock(&nc->ctx_lock);
335 	if (nc->ctx_ops != NULL) {
336 		rm_wunlock(&nc->ctx_lock);
337 		return (EINVAL);
338 	}
339 	nc->ctx = ctx;
340 	nc->ctx_ops = ctx_ops;
341 
342 	/*
343 	 * If applicaiton driver asks for link events, generate fake one now
344 	 * to let it update link state without races while we hold the lock.
345 	 */
346 	if (ctx_ops->link_event != NULL)
347 		ctx_ops->link_event(ctx);
348 	rm_wunlock(&nc->ctx_lock);
349 
350 	return (0);
351 }
352 
353 void *
354 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
355 {
356 	struct ntb_child *nc = device_get_ivars(ntb);
357 
358 	KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
359 	if (ctx_ops != NULL)
360 		*ctx_ops = nc->ctx_ops;
361 	return (nc->ctx);
362 }
363 
364 void
365 ntb_clear_ctx(device_t ntb)
366 {
367 	struct ntb_child *nc = device_get_ivars(ntb);
368 
369 	rm_wlock(&nc->ctx_lock);
370 	nc->ctx = NULL;
371 	nc->ctx_ops = NULL;
372 	rm_wunlock(&nc->ctx_lock);
373 }
374 
375 uint8_t
376 ntb_mw_count(device_t ntb)
377 {
378 	struct ntb_child *nc = device_get_ivars(ntb);
379 
380 	return (nc->mwcnt);
381 }
382 
383 int
384 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
385     caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
386     bus_addr_t *plimit)
387 {
388 	struct ntb_child *nc = device_get_ivars(ntb);
389 
390 	return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
391 	    base, vbase, size, align, align_size, plimit));
392 }
393 
394 int
395 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
396 {
397 	struct ntb_child *nc = device_get_ivars(ntb);
398 
399 	return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
400 	    addr, size));
401 }
402 
403 int
404 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
405 {
406 	struct ntb_child *nc = device_get_ivars(ntb);
407 
408 	return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
409 }
410 
411 int
412 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
413 {
414 	struct ntb_child *nc = device_get_ivars(ntb);
415 
416 	return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
417 }
418 
419 int
420 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
421 {
422 	struct ntb_child *nc = device_get_ivars(ntb);
423 
424 	return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
425 }
426 
427 uint8_t
428 ntb_spad_count(device_t ntb)
429 {
430 	struct ntb_child *nc = device_get_ivars(ntb);
431 
432 	return (nc->spadcnt);
433 }
434 
435 void
436 ntb_spad_clear(device_t ntb)
437 {
438 	struct ntb_child *nc = device_get_ivars(ntb);
439 	unsigned i;
440 
441 	for (i = 0; i < nc->spadcnt; i++)
442 		NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
443 }
444 
445 int
446 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
447 {
448 	struct ntb_child *nc = device_get_ivars(ntb);
449 
450 	return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
451 }
452 
453 int
454 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
455 {
456 	struct ntb_child *nc = device_get_ivars(ntb);
457 
458 	return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
459 }
460 
461 int
462 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
463 {
464 	struct ntb_child *nc = device_get_ivars(ntb);
465 
466 	return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
467 	    val));
468 }
469 
470 int
471 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
472 {
473 	struct ntb_child *nc = device_get_ivars(ntb);
474 
475 	return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
476 	    val));
477 }
478 
479 uint64_t
480 ntb_db_valid_mask(device_t ntb)
481 {
482 	struct ntb_child *nc = device_get_ivars(ntb);
483 
484 	return (nc->dbmask);
485 }
486 
487 int
488 ntb_db_vector_count(device_t ntb)
489 {
490 
491 	return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
492 }
493 
494 uint64_t
495 ntb_db_vector_mask(device_t ntb, uint32_t vector)
496 {
497 	struct ntb_child *nc = device_get_ivars(ntb);
498 
499 	return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
500 	    >> nc->dboff) & nc->dbmask);
501 }
502 
503 int
504 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
505 {
506 
507 	return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
508 }
509 
510 void
511 ntb_db_clear(device_t ntb, uint64_t bits)
512 {
513 	struct ntb_child *nc = device_get_ivars(ntb);
514 
515 	return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
516 }
517 
518 void
519 ntb_db_clear_mask(device_t ntb, uint64_t bits)
520 {
521 	struct ntb_child *nc = device_get_ivars(ntb);
522 
523 	return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
524 }
525 
526 uint64_t
527 ntb_db_read(device_t ntb)
528 {
529 	struct ntb_child *nc = device_get_ivars(ntb);
530 
531 	return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
532 	    & nc->dbmask);
533 }
534 
535 void
536 ntb_db_set_mask(device_t ntb, uint64_t bits)
537 {
538 	struct ntb_child *nc = device_get_ivars(ntb);
539 
540 	return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
541 }
542 
543 void
544 ntb_peer_db_set(device_t ntb, uint64_t bits)
545 {
546 	struct ntb_child *nc = device_get_ivars(ntb);
547 
548 	return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
549 }
550 
551 MODULE_VERSION(ntb, 1);
552