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