Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
0003  * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
0004  *
0005  * Copyright (C) 2016  PEAK System-Technik GmbH
0006  */
0007 
0008 #include <linux/can.h>
0009 #include <linux/can/dev.h>
0010 #include <linux/ethtool.h>
0011 
0012 #include "peak_canfd_user.h"
0013 
0014 /* internal IP core cache size (used as default echo skbs max number) */
0015 #define PCANFD_ECHO_SKB_MAX     24
0016 
0017 /* bittiming ranges of the PEAK-System PC CAN-FD interfaces */
0018 static const struct can_bittiming_const peak_canfd_nominal_const = {
0019     .name = "peak_canfd",
0020     .tseg1_min = 1,
0021     .tseg1_max = (1 << PUCAN_TSLOW_TSGEG1_BITS),
0022     .tseg2_min = 1,
0023     .tseg2_max = (1 << PUCAN_TSLOW_TSGEG2_BITS),
0024     .sjw_max = (1 << PUCAN_TSLOW_SJW_BITS),
0025     .brp_min = 1,
0026     .brp_max = (1 << PUCAN_TSLOW_BRP_BITS),
0027     .brp_inc = 1,
0028 };
0029 
0030 static const struct can_bittiming_const peak_canfd_data_const = {
0031     .name = "peak_canfd",
0032     .tseg1_min = 1,
0033     .tseg1_max = (1 << PUCAN_TFAST_TSGEG1_BITS),
0034     .tseg2_min = 1,
0035     .tseg2_max = (1 << PUCAN_TFAST_TSGEG2_BITS),
0036     .sjw_max = (1 << PUCAN_TFAST_SJW_BITS),
0037     .brp_min = 1,
0038     .brp_max = (1 << PUCAN_TFAST_BRP_BITS),
0039     .brp_inc = 1,
0040 };
0041 
0042 static struct peak_canfd_priv *pucan_init_cmd(struct peak_canfd_priv *priv)
0043 {
0044     priv->cmd_len = 0;
0045     return priv;
0046 }
0047 
0048 static void *pucan_add_cmd(struct peak_canfd_priv *priv, int cmd_op)
0049 {
0050     struct pucan_command *cmd;
0051 
0052     if (priv->cmd_len + sizeof(*cmd) > priv->cmd_maxlen)
0053         return NULL;
0054 
0055     cmd = priv->cmd_buffer + priv->cmd_len;
0056 
0057     /* reset all unused bit to default */
0058     memset(cmd, 0, sizeof(*cmd));
0059 
0060     cmd->opcode_channel = pucan_cmd_opcode_channel(priv->index, cmd_op);
0061     priv->cmd_len += sizeof(*cmd);
0062 
0063     return cmd;
0064 }
0065 
0066 static int pucan_write_cmd(struct peak_canfd_priv *priv)
0067 {
0068     int err;
0069 
0070     if (priv->pre_cmd) {
0071         err = priv->pre_cmd(priv);
0072         if (err)
0073             return err;
0074     }
0075 
0076     err = priv->write_cmd(priv);
0077     if (err)
0078         return err;
0079 
0080     if (priv->post_cmd)
0081         err = priv->post_cmd(priv);
0082 
0083     return err;
0084 }
0085 
0086 /* uCAN commands interface functions */
0087 static int pucan_set_reset_mode(struct peak_canfd_priv *priv)
0088 {
0089     pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RESET_MODE);
0090     return pucan_write_cmd(priv);
0091 }
0092 
0093 static int pucan_set_normal_mode(struct peak_canfd_priv *priv)
0094 {
0095     int err;
0096 
0097     pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_NORMAL_MODE);
0098     err = pucan_write_cmd(priv);
0099     if (!err)
0100         priv->can.state = CAN_STATE_ERROR_ACTIVE;
0101 
0102     return err;
0103 }
0104 
0105 static int pucan_set_listen_only_mode(struct peak_canfd_priv *priv)
0106 {
0107     int err;
0108 
0109     pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_LISTEN_ONLY_MODE);
0110     err = pucan_write_cmd(priv);
0111     if (!err)
0112         priv->can.state = CAN_STATE_ERROR_ACTIVE;
0113 
0114     return err;
0115 }
0116 
0117 static int pucan_set_timing_slow(struct peak_canfd_priv *priv,
0118                  const struct can_bittiming *pbt)
0119 {
0120     struct pucan_timing_slow *cmd;
0121 
0122     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_SLOW);
0123 
0124     cmd->sjw_t = PUCAN_TSLOW_SJW_T(pbt->sjw - 1,
0125                        priv->can.ctrlmode &
0126                        CAN_CTRLMODE_3_SAMPLES);
0127     cmd->tseg1 = PUCAN_TSLOW_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
0128     cmd->tseg2 = PUCAN_TSLOW_TSEG2(pbt->phase_seg2 - 1);
0129     cmd->brp = cpu_to_le16(PUCAN_TSLOW_BRP(pbt->brp - 1));
0130 
0131     cmd->ewl = 96;  /* default */
0132 
0133     netdev_dbg(priv->ndev,
0134            "nominal: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
0135            le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw_t);
0136 
0137     return pucan_write_cmd(priv);
0138 }
0139 
0140 static int pucan_set_timing_fast(struct peak_canfd_priv *priv,
0141                  const struct can_bittiming *pbt)
0142 {
0143     struct pucan_timing_fast *cmd;
0144 
0145     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TIMING_FAST);
0146 
0147     cmd->sjw = PUCAN_TFAST_SJW(pbt->sjw - 1);
0148     cmd->tseg1 = PUCAN_TFAST_TSEG1(pbt->prop_seg + pbt->phase_seg1 - 1);
0149     cmd->tseg2 = PUCAN_TFAST_TSEG2(pbt->phase_seg2 - 1);
0150     cmd->brp = cpu_to_le16(PUCAN_TFAST_BRP(pbt->brp - 1));
0151 
0152     netdev_dbg(priv->ndev,
0153            "data: brp=%u tseg1=%u tseg2=%u sjw=%u\n",
0154            le16_to_cpu(cmd->brp), cmd->tseg1, cmd->tseg2, cmd->sjw);
0155 
0156     return pucan_write_cmd(priv);
0157 }
0158 
0159 static int pucan_set_std_filter(struct peak_canfd_priv *priv, u8 row, u32 mask)
0160 {
0161     struct pucan_std_filter *cmd;
0162 
0163     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_STD_FILTER);
0164 
0165     /* all the 11-bits CAN ID values are represented by one bit in a
0166      * 64 rows array of 32 bits: the upper 6 bits of the CAN ID select the
0167      * row while the lowest 5 bits select the bit in that row.
0168      *
0169      * bit  filter
0170      * 1    passed
0171      * 0    discarded
0172      */
0173 
0174     /* select the row */
0175     cmd->idx = row;
0176 
0177     /* set/unset bits in the row */
0178     cmd->mask = cpu_to_le32(mask);
0179 
0180     return pucan_write_cmd(priv);
0181 }
0182 
0183 static int pucan_tx_abort(struct peak_canfd_priv *priv, u16 flags)
0184 {
0185     struct pucan_tx_abort *cmd;
0186 
0187     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_TX_ABORT);
0188 
0189     cmd->flags = cpu_to_le16(flags);
0190 
0191     return pucan_write_cmd(priv);
0192 }
0193 
0194 static int pucan_clr_err_counters(struct peak_canfd_priv *priv)
0195 {
0196     struct pucan_wr_err_cnt *cmd;
0197 
0198     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_WR_ERR_CNT);
0199 
0200     cmd->sel_mask = cpu_to_le16(PUCAN_WRERRCNT_TE | PUCAN_WRERRCNT_RE);
0201     cmd->tx_counter = 0;
0202     cmd->rx_counter = 0;
0203 
0204     return pucan_write_cmd(priv);
0205 }
0206 
0207 static int pucan_set_options(struct peak_canfd_priv *priv, u16 opt_mask)
0208 {
0209     struct pucan_options *cmd;
0210 
0211     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_SET_EN_OPTION);
0212 
0213     cmd->options = cpu_to_le16(opt_mask);
0214 
0215     return pucan_write_cmd(priv);
0216 }
0217 
0218 static int pucan_clr_options(struct peak_canfd_priv *priv, u16 opt_mask)
0219 {
0220     struct pucan_options *cmd;
0221 
0222     cmd = pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_CLR_DIS_OPTION);
0223 
0224     cmd->options = cpu_to_le16(opt_mask);
0225 
0226     return pucan_write_cmd(priv);
0227 }
0228 
0229 static int pucan_setup_rx_barrier(struct peak_canfd_priv *priv)
0230 {
0231     pucan_add_cmd(pucan_init_cmd(priv), PUCAN_CMD_RX_BARRIER);
0232 
0233     return pucan_write_cmd(priv);
0234 }
0235 
0236 static int pucan_netif_rx(struct sk_buff *skb, __le32 ts_low, __le32 ts_high)
0237 {
0238     struct skb_shared_hwtstamps *hwts = skb_hwtstamps(skb);
0239     u64 ts_us;
0240 
0241     ts_us = (u64)le32_to_cpu(ts_high) << 32;
0242     ts_us |= le32_to_cpu(ts_low);
0243 
0244     /* IP core timestamps are µs. */
0245     hwts->hwtstamp = ns_to_ktime(ts_us * NSEC_PER_USEC);
0246 
0247     return netif_rx(skb);
0248 }
0249 
0250 /* handle the reception of one CAN frame */
0251 static int pucan_handle_can_rx(struct peak_canfd_priv *priv,
0252                    struct pucan_rx_msg *msg)
0253 {
0254     struct net_device_stats *stats = &priv->ndev->stats;
0255     struct canfd_frame *cf;
0256     struct sk_buff *skb;
0257     const u16 rx_msg_flags = le16_to_cpu(msg->flags);
0258     u8 cf_len;
0259 
0260     if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN)
0261         cf_len = can_fd_dlc2len(pucan_msg_get_dlc(msg));
0262     else
0263         cf_len = can_cc_dlc2len(pucan_msg_get_dlc(msg));
0264 
0265     /* if this frame is an echo, */
0266     if (rx_msg_flags & PUCAN_MSG_LOOPED_BACK) {
0267         unsigned long flags;
0268 
0269         spin_lock_irqsave(&priv->echo_lock, flags);
0270 
0271         /* count bytes of the echo instead of skb */
0272         stats->tx_bytes += can_get_echo_skb(priv->ndev, msg->client, NULL);
0273         stats->tx_packets++;
0274 
0275         /* restart tx queue (a slot is free) */
0276         netif_wake_queue(priv->ndev);
0277 
0278         spin_unlock_irqrestore(&priv->echo_lock, flags);
0279 
0280         /* if this frame is only an echo, stop here. Otherwise,
0281          * continue to push this application self-received frame into
0282          * its own rx queue.
0283          */
0284         if (!(rx_msg_flags & PUCAN_MSG_SELF_RECEIVE))
0285             return 0;
0286     }
0287 
0288     /* otherwise, it should be pushed into rx fifo */
0289     if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
0290         /* CANFD frame case */
0291         skb = alloc_canfd_skb(priv->ndev, &cf);
0292         if (!skb)
0293             return -ENOMEM;
0294 
0295         if (rx_msg_flags & PUCAN_MSG_BITRATE_SWITCH)
0296             cf->flags |= CANFD_BRS;
0297 
0298         if (rx_msg_flags & PUCAN_MSG_ERROR_STATE_IND)
0299             cf->flags |= CANFD_ESI;
0300     } else {
0301         /* CAN 2.0 frame case */
0302         skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf);
0303         if (!skb)
0304             return -ENOMEM;
0305     }
0306 
0307     cf->can_id = le32_to_cpu(msg->can_id);
0308     cf->len = cf_len;
0309 
0310     if (rx_msg_flags & PUCAN_MSG_EXT_ID)
0311         cf->can_id |= CAN_EFF_FLAG;
0312 
0313     if (rx_msg_flags & PUCAN_MSG_RTR) {
0314         cf->can_id |= CAN_RTR_FLAG;
0315     } else {
0316         memcpy(cf->data, msg->d, cf->len);
0317 
0318         stats->rx_bytes += cf->len;
0319     }
0320     stats->rx_packets++;
0321 
0322     pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
0323 
0324     return 0;
0325 }
0326 
0327 /* handle rx/tx error counters notification */
0328 static int pucan_handle_error(struct peak_canfd_priv *priv,
0329                   struct pucan_error_msg *msg)
0330 {
0331     priv->bec.txerr = msg->tx_err_cnt;
0332     priv->bec.rxerr = msg->rx_err_cnt;
0333 
0334     return 0;
0335 }
0336 
0337 /* handle status notification */
0338 static int pucan_handle_status(struct peak_canfd_priv *priv,
0339                    struct pucan_status_msg *msg)
0340 {
0341     struct net_device *ndev = priv->ndev;
0342     struct net_device_stats *stats = &ndev->stats;
0343     struct can_frame *cf;
0344     struct sk_buff *skb;
0345 
0346     /* this STATUS is the CNF of the RX_BARRIER: Tx path can be setup */
0347     if (pucan_status_is_rx_barrier(msg)) {
0348         if (priv->enable_tx_path) {
0349             int err = priv->enable_tx_path(priv);
0350 
0351             if (err)
0352                 return err;
0353         }
0354 
0355         /* wake network queue up (echo_skb array is empty) */
0356         netif_wake_queue(ndev);
0357 
0358         return 0;
0359     }
0360 
0361     skb = alloc_can_err_skb(ndev, &cf);
0362 
0363     /* test state error bits according to their priority */
0364     if (pucan_status_is_busoff(msg)) {
0365         netdev_dbg(ndev, "Bus-off entry status\n");
0366         priv->can.state = CAN_STATE_BUS_OFF;
0367         priv->can.can_stats.bus_off++;
0368         can_bus_off(ndev);
0369         if (skb)
0370             cf->can_id |= CAN_ERR_BUSOFF;
0371 
0372     } else if (pucan_status_is_passive(msg)) {
0373         netdev_dbg(ndev, "Error passive status\n");
0374         priv->can.state = CAN_STATE_ERROR_PASSIVE;
0375         priv->can.can_stats.error_passive++;
0376         if (skb) {
0377             cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
0378             cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
0379                     CAN_ERR_CRTL_TX_PASSIVE :
0380                     CAN_ERR_CRTL_RX_PASSIVE;
0381             cf->data[6] = priv->bec.txerr;
0382             cf->data[7] = priv->bec.rxerr;
0383         }
0384 
0385     } else if (pucan_status_is_warning(msg)) {
0386         netdev_dbg(ndev, "Error warning status\n");
0387         priv->can.state = CAN_STATE_ERROR_WARNING;
0388         priv->can.can_stats.error_warning++;
0389         if (skb) {
0390             cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
0391             cf->data[1] = (priv->bec.txerr > priv->bec.rxerr) ?
0392                     CAN_ERR_CRTL_TX_WARNING :
0393                     CAN_ERR_CRTL_RX_WARNING;
0394             cf->data[6] = priv->bec.txerr;
0395             cf->data[7] = priv->bec.rxerr;
0396         }
0397 
0398     } else if (priv->can.state != CAN_STATE_ERROR_ACTIVE) {
0399         /* back to ERROR_ACTIVE */
0400         netdev_dbg(ndev, "Error active status\n");
0401         can_change_state(ndev, cf, CAN_STATE_ERROR_ACTIVE,
0402                  CAN_STATE_ERROR_ACTIVE);
0403     } else {
0404         dev_kfree_skb(skb);
0405         return 0;
0406     }
0407 
0408     if (!skb) {
0409         stats->rx_dropped++;
0410         return -ENOMEM;
0411     }
0412 
0413     pucan_netif_rx(skb, msg->ts_low, msg->ts_high);
0414 
0415     return 0;
0416 }
0417 
0418 /* handle uCAN Rx overflow notification */
0419 static int pucan_handle_cache_critical(struct peak_canfd_priv *priv)
0420 {
0421     struct net_device_stats *stats = &priv->ndev->stats;
0422     struct can_frame *cf;
0423     struct sk_buff *skb;
0424 
0425     stats->rx_over_errors++;
0426     stats->rx_errors++;
0427 
0428     skb = alloc_can_err_skb(priv->ndev, &cf);
0429     if (!skb) {
0430         stats->rx_dropped++;
0431         return -ENOMEM;
0432     }
0433 
0434     cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
0435     cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
0436 
0437     cf->data[6] = priv->bec.txerr;
0438     cf->data[7] = priv->bec.rxerr;
0439 
0440     netif_rx(skb);
0441 
0442     return 0;
0443 }
0444 
0445 /* handle a single uCAN message */
0446 int peak_canfd_handle_msg(struct peak_canfd_priv *priv,
0447               struct pucan_rx_msg *msg)
0448 {
0449     u16 msg_type = le16_to_cpu(msg->type);
0450     int msg_size = le16_to_cpu(msg->size);
0451     int err;
0452 
0453     if (!msg_size || !msg_type) {
0454         /* null packet found: end of list */
0455         goto exit;
0456     }
0457 
0458     switch (msg_type) {
0459     case PUCAN_MSG_CAN_RX:
0460         err = pucan_handle_can_rx(priv, (struct pucan_rx_msg *)msg);
0461         break;
0462     case PUCAN_MSG_ERROR:
0463         err = pucan_handle_error(priv, (struct pucan_error_msg *)msg);
0464         break;
0465     case PUCAN_MSG_STATUS:
0466         err = pucan_handle_status(priv, (struct pucan_status_msg *)msg);
0467         break;
0468     case PUCAN_MSG_CACHE_CRITICAL:
0469         err = pucan_handle_cache_critical(priv);
0470         break;
0471     default:
0472         err = 0;
0473     }
0474 
0475     if (err < 0)
0476         return err;
0477 
0478 exit:
0479     return msg_size;
0480 }
0481 
0482 /* handle a list of rx_count messages from rx_msg memory address */
0483 int peak_canfd_handle_msgs_list(struct peak_canfd_priv *priv,
0484                 struct pucan_rx_msg *msg_list, int msg_count)
0485 {
0486     void *msg_ptr = msg_list;
0487     int i, msg_size = 0;
0488 
0489     for (i = 0; i < msg_count; i++) {
0490         msg_size = peak_canfd_handle_msg(priv, msg_ptr);
0491 
0492         /* a null packet can be found at the end of a list */
0493         if (msg_size <= 0)
0494             break;
0495 
0496         msg_ptr += ALIGN(msg_size, 4);
0497     }
0498 
0499     if (msg_size < 0)
0500         return msg_size;
0501 
0502     return i;
0503 }
0504 
0505 static int peak_canfd_start(struct peak_canfd_priv *priv)
0506 {
0507     int err;
0508 
0509     err = pucan_clr_err_counters(priv);
0510     if (err)
0511         goto err_exit;
0512 
0513     priv->echo_idx = 0;
0514 
0515     priv->bec.txerr = 0;
0516     priv->bec.rxerr = 0;
0517 
0518     if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
0519         err = pucan_set_listen_only_mode(priv);
0520     else
0521         err = pucan_set_normal_mode(priv);
0522 
0523 err_exit:
0524     return err;
0525 }
0526 
0527 static void peak_canfd_stop(struct peak_canfd_priv *priv)
0528 {
0529     int err;
0530 
0531     /* go back to RESET mode */
0532     err = pucan_set_reset_mode(priv);
0533     if (err) {
0534         netdev_err(priv->ndev, "channel %u reset failed\n",
0535                priv->index);
0536     } else {
0537         /* abort last Tx (MUST be done in RESET mode only!) */
0538         pucan_tx_abort(priv, PUCAN_TX_ABORT_FLUSH);
0539     }
0540 }
0541 
0542 static int peak_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
0543 {
0544     struct peak_canfd_priv *priv = netdev_priv(ndev);
0545 
0546     switch (mode) {
0547     case CAN_MODE_START:
0548         peak_canfd_start(priv);
0549         netif_wake_queue(ndev);
0550         break;
0551     default:
0552         return -EOPNOTSUPP;
0553     }
0554 
0555     return 0;
0556 }
0557 
0558 static int peak_canfd_get_berr_counter(const struct net_device *ndev,
0559                        struct can_berr_counter *bec)
0560 {
0561     struct peak_canfd_priv *priv = netdev_priv(ndev);
0562 
0563     *bec = priv->bec;
0564     return 0;
0565 }
0566 
0567 static int peak_canfd_open(struct net_device *ndev)
0568 {
0569     struct peak_canfd_priv *priv = netdev_priv(ndev);
0570     int i, err = 0;
0571 
0572     err = open_candev(ndev);
0573     if (err) {
0574         netdev_err(ndev, "open_candev() failed, error %d\n", err);
0575         goto err_exit;
0576     }
0577 
0578     err = pucan_set_reset_mode(priv);
0579     if (err)
0580         goto err_close;
0581 
0582     if (priv->can.ctrlmode & CAN_CTRLMODE_FD) {
0583         if (priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
0584             err = pucan_clr_options(priv, PUCAN_OPTION_CANDFDISO);
0585         else
0586             err = pucan_set_options(priv, PUCAN_OPTION_CANDFDISO);
0587 
0588         if (err)
0589             goto err_close;
0590     }
0591 
0592     /* set option: get rx/tx error counters */
0593     err = pucan_set_options(priv, PUCAN_OPTION_ERROR);
0594     if (err)
0595         goto err_close;
0596 
0597     /* accept all standard CAN ID */
0598     for (i = 0; i <= PUCAN_FLTSTD_ROW_IDX_MAX; i++)
0599         pucan_set_std_filter(priv, i, 0xffffffff);
0600 
0601     err = peak_canfd_start(priv);
0602     if (err)
0603         goto err_close;
0604 
0605     /* receiving the RB status says when Tx path is ready */
0606     err = pucan_setup_rx_barrier(priv);
0607     if (!err)
0608         goto err_exit;
0609 
0610 err_close:
0611     close_candev(ndev);
0612 err_exit:
0613     return err;
0614 }
0615 
0616 static int peak_canfd_set_bittiming(struct net_device *ndev)
0617 {
0618     struct peak_canfd_priv *priv = netdev_priv(ndev);
0619 
0620     return pucan_set_timing_slow(priv, &priv->can.bittiming);
0621 }
0622 
0623 static int peak_canfd_set_data_bittiming(struct net_device *ndev)
0624 {
0625     struct peak_canfd_priv *priv = netdev_priv(ndev);
0626 
0627     return pucan_set_timing_fast(priv, &priv->can.data_bittiming);
0628 }
0629 
0630 static int peak_canfd_close(struct net_device *ndev)
0631 {
0632     struct peak_canfd_priv *priv = netdev_priv(ndev);
0633 
0634     netif_stop_queue(ndev);
0635     peak_canfd_stop(priv);
0636     close_candev(ndev);
0637 
0638     return 0;
0639 }
0640 
0641 static netdev_tx_t peak_canfd_start_xmit(struct sk_buff *skb,
0642                      struct net_device *ndev)
0643 {
0644     struct peak_canfd_priv *priv = netdev_priv(ndev);
0645     struct net_device_stats *stats = &ndev->stats;
0646     struct canfd_frame *cf = (struct canfd_frame *)skb->data;
0647     struct pucan_tx_msg *msg;
0648     u16 msg_size, msg_flags;
0649     unsigned long flags;
0650     bool should_stop_tx_queue;
0651     int room_left;
0652     u8 len;
0653 
0654     if (can_dropped_invalid_skb(ndev, skb))
0655         return NETDEV_TX_OK;
0656 
0657     msg_size = ALIGN(sizeof(*msg) + cf->len, 4);
0658     msg = priv->alloc_tx_msg(priv, msg_size, &room_left);
0659 
0660     /* should never happen except under bus-off condition and (auto-)restart
0661      * mechanism
0662      */
0663     if (!msg) {
0664         stats->tx_dropped++;
0665         netif_stop_queue(ndev);
0666         return NETDEV_TX_BUSY;
0667     }
0668 
0669     msg->size = cpu_to_le16(msg_size);
0670     msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
0671     msg_flags = 0;
0672 
0673     if (cf->can_id & CAN_EFF_FLAG) {
0674         msg_flags |= PUCAN_MSG_EXT_ID;
0675         msg->can_id = cpu_to_le32(cf->can_id & CAN_EFF_MASK);
0676     } else {
0677         msg->can_id = cpu_to_le32(cf->can_id & CAN_SFF_MASK);
0678     }
0679 
0680     if (can_is_canfd_skb(skb)) {
0681         /* CAN FD frame format */
0682         len = can_fd_len2dlc(cf->len);
0683 
0684         msg_flags |= PUCAN_MSG_EXT_DATA_LEN;
0685 
0686         if (cf->flags & CANFD_BRS)
0687             msg_flags |= PUCAN_MSG_BITRATE_SWITCH;
0688 
0689         if (cf->flags & CANFD_ESI)
0690             msg_flags |= PUCAN_MSG_ERROR_STATE_IND;
0691     } else {
0692         /* CAN 2.0 frame format */
0693         len = cf->len;
0694 
0695         if (cf->can_id & CAN_RTR_FLAG)
0696             msg_flags |= PUCAN_MSG_RTR;
0697     }
0698 
0699     /* always ask loopback for echo management */
0700     msg_flags |= PUCAN_MSG_LOOPED_BACK;
0701 
0702     /* set driver specific bit to differentiate with application loopback */
0703     if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
0704         msg_flags |= PUCAN_MSG_SELF_RECEIVE;
0705 
0706     msg->flags = cpu_to_le16(msg_flags);
0707     msg->channel_dlc = PUCAN_MSG_CHANNEL_DLC(priv->index, len);
0708     memcpy(msg->d, cf->data, cf->len);
0709 
0710     /* struct msg client field is used as an index in the echo skbs ring */
0711     msg->client = priv->echo_idx;
0712 
0713     spin_lock_irqsave(&priv->echo_lock, flags);
0714 
0715     /* prepare and save echo skb in internal slot */
0716     can_put_echo_skb(skb, ndev, priv->echo_idx, 0);
0717 
0718     /* move echo index to the next slot */
0719     priv->echo_idx = (priv->echo_idx + 1) % priv->can.echo_skb_max;
0720 
0721     /* if next slot is not free, stop network queue (no slot free in echo
0722      * skb ring means that the controller did not write these frames on
0723      * the bus: no need to continue).
0724      */
0725     should_stop_tx_queue = !!(priv->can.echo_skb[priv->echo_idx]);
0726 
0727     /* stop network tx queue if not enough room to save one more msg too */
0728     if (priv->can.ctrlmode & CAN_CTRLMODE_FD)
0729         should_stop_tx_queue |= (room_left <
0730                     (sizeof(*msg) + CANFD_MAX_DLEN));
0731     else
0732         should_stop_tx_queue |= (room_left <
0733                     (sizeof(*msg) + CAN_MAX_DLEN));
0734 
0735     if (should_stop_tx_queue)
0736         netif_stop_queue(ndev);
0737 
0738     spin_unlock_irqrestore(&priv->echo_lock, flags);
0739 
0740     /* write the skb on the interface */
0741     priv->write_tx_msg(priv, msg);
0742 
0743     return NETDEV_TX_OK;
0744 }
0745 
0746 static int peak_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
0747 {
0748     struct hwtstamp_config hwts_cfg = { 0 };
0749 
0750     switch (cmd) {
0751     case SIOCSHWTSTAMP: /* set */
0752         if (copy_from_user(&hwts_cfg, ifr->ifr_data, sizeof(hwts_cfg)))
0753             return -EFAULT;
0754         if (hwts_cfg.tx_type == HWTSTAMP_TX_OFF &&
0755             hwts_cfg.rx_filter == HWTSTAMP_FILTER_ALL)
0756             return 0;
0757         return -ERANGE;
0758 
0759     case SIOCGHWTSTAMP: /* get */
0760         hwts_cfg.tx_type = HWTSTAMP_TX_OFF;
0761         hwts_cfg.rx_filter = HWTSTAMP_FILTER_ALL;
0762         if (copy_to_user(ifr->ifr_data, &hwts_cfg, sizeof(hwts_cfg)))
0763             return -EFAULT;
0764         return 0;
0765 
0766     default:
0767         return -EOPNOTSUPP;
0768     }
0769 }
0770 
0771 static const struct net_device_ops peak_canfd_netdev_ops = {
0772     .ndo_open = peak_canfd_open,
0773     .ndo_stop = peak_canfd_close,
0774     .ndo_eth_ioctl = peak_eth_ioctl,
0775     .ndo_start_xmit = peak_canfd_start_xmit,
0776     .ndo_change_mtu = can_change_mtu,
0777 };
0778 
0779 static int peak_get_ts_info(struct net_device *dev,
0780                 struct ethtool_ts_info *info)
0781 {
0782     info->so_timestamping =
0783         SOF_TIMESTAMPING_TX_SOFTWARE |
0784         SOF_TIMESTAMPING_RX_SOFTWARE |
0785         SOF_TIMESTAMPING_SOFTWARE |
0786         SOF_TIMESTAMPING_RX_HARDWARE |
0787         SOF_TIMESTAMPING_RAW_HARDWARE;
0788     info->phc_index = -1;
0789     info->tx_types = BIT(HWTSTAMP_TX_OFF);
0790     info->rx_filters = BIT(HWTSTAMP_FILTER_ALL);
0791 
0792     return 0;
0793 }
0794 
0795 static const struct ethtool_ops peak_canfd_ethtool_ops = {
0796     .get_ts_info = peak_get_ts_info,
0797 };
0798 
0799 struct net_device *alloc_peak_canfd_dev(int sizeof_priv, int index,
0800                     int echo_skb_max)
0801 {
0802     struct net_device *ndev;
0803     struct peak_canfd_priv *priv;
0804 
0805     /* we DO support local echo */
0806     if (echo_skb_max < 0)
0807         echo_skb_max = PCANFD_ECHO_SKB_MAX;
0808 
0809     /* allocate the candev object */
0810     ndev = alloc_candev(sizeof_priv, echo_skb_max);
0811     if (!ndev)
0812         return NULL;
0813 
0814     priv = netdev_priv(ndev);
0815 
0816     /* complete now socket-can initialization side */
0817     priv->can.state = CAN_STATE_STOPPED;
0818     priv->can.bittiming_const = &peak_canfd_nominal_const;
0819     priv->can.data_bittiming_const = &peak_canfd_data_const;
0820 
0821     priv->can.do_set_mode = peak_canfd_set_mode;
0822     priv->can.do_get_berr_counter = peak_canfd_get_berr_counter;
0823     priv->can.do_set_bittiming = peak_canfd_set_bittiming;
0824     priv->can.do_set_data_bittiming = peak_canfd_set_data_bittiming;
0825     priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
0826                        CAN_CTRLMODE_LISTENONLY |
0827                        CAN_CTRLMODE_3_SAMPLES |
0828                        CAN_CTRLMODE_FD |
0829                        CAN_CTRLMODE_FD_NON_ISO |
0830                        CAN_CTRLMODE_BERR_REPORTING;
0831 
0832     priv->ndev = ndev;
0833     priv->index = index;
0834     priv->cmd_len = 0;
0835     spin_lock_init(&priv->echo_lock);
0836 
0837     ndev->flags |= IFF_ECHO;
0838     ndev->netdev_ops = &peak_canfd_netdev_ops;
0839     ndev->ethtool_ops = &peak_canfd_ethtool_ops;
0840     ndev->dev_id = index;
0841 
0842     return ndev;
0843 }