xref: /OK3568_Linux_fs/kernel/net/wimax/id-table.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Linux WiMAX
4*4882a593Smuzhiyun  * Mappping of generic netlink family IDs to net devices
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com>
7*4882a593Smuzhiyun  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * We assign a single generic netlink family ID to each device (to
10*4882a593Smuzhiyun  * simplify lookup).
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * We need a way to map family ID to a wimax_dev pointer.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The idea is to use a very simple lookup. Using a netlink attribute
15*4882a593Smuzhiyun  * with (for example) the interface name implies a heavier search over
16*4882a593Smuzhiyun  * all the network devices; seemed kind of a waste given that we know
17*4882a593Smuzhiyun  * we are looking for a WiMAX device and that most systems will have
18*4882a593Smuzhiyun  * just a single WiMAX adapter.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * We put all the WiMAX devices in the system in a linked list and
21*4882a593Smuzhiyun  * match the generic link family ID against the list.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * By using a linked list, the case of a single adapter in the system
24*4882a593Smuzhiyun  * becomes (almost) no overhead, while still working for many more. If
25*4882a593Smuzhiyun  * it ever goes beyond two, I'll be surprised.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun #include <linux/device.h>
28*4882a593Smuzhiyun #include <net/genetlink.h>
29*4882a593Smuzhiyun #include <linux/netdevice.h>
30*4882a593Smuzhiyun #include <linux/list.h>
31*4882a593Smuzhiyun #include <linux/wimax.h>
32*4882a593Smuzhiyun #include "wimax-internal.h"
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define D_SUBMODULE id_table
36*4882a593Smuzhiyun #include "debug-levels.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static DEFINE_SPINLOCK(wimax_id_table_lock);
40*4882a593Smuzhiyun static struct list_head wimax_id_table = LIST_HEAD_INIT(wimax_id_table);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun  * wimax_id_table_add - add a gennetlink familiy ID / wimax_dev mapping
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * @wimax_dev: WiMAX device descriptor to associate to the Generic
47*4882a593Smuzhiyun  *     Netlink family ID.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * Look for an empty spot in the ID table; if none found, double the
50*4882a593Smuzhiyun  * table's size and get the first spot.
51*4882a593Smuzhiyun  */
wimax_id_table_add(struct wimax_dev * wimax_dev)52*4882a593Smuzhiyun void wimax_id_table_add(struct wimax_dev *wimax_dev)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev);
55*4882a593Smuzhiyun 	spin_lock(&wimax_id_table_lock);
56*4882a593Smuzhiyun 	list_add(&wimax_dev->id_table_node, &wimax_id_table);
57*4882a593Smuzhiyun 	spin_unlock(&wimax_id_table_lock);
58*4882a593Smuzhiyun 	d_fnend(3, NULL, "(wimax_dev %p)\n", wimax_dev);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun  * wimax_get_netdev_by_info - lookup a wimax_dev from the gennetlink info
64*4882a593Smuzhiyun  *
65*4882a593Smuzhiyun  * The generic netlink family ID has been filled out in the
66*4882a593Smuzhiyun  * nlmsghdr->nlmsg_type field, so we pull it from there, look it up in
67*4882a593Smuzhiyun  * the mapping table and reference the wimax_dev.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * When done, the reference should be dropped with
70*4882a593Smuzhiyun  * 'dev_put(wimax_dev->net_dev)'.
71*4882a593Smuzhiyun  */
wimax_dev_get_by_genl_info(struct genl_info * info,int ifindex)72*4882a593Smuzhiyun struct wimax_dev *wimax_dev_get_by_genl_info(
73*4882a593Smuzhiyun 	struct genl_info *info, int ifindex)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct wimax_dev *wimax_dev = NULL;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	d_fnstart(3, NULL, "(info %p ifindex %d)\n", info, ifindex);
78*4882a593Smuzhiyun 	spin_lock(&wimax_id_table_lock);
79*4882a593Smuzhiyun 	list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) {
80*4882a593Smuzhiyun 		if (wimax_dev->net_dev->ifindex == ifindex) {
81*4882a593Smuzhiyun 			dev_hold(wimax_dev->net_dev);
82*4882a593Smuzhiyun 			goto found;
83*4882a593Smuzhiyun 		}
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 	wimax_dev = NULL;
86*4882a593Smuzhiyun 	d_printf(1, NULL, "wimax: no devices found with ifindex %d\n",
87*4882a593Smuzhiyun 		 ifindex);
88*4882a593Smuzhiyun found:
89*4882a593Smuzhiyun 	spin_unlock(&wimax_id_table_lock);
90*4882a593Smuzhiyun 	d_fnend(3, NULL, "(info %p ifindex %d) = %p\n",
91*4882a593Smuzhiyun 		info, ifindex, wimax_dev);
92*4882a593Smuzhiyun 	return wimax_dev;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * wimax_id_table_rm - Remove a gennetlink familiy ID / wimax_dev mapping
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * @id: family ID to remove from the table
100*4882a593Smuzhiyun  */
wimax_id_table_rm(struct wimax_dev * wimax_dev)101*4882a593Smuzhiyun void wimax_id_table_rm(struct wimax_dev *wimax_dev)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	spin_lock(&wimax_id_table_lock);
104*4882a593Smuzhiyun 	list_del_init(&wimax_dev->id_table_node);
105*4882a593Smuzhiyun 	spin_unlock(&wimax_id_table_lock);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun  * Release the gennetlink family id / mapping table
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * On debug, verify that the table is empty upon removal. We want the
113*4882a593Smuzhiyun  * code always compiled, to ensure it doesn't bit rot. It will be
114*4882a593Smuzhiyun  * compiled out if CONFIG_BUG is disabled.
115*4882a593Smuzhiyun  */
wimax_id_table_release(void)116*4882a593Smuzhiyun void wimax_id_table_release(void)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	struct wimax_dev *wimax_dev;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #ifndef CONFIG_BUG
121*4882a593Smuzhiyun 	return;
122*4882a593Smuzhiyun #endif
123*4882a593Smuzhiyun 	spin_lock(&wimax_id_table_lock);
124*4882a593Smuzhiyun 	list_for_each_entry(wimax_dev, &wimax_id_table, id_table_node) {
125*4882a593Smuzhiyun 		pr_err("BUG: %s wimax_dev %p ifindex %d not cleared\n",
126*4882a593Smuzhiyun 		       __func__, wimax_dev, wimax_dev->net_dev->ifindex);
127*4882a593Smuzhiyun 		WARN_ON(1);
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 	spin_unlock(&wimax_id_table_lock);
130*4882a593Smuzhiyun }
131