18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Linux WiMAX 48c2ecf20Sopenharmony_ci * Mappping of generic netlink family IDs to net devices 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com> 78c2ecf20Sopenharmony_ci * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * We assign a single generic netlink family ID to each device (to 108c2ecf20Sopenharmony_ci * simplify lookup). 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * We need a way to map family ID to a wimax_dev pointer. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * The idea is to use a very simple lookup. Using a netlink attribute 158c2ecf20Sopenharmony_ci * with (for example) the interface name implies a heavier search over 168c2ecf20Sopenharmony_ci * all the network devices; seemed kind of a waste given that we know 178c2ecf20Sopenharmony_ci * we are looking for a WiMAX device and that most systems will have 188c2ecf20Sopenharmony_ci * just a single WiMAX adapter. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * We put all the WiMAX devices in the system in a linked list and 218c2ecf20Sopenharmony_ci * match the generic link family ID against the list. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * By using a linked list, the case of a single adapter in the system 248c2ecf20Sopenharmony_ci * becomes (almost) no overhead, while still working for many more. If 258c2ecf20Sopenharmony_ci * it ever goes beyond two, I'll be surprised. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci#include <linux/device.h> 288c2ecf20Sopenharmony_ci#include <net/genetlink.h> 298c2ecf20Sopenharmony_ci#include <linux/netdevice.h> 308c2ecf20Sopenharmony_ci#include <linux/list.h> 318c2ecf20Sopenharmony_ci#include <linux/wimax.h> 328c2ecf20Sopenharmony_ci#include "wimax-internal.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define D_SUBMODULE id_table 368c2ecf20Sopenharmony_ci#include "debug-levels.h" 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(wimax_id_table_lock); 408c2ecf20Sopenharmony_cistatic struct list_head wimax_id_table = LIST_HEAD_INIT(wimax_id_table); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* 448c2ecf20Sopenharmony_ci * wimax_id_table_add - add a gennetlink familiy ID / wimax_dev mapping 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci * @wimax_dev: WiMAX device descriptor to associate to the Generic 478c2ecf20Sopenharmony_ci * Netlink family ID. 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * Look for an empty spot in the ID table; if none found, double the 508c2ecf20Sopenharmony_ci * table's size and get the first spot. 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_civoid wimax_id_table_add(struct wimax_dev *wimax_dev) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev); 558c2ecf20Sopenharmony_ci spin_lock(&wimax_id_table_lock); 568c2ecf20Sopenharmony_ci list_add(&wimax_dev->id_table_node, &wimax_id_table); 578c2ecf20Sopenharmony_ci spin_unlock(&wimax_id_table_lock); 588c2ecf20Sopenharmony_ci d_fnend(3, NULL, "(wimax_dev %p)\n", wimax_dev); 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* 638c2ecf20Sopenharmony_ci * wimax_get_netdev_by_info - lookup a wimax_dev from the gennetlink info 648c2ecf20Sopenharmony_ci * 658c2ecf20Sopenharmony_ci * The generic netlink family ID has been filled out in the 668c2ecf20Sopenharmony_ci * nlmsghdr->nlmsg_type field, so we pull it from there, look it up in 678c2ecf20Sopenharmony_ci * the mapping table and reference the wimax_dev. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * When done, the reference should be dropped with 708c2ecf20Sopenharmony_ci * 'dev_put(wimax_dev->net_dev)'. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistruct wimax_dev *wimax_dev_get_by_genl_info( 738c2ecf20Sopenharmony_ci struct genl_info *info, int ifindex) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci struct wimax_dev *wimax_dev = NULL; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci d_fnstart(3, NULL, "(info %p ifindex %d)\n", info, ifindex); 788c2ecf20Sopenharmony_ci spin_lock(&wimax_id_table_lock); 798c2ecf20Sopenharmony_ci list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) { 808c2ecf20Sopenharmony_ci if (wimax_dev->net_dev->ifindex == ifindex) { 818c2ecf20Sopenharmony_ci dev_hold(wimax_dev->net_dev); 828c2ecf20Sopenharmony_ci goto found; 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci } 858c2ecf20Sopenharmony_ci wimax_dev = NULL; 868c2ecf20Sopenharmony_ci d_printf(1, NULL, "wimax: no devices found with ifindex %d\n", 878c2ecf20Sopenharmony_ci ifindex); 888c2ecf20Sopenharmony_cifound: 898c2ecf20Sopenharmony_ci spin_unlock(&wimax_id_table_lock); 908c2ecf20Sopenharmony_ci d_fnend(3, NULL, "(info %p ifindex %d) = %p\n", 918c2ecf20Sopenharmony_ci info, ifindex, wimax_dev); 928c2ecf20Sopenharmony_ci return wimax_dev; 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/* 978c2ecf20Sopenharmony_ci * wimax_id_table_rm - Remove a gennetlink familiy ID / wimax_dev mapping 988c2ecf20Sopenharmony_ci * 998c2ecf20Sopenharmony_ci * @id: family ID to remove from the table 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_civoid wimax_id_table_rm(struct wimax_dev *wimax_dev) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci spin_lock(&wimax_id_table_lock); 1048c2ecf20Sopenharmony_ci list_del_init(&wimax_dev->id_table_node); 1058c2ecf20Sopenharmony_ci spin_unlock(&wimax_id_table_lock); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* 1108c2ecf20Sopenharmony_ci * Release the gennetlink family id / mapping table 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * On debug, verify that the table is empty upon removal. We want the 1138c2ecf20Sopenharmony_ci * code always compiled, to ensure it doesn't bit rot. It will be 1148c2ecf20Sopenharmony_ci * compiled out if CONFIG_BUG is disabled. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_civoid wimax_id_table_release(void) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct wimax_dev *wimax_dev; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci#ifndef CONFIG_BUG 1218c2ecf20Sopenharmony_ci return; 1228c2ecf20Sopenharmony_ci#endif 1238c2ecf20Sopenharmony_ci spin_lock(&wimax_id_table_lock); 1248c2ecf20Sopenharmony_ci list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) { 1258c2ecf20Sopenharmony_ci pr_err("BUG: %s wimax_dev %p ifindex %d not cleared\n", 1268c2ecf20Sopenharmony_ci __func__, wimax_dev, wimax_dev->net_dev->ifindex); 1278c2ecf20Sopenharmony_ci WARN_ON(1); 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci spin_unlock(&wimax_id_table_lock); 1308c2ecf20Sopenharmony_ci} 131