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 	if (size >= IFNAMSIZ)
225 		return -EINVAL;
226 
227 	cancel_delayed_work_sync(&trigger_data->work);
228 
229 	/*
230 	 * Take RTNL lock before trigger_data lock to prevent potential
231 	 * deadlock with netdev notifier registration.
232 	 */
233 	rtnl_lock();
234 	mutex_lock(&trigger_data->lock);
235 
236 	if (trigger_data->net_dev) {
237 		dev_put(trigger_data->net_dev);
238 		trigger_data->net_dev = NULL;
239 	}
240 
241 	memcpy(trigger_data->device_name, name, size);
242 	trigger_data->device_name[size] = 0;
243 	if (size > 0 && trigger_data->device_name[size - 1] == '\n')
244 		trigger_data->device_name[size - 1] = 0;
245 
246 	if (trigger_data->device_name[0] != 0)
247 		trigger_data->net_dev =
248 		    dev_get_by_name(&init_net, trigger_data->device_name);
249 
250 	trigger_data->carrier_link_up = false;
251 	trigger_data->link_speed = SPEED_UNKNOWN;
252 	trigger_data->duplex = DUPLEX_UNKNOWN;
253 	if (trigger_data->net_dev)
254 		get_device_state(trigger_data);
255 
256 	trigger_data->last_activity = 0;
257 
258 	set_baseline_state(trigger_data);
259 	mutex_unlock(&trigger_data->lock);
260 	rtnl_unlock();
261 
262 	return 0;
263 }
264 
265 static ssize_t device_name_store(struct device *dev,
266 				 struct device_attribute *attr, const char *buf,
267 				 size_t size)
268 {
269 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
270 	int ret;
271 
272 	ret = set_device_name(trigger_data, buf, size);
273 
274 	if (ret < 0)
275 		return ret;
276 	return size;
277 }
278 
279 static DEVICE_ATTR_RW(device_name);
280 
281 static ssize_t netdev_led_attr_show(struct device *dev, char *buf,
282 				    enum led_trigger_netdev_modes attr)
283 {
284 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
285 	int bit;
286 
287 	switch (attr) {
288 	case TRIGGER_NETDEV_LINK:
289 	case TRIGGER_NETDEV_LINK_10:
290 	case TRIGGER_NETDEV_LINK_100:
291 	case TRIGGER_NETDEV_LINK_1000:
292 	case TRIGGER_NETDEV_HALF_DUPLEX:
293 	case TRIGGER_NETDEV_FULL_DUPLEX:
294 	case TRIGGER_NETDEV_TX:
295 	case TRIGGER_NETDEV_RX:
296 		bit = attr;
297 		break;
298 	default:
299 		return -EINVAL;
300 	}
301 
302 	return sprintf(buf, "%u\n", test_bit(bit, &trigger_data->mode));
303 }
304 
305 static ssize_t netdev_led_attr_store(struct device *dev, const char *buf,
306 				     size_t size, enum led_trigger_netdev_modes attr)
307 {
308 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
309 	unsigned long state, mode = trigger_data->mode;
310 	int ret;
311 	int bit;
312 
313 	ret = kstrtoul(buf, 0, &state);
314 	if (ret)
315 		return ret;
316 
317 	switch (attr) {
318 	case TRIGGER_NETDEV_LINK:
319 	case TRIGGER_NETDEV_LINK_10:
320 	case TRIGGER_NETDEV_LINK_100:
321 	case TRIGGER_NETDEV_LINK_1000:
322 	case TRIGGER_NETDEV_HALF_DUPLEX:
323 	case TRIGGER_NETDEV_FULL_DUPLEX:
324 	case TRIGGER_NETDEV_TX:
325 	case TRIGGER_NETDEV_RX:
326 		bit = attr;
327 		break;
328 	default:
329 		return -EINVAL;
330 	}
331 
332 	if (state)
333 		set_bit(bit, &mode);
334 	else
335 		clear_bit(bit, &mode);
336 
337 	if (test_bit(TRIGGER_NETDEV_LINK, &mode) &&
338 	    (test_bit(TRIGGER_NETDEV_LINK_10, &mode) ||
339 	     test_bit(TRIGGER_NETDEV_LINK_100, &mode) ||
340 	     test_bit(TRIGGER_NETDEV_LINK_1000, &mode)))
341 		return -EINVAL;
342 
343 	cancel_delayed_work_sync(&trigger_data->work);
344 
345 	trigger_data->mode = mode;
346 	trigger_data->hw_control = can_hw_control(trigger_data);
347 
348 	set_baseline_state(trigger_data);
349 
350 	return size;
351 }
352 
353 #define DEFINE_NETDEV_TRIGGER(trigger_name, trigger) \
354 	static ssize_t trigger_name##_show(struct device *dev, \
355 		struct device_attribute *attr, char *buf) \
356 	{ \
357 		return netdev_led_attr_show(dev, buf, trigger); \
358 	} \
359 	static ssize_t trigger_name##_store(struct device *dev, \
360 		struct device_attribute *attr, const char *buf, size_t size) \
361 	{ \
362 		return netdev_led_attr_store(dev, buf, size, trigger); \
363 	} \
364 	static DEVICE_ATTR_RW(trigger_name)
365 
366 DEFINE_NETDEV_TRIGGER(link, TRIGGER_NETDEV_LINK);
367 DEFINE_NETDEV_TRIGGER(link_10, TRIGGER_NETDEV_LINK_10);
368 DEFINE_NETDEV_TRIGGER(link_100, TRIGGER_NETDEV_LINK_100);
369 DEFINE_NETDEV_TRIGGER(link_1000, TRIGGER_NETDEV_LINK_1000);
370 DEFINE_NETDEV_TRIGGER(half_duplex, TRIGGER_NETDEV_HALF_DUPLEX);
371 DEFINE_NETDEV_TRIGGER(full_duplex, TRIGGER_NETDEV_FULL_DUPLEX);
372 DEFINE_NETDEV_TRIGGER(tx, TRIGGER_NETDEV_TX);
373 DEFINE_NETDEV_TRIGGER(rx, TRIGGER_NETDEV_RX);
374 
375 static ssize_t interval_show(struct device *dev,
376 			     struct device_attribute *attr, char *buf)
377 {
378 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
379 
380 	return sprintf(buf, "%u\n",
381 		       jiffies_to_msecs(atomic_read(&trigger_data->interval)));
382 }
383 
384 static ssize_t interval_store(struct device *dev,
385 			      struct device_attribute *attr, const char *buf,
386 			      size_t size)
387 {
388 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
389 	unsigned long value;
390 	int ret;
391 
392 	if (trigger_data->hw_control)
393 		return -EINVAL;
394 
395 	ret = kstrtoul(buf, 0, &value);
396 	if (ret)
397 		return ret;
398 
399 	/* impose some basic bounds on the timer interval */
400 	if (value >= 5 && value <= 10000) {
401 		cancel_delayed_work_sync(&trigger_data->work);
402 
403 		atomic_set(&trigger_data->interval, msecs_to_jiffies(value));
404 		set_baseline_state(trigger_data);	/* resets timer */
405 	}
406 
407 	return size;
408 }
409 
410 static DEVICE_ATTR_RW(interval);
411 
412 static ssize_t offloaded_show(struct device *dev,
413 			      struct device_attribute *attr, char *buf)
414 {
415 	struct led_netdev_data *trigger_data = led_trigger_get_drvdata(dev);
416 
417 	return sprintf(buf, "%d\n", trigger_data->hw_control);
418 }
419 
420 static DEVICE_ATTR_RO(offloaded);
421 
422 static struct attribute *netdev_trig_attrs[] = {
423 	&dev_attr_device_name.attr,
424 	&dev_attr_link.attr,
425 	&dev_attr_link_10.attr,
426 	&dev_attr_link_100.attr,
427 	&dev_attr_link_1000.attr,
428 	&dev_attr_full_duplex.attr,
429 	&dev_attr_half_duplex.attr,
430 	&dev_attr_rx.attr,
431 	&dev_attr_tx.attr,
432 	&dev_attr_interval.attr,
433 	&dev_attr_offloaded.attr,
434 	NULL
435 };
436 ATTRIBUTE_GROUPS(netdev_trig);
437 
438 static int netdev_trig_notify(struct notifier_block *nb,
439 			      unsigned long evt, void *dv)
440 {
441 	struct net_device *dev =
442 		netdev_notifier_info_to_dev((struct netdev_notifier_info *)dv);
443 	struct led_netdev_data *trigger_data =
444 		container_of(nb, struct led_netdev_data, notifier);
445 
446 	if (evt != NETDEV_UP && evt != NETDEV_DOWN && evt != NETDEV_CHANGE
447 	    && evt != NETDEV_REGISTER && evt != NETDEV_UNREGISTER
448 	    && evt != NETDEV_CHANGENAME)
449 		return NOTIFY_DONE;
450 
451 	if (!(dev == trigger_data->net_dev ||
452 	      (evt == NETDEV_CHANGENAME && !strcmp(dev->name, trigger_data->device_name)) ||
453 	      (evt == NETDEV_REGISTER && !strcmp(dev->name, trigger_data->device_name))))
454 		return NOTIFY_DONE;
455 
456 	cancel_delayed_work_sync(&trigger_data->work);
457 
458 	mutex_lock(&trigger_data->lock);
459 
460 	trigger_data->carrier_link_up = false;
461 	trigger_data->link_speed = SPEED_UNKNOWN;
462 	trigger_data->duplex = DUPLEX_UNKNOWN;
463 	switch (evt) {
464 	case NETDEV_CHANGENAME:
465 		get_device_state(trigger_data);
466 		fallthrough;
467 	case NETDEV_REGISTER:
468 		dev_put(trigger_data->net_dev);
469 		dev_hold(dev);
470 		trigger_data->net_dev = dev;
471 		break;
472 	case NETDEV_UNREGISTER:
473 		dev_put(trigger_data->net_dev);
474 		trigger_data->net_dev = NULL;
475 		break;
476 	case NETDEV_UP:
477 	case NETDEV_CHANGE:
478 		get_device_state(trigger_data);
479 		break;
480 	}
481 
482 	set_baseline_state(trigger_data);
483 
484 	mutex_unlock(&trigger_data->lock);
485 
486 	return NOTIFY_DONE;
487 }
488 
489 /* here's the real work! */
490 static void netdev_trig_work(struct work_struct *work)
491 {
492 	struct led_netdev_data *trigger_data =
493 		container_of(work, struct led_netdev_data, work.work);
494 	struct rtnl_link_stats64 *dev_stats;
495 	unsigned int new_activity;
496 	struct rtnl_link_stats64 temp;
497 	unsigned long interval;
498 	int invert;
499 
500 	/* If we dont have a device, insure we are off */
501 	if (!trigger_data->net_dev) {
502 		led_set_brightness(trigger_data->led_cdev, LED_OFF);
503 		return;
504 	}
505 
506 	/* If we are not looking for RX/TX then return  */
507 	if (!test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) &&
508 	    !test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode))
509 		return;
510 
511 	dev_stats = dev_get_stats(trigger_data->net_dev, &temp);
512 	new_activity =
513 	    (test_bit(TRIGGER_NETDEV_TX, &trigger_data->mode) ?
514 		dev_stats->tx_packets : 0) +
515 	    (test_bit(TRIGGER_NETDEV_RX, &trigger_data->mode) ?
516 		dev_stats->rx_packets : 0);
517 
518 	if (trigger_data->last_activity != new_activity) {
519 		led_stop_software_blink(trigger_data->led_cdev);
520 
521 		invert = test_bit(TRIGGER_NETDEV_LINK, &trigger_data->mode) ||
522 			 test_bit(TRIGGER_NETDEV_LINK_10, &trigger_data->mode) ||
523 			 test_bit(TRIGGER_NETDEV_LINK_100, &trigger_data->mode) ||
524 			 test_bit(TRIGGER_NETDEV_LINK_1000, &trigger_data->mode) ||
525 			 test_bit(TRIGGER_NETDEV_HALF_DUPLEX, &trigger_data->mode) ||
526 			 test_bit(TRIGGER_NETDEV_FULL_DUPLEX, &trigger_data->mode);
527 		interval = jiffies_to_msecs(
528 				atomic_read(&trigger_data->interval));
529 		/* base state is ON (link present) */
530 		led_blink_set_oneshot(trigger_data->led_cdev,
531 				      &interval,
532 				      &interval,
533 				      invert);
534 		trigger_data->last_activity = new_activity;
535 	}
536 
537 	schedule_delayed_work(&trigger_data->work,
538 			(atomic_read(&trigger_data->interval)*2));
539 }
540 
541 static int netdev_trig_activate(struct led_classdev *led_cdev)
542 {
543 	struct led_netdev_data *trigger_data;
544 	unsigned long mode = 0;
545 	struct device *dev;
546 	int rc;
547 
548 	trigger_data = kzalloc(sizeof(struct led_netdev_data), GFP_KERNEL);
549 	if (!trigger_data)
550 		return -ENOMEM;
551 
552 	mutex_init(&trigger_data->lock);
553 
554 	trigger_data->notifier.notifier_call = netdev_trig_notify;
555 	trigger_data->notifier.priority = 10;
556 
557 	INIT_DELAYED_WORK(&trigger_data->work, netdev_trig_work);
558 
559 	trigger_data->led_cdev = led_cdev;
560 	trigger_data->net_dev = NULL;
561 	trigger_data->device_name[0] = 0;
562 
563 	trigger_data->mode = 0;
564 	atomic_set(&trigger_data->interval, msecs_to_jiffies(NETDEV_LED_DEFAULT_INTERVAL));
565 	trigger_data->last_activity = 0;
566 
567 	/* Check if hw control is active by default on the LED.
568 	 * Init already enabled mode in hw control.
569 	 */
570 	if (supports_hw_control(led_cdev)) {
571 		dev = led_cdev->hw_control_get_device(led_cdev);
572 		if (dev) {
573 			const char *name = dev_name(dev);
574 
575 			set_device_name(trigger_data, name, strlen(name));
576 			trigger_data->hw_control = true;
577 
578 			rc = led_cdev->hw_control_get(led_cdev, &mode);
579 			if (!rc)
580 				trigger_data->mode = mode;
581 		}
582 	}
583 
584 	led_set_trigger_data(led_cdev, trigger_data);
585 
586 	rc = register_netdevice_notifier(&trigger_data->notifier);
587 	if (rc)
588 		kfree(trigger_data);
589 
590 	return rc;
591 }
592 
593 static void netdev_trig_deactivate(struct led_classdev *led_cdev)
594 {
595 	struct led_netdev_data *trigger_data = led_get_trigger_data(led_cdev);
596 
597 	unregister_netdevice_notifier(&trigger_data->notifier);
598 
599 	cancel_delayed_work_sync(&trigger_data->work);
600 
601 	led_set_brightness(led_cdev, LED_OFF);
602 
603 	dev_put(trigger_data->net_dev);
604 
605 	kfree(trigger_data);
606 }
607 
608 static struct led_trigger netdev_led_trigger = {
609 	.name = "netdev",
610 	.activate = netdev_trig_activate,
611 	.deactivate = netdev_trig_deactivate,
612 	.groups = netdev_trig_groups,
613 };
614 
615 module_led_trigger(netdev_led_trigger);
616 
617 MODULE_AUTHOR("Ben Whitten <ben.whitten@gmail.com>");
618 MODULE_AUTHOR("Oliver Jowett <oliver@opencloud.com>");
619 MODULE_DESCRIPTION("Netdev LED trigger");
620 MODULE_LICENSE("GPL v2");
621