Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'via-rhine-rework'

Francois Romieu says:

====================
via-rhine rework

The series applies against davem-next as of
9dd3c797496affd699805c8a9d8429ad318c892f ("drivers: net: xgene: fix kbuild
warnings").

Patches #1..#4 avoid holes in the receive ring.

Patch #5 is a small leftover cleanup for #1..#4.

Patches #6 and #7 are fairly simple barrier stuff.

Patch #8 closes some SMP transmit races - not that anyone really
complained about these but it's a bit hard to handwave that they
can be safely ignored. Some testing, especially SMP testing of
course, would be welcome.

. Changes since #2:
- added dma_rmb barrier in vlan related patch 6.
- s/wmb/dma_wmb/ in (*new*) patch 7 of 8.
- added explicit SMP barriers in (*new*) patch 8 of 8.

. Changes since #1:
- turned wmb() into dma_wmb() as suggested by davem and Alexander Duyck
in patch 1 of 6.
- forgot to reset rx_head_desc in rhine_reset_rbufs in patch 4 of 6.
- removed rx_head_desc altogether in (*new*) patch 5 of 6
- remoed some vlan receive uglyness in (*new*) patch 6 of 6.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+162 -87
+162 -87
drivers/net/ethernet/via/via-rhine.c
··· 472 472 473 473 /* Frequently used values: keep some adjacent for cache effect. */ 474 474 u32 quirks; 475 - struct rx_desc *rx_head_desc; 476 - unsigned int cur_rx, dirty_rx; /* Producer/consumer ring indices */ 475 + unsigned int cur_rx; 477 476 unsigned int cur_tx, dirty_tx; 478 477 unsigned int rx_buf_sz; /* Based on MTU+slack. */ 479 478 struct rhine_stats rx_stats; ··· 1212 1213 1213 1214 } 1214 1215 1215 - static void alloc_rbufs(struct net_device *dev) 1216 + struct rhine_skb_dma { 1217 + struct sk_buff *skb; 1218 + dma_addr_t dma; 1219 + }; 1220 + 1221 + static inline int rhine_skb_dma_init(struct net_device *dev, 1222 + struct rhine_skb_dma *sd) 1216 1223 { 1217 1224 struct rhine_private *rp = netdev_priv(dev); 1218 1225 struct device *hwdev = dev->dev.parent; 1219 - dma_addr_t next; 1226 + const int size = rp->rx_buf_sz; 1227 + 1228 + sd->skb = netdev_alloc_skb(dev, size); 1229 + if (!sd->skb) 1230 + return -ENOMEM; 1231 + 1232 + sd->dma = dma_map_single(hwdev, sd->skb->data, size, DMA_FROM_DEVICE); 1233 + if (unlikely(dma_mapping_error(hwdev, sd->dma))) { 1234 + netif_err(rp, drv, dev, "Rx DMA mapping failure\n"); 1235 + dev_kfree_skb_any(sd->skb); 1236 + return -EIO; 1237 + } 1238 + 1239 + return 0; 1240 + } 1241 + 1242 + static void rhine_reset_rbufs(struct rhine_private *rp) 1243 + { 1220 1244 int i; 1221 1245 1222 - rp->dirty_rx = rp->cur_rx = 0; 1246 + rp->cur_rx = 0; 1247 + 1248 + for (i = 0; i < RX_RING_SIZE; i++) 1249 + rp->rx_ring[i].rx_status = cpu_to_le32(DescOwn); 1250 + } 1251 + 1252 + static inline void rhine_skb_dma_nic_store(struct rhine_private *rp, 1253 + struct rhine_skb_dma *sd, int entry) 1254 + { 1255 + rp->rx_skbuff_dma[entry] = sd->dma; 1256 + rp->rx_skbuff[entry] = sd->skb; 1257 + 1258 + rp->rx_ring[entry].addr = cpu_to_le32(sd->dma); 1259 + dma_wmb(); 1260 + } 1261 + 1262 + static void free_rbufs(struct net_device* dev); 1263 + 1264 + static int alloc_rbufs(struct net_device *dev) 1265 + { 1266 + struct rhine_private *rp = netdev_priv(dev); 1267 + dma_addr_t next; 1268 + int rc, i; 1223 1269 1224 1270 rp->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32); 1225 - rp->rx_head_desc = &rp->rx_ring[0]; 1226 1271 next = rp->rx_ring_dma; 1227 1272 1228 1273 /* Init the ring entries */ ··· 1282 1239 1283 1240 /* Fill in the Rx buffers. Handle allocation failure gracefully. */ 1284 1241 for (i = 0; i < RX_RING_SIZE; i++) { 1285 - struct sk_buff *skb = netdev_alloc_skb(dev, rp->rx_buf_sz); 1286 - rp->rx_skbuff[i] = skb; 1287 - if (skb == NULL) 1288 - break; 1242 + struct rhine_skb_dma sd; 1289 1243 1290 - rp->rx_skbuff_dma[i] = 1291 - dma_map_single(hwdev, skb->data, rp->rx_buf_sz, 1292 - DMA_FROM_DEVICE); 1293 - if (dma_mapping_error(hwdev, rp->rx_skbuff_dma[i])) { 1294 - rp->rx_skbuff_dma[i] = 0; 1295 - dev_kfree_skb(skb); 1296 - break; 1244 + rc = rhine_skb_dma_init(dev, &sd); 1245 + if (rc < 0) { 1246 + free_rbufs(dev); 1247 + goto out; 1297 1248 } 1298 - rp->rx_ring[i].addr = cpu_to_le32(rp->rx_skbuff_dma[i]); 1299 - rp->rx_ring[i].rx_status = cpu_to_le32(DescOwn); 1249 + 1250 + rhine_skb_dma_nic_store(rp, &sd, i); 1300 1251 } 1301 - rp->dirty_rx = (unsigned int)(i - RX_RING_SIZE); 1252 + 1253 + rhine_reset_rbufs(rp); 1254 + out: 1255 + return rc; 1302 1256 } 1303 1257 1304 1258 static void free_rbufs(struct net_device* dev) ··· 1699 1659 1700 1660 rc = request_irq(rp->irq, rhine_interrupt, IRQF_SHARED, dev->name, dev); 1701 1661 if (rc) 1702 - return rc; 1662 + goto out; 1703 1663 1704 1664 netif_dbg(rp, ifup, dev, "%s() irq %d\n", __func__, rp->irq); 1705 1665 1706 1666 rc = alloc_ring(dev); 1707 - if (rc) { 1708 - free_irq(rp->irq, dev); 1709 - return rc; 1710 - } 1711 - alloc_rbufs(dev); 1667 + if (rc < 0) 1668 + goto out_free_irq; 1669 + 1670 + rc = alloc_rbufs(dev); 1671 + if (rc < 0) 1672 + goto out_free_ring; 1673 + 1712 1674 alloc_tbufs(dev); 1713 1675 rhine_chip_reset(dev); 1714 1676 rhine_task_enable(rp); ··· 1722 1680 1723 1681 netif_start_queue(dev); 1724 1682 1725 - return 0; 1683 + out: 1684 + return rc; 1685 + 1686 + out_free_ring: 1687 + free_ring(dev); 1688 + out_free_irq: 1689 + free_irq(rp->irq, dev); 1690 + goto out; 1726 1691 } 1727 1692 1728 1693 static void rhine_reset_task(struct work_struct *work) ··· 1749 1700 1750 1701 /* clear all descriptors */ 1751 1702 free_tbufs(dev); 1752 - free_rbufs(dev); 1753 1703 alloc_tbufs(dev); 1754 - alloc_rbufs(dev); 1704 + 1705 + rhine_reset_rbufs(rp); 1755 1706 1756 1707 /* Reinitialize the hardware. */ 1757 1708 rhine_chip_reset(dev); ··· 1777 1728 mdio_read(dev, rp->mii_if.phy_id, MII_BMSR)); 1778 1729 1779 1730 schedule_work(&rp->reset_task); 1731 + } 1732 + 1733 + static inline bool rhine_tx_queue_full(struct rhine_private *rp) 1734 + { 1735 + return (rp->cur_tx - rp->dirty_tx) >= TX_QUEUE_LEN; 1780 1736 } 1781 1737 1782 1738 static netdev_tx_t rhine_start_tx(struct sk_buff *skb, ··· 1854 1800 1855 1801 netdev_sent_queue(dev, skb->len); 1856 1802 /* lock eth irq */ 1857 - wmb(); 1803 + dma_wmb(); 1858 1804 rp->tx_ring[entry].tx_status |= cpu_to_le32(DescOwn); 1859 1805 wmb(); 1860 1806 1861 1807 rp->cur_tx++; 1808 + /* 1809 + * Nobody wants cur_tx write to rot for ages after the NIC will have 1810 + * seen the transmit request, especially as the transmit completion 1811 + * handler could miss it. 1812 + */ 1813 + smp_wmb(); 1862 1814 1863 1815 /* Non-x86 Todo: explicitly flush cache lines here. */ 1864 1816 ··· 1877 1817 ioaddr + ChipCmd1); 1878 1818 IOSYNC; 1879 1819 1880 - if (rp->cur_tx == rp->dirty_tx + TX_QUEUE_LEN) 1820 + /* dirty_tx may be pessimistically out-of-sync. See rhine_tx. */ 1821 + if (rhine_tx_queue_full(rp)) { 1881 1822 netif_stop_queue(dev); 1823 + smp_rmb(); 1824 + /* Rejuvenate. */ 1825 + if (!rhine_tx_queue_full(rp)) 1826 + netif_wake_queue(dev); 1827 + } 1882 1828 1883 1829 netif_dbg(rp, tx_queued, dev, "Transmit frame #%d queued in slot %d\n", 1884 1830 rp->cur_tx - 1, entry); ··· 1932 1866 { 1933 1867 struct rhine_private *rp = netdev_priv(dev); 1934 1868 struct device *hwdev = dev->dev.parent; 1935 - int txstatus = 0, entry = rp->dirty_tx % TX_RING_SIZE; 1936 1869 unsigned int pkts_compl = 0, bytes_compl = 0; 1870 + unsigned int dirty_tx = rp->dirty_tx; 1871 + unsigned int cur_tx; 1937 1872 struct sk_buff *skb; 1938 1873 1874 + /* 1875 + * The race with rhine_start_tx does not matter here as long as the 1876 + * driver enforces a value of cur_tx that was relevant when the 1877 + * packet was scheduled to the network chipset. 1878 + * Executive summary: smp_rmb() balances smp_wmb() in rhine_start_tx. 1879 + */ 1880 + smp_rmb(); 1881 + cur_tx = rp->cur_tx; 1939 1882 /* find and cleanup dirty tx descriptors */ 1940 - while (rp->dirty_tx != rp->cur_tx) { 1941 - txstatus = le32_to_cpu(rp->tx_ring[entry].tx_status); 1883 + while (dirty_tx != cur_tx) { 1884 + unsigned int entry = dirty_tx % TX_RING_SIZE; 1885 + u32 txstatus = le32_to_cpu(rp->tx_ring[entry].tx_status); 1886 + 1942 1887 netif_dbg(rp, tx_done, dev, "Tx scavenge %d status %08x\n", 1943 1888 entry, txstatus); 1944 1889 if (txstatus & DescOwn) ··· 1998 1921 pkts_compl++; 1999 1922 dev_consume_skb_any(skb); 2000 1923 rp->tx_skbuff[entry] = NULL; 2001 - entry = (++rp->dirty_tx) % TX_RING_SIZE; 1924 + dirty_tx++; 2002 1925 } 2003 1926 1927 + rp->dirty_tx = dirty_tx; 1928 + /* Pity we can't rely on the nearby BQL completion implicit barrier. */ 1929 + smp_wmb(); 1930 + 2004 1931 netdev_completed_queue(dev, pkts_compl, bytes_compl); 2005 - if ((rp->cur_tx - rp->dirty_tx) < TX_QUEUE_LEN - 4) 1932 + 1933 + /* cur_tx may be optimistically out-of-sync. See rhine_start_tx. */ 1934 + if (!rhine_tx_queue_full(rp) && netif_queue_stopped(dev)) { 2006 1935 netif_wake_queue(dev); 1936 + smp_rmb(); 1937 + /* Rejuvenate. */ 1938 + if (rhine_tx_queue_full(rp)) 1939 + netif_stop_queue(dev); 1940 + } 2007 1941 } 2008 1942 2009 1943 /** ··· 2032 1944 return be16_to_cpup((__be16 *)trailer); 2033 1945 } 2034 1946 1947 + static inline void rhine_rx_vlan_tag(struct sk_buff *skb, struct rx_desc *desc, 1948 + int data_size) 1949 + { 1950 + dma_rmb(); 1951 + if (unlikely(desc->desc_length & cpu_to_le32(DescTag))) { 1952 + u16 vlan_tci; 1953 + 1954 + vlan_tci = rhine_get_vlan_tci(skb, data_size); 1955 + __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci); 1956 + } 1957 + } 1958 + 2035 1959 /* Process up to limit frames from receive ring */ 2036 1960 static int rhine_rx(struct net_device *dev, int limit) 2037 1961 { 2038 1962 struct rhine_private *rp = netdev_priv(dev); 2039 1963 struct device *hwdev = dev->dev.parent; 2040 - int count; 2041 1964 int entry = rp->cur_rx % RX_RING_SIZE; 1965 + int count; 2042 1966 2043 1967 netif_dbg(rp, rx_status, dev, "%s(), entry %d status %08x\n", __func__, 2044 - entry, le32_to_cpu(rp->rx_head_desc->rx_status)); 1968 + entry, le32_to_cpu(rp->rx_ring[entry].rx_status)); 2045 1969 2046 1970 /* If EOP is set on the next entry, it's a new packet. Send it up. */ 2047 1971 for (count = 0; count < limit; ++count) { 2048 - struct rx_desc *desc = rp->rx_head_desc; 1972 + struct rx_desc *desc = rp->rx_ring + entry; 2049 1973 u32 desc_status = le32_to_cpu(desc->rx_status); 2050 - u32 desc_length = le32_to_cpu(desc->desc_length); 2051 1974 int data_size = desc_status >> 16; 2052 1975 2053 1976 if (desc_status & DescOwn) ··· 2074 1975 "entry %#x length %d status %08x!\n", 2075 1976 entry, data_size, 2076 1977 desc_status); 2077 - netdev_warn(dev, 2078 - "Oversized Ethernet frame %p vs %p\n", 2079 - rp->rx_head_desc, 2080 - &rp->rx_ring[entry]); 2081 1978 dev->stats.rx_length_errors++; 2082 1979 } else if (desc_status & RxErr) { 2083 1980 /* There was a error. */ ··· 2095 2000 } 2096 2001 } 2097 2002 } else { 2098 - struct sk_buff *skb = NULL; 2099 2003 /* Length should omit the CRC */ 2100 2004 int pkt_len = data_size - 4; 2101 - u16 vlan_tci = 0; 2005 + struct sk_buff *skb; 2102 2006 2103 2007 /* Check if the packet is long enough to accept without 2104 2008 copying to a minimally-sized skbuff. */ 2105 - if (pkt_len < rx_copybreak) 2009 + if (pkt_len < rx_copybreak) { 2106 2010 skb = netdev_alloc_skb_ip_align(dev, pkt_len); 2107 - if (skb) { 2011 + if (unlikely(!skb)) 2012 + goto drop; 2013 + 2108 2014 dma_sync_single_for_cpu(hwdev, 2109 2015 rp->rx_skbuff_dma[entry], 2110 2016 rp->rx_buf_sz, ··· 2114 2018 skb_copy_to_linear_data(skb, 2115 2019 rp->rx_skbuff[entry]->data, 2116 2020 pkt_len); 2117 - skb_put(skb, pkt_len); 2021 + 2118 2022 dma_sync_single_for_device(hwdev, 2119 2023 rp->rx_skbuff_dma[entry], 2120 2024 rp->rx_buf_sz, 2121 2025 DMA_FROM_DEVICE); 2122 2026 } else { 2027 + struct rhine_skb_dma sd; 2028 + 2029 + if (unlikely(rhine_skb_dma_init(dev, &sd) < 0)) 2030 + goto drop; 2031 + 2123 2032 skb = rp->rx_skbuff[entry]; 2124 - if (skb == NULL) { 2125 - netdev_err(dev, "Inconsistent Rx descriptor chain\n"); 2126 - break; 2127 - } 2128 - rp->rx_skbuff[entry] = NULL; 2129 - skb_put(skb, pkt_len); 2033 + 2130 2034 dma_unmap_single(hwdev, 2131 2035 rp->rx_skbuff_dma[entry], 2132 2036 rp->rx_buf_sz, 2133 2037 DMA_FROM_DEVICE); 2038 + rhine_skb_dma_nic_store(rp, &sd, entry); 2134 2039 } 2135 2040 2136 - if (unlikely(desc_length & DescTag)) 2137 - vlan_tci = rhine_get_vlan_tci(skb, data_size); 2138 - 2041 + skb_put(skb, pkt_len); 2139 2042 skb->protocol = eth_type_trans(skb, dev); 2140 2043 2141 - if (unlikely(desc_length & DescTag)) 2142 - __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci); 2044 + rhine_rx_vlan_tag(skb, desc, data_size); 2045 + 2143 2046 netif_receive_skb(skb); 2144 2047 2145 2048 u64_stats_update_begin(&rp->rx_stats.syncp); ··· 2146 2051 rp->rx_stats.packets++; 2147 2052 u64_stats_update_end(&rp->rx_stats.syncp); 2148 2053 } 2054 + give_descriptor_to_nic: 2055 + desc->rx_status = cpu_to_le32(DescOwn); 2149 2056 entry = (++rp->cur_rx) % RX_RING_SIZE; 2150 - rp->rx_head_desc = &rp->rx_ring[entry]; 2151 - } 2152 - 2153 - /* Refill the Rx ring buffers. */ 2154 - for (; rp->cur_rx - rp->dirty_rx > 0; rp->dirty_rx++) { 2155 - struct sk_buff *skb; 2156 - entry = rp->dirty_rx % RX_RING_SIZE; 2157 - if (rp->rx_skbuff[entry] == NULL) { 2158 - skb = netdev_alloc_skb(dev, rp->rx_buf_sz); 2159 - rp->rx_skbuff[entry] = skb; 2160 - if (skb == NULL) 2161 - break; /* Better luck next round. */ 2162 - rp->rx_skbuff_dma[entry] = 2163 - dma_map_single(hwdev, skb->data, 2164 - rp->rx_buf_sz, 2165 - DMA_FROM_DEVICE); 2166 - if (dma_mapping_error(hwdev, 2167 - rp->rx_skbuff_dma[entry])) { 2168 - dev_kfree_skb(skb); 2169 - rp->rx_skbuff_dma[entry] = 0; 2170 - break; 2171 - } 2172 - rp->rx_ring[entry].addr = cpu_to_le32(rp->rx_skbuff_dma[entry]); 2173 - } 2174 - rp->rx_ring[entry].rx_status = cpu_to_le32(DescOwn); 2175 2057 } 2176 2058 2177 2059 return count; 2060 + 2061 + drop: 2062 + dev->stats.rx_dropped++; 2063 + goto give_descriptor_to_nic; 2178 2064 } 2179 2065 2180 2066 static void rhine_restart_tx(struct net_device *dev) { ··· 2560 2484 enable_mmio(rp->pioaddr, rp->quirks); 2561 2485 rhine_power_init(dev); 2562 2486 free_tbufs(dev); 2563 - free_rbufs(dev); 2564 2487 alloc_tbufs(dev); 2565 - alloc_rbufs(dev); 2488 + rhine_reset_rbufs(rp); 2566 2489 rhine_task_enable(rp); 2567 2490 spin_lock_bh(&rp->lock); 2568 2491 init_registers(dev);