1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2018 Mellanox Technologies
3 
4 #include <linux/hyperv.h>
5 #include "mlx5_core.h"
6 #include "lib/hv.h"
7 
mlx5_hv_config_common(struct mlx5_core_dev * dev,void * buf,int len,int offset,bool read)8 static int mlx5_hv_config_common(struct mlx5_core_dev *dev, void *buf, int len,
9 				 int offset, bool read)
10 {
11 	int rc = -EOPNOTSUPP;
12 	int bytes_returned;
13 	int block_id;
14 
15 	if (offset % HV_CONFIG_BLOCK_SIZE_MAX || len != HV_CONFIG_BLOCK_SIZE_MAX)
16 		return -EINVAL;
17 
18 	block_id = offset / HV_CONFIG_BLOCK_SIZE_MAX;
19 
20 	rc = read ?
21 	     hyperv_read_cfg_blk(dev->pdev, buf,
22 				 HV_CONFIG_BLOCK_SIZE_MAX, block_id,
23 				 &bytes_returned) :
24 	     hyperv_write_cfg_blk(dev->pdev, buf,
25 				  HV_CONFIG_BLOCK_SIZE_MAX, block_id);
26 
27 	/* Make sure len bytes were read successfully  */
28 	if (read && !rc && len != bytes_returned)
29 		rc = -EIO;
30 
31 	if (rc) {
32 		mlx5_core_err(dev, "Failed to %s hv config, err = %d, len = %d, offset = %d\n",
33 			      read ? "read" : "write", rc, len,
34 			      offset);
35 		return rc;
36 	}
37 
38 	return 0;
39 }
40 
mlx5_hv_read_config(struct mlx5_core_dev * dev,void * buf,int len,int offset)41 int mlx5_hv_read_config(struct mlx5_core_dev *dev, void *buf, int len,
42 			int offset)
43 {
44 	return mlx5_hv_config_common(dev, buf, len, offset, true);
45 }
46 
mlx5_hv_write_config(struct mlx5_core_dev * dev,void * buf,int len,int offset)47 int mlx5_hv_write_config(struct mlx5_core_dev *dev, void *buf, int len,
48 			 int offset)
49 {
50 	return mlx5_hv_config_common(dev, buf, len, offset, false);
51 }
52 
mlx5_hv_register_invalidate(struct mlx5_core_dev * dev,void * context,void (* block_invalidate)(void * context,u64 block_mask))53 int mlx5_hv_register_invalidate(struct mlx5_core_dev *dev, void *context,
54 				void (*block_invalidate)(void *context,
55 							 u64 block_mask))
56 {
57 	return hyperv_reg_block_invalidate(dev->pdev, context,
58 					   block_invalidate);
59 }
60 
mlx5_hv_unregister_invalidate(struct mlx5_core_dev * dev)61 void mlx5_hv_unregister_invalidate(struct mlx5_core_dev *dev)
62 {
63 	hyperv_reg_block_invalidate(dev->pdev, NULL, NULL);
64 }
65