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