]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/net/can/peak_canfd/peak_canfd.c
UBUNTU: Ubuntu-5.4.0-117.132
[mirror_ubuntu-focal-kernel.git] / drivers / net / can / peak_canfd / peak_canfd.c
CommitLineData
aaa7cb26 1// SPDX-License-Identifier: GPL-2.0-only
8ac8321e
SG
2/*
3 * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
4 * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
5 *
6 * Copyright (C) 2016 PEAK System-Technik GmbH
8ac8321e
SG
7 */
8
9#include <linux/can.h>
10#include <linux/can/dev.h>
11
12#include "peak_canfd_user.h"
13
14/* internal IP core cache size (used as default echo skbs max number) */
15#define PCANFD_ECHO_SKB_MAX 24
16
17/* bittiming ranges of the PEAK-System PC CAN-FD interfaces */
18static const struct can_bittiming_const peak_canfd_nominal_const = {
19 .name = "peak_canfd",
20 .tseg1_min = 1,
21 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
22 .tseg2_min = 1,
23 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
24 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
25 .brp_min = 1,
26 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
27 .brp_inc = 1,
28};
29
30static const struct can_bittiming_const peak_canfd_data_const = {
31 .name = "peak_canfd",
32 .tseg1_min = 1,
33 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
34 .tseg2_min = 1,
35 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
36 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
37 .brp_min = 1,
38 .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
39 .brp_inc = 1,
40};
41
42static struct peak_canfd_priv *pucan_init_cmd(struct peak_canfd_priv *priv)
43{
44 priv->cmd_len = 0;
45 return priv;
46}
47
48static void *pucan_add_cmd(struct peak_canfd_priv *priv, int cmd_op)
49{
50 struct pucan_command *cmd;
51
52 if (priv->cmd_len + sizeof(*cmd) > priv->cmd_maxlen)
53 return NULL;
54
55 cmd = priv->cmd_buffer + priv->cmd_len;
56
57 /* reset all unused bit to default */
58 memset(cmd, 0, sizeof(*cmd));
59
60 cmd->opcode_channel = pucan_cmd_opcode_channel(priv->index, cmd_op);
61 priv->cmd_len += sizeof(*cmd);
62
63 return cmd;
64}
65
66static int pucan_write_cmd(struct peak_canfd_priv *priv)
67{
68 int err;
69
70 if (priv->pre_cmd) {
71 err = priv->pre_cmd(priv);
72 if (err)
73 return err;
74 }
75
76 err = priv->write_cmd(priv);
77 if (err)
78 return err;
79
80 if (priv->post_cmd)
81 err = priv->post_cmd(priv);
82
83 return err;
84}
85
86/* uCAN commands interface functions */
87static int pucan_set_reset_mode(struct peak_canfd_priv *priv)
88{
89 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RESET_MODE);
90 return pucan_write_cmd(priv);
91}
92
93static int pucan_set_normal_mode(struct peak_canfd_priv *priv)
94{
95 int err;
96
97 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_NORMAL_MODE);
98 err = pucan_write_cmd(priv);
99 if (!err)
100 priv->can.state = CAN_STATE_ERROR_ACTIVE;
101
102 return err;
103}
104
105static int pucan_set_listen_only_mode(struct peak_canfd_priv *priv)
106{
107 int err;
108
109 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_LISTEN_ONLY_MODE);
110 err = pucan_write_cmd(priv);
111 if (!err)
112 priv->can.state = CAN_STATE_ERROR_ACTIVE;
113
114 return err;
115}
116
117static int pucan_set_timing_slow(struct peak_canfd_priv *priv,
118 const struct can_bittiming *pbt)
119{
120 struct pucan_timing_slow *cmd;
121
122 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_SLOW);
123
124 cmd->sjw_t = PUCAN_TSLOW_SJW_T(pbt->sjw - 1,
125 priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES);
126 cmd->tseg1 = PUCAN_TSLOW_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
127 cmd->tseg2 = PUCAN_TSLOW_TSEG2(pbt->phase_seg2 - 1);
128 cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(pbt->brp - 1));
129
130 cmd->ewl = 96; /* default */
131
132 netdev_dbg(priv->ndev,
133 "nominal: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
134 le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw_t);
135
136 return pucan_write_cmd(priv);
137}
138
139static int pucan_set_timing_fast(struct peak_canfd_priv *priv,
140 const struct can_bittiming *pbt)
141{
142 struct pucan_timing_fast *cmd;
143
144 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_FAST);
145
146 cmd->sjw = PUCAN_TFAST_SJW(pbt->sjw - 1);
147 cmd->tseg1 = PUCAN_TFAST_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
148 cmd->tseg2 = PUCAN_TFAST_TSEG2(pbt->phase_seg2 - 1);
149 cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(pbt->brp - 1));
150
151 netdev_dbg(priv->ndev,
152 "data: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
153 le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw);
154
155 return pucan_write_cmd(priv);
156}
157
158static int pucan_set_std_filter(struct peak_canfd_priv *priv, u8 row, u32 mask)
159{
160 struct pucan_std_filter *cmd;
161
162 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_STD_FILTER);
163
164 /* all the 11-bits CAN ID values are represented by one bit in a
165 * 64 rows array of 32 bits: the upper 6 bits of the CAN ID select the
166 * row while the lowest 5 bits select the bit in that row.
167 *
168 * bit filter
169 * 1 passed
170 * 0 discarded
171 */
172
173 /* select the row */
174 cmd->idx = row;
175
176 /* set/unset bits in the row */
177 cmd->mask = cpu_to_le32(mask);
178
179 return pucan_write_cmd(priv);
180}
181
182static int pucan_tx_abort(struct peak_canfd_priv *priv, u16 flags)
183{
184 struct pucan_tx_abort *cmd;
185
186 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TX_ABORT);
187
188 cmd->flags = cpu_to_le16(flags);
189
190 return pucan_write_cmd(priv);
191}
192
193static int pucan_clr_err_counters(struct peak_canfd_priv *priv)
194{
195 struct pucan_wr_err_cnt *cmd;
196
197 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_WR_ERR_CNT);
198
199 cmd->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE | PUCAN_WRERRCNT_RE);
200 cmd->tx_counter = 0;
201 cmd->rx_counter = 0;
202
203 return pucan_write_cmd(priv);
204}
205
206static int pucan_set_options(struct peak_canfd_priv *priv, u16 opt_mask)
207{
208 struct pucan_options *cmd;
209
210 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_EN_OPTION);
211
212 cmd->options = cpu_to_le16(opt_mask);
213
214 return pucan_write_cmd(priv);
215}
216
217static int pucan_clr_options(struct peak_canfd_priv *priv, u16 opt_mask)
218{
219 struct pucan_options *cmd;
220
221 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_CLR_DIS_OPTION);
222
223 cmd->options = cpu_to_le16(opt_mask);
224
225 return pucan_write_cmd(priv);
226}
227
228static int pucan_setup_rx_barrier(struct peak_canfd_priv *priv)
229{
230 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RX_BARRIER);
231
232 return pucan_write_cmd(priv);
233}
234
d3863ba8
SG
235static int pucan_netif_rx(struct sk_buff *skb, __le32 ts_low, __le32 ts_high)
236{
237 struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
238 u64 ts_us;
239
240 ts_us = (u64)le32_to_cpu(ts_high) << 32;
241 ts_us |= le32_to_cpu(ts_low);
242
243 /* IP core timestamps are µs. */
244 hwts->hwtstamp = ns_to_ktime(ts_us * NSEC_PER_USEC);
245
246 return netif_rx(skb);
247}
248
8ac8321e
SG
249/* handle the reception of one CAN frame */
250static int pucan_handle_can_rx(struct peak_canfd_priv *priv,
251 struct pucan_rx_msg *msg)
252{
253 struct net_device_stats *stats = &priv->ndev->stats;
254 struct canfd_frame *cf;
255 struct sk_buff *skb;
256 const u16 rx_msg_flags = le16_to_cpu(msg->flags);
257 u8 cf_len;
258
259 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN)
260 cf_len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(msg)));
261 else
262 cf_len = get_can_dlc(pucan_msg_get_dlc(msg));
263
264 /* if this frame is an echo, */
3cf79bcb 265 if (rx_msg_flags & PUCAN_MSG_LOOPED_BACK) {
8ac8321e
SG
266 unsigned long flags;
267
268 spin_lock_irqsave(&priv->echo_lock, flags);
91785de6 269 can_get_echo_skb(priv->ndev, msg->client);
8ac8321e
SG
270
271 /* count bytes of the echo instead of skb */
272 stats->tx_bytes += cf_len;
273 stats->tx_packets++;
274
91785de6
SG
275 /* restart tx queue (a slot is free) */
276 netif_wake_queue(priv->ndev);
8ac8321e 277
e6048a00 278 spin_unlock_irqrestore(&priv->echo_lock, flags);
3cf79bcb
SG
279
280 /* if this frame is only an echo, stop here. Otherwise,
281 * continue to push this application self-received frame into
282 * its own rx queue.
283 */
284 if (!(rx_msg_flags & PUCAN_MSG_SELF_RECEIVE))
285 return 0;
8ac8321e
SG
286 }
287
288 /* otherwise, it should be pushed into rx fifo */
289 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
290 /* CANFD frame case */
291 skb = alloc_canfd_skb(priv->ndev, &cf);
292 if (!skb)
293 return -ENOMEM;
294
295 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
296 cf->flags |= CANFD_BRS;
297
298 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
299 cf->flags |= CANFD_ESI;
300 } else {
301 /* CAN 2.0 frame case */
302 skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
303 if (!skb)
304 return -ENOMEM;
305 }
306
307 cf->can_id = le32_to_cpu(msg->can_id);
308 cf->len = cf_len;
309
310 if (rx_msg_flags & PUCAN_MSG_EXT_ID)
311 cf->can_id |= CAN_EFF_FLAG;
312
313 if (rx_msg_flags & PUCAN_MSG_RTR)
314 cf->can_id |= CAN_RTR_FLAG;
315 else
316 memcpy(cf->data, msg->d, cf->len);
317
318 stats->rx_bytes += cf->len;
319 stats->rx_packets++;
320
d3863ba8 321 pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
8ac8321e
SG
322
323 return 0;
324}
325
326/* handle rx/tx error counters notification */
327static int pucan_handle_error(struct peak_canfd_priv *priv,
328 struct pucan_error_msg *msg)
329{
330 priv->bec.txerr = msg->tx_err_cnt;
331 priv->bec.rxerr = msg->rx_err_cnt;
332
333 return 0;
334}
335
336/* handle status notification */
337static int pucan_handle_status(struct peak_canfd_priv *priv,
338 struct pucan_status_msg *msg)
339{
340 struct net_device *ndev = priv->ndev;
341 struct net_device_stats *stats = &ndev->stats;
342 struct can_frame *cf;
343 struct sk_buff *skb;
344
345 /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */
346 if (pucan_status_is_rx_barrier(msg)) {
8ac8321e
SG
347
348 if (priv->enable_tx_path) {
349 int err = priv->enable_tx_path(priv);
350
351 if (err)
352 return err;
353 }
354
95c1bc4f
SG
355 /* wake network queue up (echo_skb array is empty) */
356 netif_wake_queue(ndev);
8ac8321e
SG
357
358 return 0;
359 }
360
361 skb = alloc_can_err_skb(ndev, &cf);
362
363 /* test state error bits according to their priority */
364 if (pucan_status_is_busoff(msg)) {
365 netdev_dbg(ndev, "Bus-off entry status\n");
366 priv->can.state = CAN_STATE_BUS_OFF;
367 priv->can.can_stats.bus_off++;
368 can_bus_off(ndev);
369 if (skb)
370 cf->can_id |= CAN_ERR_BUSOFF;
371
372 } else if (pucan_status_is_passive(msg)) {
373 netdev_dbg(ndev, "Error passive status\n");
374 priv->can.state = CAN_STATE_ERROR_PASSIVE;
375 priv->can.can_stats.error_passive++;
376 if (skb) {
377 cf->can_id |= CAN_ERR_CRTL;
378 cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
379 CAN_ERR_CRTL_TX_PASSIVE :
380 CAN_ERR_CRTL_RX_PASSIVE;
381 cf->data[6] = priv->bec.txerr;
382 cf->data[7] = priv->bec.rxerr;
383 }
384
385 } else if (pucan_status_is_warning(msg)) {
386 netdev_dbg(ndev, "Error warning status\n");
387 priv->can.state = CAN_STATE_ERROR_WARNING;
388 priv->can.can_stats.error_warning++;
389 if (skb) {
390 cf->can_id |= CAN_ERR_CRTL;
391 cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
392 CAN_ERR_CRTL_TX_WARNING :
393 CAN_ERR_CRTL_RX_WARNING;
394 cf->data[6] = priv->bec.txerr;
395 cf->data[7] = priv->bec.rxerr;
396 }
397
398 } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) {
399 /* back to ERROR_ACTIVE */
400 netdev_dbg(ndev, "Error active status\n");
401 can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE,
402 CAN_STATE_ERROR_ACTIVE);
403 } else {
404 dev_kfree_skb(skb);
405 return 0;
406 }
407
408 if (!skb) {
409 stats->rx_dropped++;
410 return -ENOMEM;
411 }
412
413 stats->rx_packets++;
414 stats->rx_bytes += cf->can_dlc;
d3863ba8 415 pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
8ac8321e
SG
416
417 return 0;
418}
419
420/* handle uCAN Rx overflow notification */
421static int pucan_handle_cache_critical(struct peak_canfd_priv *priv)
422{
423 struct net_device_stats *stats = &priv->ndev->stats;
424 struct can_frame *cf;
425 struct sk_buff *skb;
426
427 stats->rx_over_errors++;
428 stats->rx_errors++;
429
430 skb = alloc_can_err_skb(priv->ndev, &cf);
431 if (!skb) {
432 stats->rx_dropped++;
433 return -ENOMEM;
434 }
435
436 cf->can_id |= CAN_ERR_CRTL;
437 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
438
439 cf->data[6] = priv->bec.txerr;
440 cf->data[7] = priv->bec.rxerr;
441
442 stats->rx_bytes += cf->can_dlc;
443 stats->rx_packets++;
444 netif_rx(skb);
445
446 return 0;
447}
448
449/* handle a single uCAN message */
450int peak_canfd_handle_msg(struct peak_canfd_priv *priv,
451 struct pucan_rx_msg *msg)
452{
453 u16 msg_type = le16_to_cpu(msg->type);
454 int msg_size = le16_to_cpu(msg->size);
455 int err;
456
457 if (!msg_size || !msg_type) {
458 /* null packet found: end of list */
459 goto exit;
460 }
461
462 switch (msg_type) {
463 case PUCAN_MSG_CAN_RX:
464 err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg);
465 break;
466 case PUCAN_MSG_ERROR:
467 err = pucan_handle_error(priv, (struct pucan_error_msg *)msg);
468 break;
469 case PUCAN_MSG_STATUS:
470 err = pucan_handle_status(priv, (struct pucan_status_msg *)msg);
471 break;
472 case PUCAN_MSG_CACHE_CRITICAL:
473 err = pucan_handle_cache_critical(priv);
474 break;
475 default:
476 err = 0;
477 }
478
479 if (err < 0)
480 return err;
481
482exit:
483 return msg_size;
484}
485
486/* handle a list of rx_count messages from rx_msg memory address */
487int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv,
488 struct pucan_rx_msg *msg_list, int msg_count)
489{
490 void *msg_ptr = msg_list;
f2a918b4 491 int i, msg_size = 0;
8ac8321e
SG
492
493 for (i = 0; i < msg_count; i++) {
494 msg_size = peak_canfd_handle_msg(priv, msg_ptr);
495
496 /* a null packet can be found at the end of a list */
497 if (msg_size <= 0)
498 break;
499
0cccf0ab 500 msg_ptr += ALIGN(msg_size, 4);
8ac8321e
SG
501 }
502
503 if (msg_size < 0)
504 return msg_size;
505
506 return i;
507}
508
509static int peak_canfd_start(struct peak_canfd_priv *priv)
510{
511 int err;
512
513 err = pucan_clr_err_counters(priv);
514 if (err)
515 goto err_exit;
516
517 priv->echo_idx = 0;
518
519 priv->bec.txerr = 0;
520 priv->bec.rxerr = 0;
521
522 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
523 err = pucan_set_listen_only_mode(priv);
524 else
525 err = pucan_set_normal_mode(priv);
526
527err_exit:
528 return err;
529}
530
531static void peak_canfd_stop(struct peak_canfd_priv *priv)
532{
533 int err;
534
535 /* go back to RESET mode */
536 err = pucan_set_reset_mode(priv);
537 if (err) {
538 netdev_err(priv->ndev, "channel %u reset failed\n",
539 priv->index);
540 } else {
541 /* abort last Tx (MUST be done in RESET mode only!) */
542 pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH);
543 }
544}
545
546static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
547{
548 struct peak_canfd_priv *priv = netdev_priv(ndev);
549
550 switch (mode) {
551 case CAN_MODE_START:
552 peak_canfd_start(priv);
553 netif_wake_queue(ndev);
554 break;
555 default:
556 return -EOPNOTSUPP;
557 }
558
559 return 0;
560}
561
562static int peak_canfd_get_berr_counter(const struct net_device *ndev,
563 struct can_berr_counter *bec)
564{
565 struct peak_canfd_priv *priv = netdev_priv(ndev);
566
567 *bec = priv->bec;
568 return 0;
569}
570
571static int peak_canfd_open(struct net_device *ndev)
572{
573 struct peak_canfd_priv *priv = netdev_priv(ndev);
574 int i, err = 0;
575
576 err = open_candev(ndev);
577 if (err) {
578 netdev_err(ndev, "open_candev() failed, error %d\n", err);
579 goto err_exit;
580 }
581
582 err = pucan_set_reset_mode(priv);
583 if (err)
584 goto err_close;
585
586 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
587 if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
588 err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO);
589 else
590 err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO);
591
592 if (err)
593 goto err_close;
594 }
595
596 /* set option: get rx/tx error counters */
597 err = pucan_set_options(priv, PUCAN_OPTION_ERROR);
598 if (err)
599 goto err_close;
600
601 /* accept all standard CAN ID */
602 for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++)
603 pucan_set_std_filter(priv, i, 0xffffffff);
604
605 err = peak_canfd_start(priv);
606 if (err)
607 goto err_close;
608
609 /* receiving the RB status says when Tx path is ready */
610 err = pucan_setup_rx_barrier(priv);
611 if (!err)
612 goto err_exit;
613
614err_close:
615 close_candev(ndev);
616err_exit:
617 return err;
618}
619
620static int peak_canfd_set_bittiming(struct net_device *ndev)
621{
622 struct peak_canfd_priv *priv = netdev_priv(ndev);
623
624 return pucan_set_timing_slow(priv, &priv->can.bittiming);
625}
626
627static int peak_canfd_set_data_bittiming(struct net_device *ndev)
628{
629 struct peak_canfd_priv *priv = netdev_priv(ndev);
630
631 return pucan_set_timing_fast(priv, &priv->can.data_bittiming);
632}
633
634static int peak_canfd_close(struct net_device *ndev)
635{
636 struct peak_canfd_priv *priv = netdev_priv(ndev);
637
638 netif_stop_queue(ndev);
639 peak_canfd_stop(priv);
640 close_candev(ndev);
641
642 return 0;
643}
644
645static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb,
646 struct net_device *ndev)
647{
648 struct peak_canfd_priv *priv = netdev_priv(ndev);
649 struct net_device_stats *stats = &ndev->stats;
650 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
651 struct pucan_tx_msg *msg;
652 u16 msg_size, msg_flags;
653 unsigned long flags;
654 bool should_stop_tx_queue;
655 int room_left;
656 u8 can_dlc;
657
658 if (can_dropped_invalid_skb(ndev, skb))
659 return NETDEV_TX_OK;
660
661 msg_size = ALIGN(sizeof(*msg) + cf->len, 4);
662 msg = priv->alloc_tx_msg(priv, msg_size, &room_left);
663
664 /* should never happen except under bus-off condition and (auto-)restart
665 * mechanism
666 */
667 if (!msg) {
668 stats->tx_dropped++;
669 netif_stop_queue(ndev);
670 return NETDEV_TX_BUSY;
671 }
672
673 msg->size = cpu_to_le16(msg_size);
674 msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
675 msg_flags = 0;
676
677 if (cf->can_id & CAN_EFF_FLAG) {
678 msg_flags |= PUCAN_MSG_EXT_ID;
679 msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK);
680 } else {
681 msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK);
682 }
683
684 if (can_is_canfd_skb(skb)) {
685 /* CAN FD frame format */
686 can_dlc = can_len2dlc(cf->len);
687
688 msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
689
690 if (cf->flags & CANFD_BRS)
691 msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
692
693 if (cf->flags & CANFD_ESI)
694 msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
695 } else {
696 /* CAN 2.0 frame format */
697 can_dlc = cf->len;
698
699 if (cf->can_id & CAN_RTR_FLAG)
700 msg_flags |= PUCAN_MSG_RTR;
701 }
702
703 /* always ask loopback for echo management */
704 msg_flags |= PUCAN_MSG_LOOPED_BACK;
705
706 /* set driver specific bit to differentiate with application loopback */
707 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
708 msg_flags |= PUCAN_MSG_SELF_RECEIVE;
709
710 msg->flags = cpu_to_le16(msg_flags);
711 msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, can_dlc);
712 memcpy(msg->d, cf->data, cf->len);
713
714 /* struct msg client field is used as an index in the echo skbs ring */
715 msg->client = priv->echo_idx;
716
717 spin_lock_irqsave(&priv->echo_lock, flags);
718
719 /* prepare and save echo skb in internal slot */
720 can_put_echo_skb(skb, ndev, priv->echo_idx);
721
722 /* move echo index to the next slot */
723 priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max;
724
725 /* if next slot is not free, stop network queue (no slot free in echo
726 * skb ring means that the controller did not write these frames on
727 * the bus: no need to continue).
728 */
729 should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]);
730
8ac8321e
SG
731 /* stop network tx queue if not enough room to save one more msg too */
732 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
733 should_stop_tx_queue |= (room_left <
734 (sizeof(*msg) + CANFD_MAX_DLEN));
735 else
736 should_stop_tx_queue |= (room_left <
737 (sizeof(*msg) + CAN_MAX_DLEN));
738
739 if (should_stop_tx_queue)
740 netif_stop_queue(ndev);
741
e6048a00
SG
742 spin_unlock_irqrestore(&priv->echo_lock, flags);
743
744 /* write the skb on the interface */
745 priv->write_tx_msg(priv, msg);
746
8ac8321e
SG
747 return NETDEV_TX_OK;
748}
749
750static const struct net_device_ops peak_canfd_netdev_ops = {
751 .ndo_open = peak_canfd_open,
752 .ndo_stop = peak_canfd_close,
753 .ndo_start_xmit = peak_canfd_start_xmit,
754 .ndo_change_mtu = can_change_mtu,
755};
756
757struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index,
758 int echo_skb_max)
759{
760 struct net_device *ndev;
761 struct peak_canfd_priv *priv;
762
763 /* we DO support local echo */
764 if (echo_skb_max < 0)
765 echo_skb_max = PCANFD_ECHO_SKB_MAX;
766
767 /* allocate the candev object */
768 ndev = alloc_candev(sizeof_priv, echo_skb_max);
769 if (!ndev)
770 return NULL;
771
772 priv = netdev_priv(ndev);
773
774 /* complete now socket-can initialization side */
775 priv->can.state = CAN_STATE_STOPPED;
776 priv->can.bittiming_const = &peak_canfd_nominal_const;
777 priv->can.data_bittiming_const = &peak_canfd_data_const;
778
779 priv->can.do_set_mode = peak_canfd_set_mode;
780 priv->can.do_get_berr_counter = peak_canfd_get_berr_counter;
781 priv->can.do_set_bittiming = peak_canfd_set_bittiming;
782 priv->can.do_set_data_bittiming = peak_canfd_set_data_bittiming;
783 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
784 CAN_CTRLMODE_LISTENONLY |
785 CAN_CTRLMODE_3_SAMPLES |
786 CAN_CTRLMODE_FD |
787 CAN_CTRLMODE_FD_NON_ISO |
788 CAN_CTRLMODE_BERR_REPORTING;
789
790 priv->ndev = ndev;
791 priv->index = index;
792 priv->cmd_len = 0;
793 spin_lock_init(&priv->echo_lock);
794
795 ndev->flags |= IFF_ECHO;
796 ndev->netdev_ops = &peak_canfd_netdev_ops;
797 ndev->dev_id = index;
798
799 return ndev;
800}