1 // SPDX-License-Identifier: GPL-2.0
3 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
5 * File es58x_core.c: Core logic to manage the network devices and the
8 * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
9 * Copyright (c) 2020 ETAS K.K.. All rights reserved.
10 * Copyright (c) 2020-2022 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
13 #include <asm/unaligned.h>
14 #include <linux/crc16.h>
15 #include <linux/ethtool.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <net/devlink.h>
21 #include "es58x_core.h"
23 MODULE_AUTHOR("Vincent Mailhol <mailhol.vincent@wanadoo.fr>");
24 MODULE_AUTHOR("Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>");
25 MODULE_DESCRIPTION("Socket CAN driver for ETAS ES58X USB adapters");
26 MODULE_LICENSE("GPL v2");
28 #define ES58X_VENDOR_ID 0x108C
29 #define ES581_4_PRODUCT_ID 0x0159
30 #define ES582_1_PRODUCT_ID 0x0168
31 #define ES584_1_PRODUCT_ID 0x0169
33 /* ES58X FD has some interface protocols unsupported by this driver. */
34 #define ES58X_FD_INTERFACE_PROTOCOL 0
36 /* Table of devices which work with this driver. */
37 static const struct usb_device_id es58x_id_table[] = {
39 /* ETAS GmbH ES581.4 USB dual-channel CAN Bus Interface module. */
40 USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID),
41 .driver_info = ES58X_DUAL_CHANNEL
43 /* ETAS GmbH ES582.1 USB dual-channel CAN FD Bus Interface module. */
44 USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES582_1_PRODUCT_ID,
45 ES58X_FD_INTERFACE_PROTOCOL),
46 .driver_info = ES58X_DUAL_CHANNEL | ES58X_FD_FAMILY
48 /* ETAS GmbH ES584.1 USB single-channel CAN FD Bus Interface module. */
49 USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES584_1_PRODUCT_ID,
50 ES58X_FD_INTERFACE_PROTOCOL),
51 .driver_info = ES58X_FD_FAMILY
53 /* Terminating entry */
57 MODULE_DEVICE_TABLE(usb, es58x_id_table);
59 #define es58x_print_hex_dump(buf, len) \
60 print_hex_dump(KERN_DEBUG, \
61 KBUILD_MODNAME " " __stringify(buf) ": ", \
62 DUMP_PREFIX_NONE, 16, 1, buf, len, false)
64 #define es58x_print_hex_dump_debug(buf, len) \
65 print_hex_dump_debug(KBUILD_MODNAME " " __stringify(buf) ": ",\
66 DUMP_PREFIX_NONE, 16, 1, buf, len, false)
68 /* The last two bytes of an ES58X command is a CRC16. The first two
69 * bytes (the start of frame) are skipped and the CRC calculation
70 * starts on the third byte.
72 #define ES58X_CRC_CALC_OFFSET sizeof_field(union es58x_urb_cmd, sof)
75 * es58x_calculate_crc() - Compute the crc16 of a given URB.
76 * @urb_cmd: The URB command for which we want to calculate the CRC.
77 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
78 * (ES58X_CRC_CALC_OFFSET + sizeof(crc))
80 * Return: crc16 value.
82 static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
85 ssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc);
87 crc = crc16(0, &urb_cmd->raw_cmd[ES58X_CRC_CALC_OFFSET], len);
92 * es58x_get_crc() - Get the CRC value of a given URB.
93 * @urb_cmd: The URB command for which we want to get the CRC.
94 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
95 * (ES58X_CRC_CALC_OFFSET + sizeof(crc))
97 * Return: crc16 value.
99 static u16 es58x_get_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
102 const __le16 *crc_addr;
104 crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
105 crc = get_unaligned_le16(crc_addr);
110 * es58x_set_crc() - Set the CRC value of a given URB.
111 * @urb_cmd: The URB command for which we want to get the CRC.
112 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
113 * (ES58X_CRC_CALC_OFFSET + sizeof(crc))
115 static void es58x_set_crc(union es58x_urb_cmd *urb_cmd, u16 urb_len)
120 crc = es58x_calculate_crc(urb_cmd, urb_len);
121 crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
122 put_unaligned_le16(crc, crc_addr);
126 * es58x_check_crc() - Validate the CRC value of a given URB.
127 * @es58x_dev: ES58X device.
128 * @urb_cmd: The URB command for which we want to check the CRC.
129 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4
130 * (ES58X_CRC_CALC_OFFSET + sizeof(crc))
132 * Return: zero on success, -EBADMSG if the CRC check fails.
134 static int es58x_check_crc(struct es58x_device *es58x_dev,
135 const union es58x_urb_cmd *urb_cmd, u16 urb_len)
137 u16 calculated_crc = es58x_calculate_crc(urb_cmd, urb_len);
138 u16 expected_crc = es58x_get_crc(urb_cmd, urb_len);
140 if (expected_crc != calculated_crc) {
141 dev_err_ratelimited(es58x_dev->dev,
142 "%s: Bad CRC, urb_len: %d\n",
151 * es58x_timestamp_to_ns() - Convert a timestamp value received from a
152 * ES58X device to nanoseconds.
153 * @timestamp: Timestamp received from a ES58X device.
155 * The timestamp received from ES58X is expressed in multiples of 0.5
156 * micro seconds. This function converts it in to nanoseconds.
158 * Return: Timestamp value in nanoseconds.
160 static u64 es58x_timestamp_to_ns(u64 timestamp)
162 const u64 es58x_timestamp_ns_mult_coef = 500ULL;
164 return es58x_timestamp_ns_mult_coef * timestamp;
168 * es58x_set_skb_timestamp() - Set the hardware timestamp of an skb.
169 * @netdev: CAN network device.
170 * @skb: socket buffer of a CAN message.
171 * @timestamp: Timestamp received from an ES58X device.
173 * Used for both received and echo messages.
175 static void es58x_set_skb_timestamp(struct net_device *netdev,
176 struct sk_buff *skb, u64 timestamp)
178 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
179 struct skb_shared_hwtstamps *hwts;
181 hwts = skb_hwtstamps(skb);
182 /* Ignoring overflow (overflow on 64 bits timestamp with nano
183 * second precision would occur after more than 500 years).
185 hwts->hwtstamp = ns_to_ktime(es58x_timestamp_to_ns(timestamp) +
186 es58x_dev->realtime_diff_ns);
190 * es58x_rx_timestamp() - Handle a received timestamp.
191 * @es58x_dev: ES58X device.
192 * @timestamp: Timestamp received from a ES58X device.
194 * Calculate the difference between the ES58X device and the kernel
195 * internal clocks. This difference will be later used as an offset to
196 * convert the timestamps of RX and echo messages to match the kernel
197 * system time (e.g. convert to UNIX time).
199 void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp)
201 u64 ktime_real_ns = ktime_get_real_ns();
202 u64 device_timestamp = es58x_timestamp_to_ns(timestamp);
204 dev_dbg(es58x_dev->dev, "%s: request round-trip time: %llu ns\n",
205 __func__, ktime_real_ns - es58x_dev->ktime_req_ns);
207 es58x_dev->realtime_diff_ns =
208 (es58x_dev->ktime_req_ns + ktime_real_ns) / 2 - device_timestamp;
209 es58x_dev->ktime_req_ns = 0;
211 dev_dbg(es58x_dev->dev,
212 "%s: Device timestamp: %llu, diff with kernel: %llu\n",
213 __func__, device_timestamp, es58x_dev->realtime_diff_ns);
217 * es58x_set_realtime_diff_ns() - Calculate difference between the
218 * clocks of the ES58X device and the kernel
219 * @es58x_dev: ES58X device.
221 * Request a timestamp from the ES58X device. Once the answer is
222 * received, the timestamp difference will be set by the callback
223 * function es58x_rx_timestamp().
225 * Return: zero on success, errno when any error occurs.
227 static int es58x_set_realtime_diff_ns(struct es58x_device *es58x_dev)
229 if (es58x_dev->ktime_req_ns) {
230 dev_warn(es58x_dev->dev,
231 "%s: Previous request to set timestamp has not completed yet\n",
236 es58x_dev->ktime_req_ns = ktime_get_real_ns();
237 return es58x_dev->ops->get_timestamp(es58x_dev);
241 * es58x_is_can_state_active() - Is the network device in an active
243 * @netdev: CAN network device.
245 * The device is considered active if it is able to send or receive
246 * CAN frames, that is to say if it is in any of
247 * CAN_STATE_ERROR_ACTIVE, CAN_STATE_ERROR_WARNING or
248 * CAN_STATE_ERROR_PASSIVE states.
250 * Caution: when recovering from a bus-off,
251 * net/core/dev.c#can_restart() will call
252 * net/core/dev.c#can_flush_echo_skb() without using any kind of
253 * locks. For this reason, it is critical to guarantee that no TX or
254 * echo operations (i.e. any access to priv->echo_skb[]) can be done
255 * while this function is returning false.
257 * Return: true if the device is active, else returns false.
259 static bool es58x_is_can_state_active(struct net_device *netdev)
261 return es58x_priv(netdev)->can.state < CAN_STATE_BUS_OFF;
265 * es58x_is_echo_skb_threshold_reached() - Determine the limit of how
266 * many skb slots can be taken before we should stop the network
268 * @priv: ES58X private parameters related to the network device.
270 * We need to save enough free skb slots in order to be able to do
271 * bulk send. This function can be used to determine when to wake or
272 * stop the network queue in regard to the number of skb slots already
273 * taken if the echo FIFO.
277 static bool es58x_is_echo_skb_threshold_reached(struct es58x_priv *priv)
279 u32 num_echo_skb = priv->tx_head - priv->tx_tail;
280 u32 threshold = priv->can.echo_skb_max -
281 priv->es58x_dev->param->tx_bulk_max + 1;
283 return num_echo_skb >= threshold;
287 * es58x_can_free_echo_skb_tail() - Remove the oldest echo skb of the
289 * @netdev: CAN network device.
291 * Naming convention: the tail is the beginning of the FIFO, i.e. the
292 * first skb to have entered the FIFO.
294 static void es58x_can_free_echo_skb_tail(struct net_device *netdev)
296 struct es58x_priv *priv = es58x_priv(netdev);
297 u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
298 unsigned int frame_len = 0;
300 can_free_echo_skb(netdev, priv->tx_tail & fifo_mask, &frame_len);
301 netdev_completed_queue(netdev, 1, frame_len);
305 netdev->stats.tx_dropped++;
309 * es58x_can_get_echo_skb_recovery() - Try to re-sync the echo FIFO.
310 * @netdev: CAN network device.
311 * @rcv_packet_idx: Index
313 * This function should not be called under normal circumstances. In
314 * the unlikely case that one or several URB packages get dropped by
315 * the device, the index will get out of sync. Try to recover by
316 * dropping the echo skb packets with older indexes.
318 * Return: zero if recovery was successful, -EINVAL otherwise.
320 static int es58x_can_get_echo_skb_recovery(struct net_device *netdev,
323 struct es58x_priv *priv = es58x_priv(netdev);
326 netdev->stats.tx_errors++;
330 "Bad echo packet index: %u. First index: %u, end index %u, num_echo_skb: %02u/%02u\n",
331 rcv_packet_idx, priv->tx_tail, priv->tx_head,
332 priv->tx_head - priv->tx_tail,
333 priv->can.echo_skb_max);
335 if ((s32)(rcv_packet_idx - priv->tx_tail) < 0) {
338 "Received echo index is from the past. Ignoring it\n");
340 } else if ((s32)(rcv_packet_idx - priv->tx_head) >= 0) {
343 "Received echo index is from the future. Ignoring it\n");
348 "Recovery: dropping %u echo skb from index %u to %u\n",
349 rcv_packet_idx - priv->tx_tail,
350 priv->tx_tail, rcv_packet_idx - 1);
351 while (priv->tx_tail != rcv_packet_idx) {
352 if (priv->tx_tail == priv->tx_head)
354 es58x_can_free_echo_skb_tail(netdev);
361 * es58x_can_get_echo_skb() - Get the skb from the echo FIFO and loop
363 * @netdev: CAN network device.
364 * @rcv_packet_idx: Index of the first packet received from the device.
365 * @tstamps: Array of hardware timestamps received from a ES58X device.
366 * @pkts: Number of packets (and so, length of @tstamps).
368 * Callback function for when we receive a self reception
369 * acknowledgment. Retrieves the skb from the echo FIFO, sets its
370 * hardware timestamp (the actual time it was sent) and loops it back
373 * The device has to be active (i.e. network interface UP and not in
374 * bus off state or restarting).
376 * Packet indexes must be consecutive (i.e. index of first packet is
377 * @rcv_packet_idx, index of second packet is @rcv_packet_idx + 1 and
378 * index of last packet is @rcv_packet_idx + @pkts - 1).
380 * Return: zero on success.
382 int es58x_can_get_echo_skb(struct net_device *netdev, u32 rcv_packet_idx,
383 u64 *tstamps, unsigned int pkts)
385 struct es58x_priv *priv = es58x_priv(netdev);
386 unsigned int rx_total_frame_len = 0;
387 unsigned int num_echo_skb = priv->tx_head - priv->tx_tail;
389 u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
391 if (!netif_running(netdev)) {
394 "%s: %s is down, dropping %d echo packets\n",
395 __func__, netdev->name, pkts);
396 netdev->stats.tx_dropped += pkts;
398 } else if (!es58x_is_can_state_active(netdev)) {
401 "Bus is off or device is restarting. Ignoring %u echo packets from index %u\n",
402 pkts, rcv_packet_idx);
403 /* stats.tx_dropped will be (or was already)
405 * drivers/net/can/net/dev.c:can_flush_echo_skb().
408 } else if (num_echo_skb == 0) {
411 "Received %u echo packets from index: %u but echo skb queue is empty.\n",
412 pkts, rcv_packet_idx);
413 netdev->stats.tx_dropped += pkts;
417 if (priv->tx_tail != rcv_packet_idx) {
418 if (es58x_can_get_echo_skb_recovery(netdev, rcv_packet_idx) < 0) {
421 "Could not find echo skb for echo packet index: %u\n",
426 if (num_echo_skb < pkts) {
427 int pkts_drop = pkts - num_echo_skb;
431 "Received %u echo packets but have only %d echo skb. Dropping %d echo skb\n",
432 pkts, num_echo_skb, pkts_drop);
433 netdev->stats.tx_dropped += pkts_drop;
437 for (i = 0; i < pkts; i++) {
438 unsigned int skb_idx = priv->tx_tail & fifo_mask;
439 struct sk_buff *skb = priv->can.echo_skb[skb_idx];
440 unsigned int frame_len = 0;
443 es58x_set_skb_timestamp(netdev, skb, tstamps[i]);
445 netdev->stats.tx_bytes += can_get_echo_skb(netdev, skb_idx,
447 rx_total_frame_len += frame_len;
452 netdev_completed_queue(netdev, pkts, rx_total_frame_len);
453 netdev->stats.tx_packets += pkts;
455 priv->err_passive_before_rtx_success = 0;
456 if (!es58x_is_echo_skb_threshold_reached(priv))
457 netif_wake_queue(netdev);
463 * es58x_can_reset_echo_fifo() - Reset the echo FIFO.
464 * @netdev: CAN network device.
466 * The echo_skb array of struct can_priv will be flushed by
467 * drivers/net/can/dev.c:can_flush_echo_skb(). This function resets
468 * the parameters of the struct es58x_priv of our device and reset the
471 static void es58x_can_reset_echo_fifo(struct net_device *netdev)
473 struct es58x_priv *priv = es58x_priv(netdev);
478 priv->err_passive_before_rtx_success = 0;
479 netdev_reset_queue(netdev);
483 * es58x_flush_pending_tx_msg() - Reset the buffer for transmission messages.
484 * @netdev: CAN network device.
486 * es58x_start_xmit() will queue up to tx_bulk_max messages in
487 * &tx_urb buffer and do a bulk send of all messages in one single URB
488 * (c.f. xmit_more flag). When the device recovers from a bus off
489 * state or when the device stops, the tx_urb buffer might still have
490 * pending messages in it and thus need to be flushed.
492 static void es58x_flush_pending_tx_msg(struct net_device *netdev)
494 struct es58x_priv *priv = es58x_priv(netdev);
495 struct es58x_device *es58x_dev = priv->es58x_dev;
498 netdev_warn(netdev, "%s: dropping %d TX messages\n",
499 __func__, priv->tx_can_msg_cnt);
500 netdev->stats.tx_dropped += priv->tx_can_msg_cnt;
501 while (priv->tx_can_msg_cnt > 0) {
502 unsigned int frame_len = 0;
503 u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
506 priv->tx_can_msg_cnt--;
507 can_free_echo_skb(netdev, priv->tx_head & fifo_mask,
509 netdev_completed_queue(netdev, 1, frame_len);
511 usb_anchor_urb(priv->tx_urb, &priv->es58x_dev->tx_urbs_idle);
512 atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
513 usb_free_urb(priv->tx_urb);
519 * es58x_tx_ack_msg() - Handle acknowledgment messages.
520 * @netdev: CAN network device.
521 * @tx_free_entries: Number of free entries in the device transmit FIFO.
522 * @rx_cmd_ret_u32: error code as returned by the ES58X device.
524 * ES58X sends an acknowledgment message after a transmission request
525 * is done. This is mandatory for the ES581.4 but is optional (and
526 * deactivated in this driver) for the ES58X_FD family.
528 * Under normal circumstances, this function should never throw an
531 * Return: zero on success, errno when any error occurs.
533 int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
534 enum es58x_ret_u32 rx_cmd_ret_u32)
536 struct es58x_priv *priv = es58x_priv(netdev);
538 if (tx_free_entries <= priv->es58x_dev->param->tx_bulk_max) {
541 "Only %d entries left in device queue, num_echo_skb: %d/%d\n",
543 priv->tx_head - priv->tx_tail,
544 priv->can.echo_skb_max);
545 netif_stop_queue(netdev);
548 return es58x_rx_cmd_ret_u32(netdev, ES58X_RET_TYPE_TX_MSG,
553 * es58x_rx_can_msg() - Handle a received a CAN message.
554 * @netdev: CAN network device.
555 * @timestamp: Hardware time stamp (only relevant in rx branches).
556 * @data: CAN payload.
558 * @es58x_flags: Please refer to enum es58x_flag.
559 * @dlc: Data Length Code (raw value).
561 * Fill up a CAN skb and post it.
563 * This function handles the case where the DLC of a classical CAN
564 * frame is greater than CAN_MAX_DLEN (c.f. the len8_dlc field of
567 * Return: zero on success.
569 int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data,
570 canid_t can_id, enum es58x_flag es58x_flags, u8 dlc)
572 struct canfd_frame *cfd;
573 struct can_frame *ccf;
576 bool is_can_fd = !!(es58x_flags & ES58X_FLAG_FD_DATA);
578 if (dlc > CAN_MAX_RAW_DLC) {
580 "%s: DLC is %d but maximum should be %d\n",
581 __func__, dlc, CAN_MAX_RAW_DLC);
586 len = can_fd_dlc2len(dlc);
587 skb = alloc_canfd_skb(netdev, &cfd);
589 len = can_cc_dlc2len(dlc);
590 skb = alloc_can_skb(netdev, &ccf);
591 cfd = (struct canfd_frame *)ccf;
594 netdev->stats.rx_dropped++;
598 cfd->can_id = can_id;
599 if (es58x_flags & ES58X_FLAG_EFF)
600 cfd->can_id |= CAN_EFF_FLAG;
603 if (es58x_flags & ES58X_FLAG_FD_BRS)
604 cfd->flags |= CANFD_BRS;
605 if (es58x_flags & ES58X_FLAG_FD_ESI)
606 cfd->flags |= CANFD_ESI;
608 can_frame_set_cc_len(ccf, dlc, es58x_priv(netdev)->can.ctrlmode);
609 if (es58x_flags & ES58X_FLAG_RTR) {
610 ccf->can_id |= CAN_RTR_FLAG;
614 memcpy(cfd->data, data, len);
615 netdev->stats.rx_packets++;
616 netdev->stats.rx_bytes += len;
618 es58x_set_skb_timestamp(netdev, skb, timestamp);
621 es58x_priv(netdev)->err_passive_before_rtx_success = 0;
627 * es58x_rx_err_msg() - Handle a received CAN event or error message.
628 * @netdev: CAN network device.
629 * @error: Error code.
630 * @event: Event code.
631 * @timestamp: Timestamp received from a ES58X device.
633 * Handle the errors and events received by the ES58X device, create
634 * a CAN error skb and post it.
636 * In some rare cases the devices might get stuck alternating between
637 * CAN_STATE_ERROR_PASSIVE and CAN_STATE_ERROR_WARNING. To prevent
638 * this behavior, we force a bus off state if the device goes in
639 * CAN_STATE_ERROR_WARNING for ES58X_MAX_CONSECUTIVE_WARN consecutive
640 * times with no successful transmission or reception in between.
642 * Once the device is in bus off state, the only way to restart it is
643 * through the drivers/net/can/dev.c:can_restart() function. The
644 * device is technically capable to recover by itself under certain
645 * circumstances, however, allowing self recovery would create
646 * complex race conditions with drivers/net/can/dev.c:can_restart()
647 * and thus was not implemented. To activate automatic restart, please
648 * set the restart-ms parameter (e.g. ip link set can0 type can
651 * If the bus is really instable, this function would try to send a
652 * lot of log messages. Those are rate limited (i.e. you will see
653 * messages such as "net_ratelimit: XXX callbacks suppressed" in
656 * Return: zero on success, errno when any error occurs.
658 int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
659 enum es58x_event event, u64 timestamp)
661 struct es58x_priv *priv = es58x_priv(netdev);
662 struct can_priv *can = netdev_priv(netdev);
663 struct can_device_stats *can_stats = &can->can_stats;
664 struct can_frame *cf = NULL;
668 if (!netif_running(netdev)) {
670 netdev_info(netdev, "%s: %s is down, dropping packet\n",
671 __func__, netdev->name);
672 netdev->stats.rx_dropped++;
676 if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {
677 netdev_err(netdev, "%s: Both error and event are zero\n",
682 skb = alloc_can_err_skb(netdev, &cf);
685 case ES58X_ERR_OK: /* 0: No error */
688 case ES58X_ERR_PROT_STUFF:
690 netdev_dbg(netdev, "Error BITSTUFF\n");
692 cf->data[2] |= CAN_ERR_PROT_STUFF;
695 case ES58X_ERR_PROT_FORM:
697 netdev_dbg(netdev, "Error FORMAT\n");
699 cf->data[2] |= CAN_ERR_PROT_FORM;
704 netdev_dbg(netdev, "Error ACK\n");
706 cf->can_id |= CAN_ERR_ACK;
709 case ES58X_ERR_PROT_BIT:
711 netdev_dbg(netdev, "Error BIT\n");
713 cf->data[2] |= CAN_ERR_PROT_BIT;
716 case ES58X_ERR_PROT_CRC:
718 netdev_dbg(netdev, "Error CRC\n");
720 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
723 case ES58X_ERR_PROT_BIT1:
726 "Error: expected a recessive bit but monitored a dominant one\n");
728 cf->data[2] |= CAN_ERR_PROT_BIT1;
731 case ES58X_ERR_PROT_BIT0:
734 "Error expected a dominant bit but monitored a recessive one\n");
736 cf->data[2] |= CAN_ERR_PROT_BIT0;
739 case ES58X_ERR_PROT_OVERLOAD:
741 netdev_dbg(netdev, "Error OVERLOAD\n");
743 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
746 case ES58X_ERR_PROT_UNSPEC:
748 netdev_dbg(netdev, "Unspecified error\n");
750 cf->can_id |= CAN_ERR_PROT;
756 "%s: Unspecified error code 0x%04X\n",
757 __func__, (int)error);
759 cf->can_id |= CAN_ERR_PROT;
764 case ES58X_EVENT_OK: /* 0: No event */
767 case ES58X_EVENT_CRTL_ACTIVE:
768 if (can->state == CAN_STATE_BUS_OFF) {
770 "%s: state transition: BUS OFF -> ACTIVE\n",
774 netdev_dbg(netdev, "Event CAN BUS ACTIVE\n");
776 cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
777 can->state = CAN_STATE_ERROR_ACTIVE;
780 case ES58X_EVENT_CRTL_PASSIVE:
782 netdev_dbg(netdev, "Event CAN BUS PASSIVE\n");
783 /* Either TX or RX error count reached passive state
784 * but we do not know which. Setting both flags by
788 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
789 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
791 if (can->state < CAN_STATE_BUS_OFF)
792 can->state = CAN_STATE_ERROR_PASSIVE;
793 can_stats->error_passive++;
794 if (priv->err_passive_before_rtx_success < U8_MAX)
795 priv->err_passive_before_rtx_success++;
798 case ES58X_EVENT_CRTL_WARNING:
800 netdev_dbg(netdev, "Event CAN BUS WARNING\n");
801 /* Either TX or RX error count reached warning state
802 * but we do not know which. Setting both flags by
806 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
807 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
809 if (can->state < CAN_STATE_BUS_OFF)
810 can->state = CAN_STATE_ERROR_WARNING;
811 can_stats->error_warning++;
814 case ES58X_EVENT_BUSOFF:
816 netdev_dbg(netdev, "Event CAN BUS OFF\n");
818 cf->can_id |= CAN_ERR_BUSOFF;
819 can_stats->bus_off++;
820 netif_stop_queue(netdev);
821 if (can->state != CAN_STATE_BUS_OFF) {
822 can->state = CAN_STATE_BUS_OFF;
824 ret = can->do_set_mode(netdev, CAN_MODE_STOP);
828 case ES58X_EVENT_SINGLE_WIRE:
831 "Lost connection on either CAN high or CAN low\n");
832 /* Lost connection on either CAN high or CAN
833 * low. Setting both flags by default.
836 cf->data[4] |= CAN_ERR_TRX_CANH_NO_WIRE;
837 cf->data[4] |= CAN_ERR_TRX_CANL_NO_WIRE;
844 "%s: Unspecified event code 0x%04X\n",
845 __func__, (int)event);
847 cf->can_id |= CAN_ERR_CRTL;
853 cf->can_id |= CAN_ERR_CRTL;
854 if (cf->data[2] || cf->data[3]) {
855 cf->can_id |= CAN_ERR_PROT;
856 can_stats->bus_error++;
859 cf->can_id |= CAN_ERR_TRX;
861 es58x_set_skb_timestamp(netdev, skb, timestamp);
865 if ((event & ES58X_EVENT_CRTL_PASSIVE) &&
866 priv->err_passive_before_rtx_success == ES58X_CONSECUTIVE_ERR_PASSIVE_MAX) {
868 "Got %d consecutive warning events with no successful RX or TX. Forcing bus-off\n",
869 priv->err_passive_before_rtx_success);
870 return es58x_rx_err_msg(netdev, ES58X_ERR_OK,
871 ES58X_EVENT_BUSOFF, timestamp);
878 * es58x_cmd_ret_desc() - Convert a command type to a string.
879 * @cmd_ret_type: Type of the command which triggered the return code.
881 * The final line (return "<unknown>") should not be reached. If this
882 * is the case, there is an implementation bug.
884 * Return: a readable description of the @cmd_ret_type.
886 static const char *es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type)
888 switch (cmd_ret_type) {
889 case ES58X_RET_TYPE_SET_BITTIMING:
890 return "Set bittiming";
891 case ES58X_RET_TYPE_ENABLE_CHANNEL:
892 return "Enable channel";
893 case ES58X_RET_TYPE_DISABLE_CHANNEL:
894 return "Disable channel";
895 case ES58X_RET_TYPE_TX_MSG:
896 return "Transmit message";
897 case ES58X_RET_TYPE_RESET_RX:
899 case ES58X_RET_TYPE_RESET_TX:
901 case ES58X_RET_TYPE_DEVICE_ERR:
902 return "Device error";
909 * es58x_rx_cmd_ret_u8() - Handle the command's return code received
910 * from the ES58X device.
911 * @dev: Device, only used for the dev_XXX() print functions.
912 * @cmd_ret_type: Type of the command which triggered the return code.
913 * @rx_cmd_ret_u8: Command error code as returned by the ES58X device.
915 * Handles the 8 bits command return code. Those are specific to the
916 * ES581.4 device. The return value will eventually be used by
917 * es58x_handle_urb_cmd() function which will take proper actions in
918 * case of critical issues such and memory errors or bad CRC values.
920 * In contrast with es58x_rx_cmd_ret_u32(), the network device is
923 * Return: zero on success, return errno when any error occurs.
925 int es58x_rx_cmd_ret_u8(struct device *dev,
926 enum es58x_ret_type cmd_ret_type,
927 enum es58x_ret_u8 rx_cmd_ret_u8)
929 const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
931 switch (rx_cmd_ret_u8) {
932 case ES58X_RET_U8_OK:
933 dev_dbg_ratelimited(dev, "%s: OK\n", ret_desc);
936 case ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE:
937 dev_err(dev, "%s: unspecified failure\n", ret_desc);
940 case ES58X_RET_U8_ERR_NO_MEM:
941 dev_err(dev, "%s: device ran out of memory\n", ret_desc);
944 case ES58X_RET_U8_ERR_BAD_CRC:
945 dev_err(dev, "%s: CRC of previous command is incorrect\n",
950 dev_err(dev, "%s: returned unknown value: 0x%02X\n",
951 ret_desc, rx_cmd_ret_u8);
957 * es58x_rx_cmd_ret_u32() - Handle the command return code received
958 * from the ES58X device.
959 * @netdev: CAN network device.
960 * @cmd_ret_type: Type of the command which triggered the return code.
961 * @rx_cmd_ret_u32: error code as returned by the ES58X device.
963 * Handles the 32 bits command return code. The return value will
964 * eventually be used by es58x_handle_urb_cmd() function which will
965 * take proper actions in case of critical issues such and memory
966 * errors or bad CRC values.
968 * Return: zero on success, errno when any error occurs.
970 int es58x_rx_cmd_ret_u32(struct net_device *netdev,
971 enum es58x_ret_type cmd_ret_type,
972 enum es58x_ret_u32 rx_cmd_ret_u32)
974 struct es58x_priv *priv = es58x_priv(netdev);
975 const struct es58x_operators *ops = priv->es58x_dev->ops;
976 const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
978 switch (rx_cmd_ret_u32) {
979 case ES58X_RET_U32_OK:
980 switch (cmd_ret_type) {
981 case ES58X_RET_TYPE_ENABLE_CHANNEL:
982 es58x_can_reset_echo_fifo(netdev);
983 priv->can.state = CAN_STATE_ERROR_ACTIVE;
984 netif_wake_queue(netdev);
986 "%s: %s (Serial Number %s): CAN%d channel becomes ready\n",
987 ret_desc, priv->es58x_dev->udev->product,
988 priv->es58x_dev->udev->serial,
989 priv->channel_idx + 1);
992 case ES58X_RET_TYPE_TX_MSG:
993 if (IS_ENABLED(CONFIG_VERBOSE_DEBUG) && net_ratelimit())
994 netdev_vdbg(netdev, "%s: OK\n", ret_desc);
998 netdev_dbg(netdev, "%s: OK\n", ret_desc);
1003 case ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE:
1004 if (cmd_ret_type == ES58X_RET_TYPE_ENABLE_CHANNEL) {
1008 "%s: channel is already opened, closing and re-opening it to reflect new configuration\n",
1010 ret = ops->disable_channel(es58x_priv(netdev));
1013 return ops->enable_channel(es58x_priv(netdev));
1015 if (cmd_ret_type == ES58X_RET_TYPE_DISABLE_CHANNEL) {
1017 "%s: channel is already closed\n", ret_desc);
1021 "%s: unspecified failure\n", ret_desc);
1024 case ES58X_RET_U32_ERR_NO_MEM:
1025 netdev_err(netdev, "%s: device ran out of memory\n", ret_desc);
1028 case ES58X_RET_U32_WARN_PARAM_ADJUSTED:
1030 "%s: some incompatible parameters have been adjusted\n",
1034 case ES58X_RET_U32_WARN_TX_MAYBE_REORDER:
1036 "%s: TX messages might have been reordered\n",
1040 case ES58X_RET_U32_ERR_TIMEDOUT:
1041 netdev_err(netdev, "%s: command timed out\n", ret_desc);
1044 case ES58X_RET_U32_ERR_FIFO_FULL:
1045 netdev_warn(netdev, "%s: fifo is full\n", ret_desc);
1048 case ES58X_RET_U32_ERR_BAD_CONFIG:
1049 netdev_err(netdev, "%s: bad configuration\n", ret_desc);
1052 case ES58X_RET_U32_ERR_NO_RESOURCE:
1053 netdev_err(netdev, "%s: no resource available\n", ret_desc);
1057 netdev_err(netdev, "%s returned unknown value: 0x%08X\n",
1058 ret_desc, rx_cmd_ret_u32);
1064 * es58x_increment_rx_errors() - Increment the network devices' error
1066 * @es58x_dev: ES58X device.
1068 * If an error occurs on the early stages on receiving an URB command,
1069 * we might not be able to figure out on which network device the
1070 * error occurred. In such case, we arbitrarily increment the error
1071 * count of all the network devices attached to our ES58X device.
1073 static void es58x_increment_rx_errors(struct es58x_device *es58x_dev)
1077 for (i = 0; i < es58x_dev->num_can_ch; i++)
1078 if (es58x_dev->netdev[i])
1079 es58x_dev->netdev[i]->stats.rx_errors++;
1083 * es58x_handle_urb_cmd() - Handle the URB command
1084 * @es58x_dev: ES58X device.
1085 * @urb_cmd: The URB command received from the ES58X device, might not
1088 * Sends the URB command to the device specific function. Manages the
1089 * errors thrown back by those functions.
1091 static void es58x_handle_urb_cmd(struct es58x_device *es58x_dev,
1092 const union es58x_urb_cmd *urb_cmd)
1094 const struct es58x_operators *ops = es58x_dev->ops;
1098 ret = ops->handle_urb_cmd(es58x_dev, urb_cmd);
1104 dev_err_ratelimited(es58x_dev->dev, "Device is not ready\n");
1113 cmd_len = es58x_get_urb_cmd_len(es58x_dev,
1114 ops->get_msg_len(urb_cmd));
1115 dev_err(es58x_dev->dev,
1116 "ops->handle_urb_cmd() returned error %pe",
1118 es58x_print_hex_dump(urb_cmd, cmd_len);
1125 dev_crit(es58x_dev->dev,
1126 "ops->handle_urb_cmd() returned error %pe, detaching all network devices\n",
1128 for (i = 0; i < es58x_dev->num_can_ch; i++)
1129 if (es58x_dev->netdev[i])
1130 netif_device_detach(es58x_dev->netdev[i]);
1131 if (es58x_dev->ops->reset_device)
1132 es58x_dev->ops->reset_device(es58x_dev);
1136 /* Because the urb command could not fully be parsed,
1137 * channel_id is not confirmed. Incrementing rx_errors count
1140 es58x_increment_rx_errors(es58x_dev);
1144 * es58x_check_rx_urb() - Check the length and format of the URB command.
1145 * @es58x_dev: ES58X device.
1146 * @urb_cmd: The URB command received from the ES58X device, might not
1148 * @urb_actual_len: The actual length of the URB command.
1150 * Check if the first message of the received urb is valid, that is to
1151 * say that both the header and the length are coherent.
1154 * the length of the first message of the URB on success.
1156 * -ENODATA if the URB command is incomplete (in which case, the URB
1157 * command should be buffered and combined with the next URB to try to
1158 * reconstitute the URB command).
1160 * -EOVERFLOW if the length is bigger than the maximum expected one.
1162 * -EBADRQC if the start of frame does not match the expected value.
1164 static signed int es58x_check_rx_urb(struct es58x_device *es58x_dev,
1165 const union es58x_urb_cmd *urb_cmd,
1168 const struct device *dev = es58x_dev->dev;
1169 const struct es58x_parameters *param = es58x_dev->param;
1171 signed int urb_cmd_len, ret;
1173 if (urb_actual_len < param->urb_cmd_header_len) {
1175 "%s: Received %d bytes [%*ph]: header incomplete\n",
1176 __func__, urb_actual_len, urb_actual_len,
1181 sof = get_unaligned_le16(&urb_cmd->sof);
1182 if (sof != param->rx_start_of_frame) {
1183 dev_err_ratelimited(es58x_dev->dev,
1184 "%s: Expected sequence 0x%04X for start of frame but got 0x%04X.\n",
1185 __func__, param->rx_start_of_frame, sof);
1189 msg_len = es58x_dev->ops->get_msg_len(urb_cmd);
1190 urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1191 if (urb_cmd_len > param->rx_urb_cmd_max_len) {
1192 dev_err_ratelimited(es58x_dev->dev,
1193 "%s: Biggest expected size for rx urb_cmd is %u but receive a command of size %d\n",
1195 param->rx_urb_cmd_max_len, urb_cmd_len);
1197 } else if (urb_actual_len < urb_cmd_len) {
1198 dev_vdbg(dev, "%s: Received %02d/%02d bytes\n",
1199 __func__, urb_actual_len, urb_cmd_len);
1203 ret = es58x_check_crc(es58x_dev, urb_cmd, urb_cmd_len);
1211 * es58x_copy_to_cmd_buf() - Copy an array to the URB command buffer.
1212 * @es58x_dev: ES58X device.
1213 * @raw_cmd: the buffer we want to copy.
1214 * @raw_cmd_len: length of @raw_cmd.
1216 * Concatenates @raw_cmd_len bytes of @raw_cmd to the end of the URB
1219 * Return: zero on success, -EMSGSIZE if not enough space is available
1222 static int es58x_copy_to_cmd_buf(struct es58x_device *es58x_dev,
1223 u8 *raw_cmd, int raw_cmd_len)
1225 if (es58x_dev->rx_cmd_buf_len + raw_cmd_len >
1226 es58x_dev->param->rx_urb_cmd_max_len)
1229 memcpy(&es58x_dev->rx_cmd_buf.raw_cmd[es58x_dev->rx_cmd_buf_len],
1230 raw_cmd, raw_cmd_len);
1231 es58x_dev->rx_cmd_buf_len += raw_cmd_len;
1237 * es58x_split_urb_try_recovery() - Try to recover bad URB sequences.
1238 * @es58x_dev: ES58X device.
1239 * @raw_cmd: pointer to the buffer we want to copy.
1240 * @raw_cmd_len: length of @raw_cmd.
1242 * Under some rare conditions, we might get incorrect URBs from the
1243 * device. From our observations, one of the valid URB gets replaced
1244 * by one from the past. The full root cause is not identified.
1246 * This function looks for the next start of frame in the urb buffer
1247 * in order to try to recover.
1249 * Such behavior was not observed on the devices of the ES58X FD
1250 * family and only seems to impact the ES581.4.
1252 * Return: the number of bytes dropped on success, -EBADMSG if recovery failed.
1254 static int es58x_split_urb_try_recovery(struct es58x_device *es58x_dev,
1255 u8 *raw_cmd, size_t raw_cmd_len)
1257 union es58x_urb_cmd *urb_cmd;
1258 signed int urb_cmd_len;
1260 int dropped_bytes = 0;
1262 es58x_increment_rx_errors(es58x_dev);
1264 while (raw_cmd_len > sizeof(sof)) {
1265 urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1266 sof = get_unaligned_le16(&urb_cmd->sof);
1268 if (sof == es58x_dev->param->rx_start_of_frame) {
1269 urb_cmd_len = es58x_check_rx_urb(es58x_dev,
1270 urb_cmd, raw_cmd_len);
1271 if ((urb_cmd_len == -ENODATA) || urb_cmd_len > 0) {
1272 dev_info_ratelimited(es58x_dev->dev,
1273 "Recovery successful! Dropped %d bytes (urb_cmd_len: %d)\n",
1276 return dropped_bytes;
1284 dev_warn_ratelimited(es58x_dev->dev, "%s: Recovery failed\n", __func__);
1289 * es58x_handle_incomplete_cmd() - Reconstitute an URB command from
1290 * different URB pieces.
1291 * @es58x_dev: ES58X device.
1292 * @urb: last urb buffer received.
1294 * The device might split the URB commands in an arbitrary amount of
1295 * pieces. This function concatenates those in an URB buffer until a
1296 * full URB command is reconstituted and consume it.
1299 * number of bytes consumed from @urb if successful.
1301 * -ENODATA if the URB command is still incomplete.
1303 * -EBADMSG if the URB command is incorrect.
1305 static signed int es58x_handle_incomplete_cmd(struct es58x_device *es58x_dev,
1309 signed int urb_cmd_len, tmp_cmd_buf_len, ret;
1311 tmp_cmd_buf_len = es58x_dev->rx_cmd_buf_len;
1312 cpy_len = min_t(int, es58x_dev->param->rx_urb_cmd_max_len -
1313 es58x_dev->rx_cmd_buf_len, urb->actual_length);
1314 ret = es58x_copy_to_cmd_buf(es58x_dev, urb->transfer_buffer, cpy_len);
1318 urb_cmd_len = es58x_check_rx_urb(es58x_dev, &es58x_dev->rx_cmd_buf,
1319 es58x_dev->rx_cmd_buf_len);
1320 if (urb_cmd_len == -ENODATA) {
1322 } else if (urb_cmd_len < 0) {
1323 dev_err_ratelimited(es58x_dev->dev,
1324 "Could not reconstitute incomplete command from previous URB, dropping %d bytes\n",
1325 tmp_cmd_buf_len + urb->actual_length);
1326 dev_err_ratelimited(es58x_dev->dev,
1327 "Error code: %pe, es58x_dev->rx_cmd_buf_len: %d, urb->actual_length: %u\n",
1328 ERR_PTR(urb_cmd_len),
1329 tmp_cmd_buf_len, urb->actual_length);
1330 es58x_print_hex_dump(&es58x_dev->rx_cmd_buf, tmp_cmd_buf_len);
1331 es58x_print_hex_dump(urb->transfer_buffer, urb->actual_length);
1332 return urb->actual_length;
1335 es58x_handle_urb_cmd(es58x_dev, &es58x_dev->rx_cmd_buf);
1336 return urb_cmd_len - tmp_cmd_buf_len; /* consumed length */
1340 * es58x_split_urb() - Cut the received URB in individual URB commands.
1341 * @es58x_dev: ES58X device.
1342 * @urb: last urb buffer received.
1344 * The device might send urb in bulk format (i.e. several URB commands
1345 * concatenated together). This function will split all the commands
1346 * contained in the urb.
1349 * number of bytes consumed from @urb if successful.
1351 * -ENODATA if the URB command is incomplete.
1353 * -EBADMSG if the URB command is incorrect.
1355 static signed int es58x_split_urb(struct es58x_device *es58x_dev,
1358 union es58x_urb_cmd *urb_cmd;
1359 u8 *raw_cmd = urb->transfer_buffer;
1360 s32 raw_cmd_len = urb->actual_length;
1363 if (es58x_dev->rx_cmd_buf_len != 0) {
1364 ret = es58x_handle_incomplete_cmd(es58x_dev, urb);
1365 if (ret != -ENODATA)
1366 es58x_dev->rx_cmd_buf_len = 0;
1374 while (raw_cmd_len > 0) {
1375 if (raw_cmd[0] == ES58X_HEARTBEAT) {
1380 urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1381 ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
1383 es58x_handle_urb_cmd(es58x_dev, urb_cmd);
1384 } else if (ret == -ENODATA) {
1385 es58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len);
1387 } else if (ret < 0) {
1388 ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,
1401 * es58x_read_bulk_callback() - Callback for reading data from device.
1402 * @urb: last urb buffer received.
1404 * This function gets eventually called each time an URB is received
1405 * from the ES58X device.
1407 * Checks urb status, calls read function and resubmits urb read
1410 static void es58x_read_bulk_callback(struct urb *urb)
1412 struct es58x_device *es58x_dev = urb->context;
1413 const struct device *dev = es58x_dev->dev;
1416 switch (urb->status) {
1417 case 0: /* success */
1421 dev_err_ratelimited(dev, "%s: error %pe\n",
1422 __func__, ERR_PTR(urb->status));
1423 es58x_print_hex_dump_debug(urb->transfer_buffer,
1424 urb->transfer_buffer_length);
1428 dev_warn_ratelimited(dev, "%s: error %pe. Device unplugged?\n",
1429 __func__, ERR_PTR(urb->status));
1434 dev_err_ratelimited(dev, "%s: error %pe\n",
1435 __func__, ERR_PTR(urb->status));
1439 dev_dbg_ratelimited(dev, "%s: error %pe\n",
1440 __func__, ERR_PTR(urb->status));
1444 dev_err_ratelimited(dev, "%s: error %pe\n",
1445 __func__, ERR_PTR(urb->status));
1449 ret = es58x_split_urb(es58x_dev, urb);
1450 if ((ret != -ENODATA) && ret < 0) {
1451 dev_err(es58x_dev->dev, "es58x_split_urb() returned error %pe",
1453 es58x_print_hex_dump_debug(urb->transfer_buffer,
1454 urb->actual_length);
1456 /* Because the urb command could not be parsed,
1457 * channel_id is not confirmed. Incrementing rx_errors
1458 * count of all channels.
1460 es58x_increment_rx_errors(es58x_dev);
1464 ret = usb_submit_urb(urb, GFP_ATOMIC);
1465 if (ret == -ENODEV) {
1466 for (i = 0; i < es58x_dev->num_can_ch; i++)
1467 if (es58x_dev->netdev[i])
1468 netif_device_detach(es58x_dev->netdev[i]);
1470 dev_err_ratelimited(dev,
1471 "Failed resubmitting read bulk urb: %pe\n",
1476 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
1477 urb->transfer_buffer, urb->transfer_dma);
1481 * es58x_write_bulk_callback() - Callback after writing data to the device.
1482 * @urb: urb buffer which was previously submitted.
1484 * This function gets eventually called each time an URB was sent to
1487 * Puts the @urb back to the urbs idle anchor and tries to restart the
1490 static void es58x_write_bulk_callback(struct urb *urb)
1492 struct net_device *netdev = urb->context;
1493 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1495 switch (urb->status) {
1496 case 0: /* success */
1500 if (net_ratelimit())
1501 netdev_err(netdev, "%s: error %pe\n",
1502 __func__, ERR_PTR(urb->status));
1503 es58x_print_hex_dump(urb->transfer_buffer,
1504 urb->transfer_buffer_length);
1508 if (net_ratelimit())
1509 netdev_dbg(netdev, "%s: error %pe\n",
1510 __func__, ERR_PTR(urb->status));
1511 usb_free_coherent(urb->dev,
1512 es58x_dev->param->tx_urb_cmd_max_len,
1513 urb->transfer_buffer, urb->transfer_dma);
1517 if (net_ratelimit())
1518 netdev_info(netdev, "%s: error %pe\n",
1519 __func__, ERR_PTR(urb->status));
1523 usb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);
1524 atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
1528 * es58x_alloc_urb() - Allocate memory for an URB and its transfer
1530 * @es58x_dev: ES58X device.
1531 * @urb: URB to be allocated.
1532 * @buf: used to return DMA address of buffer.
1533 * @buf_len: requested buffer size.
1534 * @mem_flags: affect whether allocation may block.
1536 * Allocates an URB and its @transfer_buffer and set its @transfer_dma
1539 * This function is used at start-up to allocate all RX URBs at once
1540 * and during run time for TX URBs.
1542 * Return: zero on success, -ENOMEM if no memory is available.
1544 static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb,
1545 u8 **buf, size_t buf_len, gfp_t mem_flags)
1547 *urb = usb_alloc_urb(0, mem_flags);
1549 dev_err(es58x_dev->dev, "No memory left for URBs\n");
1553 *buf = usb_alloc_coherent(es58x_dev->udev, buf_len,
1554 mem_flags, &(*urb)->transfer_dma);
1556 dev_err(es58x_dev->dev, "No memory left for USB buffer\n");
1561 (*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1567 * es58x_get_tx_urb() - Get an URB for transmission.
1568 * @es58x_dev: ES58X device.
1570 * Gets an URB from the idle urbs anchor or allocate a new one if the
1573 * If there are more than ES58X_TX_URBS_MAX in the idle anchor, do
1574 * some garbage collection. The garbage collection is done here
1575 * instead of within es58x_write_bulk_callback() because
1576 * usb_free_coherent() should not be used in IRQ context:
1577 * c.f. WARN_ON(irqs_disabled()) in dma_free_attrs().
1579 * Return: a pointer to an URB on success, NULL if no memory is
1582 static struct urb *es58x_get_tx_urb(struct es58x_device *es58x_dev)
1584 atomic_t *idle_cnt = &es58x_dev->tx_urbs_idle_cnt;
1585 struct urb *urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1591 tx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;
1592 if (es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,
1596 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1597 buf, tx_buf_len, es58x_write_bulk_callback,
1602 while (atomic_dec_return(idle_cnt) > ES58X_TX_URBS_MAX) {
1603 /* Garbage collector */
1604 struct urb *tmp = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1608 usb_free_coherent(tmp->dev,
1609 es58x_dev->param->tx_urb_cmd_max_len,
1610 tmp->transfer_buffer, tmp->transfer_dma);
1618 * es58x_submit_urb() - Send data to the device.
1619 * @es58x_dev: ES58X device.
1620 * @urb: URB to be sent.
1621 * @netdev: CAN network device.
1623 * Return: zero on success, errno when any error occurs.
1625 static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb,
1626 struct net_device *netdev)
1630 es58x_set_crc(urb->transfer_buffer, urb->transfer_buffer_length);
1631 urb->context = netdev;
1632 usb_anchor_urb(urb, &es58x_dev->tx_urbs_busy);
1633 ret = usb_submit_urb(urb, GFP_ATOMIC);
1635 netdev_err(netdev, "%s: USB send urb failure: %pe\n",
1636 __func__, ERR_PTR(ret));
1637 usb_unanchor_urb(urb);
1638 usb_free_coherent(urb->dev,
1639 es58x_dev->param->tx_urb_cmd_max_len,
1640 urb->transfer_buffer, urb->transfer_dma);
1648 * es58x_send_msg() - Prepare an URB and submit it.
1649 * @es58x_dev: ES58X device.
1650 * @cmd_type: Command type.
1651 * @cmd_id: Command ID.
1652 * @msg: ES58X message to be sent.
1653 * @msg_len: Length of @msg.
1654 * @channel_idx: Index of the network device.
1656 * Creates an URB command from a given message, sets the header and the
1657 * CRC and then submits it.
1659 * Return: zero on success, errno when any error occurs.
1661 int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
1662 const void *msg, u16 msg_len, int channel_idx)
1664 struct net_device *netdev;
1665 union es58x_urb_cmd *urb_cmd;
1669 if (channel_idx == ES58X_CHANNEL_IDX_NA)
1670 netdev = es58x_dev->netdev[0]; /* Default to first channel */
1672 netdev = es58x_dev->netdev[channel_idx];
1674 urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1675 if (urb_cmd_len > es58x_dev->param->tx_urb_cmd_max_len)
1678 urb = es58x_get_tx_urb(es58x_dev);
1682 urb_cmd = urb->transfer_buffer;
1683 es58x_dev->ops->fill_urb_header(urb_cmd, cmd_type, cmd_id,
1684 channel_idx, msg_len);
1685 memcpy(&urb_cmd->raw_cmd[es58x_dev->param->urb_cmd_header_len],
1687 urb->transfer_buffer_length = urb_cmd_len;
1689 return es58x_submit_urb(es58x_dev, urb, netdev);
1693 * es58x_alloc_rx_urbs() - Allocate RX URBs.
1694 * @es58x_dev: ES58X device.
1696 * Allocate URBs for reception and anchor them.
1698 * Return: zero on success, errno when any error occurs.
1700 static int es58x_alloc_rx_urbs(struct es58x_device *es58x_dev)
1702 const struct device *dev = es58x_dev->dev;
1703 const struct es58x_parameters *param = es58x_dev->param;
1704 u16 rx_buf_len = usb_maxpacket(es58x_dev->udev, es58x_dev->rx_pipe);
1710 for (i = 0; i < param->rx_urb_max; i++) {
1711 ret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len,
1716 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1717 buf, rx_buf_len, es58x_read_bulk_callback,
1719 usb_anchor_urb(urb, &es58x_dev->rx_urbs);
1721 ret = usb_submit_urb(urb, GFP_KERNEL);
1723 usb_unanchor_urb(urb);
1724 usb_free_coherent(es58x_dev->udev, rx_buf_len,
1725 buf, urb->transfer_dma);
1733 dev_err(dev, "%s: Could not setup any rx URBs\n", __func__);
1736 dev_dbg(dev, "%s: Allocated %d rx URBs each of size %u\n",
1737 __func__, i, rx_buf_len);
1743 * es58x_free_urbs() - Free all the TX and RX URBs.
1744 * @es58x_dev: ES58X device.
1746 static void es58x_free_urbs(struct es58x_device *es58x_dev)
1750 if (!usb_wait_anchor_empty_timeout(&es58x_dev->tx_urbs_busy, 1000)) {
1751 dev_err(es58x_dev->dev, "%s: Timeout, some TX urbs still remain\n",
1753 usb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy);
1756 while ((urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle)) != NULL) {
1757 usb_free_coherent(urb->dev, es58x_dev->param->tx_urb_cmd_max_len,
1758 urb->transfer_buffer, urb->transfer_dma);
1760 atomic_dec(&es58x_dev->tx_urbs_idle_cnt);
1762 if (atomic_read(&es58x_dev->tx_urbs_idle_cnt))
1763 dev_err(es58x_dev->dev,
1764 "All idle urbs were freed but tx_urb_idle_cnt is %d\n",
1765 atomic_read(&es58x_dev->tx_urbs_idle_cnt));
1767 usb_kill_anchored_urbs(&es58x_dev->rx_urbs);
1771 * es58x_open() - Enable the network device.
1772 * @netdev: CAN network device.
1774 * Called when the network transitions to the up state. Allocate the
1775 * URB resources if needed and open the channel.
1777 * Return: zero on success, errno when any error occurs.
1779 static int es58x_open(struct net_device *netdev)
1781 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1784 if (!es58x_dev->opened_channel_cnt) {
1785 ret = es58x_alloc_rx_urbs(es58x_dev);
1789 ret = es58x_set_realtime_diff_ns(es58x_dev);
1794 ret = open_candev(netdev);
1798 ret = es58x_dev->ops->enable_channel(es58x_priv(netdev));
1802 es58x_dev->opened_channel_cnt++;
1803 netif_start_queue(netdev);
1808 if (!es58x_dev->opened_channel_cnt)
1809 es58x_free_urbs(es58x_dev);
1810 netdev_err(netdev, "%s: Could not open the network device: %pe\n",
1811 __func__, ERR_PTR(ret));
1817 * es58x_stop() - Disable the network device.
1818 * @netdev: CAN network device.
1820 * Called when the network transitions to the down state. If all the
1821 * channels of the device are closed, free the URB resources which are
1822 * not needed anymore.
1824 * Return: zero on success, errno when any error occurs.
1826 static int es58x_stop(struct net_device *netdev)
1828 struct es58x_priv *priv = es58x_priv(netdev);
1829 struct es58x_device *es58x_dev = priv->es58x_dev;
1832 netif_stop_queue(netdev);
1833 ret = es58x_dev->ops->disable_channel(priv);
1837 priv->can.state = CAN_STATE_STOPPED;
1838 es58x_can_reset_echo_fifo(netdev);
1839 close_candev(netdev);
1841 es58x_flush_pending_tx_msg(netdev);
1843 es58x_dev->opened_channel_cnt--;
1844 if (!es58x_dev->opened_channel_cnt)
1845 es58x_free_urbs(es58x_dev);
1851 * es58x_xmit_commit() - Send the bulk urb.
1852 * @netdev: CAN network device.
1854 * Do the bulk send. This function should be called only once by bulk
1857 * Return: zero on success, errno when any error occurs.
1859 static int es58x_xmit_commit(struct net_device *netdev)
1861 struct es58x_priv *priv = es58x_priv(netdev);
1864 if (!es58x_is_can_state_active(netdev))
1867 if (es58x_is_echo_skb_threshold_reached(priv))
1868 netif_stop_queue(netdev);
1870 ret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev);
1872 priv->tx_urb = NULL;
1878 * es58x_xmit_more() - Can we put more packets?
1879 * @priv: ES58X private parameters related to the network device.
1881 * Return: true if we can put more, false if it is time to send.
1883 static bool es58x_xmit_more(struct es58x_priv *priv)
1885 unsigned int free_slots =
1886 priv->can.echo_skb_max - (priv->tx_head - priv->tx_tail);
1888 return netdev_xmit_more() && free_slots > 0 &&
1889 priv->tx_can_msg_cnt < priv->es58x_dev->param->tx_bulk_max;
1893 * es58x_start_xmit() - Transmit an skb.
1894 * @skb: socket buffer of a CAN message.
1895 * @netdev: CAN network device.
1897 * Called when a packet needs to be transmitted.
1899 * This function relies on Byte Queue Limits (BQL). The main benefit
1900 * is to increase the throughput by allowing bulk transfers
1901 * (c.f. xmit_more flag).
1903 * Queues up to tx_bulk_max messages in &tx_urb buffer and does
1904 * a bulk send of all messages in one single URB.
1906 * Return: NETDEV_TX_OK regardless of if we could transmit the @skb or
1909 static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
1910 struct net_device *netdev)
1912 struct es58x_priv *priv = es58x_priv(netdev);
1913 struct es58x_device *es58x_dev = priv->es58x_dev;
1914 unsigned int frame_len;
1917 if (can_dev_dropped_skb(netdev, skb)) {
1920 return NETDEV_TX_OK;
1923 if (priv->tx_urb && priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
1924 /* Can not do bulk send with mixed CAN and CAN FD frames. */
1925 ret = es58x_xmit_commit(netdev);
1930 if (!priv->tx_urb) {
1931 priv->tx_urb = es58x_get_tx_urb(es58x_dev);
1932 if (!priv->tx_urb) {
1936 priv->tx_can_msg_cnt = 0;
1937 priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
1940 ret = es58x_dev->ops->tx_can_msg(priv, skb);
1944 frame_len = can_skb_get_frame_len(skb);
1945 ret = can_put_echo_skb(skb, netdev,
1946 priv->tx_head & es58x_dev->param->fifo_mask,
1950 netdev_sent_queue(netdev, frame_len);
1953 priv->tx_can_msg_cnt++;
1956 if (!es58x_xmit_more(priv)) {
1957 ret = es58x_xmit_commit(netdev);
1962 return NETDEV_TX_OK;
1966 netdev->stats.tx_dropped++;
1968 netdev_warn(netdev, "%s: send message failure: %pe\n",
1969 __func__, ERR_PTR(ret));
1970 netdev->stats.tx_errors++;
1971 es58x_flush_pending_tx_msg(netdev);
1972 return NETDEV_TX_OK;
1975 static const struct net_device_ops es58x_netdev_ops = {
1976 .ndo_open = es58x_open,
1977 .ndo_stop = es58x_stop,
1978 .ndo_start_xmit = es58x_start_xmit,
1979 .ndo_eth_ioctl = can_eth_ioctl_hwts,
1982 static const struct ethtool_ops es58x_ethtool_ops = {
1983 .get_ts_info = can_ethtool_op_get_ts_info_hwts,
1987 * es58x_set_mode() - Change network device mode.
1988 * @netdev: CAN network device.
1989 * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP
1991 * Currently, this function is only used to stop and restart the
1992 * channel during a bus off event (c.f. es58x_rx_err_msg() and
1993 * drivers/net/can/dev.c:can_restart() which are the two only
1996 * Return: zero on success, errno when any error occurs.
1998 static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
2000 struct es58x_priv *priv = es58x_priv(netdev);
2003 case CAN_MODE_START:
2004 switch (priv->can.state) {
2005 case CAN_STATE_BUS_OFF:
2006 return priv->es58x_dev->ops->enable_channel(priv);
2008 case CAN_STATE_STOPPED:
2009 return es58x_open(netdev);
2011 case CAN_STATE_ERROR_ACTIVE:
2012 case CAN_STATE_ERROR_WARNING:
2013 case CAN_STATE_ERROR_PASSIVE:
2019 switch (priv->can.state) {
2020 case CAN_STATE_STOPPED:
2023 case CAN_STATE_ERROR_ACTIVE:
2024 case CAN_STATE_ERROR_WARNING:
2025 case CAN_STATE_ERROR_PASSIVE:
2026 case CAN_STATE_BUS_OFF:
2028 return priv->es58x_dev->ops->disable_channel(priv);
2031 case CAN_MODE_SLEEP:
2038 * es58x_init_priv() - Initialize private parameters.
2039 * @es58x_dev: ES58X device.
2040 * @priv: ES58X private parameters related to the network device.
2041 * @channel_idx: Index of the network device.
2043 * Return: zero on success, errno if devlink port could not be
2044 * properly registered.
2046 static int es58x_init_priv(struct es58x_device *es58x_dev,
2047 struct es58x_priv *priv, int channel_idx)
2049 struct devlink_port_attrs attrs = {
2050 .flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL,
2052 const struct es58x_parameters *param = es58x_dev->param;
2053 struct can_priv *can = &priv->can;
2055 priv->es58x_dev = es58x_dev;
2056 priv->channel_idx = channel_idx;
2057 priv->tx_urb = NULL;
2058 priv->tx_can_msg_cnt = 0;
2060 can->bittiming_const = param->bittiming_const;
2061 if (param->ctrlmode_supported & CAN_CTRLMODE_FD) {
2062 can->data_bittiming_const = param->data_bittiming_const;
2063 can->tdc_const = param->tdc_const;
2065 can->bitrate_max = param->bitrate_max;
2066 can->clock = param->clock;
2067 can->state = CAN_STATE_STOPPED;
2068 can->ctrlmode_supported = param->ctrlmode_supported;
2069 can->do_set_mode = es58x_set_mode;
2071 devlink_port_attrs_set(&priv->devlink_port, &attrs);
2072 return devlink_port_register(priv_to_devlink(es58x_dev),
2073 &priv->devlink_port, channel_idx);
2077 * es58x_init_netdev() - Initialize the network device.
2078 * @es58x_dev: ES58X device.
2079 * @channel_idx: Index of the network device.
2081 * Return: zero on success, errno when any error occurs.
2083 static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
2085 struct net_device *netdev;
2086 struct device *dev = es58x_dev->dev;
2089 netdev = alloc_candev(sizeof(struct es58x_priv),
2090 es58x_dev->param->fifo_mask + 1);
2092 dev_err(dev, "Could not allocate candev\n");
2095 SET_NETDEV_DEV(netdev, dev);
2096 es58x_dev->netdev[channel_idx] = netdev;
2097 ret = es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);
2100 SET_NETDEV_DEVLINK_PORT(netdev, &es58x_priv(netdev)->devlink_port);
2102 netdev->netdev_ops = &es58x_netdev_ops;
2103 netdev->ethtool_ops = &es58x_ethtool_ops;
2104 netdev->flags |= IFF_ECHO; /* We support local echo */
2105 netdev->dev_port = channel_idx;
2107 ret = register_candev(netdev);
2109 goto devlink_port_unregister;
2111 netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
2112 es58x_dev->param->dql_min_limit);
2116 devlink_port_unregister:
2117 devlink_port_unregister(&es58x_priv(netdev)->devlink_port);
2119 es58x_dev->netdev[channel_idx] = NULL;
2120 free_candev(netdev);
2125 * es58x_free_netdevs() - Release all network resources of the device.
2126 * @es58x_dev: ES58X device.
2128 static void es58x_free_netdevs(struct es58x_device *es58x_dev)
2132 for (i = 0; i < es58x_dev->num_can_ch; i++) {
2133 struct net_device *netdev = es58x_dev->netdev[i];
2137 unregister_candev(netdev);
2138 devlink_port_unregister(&es58x_priv(netdev)->devlink_port);
2139 es58x_dev->netdev[i] = NULL;
2140 free_candev(netdev);
2145 * es58x_init_es58x_dev() - Initialize the ES58X device.
2146 * @intf: USB interface.
2147 * @driver_info: Quirks of the device.
2149 * Return: pointer to an ES58X device on success, error pointer when
2152 static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf,
2153 kernel_ulong_t driver_info)
2155 struct device *dev = &intf->dev;
2156 struct es58x_device *es58x_dev;
2157 struct devlink *devlink;
2158 const struct es58x_parameters *param;
2159 const struct es58x_operators *ops;
2160 struct usb_device *udev = interface_to_usbdev(intf);
2161 struct usb_endpoint_descriptor *ep_in, *ep_out;
2164 dev_info(dev, "Starting %s %s (Serial Number %s)\n",
2165 udev->manufacturer, udev->product, udev->serial);
2167 ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
2170 return ERR_PTR(ret);
2172 if (driver_info & ES58X_FD_FAMILY) {
2173 param = &es58x_fd_param;
2174 ops = &es58x_fd_ops;
2176 param = &es581_4_param;
2180 devlink = devlink_alloc(&es58x_dl_ops, es58x_sizeof_es58x_device(param),
2183 return ERR_PTR(-ENOMEM);
2185 es58x_dev = devlink_priv(devlink);
2186 es58x_dev->param = param;
2187 es58x_dev->ops = ops;
2188 es58x_dev->dev = dev;
2189 es58x_dev->udev = udev;
2191 if (driver_info & ES58X_DUAL_CHANNEL)
2192 es58x_dev->num_can_ch = 2;
2194 es58x_dev->num_can_ch = 1;
2196 init_usb_anchor(&es58x_dev->rx_urbs);
2197 init_usb_anchor(&es58x_dev->tx_urbs_idle);
2198 init_usb_anchor(&es58x_dev->tx_urbs_busy);
2199 atomic_set(&es58x_dev->tx_urbs_idle_cnt, 0);
2200 usb_set_intfdata(intf, es58x_dev);
2202 es58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev,
2203 ep_in->bEndpointAddress);
2204 es58x_dev->tx_pipe = usb_sndbulkpipe(es58x_dev->udev,
2205 ep_out->bEndpointAddress);
2211 * es58x_probe() - Initialize the USB device.
2212 * @intf: USB interface.
2213 * @id: USB device ID.
2215 * Return: zero on success, -ENODEV if the interface is not supported
2216 * or errno when any other error occurs.
2218 static int es58x_probe(struct usb_interface *intf,
2219 const struct usb_device_id *id)
2221 struct es58x_device *es58x_dev;
2224 es58x_dev = es58x_init_es58x_dev(intf, id->driver_info);
2225 if (IS_ERR(es58x_dev))
2226 return PTR_ERR(es58x_dev);
2228 es58x_parse_product_info(es58x_dev);
2229 devlink_register(priv_to_devlink(es58x_dev));
2231 for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {
2232 int ret = es58x_init_netdev(es58x_dev, ch_idx);
2235 es58x_free_netdevs(es58x_dev);
2244 * es58x_disconnect() - Disconnect the USB device.
2245 * @intf: USB interface
2247 * Called by the usb core when driver is unloaded or device is
2250 static void es58x_disconnect(struct usb_interface *intf)
2252 struct es58x_device *es58x_dev = usb_get_intfdata(intf);
2254 dev_info(&intf->dev, "Disconnecting %s %s\n",
2255 es58x_dev->udev->manufacturer, es58x_dev->udev->product);
2257 devlink_unregister(priv_to_devlink(es58x_dev));
2258 es58x_free_netdevs(es58x_dev);
2259 es58x_free_urbs(es58x_dev);
2260 devlink_free(priv_to_devlink(es58x_dev));
2261 usb_set_intfdata(intf, NULL);
2264 static struct usb_driver es58x_driver = {
2265 .name = KBUILD_MODNAME,
2266 .probe = es58x_probe,
2267 .disconnect = es58x_disconnect,
2268 .id_table = es58x_id_table
2271 module_usb_driver(es58x_driver);