]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ntb/ntb.c
MFC r323074: Clear doorbell bits after masking them before processing.
[FreeBSD/FreeBSD.git] / sys / dev / ntb / ntb.c
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$");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/rmlock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
38
39 #include "ntb.h"
40
41 devclass_t ntb_hw_devclass;
42 SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW, 0, "NTB sysctls");
43
44 struct ntb_child {
45         device_t        dev;
46         int             enabled;
47         int             mwoff;
48         int             mwcnt;
49         int             spadoff;
50         int             spadcnt;
51         int             dboff;
52         int             dbmask;
53         void            *ctx;
54         const struct ntb_ctx_ops *ctx_ops;
55         struct rmlock   ctx_lock;
56         struct ntb_child *next;
57 };
58
59 int
60 ntb_register_device(device_t dev)
61 {
62         struct ntb_child **cpp = device_get_softc(dev);
63         struct ntb_child *nc;
64         int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt;
65         char cfg[128] = "";
66         char buf[32];
67         char *n, *np, *c, *p, *name;
68
69         mwu = 0;
70         mwt = NTB_MW_COUNT(dev);
71         spadu = 0;
72         spadt = NTB_SPAD_COUNT(dev);
73         dbu = 0;
74         dbt = flsll(NTB_DB_VALID_MASK(dev));
75
76         device_printf(dev, "%d memory windows, %d scratchpads, "
77             "%d doorbells\n", mwt, spadt, dbt);
78
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));
82         n = cfg;
83         i = 0;
84         while ((c = strsep(&n, ",")) != NULL) {
85                 np = c;
86                 name = strsep(&np, ":");
87                 if (name != NULL && name[0] == 0)
88                         name = NULL;
89                 p = strsep(&np, ":");
90                 mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu;
91                 p = strsep(&np, ":");
92                 spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu;
93                 db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu;
94
95                 if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) {
96                         device_printf(dev, "Not enough resources for config\n");
97                         break;
98                 }
99
100                 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
101                 nc->mwoff = mwu;
102                 nc->mwcnt = mw;
103                 nc->spadoff = spadu;
104                 nc->spadcnt = spad;
105                 nc->dboff = dbu;
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);
111                         return (ENOMEM);
112                 }
113                 device_set_ivars(nc->dev, nc);
114                 *cpp = nc;
115                 cpp = &nc->next;
116
117                 if (bootverbose) {
118                         device_printf(dev, "%d \"%s\":", i, name);
119                         if (mw > 0) {
120                                 printf(" memory windows %d", mwu);
121                                 if (mw > 1)
122                                         printf("-%d", mwu + mw - 1);
123                         }
124                         if (spad > 0) {
125                                 printf(" scratchpads %d", spadu);
126                                 if (spad > 1)
127                                         printf("-%d", spadu + spad - 1);
128                         }
129                         if (db > 0) {
130                                 printf(" doorbells %d", dbu);
131                                 if (db > 1)
132                                         printf("-%d", dbu + db - 1);
133                         }
134                         printf("\n");
135                 }
136
137                 mwu += mw;
138                 spadu += spad;
139                 dbu += db;
140                 i++;
141         }
142
143         bus_generic_attach(dev);
144         return (0);
145 }
146
147 int
148 ntb_unregister_device(device_t dev)
149 {
150         struct ntb_child **cpp = device_get_softc(dev);
151         struct ntb_child *nc;
152         int error = 0;
153
154         while ((nc = *cpp) != NULL) {
155                 *cpp = (*cpp)->next;
156                 error = device_delete_child(dev, nc->dev);
157                 if (error)
158                         break;
159                 rm_destroy(&nc->ctx_lock);
160                 free(nc, M_DEVBUF);
161         }
162         return (error);
163 }
164
165 void
166 ntb_link_event(device_t dev)
167 {
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;
173
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);
177         } else {
178                 device_printf(dev, "Link is down\n");
179         }
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);
185         }
186 }
187
188 void
189 ntb_db_event(device_t dev, uint32_t vec)
190 {
191         struct ntb_child **cpp = device_get_softc(dev);
192         struct ntb_child *nc;
193         struct rm_priotracker ctx_tracker;
194
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);
200         }
201 }
202
203 bool
204 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width)
205 {
206
207         return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width));
208 }
209
210 int
211 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width)
212 {
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;
216
217         for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
218                 if (nc1->enabled) {
219                         nc->enabled = 1;
220                         return (0);
221                 }
222         }
223         nc->enabled = 1;
224         return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width));
225 }
226
227 int
228 ntb_link_disable(device_t ntb)
229 {
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;
233
234         if (!nc->enabled)
235                 return (0);
236         nc->enabled = 0;
237         for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) {
238                 if (nc1->enabled)
239                         return (0);
240         }
241         return (NTB_LINK_DISABLE(device_get_parent(ntb)));
242 }
243
244 bool
245 ntb_link_enabled(device_t ntb)
246 {
247         struct ntb_child *nc = device_get_ivars(ntb);
248
249         return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb)));
250 }
251
252 int
253 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops)
254 {
255         struct ntb_child *nc = device_get_ivars(ntb);
256
257         if (ctx == NULL || ctx_ops == NULL)
258                 return (EINVAL);
259
260         rm_wlock(&nc->ctx_lock);
261         if (nc->ctx_ops != NULL) {
262                 rm_wunlock(&nc->ctx_lock);
263                 return (EINVAL);
264         }
265         nc->ctx = ctx;
266         nc->ctx_ops = ctx_ops;
267
268         /*
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.
271          */
272         if (ctx_ops->link_event != NULL)
273                 ctx_ops->link_event(ctx);
274         rm_wunlock(&nc->ctx_lock);
275
276         return (0);
277 }
278
279 void *
280 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops)
281 {
282         struct ntb_child *nc = device_get_ivars(ntb);
283
284         KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus"));
285         if (ctx_ops != NULL)
286                 *ctx_ops = nc->ctx_ops;
287         return (nc->ctx);
288 }
289
290 void
291 ntb_clear_ctx(device_t ntb)
292 {
293         struct ntb_child *nc = device_get_ivars(ntb);
294
295         rm_wlock(&nc->ctx_lock);
296         nc->ctx = NULL;
297         nc->ctx_ops = NULL;
298         rm_wunlock(&nc->ctx_lock);
299 }
300
301 uint8_t
302 ntb_mw_count(device_t ntb)
303 {
304         struct ntb_child *nc = device_get_ivars(ntb);
305
306         return (nc->mwcnt);
307 }
308
309 int
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,
312     bus_addr_t *plimit)
313 {
314         struct ntb_child *nc = device_get_ivars(ntb);
315
316         return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff,
317             base, vbase, size, align, align_size, plimit));
318 }
319
320 int
321 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size)
322 {
323         struct ntb_child *nc = device_get_ivars(ntb);
324
325         return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff,
326             addr, size));
327 }
328
329 int
330 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx)
331 {
332         struct ntb_child *nc = device_get_ivars(ntb);
333
334         return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff));
335 }
336
337 int
338 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode)
339 {
340         struct ntb_child *nc = device_get_ivars(ntb);
341
342         return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
343 }
344
345 int
346 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode)
347 {
348         struct ntb_child *nc = device_get_ivars(ntb);
349
350         return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode));
351 }
352
353 uint8_t
354 ntb_spad_count(device_t ntb)
355 {
356         struct ntb_child *nc = device_get_ivars(ntb);
357
358         return (nc->spadcnt);
359 }
360
361 void
362 ntb_spad_clear(device_t ntb)
363 {
364         struct ntb_child *nc = device_get_ivars(ntb);
365         unsigned i;
366
367         for (i = 0; i < nc->spadcnt; i++)
368                 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0);
369 }
370
371 int
372 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val)
373 {
374         struct ntb_child *nc = device_get_ivars(ntb);
375
376         return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val));
377 }
378
379 int
380 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
381 {
382         struct ntb_child *nc = device_get_ivars(ntb);
383
384         return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val));
385 }
386
387 int
388 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val)
389 {
390         struct ntb_child *nc = device_get_ivars(ntb);
391
392         return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff,
393             val));
394 }
395
396 int
397 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val)
398 {
399         struct ntb_child *nc = device_get_ivars(ntb);
400
401         return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff,
402             val));
403 }
404
405 uint64_t
406 ntb_db_valid_mask(device_t ntb)
407 {
408         struct ntb_child *nc = device_get_ivars(ntb);
409
410         return (nc->dbmask);
411 }
412
413 int
414 ntb_db_vector_count(device_t ntb)
415 {
416
417         return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb)));
418 }
419
420 uint64_t
421 ntb_db_vector_mask(device_t ntb, uint32_t vector)
422 {
423         struct ntb_child *nc = device_get_ivars(ntb);
424
425         return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector)
426             >> nc->dboff) & nc->dbmask);
427 }
428
429 int
430 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size)
431 {
432
433         return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size));
434 }
435
436 void
437 ntb_db_clear(device_t ntb, uint64_t bits)
438 {
439         struct ntb_child *nc = device_get_ivars(ntb);
440
441         return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff));
442 }
443
444 void
445 ntb_db_clear_mask(device_t ntb, uint64_t bits)
446 {
447         struct ntb_child *nc = device_get_ivars(ntb);
448
449         return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff));
450 }
451
452 uint64_t
453 ntb_db_read(device_t ntb)
454 {
455         struct ntb_child *nc = device_get_ivars(ntb);
456
457         return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff)
458             & nc->dbmask);
459 }
460
461 void
462 ntb_db_set_mask(device_t ntb, uint64_t bits)
463 {
464         struct ntb_child *nc = device_get_ivars(ntb);
465
466         return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff));
467 }
468
469 void
470 ntb_peer_db_set(device_t ntb, uint64_t bits)
471 {
472         struct ntb_child *nc = device_get_ivars(ntb);
473
474         return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff));
475 }
476
477 MODULE_VERSION(ntb, 1);