xref: /linux/drivers/net/can/usb/etas_es58x/es58x_core.c (revision fab183d632628381b466a41479489541ac0e29a0)
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-2025 Vincent Mailhol <mailhol@kernel.org>
11  */
12 
13 #include <linux/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 
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");
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. */
37 static 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 
57 MODULE_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  */
es58x_calculate_crc(const union es58x_urb_cmd * urb_cmd,u16 urb_len)82 static 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  */
es58x_get_crc(const union es58x_urb_cmd * urb_cmd,u16 urb_len)99 static 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  */
es58x_set_crc(union es58x_urb_cmd * urb_cmd,u16 urb_len)115 static 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  */
es58x_check_crc(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd,u16 urb_len)134 static 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  */
es58x_timestamp_to_ns(u64 timestamp)160 static 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  */
es58x_set_skb_timestamp(struct net_device * netdev,struct sk_buff * skb,u64 timestamp)175 static 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  */
es58x_rx_timestamp(struct es58x_device * es58x_dev,u64 timestamp)199 void 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  */
es58x_set_realtime_diff_ns(struct es58x_device * es58x_dev)227 static 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  */
es58x_is_can_state_active(struct net_device * netdev)259 static 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  */
es58x_is_echo_skb_threshold_reached(struct es58x_priv * priv)277 static 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  */
es58x_can_free_echo_skb_tail(struct net_device * netdev)294 static 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  */
es58x_can_get_echo_skb_recovery(struct net_device * netdev,u32 rcv_packet_idx)320 static 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  */
es58x_can_get_echo_skb(struct net_device * netdev,u32 rcv_packet_idx,u64 * tstamps,unsigned int pkts)382 int 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  */
es58x_can_reset_echo_fifo(struct net_device * netdev)471 static 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  */
es58x_flush_pending_tx_msg(struct net_device * netdev)492 static 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  */
es58x_tx_ack_msg(struct net_device * netdev,u16 tx_free_entries,enum es58x_ret_u32 rx_cmd_ret_u32)533 int 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  */
es58x_rx_can_msg(struct net_device * netdev,u64 timestamp,const u8 * data,canid_t can_id,enum es58x_flag es58x_flags,u8 dlc)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)
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  */
es58x_rx_err_msg(struct net_device * netdev,enum es58x_err error,enum es58x_event event,u64 timestamp)658 int 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  */
es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type)886 static 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  */
es58x_rx_cmd_ret_u8(struct device * dev,enum es58x_ret_type cmd_ret_type,enum es58x_ret_u8 rx_cmd_ret_u8)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)
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  */
es58x_rx_cmd_ret_u32(struct net_device * netdev,enum es58x_ret_type cmd_ret_type,enum es58x_ret_u32 rx_cmd_ret_u32)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)
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  */
es58x_increment_rx_errors(struct es58x_device * es58x_dev)1073 static 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  */
es58x_handle_urb_cmd(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd)1091 static 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  */
es58x_check_rx_urb(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd,u32 urb_actual_len)1164 static 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  */
es58x_copy_to_cmd_buf(struct es58x_device * es58x_dev,u8 * raw_cmd,int raw_cmd_len)1222 static 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  */
es58x_split_urb_try_recovery(struct es58x_device * es58x_dev,u8 * raw_cmd,size_t raw_cmd_len)1254 static 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  */
es58x_handle_incomplete_cmd(struct es58x_device * es58x_dev,struct urb * urb)1305 static 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  */
es58x_split_urb(struct es58x_device * es58x_dev,struct urb * urb)1355 static 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  */
es58x_read_bulk_callback(struct urb * urb)1410 static 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 	usb_anchor_urb(urb, &es58x_dev->rx_urbs);
1465 	ret = usb_submit_urb(urb, GFP_ATOMIC);
1466 	if (!ret)
1467 		return;
1468 
1469 	usb_unanchor_urb(urb);
1470 
1471 	if (ret == -ENODEV) {
1472 		for (i = 0; i < es58x_dev->num_can_ch; i++)
1473 			if (es58x_dev->netdev[i])
1474 				netif_device_detach(es58x_dev->netdev[i]);
1475 	} else
1476 		dev_err_ratelimited(dev,
1477 				    "Failed resubmitting read bulk urb: %pe\n",
1478 				    ERR_PTR(ret));
1479 
1480  free_urb:
1481 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
1482 			  urb->transfer_buffer, urb->transfer_dma);
1483 }
1484 
1485 /**
1486  * es58x_write_bulk_callback() - Callback after writing data to the device.
1487  * @urb: urb buffer which was previously submitted.
1488  *
1489  * This function gets eventually called each time an URB was sent to
1490  * the ES58X device.
1491  *
1492  * Puts the @urb back to the urbs idle anchor and tries to restart the
1493  * network queue.
1494  */
es58x_write_bulk_callback(struct urb * urb)1495 static void es58x_write_bulk_callback(struct urb *urb)
1496 {
1497 	struct net_device *netdev = urb->context;
1498 	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1499 
1500 	switch (urb->status) {
1501 	case 0:		/* success */
1502 		break;
1503 
1504 	case -EOVERFLOW:
1505 		if (net_ratelimit())
1506 			netdev_err(netdev, "%s: error %pe\n",
1507 				   __func__, ERR_PTR(urb->status));
1508 		es58x_print_hex_dump(urb->transfer_buffer,
1509 				     urb->transfer_buffer_length);
1510 		break;
1511 
1512 	case -ENOENT:
1513 		if (net_ratelimit())
1514 			netdev_dbg(netdev, "%s: error %pe\n",
1515 				   __func__, ERR_PTR(urb->status));
1516 		usb_free_coherent(urb->dev,
1517 				  es58x_dev->param->tx_urb_cmd_max_len,
1518 				  urb->transfer_buffer, urb->transfer_dma);
1519 		return;
1520 
1521 	default:
1522 		if (net_ratelimit())
1523 			netdev_info(netdev, "%s: error %pe\n",
1524 				    __func__, ERR_PTR(urb->status));
1525 		break;
1526 	}
1527 
1528 	usb_anchor_urb(urb, &es58x_dev->tx_urbs_idle);
1529 	atomic_inc(&es58x_dev->tx_urbs_idle_cnt);
1530 }
1531 
1532 /**
1533  * es58x_alloc_urb() - Allocate memory for an URB and its transfer
1534  *	buffer.
1535  * @es58x_dev: ES58X device.
1536  * @urb: URB to be allocated.
1537  * @buf: used to return DMA address of buffer.
1538  * @buf_len: requested buffer size.
1539  * @mem_flags: affect whether allocation may block.
1540  *
1541  * Allocates an URB and its @transfer_buffer and set its @transfer_dma
1542  * address.
1543  *
1544  * This function is used at start-up to allocate all RX URBs at once
1545  * and during run time for TX URBs.
1546  *
1547  * Return: zero on success, -ENOMEM if no memory is available.
1548  */
es58x_alloc_urb(struct es58x_device * es58x_dev,struct urb ** urb,u8 ** buf,size_t buf_len,gfp_t mem_flags)1549 static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb,
1550 			   u8 **buf, size_t buf_len, gfp_t mem_flags)
1551 {
1552 	*urb = usb_alloc_urb(0, mem_flags);
1553 	if (!*urb) {
1554 		dev_err(es58x_dev->dev, "No memory left for URBs\n");
1555 		return -ENOMEM;
1556 	}
1557 
1558 	*buf = usb_alloc_coherent(es58x_dev->udev, buf_len,
1559 				  mem_flags, &(*urb)->transfer_dma);
1560 	if (!*buf) {
1561 		dev_err(es58x_dev->dev, "No memory left for USB buffer\n");
1562 		usb_free_urb(*urb);
1563 		return -ENOMEM;
1564 	}
1565 
1566 	(*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1567 
1568 	return 0;
1569 }
1570 
1571 /**
1572  * es58x_get_tx_urb() - Get an URB for transmission.
1573  * @es58x_dev: ES58X device.
1574  *
1575  * Gets an URB from the idle urbs anchor or allocate a new one if the
1576  * anchor is empty.
1577  *
1578  * If there are more than ES58X_TX_URBS_MAX in the idle anchor, do
1579  * some garbage collection. The garbage collection is done here
1580  * instead of within es58x_write_bulk_callback() because
1581  * usb_free_coherent() should not be used in IRQ context:
1582  * c.f. WARN_ON(irqs_disabled()) in dma_free_attrs().
1583  *
1584  * Return: a pointer to an URB on success, NULL if no memory is
1585  * available.
1586  */
es58x_get_tx_urb(struct es58x_device * es58x_dev)1587 static struct urb *es58x_get_tx_urb(struct es58x_device *es58x_dev)
1588 {
1589 	atomic_t *idle_cnt = &es58x_dev->tx_urbs_idle_cnt;
1590 	struct urb *urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1591 
1592 	if (!urb) {
1593 		size_t tx_buf_len;
1594 		u8 *buf;
1595 
1596 		tx_buf_len = es58x_dev->param->tx_urb_cmd_max_len;
1597 		if (es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len,
1598 				    GFP_ATOMIC))
1599 			return NULL;
1600 
1601 		usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe,
1602 				  buf, tx_buf_len, es58x_write_bulk_callback,
1603 				  NULL);
1604 		return urb;
1605 	}
1606 
1607 	while (atomic_dec_return(idle_cnt) > ES58X_TX_URBS_MAX) {
1608 		/* Garbage collector */
1609 		struct urb *tmp = usb_get_from_anchor(&es58x_dev->tx_urbs_idle);
1610 
1611 		if (!tmp)
1612 			break;
1613 		usb_free_coherent(tmp->dev,
1614 				  es58x_dev->param->tx_urb_cmd_max_len,
1615 				  tmp->transfer_buffer, tmp->transfer_dma);
1616 		usb_free_urb(tmp);
1617 	}
1618 
1619 	return urb;
1620 }
1621 
1622 /**
1623  * es58x_submit_urb() - Send data to the device.
1624  * @es58x_dev: ES58X device.
1625  * @urb: URB to be sent.
1626  * @netdev: CAN network device.
1627  *
1628  * Return: zero on success, errno when any error occurs.
1629  */
es58x_submit_urb(struct es58x_device * es58x_dev,struct urb * urb,struct net_device * netdev)1630 static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb,
1631 			    struct net_device *netdev)
1632 {
1633 	int ret;
1634 
1635 	es58x_set_crc(urb->transfer_buffer, urb->transfer_buffer_length);
1636 	urb->context = netdev;
1637 	usb_anchor_urb(urb, &es58x_dev->tx_urbs_busy);
1638 	ret = usb_submit_urb(urb, GFP_ATOMIC);
1639 	if (ret) {
1640 		netdev_err(netdev, "%s: USB send urb failure: %pe\n",
1641 			   __func__, ERR_PTR(ret));
1642 		usb_unanchor_urb(urb);
1643 		usb_free_coherent(urb->dev,
1644 				  es58x_dev->param->tx_urb_cmd_max_len,
1645 				  urb->transfer_buffer, urb->transfer_dma);
1646 	}
1647 	usb_free_urb(urb);
1648 
1649 	return ret;
1650 }
1651 
1652 /**
1653  * es58x_send_msg() - Prepare an URB and submit it.
1654  * @es58x_dev: ES58X device.
1655  * @cmd_type: Command type.
1656  * @cmd_id: Command ID.
1657  * @msg: ES58X message to be sent.
1658  * @msg_len: Length of @msg.
1659  * @channel_idx: Index of the network device.
1660  *
1661  * Creates an URB command from a given message, sets the header and the
1662  * CRC and then submits it.
1663  *
1664  * Return: zero on success, errno when any error occurs.
1665  */
es58x_send_msg(struct es58x_device * es58x_dev,u8 cmd_type,u8 cmd_id,const void * msg,u16 msg_len,int channel_idx)1666 int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id,
1667 		   const void *msg, u16 msg_len, int channel_idx)
1668 {
1669 	struct net_device *netdev;
1670 	union es58x_urb_cmd *urb_cmd;
1671 	struct urb *urb;
1672 	int urb_cmd_len;
1673 
1674 	if (channel_idx == ES58X_CHANNEL_IDX_NA)
1675 		netdev = es58x_dev->netdev[0];	/* Default to first channel */
1676 	else
1677 		netdev = es58x_dev->netdev[channel_idx];
1678 
1679 	urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len);
1680 	if (urb_cmd_len > es58x_dev->param->tx_urb_cmd_max_len)
1681 		return -EOVERFLOW;
1682 
1683 	urb = es58x_get_tx_urb(es58x_dev);
1684 	if (!urb)
1685 		return -ENOMEM;
1686 
1687 	urb_cmd = urb->transfer_buffer;
1688 	es58x_dev->ops->fill_urb_header(urb_cmd, cmd_type, cmd_id,
1689 					channel_idx, msg_len);
1690 	memcpy(&urb_cmd->raw_cmd[es58x_dev->param->urb_cmd_header_len],
1691 	       msg, msg_len);
1692 	urb->transfer_buffer_length = urb_cmd_len;
1693 
1694 	return es58x_submit_urb(es58x_dev, urb, netdev);
1695 }
1696 
1697 /**
1698  * es58x_alloc_rx_urbs() - Allocate RX URBs.
1699  * @es58x_dev: ES58X device.
1700  *
1701  * Allocate URBs for reception and anchor them.
1702  *
1703  * Return: zero on success, errno when any error occurs.
1704  */
es58x_alloc_rx_urbs(struct es58x_device * es58x_dev)1705 static int es58x_alloc_rx_urbs(struct es58x_device *es58x_dev)
1706 {
1707 	const struct device *dev = es58x_dev->dev;
1708 	const struct es58x_parameters *param = es58x_dev->param;
1709 	u16 rx_buf_len = usb_maxpacket(es58x_dev->udev, es58x_dev->rx_pipe);
1710 	struct urb *urb;
1711 	u8 *buf;
1712 	int i;
1713 	int ret = -EINVAL;
1714 
1715 	for (i = 0; i < param->rx_urb_max; i++) {
1716 		ret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len,
1717 				      GFP_KERNEL);
1718 		if (ret)
1719 			break;
1720 
1721 		usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe,
1722 				  buf, rx_buf_len, es58x_read_bulk_callback,
1723 				  es58x_dev);
1724 		usb_anchor_urb(urb, &es58x_dev->rx_urbs);
1725 
1726 		ret = usb_submit_urb(urb, GFP_KERNEL);
1727 		if (ret) {
1728 			usb_unanchor_urb(urb);
1729 			usb_free_coherent(es58x_dev->udev, rx_buf_len,
1730 					  buf, urb->transfer_dma);
1731 			usb_free_urb(urb);
1732 			break;
1733 		}
1734 		usb_free_urb(urb);
1735 	}
1736 
1737 	if (i == 0) {
1738 		dev_err(dev, "%s: Could not setup any rx URBs\n", __func__);
1739 		return ret;
1740 	}
1741 	dev_dbg(dev, "%s: Allocated %d rx URBs each of size %u\n",
1742 		__func__, i, rx_buf_len);
1743 
1744 	return 0;
1745 }
1746 
1747 /**
1748  * es58x_free_urbs() - Free all the TX and RX URBs.
1749  * @es58x_dev: ES58X device.
1750  */
es58x_free_urbs(struct es58x_device * es58x_dev)1751 static void es58x_free_urbs(struct es58x_device *es58x_dev)
1752 {
1753 	struct urb *urb;
1754 
1755 	if (!usb_wait_anchor_empty_timeout(&es58x_dev->tx_urbs_busy, 1000)) {
1756 		dev_err(es58x_dev->dev, "%s: Timeout, some TX urbs still remain\n",
1757 			__func__);
1758 		usb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy);
1759 	}
1760 
1761 	while ((urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle)) != NULL) {
1762 		usb_free_coherent(urb->dev, es58x_dev->param->tx_urb_cmd_max_len,
1763 				  urb->transfer_buffer, urb->transfer_dma);
1764 		usb_free_urb(urb);
1765 		atomic_dec(&es58x_dev->tx_urbs_idle_cnt);
1766 	}
1767 	if (atomic_read(&es58x_dev->tx_urbs_idle_cnt))
1768 		dev_err(es58x_dev->dev,
1769 			"All idle urbs were freed but tx_urb_idle_cnt is %d\n",
1770 			atomic_read(&es58x_dev->tx_urbs_idle_cnt));
1771 
1772 	usb_kill_anchored_urbs(&es58x_dev->rx_urbs);
1773 }
1774 
1775 /**
1776  * es58x_open() - Enable the network device.
1777  * @netdev: CAN network device.
1778  *
1779  * Called when the network transitions to the up state. Allocate the
1780  * URB resources if needed and open the channel.
1781  *
1782  * Return: zero on success, errno when any error occurs.
1783  */
es58x_open(struct net_device * netdev)1784 static int es58x_open(struct net_device *netdev)
1785 {
1786 	struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
1787 	int ret;
1788 
1789 	if (!es58x_dev->opened_channel_cnt) {
1790 		ret = es58x_alloc_rx_urbs(es58x_dev);
1791 		if (ret)
1792 			return ret;
1793 
1794 		ret = es58x_set_realtime_diff_ns(es58x_dev);
1795 		if (ret)
1796 			goto free_urbs;
1797 	}
1798 
1799 	ret = open_candev(netdev);
1800 	if (ret)
1801 		goto free_urbs;
1802 
1803 	ret = es58x_dev->ops->enable_channel(es58x_priv(netdev));
1804 	if (ret)
1805 		goto free_urbs;
1806 
1807 	es58x_dev->opened_channel_cnt++;
1808 	netif_start_queue(netdev);
1809 
1810 	return ret;
1811 
1812  free_urbs:
1813 	if (!es58x_dev->opened_channel_cnt)
1814 		es58x_free_urbs(es58x_dev);
1815 	netdev_err(netdev, "%s: Could not open the network device: %pe\n",
1816 		   __func__, ERR_PTR(ret));
1817 
1818 	return ret;
1819 }
1820 
1821 /**
1822  * es58x_stop() - Disable the network device.
1823  * @netdev: CAN network device.
1824  *
1825  * Called when the network transitions to the down state. If all the
1826  * channels of the device are closed, free the URB resources which are
1827  * not needed anymore.
1828  *
1829  * Return: zero on success, errno when any error occurs.
1830  */
es58x_stop(struct net_device * netdev)1831 static int es58x_stop(struct net_device *netdev)
1832 {
1833 	struct es58x_priv *priv = es58x_priv(netdev);
1834 	struct es58x_device *es58x_dev = priv->es58x_dev;
1835 	int ret;
1836 
1837 	netif_stop_queue(netdev);
1838 	ret = es58x_dev->ops->disable_channel(priv);
1839 	if (ret)
1840 		return ret;
1841 
1842 	priv->can.state = CAN_STATE_STOPPED;
1843 	es58x_can_reset_echo_fifo(netdev);
1844 	close_candev(netdev);
1845 
1846 	es58x_flush_pending_tx_msg(netdev);
1847 
1848 	es58x_dev->opened_channel_cnt--;
1849 	if (!es58x_dev->opened_channel_cnt)
1850 		es58x_free_urbs(es58x_dev);
1851 
1852 	return 0;
1853 }
1854 
1855 /**
1856  * es58x_xmit_commit() - Send the bulk urb.
1857  * @netdev: CAN network device.
1858  *
1859  * Do the bulk send. This function should be called only once by bulk
1860  * transmission.
1861  *
1862  * Return: zero on success, errno when any error occurs.
1863  */
es58x_xmit_commit(struct net_device * netdev)1864 static int es58x_xmit_commit(struct net_device *netdev)
1865 {
1866 	struct es58x_priv *priv = es58x_priv(netdev);
1867 	int ret;
1868 
1869 	if (!es58x_is_can_state_active(netdev))
1870 		return -ENETDOWN;
1871 
1872 	if (es58x_is_echo_skb_threshold_reached(priv))
1873 		netif_stop_queue(netdev);
1874 
1875 	ret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev);
1876 	if (ret == 0)
1877 		priv->tx_urb = NULL;
1878 
1879 	return ret;
1880 }
1881 
1882 /**
1883  * es58x_xmit_more() - Can we put more packets?
1884  * @priv: ES58X private parameters related to the network device.
1885  *
1886  * Return: true if we can put more, false if it is time to send.
1887  */
es58x_xmit_more(struct es58x_priv * priv)1888 static bool es58x_xmit_more(struct es58x_priv *priv)
1889 {
1890 	unsigned int free_slots =
1891 	    priv->can.echo_skb_max - (priv->tx_head - priv->tx_tail);
1892 
1893 	return netdev_xmit_more() && free_slots > 0 &&
1894 		priv->tx_can_msg_cnt < priv->es58x_dev->param->tx_bulk_max;
1895 }
1896 
1897 /**
1898  * es58x_start_xmit() - Transmit an skb.
1899  * @skb: socket buffer of a CAN message.
1900  * @netdev: CAN network device.
1901  *
1902  * Called when a packet needs to be transmitted.
1903  *
1904  * This function relies on Byte Queue Limits (BQL). The main benefit
1905  * is to increase the throughput by allowing bulk transfers
1906  * (c.f. xmit_more flag).
1907  *
1908  * Queues up to tx_bulk_max messages in &tx_urb buffer and does
1909  * a bulk send of all messages in one single URB.
1910  *
1911  * Return: NETDEV_TX_OK regardless of if we could transmit the @skb or
1912  *	had to drop it.
1913  */
es58x_start_xmit(struct sk_buff * skb,struct net_device * netdev)1914 static netdev_tx_t es58x_start_xmit(struct sk_buff *skb,
1915 				    struct net_device *netdev)
1916 {
1917 	struct es58x_priv *priv = es58x_priv(netdev);
1918 	struct es58x_device *es58x_dev = priv->es58x_dev;
1919 	unsigned int frame_len;
1920 	int ret;
1921 
1922 	if (can_dev_dropped_skb(netdev, skb)) {
1923 		if (priv->tx_urb)
1924 			goto xmit_commit;
1925 		return NETDEV_TX_OK;
1926 	}
1927 
1928 	if (priv->tx_urb && priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) {
1929 		/* Can not do bulk send with mixed CAN and CAN FD frames. */
1930 		ret = es58x_xmit_commit(netdev);
1931 		if (ret)
1932 			goto drop_skb;
1933 	}
1934 
1935 	if (!priv->tx_urb) {
1936 		priv->tx_urb = es58x_get_tx_urb(es58x_dev);
1937 		if (!priv->tx_urb) {
1938 			ret = -ENOMEM;
1939 			goto drop_skb;
1940 		}
1941 		priv->tx_can_msg_cnt = 0;
1942 		priv->tx_can_msg_is_fd = can_is_canfd_skb(skb);
1943 	}
1944 
1945 	ret = es58x_dev->ops->tx_can_msg(priv, skb);
1946 	if (ret)
1947 		goto drop_skb;
1948 
1949 	frame_len = can_skb_get_frame_len(skb);
1950 	ret = can_put_echo_skb(skb, netdev,
1951 			       priv->tx_head & es58x_dev->param->fifo_mask,
1952 			       frame_len);
1953 	if (ret)
1954 		goto xmit_failure;
1955 	netdev_sent_queue(netdev, frame_len);
1956 
1957 	priv->tx_head++;
1958 	priv->tx_can_msg_cnt++;
1959 
1960  xmit_commit:
1961 	if (!es58x_xmit_more(priv)) {
1962 		ret = es58x_xmit_commit(netdev);
1963 		if (ret)
1964 			goto xmit_failure;
1965 	}
1966 
1967 	return NETDEV_TX_OK;
1968 
1969  drop_skb:
1970 	dev_kfree_skb(skb);
1971 	netdev->stats.tx_dropped++;
1972  xmit_failure:
1973 	netdev_warn(netdev, "%s: send message failure: %pe\n",
1974 		    __func__, ERR_PTR(ret));
1975 	netdev->stats.tx_errors++;
1976 	es58x_flush_pending_tx_msg(netdev);
1977 	return NETDEV_TX_OK;
1978 }
1979 
1980 static const struct net_device_ops es58x_netdev_ops = {
1981 	.ndo_open = es58x_open,
1982 	.ndo_stop = es58x_stop,
1983 	.ndo_start_xmit = es58x_start_xmit,
1984 	.ndo_hwtstamp_get = can_hwtstamp_get,
1985 	.ndo_hwtstamp_set = can_hwtstamp_set,
1986 };
1987 
1988 static const struct ethtool_ops es58x_ethtool_ops = {
1989 	.get_ts_info = can_ethtool_op_get_ts_info_hwts,
1990 };
1991 
1992 /**
1993  * es58x_set_mode() - Change network device mode.
1994  * @netdev: CAN network device.
1995  * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP
1996  *
1997  * Currently, this function is only used to stop and restart the
1998  * channel during a bus off event (c.f. es58x_rx_err_msg() and
1999  * drivers/net/can/dev.c:can_restart() which are the two only
2000  * callers).
2001  *
2002  * Return: zero on success, errno when any error occurs.
2003  */
es58x_set_mode(struct net_device * netdev,enum can_mode mode)2004 static int es58x_set_mode(struct net_device *netdev, enum can_mode mode)
2005 {
2006 	struct es58x_priv *priv = es58x_priv(netdev);
2007 
2008 	switch (mode) {
2009 	case CAN_MODE_START:
2010 		switch (priv->can.state) {
2011 		case CAN_STATE_BUS_OFF:
2012 			return priv->es58x_dev->ops->enable_channel(priv);
2013 
2014 		case CAN_STATE_STOPPED:
2015 			return es58x_open(netdev);
2016 
2017 		case CAN_STATE_ERROR_ACTIVE:
2018 		case CAN_STATE_ERROR_WARNING:
2019 		case CAN_STATE_ERROR_PASSIVE:
2020 		default:
2021 			return 0;
2022 		}
2023 
2024 	case CAN_MODE_STOP:
2025 		switch (priv->can.state) {
2026 		case CAN_STATE_STOPPED:
2027 			return 0;
2028 
2029 		case CAN_STATE_ERROR_ACTIVE:
2030 		case CAN_STATE_ERROR_WARNING:
2031 		case CAN_STATE_ERROR_PASSIVE:
2032 		case CAN_STATE_BUS_OFF:
2033 		default:
2034 			return priv->es58x_dev->ops->disable_channel(priv);
2035 		}
2036 
2037 	case CAN_MODE_SLEEP:
2038 	default:
2039 		return -EOPNOTSUPP;
2040 	}
2041 }
2042 
2043 /**
2044  * es58x_init_priv() - Initialize private parameters.
2045  * @es58x_dev: ES58X device.
2046  * @priv: ES58X private parameters related to the network device.
2047  * @channel_idx: Index of the network device.
2048  *
2049  * Return: zero on success, errno if devlink port could not be
2050  *	properly registered.
2051  */
es58x_init_priv(struct es58x_device * es58x_dev,struct es58x_priv * priv,int channel_idx)2052 static int es58x_init_priv(struct es58x_device *es58x_dev,
2053 			   struct es58x_priv *priv, int channel_idx)
2054 {
2055 	struct devlink_port_attrs attrs = {
2056 		.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL,
2057 	};
2058 	const struct es58x_parameters *param = es58x_dev->param;
2059 	struct can_priv *can = &priv->can;
2060 
2061 	priv->es58x_dev = es58x_dev;
2062 	priv->channel_idx = channel_idx;
2063 	priv->tx_urb = NULL;
2064 	priv->tx_can_msg_cnt = 0;
2065 
2066 	can->bittiming_const = param->bittiming_const;
2067 	if (param->ctrlmode_supported & CAN_CTRLMODE_FD) {
2068 		can->fd.data_bittiming_const = param->data_bittiming_const;
2069 		can->fd.tdc_const = param->tdc_const;
2070 	}
2071 	can->bitrate_max = param->bitrate_max;
2072 	can->clock = param->clock;
2073 	can->state = CAN_STATE_STOPPED;
2074 	can->ctrlmode_supported = param->ctrlmode_supported;
2075 	can->do_set_mode = es58x_set_mode;
2076 
2077 	devlink_port_attrs_set(&priv->devlink_port, &attrs);
2078 	return devlink_port_register(priv_to_devlink(es58x_dev),
2079 				     &priv->devlink_port, channel_idx);
2080 }
2081 
2082 /**
2083  * es58x_init_netdev() - Initialize the network device.
2084  * @es58x_dev: ES58X device.
2085  * @channel_idx: Index of the network device.
2086  *
2087  * Return: zero on success, errno when any error occurs.
2088  */
es58x_init_netdev(struct es58x_device * es58x_dev,int channel_idx)2089 static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx)
2090 {
2091 	struct net_device *netdev;
2092 	struct device *dev = es58x_dev->dev;
2093 	int ret;
2094 
2095 	netdev = alloc_candev(sizeof(struct es58x_priv),
2096 			      es58x_dev->param->fifo_mask + 1);
2097 	if (!netdev) {
2098 		dev_err(dev, "Could not allocate candev\n");
2099 		return -ENOMEM;
2100 	}
2101 	SET_NETDEV_DEV(netdev, dev);
2102 	es58x_dev->netdev[channel_idx] = netdev;
2103 	ret = es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx);
2104 	if (ret)
2105 		goto free_candev;
2106 	SET_NETDEV_DEVLINK_PORT(netdev, &es58x_priv(netdev)->devlink_port);
2107 
2108 	netdev->netdev_ops = &es58x_netdev_ops;
2109 	netdev->ethtool_ops = &es58x_ethtool_ops;
2110 	netdev->flags |= IFF_ECHO;	/* We support local echo */
2111 	netdev->dev_port = channel_idx;
2112 
2113 	ret = register_candev(netdev);
2114 	if (ret)
2115 		goto devlink_port_unregister;
2116 
2117 	netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0),
2118 				       es58x_dev->param->dql_min_limit);
2119 
2120 	return ret;
2121 
2122  devlink_port_unregister:
2123 	devlink_port_unregister(&es58x_priv(netdev)->devlink_port);
2124  free_candev:
2125 	es58x_dev->netdev[channel_idx] = NULL;
2126 	free_candev(netdev);
2127 	return ret;
2128 }
2129 
2130 /**
2131  * es58x_free_netdevs() - Release all network resources of the device.
2132  * @es58x_dev: ES58X device.
2133  */
es58x_free_netdevs(struct es58x_device * es58x_dev)2134 static void es58x_free_netdevs(struct es58x_device *es58x_dev)
2135 {
2136 	int i;
2137 
2138 	for (i = 0; i < es58x_dev->num_can_ch; i++) {
2139 		struct net_device *netdev = es58x_dev->netdev[i];
2140 
2141 		if (!netdev)
2142 			continue;
2143 		unregister_candev(netdev);
2144 		devlink_port_unregister(&es58x_priv(netdev)->devlink_port);
2145 		es58x_dev->netdev[i] = NULL;
2146 		free_candev(netdev);
2147 	}
2148 }
2149 
2150 /**
2151  * es58x_init_es58x_dev() - Initialize the ES58X device.
2152  * @intf: USB interface.
2153  * @driver_info: Quirks of the device.
2154  *
2155  * Return: pointer to an ES58X device on success, error pointer when
2156  *	any error occurs.
2157  */
es58x_init_es58x_dev(struct usb_interface * intf,kernel_ulong_t driver_info)2158 static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf,
2159 						 kernel_ulong_t driver_info)
2160 {
2161 	struct device *dev = &intf->dev;
2162 	struct es58x_device *es58x_dev;
2163 	struct devlink *devlink;
2164 	const struct es58x_parameters *param;
2165 	const struct es58x_operators *ops;
2166 	struct usb_device *udev = interface_to_usbdev(intf);
2167 	struct usb_endpoint_descriptor *ep_in, *ep_out;
2168 	int ret;
2169 
2170 	dev_info(dev, "Starting %s %s (Serial Number %s)\n",
2171 		 udev->manufacturer, udev->product, udev->serial);
2172 
2173 	ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out,
2174 					NULL, NULL);
2175 	if (ret)
2176 		return ERR_PTR(ret);
2177 
2178 	if (driver_info & ES58X_FD_FAMILY) {
2179 		param = &es58x_fd_param;
2180 		ops = &es58x_fd_ops;
2181 	} else {
2182 		param = &es581_4_param;
2183 		ops = &es581_4_ops;
2184 	}
2185 
2186 	devlink = devlink_alloc(&es58x_dl_ops, es58x_sizeof_es58x_device(param),
2187 				dev);
2188 	if (!devlink)
2189 		return ERR_PTR(-ENOMEM);
2190 
2191 	es58x_dev = devlink_priv(devlink);
2192 	es58x_dev->param = param;
2193 	es58x_dev->ops = ops;
2194 	es58x_dev->dev = dev;
2195 	es58x_dev->udev = udev;
2196 
2197 	if (driver_info & ES58X_DUAL_CHANNEL)
2198 		es58x_dev->num_can_ch = 2;
2199 	else
2200 		es58x_dev->num_can_ch = 1;
2201 
2202 	init_usb_anchor(&es58x_dev->rx_urbs);
2203 	init_usb_anchor(&es58x_dev->tx_urbs_idle);
2204 	init_usb_anchor(&es58x_dev->tx_urbs_busy);
2205 	atomic_set(&es58x_dev->tx_urbs_idle_cnt, 0);
2206 	usb_set_intfdata(intf, es58x_dev);
2207 
2208 	es58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev,
2209 					     ep_in->bEndpointAddress);
2210 	es58x_dev->tx_pipe = usb_sndbulkpipe(es58x_dev->udev,
2211 					     ep_out->bEndpointAddress);
2212 
2213 	return es58x_dev;
2214 }
2215 
2216 /**
2217  * es58x_probe() - Initialize the USB device.
2218  * @intf: USB interface.
2219  * @id: USB device ID.
2220  *
2221  * Return: zero on success, -ENODEV if the interface is not supported
2222  * or errno when any other error occurs.
2223  */
es58x_probe(struct usb_interface * intf,const struct usb_device_id * id)2224 static int es58x_probe(struct usb_interface *intf,
2225 		       const struct usb_device_id *id)
2226 {
2227 	struct es58x_device *es58x_dev;
2228 	int ch_idx;
2229 
2230 	es58x_dev = es58x_init_es58x_dev(intf, id->driver_info);
2231 	if (IS_ERR(es58x_dev))
2232 		return PTR_ERR(es58x_dev);
2233 
2234 	es58x_parse_product_info(es58x_dev);
2235 	devlink_register(priv_to_devlink(es58x_dev));
2236 
2237 	for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) {
2238 		int ret = es58x_init_netdev(es58x_dev, ch_idx);
2239 
2240 		if (ret) {
2241 			es58x_free_netdevs(es58x_dev);
2242 			return ret;
2243 		}
2244 	}
2245 
2246 	return 0;
2247 }
2248 
2249 /**
2250  * es58x_disconnect() - Disconnect the USB device.
2251  * @intf: USB interface
2252  *
2253  * Called by the usb core when driver is unloaded or device is
2254  * removed.
2255  */
es58x_disconnect(struct usb_interface * intf)2256 static void es58x_disconnect(struct usb_interface *intf)
2257 {
2258 	struct es58x_device *es58x_dev = usb_get_intfdata(intf);
2259 
2260 	dev_info(&intf->dev, "Disconnecting %s %s\n",
2261 		 es58x_dev->udev->manufacturer, es58x_dev->udev->product);
2262 
2263 	devlink_unregister(priv_to_devlink(es58x_dev));
2264 	es58x_free_netdevs(es58x_dev);
2265 	es58x_free_urbs(es58x_dev);
2266 	devlink_free(priv_to_devlink(es58x_dev));
2267 	usb_set_intfdata(intf, NULL);
2268 }
2269 
2270 static struct usb_driver es58x_driver = {
2271 	.name = KBUILD_MODNAME,
2272 	.probe = es58x_probe,
2273 	.disconnect = es58x_disconnect,
2274 	.id_table = es58x_id_table
2275 };
2276 
2277 module_usb_driver(es58x_driver);
2278