]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/net/can/dev.c
Merge tag 's390-5.3-5' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[thirdparty/linux.git] / drivers / net / can / dev.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
4 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
5 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
6 */
7
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/netdevice.h>
12 #include <linux/if_arp.h>
13 #include <linux/workqueue.h>
14 #include <linux/can.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/skb.h>
17 #include <linux/can/netlink.h>
18 #include <linux/can/led.h>
19 #include <linux/of.h>
20 #include <net/rtnetlink.h>
21
22 #define MOD_DESC "CAN device driver interface"
23
24 MODULE_DESCRIPTION(MOD_DESC);
25 MODULE_LICENSE("GPL v2");
26 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
27
28 /* CAN DLC to real data length conversion helpers */
29
30 static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
31 8, 12, 16, 20, 24, 32, 48, 64};
32
33 /* get data length from can_dlc with sanitized can_dlc */
34 u8 can_dlc2len(u8 can_dlc)
35 {
36 return dlc2len[can_dlc & 0x0F];
37 }
38 EXPORT_SYMBOL_GPL(can_dlc2len);
39
40 static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
41 9, 9, 9, 9, /* 9 - 12 */
42 10, 10, 10, 10, /* 13 - 16 */
43 11, 11, 11, 11, /* 17 - 20 */
44 12, 12, 12, 12, /* 21 - 24 */
45 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
46 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
47 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
48 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
49 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
50
51 /* map the sanitized data length to an appropriate data length code */
52 u8 can_len2dlc(u8 len)
53 {
54 if (unlikely(len > 64))
55 return 0xF;
56
57 return len2dlc[len];
58 }
59 EXPORT_SYMBOL_GPL(can_len2dlc);
60
61 #ifdef CONFIG_CAN_CALC_BITTIMING
62 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
63 #define CAN_CALC_SYNC_SEG 1
64
65 /*
66 * Bit-timing calculation derived from:
67 *
68 * Code based on LinCAN sources and H8S2638 project
69 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
70 * Copyright 2005 Stanislav Marek
71 * email: pisa@cmp.felk.cvut.cz
72 *
73 * Calculates proper bit-timing parameters for a specified bit-rate
74 * and sample-point, which can then be used to set the bit-timing
75 * registers of the CAN controller. You can find more information
76 * in the header file linux/can/netlink.h.
77 */
78 static int can_update_sample_point(const struct can_bittiming_const *btc,
79 unsigned int sample_point_nominal, unsigned int tseg,
80 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
81 unsigned int *sample_point_error_ptr)
82 {
83 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
84 unsigned int sample_point, best_sample_point = 0;
85 unsigned int tseg1, tseg2;
86 int i;
87
88 for (i = 0; i <= 1; i++) {
89 tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
90 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
91 tseg1 = tseg - tseg2;
92 if (tseg1 > btc->tseg1_max) {
93 tseg1 = btc->tseg1_max;
94 tseg2 = tseg - tseg1;
95 }
96
97 sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
98 sample_point_error = abs(sample_point_nominal - sample_point);
99
100 if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
101 best_sample_point = sample_point;
102 best_sample_point_error = sample_point_error;
103 *tseg1_ptr = tseg1;
104 *tseg2_ptr = tseg2;
105 }
106 }
107
108 if (sample_point_error_ptr)
109 *sample_point_error_ptr = best_sample_point_error;
110
111 return best_sample_point;
112 }
113
114 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
115 const struct can_bittiming_const *btc)
116 {
117 struct can_priv *priv = netdev_priv(dev);
118 unsigned int bitrate; /* current bitrate */
119 unsigned int bitrate_error; /* difference between current and nominal value */
120 unsigned int best_bitrate_error = UINT_MAX;
121 unsigned int sample_point_error; /* difference between current and nominal value */
122 unsigned int best_sample_point_error = UINT_MAX;
123 unsigned int sample_point_nominal; /* nominal sample point */
124 unsigned int best_tseg = 0; /* current best value for tseg */
125 unsigned int best_brp = 0; /* current best value for brp */
126 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
127 u64 v64;
128
129 /* Use CiA recommended sample points */
130 if (bt->sample_point) {
131 sample_point_nominal = bt->sample_point;
132 } else {
133 if (bt->bitrate > 800000)
134 sample_point_nominal = 750;
135 else if (bt->bitrate > 500000)
136 sample_point_nominal = 800;
137 else
138 sample_point_nominal = 875;
139 }
140
141 /* tseg even = round down, odd = round up */
142 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
143 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
144 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
145
146 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
147 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
148
149 /* choose brp step which is possible in system */
150 brp = (brp / btc->brp_inc) * btc->brp_inc;
151 if ((brp < btc->brp_min) || (brp > btc->brp_max))
152 continue;
153
154 bitrate = priv->clock.freq / (brp * tsegall);
155 bitrate_error = abs(bt->bitrate - bitrate);
156
157 /* tseg brp biterror */
158 if (bitrate_error > best_bitrate_error)
159 continue;
160
161 /* reset sample point error if we have a better bitrate */
162 if (bitrate_error < best_bitrate_error)
163 best_sample_point_error = UINT_MAX;
164
165 can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
166 if (sample_point_error > best_sample_point_error)
167 continue;
168
169 best_sample_point_error = sample_point_error;
170 best_bitrate_error = bitrate_error;
171 best_tseg = tseg / 2;
172 best_brp = brp;
173
174 if (bitrate_error == 0 && sample_point_error == 0)
175 break;
176 }
177
178 if (best_bitrate_error) {
179 /* Error in one-tenth of a percent */
180 v64 = (u64)best_bitrate_error * 1000;
181 do_div(v64, bt->bitrate);
182 bitrate_error = (u32)v64;
183 if (bitrate_error > CAN_CALC_MAX_ERROR) {
184 netdev_err(dev,
185 "bitrate error %d.%d%% too high\n",
186 bitrate_error / 10, bitrate_error % 10);
187 return -EDOM;
188 }
189 netdev_warn(dev, "bitrate error %d.%d%%\n",
190 bitrate_error / 10, bitrate_error % 10);
191 }
192
193 /* real sample point */
194 bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
195 &tseg1, &tseg2, NULL);
196
197 v64 = (u64)best_brp * 1000 * 1000 * 1000;
198 do_div(v64, priv->clock.freq);
199 bt->tq = (u32)v64;
200 bt->prop_seg = tseg1 / 2;
201 bt->phase_seg1 = tseg1 - bt->prop_seg;
202 bt->phase_seg2 = tseg2;
203
204 /* check for sjw user settings */
205 if (!bt->sjw || !btc->sjw_max) {
206 bt->sjw = 1;
207 } else {
208 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
209 if (bt->sjw > btc->sjw_max)
210 bt->sjw = btc->sjw_max;
211 /* bt->sjw must not be higher than tseg2 */
212 if (tseg2 < bt->sjw)
213 bt->sjw = tseg2;
214 }
215
216 bt->brp = best_brp;
217
218 /* real bitrate */
219 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
220
221 return 0;
222 }
223 #else /* !CONFIG_CAN_CALC_BITTIMING */
224 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
225 const struct can_bittiming_const *btc)
226 {
227 netdev_err(dev, "bit-timing calculation not available\n");
228 return -EINVAL;
229 }
230 #endif /* CONFIG_CAN_CALC_BITTIMING */
231
232 /*
233 * Checks the validity of the specified bit-timing parameters prop_seg,
234 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
235 * prescaler value brp. You can find more information in the header
236 * file linux/can/netlink.h.
237 */
238 static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
239 const struct can_bittiming_const *btc)
240 {
241 struct can_priv *priv = netdev_priv(dev);
242 int tseg1, alltseg;
243 u64 brp64;
244
245 tseg1 = bt->prop_seg + bt->phase_seg1;
246 if (!bt->sjw)
247 bt->sjw = 1;
248 if (bt->sjw > btc->sjw_max ||
249 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
250 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
251 return -ERANGE;
252
253 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
254 if (btc->brp_inc > 1)
255 do_div(brp64, btc->brp_inc);
256 brp64 += 500000000UL - 1;
257 do_div(brp64, 1000000000UL); /* the practicable BRP */
258 if (btc->brp_inc > 1)
259 brp64 *= btc->brp_inc;
260 bt->brp = (u32)brp64;
261
262 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
263 return -EINVAL;
264
265 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
266 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
267 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
268
269 return 0;
270 }
271
272 /* Checks the validity of predefined bitrate settings */
273 static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
274 const u32 *bitrate_const,
275 const unsigned int bitrate_const_cnt)
276 {
277 struct can_priv *priv = netdev_priv(dev);
278 unsigned int i;
279
280 for (i = 0; i < bitrate_const_cnt; i++) {
281 if (bt->bitrate == bitrate_const[i])
282 break;
283 }
284
285 if (i >= priv->bitrate_const_cnt)
286 return -EINVAL;
287
288 return 0;
289 }
290
291 static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
292 const struct can_bittiming_const *btc,
293 const u32 *bitrate_const,
294 const unsigned int bitrate_const_cnt)
295 {
296 int err;
297
298 /*
299 * Depending on the given can_bittiming parameter structure the CAN
300 * timing parameters are calculated based on the provided bitrate OR
301 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
302 * provided directly which are then checked and fixed up.
303 */
304 if (!bt->tq && bt->bitrate && btc)
305 err = can_calc_bittiming(dev, bt, btc);
306 else if (bt->tq && !bt->bitrate && btc)
307 err = can_fixup_bittiming(dev, bt, btc);
308 else if (!bt->tq && bt->bitrate && bitrate_const)
309 err = can_validate_bitrate(dev, bt, bitrate_const,
310 bitrate_const_cnt);
311 else
312 err = -EINVAL;
313
314 return err;
315 }
316
317 static void can_update_state_error_stats(struct net_device *dev,
318 enum can_state new_state)
319 {
320 struct can_priv *priv = netdev_priv(dev);
321
322 if (new_state <= priv->state)
323 return;
324
325 switch (new_state) {
326 case CAN_STATE_ERROR_WARNING:
327 priv->can_stats.error_warning++;
328 break;
329 case CAN_STATE_ERROR_PASSIVE:
330 priv->can_stats.error_passive++;
331 break;
332 case CAN_STATE_BUS_OFF:
333 priv->can_stats.bus_off++;
334 break;
335 default:
336 break;
337 }
338 }
339
340 static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
341 {
342 switch (state) {
343 case CAN_STATE_ERROR_ACTIVE:
344 return CAN_ERR_CRTL_ACTIVE;
345 case CAN_STATE_ERROR_WARNING:
346 return CAN_ERR_CRTL_TX_WARNING;
347 case CAN_STATE_ERROR_PASSIVE:
348 return CAN_ERR_CRTL_TX_PASSIVE;
349 default:
350 return 0;
351 }
352 }
353
354 static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
355 {
356 switch (state) {
357 case CAN_STATE_ERROR_ACTIVE:
358 return CAN_ERR_CRTL_ACTIVE;
359 case CAN_STATE_ERROR_WARNING:
360 return CAN_ERR_CRTL_RX_WARNING;
361 case CAN_STATE_ERROR_PASSIVE:
362 return CAN_ERR_CRTL_RX_PASSIVE;
363 default:
364 return 0;
365 }
366 }
367
368 void can_change_state(struct net_device *dev, struct can_frame *cf,
369 enum can_state tx_state, enum can_state rx_state)
370 {
371 struct can_priv *priv = netdev_priv(dev);
372 enum can_state new_state = max(tx_state, rx_state);
373
374 if (unlikely(new_state == priv->state)) {
375 netdev_warn(dev, "%s: oops, state did not change", __func__);
376 return;
377 }
378
379 netdev_dbg(dev, "New error state: %d\n", new_state);
380
381 can_update_state_error_stats(dev, new_state);
382 priv->state = new_state;
383
384 if (!cf)
385 return;
386
387 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
388 cf->can_id |= CAN_ERR_BUSOFF;
389 return;
390 }
391
392 cf->can_id |= CAN_ERR_CRTL;
393 cf->data[1] |= tx_state >= rx_state ?
394 can_tx_state_to_frame(dev, tx_state) : 0;
395 cf->data[1] |= tx_state <= rx_state ?
396 can_rx_state_to_frame(dev, rx_state) : 0;
397 }
398 EXPORT_SYMBOL_GPL(can_change_state);
399
400 /*
401 * Local echo of CAN messages
402 *
403 * CAN network devices *should* support a local echo functionality
404 * (see Documentation/networking/can.rst). To test the handling of CAN
405 * interfaces that do not support the local echo both driver types are
406 * implemented. In the case that the driver does not support the echo
407 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
408 * to perform the echo as a fallback solution.
409 */
410 static void can_flush_echo_skb(struct net_device *dev)
411 {
412 struct can_priv *priv = netdev_priv(dev);
413 struct net_device_stats *stats = &dev->stats;
414 int i;
415
416 for (i = 0; i < priv->echo_skb_max; i++) {
417 if (priv->echo_skb[i]) {
418 kfree_skb(priv->echo_skb[i]);
419 priv->echo_skb[i] = NULL;
420 stats->tx_dropped++;
421 stats->tx_aborted_errors++;
422 }
423 }
424 }
425
426 /*
427 * Put the skb on the stack to be looped backed locally lateron
428 *
429 * The function is typically called in the start_xmit function
430 * of the device driver. The driver must protect access to
431 * priv->echo_skb, if necessary.
432 */
433 void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
434 unsigned int idx)
435 {
436 struct can_priv *priv = netdev_priv(dev);
437
438 BUG_ON(idx >= priv->echo_skb_max);
439
440 /* check flag whether this packet has to be looped back */
441 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
442 (skb->protocol != htons(ETH_P_CAN) &&
443 skb->protocol != htons(ETH_P_CANFD))) {
444 kfree_skb(skb);
445 return;
446 }
447
448 if (!priv->echo_skb[idx]) {
449
450 skb = can_create_echo_skb(skb);
451 if (!skb)
452 return;
453
454 /* make settings for echo to reduce code in irq context */
455 skb->pkt_type = PACKET_BROADCAST;
456 skb->ip_summed = CHECKSUM_UNNECESSARY;
457 skb->dev = dev;
458
459 /* save this skb for tx interrupt echo handling */
460 priv->echo_skb[idx] = skb;
461 } else {
462 /* locking problem with netif_stop_queue() ?? */
463 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
464 kfree_skb(skb);
465 }
466 }
467 EXPORT_SYMBOL_GPL(can_put_echo_skb);
468
469 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
470 {
471 struct can_priv *priv = netdev_priv(dev);
472
473 if (idx >= priv->echo_skb_max) {
474 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
475 __func__, idx, priv->echo_skb_max);
476 return NULL;
477 }
478
479 if (priv->echo_skb[idx]) {
480 /* Using "struct canfd_frame::len" for the frame
481 * length is supported on both CAN and CANFD frames.
482 */
483 struct sk_buff *skb = priv->echo_skb[idx];
484 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
485 u8 len = cf->len;
486
487 *len_ptr = len;
488 priv->echo_skb[idx] = NULL;
489
490 return skb;
491 }
492
493 return NULL;
494 }
495
496 /*
497 * Get the skb from the stack and loop it back locally
498 *
499 * The function is typically called when the TX done interrupt
500 * is handled in the device driver. The driver must protect
501 * access to priv->echo_skb, if necessary.
502 */
503 unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
504 {
505 struct sk_buff *skb;
506 u8 len;
507
508 skb = __can_get_echo_skb(dev, idx, &len);
509 if (!skb)
510 return 0;
511
512 netif_rx(skb);
513
514 return len;
515 }
516 EXPORT_SYMBOL_GPL(can_get_echo_skb);
517
518 /*
519 * Remove the skb from the stack and free it.
520 *
521 * The function is typically called when TX failed.
522 */
523 void can_free_echo_skb(struct net_device *dev, unsigned int idx)
524 {
525 struct can_priv *priv = netdev_priv(dev);
526
527 BUG_ON(idx >= priv->echo_skb_max);
528
529 if (priv->echo_skb[idx]) {
530 dev_kfree_skb_any(priv->echo_skb[idx]);
531 priv->echo_skb[idx] = NULL;
532 }
533 }
534 EXPORT_SYMBOL_GPL(can_free_echo_skb);
535
536 /*
537 * CAN device restart for bus-off recovery
538 */
539 static void can_restart(struct net_device *dev)
540 {
541 struct can_priv *priv = netdev_priv(dev);
542 struct net_device_stats *stats = &dev->stats;
543 struct sk_buff *skb;
544 struct can_frame *cf;
545 int err;
546
547 BUG_ON(netif_carrier_ok(dev));
548
549 /*
550 * No synchronization needed because the device is bus-off and
551 * no messages can come in or go out.
552 */
553 can_flush_echo_skb(dev);
554
555 /* send restart message upstream */
556 skb = alloc_can_err_skb(dev, &cf);
557 if (skb == NULL) {
558 err = -ENOMEM;
559 goto restart;
560 }
561 cf->can_id |= CAN_ERR_RESTARTED;
562
563 netif_rx(skb);
564
565 stats->rx_packets++;
566 stats->rx_bytes += cf->can_dlc;
567
568 restart:
569 netdev_dbg(dev, "restarted\n");
570 priv->can_stats.restarts++;
571
572 /* Now restart the device */
573 err = priv->do_set_mode(dev, CAN_MODE_START);
574
575 netif_carrier_on(dev);
576 if (err)
577 netdev_err(dev, "Error %d during restart", err);
578 }
579
580 static void can_restart_work(struct work_struct *work)
581 {
582 struct delayed_work *dwork = to_delayed_work(work);
583 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
584
585 can_restart(priv->dev);
586 }
587
588 int can_restart_now(struct net_device *dev)
589 {
590 struct can_priv *priv = netdev_priv(dev);
591
592 /*
593 * A manual restart is only permitted if automatic restart is
594 * disabled and the device is in the bus-off state
595 */
596 if (priv->restart_ms)
597 return -EINVAL;
598 if (priv->state != CAN_STATE_BUS_OFF)
599 return -EBUSY;
600
601 cancel_delayed_work_sync(&priv->restart_work);
602 can_restart(dev);
603
604 return 0;
605 }
606
607 /*
608 * CAN bus-off
609 *
610 * This functions should be called when the device goes bus-off to
611 * tell the netif layer that no more packets can be sent or received.
612 * If enabled, a timer is started to trigger bus-off recovery.
613 */
614 void can_bus_off(struct net_device *dev)
615 {
616 struct can_priv *priv = netdev_priv(dev);
617
618 netdev_info(dev, "bus-off\n");
619
620 netif_carrier_off(dev);
621
622 if (priv->restart_ms)
623 schedule_delayed_work(&priv->restart_work,
624 msecs_to_jiffies(priv->restart_ms));
625 }
626 EXPORT_SYMBOL_GPL(can_bus_off);
627
628 static void can_setup(struct net_device *dev)
629 {
630 dev->type = ARPHRD_CAN;
631 dev->mtu = CAN_MTU;
632 dev->hard_header_len = 0;
633 dev->addr_len = 0;
634 dev->tx_queue_len = 10;
635
636 /* New-style flags. */
637 dev->flags = IFF_NOARP;
638 dev->features = NETIF_F_HW_CSUM;
639 }
640
641 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
642 {
643 struct sk_buff *skb;
644
645 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
646 sizeof(struct can_frame));
647 if (unlikely(!skb))
648 return NULL;
649
650 skb->protocol = htons(ETH_P_CAN);
651 skb->pkt_type = PACKET_BROADCAST;
652 skb->ip_summed = CHECKSUM_UNNECESSARY;
653
654 skb_reset_mac_header(skb);
655 skb_reset_network_header(skb);
656 skb_reset_transport_header(skb);
657
658 can_skb_reserve(skb);
659 can_skb_prv(skb)->ifindex = dev->ifindex;
660 can_skb_prv(skb)->skbcnt = 0;
661
662 *cf = skb_put_zero(skb, sizeof(struct can_frame));
663
664 return skb;
665 }
666 EXPORT_SYMBOL_GPL(alloc_can_skb);
667
668 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
669 struct canfd_frame **cfd)
670 {
671 struct sk_buff *skb;
672
673 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
674 sizeof(struct canfd_frame));
675 if (unlikely(!skb))
676 return NULL;
677
678 skb->protocol = htons(ETH_P_CANFD);
679 skb->pkt_type = PACKET_BROADCAST;
680 skb->ip_summed = CHECKSUM_UNNECESSARY;
681
682 skb_reset_mac_header(skb);
683 skb_reset_network_header(skb);
684 skb_reset_transport_header(skb);
685
686 can_skb_reserve(skb);
687 can_skb_prv(skb)->ifindex = dev->ifindex;
688 can_skb_prv(skb)->skbcnt = 0;
689
690 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
691
692 return skb;
693 }
694 EXPORT_SYMBOL_GPL(alloc_canfd_skb);
695
696 struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
697 {
698 struct sk_buff *skb;
699
700 skb = alloc_can_skb(dev, cf);
701 if (unlikely(!skb))
702 return NULL;
703
704 (*cf)->can_id = CAN_ERR_FLAG;
705 (*cf)->can_dlc = CAN_ERR_DLC;
706
707 return skb;
708 }
709 EXPORT_SYMBOL_GPL(alloc_can_err_skb);
710
711 /*
712 * Allocate and setup space for the CAN network device
713 */
714 struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
715 unsigned int txqs, unsigned int rxqs)
716 {
717 struct net_device *dev;
718 struct can_priv *priv;
719 int size;
720
721 if (echo_skb_max)
722 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
723 echo_skb_max * sizeof(struct sk_buff *);
724 else
725 size = sizeof_priv;
726
727 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
728 txqs, rxqs);
729 if (!dev)
730 return NULL;
731
732 priv = netdev_priv(dev);
733 priv->dev = dev;
734
735 if (echo_skb_max) {
736 priv->echo_skb_max = echo_skb_max;
737 priv->echo_skb = (void *)priv +
738 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
739 }
740
741 priv->state = CAN_STATE_STOPPED;
742
743 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
744
745 return dev;
746 }
747 EXPORT_SYMBOL_GPL(alloc_candev_mqs);
748
749 /*
750 * Free space of the CAN network device
751 */
752 void free_candev(struct net_device *dev)
753 {
754 free_netdev(dev);
755 }
756 EXPORT_SYMBOL_GPL(free_candev);
757
758 /*
759 * changing MTU and control mode for CAN/CANFD devices
760 */
761 int can_change_mtu(struct net_device *dev, int new_mtu)
762 {
763 struct can_priv *priv = netdev_priv(dev);
764
765 /* Do not allow changing the MTU while running */
766 if (dev->flags & IFF_UP)
767 return -EBUSY;
768
769 /* allow change of MTU according to the CANFD ability of the device */
770 switch (new_mtu) {
771 case CAN_MTU:
772 /* 'CANFD-only' controllers can not switch to CAN_MTU */
773 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
774 return -EINVAL;
775
776 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
777 break;
778
779 case CANFD_MTU:
780 /* check for potential CANFD ability */
781 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
782 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
783 return -EINVAL;
784
785 priv->ctrlmode |= CAN_CTRLMODE_FD;
786 break;
787
788 default:
789 return -EINVAL;
790 }
791
792 dev->mtu = new_mtu;
793 return 0;
794 }
795 EXPORT_SYMBOL_GPL(can_change_mtu);
796
797 /*
798 * Common open function when the device gets opened.
799 *
800 * This function should be called in the open function of the device
801 * driver.
802 */
803 int open_candev(struct net_device *dev)
804 {
805 struct can_priv *priv = netdev_priv(dev);
806
807 if (!priv->bittiming.bitrate) {
808 netdev_err(dev, "bit-timing not yet defined\n");
809 return -EINVAL;
810 }
811
812 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
813 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
814 (!priv->data_bittiming.bitrate ||
815 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
816 netdev_err(dev, "incorrect/missing data bit-timing\n");
817 return -EINVAL;
818 }
819
820 /* Switch carrier on if device was stopped while in bus-off state */
821 if (!netif_carrier_ok(dev))
822 netif_carrier_on(dev);
823
824 return 0;
825 }
826 EXPORT_SYMBOL_GPL(open_candev);
827
828 #ifdef CONFIG_OF
829 /* Common function that can be used to understand the limitation of
830 * a transceiver when it provides no means to determine these limitations
831 * at runtime.
832 */
833 void of_can_transceiver(struct net_device *dev)
834 {
835 struct device_node *dn;
836 struct can_priv *priv = netdev_priv(dev);
837 struct device_node *np = dev->dev.parent->of_node;
838 int ret;
839
840 dn = of_get_child_by_name(np, "can-transceiver");
841 if (!dn)
842 return;
843
844 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
845 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
846 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
847 }
848 EXPORT_SYMBOL_GPL(of_can_transceiver);
849 #endif
850
851 /*
852 * Common close function for cleanup before the device gets closed.
853 *
854 * This function should be called in the close function of the device
855 * driver.
856 */
857 void close_candev(struct net_device *dev)
858 {
859 struct can_priv *priv = netdev_priv(dev);
860
861 cancel_delayed_work_sync(&priv->restart_work);
862 can_flush_echo_skb(dev);
863 }
864 EXPORT_SYMBOL_GPL(close_candev);
865
866 /*
867 * CAN netlink interface
868 */
869 static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
870 [IFLA_CAN_STATE] = { .type = NLA_U32 },
871 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
872 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
873 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
874 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
875 [IFLA_CAN_BITTIMING_CONST]
876 = { .len = sizeof(struct can_bittiming_const) },
877 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
878 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
879 [IFLA_CAN_DATA_BITTIMING]
880 = { .len = sizeof(struct can_bittiming) },
881 [IFLA_CAN_DATA_BITTIMING_CONST]
882 = { .len = sizeof(struct can_bittiming_const) },
883 };
884
885 static int can_validate(struct nlattr *tb[], struct nlattr *data[],
886 struct netlink_ext_ack *extack)
887 {
888 bool is_can_fd = false;
889
890 /* Make sure that valid CAN FD configurations always consist of
891 * - nominal/arbitration bittiming
892 * - data bittiming
893 * - control mode with CAN_CTRLMODE_FD set
894 */
895
896 if (!data)
897 return 0;
898
899 if (data[IFLA_CAN_CTRLMODE]) {
900 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
901
902 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
903 }
904
905 if (is_can_fd) {
906 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
907 return -EOPNOTSUPP;
908 }
909
910 if (data[IFLA_CAN_DATA_BITTIMING]) {
911 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
912 return -EOPNOTSUPP;
913 }
914
915 return 0;
916 }
917
918 static int can_changelink(struct net_device *dev, struct nlattr *tb[],
919 struct nlattr *data[],
920 struct netlink_ext_ack *extack)
921 {
922 struct can_priv *priv = netdev_priv(dev);
923 int err;
924
925 /* We need synchronization with dev->stop() */
926 ASSERT_RTNL();
927
928 if (data[IFLA_CAN_BITTIMING]) {
929 struct can_bittiming bt;
930
931 /* Do not allow changing bittiming while running */
932 if (dev->flags & IFF_UP)
933 return -EBUSY;
934
935 /* Calculate bittiming parameters based on
936 * bittiming_const if set, otherwise pass bitrate
937 * directly via do_set_bitrate(). Bail out if neither
938 * is given.
939 */
940 if (!priv->bittiming_const && !priv->do_set_bittiming)
941 return -EOPNOTSUPP;
942
943 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
944 err = can_get_bittiming(dev, &bt,
945 priv->bittiming_const,
946 priv->bitrate_const,
947 priv->bitrate_const_cnt);
948 if (err)
949 return err;
950
951 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
952 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
953 priv->bitrate_max);
954 return -EINVAL;
955 }
956
957 memcpy(&priv->bittiming, &bt, sizeof(bt));
958
959 if (priv->do_set_bittiming) {
960 /* Finally, set the bit-timing registers */
961 err = priv->do_set_bittiming(dev);
962 if (err)
963 return err;
964 }
965 }
966
967 if (data[IFLA_CAN_CTRLMODE]) {
968 struct can_ctrlmode *cm;
969 u32 ctrlstatic;
970 u32 maskedflags;
971
972 /* Do not allow changing controller mode while running */
973 if (dev->flags & IFF_UP)
974 return -EBUSY;
975 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
976 ctrlstatic = priv->ctrlmode_static;
977 maskedflags = cm->flags & cm->mask;
978
979 /* check whether provided bits are allowed to be passed */
980 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
981 return -EOPNOTSUPP;
982
983 /* do not check for static fd-non-iso if 'fd' is disabled */
984 if (!(maskedflags & CAN_CTRLMODE_FD))
985 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
986
987 /* make sure static options are provided by configuration */
988 if ((maskedflags & ctrlstatic) != ctrlstatic)
989 return -EOPNOTSUPP;
990
991 /* clear bits to be modified and copy the flag values */
992 priv->ctrlmode &= ~cm->mask;
993 priv->ctrlmode |= maskedflags;
994
995 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
996 if (priv->ctrlmode & CAN_CTRLMODE_FD)
997 dev->mtu = CANFD_MTU;
998 else
999 dev->mtu = CAN_MTU;
1000 }
1001
1002 if (data[IFLA_CAN_RESTART_MS]) {
1003 /* Do not allow changing restart delay while running */
1004 if (dev->flags & IFF_UP)
1005 return -EBUSY;
1006 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
1007 }
1008
1009 if (data[IFLA_CAN_RESTART]) {
1010 /* Do not allow a restart while not running */
1011 if (!(dev->flags & IFF_UP))
1012 return -EINVAL;
1013 err = can_restart_now(dev);
1014 if (err)
1015 return err;
1016 }
1017
1018 if (data[IFLA_CAN_DATA_BITTIMING]) {
1019 struct can_bittiming dbt;
1020
1021 /* Do not allow changing bittiming while running */
1022 if (dev->flags & IFF_UP)
1023 return -EBUSY;
1024
1025 /* Calculate bittiming parameters based on
1026 * data_bittiming_const if set, otherwise pass bitrate
1027 * directly via do_set_bitrate(). Bail out if neither
1028 * is given.
1029 */
1030 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
1031 return -EOPNOTSUPP;
1032
1033 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
1034 sizeof(dbt));
1035 err = can_get_bittiming(dev, &dbt,
1036 priv->data_bittiming_const,
1037 priv->data_bitrate_const,
1038 priv->data_bitrate_const_cnt);
1039 if (err)
1040 return err;
1041
1042 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
1043 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
1044 priv->bitrate_max);
1045 return -EINVAL;
1046 }
1047
1048 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
1049
1050 if (priv->do_set_data_bittiming) {
1051 /* Finally, set the bit-timing registers */
1052 err = priv->do_set_data_bittiming(dev);
1053 if (err)
1054 return err;
1055 }
1056 }
1057
1058 if (data[IFLA_CAN_TERMINATION]) {
1059 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
1060 const unsigned int num_term = priv->termination_const_cnt;
1061 unsigned int i;
1062
1063 if (!priv->do_set_termination)
1064 return -EOPNOTSUPP;
1065
1066 /* check whether given value is supported by the interface */
1067 for (i = 0; i < num_term; i++) {
1068 if (termval == priv->termination_const[i])
1069 break;
1070 }
1071 if (i >= num_term)
1072 return -EINVAL;
1073
1074 /* Finally, set the termination value */
1075 err = priv->do_set_termination(dev, termval);
1076 if (err)
1077 return err;
1078
1079 priv->termination = termval;
1080 }
1081
1082 return 0;
1083 }
1084
1085 static size_t can_get_size(const struct net_device *dev)
1086 {
1087 struct can_priv *priv = netdev_priv(dev);
1088 size_t size = 0;
1089
1090 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
1091 size += nla_total_size(sizeof(struct can_bittiming));
1092 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
1093 size += nla_total_size(sizeof(struct can_bittiming_const));
1094 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
1095 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
1096 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
1097 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
1098 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
1099 size += nla_total_size(sizeof(struct can_berr_counter));
1100 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
1101 size += nla_total_size(sizeof(struct can_bittiming));
1102 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
1103 size += nla_total_size(sizeof(struct can_bittiming_const));
1104 if (priv->termination_const) {
1105 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
1106 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
1107 priv->termination_const_cnt);
1108 }
1109 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
1110 size += nla_total_size(sizeof(*priv->bitrate_const) *
1111 priv->bitrate_const_cnt);
1112 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
1113 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
1114 priv->data_bitrate_const_cnt);
1115 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
1116
1117 return size;
1118 }
1119
1120 static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
1121 {
1122 struct can_priv *priv = netdev_priv(dev);
1123 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
1124 struct can_berr_counter bec;
1125 enum can_state state = priv->state;
1126
1127 if (priv->do_get_state)
1128 priv->do_get_state(dev, &state);
1129
1130 if ((priv->bittiming.bitrate &&
1131 nla_put(skb, IFLA_CAN_BITTIMING,
1132 sizeof(priv->bittiming), &priv->bittiming)) ||
1133
1134 (priv->bittiming_const &&
1135 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
1136 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
1137
1138 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
1139 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
1140 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
1141 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
1142
1143 (priv->do_get_berr_counter &&
1144 !priv->do_get_berr_counter(dev, &bec) &&
1145 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
1146
1147 (priv->data_bittiming.bitrate &&
1148 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
1149 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
1150
1151 (priv->data_bittiming_const &&
1152 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
1153 sizeof(*priv->data_bittiming_const),
1154 priv->data_bittiming_const)) ||
1155
1156 (priv->termination_const &&
1157 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
1158 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
1159 sizeof(*priv->termination_const) *
1160 priv->termination_const_cnt,
1161 priv->termination_const))) ||
1162
1163 (priv->bitrate_const &&
1164 nla_put(skb, IFLA_CAN_BITRATE_CONST,
1165 sizeof(*priv->bitrate_const) *
1166 priv->bitrate_const_cnt,
1167 priv->bitrate_const)) ||
1168
1169 (priv->data_bitrate_const &&
1170 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
1171 sizeof(*priv->data_bitrate_const) *
1172 priv->data_bitrate_const_cnt,
1173 priv->data_bitrate_const)) ||
1174
1175 (nla_put(skb, IFLA_CAN_BITRATE_MAX,
1176 sizeof(priv->bitrate_max),
1177 &priv->bitrate_max))
1178 )
1179
1180 return -EMSGSIZE;
1181
1182 return 0;
1183 }
1184
1185 static size_t can_get_xstats_size(const struct net_device *dev)
1186 {
1187 return sizeof(struct can_device_stats);
1188 }
1189
1190 static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1191 {
1192 struct can_priv *priv = netdev_priv(dev);
1193
1194 if (nla_put(skb, IFLA_INFO_XSTATS,
1195 sizeof(priv->can_stats), &priv->can_stats))
1196 goto nla_put_failure;
1197 return 0;
1198
1199 nla_put_failure:
1200 return -EMSGSIZE;
1201 }
1202
1203 static int can_newlink(struct net *src_net, struct net_device *dev,
1204 struct nlattr *tb[], struct nlattr *data[],
1205 struct netlink_ext_ack *extack)
1206 {
1207 return -EOPNOTSUPP;
1208 }
1209
1210 static void can_dellink(struct net_device *dev, struct list_head *head)
1211 {
1212 return;
1213 }
1214
1215 static struct rtnl_link_ops can_link_ops __read_mostly = {
1216 .kind = "can",
1217 .maxtype = IFLA_CAN_MAX,
1218 .policy = can_policy,
1219 .setup = can_setup,
1220 .validate = can_validate,
1221 .newlink = can_newlink,
1222 .changelink = can_changelink,
1223 .dellink = can_dellink,
1224 .get_size = can_get_size,
1225 .fill_info = can_fill_info,
1226 .get_xstats_size = can_get_xstats_size,
1227 .fill_xstats = can_fill_xstats,
1228 };
1229
1230 /*
1231 * Register the CAN network device
1232 */
1233 int register_candev(struct net_device *dev)
1234 {
1235 struct can_priv *priv = netdev_priv(dev);
1236
1237 /* Ensure termination_const, termination_const_cnt and
1238 * do_set_termination consistency. All must be either set or
1239 * unset.
1240 */
1241 if ((!priv->termination_const != !priv->termination_const_cnt) ||
1242 (!priv->termination_const != !priv->do_set_termination))
1243 return -EINVAL;
1244
1245 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
1246 return -EINVAL;
1247
1248 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
1249 return -EINVAL;
1250
1251 dev->rtnl_link_ops = &can_link_ops;
1252 netif_carrier_off(dev);
1253
1254 return register_netdev(dev);
1255 }
1256 EXPORT_SYMBOL_GPL(register_candev);
1257
1258 /*
1259 * Unregister the CAN network device
1260 */
1261 void unregister_candev(struct net_device *dev)
1262 {
1263 unregister_netdev(dev);
1264 }
1265 EXPORT_SYMBOL_GPL(unregister_candev);
1266
1267 /*
1268 * Test if a network device is a candev based device
1269 * and return the can_priv* if so.
1270 */
1271 struct can_priv *safe_candev_priv(struct net_device *dev)
1272 {
1273 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1274 return NULL;
1275
1276 return netdev_priv(dev);
1277 }
1278 EXPORT_SYMBOL_GPL(safe_candev_priv);
1279
1280 static __init int can_dev_init(void)
1281 {
1282 int err;
1283
1284 can_led_notifier_init();
1285
1286 err = rtnl_link_register(&can_link_ops);
1287 if (!err)
1288 printk(KERN_INFO MOD_DESC "\n");
1289
1290 return err;
1291 }
1292 module_init(can_dev_init);
1293
1294 static __exit void can_dev_exit(void)
1295 {
1296 rtnl_link_unregister(&can_link_ops);
1297
1298 can_led_notifier_exit();
1299 }
1300 module_exit(can_dev_exit);
1301
1302 MODULE_ALIAS_RTNL_LINK("can");