diff --git a/include/mtcr_ul/mtcr_mf.h b/include/mtcr_ul/mtcr_mf.h index 4702772..adf87bf 100644 --- a/include/mtcr_ul/mtcr_mf.h +++ b/include/mtcr_ul/mtcr_mf.h @@ -80,6 +80,7 @@ struct mfile_t { int sock; /* in not -1 - remote interface */ int is_mtserver_req; /* request came from mtServer - means came from remote client */ void * bar_virtual_addr; + size_t bar_map_size; /* actual size of the mapped BAR0 region */ unsigned int big_endian; unsigned int cr_space_offset; unsigned int map_size; diff --git a/mtcr_ul/mtcr_ul_com.c b/mtcr_ul/mtcr_ul_com.c index 39c893b..2ad4046 100644 --- a/mtcr_ul/mtcr_ul_com.c +++ b/mtcr_ul/mtcr_ul_com.c @@ -50,6 +50,9 @@ #define _FILE_OFFSET_BITS 64 #endif +/* Upper bound for the mapped BAR0 region. The mapping is sized from the + * device's actual resource0 when that can be determined; this is the fallback + * and cap. */ #define MTCR_MAP_SIZE 0x4000000 #define GPU_NETIR_CR_SPACE_OFFSET 0x3000000 @@ -375,12 +378,82 @@ void mpci_change_ul(mfile* mf); * 1 : Device does not support memory access * */ +/* Determine the size of the device's BAR0 mapping. + * + * Historically this was the constant MTCR_MAP_SIZE (64 MB), which assumes a + * full-size Mellanox BAR. Some OEM boards (e.g. DDN SFA12K, a ConnectX-3 + * carrier) expose only 1 MB, so mmap() of 64 MB fails with EINVAL, the mapping + * is left NULL and every subsequent CR-space access dereferences it. + * + * Size the mapping from the device's actual resource0 instead, capped at + * MTCR_MAP_SIZE. Returns MTCR_MAP_SIZE if the size cannot be determined, which + * preserves the previous behaviour on hardware where the sysfs read fails. + */ +static size_t mtcr_map_size(const char* name) +{ + char path[PATH_MAX]; + char line[128]; + unsigned long long start, end, flags; + size_t size; + FILE* f; + + if (!name) + { + return MTCR_MAP_SIZE; + } + + if (snprintf(path, sizeof(path), "%s", name) >= (int)sizeof(path)) + { + return MTCR_MAP_SIZE; + } + + f = fopen(path, "r"); + if (!f) + { + return MTCR_MAP_SIZE; + } + + if (fgets(line, sizeof(line), f) == NULL) + { + fclose(f); + return MTCR_MAP_SIZE; + } + fclose(f); + + if (sscanf(line, "0x%llx 0x%llx 0x%llx", &start, &end, &flags) != 3 || + end < start) + { + return MTCR_MAP_SIZE; + } + + size = (size_t)(end - start + 1); + + /* Round up to a page so mmap() is happy, and never exceed the historical + * maximum. */ + size = (size + 0xfff) & ~(size_t)0xfff; + if (size > MTCR_MAP_SIZE) + { + size = MTCR_MAP_SIZE; + } + return size; +} + static int mtcr_check_signature(mfile* mf) { unsigned signature = 0; int rc; char* connectx_flush = getenv("CONNECTX_FLUSH"); + /* Some OEM boards (DDN SFA12K) expose a 1 MB BAR0, so 0x30F0014 lies + * outside the mapping. Skip the signature probe there rather than + * dereferencing past the mapped region. */ + if ((size_t)0x30F0014 + 4 > mf->bar_map_size) + { + DBG_PRINTF("signature addr 0x30f0014 outside %.1f MB BAR; skipping\n", + (double)mf->bar_map_size / (1024.0 * 1024.0)); + return 0; + } + rc = mread4_ul(mf, 0x30F0014, &signature); if (rc != 4) { @@ -596,7 +669,7 @@ static int mtcr_pcicr_mclose(mfile* mf) { if (mf->bar_virtual_addr) { - munmap(mf->bar_virtual_addr, MTCR_MAP_SIZE); + munmap(mf->bar_virtual_addr, mf->bar_map_size); } if (mf->fd > 0) { @@ -628,7 +701,12 @@ static int mtcr_mmap(mfile* mf, const char* name, off_t off, int ioctl_needed) return -1; } - mf->bar_virtual_addr = mmap(NULL, MTCR_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, mf->fd, off); + mf->bar_map_size = mtcr_map_size(name); + mf->bar_virtual_addr = mmap(NULL, mf->bar_map_size, PROT_READ | PROT_WRITE, + MAP_SHARED, mf->fd, off); + + DBG_PRINTF("MMAP: size=0x%zx off=0x%llx -> %p errno=%d\n", mf->bar_map_size, + (unsigned long long)off, mf->bar_virtual_addr, errno); if (!mf->bar_virtual_addr || (mf->bar_virtual_addr == MAP_FAILED)) { @@ -644,7 +722,7 @@ int mtcr_pcicr_mread4(mfile* mf, unsigned int offset, u_int32_t* value) { ul_ctx_t* ctx = mf->ul_ctx; - if (offset - mf->cr_space_offset >= MTCR_MAP_SIZE) + if (offset - mf->cr_space_offset >= mf->bar_map_size) { errno = EINVAL; return 0; @@ -682,7 +760,7 @@ int mtcr_pcicr_mwrite4(mfile* mf, unsigned int offset, u_int32_t value) { ul_ctx_t* ctx = mf->ul_ctx; - if (offset - mf->cr_space_offset >= MTCR_MAP_SIZE) + if (offset - mf->cr_space_offset >= mf->bar_map_size) { errno = EINVAL; return 0; @@ -2248,6 +2326,27 @@ static int mtcr_vfio_device_open(mfile* mf, const char* name, unsigned domain, u init_vsec_cap_mask(mf); + /* DDN OEM boards expose a stub VSC: registers accept writes but the + * "space supported" status bit is never set, so vsec_cap_mask stays 0 + * and every CR-space access fails with EINVAL. Detect that and fall + * back to the legacy pciconf gateway path instead. */ + /* DDN OEM boards expose a stub VSC: the capability registers accept + * writes but the "space supported" status bit is never set, so + * vsec_cap_mask ends up with only VCC_INITIALIZED. Fall back to the + * legacy pciconf gateway in that case. */ + DBG_PRINTF("VSEC_MASK_DUMP = 0x%x (CRSPACE bit %d)\n", mf->vsec_cap_mask, + (mf->vsec_cap_mask >> VCC_CRSPACE_SPACE_SUPPORTED) & 1); + if (!(mf->vsec_cap_mask & (1 << VCC_CRSPACE_SPACE_SUPPORTED)) && + !(mf->vsec_cap_mask & (1 << VCC_PCI_CRSPACE_SPACE_SUPPORTED)) && + !(mf->vsec_cap_mask & (1 << VCC_ICMD_SPACE_SUPPORTED)) && + !(mf->vsec_cap_mask & (1 << VCC_ICMD_EXT_SPACE_SUPPORTED))) + { + DBG_PRINTF("VSC present but reports no usable spaces (mask=0x%x); " + "falling back to legacy gateway\n", mf->vsec_cap_mask); + mf->vsec_addr = 0; + mf->functional_vsec_supp = 0; + } + if (VSEC_SUPPORTED_UL(mf)) { mf->address_space = AS_CR_SPACE; @@ -2265,7 +2364,8 @@ static int mtcr_vfio_device_open(mfile* mf, const char* name, unsigned domain, u } DBG_PRINTF("mf->pxir_vsec_supp: %d\n", mf->pxir_vsec_supp); } - else + + if (!mf->vsec_addr) { ctx->wo_addr = is_wo_pciconf_gw(mf); DBG_PRINTF("Write Only Address: %d\n", ctx->wo_addr);