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 { 37 struct otx2_devlink *otx2_dl = devlink_priv(devlink); 38 struct otx2_nic *pfvf = otx2_dl->pfvf; 39 40 if (!pfvf->flow_cfg) 41 return 0; 42 43 otx2_alloc_mcam_entries(pfvf, ctx->val.vu16); 44 otx2_tc_alloc_ent_bitmap(pfvf); 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 /* Devlink OPs */ 81 static int otx2_devlink_info_get(struct devlink *devlink, 82 struct devlink_info_req *req, 83 struct netlink_ext_ack *extack) 84 { 85 struct otx2_devlink *otx2_dl = devlink_priv(devlink); 86 struct otx2_nic *pfvf = otx2_dl->pfvf; 87 88 if (is_otx2_vf(pfvf->pcifunc)) 89 return devlink_info_driver_name_put(req, "rvu_nicvf"); 90 91 return devlink_info_driver_name_put(req, "rvu_nicpf"); 92 } 93 94 static const struct devlink_ops otx2_devlink_ops = { 95 .info_get = otx2_devlink_info_get, 96 }; 97 98 int otx2_register_dl(struct otx2_nic *pfvf) 99 { 100 struct otx2_devlink *otx2_dl; 101 struct devlink *dl; 102 int err; 103 104 dl = devlink_alloc(&otx2_devlink_ops, 105 sizeof(struct otx2_devlink), pfvf->dev); 106 if (!dl) { 107 dev_warn(pfvf->dev, "devlink_alloc failed\n"); 108 return -ENOMEM; 109 } 110 111 err = devlink_register(dl); 112 if (err) { 113 dev_err(pfvf->dev, "devlink register failed with error %d\n", err); 114 devlink_free(dl); 115 return err; 116 } 117 118 otx2_dl = devlink_priv(dl); 119 otx2_dl->dl = dl; 120 otx2_dl->pfvf = pfvf; 121 pfvf->dl = otx2_dl; 122 123 err = devlink_params_register(dl, otx2_dl_params, 124 ARRAY_SIZE(otx2_dl_params)); 125 if (err) { 126 dev_err(pfvf->dev, 127 "devlink params register failed with error %d", err); 128 goto err_dl; 129 } 130 131 devlink_params_publish(dl); 132 133 return 0; 134 135 err_dl: 136 devlink_unregister(dl); 137 devlink_free(dl); 138 return err; 139 } 140 141 void otx2_unregister_dl(struct otx2_nic *pfvf) 142 { 143 struct otx2_devlink *otx2_dl = pfvf->dl; 144 struct devlink *dl; 145 146 if (!otx2_dl || !otx2_dl->dl) 147 return; 148 149 dl = otx2_dl->dl; 150 151 devlink_params_unregister(dl, otx2_dl_params, 152 ARRAY_SIZE(otx2_dl_params)); 153 154 devlink_unregister(dl); 155 devlink_free(dl); 156 } 157