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 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
32 #include <sys/rmlock.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
36 #include <sys/sysctl.h>
40 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
55 const struct ntb_ctx_ops *ctx_ops;
56 struct rmlock ctx_lock;
57 struct ntb_child *next;
61 ntb_register_device(device_t dev)
63 struct ntb_child **cpp = device_get_softc(dev);
65 int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
68 char *n, *np, *c, *p, *name;
71 mwt = NTB_MW_COUNT(dev);
73 spadt = NTB_SPAD_COUNT(dev);
75 dbt = flsll(NTB_DB_VALID_MASK(dev));
77 device_printf(dev, "%d memory windows, %d scratchpads, "
78 "%d doorbells\n", mwt, spadt, dbt);
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));
85 while ((c = strsep(&n, ",")) != NULL) {
87 name = strsep(&np, ":");
88 if (name != NULL && name[0] == 0)
91 mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
93 spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
94 db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
96 if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
97 device_printf(dev, "Not enough resources for config\n");
101 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
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);
116 device_set_ivars(nc->dev, nc);
121 device_printf(dev, "%d \"%s\":", i, name);
123 printf(" memory windows %d", mwu);
125 printf("-%d", mwu + mw - 1);
128 printf(" scratchpads %d", spadu);
130 printf("-%d", spadu + spad - 1);
133 printf(" doorbells %d", dbu);
135 printf("-%d", dbu + db - 1);
146 bus_generic_attach(dev);
151 ntb_unregister_device(device_t dev)
153 struct ntb_child **cpp = device_get_softc(dev);
154 struct ntb_child *nc;
157 while ((nc = *cpp) != NULL) {
159 error = device_delete_child(dev, nc->dev);
162 rm_destroy(&nc->ctx_lock);
169 ntb_child_location(device_t dev, device_t child, struct sbuf *sb)
171 struct ntb_child *nc = device_get_ivars(child);
173 sbuf_printf(sb, "function=%d", nc->function);
178 ntb_print_child(device_t dev, device_t child)
180 struct ntb_child *nc = device_get_ivars(child);
183 retval = bus_print_child_header(dev, child);
185 printf(" mw %d", nc->mwoff);
187 printf("-%d", nc->mwoff + nc->mwcnt - 1);
189 if (nc->spadcnt > 0) {
190 printf(" spad %d", nc->spadoff);
192 printf("-%d", nc->spadoff + nc->spadcnt - 1);
195 printf(" db %d", nc->dboff);
197 printf("-%d", nc->dboff + nc->dbcnt - 1);
199 retval += printf(" at function %d", nc->function);
200 retval += bus_print_child_domain(dev, child);
201 retval += bus_print_child_footer(dev, child);
207 ntb_get_dma_tag(device_t bus, device_t child)
210 return (bus_get_dma_tag(bus));
214 ntb_link_event(device_t dev)
216 struct ntb_child **cpp = device_get_softc(dev);
217 struct ntb_child *nc;
218 struct rm_priotracker ctx_tracker;
219 enum ntb_speed speed;
220 enum ntb_width width;
222 if (NTB_LINK_IS_UP(dev, &speed, &width)) {
223 device_printf(dev, "Link is up (PCIe %d.x / x%d)\n",
224 (int)speed, (int)width);
226 device_printf(dev, "Link is down\n");
228 for (nc = *cpp; nc != NULL; nc = nc->next) {
229 rm_rlock(&nc->ctx_lock, &ctx_tracker);
230 if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
231 nc->ctx_ops->link_event(nc->ctx);
232 rm_runlock(&nc->ctx_lock, &ctx_tracker);
237 ntb_db_event(device_t dev, uint32_t vec)
239 struct ntb_child **cpp = device_get_softc(dev);
240 struct ntb_child *nc;
241 struct rm_priotracker ctx_tracker;
243 for (nc = *cpp; nc != NULL; nc = nc->next) {
244 rm_rlock(&nc->ctx_lock, &ctx_tracker);
245 if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
246 nc->ctx_ops->db_event(nc->ctx, vec);
247 rm_runlock(&nc->ctx_lock, &ctx_tracker);
252 ntb_port_number(device_t ntb)
254 return (NTB_PORT_NUMBER(device_get_parent(ntb)));
258 ntb_peer_port_count(device_t ntb)
260 return (NTB_PEER_PORT_COUNT(device_get_parent(ntb)));
264 ntb_peer_port_number(device_t ntb, int pidx)
266 return (NTB_PEER_PORT_NUMBER(device_get_parent(ntb), pidx));
270 ntb_peer_port_idx(device_t ntb, int port)
272 return (NTB_PEER_PORT_IDX(device_get_parent(ntb), port));
276 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
279 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
283 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
285 struct ntb_child *nc = device_get_ivars(ntb);
286 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
287 struct ntb_child *nc1;
289 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
296 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
300 ntb_link_disable(device_t ntb)
302 struct ntb_child *nc = device_get_ivars(ntb);
303 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
304 struct ntb_child *nc1;
309 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
313 return (NTB_LINK_DISABLE(device_get_parent(ntb)));
317 ntb_link_enabled(device_t ntb)
319 struct ntb_child *nc = device_get_ivars(ntb);
321 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
325 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
327 struct ntb_child *nc = device_get_ivars(ntb);
329 if (ctx == NULL || ctx_ops == NULL)
332 rm_wlock(&nc->ctx_lock);
333 if (nc->ctx_ops != NULL) {
334 rm_wunlock(&nc->ctx_lock);
338 nc->ctx_ops = ctx_ops;
341 * If applicaiton driver asks for link events, generate fake one now
342 * to let it update link state without races while we hold the lock.
344 if (ctx_ops->link_event != NULL)
345 ctx_ops->link_event(ctx);
346 rm_wunlock(&nc->ctx_lock);
352 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
354 struct ntb_child *nc = device_get_ivars(ntb);
356 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
358 *ctx_ops = nc->ctx_ops;
363 ntb_clear_ctx(device_t ntb)
365 struct ntb_child *nc = device_get_ivars(ntb);
367 rm_wlock(&nc->ctx_lock);
370 rm_wunlock(&nc->ctx_lock);
374 ntb_mw_count(device_t ntb)
376 struct ntb_child *nc = device_get_ivars(ntb);
382 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
383 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
386 struct ntb_child *nc = device_get_ivars(ntb);
388 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
389 base, vbase, size, align, align_size, plimit));
393 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
395 struct ntb_child *nc = device_get_ivars(ntb);
397 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
402 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
404 struct ntb_child *nc = device_get_ivars(ntb);
406 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
410 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
412 struct ntb_child *nc = device_get_ivars(ntb);
414 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
418 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
420 struct ntb_child *nc = device_get_ivars(ntb);
422 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
426 ntb_spad_count(device_t ntb)
428 struct ntb_child *nc = device_get_ivars(ntb);
430 return (nc->spadcnt);
434 ntb_spad_clear(device_t ntb)
436 struct ntb_child *nc = device_get_ivars(ntb);
439 for (i = 0; i < nc->spadcnt; i++)
440 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
444 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
446 struct ntb_child *nc = device_get_ivars(ntb);
448 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
452 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
454 struct ntb_child *nc = device_get_ivars(ntb);
456 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
460 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
462 struct ntb_child *nc = device_get_ivars(ntb);
464 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
469 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
471 struct ntb_child *nc = device_get_ivars(ntb);
473 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
478 ntb_db_valid_mask(device_t ntb)
480 struct ntb_child *nc = device_get_ivars(ntb);
486 ntb_db_vector_count(device_t ntb)
489 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
493 ntb_db_vector_mask(device_t ntb, uint32_t vector)
495 struct ntb_child *nc = device_get_ivars(ntb);
497 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
498 >> nc->dboff) & nc->dbmask);
502 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
505 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
509 ntb_db_clear(device_t ntb, uint64_t bits)
511 struct ntb_child *nc = device_get_ivars(ntb);
513 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
517 ntb_db_clear_mask(device_t ntb, uint64_t bits)
519 struct ntb_child *nc = device_get_ivars(ntb);
521 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
525 ntb_db_read(device_t ntb)
527 struct ntb_child *nc = device_get_ivars(ntb);
529 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
534 ntb_db_set_mask(device_t ntb, uint64_t bits)
536 struct ntb_child *nc = device_get_ivars(ntb);
538 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
542 ntb_peer_db_set(device_t ntb, uint64_t bits)
544 struct ntb_child *nc = device_get_ivars(ntb);
546 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
549 MODULE_VERSION(ntb, 1);