1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2018-2024 Advanced Micro Devices, Inc.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdbool.h>
#include <eal_export.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_string_fns.h>
#include "ionic_common.h"
#define IONIC_MDEV_UNK "mdev_unknown"
#define IONIC_MNIC "cpu_mnic"
#define IONIC_MCRYPT "cpu_mcrypt"
#define IONIC_MAX_NAME_LEN 20
#define IONIC_MAX_MNETS 5
#define IONIC_MAX_MCPTS 1
#define IONIC_MAX_DEVICES (IONIC_MAX_MNETS + IONIC_MAX_MCPTS)
#define IONIC_MAX_U16_IDX 0xFFFF
#define IONIC_UIO_MAX_TRIES 32
/*
* Note: the driver can assign any idx number
* in the range [0-IONIC_MAX_MDEV_SCAN)
*/
#define IONIC_MAX_MDEV_SCAN 32
struct ionic_map_tbl {
char dev_name[IONIC_MAX_NAME_LEN];
uint16_t dev_idx;
uint16_t uio_idx;
char mdev_name[IONIC_MAX_NAME_LEN];
};
struct ionic_map_tbl ionic_mdev_map[IONIC_MAX_DEVICES] = {
{ "net_ionic0", 0, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
{ "net_ionic1", 1, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
{ "net_ionic2", 2, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
{ "net_ionic3", 3, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
{ "net_ionic4", 4, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
{ "crypto_ionic0", 5, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
};
struct uio_name {
uint16_t idx;
char name[IONIC_MAX_NAME_LEN];
};
static void
uio_fill_name_cache(struct uio_name *name_cache, const char *pfx)
{
char file[64];
FILE *fp;
char *ret;
int name_idx = 0;
int i;
for (i = 0; i < IONIC_UIO_MAX_TRIES &&
name_idx < IONIC_MAX_DEVICES; i++) {
sprintf(file, "/sys/class/uio/uio%d/name", i);
fp = fopen(file, "r");
if (fp == NULL)
continue;
ret = fgets(name_cache[name_idx].name, IONIC_MAX_NAME_LEN, fp);
if (ret == NULL) {
fclose(fp);
continue;
}
name_cache[name_idx].idx = i;
fclose(fp);
if (strncmp(name_cache[name_idx].name, pfx, strlen(pfx)) == 0)
name_idx++;
}
}
static int
uio_get_idx_for_devname(struct uio_name *name_cache, char *devname)
{
int i;
for (i = 0; i < IONIC_MAX_DEVICES; i++)
if (strncmp(name_cache[i].name, devname, strlen(devname)) == 0)
return name_cache[i].idx;
return -1;
}
RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_scan_mnet_devices)
void
ionic_uio_scan_mnet_devices(void)
{
struct ionic_map_tbl *map;
char devname[IONIC_MAX_NAME_LEN];
struct uio_name name_cache[IONIC_MAX_DEVICES];
bool done;
int mdev_idx = 0;
int uio_idx;
int i;
static bool scan_done;
if (scan_done)
return;
scan_done = true;
uio_fill_name_cache(name_cache, IONIC_MNIC);
for (i = 0; i < IONIC_MAX_MNETS; i++) {
done = false;
while (!done) {
if (mdev_idx > IONIC_MAX_MDEV_SCAN)
break;
/* Look for a matching mnic */
snprintf(devname, IONIC_MAX_NAME_LEN,
IONIC_MNIC "%d", mdev_idx);
uio_idx = uio_get_idx_for_devname(name_cache, devname);
if (uio_idx >= 0) {
map = &ionic_mdev_map[i];
map->uio_idx = (uint16_t)uio_idx;
strlcpy(map->mdev_name, devname,
IONIC_MAX_NAME_LEN);
done = true;
}
mdev_idx++;
}
}
}
RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_scan_mcrypt_devices)
void
ionic_uio_scan_mcrypt_devices(void)
{
struct ionic_map_tbl *map;
char devname[IONIC_MAX_NAME_LEN];
struct uio_name name_cache[IONIC_MAX_DEVICES];
bool done;
int mdev_idx = 0;
int uio_idx;
int i;
static bool scan_done;
if (scan_done)
return;
scan_done = true;
uio_fill_name_cache(name_cache, IONIC_MCRYPT);
for (i = IONIC_MAX_MNETS; i < IONIC_MAX_DEVICES; i++) {
done = false;
while (!done) {
if (mdev_idx > IONIC_MAX_MDEV_SCAN)
break;
/* Look for a matching mcrypt */
snprintf(devname, IONIC_MAX_NAME_LEN,
IONIC_MCRYPT "%d", mdev_idx);
uio_idx = uio_get_idx_for_devname(name_cache, devname);
if (uio_idx >= 0) {
map = &ionic_mdev_map[i];
map->uio_idx = (uint16_t)uio_idx;
strlcpy(map->mdev_name, devname,
IONIC_MAX_NAME_LEN);
done = true;
}
mdev_idx++;
}
}
}
static int
uio_get_multi_dev_uionum(const char *name)
{
struct ionic_map_tbl *map;
int i;
for (i = 0; i < IONIC_MAX_DEVICES; i++) {
map = &ionic_mdev_map[i];
if (strncmp(map->dev_name, name, IONIC_MAX_NAME_LEN) == 0) {
if (map->uio_idx == IONIC_MAX_U16_IDX)
return -1;
else
return map->uio_idx;
}
}
return -1;
}
static unsigned long
uio_get_res_size(int uio_idx, int res_idx)
{
unsigned long size;
char file[64];
FILE *fp;
sprintf(file, "/sys/class/uio/uio%d/maps/map%d/size",
uio_idx, res_idx);
fp = fopen(file, "r");
if (fp == NULL)
return 0;
if (fscanf(fp, "0x%lx", &size) != 1)
size = 0;
fclose(fp);
return size;
}
static unsigned long
uio_get_res_phy_addr_offs(int uio_idx, int res_idx)
{
unsigned long offset;
char file[64];
FILE *fp;
sprintf(file, "/sys/class/uio/uio%d/maps/map%d/offset",
uio_idx, res_idx);
fp = fopen(file, "r");
if (fp == NULL)
return 0;
if (fscanf(fp, "0x%lx", &offset) != 1)
offset = 0;
fclose(fp);
return offset;
}
static unsigned long
uio_get_res_phy_addr(int uio_idx, int res_idx)
{
unsigned long addr;
char file[64];
FILE *fp;
sprintf(file, "/sys/class/uio/uio%d/maps/map%d/addr",
uio_idx, res_idx);
fp = fopen(file, "r");
if (fp == NULL)
return 0;
if (fscanf(fp, "0x%lx", &addr) != 1)
addr = 0;
fclose(fp);
return addr;
}
static void *
uio_get_map_res_addr(int uio_idx, int size, int res_idx)
{
char name[64];
int fd;
void *addr;
if (size == 0)
return NULL;
sprintf(name, "/dev/uio%d", uio_idx);
fd = open(name, O_RDWR);
if (fd < 0)
return NULL;
if (size < getpagesize())
size = getpagesize();
addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, res_idx * getpagesize());
close(fd);
return addr;
}
RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_get_rsrc)
void
ionic_uio_get_rsrc(const char *name, int idx, struct ionic_dev_bar *bar)
{
int num;
int offs;
num = uio_get_multi_dev_uionum(name);
if (num < 0)
return;
bar->len = uio_get_res_size(num, idx);
offs = uio_get_res_phy_addr_offs(num, idx);
bar->bus_addr = uio_get_res_phy_addr(num, idx);
bar->bus_addr += offs;
bar->vaddr = uio_get_map_res_addr(num, bar->len, idx);
bar->vaddr = ((char *)bar->vaddr) + offs;
}
RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_rel_rsrc)
void
ionic_uio_rel_rsrc(const char *name, int idx, struct ionic_dev_bar *bar)
{
int num, offs;
num = uio_get_multi_dev_uionum(name);
if (num < 0)
return;
if (bar->vaddr == NULL)
return;
offs = uio_get_res_phy_addr_offs(num, idx);
munmap(((char *)bar->vaddr) - offs, bar->len);
}
|