1 /* 2 * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com> 3 * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com> 4 * 5 * Copyright (C) 2016 PEAK System-Technik GmbH 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the version 2 of the GNU General Public License 9 * as published by the Free Software Foundation 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #include <linux/can.h> 18 #include <linux/can/dev.h> 19 20 #include "peak_canfd_user.h" 21 22 /* internal IP core cache size (used as default echo skbs max number) */ 23 #define PCANFD_ECHO_SKB_MAX 24 24 25 /* bittiming ranges of the PEAK-System PC CAN-FD interfaces */ 26 static const struct can_bittiming_const peak_canfd_nominal_const = { 27 .name = "peak_canfd", 28 .tseg1_min = 1, 29 .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS), 30 .tseg2_min = 1, 31 .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS), 32 .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS), 33 .brp_min = 1, 34 .brp_max = (1 << PUCAN_TSLOW_BRP_BITS), 35 .brp_inc = 1, 36 }; 37 38 static const struct can_bittiming_const peak_canfd_data_const = { 39 .name = "peak_canfd", 40 .tseg1_min = 1, 41 .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS), 42 .tseg2_min = 1, 43 .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS), 44 .sjw_max = (1 << PUCAN_TFAST_SJW_BITS), 45 .brp_min = 1, 46 .brp_max = (1 << PUCAN_TFAST_BRP_BITS), 47 .brp_inc = 1, 48 }; 49 50 static struct peak_canfd_priv *pucan_init_cmd(struct peak_canfd_priv *priv) 51 { 52 priv->cmd_len = 0; 53 return priv; 54 } 55 56 static void *pucan_add_cmd(struct peak_canfd_priv *priv, int cmd_op) 57 { 58 struct pucan_command *cmd; 59 60 if (priv->cmd_len + sizeof(*cmd) > priv->cmd_maxlen) 61 return NULL; 62 63 cmd = priv->cmd_buffer + priv->cmd_len; 64 65 /* reset all unused bit to default */ 66 memset(cmd, 0, sizeof(*cmd)); 67 68 cmd->opcode_channel = pucan_cmd_opcode_channel(priv->index, cmd_op); 69 priv->cmd_len += sizeof(*cmd); 70 71 return cmd; 72 } 73 74 static int pucan_write_cmd(struct peak_canfd_priv *priv) 75 { 76 int err; 77 78 if (priv->pre_cmd) { 79 err = priv->pre_cmd(priv); 80 if (err) 81 return err; 82 } 83 84 err = priv->write_cmd(priv); 85 if (err) 86 return err; 87 88 if (priv->post_cmd) 89 err = priv->post_cmd(priv); 90 91 return err; 92 } 93 94 /* uCAN commands interface functions */ 95 static int pucan_set_reset_mode(struct peak_canfd_priv *priv) 96 { 97 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RESET_MODE); 98 return pucan_write_cmd(priv); 99 } 100 101 static int pucan_set_normal_mode(struct peak_canfd_priv *priv) 102 { 103 int err; 104 105 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_NORMAL_MODE); 106 err = pucan_write_cmd(priv); 107 if (!err) 108 priv->can.state = CAN_STATE_ERROR_ACTIVE; 109 110 return err; 111 } 112 113 static int pucan_set_listen_only_mode(struct peak_canfd_priv *priv) 114 { 115 int err; 116 117 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_LISTEN_ONLY_MODE); 118 err = pucan_write_cmd(priv); 119 if (!err) 120 priv->can.state = CAN_STATE_ERROR_ACTIVE; 121 122 return err; 123 } 124 125 static int pucan_set_timing_slow(struct peak_canfd_priv *priv, 126 const struct can_bittiming *pbt) 127 { 128 struct pucan_timing_slow *cmd; 129 130 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_SLOW); 131 132 cmd->sjw_t = PUCAN_TSLOW_SJW_T(pbt->sjw - 1, 133 priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES); 134 cmd->tseg1 = PUCAN_TSLOW_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1); 135 cmd->tseg2 = PUCAN_TSLOW_TSEG2(pbt->phase_seg2 - 1); 136 cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(pbt->brp - 1)); 137 138 cmd->ewl = 96; /* default */ 139 140 netdev_dbg(priv->ndev, 141 "nominal: brp=%u tseg1=%u tseg2=%u sjw=%u\n", 142 le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw_t); 143 144 return pucan_write_cmd(priv); 145 } 146 147 static int pucan_set_timing_fast(struct peak_canfd_priv *priv, 148 const struct can_bittiming *pbt) 149 { 150 struct pucan_timing_fast *cmd; 151 152 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_FAST); 153 154 cmd->sjw = PUCAN_TFAST_SJW(pbt->sjw - 1); 155 cmd->tseg1 = PUCAN_TFAST_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1); 156 cmd->tseg2 = PUCAN_TFAST_TSEG2(pbt->phase_seg2 - 1); 157 cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(pbt->brp - 1)); 158 159 netdev_dbg(priv->ndev, 160 "data: brp=%u tseg1=%u tseg2=%u sjw=%u\n", 161 le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw); 162 163 return pucan_write_cmd(priv); 164 } 165 166 static int pucan_set_std_filter(struct peak_canfd_priv *priv, u8 row, u32 mask) 167 { 168 struct pucan_std_filter *cmd; 169 170 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_STD_FILTER); 171 172 /* all the 11-bits CAN ID values are represented by one bit in a 173 * 64 rows array of 32 bits: the upper 6 bits of the CAN ID select the 174 * row while the lowest 5 bits select the bit in that row. 175 * 176 * bit filter 177 * 1 passed 178 * 0 discarded 179 */ 180 181 /* select the row */ 182 cmd->idx = row; 183 184 /* set/unset bits in the row */ 185 cmd->mask = cpu_to_le32(mask); 186 187 return pucan_write_cmd(priv); 188 } 189 190 static int pucan_tx_abort(struct peak_canfd_priv *priv, u16 flags) 191 { 192 struct pucan_tx_abort *cmd; 193 194 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TX_ABORT); 195 196 cmd->flags = cpu_to_le16(flags); 197 198 return pucan_write_cmd(priv); 199 } 200 201 static int pucan_clr_err_counters(struct peak_canfd_priv *priv) 202 { 203 struct pucan_wr_err_cnt *cmd; 204 205 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_WR_ERR_CNT); 206 207 cmd->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE | PUCAN_WRERRCNT_RE); 208 cmd->tx_counter = 0; 209 cmd->rx_counter = 0; 210 211 return pucan_write_cmd(priv); 212 } 213 214 static int pucan_set_options(struct peak_canfd_priv *priv, u16 opt_mask) 215 { 216 struct pucan_options *cmd; 217 218 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_EN_OPTION); 219 220 cmd->options = cpu_to_le16(opt_mask); 221 222 return pucan_write_cmd(priv); 223 } 224 225 static int pucan_clr_options(struct peak_canfd_priv *priv, u16 opt_mask) 226 { 227 struct pucan_options *cmd; 228 229 cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_CLR_DIS_OPTION); 230 231 cmd->options = cpu_to_le16(opt_mask); 232 233 return pucan_write_cmd(priv); 234 } 235 236 static int pucan_setup_rx_barrier(struct peak_canfd_priv *priv) 237 { 238 pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RX_BARRIER); 239 240 return pucan_write_cmd(priv); 241 } 242 243 /* handle the reception of one CAN frame */ 244 static int pucan_handle_can_rx(struct peak_canfd_priv *priv, 245 struct pucan_rx_msg *msg) 246 { 247 struct net_device_stats *stats = &priv->ndev->stats; 248 struct canfd_frame *cf; 249 struct sk_buff *skb; 250 const u16 rx_msg_flags = le16_to_cpu(msg->flags); 251 u8 cf_len; 252 253 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) 254 cf_len = can_dlc2len(get_canfd_dlc(pucan_msg_get_dlc(msg))); 255 else 256 cf_len = get_can_dlc(pucan_msg_get_dlc(msg)); 257 258 /* if this frame is an echo, */ 259 if ((rx_msg_flags & PUCAN_MSG_LOOPED_BACK) && 260 !(rx_msg_flags & PUCAN_MSG_SELF_RECEIVE)) { 261 unsigned long flags; 262 263 spin_lock_irqsave(&priv->echo_lock, flags); 264 can_get_echo_skb(priv->ndev, msg->client); 265 266 /* count bytes of the echo instead of skb */ 267 stats->tx_bytes += cf_len; 268 stats->tx_packets++; 269 270 /* restart tx queue (a slot is free) */ 271 netif_wake_queue(priv->ndev); 272 273 spin_unlock_irqrestore(&priv->echo_lock, flags); 274 return 0; 275 } 276 277 /* otherwise, it should be pushed into rx fifo */ 278 if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) { 279 /* CANFD frame case */ 280 skb = alloc_canfd_skb(priv->ndev, &cf); 281 if (!skb) 282 return -ENOMEM; 283 284 if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH) 285 cf->flags |= CANFD_BRS; 286 287 if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND) 288 cf->flags |= CANFD_ESI; 289 } else { 290 /* CAN 2.0 frame case */ 291 skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf); 292 if (!skb) 293 return -ENOMEM; 294 } 295 296 cf->can_id = le32_to_cpu(msg->can_id); 297 cf->len = cf_len; 298 299 if (rx_msg_flags & PUCAN_MSG_EXT_ID) 300 cf->can_id |= CAN_EFF_FLAG; 301 302 if (rx_msg_flags & PUCAN_MSG_RTR) 303 cf->can_id |= CAN_RTR_FLAG; 304 else 305 memcpy(cf->data, msg->d, cf->len); 306 307 stats->rx_bytes += cf->len; 308 stats->rx_packets++; 309 310 netif_rx(skb); 311 312 return 0; 313 } 314 315 /* handle rx/tx error counters notification */ 316 static int pucan_handle_error(struct peak_canfd_priv *priv, 317 struct pucan_error_msg *msg) 318 { 319 priv->bec.txerr = msg->tx_err_cnt; 320 priv->bec.rxerr = msg->rx_err_cnt; 321 322 return 0; 323 } 324 325 /* handle status notification */ 326 static int pucan_handle_status(struct peak_canfd_priv *priv, 327 struct pucan_status_msg *msg) 328 { 329 struct net_device *ndev = priv->ndev; 330 struct net_device_stats *stats = &ndev->stats; 331 struct can_frame *cf; 332 struct sk_buff *skb; 333 334 /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */ 335 if (pucan_status_is_rx_barrier(msg)) { 336 337 if (priv->enable_tx_path) { 338 int err = priv->enable_tx_path(priv); 339 340 if (err) 341 return err; 342 } 343 344 /* start network queue (echo_skb array is empty) */ 345 netif_start_queue(ndev); 346 347 return 0; 348 } 349 350 skb = alloc_can_err_skb(ndev, &cf); 351 352 /* test state error bits according to their priority */ 353 if (pucan_status_is_busoff(msg)) { 354 netdev_dbg(ndev, "Bus-off entry status\n"); 355 priv->can.state = CAN_STATE_BUS_OFF; 356 priv->can.can_stats.bus_off++; 357 can_bus_off(ndev); 358 if (skb) 359 cf->can_id |= CAN_ERR_BUSOFF; 360 361 } else if (pucan_status_is_passive(msg)) { 362 netdev_dbg(ndev, "Error passive status\n"); 363 priv->can.state = CAN_STATE_ERROR_PASSIVE; 364 priv->can.can_stats.error_passive++; 365 if (skb) { 366 cf->can_id |= CAN_ERR_CRTL; 367 cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ? 368 CAN_ERR_CRTL_TX_PASSIVE : 369 CAN_ERR_CRTL_RX_PASSIVE; 370 cf->data[6] = priv->bec.txerr; 371 cf->data[7] = priv->bec.rxerr; 372 } 373 374 } else if (pucan_status_is_warning(msg)) { 375 netdev_dbg(ndev, "Error warning status\n"); 376 priv->can.state = CAN_STATE_ERROR_WARNING; 377 priv->can.can_stats.error_warning++; 378 if (skb) { 379 cf->can_id |= CAN_ERR_CRTL; 380 cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ? 381 CAN_ERR_CRTL_TX_WARNING : 382 CAN_ERR_CRTL_RX_WARNING; 383 cf->data[6] = priv->bec.txerr; 384 cf->data[7] = priv->bec.rxerr; 385 } 386 387 } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) { 388 /* back to ERROR_ACTIVE */ 389 netdev_dbg(ndev, "Error active status\n"); 390 can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE, 391 CAN_STATE_ERROR_ACTIVE); 392 } else { 393 dev_kfree_skb(skb); 394 return 0; 395 } 396 397 if (!skb) { 398 stats->rx_dropped++; 399 return -ENOMEM; 400 } 401 402 stats->rx_packets++; 403 stats->rx_bytes += cf->can_dlc; 404 netif_rx(skb); 405 406 return 0; 407 } 408 409 /* handle uCAN Rx overflow notification */ 410 static int pucan_handle_cache_critical(struct peak_canfd_priv *priv) 411 { 412 struct net_device_stats *stats = &priv->ndev->stats; 413 struct can_frame *cf; 414 struct sk_buff *skb; 415 416 stats->rx_over_errors++; 417 stats->rx_errors++; 418 419 skb = alloc_can_err_skb(priv->ndev, &cf); 420 if (!skb) { 421 stats->rx_dropped++; 422 return -ENOMEM; 423 } 424 425 cf->can_id |= CAN_ERR_CRTL; 426 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 427 428 cf->data[6] = priv->bec.txerr; 429 cf->data[7] = priv->bec.rxerr; 430 431 stats->rx_bytes += cf->can_dlc; 432 stats->rx_packets++; 433 netif_rx(skb); 434 435 return 0; 436 } 437 438 /* handle a single uCAN message */ 439 int peak_canfd_handle_msg(struct peak_canfd_priv *priv, 440 struct pucan_rx_msg *msg) 441 { 442 u16 msg_type = le16_to_cpu(msg->type); 443 int msg_size = le16_to_cpu(msg->size); 444 int err; 445 446 if (!msg_size || !msg_type) { 447 /* null packet found: end of list */ 448 goto exit; 449 } 450 451 switch (msg_type) { 452 case PUCAN_MSG_CAN_RX: 453 err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg); 454 break; 455 case PUCAN_MSG_ERROR: 456 err = pucan_handle_error(priv, (struct pucan_error_msg *)msg); 457 break; 458 case PUCAN_MSG_STATUS: 459 err = pucan_handle_status(priv, (struct pucan_status_msg *)msg); 460 break; 461 case PUCAN_MSG_CACHE_CRITICAL: 462 err = pucan_handle_cache_critical(priv); 463 break; 464 default: 465 err = 0; 466 } 467 468 if (err < 0) 469 return err; 470 471 exit: 472 return msg_size; 473 } 474 475 /* handle a list of rx_count messages from rx_msg memory address */ 476 int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv, 477 struct pucan_rx_msg *msg_list, int msg_count) 478 { 479 void *msg_ptr = msg_list; 480 int i, msg_size = 0; 481 482 for (i = 0; i < msg_count; i++) { 483 msg_size = peak_canfd_handle_msg(priv, msg_ptr); 484 485 /* a null packet can be found at the end of a list */ 486 if (msg_size <= 0) 487 break; 488 489 msg_ptr += ALIGN(msg_size, 4); 490 } 491 492 if (msg_size < 0) 493 return msg_size; 494 495 return i; 496 } 497 498 static int peak_canfd_start(struct peak_canfd_priv *priv) 499 { 500 int err; 501 502 err = pucan_clr_err_counters(priv); 503 if (err) 504 goto err_exit; 505 506 priv->echo_idx = 0; 507 508 priv->bec.txerr = 0; 509 priv->bec.rxerr = 0; 510 511 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 512 err = pucan_set_listen_only_mode(priv); 513 else 514 err = pucan_set_normal_mode(priv); 515 516 err_exit: 517 return err; 518 } 519 520 static void peak_canfd_stop(struct peak_canfd_priv *priv) 521 { 522 int err; 523 524 /* go back to RESET mode */ 525 err = pucan_set_reset_mode(priv); 526 if (err) { 527 netdev_err(priv->ndev, "channel %u reset failed\n", 528 priv->index); 529 } else { 530 /* abort last Tx (MUST be done in RESET mode only!) */ 531 pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH); 532 } 533 } 534 535 static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode) 536 { 537 struct peak_canfd_priv *priv = netdev_priv(ndev); 538 539 switch (mode) { 540 case CAN_MODE_START: 541 peak_canfd_start(priv); 542 netif_wake_queue(ndev); 543 break; 544 default: 545 return -EOPNOTSUPP; 546 } 547 548 return 0; 549 } 550 551 static int peak_canfd_get_berr_counter(const struct net_device *ndev, 552 struct can_berr_counter *bec) 553 { 554 struct peak_canfd_priv *priv = netdev_priv(ndev); 555 556 *bec = priv->bec; 557 return 0; 558 } 559 560 static int peak_canfd_open(struct net_device *ndev) 561 { 562 struct peak_canfd_priv *priv = netdev_priv(ndev); 563 int i, err = 0; 564 565 err = open_candev(ndev); 566 if (err) { 567 netdev_err(ndev, "open_candev() failed, error %d\n", err); 568 goto err_exit; 569 } 570 571 err = pucan_set_reset_mode(priv); 572 if (err) 573 goto err_close; 574 575 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { 576 if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) 577 err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO); 578 else 579 err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO); 580 581 if (err) 582 goto err_close; 583 } 584 585 /* set option: get rx/tx error counters */ 586 err = pucan_set_options(priv, PUCAN_OPTION_ERROR); 587 if (err) 588 goto err_close; 589 590 /* accept all standard CAN ID */ 591 for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++) 592 pucan_set_std_filter(priv, i, 0xffffffff); 593 594 err = peak_canfd_start(priv); 595 if (err) 596 goto err_close; 597 598 /* receiving the RB status says when Tx path is ready */ 599 err = pucan_setup_rx_barrier(priv); 600 if (!err) 601 goto err_exit; 602 603 err_close: 604 close_candev(ndev); 605 err_exit: 606 return err; 607 } 608 609 static int peak_canfd_set_bittiming(struct net_device *ndev) 610 { 611 struct peak_canfd_priv *priv = netdev_priv(ndev); 612 613 return pucan_set_timing_slow(priv, &priv->can.bittiming); 614 } 615 616 static int peak_canfd_set_data_bittiming(struct net_device *ndev) 617 { 618 struct peak_canfd_priv *priv = netdev_priv(ndev); 619 620 return pucan_set_timing_fast(priv, &priv->can.data_bittiming); 621 } 622 623 static int peak_canfd_close(struct net_device *ndev) 624 { 625 struct peak_canfd_priv *priv = netdev_priv(ndev); 626 627 netif_stop_queue(ndev); 628 peak_canfd_stop(priv); 629 close_candev(ndev); 630 631 return 0; 632 } 633 634 static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb, 635 struct net_device *ndev) 636 { 637 struct peak_canfd_priv *priv = netdev_priv(ndev); 638 struct net_device_stats *stats = &ndev->stats; 639 struct canfd_frame *cf = (struct canfd_frame *)skb->data; 640 struct pucan_tx_msg *msg; 641 u16 msg_size, msg_flags; 642 unsigned long flags; 643 bool should_stop_tx_queue; 644 int room_left; 645 u8 can_dlc; 646 647 if (can_dropped_invalid_skb(ndev, skb)) 648 return NETDEV_TX_OK; 649 650 msg_size = ALIGN(sizeof(*msg) + cf->len, 4); 651 msg = priv->alloc_tx_msg(priv, msg_size, &room_left); 652 653 /* should never happen except under bus-off condition and (auto-)restart 654 * mechanism 655 */ 656 if (!msg) { 657 stats->tx_dropped++; 658 netif_stop_queue(ndev); 659 return NETDEV_TX_BUSY; 660 } 661 662 msg->size = cpu_to_le16(msg_size); 663 msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX); 664 msg_flags = 0; 665 666 if (cf->can_id & CAN_EFF_FLAG) { 667 msg_flags |= PUCAN_MSG_EXT_ID; 668 msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK); 669 } else { 670 msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK); 671 } 672 673 if (can_is_canfd_skb(skb)) { 674 /* CAN FD frame format */ 675 can_dlc = can_len2dlc(cf->len); 676 677 msg_flags |= PUCAN_MSG_EXT_DATA_LEN; 678 679 if (cf->flags & CANFD_BRS) 680 msg_flags |= PUCAN_MSG_BITRATE_SWITCH; 681 682 if (cf->flags & CANFD_ESI) 683 msg_flags |= PUCAN_MSG_ERROR_STATE_IND; 684 } else { 685 /* CAN 2.0 frame format */ 686 can_dlc = cf->len; 687 688 if (cf->can_id & CAN_RTR_FLAG) 689 msg_flags |= PUCAN_MSG_RTR; 690 } 691 692 /* always ask loopback for echo management */ 693 msg_flags |= PUCAN_MSG_LOOPED_BACK; 694 695 /* set driver specific bit to differentiate with application loopback */ 696 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) 697 msg_flags |= PUCAN_MSG_SELF_RECEIVE; 698 699 msg->flags = cpu_to_le16(msg_flags); 700 msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, can_dlc); 701 memcpy(msg->d, cf->data, cf->len); 702 703 /* struct msg client field is used as an index in the echo skbs ring */ 704 msg->client = priv->echo_idx; 705 706 spin_lock_irqsave(&priv->echo_lock, flags); 707 708 /* prepare and save echo skb in internal slot */ 709 can_put_echo_skb(skb, ndev, priv->echo_idx); 710 711 /* move echo index to the next slot */ 712 priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max; 713 714 /* if next slot is not free, stop network queue (no slot free in echo 715 * skb ring means that the controller did not write these frames on 716 * the bus: no need to continue). 717 */ 718 should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]); 719 720 /* stop network tx queue if not enough room to save one more msg too */ 721 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) 722 should_stop_tx_queue |= (room_left < 723 (sizeof(*msg) + CANFD_MAX_DLEN)); 724 else 725 should_stop_tx_queue |= (room_left < 726 (sizeof(*msg) + CAN_MAX_DLEN)); 727 728 if (should_stop_tx_queue) 729 netif_stop_queue(ndev); 730 731 spin_unlock_irqrestore(&priv->echo_lock, flags); 732 733 /* write the skb on the interface */ 734 priv->write_tx_msg(priv, msg); 735 736 return NETDEV_TX_OK; 737 } 738 739 static const struct net_device_ops peak_canfd_netdev_ops = { 740 .ndo_open = peak_canfd_open, 741 .ndo_stop = peak_canfd_close, 742 .ndo_start_xmit = peak_canfd_start_xmit, 743 .ndo_change_mtu = can_change_mtu, 744 }; 745 746 struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index, 747 int echo_skb_max) 748 { 749 struct net_device *ndev; 750 struct peak_canfd_priv *priv; 751 752 /* we DO support local echo */ 753 if (echo_skb_max < 0) 754 echo_skb_max = PCANFD_ECHO_SKB_MAX; 755 756 /* allocate the candev object */ 757 ndev = alloc_candev(sizeof_priv, echo_skb_max); 758 if (!ndev) 759 return NULL; 760 761 priv = netdev_priv(ndev); 762 763 /* complete now socket-can initialization side */ 764 priv->can.state = CAN_STATE_STOPPED; 765 priv->can.bittiming_const = &peak_canfd_nominal_const; 766 priv->can.data_bittiming_const = &peak_canfd_data_const; 767 768 priv->can.do_set_mode = peak_canfd_set_mode; 769 priv->can.do_get_berr_counter = peak_canfd_get_berr_counter; 770 priv->can.do_set_bittiming = peak_canfd_set_bittiming; 771 priv->can.do_set_data_bittiming = peak_canfd_set_data_bittiming; 772 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | 773 CAN_CTRLMODE_LISTENONLY | 774 CAN_CTRLMODE_3_SAMPLES | 775 CAN_CTRLMODE_FD | 776 CAN_CTRLMODE_FD_NON_ISO | 777 CAN_CTRLMODE_BERR_REPORTING; 778 779 priv->ndev = ndev; 780 priv->index = index; 781 priv->cmd_len = 0; 782 spin_lock_init(&priv->echo_lock); 783 784 ndev->flags |= IFF_ECHO; 785 ndev->netdev_ops = &peak_canfd_netdev_ops; 786 ndev->dev_id = index; 787 788 return ndev; 789 } 790