Skip to content

Commit 119c68e

Browse files
dtatuleamstsirkin
authored andcommitted
vdpa/mlx5: Allow creation/deletion of any given mr struct
This patch adapts the mr creation/deletion code to be able to work with any given mr struct pointer. All the APIs are adapted to take an extra parameter for the mr. mlx5_vdpa_create/delete_mr doesn't need a ASID parameter anymore. The check is done in the caller instead (mlx5_set_map). This change is needed for a followup patch which will introduce an additional mr for the vq descriptor data. Signed-off-by: Dragos Tatulea <dtatulea@nvidia.com> Acked-by: Eugenio Pérez <eperezma@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Message-Id: <20231018171456.1624030-12-dtatulea@nvidia.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 39f7c12 commit 119c68e

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

drivers/vdpa/mlx5/core/mlx5_vdpa.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ int mlx5_vdpa_create_mkey(struct mlx5_vdpa_dev *mvdev, u32 *mkey, u32 *in,
116116
int mlx5_vdpa_destroy_mkey(struct mlx5_vdpa_dev *mvdev, u32 mkey);
117117
int mlx5_vdpa_handle_set_map(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
118118
bool *change_map, unsigned int asid);
119-
int mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
120-
unsigned int asid);
119+
int mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev,
120+
struct mlx5_vdpa_mr *mr,
121+
struct vhost_iotlb *iotlb);
121122
void mlx5_vdpa_destroy_mr_resources(struct mlx5_vdpa_dev *mvdev);
122-
void mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev, unsigned int asid);
123+
void mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev,
124+
struct mlx5_vdpa_mr *mr);
123125
int mlx5_vdpa_update_cvq_iotlb(struct mlx5_vdpa_dev *mvdev,
124126
struct vhost_iotlb *iotlb,
125127
unsigned int asid);

drivers/vdpa/mlx5/core/mr.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,13 @@ static void unmap_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct
301301
sg_free_table(&mr->sg_head);
302302
}
303303

304-
static int add_direct_chain(struct mlx5_vdpa_dev *mvdev, u64 start, u64 size, u8 perm,
304+
static int add_direct_chain(struct mlx5_vdpa_dev *mvdev,
305+
struct mlx5_vdpa_mr *mr,
306+
u64 start,
307+
u64 size,
308+
u8 perm,
305309
struct vhost_iotlb *iotlb)
306310
{
307-
struct mlx5_vdpa_mr *mr = &mvdev->mr;
308311
struct mlx5_vdpa_direct_mr *dmr;
309312
struct mlx5_vdpa_direct_mr *n;
310313
LIST_HEAD(tmp);
@@ -354,9 +357,10 @@ static int add_direct_chain(struct mlx5_vdpa_dev *mvdev, u64 start, u64 size, u8
354357
* indirect memory key that provides access to the enitre address space given
355358
* by iotlb.
356359
*/
357-
static int create_user_mr(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb)
360+
static int create_user_mr(struct mlx5_vdpa_dev *mvdev,
361+
struct mlx5_vdpa_mr *mr,
362+
struct vhost_iotlb *iotlb)
358363
{
359-
struct mlx5_vdpa_mr *mr = &mvdev->mr;
360364
struct mlx5_vdpa_direct_mr *dmr;
361365
struct mlx5_vdpa_direct_mr *n;
362366
struct vhost_iotlb_map *map;
@@ -384,7 +388,7 @@ static int create_user_mr(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb
384388
LOG_MAX_KLM_SIZE);
385389
mr->num_klms += nnuls;
386390
}
387-
err = add_direct_chain(mvdev, ps, pe - ps, pperm, iotlb);
391+
err = add_direct_chain(mvdev, mr, ps, pe - ps, pperm, iotlb);
388392
if (err)
389393
goto err_chain;
390394
}
@@ -393,7 +397,7 @@ static int create_user_mr(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb
393397
pperm = map->perm;
394398
}
395399
}
396-
err = add_direct_chain(mvdev, ps, pe - ps, pperm, iotlb);
400+
err = add_direct_chain(mvdev, mr, ps, pe - ps, pperm, iotlb);
397401
if (err)
398402
goto err_chain;
399403

@@ -489,13 +493,8 @@ static void destroy_user_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_mr *mr
489493
}
490494
}
491495

492-
static void _mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev, unsigned int asid)
496+
static void _mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_mr *mr)
493497
{
494-
struct mlx5_vdpa_mr *mr = &mvdev->mr;
495-
496-
if (mvdev->group2asid[MLX5_VDPA_DATAVQ_GROUP] != asid)
497-
return;
498-
499498
if (!mr->initialized)
500499
return;
501500

@@ -507,38 +506,33 @@ static void _mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev, unsigned int asid
507506
mr->initialized = false;
508507
}
509508

510-
void mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev, unsigned int asid)
509+
void mlx5_vdpa_destroy_mr(struct mlx5_vdpa_dev *mvdev,
510+
struct mlx5_vdpa_mr *mr)
511511
{
512-
struct mlx5_vdpa_mr *mr = &mvdev->mr;
513-
514512
mutex_lock(&mr->mkey_mtx);
515513

516-
_mlx5_vdpa_destroy_mr(mvdev, asid);
514+
_mlx5_vdpa_destroy_mr(mvdev, mr);
517515

518516
mutex_unlock(&mr->mkey_mtx);
519517
}
520518

521519
void mlx5_vdpa_destroy_mr_resources(struct mlx5_vdpa_dev *mvdev)
522520
{
523-
mlx5_vdpa_destroy_mr(mvdev, mvdev->group2asid[MLX5_VDPA_DATAVQ_GROUP]);
521+
mlx5_vdpa_destroy_mr(mvdev, &mvdev->mr);
524522
prune_iotlb(mvdev);
525523
}
526524

527525
static int _mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev,
528-
struct vhost_iotlb *iotlb,
529-
unsigned int asid)
526+
struct mlx5_vdpa_mr *mr,
527+
struct vhost_iotlb *iotlb)
530528
{
531-
struct mlx5_vdpa_mr *mr = &mvdev->mr;
532529
int err;
533530

534-
if (mvdev->group2asid[MLX5_VDPA_DATAVQ_GROUP] != asid)
535-
return 0;
536-
537531
if (mr->initialized)
538532
return 0;
539533

540534
if (iotlb)
541-
err = create_user_mr(mvdev, iotlb);
535+
err = create_user_mr(mvdev, mr, iotlb);
542536
else
543537
err = create_dma_mr(mvdev, mr);
544538

@@ -550,13 +544,14 @@ static int _mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev,
550544
return 0;
551545
}
552546

553-
int mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
554-
unsigned int asid)
547+
int mlx5_vdpa_create_mr(struct mlx5_vdpa_dev *mvdev,
548+
struct mlx5_vdpa_mr *mr,
549+
struct vhost_iotlb *iotlb)
555550
{
556551
int err;
557552

558553
mutex_lock(&mvdev->mr.mkey_mtx);
559-
err = _mlx5_vdpa_create_mr(mvdev, iotlb, asid);
554+
err = _mlx5_vdpa_create_mr(mvdev, mr, iotlb);
560555
mutex_unlock(&mvdev->mr.mkey_mtx);
561556
return err;
562557
}
@@ -574,7 +569,7 @@ int mlx5_vdpa_handle_set_map(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *io
574569
*change_map = true;
575570
}
576571
if (!*change_map)
577-
err = _mlx5_vdpa_create_mr(mvdev, iotlb, asid);
572+
err = _mlx5_vdpa_create_mr(mvdev, mr, iotlb);
578573
mutex_unlock(&mr->mkey_mtx);
579574

580575
return err;
@@ -603,7 +598,7 @@ int mlx5_vdpa_create_dma_mr(struct mlx5_vdpa_dev *mvdev)
603598
{
604599
int err;
605600

606-
err = mlx5_vdpa_create_mr(mvdev, NULL, 0);
601+
err = mlx5_vdpa_create_mr(mvdev, &mvdev->mr, NULL);
607602
if (err)
608603
return err;
609604

drivers/vdpa/mlx5/net/mlx5_vnet.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,8 +2690,8 @@ static int mlx5_vdpa_change_map(struct mlx5_vdpa_dev *mvdev,
26902690
goto err_mr;
26912691

26922692
teardown_driver(ndev);
2693-
mlx5_vdpa_destroy_mr(mvdev, asid);
2694-
err = mlx5_vdpa_create_mr(mvdev, iotlb, asid);
2693+
mlx5_vdpa_destroy_mr(mvdev, &mvdev->mr);
2694+
err = mlx5_vdpa_create_mr(mvdev, &mvdev->mr, iotlb);
26952695
if (err)
26962696
goto err_mr;
26972697

@@ -2706,7 +2706,7 @@ static int mlx5_vdpa_change_map(struct mlx5_vdpa_dev *mvdev,
27062706
return 0;
27072707

27082708
err_setup:
2709-
mlx5_vdpa_destroy_mr(mvdev, asid);
2709+
mlx5_vdpa_destroy_mr(mvdev, &mvdev->mr);
27102710
err_mr:
27112711
return err;
27122712
}
@@ -2928,6 +2928,9 @@ static int set_map_data(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
29282928
bool change_map;
29292929
int err;
29302930

2931+
if (mvdev->group2asid[MLX5_VDPA_DATAVQ_GROUP] != asid)
2932+
goto end;
2933+
29312934
err = mlx5_vdpa_handle_set_map(mvdev, iotlb, &change_map, asid);
29322935
if (err) {
29332936
mlx5_vdpa_warn(mvdev, "set map failed(%d)\n", err);
@@ -2940,6 +2943,7 @@ static int set_map_data(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb *iotlb,
29402943
return err;
29412944
}
29422945

2946+
end:
29432947
return mlx5_vdpa_update_cvq_iotlb(mvdev, iotlb, asid);
29442948
}
29452949

0 commit comments

Comments
 (0)