1 /*-
2 * Copyright (c) 2016 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 __FBSDID("$FreeBSD: stable/10/sys/dev/ntb/ntb.c 304404 2016-08-18 10:59:12Z mav $");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <machine/bus.h>
35 #include <sys/rmlock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39
40 #include "ntb.h"
41
42 devclass_t ntb_hw_devclass;
43 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW, 0, "NTB sysctls");
44
45 struct ntb_child {
46 device_t dev;
47 int enabled;
48 int mwoff;
49 int mwcnt;
50 int spadoff;
51 int spadcnt;
52 int dboff;
53 int 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->mwoff = mwu;
103 nc->mwcnt = mw;
104 nc->spadoff = spadu;
105 nc->spadcnt = spad;
106 nc->dboff = dbu;
107 nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db));
108 rm_init(&nc->ctx_lock, "ntb ctx");
109 nc->dev = device_add_child(dev, name, -1);
110 if (nc->dev == NULL) {
111 ntb_unregister_device(dev);
112 return (ENOMEM);
113 }
114 device_set_ivars(nc->dev, nc);
115 *cpp = nc;
116 cpp = &nc->next;
117
118 if (bootverbose) {
119 device_printf(dev, "%d \"%s\":", i, name);
120 if (mw > 0) {
121 printf(" memory windows %d", mwu);
122 if (mw > 1)
123 printf("-%d", mwu + mw - 1);
124 }
125 if (spad > 0) {
126 printf(" scratchpads %d", spadu);
127 if (spad > 1)
128 printf("-%d", spadu + spad - 1);
129 }
130 if (db > 0) {
131 printf(" doorbells %d", dbu);
132 if (db > 1)
133 printf("-%d", dbu + db - 1);
134 }
135 printf("\n");
136 }
137
138 mwu += mw;
139 spadu += spad;
140 dbu += db;
141 i++;
142 }
143
144 bus_generic_attach(dev);
145 return (0);
146 }
147
148 int
ntb_unregister_device(device_t dev)149 ntb_unregister_device(device_t dev)
150 {
151 struct ntb_child **cpp = device_get_softc(dev);
152 struct ntb_child *nc;
153 int error = 0;
154
155 while ((nc = *cpp) != NULL) {
156 *cpp = (*cpp)->next;
157 error = device_delete_child(dev, nc->dev);
158 if (error)
159 break;
160 rm_destroy(&nc->ctx_lock);
161 free(nc, M_DEVBUF);
162 }
163 return (error);
164 }
165
166 void
ntb_link_event(device_t dev)167 ntb_link_event(device_t dev)
168 {
169 struct ntb_child **cpp = device_get_softc(dev);
170 struct ntb_child *nc;
171 struct rm_priotracker ctx_tracker;
172
173 for (nc = *cpp; nc != NULL; nc = nc->next) {
174 rm_rlock(&nc->ctx_lock, &ctx_tracker);
175 if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL)
176 nc->ctx_ops->link_event(nc->ctx);
177 rm_runlock(&nc->ctx_lock, &ctx_tracker);
178 }
179 }
180
181 void
ntb_db_event(device_t dev,uint32_t vec)182 ntb_db_event(device_t dev, uint32_t vec)
183 {
184 struct ntb_child **cpp = device_get_softc(dev);
185 struct ntb_child *nc;
186 struct rm_priotracker ctx_tracker;
187
188 for (nc = *cpp; nc != NULL; nc = nc->next) {
189 rm_rlock(&nc->ctx_lock, &ctx_tracker);
190 if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL)
191 nc->ctx_ops->db_event(nc->ctx, vec);
192 rm_runlock(&nc->ctx_lock, &ctx_tracker);
193 }
194 }
195
196 bool
ntb_link_is_up(device_t ntb,enum ntb_speed * speed,enum ntb_width * width)197 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
198 {
199
200 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
201 }
202
203 int
ntb_link_enable(device_t ntb,enum ntb_speed speed,enum ntb_width width)204 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
205 {
206 struct ntb_child *nc = device_get_ivars(ntb);
207 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
208 struct ntb_child *nc1;
209
210 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
211 if (nc1->enabled) {
212 nc->enabled = 1;
213 return (0);
214 }
215 }
216 nc->enabled = 1;
217 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
218 }
219
220 int
ntb_link_disable(device_t ntb)221 ntb_link_disable(device_t ntb)
222 {
223 struct ntb_child *nc = device_get_ivars(ntb);
224 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev));
225 struct ntb_child *nc1;
226
227 if (!nc->enabled)
228 return (0);
229 nc->enabled = 0;
230 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
231 if (nc1->enabled)
232 return (0);
233 }
234 return (NTB_LINK_DISABLE(device_get_parent(ntb)));
235 }
236
237 bool
ntb_link_enabled(device_t ntb)238 ntb_link_enabled(device_t ntb)
239 {
240 struct ntb_child *nc = device_get_ivars(ntb);
241
242 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
243 }
244
245 int
ntb_set_ctx(device_t ntb,void * ctx,const struct ntb_ctx_ops * ctx_ops)246 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
247 {
248 struct ntb_child *nc = device_get_ivars(ntb);
249
250 if (ctx == NULL || ctx_ops == NULL)
251 return (EINVAL);
252
253 rm_wlock(&nc->ctx_lock);
254 if (nc->ctx_ops != NULL) {
255 rm_wunlock(&nc->ctx_lock);
256 return (EINVAL);
257 }
258 nc->ctx = ctx;
259 nc->ctx_ops = ctx_ops;
260 rm_wunlock(&nc->ctx_lock);
261
262 return (0);
263 }
264
265 void *
ntb_get_ctx(device_t ntb,const struct ntb_ctx_ops ** ctx_ops)266 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
267 {
268 struct ntb_child *nc = device_get_ivars(ntb);
269
270 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
271 if (ctx_ops != NULL)
272 *ctx_ops = nc->ctx_ops;
273 return (nc->ctx);
274 }
275
276 void
ntb_clear_ctx(device_t ntb)277 ntb_clear_ctx(device_t ntb)
278 {
279 struct ntb_child *nc = device_get_ivars(ntb);
280
281 rm_wlock(&nc->ctx_lock);
282 nc->ctx = NULL;
283 nc->ctx_ops = NULL;
284 rm_wunlock(&nc->ctx_lock);
285 }
286
287 uint8_t
ntb_mw_count(device_t ntb)288 ntb_mw_count(device_t ntb)
289 {
290 struct ntb_child *nc = device_get_ivars(ntb);
291
292 return (nc->mwcnt);
293 }
294
295 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)296 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base,
297 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size,
298 bus_addr_t *plimit)
299 {
300 struct ntb_child *nc = device_get_ivars(ntb);
301
302 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
303 base, vbase, size, align, align_size, plimit));
304 }
305
306 int
ntb_mw_set_trans(device_t ntb,unsigned mw_idx,bus_addr_t addr,size_t size)307 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
308 {
309 struct ntb_child *nc = device_get_ivars(ntb);
310
311 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
312 addr, size));
313 }
314
315 int
ntb_mw_clear_trans(device_t ntb,unsigned mw_idx)316 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
317 {
318 struct ntb_child *nc = device_get_ivars(ntb);
319
320 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
321 }
322
323 int
ntb_mw_get_wc(device_t ntb,unsigned mw_idx,vm_memattr_t * mode)324 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
325 {
326 struct ntb_child *nc = device_get_ivars(ntb);
327
328 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
329 }
330
331 int
ntb_mw_set_wc(device_t ntb,unsigned mw_idx,vm_memattr_t mode)332 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
333 {
334 struct ntb_child *nc = device_get_ivars(ntb);
335
336 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
337 }
338
339 uint8_t
ntb_spad_count(device_t ntb)340 ntb_spad_count(device_t ntb)
341 {
342 struct ntb_child *nc = device_get_ivars(ntb);
343
344 return (nc->spadcnt);
345 }
346
347 void
ntb_spad_clear(device_t ntb)348 ntb_spad_clear(device_t ntb)
349 {
350 struct ntb_child *nc = device_get_ivars(ntb);
351 unsigned i;
352
353 for (i = 0; i < nc->spadcnt; i++)
354 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
355 }
356
357 int
ntb_spad_write(device_t ntb,unsigned int idx,uint32_t val)358 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
359 {
360 struct ntb_child *nc = device_get_ivars(ntb);
361
362 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
363 }
364
365 int
ntb_spad_read(device_t ntb,unsigned int idx,uint32_t * val)366 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
367 {
368 struct ntb_child *nc = device_get_ivars(ntb);
369
370 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
371 }
372
373 int
ntb_peer_spad_write(device_t ntb,unsigned int idx,uint32_t val)374 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
375 {
376 struct ntb_child *nc = device_get_ivars(ntb);
377
378 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
379 val));
380 }
381
382 int
ntb_peer_spad_read(device_t ntb,unsigned int idx,uint32_t * val)383 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
384 {
385 struct ntb_child *nc = device_get_ivars(ntb);
386
387 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
388 val));
389 }
390
391 uint64_t
ntb_db_valid_mask(device_t ntb)392 ntb_db_valid_mask(device_t ntb)
393 {
394 struct ntb_child *nc = device_get_ivars(ntb);
395
396 return (nc->dbmask);
397 }
398
399 int
ntb_db_vector_count(device_t ntb)400 ntb_db_vector_count(device_t ntb)
401 {
402
403 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
404 }
405
406 uint64_t
ntb_db_vector_mask(device_t ntb,uint32_t vector)407 ntb_db_vector_mask(device_t ntb, uint32_t vector)
408 {
409 struct ntb_child *nc = device_get_ivars(ntb);
410
411 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
412 >> nc->dboff) & nc->dbmask);
413 }
414
415 int
ntb_peer_db_addr(device_t ntb,bus_addr_t * db_addr,vm_size_t * db_size)416 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
417 {
418
419 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
420 }
421
422 void
ntb_db_clear(device_t ntb,uint64_t bits)423 ntb_db_clear(device_t ntb, uint64_t bits)
424 {
425 struct ntb_child *nc = device_get_ivars(ntb);
426
427 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
428 }
429
430 void
ntb_db_clear_mask(device_t ntb,uint64_t bits)431 ntb_db_clear_mask(device_t ntb, uint64_t bits)
432 {
433 struct ntb_child *nc = device_get_ivars(ntb);
434
435 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
436 }
437
438 uint64_t
ntb_db_read(device_t ntb)439 ntb_db_read(device_t ntb)
440 {
441 struct ntb_child *nc = device_get_ivars(ntb);
442
443 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
444 & nc->dbmask);
445 }
446
447 void
ntb_db_set_mask(device_t ntb,uint64_t bits)448 ntb_db_set_mask(device_t ntb, uint64_t bits)
449 {
450 struct ntb_child *nc = device_get_ivars(ntb);
451
452 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
453 }
454
455 void
ntb_peer_db_set(device_t ntb,uint64_t bits)456 ntb_peer_db_set(device_t ntb, uint64_t bits)
457 {
458 struct ntb_child *nc = device_get_ivars(ntb);
459
460 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
461 }
462
463 MODULE_VERSION(ntb, 1);
464