1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
4  *
5  * File es581_4.c: Adds support to ETAS ES581.4.
6  *
7  * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
8  * Copyright (c) 2020 ETAS K.K.. All rights reserved.
9  * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
10  */
11 
12 #include <linux/kernel.h>
13 #include <asm/unaligned.h>
14 
15 #include "es58x_core.h"
16 #include "es581_4.h"
17 
18 /**
19  * es581_4_sizeof_rx_tx_msg() - Calculate the actual length of the
20  *	structure of a rx or tx message.
21  * @msg: message of variable length, must have a dlc field.
22  *
23  * Even if RTR frames have actually no payload, the ES58X devices
24  * still expect it. Must be a macro in order to accept several types
25  * (struct es581_4_tx_can_msg and struct es581_4_rx_can_msg) as an
26  * input.
27  *
28  * Return: length of the message.
29  */
30 #define es581_4_sizeof_rx_tx_msg(msg)				\
31 	offsetof(typeof(msg), data[can_cc_dlc2len((msg).dlc)])
32 
33 static u16 es581_4_get_msg_len(const union es58x_urb_cmd *urb_cmd)
34 {
35 	return get_unaligned_le16(&urb_cmd->es581_4_urb_cmd.msg_len);
36 }
37 
38 static int es581_4_echo_msg(struct es58x_device *es58x_dev,
39 			    const struct es581_4_urb_cmd *es581_4_urb_cmd)
40 {
41 	struct net_device *netdev;
42 	const struct es581_4_bulk_echo_msg *bulk_echo_msg;
43 	const struct es581_4_echo_msg *echo_msg;
44 	u64 *tstamps = es58x_dev->timestamps;
45 	u16 msg_len;
46 	u32 first_packet_idx, packet_idx;
47 	unsigned int dropped = 0;
48 	int i, num_element, ret;
49 
50 	bulk_echo_msg = &es581_4_urb_cmd->bulk_echo_msg;
51 	msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len) -
52 	    sizeof(bulk_echo_msg->channel_no);
53 	num_element = es58x_msg_num_element(es58x_dev->dev,
54 					    bulk_echo_msg->echo_msg, msg_len);
55 	if (num_element <= 0)
56 		return num_element;
57 
58 	ret = es58x_get_netdev(es58x_dev, bulk_echo_msg->channel_no,
59 			       ES581_4_CHANNEL_IDX_OFFSET, &netdev);
60 	if (ret)
61 		return ret;
62 
63 	echo_msg = &bulk_echo_msg->echo_msg[0];
64 	first_packet_idx = get_unaligned_le32(&echo_msg->packet_idx);
65 	packet_idx = first_packet_idx;
66 	for (i = 0; i < num_element; i++) {
67 		u32 tmp_idx;
68 
69 		echo_msg = &bulk_echo_msg->echo_msg[i];
70 		tmp_idx = get_unaligned_le32(&echo_msg->packet_idx);
71 		if (tmp_idx == packet_idx - 1) {
72 			if (net_ratelimit())
73 				netdev_warn(netdev,
74 					    "Received echo packet idx %u twice\n",
75 					    packet_idx - 1);
76 			dropped++;
77 			continue;
78 		}
79 		if (tmp_idx != packet_idx) {
80 			netdev_err(netdev, "Echo packet idx jumped from %u to %u\n",
81 				   packet_idx - 1, echo_msg->packet_idx);
82 			return -EBADMSG;
83 		}
84 
85 		tstamps[i] = get_unaligned_le64(&echo_msg->timestamp);
86 		packet_idx++;
87 	}
88 
89 	netdev->stats.tx_dropped += dropped;
90 	return es58x_can_get_echo_skb(netdev, first_packet_idx,
91 				      tstamps, num_element - dropped);
92 }
93 
94 static int es581_4_rx_can_msg(struct es58x_device *es58x_dev,
95 			      const struct es581_4_urb_cmd *es581_4_urb_cmd,
96 			      u16 msg_len)
97 {
98 	const struct device *dev = es58x_dev->dev;
99 	struct net_device *netdev;
100 	int pkts, num_element, channel_no, ret;
101 
102 	num_element = es58x_msg_num_element(dev, es581_4_urb_cmd->rx_can_msg,
103 					    msg_len);
104 	if (num_element <= 0)
105 		return num_element;
106 
107 	channel_no = es581_4_urb_cmd->rx_can_msg[0].channel_no;
108 	ret = es58x_get_netdev(es58x_dev, channel_no,
109 			       ES581_4_CHANNEL_IDX_OFFSET, &netdev);
110 	if (ret)
111 		return ret;
112 
113 	if (!netif_running(netdev)) {
114 		if (net_ratelimit())
115 			netdev_info(netdev,
116 				    "%s: %s is down, dropping %d rx packets\n",
117 				    __func__, netdev->name, num_element);
118 		netdev->stats.rx_dropped += num_element;
119 		return 0;
120 	}
121 
122 	for (pkts = 0; pkts < num_element; pkts++) {
123 		const struct es581_4_rx_can_msg *rx_can_msg =
124 		    &es581_4_urb_cmd->rx_can_msg[pkts];
125 		u64 tstamp = get_unaligned_le64(&rx_can_msg->timestamp);
126 		canid_t can_id = get_unaligned_le32(&rx_can_msg->can_id);
127 
128 		if (channel_no != rx_can_msg->channel_no)
129 			return -EBADMSG;
130 
131 		ret = es58x_rx_can_msg(netdev, tstamp, rx_can_msg->data,
132 				       can_id, rx_can_msg->flags,
133 				       rx_can_msg->dlc);
134 		if (ret)
135 			break;
136 	}
137 
138 	return ret;
139 }
140 
141 static int es581_4_rx_err_msg(struct es58x_device *es58x_dev,
142 			      const struct es581_4_rx_err_msg *rx_err_msg)
143 {
144 	struct net_device *netdev;
145 	enum es58x_err error = get_unaligned_le32(&rx_err_msg->error);
146 	int ret;
147 
148 	ret = es58x_get_netdev(es58x_dev, rx_err_msg->channel_no,
149 			       ES581_4_CHANNEL_IDX_OFFSET, &netdev);
150 	if (ret)
151 		return ret;
152 
153 	return es58x_rx_err_msg(netdev, error, 0,
154 				get_unaligned_le64(&rx_err_msg->timestamp));
155 }
156 
157 static int es581_4_rx_event_msg(struct es58x_device *es58x_dev,
158 				const struct es581_4_rx_event_msg *rx_event_msg)
159 {
160 	struct net_device *netdev;
161 	enum es58x_event event = get_unaligned_le32(&rx_event_msg->event);
162 	int ret;
163 
164 	ret = es58x_get_netdev(es58x_dev, rx_event_msg->channel_no,
165 			       ES581_4_CHANNEL_IDX_OFFSET, &netdev);
166 	if (ret)
167 		return ret;
168 
169 	return es58x_rx_err_msg(netdev, 0, event,
170 				get_unaligned_le64(&rx_event_msg->timestamp));
171 }
172 
173 static int es581_4_rx_cmd_ret_u32(struct es58x_device *es58x_dev,
174 				  const struct es581_4_urb_cmd *es581_4_urb_cmd,
175 				  enum es58x_ret_type ret_type)
176 {
177 	struct net_device *netdev;
178 	const struct es581_4_rx_cmd_ret *rx_cmd_ret;
179 	u16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);
180 	int ret;
181 
182 	ret = es58x_check_msg_len(es58x_dev->dev,
183 				  es581_4_urb_cmd->rx_cmd_ret, msg_len);
184 	if (ret)
185 		return ret;
186 
187 	rx_cmd_ret = &es581_4_urb_cmd->rx_cmd_ret;
188 
189 	ret = es58x_get_netdev(es58x_dev, rx_cmd_ret->channel_no,
190 			       ES581_4_CHANNEL_IDX_OFFSET, &netdev);
191 	if (ret)
192 		return ret;
193 
194 	return es58x_rx_cmd_ret_u32(netdev, ret_type,
195 				    get_unaligned_le32(&rx_cmd_ret->rx_cmd_ret_le32));
196 }
197 
198 static int es581_4_tx_ack_msg(struct es58x_device *es58x_dev,
199 			      const struct es581_4_urb_cmd *es581_4_urb_cmd)
200 {
201 	struct net_device *netdev;
202 	const struct es581_4_tx_ack_msg *tx_ack_msg;
203 	u16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);
204 	int ret;
205 
206 	tx_ack_msg = &es581_4_urb_cmd->tx_ack_msg;
207 	ret = es58x_check_msg_len(es58x_dev->dev, *tx_ack_msg, msg_len);
208 	if (ret)
209 		return ret;
210 
211 	if (tx_ack_msg->rx_cmd_ret_u8 != ES58X_RET_U8_OK)
212 		return es58x_rx_cmd_ret_u8(es58x_dev->dev,
213 					   ES58X_RET_TYPE_TX_MSG,
214 					   tx_ack_msg->rx_cmd_ret_u8);
215 
216 	ret = es58x_get_netdev(es58x_dev, tx_ack_msg->channel_no,
217 			       ES581_4_CHANNEL_IDX_OFFSET, &netdev);
218 	if (ret)
219 		return ret;
220 
221 	return es58x_tx_ack_msg(netdev,
222 				get_unaligned_le16(&tx_ack_msg->tx_free_entries),
223 				ES58X_RET_U32_OK);
224 }
225 
226 static int es581_4_dispatch_rx_cmd(struct es58x_device *es58x_dev,
227 				   const struct es581_4_urb_cmd *es581_4_urb_cmd)
228 {
229 	const struct device *dev = es58x_dev->dev;
230 	u16 msg_len = get_unaligned_le16(&es581_4_urb_cmd->msg_len);
231 	enum es581_4_rx_type rx_type = es581_4_urb_cmd->rx_can_msg[0].rx_type;
232 	int ret = 0;
233 
234 	switch (rx_type) {
235 	case ES581_4_RX_TYPE_MESSAGE:
236 		return es581_4_rx_can_msg(es58x_dev, es581_4_urb_cmd, msg_len);
237 
238 	case ES581_4_RX_TYPE_ERROR:
239 		ret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_err_msg,
240 					  msg_len);
241 		if (ret < 0)
242 			return ret;
243 		return es581_4_rx_err_msg(es58x_dev,
244 					  &es581_4_urb_cmd->rx_err_msg);
245 
246 	case ES581_4_RX_TYPE_EVENT:
247 		ret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_event_msg,
248 					  msg_len);
249 		if (ret < 0)
250 			return ret;
251 		return es581_4_rx_event_msg(es58x_dev,
252 					    &es581_4_urb_cmd->rx_event_msg);
253 
254 	default:
255 		dev_err(dev, "%s: Unknown rx_type 0x%02X\n", __func__, rx_type);
256 		return -EBADRQC;
257 	}
258 }
259 
260 static int es581_4_handle_urb_cmd(struct es58x_device *es58x_dev,
261 				  const union es58x_urb_cmd *urb_cmd)
262 {
263 	const struct es581_4_urb_cmd *es581_4_urb_cmd;
264 	struct device *dev = es58x_dev->dev;
265 	u16 msg_len = es581_4_get_msg_len(urb_cmd);
266 	int ret;
267 
268 	es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;
269 
270 	if (es581_4_urb_cmd->cmd_type != ES581_4_CAN_COMMAND_TYPE) {
271 		dev_err(dev, "%s: Unknown command type (0x%02X)\n",
272 			__func__, es581_4_urb_cmd->cmd_type);
273 		return -EBADRQC;
274 	}
275 
276 	switch ((enum es581_4_cmd_id)es581_4_urb_cmd->cmd_id) {
277 	case ES581_4_CMD_ID_SET_BITTIMING:
278 		return es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
279 					      ES58X_RET_TYPE_SET_BITTIMING);
280 
281 	case ES581_4_CMD_ID_ENABLE_CHANNEL:
282 		return es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
283 					      ES58X_RET_TYPE_ENABLE_CHANNEL);
284 
285 	case ES581_4_CMD_ID_TX_MSG:
286 		return es581_4_tx_ack_msg(es58x_dev, es581_4_urb_cmd);
287 
288 	case ES581_4_CMD_ID_RX_MSG:
289 		return es581_4_dispatch_rx_cmd(es58x_dev, es581_4_urb_cmd);
290 
291 	case ES581_4_CMD_ID_RESET_RX:
292 		ret = es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
293 					     ES58X_RET_TYPE_RESET_RX);
294 		return ret;
295 
296 	case ES581_4_CMD_ID_RESET_TX:
297 		ret = es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
298 					     ES58X_RET_TYPE_RESET_TX);
299 		return ret;
300 
301 	case ES581_4_CMD_ID_DISABLE_CHANNEL:
302 		return es581_4_rx_cmd_ret_u32(es58x_dev, es581_4_urb_cmd,
303 					      ES58X_RET_TYPE_DISABLE_CHANNEL);
304 
305 	case ES581_4_CMD_ID_TIMESTAMP:
306 		ret = es58x_check_msg_len(dev, es581_4_urb_cmd->timestamp,
307 					  msg_len);
308 		if (ret < 0)
309 			return ret;
310 		es58x_rx_timestamp(es58x_dev,
311 				   get_unaligned_le64(&es581_4_urb_cmd->timestamp));
312 		return 0;
313 
314 	case ES581_4_CMD_ID_ECHO:
315 		return es581_4_echo_msg(es58x_dev, es581_4_urb_cmd);
316 
317 	case ES581_4_CMD_ID_DEVICE_ERR:
318 		ret = es58x_check_msg_len(dev, es581_4_urb_cmd->rx_cmd_ret_u8,
319 					  msg_len);
320 		if (ret)
321 			return ret;
322 		return es58x_rx_cmd_ret_u8(dev, ES58X_RET_TYPE_DEVICE_ERR,
323 					   es581_4_urb_cmd->rx_cmd_ret_u8);
324 
325 	default:
326 		dev_warn(dev, "%s: Unexpected command ID: 0x%02X\n",
327 			 __func__, es581_4_urb_cmd->cmd_id);
328 		return -EBADRQC;
329 	}
330 }
331 
332 static void es581_4_fill_urb_header(union es58x_urb_cmd *urb_cmd, u8 cmd_type,
333 				    u8 cmd_id, u8 channel_idx, u16 msg_len)
334 {
335 	struct es581_4_urb_cmd *es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;
336 
337 	es581_4_urb_cmd->SOF = cpu_to_le16(es581_4_param.tx_start_of_frame);
338 	es581_4_urb_cmd->cmd_type = cmd_type;
339 	es581_4_urb_cmd->cmd_id = cmd_id;
340 	es581_4_urb_cmd->msg_len = cpu_to_le16(msg_len);
341 }
342 
343 static int es581_4_tx_can_msg(struct es58x_priv *priv,
344 			      const struct sk_buff *skb)
345 {
346 	struct es58x_device *es58x_dev = priv->es58x_dev;
347 	union es58x_urb_cmd *urb_cmd = priv->tx_urb->transfer_buffer;
348 	struct es581_4_urb_cmd *es581_4_urb_cmd = &urb_cmd->es581_4_urb_cmd;
349 	struct can_frame *cf = (struct can_frame *)skb->data;
350 	struct es581_4_tx_can_msg *tx_can_msg;
351 	u16 msg_len;
352 	int ret;
353 
354 	if (can_is_canfd_skb(skb))
355 		return -EMSGSIZE;
356 
357 	if (priv->tx_can_msg_cnt == 0) {
358 		msg_len = sizeof(es581_4_urb_cmd->bulk_tx_can_msg.num_can_msg);
359 		es581_4_fill_urb_header(urb_cmd, ES581_4_CAN_COMMAND_TYPE,
360 					ES581_4_CMD_ID_TX_MSG,
361 					priv->channel_idx, msg_len);
362 		es581_4_urb_cmd->bulk_tx_can_msg.num_can_msg = 0;
363 	} else {
364 		msg_len = es581_4_get_msg_len(urb_cmd);
365 	}
366 
367 	ret = es58x_check_msg_max_len(es58x_dev->dev,
368 				      es581_4_urb_cmd->bulk_tx_can_msg,
369 				      msg_len + sizeof(*tx_can_msg));
370 	if (ret)
371 		return ret;
372 
373 	/* Fill message contents. */
374 	tx_can_msg = (typeof(tx_can_msg))&es581_4_urb_cmd->raw_msg[msg_len];
375 	put_unaligned_le32(es58x_get_raw_can_id(cf), &tx_can_msg->can_id);
376 	put_unaligned_le32(priv->tx_head, &tx_can_msg->packet_idx);
377 	put_unaligned_le16((u16)es58x_get_flags(skb), &tx_can_msg->flags);
378 	tx_can_msg->channel_no = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
379 	tx_can_msg->dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
380 
381 	memcpy(tx_can_msg->data, cf->data, cf->len);
382 
383 	/* Calculate new sizes. */
384 	es581_4_urb_cmd->bulk_tx_can_msg.num_can_msg++;
385 	msg_len += es581_4_sizeof_rx_tx_msg(*tx_can_msg);
386 	priv->tx_urb->transfer_buffer_length = es58x_get_urb_cmd_len(es58x_dev,
387 								     msg_len);
388 	es581_4_urb_cmd->msg_len = cpu_to_le16(msg_len);
389 
390 	return 0;
391 }
392 
393 static int es581_4_set_bittiming(struct es58x_priv *priv)
394 {
395 	struct es581_4_tx_conf_msg tx_conf_msg = { 0 };
396 	struct can_bittiming *bt = &priv->can.bittiming;
397 
398 	tx_conf_msg.bitrate = cpu_to_le32(bt->bitrate);
399 	/* bt->sample_point is in tenth of percent. Convert it to percent. */
400 	tx_conf_msg.sample_point = cpu_to_le32(bt->sample_point / 10U);
401 	tx_conf_msg.samples_per_bit = cpu_to_le32(ES58X_SAMPLES_PER_BIT_ONE);
402 	tx_conf_msg.bit_time = cpu_to_le32(can_bit_time(bt));
403 	tx_conf_msg.sjw = cpu_to_le32(bt->sjw);
404 	tx_conf_msg.sync_edge = cpu_to_le32(ES58X_SYNC_EDGE_SINGLE);
405 	tx_conf_msg.physical_layer =
406 	    cpu_to_le32(ES58X_PHYSICAL_LAYER_HIGH_SPEED);
407 	tx_conf_msg.echo_mode = cpu_to_le32(ES58X_ECHO_ON);
408 	tx_conf_msg.channel_no = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
409 
410 	return es58x_send_msg(priv->es58x_dev, ES581_4_CAN_COMMAND_TYPE,
411 			      ES581_4_CMD_ID_SET_BITTIMING, &tx_conf_msg,
412 			      sizeof(tx_conf_msg), priv->channel_idx);
413 }
414 
415 static int es581_4_enable_channel(struct es58x_priv *priv)
416 {
417 	int ret;
418 	u8 msg = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
419 
420 	ret = es581_4_set_bittiming(priv);
421 	if (ret)
422 		return ret;
423 
424 	return es58x_send_msg(priv->es58x_dev, ES581_4_CAN_COMMAND_TYPE,
425 			      ES581_4_CMD_ID_ENABLE_CHANNEL, &msg, sizeof(msg),
426 			      priv->channel_idx);
427 }
428 
429 static int es581_4_disable_channel(struct es58x_priv *priv)
430 {
431 	u8 msg = priv->channel_idx + ES581_4_CHANNEL_IDX_OFFSET;
432 
433 	return es58x_send_msg(priv->es58x_dev, ES581_4_CAN_COMMAND_TYPE,
434 			      ES581_4_CMD_ID_DISABLE_CHANNEL, &msg, sizeof(msg),
435 			      priv->channel_idx);
436 }
437 
438 static int es581_4_reset_device(struct es58x_device *es58x_dev)
439 {
440 	return es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,
441 			      ES581_4_CMD_ID_RESET_DEVICE,
442 			      ES58X_EMPTY_MSG, 0, ES58X_CHANNEL_IDX_NA);
443 }
444 
445 static int es581_4_get_timestamp(struct es58x_device *es58x_dev)
446 {
447 	return es58x_send_msg(es58x_dev, ES581_4_CAN_COMMAND_TYPE,
448 			      ES581_4_CMD_ID_TIMESTAMP,
449 			      ES58X_EMPTY_MSG, 0, ES58X_CHANNEL_IDX_NA);
450 }
451 
452 /* Nominal bittiming constants for ES581.4 as specified in the
453  * microcontroller datasheet: "Stellaris(R) LM3S5B91 Microcontroller"
454  * table 17-4 "CAN Protocol Ranges" from Texas Instruments.
455  */
456 static const struct can_bittiming_const es581_4_bittiming_const = {
457 	.name = "ES581.4",
458 	.tseg1_min = 1,
459 	.tseg1_max = 8,
460 	.tseg2_min = 1,
461 	.tseg2_max = 8,
462 	.sjw_max = 4,
463 	.brp_min = 1,
464 	.brp_max = 128,
465 	.brp_inc = 1
466 };
467 
468 const struct es58x_parameters es581_4_param = {
469 	.bittiming_const = &es581_4_bittiming_const,
470 	.data_bittiming_const = NULL,
471 	.tdc_const = NULL,
472 	.bitrate_max = 1 * CAN_MBPS,
473 	.clock = {.freq = 50 * CAN_MHZ},
474 	.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC,
475 	.tx_start_of_frame = 0xAFAF,
476 	.rx_start_of_frame = 0xFAFA,
477 	.tx_urb_cmd_max_len = ES581_4_TX_URB_CMD_MAX_LEN,
478 	.rx_urb_cmd_max_len = ES581_4_RX_URB_CMD_MAX_LEN,
479 	/* Size of internal device TX queue is 330.
480 	 *
481 	 * However, we witnessed some ES58X_ERR_PROT_CRC errors from
482 	 * the device and thus, echo_skb_max was lowered to the
483 	 * empirical value of 75 which seems stable and then rounded
484 	 * down to become a power of two.
485 	 *
486 	 * Root cause of those ES58X_ERR_PROT_CRC errors is still
487 	 * unclear.
488 	 */
489 	.fifo_mask = 63, /* echo_skb_max = 64 */
490 	.dql_min_limit = CAN_FRAME_LEN_MAX * 50, /* Empirical value. */
491 	.tx_bulk_max = ES581_4_TX_BULK_MAX,
492 	.urb_cmd_header_len = ES581_4_URB_CMD_HEADER_LEN,
493 	.rx_urb_max = ES58X_RX_URBS_MAX,
494 	.tx_urb_max = ES58X_TX_URBS_MAX
495 };
496 
497 const struct es58x_operators es581_4_ops = {
498 	.get_msg_len = es581_4_get_msg_len,
499 	.handle_urb_cmd = es581_4_handle_urb_cmd,
500 	.fill_urb_header = es581_4_fill_urb_header,
501 	.tx_can_msg = es581_4_tx_can_msg,
502 	.enable_channel = es581_4_enable_channel,
503 	.disable_channel = es581_4_disable_channel,
504 	.reset_device = es581_4_reset_device,
505 	.get_timestamp = es581_4_get_timestamp
506 };
507