FILE
es58x_core.c
File preview with syntax highlighting.
linux-6.19.14/drivers/net/can/usb/etas_es58x/es58x_core.c
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 23MODULE_AUTHOR("Vincent Mailhol <mailhol.vincent@wanadoo.fr>"); 24MODULE_AUTHOR("Arunachalam Santhanam <arunachalam.santhanam@in.bosch.com>"); 25MODULE_DESCRIPTION("Socket CAN driver for ETAS ES58X USB adapters"); 26MODULE_LICENSE("GPL v2"); 27 28#define ES58X_VENDOR_ID 0x108C 29#define ES581_4_PRODUCT_ID 0x0159 30#define ES582_1_PRODUCT_ID 0x0168 31#define ES584_1_PRODUCT_ID 0x0169 32 33/* ES58X FD has some interface protocols unsupported by this driver. */ 34#define ES58X_FD_INTERFACE_PROTOCOL 0 35 36/* Table of devices which work with this driver. */ 37static const struct usb_device_id es58x_id_table[] = { 38 { 39 /* ETAS GmbH ES581.4 USB dual-channel CAN Bus Interface module. */ 40 USB_DEVICE(ES58X_VENDOR_ID, ES581_4_PRODUCT_ID), 41 .driver_info = ES58X_DUAL_CHANNEL 42 }, { 43 /* ETAS GmbH ES582.1 USB dual-channel CAN FD Bus Interface module. */ 44 USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES582_1_PRODUCT_ID, 45 ES58X_FD_INTERFACE_PROTOCOL), 46 .driver_info = ES58X_DUAL_CHANNEL | ES58X_FD_FAMILY 47 }, { 48 /* ETAS GmbH ES584.1 USB single-channel CAN FD Bus Interface module. */ 49 USB_DEVICE_INTERFACE_PROTOCOL(ES58X_VENDOR_ID, ES584_1_PRODUCT_ID, 50 ES58X_FD_INTERFACE_PROTOCOL), 51 .driver_info = ES58X_FD_FAMILY 52 }, { 53 /* Terminating entry */ 54 } 55}; 56 57MODULE_DEVICE_TABLE(usb, es58x_id_table); 58 59#define es58x_print_hex_dump(buf, len) \ 60 print_hex_dump(KERN_DEBUG, \ 61 KBUILD_MODNAME " " __stringify(buf) ": ", \ 62 DUMP_PREFIX_NONE, 16, 1, buf, len, false) 63 64#define es58x_print_hex_dump_debug(buf, len) \ 65 print_hex_dump_debug(KBUILD_MODNAME " " __stringify(buf) ": ",\ 66 DUMP_PREFIX_NONE, 16, 1, buf, len, false) 67 68/* The last two bytes of an ES58X command is a CRC16. The first two 69 * bytes (the start of frame) are skipped and the CRC calculation 70 * starts on the third byte. 71 */ 72#define ES58X_CRC_CALC_OFFSET sizeof_field(union es58x_urb_cmd, sof) 73 74/** 75 * es58x_calculate_crc() - Compute the crc16 of a given URB. 76 * @urb_cmd: The URB command for which we want to calculate the CRC. 77 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4 78 * (ES58X_CRC_CALC_OFFSET + sizeof(crc)) 79 * 80 * Return: crc16 value. 81 */ 82static u16 es58x_calculate_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len) 83{ 84 u16 crc; 85 ssize_t len = urb_len - ES58X_CRC_CALC_OFFSET - sizeof(crc); 86 87 crc = crc16(0, &urb_cmd->raw_cmd[ES58X_CRC_CALC_OFFSET], len); 88 return crc; 89} 90 91/** 92 * es58x_get_crc() - Get the CRC value of a given URB. 93 * @urb_cmd: The URB command for which we want to get the CRC. 94 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4 95 * (ES58X_CRC_CALC_OFFSET + sizeof(crc)) 96 * 97 * Return: crc16 value. 98 */ 99static u16 es58x_get_crc(const union es58x_urb_cmd *urb_cmd, u16 urb_len) 100{ 101 u16 crc; 102 const __le16 *crc_addr; 103 104 crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)]; 105 crc = get_unaligned_le16(crc_addr); 106 return crc; 107} 108 109/** 110 * es58x_set_crc() - Set the CRC value of a given URB. 111 * @urb_cmd: The URB command for which we want to get the CRC. 112 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4 113 * (ES58X_CRC_CALC_OFFSET + sizeof(crc)) 114 */ 115static void es58x_set_crc(union es58x_urb_cmd *urb_cmd, u16 urb_len) 116{ 117 u16 crc; 118 __le16 *crc_addr; 119 120 crc = es58x_calculate_crc(urb_cmd, urb_len); 121 crc_addr = (__le16 *)&urb_cmd->raw_cmd[urb_len - sizeof(crc)]; 122 put_unaligned_le16(crc, crc_addr); 123} 124 125/** 126 * es58x_check_crc() - Validate the CRC value of a given URB. 127 * @es58x_dev: ES58X device. 128 * @urb_cmd: The URB command for which we want to check the CRC. 129 * @urb_len: Length of @urb_cmd. Must be at least bigger than 4 130 * (ES58X_CRC_CALC_OFFSET + sizeof(crc)) 131 * 132 * Return: zero on success, -EBADMSG if the CRC check fails. 133 */ 134static int es58x_check_crc(struct es58x_device *es58x_dev, 135 const union es58x_urb_cmd *urb_cmd, u16 urb_len) 136{ 137 u16 calculated_crc = es58x_calculate_crc(urb_cmd, urb_len); 138 u16 expected_crc = es58x_get_crc(urb_cmd, urb_len); 139 140 if (expected_crc != calculated_crc) { 141 dev_err_ratelimited(es58x_dev->dev, 142 "%s: Bad CRC, urb_len: %d\n", 143 __func__, urb_len); 144 return -EBADMSG; 145 } 146 147 return 0; 148} 149 150/** 151 * es58x_timestamp_to_ns() - Convert a timestamp value received from a 152 * ES58X device to nanoseconds. 153 * @timestamp: Timestamp received from a ES58X device. 154 * 155 * The timestamp received from ES58X is expressed in multiples of 0.5 156 * micro seconds. This function converts it in to nanoseconds. 157 * 158 * Return: Timestamp value in nanoseconds. 159 */ 160static u64 es58x_timestamp_to_ns(u64 timestamp) 161{ 162 const u64 es58x_timestamp_ns_mult_coef = 500ULL; 163 164 return es58x_timestamp_ns_mult_coef * timestamp; 165} 166 167/** 168 * es58x_set_skb_timestamp() - Set the hardware timestamp of an skb. 169 * @netdev: CAN network device. 170 * @skb: socket buffer of a CAN message. 171 * @timestamp: Timestamp received from an ES58X device. 172 * 173 * Used for both received and echo messages. 174 */ 175static void es58x_set_skb_timestamp(struct net_device *netdev, 176 struct sk_buff *skb, u64 timestamp) 177{ 178 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev; 179 struct skb_shared_hwtstamps *hwts; 180 181 hwts = skb_hwtstamps(skb); 182 /* Ignoring overflow (overflow on 64 bits timestamp with nano 183 * second precision would occur after more than 500 years). 184 */ 185 hwts->hwtstamp = ns_to_ktime(es58x_timestamp_to_ns(timestamp) + 186 es58x_dev->realtime_diff_ns); 187} 188 189/** 190 * es58x_rx_timestamp() - Handle a received timestamp. 191 * @es58x_dev: ES58X device. 192 * @timestamp: Timestamp received from a ES58X device. 193 * 194 * Calculate the difference between the ES58X device and the kernel 195 * internal clocks. This difference will be later used as an offset to 196 * convert the timestamps of RX and echo messages to match the kernel 197 * system time (e.g. convert to UNIX time). 198 */ 199void es58x_rx_timestamp(struct es58x_device *es58x_dev, u64 timestamp) 200{ 201 u64 ktime_real_ns = ktime_get_real_ns(); 202 u64 device_timestamp = es58x_timestamp_to_ns(timestamp); 203 204 dev_dbg(es58x_dev->dev, "%s: request round-trip time: %llu ns\n", 205 __func__, ktime_real_ns - es58x_dev->ktime_req_ns); 206 207 es58x_dev->realtime_diff_ns = 208 (es58x_dev->ktime_req_ns + ktime_real_ns) / 2 - device_timestamp; 209 es58x_dev->ktime_req_ns = 0; 210 211 dev_dbg(es58x_dev->dev, 212 "%s: Device timestamp: %llu, diff with kernel: %llu\n", 213 __func__, device_timestamp, es58x_dev->realtime_diff_ns); 214} 215 216/** 217 * es58x_set_realtime_diff_ns() - Calculate difference between the 218 * clocks of the ES58X device and the kernel 219 * @es58x_dev: ES58X device. 220 * 221 * Request a timestamp from the ES58X device. Once the answer is 222 * received, the timestamp difference will be set by the callback 223 * function es58x_rx_timestamp(). 224 * 225 * Return: zero on success, errno when any error occurs. 226 */ 227static int es58x_set_realtime_diff_ns(struct es58x_device *es58x_dev) 228{ 229 if (es58x_dev->ktime_req_ns) { 230 dev_warn(es58x_dev->dev, 231 "%s: Previous request to set timestamp has not completed yet\n", 232 __func__); 233 return -EBUSY; 234 } 235 236 es58x_dev->ktime_req_ns = ktime_get_real_ns(); 237 return es58x_dev->ops->get_timestamp(es58x_dev); 238} 239 240/** 241 * es58x_is_can_state_active() - Is the network device in an active 242 * CAN state? 243 * @netdev: CAN network device. 244 * 245 * The device is considered active if it is able to send or receive 246 * CAN frames, that is to say if it is in any of 247 * CAN_STATE_ERROR_ACTIVE, CAN_STATE_ERROR_WARNING or 248 * CAN_STATE_ERROR_PASSIVE states. 249 * 250 * Caution: when recovering from a bus-off, 251 * net/core/dev.c#can_restart() will call 252 * net/core/dev.c#can_flush_echo_skb() without using any kind of 253 * locks. For this reason, it is critical to guarantee that no TX or 254 * echo operations (i.e. any access to priv->echo_skb[]) can be done 255 * while this function is returning false. 256 * 257 * Return: true if the device is active, else returns false. 258 */ 259static bool es58x_is_can_state_active(struct net_device *netdev) 260{ 261 return es58x_priv(netdev)->can.state < CAN_STATE_BUS_OFF; 262} 263 264/** 265 * es58x_is_echo_skb_threshold_reached() - Determine the limit of how 266 * many skb slots can be taken before we should stop the network 267 * queue. 268 * @priv: ES58X private parameters related to the network device. 269 * 270 * We need to save enough free skb slots in order to be able to do 271 * bulk send. This function can be used to determine when to wake or 272 * stop the network queue in regard to the number of skb slots already 273 * taken if the echo FIFO. 274 * 275 * Return: boolean. 276 */ 277static bool es58x_is_echo_skb_threshold_reached(struct es58x_priv *priv) 278{ 279 u32 num_echo_skb = priv->tx_head - priv->tx_tail; 280 u32 threshold = priv->can.echo_skb_max - 281 priv->es58x_dev->param->tx_bulk_max + 1; 282 283 return num_echo_skb >= threshold; 284} 285 286/** 287 * es58x_can_free_echo_skb_tail() - Remove the oldest echo skb of the 288 * echo FIFO. 289 * @netdev: CAN network device. 290 * 291 * Naming convention: the tail is the beginning of the FIFO, i.e. the 292 * first skb to have entered the FIFO. 293 */ 294static void es58x_can_free_echo_skb_tail(struct net_device *netdev) 295{ 296 struct es58x_priv *priv = es58x_priv(netdev); 297 u16 fifo_mask = priv->es58x_dev->param->fifo_mask; 298 unsigned int frame_len = 0; 299 300 can_free_echo_skb(netdev, priv->tx_tail & fifo_mask, &frame_len); 301 netdev_completed_queue(netdev, 1, frame_len); 302 303 priv->tx_tail++; 304 305 netdev->stats.tx_dropped++; 306} 307 308/** 309 * es58x_can_get_echo_skb_recovery() - Try to re-sync the echo FIFO. 310 * @netdev: CAN network device. 311 * @rcv_packet_idx: Index 312 * 313 * This function should not be called under normal circumstances. In 314 * the unlikely case that one or several URB packages get dropped by 315 * the device, the index will get out of sync. Try to recover by 316 * dropping the echo skb packets with older indexes. 317 * 318 * Return: zero if recovery was successful, -EINVAL otherwise. 319 */ 320static int es58x_can_get_echo_skb_recovery(struct net_device *netdev, 321 u32 rcv_packet_idx) 322{ 323 struct es58x_priv *priv = es58x_priv(netdev); 324 int ret = 0; 325 326 netdev->stats.tx_errors++; 327 328 if (net_ratelimit()) 329 netdev_warn(netdev, 330 "Bad echo packet index: %u. First index: %u, end index %u, num_echo_skb: %02u/%02u\n", 331 rcv_packet_idx, priv->tx_tail, priv->tx_head, 332 priv->tx_head - priv->tx_tail, 333 priv->can.echo_skb_max); 334 335 if ((s32)(rcv_packet_idx - priv->tx_tail) < 0) { 336 if (net_ratelimit()) 337 netdev_warn(netdev, 338 "Received echo index is from the past. Ignoring it\n"); 339 ret = -EINVAL; 340 } else if ((s32)(rcv_packet_idx - priv->tx_head) >= 0) { 341 if (net_ratelimit()) 342 netdev_err(netdev, 343 "Received echo index is from the future. Ignoring it\n"); 344 ret = -EINVAL; 345 } else { 346 if (net_ratelimit()) 347 netdev_warn(netdev, 348 "Recovery: dropping %u echo skb from index %u to %u\n", 349 rcv_packet_idx - priv->tx_tail, 350 priv->tx_tail, rcv_packet_idx - 1); 351 while (priv->tx_tail != rcv_packet_idx) { 352 if (priv->tx_tail == priv->tx_head) 353 return -EINVAL; 354 es58x_can_free_echo_skb_tail(netdev); 355 } 356 } 357 return ret; 358} 359 360/** 361 * es58x_can_get_echo_skb() - Get the skb from the echo FIFO and loop 362 * it back locally. 363 * @netdev: CAN network device. 364 * @rcv_packet_idx: Index of the first packet received from the device. 365 * @tstamps: Array of hardware timestamps received from a ES58X device. 366 * @pkts: Number of packets (and so, length of @tstamps). 367 * 368 * Callback function for when we receive a self reception 369 * acknowledgment. Retrieves the skb from the echo FIFO, sets its 370 * hardware timestamp (the actual time it was sent) and loops it back 371 * locally. 372 * 373 * The device has to be active (i.e. network interface UP and not in 374 * bus off state or restarting). 375 * 376 * Packet indexes must be consecutive (i.e. index of first packet is 377 * @rcv_packet_idx, index of second packet is @rcv_packet_idx + 1 and 378 * index of last packet is @rcv_packet_idx + @pkts - 1). 379 * 380 * Return: zero on success. 381 */ 382int es58x_can_get_echo_skb(struct net_device *netdev, u32 rcv_packet_idx, 383 u64 *tstamps, unsigned int pkts) 384{ 385 struct es58x_priv *priv = es58x_priv(netdev); 386 unsigned int rx_total_frame_len = 0; 387 unsigned int num_echo_skb = priv->tx_head - priv->tx_tail; 388 int i; 389 u16 fifo_mask = priv->es58x_dev->param->fifo_mask; 390 391 if (!netif_running(netdev)) { 392 if (net_ratelimit()) 393 netdev_info(netdev, 394 "%s: %s is down, dropping %d echo packets\n", 395 __func__, netdev->name, pkts); 396 netdev->stats.tx_dropped += pkts; 397 return 0; 398 } else if (!es58x_is_can_state_active(netdev)) { 399 if (net_ratelimit()) 400 netdev_dbg(netdev, 401 "Bus is off or device is restarting. Ignoring %u echo packets from index %u\n", 402 pkts, rcv_packet_idx); 403 /* stats.tx_dropped will be (or was already) 404 * incremented by 405 * drivers/net/can/net/dev.c:can_flush_echo_skb(). 406 */ 407 return 0; 408 } else if (num_echo_skb == 0) { 409 if (net_ratelimit()) 410 netdev_warn(netdev, 411 "Received %u echo packets from index: %u but echo skb queue is empty.\n", 412 pkts, rcv_packet_idx); 413 netdev->stats.tx_dropped += pkts; 414 return 0; 415 } 416 417 if (priv->tx_tail != rcv_packet_idx) { 418 if (es58x_can_get_echo_skb_recovery(netdev, rcv_packet_idx) < 0) { 419 if (net_ratelimit()) 420 netdev_warn(netdev, 421 "Could not find echo skb for echo packet index: %u\n", 422 rcv_packet_idx); 423 return 0; 424 } 425 } 426 if (num_echo_skb < pkts) { 427 int pkts_drop = pkts - num_echo_skb; 428 429 if (net_ratelimit()) 430 netdev_err(netdev, 431 "Received %u echo packets but have only %d echo skb. Dropping %d echo skb\n", 432 pkts, num_echo_skb, pkts_drop); 433 netdev->stats.tx_dropped += pkts_drop; 434 pkts -= pkts_drop; 435 } 436 437 for (i = 0; i < pkts; i++) { 438 unsigned int skb_idx = priv->tx_tail & fifo_mask; 439 struct sk_buff *skb = priv->can.echo_skb[skb_idx]; 440 unsigned int frame_len = 0; 441 442 if (skb) 443 es58x_set_skb_timestamp(netdev, skb, tstamps[i]); 444 445 netdev->stats.tx_bytes += can_get_echo_skb(netdev, skb_idx, 446 &frame_len); 447 rx_total_frame_len += frame_len; 448 449 priv->tx_tail++; 450 } 451 452 netdev_completed_queue(netdev, pkts, rx_total_frame_len); 453 netdev->stats.tx_packets += pkts; 454 455 priv->err_passive_before_rtx_success = 0; 456 if (!es58x_is_echo_skb_threshold_reached(priv)) 457 netif_wake_queue(netdev); 458 459 return 0; 460} 461 462/** 463 * es58x_can_reset_echo_fifo() - Reset the echo FIFO. 464 * @netdev: CAN network device. 465 * 466 * The echo_skb array of struct can_priv will be flushed by 467 * drivers/net/can/dev.c:can_flush_echo_skb(). This function resets 468 * the parameters of the struct es58x_priv of our device and reset the 469 * queue (c.f. BQL). 470 */ 471static void es58x_can_reset_echo_fifo(struct net_device *netdev) 472{ 473 struct es58x_priv *priv = es58x_priv(netdev); 474 475 priv->tx_tail = 0; 476 priv->tx_head = 0; 477 priv->tx_urb = NULL; 478 priv->err_passive_before_rtx_success = 0; 479 netdev_reset_queue(netdev); 480} 481 482/** 483 * es58x_flush_pending_tx_msg() - Reset the buffer for transmission messages. 484 * @netdev: CAN network device. 485 * 486 * es58x_start_xmit() will queue up to tx_bulk_max messages in 487 * &tx_urb buffer and do a bulk send of all messages in one single URB 488 * (c.f. xmit_more flag). When the device recovers from a bus off 489 * state or when the device stops, the tx_urb buffer might still have 490 * pending messages in it and thus need to be flushed. 491 */ 492static void es58x_flush_pending_tx_msg(struct net_device *netdev) 493{ 494 struct es58x_priv *priv = es58x_priv(netdev); 495 struct es58x_device *es58x_dev = priv->es58x_dev; 496 497 if (priv->tx_urb) { 498 netdev_warn(netdev, "%s: dropping %d TX messages\n", 499 __func__, priv->tx_can_msg_cnt); 500 netdev->stats.tx_dropped += priv->tx_can_msg_cnt; 501 while (priv->tx_can_msg_cnt > 0) { 502 unsigned int frame_len = 0; 503 u16 fifo_mask = priv->es58x_dev->param->fifo_mask; 504 505 priv->tx_head--; 506 priv->tx_can_msg_cnt--; 507 can_free_echo_skb(netdev, priv->tx_head & fifo_mask, 508 &frame_len); 509 netdev_completed_queue(netdev, 1, frame_len); 510 } 511 usb_anchor_urb(priv->tx_urb, &priv->es58x_dev->tx_urbs_idle); 512 atomic_inc(&es58x_dev->tx_urbs_idle_cnt); 513 usb_free_urb(priv->tx_urb); 514 } 515 priv->tx_urb = NULL; 516} 517 518/** 519 * es58x_tx_ack_msg() - Handle acknowledgment messages. 520 * @netdev: CAN network device. 521 * @tx_free_entries: Number of free entries in the device transmit FIFO. 522 * @rx_cmd_ret_u32: error code as returned by the ES58X device. 523 * 524 * ES58X sends an acknowledgment message after a transmission request 525 * is done. This is mandatory for the ES581.4 but is optional (and 526 * deactivated in this driver) for the ES58X_FD family. 527 * 528 * Under normal circumstances, this function should never throw an 529 * error message. 530 * 531 * Return: zero on success, errno when any error occurs. 532 */ 533int es58x_tx_ack_msg(struct net_device *netdev, u16 tx_free_entries, 534 enum es58x_ret_u32 rx_cmd_ret_u32) 535{ 536 struct es58x_priv *priv = es58x_priv(netdev); 537 538 if (tx_free_entries <= priv->es58x_dev->param->tx_bulk_max) { 539 if (net_ratelimit()) 540 netdev_err(netdev, 541 "Only %d entries left in device queue, num_echo_skb: %d/%d\n", 542 tx_free_entries, 543 priv->tx_head - priv->tx_tail, 544 priv->can.echo_skb_max); 545 netif_stop_queue(netdev); 546 } 547 548 return es58x_rx_cmd_ret_u32(netdev, ES58X_RET_TYPE_TX_MSG, 549 rx_cmd_ret_u32); 550} 551 552/** 553 * es58x_rx_can_msg() - Handle a received a CAN message. 554 * @netdev: CAN network device. 555 * @timestamp: Hardware time stamp (only relevant in rx branches). 556 * @data: CAN payload. 557 * @can_id: CAN ID. 558 * @es58x_flags: Please refer to enum es58x_flag. 559 * @dlc: Data Length Code (raw value). 560 * 561 * Fill up a CAN skb and post it. 562 * 563 * This function handles the case where the DLC of a classical CAN 564 * frame is greater than CAN_MAX_DLEN (c.f. the len8_dlc field of 565 * struct can_frame). 566 * 567 * Return: zero on success. 568 */ 569int es58x_rx_can_msg(struct net_device *netdev, u64 timestamp, const u8 *data, 570 canid_t can_id, enum es58x_flag es58x_flags, u8 dlc) 571{ 572 struct canfd_frame *cfd; 573 struct can_frame *ccf; 574 struct sk_buff *skb; 575 u8 len; 576 bool is_can_fd = !!(es58x_flags & ES58X_FLAG_FD_DATA); 577 578 if (dlc > CAN_MAX_RAW_DLC) { 579 netdev_err(netdev, 580 "%s: DLC is %d but maximum should be %d\n", 581 __func__, dlc, CAN_MAX_RAW_DLC); 582 return -EMSGSIZE; 583 } 584 585 if (is_can_fd) { 586 len = can_fd_dlc2len(dlc); 587 skb = alloc_canfd_skb(netdev, &cfd); 588 } else { 589 len = can_cc_dlc2len(dlc); 590 skb = alloc_can_skb(netdev, &ccf); 591 cfd = (struct canfd_frame *)ccf; 592 } 593 if (!skb) { 594 netdev->stats.rx_dropped++; 595 return 0; 596 } 597 598 cfd->can_id = can_id; 599 if (es58x_flags & ES58X_FLAG_EFF) 600 cfd->can_id |= CAN_EFF_FLAG; 601 if (is_can_fd) { 602 cfd->len = len; 603 if (es58x_flags & ES58X_FLAG_FD_BRS) 604 cfd->flags |= CANFD_BRS; 605 if (es58x_flags & ES58X_FLAG_FD_ESI) 606 cfd->flags |= CANFD_ESI; 607 } else { 608 can_frame_set_cc_len(ccf, dlc, es58x_priv(netdev)->can.ctrlmode); 609 if (es58x_flags & ES58X_FLAG_RTR) { 610 ccf->can_id |= CAN_RTR_FLAG; 611 len = 0; 612 } 613 } 614 memcpy(cfd->data, data, len); 615 netdev->stats.rx_packets++; 616 netdev->stats.rx_bytes += len; 617 618 es58x_set_skb_timestamp(netdev, skb, timestamp); 619 netif_rx(skb); 620 621 es58x_priv(netdev)->err_passive_before_rtx_success = 0; 622 623 return 0; 624} 625 626/** 627 * es58x_rx_err_msg() - Handle a received CAN event or error message. 628 * @netdev: CAN network device. 629 * @error: Error code. 630 * @event: Event code. 631 * @timestamp: Timestamp received from a ES58X device. 632 * 633 * Handle the errors and events received by the ES58X device, create 634 * a CAN error skb and post it. 635 * 636 * In some rare cases the devices might get stuck alternating between 637 * CAN_STATE_ERROR_PASSIVE and CAN_STATE_ERROR_WARNING. To prevent 638 * this behavior, we force a bus off state if the device goes in 639 * CAN_STATE_ERROR_WARNING for ES58X_MAX_CONSECUTIVE_WARN consecutive 640 * times with no successful transmission or reception in between. 641 * 642 * Once the device is in bus off state, the only way to restart it is 643 * through the drivers/net/can/dev.c:can_restart() function. The 644 * device is technically capable to recover by itself under certain 645 * circumstances, however, allowing self recovery would create 646 * complex race conditions with drivers/net/can/dev.c:can_restart() 647 * and thus was not implemented. To activate automatic restart, please 648 * set the restart-ms parameter (e.g. ip link set can0 type can 649 * restart-ms 100). 650 * 651 * If the bus is really instable, this function would try to send a 652 * lot of log messages. Those are rate limited (i.e. you will see 653 * messages such as "net_ratelimit: XXX callbacks suppressed" in 654 * dmesg). 655 * 656 * Return: zero on success, errno when any error occurs. 657 */ 658int es58x_rx_err_msg(struct net_device *netdev, enum es58x_err error, 659 enum es58x_event event, u64 timestamp) 660{ 661 struct es58x_priv *priv = es58x_priv(netdev); 662 struct can_priv *can = netdev_priv(netdev); 663 struct can_device_stats *can_stats = &can->can_stats; 664 struct can_frame *cf = NULL; 665 struct sk_buff *skb; 666 int ret = 0; 667 668 if (!netif_running(netdev)) { 669 if (net_ratelimit()) 670 netdev_info(netdev, "%s: %s is down, dropping packet\n", 671 __func__, netdev->name); 672 netdev->stats.rx_dropped++; 673 return 0; 674 } 675 676 if (error == ES58X_ERR_OK && event == ES58X_EVENT_OK) { 677 netdev_err(netdev, "%s: Both error and event are zero\n", 678 __func__); 679 return -EINVAL; 680 } 681 682 skb = alloc_can_err_skb(netdev, &cf); 683 684 switch (error) { 685 case ES58X_ERR_OK: /* 0: No error */ 686 break; 687 688 case ES58X_ERR_PROT_STUFF: 689 if (net_ratelimit()) 690 netdev_dbg(netdev, "Error BITSTUFF\n"); 691 if (cf) 692 cf->data[2] |= CAN_ERR_PROT_STUFF; 693 break; 694 695 case ES58X_ERR_PROT_FORM: 696 if (net_ratelimit()) 697 netdev_dbg(netdev, "Error FORMAT\n"); 698 if (cf) 699 cf->data[2] |= CAN_ERR_PROT_FORM; 700 break; 701 702 case ES58X_ERR_ACK: 703 if (net_ratelimit()) 704 netdev_dbg(netdev, "Error ACK\n"); 705 if (cf) 706 cf->can_id |= CAN_ERR_ACK; 707 break; 708 709 case ES58X_ERR_PROT_BIT: 710 if (net_ratelimit()) 711 netdev_dbg(netdev, "Error BIT\n"); 712 if (cf) 713 cf->data[2] |= CAN_ERR_PROT_BIT; 714 break; 715 716 case ES58X_ERR_PROT_CRC: 717 if (net_ratelimit()) 718 netdev_dbg(netdev, "Error CRC\n"); 719 if (cf) 720 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; 721 break; 722 723 case ES58X_ERR_PROT_BIT1: 724 if (net_ratelimit()) 725 netdev_dbg(netdev, 726 "Error: expected a recessive bit but monitored a dominant one\n"); 727 if (cf) 728 cf->data[2] |= CAN_ERR_PROT_BIT1; 729 break; 730 731 case ES58X_ERR_PROT_BIT0: 732 if (net_ratelimit()) 733 netdev_dbg(netdev, 734 "Error expected a dominant bit but monitored a recessive one\n"); 735 if (cf) 736 cf->data[2] |= CAN_ERR_PROT_BIT0; 737 break; 738 739 case ES58X_ERR_PROT_OVERLOAD: 740 if (net_ratelimit()) 741 netdev_dbg(netdev, "Error OVERLOAD\n"); 742 if (cf) 743 cf->data[2] |= CAN_ERR_PROT_OVERLOAD; 744 break; 745 746 case ES58X_ERR_PROT_UNSPEC: 747 if (net_ratelimit()) 748 netdev_dbg(netdev, "Unspecified error\n"); 749 if (cf) 750 cf->can_id |= CAN_ERR_PROT; 751 break; 752 753 default: 754 if (net_ratelimit()) 755 netdev_err(netdev, 756 "%s: Unspecified error code 0x%04X\n", 757 __func__, (int)error); 758 if (cf) 759 cf->can_id |= CAN_ERR_PROT; 760 break; 761 } 762 763 switch (event) { 764 case ES58X_EVENT_OK: /* 0: No event */ 765 break; 766 767 case ES58X_EVENT_CRTL_ACTIVE: 768 if (can->state == CAN_STATE_BUS_OFF) { 769 netdev_err(netdev, 770 "%s: state transition: BUS OFF -> ACTIVE\n", 771 __func__); 772 } 773 if (net_ratelimit()) 774 netdev_dbg(netdev, "Event CAN BUS ACTIVE\n"); 775 if (cf) 776 cf->data[1] |= CAN_ERR_CRTL_ACTIVE; 777 can->state = CAN_STATE_ERROR_ACTIVE; 778 break; 779 780 case ES58X_EVENT_CRTL_PASSIVE: 781 if (net_ratelimit()) 782 netdev_dbg(netdev, "Event CAN BUS PASSIVE\n"); 783 /* Either TX or RX error count reached passive state 784 * but we do not know which. Setting both flags by 785 * default. 786 */ 787 if (cf) { 788 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE; 789 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE; 790 } 791 if (can->state < CAN_STATE_BUS_OFF) 792 can->state = CAN_STATE_ERROR_PASSIVE; 793 can_stats->error_passive++; 794 if (priv->err_passive_before_rtx_success < U8_MAX) 795 priv->err_passive_before_rtx_success++; 796 break; 797 798 case ES58X_EVENT_CRTL_WARNING: 799 if (net_ratelimit()) 800 netdev_dbg(netdev, "Event CAN BUS WARNING\n"); 801 /* Either TX or RX error count reached warning state 802 * but we do not know which. Setting both flags by 803 * default. 804 */ 805 if (cf) { 806 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING; 807 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING; 808 } 809 if (can->state < CAN_STATE_BUS_OFF) 810 can->state = CAN_STATE_ERROR_WARNING; 811 can_stats->error_warning++; 812 break; 813 814 case ES58X_EVENT_BUSOFF: 815 if (net_ratelimit()) 816 netdev_dbg(netdev, "Event CAN BUS OFF\n"); 817 if (cf) 818 cf->can_id |= CAN_ERR_BUSOFF; 819 can_stats->bus_off++; 820 netif_stop_queue(netdev); 821 if (can->state != CAN_STATE_BUS_OFF) { 822 can->state = CAN_STATE_BUS_OFF; 823 can_bus_off(netdev); 824 ret = can->do_set_mode(netdev, CAN_MODE_STOP); 825 } 826 break; 827 828 case ES58X_EVENT_SINGLE_WIRE: 829 if (net_ratelimit()) 830 netdev_warn(netdev, 831 "Lost connection on either CAN high or CAN low\n"); 832 /* Lost connection on either CAN high or CAN 833 * low. Setting both flags by default. 834 */ 835 if (cf) { 836 cf->data[4] |= CAN_ERR_TRX_CANH_NO_WIRE; 837 cf->data[4] |= CAN_ERR_TRX_CANL_NO_WIRE; 838 } 839 break; 840 841 default: 842 if (net_ratelimit()) 843 netdev_err(netdev, 844 "%s: Unspecified event code 0x%04X\n", 845 __func__, (int)event); 846 if (cf) 847 cf->can_id |= CAN_ERR_CRTL; 848 break; 849 } 850 851 if (cf) { 852 if (cf->data[1]) 853 cf->can_id |= CAN_ERR_CRTL; 854 if (cf->data[2] || cf->data[3]) { 855 cf->can_id |= CAN_ERR_PROT; 856 can_stats->bus_error++; 857 } 858 if (cf->data[4]) 859 cf->can_id |= CAN_ERR_TRX; 860 861 es58x_set_skb_timestamp(netdev, skb, timestamp); 862 netif_rx(skb); 863 } 864 865 if ((event & ES58X_EVENT_CRTL_PASSIVE) && 866 priv->err_passive_before_rtx_success == ES58X_CONSECUTIVE_ERR_PASSIVE_MAX) { 867 netdev_info(netdev, 868 "Got %d consecutive warning events with no successful RX or TX. Forcing bus-off\n", 869 priv->err_passive_before_rtx_success); 870 return es58x_rx_err_msg(netdev, ES58X_ERR_OK, 871 ES58X_EVENT_BUSOFF, timestamp); 872 } 873 874 return ret; 875} 876 877/** 878 * es58x_cmd_ret_desc() - Convert a command type to a string. 879 * @cmd_ret_type: Type of the command which triggered the return code. 880 * 881 * The final line (return "<unknown>") should not be reached. If this 882 * is the case, there is an implementation bug. 883 * 884 * Return: a readable description of the @cmd_ret_type. 885 */ 886static const char *es58x_cmd_ret_desc(enum es58x_ret_type cmd_ret_type) 887{ 888 switch (cmd_ret_type) { 889 case ES58X_RET_TYPE_SET_BITTIMING: 890 return "Set bittiming"; 891 case ES58X_RET_TYPE_ENABLE_CHANNEL: 892 return "Enable channel"; 893 case ES58X_RET_TYPE_DISABLE_CHANNEL: 894 return "Disable channel"; 895 case ES58X_RET_TYPE_TX_MSG: 896 return "Transmit message"; 897 case ES58X_RET_TYPE_RESET_RX: 898 return "Reset RX"; 899 case ES58X_RET_TYPE_RESET_TX: 900 return "Reset TX"; 901 case ES58X_RET_TYPE_DEVICE_ERR: 902 return "Device error"; 903 } 904 905 return "<unknown>"; 906}; 907 908/** 909 * es58x_rx_cmd_ret_u8() - Handle the command's return code received 910 * from the ES58X device. 911 * @dev: Device, only used for the dev_XXX() print functions. 912 * @cmd_ret_type: Type of the command which triggered the return code. 913 * @rx_cmd_ret_u8: Command error code as returned by the ES58X device. 914 * 915 * Handles the 8 bits command return code. Those are specific to the 916 * ES581.4 device. The return value will eventually be used by 917 * es58x_handle_urb_cmd() function which will take proper actions in 918 * case of critical issues such and memory errors or bad CRC values. 919 * 920 * In contrast with es58x_rx_cmd_ret_u32(), the network device is 921 * unknown. 922 * 923 * Return: zero on success, return errno when any error occurs. 924 */ 925int es58x_rx_cmd_ret_u8(struct device *dev, 926 enum es58x_ret_type cmd_ret_type, 927 enum es58x_ret_u8 rx_cmd_ret_u8) 928{ 929 const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type); 930 931 switch (rx_cmd_ret_u8) { 932 case ES58X_RET_U8_OK: 933 dev_dbg_ratelimited(dev, "%s: OK\n", ret_desc); 934 return 0; 935 936 case ES58X_RET_U8_ERR_UNSPECIFIED_FAILURE: 937 dev_err(dev, "%s: unspecified failure\n", ret_desc); 938 return -EBADMSG; 939 940 case ES58X_RET_U8_ERR_NO_MEM: 941 dev_err(dev, "%s: device ran out of memory\n", ret_desc); 942 return -ENOMEM; 943 944 case ES58X_RET_U8_ERR_BAD_CRC: 945 dev_err(dev, "%s: CRC of previous command is incorrect\n", 946 ret_desc); 947 return -EIO; 948 949 default: 950 dev_err(dev, "%s: returned unknown value: 0x%02X\n", 951 ret_desc, rx_cmd_ret_u8); 952 return -EBADMSG; 953 } 954} 955 956/** 957 * es58x_rx_cmd_ret_u32() - Handle the command return code received 958 * from the ES58X device. 959 * @netdev: CAN network device. 960 * @cmd_ret_type: Type of the command which triggered the return code. 961 * @rx_cmd_ret_u32: error code as returned by the ES58X device. 962 * 963 * Handles the 32 bits command return code. The return value will 964 * eventually be used by es58x_handle_urb_cmd() function which will 965 * take proper actions in case of critical issues such and memory 966 * errors or bad CRC values. 967 * 968 * Return: zero on success, errno when any error occurs. 969 */ 970int es58x_rx_cmd_ret_u32(struct net_device *netdev, 971 enum es58x_ret_type cmd_ret_type, 972 enum es58x_ret_u32 rx_cmd_ret_u32) 973{ 974 struct es58x_priv *priv = es58x_priv(netdev); 975 const struct es58x_operators *ops = priv->es58x_dev->ops; 976 const char *ret_desc = es58x_cmd_ret_desc(cmd_ret_type); 977 978 switch (rx_cmd_ret_u32) { 979 case ES58X_RET_U32_OK: 980 switch (cmd_ret_type) { 981 case ES58X_RET_TYPE_ENABLE_CHANNEL: 982 es58x_can_reset_echo_fifo(netdev); 983 priv->can.state = CAN_STATE_ERROR_ACTIVE; 984 netif_wake_queue(netdev); 985 netdev_info(netdev, 986 "%s: %s (Serial Number %s): CAN%d channel becomes ready\n", 987 ret_desc, priv->es58x_dev->udev->product, 988 priv->es58x_dev->udev->serial, 989 priv->channel_idx + 1); 990 break; 991 992 case ES58X_RET_TYPE_TX_MSG: 993 if (IS_ENABLED(CONFIG_VERBOSE_DEBUG) && net_ratelimit()) 994 netdev_vdbg(netdev, "%s: OK\n", ret_desc); 995 break; 996 997 default: 998 netdev_dbg(netdev, "%s: OK\n", ret_desc); 999 break; 1000 } 1001 return 0; 1002 1003 case ES58X_RET_U32_ERR_UNSPECIFIED_FAILURE: 1004 if (cmd_ret_type == ES58X_RET_TYPE_ENABLE_CHANNEL) { 1005 int ret; 1006 1007 netdev_warn(netdev, 1008 "%s: channel is already opened, closing and re-opening it to reflect new configuration\n", 1009 ret_desc); 1010 ret = ops->disable_channel(es58x_priv(netdev)); 1011 if (ret) 1012 return ret; 1013 return ops->enable_channel(es58x_priv(netdev)); 1014 } 1015 if (cmd_ret_type == ES58X_RET_TYPE_DISABLE_CHANNEL) { 1016 netdev_info(netdev, 1017 "%s: channel is already closed\n", ret_desc); 1018 return 0; 1019 } 1020 netdev_err(netdev, 1021 "%s: unspecified failure\n", ret_desc); 1022 return -EBADMSG; 1023 1024 case ES58X_RET_U32_ERR_NO_MEM: 1025 netdev_err(netdev, "%s: device ran out of memory\n", ret_desc); 1026 return -ENOMEM; 1027 1028 case ES58X_RET_U32_WARN_PARAM_ADJUSTED: 1029 netdev_warn(netdev, 1030 "%s: some incompatible parameters have been adjusted\n", 1031 ret_desc); 1032 return 0; 1033 1034 case ES58X_RET_U32_WARN_TX_MAYBE_REORDER: 1035 netdev_warn(netdev, 1036 "%s: TX messages might have been reordered\n", 1037 ret_desc); 1038 return 0; 1039 1040 case ES58X_RET_U32_ERR_TIMEDOUT: 1041 netdev_err(netdev, "%s: command timed out\n", ret_desc); 1042 return -ETIMEDOUT; 1043 1044 case ES58X_RET_U32_ERR_FIFO_FULL: 1045 netdev_warn(netdev, "%s: fifo is full\n", ret_desc); 1046 return 0; 1047 1048 case ES58X_RET_U32_ERR_BAD_CONFIG: 1049 netdev_err(netdev, "%s: bad configuration\n", ret_desc); 1050 return -EINVAL; 1051 1052 case ES58X_RET_U32_ERR_NO_RESOURCE: 1053 netdev_err(netdev, "%s: no resource available\n", ret_desc); 1054 return -EBUSY; 1055 1056 default: 1057 netdev_err(netdev, "%s returned unknown value: 0x%08X\n", 1058 ret_desc, rx_cmd_ret_u32); 1059 return -EBADMSG; 1060 } 1061} 1062 1063/** 1064 * es58x_increment_rx_errors() - Increment the network devices' error 1065 * count. 1066 * @es58x_dev: ES58X device. 1067 * 1068 * If an error occurs on the early stages on receiving an URB command, 1069 * we might not be able to figure out on which network device the 1070 * error occurred. In such case, we arbitrarily increment the error 1071 * count of all the network devices attached to our ES58X device. 1072 */ 1073static void es58x_increment_rx_errors(struct es58x_device *es58x_dev) 1074{ 1075 int i; 1076 1077 for (i = 0; i < es58x_dev->num_can_ch; i++) 1078 if (es58x_dev->netdev[i]) 1079 es58x_dev->netdev[i]->stats.rx_errors++; 1080} 1081 1082/** 1083 * es58x_handle_urb_cmd() - Handle the URB command 1084 * @es58x_dev: ES58X device. 1085 * @urb_cmd: The URB command received from the ES58X device, might not 1086 * be aligned. 1087 * 1088 * Sends the URB command to the device specific function. Manages the 1089 * errors thrown back by those functions. 1090 */ 1091static void es58x_handle_urb_cmd(struct es58x_device *es58x_dev, 1092 const union es58x_urb_cmd *urb_cmd) 1093{ 1094 const struct es58x_operators *ops = es58x_dev->ops; 1095 size_t cmd_len; 1096 int i, ret; 1097 1098 ret = ops->handle_urb_cmd(es58x_dev, urb_cmd); 1099 switch (ret) { 1100 case 0: /* OK */ 1101 return; 1102 1103 case -ENODEV: 1104 dev_err_ratelimited(es58x_dev->dev, "Device is not ready\n"); 1105 break; 1106 1107 case -EINVAL: 1108 case -EMSGSIZE: 1109 case -EBADRQC: 1110 case -EBADMSG: 1111 case -ECHRNG: 1112 case -ETIMEDOUT: 1113 cmd_len = es58x_get_urb_cmd_len(es58x_dev, 1114 ops->get_msg_len(urb_cmd)); 1115 dev_err(es58x_dev->dev, 1116 "ops->handle_urb_cmd() returned error %pe", 1117 ERR_PTR(ret)); 1118 es58x_print_hex_dump(urb_cmd, cmd_len); 1119 break; 1120 1121 case -EFAULT: 1122 case -ENOMEM: 1123 case -EIO: 1124 default: 1125 dev_crit(es58x_dev->dev, 1126 "ops->handle_urb_cmd() returned error %pe, detaching all network devices\n", 1127 ERR_PTR(ret)); 1128 for (i = 0; i < es58x_dev->num_can_ch; i++) 1129 if (es58x_dev->netdev[i]) 1130 netif_device_detach(es58x_dev->netdev[i]); 1131 if (es58x_dev->ops->reset_device) 1132 es58x_dev->ops->reset_device(es58x_dev); 1133 break; 1134 } 1135 1136 /* Because the urb command could not fully be parsed, 1137 * channel_id is not confirmed. Incrementing rx_errors count 1138 * of all channels. 1139 */ 1140 es58x_increment_rx_errors(es58x_dev); 1141} 1142 1143/** 1144 * es58x_check_rx_urb() - Check the length and format of the URB command. 1145 * @es58x_dev: ES58X device. 1146 * @urb_cmd: The URB command received from the ES58X device, might not 1147 * be aligned. 1148 * @urb_actual_len: The actual length of the URB command. 1149 * 1150 * Check if the first message of the received urb is valid, that is to 1151 * say that both the header and the length are coherent. 1152 * 1153 * Return: 1154 * the length of the first message of the URB on success. 1155 * 1156 * -ENODATA if the URB command is incomplete (in which case, the URB 1157 * command should be buffered and combined with the next URB to try to 1158 * reconstitute the URB command). 1159 * 1160 * -EOVERFLOW if the length is bigger than the maximum expected one. 1161 * 1162 * -EBADRQC if the start of frame does not match the expected value. 1163 */ 1164static signed int es58x_check_rx_urb(struct es58x_device *es58x_dev, 1165 const union es58x_urb_cmd *urb_cmd, 1166 u32 urb_actual_len) 1167{ 1168 const struct device *dev = es58x_dev->dev; 1169 const struct es58x_parameters *param = es58x_dev->param; 1170 u16 sof, msg_len; 1171 signed int urb_cmd_len, ret; 1172 1173 if (urb_actual_len < param->urb_cmd_header_len) { 1174 dev_vdbg(dev, 1175 "%s: Received %d bytes [%*ph]: header incomplete\n", 1176 __func__, urb_actual_len, urb_actual_len, 1177 urb_cmd->raw_cmd); 1178 return -ENODATA; 1179 } 1180 1181 sof = get_unaligned_le16(&urb_cmd->sof); 1182 if (sof != param->rx_start_of_frame) { 1183 dev_err_ratelimited(es58x_dev->dev, 1184 "%s: Expected sequence 0x%04X for start of frame but got 0x%04X.\n", 1185 __func__, param->rx_start_of_frame, sof); 1186 return -EBADRQC; 1187 } 1188 1189 msg_len = es58x_dev->ops->get_msg_len(urb_cmd); 1190 urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len); 1191 if (urb_cmd_len > param->rx_urb_cmd_max_len) { 1192 dev_err_ratelimited(es58x_dev->dev, 1193 "%s: Biggest expected size for rx urb_cmd is %u but receive a command of size %d\n", 1194 __func__, 1195 param->rx_urb_cmd_max_len, urb_cmd_len); 1196 return -EOVERFLOW; 1197 } else if (urb_actual_len < urb_cmd_len) { 1198 dev_vdbg(dev, "%s: Received %02d/%02d bytes\n", 1199 __func__, urb_actual_len, urb_cmd_len); 1200 return -ENODATA; 1201 } 1202 1203 ret = es58x_check_crc(es58x_dev, urb_cmd, urb_cmd_len); 1204 if (ret) 1205 return ret; 1206 1207 return urb_cmd_len; 1208} 1209 1210/** 1211 * es58x_copy_to_cmd_buf() - Copy an array to the URB command buffer. 1212 * @es58x_dev: ES58X device. 1213 * @raw_cmd: the buffer we want to copy. 1214 * @raw_cmd_len: length of @raw_cmd. 1215 * 1216 * Concatenates @raw_cmd_len bytes of @raw_cmd to the end of the URB 1217 * command buffer. 1218 * 1219 * Return: zero on success, -EMSGSIZE if not enough space is available 1220 * to do the copy. 1221 */ 1222static int es58x_copy_to_cmd_buf(struct es58x_device *es58x_dev, 1223 u8 *raw_cmd, int raw_cmd_len) 1224{ 1225 if (es58x_dev->rx_cmd_buf_len + raw_cmd_len > 1226 es58x_dev->param->rx_urb_cmd_max_len) 1227 return -EMSGSIZE; 1228 1229 memcpy(&es58x_dev->rx_cmd_buf.raw_cmd[es58x_dev->rx_cmd_buf_len], 1230 raw_cmd, raw_cmd_len); 1231 es58x_dev->rx_cmd_buf_len += raw_cmd_len; 1232 1233 return 0; 1234} 1235 1236/** 1237 * es58x_split_urb_try_recovery() - Try to recover bad URB sequences. 1238 * @es58x_dev: ES58X device. 1239 * @raw_cmd: pointer to the buffer we want to copy. 1240 * @raw_cmd_len: length of @raw_cmd. 1241 * 1242 * Under some rare conditions, we might get incorrect URBs from the 1243 * device. From our observations, one of the valid URB gets replaced 1244 * by one from the past. The full root cause is not identified. 1245 * 1246 * This function looks for the next start of frame in the urb buffer 1247 * in order to try to recover. 1248 * 1249 * Such behavior was not observed on the devices of the ES58X FD 1250 * family and only seems to impact the ES581.4. 1251 * 1252 * Return: the number of bytes dropped on success, -EBADMSG if recovery failed. 1253 */ 1254static int es58x_split_urb_try_recovery(struct es58x_device *es58x_dev, 1255 u8 *raw_cmd, size_t raw_cmd_len) 1256{ 1257 union es58x_urb_cmd *urb_cmd; 1258 signed int urb_cmd_len; 1259 u16 sof; 1260 int dropped_bytes = 0; 1261 1262 es58x_increment_rx_errors(es58x_dev); 1263 1264 while (raw_cmd_len > sizeof(sof)) { 1265 urb_cmd = (union es58x_urb_cmd *)raw_cmd; 1266 sof = get_unaligned_le16(&urb_cmd->sof); 1267 1268 if (sof == es58x_dev->param->rx_start_of_frame) { 1269 urb_cmd_len = es58x_check_rx_urb(es58x_dev, 1270 urb_cmd, raw_cmd_len); 1271 if ((urb_cmd_len == -ENODATA) || urb_cmd_len > 0) { 1272 dev_info_ratelimited(es58x_dev->dev, 1273 "Recovery successful! Dropped %d bytes (urb_cmd_len: %d)\n", 1274 dropped_bytes, 1275 urb_cmd_len); 1276 return dropped_bytes; 1277 } 1278 } 1279 raw_cmd++; 1280 raw_cmd_len--; 1281 dropped_bytes++; 1282 } 1283 1284 dev_warn_ratelimited(es58x_dev->dev, "%s: Recovery failed\n", __func__); 1285 return -EBADMSG; 1286} 1287 1288/** 1289 * es58x_handle_incomplete_cmd() - Reconstitute an URB command from 1290 * different URB pieces. 1291 * @es58x_dev: ES58X device. 1292 * @urb: last urb buffer received. 1293 * 1294 * The device might split the URB commands in an arbitrary amount of 1295 * pieces. This function concatenates those in an URB buffer until a 1296 * full URB command is reconstituted and consume it. 1297 * 1298 * Return: 1299 * number of bytes consumed from @urb if successful. 1300 * 1301 * -ENODATA if the URB command is still incomplete. 1302 * 1303 * -EBADMSG if the URB command is incorrect. 1304 */ 1305static signed int es58x_handle_incomplete_cmd(struct es58x_device *es58x_dev, 1306 struct urb *urb) 1307{ 1308 size_t cpy_len; 1309 signed int urb_cmd_len, tmp_cmd_buf_len, ret; 1310 1311 tmp_cmd_buf_len = es58x_dev->rx_cmd_buf_len; 1312 cpy_len = min_t(int, es58x_dev->param->rx_urb_cmd_max_len - 1313 es58x_dev->rx_cmd_buf_len, urb->actual_length); 1314 ret = es58x_copy_to_cmd_buf(es58x_dev, urb->transfer_buffer, cpy_len); 1315 if (ret < 0) 1316 return ret; 1317 1318 urb_cmd_len = es58x_check_rx_urb(es58x_dev, &es58x_dev->rx_cmd_buf, 1319 es58x_dev->rx_cmd_buf_len); 1320 if (urb_cmd_len == -ENODATA) { 1321 return -ENODATA; 1322 } else if (urb_cmd_len < 0) { 1323 dev_err_ratelimited(es58x_dev->dev, 1324 "Could not reconstitute incomplete command from previous URB, dropping %d bytes\n", 1325 tmp_cmd_buf_len + urb->actual_length); 1326 dev_err_ratelimited(es58x_dev->dev, 1327 "Error code: %pe, es58x_dev->rx_cmd_buf_len: %d, urb->actual_length: %u\n", 1328 ERR_PTR(urb_cmd_len), 1329 tmp_cmd_buf_len, urb->actual_length); 1330 es58x_print_hex_dump(&es58x_dev->rx_cmd_buf, tmp_cmd_buf_len); 1331 es58x_print_hex_dump(urb->transfer_buffer, urb->actual_length); 1332 return urb->actual_length; 1333 } 1334 1335 es58x_handle_urb_cmd(es58x_dev, &es58x_dev->rx_cmd_buf); 1336 return urb_cmd_len - tmp_cmd_buf_len; /* consumed length */ 1337} 1338 1339/** 1340 * es58x_split_urb() - Cut the received URB in individual URB commands. 1341 * @es58x_dev: ES58X device. 1342 * @urb: last urb buffer received. 1343 * 1344 * The device might send urb in bulk format (i.e. several URB commands 1345 * concatenated together). This function will split all the commands 1346 * contained in the urb. 1347 * 1348 * Return: 1349 * number of bytes consumed from @urb if successful. 1350 * 1351 * -ENODATA if the URB command is incomplete. 1352 * 1353 * -EBADMSG if the URB command is incorrect. 1354 */ 1355static signed int es58x_split_urb(struct es58x_device *es58x_dev, 1356 struct urb *urb) 1357{ 1358 union es58x_urb_cmd *urb_cmd; 1359 u8 *raw_cmd = urb->transfer_buffer; 1360 s32 raw_cmd_len = urb->actual_length; 1361 int ret; 1362 1363 if (es58x_dev->rx_cmd_buf_len != 0) { 1364 ret = es58x_handle_incomplete_cmd(es58x_dev, urb); 1365 if (ret != -ENODATA) 1366 es58x_dev->rx_cmd_buf_len = 0; 1367 if (ret < 0) 1368 return ret; 1369 1370 raw_cmd += ret; 1371 raw_cmd_len -= ret; 1372 } 1373 1374 while (raw_cmd_len > 0) { 1375 if (raw_cmd[0] == ES58X_HEARTBEAT) { 1376 raw_cmd++; 1377 raw_cmd_len--; 1378 continue; 1379 } 1380 urb_cmd = (union es58x_urb_cmd *)raw_cmd; 1381 ret = es58x_check_rx_urb(es58x_dev, urb_cmd, raw_cmd_len); 1382 if (ret > 0) { 1383 es58x_handle_urb_cmd(es58x_dev, urb_cmd); 1384 } else if (ret == -ENODATA) { 1385 es58x_copy_to_cmd_buf(es58x_dev, raw_cmd, raw_cmd_len); 1386 return -ENODATA; 1387 } else if (ret < 0) { 1388 ret = es58x_split_urb_try_recovery(es58x_dev, raw_cmd, 1389 raw_cmd_len); 1390 if (ret < 0) 1391 return ret; 1392 } 1393 raw_cmd += ret; 1394 raw_cmd_len -= ret; 1395 } 1396 1397 return 0; 1398} 1399 1400/** 1401 * es58x_read_bulk_callback() - Callback for reading data from device. 1402 * @urb: last urb buffer received. 1403 * 1404 * This function gets eventually called each time an URB is received 1405 * from the ES58X device. 1406 * 1407 * Checks urb status, calls read function and resubmits urb read 1408 * operation. 1409 */ 1410static void es58x_read_bulk_callback(struct urb *urb) 1411{ 1412 struct es58x_device *es58x_dev = urb->context; 1413 const struct device *dev = es58x_dev->dev; 1414 int i, ret; 1415 1416 switch (urb->status) { 1417 case 0: /* success */ 1418 break; 1419 1420 case -EOVERFLOW: 1421 dev_err_ratelimited(dev, "%s: error %pe\n", 1422 __func__, ERR_PTR(urb->status)); 1423 es58x_print_hex_dump_debug(urb->transfer_buffer, 1424 urb->transfer_buffer_length); 1425 goto resubmit_urb; 1426 1427 case -EPROTO: 1428 dev_warn_ratelimited(dev, "%s: error %pe. Device unplugged?\n", 1429 __func__, ERR_PTR(urb->status)); 1430 goto free_urb; 1431 1432 case -ENOENT: 1433 case -EPIPE: 1434 dev_err_ratelimited(dev, "%s: error %pe\n", 1435 __func__, ERR_PTR(urb->status)); 1436 goto free_urb; 1437 1438 case -ESHUTDOWN: 1439 dev_dbg_ratelimited(dev, "%s: error %pe\n", 1440 __func__, ERR_PTR(urb->status)); 1441 goto free_urb; 1442 1443 default: 1444 dev_err_ratelimited(dev, "%s: error %pe\n", 1445 __func__, ERR_PTR(urb->status)); 1446 goto resubmit_urb; 1447 } 1448 1449 ret = es58x_split_urb(es58x_dev, urb); 1450 if ((ret != -ENODATA) && ret < 0) { 1451 dev_err(es58x_dev->dev, "es58x_split_urb() returned error %pe", 1452 ERR_PTR(ret)); 1453 es58x_print_hex_dump_debug(urb->transfer_buffer, 1454 urb->actual_length); 1455 1456 /* Because the urb command could not be parsed, 1457 * channel_id is not confirmed. Incrementing rx_errors 1458 * count of all channels. 1459 */ 1460 es58x_increment_rx_errors(es58x_dev); 1461 } 1462 1463 resubmit_urb: 1464 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 return; 1480 1481 free_urb: 1482 usb_free_coherent(urb->dev, urb->transfer_buffer_length, 1483 urb->transfer_buffer, urb->transfer_dma); 1484} 1485 1486/** 1487 * es58x_write_bulk_callback() - Callback after writing data to the device. 1488 * @urb: urb buffer which was previously submitted. 1489 * 1490 * This function gets eventually called each time an URB was sent to 1491 * the ES58X device. 1492 * 1493 * Puts the @urb back to the urbs idle anchor and tries to restart the 1494 * network queue. 1495 */ 1496static void es58x_write_bulk_callback(struct urb *urb) 1497{ 1498 struct net_device *netdev = urb->context; 1499 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev; 1500 1501 switch (urb->status) { 1502 case 0: /* success */ 1503 break; 1504 1505 case -EOVERFLOW: 1506 if (net_ratelimit()) 1507 netdev_err(netdev, "%s: error %pe\n", 1508 __func__, ERR_PTR(urb->status)); 1509 es58x_print_hex_dump(urb->transfer_buffer, 1510 urb->transfer_buffer_length); 1511 break; 1512 1513 case -ENOENT: 1514 if (net_ratelimit()) 1515 netdev_dbg(netdev, "%s: error %pe\n", 1516 __func__, ERR_PTR(urb->status)); 1517 usb_free_coherent(urb->dev, 1518 es58x_dev->param->tx_urb_cmd_max_len, 1519 urb->transfer_buffer, urb->transfer_dma); 1520 return; 1521 1522 default: 1523 if (net_ratelimit()) 1524 netdev_info(netdev, "%s: error %pe\n", 1525 __func__, ERR_PTR(urb->status)); 1526 break; 1527 } 1528 1529 usb_anchor_urb(urb, &es58x_dev->tx_urbs_idle); 1530 atomic_inc(&es58x_dev->tx_urbs_idle_cnt); 1531} 1532 1533/** 1534 * es58x_alloc_urb() - Allocate memory for an URB and its transfer 1535 * buffer. 1536 * @es58x_dev: ES58X device. 1537 * @urb: URB to be allocated. 1538 * @buf: used to return DMA address of buffer. 1539 * @buf_len: requested buffer size. 1540 * @mem_flags: affect whether allocation may block. 1541 * 1542 * Allocates an URB and its @transfer_buffer and set its @transfer_dma 1543 * address. 1544 * 1545 * This function is used at start-up to allocate all RX URBs at once 1546 * and during run time for TX URBs. 1547 * 1548 * Return: zero on success, -ENOMEM if no memory is available. 1549 */ 1550static int es58x_alloc_urb(struct es58x_device *es58x_dev, struct urb **urb, 1551 u8 **buf, size_t buf_len, gfp_t mem_flags) 1552{ 1553 *urb = usb_alloc_urb(0, mem_flags); 1554 if (!*urb) { 1555 dev_err(es58x_dev->dev, "No memory left for URBs\n"); 1556 return -ENOMEM; 1557 } 1558 1559 *buf = usb_alloc_coherent(es58x_dev->udev, buf_len, 1560 mem_flags, &(*urb)->transfer_dma); 1561 if (!*buf) { 1562 dev_err(es58x_dev->dev, "No memory left for USB buffer\n"); 1563 usb_free_urb(*urb); 1564 return -ENOMEM; 1565 } 1566 1567 (*urb)->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1568 1569 return 0; 1570} 1571 1572/** 1573 * es58x_get_tx_urb() - Get an URB for transmission. 1574 * @es58x_dev: ES58X device. 1575 * 1576 * Gets an URB from the idle urbs anchor or allocate a new one if the 1577 * anchor is empty. 1578 * 1579 * If there are more than ES58X_TX_URBS_MAX in the idle anchor, do 1580 * some garbage collection. The garbage collection is done here 1581 * instead of within es58x_write_bulk_callback() because 1582 * usb_free_coherent() should not be used in IRQ context: 1583 * c.f. WARN_ON(irqs_disabled()) in dma_free_attrs(). 1584 * 1585 * Return: a pointer to an URB on success, NULL if no memory is 1586 * available. 1587 */ 1588static struct urb *es58x_get_tx_urb(struct es58x_device *es58x_dev) 1589{ 1590 atomic_t *idle_cnt = &es58x_dev->tx_urbs_idle_cnt; 1591 struct urb *urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle); 1592 1593 if (!urb) { 1594 size_t tx_buf_len; 1595 u8 *buf; 1596 1597 tx_buf_len = es58x_dev->param->tx_urb_cmd_max_len; 1598 if (es58x_alloc_urb(es58x_dev, &urb, &buf, tx_buf_len, 1599 GFP_ATOMIC)) 1600 return NULL; 1601 1602 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->tx_pipe, 1603 buf, tx_buf_len, es58x_write_bulk_callback, 1604 NULL); 1605 return urb; 1606 } 1607 1608 while (atomic_dec_return(idle_cnt) > ES58X_TX_URBS_MAX) { 1609 /* Garbage collector */ 1610 struct urb *tmp = usb_get_from_anchor(&es58x_dev->tx_urbs_idle); 1611 1612 if (!tmp) 1613 break; 1614 usb_free_coherent(tmp->dev, 1615 es58x_dev->param->tx_urb_cmd_max_len, 1616 tmp->transfer_buffer, tmp->transfer_dma); 1617 usb_free_urb(tmp); 1618 } 1619 1620 return urb; 1621} 1622 1623/** 1624 * es58x_submit_urb() - Send data to the device. 1625 * @es58x_dev: ES58X device. 1626 * @urb: URB to be sent. 1627 * @netdev: CAN network device. 1628 * 1629 * Return: zero on success, errno when any error occurs. 1630 */ 1631static int es58x_submit_urb(struct es58x_device *es58x_dev, struct urb *urb, 1632 struct net_device *netdev) 1633{ 1634 int ret; 1635 1636 es58x_set_crc(urb->transfer_buffer, urb->transfer_buffer_length); 1637 urb->context = netdev; 1638 usb_anchor_urb(urb, &es58x_dev->tx_urbs_busy); 1639 ret = usb_submit_urb(urb, GFP_ATOMIC); 1640 if (ret) { 1641 netdev_err(netdev, "%s: USB send urb failure: %pe\n", 1642 __func__, ERR_PTR(ret)); 1643 usb_unanchor_urb(urb); 1644 usb_free_coherent(urb->dev, 1645 es58x_dev->param->tx_urb_cmd_max_len, 1646 urb->transfer_buffer, urb->transfer_dma); 1647 } 1648 usb_free_urb(urb); 1649 1650 return ret; 1651} 1652 1653/** 1654 * es58x_send_msg() - Prepare an URB and submit it. 1655 * @es58x_dev: ES58X device. 1656 * @cmd_type: Command type. 1657 * @cmd_id: Command ID. 1658 * @msg: ES58X message to be sent. 1659 * @msg_len: Length of @msg. 1660 * @channel_idx: Index of the network device. 1661 * 1662 * Creates an URB command from a given message, sets the header and the 1663 * CRC and then submits it. 1664 * 1665 * Return: zero on success, errno when any error occurs. 1666 */ 1667int es58x_send_msg(struct es58x_device *es58x_dev, u8 cmd_type, u8 cmd_id, 1668 const void *msg, u16 msg_len, int channel_idx) 1669{ 1670 struct net_device *netdev; 1671 union es58x_urb_cmd *urb_cmd; 1672 struct urb *urb; 1673 int urb_cmd_len; 1674 1675 if (channel_idx == ES58X_CHANNEL_IDX_NA) 1676 netdev = es58x_dev->netdev[0]; /* Default to first channel */ 1677 else 1678 netdev = es58x_dev->netdev[channel_idx]; 1679 1680 urb_cmd_len = es58x_get_urb_cmd_len(es58x_dev, msg_len); 1681 if (urb_cmd_len > es58x_dev->param->tx_urb_cmd_max_len) 1682 return -EOVERFLOW; 1683 1684 urb = es58x_get_tx_urb(es58x_dev); 1685 if (!urb) 1686 return -ENOMEM; 1687 1688 urb_cmd = urb->transfer_buffer; 1689 es58x_dev->ops->fill_urb_header(urb_cmd, cmd_type, cmd_id, 1690 channel_idx, msg_len); 1691 memcpy(&urb_cmd->raw_cmd[es58x_dev->param->urb_cmd_header_len], 1692 msg, msg_len); 1693 urb->transfer_buffer_length = urb_cmd_len; 1694 1695 return es58x_submit_urb(es58x_dev, urb, netdev); 1696} 1697 1698/** 1699 * es58x_alloc_rx_urbs() - Allocate RX URBs. 1700 * @es58x_dev: ES58X device. 1701 * 1702 * Allocate URBs for reception and anchor them. 1703 * 1704 * Return: zero on success, errno when any error occurs. 1705 */ 1706static int es58x_alloc_rx_urbs(struct es58x_device *es58x_dev) 1707{ 1708 const struct device *dev = es58x_dev->dev; 1709 const struct es58x_parameters *param = es58x_dev->param; 1710 u16 rx_buf_len = usb_maxpacket(es58x_dev->udev, es58x_dev->rx_pipe); 1711 struct urb *urb; 1712 u8 *buf; 1713 int i; 1714 int ret = -EINVAL; 1715 1716 for (i = 0; i < param->rx_urb_max; i++) { 1717 ret = es58x_alloc_urb(es58x_dev, &urb, &buf, rx_buf_len, 1718 GFP_KERNEL); 1719 if (ret) 1720 break; 1721 1722 usb_fill_bulk_urb(urb, es58x_dev->udev, es58x_dev->rx_pipe, 1723 buf, rx_buf_len, es58x_read_bulk_callback, 1724 es58x_dev); 1725 usb_anchor_urb(urb, &es58x_dev->rx_urbs); 1726 1727 ret = usb_submit_urb(urb, GFP_KERNEL); 1728 if (ret) { 1729 usb_unanchor_urb(urb); 1730 usb_free_coherent(es58x_dev->udev, rx_buf_len, 1731 buf, urb->transfer_dma); 1732 usb_free_urb(urb); 1733 break; 1734 } 1735 usb_free_urb(urb); 1736 } 1737 1738 if (i == 0) { 1739 dev_err(dev, "%s: Could not setup any rx URBs\n", __func__); 1740 return ret; 1741 } 1742 dev_dbg(dev, "%s: Allocated %d rx URBs each of size %u\n", 1743 __func__, i, rx_buf_len); 1744 1745 return 0; 1746} 1747 1748/** 1749 * es58x_free_urbs() - Free all the TX and RX URBs. 1750 * @es58x_dev: ES58X device. 1751 */ 1752static void es58x_free_urbs(struct es58x_device *es58x_dev) 1753{ 1754 struct urb *urb; 1755 1756 if (!usb_wait_anchor_empty_timeout(&es58x_dev->tx_urbs_busy, 1000)) { 1757 dev_err(es58x_dev->dev, "%s: Timeout, some TX urbs still remain\n", 1758 __func__); 1759 usb_kill_anchored_urbs(&es58x_dev->tx_urbs_busy); 1760 } 1761 1762 while ((urb = usb_get_from_anchor(&es58x_dev->tx_urbs_idle)) != NULL) { 1763 usb_free_coherent(urb->dev, es58x_dev->param->tx_urb_cmd_max_len, 1764 urb->transfer_buffer, urb->transfer_dma); 1765 usb_free_urb(urb); 1766 atomic_dec(&es58x_dev->tx_urbs_idle_cnt); 1767 } 1768 if (atomic_read(&es58x_dev->tx_urbs_idle_cnt)) 1769 dev_err(es58x_dev->dev, 1770 "All idle urbs were freed but tx_urb_idle_cnt is %d\n", 1771 atomic_read(&es58x_dev->tx_urbs_idle_cnt)); 1772 1773 usb_kill_anchored_urbs(&es58x_dev->rx_urbs); 1774} 1775 1776/** 1777 * es58x_open() - Enable the network device. 1778 * @netdev: CAN network device. 1779 * 1780 * Called when the network transitions to the up state. Allocate the 1781 * URB resources if needed and open the channel. 1782 * 1783 * Return: zero on success, errno when any error occurs. 1784 */ 1785static int es58x_open(struct net_device *netdev) 1786{ 1787 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev; 1788 int ret; 1789 1790 if (!es58x_dev->opened_channel_cnt) { 1791 ret = es58x_alloc_rx_urbs(es58x_dev); 1792 if (ret) 1793 return ret; 1794 1795 ret = es58x_set_realtime_diff_ns(es58x_dev); 1796 if (ret) 1797 goto free_urbs; 1798 } 1799 1800 ret = open_candev(netdev); 1801 if (ret) 1802 goto free_urbs; 1803 1804 ret = es58x_dev->ops->enable_channel(es58x_priv(netdev)); 1805 if (ret) 1806 goto free_urbs; 1807 1808 es58x_dev->opened_channel_cnt++; 1809 netif_start_queue(netdev); 1810 1811 return ret; 1812 1813 free_urbs: 1814 if (!es58x_dev->opened_channel_cnt) 1815 es58x_free_urbs(es58x_dev); 1816 netdev_err(netdev, "%s: Could not open the network device: %pe\n", 1817 __func__, ERR_PTR(ret)); 1818 1819 return ret; 1820} 1821 1822/** 1823 * es58x_stop() - Disable the network device. 1824 * @netdev: CAN network device. 1825 * 1826 * Called when the network transitions to the down state. If all the 1827 * channels of the device are closed, free the URB resources which are 1828 * not needed anymore. 1829 * 1830 * Return: zero on success, errno when any error occurs. 1831 */ 1832static int es58x_stop(struct net_device *netdev) 1833{ 1834 struct es58x_priv *priv = es58x_priv(netdev); 1835 struct es58x_device *es58x_dev = priv->es58x_dev; 1836 int ret; 1837 1838 netif_stop_queue(netdev); 1839 ret = es58x_dev->ops->disable_channel(priv); 1840 if (ret) 1841 return ret; 1842 1843 priv->can.state = CAN_STATE_STOPPED; 1844 es58x_can_reset_echo_fifo(netdev); 1845 close_candev(netdev); 1846 1847 es58x_flush_pending_tx_msg(netdev); 1848 1849 es58x_dev->opened_channel_cnt--; 1850 if (!es58x_dev->opened_channel_cnt) 1851 es58x_free_urbs(es58x_dev); 1852 1853 return 0; 1854} 1855 1856/** 1857 * es58x_xmit_commit() - Send the bulk urb. 1858 * @netdev: CAN network device. 1859 * 1860 * Do the bulk send. This function should be called only once by bulk 1861 * transmission. 1862 * 1863 * Return: zero on success, errno when any error occurs. 1864 */ 1865static int es58x_xmit_commit(struct net_device *netdev) 1866{ 1867 struct es58x_priv *priv = es58x_priv(netdev); 1868 int ret; 1869 1870 if (!es58x_is_can_state_active(netdev)) 1871 return -ENETDOWN; 1872 1873 if (es58x_is_echo_skb_threshold_reached(priv)) 1874 netif_stop_queue(netdev); 1875 1876 ret = es58x_submit_urb(priv->es58x_dev, priv->tx_urb, netdev); 1877 if (ret == 0) 1878 priv->tx_urb = NULL; 1879 1880 return ret; 1881} 1882 1883/** 1884 * es58x_xmit_more() - Can we put more packets? 1885 * @priv: ES58X private parameters related to the network device. 1886 * 1887 * Return: true if we can put more, false if it is time to send. 1888 */ 1889static bool es58x_xmit_more(struct es58x_priv *priv) 1890{ 1891 unsigned int free_slots = 1892 priv->can.echo_skb_max - (priv->tx_head - priv->tx_tail); 1893 1894 return netdev_xmit_more() && free_slots > 0 && 1895 priv->tx_can_msg_cnt < priv->es58x_dev->param->tx_bulk_max; 1896} 1897 1898/** 1899 * es58x_start_xmit() - Transmit an skb. 1900 * @skb: socket buffer of a CAN message. 1901 * @netdev: CAN network device. 1902 * 1903 * Called when a packet needs to be transmitted. 1904 * 1905 * This function relies on Byte Queue Limits (BQL). The main benefit 1906 * is to increase the throughput by allowing bulk transfers 1907 * (c.f. xmit_more flag). 1908 * 1909 * Queues up to tx_bulk_max messages in &tx_urb buffer and does 1910 * a bulk send of all messages in one single URB. 1911 * 1912 * Return: NETDEV_TX_OK regardless of if we could transmit the @skb or 1913 * had to drop it. 1914 */ 1915static netdev_tx_t es58x_start_xmit(struct sk_buff *skb, 1916 struct net_device *netdev) 1917{ 1918 struct es58x_priv *priv = es58x_priv(netdev); 1919 struct es58x_device *es58x_dev = priv->es58x_dev; 1920 unsigned int frame_len; 1921 int ret; 1922 1923 if (can_dev_dropped_skb(netdev, skb)) { 1924 if (priv->tx_urb) 1925 goto xmit_commit; 1926 return NETDEV_TX_OK; 1927 } 1928 1929 if (priv->tx_urb && priv->tx_can_msg_is_fd != can_is_canfd_skb(skb)) { 1930 /* Can not do bulk send with mixed CAN and CAN FD frames. */ 1931 ret = es58x_xmit_commit(netdev); 1932 if (ret) 1933 goto drop_skb; 1934 } 1935 1936 if (!priv->tx_urb) { 1937 priv->tx_urb = es58x_get_tx_urb(es58x_dev); 1938 if (!priv->tx_urb) { 1939 ret = -ENOMEM; 1940 goto drop_skb; 1941 } 1942 priv->tx_can_msg_cnt = 0; 1943 priv->tx_can_msg_is_fd = can_is_canfd_skb(skb); 1944 } 1945 1946 ret = es58x_dev->ops->tx_can_msg(priv, skb); 1947 if (ret) 1948 goto drop_skb; 1949 1950 frame_len = can_skb_get_frame_len(skb); 1951 ret = can_put_echo_skb(skb, netdev, 1952 priv->tx_head & es58x_dev->param->fifo_mask, 1953 frame_len); 1954 if (ret) 1955 goto xmit_failure; 1956 netdev_sent_queue(netdev, frame_len); 1957 1958 priv->tx_head++; 1959 priv->tx_can_msg_cnt++; 1960 1961 xmit_commit: 1962 if (!es58x_xmit_more(priv)) { 1963 ret = es58x_xmit_commit(netdev); 1964 if (ret) 1965 goto xmit_failure; 1966 } 1967 1968 return NETDEV_TX_OK; 1969 1970 drop_skb: 1971 dev_kfree_skb(skb); 1972 netdev->stats.tx_dropped++; 1973 xmit_failure: 1974 netdev_warn(netdev, "%s: send message failure: %pe\n", 1975 __func__, ERR_PTR(ret)); 1976 netdev->stats.tx_errors++; 1977 es58x_flush_pending_tx_msg(netdev); 1978 return NETDEV_TX_OK; 1979} 1980 1981static const struct net_device_ops es58x_netdev_ops = { 1982 .ndo_open = es58x_open, 1983 .ndo_stop = es58x_stop, 1984 .ndo_start_xmit = es58x_start_xmit, 1985 .ndo_hwtstamp_get = can_hwtstamp_get, 1986 .ndo_hwtstamp_set = can_hwtstamp_set, 1987}; 1988 1989static const struct ethtool_ops es58x_ethtool_ops = { 1990 .get_ts_info = can_ethtool_op_get_ts_info_hwts, 1991}; 1992 1993/** 1994 * es58x_set_mode() - Change network device mode. 1995 * @netdev: CAN network device. 1996 * @mode: either %CAN_MODE_START, %CAN_MODE_STOP or %CAN_MODE_SLEEP 1997 * 1998 * Currently, this function is only used to stop and restart the 1999 * channel during a bus off event (c.f. es58x_rx_err_msg() and 2000 * drivers/net/can/dev.c:can_restart() which are the two only 2001 * callers). 2002 * 2003 * Return: zero on success, errno when any error occurs. 2004 */ 2005static int es58x_set_mode(struct net_device *netdev, enum can_mode mode) 2006{ 2007 struct es58x_priv *priv = es58x_priv(netdev); 2008 2009 switch (mode) { 2010 case CAN_MODE_START: 2011 switch (priv->can.state) { 2012 case CAN_STATE_BUS_OFF: 2013 return priv->es58x_dev->ops->enable_channel(priv); 2014 2015 case CAN_STATE_STOPPED: 2016 return es58x_open(netdev); 2017 2018 case CAN_STATE_ERROR_ACTIVE: 2019 case CAN_STATE_ERROR_WARNING: 2020 case CAN_STATE_ERROR_PASSIVE: 2021 default: 2022 return 0; 2023 } 2024 2025 case CAN_MODE_STOP: 2026 switch (priv->can.state) { 2027 case CAN_STATE_STOPPED: 2028 return 0; 2029 2030 case CAN_STATE_ERROR_ACTIVE: 2031 case CAN_STATE_ERROR_WARNING: 2032 case CAN_STATE_ERROR_PASSIVE: 2033 case CAN_STATE_BUS_OFF: 2034 default: 2035 return priv->es58x_dev->ops->disable_channel(priv); 2036 } 2037 2038 case CAN_MODE_SLEEP: 2039 default: 2040 return -EOPNOTSUPP; 2041 } 2042} 2043 2044/** 2045 * es58x_init_priv() - Initialize private parameters. 2046 * @es58x_dev: ES58X device. 2047 * @priv: ES58X private parameters related to the network device. 2048 * @channel_idx: Index of the network device. 2049 * 2050 * Return: zero on success, errno if devlink port could not be 2051 * properly registered. 2052 */ 2053static int es58x_init_priv(struct es58x_device *es58x_dev, 2054 struct es58x_priv *priv, int channel_idx) 2055{ 2056 struct devlink_port_attrs attrs = { 2057 .flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL, 2058 }; 2059 const struct es58x_parameters *param = es58x_dev->param; 2060 struct can_priv *can = &priv->can; 2061 2062 priv->es58x_dev = es58x_dev; 2063 priv->channel_idx = channel_idx; 2064 priv->tx_urb = NULL; 2065 priv->tx_can_msg_cnt = 0; 2066 2067 can->bittiming_const = param->bittiming_const; 2068 if (param->ctrlmode_supported & CAN_CTRLMODE_FD) { 2069 can->fd.data_bittiming_const = param->data_bittiming_const; 2070 can->fd.tdc_const = param->tdc_const; 2071 } 2072 can->bitrate_max = param->bitrate_max; 2073 can->clock = param->clock; 2074 can->state = CAN_STATE_STOPPED; 2075 can->ctrlmode_supported = param->ctrlmode_supported; 2076 can->do_set_mode = es58x_set_mode; 2077 2078 devlink_port_attrs_set(&priv->devlink_port, &attrs); 2079 return devlink_port_register(priv_to_devlink(es58x_dev), 2080 &priv->devlink_port, channel_idx); 2081} 2082 2083/** 2084 * es58x_init_netdev() - Initialize the network device. 2085 * @es58x_dev: ES58X device. 2086 * @channel_idx: Index of the network device. 2087 * 2088 * Return: zero on success, errno when any error occurs. 2089 */ 2090static int es58x_init_netdev(struct es58x_device *es58x_dev, int channel_idx) 2091{ 2092 struct net_device *netdev; 2093 struct device *dev = es58x_dev->dev; 2094 int ret; 2095 2096 netdev = alloc_candev(sizeof(struct es58x_priv), 2097 es58x_dev->param->fifo_mask + 1); 2098 if (!netdev) { 2099 dev_err(dev, "Could not allocate candev\n"); 2100 return -ENOMEM; 2101 } 2102 SET_NETDEV_DEV(netdev, dev); 2103 es58x_dev->netdev[channel_idx] = netdev; 2104 ret = es58x_init_priv(es58x_dev, es58x_priv(netdev), channel_idx); 2105 if (ret) 2106 goto free_candev; 2107 SET_NETDEV_DEVLINK_PORT(netdev, &es58x_priv(netdev)->devlink_port); 2108 2109 netdev->netdev_ops = &es58x_netdev_ops; 2110 netdev->ethtool_ops = &es58x_ethtool_ops; 2111 netdev->flags |= IFF_ECHO; /* We support local echo */ 2112 netdev->dev_port = channel_idx; 2113 2114 ret = register_candev(netdev); 2115 if (ret) 2116 goto devlink_port_unregister; 2117 2118 netdev_queue_set_dql_min_limit(netdev_get_tx_queue(netdev, 0), 2119 es58x_dev->param->dql_min_limit); 2120 2121 return ret; 2122 2123 devlink_port_unregister: 2124 devlink_port_unregister(&es58x_priv(netdev)->devlink_port); 2125 free_candev: 2126 es58x_dev->netdev[channel_idx] = NULL; 2127 free_candev(netdev); 2128 return ret; 2129} 2130 2131/** 2132 * es58x_free_netdevs() - Release all network resources of the device. 2133 * @es58x_dev: ES58X device. 2134 */ 2135static void es58x_free_netdevs(struct es58x_device *es58x_dev) 2136{ 2137 int i; 2138 2139 for (i = 0; i < es58x_dev->num_can_ch; i++) { 2140 struct net_device *netdev = es58x_dev->netdev[i]; 2141 2142 if (!netdev) 2143 continue; 2144 unregister_candev(netdev); 2145 devlink_port_unregister(&es58x_priv(netdev)->devlink_port); 2146 es58x_dev->netdev[i] = NULL; 2147 free_candev(netdev); 2148 } 2149} 2150 2151/** 2152 * es58x_init_es58x_dev() - Initialize the ES58X device. 2153 * @intf: USB interface. 2154 * @driver_info: Quirks of the device. 2155 * 2156 * Return: pointer to an ES58X device on success, error pointer when 2157 * any error occurs. 2158 */ 2159static struct es58x_device *es58x_init_es58x_dev(struct usb_interface *intf, 2160 kernel_ulong_t driver_info) 2161{ 2162 struct device *dev = &intf->dev; 2163 struct es58x_device *es58x_dev; 2164 struct devlink *devlink; 2165 const struct es58x_parameters *param; 2166 const struct es58x_operators *ops; 2167 struct usb_device *udev = interface_to_usbdev(intf); 2168 struct usb_endpoint_descriptor *ep_in, *ep_out; 2169 int ret; 2170 2171 dev_info(dev, "Starting %s %s (Serial Number %s)\n", 2172 udev->manufacturer, udev->product, udev->serial); 2173 2174 ret = usb_find_common_endpoints(intf->cur_altsetting, &ep_in, &ep_out, 2175 NULL, NULL); 2176 if (ret) 2177 return ERR_PTR(ret); 2178 2179 if (driver_info & ES58X_FD_FAMILY) { 2180 param = &es58x_fd_param; 2181 ops = &es58x_fd_ops; 2182 } else { 2183 param = &es581_4_param; 2184 ops = &es581_4_ops; 2185 } 2186 2187 devlink = devlink_alloc(&es58x_dl_ops, es58x_sizeof_es58x_device(param), 2188 dev); 2189 if (!devlink) 2190 return ERR_PTR(-ENOMEM); 2191 2192 es58x_dev = devlink_priv(devlink); 2193 es58x_dev->param = param; 2194 es58x_dev->ops = ops; 2195 es58x_dev->dev = dev; 2196 es58x_dev->udev = udev; 2197 2198 if (driver_info & ES58X_DUAL_CHANNEL) 2199 es58x_dev->num_can_ch = 2; 2200 else 2201 es58x_dev->num_can_ch = 1; 2202 2203 init_usb_anchor(&es58x_dev->rx_urbs); 2204 init_usb_anchor(&es58x_dev->tx_urbs_idle); 2205 init_usb_anchor(&es58x_dev->tx_urbs_busy); 2206 atomic_set(&es58x_dev->tx_urbs_idle_cnt, 0); 2207 usb_set_intfdata(intf, es58x_dev); 2208 2209 es58x_dev->rx_pipe = usb_rcvbulkpipe(es58x_dev->udev, 2210 ep_in->bEndpointAddress); 2211 es58x_dev->tx_pipe = usb_sndbulkpipe(es58x_dev->udev, 2212 ep_out->bEndpointAddress); 2213 2214 return es58x_dev; 2215} 2216 2217/** 2218 * es58x_probe() - Initialize the USB device. 2219 * @intf: USB interface. 2220 * @id: USB device ID. 2221 * 2222 * Return: zero on success, -ENODEV if the interface is not supported 2223 * or errno when any other error occurs. 2224 */ 2225static int es58x_probe(struct usb_interface *intf, 2226 const struct usb_device_id *id) 2227{ 2228 struct es58x_device *es58x_dev; 2229 int ch_idx; 2230 2231 es58x_dev = es58x_init_es58x_dev(intf, id->driver_info); 2232 if (IS_ERR(es58x_dev)) 2233 return PTR_ERR(es58x_dev); 2234 2235 es58x_parse_product_info(es58x_dev); 2236 devlink_register(priv_to_devlink(es58x_dev)); 2237 2238 for (ch_idx = 0; ch_idx < es58x_dev->num_can_ch; ch_idx++) { 2239 int ret = es58x_init_netdev(es58x_dev, ch_idx); 2240 2241 if (ret) { 2242 es58x_free_netdevs(es58x_dev); 2243 return ret; 2244 } 2245 } 2246 2247 return 0; 2248} 2249 2250/** 2251 * es58x_disconnect() - Disconnect the USB device. 2252 * @intf: USB interface 2253 * 2254 * Called by the usb core when driver is unloaded or device is 2255 * removed. 2256 */ 2257static void es58x_disconnect(struct usb_interface *intf) 2258{ 2259 struct es58x_device *es58x_dev = usb_get_intfdata(intf); 2260 2261 dev_info(&intf->dev, "Disconnecting %s %s\n", 2262 es58x_dev->udev->manufacturer, es58x_dev->udev->product); 2263 2264 devlink_unregister(priv_to_devlink(es58x_dev)); 2265 es58x_free_netdevs(es58x_dev); 2266 es58x_free_urbs(es58x_dev); 2267 devlink_free(priv_to_devlink(es58x_dev)); 2268 usb_set_intfdata(intf, NULL); 2269} 2270 2271static struct usb_driver es58x_driver = { 2272 .name = KBUILD_MODNAME, 2273 .probe = es58x_probe, 2274 .disconnect = es58x_disconnect, 2275 .id_table = es58x_id_table 2276}; 2277 2278module_usb_driver(es58x_driver);
~/
Read-only file preview.