Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / can / peak_canfd / peak_canfd.c
blob55513411a82e68e11d6b1ca30e90ea4337a0f2ee
1 /*
2 * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
3 * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
5 * Copyright (C) 2016 PEAK System-Technik GmbH
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
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.
17 #include <linux/can.h>
18 #include <linux/can/dev.h>
20 #include "peak_canfd_user.h"
22 /* internal IP core cache size (used as default echo skbs max number) */
23 #define PCANFD_ECHO_SKB_MAX 24
25 /* bittiming ranges of the PEAK-System PC CAN-FD interfaces */
26 static 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,
38 static 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,
50 static struct peak_canfd_priv *pucan_init_cmd(struct peak_canfd_priv *priv)
52 priv->cmd_len = 0;
53 return priv;
56 static void *pucan_add_cmd(struct peak_canfd_priv *priv, int cmd_op)
58 struct pucan_command *cmd;
60 if (priv->cmd_len + sizeof(*cmd) > priv->cmd_maxlen)
61 return NULL;
63 cmd = priv->cmd_buffer + priv->cmd_len;
65 /* reset all unused bit to default */
66 memset(cmd, 0, sizeof(*cmd));
68 cmd->opcode_channel = pucan_cmd_opcode_channel(priv->index, cmd_op);
69 priv->cmd_len += sizeof(*cmd);
71 return cmd;
74 static int pucan_write_cmd(struct peak_canfd_priv *priv)
76 int err;
78 if (priv->pre_cmd) {
79 err = priv->pre_cmd(priv);
80 if (err)
81 return err;
84 err = priv->write_cmd(priv);
85 if (err)
86 return err;
88 if (priv->post_cmd)
89 err = priv->post_cmd(priv);
91 return err;
94 /* uCAN commands interface functions */
95 static int pucan_set_reset_mode(struct peak_canfd_priv *priv)
97 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RESET_MODE);
98 return pucan_write_cmd(priv);
101 static int pucan_set_normal_mode(struct peak_canfd_priv *priv)
103 int err;
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;
110 return err;
113 static int pucan_set_listen_only_mode(struct peak_canfd_priv *priv)
115 int err;
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;
122 return err;
125 static int pucan_set_timing_slow(struct peak_canfd_priv *priv,
126 const struct can_bittiming *pbt)
128 struct pucan_timing_slow *cmd;
130 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_SLOW);
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));
138 cmd->ewl = 96; /* default */
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);
144 return pucan_write_cmd(priv);
147 static int pucan_set_timing_fast(struct peak_canfd_priv *priv,
148 const struct can_bittiming *pbt)
150 struct pucan_timing_fast *cmd;
152 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_FAST);
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));
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);
163 return pucan_write_cmd(priv);
166 static int pucan_set_std_filter(struct peak_canfd_priv *priv, u8 row, u32 mask)
168 struct pucan_std_filter *cmd;
170 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_STD_FILTER);
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.
176 * bit filter
177 * 1 passed
178 * 0 discarded
181 /* select the row */
182 cmd->idx = row;
184 /* set/unset bits in the row */
185 cmd->mask = cpu_to_le32(mask);
187 return pucan_write_cmd(priv);
190 static int pucan_tx_abort(struct peak_canfd_priv *priv, u16 flags)
192 struct pucan_tx_abort *cmd;
194 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TX_ABORT);
196 cmd->flags = cpu_to_le16(flags);
198 return pucan_write_cmd(priv);
201 static int pucan_clr_err_counters(struct peak_canfd_priv *priv)
203 struct pucan_wr_err_cnt *cmd;
205 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_WR_ERR_CNT);
207 cmd->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE | PUCAN_WRERRCNT_RE);
208 cmd->tx_counter = 0;
209 cmd->rx_counter = 0;
211 return pucan_write_cmd(priv);
214 static int pucan_set_options(struct peak_canfd_priv *priv, u16 opt_mask)
216 struct pucan_options *cmd;
218 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_EN_OPTION);
220 cmd->options = cpu_to_le16(opt_mask);
222 return pucan_write_cmd(priv);
225 static int pucan_clr_options(struct peak_canfd_priv *priv, u16 opt_mask)
227 struct pucan_options *cmd;
229 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_CLR_DIS_OPTION);
231 cmd->options = cpu_to_le16(opt_mask);
233 return pucan_write_cmd(priv);
236 static int pucan_setup_rx_barrier(struct peak_canfd_priv *priv)
238 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RX_BARRIER);
240 return pucan_write_cmd(priv);
243 /* handle the reception of one CAN frame */
244 static int pucan_handle_can_rx(struct peak_canfd_priv *priv,
245 struct pucan_rx_msg *msg)
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;
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));
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 unsigned long flags;
263 spin_lock_irqsave(&priv->echo_lock, flags);
264 can_get_echo_skb(priv->ndev, msg->client);
265 spin_unlock_irqrestore(&priv->echo_lock, flags);
267 /* count bytes of the echo instead of skb */
268 stats->tx_bytes += cf_len;
269 stats->tx_packets++;
271 /* restart tx queue (a slot is free) */
272 netif_wake_queue(priv->ndev);
274 return 0;
277 /* otherwise, it should be pushed into rx fifo */
278 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
279 /* CANFD frame case */
280 skb = alloc_canfd_skb(priv->ndev, &cf);
281 if (!skb)
282 return -ENOMEM;
284 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
285 cf->flags |= CANFD_BRS;
287 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
288 cf->flags |= CANFD_ESI;
289 } else {
290 /* CAN 2.0 frame case */
291 skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
292 if (!skb)
293 return -ENOMEM;
296 cf->can_id = le32_to_cpu(msg->can_id);
297 cf->len = cf_len;
299 if (rx_msg_flags & PUCAN_MSG_EXT_ID)
300 cf->can_id |= CAN_EFF_FLAG;
302 if (rx_msg_flags & PUCAN_MSG_RTR)
303 cf->can_id |= CAN_RTR_FLAG;
304 else
305 memcpy(cf->data, msg->d, cf->len);
307 stats->rx_bytes += cf->len;
308 stats->rx_packets++;
310 netif_rx(skb);
312 return 0;
315 /* handle rx/tx error counters notification */
316 static int pucan_handle_error(struct peak_canfd_priv *priv,
317 struct pucan_error_msg *msg)
319 priv->bec.txerr = msg->tx_err_cnt;
320 priv->bec.rxerr = msg->rx_err_cnt;
322 return 0;
325 /* handle status notification */
326 static int pucan_handle_status(struct peak_canfd_priv *priv,
327 struct pucan_status_msg *msg)
329 struct net_device *ndev = priv->ndev;
330 struct net_device_stats *stats = &ndev->stats;
331 struct can_frame *cf;
332 struct sk_buff *skb;
334 /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */
335 if (pucan_status_is_rx_barrier(msg)) {
336 unsigned long flags;
338 if (priv->enable_tx_path) {
339 int err = priv->enable_tx_path(priv);
341 if (err)
342 return err;
345 /* restart network queue only if echo skb array is free */
346 spin_lock_irqsave(&priv->echo_lock, flags);
348 if (!priv->can.echo_skb[priv->echo_idx]) {
349 spin_unlock_irqrestore(&priv->echo_lock, flags);
351 netif_wake_queue(ndev);
352 } else {
353 spin_unlock_irqrestore(&priv->echo_lock, flags);
356 return 0;
359 skb = alloc_can_err_skb(ndev, &cf);
361 /* test state error bits according to their priority */
362 if (pucan_status_is_busoff(msg)) {
363 netdev_dbg(ndev, "Bus-off entry status\n");
364 priv->can.state = CAN_STATE_BUS_OFF;
365 priv->can.can_stats.bus_off++;
366 can_bus_off(ndev);
367 if (skb)
368 cf->can_id |= CAN_ERR_BUSOFF;
370 } else if (pucan_status_is_passive(msg)) {
371 netdev_dbg(ndev, "Error passive status\n");
372 priv->can.state = CAN_STATE_ERROR_PASSIVE;
373 priv->can.can_stats.error_passive++;
374 if (skb) {
375 cf->can_id |= CAN_ERR_CRTL;
376 cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
377 CAN_ERR_CRTL_TX_PASSIVE :
378 CAN_ERR_CRTL_RX_PASSIVE;
379 cf->data[6] = priv->bec.txerr;
380 cf->data[7] = priv->bec.rxerr;
383 } else if (pucan_status_is_warning(msg)) {
384 netdev_dbg(ndev, "Error warning status\n");
385 priv->can.state = CAN_STATE_ERROR_WARNING;
386 priv->can.can_stats.error_warning++;
387 if (skb) {
388 cf->can_id |= CAN_ERR_CRTL;
389 cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
390 CAN_ERR_CRTL_TX_WARNING :
391 CAN_ERR_CRTL_RX_WARNING;
392 cf->data[6] = priv->bec.txerr;
393 cf->data[7] = priv->bec.rxerr;
396 } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) {
397 /* back to ERROR_ACTIVE */
398 netdev_dbg(ndev, "Error active status\n");
399 can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE,
400 CAN_STATE_ERROR_ACTIVE);
401 } else {
402 dev_kfree_skb(skb);
403 return 0;
406 if (!skb) {
407 stats->rx_dropped++;
408 return -ENOMEM;
411 stats->rx_packets++;
412 stats->rx_bytes += cf->can_dlc;
413 netif_rx(skb);
415 return 0;
418 /* handle uCAN Rx overflow notification */
419 static int pucan_handle_cache_critical(struct peak_canfd_priv *priv)
421 struct net_device_stats *stats = &priv->ndev->stats;
422 struct can_frame *cf;
423 struct sk_buff *skb;
425 stats->rx_over_errors++;
426 stats->rx_errors++;
428 skb = alloc_can_err_skb(priv->ndev, &cf);
429 if (!skb) {
430 stats->rx_dropped++;
431 return -ENOMEM;
434 cf->can_id |= CAN_ERR_CRTL;
435 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
437 cf->data[6] = priv->bec.txerr;
438 cf->data[7] = priv->bec.rxerr;
440 stats->rx_bytes += cf->can_dlc;
441 stats->rx_packets++;
442 netif_rx(skb);
444 return 0;
447 /* handle a single uCAN message */
448 int peak_canfd_handle_msg(struct peak_canfd_priv *priv,
449 struct pucan_rx_msg *msg)
451 u16 msg_type = le16_to_cpu(msg->type);
452 int msg_size = le16_to_cpu(msg->size);
453 int err;
455 if (!msg_size || !msg_type) {
456 /* null packet found: end of list */
457 goto exit;
460 switch (msg_type) {
461 case PUCAN_MSG_CAN_RX:
462 err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg);
463 break;
464 case PUCAN_MSG_ERROR:
465 err = pucan_handle_error(priv, (struct pucan_error_msg *)msg);
466 break;
467 case PUCAN_MSG_STATUS:
468 err = pucan_handle_status(priv, (struct pucan_status_msg *)msg);
469 break;
470 case PUCAN_MSG_CACHE_CRITICAL:
471 err = pucan_handle_cache_critical(priv);
472 break;
473 default:
474 err = 0;
477 if (err < 0)
478 return err;
480 exit:
481 return msg_size;
484 /* handle a list of rx_count messages from rx_msg memory address */
485 int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv,
486 struct pucan_rx_msg *msg_list, int msg_count)
488 void *msg_ptr = msg_list;
489 int i, msg_size = 0;
491 for (i = 0; i < msg_count; i++) {
492 msg_size = peak_canfd_handle_msg(priv, msg_ptr);
494 /* a null packet can be found at the end of a list */
495 if (msg_size <= 0)
496 break;
498 msg_ptr += msg_size;
501 if (msg_size < 0)
502 return msg_size;
504 return i;
507 static int peak_canfd_start(struct peak_canfd_priv *priv)
509 int err;
511 err = pucan_clr_err_counters(priv);
512 if (err)
513 goto err_exit;
515 priv->echo_idx = 0;
517 priv->bec.txerr = 0;
518 priv->bec.rxerr = 0;
520 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
521 err = pucan_set_listen_only_mode(priv);
522 else
523 err = pucan_set_normal_mode(priv);
525 err_exit:
526 return err;
529 static void peak_canfd_stop(struct peak_canfd_priv *priv)
531 int err;
533 /* go back to RESET mode */
534 err = pucan_set_reset_mode(priv);
535 if (err) {
536 netdev_err(priv->ndev, "channel %u reset failed\n",
537 priv->index);
538 } else {
539 /* abort last Tx (MUST be done in RESET mode only!) */
540 pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH);
544 static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
546 struct peak_canfd_priv *priv = netdev_priv(ndev);
548 switch (mode) {
549 case CAN_MODE_START:
550 peak_canfd_start(priv);
551 netif_wake_queue(ndev);
552 break;
553 default:
554 return -EOPNOTSUPP;
557 return 0;
560 static int peak_canfd_get_berr_counter(const struct net_device *ndev,
561 struct can_berr_counter *bec)
563 struct peak_canfd_priv *priv = netdev_priv(ndev);
565 *bec = priv->bec;
566 return 0;
569 static int peak_canfd_open(struct net_device *ndev)
571 struct peak_canfd_priv *priv = netdev_priv(ndev);
572 int i, err = 0;
574 err = open_candev(ndev);
575 if (err) {
576 netdev_err(ndev, "open_candev() failed, error %d\n", err);
577 goto err_exit;
580 err = pucan_set_reset_mode(priv);
581 if (err)
582 goto err_close;
584 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
585 if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
586 err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO);
587 else
588 err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO);
590 if (err)
591 goto err_close;
594 /* set option: get rx/tx error counters */
595 err = pucan_set_options(priv, PUCAN_OPTION_ERROR);
596 if (err)
597 goto err_close;
599 /* accept all standard CAN ID */
600 for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++)
601 pucan_set_std_filter(priv, i, 0xffffffff);
603 err = peak_canfd_start(priv);
604 if (err)
605 goto err_close;
607 /* receiving the RB status says when Tx path is ready */
608 err = pucan_setup_rx_barrier(priv);
609 if (!err)
610 goto err_exit;
612 err_close:
613 close_candev(ndev);
614 err_exit:
615 return err;
618 static int peak_canfd_set_bittiming(struct net_device *ndev)
620 struct peak_canfd_priv *priv = netdev_priv(ndev);
622 return pucan_set_timing_slow(priv, &priv->can.bittiming);
625 static int peak_canfd_set_data_bittiming(struct net_device *ndev)
627 struct peak_canfd_priv *priv = netdev_priv(ndev);
629 return pucan_set_timing_fast(priv, &priv->can.data_bittiming);
632 static int peak_canfd_close(struct net_device *ndev)
634 struct peak_canfd_priv *priv = netdev_priv(ndev);
636 netif_stop_queue(ndev);
637 peak_canfd_stop(priv);
638 close_candev(ndev);
640 return 0;
643 static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb,
644 struct net_device *ndev)
646 struct peak_canfd_priv *priv = netdev_priv(ndev);
647 struct net_device_stats *stats = &ndev->stats;
648 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
649 struct pucan_tx_msg *msg;
650 u16 msg_size, msg_flags;
651 unsigned long flags;
652 bool should_stop_tx_queue;
653 int room_left;
654 u8 can_dlc;
656 if (can_dropped_invalid_skb(ndev, skb))
657 return NETDEV_TX_OK;
659 msg_size = ALIGN(sizeof(*msg) + cf->len, 4);
660 msg = priv->alloc_tx_msg(priv, msg_size, &room_left);
662 /* should never happen except under bus-off condition and (auto-)restart
663 * mechanism
665 if (!msg) {
666 stats->tx_dropped++;
667 netif_stop_queue(ndev);
668 return NETDEV_TX_BUSY;
671 msg->size = cpu_to_le16(msg_size);
672 msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
673 msg_flags = 0;
675 if (cf->can_id & CAN_EFF_FLAG) {
676 msg_flags |= PUCAN_MSG_EXT_ID;
677 msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK);
678 } else {
679 msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK);
682 if (can_is_canfd_skb(skb)) {
683 /* CAN FD frame format */
684 can_dlc = can_len2dlc(cf->len);
686 msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
688 if (cf->flags & CANFD_BRS)
689 msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
691 if (cf->flags & CANFD_ESI)
692 msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
693 } else {
694 /* CAN 2.0 frame format */
695 can_dlc = cf->len;
697 if (cf->can_id & CAN_RTR_FLAG)
698 msg_flags |= PUCAN_MSG_RTR;
701 /* always ask loopback for echo management */
702 msg_flags |= PUCAN_MSG_LOOPED_BACK;
704 /* set driver specific bit to differentiate with application loopback */
705 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
706 msg_flags |= PUCAN_MSG_SELF_RECEIVE;
708 msg->flags = cpu_to_le16(msg_flags);
709 msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, can_dlc);
710 memcpy(msg->d, cf->data, cf->len);
712 /* struct msg client field is used as an index in the echo skbs ring */
713 msg->client = priv->echo_idx;
715 spin_lock_irqsave(&priv->echo_lock, flags);
717 /* prepare and save echo skb in internal slot */
718 can_put_echo_skb(skb, ndev, priv->echo_idx);
720 /* move echo index to the next slot */
721 priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max;
723 /* if next slot is not free, stop network queue (no slot free in echo
724 * skb ring means that the controller did not write these frames on
725 * the bus: no need to continue).
727 should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]);
729 spin_unlock_irqrestore(&priv->echo_lock, flags);
731 /* write the skb on the interface */
732 priv->write_tx_msg(priv, msg);
734 /* stop network tx queue if not enough room to save one more msg too */
735 if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
736 should_stop_tx_queue |= (room_left <
737 (sizeof(*msg) + CANFD_MAX_DLEN));
738 else
739 should_stop_tx_queue |= (room_left <
740 (sizeof(*msg) + CAN_MAX_DLEN));
742 if (should_stop_tx_queue)
743 netif_stop_queue(ndev);
745 return NETDEV_TX_OK;
748 static const struct net_device_ops peak_canfd_netdev_ops = {
749 .ndo_open = peak_canfd_open,
750 .ndo_stop = peak_canfd_close,
751 .ndo_start_xmit = peak_canfd_start_xmit,
752 .ndo_change_mtu = can_change_mtu,
755 struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index,
756 int echo_skb_max)
758 struct net_device *ndev;
759 struct peak_canfd_priv *priv;
761 /* we DO support local echo */
762 if (echo_skb_max < 0)
763 echo_skb_max = PCANFD_ECHO_SKB_MAX;
765 /* allocate the candev object */
766 ndev = alloc_candev(sizeof_priv, echo_skb_max);
767 if (!ndev)
768 return NULL;
770 priv = netdev_priv(ndev);
772 /* complete now socket-can initialization side */
773 priv->can.state = CAN_STATE_STOPPED;
774 priv->can.bittiming_const = &peak_canfd_nominal_const;
775 priv->can.data_bittiming_const = &peak_canfd_data_const;
777 priv->can.do_set_mode = peak_canfd_set_mode;
778 priv->can.do_get_berr_counter = peak_canfd_get_berr_counter;
779 priv->can.do_set_bittiming = peak_canfd_set_bittiming;
780 priv->can.do_set_data_bittiming = peak_canfd_set_data_bittiming;
781 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
782 CAN_CTRLMODE_LISTENONLY |
783 CAN_CTRLMODE_3_SAMPLES |
784 CAN_CTRLMODE_FD |
785 CAN_CTRLMODE_FD_NON_ISO |
786 CAN_CTRLMODE_BERR_REPORTING;
788 priv->ndev = ndev;
789 priv->index = index;
790 priv->cmd_len = 0;
791 spin_lock_init(&priv->echo_lock);
793 ndev->flags |= IFF_ECHO;
794 ndev->netdev_ops = &peak_canfd_netdev_ops;
795 ndev->dev_id = index;
797 return ndev;