From owner-svn-src-all@freebsd.org Thu Jul 28 10:48:21 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D7F4DBA7ABC; Thu, 28 Jul 2016 10:48:21 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7289A13DE; Thu, 28 Jul 2016 10:48:21 +0000 (UTC) (envelope-from mav@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u6SAmKYF004067; Thu, 28 Jul 2016 10:48:20 GMT (envelope-from mav@FreeBSD.org) Received: (from mav@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u6SAmK3l004061; Thu, 28 Jul 2016 10:48:20 GMT (envelope-from mav@FreeBSD.org) Message-Id: <201607281048.u6SAmK3l004061@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mav set sender to mav@FreeBSD.org using -f From: Alexander Motin Date: Thu, 28 Jul 2016 10:48:20 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r303429 - in head: share/man/man4 sys/dev/ntb sys/dev/ntb/ntb_hw X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jul 2016 10:48:22 -0000 Author: mav Date: Thu Jul 28 10:48:20 2016 New Revision: 303429 URL: https://svnweb.freebsd.org/changeset/base/303429 Log: Once more refactor KPI between NTB hardware and consumers. New design allows hardware resources to be split between several consumers. For example, one BAR can be dedicated for remote memory access, while other resources can be used for packet transport for virtual Ethernet interface. And even without resource split, this code allows to specify which consumer driver should attach the hardware. From some points this makes the code even closer to Linux one, even though Linux does not provide the described flexibility. Modified: head/share/man/man4/ntb_hw.4 head/sys/dev/ntb/ntb.c head/sys/dev/ntb/ntb.h head/sys/dev/ntb/ntb_hw/ntb_hw.c head/sys/dev/ntb/ntb_if.m head/sys/dev/ntb/ntb_transport.c Modified: head/share/man/man4/ntb_hw.4 ============================================================================== --- head/share/man/man4/ntb_hw.4 Thu Jul 28 10:05:41 2016 (r303428) +++ head/share/man/man4/ntb_hw.4 Thu Jul 28 10:48:20 2016 (r303429) @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 10, 2016 +.Dd July 28, 2016 .Dt NTB_HW 4 .Os .Sh NAME @@ -51,6 +51,20 @@ The following tunables are settable from .It Va hw.ntb.debug_level Driver debug level. The default value is 0, higher means more verbose. +.It Va hint.ntb_hw. Ns Ar X Ns Va .config +Configures NTB resources split between several consumer devices. +Configuration of multiple consumer devices separated by commas. +Each device can be configured as: "[:[:[:]]]", where: +.Va name +is a name of the driver which should attach the device (empty means any), +.Va mw +is a number of memory windows to allocate (empty means all available), +.Va spad +is a number of scratchpad registers to allocate (empty means all available), +.Va db +is a number of doorbells to allocate (empty means all available). +The default configuration is empty string, which means single device +with all available resources allowing any driver attachment. .El .Sh DESCRIPTION The NTB allows you to connect two computer systems using a PCIe link if they @@ -69,7 +83,7 @@ The hardware provides 2-3 memory windows On Xeon processors one of memory windows is typically consumed by the driver to workaround multiple hardware erratas. .Sh CONFIGURATION -Tne NTB configuration should be set by BIOS. +The NTB configuration should be set by BIOS. It includes enabling NTB, choosing between NTB-to-NTB or NTB-to-Root Port mode, enabling split BAR mode (one of two 64-bit BARs can be split into two 32-bit ones) and configuring BAR sizes in bits (from 12 to 29/39) for both NTB sides. Modified: head/sys/dev/ntb/ntb.c ============================================================================== --- head/sys/dev/ntb/ntb.c Thu Jul 28 10:05:41 2016 (r303428) +++ head/sys/dev/ntb/ntb.c Thu Jul 28 10:48:20 2016 (r303429) @@ -31,6 +31,8 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#include #include #include @@ -39,4 +41,422 @@ __FBSDID("$FreeBSD$"); devclass_t ntb_hw_devclass; SYSCTL_NODE(_hw, OID_AUTO, ntb, CTLFLAG_RW, 0, "NTB sysctls"); +struct ntb_child { + device_t dev; + int enabled; + int mwoff; + int mwcnt; + int spadoff; + int spadcnt; + int dboff; + int dbmask; + void *ctx; + const struct ntb_ctx_ops *ctx_ops; + struct rmlock ctx_lock; + struct ntb_child *next; +}; + +int +ntb_register_device(device_t dev) +{ + struct ntb_child **cpp = device_get_softc(dev); + struct ntb_child *nc; + int i, mw, mwu, mwt, spad, spadu, spadt, db, dbu, dbt; + char cfg[128] = ""; + char buf[32]; + char *n, *np, *c, *p, *name; + + mwu = 0; + mwt = NTB_MW_COUNT(dev); + spadu = 0; + spadt = NTB_SPAD_COUNT(dev); + dbu = 0; + dbt = flsll(NTB_DB_VALID_MASK(dev)); + + device_printf(dev, "%d memory windows, %d scratchpads, " + "%d doorbells\n", mwt, spadt, dbt); + + snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev), + device_get_unit(dev)); + TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg)); + n = cfg; + i = 0; + while ((c = strsep(&n, ",")) != NULL) { + np = c; + name = strsep(&np, ":"); + if (name != NULL && name[0] == 0) + name = NULL; + p = strsep(&np, ":"); + mw = (p && p[0] != 0) ? strtol(p, NULL, 10) : mwt - mwu; + p = strsep(&np, ":"); + spad = (p && p[0] != 0) ? strtol(p, NULL, 10) : spadt - spadu; + db = (np && np[0] != 0) ? strtol(np, NULL, 10) : dbt - dbu; + + if (mw > mwt - mwu || spad > spadt - spadu || db > dbt - dbu) { + device_printf(dev, "Not enough resources for config\n"); + break; + } + + nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO); + nc->mwoff = mwu; + nc->mwcnt = mw; + nc->spadoff = spadu; + nc->spadcnt = spad; + nc->dboff = dbu; + nc->dbmask = (db == 0) ? 0 : (0xffffffffffffffff >> (64 - db)); + rm_init(&nc->ctx_lock, "ntb ctx"); + nc->dev = device_add_child(dev, name, -1); + if (nc->dev == NULL) { + ntb_unregister_device(dev); + return (ENOMEM); + } + device_set_ivars(nc->dev, nc); + *cpp = nc; + cpp = &nc->next; + + if (bootverbose) { + device_printf(dev, "%d \"%s\":", i, name); + if (mw > 0) { + printf(" memory windows %d", mwu); + if (mw > 1) + printf("-%d", mwu + mw - 1); + } + if (spad > 0) { + printf(" scratchpads %d", spadu); + if (spad > 1) + printf("-%d", spadu + spad - 1); + } + if (db > 0) { + printf(" doorbells %d", dbu); + if (db > 1) + printf("-%d", dbu + db - 1); + } + printf("\n"); + } + + mwu += mw; + spadu += spad; + dbu += db; + i++; + } + + bus_generic_attach(dev); + return (0); +} + +int +ntb_unregister_device(device_t dev) +{ + struct ntb_child **cpp = device_get_softc(dev); + struct ntb_child *nc; + int error = 0; + + while ((nc = *cpp) != NULL) { + *cpp = (*cpp)->next; + error = device_delete_child(dev, nc->dev); + if (error) + break; + rm_destroy(&nc->ctx_lock); + free(nc, M_DEVBUF); + } + return (error); +} + +void +ntb_link_event(device_t dev) +{ + struct ntb_child **cpp = device_get_softc(dev); + struct ntb_child *nc; + struct rm_priotracker ctx_tracker; + + for (nc = *cpp; nc != NULL; nc = nc->next) { + rm_rlock(&nc->ctx_lock, &ctx_tracker); + if (nc->ctx_ops != NULL && nc->ctx_ops->link_event != NULL) + nc->ctx_ops->link_event(nc->ctx); + rm_runlock(&nc->ctx_lock, &ctx_tracker); + } +} + +void +ntb_db_event(device_t dev, uint32_t vec) +{ + struct ntb_child **cpp = device_get_softc(dev); + struct ntb_child *nc; + struct rm_priotracker ctx_tracker; + + for (nc = *cpp; nc != NULL; nc = nc->next) { + rm_rlock(&nc->ctx_lock, &ctx_tracker); + if (nc->ctx_ops != NULL && nc->ctx_ops->db_event != NULL) + nc->ctx_ops->db_event(nc->ctx, vec); + rm_runlock(&nc->ctx_lock, &ctx_tracker); + } +} + +bool +ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width) +{ + + return (NTB_LINK_IS_UP(device_get_parent(ntb), speed, width)); +} + +int +ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width) +{ + struct ntb_child *nc = device_get_ivars(ntb); + struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev)); + struct ntb_child *nc1; + + for (nc1 = *cpp; nc1 != NULL; nc1 = nc->next) { + if (nc1->enabled) { + nc->enabled = 1; + return (0); + } + } + nc->enabled = 1; + return (NTB_LINK_ENABLE(device_get_parent(ntb), speed, width)); +} + +int +ntb_link_disable(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + struct ntb_child **cpp = device_get_softc(device_get_parent(nc->dev)); + struct ntb_child *nc1; + + if (!nc->enabled) + return (0); + nc->enabled = 0; + for (nc1 = *cpp; nc1 != NULL; nc1 = nc->next) { + if (nc1->enabled) + return (0); + } + return (NTB_LINK_DISABLE(device_get_parent(ntb))); +} + +bool +ntb_link_enabled(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (nc->enabled && NTB_LINK_ENABLED(device_get_parent(ntb))); +} + +int +ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + if (ctx == NULL || ctx_ops == NULL) + return (EINVAL); + + rm_wlock(&nc->ctx_lock); + if (nc->ctx_ops != NULL) { + rm_wunlock(&nc->ctx_lock); + return (EINVAL); + } + nc->ctx = ctx; + nc->ctx_ops = ctx_ops; + rm_wunlock(&nc->ctx_lock); + + return (0); +} + +void * +ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + KASSERT(nc->ntb_ctx != NULL && nc->ctx_ops != NULL, ("bogus")); + if (ctx_ops != NULL) + *ctx_ops = nc->ctx_ops; + return (nc->ctx); +} + +void +ntb_clear_ctx(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + rm_wlock(&nc->ctx_lock); + nc->ctx = NULL; + nc->ctx_ops = NULL; + rm_wunlock(&nc->ctx_lock); +} + +uint8_t +ntb_mw_count(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (nc->mwcnt); +} + +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) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_MW_GET_RANGE(device_get_parent(ntb), mw_idx + nc->mwoff, + base, vbase, size, align, align_size, plimit)); +} + +int +ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, size_t size) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_MW_SET_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff, + addr, size)); +} + +int +ntb_mw_clear_trans(device_t ntb, unsigned mw_idx) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_MW_CLEAR_TRANS(device_get_parent(ntb), mw_idx + nc->mwoff)); +} + +int +ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_MW_GET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode)); +} + +int +ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_MW_SET_WC(device_get_parent(ntb), mw_idx + nc->mwoff, mode)); +} + +uint8_t +ntb_spad_count(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (nc->spadcnt); +} + +void +ntb_spad_clear(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + unsigned i; + + for (i = 0; i < nc->spadcnt; i++) + NTB_SPAD_WRITE(device_get_parent(ntb), i + nc->spadoff, 0); +} + +int +ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, val)); +} + +int +ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, val)); +} + +int +ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_PEER_SPAD_WRITE(device_get_parent(ntb), idx + nc->spadoff, + val)); +} + +int +ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_PEER_SPAD_READ(device_get_parent(ntb), idx + nc->spadoff, + val)); +} + +uint64_t +ntb_db_valid_mask(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (nc->dbmask); +} + +int +ntb_db_vector_count(device_t ntb) +{ + + return (NTB_DB_VECTOR_COUNT(device_get_parent(ntb))); +} + +uint64_t +ntb_db_vector_mask(device_t ntb, uint32_t vector) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return ((NTB_DB_VECTOR_MASK(device_get_parent(ntb), vector) + >> nc->dboff) & nc->dbmask); +} + +int +ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size) +{ + + return (NTB_PEER_DB_ADDR(device_get_parent(ntb), db_addr, db_size)); +} + +void +ntb_db_clear(device_t ntb, uint64_t bits) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_DB_CLEAR(device_get_parent(ntb), bits << nc->dboff)); +} + +void +ntb_db_clear_mask(device_t ntb, uint64_t bits) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_DB_CLEAR_MASK(device_get_parent(ntb), bits << nc->dboff)); +} + +uint64_t +ntb_db_read(device_t ntb) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return ((NTB_DB_READ(device_get_parent(ntb)) >> nc->dboff) + & nc->dbmask); +} + +void +ntb_db_set_mask(device_t ntb, uint64_t bits) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_DB_SET_MASK(device_get_parent(ntb), bits << nc->dboff)); +} + +void +ntb_peer_db_set(device_t ntb, uint64_t bits) +{ + struct ntb_child *nc = device_get_ivars(ntb); + + return (NTB_PEER_DB_SET(device_get_parent(ntb), bits << nc->dboff)); +} + MODULE_VERSION(ntb, 1); Modified: head/sys/dev/ntb/ntb.h ============================================================================== --- head/sys/dev/ntb/ntb.h Thu Jul 28 10:05:41 2016 (r303428) +++ head/sys/dev/ntb/ntb.h Thu Jul 28 10:48:20 2016 (r303429) @@ -34,4 +34,376 @@ extern devclass_t ntb_hw_devclass; SYSCTL_DECL(_hw_ntb); +int ntb_register_device(device_t ntb); +int ntb_unregister_device(device_t ntb); + +/* + * ntb_link_event() - notify driver context of a change in link status + * @ntb: NTB device context + * + * Notify the driver context that the link status may have changed. The driver + * should call intb_link_is_up() to get the current status. + */ +void ntb_link_event(device_t ntb); + +/* + * ntb_db_event() - notify driver context of a doorbell event + * @ntb: NTB device context + * @vector: Interrupt vector number + * + * Notify the driver context of a doorbell event. If hardware supports + * multiple interrupt vectors for doorbells, the vector number indicates which + * vector received the interrupt. The vector number is relative to the first + * vector used for doorbells, starting at zero, and must be less than + * ntb_db_vector_count(). The driver may call ntb_db_read() to check which + * doorbell bits need service, and ntb_db_vector_mask() to determine which of + * those bits are associated with the vector number. + */ +void ntb_db_event(device_t ntb, uint32_t vec); + +/* + * ntb_link_is_up() - get the current ntb link state + * @ntb: NTB device context + * @speed: OUT - The link speed expressed as PCIe generation number + * @width: OUT - The link width expressed as the number of PCIe lanes + * + * RETURNS: true or false based on the hardware link state + */ +bool ntb_link_is_up(device_t ntb, enum ntb_speed *speed, enum ntb_width *width); + +/* + * ntb_link_enable() - enable the link on the secondary side of the ntb + * @ntb: NTB device context + * @max_speed: The maximum link speed expressed as PCIe generation number[0] + * @max_width: The maximum link width expressed as the number of PCIe lanes[0] + * + * Enable the link on the secondary side of the ntb. This can only be done + * from the primary side of the ntb in primary or b2b topology. The ntb device + * should train the link to its maximum speed and width, or the requested speed + * and width, whichever is smaller, if supported. + * + * Return: Zero on success, otherwise an error number. + * + * [0]: Only NTB_SPEED_AUTO and NTB_WIDTH_AUTO are valid inputs; other speed + * and width input will be ignored. + */ +int ntb_link_enable(device_t ntb, enum ntb_speed speed, enum ntb_width width); + +/* + * ntb_link_disable() - disable the link on the secondary side of the ntb + * @ntb: NTB device context + * + * Disable the link on the secondary side of the ntb. This can only be done + * from the primary side of the ntb in primary or b2b topology. The ntb device + * should disable the link. Returning from this call must indicate that a + * barrier has passed, though with no more writes may pass in either direction + * across the link, except if this call returns an error number. + * + * Return: Zero on success, otherwise an error number. + */ +int ntb_link_disable(device_t ntb); + +/* + * get enable status of the link on the secondary side of the ntb + */ +bool ntb_link_enabled(device_t ntb); + +/* + * ntb_set_ctx() - associate a driver context with an ntb device + * @ntb: NTB device context + * @ctx: Driver context + * @ctx_ops: Driver context operations + * + * Associate a driver context and operations with a ntb device. The context is + * provided by the client driver, and the driver may associate a different + * context with each ntb device. + * + * Return: Zero if the context is associated, otherwise an error number. + */ +int ntb_set_ctx(device_t ntb, void *ctx, const struct ntb_ctx_ops *ctx_ops); + +/* + * ntb_set_ctx() - get a driver context associated with an ntb device + * @ntb: NTB device context + * @ctx_ops: Driver context operations + * + * Get a driver context and operations associated with a ntb device. + */ +void * ntb_get_ctx(device_t ntb, const struct ntb_ctx_ops **ctx_ops); + +/* + * ntb_clear_ctx() - disassociate any driver context from an ntb device + * @ntb: NTB device context + * + * Clear any association that may exist between a driver context and the ntb + * device. + */ +void ntb_clear_ctx(device_t ntb); + +/* + * ntb_mw_count() - Get the number of memory windows available for KPI + * consumers. + * + * (Excludes any MW wholly reserved for register access.) + */ +uint8_t ntb_mw_count(device_t ntb); + +/* + * ntb_mw_get_range() - get the range of a memory window + * @ntb: NTB device context + * @idx: Memory window number + * @base: OUT - the base address for mapping the memory window + * @size: OUT - the size for mapping the memory window + * @align: OUT - the base alignment for translating the memory window + * @align_size: OUT - the size alignment for translating the memory window + * + * Get the range of a memory window. NULL may be given for any output + * parameter if the value is not needed. The base and size may be used for + * mapping the memory window, to access the peer memory. The alignment and + * size may be used for translating the memory window, for the peer to access + * memory on the local system. + * + * Return: Zero on success, otherwise an error number. + */ +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); + +/* + * ntb_mw_set_trans() - set the translation of a memory window + * @ntb: NTB device context + * @idx: Memory window number + * @addr: The dma address local memory to expose to the peer + * @size: The size of the local memory to expose to the peer + * + * Set the translation of a memory window. The peer may access local memory + * through the window starting at the address, up to the size. The address + * must be aligned to the alignment specified by ntb_mw_get_range(). The size + * must be aligned to the size alignment specified by ntb_mw_get_range(). The + * address must be below the plimit specified by ntb_mw_get_range() (i.e. for + * 32-bit BARs). + * + * Return: Zero on success, otherwise an error number. + */ +int ntb_mw_set_trans(device_t ntb, unsigned mw_idx, bus_addr_t addr, + size_t size); + +/* + * ntb_mw_clear_trans() - clear the translation of a memory window + * @ntb: NTB device context + * @idx: Memory window number + * + * Clear the translation of a memory window. The peer may no longer access + * local memory through the window. + * + * Return: Zero on success, otherwise an error number. + */ +int ntb_mw_clear_trans(device_t ntb, unsigned mw_idx); + +/* + * ntb_mw_get_wc - Get the write-combine status of a memory window + * + * Returns: Zero on success, setting *wc; otherwise an error number (e.g. if + * idx is an invalid memory window). + * + * Mode is a VM_MEMATTR_* type. + */ +int ntb_mw_get_wc(device_t ntb, unsigned mw_idx, vm_memattr_t *mode); + +/* + * ntb_mw_set_wc - Set the write-combine status of a memory window + * + * If 'mode' matches the current status, this does nothing and succeeds. Mode + * is a VM_MEMATTR_* type. + * + * Returns: Zero on success, setting the caching attribute on the virtual + * mapping of the BAR; otherwise an error number (e.g. if idx is an invalid + * memory window, or if changing the caching attribute fails). + */ +int ntb_mw_set_wc(device_t ntb, unsigned mw_idx, vm_memattr_t mode); + +/* + * ntb_spad_count() - get the total scratch regs usable + * @ntb: pointer to ntb_softc instance + * + * This function returns the max 32bit scratchpad registers usable by the + * upper layer. + * + * RETURNS: total number of scratch pad registers available + */ +uint8_t ntb_spad_count(device_t ntb); + +/* + * ntb_get_max_spads() - zero local scratch registers + * @ntb: pointer to ntb_softc instance + * + * This functions overwrites all local scratchpad registers with zeroes. + */ +void ntb_spad_clear(device_t ntb); + +/* + * ntb_spad_write() - write to the secondary scratchpad register + * @ntb: pointer to ntb_softc instance + * @idx: index to the scratchpad register, 0 based + * @val: the data value to put into the register + * + * This function allows writing of a 32bit value to the indexed scratchpad + * register. The register resides on the secondary (external) side. + * + * RETURNS: An appropriate ERRNO error value on error, or zero for success. + */ +int ntb_spad_write(device_t ntb, unsigned int idx, uint32_t val); + +/* + * ntb_spad_read() - read from the primary scratchpad register + * @ntb: pointer to ntb_softc instance + * @idx: index to scratchpad register, 0 based + * @val: pointer to 32bit integer for storing the register value + * + * This function allows reading of the 32bit scratchpad register on + * the primary (internal) side. + * + * RETURNS: An appropriate ERRNO error value on error, or zero for success. + */ +int ntb_spad_read(device_t ntb, unsigned int idx, uint32_t *val); + +/* + * ntb_peer_spad_write() - write to the secondary scratchpad register + * @ntb: pointer to ntb_softc instance + * @idx: index to the scratchpad register, 0 based + * @val: the data value to put into the register + * + * This function allows writing of a 32bit value to the indexed scratchpad + * register. The register resides on the secondary (external) side. + * + * RETURNS: An appropriate ERRNO error value on error, or zero for success. + */ +int ntb_peer_spad_write(device_t ntb, unsigned int idx, uint32_t val); + +/* + * ntb_peer_spad_read() - read from the primary scratchpad register + * @ntb: pointer to ntb_softc instance + * @idx: index to scratchpad register, 0 based + * @val: pointer to 32bit integer for storing the register value + * + * This function allows reading of the 32bit scratchpad register on + * the primary (internal) side. + * + * RETURNS: An appropriate ERRNO error value on error, or zero for success. + */ +int ntb_peer_spad_read(device_t ntb, unsigned int idx, uint32_t *val); + +/* + * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb + * @ntb: NTB device context + * + * Hardware may support different number or arrangement of doorbell bits. + * + * Return: A mask of doorbell bits supported by the ntb. + */ +uint64_t ntb_db_valid_mask(device_t ntb); + +/* + * ntb_db_vector_count() - get the number of doorbell interrupt vectors + * @ntb: NTB device context. + * + * Hardware may support different number of interrupt vectors. + * + * Return: The number of doorbell interrupt vectors. + */ +int ntb_db_vector_count(device_t ntb); + +/* + * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector + * @ntb: NTB device context + * @vector: Doorbell vector number + * + * Each interrupt vector may have a different number or arrangement of bits. + * + * Return: A mask of doorbell bits serviced by a vector. + */ +uint64_t ntb_db_vector_mask(device_t ntb, uint32_t vector); + +/* + * ntb_peer_db_addr() - address and size of the peer doorbell register + * @ntb: NTB device context. + * @db_addr: OUT - The address of the peer doorbell register. + * @db_size: OUT - The number of bytes to write the peer doorbell register. + * + * Return the address of the peer doorbell register. This may be used, for + * example, by drivers that offload memory copy operations to a dma engine. + * The drivers may wish to ring the peer doorbell at the completion of memory + * copy operations. For efficiency, and to simplify ordering of operations + * between the dma memory copies and the ringing doorbell, the driver may + * append one additional dma memory copy with the doorbell register as the + * destination, after the memory copy operations. + * + * Return: Zero on success, otherwise an error number. + * + * Note that writing the peer doorbell via a memory window will *not* generate + * an interrupt on the remote host; that must be done separately. + */ +int ntb_peer_db_addr(device_t ntb, bus_addr_t *db_addr, vm_size_t *db_size); + +/* + * ntb_db_clear() - clear bits in the local doorbell register + * @ntb: NTB device context. + * @db_bits: Doorbell bits to clear. + * + * Clear bits in the local doorbell register, arming the bits for the next + * doorbell. + * + * Return: Zero on success, otherwise an error number. + */ +void ntb_db_clear(device_t ntb, uint64_t bits); + +/* + * ntb_db_clear_mask() - clear bits in the local doorbell mask + * @ntb: NTB device context. + * @db_bits: Doorbell bits to clear. + * + * Clear bits in the local doorbell mask register, allowing doorbell interrupts + * from being generated for those doorbell bits. If a doorbell bit is already + * set at the time the mask is cleared, and the corresponding mask bit is + * changed from set to clear, then the ntb driver must ensure that + * ntb_db_event() is called. If the hardware does not generate the interrupt + * on clearing the mask bit, then the driver must call ntb_db_event() anyway. + * + * Return: Zero on success, otherwise an error number. + */ +void ntb_db_clear_mask(device_t ntb, uint64_t bits); + +/* + * ntb_db_read() - read the local doorbell register + * @ntb: NTB device context. + * + * Read the local doorbell register, and return the bits that are set. + * + * Return: The bits currently set in the local doorbell register. + */ +uint64_t ntb_db_read(device_t ntb); + +/* + * ntb_db_set_mask() - set bits in the local doorbell mask + * @ntb: NTB device context. + * @db_bits: Doorbell mask bits to set. + * + * Set bits in the local doorbell mask register, preventing doorbell interrupts + * from being generated for those doorbell bits. Bits that were already set + * must remain set. + * + * Return: Zero on success, otherwise an error number. + */ +void ntb_db_set_mask(device_t ntb, uint64_t bits); + +/* + * ntb_peer_db_set() - Set the doorbell on the secondary/external side + * @ntb: pointer to ntb_softc instance + * @bit: doorbell bits to ring + * + * This function allows triggering of a doorbell on the secondary/external + * side that will initiate an interrupt on the remote host + */ +void ntb_peer_db_set(device_t ntb, uint64_t bits); + #endif /* _NTB_H_ */ Modified: head/sys/dev/ntb/ntb_hw/ntb_hw.c ============================================================================== --- head/sys/dev/ntb/ntb_hw/ntb_hw.c Thu Jul 28 10:05:41 2016 (r303428) +++ head/sys/dev/ntb/ntb_hw/ntb_hw.c Thu Jul 28 10:48:20 2016 (r303429) @@ -51,7 +51,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include @@ -201,6 +200,9 @@ struct ntb_msix_data { }; struct ntb_softc { + /* ntb.c context. Do not move! Must go first! */ + void *ntb_store; + device_t device; enum ntb_device_type type; uint32_t features; @@ -219,10 +221,7 @@ struct ntb_softc { struct callout heartbeat_timer; struct callout lr_timer; - void *ntb_ctx; - const struct ntb_ctx_ops *ctx_ops; struct ntb_vec *msix_vec; - struct rmlock ctx_lock; uint32_t ppd; enum ntb_conn_type conn_type; @@ -284,72 +283,74 @@ bus_space_write_8(bus_space_tag_t tag, b } #endif -#define ntb_bar_read(SIZE, bar, offset) \ +#define intel_ntb_bar_read(SIZE, bar, offset) \ bus_space_read_ ## SIZE (ntb->bar_info[(bar)].pci_bus_tag, \ ntb->bar_info[(bar)].pci_bus_handle, (offset)) -#define ntb_bar_write(SIZE, bar, offset, val) \ +#define intel_ntb_bar_write(SIZE, bar, offset, val) \ bus_space_write_ ## SIZE (ntb->bar_info[(bar)].pci_bus_tag, \ ntb->bar_info[(bar)].pci_bus_handle, (offset), (val)) -#define ntb_reg_read(SIZE, offset) ntb_bar_read(SIZE, NTB_CONFIG_BAR, offset) -#define ntb_reg_write(SIZE, offset, val) \ - ntb_bar_write(SIZE, NTB_CONFIG_BAR, offset, val) -#define ntb_mw_read(SIZE, offset) \ - ntb_bar_read(SIZE, ntb_mw_to_bar(ntb, ntb->b2b_mw_idx), offset) -#define ntb_mw_write(SIZE, offset, val) \ - ntb_bar_write(SIZE, ntb_mw_to_bar(ntb, ntb->b2b_mw_idx), \ +#define intel_ntb_reg_read(SIZE, offset) \ + intel_ntb_bar_read(SIZE, NTB_CONFIG_BAR, offset) +#define intel_ntb_reg_write(SIZE, offset, val) \ + intel_ntb_bar_write(SIZE, NTB_CONFIG_BAR, offset, val) +#define intel_ntb_mw_read(SIZE, offset) \ + intel_ntb_bar_read(SIZE, intel_ntb_mw_to_bar(ntb, ntb->b2b_mw_idx), \ + offset) +#define intel_ntb_mw_write(SIZE, offset, val) \ + intel_ntb_bar_write(SIZE, intel_ntb_mw_to_bar(ntb, ntb->b2b_mw_idx), \ offset, val) -static int ntb_probe(device_t device); -static int ntb_attach(device_t device); -static int ntb_detach(device_t device); -static uint64_t ntb_db_valid_mask(device_t dev); -static void ntb_spad_clear(device_t dev); -static uint64_t ntb_db_vector_mask(device_t dev, uint32_t vector); -static bool ntb_link_is_up(device_t dev, enum ntb_speed *speed, +static int intel_ntb_probe(device_t device); +static int intel_ntb_attach(device_t device); +static int intel_ntb_detach(device_t device); +static uint64_t intel_ntb_db_valid_mask(device_t dev); +static void intel_ntb_spad_clear(device_t dev); +static uint64_t intel_ntb_db_vector_mask(device_t dev, uint32_t vector); +static bool intel_ntb_link_is_up(device_t dev, enum ntb_speed *speed, enum ntb_width *width); -static int ntb_link_enable(device_t dev, enum ntb_speed speed, +static int intel_ntb_link_enable(device_t dev, enum ntb_speed speed, enum ntb_width width); -static int ntb_link_disable(device_t dev); -static int ntb_spad_read(device_t dev, unsigned int idx, uint32_t *val); -static int ntb_peer_spad_write(device_t dev, unsigned int idx, uint32_t val); +static int intel_ntb_link_disable(device_t dev); +static int intel_ntb_spad_read(device_t dev, unsigned int idx, uint32_t *val); +static int intel_ntb_peer_spad_write(device_t dev, unsigned int idx, uint32_t val); -static unsigned ntb_user_mw_to_idx(struct ntb_softc *, unsigned uidx); -static inline enum ntb_bar ntb_mw_to_bar(struct ntb_softc *, unsigned mw); +static unsigned intel_ntb_user_mw_to_idx(struct ntb_softc *, unsigned uidx); +static inline enum ntb_bar intel_ntb_mw_to_bar(struct ntb_softc *, unsigned mw); static inline bool bar_is_64bit(struct ntb_softc *, enum ntb_bar); static inline void bar_get_xlat_params(struct ntb_softc *, enum ntb_bar, uint32_t *base, uint32_t *xlat, uint32_t *lmt); -static int ntb_map_pci_bars(struct ntb_softc *ntb); -static int ntb_mw_set_wc_internal(struct ntb_softc *, unsigned idx, +static int intel_ntb_map_pci_bars(struct ntb_softc *ntb); +static int intel_ntb_mw_set_wc_internal(struct ntb_softc *, unsigned idx, vm_memattr_t); static void print_map_success(struct ntb_softc *, struct ntb_pci_bar_info *, const char *); static int map_mmr_bar(struct ntb_softc *ntb, struct ntb_pci_bar_info *bar); static int map_memory_window_bar(struct ntb_softc *ntb, struct ntb_pci_bar_info *bar); -static void ntb_unmap_pci_bar(struct ntb_softc *ntb); -static int ntb_remap_msix(device_t, uint32_t desired, uint32_t avail); -static int ntb_init_isr(struct ntb_softc *ntb); -static int ntb_setup_legacy_interrupt(struct ntb_softc *ntb); -static int ntb_setup_msix(struct ntb_softc *ntb, uint32_t num_vectors); -static void ntb_teardown_interrupts(struct ntb_softc *ntb); -static inline uint64_t ntb_vec_mask(struct ntb_softc *, uint64_t db_vector); -static void ntb_interrupt(struct ntb_softc *, uint32_t vec); +static void intel_ntb_unmap_pci_bar(struct ntb_softc *ntb); +static int intel_ntb_remap_msix(device_t, uint32_t desired, uint32_t avail); +static int intel_ntb_init_isr(struct ntb_softc *ntb); +static int intel_ntb_setup_legacy_interrupt(struct ntb_softc *ntb); +static int intel_ntb_setup_msix(struct ntb_softc *ntb, uint32_t num_vectors); +static void intel_ntb_teardown_interrupts(struct ntb_softc *ntb); +static inline uint64_t intel_ntb_vec_mask(struct ntb_softc *, uint64_t db_vector); +static void intel_ntb_interrupt(struct ntb_softc *, uint32_t vec); static void ndev_vec_isr(void *arg); static void ndev_irq_isr(void *arg); static inline uint64_t db_ioread(struct ntb_softc *, uint64_t regoff); static inline void db_iowrite(struct ntb_softc *, uint64_t regoff, uint64_t); static inline void db_iowrite_raw(struct ntb_softc *, uint64_t regoff, uint64_t); -static int ntb_create_msix_vec(struct ntb_softc *ntb, uint32_t num_vectors); -static void ntb_free_msix_vec(struct ntb_softc *ntb); -static void ntb_get_msix_info(struct ntb_softc *ntb); -static void ntb_exchange_msix(void *); -static struct ntb_hw_info *ntb_get_device_info(uint32_t device_id); -static void ntb_detect_max_mw(struct ntb_softc *ntb); -static int ntb_detect_xeon(struct ntb_softc *ntb); -static int ntb_detect_atom(struct ntb_softc *ntb); -static int ntb_xeon_init_dev(struct ntb_softc *ntb); -static int ntb_atom_init_dev(struct ntb_softc *ntb); -static void ntb_teardown_xeon(struct ntb_softc *ntb); +static int intel_ntb_create_msix_vec(struct ntb_softc *ntb, uint32_t num_vectors); +static void intel_ntb_free_msix_vec(struct ntb_softc *ntb); +static void intel_ntb_get_msix_info(struct ntb_softc *ntb); +static void intel_ntb_exchange_msix(void *); +static struct ntb_hw_info *intel_ntb_get_device_info(uint32_t device_id); +static void intel_ntb_detect_max_mw(struct ntb_softc *ntb); +static int intel_ntb_detect_xeon(struct ntb_softc *ntb); +static int intel_ntb_detect_atom(struct ntb_softc *ntb); +static int intel_ntb_xeon_init_dev(struct ntb_softc *ntb); +static int intel_ntb_atom_init_dev(struct ntb_softc *ntb); +static void intel_ntb_teardown_xeon(struct ntb_softc *ntb); *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***