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