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 172 for (nc = *cpp; nc != NULL; nc = nc->next) { 173 rm_rlock(&nc->ctx_lock, &ctx_tracker); 174 if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL) 175 nc->ctx_ops->link_event(nc->ctx); 176 rm_runlock(&nc->ctx_lock, &ctx_tracker); 177 } 178 } 179 180 void 181 ntb_db_event(device_t dev, uint32_t vec) 182 { 183 struct ntb_child **cpp = device_get_softc(dev); 184 struct ntb_child *nc; 185 struct rm_priotracker ctx_tracker; 186 187 for (nc = *cpp; nc != NULL; nc = nc->next) { 188 rm_rlock(&nc->ctx_lock, &ctx_tracker); 189 if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL) 190 nc->ctx_ops->db_event(nc->ctx, vec); 191 rm_runlock(&nc->ctx_lock, &ctx_tracker); 192 } 193 } 194 195 bool 196 ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width) 197 { 198 199 return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width)); 200 } 201 202 int 203 ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width) 204 { 205 struct ntb_child *nc = device_get_ivars(ntb); 206 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev)); 207 struct ntb_child *nc1; 208 209 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) { 210 if (nc1->enabled) { 211 nc->enabled = 1; 212 return (0); 213 } 214 } 215 nc->enabled = 1; 216 return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width)); 217 } 218 219 int 220 ntb_link_disable(device_t ntb) 221 { 222 struct ntb_child *nc = device_get_ivars(ntb); 223 struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev)); 224 struct ntb_child *nc1; 225 226 if (!nc->enabled) 227 return (0); 228 nc->enabled = 0; 229 for (nc1 = *cpp; nc1 != NULL; nc1 = nc1->next) { 230 if (nc1->enabled) 231 return (0); 232 } 233 return (NTB_LINK_DISABLE(device_get_parent(ntb))); 234 } 235 236 bool 237 ntb_link_enabled(device_t ntb) 238 { 239 struct ntb_child *nc = device_get_ivars(ntb); 240 241 return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb))); 242 } 243 244 int 245 ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops) 246 { 247 struct ntb_child *nc = device_get_ivars(ntb); 248 249 if (ctx == NULL || ctx_ops == NULL) 250 return (EINVAL); 251 252 rm_wlock(&nc->ctx_lock); 253 if (nc->ctx_ops != NULL) { 254 rm_wunlock(&nc->ctx_lock); 255 return (EINVAL); 256 } 257 nc->ctx = ctx; 258 nc->ctx_ops = ctx_ops; 259 rm_wunlock(&nc->ctx_lock); 260 261 return (0); 262 } 263 264 void * 265 ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops) 266 { 267 struct ntb_child *nc = device_get_ivars(ntb); 268 269 KASSERT(nc->ctx != NULL && nc->ctx_ops != NULL, ("bogus")); 270 if (ctx_ops != NULL) 271 *ctx_ops = nc->ctx_ops; 272 return (nc->ctx); 273 } 274 275 void 276 ntb_clear_ctx(device_t ntb) 277 { 278 struct ntb_child *nc = device_get_ivars(ntb); 279 280 rm_wlock(&nc->ctx_lock); 281 nc->ctx = NULL; 282 nc->ctx_ops = NULL; 283 rm_wunlock(&nc->ctx_lock); 284 } 285 286 uint8_t 287 ntb_mw_count(device_t ntb) 288 { 289 struct ntb_child *nc = device_get_ivars(ntb); 290 291 return (nc->mwcnt); 292 } 293 294 int 295 ntb_mw_get_range(device_t ntb, unsigned mw_idx, vm_paddr_t *base, 296 caddr_t *vbase, size_t *size, size_t *align, size_t *align_size, 297 bus_addr_t *plimit) 298 { 299 struct ntb_child *nc = device_get_ivars(ntb); 300 301 return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff, 302 base, vbase, size, align, align_size, plimit)); 303 } 304 305 int 306 ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size) 307 { 308 struct ntb_child *nc = device_get_ivars(ntb); 309 310 return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff, 311 addr, size)); 312 } 313 314 int 315 ntb_mw_clear_trans(device_t ntb, unsigned mw_idx) 316 { 317 struct ntb_child *nc = device_get_ivars(ntb); 318 319 return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff)); 320 } 321 322 int 323 ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode) 324 { 325 struct ntb_child *nc = device_get_ivars(ntb); 326 327 return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode)); 328 } 329 330 int 331 ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode) 332 { 333 struct ntb_child *nc = device_get_ivars(ntb); 334 335 return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode)); 336 } 337 338 uint8_t 339 ntb_spad_count(device_t ntb) 340 { 341 struct ntb_child *nc = device_get_ivars(ntb); 342 343 return (nc->spadcnt); 344 } 345 346 void 347 ntb_spad_clear(device_t ntb) 348 { 349 struct ntb_child *nc = device_get_ivars(ntb); 350 unsigned i; 351 352 for (i = 0; i < nc->spadcnt; i++) 353 NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0); 354 } 355 356 int 357 ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val) 358 { 359 struct ntb_child *nc = device_get_ivars(ntb); 360 361 return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val)); 362 } 363 364 int 365 ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val) 366 { 367 struct ntb_child *nc = device_get_ivars(ntb); 368 369 return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val)); 370 } 371 372 int 373 ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val) 374 { 375 struct ntb_child *nc = device_get_ivars(ntb); 376 377 return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, 378 val)); 379 } 380 381 int 382 ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val) 383 { 384 struct ntb_child *nc = device_get_ivars(ntb); 385 386 return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, 387 val)); 388 } 389 390 uint64_t 391 ntb_db_valid_mask(device_t ntb) 392 { 393 struct ntb_child *nc = device_get_ivars(ntb); 394 395 return (nc->dbmask); 396 } 397 398 int 399 ntb_db_vector_count(device_t ntb) 400 { 401 402 return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb))); 403 } 404 405 uint64_t 406 ntb_db_vector_mask(device_t ntb, uint32_t vector) 407 { 408 struct ntb_child *nc = device_get_ivars(ntb); 409 410 return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector) 411 >> nc->dboff) & nc->dbmask); 412 } 413 414 int 415 ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size) 416 { 417 418 return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size)); 419 } 420 421 void 422 ntb_db_clear(device_t ntb, uint64_t bits) 423 { 424 struct ntb_child *nc = device_get_ivars(ntb); 425 426 return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff)); 427 } 428 429 void 430 ntb_db_clear_mask(device_t ntb, uint64_t bits) 431 { 432 struct ntb_child *nc = device_get_ivars(ntb); 433 434 return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff)); 435 } 436 437 uint64_t 438 ntb_db_read(device_t ntb) 439 { 440 struct ntb_child *nc = device_get_ivars(ntb); 441 442 return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff) 443 & nc->dbmask); 444 } 445 446 void 447 ntb_db_set_mask(device_t ntb, uint64_t bits) 448 { 449 struct ntb_child *nc = device_get_ivars(ntb); 450 451 return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff)); 452 } 453 454 void 455 ntb_peer_db_set(device_t ntb, uint64_t bits) 456 { 457 struct ntb_child *nc = device_get_ivars(ntb); 458 459 return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff)); 460 } 461 462 MODULE_VERSION(ntb, 1); 463