1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ 3 4 #include <linux/rtnetlink.h> 5 #include <net/devlink.h> 6 7 #include "nfpcore/nfp.h" 8 #include "nfpcore/nfp_nsp.h" 9 #include "nfp_app.h" 10 #include "nfp_main.h" 11 #include "nfp_port.h" 12 13 static int 14 nfp_devlink_fill_eth_port(struct nfp_port *port, 15 struct nfp_eth_table_port *copy) 16 { 17 struct nfp_eth_table_port *eth_port; 18 19 eth_port = __nfp_port_get_eth_port(port); 20 if (!eth_port) 21 return -EINVAL; 22 23 memcpy(copy, eth_port, sizeof(*eth_port)); 24 25 return 0; 26 } 27 28 static int 29 nfp_devlink_fill_eth_port_from_id(struct nfp_pf *pf, 30 struct devlink_port *dl_port, 31 struct nfp_eth_table_port *copy) 32 { 33 struct nfp_port *port = container_of(dl_port, struct nfp_port, dl_port); 34 35 return nfp_devlink_fill_eth_port(port, copy); 36 } 37 38 static int 39 nfp_devlink_set_lanes(struct nfp_pf *pf, unsigned int idx, unsigned int lanes) 40 { 41 struct nfp_nsp *nsp; 42 int ret; 43 44 nsp = nfp_eth_config_start(pf->cpp, idx); 45 if (IS_ERR(nsp)) 46 return PTR_ERR(nsp); 47 48 ret = __nfp_eth_set_split(nsp, lanes); 49 if (ret) { 50 nfp_eth_config_cleanup_end(nsp); 51 return ret; 52 } 53 54 ret = nfp_eth_config_commit_end(nsp); 55 if (ret < 0) 56 return ret; 57 if (ret) /* no change */ 58 return 0; 59 60 return nfp_net_refresh_port_table_sync(pf); 61 } 62 63 static int 64 nfp_devlink_port_split(struct devlink *devlink, struct devlink_port *port, 65 unsigned int count, struct netlink_ext_ack *extack) 66 { 67 struct nfp_pf *pf = devlink_priv(devlink); 68 struct nfp_eth_table_port eth_port; 69 unsigned int lanes; 70 int ret; 71 72 rtnl_lock(); 73 ret = nfp_devlink_fill_eth_port_from_id(pf, port, ð_port); 74 rtnl_unlock(); 75 if (ret) 76 return ret; 77 78 if (eth_port.port_lanes % count) 79 return -EINVAL; 80 81 /* Special case the 100G CXP -> 2x40G split */ 82 lanes = eth_port.port_lanes / count; 83 if (eth_port.lanes == 10 && count == 2) 84 lanes = 8 / count; 85 86 return nfp_devlink_set_lanes(pf, eth_port.index, lanes); 87 } 88 89 static int 90 nfp_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port, 91 struct netlink_ext_ack *extack) 92 { 93 struct nfp_pf *pf = devlink_priv(devlink); 94 struct nfp_eth_table_port eth_port; 95 unsigned int lanes; 96 int ret; 97 98 rtnl_lock(); 99 ret = nfp_devlink_fill_eth_port_from_id(pf, port, ð_port); 100 rtnl_unlock(); 101 if (ret) 102 return ret; 103 104 if (!eth_port.is_split) 105 return -EINVAL; 106 107 /* Special case the 100G CXP -> 2x40G unsplit */ 108 lanes = eth_port.port_lanes; 109 if (eth_port.port_lanes == 8) 110 lanes = 10; 111 112 return nfp_devlink_set_lanes(pf, eth_port.index, lanes); 113 } 114 115 static int 116 nfp_devlink_sb_pool_get(struct devlink *devlink, unsigned int sb_index, 117 u16 pool_index, struct devlink_sb_pool_info *pool_info) 118 { 119 struct nfp_pf *pf = devlink_priv(devlink); 120 121 return nfp_shared_buf_pool_get(pf, sb_index, pool_index, pool_info); 122 } 123 124 static int 125 nfp_devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index, 126 u16 pool_index, 127 u32 size, enum devlink_sb_threshold_type threshold_type, 128 struct netlink_ext_ack *extack) 129 { 130 struct nfp_pf *pf = devlink_priv(devlink); 131 132 return nfp_shared_buf_pool_set(pf, sb_index, pool_index, 133 size, threshold_type); 134 } 135 136 static int nfp_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode) 137 { 138 struct nfp_pf *pf = devlink_priv(devlink); 139 140 return nfp_app_eswitch_mode_get(pf->app, mode); 141 } 142 143 static int nfp_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode, 144 struct netlink_ext_ack *extack) 145 { 146 struct nfp_pf *pf = devlink_priv(devlink); 147 148 return nfp_app_eswitch_mode_set(pf->app, mode); 149 } 150 151 static const struct nfp_devlink_versions_simple { 152 const char *key; 153 const char *hwinfo; 154 } nfp_devlink_versions_hwinfo[] = { 155 { DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, "assembly.partno", }, 156 { DEVLINK_INFO_VERSION_GENERIC_BOARD_REV, "assembly.revision", }, 157 { DEVLINK_INFO_VERSION_GENERIC_BOARD_MANUFACTURE, "assembly.vendor", }, 158 { "board.model", /* code name */ "assembly.model", }, 159 }; 160 161 static int 162 nfp_devlink_versions_get_hwinfo(struct nfp_pf *pf, struct devlink_info_req *req) 163 { 164 unsigned int i; 165 int err; 166 167 for (i = 0; i < ARRAY_SIZE(nfp_devlink_versions_hwinfo); i++) { 168 const struct nfp_devlink_versions_simple *info; 169 const char *val; 170 171 info = &nfp_devlink_versions_hwinfo[i]; 172 173 val = nfp_hwinfo_lookup(pf->hwinfo, info->hwinfo); 174 if (!val) 175 continue; 176 177 err = devlink_info_version_fixed_put(req, info->key, val); 178 if (err) 179 return err; 180 } 181 182 return 0; 183 } 184 185 static const struct nfp_devlink_versions { 186 enum nfp_nsp_versions id; 187 const char *key; 188 } nfp_devlink_versions_nsp[] = { 189 { NFP_VERSIONS_BUNDLE, DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, }, 190 { NFP_VERSIONS_BSP, DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, }, 191 { NFP_VERSIONS_CPLD, "fw.cpld", }, 192 { NFP_VERSIONS_APP, DEVLINK_INFO_VERSION_GENERIC_FW_APP, }, 193 { NFP_VERSIONS_UNDI, DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, }, 194 { NFP_VERSIONS_NCSI, DEVLINK_INFO_VERSION_GENERIC_FW_NCSI, }, 195 { NFP_VERSIONS_CFGR, "chip.init", }, 196 }; 197 198 static int 199 nfp_devlink_versions_get_nsp(struct devlink_info_req *req, bool flash, 200 const u8 *buf, unsigned int size) 201 { 202 unsigned int i; 203 int err; 204 205 for (i = 0; i < ARRAY_SIZE(nfp_devlink_versions_nsp); i++) { 206 const struct nfp_devlink_versions *info; 207 const char *version; 208 209 info = &nfp_devlink_versions_nsp[i]; 210 211 version = nfp_nsp_versions_get(info->id, flash, buf, size); 212 if (IS_ERR(version)) { 213 if (PTR_ERR(version) == -ENOENT) 214 continue; 215 else 216 return PTR_ERR(version); 217 } 218 219 if (flash) 220 err = devlink_info_version_stored_put(req, info->key, 221 version); 222 else 223 err = devlink_info_version_running_put(req, info->key, 224 version); 225 if (err) 226 return err; 227 } 228 229 return 0; 230 } 231 232 static int 233 nfp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 234 struct netlink_ext_ack *extack) 235 { 236 struct nfp_pf *pf = devlink_priv(devlink); 237 const char *sn, *vendor, *part; 238 struct nfp_nsp *nsp; 239 char *buf = NULL; 240 int err; 241 242 vendor = nfp_hwinfo_lookup(pf->hwinfo, "assembly.vendor"); 243 part = nfp_hwinfo_lookup(pf->hwinfo, "assembly.partno"); 244 sn = nfp_hwinfo_lookup(pf->hwinfo, "assembly.serial"); 245 if (vendor && part && sn) { 246 char *buf; 247 248 buf = kmalloc(strlen(vendor) + strlen(part) + strlen(sn) + 1, 249 GFP_KERNEL); 250 if (!buf) 251 return -ENOMEM; 252 253 buf[0] = '\0'; 254 strcat(buf, vendor); 255 strcat(buf, part); 256 strcat(buf, sn); 257 258 err = devlink_info_serial_number_put(req, buf); 259 kfree(buf); 260 if (err) 261 return err; 262 } 263 264 nsp = nfp_nsp_open(pf->cpp); 265 if (IS_ERR(nsp)) { 266 NL_SET_ERR_MSG_MOD(extack, "can't access NSP"); 267 return PTR_ERR(nsp); 268 } 269 270 if (nfp_nsp_has_versions(nsp)) { 271 buf = kzalloc(NFP_NSP_VERSION_BUFSZ, GFP_KERNEL); 272 if (!buf) { 273 err = -ENOMEM; 274 goto err_close_nsp; 275 } 276 277 err = nfp_nsp_versions(nsp, buf, NFP_NSP_VERSION_BUFSZ); 278 if (err) 279 goto err_free_buf; 280 281 err = nfp_devlink_versions_get_nsp(req, false, 282 buf, NFP_NSP_VERSION_BUFSZ); 283 if (err) 284 goto err_free_buf; 285 286 err = nfp_devlink_versions_get_nsp(req, true, 287 buf, NFP_NSP_VERSION_BUFSZ); 288 if (err) 289 goto err_free_buf; 290 291 kfree(buf); 292 } 293 294 nfp_nsp_close(nsp); 295 296 return nfp_devlink_versions_get_hwinfo(pf, req); 297 298 err_free_buf: 299 kfree(buf); 300 err_close_nsp: 301 nfp_nsp_close(nsp); 302 return err; 303 } 304 305 static int 306 nfp_devlink_flash_update(struct devlink *devlink, 307 struct devlink_flash_update_params *params, 308 struct netlink_ext_ack *extack) 309 { 310 return nfp_flash_update_common(devlink_priv(devlink), params->fw, extack); 311 } 312 313 const struct devlink_ops nfp_devlink_ops = { 314 .port_split = nfp_devlink_port_split, 315 .port_unsplit = nfp_devlink_port_unsplit, 316 .sb_pool_get = nfp_devlink_sb_pool_get, 317 .sb_pool_set = nfp_devlink_sb_pool_set, 318 .eswitch_mode_get = nfp_devlink_eswitch_mode_get, 319 .eswitch_mode_set = nfp_devlink_eswitch_mode_set, 320 .info_get = nfp_devlink_info_get, 321 .flash_update = nfp_devlink_flash_update, 322 }; 323 324 int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port) 325 { 326 struct devlink_port_attrs attrs = {}; 327 struct nfp_eth_table_port eth_port; 328 struct devlink *devlink; 329 const u8 *serial; 330 int serial_len; 331 int ret; 332 333 SET_NETDEV_DEVLINK_PORT(port->netdev, &port->dl_port); 334 335 rtnl_lock(); 336 ret = nfp_devlink_fill_eth_port(port, ð_port); 337 rtnl_unlock(); 338 if (ret) 339 return ret; 340 341 attrs.split = eth_port.is_split; 342 attrs.splittable = eth_port.port_lanes > 1 && !attrs.split; 343 attrs.lanes = eth_port.port_lanes; 344 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 345 attrs.phys.port_number = eth_port.label_port; 346 attrs.phys.split_subport_number = eth_port.label_subport; 347 serial_len = nfp_cpp_serial(port->app->cpp, &serial); 348 memcpy(attrs.switch_id.id, serial, serial_len); 349 attrs.switch_id.id_len = serial_len; 350 devlink_port_attrs_set(&port->dl_port, &attrs); 351 352 devlink = priv_to_devlink(app->pf); 353 354 return devl_port_register(devlink, &port->dl_port, port->eth_id); 355 } 356 357 void nfp_devlink_port_unregister(struct nfp_port *port) 358 { 359 devl_port_unregister(&port->dl_port); 360 } 361