blob: 365a8cc6240506ed165bc9141daf15ab29018541 [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>
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000030#include <net/rtnetlink.h>
31
32#define MOD_DESC "CAN device driver interface"
33
34MODULE_DESCRIPTION(MOD_DESC);
35MODULE_LICENSE("GPL v2");
36MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
37
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +020038/* CAN DLC to real data length conversion helpers */
39
40static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
41 8, 12, 16, 20, 24, 32, 48, 64};
42
43/* get data length from can_dlc with sanitized can_dlc */
44u8 can_dlc2len(u8 can_dlc)
45{
46 return dlc2len[can_dlc & 0x0F];
47}
48EXPORT_SYMBOL_GPL(can_dlc2len);
49
50static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
51 9, 9, 9, 9, /* 9 - 12 */
52 10, 10, 10, 10, /* 13 - 16 */
53 11, 11, 11, 11, /* 17 - 20 */
54 12, 12, 12, 12, /* 21 - 24 */
55 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
56 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
57 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
58 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
59 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
60
61/* map the sanitized data length to an appropriate data length code */
62u8 can_len2dlc(u8 len)
63{
64 if (unlikely(len > 64))
65 return 0xF;
66
67 return len2dlc[len];
68}
69EXPORT_SYMBOL_GPL(can_len2dlc);
70
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000071#ifdef CONFIG_CAN_CALC_BITTIMING
72#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +020073#define CAN_CALC_SYNC_SEG 1
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000074
75/*
76 * Bit-timing calculation derived from:
77 *
78 * Code based on LinCAN sources and H8S2638 project
79 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
80 * Copyright 2005 Stanislav Marek
81 * email: pisa@cmp.felk.cvut.cz
82 *
83 * Calculates proper bit-timing parameters for a specified bit-rate
84 * and sample-point, which can then be used to set the bit-timing
85 * registers of the CAN controller. You can find more information
86 * in the header file linux/can/netlink.h.
87 */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +020088static int can_update_sample_point(const struct can_bittiming_const *btc,
89 unsigned int sample_point_nominal, unsigned int tseg,
90 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
91 unsigned int *sample_point_error_ptr)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +000092{
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +020093 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
94 unsigned int sample_point, best_sample_point = 0;
95 unsigned int tseg1, tseg2;
96 int i;
97
98 for (i = 0; i <= 1; i++) {
99 tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
100 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
101 tseg1 = tseg - tseg2;
102 if (tseg1 > btc->tseg1_max) {
103 tseg1 = btc->tseg1_max;
104 tseg2 = tseg - tseg1;
105 }
106
107 sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
108 sample_point_error = abs(sample_point_nominal - sample_point);
109
110 if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
111 best_sample_point = sample_point;
112 best_sample_point_error = sample_point_error;
113 *tseg1_ptr = tseg1;
114 *tseg2_ptr = tseg2;
115 }
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000116 }
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200117
118 if (sample_point_error_ptr)
119 *sample_point_error_ptr = best_sample_point_error;
120
121 return best_sample_point;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000122}
123
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100124static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
125 const struct can_bittiming_const *btc)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000126{
127 struct can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200128 unsigned int bitrate; /* current bitrate */
129 unsigned int bitrate_error; /* difference between current and nominal value */
130 unsigned int best_bitrate_error = UINT_MAX;
131 unsigned int sample_point_error; /* difference between current and nominal value */
132 unsigned int best_sample_point_error = UINT_MAX;
133 unsigned int sample_point_nominal; /* nominal sample point */
134 unsigned int best_tseg = 0; /* current best value for tseg */
135 unsigned int best_brp = 0; /* current best value for brp */
136 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000137 u64 v64;
138
Roman Fietze67b59092014-10-20 10:32:42 +0200139 /* Use CiA recommended sample points */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000140 if (bt->sample_point) {
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200141 sample_point_nominal = bt->sample_point;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000142 } else {
143 if (bt->bitrate > 800000)
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200144 sample_point_nominal = 750;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000145 else if (bt->bitrate > 500000)
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200146 sample_point_nominal = 800;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000147 else
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200148 sample_point_nominal = 875;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000149 }
150
151 /* tseg even = round down, odd = round up */
152 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
153 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200154 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
155
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000156 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
157 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200158
159 /* choose brp step which is possible in system */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000160 brp = (brp / btc->brp_inc) * btc->brp_inc;
161 if ((brp < btc->brp_min) || (brp > btc->brp_max))
162 continue;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200163
164 bitrate = priv->clock.freq / (brp * tsegall);
165 bitrate_error = abs(bt->bitrate - bitrate);
166
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000167 /* tseg brp biterror */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200168 if (bitrate_error > best_bitrate_error)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000169 continue;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200170
171 /* reset sample point error if we have a better bitrate */
172 if (bitrate_error < best_bitrate_error)
173 best_sample_point_error = UINT_MAX;
174
175 can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
176 if (sample_point_error > best_sample_point_error)
177 continue;
178
179 best_sample_point_error = sample_point_error;
180 best_bitrate_error = bitrate_error;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000181 best_tseg = tseg / 2;
182 best_brp = brp;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200183
184 if (bitrate_error == 0 && sample_point_error == 0)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000185 break;
186 }
187
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200188 if (best_bitrate_error) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000189 /* Error in one-tenth of a percent */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200190 v64 = (u64)best_bitrate_error * 1000;
191 do_div(v64, bt->bitrate);
192 bitrate_error = (u32)v64;
193 if (bitrate_error > CAN_CALC_MAX_ERROR) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100194 netdev_err(dev,
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200195 "bitrate error %d.%d%% too high\n",
196 bitrate_error / 10, bitrate_error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000197 return -EDOM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000198 }
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200199 netdev_warn(dev, "bitrate error %d.%d%%\n",
200 bitrate_error / 10, bitrate_error % 10);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000201 }
202
203 /* real sample point */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200204 bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
205 &tseg1, &tseg2, NULL);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000206
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200207 v64 = (u64)best_brp * 1000 * 1000 * 1000;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000208 do_div(v64, priv->clock.freq);
209 bt->tq = (u32)v64;
210 bt->prop_seg = tseg1 / 2;
211 bt->phase_seg1 = tseg1 - bt->prop_seg;
212 bt->phase_seg2 = tseg2;
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000213
214 /* check for sjw user settings */
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200215 if (!bt->sjw || !btc->sjw_max) {
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000216 bt->sjw = 1;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200217 } else {
Oliver Hartkopp2e114372011-09-28 02:50:11 +0000218 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
219 if (bt->sjw > btc->sjw_max)
220 bt->sjw = btc->sjw_max;
221 /* bt->sjw must not be higher than tseg2 */
222 if (tseg2 < bt->sjw)
223 bt->sjw = tseg2;
224 }
225
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000226 bt->brp = best_brp;
Marc Kleine-Budde7da29f92016-05-09 14:13:06 +0200227
228 /* real bitrate */
229 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000230
231 return 0;
232}
233#else /* !CONFIG_CAN_CALC_BITTIMING */
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100234static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
235 const struct can_bittiming_const *btc)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000236{
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100237 netdev_err(dev, "bit-timing calculation not available\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000238 return -EINVAL;
239}
240#endif /* CONFIG_CAN_CALC_BITTIMING */
241
242/*
243 * Checks the validity of the specified bit-timing parameters prop_seg,
244 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
245 * prescaler value brp. You can find more information in the header
246 * file linux/can/netlink.h.
247 */
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100248static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
249 const struct can_bittiming_const *btc)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000250{
251 struct can_priv *priv = netdev_priv(dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000252 int tseg1, alltseg;
253 u64 brp64;
254
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000255 tseg1 = bt->prop_seg + bt->phase_seg1;
256 if (!bt->sjw)
257 bt->sjw = 1;
258 if (bt->sjw > btc->sjw_max ||
259 tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
260 bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
261 return -ERANGE;
262
263 brp64 = (u64)priv->clock.freq * (u64)bt->tq;
264 if (btc->brp_inc > 1)
265 do_div(brp64, btc->brp_inc);
266 brp64 += 500000000UL - 1;
267 do_div(brp64, 1000000000UL); /* the practicable BRP */
268 if (btc->brp_inc > 1)
269 brp64 *= btc->brp_inc;
270 bt->brp = (u32)brp64;
271
272 if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
273 return -EINVAL;
274
275 alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
276 bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
277 bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
278
279 return 0;
280}
281
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100282/* Checks the validity of predefined bitrate settings */
283static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
284 const u32 *bitrate_const,
285 const unsigned int bitrate_const_cnt)
286{
287 struct can_priv *priv = netdev_priv(dev);
288 unsigned int i;
289
290 for (i = 0; i < bitrate_const_cnt; i++) {
291 if (bt->bitrate == bitrate_const[i])
292 break;
293 }
294
295 if (i >= priv->bitrate_const_cnt)
296 return -EINVAL;
297
298 return 0;
299}
300
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100301static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100302 const struct can_bittiming_const *btc,
303 const u32 *bitrate_const,
304 const unsigned int bitrate_const_cnt)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000305{
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000306 int err;
307
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100308 /*
309 * Depending on the given can_bittiming parameter structure the CAN
310 * timing parameters are calculated based on the provided bitrate OR
311 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
312 * provided directly which are then checked and fixed up.
313 */
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100314 if (!bt->tq && bt->bitrate && btc)
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100315 err = can_calc_bittiming(dev, bt, btc);
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100316 else if (bt->tq && !bt->bitrate && btc)
Oliver Hartkopp08da7da2014-02-28 16:36:22 +0100317 err = can_fixup_bittiming(dev, bt, btc);
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100318 else if (!bt->tq && bt->bitrate && bitrate_const)
319 err = can_validate_bitrate(dev, bt, bitrate_const,
320 bitrate_const_cnt);
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100321 else
322 err = -EINVAL;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000323
Oliver Hartkoppd5298df2014-02-28 16:36:21 +0100324 return err;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000325}
326
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000327static void can_update_state_error_stats(struct net_device *dev,
328 enum can_state new_state)
329{
330 struct can_priv *priv = netdev_priv(dev);
331
332 if (new_state <= priv->state)
333 return;
334
335 switch (new_state) {
336 case CAN_STATE_ERROR_WARNING:
337 priv->can_stats.error_warning++;
338 break;
339 case CAN_STATE_ERROR_PASSIVE:
340 priv->can_stats.error_passive++;
341 break;
342 case CAN_STATE_BUS_OFF:
Andri Yngvasonbe38a6f2015-01-16 14:30:28 +0000343 priv->can_stats.bus_off++;
344 break;
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000345 default:
346 break;
kbuild test robot5b5ba2a2015-01-16 16:52:21 +0800347 }
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000348}
349
350static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
351{
352 switch (state) {
353 case CAN_STATE_ERROR_ACTIVE:
354 return CAN_ERR_CRTL_ACTIVE;
355 case CAN_STATE_ERROR_WARNING:
356 return CAN_ERR_CRTL_TX_WARNING;
357 case CAN_STATE_ERROR_PASSIVE:
358 return CAN_ERR_CRTL_TX_PASSIVE;
359 default:
360 return 0;
361 }
362}
363
364static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
365{
366 switch (state) {
367 case CAN_STATE_ERROR_ACTIVE:
368 return CAN_ERR_CRTL_ACTIVE;
369 case CAN_STATE_ERROR_WARNING:
370 return CAN_ERR_CRTL_RX_WARNING;
371 case CAN_STATE_ERROR_PASSIVE:
372 return CAN_ERR_CRTL_RX_PASSIVE;
373 default:
374 return 0;
375 }
376}
377
378void can_change_state(struct net_device *dev, struct can_frame *cf,
379 enum can_state tx_state, enum can_state rx_state)
380{
381 struct can_priv *priv = netdev_priv(dev);
382 enum can_state new_state = max(tx_state, rx_state);
383
384 if (unlikely(new_state == priv->state)) {
385 netdev_warn(dev, "%s: oops, state did not change", __func__);
386 return;
387 }
388
389 netdev_dbg(dev, "New error state: %d\n", new_state);
390
391 can_update_state_error_stats(dev, new_state);
392 priv->state = new_state;
393
Marc Kleine-Buddeff3416f2017-05-18 10:22:22 +0200394 if (!cf)
395 return;
396
Andri Yngvasonbac78aa2014-12-03 17:54:13 +0000397 if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
398 cf->can_id |= CAN_ERR_BUSOFF;
399 return;
400 }
401
402 cf->can_id |= CAN_ERR_CRTL;
403 cf->data[1] |= tx_state >= rx_state ?
404 can_tx_state_to_frame(dev, tx_state) : 0;
405 cf->data[1] |= tx_state <= rx_state ?
406 can_rx_state_to_frame(dev, rx_state) : 0;
407}
408EXPORT_SYMBOL_GPL(can_change_state);
409
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000410/*
411 * Local echo of CAN messages
412 *
413 * CAN network devices *should* support a local echo functionality
414 * (see Documentation/networking/can.txt). To test the handling of CAN
415 * interfaces that do not support the local echo both driver types are
416 * implemented. In the case that the driver does not support the echo
417 * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
418 * to perform the echo as a fallback solution.
419 */
420static void can_flush_echo_skb(struct net_device *dev)
421{
422 struct can_priv *priv = netdev_priv(dev);
423 struct net_device_stats *stats = &dev->stats;
424 int i;
425
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000426 for (i = 0; i < priv->echo_skb_max; i++) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000427 if (priv->echo_skb[i]) {
428 kfree_skb(priv->echo_skb[i]);
429 priv->echo_skb[i] = NULL;
430 stats->tx_dropped++;
431 stats->tx_aborted_errors++;
432 }
433 }
434}
435
436/*
437 * Put the skb on the stack to be looped backed locally lateron
438 *
439 * The function is typically called in the start_xmit function
440 * of the device driver. The driver must protect access to
441 * priv->echo_skb, if necessary.
442 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000443void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
444 unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000445{
446 struct can_priv *priv = netdev_priv(dev);
447
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000448 BUG_ON(idx >= priv->echo_skb_max);
449
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000450 /* check flag whether this packet has to be looped back */
Oliver Hartkoppa94bc9c2014-02-28 16:36:19 +0100451 if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
452 (skb->protocol != htons(ETH_P_CAN) &&
453 skb->protocol != htons(ETH_P_CANFD))) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000454 kfree_skb(skb);
455 return;
456 }
457
458 if (!priv->echo_skb[idx]) {
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000459
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100460 skb = can_create_echo_skb(skb);
461 if (!skb)
462 return;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000463
464 /* make settings for echo to reduce code in irq context */
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000465 skb->pkt_type = PACKET_BROADCAST;
466 skb->ip_summed = CHECKSUM_UNNECESSARY;
467 skb->dev = dev;
468
469 /* save this skb for tx interrupt echo handling */
470 priv->echo_skb[idx] = skb;
471 } else {
472 /* locking problem with netif_stop_queue() ?? */
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100473 netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000474 kfree_skb(skb);
475 }
476}
477EXPORT_SYMBOL_GPL(can_put_echo_skb);
478
479/*
480 * Get the skb from the stack and loop it back locally
481 *
482 * The function is typically called when the TX done interrupt
483 * is handled in the device driver. The driver must protect
484 * access to priv->echo_skb, if necessary.
485 */
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200486unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000487{
488 struct can_priv *priv = netdev_priv(dev);
489
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000490 BUG_ON(idx >= priv->echo_skb_max);
491
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000492 if (priv->echo_skb[idx]) {
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200493 struct sk_buff *skb = priv->echo_skb[idx];
494 struct can_frame *cf = (struct can_frame *)skb->data;
495 u8 dlc = cf->can_dlc;
496
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000497 netif_rx(priv->echo_skb[idx]);
498 priv->echo_skb[idx] = NULL;
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200499
500 return dlc;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000501 }
Marc Kleine-Buddecf5046b2011-10-10 23:43:53 +0200502
503 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000504}
505EXPORT_SYMBOL_GPL(can_get_echo_skb);
506
507/*
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000508 * Remove the skb from the stack and free it.
509 *
510 * The function is typically called when TX failed.
511 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000512void can_free_echo_skb(struct net_device *dev, unsigned int idx)
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000513{
514 struct can_priv *priv = netdev_priv(dev);
515
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000516 BUG_ON(idx >= priv->echo_skb_max);
517
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000518 if (priv->echo_skb[idx]) {
Thomas Körper5247a582014-10-31 07:33:54 +0100519 dev_kfree_skb_any(priv->echo_skb[idx]);
Wolfgang Grandegger39e3ab62009-09-01 05:26:12 +0000520 priv->echo_skb[idx] = NULL;
521 }
522}
523EXPORT_SYMBOL_GPL(can_free_echo_skb);
524
525/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000526 * CAN device restart for bus-off recovery
527 */
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300528static void can_restart(struct net_device *dev)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000529{
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000530 struct can_priv *priv = netdev_priv(dev);
531 struct net_device_stats *stats = &dev->stats;
532 struct sk_buff *skb;
533 struct can_frame *cf;
534 int err;
535
536 BUG_ON(netif_carrier_ok(dev));
537
538 /*
539 * No synchronization needed because the device is bus-off and
540 * no messages can come in or go out.
541 */
542 can_flush_echo_skb(dev);
543
544 /* send restart message upstream */
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700545 skb = alloc_can_err_skb(dev, &cf);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000546 if (skb == NULL) {
547 err = -ENOMEM;
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000548 goto restart;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000549 }
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700550 cf->can_id |= CAN_ERR_RESTARTED;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000551
552 netif_rx(skb);
553
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000554 stats->rx_packets++;
555 stats->rx_bytes += cf->can_dlc;
556
Wolfgang Grandeggerb3d0df72009-07-20 04:06:40 +0000557restart:
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100558 netdev_dbg(dev, "restarted\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000559 priv->can_stats.restarts++;
560
561 /* Now restart the device */
562 err = priv->do_set_mode(dev, CAN_MODE_START);
563
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000564 netif_carrier_on(dev);
565 if (err)
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100566 netdev_err(dev, "Error %d during restart", err);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000567}
568
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300569static void can_restart_work(struct work_struct *work)
570{
571 struct delayed_work *dwork = to_delayed_work(work);
572 struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
573
574 can_restart(priv->dev);
575}
576
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000577int can_restart_now(struct net_device *dev)
578{
579 struct can_priv *priv = netdev_priv(dev);
580
581 /*
582 * A manual restart is only permitted if automatic restart is
583 * disabled and the device is in the bus-off state
584 */
585 if (priv->restart_ms)
586 return -EINVAL;
587 if (priv->state != CAN_STATE_BUS_OFF)
588 return -EBUSY;
589
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300590 cancel_delayed_work_sync(&priv->restart_work);
591 can_restart(dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000592
593 return 0;
594}
595
596/*
597 * CAN bus-off
598 *
599 * This functions should be called when the device goes bus-off to
600 * tell the netif layer that no more packets can be sent or received.
601 * If enabled, a timer is started to trigger bus-off recovery.
602 */
603void can_bus_off(struct net_device *dev)
604{
605 struct can_priv *priv = netdev_priv(dev);
606
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100607 netdev_dbg(dev, "bus-off\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000608
609 netif_carrier_off(dev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000610
611 if (priv->restart_ms)
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300612 schedule_delayed_work(&priv->restart_work,
613 msecs_to_jiffies(priv->restart_ms));
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000614}
615EXPORT_SYMBOL_GPL(can_bus_off);
616
617static void can_setup(struct net_device *dev)
618{
619 dev->type = ARPHRD_CAN;
Oliver Hartkopp1e0625f2012-06-13 20:48:21 +0200620 dev->mtu = CAN_MTU;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000621 dev->hard_header_len = 0;
622 dev->addr_len = 0;
623 dev->tx_queue_len = 10;
624
625 /* New-style flags. */
626 dev->flags = IFF_NOARP;
Michał Mirosław34324dc2011-11-15 15:29:55 +0000627 dev->features = NETIF_F_HW_CSUM;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000628}
629
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700630struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
631{
632 struct sk_buff *skb;
633
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100634 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
635 sizeof(struct can_frame));
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700636 if (unlikely(!skb))
637 return NULL;
638
639 skb->protocol = htons(ETH_P_CAN);
640 skb->pkt_type = PACKET_BROADCAST;
641 skb->ip_summed = CHECKSUM_UNNECESSARY;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100642
Oliver Hartkopp9694390162015-02-23 20:37:54 +0100643 skb_reset_mac_header(skb);
644 skb_reset_network_header(skb);
645 skb_reset_transport_header(skb);
646
Oliver Hartkopp2bf34402013-01-28 08:33:33 +0000647 can_skb_reserve(skb);
648 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200649 can_skb_prv(skb)->skbcnt = 0;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100650
Johannes Berg4df864c2017-06-16 14:29:21 +0200651 *cf = skb_put(skb, sizeof(struct can_frame));
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700652 memset(*cf, 0, sizeof(struct can_frame));
653
654 return skb;
655}
656EXPORT_SYMBOL_GPL(alloc_can_skb);
657
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100658struct sk_buff *alloc_canfd_skb(struct net_device *dev,
659 struct canfd_frame **cfd)
660{
661 struct sk_buff *skb;
662
663 skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
664 sizeof(struct canfd_frame));
665 if (unlikely(!skb))
666 return NULL;
667
668 skb->protocol = htons(ETH_P_CANFD);
669 skb->pkt_type = PACKET_BROADCAST;
670 skb->ip_summed = CHECKSUM_UNNECESSARY;
671
Oliver Hartkopp9694390162015-02-23 20:37:54 +0100672 skb_reset_mac_header(skb);
673 skb_reset_network_header(skb);
674 skb_reset_transport_header(skb);
675
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100676 can_skb_reserve(skb);
677 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200678 can_skb_prv(skb)->skbcnt = 0;
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100679
Johannes Berg4df864c2017-06-16 14:29:21 +0200680 *cfd = skb_put(skb, sizeof(struct canfd_frame));
Stephane Grosjeancb2518c2014-01-15 09:50:13 +0100681 memset(*cfd, 0, sizeof(struct canfd_frame));
682
683 return skb;
684}
685EXPORT_SYMBOL_GPL(alloc_canfd_skb);
686
Wolfgang Grandegger7b6856a02009-10-20 00:08:01 -0700687struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
688{
689 struct sk_buff *skb;
690
691 skb = alloc_can_skb(dev, cf);
692 if (unlikely(!skb))
693 return NULL;
694
695 (*cf)->can_id = CAN_ERR_FLAG;
696 (*cf)->can_dlc = CAN_ERR_DLC;
697
698 return skb;
699}
700EXPORT_SYMBOL_GPL(alloc_can_err_skb);
701
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000702/*
703 * Allocate and setup space for the CAN network device
704 */
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000705struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000706{
707 struct net_device *dev;
708 struct can_priv *priv;
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000709 int size;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000710
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000711 if (echo_skb_max)
712 size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
713 echo_skb_max * sizeof(struct sk_buff *);
714 else
715 size = sizeof_priv;
716
Tom Gundersenc835a672014-07-14 16:37:24 +0200717 dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000718 if (!dev)
719 return NULL;
720
721 priv = netdev_priv(dev);
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300722 priv->dev = dev;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000723
Wolfgang Grandeggera6e4bc52009-10-08 22:17:11 +0000724 if (echo_skb_max) {
725 priv->echo_skb_max = echo_skb_max;
726 priv->echo_skb = (void *)priv +
727 ALIGN(sizeof_priv, sizeof(struct sk_buff *));
728 }
729
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000730 priv->state = CAN_STATE_STOPPED;
731
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300732 INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000733
734 return dev;
735}
736EXPORT_SYMBOL_GPL(alloc_candev);
737
738/*
739 * Free space of the CAN network device
740 */
741void free_candev(struct net_device *dev)
742{
743 free_netdev(dev);
744}
745EXPORT_SYMBOL_GPL(free_candev);
746
747/*
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100748 * changing MTU and control mode for CAN/CANFD devices
749 */
750int can_change_mtu(struct net_device *dev, int new_mtu)
751{
752 struct can_priv *priv = netdev_priv(dev);
753
754 /* Do not allow changing the MTU while running */
755 if (dev->flags & IFF_UP)
756 return -EBUSY;
757
758 /* allow change of MTU according to the CANFD ability of the device */
759 switch (new_mtu) {
760 case CAN_MTU:
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100761 /* 'CANFD-only' controllers can not switch to CAN_MTU */
762 if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
763 return -EINVAL;
764
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100765 priv->ctrlmode &= ~CAN_CTRLMODE_FD;
766 break;
767
768 case CANFD_MTU:
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100769 /* check for potential CANFD ability */
770 if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
771 !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100772 return -EINVAL;
773
774 priv->ctrlmode |= CAN_CTRLMODE_FD;
775 break;
776
777 default:
778 return -EINVAL;
779 }
780
781 dev->mtu = new_mtu;
782 return 0;
783}
784EXPORT_SYMBOL_GPL(can_change_mtu);
785
786/*
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000787 * Common open function when the device gets opened.
788 *
789 * This function should be called in the open function of the device
790 * driver.
791 */
792int open_candev(struct net_device *dev)
793{
794 struct can_priv *priv = netdev_priv(dev);
795
Oliver Hartkoppb30749f2014-02-28 16:36:20 +0100796 if (!priv->bittiming.bitrate) {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100797 netdev_err(dev, "bit-timing not yet defined\n");
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000798 return -EINVAL;
799 }
800
Oliver Hartkoppdd225862014-02-28 16:36:25 +0100801 /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
802 if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
803 (!priv->data_bittiming.bitrate ||
804 (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
805 netdev_err(dev, "incorrect/missing data bit-timing\n");
806 return -EINVAL;
807 }
808
Wolfgang Grandegger1b0d9222009-07-20 04:06:41 +0000809 /* Switch carrier on if device was stopped while in bus-off state */
810 if (!netif_carrier_ok(dev))
811 netif_carrier_on(dev);
812
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000813 return 0;
814}
Wolfgang Grandegger128ced82009-05-30 07:55:48 +0000815EXPORT_SYMBOL_GPL(open_candev);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000816
817/*
818 * Common close function for cleanup before the device gets closed.
819 *
820 * This function should be called in the close function of the device
821 * driver.
822 */
823void close_candev(struct net_device *dev)
824{
825 struct can_priv *priv = netdev_priv(dev);
826
Sergei Miroshnichenko9abefcb2016-09-07 16:51:12 +0300827 cancel_delayed_work_sync(&priv->restart_work);
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000828 can_flush_echo_skb(dev);
829}
830EXPORT_SYMBOL_GPL(close_candev);
831
832/*
833 * CAN netlink interface
834 */
835static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
836 [IFLA_CAN_STATE] = { .type = NLA_U32 },
837 [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
838 [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
839 [IFLA_CAN_RESTART] = { .type = NLA_U32 },
840 [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
841 [IFLA_CAN_BITTIMING_CONST]
842 = { .len = sizeof(struct can_bittiming_const) },
843 [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +0000844 [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +0100845 [IFLA_CAN_DATA_BITTIMING]
846 = { .len = sizeof(struct can_bittiming) },
847 [IFLA_CAN_DATA_BITTIMING_CONST]
848 = { .len = sizeof(struct can_bittiming_const) },
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000849};
850
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200851static int can_validate(struct nlattr *tb[], struct nlattr *data[],
852 struct netlink_ext_ack *extack)
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100853{
854 bool is_can_fd = false;
855
856 /* Make sure that valid CAN FD configurations always consist of
857 * - nominal/arbitration bittiming
858 * - data bittiming
859 * - control mode with CAN_CTRLMODE_FD set
860 */
861
Oliver Hartkoppbce271f2016-06-21 12:14:07 +0200862 if (!data)
863 return 0;
864
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100865 if (data[IFLA_CAN_CTRLMODE]) {
866 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
867
868 is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
869 }
870
871 if (is_can_fd) {
872 if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
873 return -EOPNOTSUPP;
874 }
875
876 if (data[IFLA_CAN_DATA_BITTIMING]) {
877 if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
878 return -EOPNOTSUPP;
879 }
880
881 return 0;
882}
883
Matthias Schifferad744b22017-06-25 23:56:00 +0200884static int can_changelink(struct net_device *dev, struct nlattr *tb[],
885 struct nlattr *data[],
886 struct netlink_ext_ack *extack)
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000887{
888 struct can_priv *priv = netdev_priv(dev);
889 int err;
890
891 /* We need synchronization with dev->stop() */
892 ASSERT_RTNL();
893
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000894 if (data[IFLA_CAN_BITTIMING]) {
895 struct can_bittiming bt;
896
897 /* Do not allow changing bittiming while running */
898 if (dev->flags & IFF_UP)
899 return -EBUSY;
Marc Kleine-Buddec3606d42017-01-06 11:12:41 +0100900
901 /* Calculate bittiming parameters based on
902 * bittiming_const if set, otherwise pass bitrate
903 * directly via do_set_bitrate(). Bail out if neither
904 * is given.
905 */
906 if (!priv->bittiming_const && !priv->do_set_bittiming)
907 return -EOPNOTSUPP;
908
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000909 memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100910 err = can_get_bittiming(dev, &bt,
911 priv->bittiming_const,
912 priv->bitrate_const,
913 priv->bitrate_const_cnt);
914 if (err)
915 return err;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000916 memcpy(&priv->bittiming, &bt, sizeof(bt));
917
918 if (priv->do_set_bittiming) {
919 /* Finally, set the bit-timing registers */
920 err = priv->do_set_bittiming(dev);
921 if (err)
922 return err;
923 }
924 }
925
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200926 if (data[IFLA_CAN_CTRLMODE]) {
927 struct can_ctrlmode *cm;
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100928 u32 ctrlstatic;
929 u32 maskedflags;
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200930
931 /* Do not allow changing controller mode while running */
932 if (dev->flags & IFF_UP)
933 return -EBUSY;
934 cm = nla_data(data[IFLA_CAN_CTRLMODE]);
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100935 ctrlstatic = priv->ctrlmode_static;
936 maskedflags = cm->flags & cm->mask;
Oliver Hartkopp9b1087a2015-01-05 18:40:15 +0100937
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100938 /* check whether provided bits are allowed to be passed */
939 if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
940 return -EOPNOTSUPP;
941
942 /* do not check for static fd-non-iso if 'fd' is disabled */
943 if (!(maskedflags & CAN_CTRLMODE_FD))
944 ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
945
946 /* make sure static options are provided by configuration */
947 if ((maskedflags & ctrlstatic) != ctrlstatic)
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200948 return -EOPNOTSUPP;
Oliver Hartkopp9b1087a2015-01-05 18:40:15 +0100949
950 /* clear bits to be modified and copy the flag values */
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200951 priv->ctrlmode &= ~cm->mask;
Oliver Hartkoppbb208f12016-03-21 20:18:21 +0100952 priv->ctrlmode |= maskedflags;
Oliver Hartkoppbc05a892014-02-28 16:36:24 +0100953
954 /* CAN_CTRLMODE_FD can only be set when driver supports FD */
955 if (priv->ctrlmode & CAN_CTRLMODE_FD)
956 dev->mtu = CANFD_MTU;
957 else
958 dev->mtu = CAN_MTU;
Marc Kleine-Budde49cb5c02013-10-04 15:40:22 +0200959 }
960
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +0000961 if (data[IFLA_CAN_RESTART_MS]) {
962 /* Do not allow changing restart delay while running */
963 if (dev->flags & IFF_UP)
964 return -EBUSY;
965 priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
966 }
967
968 if (data[IFLA_CAN_RESTART]) {
969 /* Do not allow a restart while not running */
970 if (!(dev->flags & IFF_UP))
971 return -EINVAL;
972 err = can_restart_now(dev);
973 if (err)
974 return err;
975 }
976
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +0100977 if (data[IFLA_CAN_DATA_BITTIMING]) {
978 struct can_bittiming dbt;
979
980 /* Do not allow changing bittiming while running */
981 if (dev->flags & IFF_UP)
982 return -EBUSY;
Marc Kleine-Buddec3606d42017-01-06 11:12:41 +0100983
984 /* Calculate bittiming parameters based on
985 * data_bittiming_const if set, otherwise pass bitrate
986 * directly via do_set_bitrate(). Bail out if neither
987 * is given.
988 */
989 if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
990 return -EOPNOTSUPP;
991
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +0100992 memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
993 sizeof(dbt));
Marc Kleine-Budde431af772017-01-11 17:05:35 +0100994 err = can_get_bittiming(dev, &dbt,
995 priv->data_bittiming_const,
996 priv->data_bitrate_const,
997 priv->data_bitrate_const_cnt);
998 if (err)
999 return err;
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001000 memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
1001
1002 if (priv->do_set_data_bittiming) {
1003 /* Finally, set the bit-timing registers */
1004 err = priv->do_set_data_bittiming(dev);
1005 if (err)
1006 return err;
1007 }
1008 }
1009
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001010 if (data[IFLA_CAN_TERMINATION]) {
1011 const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
1012 const unsigned int num_term = priv->termination_const_cnt;
1013 unsigned int i;
1014
1015 if (!priv->do_set_termination)
1016 return -EOPNOTSUPP;
1017
1018 /* check whether given value is supported by the interface */
1019 for (i = 0; i < num_term; i++) {
1020 if (termval == priv->termination_const[i])
1021 break;
1022 }
1023 if (i >= num_term)
1024 return -EINVAL;
1025
1026 /* Finally, set the termination value */
1027 err = priv->do_set_termination(dev, termval);
1028 if (err)
1029 return err;
1030
1031 priv->termination = termval;
1032 }
1033
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001034 return 0;
1035}
1036
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001037static size_t can_get_size(const struct net_device *dev)
1038{
1039 struct can_priv *priv = netdev_priv(dev);
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +02001040 size_t size = 0;
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001041
Oliver Hartkoppb30749f2014-02-28 16:36:20 +01001042 if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
1043 size += nla_total_size(sizeof(struct can_bittiming));
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +02001044 if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
Marc Kleine-Buddefe119a02013-10-05 21:25:17 +02001045 size += nla_total_size(sizeof(struct can_bittiming_const));
Marc Kleine-Buddec13c64d2013-10-05 20:34:47 +02001046 size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
1047 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
1048 size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
1049 size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
1050 if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
1051 size += nla_total_size(sizeof(struct can_berr_counter));
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001052 if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
1053 size += nla_total_size(sizeof(struct can_bittiming));
1054 if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
1055 size += nla_total_size(sizeof(struct can_bittiming_const));
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001056 if (priv->termination_const) {
1057 size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
1058 size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
1059 priv->termination_const_cnt);
1060 }
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001061 if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
1062 size += nla_total_size(sizeof(*priv->bitrate_const) *
1063 priv->bitrate_const_cnt);
1064 if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
1065 size += nla_total_size(sizeof(*priv->data_bitrate_const) *
1066 priv->data_bitrate_const_cnt);
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001067
1068 return size;
1069}
1070
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001071static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
1072{
1073 struct can_priv *priv = netdev_priv(dev);
1074 struct can_ctrlmode cm = {.flags = priv->ctrlmode};
Wolfgang Grandegger52c793f2010-02-22 22:21:17 +00001075 struct can_berr_counter bec;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001076 enum can_state state = priv->state;
1077
1078 if (priv->do_get_state)
1079 priv->do_get_state(dev, &state);
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001080
Oliver Hartkoppb30749f2014-02-28 16:36:20 +01001081 if ((priv->bittiming.bitrate &&
1082 nla_put(skb, IFLA_CAN_BITTIMING,
1083 sizeof(priv->bittiming), &priv->bittiming)) ||
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001084
David S. Miller31e0e322012-04-01 20:21:11 -04001085 (priv->bittiming_const &&
1086 nla_put(skb, IFLA_CAN_BITTIMING_CONST,
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001087 sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001088
Marek Vasut562b1032015-10-30 13:48:19 +01001089 nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001090 nla_put_u32(skb, IFLA_CAN_STATE, state) ||
1091 nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
1092 nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001093
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001094 (priv->do_get_berr_counter &&
1095 !priv->do_get_berr_counter(dev, &bec) &&
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001096 nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
1097
1098 (priv->data_bittiming.bitrate &&
1099 nla_put(skb, IFLA_CAN_DATA_BITTIMING,
1100 sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
1101
1102 (priv->data_bittiming_const &&
1103 nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
1104 sizeof(*priv->data_bittiming_const),
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001105 priv->data_bittiming_const)) ||
1106
1107 (priv->termination_const &&
1108 (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
1109 nla_put(skb, IFLA_CAN_TERMINATION_CONST,
1110 sizeof(*priv->termination_const) *
1111 priv->termination_const_cnt,
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001112 priv->termination_const))) ||
1113
1114 (priv->bitrate_const &&
1115 nla_put(skb, IFLA_CAN_BITRATE_CONST,
1116 sizeof(*priv->bitrate_const) *
1117 priv->bitrate_const_cnt,
1118 priv->bitrate_const)) ||
1119
1120 (priv->data_bitrate_const &&
1121 nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
1122 sizeof(*priv->data_bitrate_const) *
1123 priv->data_bitrate_const_cnt,
1124 priv->data_bitrate_const))
1125 )
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001126
Marc Kleine-Budde57a59b92013-10-04 13:35:57 +02001127 return -EMSGSIZE;
Oliver Hartkopp9859ccd2014-02-28 16:36:23 +01001128
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001129 return 0;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001130}
1131
Wolfgang Grandegger55369c02009-11-12 05:34:05 +00001132static size_t can_get_xstats_size(const struct net_device *dev)
1133{
1134 return sizeof(struct can_device_stats);
1135}
1136
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001137static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
1138{
1139 struct can_priv *priv = netdev_priv(dev);
1140
David S. Miller31e0e322012-04-01 20:21:11 -04001141 if (nla_put(skb, IFLA_INFO_XSTATS,
1142 sizeof(priv->can_stats), &priv->can_stats))
1143 goto nla_put_failure;
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001144 return 0;
1145
1146nla_put_failure:
1147 return -EMSGSIZE;
1148}
1149
Eric W. Biederman81adee42009-11-08 00:53:51 -08001150static int can_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001151 struct nlattr *tb[], struct nlattr *data[],
1152 struct netlink_ext_ack *extack)
Oliver Hartkopp993e6f22009-08-11 02:41:24 +00001153{
1154 return -EOPNOTSUPP;
1155}
1156
Oliver Hartkopp25e1ed62016-06-21 15:45:47 +02001157static void can_dellink(struct net_device *dev, struct list_head *head)
1158{
1159 return;
1160}
1161
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001162static struct rtnl_link_ops can_link_ops __read_mostly = {
1163 .kind = "can",
1164 .maxtype = IFLA_CAN_MAX,
1165 .policy = can_policy,
1166 .setup = can_setup,
Oliver Hartkoppbb208f12016-03-21 20:18:21 +01001167 .validate = can_validate,
Oliver Hartkopp993e6f22009-08-11 02:41:24 +00001168 .newlink = can_newlink,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001169 .changelink = can_changelink,
Oliver Hartkopp25e1ed62016-06-21 15:45:47 +02001170 .dellink = can_dellink,
Wolfgang Grandegger53a0ef862009-11-06 23:53:13 +00001171 .get_size = can_get_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001172 .fill_info = can_fill_info,
Wolfgang Grandegger55369c02009-11-12 05:34:05 +00001173 .get_xstats_size = can_get_xstats_size,
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001174 .fill_xstats = can_fill_xstats,
1175};
1176
1177/*
1178 * Register the CAN network device
1179 */
1180int register_candev(struct net_device *dev)
1181{
Oliver Hartkopp12a60752017-01-10 18:52:06 +01001182 struct can_priv *priv = netdev_priv(dev);
1183
1184 /* Ensure termination_const, termination_const_cnt and
1185 * do_set_termination consistency. All must be either set or
1186 * unset.
1187 */
1188 if ((!priv->termination_const != !priv->termination_const_cnt) ||
1189 (!priv->termination_const != !priv->do_set_termination))
1190 return -EINVAL;
1191
Marc Kleine-Budde431af772017-01-11 17:05:35 +01001192 if (!priv->bitrate_const != !priv->bitrate_const_cnt)
1193 return -EINVAL;
1194
1195 if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
1196 return -EINVAL;
1197
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001198 dev->rtnl_link_ops = &can_link_ops;
1199 return register_netdev(dev);
1200}
1201EXPORT_SYMBOL_GPL(register_candev);
1202
1203/*
1204 * Unregister the CAN network device
1205 */
1206void unregister_candev(struct net_device *dev)
1207{
1208 unregister_netdev(dev);
1209}
1210EXPORT_SYMBOL_GPL(unregister_candev);
1211
Kurt Van Dijckbf03a532012-12-18 18:50:56 +01001212/*
1213 * Test if a network device is a candev based device
1214 * and return the can_priv* if so.
1215 */
1216struct can_priv *safe_candev_priv(struct net_device *dev)
1217{
1218 if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
1219 return NULL;
1220
1221 return netdev_priv(dev);
1222}
1223EXPORT_SYMBOL_GPL(safe_candev_priv);
1224
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001225static __init int can_dev_init(void)
1226{
1227 int err;
1228
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +01001229 can_led_notifier_init();
1230
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001231 err = rtnl_link_register(&can_link_ops);
1232 if (!err)
1233 printk(KERN_INFO MOD_DESC "\n");
1234
1235 return err;
1236}
1237module_init(can_dev_init);
1238
1239static __exit void can_dev_exit(void)
1240{
1241 rtnl_link_unregister(&can_link_ops);
Kurt Van Dijcka1ef7bd2012-12-18 18:50:57 +01001242
1243 can_led_notifier_exit();
Wolfgang Grandegger39549ee2009-05-15 23:39:29 +00001244}
1245module_exit(can_dev_exit);
1246
1247MODULE_ALIAS_RTNL_LINK("can");