xref: /linux-6.15/net/caif/caif_dev.c (revision e977b4cf)
1 /*
2  * CAIF Interface registration.
3  * Copyright (C) ST-Ericsson AB 2010
4  * Author:	Sjur Brendeland/sjur.brandeland@stericsson.com
5  * License terms: GNU General Public License (GPL) version 2
6  *
7  * Borrowed heavily from file: pn_dev.c. Thanks to
8  *  Remi Denis-Courmont <[email protected]>
9  *  and Sakari Ailus <[email protected]>
10  */
11 
12 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
13 
14 #include <linux/kernel.h>
15 #include <linux/if_arp.h>
16 #include <linux/net.h>
17 #include <linux/netdevice.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <net/netns/generic.h>
21 #include <net/net_namespace.h>
22 #include <net/pkt_sched.h>
23 #include <net/caif/caif_device.h>
24 #include <net/caif/caif_layer.h>
25 #include <net/caif/cfpkt.h>
26 #include <net/caif/cfcnfg.h>
27 #include <net/caif/cfserl.h>
28 
29 MODULE_LICENSE("GPL");
30 
31 /* Used for local tracking of the CAIF net devices */
32 struct caif_device_entry {
33 	struct cflayer layer;
34 	struct list_head list;
35 	struct net_device *netdev;
36 	int __percpu *pcpu_refcnt;
37 };
38 
39 struct caif_device_entry_list {
40 	struct list_head list;
41 	/* Protects simulanous deletes in list */
42 	struct mutex lock;
43 };
44 
45 struct caif_net {
46 	struct cfcnfg *cfg;
47 	struct caif_device_entry_list caifdevs;
48 };
49 
50 static int caif_net_id;
51 
52 struct cfcnfg *get_cfcnfg(struct net *net)
53 {
54 	struct caif_net *caifn;
55 	BUG_ON(!net);
56 	caifn = net_generic(net, caif_net_id);
57 	if (!caifn)
58 		return NULL;
59 	return caifn->cfg;
60 }
61 EXPORT_SYMBOL(get_cfcnfg);
62 
63 static struct caif_device_entry_list *caif_device_list(struct net *net)
64 {
65 	struct caif_net *caifn;
66 	BUG_ON(!net);
67 	caifn = net_generic(net, caif_net_id);
68 	if (!caifn)
69 		return NULL;
70 	return &caifn->caifdevs;
71 }
72 
73 static void caifd_put(struct caif_device_entry *e)
74 {
75 	irqsafe_cpu_dec(*e->pcpu_refcnt);
76 }
77 
78 static void caifd_hold(struct caif_device_entry *e)
79 {
80 	irqsafe_cpu_inc(*e->pcpu_refcnt);
81 }
82 
83 static int caifd_refcnt_read(struct caif_device_entry *e)
84 {
85 	int i, refcnt = 0;
86 	for_each_possible_cpu(i)
87 		refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
88 	return refcnt;
89 }
90 
91 /* Allocate new CAIF device. */
92 static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
93 {
94 	struct caif_device_entry_list *caifdevs;
95 	struct caif_device_entry *caifd;
96 
97 	caifdevs = caif_device_list(dev_net(dev));
98 	if (!caifdevs)
99 		return NULL;
100 
101 	caifd = kzalloc(sizeof(*caifd), GFP_KERNEL);
102 	if (!caifd)
103 		return NULL;
104 	caifd->pcpu_refcnt = alloc_percpu(int);
105 	if (!caifd->pcpu_refcnt) {
106 		kfree(caifd);
107 		return NULL;
108 	}
109 	caifd->netdev = dev;
110 	dev_hold(dev);
111 	return caifd;
112 }
113 
114 static struct caif_device_entry *caif_get(struct net_device *dev)
115 {
116 	struct caif_device_entry_list *caifdevs =
117 	    caif_device_list(dev_net(dev));
118 	struct caif_device_entry *caifd;
119 	if (!caifdevs)
120 		return NULL;
121 
122 	list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
123 		if (caifd->netdev == dev)
124 			return caifd;
125 	}
126 	return NULL;
127 }
128 
129 static int transmit(struct cflayer *layer, struct cfpkt *pkt)
130 {
131 	int err;
132 	struct caif_device_entry *caifd =
133 	    container_of(layer, struct caif_device_entry, layer);
134 	struct sk_buff *skb;
135 
136 	skb = cfpkt_tonative(pkt);
137 	skb->dev = caifd->netdev;
138 	skb_reset_network_header(skb);
139 	skb->protocol = htons(ETH_P_CAIF);
140 
141 	err = dev_queue_xmit(skb);
142 	if (err > 0)
143 		err = -EIO;
144 
145 	return err;
146 }
147 
148 /*
149  * Stuff received packets into the CAIF stack.
150  * On error, returns non-zero and releases the skb.
151  */
152 static int receive(struct sk_buff *skb, struct net_device *dev,
153 		   struct packet_type *pkttype, struct net_device *orig_dev)
154 {
155 	struct cfpkt *pkt;
156 	struct caif_device_entry *caifd;
157 	int err;
158 
159 	pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
160 
161 	rcu_read_lock();
162 	caifd = caif_get(dev);
163 
164 	if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
165 			!netif_oper_up(caifd->netdev)) {
166 		rcu_read_unlock();
167 		kfree_skb(skb);
168 		return NET_RX_DROP;
169 	}
170 
171 	/* Hold reference to netdevice while using CAIF stack */
172 	caifd_hold(caifd);
173 	rcu_read_unlock();
174 
175 	err = caifd->layer.up->receive(caifd->layer.up, pkt);
176 
177 	/* For -EILSEQ the packet is not freed so so it now */
178 	if (err == -EILSEQ)
179 		cfpkt_destroy(pkt);
180 
181 	/* Release reference to stack upwards */
182 	caifd_put(caifd);
183 
184 	if (err != 0)
185 		err = NET_RX_DROP;
186 	return err;
187 }
188 
189 static struct packet_type caif_packet_type __read_mostly = {
190 	.type = cpu_to_be16(ETH_P_CAIF),
191 	.func = receive,
192 };
193 
194 static void dev_flowctrl(struct net_device *dev, int on)
195 {
196 	struct caif_device_entry *caifd;
197 
198 	rcu_read_lock();
199 
200 	caifd = caif_get(dev);
201 	if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
202 		rcu_read_unlock();
203 		return;
204 	}
205 
206 	caifd_hold(caifd);
207 	rcu_read_unlock();
208 
209 	caifd->layer.up->ctrlcmd(caifd->layer.up,
210 				 on ?
211 				 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
212 				 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
213 				 caifd->layer.id);
214 	caifd_put(caifd);
215 }
216 
217 void caif_enroll_dev(struct net_device *dev, struct caif_dev_common *caifdev,
218 			struct cflayer *link_support, int head_room,
219 			struct cflayer **layer, int (**rcv_func)(
220 				struct sk_buff *, struct net_device *,
221 				struct packet_type *, struct net_device *))
222 {
223 	struct caif_device_entry *caifd;
224 	enum cfcnfg_phy_preference pref;
225 	struct cfcnfg *cfg = get_cfcnfg(dev_net(dev));
226 	struct caif_device_entry_list *caifdevs;
227 
228 	caifdevs = caif_device_list(dev_net(dev));
229 	if (!cfg || !caifdevs)
230 		return;
231 	caifd = caif_device_alloc(dev);
232 	if (!caifd)
233 		return;
234 	*layer = &caifd->layer;
235 
236 	switch (caifdev->link_select) {
237 	case CAIF_LINK_HIGH_BANDW:
238 		pref = CFPHYPREF_HIGH_BW;
239 		break;
240 	case CAIF_LINK_LOW_LATENCY:
241 		pref = CFPHYPREF_LOW_LAT;
242 		break;
243 	default:
244 		pref = CFPHYPREF_HIGH_BW;
245 		break;
246 	}
247 	mutex_lock(&caifdevs->lock);
248 	list_add_rcu(&caifd->list, &caifdevs->list);
249 
250 	strncpy(caifd->layer.name, dev->name,
251 		sizeof(caifd->layer.name) - 1);
252 	caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
253 	caifd->layer.transmit = transmit;
254 	cfcnfg_add_phy_layer(cfg,
255 				dev,
256 				&caifd->layer,
257 				pref,
258 				link_support,
259 				caifdev->use_fcs,
260 				head_room);
261 	mutex_unlock(&caifdevs->lock);
262 	if (rcv_func)
263 		*rcv_func = receive;
264 }
265 
266 /* notify Caif of device events */
267 static int caif_device_notify(struct notifier_block *me, unsigned long what,
268 			      void *arg)
269 {
270 	struct net_device *dev = arg;
271 	struct caif_device_entry *caifd = NULL;
272 	struct caif_dev_common *caifdev;
273 	struct cfcnfg *cfg;
274 	struct cflayer *layer, *link_support;
275 	int head_room = 0;
276 	struct caif_device_entry_list *caifdevs;
277 
278 	cfg = get_cfcnfg(dev_net(dev));
279 	caifdevs = caif_device_list(dev_net(dev));
280 	if (!cfg || !caifdevs)
281 		return 0;
282 
283 	caifd = caif_get(dev);
284 	if (caifd == NULL && dev->type != ARPHRD_CAIF)
285 		return 0;
286 
287 	switch (what) {
288 	case NETDEV_REGISTER:
289 		if (caifd != NULL)
290 			break;
291 
292 		caifdev = netdev_priv(dev);
293 
294 		link_support = NULL;
295 		if (caifdev->use_frag) {
296 			head_room = 1;
297 			link_support = cfserl_create(dev->ifindex,
298 							caifdev->use_stx);
299 			if (!link_support) {
300 				pr_warn("Out of memory\n");
301 				break;
302 			}
303 		}
304 		caif_enroll_dev(dev, caifdev, link_support, head_room,
305 				&layer, NULL);
306 		caifdev->flowctrl = dev_flowctrl;
307 		break;
308 
309 	case NETDEV_UP:
310 		rcu_read_lock();
311 
312 		caifd = caif_get(dev);
313 		if (caifd == NULL) {
314 			rcu_read_unlock();
315 			break;
316 		}
317 
318 		cfcnfg_set_phy_state(cfg, &caifd->layer, true);
319 		rcu_read_unlock();
320 
321 		break;
322 
323 	case NETDEV_DOWN:
324 		rcu_read_lock();
325 
326 		caifd = caif_get(dev);
327 		if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
328 			rcu_read_unlock();
329 			return -EINVAL;
330 		}
331 
332 		cfcnfg_set_phy_state(cfg, &caifd->layer, false);
333 		caifd_hold(caifd);
334 		rcu_read_unlock();
335 
336 		caifd->layer.up->ctrlcmd(caifd->layer.up,
337 					 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
338 					 caifd->layer.id);
339 		caifd_put(caifd);
340 		break;
341 
342 	case NETDEV_UNREGISTER:
343 		mutex_lock(&caifdevs->lock);
344 
345 		caifd = caif_get(dev);
346 		if (caifd == NULL) {
347 			mutex_unlock(&caifdevs->lock);
348 			break;
349 		}
350 		list_del_rcu(&caifd->list);
351 
352 		/*
353 		 * NETDEV_UNREGISTER is called repeatedly until all reference
354 		 * counts for the net-device are released. If references to
355 		 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
356 		 * the next call to NETDEV_UNREGISTER.
357 		 *
358 		 * If any packets are in flight down the CAIF Stack,
359 		 * cfcnfg_del_phy_layer will return nonzero.
360 		 * If no packets are in flight, the CAIF Stack associated
361 		 * with the net-device un-registering is freed.
362 		 */
363 
364 		if (caifd_refcnt_read(caifd) != 0 ||
365 			cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
366 
367 			pr_info("Wait for device inuse\n");
368 			/* Enrole device if CAIF Stack is still in use */
369 			list_add_rcu(&caifd->list, &caifdevs->list);
370 			mutex_unlock(&caifdevs->lock);
371 			break;
372 		}
373 
374 		synchronize_rcu();
375 		dev_put(caifd->netdev);
376 		free_percpu(caifd->pcpu_refcnt);
377 		kfree(caifd);
378 
379 		mutex_unlock(&caifdevs->lock);
380 		break;
381 	}
382 	return 0;
383 }
384 
385 static struct notifier_block caif_device_notifier = {
386 	.notifier_call = caif_device_notify,
387 	.priority = 0,
388 };
389 
390 /* Per-namespace Caif devices handling */
391 static int caif_init_net(struct net *net)
392 {
393 	struct caif_net *caifn = net_generic(net, caif_net_id);
394 	BUG_ON(!caifn);
395 	INIT_LIST_HEAD(&caifn->caifdevs.list);
396 	mutex_init(&caifn->caifdevs.lock);
397 
398 	caifn->cfg = cfcnfg_create();
399 	if (!caifn->cfg) {
400 		pr_warn("can't create cfcnfg\n");
401 		return -ENOMEM;
402 	}
403 
404 	return 0;
405 }
406 
407 static void caif_exit_net(struct net *net)
408 {
409 	struct caif_device_entry *caifd, *tmp;
410 	struct caif_device_entry_list *caifdevs =
411 	    caif_device_list(net);
412 	struct cfcnfg *cfg =  get_cfcnfg(net);
413 
414 	if (!cfg || !caifdevs)
415 		return;
416 
417 	rtnl_lock();
418 	mutex_lock(&caifdevs->lock);
419 
420 	list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
421 		int i = 0;
422 		list_del_rcu(&caifd->list);
423 		cfcnfg_set_phy_state(cfg, &caifd->layer, false);
424 
425 		while (i < 10 &&
426 			(caifd_refcnt_read(caifd) != 0 ||
427 			cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
428 
429 			pr_info("Wait for device inuse\n");
430 			msleep(250);
431 			i++;
432 		}
433 		synchronize_rcu();
434 		dev_put(caifd->netdev);
435 		free_percpu(caifd->pcpu_refcnt);
436 		kfree(caifd);
437 	}
438 	cfcnfg_remove(cfg);
439 
440 	mutex_unlock(&caifdevs->lock);
441 	rtnl_unlock();
442 }
443 
444 static struct pernet_operations caif_net_ops = {
445 	.init = caif_init_net,
446 	.exit = caif_exit_net,
447 	.id   = &caif_net_id,
448 	.size = sizeof(struct caif_net),
449 };
450 
451 /* Initialize Caif devices list */
452 static int __init caif_device_init(void)
453 {
454 	int result;
455 
456 	result = register_pernet_device(&caif_net_ops);
457 
458 	if (result)
459 		return result;
460 
461 	register_netdevice_notifier(&caif_device_notifier);
462 	dev_add_pack(&caif_packet_type);
463 
464 	return result;
465 }
466 
467 static void __exit caif_device_exit(void)
468 {
469 	unregister_pernet_device(&caif_net_ops);
470 	unregister_netdevice_notifier(&caif_device_notifier);
471 	dev_remove_pack(&caif_packet_type);
472 }
473 
474 module_init(caif_device_init);
475 module_exit(caif_device_exit);
476