blob: 3b3f88ffab53cded04a2c1586727bf82472f43ad [file] [log] [blame]
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001/*
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
Jeff Kirsher05780d92013-12-06 06:28:45 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000017 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000022#include <linux/netdevice.h>
23#include <linux/if_arp.h>
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +030024#include <linux/workqueue.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000025#include <linux/can.h>
26#include <linux/can/dev.h>
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010027#include <linux/can/skb.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000028#include <linux/can/netlink.h>
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +010029#include <linux/can/led.h>
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +053030#include <linux/of.h>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000031#include <net/rtnetlink.h>
32
33#define MOD_DESC "CAN device driver interface"
34
35MODULE_DESCRIPTION(MOD_DESC);
36MODULE_LICENSE("GPL v2");
37MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
38
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +020039/* CAN DLC to real data length conversion helpers */
40
41static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
42 8, 12, 16, 20, 24, 32, 48, 64};
43
44/* get data length from can_dlc with sanitized can_dlc */
45u8 can_dlc2len(u8 can_dlc)
46{
47 return dlc2len[can_dlc & 0x0F];
48}
49EXPORT_SYMBOL_GPL(can_dlc2len);
50
51static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
52 9, 9, 9, 9, /* 9 - 12 */
53 10, 10, 10, 10, /* 13 - 16 */
54 11, 11, 11, 11, /* 17 - 20 */
55 12, 12, 12, 12, /* 21 - 24 */
56 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
58 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
59 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
60 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
61
62/* map the sanitized data length to an appropriate data length code */
63u8 can_len2dlc(u8 len)
64{
65 if (unlikely(len > 64))
66 return 0xF;
67
68 return len2dlc[len];
69}
70EXPORT_SYMBOL_GPL(can_len2dlc);
71
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000072#ifdef CONFIG_CAN_CALC_BITTIMING
73#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +020074#define CAN_CALC_SYNC_SEG 1
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000075
76/*
77 * Bit-timing calculation derived from:
78 *
79 * Code based on LinCAN sources and H8S2638 project
80 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
81 * Copyright 2005 Stanislav Marek
82 * email: pisa@cmp.felk.cvut.cz
83 *
84 * Calculates proper bit-timing parameters for a specified bit-rate
85 * and sample-point, which can then be used to set the bit-timing
86 * registers of the CAN controller. You can find more information
87 * in the header file linux/can/netlink.h.
88 */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +020089static int can_update_sample_point(const struct can_bittiming_const *btc,
90 unsigned int sample_point_nominal, unsigned int tseg,
91 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
92 unsigned int *sample_point_error_ptr)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000093{
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +020094 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
95 unsigned int sample_point, best_sample_point = 0;
96 unsigned int tseg1, tseg2;
97 int i;
98
99 for (i = 0; i <= 1; i++) {
100 tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
101 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
102 tseg1 = tseg - tseg2;
103 if (tseg1 > btc->tseg1_max) {
104 tseg1 = btc->tseg1_max;
105 tseg2 = tseg - tseg1;
106 }
107
108 sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
109 sample_point_error = abs(sample_point_nominal - sample_point);
110
111 if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
112 best_sample_point = sample_point;
113 best_sample_point_error = sample_point_error;
114 *tseg1_ptr = tseg1;
115 *tseg2_ptr = tseg2;
116 }
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000117 }
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200118
119 if (sample_point_error_ptr)
120 *sample_point_error_ptr = best_sample_point_error;
121
122 return best_sample_point;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000123}
124
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100125static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
126 const struct can_bittiming_const *btc)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000127{
128 struct can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200129 unsigned int bitrate; /* current bitrate */
130 unsigned int bitrate_error; /* difference between current and nominal value */
131 unsigned int best_bitrate_error = UINT_MAX;
132 unsigned int sample_point_error; /* difference between current and nominal value */
133 unsigned int best_sample_point_error = UINT_MAX;
134 unsigned int sample_point_nominal; /* nominal sample point */
135 unsigned int best_tseg = 0; /* current best value for tseg */
136 unsigned int best_brp = 0; /* current best value for brp */
137 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000138 u64 v64;
139
Roman Fietze67b59092014-10-20 10:32:42 +0200140 /* Use CiA recommended sample points */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000141 if (bt->sample_point) {
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200142 sample_point_nominal = bt->sample_point;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000143 } else {
144 if (bt->bitrate > 800000)
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200145 sample_point_nominal = 750;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000146 else if (bt->bitrate > 500000)
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200147 sample_point_nominal = 800;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000148 else
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200149 sample_point_nominal = 875;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000150 }
151
152 /* tseg even = round down, odd = round up */
153 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
154 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200155 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
156
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000157 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
158 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200159
160 /* choose brp step which is possible in system */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000161 brp = (brp / btc->brp_inc) * btc->brp_inc;
162 if ((brp < btc->brp_min) || (brp > btc->brp_max))
163 continue;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200164
165 bitrate = priv->clock.freq / (brp * tsegall);
166 bitrate_error = abs(bt->bitrate - bitrate);
167
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000168 /* tseg brp biterror */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200169 if (bitrate_error > best_bitrate_error)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000170 continue;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200171
172 /* reset sample point error if we have a better bitrate */
173 if (bitrate_error < best_bitrate_error)
174 best_sample_point_error = UINT_MAX;
175
176 can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
177 if (sample_point_error > best_sample_point_error)
178 continue;
179
180 best_sample_point_error = sample_point_error;
181 best_bitrate_error = bitrate_error;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000182 best_tseg = tseg / 2;
183 best_brp = brp;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200184
185 if (bitrate_error == 0 && sample_point_error == 0)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000186 break;
187 }
188
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200189 if (best_bitrate_error) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000190 /* Error in one-tenth of a percent */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200191 v64 = (u64)best_bitrate_error * 1000;
192 do_div(v64, bt->bitrate);
193 bitrate_error = (u32)v64;
194 if (bitrate_error > CAN_CALC_MAX_ERROR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100195 netdev_err(dev,
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200196 "bitrate error %d.%d%% too high\n",
197 bitrate_error / 10, bitrate_error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000198 return -EDOM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000199 }
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200200 netdev_warn(dev, "bitrate error %d.%d%%\n",
201 bitrate_error / 10, bitrate_error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000202 }
203
204 /* real sample point */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200205 bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
206 &tseg1, &tseg2, NULL);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000207
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200208 v64 = (u64)best_brp * 1000 * 1000 * 1000;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000209 do_div(v64, priv->clock.freq);
210 bt->tq = (u32)v64;
211 bt->prop_seg = tseg1 / 2;
212 bt->phase_seg1 = tseg1 - bt->prop_seg;
213 bt->phase_seg2 = tseg2;
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000214
215 /* check for sjw user settings */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200216 if (!bt->sjw || !btc->sjw_max) {
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000217 bt->sjw = 1;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200218 } else {
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000219 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
220 if (bt->sjw > btc->sjw_max)
221 bt->sjw = btc->sjw_max;
222 /* bt->sjw must not be higher than tseg2 */
223 if (tseg2 < bt->sjw)
224 bt->sjw = tseg2;
225 }
226
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000227 bt->brp = best_brp;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200228
229 /* real bitrate */
230 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000231
232 return 0;
233}
234#else /* !CONFIG_CAN_CALC_BITTIMING */
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100235static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
236 const struct can_bittiming_const *btc)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000237{
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100238 netdev_err(dev, "bit-timing calculation not available\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000239 return -EINVAL;
240}
241#endif /* CONFIG_CAN_CALC_BITTIMING */
242
243/*
244 * Checks the validity of the specified bit-timing parameters prop_seg,
245 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
246 * prescaler value brp. You can find more information in the header
247 * file linux/can/netlink.h.
248 */
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100249static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
250 const struct can_bittiming_const *btc)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000251{
252 struct can_priv *priv = netdev_priv(dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000253 int tseg1, alltseg;
254 u64 brp64;
255
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000256 tseg1 = bt->prop_seg + bt->phase_seg1;
257 if (!bt->sjw)
258 bt->sjw = 1;
259 if (bt->sjw > btc->sjw_max ||
260 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
261 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
262 return -ERANGE;
263
264 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
265 if (btc->brp_inc > 1)
266 do_div(brp64, btc->brp_inc);
267 brp64 += 500000000UL - 1;
268 do_div(brp64, 1000000000UL); /* the practicable BRP */
269 if (btc->brp_inc > 1)
270 brp64 *= btc->brp_inc;
271 bt->brp = (u32)brp64;
272
273 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
274 return -EINVAL;
275
276 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
277 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
278 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
279
280 return 0;
281}
282
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100283/* Checks the validity of predefined bitrate settings */
284static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
285 const u32 *bitrate_const,
286 const unsigned int bitrate_const_cnt)
287{
288 struct can_priv *priv = netdev_priv(dev);
289 unsigned int i;
290
291 for (i = 0; i < bitrate_const_cnt; i++) {
292 if (bt->bitrate == bitrate_const[i])
293 break;
294 }
295
296 if (i >= priv->bitrate_const_cnt)
297 return -EINVAL;
298
299 return 0;
300}
301
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100302static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100303 const struct can_bittiming_const *btc,
304 const u32 *bitrate_const,
305 const unsigned int bitrate_const_cnt)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000306{
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000307 int err;
308
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100309 /*
310 * Depending on the given can_bittiming parameter structure the CAN
311 * timing parameters are calculated based on the provided bitrate OR
312 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
313 * provided directly which are then checked and fixed up.
314 */
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100315 if (!bt->tq && bt->bitrate && btc)
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100316 err = can_calc_bittiming(dev, bt, btc);
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100317 else if (bt->tq && !bt->bitrate && btc)
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100318 err = can_fixup_bittiming(dev, bt, btc);
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100319 else if (!bt->tq && bt->bitrate && bitrate_const)
320 err = can_validate_bitrate(dev, bt, bitrate_const,
321 bitrate_const_cnt);
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100322 else
323 err = -EINVAL;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000324
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100325 return err;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000326}
327
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000328static void can_update_state_error_stats(struct net_device *dev,
329 enum can_state new_state)
330{
331 struct can_priv *priv = netdev_priv(dev);
332
333 if (new_state <= priv->state)
334 return;
335
336 switch (new_state) {
337 case CAN_STATE_ERROR_WARNING:
338 priv->can_stats.error_warning++;
339 break;
340 case CAN_STATE_ERROR_PASSIVE:
341 priv->can_stats.error_passive++;
342 break;
343 case CAN_STATE_BUS_OFF:
Andri Yngvasonbe38a6f2015-01-16 14:30:28 +0000344 priv->can_stats.bus_off++;
345 break;
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000346 default:
347 break;
kbuild test robot5b5ba2a2015-01-16 16:52:21 +0800348 }
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000349}
350
351static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
352{
353 switch (state) {
354 case CAN_STATE_ERROR_ACTIVE:
355 return CAN_ERR_CRTL_ACTIVE;
356 case CAN_STATE_ERROR_WARNING:
357 return CAN_ERR_CRTL_TX_WARNING;
358 case CAN_STATE_ERROR_PASSIVE:
359 return CAN_ERR_CRTL_TX_PASSIVE;
360 default:
361 return 0;
362 }
363}
364
365static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
366{
367 switch (state) {
368 case CAN_STATE_ERROR_ACTIVE:
369 return CAN_ERR_CRTL_ACTIVE;
370 case CAN_STATE_ERROR_WARNING:
371 return CAN_ERR_CRTL_RX_WARNING;
372 case CAN_STATE_ERROR_PASSIVE:
373 return CAN_ERR_CRTL_RX_PASSIVE;
374 default:
375 return 0;
376 }
377}
378
379void can_change_state(struct net_device *dev, struct can_frame *cf,
380 enum can_state tx_state, enum can_state rx_state)
381{
382 struct can_priv *priv = netdev_priv(dev);
383 enum can_state new_state = max(tx_state, rx_state);
384
385 if (unlikely(new_state == priv->state)) {
386 netdev_warn(dev, "%s: oops, state did not change", __func__);
387 return;
388 }
389
390 netdev_dbg(dev, "New error state: %d\n", new_state);
391
392 can_update_state_error_stats(dev, new_state);
393 priv->state = new_state;
394
Marc Kleine-Buddeff3416f2017-05-18 10:22:22 +0200395 if (!cf)
396 return;
397
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000398 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
399 cf->can_id |= CAN_ERR_BUSOFF;
400 return;
401 }
402
403 cf->can_id |= CAN_ERR_CRTL;
404 cf->data[1] |= tx_state >= rx_state ?
405 can_tx_state_to_frame(dev, tx_state) : 0;
406 cf->data[1] |= tx_state <= rx_state ?
407 can_rx_state_to_frame(dev, rx_state) : 0;
408}
409EXPORT_SYMBOL_GPL(can_change_state);
410
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000411/*
412 * Local echo of CAN messages
413 *
414 * CAN network devices *should* support a local echo functionality
Robert Schwebel7d597732018-01-24 11:19:11 +0100415 * (see Documentation/networking/can.rst). To test the handling of CAN
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000416 * interfaces that do not support the local echo both driver types are
417 * implemented. In the case that the driver does not support the echo
418 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
419 * to perform the echo as a fallback solution.
420 */
421static void can_flush_echo_skb(struct net_device *dev)
422{
423 struct can_priv *priv = netdev_priv(dev);
424 struct net_device_stats *stats = &dev->stats;
425 int i;
426
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000427 for (i = 0; i < priv->echo_skb_max; i++) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000428 if (priv->echo_skb[i]) {
429 kfree_skb(priv->echo_skb[i]);
430 priv->echo_skb[i] = NULL;
431 stats->tx_dropped++;
432 stats->tx_aborted_errors++;
433 }
434 }
435}
436
437/*
438 * Put the skb on the stack to be looped backed locally lateron
439 *
440 * The function is typically called in the start_xmit function
441 * of the device driver. The driver must protect access to
442 * priv->echo_skb, if necessary.
443 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000444void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
445 unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000446{
447 struct can_priv *priv = netdev_priv(dev);
448
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000449 BUG_ON(idx >= priv->echo_skb_max);
450
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000451 /* check flag whether this packet has to be looped back */
Oliver Hartkoppa94bc9c2014-02-28 16:36:19 +0100452 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
453 (skb->protocol != htons(ETH_P_CAN) &&
454 skb->protocol != htons(ETH_P_CANFD))) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000455 kfree_skb(skb);
456 return;
457 }
458
459 if (!priv->echo_skb[idx]) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000460
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100461 skb = can_create_echo_skb(skb);
462 if (!skb)
463 return;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000464
465 /* make settings for echo to reduce code in irq context */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000466 skb->pkt_type = PACKET_BROADCAST;
467 skb->ip_summed = CHECKSUM_UNNECESSARY;
468 skb->dev = dev;
469
470 /* save this skb for tx interrupt echo handling */
471 priv->echo_skb[idx] = skb;
472 } else {
473 /* locking problem with netif_stop_queue() ?? */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100474 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000475 kfree_skb(skb);
476 }
477}
478EXPORT_SYMBOL_GPL(can_put_echo_skb);
479
Marc Kleine-Buddea4310fa2018-10-31 10:37:46 +0100480struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000481{
482 struct can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde7da11ba2018-10-31 14:15:13 +0100483 struct sk_buff *skb = priv->echo_skb[idx];
484 struct canfd_frame *cf;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000485
Marc Kleine-Buddee7a69942018-10-31 14:05:26 +0100486 if (idx >= priv->echo_skb_max) {
487 netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
488 __func__, idx, priv->echo_skb_max);
489 return NULL;
490 }
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000491
Marc Kleine-Budde7da11ba2018-10-31 14:15:13 +0100492 if (!skb) {
493 netdev_err(dev, "%s: BUG! Trying to echo non existing skb: can_priv::echo_skb[%u]\n",
494 __func__, idx);
495 return NULL;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000496 }
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200497
Marc Kleine-Budde7da11ba2018-10-31 14:15:13 +0100498 /* Using "struct canfd_frame::len" for the frame
499 * length is supported on both CAN and CANFD frames.
500 */
501 cf = (struct canfd_frame *)skb->data;
502 *len_ptr = cf->len;
503 priv->echo_skb[idx] = NULL;
504
505 return skb;
Marc Kleine-Buddea4310fa2018-10-31 10:37:46 +0100506}
507
508/*
509 * Get the skb from the stack and loop it back locally
510 *
511 * The function is typically called when the TX done interrupt
512 * is handled in the device driver. The driver must protect
513 * access to priv->echo_skb, if necessary.
514 */
515unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
516{
517 struct sk_buff *skb;
518 u8 len;
519
520 skb = __can_get_echo_skb(dev, idx, &len);
521 if (!skb)
522 return 0;
523
524 netif_rx(skb);
525
526 return len;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000527}
528EXPORT_SYMBOL_GPL(can_get_echo_skb);
529
530/*
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000531 * Remove the skb from the stack and free it.
532 *
533 * The function is typically called when TX failed.
534 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000535void can_free_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000536{
537 struct can_priv *priv = netdev_priv(dev);
538
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000539 BUG_ON(idx >= priv->echo_skb_max);
540
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000541 if (priv->echo_skb[idx]) {
Thomas Körper5247a582014-10-31 07:33:54 +0100542 dev_kfree_skb_any(priv->echo_skb[idx]);
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000543 priv->echo_skb[idx] = NULL;
544 }
545}
546EXPORT_SYMBOL_GPL(can_free_echo_skb);
547
548/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000549 * CAN device restart for bus-off recovery
550 */
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300551static void can_restart(struct net_device *dev)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000552{
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000553 struct can_priv *priv = netdev_priv(dev);
554 struct net_device_stats *stats = &dev->stats;
555 struct sk_buff *skb;
556 struct can_frame *cf;
557 int err;
558
559 BUG_ON(netif_carrier_ok(dev));
560
561 /*
562 * No synchronization needed because the device is bus-off and
563 * no messages can come in or go out.
564 */
565 can_flush_echo_skb(dev);
566
567 /* send restart message upstream */
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700568 skb = alloc_can_err_skb(dev, &cf);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000569 if (skb == NULL) {
570 err = -ENOMEM;
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000571 goto restart;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000572 }
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700573 cf->can_id |= CAN_ERR_RESTARTED;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000574
575 netif_rx(skb);
576
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000577 stats->rx_packets++;
578 stats->rx_bytes += cf->can_dlc;
579
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000580restart:
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100581 netdev_dbg(dev, "restarted\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000582 priv->can_stats.restarts++;
583
584 /* Now restart the device */
585 err = priv->do_set_mode(dev, CAN_MODE_START);
586
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000587 netif_carrier_on(dev);
588 if (err)
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100589 netdev_err(dev, "Error %d during restart", err);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000590}
591
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300592static void can_restart_work(struct work_struct *work)
593{
594 struct delayed_work *dwork = to_delayed_work(work);
595 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
596
597 can_restart(priv->dev);
598}
599
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000600int can_restart_now(struct net_device *dev)
601{
602 struct can_priv *priv = netdev_priv(dev);
603
604 /*
605 * A manual restart is only permitted if automatic restart is
606 * disabled and the device is in the bus-off state
607 */
608 if (priv->restart_ms)
609 return -EINVAL;
610 if (priv->state != CAN_STATE_BUS_OFF)
611 return -EBUSY;
612
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300613 cancel_delayed_work_sync(&priv->restart_work);
614 can_restart(dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000615
616 return 0;
617}
618
619/*
620 * CAN bus-off
621 *
622 * This functions should be called when the device goes bus-off to
623 * tell the netif layer that no more packets can be sent or received.
624 * If enabled, a timer is started to trigger bus-off recovery.
625 */
626void can_bus_off(struct net_device *dev)
627{
628 struct can_priv *priv = netdev_priv(dev);
629
Jakob Unterwurzacher71c23a82018-04-18 16:10:03 +0200630 netdev_info(dev, "bus-off\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000631
632 netif_carrier_off(dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000633
634 if (priv->restart_ms)
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300635 schedule_delayed_work(&priv->restart_work,
636 msecs_to_jiffies(priv->restart_ms));
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000637}
638EXPORT_SYMBOL_GPL(can_bus_off);
639
640static void can_setup(struct net_device *dev)
641{
642 dev->type = ARPHRD_CAN;
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200643 dev->mtu = CAN_MTU;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000644 dev->hard_header_len = 0;
645 dev->addr_len = 0;
646 dev->tx_queue_len = 10;
647
648 /* New-style flags. */
649 dev->flags = IFF_NOARP;
Michał Mirosław34324dc2011-11-15 15:29:55 +0000650 dev->features = NETIF_F_HW_CSUM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000651}
652
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700653struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
654{
655 struct sk_buff *skb;
656
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100657 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
658 sizeof(struct can_frame));
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700659 if (unlikely(!skb))
660 return NULL;
661
662 skb->protocol = htons(ETH_P_CAN);
663 skb->pkt_type = PACKET_BROADCAST;
664 skb->ip_summed = CHECKSUM_UNNECESSARY;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100665
Oliver Hartkopp9694390162015-02-23 20:37:54 +0100666 skb_reset_mac_header(skb);
667 skb_reset_network_header(skb);
668 skb_reset_transport_header(skb);
669
Oliver Hartkopp2bf34402013-01-28 08:33:33 +0000670 can_skb_reserve(skb);
671 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200672 can_skb_prv(skb)->skbcnt = 0;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100673
YueHaibing8551e712018-04-28 15:49:57 +0800674 *cf = skb_put_zero(skb, sizeof(struct can_frame));
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700675
676 return skb;
677}
678EXPORT_SYMBOL_GPL(alloc_can_skb);
679
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100680struct sk_buff *alloc_canfd_skb(struct net_device *dev,
681 struct canfd_frame **cfd)
682{
683 struct sk_buff *skb;
684
685 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
686 sizeof(struct canfd_frame));
687 if (unlikely(!skb))
688 return NULL;
689
690 skb->protocol = htons(ETH_P_CANFD);
691 skb->pkt_type = PACKET_BROADCAST;
692 skb->ip_summed = CHECKSUM_UNNECESSARY;
693
Oliver Hartkopp9694390162015-02-23 20:37:54 +0100694 skb_reset_mac_header(skb);
695 skb_reset_network_header(skb);
696 skb_reset_transport_header(skb);
697
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100698 can_skb_reserve(skb);
699 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200700 can_skb_prv(skb)->skbcnt = 0;
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100701
YueHaibing8551e712018-04-28 15:49:57 +0800702 *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100703
704 return skb;
705}
706EXPORT_SYMBOL_GPL(alloc_canfd_skb);
707
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700708struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
709{
710 struct sk_buff *skb;
711
712 skb = alloc_can_skb(dev, cf);
713 if (unlikely(!skb))
714 return NULL;
715
716 (*cf)->can_id = CAN_ERR_FLAG;
717 (*cf)->can_dlc = CAN_ERR_DLC;
718
719 return skb;
720}
721EXPORT_SYMBOL_GPL(alloc_can_err_skb);
722
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000723/*
724 * Allocate and setup space for the CAN network device
725 */
Zhu Yi03870902018-06-13 16:37:17 +0200726struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
727 unsigned int txqs, unsigned int rxqs)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000728{
729 struct net_device *dev;
730 struct can_priv *priv;
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000731 int size;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000732
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000733 if (echo_skb_max)
734 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
735 echo_skb_max * sizeof(struct sk_buff *);
736 else
737 size = sizeof_priv;
738
Zhu Yi03870902018-06-13 16:37:17 +0200739 dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
740 txqs, rxqs);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000741 if (!dev)
742 return NULL;
743
744 priv = netdev_priv(dev);
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300745 priv->dev = dev;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000746
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000747 if (echo_skb_max) {
748 priv->echo_skb_max = echo_skb_max;
749 priv->echo_skb = (void *)priv +
750 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
751 }
752
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000753 priv->state = CAN_STATE_STOPPED;
754
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300755 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000756
757 return dev;
758}
Zhu Yi03870902018-06-13 16:37:17 +0200759EXPORT_SYMBOL_GPL(alloc_candev_mqs);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000760
761/*
762 * Free space of the CAN network device
763 */
764void free_candev(struct net_device *dev)
765{
766 free_netdev(dev);
767}
768EXPORT_SYMBOL_GPL(free_candev);
769
770/*
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100771 * changing MTU and control mode for CAN/CANFD devices
772 */
773int can_change_mtu(struct net_device *dev, int new_mtu)
774{
775 struct can_priv *priv = netdev_priv(dev);
776
777 /* Do not allow changing the MTU while running */
778 if (dev->flags & IFF_UP)
779 return -EBUSY;
780
781 /* allow change of MTU according to the CANFD ability of the device */
782 switch (new_mtu) {
783 case CAN_MTU:
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100784 /* 'CANFD-only' controllers can not switch to CAN_MTU */
785 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
786 return -EINVAL;
787
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100788 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
789 break;
790
791 case CANFD_MTU:
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100792 /* check for potential CANFD ability */
793 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
794 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100795 return -EINVAL;
796
797 priv->ctrlmode |= CAN_CTRLMODE_FD;
798 break;
799
800 default:
801 return -EINVAL;
802 }
803
804 dev->mtu = new_mtu;
805 return 0;
806}
807EXPORT_SYMBOL_GPL(can_change_mtu);
808
809/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000810 * Common open function when the device gets opened.
811 *
812 * This function should be called in the open function of the device
813 * driver.
814 */
815int open_candev(struct net_device *dev)
816{
817 struct can_priv *priv = netdev_priv(dev);
818
Oliver Hartkoppb30749f2014-02-28 16:36:20 +0100819 if (!priv->bittiming.bitrate) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100820 netdev_err(dev, "bit-timing not yet defined\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000821 return -EINVAL;
822 }
823
Oliver Hartkoppdd225862014-02-28 16:36:25 +0100824 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
825 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
826 (!priv->data_bittiming.bitrate ||
827 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
828 netdev_err(dev, "incorrect/missing data bit-timing\n");
829 return -EINVAL;
830 }
831
Wolfgang Grandegger1b0d9222009-07-20 04:06:41 +0000832 /* Switch carrier on if device was stopped while in bus-off state */
833 if (!netif_carrier_ok(dev))
834 netif_carrier_on(dev);
835
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000836 return 0;
837}
Wolfgang Grandegger128ced82009-05-30 07:55:48 +0000838EXPORT_SYMBOL_GPL(open_candev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000839
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +0530840#ifdef CONFIG_OF
841/* Common function that can be used to understand the limitation of
842 * a transceiver when it provides no means to determine these limitations
843 * at runtime.
844 */
845void of_can_transceiver(struct net_device *dev)
846{
847 struct device_node *dn;
848 struct can_priv *priv = netdev_priv(dev);
849 struct device_node *np = dev->dev.parent->of_node;
850 int ret;
851
852 dn = of_get_child_by_name(np, "can-transceiver");
853 if (!dn)
854 return;
855
856 ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
857 if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
858 netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
859}
860EXPORT_SYMBOL_GPL(of_can_transceiver);
861#endif
862
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000863/*
864 * Common close function for cleanup before the device gets closed.
865 *
866 * This function should be called in the close function of the device
867 * driver.
868 */
869void close_candev(struct net_device *dev)
870{
871 struct can_priv *priv = netdev_priv(dev);
872
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300873 cancel_delayed_work_sync(&priv->restart_work);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000874 can_flush_echo_skb(dev);
875}
876EXPORT_SYMBOL_GPL(close_candev);
877
878/*
879 * CAN netlink interface
880 */
881static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
882 [IFLA_CAN_STATE] = { .type = NLA_U32 },
883 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
884 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
885 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
886 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
887 [IFLA_CAN_BITTIMING_CONST]
888 = { .len = sizeof(struct can_bittiming_const) },
889 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000890 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +0100891 [IFLA_CAN_DATA_BITTIMING]
892 = { .len = sizeof(struct can_bittiming) },
893 [IFLA_CAN_DATA_BITTIMING_CONST]
894 = { .len = sizeof(struct can_bittiming_const) },
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000895};
896
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200897static int can_validate(struct nlattr *tb[], struct nlattr *data[],
898 struct netlink_ext_ack *extack)
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100899{
900 bool is_can_fd = false;
901
902 /* Make sure that valid CAN FD configurations always consist of
903 * - nominal/arbitration bittiming
904 * - data bittiming
905 * - control mode with CAN_CTRLMODE_FD set
906 */
907
Oliver Hartkoppbce271f2016-06-21 12:14:07 +0200908 if (!data)
909 return 0;
910
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100911 if (data[IFLA_CAN_CTRLMODE]) {
912 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
913
914 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
915 }
916
917 if (is_can_fd) {
918 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
919 return -EOPNOTSUPP;
920 }
921
922 if (data[IFLA_CAN_DATA_BITTIMING]) {
923 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
924 return -EOPNOTSUPP;
925 }
926
927 return 0;
928}
929
Matthias Schifferad744b22017-06-25 23:56:00 +0200930static int can_changelink(struct net_device *dev, struct nlattr *tb[],
931 struct nlattr *data[],
932 struct netlink_ext_ack *extack)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000933{
934 struct can_priv *priv = netdev_priv(dev);
935 int err;
936
937 /* We need synchronization with dev->stop() */
938 ASSERT_RTNL();
939
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000940 if (data[IFLA_CAN_BITTIMING]) {
941 struct can_bittiming bt;
942
943 /* Do not allow changing bittiming while running */
944 if (dev->flags & IFF_UP)
945 return -EBUSY;
Marc Kleine-Buddec3606d42017-01-06 11:12:41 +0100946
947 /* Calculate bittiming parameters based on
948 * bittiming_const if set, otherwise pass bitrate
949 * directly via do_set_bitrate(). Bail out if neither
950 * is given.
951 */
952 if (!priv->bittiming_const && !priv->do_set_bittiming)
953 return -EOPNOTSUPP;
954
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000955 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100956 err = can_get_bittiming(dev, &bt,
957 priv->bittiming_const,
958 priv->bitrate_const,
959 priv->bitrate_const_cnt);
960 if (err)
961 return err;
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +0530962
963 if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
964 netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
965 priv->bitrate_max);
966 return -EINVAL;
967 }
968
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000969 memcpy(&priv->bittiming, &bt, sizeof(bt));
970
971 if (priv->do_set_bittiming) {
972 /* Finally, set the bit-timing registers */
973 err = priv->do_set_bittiming(dev);
974 if (err)
975 return err;
976 }
977 }
978
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200979 if (data[IFLA_CAN_CTRLMODE]) {
980 struct can_ctrlmode *cm;
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100981 u32 ctrlstatic;
982 u32 maskedflags;
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200983
984 /* Do not allow changing controller mode while running */
985 if (dev->flags & IFF_UP)
986 return -EBUSY;
987 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100988 ctrlstatic = priv->ctrlmode_static;
989 maskedflags = cm->flags & cm->mask;
Oliver Hartkopp9b1087a2015-01-05 18:40:15 +0100990
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100991 /* check whether provided bits are allowed to be passed */
992 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
993 return -EOPNOTSUPP;
994
995 /* do not check for static fd-non-iso if 'fd' is disabled */
996 if (!(maskedflags & CAN_CTRLMODE_FD))
997 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
998
999 /* make sure static options are provided by configuration */
1000 if ((maskedflags & ctrlstatic) != ctrlstatic)
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +02001001 return -EOPNOTSUPP;
Oliver Hartkopp9b1087a2015-01-05 18:40:15 +01001002
1003 /* clear bits to be modified and copy the flag values */
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +02001004 priv->ctrlmode &= ~cm->mask;
Oliver Hartkoppbb208f12016-03-21 20:18:21 +01001005 priv->ctrlmode |= maskedflags;
Oliver Hartkoppbc05a892014-02-28 16:36:24 +01001006
1007 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
1008 if (priv->ctrlmode & CAN_CTRLMODE_FD)
1009 dev->mtu = CANFD_MTU;
1010 else
1011 dev->mtu = CAN_MTU;
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +02001012 }
1013
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001014 if (data[IFLA_CAN_RESTART_MS]) {
1015 /* Do not allow changing restart delay while running */
1016 if (dev->flags & IFF_UP)
1017 return -EBUSY;
1018 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
1019 }
1020
1021 if (data[IFLA_CAN_RESTART]) {
1022 /* Do not allow a restart while not running */
1023 if (!(dev->flags & IFF_UP))
1024 return -EINVAL;
1025 err = can_restart_now(dev);
1026 if (err)
1027 return err;
1028 }
1029
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001030 if (data[IFLA_CAN_DATA_BITTIMING]) {
1031 struct can_bittiming dbt;
1032
1033 /* Do not allow changing bittiming while running */
1034 if (dev->flags & IFF_UP)
1035 return -EBUSY;
Marc Kleine-Buddec3606d42017-01-06 11:12:41 +01001036
1037 /* Calculate bittiming parameters based on
1038 * data_bittiming_const if set, otherwise pass bitrate
1039 * directly via do_set_bitrate(). Bail out if neither
1040 * is given.
1041 */
1042 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
1043 return -EOPNOTSUPP;
1044
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001045 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
1046 sizeof(dbt));
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001047 err = can_get_bittiming(dev, &dbt,
1048 priv->data_bittiming_const,
1049 priv->data_bitrate_const,
1050 priv->data_bitrate_const_cnt);
1051 if (err)
1052 return err;
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +05301053
1054 if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
1055 netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
1056 priv->bitrate_max);
1057 return -EINVAL;
1058 }
1059
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001060 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
1061
1062 if (priv->do_set_data_bittiming) {
1063 /* Finally, set the bit-timing registers */
1064 err = priv->do_set_data_bittiming(dev);
1065 if (err)
1066 return err;
1067 }
1068 }
1069
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001070 if (data[IFLA_CAN_TERMINATION]) {
1071 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
1072 const unsigned int num_term = priv->termination_const_cnt;
1073 unsigned int i;
1074
1075 if (!priv->do_set_termination)
1076 return -EOPNOTSUPP;
1077
1078 /* check whether given value is supported by the interface */
1079 for (i = 0; i < num_term; i++) {
1080 if (termval == priv->termination_const[i])
1081 break;
1082 }
1083 if (i >= num_term)
1084 return -EINVAL;
1085
1086 /* Finally, set the termination value */
1087 err = priv->do_set_termination(dev, termval);
1088 if (err)
1089 return err;
1090
1091 priv->termination = termval;
1092 }
1093
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001094 return 0;
1095}
1096
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001097static size_t can_get_size(const struct net_device *dev)
1098{
1099 struct can_priv *priv = netdev_priv(dev);
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +02001100 size_t size = 0;
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001101
Oliver Hartkoppb30749f2014-02-28 16:36:20 +01001102 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
1103 size += nla_total_size(sizeof(struct can_bittiming));
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +02001104 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
Marc Kleine-Buddefe119a02013-10-05 21:25:17 +02001105 size += nla_total_size(sizeof(struct can_bittiming_const));
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +02001106 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
1107 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
1108 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
1109 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
1110 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
1111 size += nla_total_size(sizeof(struct can_berr_counter));
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001112 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
1113 size += nla_total_size(sizeof(struct can_bittiming));
1114 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
1115 size += nla_total_size(sizeof(struct can_bittiming_const));
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001116 if (priv->termination_const) {
1117 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
1118 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
1119 priv->termination_const_cnt);
1120 }
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001121 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
1122 size += nla_total_size(sizeof(*priv->bitrate_const) *
1123 priv->bitrate_const_cnt);
1124 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
1125 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
1126 priv->data_bitrate_const_cnt);
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +05301127 size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001128
1129 return size;
1130}
1131
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001132static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
1133{
1134 struct can_priv *priv = netdev_priv(dev);
1135 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +00001136 struct can_berr_counter bec;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001137 enum can_state state = priv->state;
1138
1139 if (priv->do_get_state)
1140 priv->do_get_state(dev, &state);
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001141
Oliver Hartkoppb30749f2014-02-28 16:36:20 +01001142 if ((priv->bittiming.bitrate &&
1143 nla_put(skb, IFLA_CAN_BITTIMING,
1144 sizeof(priv->bittiming), &priv->bittiming)) ||
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001145
David S. Miller31e0e322012-04-01 20:21:11 -04001146 (priv->bittiming_const &&
1147 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001148 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001149
Marek Vasut562b1032015-10-30 13:48:19 +01001150 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001151 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
1152 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
1153 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001154
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001155 (priv->do_get_berr_counter &&
1156 !priv->do_get_berr_counter(dev, &bec) &&
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001157 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
1158
1159 (priv->data_bittiming.bitrate &&
1160 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
1161 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
1162
1163 (priv->data_bittiming_const &&
1164 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
1165 sizeof(*priv->data_bittiming_const),
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001166 priv->data_bittiming_const)) ||
1167
1168 (priv->termination_const &&
1169 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
1170 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
1171 sizeof(*priv->termination_const) *
1172 priv->termination_const_cnt,
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001173 priv->termination_const))) ||
1174
1175 (priv->bitrate_const &&
1176 nla_put(skb, IFLA_CAN_BITRATE_CONST,
1177 sizeof(*priv->bitrate_const) *
1178 priv->bitrate_const_cnt,
1179 priv->bitrate_const)) ||
1180
1181 (priv->data_bitrate_const &&
1182 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
1183 sizeof(*priv->data_bitrate_const) *
1184 priv->data_bitrate_const_cnt,
Franklin S Cooper Jr2290aef2018-01-10 16:25:18 +05301185 priv->data_bitrate_const)) ||
1186
1187 (nla_put(skb, IFLA_CAN_BITRATE_MAX,
1188 sizeof(priv->bitrate_max),
1189 &priv->bitrate_max))
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001190 )
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001191
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001192 return -EMSGSIZE;
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001193
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001194 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001195}
1196
Wolfgang Grandegger55369c02009-11-12 05:34:05 +00001197static size_t can_get_xstats_size(const struct net_device *dev)
1198{
1199 return sizeof(struct can_device_stats);
1200}
1201
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001202static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1203{
1204 struct can_priv *priv = netdev_priv(dev);
1205
David S. Miller31e0e322012-04-01 20:21:11 -04001206 if (nla_put(skb, IFLA_INFO_XSTATS,
1207 sizeof(priv->can_stats), &priv->can_stats))
1208 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001209 return 0;
1210
1211nla_put_failure:
1212 return -EMSGSIZE;
1213}
1214
Eric W. Biederman81adee42009-11-08 00:53:51 -08001215static int can_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001216 struct nlattr *tb[], struct nlattr *data[],
1217 struct netlink_ext_ack *extack)
Oliver Hartkopp993e6f22009-08-11 02:41:24 +00001218{
1219 return -EOPNOTSUPP;
1220}
1221
Oliver Hartkopp25e1ed62016-06-21 15:45:47 +02001222static void can_dellink(struct net_device *dev, struct list_head *head)
1223{
1224 return;
1225}
1226
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001227static struct rtnl_link_ops can_link_ops __read_mostly = {
1228 .kind = "can",
1229 .maxtype = IFLA_CAN_MAX,
1230 .policy = can_policy,
1231 .setup = can_setup,
Oliver Hartkoppbb208f12016-03-21 20:18:21 +01001232 .validate = can_validate,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +00001233 .newlink = can_newlink,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001234 .changelink = can_changelink,
Oliver Hartkopp25e1ed62016-06-21 15:45:47 +02001235 .dellink = can_dellink,
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001236 .get_size = can_get_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001237 .fill_info = can_fill_info,
Wolfgang Grandegger55369c02009-11-12 05:34:05 +00001238 .get_xstats_size = can_get_xstats_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001239 .fill_xstats = can_fill_xstats,
1240};
1241
1242/*
1243 * Register the CAN network device
1244 */
1245int register_candev(struct net_device *dev)
1246{
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001247 struct can_priv *priv = netdev_priv(dev);
1248
1249 /* Ensure termination_const, termination_const_cnt and
1250 * do_set_termination consistency. All must be either set or
1251 * unset.
1252 */
1253 if ((!priv->termination_const != !priv->termination_const_cnt) ||
1254 (!priv->termination_const != !priv->do_set_termination))
1255 return -EINVAL;
1256
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001257 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
1258 return -EINVAL;
1259
1260 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
1261 return -EINVAL;
1262
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001263 dev->rtnl_link_ops = &can_link_ops;
1264 return register_netdev(dev);
1265}
1266EXPORT_SYMBOL_GPL(register_candev);
1267
1268/*
1269 * Unregister the CAN network device
1270 */
1271void unregister_candev(struct net_device *dev)
1272{
1273 unregister_netdev(dev);
1274}
1275EXPORT_SYMBOL_GPL(unregister_candev);
1276
Kurt Van Dijckbf03a532012-12-18 18:50:56 +01001277/*
1278 * Test if a network device is a candev based device
1279 * and return the can_priv* if so.
1280 */
1281struct can_priv *safe_candev_priv(struct net_device *dev)
1282{
1283 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1284 return NULL;
1285
1286 return netdev_priv(dev);
1287}
1288EXPORT_SYMBOL_GPL(safe_candev_priv);
1289
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001290static __init int can_dev_init(void)
1291{
1292 int err;
1293
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +01001294 can_led_notifier_init();
1295
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001296 err = rtnl_link_register(&can_link_ops);
1297 if (!err)
1298 printk(KERN_INFO MOD_DESC "\n");
1299
1300 return err;
1301}
1302module_init(can_dev_init);
1303
1304static __exit void can_dev_exit(void)
1305{
1306 rtnl_link_unregister(&can_link_ops);
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +01001307
1308 can_led_notifier_exit();
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001309}
1310module_exit(can_dev_exit);
1311
1312MODULE_ALIAS_RTNL_LINK("can");