Skip to content

Commit c0991e5

Browse files
committed
restore tph ctrl for uio and vfio
tph ctrl in config space (0x168) is reset by uio and vfio drivers, restore the settings for tlp processing hint from dsa to take effect. This is required for correct operation of the CC descriptor flag. Reviewed-by: Narayan Ranganathan <[email protected]> Signed-off-by: Nikhil Rao <[email protected]>
1 parent 7f5ce9b commit c0991e5

File tree

1 file changed

+98
-5
lines changed

1 file changed

+98
-5
lines changed

src/user_device.c

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,57 @@ pci_vfio_enable_bus_memory(int dev_fd)
258258
return 0;
259259
}
260260

261+
static int
262+
exec_cmd(char *cmd, uint32_t *v)
263+
{
264+
FILE *fp;
265+
int rc;
266+
267+
fp = popen(cmd, "r");
268+
if (!fp) {
269+
ERR("popen failed\n");
270+
return -1;
271+
}
272+
273+
rc = v ? fscanf(fp, "%x", v) == 1 ? 0 : -1 : 0;
274+
pclose(fp);
275+
276+
return rc;
277+
}
278+
279+
#define TPH_CTL "168"
280+
281+
static int
282+
read_tph(char *bdf, uint32_t *tph)
283+
{
284+
char *cmd;
285+
int rc;
286+
287+
rc = asprintf(&cmd, "setpci -s %s "TPH_CTL".l", bdf);
288+
if (rc < 0)
289+
return -1;
290+
rc = exec_cmd(cmd, tph);
291+
free(cmd);
292+
293+
return rc;
294+
}
295+
296+
static int
297+
write_tph(char *bdf, int32_t v)
298+
{
299+
char *cmd;
300+
int rc;
301+
302+
rc = asprintf(&cmd, "setpci -s %s "TPH_CTL".l=0x%x", bdf, v);
303+
if (rc < 0)
304+
return -1;
305+
306+
rc = exec_cmd(cmd, NULL);
307+
free(cmd);
308+
309+
return rc;
310+
}
311+
261312
static int
262313
uio_setup_device(char *bdf)
263314
{
@@ -1154,6 +1205,45 @@ find_free_wq_info(const void *key, const void *p2)
11541205

11551206
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
11561207

1208+
static int
1209+
common_init(int uio_cnt, struct udev_info *pd)
1210+
{
1211+
uint32_t tph, tmp;
1212+
int rc;
1213+
1214+
rc = read_tph(pd->bdf, &tph);
1215+
if (rc == -1) {
1216+
ERR("Error reading tph\n");
1217+
return -EIO;
1218+
}
1219+
1220+
if (!tph) {
1221+
ERR("TPH capability is disabled\n");
1222+
return -EINVAL;
1223+
}
1224+
1225+
rc = uio_cnt ? uio_init(pd) : vfio_init(pd);
1226+
if (rc)
1227+
return rc;
1228+
1229+
rc = read_tph(pd->bdf, &tmp);
1230+
if (rc == -1) {
1231+
ERR("Error reading tph\n");
1232+
return -EIO;
1233+
}
1234+
1235+
if (tmp == tph)
1236+
return 0;
1237+
1238+
rc = write_tph(pd->bdf, tph);
1239+
if (rc == -1) {
1240+
ERR("Failed to write tph value\n");
1241+
return -EIO;
1242+
}
1243+
1244+
return 0;
1245+
}
1246+
11571247
static struct ud_wq_info *
11581248
ud_wq_find(char *dname, int wq_id, int shared, int numa_node)
11591249
{
@@ -1199,11 +1289,14 @@ ud_wq_find(char *dname, int wq_id, int shared, int numa_node)
11991289
wq_id = __builtin_ffs(pd->wq_avail) - 1;
12001290

12011291
if (!pd->init_done) {
1202-
rc = uio_cnt ? uio_init(pd) : vfio_init(pd);
1203-
if (!rc)
1204-
pd->init_done = true;
1205-
if (dname && rc)
1206-
break;
1292+
rc = common_init(uio_cnt, pd);
1293+
if (rc) {
1294+
if (dname)
1295+
break;
1296+
else
1297+
continue;
1298+
}
1299+
pd->init_done = true;
12071300
}
12081301

12091302
idxd_pci_dev_command(pd->pci, idxd_enable_wq);

0 commit comments

Comments
 (0)