15 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
26 #include <linux/netdevice.h>
32 #include <linux/in6.h>
34 #include <linux/hrtimer.h>
37 #include <linux/ctype.h>
39 #include <linux/tcp.h>
41 #include <asm/checksum.h>
47 #define TILE_NET_TIMEOUT (5 * HZ)
50 #define TILE_NET_CHANNELS 32
53 #define TILE_NET_BATCH 128
56 #define TILE_NET_WEIGHT 64
59 #define IQUEUE_ENTRIES 512
62 #define EQUEUE_ENTRIES 2048
68 #define HEADER_BYTES 128
75 #define TILE_NET_MAX_COMPS 64
77 #define MAX_FRAGS (MAX_SKB_FRAGS + 1)
82 #define COMPS_SIZE (TILE_NET_CHANNELS * sizeof(struct tile_net_comps))
85 #define NOTIF_RING_SIZE (IQUEUE_ENTRIES * sizeof(gxio_mpipe_idesc_t))
92 #define TX_TIMER_DELAY_USEC 30
95 #define EGRESS_TIMER_DELAY_USEC 1000
206 #define BUFFER_SIZE_SMALL_ENUM GXIO_MPIPE_BUFFER_SIZE_128
207 #define BUFFER_SIZE_SMALL 128
208 #define BUFFER_SIZE_LARGE_ENUM GXIO_MPIPE_BUFFER_SIZE_1664
209 #define BUFFER_SIZE_LARGE 1664
212 static int small_buffer_stack = -1;
213 static int large_buffer_stack = -1;
216 static size_t buffer_stack_size;
219 static void *small_buffer_stack_va;
220 static void *large_buffer_stack_va;
223 static int first_bucket = -1;
224 static int num_buckets = 1;
227 static int ingress_irq = -1;
230 static char *network_cpus_string;
233 static struct cpumask network_cpus_map;
236 static char *loopify_link_name;
239 static char *custom_str;
248 static bool network_cpus_init(
void)
253 if (network_cpus_string ==
NULL)
258 pr_warn(
"tile_net.cpus=%s: malformed cpu list\n",
259 network_cpus_string);
264 cpumask_and(&network_cpus_map, &network_cpus_map, cpu_possible_mask);
266 if (cpumask_empty(&network_cpus_map)) {
267 pr_warn(
"Ignoring empty tile_net.cpus='%s'.\n",
268 network_cpus_string);
272 cpulist_scnprintf(buf,
sizeof(buf), &network_cpus_map);
273 pr_info(
"Linux network CPUs: %s\n", buf);
287 MODULE_PARM_DESC(loopify,
"name the device to use loop0/1 for ingress/egress");
301 static void tile_net_stats_add(
unsigned long value,
unsigned long *
field)
308 static bool tile_net_provide_buffer(
bool small)
310 int stack = small ? small_buffer_stack : large_buffer_stack;
311 const unsigned long buffer_alignment = 128;
315 len =
sizeof(
struct sk_buff **) + buffer_alignment;
317 skb = dev_alloc_skb(len);
322 skb_reserve(skb,
sizeof(
struct sk_buff **));
323 skb_reserve(skb, -(
long)skb->
data & (buffer_alignment - 1));
331 gxio_mpipe_push_buffer(&context, stack,
332 (
void *)va_to_tile_io_addr(skb->
data));
338 static struct sk_buff *mpipe_buf_to_skb(
void *
va)
345 if (skb->
data != va) {
350 panic(
"Corrupt linux buffer! va=%p, skb=%p, skb->data=%p",
357 static void tile_net_pop_all_buffers(
int stack)
360 tile_io_addr_t
addr =
361 (tile_io_addr_t)gxio_mpipe_pop_buffer(&context, stack);
369 static void tile_net_provide_needed_buffers(
void)
374 if (!tile_net_provide_buffer(
true))
380 if (!tile_net_provide_buffer(
false))
392 static inline bool filter_packet(
struct net_device *
dev,
void *buf)
400 !is_multicast_ether_addr(buf) &&
401 compare_ether_addr(dev->
dev_addr, buf) != 0)
425 tile_net_stats_add(1, &priv->
stats.rx_packets);
426 tile_net_stats_add(len, &priv->
stats.rx_bytes);
451 tile_net_stats_add(1, &priv->
stats.rx_dropped);
452 gxio_mpipe_iqueue_consume(&info->
iqueue, idesc);
454 pr_info(
"Dropping packet (insufficient buffers).\n");
459 l2_offset = custom_str ? 0 : gxio_mpipe_idesc_get_l2_offset(idesc);
462 va = tile_io_addr_to_va((
unsigned long)(
long)idesc->
va);
465 buf = va + l2_offset;
466 len = idesc->
l2_size - l2_offset;
471 filter = filter_packet(dev, buf);
473 gxio_mpipe_iqueue_drop(&info->
iqueue, idesc);
475 struct sk_buff *skb = mpipe_buf_to_skb(va);
480 tile_net_receive_skb(dev, skb, idesc, len);
483 gxio_mpipe_iqueue_consume(&info->
iqueue, idesc);
502 unsigned int work = 0;
507 while ((n = gxio_mpipe_iqueue_try_peek(&info->
iqueue, &idesc)) > 0) {
508 for (i = 0; i <
n; i++) {
511 if (tile_net_handle_packet(idesc + i)) {
512 if (++work >= budget)
525 if (gxio_mpipe_iqueue_try_peek(&info->
iqueue, &idesc) > 0)
526 napi_schedule(&info->
napi);
531 tile_net_provide_needed_buffers();
540 napi_schedule(&info->
napi);
547 int limit,
bool force_update)
553 if (!gxio_mpipe_equeue_is_complete(equeue, comp->
when,
554 force_update || n == 0))
577 static void tile_net_schedule_tx_wake_timer(
struct net_device *dev,
598 static void tile_net_schedule_egress_timer(
void)
618 unsigned long irqflags;
619 bool pending =
false;
633 tile_net_free_comps(egress->
equeue, comps, -1,
true);
639 tile_net_schedule_egress_timer();
650 static void tile_net_update_cpu(
void *
arg)
665 napi_enable(&info->
napi);
672 napi_disable(&info->
napi);
682 static int tile_net_update(
struct net_device *dev)
685 bool saw_channel =
false;
693 if (tile_net_devs_for_channel[channel] ==
NULL)
709 netdev_warn(dev,
"gxio_mpipe_rules_commit failed: %d\n", rc);
716 (saw_channel ? dev :
NULL), 1);
720 sim_enable_mpipe_links(0, -1);
744 netdev_err(dev,
"gxio_mpipe_alloc_buffer_stacks failed: %d\n",
748 small_buffer_stack =
rc;
749 large_buffer_stack = rc + 1;
752 small_buffer_stack_va =
754 if (small_buffer_stack_va == NULL) {
756 "Could not alloc %zd bytes for buffer stacks\n",
762 small_buffer_stack_va,
763 buffer_stack_size, 0);
765 netdev_err(dev,
"gxio_mpipe_init_buffer_stack: %d\n", rc);
772 "gxio_mpipe_register_buffer_memory failed: %d\n",
778 large_buffer_stack_va =
780 if (large_buffer_stack_va == NULL) {
782 "Could not alloc %zd bytes for buffer stacks\n",
788 large_buffer_stack_va,
789 buffer_stack_size, 0);
791 netdev_err(dev,
"gxio_mpipe_init_buffer_stack failed: %d\n",
799 "gxio_mpipe_register_buffer_memory failed: %d\n",
810 static int alloc_percpu_mpipe_resources(
struct net_device *dev,
822 netdev_err(dev,
"Failed to alloc %zd bytes comps memory\n",
838 "Failed to alloc %zd bytes iqueue memory\n",
847 "gxio_mpipe_iqueue_init failed: %d\n", rc);
859 static int init_notif_group_and_buckets(
struct net_device *dev,
860 int ring,
int network_cpus_count)
867 netdev_err(dev,
"gxio_mpipe_alloc_notif_groups failed: %d\n",
874 if (network_cpus_count > 4)
876 else if (network_cpus_count > 1)
882 netdev_err(dev,
"gxio_mpipe_alloc_buckets failed: %d\n", rc);
889 &context, group, ring, network_cpus_count,
890 first_bucket, num_buckets,
895 "gxio_mpipe_init_notif_group_and_buckets failed: %d\n",
908 static int tile_net_setup_interrupts(
struct net_device *dev)
914 netdev_err(dev,
"create_irq failed: %d\n", rc);
919 rc =
request_irq(ingress_irq, tile_net_handle_ingress_irq,
920 0,
"tile_net", NULL);
922 netdev_err(dev,
"request_irq failed: %d\n", rc);
941 static void tile_net_init_mpipe_fail(
void)
946 if (small_buffer_stack >= 0)
947 tile_net_pop_all_buffers(small_buffer_stack);
948 if (large_buffer_stack >= 0)
949 tile_net_pop_all_buffers(large_buffer_stack);
964 if (small_buffer_stack_va)
966 if (large_buffer_stack_va)
969 small_buffer_stack_va =
NULL;
970 large_buffer_stack_va =
NULL;
971 large_buffer_stack = -1;
972 small_buffer_stack = -1;
985 static int tile_net_init_mpipe(
struct net_device *dev)
987 int i, num_buffers,
rc;
989 int first_ring, ring;
990 int network_cpus_count =
cpus_weight(network_cpus_map);
993 netdev_err(dev,
"Networking requires hash_default!\n");
999 netdev_err(dev,
"gxio_mpipe_init failed: %d\n", rc);
1006 rc = init_buffer_stacks(dev, num_buffers);
1012 for (i = 0; i < num_buffers; i++) {
1013 if (!tile_net_provide_buffer(
true)) {
1014 netdev_err(dev,
"Cannot allocate initial sk_bufs!\n");
1018 for (i = 0; i < num_buffers; i++) {
1019 if (!tile_net_provide_buffer(
false)) {
1020 netdev_err(dev,
"Cannot allocate initial sk_bufs!\n");
1028 netdev_err(dev,
"gxio_mpipe_alloc_notif_rings failed %d\n",
1037 rc = alloc_percpu_mpipe_resources(dev, cpu, ring);
1044 rc = init_notif_group_and_buckets(dev, first_ring, network_cpus_count);
1049 rc = tile_net_setup_interrupts(dev);
1056 tile_net_init_mpipe_fail();
1064 static int tile_net_init_egress(
struct net_device *dev,
int echannel)
1066 struct page *headers_page, *edescs_page, *equeue_page;
1069 unsigned char *headers;
1070 int headers_order, edescs_order, equeue_order;
1076 if (egress_for_echannel[echannel].equeue != NULL)
1082 if (headers_page == NULL) {
1084 "Could not alloc %zd bytes for TSO headers.\n",
1094 if (edescs_page == NULL) {
1096 "Could not alloc %zd bytes for eDMA ring.\n",
1103 equeue_order =
get_order(
sizeof(*equeue));
1105 if (equeue_page == NULL) {
1107 "Could not alloc %zd bytes for equeue info.\n",
1118 netdev_warn(dev,
"gxio_mpipe_alloc_edma_rings failed: %d\n",
1126 edescs, edescs_size, 0);
1128 netdev_err(dev,
"gxio_mpipe_equeue_init failed: %d\n", rc);
1133 egress_for_echannel[echannel].equeue = equeue;
1134 egress_for_echannel[echannel].headers = headers;
1152 const char *link_name)
1156 netdev_err(dev,
"Failed to open '%s'\n", link_name);
1159 rc = gxio_mpipe_link_channel(link);
1161 netdev_err(dev,
"gxio_mpipe_link_channel bad value: %d\n", rc);
1169 static int tile_net_open(
struct net_device *dev)
1174 mutex_lock(&tile_net_devs_for_channel_mutex);
1177 if (ingress_irq < 0) {
1178 rc = tile_net_init_mpipe(dev);
1184 if (
unlikely((loopify_link_name != NULL) &&
1186 rc = tile_net_link_open(dev, &priv->
link,
"loop0");
1190 rc = tile_net_link_open(dev, &priv->
loopify_link,
"loop1");
1196 rc = tile_net_link_open(dev, &priv->
link, dev->
name);
1204 rc = tile_net_init_egress(dev, priv->
echannel);
1208 tile_net_devs_for_channel[priv->
channel] =
dev;
1210 rc = tile_net_update(dev);
1225 tx_wake->
timer.function = tile_net_handle_tx_wake_timer;
1230 netif_start_subqueue(dev, cpu);
1235 if (priv->loopify_channel >= 0) {
1237 netdev_warn(dev,
"Failed to close loopify link!\n");
1242 netdev_warn(dev,
"Failed to close link!\n");
1250 return (rc > -512) ? rc : -
EIO;
1254 static int tile_net_stop(
struct net_device *dev)
1265 netif_stop_subqueue(dev, cpu);
1268 mutex_lock(&tile_net_devs_for_channel_mutex);
1270 (
void)tile_net_update(dev);
1273 netdev_warn(dev,
"Failed to close loopify link!\n");
1278 netdev_warn(dev,
"Failed to close link!\n");
1288 static inline void *tile_net_frag_buf(
skb_frag_t *
f)
1290 unsigned long pfn =
page_to_pfn(skb_frag_page(f));
1297 static s64 tile_net_equeue_try_reserve(
struct net_device *dev,
1305 tile_net_free_comps(equeue, comps, 32,
false) != 0) {
1308 s64 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
1315 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
1321 netif_stop_subqueue(dev, tx_queue_idx);
1322 tile_net_schedule_tx_wake_timer(dev, tx_queue_idx);
1333 static int tso_count_edescs(
struct sk_buff *skb)
1336 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1340 long f_size = skb_headlen(skb) - sh_len;
1346 for (segment = 0; segment < sh->
gso_segs; segment++) {
1348 unsigned int p_used = 0;
1351 for (num_edescs++; p_used <
p_len; num_edescs++) {
1354 while (f_used >= f_size) {
1356 f_size = skb_frag_size(&sh->
frags[f_id]);
1362 if (n > f_size - f_used)
1363 n = f_size - f_used;
1370 if (data_len < p_len)
1380 static void tso_headers_prepare(
struct sk_buff *skb,
unsigned char *headers,
1386 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1387 unsigned int data_len = skb->
len - sh_len;
1389 unsigned int ih_off, th_off,
p_len;
1390 unsigned int isum_seed, tsum_seed,
id,
seq;
1392 long f_size = skb_headlen(skb) - sh_len;
1400 ih_off = skb_network_offset(skb);
1401 th_off = skb_transport_offset(skb);
1405 isum_seed = ((0xFFFF - ih->
check) +
1413 for (segment = 0; segment < sh->
gso_segs; segment++) {
1415 unsigned int p_used = 0;
1420 memcpy(buf, data, sh_len);
1423 ih = (
struct iphdr *)(buf + ih_off);
1430 th = (
struct tcphdr *)(buf + th_off);
1432 th->
check = csum_long(tsum_seed +
htons(sh_len + p_len));
1442 while (p_used < p_len) {
1445 while (f_used >= f_size) {
1447 f_size = skb_frag_size(&sh->
frags[f_id]);
1453 if (n > f_size - f_used)
1454 n = f_size - f_used;
1466 if (data_len < p_len)
1476 struct sk_buff *skb,
unsigned char *headers,
s64 slot)
1480 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1481 unsigned int data_len = skb->
len - sh_len;
1486 long f_size = skb_headlen(skb) - sh_len;
1488 void *f_data = skb->
data + sh_len;
1491 unsigned int csum_start;
1495 csum_start = skb_checksum_start_offset(skb);
1496 edesc_head.
csum = 1;
1502 edesc_head.
stack_idx = large_buffer_stack;
1503 edesc_body.
stack_idx = large_buffer_stack;
1506 for (segment = 0; segment < sh->
gso_segs; segment++) {
1508 unsigned int p_used = 0;
1513 edesc_head.
va = va_to_tile_io_addr(buf);
1514 gxio_mpipe_equeue_put_at(equeue, edesc_head, slot);
1518 while (p_used < p_len) {
1522 while (f_used >= f_size) {
1524 f_size = skb_frag_size(&sh->
frags[f_id]);
1525 f_data = tile_net_frag_buf(&sh->
frags[f_id]);
1529 va = f_data + f_used;
1533 if (n > f_size - f_used)
1534 n = f_size - f_used;
1539 edesc_body.
va = va_to_tile_io_addr(va);
1542 gxio_mpipe_equeue_put_at(equeue, edesc_body, slot);
1551 if (data_len < p_len)
1556 tile_net_stats_add(tx_packets, &priv->
stats.tx_packets);
1581 unsigned long irqflags;
1586 num_edescs = tso_count_edescs(skb);
1591 slot = tile_net_equeue_try_reserve(dev, skb->
queue_mapping, comps,
1592 equeue, num_edescs);
1599 tso_headers_prepare(skb, egress->
headers, slot);
1602 tso_egress(dev, equeue, skb, egress->
headers, slot);
1605 add_comp(equeue, comps, slot + num_edescs - 1, skb);
1610 tile_net_schedule_egress_timer();
1616 static unsigned int tile_net_tx_frags(
struct frag *frags,
1618 void *b_data,
unsigned int b_len)
1620 unsigned int i, n = 0;
1625 frags[
n].
buf = b_data;
1626 frags[n++].
length = b_len;
1629 for (i = 0; i < sh->
nr_frags; i++) {
1631 frags[
n].
buf = tile_net_frag_buf(f);
1632 frags[n++].
length = skb_frag_size(f);
1647 unsigned int len = skb->
len;
1648 unsigned char *data = skb->
data;
1649 unsigned int num_edescs;
1652 unsigned long irqflags;
1657 if (skb_is_gso(skb))
1658 return tile_net_tx_tso(skb, dev);
1660 num_edescs = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
1666 for (i = 0; i < num_edescs; i++) {
1668 edesc.
va = va_to_tile_io_addr(frags[i].buf);
1673 edescs[num_edescs - 1].
bound = 1;
1677 unsigned int csum_start = skb_checksum_start_offset(skb);
1679 edescs[0].csum_start = csum_start;
1680 edescs[0].csum_dest = csum_start + skb->
csum_offset;
1686 slot = tile_net_equeue_try_reserve(dev, skb->
queue_mapping, comps,
1687 equeue, num_edescs);
1693 for (i = 0; i < num_edescs; i++)
1694 gxio_mpipe_equeue_put_at(equeue, edescs[i], slot++);
1697 add_comp(equeue, comps, slot - 1, skb);
1700 tile_net_stats_add(1, &priv->
stats.tx_packets);
1702 &priv->
stats.tx_bytes);
1707 tile_net_schedule_egress_timer();
1719 static void tile_net_tx_timeout(
struct net_device *dev)
1724 netif_wake_subqueue(dev, cpu);
1737 return &priv->
stats;
1741 static int tile_net_change_mtu(
struct net_device *dev,
int new_mtu)
1743 if ((new_mtu < 68) || (new_mtu > 1500))
1758 static int tile_net_set_mac_address(
struct net_device *dev,
void *
p)
1762 if (!is_valid_ether_addr(addr->
sa_data))
1768 #ifdef CONFIG_NET_POLL_CONTROLLER
1773 static void tile_net_netpoll(
struct net_device *dev)
1776 tile_net_handle_ingress_irq(ingress_irq, NULL);
1782 .ndo_open = tile_net_open,
1783 .ndo_stop = tile_net_stop,
1784 .ndo_start_xmit = tile_net_tx,
1785 .ndo_select_queue = tile_net_select_queue,
1786 .ndo_do_ioctl = tile_net_ioctl,
1787 .ndo_get_stats = tile_net_get_stats,
1788 .ndo_change_mtu = tile_net_change_mtu,
1789 .ndo_tx_timeout = tile_net_tx_timeout,
1790 .ndo_set_mac_address = tile_net_set_mac_address,
1791 #ifdef CONFIG_NET_POLL_CONTROLLER
1792 .ndo_poll_controller = tile_net_netpoll,
1801 static void tile_net_setup(
struct net_device *dev)
1816 static void tile_net_dev_init(
const char *
name,
const uint8_t *
mac)
1825 if (
strncmp(name,
"loop", 4) == 0)
1834 pr_err(
"alloc_netdev_mqs(%s) failed\n", name);
1839 priv = netdev_priv(dev);
1840 memset(priv, 0,
sizeof(*priv));
1850 for (i = 0; i < 6; i++)
1857 eth_hw_addr_random(dev);
1863 netdev_err(dev,
"register_netdev failed %d\n", ret);
1870 static void tile_net_init_module_percpu(
void *
unused)
1881 info->
egress_timer.function = tile_net_handle_egress_timer;
1885 static int __init tile_net_init_module(
void)
1891 pr_info(
"Tilera Network Driver\n");
1893 mutex_init(&tile_net_devs_for_channel_mutex);
1896 on_each_cpu(tile_net_init_module_percpu, NULL, 1);
1900 tile_net_dev_init(name, mac);
1902 if (!network_cpus_init())
1903 network_cpus_map = *cpu_online_mask;