| 1 |
/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ |
| 2 |
/* Copyright (c) 2019 Mellanox Technologies. */ |
| 3 |
|
| 4 |
#include "ecpf.h" |
| 5 |
#include "eswitch.h" |
| 6 |
|
| 7 |
bool mlx5_read_embedded_cpu(struct mlx5_core_dev *dev) |
| 8 |
{ |
| 9 |
return (ioread32be(&dev->iseg->initializing) >> MLX5_ECPU_BIT_NUM) & 1; |
| 10 |
} |
| 11 |
|
| 12 |
static bool mlx5_ecpf_esw_admins_host_pf(const struct mlx5_core_dev *dev) |
| 13 |
{ |
| 14 |
/* In separate host mode, PF enables itself. |
| 15 |
* When ECPF is eswitch manager, eswitch enables host PF after |
| 16 |
* eswitch is setup. |
| 17 |
*/ |
| 18 |
return mlx5_core_is_ecpf_esw_manager(dev); |
| 19 |
} |
| 20 |
|
| 21 |
int mlx5_cmd_host_pf_enable_hca(struct mlx5_core_dev *dev) |
| 22 |
{ |
| 23 |
u32 out[MLX5_ST_SZ_DW(enable_hca_out)] = {}; |
| 24 |
u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {}; |
| 25 |
|
| 26 |
MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA); |
| 27 |
MLX5_SET(enable_hca_in, in, function_id, 0); |
| 28 |
MLX5_SET(enable_hca_in, in, embedded_cpu_function, 0); |
| 29 |
return mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out)); |
| 30 |
} |
| 31 |
|
| 32 |
int mlx5_cmd_host_pf_disable_hca(struct mlx5_core_dev *dev) |
| 33 |
{ |
| 34 |
u32 out[MLX5_ST_SZ_DW(disable_hca_out)] = {}; |
| 35 |
u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {}; |
| 36 |
|
| 37 |
MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA); |
| 38 |
MLX5_SET(disable_hca_in, in, function_id, 0); |
| 39 |
MLX5_SET(disable_hca_in, in, embedded_cpu_function, 0); |
| 40 |
return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); |
| 41 |
} |
| 42 |
|
| 43 |
static int mlx5_host_pf_init(struct mlx5_core_dev *dev) |
| 44 |
{ |
| 45 |
int err; |
| 46 |
|
| 47 |
if (mlx5_ecpf_esw_admins_host_pf(dev)) |
| 48 |
return 0; |
| 49 |
|
| 50 |
/* ECPF shall enable HCA for host PF in the same way a PF |
| 51 |
* does this for its VFs when ECPF is not a eswitch manager. |
| 52 |
*/ |
| 53 |
err = mlx5_esw_host_pf_enable_hca(dev); |
| 54 |
if (err) |
| 55 |
mlx5_core_err(dev, "Failed to enable external host PF HCA err(%d)\n", err); |
| 56 |
|
| 57 |
return err; |
| 58 |
} |
| 59 |
|
| 60 |
static void mlx5_host_pf_cleanup(struct mlx5_core_dev *dev) |
| 61 |
{ |
| 62 |
int err; |
| 63 |
|
| 64 |
if (mlx5_ecpf_esw_admins_host_pf(dev)) |
| 65 |
return; |
| 66 |
|
| 67 |
err = mlx5_esw_host_pf_disable_hca(dev); |
| 68 |
if (err) { |
| 69 |
mlx5_core_err(dev, "Failed to disable external host PF HCA err(%d)\n", err); |
| 70 |
return; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
int mlx5_ec_init(struct mlx5_core_dev *dev) |
| 75 |
{ |
| 76 |
if (!mlx5_core_is_ecpf(dev)) |
| 77 |
return 0; |
| 78 |
|
| 79 |
return mlx5_host_pf_init(dev); |
| 80 |
} |
| 81 |
|
| 82 |
void mlx5_ec_cleanup(struct mlx5_core_dev *dev) |
| 83 |
{ |
| 84 |
int err; |
| 85 |
|
| 86 |
if (!mlx5_core_is_ecpf(dev)) |
| 87 |
return; |
| 88 |
|
| 89 |
mlx5_host_pf_cleanup(dev); |
| 90 |
|
| 91 |
err = mlx5_wait_for_pages(dev, &dev->priv.page_counters[MLX5_HOST_PF]); |
| 92 |
if (err) |
| 93 |
mlx5_core_warn(dev, "Timeout reclaiming external host PF pages err(%d)\n", err); |
| 94 |
|
| 95 |
err = mlx5_wait_for_pages(dev, &dev->priv.page_counters[MLX5_VF]); |
| 96 |
if (err) |
| 97 |
mlx5_core_warn(dev, "Timeout reclaiming external host VFs pages err(%d)\n", err); |
| 98 |
} |