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