2 * Copyright (c) 2016 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");
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);
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);
113 device_set_ivars(nc->dev, nc);
118 device_printf(dev, "%d \"%s\":", i, name);
120 printf(" memory windows %d", mwu);
122 printf("-%d", mwu + mw - 1);
125 printf(" scratchpads %d", spadu);
127 printf("-%d", spadu + spad - 1);
130 printf(" doorbells %d", dbu);
132 printf("-%d", dbu + db - 1);
143 bus_generic_attach(dev);
148 ntb_unregister_device(device_t dev)
150 struct ntb_child **cpp = device_get_softc(dev);
151 struct ntb_child *nc;
154 while ((nc = *cpp) != NULL) {
156 error = device_delete_child(dev, nc->dev);
159 rm_destroy(&nc->ctx_lock);
166 ntb_link_event(device_t dev)
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;
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);
178 device_printf(dev, "Link is down\n");
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);
189 ntb_db_event(device_t dev, uint32_t vec)
191 struct ntb_child **cpp = device_get_softc(dev);
192 struct ntb_child *nc;
193 struct rm_priotracker ctx_tracker;
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);
204 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
207 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
211 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
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;
217 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
224 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
228 ntb_link_disable(device_t ntb)
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;
237 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
241 return (NTB_LINK_DISABLE(device_get_parent(ntb)));
245 ntb_link_enabled(device_t ntb)
247 struct ntb_child *nc = device_get_ivars(ntb);
249 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
253 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
255 struct ntb_child *nc = device_get_ivars(ntb);
257 if (ctx == NULL || ctx_ops == NULL)
260 rm_wlock(&nc->ctx_lock);
261 if (nc->ctx_ops != NULL) {
262 rm_wunlock(&nc->ctx_lock);
266 nc->ctx_ops = ctx_ops;
269 * If applicaiton driver asks for link events, generate fake one now
270 * to let it update link state without races while we hold the lock.
272 if (ctx_ops->link_event != NULL)
273 ctx_ops->link_event(ctx);
274 rm_wunlock(&nc->ctx_lock);
280 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
282 struct ntb_child *nc = device_get_ivars(ntb);
284 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
286 *ctx_ops = nc->ctx_ops;
291 ntb_clear_ctx(device_t ntb)
293 struct ntb_child *nc = device_get_ivars(ntb);
295 rm_wlock(&nc->ctx_lock);
298 rm_wunlock(&nc->ctx_lock);
302 ntb_mw_count(device_t ntb)
304 struct ntb_child *nc = device_get_ivars(ntb);
310 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
311 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
314 struct ntb_child *nc = device_get_ivars(ntb);
316 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
317 base, vbase, size, align, align_size, plimit));
321 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
323 struct ntb_child *nc = device_get_ivars(ntb);
325 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
330 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
332 struct ntb_child *nc = device_get_ivars(ntb);
334 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
338 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
340 struct ntb_child *nc = device_get_ivars(ntb);
342 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
346 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
348 struct ntb_child *nc = device_get_ivars(ntb);
350 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
354 ntb_spad_count(device_t ntb)
356 struct ntb_child *nc = device_get_ivars(ntb);
358 return (nc->spadcnt);
362 ntb_spad_clear(device_t ntb)
364 struct ntb_child *nc = device_get_ivars(ntb);
367 for (i = 0; i < nc->spadcnt; i++)
368 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
372 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
374 struct ntb_child *nc = device_get_ivars(ntb);
376 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
380 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
382 struct ntb_child *nc = device_get_ivars(ntb);
384 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
388 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
390 struct ntb_child *nc = device_get_ivars(ntb);
392 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
397 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
399 struct ntb_child *nc = device_get_ivars(ntb);
401 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
406 ntb_db_valid_mask(device_t ntb)
408 struct ntb_child *nc = device_get_ivars(ntb);
414 ntb_db_vector_count(device_t ntb)
417 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
421 ntb_db_vector_mask(device_t ntb, uint32_t vector)
423 struct ntb_child *nc = device_get_ivars(ntb);
425 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
426 >> nc->dboff) & nc->dbmask);
430 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
433 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
437 ntb_db_clear(device_t ntb, uint64_t bits)
439 struct ntb_child *nc = device_get_ivars(ntb);
441 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
445 ntb_db_clear_mask(device_t ntb, uint64_t bits)
447 struct ntb_child *nc = device_get_ivars(ntb);
449 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
453 ntb_db_read(device_t ntb)
455 struct ntb_child *nc = device_get_ivars(ntb);
457 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
462 ntb_db_set_mask(device_t ntb, uint64_t bits)
464 struct ntb_child *nc = device_get_ivars(ntb);
466 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
470 ntb_peer_db_set(device_t ntb, uint64_t bits)
472 struct ntb_child *nc = device_get_ivars(ntb);
474 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
477 MODULE_VERSION(ntb, 1);