1// SPDX-License-Identifier: GPL-2.0
2
3/* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
4 *
5 * File es58x_core.c: Core logic to manage the network devices and the
6 * USB interface.
7 *
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>
11 */
12
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>
20
21#include "es58x_core.h"
22
23MODULE_AUTHOR("Vincent Mailhol <mailhol.vincent@wanadoo.fr>");
24MODULE_AUTHOR("Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>");
25MODULE_DESCRIPTION("Socket CAN driver for ETAS ES58X USB adapters");
26MODULE_LICENSE("GPL v2");
27
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
32
33/* ES58X FD has some interface protocols unsupported by this driver. */
34#define ES58X_FD_INTERFACE_PROTOCOL 0
35
36/* Table of devices which work with this driver. */
37static const struct usb_device_id es58x_id_table[] = {
38	{
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
42	}, {
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
47	}, {
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
52	}, {
53		/* Terminating entry */
54	}
55};
56
57MODULE_DEVICE_TABLE(usb, es58x_id_table);
58
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)
63
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)
67
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.
71 */
72#define ES58X_CRC_CALC_OFFSET sizeof_field(union es58x_urb_cmd, sof)
73
74/**
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))
79 *
80 * Return: crc16 value.
81 */
82static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
83{
84	u16 crc;
85	ssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc);
86
87	crc = crc16(0, &urb_cmd->raw_cmd[ES58X_CRC_CALC_OFFSET], len);
88	return crc;
89}
90
91/**
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))
96 *
97 * Return: crc16 value.
98 */
99static u16 es58x_get_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len)
100{
101	u16 crc;
102	const __le16 *crc_addr;
103
104	crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)];
105	crc = get_unaligned_le16(crc_addr);
106	return crc;
107}
108
109/**
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))
114 */
115static void es58x_set_crc(union es58x_urb_cmd *urb_cmd, u16 urb_len)
116{
117	u16 crc;
118	__le16 *crc_addr;
119
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);
123}
124
125/**
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))
131 *
132 * Return: zero on success, -EBADMSG if the CRC check fails.
133 */
134static int es58x_check_crc(struct es58x_device *es58x_dev,
135			   const union es58x_urb_cmd *urb_cmd, u16 urb_len)
136{
137	u16 calculated_crc = es58x_calculate_crc(urb_cmd, urb_len);
138	u16 expected_crc = es58x_get_crc(urb_cmd, urb_len);
139
140	if (expected_crc != calculated_crc) {
141		dev_err_ratelimited(es58x_dev->dev,
142				    "%s: Bad CRC, urb_len: %d\n",
143				    __func__, urb_len);
144		return -EBADMSG;
145	}
146
147	return 0;
148}
149
150/**
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.
154 *
155 * The timestamp received from ES58X is expressed in multiples of 0.5
156 * micro seconds. This function converts it in to nanoseconds.
157 *
158 * Return: Timestamp value in nanoseconds.
159 */
160static u64 es58x_timestamp_to_ns(u64 timestamp)
161{
162	const u64 es58x_timestamp_ns_mult_coef = 500ULL;
163
164	return es58x_timestamp_ns_mult_coef * timestamp;
165}
166
167/**
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.
172 *
173 * Used for both received and echo messages.
174 */
175static void es58x_set_skb_timestamp(struct net_device *netdev,
176				    struct sk_buff *skb, u64 timestamp)
177{
178	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
179	struct skb_shared_hwtstamps *hwts;
180
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).
184	 */
185	hwts->hwtstamp = ns_to_ktime(es58x_timestamp_to_ns(timestamp) +
186				     es58x_dev->realtime_diff_ns);
187}
188
189/**
190 * es58x_rx_timestamp() - Handle a received timestamp.
191 * @es58x_dev: ES58X device.
192 * @timestamp: Timestamp received from a ES58X device.
193 *
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).
198 */
199void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp)
200{
201	u64 ktime_real_ns = ktime_get_real_ns();
202	u64 device_timestamp = es58x_timestamp_to_ns(timestamp);
203
204	dev_dbg(es58x_dev->dev, "%s: request round-trip time: %llu ns\n",
205		__func__, ktime_real_ns - es58x_dev->ktime_req_ns);
206
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;
210
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);
214}
215
216/**
217 * es58x_set_realtime_diff_ns() - Calculate difference between the
218 *	clocks of the ES58X device and the kernel
219 * @es58x_dev: ES58X device.
220 *
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().
224 *
225 * Return: zero on success, errno when any error occurs.
226 */
227static int es58x_set_realtime_diff_ns(struct es58x_device *es58x_dev)
228{
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",
232			 __func__);
233		return -EBUSY;
234	}
235
236	es58x_dev->ktime_req_ns = ktime_get_real_ns();
237	return es58x_dev->ops->get_timestamp(es58x_dev);
238}
239
240/**
241 * es58x_is_can_state_active() - Is the network device in an active
242 *	CAN state?
243 * @netdev: CAN network device.
244 *
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.
249 *
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.
256 *
257 * Return: true if the device is active, else returns false.
258 */
259static bool es58x_is_can_state_active(struct net_device *netdev)
260{
261	return es58x_priv(netdev)->can.state < CAN_STATE_BUS_OFF;
262}
263
264/**
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
267 *	queue.
268 * @priv: ES58X private parameters related to the network device.
269 *
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.
274 *
275 * Return: boolean.
276 */
277static bool es58x_is_echo_skb_threshold_reached(struct es58x_priv *priv)
278{
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;
282
283	return num_echo_skb >= threshold;
284}
285
286/**
287 * es58x_can_free_echo_skb_tail() - Remove the oldest echo skb of the
288 *	echo FIFO.
289 * @netdev: CAN network device.
290 *
291 * Naming convention: the tail is the beginning of the FIFO, i.e. the
292 * first skb to have entered the FIFO.
293 */
294static void es58x_can_free_echo_skb_tail(struct net_device *netdev)
295{
296	struct es58x_priv *priv = es58x_priv(netdev);
297	u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
298	unsigned int frame_len = 0;
299
300	can_free_echo_skb(netdev, priv->tx_tail & fifo_mask, &frame_len);
301	netdev_completed_queue(netdev, 1, frame_len);
302
303	priv->tx_tail++;
304
305	netdev->stats.tx_dropped++;
306}
307
308/**
309 * es58x_can_get_echo_skb_recovery() - Try to re-sync the echo FIFO.
310 * @netdev: CAN network device.
311 * @rcv_packet_idx: Index
312 *
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.
317 *
318 * Return: zero if recovery was successful, -EINVAL otherwise.
319 */
320static int es58x_can_get_echo_skb_recovery(struct net_device *netdev,
321					   u32 rcv_packet_idx)
322{
323	struct es58x_priv *priv = es58x_priv(netdev);
324	int ret = 0;
325
326	netdev->stats.tx_errors++;
327
328	if (net_ratelimit())
329		netdev_warn(netdev,
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);
334
335	if ((s32)(rcv_packet_idx - priv->tx_tail) < 0) {
336		if (net_ratelimit())
337			netdev_warn(netdev,
338				    "Received echo index is from the past. Ignoring it\n");
339		ret = -EINVAL;
340	} else if ((s32)(rcv_packet_idx - priv->tx_head) >= 0) {
341		if (net_ratelimit())
342			netdev_err(netdev,
343				   "Received echo index is from the future. Ignoring it\n");
344		ret = -EINVAL;
345	} else {
346		if (net_ratelimit())
347			netdev_warn(netdev,
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)
353				return -EINVAL;
354			es58x_can_free_echo_skb_tail(netdev);
355		}
356	}
357	return ret;
358}
359
360/**
361 * es58x_can_get_echo_skb() - Get the skb from the echo FIFO and loop
362 *	it back locally.
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).
367 *
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
371 * locally.
372 *
373 * The device has to be active (i.e. network interface UP and not in
374 * bus off state or restarting).
375 *
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).
379 *
380 * Return: zero on success.
381 */
382int es58x_can_get_echo_skb(struct net_device *netdev, u32 rcv_packet_idx,
383			   u64 *tstamps, unsigned int pkts)
384{
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;
388	int i;
389	u16 fifo_mask = priv->es58x_dev->param->fifo_mask;
390
391	if (!netif_running(netdev)) {
392		if (net_ratelimit())
393			netdev_info(netdev,
394				    "%s: %s is down, dropping %d echo packets\n",
395				    __func__, netdev->name, pkts);
396		netdev->stats.tx_dropped += pkts;
397		return 0;
398	} else if (!es58x_is_can_state_active(netdev)) {
399		if (net_ratelimit())
400			netdev_dbg(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)
404		 * incremented by
405		 * drivers/net/can/net/dev.c:can_flush_echo_skb().
406		 */
407		return 0;
408	} else if (num_echo_skb == 0) {
409		if (net_ratelimit())
410			netdev_warn(netdev,
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;
414		return 0;
415	}
416
417	if (priv->tx_tail != rcv_packet_idx) {
418		if (es58x_can_get_echo_skb_recovery(netdev, rcv_packet_idx) < 0) {
419			if (net_ratelimit())
420				netdev_warn(netdev,
421					    "Could not find echo skb for echo packet index: %u\n",
422					    rcv_packet_idx);
423			return 0;
424		}
425	}
426	if (num_echo_skb < pkts) {
427		int pkts_drop = pkts - num_echo_skb;
428
429		if (net_ratelimit())
430			netdev_err(netdev,
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;
434		pkts -= pkts_drop;
435	}
436
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;
441
442		if (skb)
443			es58x_set_skb_timestamp(netdev, skb, tstamps[i]);
444
445		netdev->stats.tx_bytes += can_get_echo_skb(netdev, skb_idx,
446							   &frame_len);
447		rx_total_frame_len += frame_len;
448
449		priv->tx_tail++;
450	}
451
452	netdev_completed_queue(netdev, pkts, rx_total_frame_len);
453	netdev->stats.tx_packets += pkts;
454
455	priv->err_passive_before_rtx_success = 0;
456	if (!es58x_is_echo_skb_threshold_reached(priv))
457		netif_wake_queue(netdev);
458
459	return 0;
460}
461
462/**
463 * es58x_can_reset_echo_fifo() - Reset the echo FIFO.
464 * @netdev: CAN network device.
465 *
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
469 * queue (c.f. BQL).
470 */
471static void es58x_can_reset_echo_fifo(struct net_device *netdev)
472{
473	struct es58x_priv *priv = es58x_priv(netdev);
474
475	priv->tx_tail = 0;
476	priv->tx_head = 0;
477	priv->tx_urb = NULL;
478	priv->err_passive_before_rtx_success = 0;
479	netdev_reset_queue(netdev);
480}
481
482/**
483 * es58x_flush_pending_tx_msg() - Reset the buffer for transmission messages.
484 * @netdev: CAN network device.
485 *
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.
491 */
492static void es58x_flush_pending_tx_msg(struct net_device *netdev)
493{
494	struct es58x_priv *priv = es58x_priv(netdev);
495	struct es58x_device *es58x_dev = priv->es58x_dev;
496
497	if (priv->tx_urb) {
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;
504
505			priv->tx_head--;
506			priv->tx_can_msg_cnt--;
507			can_free_echo_skb(netdev, priv->tx_head & fifo_mask,
508					  &frame_len);
509			netdev_completed_queue(netdev, 1, frame_len);
510		}
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);
514	}
515	priv->tx_urb = NULL;
516}
517
518/**
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.
523 *
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.
527 *
528 * Under normal circumstances, this function should never throw an
529 * error message.
530 *
531 * Return: zero on success, errno when any error occurs.
532 */
533int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries,
534		     enum es58x_ret_u32 rx_cmd_ret_u32)
535{
536	struct es58x_priv *priv = es58x_priv(netdev);
537
538	if (tx_free_entries <= priv->es58x_dev->param->tx_bulk_max) {
539		if (net_ratelimit())
540			netdev_err(netdev,
541				   "Only %d entries left in device queue, num_echo_skb: %d/%d\n",
542				   tx_free_entries,
543				   priv->tx_head - priv->tx_tail,
544				   priv->can.echo_skb_max);
545		netif_stop_queue(netdev);
546	}
547
548	return es58x_rx_cmd_ret_u32(netdev, ES58X_RET_TYPE_TX_MSG,
549				    rx_cmd_ret_u32);
550}
551
552/**
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.
557 * @can_id: CAN ID.
558 * @es58x_flags: Please refer to enum es58x_flag.
559 * @dlc: Data Length Code (raw value).
560 *
561 * Fill up a CAN skb and post it.
562 *
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
565 * struct can_frame).
566 *
567 * Return: zero on success.
568 */
569int 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)
571{
572	struct canfd_frame *cfd;
573	struct can_frame *ccf;
574	struct sk_buff *skb;
575	u8 len;
576	bool is_can_fd = !!(es58x_flags & ES58X_FLAG_FD_DATA);
577
578	if (dlc > CAN_MAX_RAW_DLC) {
579		netdev_err(netdev,
580			   "%s: DLC is %d but maximum should be %d\n",
581			   __func__, dlc, CAN_MAX_RAW_DLC);
582		return -EMSGSIZE;
583	}
584
585	if (is_can_fd) {
586		len = can_fd_dlc2len(dlc);
587		skb = alloc_canfd_skb(netdev, &cfd);
588	} else {
589		len = can_cc_dlc2len(dlc);
590		skb = alloc_can_skb(netdev, &ccf);
591		cfd = (struct canfd_frame *)ccf;
592	}
593	if (!skb) {
594		netdev->stats.rx_dropped++;
595		return 0;
596	}
597
598	cfd->can_id = can_id;
599	if (es58x_flags & ES58X_FLAG_EFF)
600		cfd->can_id |= CAN_EFF_FLAG;
601	if (is_can_fd) {
602		cfd->len = len;
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;
607	} else {
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;
611			len = 0;
612		}
613	}
614	memcpy(cfd->data, data, len);
615	netdev->stats.rx_packets++;
616	netdev->stats.rx_bytes += len;
617
618	es58x_set_skb_timestamp(netdev, skb, timestamp);
619	netif_rx(skb);
620
621	es58x_priv(netdev)->err_passive_before_rtx_success = 0;
622
623	return 0;
624}
625
626/**
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.
632 *
633 * Handle the errors and events received by the ES58X device, create
634 * a CAN error skb and post it.
635 *
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.
641 *
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
649 * restart-ms 100).
650 *
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
654 * dmesg).
655 *
656 * Return: zero on success, errno when any error occurs.
657 */
658int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error,
659		     enum es58x_event event, u64 timestamp)
660{
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;
665	struct sk_buff *skb;
666	int ret = 0;
667
668	if (!netif_running(netdev)) {
669		if (net_ratelimit())
670			netdev_info(netdev, "%s: %s is down, dropping packet\n",
671				    __func__, netdev->name);
672		netdev->stats.rx_dropped++;
673		return 0;
674	}
675
676	if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) {
677		netdev_err(netdev, "%s: Both error and event are zero\n",
678			   __func__);
679		return -EINVAL;
680	}
681
682	skb = alloc_can_err_skb(netdev, &cf);
683
684	switch (error) {
685	case ES58X_ERR_OK:	/* 0: No error */
686		break;
687
688	case ES58X_ERR_PROT_STUFF:
689		if (net_ratelimit())
690			netdev_dbg(netdev, "Error BITSTUFF\n");
691		if (cf)
692			cf->data[2] |= CAN_ERR_PROT_STUFF;
693		break;
694
695	case ES58X_ERR_PROT_FORM:
696		if (net_ratelimit())
697			netdev_dbg(netdev, "Error FORMAT\n");
698		if (cf)
699			cf->data[2] |= CAN_ERR_PROT_FORM;
700		break;
701
702	case ES58X_ERR_ACK:
703		if (net_ratelimit())
704			netdev_dbg(netdev, "Error ACK\n");
705		if (cf)
706			cf->can_id |= CAN_ERR_ACK;
707		break;
708
709	case ES58X_ERR_PROT_BIT:
710		if (net_ratelimit())
711			netdev_dbg(netdev, "Error BIT\n");
712		if (cf)
713			cf->data[2] |= CAN_ERR_PROT_BIT;
714		break;
715
716	case ES58X_ERR_PROT_CRC:
717		if (net_ratelimit())
718			netdev_dbg(netdev, "Error CRC\n");
719		if (cf)
720			cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
721		break;
722
723	case ES58X_ERR_PROT_BIT1:
724		if (net_ratelimit())
725			netdev_dbg(netdev,
726				   "Error: expected a recessive bit but monitored a dominant one\n");
727		if (cf)
728			cf->data[2] |= CAN_ERR_PROT_BIT1;
729		break;
730
731	case ES58X_ERR_PROT_BIT0:
732		if (net_ratelimit())
733			netdev_dbg(netdev,
734				   "Error expected a dominant bit but monitored a recessive one\n");
735		if (cf)
736			cf->data[2] |= CAN_ERR_PROT_BIT0;
737		break;
738
739	case ES58X_ERR_PROT_OVERLOAD:
740		if (net_ratelimit())
741			netdev_dbg(netdev, "Error OVERLOAD\n");
742		if (cf)
743			cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
744		break;
745
746	case ES58X_ERR_PROT_UNSPEC:
747		if (net_ratelimit())
748			netdev_dbg(netdev, "Unspecified error\n");
749		if (cf)
750			cf->can_id |= CAN_ERR_PROT;
751		break;
752
753	default:
754		if (net_ratelimit())
755			netdev_err(netdev,
756				   "%s: Unspecified error code 0x%04X\n",
757				   __func__, (int)error);
758		if (cf)
759			cf->can_id |= CAN_ERR_PROT;
760		break;
761	}
762
763	switch (event) {
764	case ES58X_EVENT_OK:	/* 0: No event */
765		break;
766
767	case ES58X_EVENT_CRTL_ACTIVE:
768		if (can->state == CAN_STATE_BUS_OFF) {
769			netdev_err(netdev,
770				   "%s: state transition: BUS OFF -> ACTIVE\n",
771				   __func__);
772		}
773		if (net_ratelimit())
774			netdev_dbg(netdev, "Event CAN BUS ACTIVE\n");
775		if (cf)
776			cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
777		can->state = CAN_STATE_ERROR_ACTIVE;
778		break;
779
780	case ES58X_EVENT_CRTL_PASSIVE:
781		if (net_ratelimit())
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
785		 * default.
786		 */
787		if (cf) {
788			cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
789			cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
790		}
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++;
796		break;
797
798	case ES58X_EVENT_CRTL_WARNING:
799		if (net_ratelimit())
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
803		 * default.
804		 */
805		if (cf) {
806			cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
807			cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
808		}
809		if (can->state < CAN_STATE_BUS_OFF)
810			can->state = CAN_STATE_ERROR_WARNING;
811		can_stats->error_warning++;
812		break;
813
814	case ES58X_EVENT_BUSOFF:
815		if (net_ratelimit())
816			netdev_dbg(netdev, "Event CAN BUS OFF\n");
817		if (cf)
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;
823			can_bus_off(netdev);
824			ret = can->do_set_mode(netdev, CAN_MODE_STOP);
825		}
826		break;
827
828	case ES58X_EVENT_SINGLE_WIRE:
829		if (net_ratelimit())
830			netdev_warn(netdev,
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.
834		 */
835		if (cf) {
836			cf->data[4] |= CAN_ERR_TRX_CANH_NO_WIRE;
837			cf->data[4] |= CAN_ERR_TRX_CANL_NO_WIRE;
838		}
839		break;
840
841	default:
842		if (net_ratelimit())
843			netdev_err(netdev,
844				   "%s: Unspecified event code 0x%04X\n",
845				   __func__, (int)event);
846		if (cf)
847			cf->can_id |= CAN_ERR_CRTL;
848		break;
849	}
850
851	if (cf) {
852		if (cf->data[1])
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++;
857		}
858		if (cf->data[4])
859			cf->can_id |= CAN_ERR_TRX;
860
861		es58x_set_skb_timestamp(netdev, skb, timestamp);
862		netif_rx(skb);
863	}
864
865	if ((event & ES58X_EVENT_CRTL_PASSIVE) &&
866	    priv->err_passive_before_rtx_success == ES58X_CONSECUTIVE_ERR_PASSIVE_MAX) {
867		netdev_info(netdev,
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);
872	}
873
874	return ret;
875}
876
877/**
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.
880 *
881 * The final line (return "<unknown>") should not be reached. If this
882 * is the case, there is an implementation bug.
883 *
884 * Return: a readable description of the @cmd_ret_type.
885 */
886static const char *es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type)
887{
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:
898		return "Reset RX";
899	case ES58X_RET_TYPE_RESET_TX:
900		return "Reset TX";
901	case ES58X_RET_TYPE_DEVICE_ERR:
902		return "Device error";
903	}
904
905	return "<unknown>";
906};
907
908/**
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.
914 *
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.
919 *
920 * In contrast with es58x_rx_cmd_ret_u32(), the network device is
921 * unknown.
922 *
923 * Return: zero on success, return errno when any error occurs.
924 */
925int 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)
928{
929	const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type);
930
931	switch (rx_cmd_ret_u8) {
932	case ES58X_RET_U8_OK:
933		dev_dbg_ratelimited(dev, "%s: OK\n", ret_desc);
934		return 0;
935
936	case ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE:
937		dev_err(dev, "%s: unspecified failure\n", ret_desc);
938		return -EBADMSG;
939
940	case ES58X_RET_U8_ERR_NO_MEM:
941		dev_err(dev, "%s: device ran out of memory\n", ret_desc);
942		return -ENOMEM;
943
944	case ES58X_RET_U8_ERR_BAD_CRC:
945		dev_err(dev, "%s: CRC of previous command is incorrect\n",
946			ret_desc);
947		return -EIO;
948
949	default:
950		dev_err(dev, "%s: returned unknown value: 0x%02X\n",
951			ret_desc, rx_cmd_ret_u8);
952		return -EBADMSG;
953	}
954}
955
956/**
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.
962 *
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.
967 *
968 * Return: zero on success, errno when any error occurs.
969 */
970int 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)
973{
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);
977
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);
985			netdev_info(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);
990			break;
991
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);
995			break;
996
997		default:
998			netdev_dbg(netdev, "%s: OK\n", ret_desc);
999			break;
1000		}
1001		return 0;
1002
1003	case ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE:
1004		if (cmd_ret_type == ES58X_RET_TYPE_ENABLE_CHANNEL) {
1005			int ret;
1006
1007			netdev_warn(netdev,
1008				    "%s: channel is already opened, closing and re-opening it to reflect new configuration\n",
1009				    ret_desc);
1010			ret = ops->disable_channel(es58x_priv(netdev));
1011			if (ret)
1012				return ret;
1013			return ops->enable_channel(es58x_priv(netdev));
1014		}
1015		if (cmd_ret_type == ES58X_RET_TYPE_DISABLE_CHANNEL) {
1016			netdev_info(netdev,
1017				    "%s: channel is already closed\n", ret_desc);
1018			return 0;
1019		}
1020		netdev_err(netdev,
1021			   "%s: unspecified failure\n", ret_desc);
1022		return -EBADMSG;
1023
1024	case ES58X_RET_U32_ERR_NO_MEM:
1025		netdev_err(netdev, "%s: device ran out of memory\n", ret_desc);
1026		return -ENOMEM;
1027
1028	case ES58X_RET_U32_WARN_PARAM_ADJUSTED:
1029		netdev_warn(netdev,
1030			    "%s: some incompatible parameters have been adjusted\n",
1031			    ret_desc);
1032		return 0;
1033
1034	case ES58X_RET_U32_WARN_TX_MAYBE_REORDER:
1035		netdev_warn(netdev,
1036			    "%s: TX messages might have been reordered\n",
1037			    ret_desc);
1038		return 0;
1039
1040	case ES58X_RET_U32_ERR_TIMEDOUT:
1041		netdev_err(netdev, "%s: command timed out\n", ret_desc);
1042		return -ETIMEDOUT;
1043
1044	case ES58X_RET_U32_ERR_FIFO_FULL:
1045		netdev_warn(netdev, "%s: fifo is full\n", ret_desc);
1046		return 0;
1047
1048	case ES58X_RET_U32_ERR_BAD_CONFIG:
1049		netdev_err(netdev, "%s: bad configuration\n", ret_desc);
1050		return -EINVAL;
1051
1052	case ES58X_RET_U32_ERR_NO_RESOURCE:
1053		netdev_err(netdev, "%s: no resource available\n", ret_desc);
1054		return -EBUSY;
1055
1056	default:
1057		netdev_err(netdev, "%s returned unknown value: 0x%08X\n",
1058			   ret_desc, rx_cmd_ret_u32);
1059		return -EBADMSG;
1060	}
1061}
1062
1063/**
1064 * es58x_increment_rx_errors() - Increment the network devices' error
1065 *	count.
1066 * @es58x_dev: ES58X device.
1067 *
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.
1072 */
1073static void es58x_increment_rx_errors(struct es58x_device *es58x_dev)
1074{
1075	int i;
1076
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++;
1080}
1081
1082/**
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
1086 *	be aligned.
1087 *
1088 * Sends the URB command to the device specific function. Manages the
1089 * errors thrown back by those functions.
1090 */
1091static void es58x_handle_urb_cmd(struct es58x_device *es58x_dev,
1092				 const union es58x_urb_cmd *urb_cmd)
1093{
1094	const struct es58x_operators *ops = es58x_dev->ops;
1095	size_t cmd_len;
1096	int i, ret;
1097
1098	ret = ops->handle_urb_cmd(es58x_dev, urb_cmd);
1099	switch (ret) {
1100	case 0:		/* OK */
1101		return;
1102
1103	case -ENODEV:
1104		dev_err_ratelimited(es58x_dev->dev, "Device is not ready\n");
1105		break;
1106
1107	case -EINVAL:
1108	case -EMSGSIZE:
1109	case -EBADRQC:
1110	case -EBADMSG:
1111	case -ECHRNG:
1112	case -ETIMEDOUT:
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",
1117			ERR_PTR(ret));
1118		es58x_print_hex_dump(urb_cmd, cmd_len);
1119		break;
1120
1121	case -EFAULT:
1122	case -ENOMEM:
1123	case -EIO:
1124	default:
1125		dev_crit(es58x_dev->dev,
1126			 "ops->handle_urb_cmd() returned error %pe, detaching all network devices\n",
1127			 ERR_PTR(ret));
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);
1133		break;
1134	}
1135
1136	/* Because the urb command could not fully be parsed,
1137	 * channel_id is not confirmed. Incrementing rx_errors count
1138	 * of all channels.
1139	 */
1140	es58x_increment_rx_errors(es58x_dev);
1141}
1142
1143/**
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
1147 *	be aligned.
1148 * @urb_actual_len: The actual length of the URB command.
1149 *
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.
1152 *
1153 * Return:
1154 * the length of the first message of the URB on success.
1155 *
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).
1159 *
1160 * -EOVERFLOW if the length is bigger than the maximum expected one.
1161 *
1162 * -EBADRQC if the start of frame does not match the expected value.
1163 */
1164static signed int es58x_check_rx_urb(struct es58x_device *es58x_dev,
1165				     const union es58x_urb_cmd *urb_cmd,
1166				     u32 urb_actual_len)
1167{
1168	const struct device *dev = es58x_dev->dev;
1169	const struct es58x_parameters *param = es58x_dev->param;
1170	u16 sof, msg_len;
1171	signed int urb_cmd_len, ret;
1172
1173	if (urb_actual_len < param->urb_cmd_header_len) {
1174		dev_vdbg(dev,
1175			 "%s: Received %d bytes [%*ph]: header incomplete\n",
1176			 __func__, urb_actual_len, urb_actual_len,
1177			 urb_cmd->raw_cmd);
1178		return -ENODATA;
1179	}
1180
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);
1186		return -EBADRQC;
1187	}
1188
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",
1194				    __func__,
1195				    param->rx_urb_cmd_max_len, urb_cmd_len);
1196		return -EOVERFLOW;
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);
1200		return -ENODATA;
1201	}
1202
1203	ret = es58x_check_crc(es58x_dev, urb_cmd, urb_cmd_len);
1204	if (ret)
1205		return ret;
1206
1207	return urb_cmd_len;
1208}
1209
1210/**
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.
1215 *
1216 * Concatenates @raw_cmd_len bytes of @raw_cmd to the end of the URB
1217 * command buffer.
1218 *
1219 * Return: zero on success, -EMSGSIZE if not enough space is available
1220 * to do the copy.
1221 */
1222static int es58x_copy_to_cmd_buf(struct es58x_device *es58x_dev,
1223				 u8 *raw_cmd, int raw_cmd_len)
1224{
1225	if (es58x_dev->rx_cmd_buf_len + raw_cmd_len >
1226	    es58x_dev->param->rx_urb_cmd_max_len)
1227		return -EMSGSIZE;
1228
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;
1232
1233	return 0;
1234}
1235
1236/**
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.
1241 *
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.
1245 *
1246 * This function looks for the next start of frame in the urb buffer
1247 * in order to try to recover.
1248 *
1249 * Such behavior was not observed on the devices of the ES58X FD
1250 * family and only seems to impact the ES581.4.
1251 *
1252 * Return: the number of bytes dropped on success, -EBADMSG if recovery failed.
1253 */
1254static int es58x_split_urb_try_recovery(struct es58x_device *es58x_dev,
1255					u8 *raw_cmd, size_t raw_cmd_len)
1256{
1257	union es58x_urb_cmd *urb_cmd;
1258	signed int urb_cmd_len;
1259	u16 sof;
1260	int dropped_bytes = 0;
1261
1262	es58x_increment_rx_errors(es58x_dev);
1263
1264	while (raw_cmd_len > sizeof(sof)) {
1265		urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1266		sof = get_unaligned_le16(&urb_cmd->sof);
1267
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",
1274						     dropped_bytes,
1275						     urb_cmd_len);
1276				return dropped_bytes;
1277			}
1278		}
1279		raw_cmd++;
1280		raw_cmd_len--;
1281		dropped_bytes++;
1282	}
1283
1284	dev_warn_ratelimited(es58x_dev->dev, "%s: Recovery failed\n", __func__);
1285	return -EBADMSG;
1286}
1287
1288/**
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.
1293 *
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.
1297 *
1298 * Return:
1299 * number of bytes consumed from @urb if successful.
1300 *
1301 * -ENODATA if the URB command is still incomplete.
1302 *
1303 * -EBADMSG if the URB command is incorrect.
1304 */
1305static signed int es58x_handle_incomplete_cmd(struct es58x_device *es58x_dev,
1306					      struct urb *urb)
1307{
1308	size_t cpy_len;
1309	signed int urb_cmd_len, tmp_cmd_buf_len, ret;
1310
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);
1315	if (ret < 0)
1316		return ret;
1317
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) {
1321		return -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;
1333	}
1334
1335	es58x_handle_urb_cmd(es58x_dev, &es58x_dev->rx_cmd_buf);
1336	return urb_cmd_len - tmp_cmd_buf_len;	/* consumed length */
1337}
1338
1339/**
1340 * es58x_split_urb() - Cut the received URB in individual URB commands.
1341 * @es58x_dev: ES58X device.
1342 * @urb: last urb buffer received.
1343 *
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.
1347 *
1348 * Return:
1349 * number of bytes consumed from @urb if successful.
1350 *
1351 * -ENODATA if the URB command is incomplete.
1352 *
1353 * -EBADMSG if the URB command is incorrect.
1354 */
1355static signed int es58x_split_urb(struct es58x_device *es58x_dev,
1356				  struct urb *urb)
1357{
1358	union es58x_urb_cmd *urb_cmd;
1359	u8 *raw_cmd = urb->transfer_buffer;
1360	s32 raw_cmd_len = urb->actual_length;
1361	int ret;
1362
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;
1367		if (ret < 0)
1368			return ret;
1369
1370		raw_cmd += ret;
1371		raw_cmd_len -= ret;
1372	}
1373
1374	while (raw_cmd_len > 0) {
1375		if (raw_cmd[0] == ES58X_HEARTBEAT) {
1376			raw_cmd++;
1377			raw_cmd_len--;
1378			continue;
1379		}
1380		urb_cmd = (union es58x_urb_cmd *)raw_cmd;
1381		ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len);
1382		if (ret > 0) {
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);
1386			return -ENODATA;
1387		} else if (ret < 0) {
1388			ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd,
1389							   raw_cmd_len);
1390			if (ret < 0)
1391				return ret;
1392		}
1393		raw_cmd += ret;
1394		raw_cmd_len -= ret;
1395	}
1396
1397	return 0;
1398}
1399
1400/**
1401 * es58x_read_bulk_callback() - Callback for reading data from device.
1402 * @urb: last urb buffer received.
1403 *
1404 * This function gets eventually called each time an URB is received
1405 * from the ES58X device.
1406 *
1407 * Checks urb status, calls read function and resubmits urb read
1408 * operation.
1409 */
1410static void es58x_read_bulk_callback(struct urb *urb)
1411{
1412	struct es58x_device *es58x_dev = urb->context;
1413	const struct device *dev = es58x_dev->dev;
1414	int i, ret;
1415
1416	switch (urb->status) {
1417	case 0:		/* success */
1418		break;
1419
1420	case -EOVERFLOW:
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);
1425		goto resubmit_urb;
1426
1427	case -EPROTO:
1428		dev_warn_ratelimited(dev, "%s: error %pe. Device unplugged?\n",
1429				     __func__, ERR_PTR(urb->status));
1430		goto free_urb;
1431
1432	case -ENOENT:
1433	case -EPIPE:
1434		dev_err_ratelimited(dev, "%s: error %pe\n",
1435				    __func__, ERR_PTR(urb->status));
1436		goto free_urb;
1437
1438	case -ESHUTDOWN:
1439		dev_dbg_ratelimited(dev, "%s: error %pe\n",
1440				    __func__, ERR_PTR(urb->status));
1441		goto free_urb;
1442
1443	default:
1444		dev_err_ratelimited(dev, "%s: error %pe\n",
1445				    __func__, ERR_PTR(urb->status));
1446		goto resubmit_urb;
1447	}
1448
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",
1452			ERR_PTR(ret));
1453		es58x_print_hex_dump_debug(urb->transfer_buffer,
1454					   urb->actual_length);
1455
1456		/* Because the urb command could not be parsed,
1457		 * channel_id is not confirmed. Incrementing rx_errors
1458		 * count of all channels.
1459		 */
1460		es58x_increment_rx_errors(es58x_dev);
1461	}
1462
1463 resubmit_urb:
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]);
1469	} else if (ret)
1470		dev_err_ratelimited(dev,
1471				    "Failed resubmitting read bulk urb: %pe\n",
1472				    ERR_PTR(ret));
1473	return;
1474
1475 free_urb:
1476	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
1477			  urb->transfer_buffer, urb->transfer_dma);
1478}
1479
1480/**
1481 * es58x_write_bulk_callback() - Callback after writing data to the device.
1482 * @urb: urb buffer which was previously submitted.
1483 *
1484 * This function gets eventually called each time an URB was sent to
1485 * the ES58X device.
1486 *
1487 * Puts the @urb back to the urbs idle anchor and tries to restart the
1488 * network queue.
1489 */
1490static void es58x_write_bulk_callback(struct urb *urb)
1491{
1492	struct net_device *netdev = urb->context;
1493	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1494
1495	switch (urb->status) {
1496	case 0:		/* success */
1497		break;
1498
1499	case -EOVERFLOW:
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);
1505		break;
1506
1507	case -ENOENT:
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);
1514		return;
1515
1516	default:
1517		if (net_ratelimit())
1518			netdev_info(netdev, "%s: error %pe\n",
1519				    __func__, ERR_PTR(urb->status));
1520		break;
1521	}
1522
1523	usb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);
1524	atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
1525}
1526
1527/**
1528 * es58x_alloc_urb() - Allocate memory for an URB and its transfer
1529 *	buffer.
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.
1535 *
1536 * Allocates an URB and its @transfer_buffer and set its @transfer_dma
1537 * address.
1538 *
1539 * This function is used at start-up to allocate all RX URBs at once
1540 * and during run time for TX URBs.
1541 *
1542 * Return: zero on success, -ENOMEM if no memory is available.
1543 */
1544static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb,
1545			   u8 **buf, size_t buf_len, gfp_t mem_flags)
1546{
1547	*urb = usb_alloc_urb(0, mem_flags);
1548	if (!*urb) {
1549		dev_err(es58x_dev->dev, "No memory left for URBs\n");
1550		return -ENOMEM;
1551	}
1552
1553	*buf = usb_alloc_coherent(es58x_dev->udev, buf_len,
1554				  mem_flags, &(*urb)->transfer_dma);
1555	if (!*buf) {
1556		dev_err(es58x_dev->dev, "No memory left for USB buffer\n");
1557		usb_free_urb(*urb);
1558		return -ENOMEM;
1559	}
1560
1561	(*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1562
1563	return 0;
1564}
1565
1566/**
1567 * es58x_get_tx_urb() - Get an URB for transmission.
1568 * @es58x_dev: ES58X device.
1569 *
1570 * Gets an URB from the idle urbs anchor or allocate a new one if the
1571 * anchor is empty.
1572 *
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().
1578 *
1579 * Return: a pointer to an URB on success, NULL if no memory is
1580 * available.
1581 */
1582static struct urb *es58x_get_tx_urb(struct es58x_device *es58x_dev)
1583{
1584	atomic_t *idle_cnt = &es58x_dev->tx_urbs_idle_cnt;
1585	struct urb *urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1586
1587	if (!urb) {
1588		size_t tx_buf_len;
1589		u8 *buf;
1590
1591		tx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;
1592		if (es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,
1593				    GFP_ATOMIC))
1594			return NULL;
1595
1596		usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1597				  buf, tx_buf_len, es58x_write_bulk_callback,
1598				  NULL);
1599		return urb;
1600	}
1601
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);
1605
1606		if (!tmp)
1607			break;
1608		usb_free_coherent(tmp->dev,
1609				  es58x_dev->param->tx_urb_cmd_max_len,
1610				  tmp->transfer_buffer, tmp->transfer_dma);
1611		usb_free_urb(tmp);
1612	}
1613
1614	return urb;
1615}
1616
1617/**
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.
1622 *
1623 * Return: zero on success, errno when any error occurs.
1624 */
1625static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb,
1626			    struct net_device *netdev)
1627{
1628	int ret;
1629
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);
1634	if (ret) {
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);
1641	}
1642	usb_free_urb(urb);
1643
1644	return ret;
1645}
1646
1647/**
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.
1655 *
1656 * Creates an URB command from a given message, sets the header and the
1657 * CRC and then submits it.
1658 *
1659 * Return: zero on success, errno when any error occurs.
1660 */
1661int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
1662		   const void *msg, u16 msg_len, int channel_idx)
1663{
1664	struct net_device *netdev;
1665	union es58x_urb_cmd *urb_cmd;
1666	struct urb *urb;
1667	int urb_cmd_len;
1668
1669	if (channel_idx == ES58X_CHANNEL_IDX_NA)
1670		netdev = es58x_dev->netdev[0];	/* Default to first channel */
1671	else
1672		netdev = es58x_dev->netdev[channel_idx];
1673
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)
1676		return -EOVERFLOW;
1677
1678	urb = es58x_get_tx_urb(es58x_dev);
1679	if (!urb)
1680		return -ENOMEM;
1681
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],
1686	       msg, msg_len);
1687	urb->transfer_buffer_length = urb_cmd_len;
1688
1689	return es58x_submit_urb(es58x_dev, urb, netdev);
1690}
1691
1692/**
1693 * es58x_alloc_rx_urbs() - Allocate RX URBs.
1694 * @es58x_dev: ES58X device.
1695 *
1696 * Allocate URBs for reception and anchor them.
1697 *
1698 * Return: zero on success, errno when any error occurs.
1699 */
1700static int es58x_alloc_rx_urbs(struct es58x_device *es58x_dev)
1701{
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);
1705	struct urb *urb;
1706	u8 *buf;
1707	int i;
1708	int ret = -EINVAL;
1709
1710	for (i = 0; i < param->rx_urb_max; i++) {
1711		ret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len,
1712				      GFP_KERNEL);
1713		if (ret)
1714			break;
1715
1716		usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1717				  buf, rx_buf_len, es58x_read_bulk_callback,
1718				  es58x_dev);
1719		usb_anchor_urb(urb, &es58x_dev->rx_urbs);
1720
1721		ret = usb_submit_urb(urb, GFP_KERNEL);
1722		if (ret) {
1723			usb_unanchor_urb(urb);
1724			usb_free_coherent(es58x_dev->udev, rx_buf_len,
1725					  buf, urb->transfer_dma);
1726			usb_free_urb(urb);
1727			break;
1728		}
1729		usb_free_urb(urb);
1730	}
1731
1732	if (i == 0) {
1733		dev_err(dev, "%s: Could not setup any rx URBs\n", __func__);
1734		return ret;
1735	}
1736	dev_dbg(dev, "%s: Allocated %d rx URBs each of size %u\n",
1737		__func__, i, rx_buf_len);
1738
1739	return ret;
1740}
1741
1742/**
1743 * es58x_free_urbs() - Free all the TX and RX URBs.
1744 * @es58x_dev: ES58X device.
1745 */
1746static void es58x_free_urbs(struct es58x_device *es58x_dev)
1747{
1748	struct urb *urb;
1749
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",
1752			__func__);
1753		usb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy);
1754	}
1755
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);
1759		usb_free_urb(urb);
1760		atomic_dec(&es58x_dev->tx_urbs_idle_cnt);
1761	}
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));
1766
1767	usb_kill_anchored_urbs(&es58x_dev->rx_urbs);
1768}
1769
1770/**
1771 * es58x_open() - Enable the network device.
1772 * @netdev: CAN network device.
1773 *
1774 * Called when the network transitions to the up state. Allocate the
1775 * URB resources if needed and open the channel.
1776 *
1777 * Return: zero on success, errno when any error occurs.
1778 */
1779static int es58x_open(struct net_device *netdev)
1780{
1781	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1782	int ret;
1783
1784	if (!es58x_dev->opened_channel_cnt) {
1785		ret = es58x_alloc_rx_urbs(es58x_dev);
1786		if (ret)
1787			return ret;
1788
1789		ret = es58x_set_realtime_diff_ns(es58x_dev);
1790		if (ret)
1791			goto free_urbs;
1792	}
1793
1794	ret = open_candev(netdev);
1795	if (ret)
1796		goto free_urbs;
1797
1798	ret = es58x_dev->ops->enable_channel(es58x_priv(netdev));
1799	if (ret)
1800		goto free_urbs;
1801
1802	es58x_dev->opened_channel_cnt++;
1803	netif_start_queue(netdev);
1804
1805	return ret;
1806
1807 free_urbs:
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));
1812
1813	return ret;
1814}
1815
1816/**
1817 * es58x_stop() - Disable the network device.
1818 * @netdev: CAN network device.
1819 *
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.
1823 *
1824 * Return: zero on success, errno when any error occurs.
1825 */
1826static int es58x_stop(struct net_device *netdev)
1827{
1828	struct es58x_priv *priv = es58x_priv(netdev);
1829	struct es58x_device *es58x_dev = priv->es58x_dev;
1830	int ret;
1831
1832	netif_stop_queue(netdev);
1833	ret = es58x_dev->ops->disable_channel(priv);
1834	if (ret)
1835		return ret;
1836
1837	priv->can.state = CAN_STATE_STOPPED;
1838	es58x_can_reset_echo_fifo(netdev);
1839	close_candev(netdev);
1840
1841	es58x_flush_pending_tx_msg(netdev);
1842
1843	es58x_dev->opened_channel_cnt--;
1844	if (!es58x_dev->opened_channel_cnt)
1845		es58x_free_urbs(es58x_dev);
1846
1847	return 0;
1848}
1849
1850/**
1851 * es58x_xmit_commit() - Send the bulk urb.
1852 * @netdev: CAN network device.
1853 *
1854 * Do the bulk send. This function should be called only once by bulk
1855 * transmission.
1856 *
1857 * Return: zero on success, errno when any error occurs.
1858 */
1859static int es58x_xmit_commit(struct net_device *netdev)
1860{
1861	struct es58x_priv *priv = es58x_priv(netdev);
1862	int ret;
1863
1864	if (!es58x_is_can_state_active(netdev))
1865		return -ENETDOWN;
1866
1867	if (es58x_is_echo_skb_threshold_reached(priv))
1868		netif_stop_queue(netdev);
1869
1870	ret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev);
1871	if (ret == 0)
1872		priv->tx_urb = NULL;
1873
1874	return ret;
1875}
1876
1877/**
1878 * es58x_xmit_more() - Can we put more packets?
1879 * @priv: ES58X private parameters related to the network device.
1880 *
1881 * Return: true if we can put more, false if it is time to send.
1882 */
1883static bool es58x_xmit_more(struct es58x_priv *priv)
1884{
1885	unsigned int free_slots =
1886	    priv->can.echo_skb_max - (priv->tx_head - priv->tx_tail);
1887
1888	return netdev_xmit_more() && free_slots > 0 &&
1889		priv->tx_can_msg_cnt < priv->es58x_dev->param->tx_bulk_max;
1890}
1891
1892/**
1893 * es58x_start_xmit() - Transmit an skb.
1894 * @skb: socket buffer of a CAN message.
1895 * @netdev: CAN network device.
1896 *
1897 * Called when a packet needs to be transmitted.
1898 *
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).
1902 *
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.
1905 *
1906 * Return: NETDEV_TX_OK regardless of if we could transmit the @skb or
1907 *	had to drop it.
1908 */
1909static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
1910				    struct net_device *netdev)
1911{
1912	struct es58x_priv *priv = es58x_priv(netdev);
1913	struct es58x_device *es58x_dev = priv->es58x_dev;
1914	unsigned int frame_len;
1915	int ret;
1916
1917	if (can_dev_dropped_skb(netdev, skb)) {
1918		if (priv->tx_urb)
1919			goto xmit_commit;
1920		return NETDEV_TX_OK;
1921	}
1922
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);
1926		if (ret)
1927			goto drop_skb;
1928	}
1929
1930	if (!priv->tx_urb) {
1931		priv->tx_urb = es58x_get_tx_urb(es58x_dev);
1932		if (!priv->tx_urb) {
1933			ret = -ENOMEM;
1934			goto drop_skb;
1935		}
1936		priv->tx_can_msg_cnt = 0;
1937		priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
1938	}
1939
1940	ret = es58x_dev->ops->tx_can_msg(priv, skb);
1941	if (ret)
1942		goto drop_skb;
1943
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,
1947			       frame_len);
1948	if (ret)
1949		goto xmit_failure;
1950	netdev_sent_queue(netdev, frame_len);
1951
1952	priv->tx_head++;
1953	priv->tx_can_msg_cnt++;
1954
1955 xmit_commit:
1956	if (!es58x_xmit_more(priv)) {
1957		ret = es58x_xmit_commit(netdev);
1958		if (ret)
1959			goto xmit_failure;
1960	}
1961
1962	return NETDEV_TX_OK;
1963
1964 drop_skb:
1965	dev_kfree_skb(skb);
1966	netdev->stats.tx_dropped++;
1967 xmit_failure:
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;
1973}
1974
1975static 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,
1980};
1981
1982static const struct ethtool_ops es58x_ethtool_ops = {
1983	.get_ts_info = can_ethtool_op_get_ts_info_hwts,
1984};
1985
1986/**
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
1990 *
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
1994 * callers).
1995 *
1996 * Return: zero on success, errno when any error occurs.
1997 */
1998static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
1999{
2000	struct es58x_priv *priv = es58x_priv(netdev);
2001
2002	switch (mode) {
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);
2007
2008		case CAN_STATE_STOPPED:
2009			return es58x_open(netdev);
2010
2011		case CAN_STATE_ERROR_ACTIVE:
2012		case CAN_STATE_ERROR_WARNING:
2013		case CAN_STATE_ERROR_PASSIVE:
2014		default:
2015			return 0;
2016		}
2017
2018	case CAN_MODE_STOP:
2019		switch (priv->can.state) {
2020		case CAN_STATE_STOPPED:
2021			return 0;
2022
2023		case CAN_STATE_ERROR_ACTIVE:
2024		case CAN_STATE_ERROR_WARNING:
2025		case CAN_STATE_ERROR_PASSIVE:
2026		case CAN_STATE_BUS_OFF:
2027		default:
2028			return priv->es58x_dev->ops->disable_channel(priv);
2029		}
2030
2031	case CAN_MODE_SLEEP:
2032	default:
2033		return -EOPNOTSUPP;
2034	}
2035}
2036
2037/**
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.
2042 *
2043 * Return: zero on success, errno if devlink port could not be
2044 *	properly registered.
2045 */
2046static int es58x_init_priv(struct es58x_device *es58x_dev,
2047			   struct es58x_priv *priv, int channel_idx)
2048{
2049	struct devlink_port_attrs attrs = {
2050		.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL,
2051	};
2052	const struct es58x_parameters *param = es58x_dev->param;
2053	struct can_priv *can = &priv->can;
2054
2055	priv->es58x_dev = es58x_dev;
2056	priv->channel_idx = channel_idx;
2057	priv->tx_urb = NULL;
2058	priv->tx_can_msg_cnt = 0;
2059
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;
2064	}
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;
2070
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);
2074}
2075
2076/**
2077 * es58x_init_netdev() - Initialize the network device.
2078 * @es58x_dev: ES58X device.
2079 * @channel_idx: Index of the network device.
2080 *
2081 * Return: zero on success, errno when any error occurs.
2082 */
2083static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
2084{
2085	struct net_device *netdev;
2086	struct device *dev = es58x_dev->dev;
2087	int ret;
2088
2089	netdev = alloc_candev(sizeof(struct es58x_priv),
2090			      es58x_dev->param->fifo_mask + 1);
2091	if (!netdev) {
2092		dev_err(dev, "Could not allocate candev\n");
2093		return -ENOMEM;
2094	}
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);
2098	if (ret)
2099		goto free_candev;
2100	SET_NETDEV_DEVLINK_PORT(netdev, &es58x_priv(netdev)->devlink_port);
2101
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;
2106
2107	ret = register_candev(netdev);
2108	if (ret)
2109		goto devlink_port_unregister;
2110
2111	netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
2112				       es58x_dev->param->dql_min_limit);
2113
2114	return ret;
2115
2116 devlink_port_unregister:
2117	devlink_port_unregister(&es58x_priv(netdev)->devlink_port);
2118 free_candev:
2119	es58x_dev->netdev[channel_idx] = NULL;
2120	free_candev(netdev);
2121	return ret;
2122}
2123
2124/**
2125 * es58x_free_netdevs() - Release all network resources of the device.
2126 * @es58x_dev: ES58X device.
2127 */
2128static void es58x_free_netdevs(struct es58x_device *es58x_dev)
2129{
2130	int i;
2131
2132	for (i = 0; i < es58x_dev->num_can_ch; i++) {
2133		struct net_device *netdev = es58x_dev->netdev[i];
2134
2135		if (!netdev)
2136			continue;
2137		unregister_candev(netdev);
2138		devlink_port_unregister(&es58x_priv(netdev)->devlink_port);
2139		es58x_dev->netdev[i] = NULL;
2140		free_candev(netdev);
2141	}
2142}
2143
2144/**
2145 * es58x_init_es58x_dev() - Initialize the ES58X device.
2146 * @intf: USB interface.
2147 * @driver_info: Quirks of the device.
2148 *
2149 * Return: pointer to an ES58X device on success, error pointer when
2150 *	any error occurs.
2151 */
2152static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf,
2153						 kernel_ulong_t driver_info)
2154{
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;
2162	int ret;
2163
2164	dev_info(dev, "Starting %s %s (Serial Number %s)\n",
2165		 udev->manufacturer, udev->product, udev->serial);
2166
2167	ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
2168					NULL, NULL);
2169	if (ret)
2170		return ERR_PTR(ret);
2171
2172	if (driver_info & ES58X_FD_FAMILY) {
2173		param = &es58x_fd_param;
2174		ops = &es58x_fd_ops;
2175	} else {
2176		param = &es581_4_param;
2177		ops = &es581_4_ops;
2178	}
2179
2180	devlink = devlink_alloc(&es58x_dl_ops, es58x_sizeof_es58x_device(param),
2181				dev);
2182	if (!devlink)
2183		return ERR_PTR(-ENOMEM);
2184
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;
2190
2191	if (driver_info & ES58X_DUAL_CHANNEL)
2192		es58x_dev->num_can_ch = 2;
2193	else
2194		es58x_dev->num_can_ch = 1;
2195
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);
2201
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);
2206
2207	return es58x_dev;
2208}
2209
2210/**
2211 * es58x_probe() - Initialize the USB device.
2212 * @intf: USB interface.
2213 * @id: USB device ID.
2214 *
2215 * Return: zero on success, -ENODEV if the interface is not supported
2216 * or errno when any other error occurs.
2217 */
2218static int es58x_probe(struct usb_interface *intf,
2219		       const struct usb_device_id *id)
2220{
2221	struct es58x_device *es58x_dev;
2222	int ch_idx;
2223
2224	es58x_dev = es58x_init_es58x_dev(intf, id->driver_info);
2225	if (IS_ERR(es58x_dev))
2226		return PTR_ERR(es58x_dev);
2227
2228	es58x_parse_product_info(es58x_dev);
2229	devlink_register(priv_to_devlink(es58x_dev));
2230
2231	for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {
2232		int ret = es58x_init_netdev(es58x_dev, ch_idx);
2233
2234		if (ret) {
2235			es58x_free_netdevs(es58x_dev);
2236			return ret;
2237		}
2238	}
2239
2240	return 0;
2241}
2242
2243/**
2244 * es58x_disconnect() - Disconnect the USB device.
2245 * @intf: USB interface
2246 *
2247 * Called by the usb core when driver is unloaded or device is
2248 * removed.
2249 */
2250static void es58x_disconnect(struct usb_interface *intf)
2251{
2252	struct es58x_device *es58x_dev = usb_get_intfdata(intf);
2253
2254	dev_info(&intf->dev, "Disconnecting %s %s\n",
2255		 es58x_dev->udev->manufacturer, es58x_dev->udev->product);
2256
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);
2262}
2263
2264static struct usb_driver es58x_driver = {
2265	.name = KBUILD_MODNAME,
2266	.probe = es58x_probe,
2267	.disconnect = es58x_disconnect,
2268	.id_table = es58x_id_table
2269};
2270
2271module_usb_driver(es58x_driver);
2272