2 * Copyright (c) 2016-2017 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
34 #include <sys/rmlock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
41 devclass_t ntb_hw_devclass;
42 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW, 0, "NTB sysctls");
56 const struct ntb_ctx_ops *ctx_ops;
57 struct rmlock ctx_lock;
58 struct ntb_child *next;
62 ntb_register_device(device_t dev)
64 struct ntb_child **cpp = device_get_softc(dev);
66 int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
69 char *n, *np, *c, *p, *name;
72 mwt = NTB_MW_COUNT(dev);
74 spadt = NTB_SPAD_COUNT(dev);
76 dbt = flsll(NTB_DB_VALID_MASK(dev));
78 device_printf(dev, "%d memory windows, %d scratchpads, "
79 "%d doorbells\n", mwt, spadt, dbt);
81 snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev),
82 device_get_unit(dev));
83 TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg));
86 while ((c = strsep(&n, ",")) != NULL) {
88 name = strsep(&np, ":");
89 if (name != NULL && name[0] == 0)
92 mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
94 spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
95 db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
97 if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
98 device_printf(dev, "Not enough resources for config\n");
102 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
110 nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db));
111 rm_init(&nc->ctx_lock, "ntb ctx");
112 nc->dev = device_add_child(dev, name, -1);
113 if (nc->dev == NULL) {
114 ntb_unregister_device(dev);
117 device_set_ivars(nc->dev, nc);
122 device_printf(dev, "%d \"%s\":", i, name);
124 printf(" memory windows %d", mwu);
126 printf("-%d", mwu + mw - 1);
129 printf(" scratchpads %d", spadu);
131 printf("-%d", spadu + spad - 1);
134 printf(" doorbells %d", dbu);
136 printf("-%d", dbu + db - 1);
147 bus_generic_attach(dev);
152 ntb_unregister_device(device_t dev)
154 struct ntb_child **cpp = device_get_softc(dev);
155 struct ntb_child *nc;
158 while ((nc = *cpp) != NULL) {
160 error = device_delete_child(dev, nc->dev);
163 rm_destroy(&nc->ctx_lock);
170 ntb_child_location_str(device_t dev, device_t child, char *buf,
173 struct ntb_child *nc = device_get_ivars(child);
175 snprintf(buf, buflen, "function=%d", nc->function);
180 ntb_print_child(device_t dev, device_t child)
182 struct ntb_child *nc = device_get_ivars(child);
185 retval = bus_print_child_header(dev, child);
187 printf(" mw %d", nc->mwoff);
189 printf("-%d", nc->mwoff + nc->mwcnt - 1);
191 if (nc->spadcnt > 0) {
192 printf(" spad %d", nc->spadoff);
194 printf("-%d", nc->spadoff + nc->spadcnt - 1);
197 printf(" db %d", nc->dboff);
199 printf("-%d", nc->dboff + nc->dbcnt - 1);
201 retval += printf(" at function %d", nc->function);
202 retval += bus_print_child_domain(dev, child);
203 retval += bus_print_child_footer(dev, child);
209 ntb_link_event(device_t dev)
211 struct ntb_child **cpp = device_get_softc(dev);
212 struct ntb_child *nc;
213 struct rm_priotracker ctx_tracker;
214 enum ntb_speed speed;
215 enum ntb_width width;
217 if (NTB_LINK_IS_UP(dev, &speed, &width)) {
218 device_printf(dev, "Link is up (PCIe %d.x / x%d)\n",
219 (int)speed, (int)width);
221 device_printf(dev, "Link is down\n");
223 for (nc = *cpp; nc != NULL; nc = nc->next) {
224 rm_rlock(&nc->ctx_lock, &ctx_tracker);
225 if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
226 nc->ctx_ops->link_event(nc->ctx);
227 rm_runlock(&nc->ctx_lock, &ctx_tracker);
232 ntb_db_event(device_t dev, uint32_t vec)
234 struct ntb_child **cpp = device_get_softc(dev);
235 struct ntb_child *nc;
236 struct rm_priotracker ctx_tracker;
238 for (nc = *cpp; nc != NULL; nc = nc->next) {
239 rm_rlock(&nc->ctx_lock, &ctx_tracker);
240 if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
241 nc->ctx_ops->db_event(nc->ctx, vec);
242 rm_runlock(&nc->ctx_lock, &ctx_tracker);
247 ntb_port_number(device_t ntb)
249 return (NTB_PORT_NUMBER(device_get_parent(ntb)));
253 ntb_peer_port_count(device_t ntb)
255 return (NTB_PEER_PORT_COUNT(device_get_parent(ntb)));
259 ntb_peer_port_number(device_t ntb, int pidx)
261 return (NTB_PEER_PORT_NUMBER(device_get_parent(ntb), pidx));
265 ntb_peer_port_idx(device_t ntb, int port)
267 return (NTB_PEER_PORT_IDX(device_get_parent(ntb), port));
271 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
274 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
278 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
280 struct ntb_child *nc = device_get_ivars(ntb);
281 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
282 struct ntb_child *nc1;
284 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
291 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
295 ntb_link_disable(device_t ntb)
297 struct ntb_child *nc = device_get_ivars(ntb);
298 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
299 struct ntb_child *nc1;
304 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
308 return (NTB_LINK_DISABLE(device_get_parent(ntb)));
312 ntb_link_enabled(device_t ntb)
314 struct ntb_child *nc = device_get_ivars(ntb);
316 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
320 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
322 struct ntb_child *nc = device_get_ivars(ntb);
324 if (ctx == NULL || ctx_ops == NULL)
327 rm_wlock(&nc->ctx_lock);
328 if (nc->ctx_ops != NULL) {
329 rm_wunlock(&nc->ctx_lock);
333 nc->ctx_ops = ctx_ops;
336 * If applicaiton driver asks for link events, generate fake one now
337 * to let it update link state without races while we hold the lock.
339 if (ctx_ops->link_event != NULL)
340 ctx_ops->link_event(ctx);
341 rm_wunlock(&nc->ctx_lock);
347 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
349 struct ntb_child *nc = device_get_ivars(ntb);
351 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
353 *ctx_ops = nc->ctx_ops;
358 ntb_clear_ctx(device_t ntb)
360 struct ntb_child *nc = device_get_ivars(ntb);
362 rm_wlock(&nc->ctx_lock);
365 rm_wunlock(&nc->ctx_lock);
369 ntb_mw_count(device_t ntb)
371 struct ntb_child *nc = device_get_ivars(ntb);
377 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
378 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
381 struct ntb_child *nc = device_get_ivars(ntb);
383 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
384 base, vbase, size, align, align_size, plimit));
388 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
390 struct ntb_child *nc = device_get_ivars(ntb);
392 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
397 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
399 struct ntb_child *nc = device_get_ivars(ntb);
401 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
405 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
407 struct ntb_child *nc = device_get_ivars(ntb);
409 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
413 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
415 struct ntb_child *nc = device_get_ivars(ntb);
417 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
421 ntb_spad_count(device_t ntb)
423 struct ntb_child *nc = device_get_ivars(ntb);
425 return (nc->spadcnt);
429 ntb_spad_clear(device_t ntb)
431 struct ntb_child *nc = device_get_ivars(ntb);
434 for (i = 0; i < nc->spadcnt; i++)
435 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
439 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
441 struct ntb_child *nc = device_get_ivars(ntb);
443 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
447 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
449 struct ntb_child *nc = device_get_ivars(ntb);
451 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
455 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
457 struct ntb_child *nc = device_get_ivars(ntb);
459 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
464 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
466 struct ntb_child *nc = device_get_ivars(ntb);
468 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
473 ntb_db_valid_mask(device_t ntb)
475 struct ntb_child *nc = device_get_ivars(ntb);
481 ntb_db_vector_count(device_t ntb)
484 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
488 ntb_db_vector_mask(device_t ntb, uint32_t vector)
490 struct ntb_child *nc = device_get_ivars(ntb);
492 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
493 >> nc->dboff) & nc->dbmask);
497 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
500 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
504 ntb_db_clear(device_t ntb, uint64_t bits)
506 struct ntb_child *nc = device_get_ivars(ntb);
508 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
512 ntb_db_clear_mask(device_t ntb, uint64_t bits)
514 struct ntb_child *nc = device_get_ivars(ntb);
516 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
520 ntb_db_read(device_t ntb)
522 struct ntb_child *nc = device_get_ivars(ntb);
524 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
529 ntb_db_set_mask(device_t ntb, uint64_t bits)
531 struct ntb_child *nc = device_get_ivars(ntb);
533 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
537 ntb_peer_db_set(device_t ntb, uint64_t bits)
539 struct ntb_child *nc = device_get_ivars(ntb);
541 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
544 MODULE_VERSION(ntb, 1);