1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2017 Ben Whitten <ben.whitten@gmail.com>
3 // Copyright 2007 Oliver Jowett <oliver@opencloud.com>
4 //
5 // LED Kernel Netdev Trigger
6 //
7 // Toggles the LED to reflect the link and traffic state of a named net device
8 //
9 // Derived from ledtrig-timer.c which is:
10 //  Copyright 2005-2006 Openedhand Ltd.
11 //  Author: Richard Purdie <rpurdie@openedhand.com>
12 
13 #include <linux/atomic.h>
14 #include <linux/ctype.h>
15 #include <linux/device.h>
16 #include <linux/ethtool.h>
17 #include <linux/init.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel.h>
20 #include <linux/leds.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/mutex.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/timer.h>
27 #include "../leds.h"
28 
29 #define NETDEV_LED_DEFAULT_INTERVAL	50
30 
31 /*
32  * Configurable sysfs attributes:
33  *
34  * device_name - network device name to monitor
35  * interval - duration of LED blink, in milliseconds
36  * link -  LED's normal state reflects whether the link is up
37  *         (has carrier) or not
38  * tx -  LED blinks on transmitted data
39  * rx -  LED blinks on receive data
40  *
41  */
42 
43 struct led_netdev_data {
44 	struct mutex lock;
45 
46 	struct delayed_work work;
47 	struct notifier_block notifier;
48 
49 	struct led_classdev *led_cdev;
50 	struct net_device *net_dev;
51 
52 	char device_name[IFNAMSIZ];
53 	atomic_t interval;
54 	unsigned int last_activity;
55 
56 	unsigned long mode;
57 	int link_speed;
58 	u8 duplex;
59 
60 	bool carrier_link_up;
61 	bool hw_control;
62 };
63 
64 static void set_baseline_state(struct led_netdev_data *trigger_data)
65 {
66 	int current_brightness;
67 	struct led_classdev *led_cdev = trigger_data->led_cdev;
68 
69 	/* Already validated, hw control is possible with the requested mode */
70 	if (trigger_data->hw_control) {
71 		led_cdev->hw_control_set(led_cdev, trigger_data->mode);
72 
73 		return;
74 	}
75 
76 	current_brightness = led_cdev->brightness;
77 	if (current_brightness)
78 		led_cdev->blink_brightness = current_brightness;
79 	if (!led_cdev->blink_brightness)
80 		led_cdev->blink_brightness = led_cdev->max_brightness;
81 
82 	if (!trigger_data->carrier_link_up) {
83 		led_set_brightness(led_cdev, LED_OFF);
84 	} else {
85 		bool blink_on = false;
86 
87 		if (test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode))
88 			blink_on = true;
89 
90 		if (test_bit(TRIGGER_NETDEV_LINK_10, &trigger_data->mode) &&
91 		    trigger_data->link_speed == SPEED_10)
92 			blink_on = true;
93 
94 		if (test_bit(TRIGGER_NETDEV_LINK_100, &trigger_data->mode) &&
95 		    trigger_data->link_speed == SPEED_100)
96 			blink_on = true;
97 
98 		if (test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode) &&
99 		    trigger_data->link_speed == SPEED_1000)
100 			blink_on = true;
101 
102 		if (test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) &&
103 		    trigger_data->duplex == DUPLEX_HALF)
104 			blink_on = true;
105 
106 		if (test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode) &&
107 		    trigger_data->duplex == DUPLEX_FULL)
108 			blink_on = true;
109 
110 		if (blink_on)
111 			led_set_brightness(led_cdev,
112 					   led_cdev->blink_brightness);
113 		else
114 			led_set_brightness(led_cdev, LED_OFF);
115 
116 		/* If we are looking for RX/TX start periodically
117 		 * checking stats
118 		 */
119 		if (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ||
120 		    test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
121 			schedule_delayed_work(&trigger_data->work, 0);
122 	}
123 }
124 
125 static bool supports_hw_control(struct led_classdev *led_cdev)
126 {
127 	if (!led_cdev->hw_control_get || !led_cdev->hw_control_set ||
128 	    !led_cdev->hw_control_is_supported)
129 		return false;
130 
131 	return !strcmp(led_cdev->hw_control_trigger, led_cdev->trigger->name);
132 }
133 
134 /*
135  * Validate the configured netdev is the same as the one associated with
136  * the LED driver in hw control.
137  */
138 static bool validate_net_dev(struct led_classdev *led_cdev,
139 			     struct net_device *net_dev)
140 {
141 	struct device *dev = led_cdev->hw_control_get_device(led_cdev);
142 	struct net_device *ndev;
143 
144 	if (!dev)
145 		return false;
146 
147 	ndev = to_net_dev(dev);
148 
149 	return ndev == net_dev;
150 }
151 
152 static bool can_hw_control(struct led_netdev_data *trigger_data)
153 {
154 	unsigned long default_interval = msecs_to_jiffies(NETDEV_LED_DEFAULT_INTERVAL);
155 	unsigned int interval = atomic_read(&trigger_data->interval);
156 	struct led_classdev *led_cdev = trigger_data->led_cdev;
157 	int ret;
158 
159 	if (!supports_hw_control(led_cdev))
160 		return false;
161 
162 	/*
163 	 * Interval must be set to the default
164 	 * value. Any different value is rejected if in hw
165 	 * control.
166 	 */
167 	if (interval != default_interval)
168 		return false;
169 
170 	/*
171 	 * net_dev must be set with hw control, otherwise no
172 	 * blinking can be happening and there is nothing to
173 	 * offloaded. Additionally, for hw control to be
174 	 * valid, the configured netdev must be the same as
175 	 * netdev associated to the LED.
176 	 */
177 	if (!validate_net_dev(led_cdev, trigger_data->net_dev))
178 		return false;
179 
180 	/* Check if the requested mode is supported */
181 	ret = led_cdev->hw_control_is_supported(led_cdev, trigger_data->mode);
182 	/* Fall back to software blinking if not supported */
183 	if (ret == -EOPNOTSUPP)
184 		return false;
185 	if (ret) {
186 		dev_warn(led_cdev->dev,
187 			 "Current mode check failed with error %d\n", ret);
188 		return false;
189 	}
190 
191 	return true;
192 }
193 
194 static void get_device_state(struct led_netdev_data *trigger_data)
195 {
196 	struct ethtool_link_ksettings cmd;
197 
198 	trigger_data->carrier_link_up = netif_carrier_ok(trigger_data->net_dev);
199 	if (!trigger_data->carrier_link_up)
200 		return;
201 
202 	if (!__ethtool_get_link_ksettings(trigger_data->net_dev, &cmd)) {
203 		trigger_data->link_speed = cmd.base.speed;
204 		trigger_data->duplex = cmd.base.duplex;
205 	}
206 }
207 
208 static ssize_t device_name_show(struct device *dev,
209 				struct device_attribute *attr, char *buf)
210 {
211 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
212 	ssize_t len;
213 
214 	mutex_lock(&trigger_data->lock);
215 	len = sprintf(buf, "%s\n", trigger_data->device_name);
216 	mutex_unlock(&trigger_data->lock);
217 
218 	return len;
219 }
220 
221 static int set_device_name(struct led_netdev_data *trigger_data,
222 			   const char *name, size_t size)
223 {
224 	cancel_delayed_work_sync(&trigger_data->work);
225 
226 	mutex_lock(&trigger_data->lock);
227 
228 	if (trigger_data->net_dev) {
229 		dev_put(trigger_data->net_dev);
230 		trigger_data->net_dev = NULL;
231 	}
232 
233 	memcpy(trigger_data->device_name, name, size);
234 	trigger_data->device_name[size] = 0;
235 	if (size > 0 && trigger_data->device_name[size - 1] == '\n')
236 		trigger_data->device_name[size - 1] = 0;
237 
238 	if (trigger_data->device_name[0] != 0)
239 		trigger_data->net_dev =
240 		    dev_get_by_name(&init_net, trigger_data->device_name);
241 
242 	trigger_data->carrier_link_up = false;
243 	trigger_data->link_speed = SPEED_UNKNOWN;
244 	trigger_data->duplex = DUPLEX_UNKNOWN;
245 	if (trigger_data->net_dev != NULL) {
246 		rtnl_lock();
247 		get_device_state(trigger_data);
248 		rtnl_unlock();
249 	}
250 
251 	trigger_data->last_activity = 0;
252 
253 	set_baseline_state(trigger_data);
254 	mutex_unlock(&trigger_data->lock);
255 
256 	return 0;
257 }
258 
259 static ssize_t device_name_store(struct device *dev,
260 				 struct device_attribute *attr, const char *buf,
261 				 size_t size)
262 {
263 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
264 	int ret;
265 
266 	if (size >= IFNAMSIZ)
267 		return -EINVAL;
268 
269 	ret = set_device_name(trigger_data, buf, size);
270 
271 	if (ret < 0)
272 		return ret;
273 	return size;
274 }
275 
276 static DEVICE_ATTR_RW(device_name);
277 
278 static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
279 				    enum led_trigger_netdev_modes attr)
280 {
281 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
282 	int bit;
283 
284 	switch (attr) {
285 	case TRIGGER_NETDEV_LINK:
286 	case TRIGGER_NETDEV_LINK_10:
287 	case TRIGGER_NETDEV_LINK_100:
288 	case TRIGGER_NETDEV_LINK_1000:
289 	case TRIGGER_NETDEV_HALF_DUPLEX:
290 	case TRIGGER_NETDEV_FULL_DUPLEX:
291 	case TRIGGER_NETDEV_TX:
292 	case TRIGGER_NETDEV_RX:
293 		bit = attr;
294 		break;
295 	default:
296 		return -EINVAL;
297 	}
298 
299 	return sprintf(buf, "%u\n", test_bit(bit, &trigger_data->mode));
300 }
301 
302 static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
303 				     size_t size, enum led_trigger_netdev_modes attr)
304 {
305 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
306 	unsigned long state, mode = trigger_data->mode;
307 	int ret;
308 	int bit;
309 
310 	ret = kstrtoul(buf, 0, &state);
311 	if (ret)
312 		return ret;
313 
314 	switch (attr) {
315 	case TRIGGER_NETDEV_LINK:
316 	case TRIGGER_NETDEV_LINK_10:
317 	case TRIGGER_NETDEV_LINK_100:
318 	case TRIGGER_NETDEV_LINK_1000:
319 	case TRIGGER_NETDEV_HALF_DUPLEX:
320 	case TRIGGER_NETDEV_FULL_DUPLEX:
321 	case TRIGGER_NETDEV_TX:
322 	case TRIGGER_NETDEV_RX:
323 		bit = attr;
324 		break;
325 	default:
326 		return -EINVAL;
327 	}
328 
329 	if (state)
330 		set_bit(bit, &mode);
331 	else
332 		clear_bit(bit, &mode);
333 
334 	if (test_bit(TRIGGER_NETDEV_LINK, &mode) &&
335 	    (test_bit(TRIGGER_NETDEV_LINK_10, &mode) ||
336 	     test_bit(TRIGGER_NETDEV_LINK_100, &mode) ||
337 	     test_bit(TRIGGER_NETDEV_LINK_1000, &mode)))
338 		return -EINVAL;
339 
340 	cancel_delayed_work_sync(&trigger_data->work);
341 
342 	trigger_data->mode = mode;
343 	trigger_data->hw_control = can_hw_control(trigger_data);
344 
345 	set_baseline_state(trigger_data);
346 
347 	return size;
348 }
349 
350 #define DEFINE_NETDEV_TRIGGER(trigger_name, trigger) \
351 	static ssize_t trigger_name##_show(struct device *dev, \
352 		struct device_attribute *attr, char *buf) \
353 	{ \
354 		return netdev_led_attr_show(dev, buf, trigger); \
355 	} \
356 	static ssize_t trigger_name##_store(struct device *dev, \
357 		struct device_attribute *attr, const char *buf, size_t size) \
358 	{ \
359 		return netdev_led_attr_store(dev, buf, size, trigger); \
360 	} \
361 	static DEVICE_ATTR_RW(trigger_name)
362 
363 DEFINE_NETDEV_TRIGGER(link, TRIGGER_NETDEV_LINK);
364 DEFINE_NETDEV_TRIGGER(link_10, TRIGGER_NETDEV_LINK_10);
365 DEFINE_NETDEV_TRIGGER(link_100, TRIGGER_NETDEV_LINK_100);
366 DEFINE_NETDEV_TRIGGER(link_1000, TRIGGER_NETDEV_LINK_1000);
367 DEFINE_NETDEV_TRIGGER(half_duplex, TRIGGER_NETDEV_HALF_DUPLEX);
368 DEFINE_NETDEV_TRIGGER(full_duplex, TRIGGER_NETDEV_FULL_DUPLEX);
369 DEFINE_NETDEV_TRIGGER(tx, TRIGGER_NETDEV_TX);
370 DEFINE_NETDEV_TRIGGER(rx, TRIGGER_NETDEV_RX);
371 
372 static ssize_t interval_show(struct device *dev,
373 			     struct device_attribute *attr, char *buf)
374 {
375 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
376 
377 	return sprintf(buf, "%u\n",
378 		       jiffies_to_msecs(atomic_read(&trigger_data->interval)));
379 }
380 
381 static ssize_t interval_store(struct device *dev,
382 			      struct device_attribute *attr, const char *buf,
383 			      size_t size)
384 {
385 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
386 	unsigned long value;
387 	int ret;
388 
389 	if (trigger_data->hw_control)
390 		return -EINVAL;
391 
392 	ret = kstrtoul(buf, 0, &value);
393 	if (ret)
394 		return ret;
395 
396 	/* impose some basic bounds on the timer interval */
397 	if (value >= 5 && value <= 10000) {
398 		cancel_delayed_work_sync(&trigger_data->work);
399 
400 		atomic_set(&trigger_data->interval, msecs_to_jiffies(value));
401 		set_baseline_state(trigger_data);	/* resets timer */
402 	}
403 
404 	return size;
405 }
406 
407 static DEVICE_ATTR_RW(interval);
408 
409 static ssize_t offloaded_show(struct device *dev,
410 			      struct device_attribute *attr, char *buf)
411 {
412 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
413 
414 	return sprintf(buf, "%d\n", trigger_data->hw_control);
415 }
416 
417 static DEVICE_ATTR_RO(offloaded);
418 
419 static struct attribute *netdev_trig_attrs[] = {
420 	&dev_attr_device_name.attr,
421 	&dev_attr_link.attr,
422 	&dev_attr_link_10.attr,
423 	&dev_attr_link_100.attr,
424 	&dev_attr_link_1000.attr,
425 	&dev_attr_full_duplex.attr,
426 	&dev_attr_half_duplex.attr,
427 	&dev_attr_rx.attr,
428 	&dev_attr_tx.attr,
429 	&dev_attr_interval.attr,
430 	&dev_attr_offloaded.attr,
431 	NULL
432 };
433 ATTRIBUTE_GROUPS(netdev_trig);
434 
435 static int netdev_trig_notify(struct notifier_block *nb,
436 			      unsigned long evt, void *dv)
437 {
438 	struct net_device *dev =
439 		netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv);
440 	struct led_netdev_data *trigger_data =
441 		container_of(nb, struct led_netdev_data, notifier);
442 
443 	if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
444 	    && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER
445 	    && evt != NETDEV_CHANGENAME)
446 		return NOTIFY_DONE;
447 
448 	if (!(dev == trigger_data->net_dev ||
449 	      (evt == NETDEV_CHANGENAME && !strcmp(dev->name, trigger_data->device_name)) ||
450 	      (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
451 		return NOTIFY_DONE;
452 
453 	cancel_delayed_work_sync(&trigger_data->work);
454 
455 	mutex_lock(&trigger_data->lock);
456 
457 	trigger_data->carrier_link_up = false;
458 	trigger_data->link_speed = SPEED_UNKNOWN;
459 	trigger_data->duplex = DUPLEX_UNKNOWN;
460 	switch (evt) {
461 	case NETDEV_CHANGENAME:
462 		get_device_state(trigger_data);
463 		fallthrough;
464 	case NETDEV_REGISTER:
465 		dev_put(trigger_data->net_dev);
466 		dev_hold(dev);
467 		trigger_data->net_dev = dev;
468 		break;
469 	case NETDEV_UNREGISTER:
470 		dev_put(trigger_data->net_dev);
471 		trigger_data->net_dev = NULL;
472 		break;
473 	case NETDEV_UP:
474 	case NETDEV_CHANGE:
475 		get_device_state(trigger_data);
476 		break;
477 	}
478 
479 	set_baseline_state(trigger_data);
480 
481 	mutex_unlock(&trigger_data->lock);
482 
483 	return NOTIFY_DONE;
484 }
485 
486 /* here's the real work! */
487 static void netdev_trig_work(struct work_struct *work)
488 {
489 	struct led_netdev_data *trigger_data =
490 		container_of(work, struct led_netdev_data, work.work);
491 	struct rtnl_link_stats64 *dev_stats;
492 	unsigned int new_activity;
493 	struct rtnl_link_stats64 temp;
494 	unsigned long interval;
495 	int invert;
496 
497 	/* If we dont have a device, insure we are off */
498 	if (!trigger_data->net_dev) {
499 		led_set_brightness(trigger_data->led_cdev, LED_OFF);
500 		return;
501 	}
502 
503 	/* If we are not looking for RX/TX then return  */
504 	if (!test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
505 	    !test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
506 		return;
507 
508 	dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
509 	new_activity =
510 	    (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ?
511 		dev_stats->tx_packets : 0) +
512 	    (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) ?
513 		dev_stats->rx_packets : 0);
514 
515 	if (trigger_data->last_activity != new_activity) {
516 		led_stop_software_blink(trigger_data->led_cdev);
517 
518 		invert = test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) ||
519 			 test_bit(TRIGGER_NETDEV_LINK_10, &trigger_data->mode) ||
520 			 test_bit(TRIGGER_NETDEV_LINK_100, &trigger_data->mode) ||
521 			 test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode) ||
522 			 test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) ||
523 			 test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode);
524 		interval = jiffies_to_msecs(
525 				atomic_read(&trigger_data->interval));
526 		/* base state is ON (link present) */
527 		led_blink_set_oneshot(trigger_data->led_cdev,
528 				      &interval,
529 				      &interval,
530 				      invert);
531 		trigger_data->last_activity = new_activity;
532 	}
533 
534 	schedule_delayed_work(&trigger_data->work,
535 			(atomic_read(&trigger_data->interval)*2));
536 }
537 
538 static int netdev_trig_activate(struct led_classdev *led_cdev)
539 {
540 	struct led_netdev_data *trigger_data;
541 	unsigned long mode = 0;
542 	struct device *dev;
543 	int rc;
544 
545 	trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
546 	if (!trigger_data)
547 		return -ENOMEM;
548 
549 	mutex_init(&trigger_data->lock);
550 
551 	trigger_data->notifier.notifier_call = netdev_trig_notify;
552 	trigger_data->notifier.priority = 10;
553 
554 	INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
555 
556 	trigger_data->led_cdev = led_cdev;
557 	trigger_data->net_dev = NULL;
558 	trigger_data->device_name[0] = 0;
559 
560 	trigger_data->mode = 0;
561 	atomic_set(&trigger_data->interval, msecs_to_jiffies(NETDEV_LED_DEFAULT_INTERVAL));
562 	trigger_data->last_activity = 0;
563 
564 	/* Check if hw control is active by default on the LED.
565 	 * Init already enabled mode in hw control.
566 	 */
567 	if (supports_hw_control(led_cdev)) {
568 		dev = led_cdev->hw_control_get_device(led_cdev);
569 		if (dev) {
570 			const char *name = dev_name(dev);
571 
572 			set_device_name(trigger_data, name, strlen(name));
573 			trigger_data->hw_control = true;
574 
575 			rc = led_cdev->hw_control_get(led_cdev, &mode);
576 			if (!rc)
577 				trigger_data->mode = mode;
578 		}
579 	}
580 
581 	led_set_trigger_data(led_cdev, trigger_data);
582 
583 	rc = register_netdevice_notifier(&trigger_data->notifier);
584 	if (rc)
585 		kfree(trigger_data);
586 
587 	return rc;
588 }
589 
590 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
591 {
592 	struct led_netdev_data *trigger_data = led_get_trigger_data(led_cdev);
593 
594 	unregister_netdevice_notifier(&trigger_data->notifier);
595 
596 	cancel_delayed_work_sync(&trigger_data->work);
597 
598 	led_set_brightness(led_cdev, LED_OFF);
599 
600 	dev_put(trigger_data->net_dev);
601 
602 	kfree(trigger_data);
603 }
604 
605 static struct led_trigger netdev_led_trigger = {
606 	.name = "netdev",
607 	.activate = netdev_trig_activate,
608 	.deactivate = netdev_trig_deactivate,
609 	.groups = netdev_trig_groups,
610 };
611 
612 module_led_trigger(netdev_led_trigger);
613 
614 MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
615 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
616 MODULE_DESCRIPTION("Netdev LED trigger");
617 MODULE_LICENSE("GPL v2");
618