Merge branch 'v3.10/topic/misc' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_net.c
1 /**
2  * Copyright (C) ARM Limited 2010-2014. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9
10 #include "gator.h"
11 #include <linux/netdevice.h>
12 #include <linux/hardirq.h>
13
14 #define NETRX           0
15 #define NETTX           1
16 #define TOTALNET        2
17
18 static ulong netrx_enabled;
19 static ulong nettx_enabled;
20 static ulong netrx_key;
21 static ulong nettx_key;
22 static int rx_total, tx_total;
23 static ulong netPrev[TOTALNET];
24 static int netGet[TOTALNET * 4];
25
26 static struct timer_list net_wake_up_timer;
27
28 // Must be run in process context as the kernel function dev_get_stats() can sleep
29 static void get_network_stats(struct work_struct *wsptr)
30 {
31         int rx = 0, tx = 0;
32         struct net_device *dev;
33
34         for_each_netdev(&init_net, dev) {
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
36                 const struct net_device_stats *stats = dev_get_stats(dev);
37 #else
38                 struct rtnl_link_stats64 temp;
39                 const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
40 #endif
41                 rx += stats->rx_bytes;
42                 tx += stats->tx_bytes;
43         }
44         rx_total = rx;
45         tx_total = tx;
46 }
47
48 DECLARE_WORK(wq_get_stats, get_network_stats);
49
50 static void net_wake_up_handler(unsigned long unused_data)
51 {
52         // had to delay scheduling work as attempting to schedule work during the context switch is illegal in kernel versions 3.5 and greater
53         schedule_work(&wq_get_stats);
54 }
55
56 static void calculate_delta(int *rx, int *tx)
57 {
58         int rx_calc, tx_calc;
59
60         rx_calc = (int)(rx_total - netPrev[NETRX]);
61         if (rx_calc < 0)
62                 rx_calc = 0;
63         netPrev[NETRX] += rx_calc;
64
65         tx_calc = (int)(tx_total - netPrev[NETTX]);
66         if (tx_calc < 0)
67                 tx_calc = 0;
68         netPrev[NETTX] += tx_calc;
69
70         *rx = rx_calc;
71         *tx = tx_calc;
72 }
73
74 static int gator_events_net_create_files(struct super_block *sb, struct dentry *root)
75 {
76         // Network counters are not currently supported in RT-Preempt full because mod_timer is used
77 #ifndef CONFIG_PREEMPT_RT_FULL
78         struct dentry *dir;
79
80         dir = gatorfs_mkdir(sb, root, "Linux_net_rx");
81         if (!dir) {
82                 return -1;
83         }
84         gatorfs_create_ulong(sb, dir, "enabled", &netrx_enabled);
85         gatorfs_create_ro_ulong(sb, dir, "key", &netrx_key);
86
87         dir = gatorfs_mkdir(sb, root, "Linux_net_tx");
88         if (!dir) {
89                 return -1;
90         }
91         gatorfs_create_ulong(sb, dir, "enabled", &nettx_enabled);
92         gatorfs_create_ro_ulong(sb, dir, "key", &nettx_key);
93 #endif
94
95         return 0;
96 }
97
98 static int gator_events_net_start(void)
99 {
100         get_network_stats(0);
101         netPrev[NETRX] = rx_total;
102         netPrev[NETTX] = tx_total;
103 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
104         setup_timer(&net_wake_up_timer, net_wake_up_handler, 0);
105 #else
106         setup_deferrable_timer_on_stack(&net_wake_up_timer, net_wake_up_handler, 0);
107 #endif
108         return 0;
109 }
110
111 static void gator_events_net_stop(void)
112 {
113         del_timer_sync(&net_wake_up_timer);
114         netrx_enabled = 0;
115         nettx_enabled = 0;
116 }
117
118 static int gator_events_net_read(int **buffer)
119 {
120         int len, rx_delta, tx_delta;
121         static int last_rx_delta = 0, last_tx_delta = 0;
122
123         if (!on_primary_core())
124                 return 0;
125
126         if (!netrx_enabled && !nettx_enabled)
127                 return 0;
128
129         mod_timer(&net_wake_up_timer, jiffies + 1);
130
131         calculate_delta(&rx_delta, &tx_delta);
132
133         len = 0;
134         if (netrx_enabled && last_rx_delta != rx_delta) {
135                 last_rx_delta = rx_delta;
136                 netGet[len++] = netrx_key;
137                 netGet[len++] = 0;      // indicates to Streamline that rx_delta bytes were transmitted now, not since the last message
138                 netGet[len++] = netrx_key;
139                 netGet[len++] = rx_delta;
140         }
141
142         if (nettx_enabled && last_tx_delta != tx_delta) {
143                 last_tx_delta = tx_delta;
144                 netGet[len++] = nettx_key;
145                 netGet[len++] = 0;      // indicates to Streamline that tx_delta bytes were transmitted now, not since the last message
146                 netGet[len++] = nettx_key;
147                 netGet[len++] = tx_delta;
148         }
149
150         if (buffer)
151                 *buffer = netGet;
152
153         return len;
154 }
155
156 static struct gator_interface gator_events_net_interface = {
157         .create_files = gator_events_net_create_files,
158         .start = gator_events_net_start,
159         .stop = gator_events_net_stop,
160         .read = gator_events_net_read,
161 };
162
163 int gator_events_net_init(void)
164 {
165         netrx_key = gator_events_get_key();
166         nettx_key = gator_events_get_key();
167
168         netrx_enabled = 0;
169         nettx_enabled = 0;
170
171         return gator_events_install(&gator_events_net_interface);
172 }