18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * This file contains all networking devres helpers. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/device.h> 78c2ecf20Sopenharmony_ci#include <linux/etherdevice.h> 88c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_cistruct net_device_devres { 118c2ecf20Sopenharmony_ci struct net_device *ndev; 128c2ecf20Sopenharmony_ci}; 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic void devm_free_netdev(struct device *dev, void *this) 158c2ecf20Sopenharmony_ci{ 168c2ecf20Sopenharmony_ci struct net_device_devres *res = this; 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci free_netdev(res->ndev); 198c2ecf20Sopenharmony_ci} 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv, 228c2ecf20Sopenharmony_ci unsigned int txqs, unsigned int rxqs) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci struct net_device_devres *dr; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci dr = devres_alloc(devm_free_netdev, sizeof(*dr), GFP_KERNEL); 278c2ecf20Sopenharmony_ci if (!dr) 288c2ecf20Sopenharmony_ci return NULL; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci dr->ndev = alloc_etherdev_mqs(sizeof_priv, txqs, rxqs); 318c2ecf20Sopenharmony_ci if (!dr->ndev) { 328c2ecf20Sopenharmony_ci devres_free(dr); 338c2ecf20Sopenharmony_ci return NULL; 348c2ecf20Sopenharmony_ci } 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci devres_add(dev, dr); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci return dr->ndev; 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ciEXPORT_SYMBOL(devm_alloc_etherdev_mqs); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic void devm_unregister_netdev(struct device *dev, void *this) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct net_device_devres *res = this; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci unregister_netdev(res->ndev); 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic int netdev_devres_match(struct device *dev, void *this, void *match_data) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci struct net_device_devres *res = this; 528c2ecf20Sopenharmony_ci struct net_device *ndev = match_data; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return ndev == res->ndev; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/** 588c2ecf20Sopenharmony_ci * devm_register_netdev - resource managed variant of register_netdev() 598c2ecf20Sopenharmony_ci * @dev: managing device for this netdev - usually the parent device 608c2ecf20Sopenharmony_ci * @ndev: device to register 618c2ecf20Sopenharmony_ci * 628c2ecf20Sopenharmony_ci * This is a devres variant of register_netdev() for which the unregister 638c2ecf20Sopenharmony_ci * function will be call automatically when the managing device is 648c2ecf20Sopenharmony_ci * detached. Note: the net_device used must also be resource managed by 658c2ecf20Sopenharmony_ci * the same struct device. 668c2ecf20Sopenharmony_ci */ 678c2ecf20Sopenharmony_ciint devm_register_netdev(struct device *dev, struct net_device *ndev) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct net_device_devres *dr; 708c2ecf20Sopenharmony_ci int ret; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* struct net_device must itself be managed. For now a managed netdev 738c2ecf20Sopenharmony_ci * can only be allocated by devm_alloc_etherdev_mqs() so the check is 748c2ecf20Sopenharmony_ci * straightforward. 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_ci if (WARN_ON(!devres_find(dev, devm_free_netdev, 778c2ecf20Sopenharmony_ci netdev_devres_match, ndev))) 788c2ecf20Sopenharmony_ci return -EINVAL; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci dr = devres_alloc(devm_unregister_netdev, sizeof(*dr), GFP_KERNEL); 818c2ecf20Sopenharmony_ci if (!dr) 828c2ecf20Sopenharmony_ci return -ENOMEM; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci ret = register_netdev(ndev); 858c2ecf20Sopenharmony_ci if (ret) { 868c2ecf20Sopenharmony_ci devres_free(dr); 878c2ecf20Sopenharmony_ci return ret; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci dr->ndev = ndev; 918c2ecf20Sopenharmony_ci devres_add(ndev->dev.parent, dr); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci return 0; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ciEXPORT_SYMBOL(devm_register_netdev); 96