xref: /wlan-driver/qca-wifi-host-cmn/qdf/linux/src/i_qdf_net_if.h (revision 5113495b16420b49004c444715d2daae2066e7dc)
1 /*
2  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: qdf_net_if
22  * QCA driver framework (QDF) network interface management APIs
23  */
24 
25 #if !defined(__I_QDF_NET_IF_H)
26 #define __I_QDF_NET_IF_H
27 
28 /* Include Files */
29 #include <qdf_types.h>
30 #include <qdf_util.h>
31 #include <linux/netdevice.h>
32 
33 struct qdf_net_if;
34 
35 /**
36  * __qdf_net_if_create_dummy_if() - create dummy interface
37  * @nif: interface handle
38  *
39  * This function will create a dummy network interface
40  *
41  * Return: QDF_STATUS_SUCCESS on success
42  */
43 static inline QDF_STATUS
__qdf_net_if_create_dummy_if(struct qdf_net_if * nif)44 __qdf_net_if_create_dummy_if(struct qdf_net_if *nif)
45 {
46 	int ret;
47 
48 	ret = init_dummy_netdev((struct net_device *)nif);
49 
50 	return qdf_status_from_os_return(ret);
51 }
52 
53 /**
54  * __qdf_net_if_get_dev_by_name() - Find a network device by its name
55  * @nif_name: network device name
56  *
57  * This function retrieves the network device by its name
58  *
59  * Return: qdf network device
60  */
61 static inline struct qdf_net_if *
__qdf_net_if_get_dev_by_name(char * nif_name)62 __qdf_net_if_get_dev_by_name(char *nif_name)
63 {
64 	if (!nif_name)
65 		return NULL;
66 
67 	return ((struct qdf_net_if *)dev_get_by_name(&init_net, nif_name));
68 }
69 
70 /**
71  * __qdf_net_if_release_dev() - Release reference to network device
72  * @nif: network device
73  *
74  * This function releases reference to the network device
75  *
76  * Return: QDF_STATUS_SUCCESS on success
77  */
78 static inline QDF_STATUS
__qdf_net_if_release_dev(struct qdf_net_if * nif)79 __qdf_net_if_release_dev(struct qdf_net_if  *nif)
80 {
81 	if (!nif)
82 		return QDF_STATUS_E_INVAL;
83 
84 	dev_put((struct net_device *)nif);
85 
86 	return QDF_STATUS_SUCCESS;
87 }
88 
89 /**
90  * __qdf_net_if_hold_dev() - hold reference to network device
91  * @nif: network device
92  *
93  * This function holds reference to the network device
94  *
95  * Return: QDF_STATUS_SUCCESS on success
96  */
97 static inline QDF_STATUS
__qdf_net_if_hold_dev(struct qdf_net_if * nif)98 __qdf_net_if_hold_dev(struct qdf_net_if  *nif)
99 {
100 	if (!nif)
101 		return QDF_STATUS_E_INVAL;
102 
103 	dev_hold((struct net_device *)nif);
104 
105 	return QDF_STATUS_SUCCESS;
106 }
107 
108 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0))
109 /**
110  * __qdf_net_update_net_device_dev_addr() - update net_device dev_addr
111  * @ndev: net_device
112  * @src_addr: source mac address
113  * @len: length
114  *
115  * kernel version 5.17 onwards made net_device->dev_addr as const unsigned char*
116  * so to update dev_addr, this function calls kernel api dev_addr_mod.
117  *
118  * Return: void
119  */
120 static inline void
__qdf_net_update_net_device_dev_addr(struct net_device * ndev,const void * src_addr,size_t len)121 __qdf_net_update_net_device_dev_addr(struct net_device *ndev,
122 				     const void *src_addr,
123 				     size_t len)
124 {
125 	dev_addr_mod(ndev, 0, src_addr, len);
126 }
127 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)) */
128 /**
129  * __qdf_net_update_net_device_dev_addr() - update net_device dev_addr
130  * @ndev: net_device
131  * @src_addr: source mac address
132  * @len: length
133  *
134  * This function updates dev_addr in net_device using mem copy.
135  *
136  * Return: void
137  */
138 static inline void
__qdf_net_update_net_device_dev_addr(struct net_device * ndev,const void * src_addr,size_t len)139 __qdf_net_update_net_device_dev_addr(struct net_device *ndev,
140 				     const void *src_addr,
141 				     size_t len)
142 {
143 	memcpy(ndev->dev_addr, src_addr, len);
144 }
145 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)) */
146 
147 /**
148  * __qdf_napi_enable() - Enable the napi schedule
149  * @napi: NAPI context
150  *
151  * This function resume NAPI from being scheduled on this context
152  *
153  * Return: NONE
154  */
155 static inline void
__qdf_napi_enable(struct napi_struct * napi)156 __qdf_napi_enable(struct napi_struct *napi)
157 {
158 	napi_enable(napi);
159 }
160 
161 /**
162  * __qdf_napi_disable() - Disable the napi schedule
163  * @napi: NAPI context
164  *
165  * This function suspends NAPI from being scheduled on this context
166  *
167  * Return: NONE
168  */
169 static inline void
__qdf_napi_disable(struct napi_struct * napi)170 __qdf_napi_disable(struct napi_struct *napi)
171 {
172 	napi_disable(napi);
173 }
174 
175 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
176 /**
177  * __qdf_netif_napi_add - initialize a NAPI context
178  * @netdev:  network device
179  * @napi: NAPI context
180  * @poll: polling function
181  * @weight: default weight
182  *
183  * Upstream commit b48b89f9c189 ("net: drop the weight argument from
184  * netif_napi_add") was introduced in Linux 6.1.  As described by the
185  * subject, this removes the weight argument from netif_napi_add().
186  *
187  * This was preceded by commit 58caed3dacb4 ("netdev: reshuffle
188  * netif_napi_add() APIs to allow dropping weight") in Linux 5.19
189  * which added new APIs to call when a non-default weight wishes to be
190  * sent.
191  *
192  * Return: NONE
193  */
194 static inline void
__qdf_netif_napi_add(struct net_device * netdev,struct napi_struct * napi,int (* poll)(struct napi_struct *,int),int weight)195 __qdf_netif_napi_add(struct net_device *netdev, struct napi_struct *napi,
196 		     int (*poll)(struct napi_struct *, int), int weight)
197 {
198 	netif_napi_add_weight(netdev, napi, poll, weight);
199 }
200 #else
201 static inline void
__qdf_netif_napi_add(struct net_device * netdev,struct napi_struct * napi,int (* poll)(struct napi_struct *,int),int weight)202 __qdf_netif_napi_add(struct net_device *netdev, struct napi_struct *napi,
203 		     int (*poll)(struct napi_struct *, int), int weight)
204 {
205 	netif_napi_add(netdev, napi, poll, weight);
206 }
207 #endif
208 
209 /**
210  * __qdf_netif_napi_del: remove a NAPI context
211  * @napi: NAPI context
212  *
213  * Return: NONE
214  */
215 static inline void
__qdf_netif_napi_del(struct napi_struct * napi)216 __qdf_netif_napi_del(struct napi_struct *napi)
217 {
218 	netif_napi_del(napi);
219 }
220 
221 #endif /*__I_QDF_NET_IF_H */
222