1 // SPDX-License-Identifier: GPL-2.0 2 /* Marvell RVU PF/VF Netdev Devlink 3 * 4 * Copyright (C) 2021 Marvell. 5 */ 6 7 #include "otx2_common.h" 8 9 /* Devlink Params APIs */ 10 static int otx2_dl_mcam_count_validate(struct devlink *devlink, u32 id, 11 union devlink_param_value val, 12 struct netlink_ext_ack *extack) 13 { 14 struct otx2_devlink *otx2_dl = devlink_priv(devlink); 15 struct otx2_nic *pfvf = otx2_dl->pfvf; 16 struct otx2_flow_config *flow_cfg; 17 18 if (!pfvf->flow_cfg) { 19 NL_SET_ERR_MSG_MOD(extack, 20 "pfvf->flow_cfg not initialized"); 21 return -EINVAL; 22 } 23 24 flow_cfg = pfvf->flow_cfg; 25 if (flow_cfg && flow_cfg->nr_flows) { 26 NL_SET_ERR_MSG_MOD(extack, 27 "Cannot modify count when there are active rules"); 28 return -EINVAL; 29 } 30 31 return 0; 32 } 33 34 static int otx2_dl_mcam_count_set(struct devlink *devlink, u32 id, 35 struct devlink_param_gset_ctx *ctx, 36 struct netlink_ext_ack *extack) 37 { 38 struct otx2_devlink *otx2_dl = devlink_priv(devlink); 39 struct otx2_nic *pfvf = otx2_dl->pfvf; 40 41 if (!pfvf->flow_cfg) 42 return 0; 43 44 otx2_alloc_mcam_entries(pfvf, ctx->val.vu16); 45 46 return 0; 47 } 48 49 static int otx2_dl_mcam_count_get(struct devlink *devlink, u32 id, 50 struct devlink_param_gset_ctx *ctx) 51 { 52 struct otx2_devlink *otx2_dl = devlink_priv(devlink); 53 struct otx2_nic *pfvf = otx2_dl->pfvf; 54 struct otx2_flow_config *flow_cfg; 55 56 if (!pfvf->flow_cfg) { 57 ctx->val.vu16 = 0; 58 return 0; 59 } 60 61 flow_cfg = pfvf->flow_cfg; 62 ctx->val.vu16 = flow_cfg->max_flows; 63 64 return 0; 65 } 66 67 enum otx2_dl_param_id { 68 OTX2_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX, 69 OTX2_DEVLINK_PARAM_ID_MCAM_COUNT, 70 }; 71 72 static const struct devlink_param otx2_dl_params[] = { 73 DEVLINK_PARAM_DRIVER(OTX2_DEVLINK_PARAM_ID_MCAM_COUNT, 74 "mcam_count", DEVLINK_PARAM_TYPE_U16, 75 BIT(DEVLINK_PARAM_CMODE_RUNTIME), 76 otx2_dl_mcam_count_get, otx2_dl_mcam_count_set, 77 otx2_dl_mcam_count_validate), 78 }; 79 80 static const struct devlink_ops otx2_devlink_ops = { 81 }; 82 83 int otx2_register_dl(struct otx2_nic *pfvf) 84 { 85 struct otx2_devlink *otx2_dl; 86 struct devlink *dl; 87 int err; 88 89 dl = devlink_alloc(&otx2_devlink_ops, 90 sizeof(struct otx2_devlink), pfvf->dev); 91 if (!dl) { 92 dev_warn(pfvf->dev, "devlink_alloc failed\n"); 93 return -ENOMEM; 94 } 95 96 otx2_dl = devlink_priv(dl); 97 otx2_dl->dl = dl; 98 otx2_dl->pfvf = pfvf; 99 pfvf->dl = otx2_dl; 100 101 err = devlink_params_register(dl, otx2_dl_params, 102 ARRAY_SIZE(otx2_dl_params)); 103 if (err) { 104 dev_err(pfvf->dev, 105 "devlink params register failed with error %d", err); 106 goto err_dl; 107 } 108 109 devlink_register(dl); 110 return 0; 111 112 err_dl: 113 devlink_free(dl); 114 return err; 115 } 116 117 void otx2_unregister_dl(struct otx2_nic *pfvf) 118 { 119 struct otx2_devlink *otx2_dl = pfvf->dl; 120 struct devlink *dl = otx2_dl->dl; 121 122 devlink_unregister(dl); 123 devlink_params_unregister(dl, otx2_dl_params, 124 ARRAY_SIZE(otx2_dl_params)); 125 devlink_free(dl); 126 } 127