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