aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/can/usb/kvaser_usb.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/can/usb/kvaser_usb.c')
-rw-r--r--drivers/net/can/usb/kvaser_usb.c176
1 files changed, 112 insertions, 64 deletions
diff --git a/drivers/net/can/usb/kvaser_usb.c b/drivers/net/can/usb/kvaser_usb.c
index 2928f7003041..57611fd91229 100644
--- a/drivers/net/can/usb/kvaser_usb.c
+++ b/drivers/net/can/usb/kvaser_usb.c
@@ -14,6 +14,8 @@
14 * Copyright (C) 2015 Valeo S.A. 14 * Copyright (C) 2015 Valeo S.A.
15 */ 15 */
16 16
17#include <linux/spinlock.h>
18#include <linux/kernel.h>
17#include <linux/completion.h> 19#include <linux/completion.h>
18#include <linux/module.h> 20#include <linux/module.h>
19#include <linux/netdevice.h> 21#include <linux/netdevice.h>
@@ -23,7 +25,6 @@
23#include <linux/can/dev.h> 25#include <linux/can/dev.h>
24#include <linux/can/error.h> 26#include <linux/can/error.h>
25 27
26#define MAX_TX_URBS 16
27#define MAX_RX_URBS 4 28#define MAX_RX_URBS 4
28#define START_TIMEOUT 1000 /* msecs */ 29#define START_TIMEOUT 1000 /* msecs */
29#define STOP_TIMEOUT 1000 /* msecs */ 30#define STOP_TIMEOUT 1000 /* msecs */
@@ -441,6 +442,7 @@ struct kvaser_usb_error_summary {
441 }; 442 };
442}; 443};
443 444
445/* Context for an outstanding, not yet ACKed, transmission */
444struct kvaser_usb_tx_urb_context { 446struct kvaser_usb_tx_urb_context {
445 struct kvaser_usb_net_priv *priv; 447 struct kvaser_usb_net_priv *priv;
446 u32 echo_index; 448 u32 echo_index;
@@ -454,8 +456,13 @@ struct kvaser_usb {
454 struct usb_endpoint_descriptor *bulk_in, *bulk_out; 456 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
455 struct usb_anchor rx_submitted; 457 struct usb_anchor rx_submitted;
456 458
459 /* @max_tx_urbs: Firmware-reported maximum number of oustanding,
460 * not yet ACKed, transmissions on this device. This value is
461 * also used as a sentinel for marking free tx contexts.
462 */
457 u32 fw_version; 463 u32 fw_version;
458 unsigned int nchannels; 464 unsigned int nchannels;
465 unsigned int max_tx_urbs;
459 enum kvaser_usb_family family; 466 enum kvaser_usb_family family;
460 467
461 bool rxinitdone; 468 bool rxinitdone;
@@ -465,18 +472,18 @@ struct kvaser_usb {
465 472
466struct kvaser_usb_net_priv { 473struct kvaser_usb_net_priv {
467 struct can_priv can; 474 struct can_priv can;
468 475 struct can_berr_counter bec;
469 atomic_t active_tx_urbs;
470 struct usb_anchor tx_submitted;
471 struct kvaser_usb_tx_urb_context tx_contexts[MAX_TX_URBS];
472
473 struct completion start_comp, stop_comp;
474 476
475 struct kvaser_usb *dev; 477 struct kvaser_usb *dev;
476 struct net_device *netdev; 478 struct net_device *netdev;
477 int channel; 479 int channel;
478 480
479 struct can_berr_counter bec; 481 struct completion start_comp, stop_comp;
482 struct usb_anchor tx_submitted;
483
484 spinlock_t tx_contexts_lock;
485 int active_tx_contexts;
486 struct kvaser_usb_tx_urb_context tx_contexts[];
480}; 487};
481 488
482static const struct usb_device_id kvaser_usb_table[] = { 489static const struct usb_device_id kvaser_usb_table[] = {
@@ -584,8 +591,15 @@ static int kvaser_usb_wait_msg(const struct kvaser_usb *dev, u8 id,
584 while (pos <= actual_len - MSG_HEADER_LEN) { 591 while (pos <= actual_len - MSG_HEADER_LEN) {
585 tmp = buf + pos; 592 tmp = buf + pos;
586 593
587 if (!tmp->len) 594 /* Handle messages crossing the USB endpoint max packet
588 break; 595 * size boundary. Check kvaser_usb_read_bulk_callback()
596 * for further details.
597 */
598 if (tmp->len == 0) {
599 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
600 wMaxPacketSize));
601 continue;
602 }
589 603
590 if (pos + tmp->len > actual_len) { 604 if (pos + tmp->len > actual_len) {
591 dev_err(dev->udev->dev.parent, 605 dev_err(dev->udev->dev.parent,
@@ -647,9 +661,13 @@ static int kvaser_usb_get_software_info(struct kvaser_usb *dev)
647 switch (dev->family) { 661 switch (dev->family) {
648 case KVASER_LEAF: 662 case KVASER_LEAF:
649 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version); 663 dev->fw_version = le32_to_cpu(msg.u.leaf.softinfo.fw_version);
664 dev->max_tx_urbs =
665 le16_to_cpu(msg.u.leaf.softinfo.max_outstanding_tx);
650 break; 666 break;
651 case KVASER_USBCAN: 667 case KVASER_USBCAN:
652 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version); 668 dev->fw_version = le32_to_cpu(msg.u.usbcan.softinfo.fw_version);
669 dev->max_tx_urbs =
670 le16_to_cpu(msg.u.usbcan.softinfo.max_outstanding_tx);
653 break; 671 break;
654 } 672 }
655 673
@@ -686,6 +704,7 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
686 struct kvaser_usb_net_priv *priv; 704 struct kvaser_usb_net_priv *priv;
687 struct sk_buff *skb; 705 struct sk_buff *skb;
688 struct can_frame *cf; 706 struct can_frame *cf;
707 unsigned long flags;
689 u8 channel, tid; 708 u8 channel, tid;
690 709
691 channel = msg->u.tx_acknowledge_header.channel; 710 channel = msg->u.tx_acknowledge_header.channel;
@@ -704,7 +723,7 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
704 723
705 stats = &priv->netdev->stats; 724 stats = &priv->netdev->stats;
706 725
707 context = &priv->tx_contexts[tid % MAX_TX_URBS]; 726 context = &priv->tx_contexts[tid % dev->max_tx_urbs];
708 727
709 /* Sometimes the state change doesn't come after a bus-off event */ 728 /* Sometimes the state change doesn't come after a bus-off event */
710 if (priv->can.restart_ms && 729 if (priv->can.restart_ms &&
@@ -729,12 +748,15 @@ static void kvaser_usb_tx_acknowledge(const struct kvaser_usb *dev,
729 748
730 stats->tx_packets++; 749 stats->tx_packets++;
731 stats->tx_bytes += context->dlc; 750 stats->tx_bytes += context->dlc;
732 can_get_echo_skb(priv->netdev, context->echo_index);
733 751
734 context->echo_index = MAX_TX_URBS; 752 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
735 atomic_dec(&priv->active_tx_urbs);
736 753
754 can_get_echo_skb(priv->netdev, context->echo_index);
755 context->echo_index = dev->max_tx_urbs;
756 --priv->active_tx_contexts;
737 netif_wake_queue(priv->netdev); 757 netif_wake_queue(priv->netdev);
758
759 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
738} 760}
739 761
740static void kvaser_usb_simple_msg_callback(struct urb *urb) 762static void kvaser_usb_simple_msg_callback(struct urb *urb)
@@ -787,7 +809,6 @@ static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
787 netdev_err(netdev, "Error transmitting URB\n"); 809 netdev_err(netdev, "Error transmitting URB\n");
788 usb_unanchor_urb(urb); 810 usb_unanchor_urb(urb);
789 usb_free_urb(urb); 811 usb_free_urb(urb);
790 kfree(buf);
791 return err; 812 return err;
792 } 813 }
793 814
@@ -796,17 +817,6 @@ static int kvaser_usb_simple_msg_async(struct kvaser_usb_net_priv *priv,
796 return 0; 817 return 0;
797} 818}
798 819
799static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
800{
801 int i;
802
803 usb_kill_anchored_urbs(&priv->tx_submitted);
804 atomic_set(&priv->active_tx_urbs, 0);
805
806 for (i = 0; i < MAX_TX_URBS; i++)
807 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
808}
809
810static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv, 820static void kvaser_usb_rx_error_update_can_state(struct kvaser_usb_net_priv *priv,
811 const struct kvaser_usb_error_summary *es, 821 const struct kvaser_usb_error_summary *es,
812 struct can_frame *cf) 822 struct can_frame *cf)
@@ -1317,8 +1327,20 @@ static void kvaser_usb_read_bulk_callback(struct urb *urb)
1317 while (pos <= urb->actual_length - MSG_HEADER_LEN) { 1327 while (pos <= urb->actual_length - MSG_HEADER_LEN) {
1318 msg = urb->transfer_buffer + pos; 1328 msg = urb->transfer_buffer + pos;
1319 1329
1320 if (!msg->len) 1330 /* The Kvaser firmware can only read and write messages that
1321 break; 1331 * does not cross the USB's endpoint wMaxPacketSize boundary.
1332 * If a follow-up command crosses such boundary, firmware puts
1333 * a placeholder zero-length command in its place then aligns
1334 * the real command to the next max packet size.
1335 *
1336 * Handle such cases or we're going to miss a significant
1337 * number of events in case of a heavy rx load on the bus.
1338 */
1339 if (msg->len == 0) {
1340 pos = round_up(pos, le16_to_cpu(dev->bulk_in->
1341 wMaxPacketSize));
1342 continue;
1343 }
1322 1344
1323 if (pos + msg->len > urb->actual_length) { 1345 if (pos + msg->len > urb->actual_length) {
1324 dev_err(dev->udev->dev.parent, "Format error\n"); 1346 dev_err(dev->udev->dev.parent, "Format error\n");
@@ -1326,7 +1348,6 @@ static void kvaser_usb_read_bulk_callback(struct urb *urb)
1326 } 1348 }
1327 1349
1328 kvaser_usb_handle_message(dev, msg); 1350 kvaser_usb_handle_message(dev, msg);
1329
1330 pos += msg->len; 1351 pos += msg->len;
1331 } 1352 }
1332 1353
@@ -1498,6 +1519,26 @@ error:
1498 return err; 1519 return err;
1499} 1520}
1500 1521
1522static void kvaser_usb_reset_tx_urb_contexts(struct kvaser_usb_net_priv *priv)
1523{
1524 int i, max_tx_urbs;
1525
1526 max_tx_urbs = priv->dev->max_tx_urbs;
1527
1528 priv->active_tx_contexts = 0;
1529 for (i = 0; i < max_tx_urbs; i++)
1530 priv->tx_contexts[i].echo_index = max_tx_urbs;
1531}
1532
1533/* This method might sleep. Do not call it in the atomic context
1534 * of URB completions.
1535 */
1536static void kvaser_usb_unlink_tx_urbs(struct kvaser_usb_net_priv *priv)
1537{
1538 usb_kill_anchored_urbs(&priv->tx_submitted);
1539 kvaser_usb_reset_tx_urb_contexts(priv);
1540}
1541
1501static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev) 1542static void kvaser_usb_unlink_all_urbs(struct kvaser_usb *dev)
1502{ 1543{
1503 int i; 1544 int i;
@@ -1615,9 +1656,9 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1615 struct urb *urb; 1656 struct urb *urb;
1616 void *buf; 1657 void *buf;
1617 struct kvaser_msg *msg; 1658 struct kvaser_msg *msg;
1618 int i, err; 1659 int i, err, ret = NETDEV_TX_OK;
1619 int ret = NETDEV_TX_OK;
1620 u8 *msg_tx_can_flags = NULL; /* GCC */ 1660 u8 *msg_tx_can_flags = NULL; /* GCC */
1661 unsigned long flags;
1621 1662
1622 if (can_dropped_invalid_skb(netdev, skb)) 1663 if (can_dropped_invalid_skb(netdev, skb))
1623 return NETDEV_TX_OK; 1664 return NETDEV_TX_OK;
@@ -1634,7 +1675,7 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1634 if (!buf) { 1675 if (!buf) {
1635 stats->tx_dropped++; 1676 stats->tx_dropped++;
1636 dev_kfree_skb(skb); 1677 dev_kfree_skb(skb);
1637 goto nobufmem; 1678 goto freeurb;
1638 } 1679 }
1639 1680
1640 msg = buf; 1681 msg = buf;
@@ -1671,22 +1712,32 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1671 if (cf->can_id & CAN_RTR_FLAG) 1712 if (cf->can_id & CAN_RTR_FLAG)
1672 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME; 1713 *msg_tx_can_flags |= MSG_FLAG_REMOTE_FRAME;
1673 1714
1674 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++) { 1715 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1675 if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { 1716 for (i = 0; i < dev->max_tx_urbs; i++) {
1717 if (priv->tx_contexts[i].echo_index == dev->max_tx_urbs) {
1676 context = &priv->tx_contexts[i]; 1718 context = &priv->tx_contexts[i];
1719
1720 context->echo_index = i;
1721 can_put_echo_skb(skb, netdev, context->echo_index);
1722 ++priv->active_tx_contexts;
1723 if (priv->active_tx_contexts >= dev->max_tx_urbs)
1724 netif_stop_queue(netdev);
1725
1677 break; 1726 break;
1678 } 1727 }
1679 } 1728 }
1729 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1680 1730
1681 /* This should never happen; it implies a flow control bug */ 1731 /* This should never happen; it implies a flow control bug */
1682 if (!context) { 1732 if (!context) {
1683 netdev_warn(netdev, "cannot find free context\n"); 1733 netdev_warn(netdev, "cannot find free context\n");
1734
1735 kfree(buf);
1684 ret = NETDEV_TX_BUSY; 1736 ret = NETDEV_TX_BUSY;
1685 goto releasebuf; 1737 goto freeurb;
1686 } 1738 }
1687 1739
1688 context->priv = priv; 1740 context->priv = priv;
1689 context->echo_index = i;
1690 context->dlc = cf->can_dlc; 1741 context->dlc = cf->can_dlc;
1691 1742
1692 msg->u.tx_can.tid = context->echo_index; 1743 msg->u.tx_can.tid = context->echo_index;
@@ -1698,18 +1749,17 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1698 kvaser_usb_write_bulk_callback, context); 1749 kvaser_usb_write_bulk_callback, context);
1699 usb_anchor_urb(urb, &priv->tx_submitted); 1750 usb_anchor_urb(urb, &priv->tx_submitted);
1700 1751
1701 can_put_echo_skb(skb, netdev, context->echo_index);
1702
1703 atomic_inc(&priv->active_tx_urbs);
1704
1705 if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS)
1706 netif_stop_queue(netdev);
1707
1708 err = usb_submit_urb(urb, GFP_ATOMIC); 1752 err = usb_submit_urb(urb, GFP_ATOMIC);
1709 if (unlikely(err)) { 1753 if (unlikely(err)) {
1754 spin_lock_irqsave(&priv->tx_contexts_lock, flags);
1755
1710 can_free_echo_skb(netdev, context->echo_index); 1756 can_free_echo_skb(netdev, context->echo_index);
1757 context->echo_index = dev->max_tx_urbs;
1758 --priv->active_tx_contexts;
1759 netif_wake_queue(netdev);
1760
1761 spin_unlock_irqrestore(&priv->tx_contexts_lock, flags);
1711 1762
1712 atomic_dec(&priv->active_tx_urbs);
1713 usb_unanchor_urb(urb); 1763 usb_unanchor_urb(urb);
1714 1764
1715 stats->tx_dropped++; 1765 stats->tx_dropped++;
@@ -1719,16 +1769,12 @@ static netdev_tx_t kvaser_usb_start_xmit(struct sk_buff *skb,
1719 else 1769 else
1720 netdev_warn(netdev, "Failed tx_urb %d\n", err); 1770 netdev_warn(netdev, "Failed tx_urb %d\n", err);
1721 1771
1722 goto releasebuf; 1772 goto freeurb;
1723 } 1773 }
1724 1774
1725 usb_free_urb(urb); 1775 ret = NETDEV_TX_OK;
1726
1727 return NETDEV_TX_OK;
1728 1776
1729releasebuf: 1777freeurb:
1730 kfree(buf);
1731nobufmem:
1732 usb_free_urb(urb); 1778 usb_free_urb(urb);
1733 return ret; 1779 return ret;
1734} 1780}
@@ -1840,13 +1886,15 @@ static int kvaser_usb_init_one(struct usb_interface *intf,
1840 struct kvaser_usb *dev = usb_get_intfdata(intf); 1886 struct kvaser_usb *dev = usb_get_intfdata(intf);
1841 struct net_device *netdev; 1887 struct net_device *netdev;
1842 struct kvaser_usb_net_priv *priv; 1888 struct kvaser_usb_net_priv *priv;
1843 int i, err; 1889 int err;
1844 1890
1845 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel); 1891 err = kvaser_usb_send_simple_msg(dev, CMD_RESET_CHIP, channel);
1846 if (err) 1892 if (err)
1847 return err; 1893 return err;
1848 1894
1849 netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS); 1895 netdev = alloc_candev(sizeof(*priv) +
1896 dev->max_tx_urbs * sizeof(*priv->tx_contexts),
1897 dev->max_tx_urbs);
1850 if (!netdev) { 1898 if (!netdev) {
1851 dev_err(&intf->dev, "Cannot alloc candev\n"); 1899 dev_err(&intf->dev, "Cannot alloc candev\n");
1852 return -ENOMEM; 1900 return -ENOMEM;
@@ -1854,19 +1902,17 @@ static int kvaser_usb_init_one(struct usb_interface *intf,
1854 1902
1855 priv = netdev_priv(netdev); 1903 priv = netdev_priv(netdev);
1856 1904
1905 init_usb_anchor(&priv->tx_submitted);
1857 init_completion(&priv->start_comp); 1906 init_completion(&priv->start_comp);
1858 init_completion(&priv->stop_comp); 1907 init_completion(&priv->stop_comp);
1859 1908
1860 init_usb_anchor(&priv->tx_submitted);
1861 atomic_set(&priv->active_tx_urbs, 0);
1862
1863 for (i = 0; i < ARRAY_SIZE(priv->tx_contexts); i++)
1864 priv->tx_contexts[i].echo_index = MAX_TX_URBS;
1865
1866 priv->dev = dev; 1909 priv->dev = dev;
1867 priv->netdev = netdev; 1910 priv->netdev = netdev;
1868 priv->channel = channel; 1911 priv->channel = channel;
1869 1912
1913 spin_lock_init(&priv->tx_contexts_lock);
1914 kvaser_usb_reset_tx_urb_contexts(priv);
1915
1870 priv->can.state = CAN_STATE_STOPPED; 1916 priv->can.state = CAN_STATE_STOPPED;
1871 priv->can.clock.freq = CAN_USB_CLOCK; 1917 priv->can.clock.freq = CAN_USB_CLOCK;
1872 priv->can.bittiming_const = &kvaser_usb_bittiming_const; 1918 priv->can.bittiming_const = &kvaser_usb_bittiming_const;
@@ -1976,6 +2022,13 @@ static int kvaser_usb_probe(struct usb_interface *intf,
1976 return err; 2022 return err;
1977 } 2023 }
1978 2024
2025 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
2026 ((dev->fw_version >> 24) & 0xff),
2027 ((dev->fw_version >> 16) & 0xff),
2028 (dev->fw_version & 0xffff));
2029
2030 dev_dbg(&intf->dev, "Max oustanding tx = %d URBs\n", dev->max_tx_urbs);
2031
1979 err = kvaser_usb_get_card_info(dev); 2032 err = kvaser_usb_get_card_info(dev);
1980 if (err) { 2033 if (err) {
1981 dev_err(&intf->dev, 2034 dev_err(&intf->dev,
@@ -1983,11 +2036,6 @@ static int kvaser_usb_probe(struct usb_interface *intf,
1983 return err; 2036 return err;
1984 } 2037 }
1985 2038
1986 dev_dbg(&intf->dev, "Firmware version: %d.%d.%d\n",
1987 ((dev->fw_version >> 24) & 0xff),
1988 ((dev->fw_version >> 16) & 0xff),
1989 (dev->fw_version & 0xffff));
1990
1991 for (i = 0; i < dev->nchannels; i++) { 2039 for (i = 0; i < dev->nchannels; i++) {
1992 err = kvaser_usb_init_one(intf, id, i); 2040 err = kvaser_usb_init_one(intf, id, i);
1993 if (err) { 2041 if (err) {