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/param.h>
28 #include <sys/kernel.h>
29 #include <sys/systm.h>
31 #include <sys/rmlock.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
35 #include <sys/sysctl.h>
39 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
54 const struct ntb_ctx_ops *ctx_ops;
55 struct rmlock ctx_lock;
56 struct ntb_child *next;
60 ntb_register_device(device_t dev)
62 struct ntb_child **cpp = device_get_softc(dev);
64 int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
67 char *n, *np, *c, *p, *name;
70 mwt = NTB_MW_COUNT(dev);
72 spadt = NTB_SPAD_COUNT(dev);
74 dbt = flsll(NTB_DB_VALID_MASK(dev));
76 device_printf(dev, "%d memory windows, %d scratchpads, "
77 "%d doorbells\n", mwt, spadt, dbt);
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));
84 while ((c = strsep(&n, ",")) != NULL) {
86 name = strsep(&np, ":");
87 if (name != NULL && name[0] == 0)
90 mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
92 spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
93 db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
95 if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
96 device_printf(dev, "Not enough resources for config\n");
100 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
108 nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db));
109 rm_init(&nc->ctx_lock, "ntb ctx");
110 nc->dev = device_add_child(dev, name, -1);
111 if (nc->dev == NULL) {
112 ntb_unregister_device(dev);
115 device_set_ivars(nc->dev, nc);
120 device_printf(dev, "%d \"%s\":", i, name);
122 printf(" memory windows %d", mwu);
124 printf("-%d", mwu + mw - 1);
127 printf(" scratchpads %d", spadu);
129 printf("-%d", spadu + spad - 1);
132 printf(" doorbells %d", dbu);
134 printf("-%d", dbu + db - 1);
145 bus_generic_attach(dev);
150 ntb_unregister_device(device_t dev)
152 struct ntb_child **cpp = device_get_softc(dev);
153 struct ntb_child *nc;
156 while ((nc = *cpp) != NULL) {
158 error = device_delete_child(dev, nc->dev);
161 rm_destroy(&nc->ctx_lock);
168 ntb_child_location(device_t dev, device_t child, struct sbuf *sb)
170 struct ntb_child *nc = device_get_ivars(child);
172 sbuf_printf(sb, "function=%d", nc->function);
177 ntb_print_child(device_t dev, device_t child)
179 struct ntb_child *nc = device_get_ivars(child);
182 retval = bus_print_child_header(dev, child);
184 printf(" mw %d", nc->mwoff);
186 printf("-%d", nc->mwoff + nc->mwcnt - 1);
188 if (nc->spadcnt > 0) {
189 printf(" spad %d", nc->spadoff);
191 printf("-%d", nc->spadoff + nc->spadcnt - 1);
194 printf(" db %d", nc->dboff);
196 printf("-%d", nc->dboff + nc->dbcnt - 1);
198 retval += printf(" at function %d", nc->function);
199 retval += bus_print_child_domain(dev, child);
200 retval += bus_print_child_footer(dev, child);
206 ntb_get_dma_tag(device_t bus, device_t child)
209 return (bus_get_dma_tag(bus));
213 ntb_link_event(device_t dev)
215 struct ntb_child **cpp = device_get_softc(dev);
216 struct ntb_child *nc;
217 struct rm_priotracker ctx_tracker;
218 enum ntb_speed speed;
219 enum ntb_width width;
221 if (NTB_LINK_IS_UP(dev, &speed, &width)) {
222 device_printf(dev, "Link is up (PCIe %d.x / x%d)\n",
223 (int)speed, (int)width);
225 device_printf(dev, "Link is down\n");
227 for (nc = *cpp; nc != NULL; nc = nc->next) {
228 rm_rlock(&nc->ctx_lock, &ctx_tracker);
229 if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
230 nc->ctx_ops->link_event(nc->ctx);
231 rm_runlock(&nc->ctx_lock, &ctx_tracker);
236 ntb_db_event(device_t dev, uint32_t vec)
238 struct ntb_child **cpp = device_get_softc(dev);
239 struct ntb_child *nc;
240 struct rm_priotracker ctx_tracker;
242 for (nc = *cpp; nc != NULL; nc = nc->next) {
243 rm_rlock(&nc->ctx_lock, &ctx_tracker);
244 if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
245 nc->ctx_ops->db_event(nc->ctx, vec);
246 rm_runlock(&nc->ctx_lock, &ctx_tracker);
251 ntb_port_number(device_t ntb)
253 return (NTB_PORT_NUMBER(device_get_parent(ntb)));
257 ntb_peer_port_count(device_t ntb)
259 return (NTB_PEER_PORT_COUNT(device_get_parent(ntb)));
263 ntb_peer_port_number(device_t ntb, int pidx)
265 return (NTB_PEER_PORT_NUMBER(device_get_parent(ntb), pidx));
269 ntb_peer_port_idx(device_t ntb, int port)
271 return (NTB_PEER_PORT_IDX(device_get_parent(ntb), port));
275 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
278 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
282 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
284 struct ntb_child *nc = device_get_ivars(ntb);
285 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
286 struct ntb_child *nc1;
288 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
295 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
299 ntb_link_disable(device_t ntb)
301 struct ntb_child *nc = device_get_ivars(ntb);
302 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
303 struct ntb_child *nc1;
308 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
312 return (NTB_LINK_DISABLE(device_get_parent(ntb)));
316 ntb_link_enabled(device_t ntb)
318 struct ntb_child *nc = device_get_ivars(ntb);
320 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
324 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
326 struct ntb_child *nc = device_get_ivars(ntb);
328 if (ctx == NULL || ctx_ops == NULL)
331 rm_wlock(&nc->ctx_lock);
332 if (nc->ctx_ops != NULL) {
333 rm_wunlock(&nc->ctx_lock);
337 nc->ctx_ops = ctx_ops;
340 * If applicaiton driver asks for link events, generate fake one now
341 * to let it update link state without races while we hold the lock.
343 if (ctx_ops->link_event != NULL)
344 ctx_ops->link_event(ctx);
345 rm_wunlock(&nc->ctx_lock);
351 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
353 struct ntb_child *nc = device_get_ivars(ntb);
355 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
357 *ctx_ops = nc->ctx_ops;
362 ntb_clear_ctx(device_t ntb)
364 struct ntb_child *nc = device_get_ivars(ntb);
366 rm_wlock(&nc->ctx_lock);
369 rm_wunlock(&nc->ctx_lock);
373 ntb_mw_count(device_t ntb)
375 struct ntb_child *nc = device_get_ivars(ntb);
381 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
382 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
385 struct ntb_child *nc = device_get_ivars(ntb);
387 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
388 base, vbase, size, align, align_size, plimit));
392 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
394 struct ntb_child *nc = device_get_ivars(ntb);
396 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
401 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
403 struct ntb_child *nc = device_get_ivars(ntb);
405 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
409 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
411 struct ntb_child *nc = device_get_ivars(ntb);
413 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
417 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
419 struct ntb_child *nc = device_get_ivars(ntb);
421 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
425 ntb_spad_count(device_t ntb)
427 struct ntb_child *nc = device_get_ivars(ntb);
429 return (nc->spadcnt);
433 ntb_spad_clear(device_t ntb)
435 struct ntb_child *nc = device_get_ivars(ntb);
438 for (i = 0; i < nc->spadcnt; i++)
439 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
443 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
445 struct ntb_child *nc = device_get_ivars(ntb);
447 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
451 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
453 struct ntb_child *nc = device_get_ivars(ntb);
455 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
459 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
461 struct ntb_child *nc = device_get_ivars(ntb);
463 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
468 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
470 struct ntb_child *nc = device_get_ivars(ntb);
472 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
477 ntb_db_valid_mask(device_t ntb)
479 struct ntb_child *nc = device_get_ivars(ntb);
485 ntb_db_vector_count(device_t ntb)
488 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
492 ntb_db_vector_mask(device_t ntb, uint32_t vector)
494 struct ntb_child *nc = device_get_ivars(ntb);
496 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
497 >> nc->dboff) & nc->dbmask);
501 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
504 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
508 ntb_db_clear(device_t ntb, uint64_t bits)
510 struct ntb_child *nc = device_get_ivars(ntb);
512 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
516 ntb_db_clear_mask(device_t ntb, uint64_t bits)
518 struct ntb_child *nc = device_get_ivars(ntb);
520 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
524 ntb_db_read(device_t ntb)
526 struct ntb_child *nc = device_get_ivars(ntb);
528 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
533 ntb_db_set_mask(device_t ntb, uint64_t bits)
535 struct ntb_child *nc = device_get_ivars(ntb);
537 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
541 ntb_peer_db_set(device_t ntb, uint64_t bits)
543 struct ntb_child *nc = device_get_ivars(ntb);
545 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
548 MODULE_VERSION(ntb, 1);