blob: 2f4b7b9fa5fb389514e29ccf04c25def2f2cf604 [file] [log] [blame]
Chris Metcalfe5a06932010-11-01 17:00:37 -04001/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05002 * Copyright 2011 Tilera Corporation. All Rights Reserved.
Chris Metcalfe5a06932010-11-01 17:00:37 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/moduleparam.h>
18#include <linux/sched.h>
19#include <linux/kernel.h> /* printk() */
20#include <linux/slab.h> /* kmalloc() */
21#include <linux/errno.h> /* error codes */
22#include <linux/types.h> /* size_t */
23#include <linux/interrupt.h>
24#include <linux/in.h>
25#include <linux/netdevice.h> /* struct device, and other headers */
26#include <linux/etherdevice.h> /* eth_type_trans */
27#include <linux/skbuff.h>
28#include <linux/ioctl.h>
29#include <linux/cdev.h>
30#include <linux/hugetlb.h>
31#include <linux/in6.h>
32#include <linux/timer.h>
33#include <linux/io.h>
Chris Metcalfd68e2d32013-07-25 12:41:15 -040034#include <linux/u64_stats_sync.h>
Chris Metcalfe5a06932010-11-01 17:00:37 -040035#include <asm/checksum.h>
36#include <asm/homecache.h>
37
38#include <hv/drv_xgbe_intf.h>
39#include <hv/drv_xgbe_impl.h>
40#include <hv/hypervisor.h>
41#include <hv/netio_intf.h>
42
43/* For TSO */
44#include <linux/ip.h>
45#include <linux/tcp.h>
46
47
Chris Metcalfe5a06932010-11-01 17:00:37 -040048/*
49 * First, "tile_net_init_module()" initializes all four "devices" which
50 * can be used by linux.
51 *
52 * Then, "ifconfig DEVICE up" calls "tile_net_open()", which analyzes
53 * the network cpus, then uses "tile_net_open_aux()" to initialize
54 * LIPP/LEPP, and then uses "tile_net_open_inner()" to register all
55 * the tiles, provide buffers to LIPP, allow ingress to start, and
56 * turn on hypervisor interrupt handling (and NAPI) on all tiles.
57 *
58 * If registration fails due to the link being down, then "retry_work"
59 * is used to keep calling "tile_net_open_inner()" until it succeeds.
60 *
61 * If "ifconfig DEVICE down" is called, it uses "tile_net_stop()" to
62 * stop egress, drain the LIPP buffers, unregister all the tiles, stop
63 * LIPP/LEPP, and wipe the LEPP queue.
64 *
65 * We start out with the ingress interrupt enabled on each CPU. When
66 * this interrupt fires, we disable it, and call "napi_schedule()".
67 * This will cause "tile_net_poll()" to be called, which will pull
68 * packets from the netio queue, filtering them out, or passing them
69 * to "netif_receive_skb()". If our budget is exhausted, we will
70 * return, knowing we will be called again later. Otherwise, we
71 * reenable the ingress interrupt, and call "napi_complete()".
72 *
Chris Metcalfd91c6412011-03-01 12:49:53 -050073 * HACK: Since disabling the ingress interrupt is not reliable, we
74 * ignore the interrupt if the global "active" flag is false.
75 *
Chris Metcalfe5a06932010-11-01 17:00:37 -040076 *
77 * NOTE: The use of "native_driver" ensures that EPP exists, and that
Chris Metcalfd91c6412011-03-01 12:49:53 -050078 * we are using "LIPP" and "LEPP".
Chris Metcalfe5a06932010-11-01 17:00:37 -040079 *
80 * NOTE: Failing to free completions for an arbitrarily long time
81 * (which is defined to be illegal) does in fact cause bizarre
82 * problems. The "egress_timer" helps prevent this from happening.
Chris Metcalfe5a06932010-11-01 17:00:37 -040083 */
84
85
86/* HACK: Allow use of "jumbo" packets. */
87/* This should be 1500 if "jumbo" is not set in LIPP. */
88/* This should be at most 10226 (10240 - 14) if "jumbo" is set in LIPP. */
89/* ISSUE: This has not been thoroughly tested (except at 1500). */
90#define TILE_NET_MTU 1500
91
Chris Metcalfe5a06932010-11-01 17:00:37 -040092/* Define this to collapse "duplicate" acks. */
93/* #define IGNORE_DUP_ACKS */
94
95/* HACK: Define this to verify incoming packets. */
96/* #define TILE_NET_VERIFY_INGRESS */
97
98/* Use 3000 to enable the Linux Traffic Control (QoS) layer, else 0. */
99#define TILE_NET_TX_QUEUE_LEN 0
100
101/* Define to dump packets (prints out the whole packet on tx and rx). */
102/* #define TILE_NET_DUMP_PACKETS */
103
104/* Define to enable debug spew (all PDEBUG's are enabled). */
105/* #define TILE_NET_DEBUG */
106
107
108/* Define to activate paranoia checks. */
109/* #define TILE_NET_PARANOIA */
110
111/* Default transmit lockup timeout period, in jiffies. */
112#define TILE_NET_TIMEOUT (5 * HZ)
113
114/* Default retry interval for bringing up the NetIO interface, in jiffies. */
115#define TILE_NET_RETRY_INTERVAL (5 * HZ)
116
117/* Number of ports (xgbe0, xgbe1, gbe0, gbe1). */
118#define TILE_NET_DEVS 4
119
120
121
122/* Paranoia. */
123#if NET_IP_ALIGN != LIPP_PACKET_PADDING
124#error "NET_IP_ALIGN must match LIPP_PACKET_PADDING."
125#endif
126
127
128/* Debug print. */
129#ifdef TILE_NET_DEBUG
130#define PDEBUG(fmt, args...) net_printk(fmt, ## args)
131#else
132#define PDEBUG(fmt, args...)
133#endif
134
135
136MODULE_AUTHOR("Tilera");
137MODULE_LICENSE("GPL");
138
Chris Metcalfd91c6412011-03-01 12:49:53 -0500139
Chris Metcalfe5a06932010-11-01 17:00:37 -0400140/*
141 * Queue of incoming packets for a specific cpu and device.
142 *
143 * Includes a pointer to the "system" data, and the actual "user" data.
144 */
145struct tile_netio_queue {
146 netio_queue_impl_t *__system_part;
147 netio_queue_user_impl_t __user_part;
148
149};
150
151
152/*
153 * Statistics counters for a specific cpu and device.
154 */
155struct tile_net_stats_t {
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400156 struct u64_stats_sync syncp;
157 u64 rx_packets; /* total packets received */
158 u64 tx_packets; /* total packets transmitted */
159 u64 rx_bytes; /* total bytes received */
160 u64 tx_bytes; /* total bytes transmitted */
161 u64 rx_errors; /* packets truncated or marked bad by hw */
162 u64 rx_dropped; /* packets not for us or intf not up */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400163};
164
165
166/*
167 * Info for a specific cpu and device.
168 *
169 * ISSUE: There is a "dev" pointer in "napi" as well.
170 */
171struct tile_net_cpu {
172 /* The NAPI struct. */
173 struct napi_struct napi;
174 /* Packet queue. */
175 struct tile_netio_queue queue;
176 /* Statistics. */
177 struct tile_net_stats_t stats;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500178 /* True iff NAPI is enabled. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400179 bool napi_enabled;
Linus Torvalds8a9ea322011-10-25 13:25:22 +0200180 /* True if this tile has successfully registered with the IPP. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400181 bool registered;
182 /* True if the link was down last time we tried to register. */
183 bool link_down;
184 /* True if "egress_timer" is scheduled. */
185 bool egress_timer_scheduled;
186 /* Number of small sk_buffs which must still be provided. */
187 unsigned int num_needed_small_buffers;
188 /* Number of large sk_buffs which must still be provided. */
189 unsigned int num_needed_large_buffers;
190 /* A timer for handling egress completions. */
191 struct timer_list egress_timer;
192};
193
194
195/*
196 * Info for a specific device.
197 */
198struct tile_net_priv {
199 /* Our network device. */
200 struct net_device *dev;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500201 /* Pages making up the egress queue. */
202 struct page *eq_pages;
203 /* Address of the actual egress queue. */
204 lepp_queue_t *eq;
205 /* Protects "eq". */
206 spinlock_t eq_lock;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400207 /* The hypervisor handle for this interface. */
208 int hv_devhdl;
209 /* The intr bit mask that IDs this device. */
210 u32 intr_id;
211 /* True iff "tile_net_open_aux()" has succeeded. */
Chris Metcalfd91c6412011-03-01 12:49:53 -0500212 bool partly_opened;
213 /* True iff the device is "active". */
214 bool active;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400215 /* Effective network cpus. */
216 struct cpumask network_cpus_map;
217 /* Number of network cpus. */
218 int network_cpus_count;
219 /* Credits per network cpu. */
220 int network_cpus_credits;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400221 /* For NetIO bringup retries. */
222 struct delayed_work retry_work;
223 /* Quick access to per cpu data. */
224 struct tile_net_cpu *cpu[NR_CPUS];
225};
226
Chris Metcalfd91c6412011-03-01 12:49:53 -0500227/* Log2 of the number of small pages needed for the egress queue. */
228#define EQ_ORDER get_order(sizeof(lepp_queue_t))
229/* Size of the egress queue's pages. */
230#define EQ_SIZE (1 << (PAGE_SHIFT + EQ_ORDER))
Chris Metcalfe5a06932010-11-01 17:00:37 -0400231
232/*
233 * The actual devices (xgbe0, xgbe1, gbe0, gbe1).
234 */
235static struct net_device *tile_net_devs[TILE_NET_DEVS];
236
237/*
238 * The "tile_net_cpu" structures for each device.
239 */
240static DEFINE_PER_CPU(struct tile_net_cpu, hv_xgbe0);
241static DEFINE_PER_CPU(struct tile_net_cpu, hv_xgbe1);
242static DEFINE_PER_CPU(struct tile_net_cpu, hv_gbe0);
243static DEFINE_PER_CPU(struct tile_net_cpu, hv_gbe1);
244
245
246/*
247 * True if "network_cpus" was specified.
248 */
249static bool network_cpus_used;
250
251/*
252 * The actual cpus in "network_cpus".
253 */
254static struct cpumask network_cpus_map;
255
256
257
258#ifdef TILE_NET_DEBUG
259/*
260 * printk with extra stuff.
261 *
262 * We print the CPU we're running in brackets.
263 */
264static void net_printk(char *fmt, ...)
265{
266 int i;
267 int len;
268 va_list args;
269 static char buf[256];
270
271 len = sprintf(buf, "tile_net[%2.2d]: ", smp_processor_id());
272 va_start(args, fmt);
273 i = vscnprintf(buf + len, sizeof(buf) - len - 1, fmt, args);
274 va_end(args);
275 buf[255] = '\0';
276 pr_notice(buf);
277}
278#endif
279
280
281#ifdef TILE_NET_DUMP_PACKETS
282/*
283 * Dump a packet.
284 */
285static void dump_packet(unsigned char *data, unsigned long length, char *s)
286{
Chris Metcalfd91c6412011-03-01 12:49:53 -0500287 int my_cpu = smp_processor_id();
288
Chris Metcalfe5a06932010-11-01 17:00:37 -0400289 unsigned long i;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500290 char buf[128];
291
Chris Metcalfe5a06932010-11-01 17:00:37 -0400292 static unsigned int count;
293
294 pr_info("dump_packet(data %p, length 0x%lx s %s count 0x%x)\n",
295 data, length, s, count++);
296
297 pr_info("\n");
298
299 for (i = 0; i < length; i++) {
300 if ((i & 0xf) == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -0500301 sprintf(buf, "[%02d] %8.8lx:", my_cpu, i);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400302 sprintf(buf + strlen(buf), " %2.2x", data[i]);
Chris Metcalfd91c6412011-03-01 12:49:53 -0500303 if ((i & 0xf) == 0xf || i == length - 1) {
304 strcat(buf, "\n");
305 pr_info("%s", buf);
306 }
Chris Metcalfe5a06932010-11-01 17:00:37 -0400307 }
308}
309#endif
310
311
312/*
313 * Provide support for the __netio_fastio1() swint
314 * (see <hv/drv_xgbe_intf.h> for how it is used).
315 *
316 * The fastio swint2 call may clobber all the caller-saved registers.
317 * It rarely clobbers memory, but we allow for the possibility in
318 * the signature just to be on the safe side.
319 *
320 * Also, gcc doesn't seem to allow an input operand to be
321 * clobbered, so we fake it with dummy outputs.
322 *
323 * This function can't be static because of the way it is declared
324 * in the netio header.
325 */
326inline int __netio_fastio1(u32 fastio_index, u32 arg0)
327{
328 long result, clobber_r1, clobber_r10;
329 asm volatile("swint2"
330 : "=R00" (result),
331 "=R01" (clobber_r1), "=R10" (clobber_r10)
332 : "R10" (fastio_index), "R01" (arg0)
333 : "memory", "r2", "r3", "r4",
334 "r5", "r6", "r7", "r8", "r9",
335 "r11", "r12", "r13", "r14",
336 "r15", "r16", "r17", "r18", "r19",
337 "r20", "r21", "r22", "r23", "r24",
338 "r25", "r26", "r27", "r28", "r29");
339 return result;
340}
341
342
Chris Metcalf92795672012-03-30 19:23:35 -0400343static void tile_net_return_credit(struct tile_net_cpu *info)
344{
345 struct tile_netio_queue *queue = &info->queue;
346 netio_queue_user_impl_t *qup = &queue->__user_part;
347
348 /* Return four credits after every fourth packet. */
349 if (--qup->__receive_credit_remaining == 0) {
350 u32 interval = qup->__receive_credit_interval;
351 qup->__receive_credit_remaining = interval;
352 __netio_fastio_return_credits(qup->__fastio_index, interval);
353 }
354}
355
356
357
Chris Metcalfe5a06932010-11-01 17:00:37 -0400358/*
359 * Provide a linux buffer to LIPP.
360 */
361static void tile_net_provide_linux_buffer(struct tile_net_cpu *info,
362 void *va, bool small)
363{
364 struct tile_netio_queue *queue = &info->queue;
365
366 /* Convert "va" and "small" to "linux_buffer_t". */
367 unsigned int buffer = ((unsigned int)(__pa(va) >> 7) << 1) + small;
368
369 __netio_fastio_free_buffer(queue->__user_part.__fastio_index, buffer);
370}
371
372
373/*
374 * Provide a linux buffer for LIPP.
Chris Metcalfd91c6412011-03-01 12:49:53 -0500375 *
376 * Note that the ACTUAL allocation for each buffer is a "struct sk_buff",
377 * plus a chunk of memory that includes not only the requested bytes, but
378 * also NET_SKB_PAD bytes of initial padding, and a "struct skb_shared_info".
379 *
380 * Note that "struct skb_shared_info" is 88 bytes with 64K pages and
381 * 268 bytes with 4K pages (since the frags[] array needs 18 entries).
382 *
383 * Without jumbo packets, the maximum packet size will be 1536 bytes,
384 * and we use 2 bytes (NET_IP_ALIGN) of padding. ISSUE: If we told
385 * the hardware to clip at 1518 bytes instead of 1536 bytes, then we
386 * could save an entire cache line, but in practice, we don't need it.
387 *
388 * Since CPAs are 38 bits, and we can only encode the high 31 bits in
389 * a "linux_buffer_t", the low 7 bits must be zero, and thus, we must
390 * align the actual "va" mod 128.
391 *
392 * We assume that the underlying "head" will be aligned mod 64. Note
393 * that in practice, we have seen "head" NOT aligned mod 128 even when
394 * using 2048 byte allocations, which is surprising.
395 *
396 * If "head" WAS always aligned mod 128, we could change LIPP to
397 * assume that the low SIX bits are zero, and the 7th bit is one, that
398 * is, align the actual "va" mod 128 plus 64, which would be "free".
399 *
400 * For now, the actual "head" pointer points at NET_SKB_PAD bytes of
401 * padding, plus 28 or 92 bytes of extra padding, plus the sk_buff
402 * pointer, plus the NET_IP_ALIGN padding, plus 126 or 1536 bytes for
403 * the actual packet, plus 62 bytes of empty padding, plus some
404 * padding and the "struct skb_shared_info".
405 *
406 * With 64K pages, a large buffer thus needs 32+92+4+2+1536+62+88
407 * bytes, or 1816 bytes, which fits comfortably into 2048 bytes.
408 *
409 * With 64K pages, a small buffer thus needs 32+92+4+2+126+88
410 * bytes, or 344 bytes, which means we are wasting 64+ bytes, and
411 * could presumably increase the size of small buffers.
412 *
413 * With 4K pages, a large buffer thus needs 32+92+4+2+1536+62+268
414 * bytes, or 1996 bytes, which fits comfortably into 2048 bytes.
415 *
416 * With 4K pages, a small buffer thus needs 32+92+4+2+126+268
417 * bytes, or 524 bytes, which is annoyingly wasteful.
418 *
419 * Maybe we should increase LIPP_SMALL_PACKET_SIZE to 192?
420 *
421 * ISSUE: Maybe we should increase "NET_SKB_PAD" to 64?
Chris Metcalfe5a06932010-11-01 17:00:37 -0400422 */
423static bool tile_net_provide_needed_buffer(struct tile_net_cpu *info,
424 bool small)
425{
Chris Metcalfd91c6412011-03-01 12:49:53 -0500426#if TILE_NET_MTU <= 1536
427 /* Without "jumbo", 2 + 1536 should be sufficient. */
428 unsigned int large_size = NET_IP_ALIGN + 1536;
429#else
430 /* ISSUE: This has not been tested. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400431 unsigned int large_size = NET_IP_ALIGN + TILE_NET_MTU + 100;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500432#endif
Chris Metcalfe5a06932010-11-01 17:00:37 -0400433
Chris Metcalfd91c6412011-03-01 12:49:53 -0500434 /* Avoid "false sharing" with last cache line. */
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000435 /* ISSUE: This is already done by "netdev_alloc_skb()". */
Chris Metcalfd91c6412011-03-01 12:49:53 -0500436 unsigned int len =
Chris Metcalfe5a06932010-11-01 17:00:37 -0400437 (((small ? LIPP_SMALL_PACKET_SIZE : large_size) +
438 CHIP_L2_LINE_SIZE() - 1) & -CHIP_L2_LINE_SIZE());
439
Chris Metcalfd91c6412011-03-01 12:49:53 -0500440 unsigned int padding = 128 - NET_SKB_PAD;
441 unsigned int align;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400442
443 struct sk_buff *skb;
444 void *va;
445
446 struct sk_buff **skb_ptr;
447
Chris Metcalfd91c6412011-03-01 12:49:53 -0500448 /* Request 96 extra bytes for alignment purposes. */
Chris Metcalf00a62d42012-04-02 13:17:37 -0400449 skb = netdev_alloc_skb(info->napi.dev, len + padding);
Chris Metcalfd91c6412011-03-01 12:49:53 -0500450 if (skb == NULL)
451 return false;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400452
Chris Metcalfd91c6412011-03-01 12:49:53 -0500453 /* Skip 32 or 96 bytes to align "data" mod 128. */
454 align = -(long)skb->data & (128 - 1);
455 BUG_ON(align > padding);
456 skb_reserve(skb, align);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400457
Chris Metcalfd91c6412011-03-01 12:49:53 -0500458 /* This address is given to IPP. */
459 va = skb->data;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400460
Chris Metcalfd91c6412011-03-01 12:49:53 -0500461 /* Buffers must not span a huge page. */
462 BUG_ON(((((long)va & ~HPAGE_MASK) + len) & HPAGE_MASK) != 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400463
Chris Metcalfd91c6412011-03-01 12:49:53 -0500464#ifdef TILE_NET_PARANOIA
465#if CHIP_HAS_CBOX_HOME_MAP()
466 if (hash_default) {
467 HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)va);
468 if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
469 panic("Non-HFH ingress buffer! VA=%p Mode=%d PTE=%llx",
470 va, hv_pte_get_mode(pte), hv_pte_val(pte));
Chris Metcalfe5a06932010-11-01 17:00:37 -0400471 }
Chris Metcalfd91c6412011-03-01 12:49:53 -0500472#endif
473#endif
474
475 /* Invalidate the packet buffer. */
476 if (!hash_default)
477 __inv_buffer(va, len);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400478
479 /* Skip two bytes to satisfy LIPP assumptions. */
480 /* Note that this aligns IP on a 16 byte boundary. */
481 /* ISSUE: Do this when the packet arrives? */
482 skb_reserve(skb, NET_IP_ALIGN);
483
484 /* Save a back-pointer to 'skb'. */
485 skb_ptr = va - sizeof(*skb_ptr);
486 *skb_ptr = skb;
487
Chris Metcalfe5a06932010-11-01 17:00:37 -0400488 /* Make sure "skb_ptr" has been flushed. */
489 __insn_mf();
490
Chris Metcalfe5a06932010-11-01 17:00:37 -0400491 /* Provide the new buffer. */
492 tile_net_provide_linux_buffer(info, va, small);
493
494 return true;
495}
496
497
498/*
499 * Provide linux buffers for LIPP.
500 */
501static void tile_net_provide_needed_buffers(struct tile_net_cpu *info)
502{
503 while (info->num_needed_small_buffers != 0) {
504 if (!tile_net_provide_needed_buffer(info, true))
505 goto oops;
506 info->num_needed_small_buffers--;
507 }
508
509 while (info->num_needed_large_buffers != 0) {
510 if (!tile_net_provide_needed_buffer(info, false))
511 goto oops;
512 info->num_needed_large_buffers--;
513 }
514
515 return;
516
517oops:
518
519 /* Add a description to the page allocation failure dump. */
520 pr_notice("Could not provide a linux buffer to LIPP.\n");
521}
522
523
524/*
525 * Grab some LEPP completions, and store them in "comps", of size
526 * "comps_size", and return the number of completions which were
527 * stored, so the caller can free them.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400528 */
Chris Metcalfd91c6412011-03-01 12:49:53 -0500529static unsigned int tile_net_lepp_grab_comps(lepp_queue_t *eq,
Chris Metcalfe5a06932010-11-01 17:00:37 -0400530 struct sk_buff *comps[],
531 unsigned int comps_size,
Chris Metcalfd91c6412011-03-01 12:49:53 -0500532 unsigned int min_size)
Chris Metcalfe5a06932010-11-01 17:00:37 -0400533{
Chris Metcalfe5a06932010-11-01 17:00:37 -0400534 unsigned int n = 0;
535
Chris Metcalfd91c6412011-03-01 12:49:53 -0500536 unsigned int comp_head = eq->comp_head;
537 unsigned int comp_busy = eq->comp_busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400538
539 while (comp_head != comp_busy && n < comps_size) {
540 comps[n++] = eq->comps[comp_head];
541 LEPP_QINC(comp_head);
542 }
543
Chris Metcalfd91c6412011-03-01 12:49:53 -0500544 if (n < min_size)
545 return 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400546
547 eq->comp_head = comp_head;
548
Chris Metcalfe5a06932010-11-01 17:00:37 -0400549 return n;
550}
551
552
553/*
Chris Metcalfd91c6412011-03-01 12:49:53 -0500554 * Free some comps, and return true iff there are still some pending.
555 */
556static bool tile_net_lepp_free_comps(struct net_device *dev, bool all)
557{
558 struct tile_net_priv *priv = netdev_priv(dev);
559
560 lepp_queue_t *eq = priv->eq;
561
562 struct sk_buff *olds[64];
563 unsigned int wanted = 64;
564 unsigned int i, n;
565 bool pending;
566
567 spin_lock(&priv->eq_lock);
568
569 if (all)
570 eq->comp_busy = eq->comp_tail;
571
572 n = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
573
574 pending = (eq->comp_head != eq->comp_tail);
575
576 spin_unlock(&priv->eq_lock);
577
578 for (i = 0; i < n; i++)
579 kfree_skb(olds[i]);
580
581 return pending;
582}
583
584
585/*
Chris Metcalfe5a06932010-11-01 17:00:37 -0400586 * Make sure the egress timer is scheduled.
587 *
588 * Note that we use "schedule if not scheduled" logic instead of the more
589 * obvious "reschedule" logic, because "reschedule" is fairly expensive.
590 */
591static void tile_net_schedule_egress_timer(struct tile_net_cpu *info)
592{
593 if (!info->egress_timer_scheduled) {
594 mod_timer_pinned(&info->egress_timer, jiffies + 1);
595 info->egress_timer_scheduled = true;
596 }
597}
598
599
600/*
601 * The "function" for "info->egress_timer".
602 *
603 * This timer will reschedule itself as long as there are any pending
604 * completions expected (on behalf of any tile).
605 *
606 * ISSUE: Realistically, will the timer ever stop scheduling itself?
607 *
608 * ISSUE: This timer is almost never actually needed, so just use a global
609 * timer that can run on any tile.
610 *
611 * ISSUE: Maybe instead track number of expected completions, and free
612 * only that many, resetting to zero if "pending" is ever false.
613 */
614static void tile_net_handle_egress_timer(unsigned long arg)
615{
616 struct tile_net_cpu *info = (struct tile_net_cpu *)arg;
617 struct net_device *dev = info->napi.dev;
618
Chris Metcalfe5a06932010-11-01 17:00:37 -0400619 /* The timer is no longer scheduled. */
620 info->egress_timer_scheduled = false;
621
Chris Metcalfd91c6412011-03-01 12:49:53 -0500622 /* Free comps, and reschedule timer if more are pending. */
623 if (tile_net_lepp_free_comps(dev, false))
Chris Metcalfe5a06932010-11-01 17:00:37 -0400624 tile_net_schedule_egress_timer(info);
625}
626
627
628#ifdef IGNORE_DUP_ACKS
629
630/*
631 * Help detect "duplicate" ACKs. These are sequential packets (for a
632 * given flow) which are exactly 66 bytes long, sharing everything but
633 * ID=2@0x12, Hsum=2@0x18, Ack=4@0x2a, WinSize=2@0x30, Csum=2@0x32,
634 * Tstamps=10@0x38. The ID's are +1, the Hsum's are -1, the Ack's are
635 * +N, and the Tstamps are usually identical.
636 *
637 * NOTE: Apparently truly duplicate acks (with identical "ack" values),
638 * should not be collapsed, as they are used for some kind of flow control.
639 */
640static bool is_dup_ack(char *s1, char *s2, unsigned int len)
641{
642 int i;
643
644 unsigned long long ignorable = 0;
645
646 /* Identification. */
647 ignorable |= (1ULL << 0x12);
648 ignorable |= (1ULL << 0x13);
649
650 /* Header checksum. */
651 ignorable |= (1ULL << 0x18);
652 ignorable |= (1ULL << 0x19);
653
654 /* ACK. */
655 ignorable |= (1ULL << 0x2a);
656 ignorable |= (1ULL << 0x2b);
657 ignorable |= (1ULL << 0x2c);
658 ignorable |= (1ULL << 0x2d);
659
660 /* WinSize. */
661 ignorable |= (1ULL << 0x30);
662 ignorable |= (1ULL << 0x31);
663
664 /* Checksum. */
665 ignorable |= (1ULL << 0x32);
666 ignorable |= (1ULL << 0x33);
667
668 for (i = 0; i < len; i++, ignorable >>= 1) {
669
670 if ((ignorable & 1) || (s1[i] == s2[i]))
671 continue;
672
673#ifdef TILE_NET_DEBUG
674 /* HACK: Mention non-timestamp diffs. */
675 if (i < 0x38 && i != 0x2f &&
676 net_ratelimit())
677 pr_info("Diff at 0x%x\n", i);
678#endif
679
680 return false;
681 }
682
683#ifdef TILE_NET_NO_SUPPRESS_DUP_ACKS
684 /* HACK: Do not suppress truly duplicate ACKs. */
685 /* ISSUE: Is this actually necessary or helpful? */
686 if (s1[0x2a] == s2[0x2a] &&
687 s1[0x2b] == s2[0x2b] &&
688 s1[0x2c] == s2[0x2c] &&
689 s1[0x2d] == s2[0x2d]) {
690 return false;
691 }
692#endif
693
694 return true;
695}
696
697#endif
698
699
700
Chris Metcalfd91c6412011-03-01 12:49:53 -0500701static void tile_net_discard_aux(struct tile_net_cpu *info, int index)
702{
703 struct tile_netio_queue *queue = &info->queue;
704 netio_queue_impl_t *qsp = queue->__system_part;
705 netio_queue_user_impl_t *qup = &queue->__user_part;
706
707 int index2_aux = index + sizeof(netio_pkt_t);
708 int index2 =
709 ((index2_aux ==
710 qsp->__packet_receive_queue.__last_packet_plus_one) ?
711 0 : index2_aux);
712
713 netio_pkt_t *pkt = (netio_pkt_t *)((unsigned long) &qsp[1] + index);
714
715 /* Extract the "linux_buffer_t". */
716 unsigned int buffer = pkt->__packet.word;
717
718 /* Convert "linux_buffer_t" to "va". */
719 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
720
721 /* Acquire the associated "skb". */
722 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
723 struct sk_buff *skb = *skb_ptr;
724
725 kfree_skb(skb);
726
727 /* Consume this packet. */
728 qup->__packet_receive_read = index2;
729}
730
731
Chris Metcalfe5a06932010-11-01 17:00:37 -0400732/*
Chris Metcalfd91c6412011-03-01 12:49:53 -0500733 * Like "tile_net_poll()", but just discard packets.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400734 */
735static void tile_net_discard_packets(struct net_device *dev)
736{
737 struct tile_net_priv *priv = netdev_priv(dev);
738 int my_cpu = smp_processor_id();
739 struct tile_net_cpu *info = priv->cpu[my_cpu];
740 struct tile_netio_queue *queue = &info->queue;
741 netio_queue_impl_t *qsp = queue->__system_part;
742 netio_queue_user_impl_t *qup = &queue->__user_part;
743
744 while (qup->__packet_receive_read !=
745 qsp->__packet_receive_queue.__packet_write) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400746 int index = qup->__packet_receive_read;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500747 tile_net_discard_aux(info, index);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400748 }
749}
750
751
752/*
753 * Handle the next packet. Return true if "processed", false if "filtered".
754 */
755static bool tile_net_poll_aux(struct tile_net_cpu *info, int index)
756{
757 struct net_device *dev = info->napi.dev;
758
759 struct tile_netio_queue *queue = &info->queue;
760 netio_queue_impl_t *qsp = queue->__system_part;
761 netio_queue_user_impl_t *qup = &queue->__user_part;
762 struct tile_net_stats_t *stats = &info->stats;
763
764 int filter;
765
766 int index2_aux = index + sizeof(netio_pkt_t);
767 int index2 =
768 ((index2_aux ==
769 qsp->__packet_receive_queue.__last_packet_plus_one) ?
770 0 : index2_aux);
771
772 netio_pkt_t *pkt = (netio_pkt_t *)((unsigned long) &qsp[1] + index);
773
774 netio_pkt_metadata_t *metadata = NETIO_PKT_METADATA(pkt);
Chris Metcalf439a93a2013-08-01 11:36:42 -0400775 netio_pkt_status_t pkt_status = NETIO_PKT_STATUS_M(metadata, pkt);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400776
Chris Metcalfd91c6412011-03-01 12:49:53 -0500777 /* Extract the packet size. FIXME: Shouldn't the second line */
778 /* get subtracted? Mostly moot, since it should be "zero". */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400779 unsigned long len =
780 (NETIO_PKT_CUSTOM_LENGTH(pkt) +
781 NET_IP_ALIGN - NETIO_PACKET_PADDING);
782
783 /* Extract the "linux_buffer_t". */
784 unsigned int buffer = pkt->__packet.word;
785
786 /* Extract "small" (vs "large"). */
787 bool small = ((buffer & 1) != 0);
788
789 /* Convert "linux_buffer_t" to "va". */
790 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
791
792 /* Extract the packet data pointer. */
793 /* Compare to "NETIO_PKT_CUSTOM_DATA(pkt)". */
794 unsigned char *buf = va + NET_IP_ALIGN;
795
Chris Metcalfe5a06932010-11-01 17:00:37 -0400796 /* Invalidate the packet buffer. */
797 if (!hash_default)
798 __inv_buffer(buf, len);
799
800 /* ISSUE: Is this needed? */
801 dev->last_rx = jiffies;
802
803#ifdef TILE_NET_DUMP_PACKETS
804 dump_packet(buf, len, "rx");
805#endif /* TILE_NET_DUMP_PACKETS */
806
807#ifdef TILE_NET_VERIFY_INGRESS
Chris Metcalf439a93a2013-08-01 11:36:42 -0400808 if (pkt_status == NETIO_PKT_STATUS_OVERSIZE && len >= 64) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400809 dump_packet(buf, len, "rx");
Chris Metcalf439a93a2013-08-01 11:36:42 -0400810 panic("Unexpected OVERSIZE.");
Chris Metcalfe5a06932010-11-01 17:00:37 -0400811 }
812#endif
813
814 filter = 0;
815
Chris Metcalf439a93a2013-08-01 11:36:42 -0400816 if (pkt_status == NETIO_PKT_STATUS_BAD) {
817 /* Handle CRC error and hardware truncation. */
818 filter = 2;
819 } else if (!(dev->flags & IFF_UP)) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400820 /* Filter packets received before we're up. */
821 filter = 1;
Chris Metcalf439a93a2013-08-01 11:36:42 -0400822 } else if (NETIO_PKT_ETHERTYPE_RECOGNIZED_M(metadata, pkt) &&
823 pkt_status == NETIO_PKT_STATUS_UNDERSIZE) {
Chris Metcalfd91c6412011-03-01 12:49:53 -0500824 /* Filter "truncated" packets. */
Chris Metcalf439a93a2013-08-01 11:36:42 -0400825 filter = 2;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400826 } else if (!(dev->flags & IFF_PROMISC)) {
Chris Metcalfd91c6412011-03-01 12:49:53 -0500827 if (!is_multicast_ether_addr(buf)) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400828 /* Filter packets not for our address. */
829 const u8 *mine = dev->dev_addr;
Joe Perches2e42e472012-05-09 17:17:46 +0000830 filter = !ether_addr_equal(mine, buf);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400831 }
832 }
833
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400834 u64_stats_update_begin(&stats->syncp);
835
Chris Metcalf439a93a2013-08-01 11:36:42 -0400836 if (filter != 0) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400837
Chris Metcalf439a93a2013-08-01 11:36:42 -0400838 if (filter == 1)
839 stats->rx_dropped++;
840 else
841 stats->rx_errors++;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400842
843 tile_net_provide_linux_buffer(info, va, small);
844
845 } else {
846
847 /* Acquire the associated "skb". */
848 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
849 struct sk_buff *skb = *skb_ptr;
850
851 /* Paranoia. */
852 if (skb->data != buf)
853 panic("Corrupt linux buffer from LIPP! "
854 "VA=%p, skb=%p, skb->data=%p\n",
855 va, skb, skb->data);
856
857 /* Encode the actual packet length. */
858 skb_put(skb, len);
859
860 /* NOTE: This call also sets "skb->dev = dev". */
861 skb->protocol = eth_type_trans(skb, dev);
862
Chris Metcalfd91c6412011-03-01 12:49:53 -0500863 /* Avoid recomputing "good" TCP/UDP checksums. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400864 if (NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt))
865 skb->ip_summed = CHECKSUM_UNNECESSARY;
866
867 netif_receive_skb(skb);
868
869 stats->rx_packets++;
870 stats->rx_bytes += len;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400871 }
872
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400873 u64_stats_update_end(&stats->syncp);
874
Chris Metcalf92795672012-03-30 19:23:35 -0400875 /* ISSUE: It would be nice to defer this until the packet has */
876 /* actually been processed. */
877 tile_net_return_credit(info);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400878
879 /* Consume this packet. */
880 qup->__packet_receive_read = index2;
881
882 return !filter;
883}
884
885
886/*
887 * Handle some packets for the given device on the current CPU.
888 *
Chris Metcalfd91c6412011-03-01 12:49:53 -0500889 * If "tile_net_stop()" is called on some other tile while this
890 * function is running, we will return, hopefully before that
891 * other tile asks us to call "napi_disable()".
892 *
893 * The "rotting packet" race condition occurs if a packet arrives
894 * during the extremely narrow window between the queue appearing to
895 * be empty, and the ingress interrupt being re-enabled. This happens
896 * a LOT under heavy network load.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400897 */
898static int tile_net_poll(struct napi_struct *napi, int budget)
899{
900 struct net_device *dev = napi->dev;
901 struct tile_net_priv *priv = netdev_priv(dev);
902 int my_cpu = smp_processor_id();
903 struct tile_net_cpu *info = priv->cpu[my_cpu];
904 struct tile_netio_queue *queue = &info->queue;
905 netio_queue_impl_t *qsp = queue->__system_part;
906 netio_queue_user_impl_t *qup = &queue->__user_part;
907
908 unsigned int work = 0;
909
Chris Metcalfd91c6412011-03-01 12:49:53 -0500910 while (priv->active) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400911 int index = qup->__packet_receive_read;
912 if (index == qsp->__packet_receive_queue.__packet_write)
913 break;
914
915 if (tile_net_poll_aux(info, index)) {
916 if (++work >= budget)
917 goto done;
918 }
919 }
920
921 napi_complete(&info->napi);
922
Chris Metcalfd91c6412011-03-01 12:49:53 -0500923 if (!priv->active)
924 goto done;
925
926 /* Re-enable the ingress interrupt. */
Chris Metcalf0c905472011-12-01 12:58:19 -0500927 enable_percpu_irq(priv->intr_id, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400928
Chris Metcalfd91c6412011-03-01 12:49:53 -0500929 /* HACK: Avoid the "rotting packet" problem (see above). */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400930 if (qup->__packet_receive_read !=
Chris Metcalfd91c6412011-03-01 12:49:53 -0500931 qsp->__packet_receive_queue.__packet_write) {
932 /* ISSUE: Sometimes this returns zero, presumably */
933 /* because an interrupt was handled for this tile. */
934 (void)napi_reschedule(&info->napi);
935 }
Chris Metcalfe5a06932010-11-01 17:00:37 -0400936
937done:
938
Chris Metcalfd91c6412011-03-01 12:49:53 -0500939 if (priv->active)
940 tile_net_provide_needed_buffers(info);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400941
942 return work;
943}
944
945
946/*
947 * Handle an ingress interrupt for the given device on the current cpu.
Chris Metcalfd91c6412011-03-01 12:49:53 -0500948 *
949 * ISSUE: Sometimes this gets called after "disable_percpu_irq()" has
950 * been called! This is probably due to "pending hypervisor downcalls".
951 *
952 * ISSUE: Is there any race condition between the "napi_schedule()" here
953 * and the "napi_complete()" call above?
Chris Metcalfe5a06932010-11-01 17:00:37 -0400954 */
955static irqreturn_t tile_net_handle_ingress_interrupt(int irq, void *dev_ptr)
956{
957 struct net_device *dev = (struct net_device *)dev_ptr;
958 struct tile_net_priv *priv = netdev_priv(dev);
959 int my_cpu = smp_processor_id();
960 struct tile_net_cpu *info = priv->cpu[my_cpu];
961
Chris Metcalfd91c6412011-03-01 12:49:53 -0500962 /* Disable the ingress interrupt. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400963 disable_percpu_irq(priv->intr_id);
964
Chris Metcalfd91c6412011-03-01 12:49:53 -0500965 /* Ignore unwanted interrupts. */
966 if (!priv->active)
967 return IRQ_HANDLED;
968
969 /* ISSUE: Sometimes "info->napi_enabled" is false here. */
970
Chris Metcalfe5a06932010-11-01 17:00:37 -0400971 napi_schedule(&info->napi);
972
973 return IRQ_HANDLED;
974}
975
976
977/*
978 * One time initialization per interface.
979 */
980static int tile_net_open_aux(struct net_device *dev)
981{
982 struct tile_net_priv *priv = netdev_priv(dev);
983
984 int ret;
985 int dummy;
986 unsigned int epp_lotar;
987
988 /*
989 * Find out where EPP memory should be homed.
990 */
991 ret = hv_dev_pread(priv->hv_devhdl, 0,
992 (HV_VirtAddr)&epp_lotar, sizeof(epp_lotar),
993 NETIO_EPP_SHM_OFF);
994 if (ret < 0) {
995 pr_err("could not read epp_shm_queue lotar.\n");
996 return -EIO;
997 }
998
999 /*
1000 * Home the page on the EPP.
1001 */
1002 {
1003 int epp_home = hv_lotar_to_cpu(epp_lotar);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001004 homecache_change_page_home(priv->eq_pages, EQ_ORDER, epp_home);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001005 }
1006
1007 /*
1008 * Register the EPP shared memory queue.
1009 */
1010 {
1011 netio_ipp_address_t ea = {
1012 .va = 0,
Chris Metcalfd91c6412011-03-01 12:49:53 -05001013 .pa = __pa(priv->eq),
Chris Metcalfe5a06932010-11-01 17:00:37 -04001014 .pte = hv_pte(0),
Chris Metcalfd91c6412011-03-01 12:49:53 -05001015 .size = EQ_SIZE,
Chris Metcalfe5a06932010-11-01 17:00:37 -04001016 };
1017 ea.pte = hv_pte_set_lotar(ea.pte, epp_lotar);
1018 ea.pte = hv_pte_set_mode(ea.pte, HV_PTE_MODE_CACHE_TILE_L3);
1019 ret = hv_dev_pwrite(priv->hv_devhdl, 0,
1020 (HV_VirtAddr)&ea,
1021 sizeof(ea),
1022 NETIO_EPP_SHM_OFF);
1023 if (ret < 0)
1024 return -EIO;
1025 }
1026
1027 /*
1028 * Start LIPP/LEPP.
1029 */
1030 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1031 sizeof(dummy), NETIO_IPP_START_SHIM_OFF) < 0) {
1032 pr_warning("Failed to start LIPP/LEPP.\n");
1033 return -EIO;
1034 }
1035
1036 return 0;
1037}
1038
1039
1040/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001041 * Register with hypervisor on the current CPU.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001042 *
1043 * Strangely, this function does important things even if it "fails",
1044 * which is especially common if the link is not up yet. Hopefully
1045 * these things are all "harmless" if done twice!
1046 */
1047static void tile_net_register(void *dev_ptr)
1048{
1049 struct net_device *dev = (struct net_device *)dev_ptr;
1050 struct tile_net_priv *priv = netdev_priv(dev);
1051 int my_cpu = smp_processor_id();
1052 struct tile_net_cpu *info;
1053
1054 struct tile_netio_queue *queue;
1055
1056 /* Only network cpus can receive packets. */
1057 int queue_id =
1058 cpumask_test_cpu(my_cpu, &priv->network_cpus_map) ? 0 : 255;
1059
1060 netio_input_config_t config = {
1061 .flags = 0,
1062 .num_receive_packets = priv->network_cpus_credits,
1063 .queue_id = queue_id
1064 };
1065
1066 int ret = 0;
1067 netio_queue_impl_t *queuep;
1068
1069 PDEBUG("tile_net_register(queue_id %d)\n", queue_id);
1070
1071 if (!strcmp(dev->name, "xgbe0"))
1072 info = &__get_cpu_var(hv_xgbe0);
1073 else if (!strcmp(dev->name, "xgbe1"))
1074 info = &__get_cpu_var(hv_xgbe1);
1075 else if (!strcmp(dev->name, "gbe0"))
1076 info = &__get_cpu_var(hv_gbe0);
1077 else if (!strcmp(dev->name, "gbe1"))
1078 info = &__get_cpu_var(hv_gbe1);
1079 else
1080 BUG();
1081
1082 /* Initialize the egress timer. */
1083 init_timer(&info->egress_timer);
1084 info->egress_timer.data = (long)info;
1085 info->egress_timer.function = tile_net_handle_egress_timer;
1086
1087 priv->cpu[my_cpu] = info;
1088
1089 /*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001090 * Register ourselves with LIPP. This does a lot of stuff,
1091 * including invoking the LIPP registration code.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001092 */
1093 ret = hv_dev_pwrite(priv->hv_devhdl, 0,
1094 (HV_VirtAddr)&config,
1095 sizeof(netio_input_config_t),
1096 NETIO_IPP_INPUT_REGISTER_OFF);
1097 PDEBUG("hv_dev_pwrite(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
1098 ret);
1099 if (ret < 0) {
Chris Metcalfd91c6412011-03-01 12:49:53 -05001100 if (ret != NETIO_LINK_DOWN) {
1101 printk(KERN_DEBUG "hv_dev_pwrite "
1102 "NETIO_IPP_INPUT_REGISTER_OFF failure %d\n",
1103 ret);
1104 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001105 info->link_down = (ret == NETIO_LINK_DOWN);
1106 return;
1107 }
1108
1109 /*
1110 * Get the pointer to our queue's system part.
1111 */
1112
1113 ret = hv_dev_pread(priv->hv_devhdl, 0,
1114 (HV_VirtAddr)&queuep,
1115 sizeof(netio_queue_impl_t *),
1116 NETIO_IPP_INPUT_REGISTER_OFF);
1117 PDEBUG("hv_dev_pread(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
1118 ret);
1119 PDEBUG("queuep %p\n", queuep);
1120 if (ret <= 0) {
1121 /* ISSUE: Shouldn't this be a fatal error? */
1122 pr_err("hv_dev_pread NETIO_IPP_INPUT_REGISTER_OFF failure\n");
1123 return;
1124 }
1125
1126 queue = &info->queue;
1127
1128 queue->__system_part = queuep;
1129
1130 memset(&queue->__user_part, 0, sizeof(netio_queue_user_impl_t));
1131
1132 /* This is traditionally "config.num_receive_packets / 2". */
1133 queue->__user_part.__receive_credit_interval = 4;
1134 queue->__user_part.__receive_credit_remaining =
1135 queue->__user_part.__receive_credit_interval;
1136
1137 /*
1138 * Get a fastio index from the hypervisor.
1139 * ISSUE: Shouldn't this check the result?
1140 */
1141 ret = hv_dev_pread(priv->hv_devhdl, 0,
1142 (HV_VirtAddr)&queue->__user_part.__fastio_index,
1143 sizeof(queue->__user_part.__fastio_index),
1144 NETIO_IPP_GET_FASTIO_OFF);
1145 PDEBUG("hv_dev_pread(NETIO_IPP_GET_FASTIO_OFF) returned %d\n", ret);
1146
Chris Metcalfe5a06932010-11-01 17:00:37 -04001147 /* Now we are registered. */
1148 info->registered = true;
1149}
1150
1151
1152/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001153 * Deregister with hypervisor on the current CPU.
1154 *
1155 * This simply discards all our credits, so no more packets will be
1156 * delivered to this tile. There may still be packets in our queue.
1157 *
1158 * Also, disable the ingress interrupt.
1159 */
1160static void tile_net_deregister(void *dev_ptr)
1161{
1162 struct net_device *dev = (struct net_device *)dev_ptr;
1163 struct tile_net_priv *priv = netdev_priv(dev);
1164 int my_cpu = smp_processor_id();
1165 struct tile_net_cpu *info = priv->cpu[my_cpu];
1166
1167 /* Disable the ingress interrupt. */
1168 disable_percpu_irq(priv->intr_id);
1169
1170 /* Do nothing else if not registered. */
1171 if (info == NULL || !info->registered)
1172 return;
1173
1174 {
1175 struct tile_netio_queue *queue = &info->queue;
1176 netio_queue_user_impl_t *qup = &queue->__user_part;
1177
1178 /* Discard all our credits. */
1179 __netio_fastio_return_credits(qup->__fastio_index, -1);
1180 }
1181}
1182
1183
1184/*
1185 * Unregister with hypervisor on the current CPU.
1186 *
1187 * Also, disable the ingress interrupt.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001188 */
1189static void tile_net_unregister(void *dev_ptr)
1190{
1191 struct net_device *dev = (struct net_device *)dev_ptr;
1192 struct tile_net_priv *priv = netdev_priv(dev);
1193 int my_cpu = smp_processor_id();
1194 struct tile_net_cpu *info = priv->cpu[my_cpu];
1195
Chris Metcalfd91c6412011-03-01 12:49:53 -05001196 int ret;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001197 int dummy = 0;
1198
Chris Metcalfd91c6412011-03-01 12:49:53 -05001199 /* Disable the ingress interrupt. */
1200 disable_percpu_irq(priv->intr_id);
1201
1202 /* Do nothing else if not registered. */
1203 if (info == NULL || !info->registered)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001204 return;
1205
Chris Metcalfd91c6412011-03-01 12:49:53 -05001206 /* Unregister ourselves with LIPP/LEPP. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001207 ret = hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1208 sizeof(dummy), NETIO_IPP_INPUT_UNREGISTER_OFF);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001209 if (ret < 0)
1210 panic("Failed to unregister with LIPP/LEPP!\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001211
Chris Metcalfd91c6412011-03-01 12:49:53 -05001212 /* Discard all packets still in our NetIO queue. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001213 tile_net_discard_packets(dev);
1214
1215 /* Reset state. */
1216 info->num_needed_small_buffers = 0;
1217 info->num_needed_large_buffers = 0;
1218
1219 /* Cancel egress timer. */
1220 del_timer(&info->egress_timer);
1221 info->egress_timer_scheduled = false;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001222}
1223
1224
1225/*
1226 * Helper function for "tile_net_stop()".
1227 *
1228 * Also used to handle registration failure in "tile_net_open_inner()",
Chris Metcalfd91c6412011-03-01 12:49:53 -05001229 * when the various extra steps in "tile_net_stop()" are not necessary.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001230 */
1231static void tile_net_stop_aux(struct net_device *dev)
1232{
1233 struct tile_net_priv *priv = netdev_priv(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001234 int i;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001235
1236 int dummy = 0;
1237
Chris Metcalfd91c6412011-03-01 12:49:53 -05001238 /*
1239 * Unregister all tiles, so LIPP will stop delivering packets.
1240 * Also, delete all the "napi" objects (sequentially, to protect
1241 * "dev->napi_list").
1242 */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001243 on_each_cpu(tile_net_unregister, (void *)dev, 1);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001244 for_each_online_cpu(i) {
1245 struct tile_net_cpu *info = priv->cpu[i];
1246 if (info != NULL && info->registered) {
1247 netif_napi_del(&info->napi);
1248 info->registered = false;
1249 }
1250 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001251
1252 /* Stop LIPP/LEPP. */
1253 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1254 sizeof(dummy), NETIO_IPP_STOP_SHIM_OFF) < 0)
1255 panic("Failed to stop LIPP/LEPP!\n");
1256
Rusty Russell3db1cd52011-12-19 13:56:45 +00001257 priv->partly_opened = false;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001258}
1259
1260
1261/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001262 * Disable NAPI for the given device on the current cpu.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001263 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001264static void tile_net_stop_disable(void *dev_ptr)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001265{
1266 struct net_device *dev = (struct net_device *)dev_ptr;
1267 struct tile_net_priv *priv = netdev_priv(dev);
1268 int my_cpu = smp_processor_id();
1269 struct tile_net_cpu *info = priv->cpu[my_cpu];
1270
Chris Metcalfe5a06932010-11-01 17:00:37 -04001271 /* Disable NAPI if needed. */
1272 if (info != NULL && info->napi_enabled) {
1273 napi_disable(&info->napi);
1274 info->napi_enabled = false;
1275 }
1276}
1277
1278
1279/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001280 * Enable NAPI and the ingress interrupt for the given device
1281 * on the current cpu.
1282 *
1283 * ISSUE: Only do this for "network cpus"?
Chris Metcalfe5a06932010-11-01 17:00:37 -04001284 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001285static void tile_net_open_enable(void *dev_ptr)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001286{
1287 struct net_device *dev = (struct net_device *)dev_ptr;
1288 struct tile_net_priv *priv = netdev_priv(dev);
1289 int my_cpu = smp_processor_id();
1290 struct tile_net_cpu *info = priv->cpu[my_cpu];
1291
Chris Metcalfe5a06932010-11-01 17:00:37 -04001292 /* Enable NAPI. */
1293 napi_enable(&info->napi);
1294 info->napi_enabled = true;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001295
1296 /* Enable the ingress interrupt. */
Chris Metcalf0c905472011-12-01 12:58:19 -05001297 enable_percpu_irq(priv->intr_id, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001298}
1299
1300
1301/*
1302 * tile_net_open_inner does most of the work of bringing up the interface.
1303 * It's called from tile_net_open(), and also from tile_net_retry_open().
1304 * The return value is 0 if the interface was brought up, < 0 if
1305 * tile_net_open() should return the return value as an error, and > 0 if
1306 * tile_net_open() should return success and schedule a work item to
1307 * periodically retry the bringup.
1308 */
1309static int tile_net_open_inner(struct net_device *dev)
1310{
1311 struct tile_net_priv *priv = netdev_priv(dev);
1312 int my_cpu = smp_processor_id();
1313 struct tile_net_cpu *info;
1314 struct tile_netio_queue *queue;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001315 int result = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001316 int i;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001317 int dummy = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001318
1319 /*
1320 * First try to register just on the local CPU, and handle any
1321 * semi-expected "link down" failure specially. Note that we
1322 * do NOT call "tile_net_stop_aux()", unlike below.
1323 */
1324 tile_net_register(dev);
1325 info = priv->cpu[my_cpu];
1326 if (!info->registered) {
1327 if (info->link_down)
1328 return 1;
1329 return -EAGAIN;
1330 }
1331
1332 /*
1333 * Now register everywhere else. If any registration fails,
1334 * even for "link down" (which might not be possible), we
Chris Metcalfd91c6412011-03-01 12:49:53 -05001335 * clean up using "tile_net_stop_aux()". Also, add all the
1336 * "napi" objects (sequentially, to protect "dev->napi_list").
1337 * ISSUE: Only use "netif_napi_add()" for "network cpus"?
Chris Metcalfe5a06932010-11-01 17:00:37 -04001338 */
1339 smp_call_function(tile_net_register, (void *)dev, 1);
1340 for_each_online_cpu(i) {
Chris Metcalfd91c6412011-03-01 12:49:53 -05001341 struct tile_net_cpu *info = priv->cpu[i];
1342 if (info->registered)
1343 netif_napi_add(dev, &info->napi, tile_net_poll, 64);
1344 else
1345 result = -EAGAIN;
1346 }
1347 if (result != 0) {
1348 tile_net_stop_aux(dev);
1349 return result;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001350 }
1351
1352 queue = &info->queue;
1353
Chris Metcalfd91c6412011-03-01 12:49:53 -05001354 if (priv->intr_id == 0) {
1355 unsigned int irq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001356
Chris Metcalfd91c6412011-03-01 12:49:53 -05001357 /*
1358 * Acquire the irq allocated by the hypervisor. Every
1359 * queue gets the same irq. The "__intr_id" field is
1360 * "1 << irq", so we use "__ffs()" to extract "irq".
1361 */
1362 priv->intr_id = queue->__system_part->__intr_id;
1363 BUG_ON(priv->intr_id == 0);
1364 irq = __ffs(priv->intr_id);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001365
Chris Metcalfd91c6412011-03-01 12:49:53 -05001366 /*
1367 * Register the ingress interrupt handler for this
1368 * device, permanently.
1369 *
1370 * We used to call "free_irq()" in "tile_net_stop()",
1371 * and then re-register the handler here every time,
1372 * but that caused DNP errors in "handle_IRQ_event()"
1373 * because "desc->action" was NULL. See bug 9143.
1374 */
1375 tile_irq_activate(irq, TILE_IRQ_PERCPU);
1376 BUG_ON(request_irq(irq, tile_net_handle_ingress_interrupt,
1377 0, dev->name, (void *)dev) != 0);
1378 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001379
Chris Metcalfd91c6412011-03-01 12:49:53 -05001380 {
Chris Metcalfe5a06932010-11-01 17:00:37 -04001381 /* Allocate initial buffers. */
1382
1383 int max_buffers =
1384 priv->network_cpus_count * priv->network_cpus_credits;
1385
1386 info->num_needed_small_buffers =
1387 min(LIPP_SMALL_BUFFERS, max_buffers);
1388
1389 info->num_needed_large_buffers =
1390 min(LIPP_LARGE_BUFFERS, max_buffers);
1391
1392 tile_net_provide_needed_buffers(info);
1393
1394 if (info->num_needed_small_buffers != 0 ||
1395 info->num_needed_large_buffers != 0)
1396 panic("Insufficient memory for buffer stack!");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001397 }
1398
Chris Metcalfd91c6412011-03-01 12:49:53 -05001399 /* We are about to be active. */
1400 priv->active = true;
1401
1402 /* Make sure "active" is visible to all tiles. */
1403 mb();
1404
1405 /* On each tile, enable NAPI and the ingress interrupt. */
1406 on_each_cpu(tile_net_open_enable, (void *)dev, 1);
1407
1408 /* Start LIPP/LEPP and activate "ingress" at the shim. */
1409 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1410 sizeof(dummy), NETIO_IPP_INPUT_INIT_OFF) < 0)
1411 panic("Failed to activate the LIPP Shim!\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001412
1413 /* Start our transmit queue. */
1414 netif_start_queue(dev);
1415
1416 return 0;
1417}
1418
1419
1420/*
1421 * Called periodically to retry bringing up the NetIO interface,
1422 * if it doesn't come up cleanly during tile_net_open().
1423 */
1424static void tile_net_open_retry(struct work_struct *w)
1425{
1426 struct delayed_work *dw =
1427 container_of(w, struct delayed_work, work);
1428
1429 struct tile_net_priv *priv =
1430 container_of(dw, struct tile_net_priv, retry_work);
1431
1432 /*
1433 * Try to bring the NetIO interface up. If it fails, reschedule
1434 * ourselves to try again later; otherwise, tell Linux we now have
1435 * a working link. ISSUE: What if the return value is negative?
1436 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001437 if (tile_net_open_inner(priv->dev) != 0)
1438 schedule_delayed_work(&priv->retry_work,
1439 TILE_NET_RETRY_INTERVAL);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001440 else
1441 netif_carrier_on(priv->dev);
1442}
1443
1444
1445/*
1446 * Called when a network interface is made active.
1447 *
1448 * Returns 0 on success, negative value on failure.
1449 *
1450 * The open entry point is called when a network interface is made
1451 * active by the system (IFF_UP). At this point all resources needed
1452 * for transmit and receive operations are allocated, the interrupt
Chris Metcalfd91c6412011-03-01 12:49:53 -05001453 * handler is registered with the OS (if needed), the watchdog timer
1454 * is started, and the stack is notified that the interface is ready.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001455 *
1456 * If the actual link is not available yet, then we tell Linux that
1457 * we have no carrier, and we keep checking until the link comes up.
1458 */
1459static int tile_net_open(struct net_device *dev)
1460{
1461 int ret = 0;
1462 struct tile_net_priv *priv = netdev_priv(dev);
1463
1464 /*
1465 * We rely on priv->partly_opened to tell us if this is the
1466 * first time this interface is being brought up. If it is
1467 * set, the IPP was already initialized and should not be
1468 * initialized again.
1469 */
1470 if (!priv->partly_opened) {
1471
1472 int count;
1473 int credits;
1474
1475 /* Initialize LIPP/LEPP, and start the Shim. */
1476 ret = tile_net_open_aux(dev);
1477 if (ret < 0) {
1478 pr_err("tile_net_open_aux failed: %d\n", ret);
1479 return ret;
1480 }
1481
1482 /* Analyze the network cpus. */
1483
1484 if (network_cpus_used)
1485 cpumask_copy(&priv->network_cpus_map,
1486 &network_cpus_map);
1487 else
1488 cpumask_copy(&priv->network_cpus_map, cpu_online_mask);
1489
1490
1491 count = cpumask_weight(&priv->network_cpus_map);
1492
1493 /* Limit credits to available buffers, and apply min. */
1494 credits = max(16, (LIPP_LARGE_BUFFERS / count) & ~1);
1495
1496 /* Apply "GBE" max limit. */
1497 /* ISSUE: Use higher limit for XGBE? */
1498 credits = min(NETIO_MAX_RECEIVE_PKTS, credits);
1499
1500 priv->network_cpus_count = count;
1501 priv->network_cpus_credits = credits;
1502
1503#ifdef TILE_NET_DEBUG
1504 pr_info("Using %d network cpus, with %d credits each\n",
1505 priv->network_cpus_count, priv->network_cpus_credits);
1506#endif
1507
Rusty Russell3db1cd52011-12-19 13:56:45 +00001508 priv->partly_opened = true;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001509
1510 } else {
1511 /* FIXME: Is this possible? */
1512 /* printk("Already partly opened.\n"); */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001513 }
1514
1515 /*
1516 * Attempt to bring up the link.
1517 */
1518 ret = tile_net_open_inner(dev);
1519 if (ret <= 0) {
1520 if (ret == 0)
1521 netif_carrier_on(dev);
1522 return ret;
1523 }
1524
1525 /*
1526 * We were unable to bring up the NetIO interface, but we want to
1527 * try again in a little bit. Tell Linux that we have no carrier
1528 * so it doesn't try to use the interface before the link comes up
1529 * and then remember to try again later.
1530 */
1531 netif_carrier_off(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001532 schedule_delayed_work(&priv->retry_work, TILE_NET_RETRY_INTERVAL);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001533
1534 return 0;
1535}
1536
1537
Chris Metcalfd91c6412011-03-01 12:49:53 -05001538static int tile_net_drain_lipp_buffers(struct tile_net_priv *priv)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001539{
Chris Metcalfd91c6412011-03-01 12:49:53 -05001540 int n = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001541
Chris Metcalfd91c6412011-03-01 12:49:53 -05001542 /* Drain all the LIPP buffers. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001543 while (true) {
Chris Metcalf92795672012-03-30 19:23:35 -04001544 unsigned int buffer;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001545
1546 /* NOTE: This should never fail. */
1547 if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&buffer,
1548 sizeof(buffer), NETIO_IPP_DRAIN_OFF) < 0)
1549 break;
1550
1551 /* Stop when done. */
1552 if (buffer == 0)
1553 break;
1554
1555 {
1556 /* Convert "linux_buffer_t" to "va". */
1557 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
1558
1559 /* Acquire the associated "skb". */
1560 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
1561 struct sk_buff *skb = *skb_ptr;
1562
1563 kfree_skb(skb);
1564 }
Chris Metcalfd91c6412011-03-01 12:49:53 -05001565
1566 n++;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001567 }
1568
Chris Metcalfd91c6412011-03-01 12:49:53 -05001569 return n;
1570}
1571
1572
1573/*
1574 * Disables a network interface.
1575 *
1576 * Returns 0, this is not allowed to fail.
1577 *
1578 * The close entry point is called when an interface is de-activated
1579 * by the OS. The hardware is still under the drivers control, but
1580 * needs to be disabled. A global MAC reset is issued to stop the
1581 * hardware, and all transmit and receive resources are freed.
1582 *
1583 * ISSUE: How closely does "netif_running(dev)" mirror "priv->active"?
1584 *
1585 * Before we are called by "__dev_close()", "netif_running()" will
1586 * have been cleared, so no NEW calls to "tile_net_poll()" will be
1587 * made by "netpoll_poll_dev()".
1588 *
1589 * Often, this can cause some tiles to still have packets in their
1590 * queues, so we must call "tile_net_discard_packets()" later.
1591 *
1592 * Note that some other tile may still be INSIDE "tile_net_poll()",
1593 * and in fact, many will be, if there is heavy network load.
1594 *
1595 * Calling "on_each_cpu(tile_net_stop_disable, (void *)dev, 1)" when
1596 * any tile is still "napi_schedule()"'d will induce a horrible crash
1597 * when "msleep()" is called. This includes tiles which are inside
1598 * "tile_net_poll()" which have not yet called "napi_complete()".
1599 *
1600 * So, we must first try to wait long enough for other tiles to finish
1601 * with any current "tile_net_poll()" call, and, hopefully, to clear
1602 * the "scheduled" flag. ISSUE: It is unclear what happens to tiles
1603 * which have called "napi_schedule()" but which had not yet tried to
1604 * call "tile_net_poll()", or which exhausted their budget inside
1605 * "tile_net_poll()" just before this function was called.
1606 */
1607static int tile_net_stop(struct net_device *dev)
1608{
1609 struct tile_net_priv *priv = netdev_priv(dev);
1610
1611 PDEBUG("tile_net_stop()\n");
1612
1613 /* Start discarding packets. */
1614 priv->active = false;
1615
1616 /* Make sure "active" is visible to all tiles. */
1617 mb();
1618
1619 /*
1620 * On each tile, make sure no NEW packets get delivered, and
1621 * disable the ingress interrupt.
1622 *
1623 * Note that the ingress interrupt can fire AFTER this,
1624 * presumably due to packets which were recently delivered,
1625 * but it will have no effect.
1626 */
1627 on_each_cpu(tile_net_deregister, (void *)dev, 1);
1628
1629 /* Optimistically drain LIPP buffers. */
1630 (void)tile_net_drain_lipp_buffers(priv);
1631
1632 /* ISSUE: Only needed if not yet fully open. */
1633 cancel_delayed_work_sync(&priv->retry_work);
1634
1635 /* Can't transmit any more. */
1636 netif_stop_queue(dev);
1637
1638 /* Disable NAPI on each tile. */
1639 on_each_cpu(tile_net_stop_disable, (void *)dev, 1);
1640
1641 /*
1642 * Drain any remaining LIPP buffers. NOTE: This "printk()"
1643 * has never been observed, but in theory it could happen.
1644 */
1645 if (tile_net_drain_lipp_buffers(priv) != 0)
1646 printk("Had to drain some extra LIPP buffers!\n");
1647
Chris Metcalfe5a06932010-11-01 17:00:37 -04001648 /* Stop LIPP/LEPP. */
1649 tile_net_stop_aux(dev);
1650
Chris Metcalfe5a06932010-11-01 17:00:37 -04001651 /*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001652 * ISSUE: It appears that, in practice anyway, by the time we
1653 * get here, there are no pending completions, but just in case,
1654 * we free (all of) them anyway.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001655 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001656 while (tile_net_lepp_free_comps(dev, true))
1657 /* loop */;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001658
Chris Metcalfd07bd862011-05-02 16:36:48 -04001659 /* Wipe the EPP queue, and wait till the stores hit the EPP. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001660 memset(priv->eq, 0, sizeof(lepp_queue_t));
Chris Metcalfd07bd862011-05-02 16:36:48 -04001661 mb();
Chris Metcalfe5a06932010-11-01 17:00:37 -04001662
1663 return 0;
1664}
1665
1666
1667/*
1668 * Prepare the "frags" info for the resulting LEPP command.
1669 *
1670 * If needed, flush the memory used by the frags.
1671 */
1672static unsigned int tile_net_tx_frags(lepp_frag_t *frags,
1673 struct sk_buff *skb,
1674 void *b_data, unsigned int b_len)
1675{
1676 unsigned int i, n = 0;
1677
1678 struct skb_shared_info *sh = skb_shinfo(skb);
1679
1680 phys_addr_t cpa;
1681
1682 if (b_len != 0) {
1683
1684 if (!hash_default)
Chris Metcalf63b7ca62011-02-28 15:48:39 -05001685 finv_buffer_remote(b_data, b_len, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001686
1687 cpa = __pa(b_data);
1688 frags[n].cpa_lo = cpa;
1689 frags[n].cpa_hi = cpa >> 32;
1690 frags[n].length = b_len;
1691 frags[n].hash_for_home = hash_default;
1692 n++;
1693 }
1694
1695 for (i = 0; i < sh->nr_frags; i++) {
1696
1697 skb_frag_t *f = &sh->frags[i];
Chris Metcalf781a5e92011-12-01 12:56:03 -05001698 unsigned long pfn = page_to_pfn(skb_frag_page(f));
Chris Metcalfe5a06932010-11-01 17:00:37 -04001699
1700 /* FIXME: Compute "hash_for_home" properly. */
1701 /* ISSUE: The hypervisor checks CHIP_HAS_REV1_DMA_PACKETS(). */
1702 int hash_for_home = hash_default;
1703
1704 /* FIXME: Hmmm. */
1705 if (!hash_default) {
1706 void *va = pfn_to_kaddr(pfn) + f->page_offset;
Chris Metcalf781a5e92011-12-01 12:56:03 -05001707 BUG_ON(PageHighMem(skb_frag_page(f)));
Chris Metcalf92795672012-03-30 19:23:35 -04001708 finv_buffer_remote(va, skb_frag_size(f), 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001709 }
1710
1711 cpa = ((phys_addr_t)pfn << PAGE_SHIFT) + f->page_offset;
1712 frags[n].cpa_lo = cpa;
1713 frags[n].cpa_hi = cpa >> 32;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001714 frags[n].length = skb_frag_size(f);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001715 frags[n].hash_for_home = hash_for_home;
1716 n++;
1717 }
1718
1719 return n;
1720}
1721
1722
1723/*
1724 * This function takes "skb", consisting of a header template and a
1725 * payload, and hands it to LEPP, to emit as one or more segments,
1726 * each consisting of a possibly modified header, plus a piece of the
1727 * payload, via a process known as "tcp segmentation offload".
1728 *
1729 * Usually, "data" will contain the header template, of size "sh_len",
1730 * and "sh->frags" will contain "skb->data_len" bytes of payload, and
1731 * there will be "sh->gso_segs" segments.
1732 *
1733 * Sometimes, if "sendfile()" requires copying, we will be called with
1734 * "data" containing the header and payload, with "frags" being empty.
1735 *
Chris Metcalf92795672012-03-30 19:23:35 -04001736 * Sometimes, for example when using NFS over TCP, a single segment can
1737 * span 3 fragments, which must be handled carefully in LEPP.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001738 *
1739 * See "emulate_large_send_offload()" for some reference code, which
1740 * does not handle checksumming.
1741 *
1742 * ISSUE: How do we make sure that high memory DMA does not migrate?
1743 */
1744static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
1745{
1746 struct tile_net_priv *priv = netdev_priv(dev);
1747 int my_cpu = smp_processor_id();
1748 struct tile_net_cpu *info = priv->cpu[my_cpu];
1749 struct tile_net_stats_t *stats = &info->stats;
1750
1751 struct skb_shared_info *sh = skb_shinfo(skb);
1752
1753 unsigned char *data = skb->data;
1754
1755 /* The ip header follows the ethernet header. */
1756 struct iphdr *ih = ip_hdr(skb);
1757 unsigned int ih_len = ih->ihl * 4;
1758
1759 /* Note that "nh == ih", by definition. */
1760 unsigned char *nh = skb_network_header(skb);
1761 unsigned int eh_len = nh - data;
1762
1763 /* The tcp header follows the ip header. */
1764 struct tcphdr *th = (struct tcphdr *)(nh + ih_len);
1765 unsigned int th_len = th->doff * 4;
1766
1767 /* The total number of header bytes. */
1768 /* NOTE: This may be less than skb_headlen(skb). */
1769 unsigned int sh_len = eh_len + ih_len + th_len;
1770
1771 /* The number of payload bytes at "skb->data + sh_len". */
1772 /* This is non-zero for sendfile() without HIGHDMA. */
1773 unsigned int b_len = skb_headlen(skb) - sh_len;
1774
1775 /* The total number of payload bytes. */
1776 unsigned int d_len = b_len + skb->data_len;
1777
1778 /* The maximum payload size. */
1779 unsigned int p_len = sh->gso_size;
1780
1781 /* The total number of segments. */
1782 unsigned int num_segs = sh->gso_segs;
1783
1784 /* The temporary copy of the command. */
1785 u32 cmd_body[(LEPP_MAX_CMD_SIZE + 3) / 4];
1786 lepp_tso_cmd_t *cmd = (lepp_tso_cmd_t *)cmd_body;
1787
1788 /* Analyze the "frags". */
1789 unsigned int num_frags =
1790 tile_net_tx_frags(cmd->frags, skb, data + sh_len, b_len);
1791
1792 /* The size of the command, including frags and header. */
1793 size_t cmd_size = LEPP_TSO_CMD_SIZE(num_frags, sh_len);
1794
1795 /* The command header. */
1796 lepp_tso_cmd_t cmd_init = {
1797 .tso = true,
1798 .header_size = sh_len,
1799 .ip_offset = eh_len,
1800 .tcp_offset = eh_len + ih_len,
1801 .payload_size = p_len,
1802 .num_frags = num_frags,
1803 };
1804
1805 unsigned long irqflags;
1806
Chris Metcalfd91c6412011-03-01 12:49:53 -05001807 lepp_queue_t *eq = priv->eq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001808
Chris Metcalfd91c6412011-03-01 12:49:53 -05001809 struct sk_buff *olds[8];
1810 unsigned int wanted = 8;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001811 unsigned int i, nolds = 0;
1812
1813 unsigned int cmd_head, cmd_tail, cmd_next;
1814 unsigned int comp_tail;
1815
Chris Metcalfe5a06932010-11-01 17:00:37 -04001816
1817 /* Paranoia. */
1818 BUG_ON(skb->protocol != htons(ETH_P_IP));
1819 BUG_ON(ih->protocol != IPPROTO_TCP);
1820 BUG_ON(skb->ip_summed != CHECKSUM_PARTIAL);
1821 BUG_ON(num_frags > LEPP_MAX_FRAGS);
1822 /*--BUG_ON(num_segs != (d_len + (p_len - 1)) / p_len); */
1823 BUG_ON(num_segs <= 1);
1824
1825
1826 /* Finish preparing the command. */
1827
1828 /* Copy the command header. */
1829 *cmd = cmd_init;
1830
1831 /* Copy the "header". */
1832 memcpy(&cmd->frags[num_frags], data, sh_len);
1833
1834
1835 /* Prefetch and wait, to minimize time spent holding the spinlock. */
1836 prefetch_L1(&eq->comp_tail);
1837 prefetch_L1(&eq->cmd_tail);
1838 mb();
1839
1840
1841 /* Enqueue the command. */
1842
Chris Metcalfd91c6412011-03-01 12:49:53 -05001843 spin_lock_irqsave(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001844
Chris Metcalf92795672012-03-30 19:23:35 -04001845 /* Handle completions if needed to make room. */
1846 /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001847 if (lepp_num_free_comp_slots(eq) == 0) {
1848 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
1849 if (nolds == 0) {
1850busy:
1851 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
1852 return NETDEV_TX_BUSY;
1853 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001854 }
1855
1856 cmd_head = eq->cmd_head;
1857 cmd_tail = eq->cmd_tail;
1858
Chris Metcalfe5a06932010-11-01 17:00:37 -04001859 /* Prepare to advance, detecting full queue. */
Chris Metcalf92795672012-03-30 19:23:35 -04001860 /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001861 cmd_next = cmd_tail + cmd_size;
1862 if (cmd_tail < cmd_head && cmd_next >= cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001863 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001864 if (cmd_next > LEPP_CMD_LIMIT) {
1865 cmd_next = 0;
1866 if (cmd_next == cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001867 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001868 }
1869
1870 /* Copy the command. */
1871 memcpy(&eq->cmds[cmd_tail], cmd, cmd_size);
1872
1873 /* Advance. */
1874 cmd_tail = cmd_next;
1875
1876 /* Record "skb" for eventual freeing. */
1877 comp_tail = eq->comp_tail;
1878 eq->comps[comp_tail] = skb;
1879 LEPP_QINC(comp_tail);
1880 eq->comp_tail = comp_tail;
1881
1882 /* Flush before allowing LEPP to handle the command. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001883 /* ISSUE: Is this the optimal location for the flush? */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001884 __insn_mf();
1885
1886 eq->cmd_tail = cmd_tail;
1887
Chris Metcalfd91c6412011-03-01 12:49:53 -05001888 /* NOTE: Using "4" here is more efficient than "0" or "2", */
1889 /* and, strangely, more efficient than pre-checking the number */
1890 /* of available completions, and comparing it to 4. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001891 if (nolds == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001892 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
1893
1894 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001895
1896 /* Handle completions. */
1897 for (i = 0; i < nolds; i++)
1898 kfree_skb(olds[i]);
1899
1900 /* Update stats. */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04001901 u64_stats_update_begin(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001902 stats->tx_packets += num_segs;
1903 stats->tx_bytes += (num_segs * sh_len) + d_len;
Chris Metcalfd68e2d32013-07-25 12:41:15 -04001904 u64_stats_update_end(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001905
1906 /* Make sure the egress timer is scheduled. */
1907 tile_net_schedule_egress_timer(info);
1908
1909 return NETDEV_TX_OK;
1910}
1911
1912
1913/*
1914 * Transmit a packet (called by the kernel via "hard_start_xmit" hook).
1915 */
1916static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
1917{
1918 struct tile_net_priv *priv = netdev_priv(dev);
1919 int my_cpu = smp_processor_id();
1920 struct tile_net_cpu *info = priv->cpu[my_cpu];
1921 struct tile_net_stats_t *stats = &info->stats;
1922
1923 unsigned long irqflags;
1924
1925 struct skb_shared_info *sh = skb_shinfo(skb);
1926
1927 unsigned int len = skb->len;
1928 unsigned char *data = skb->data;
1929
Shan Wei96339d62011-04-22 19:07:41 +08001930 unsigned int csum_start = skb_checksum_start_offset(skb);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001931
Chris Metcalf815d3ba2013-08-01 11:36:42 -04001932 lepp_frag_t frags[1 + MAX_SKB_FRAGS];
Chris Metcalfe5a06932010-11-01 17:00:37 -04001933
1934 unsigned int num_frags;
1935
Chris Metcalfd91c6412011-03-01 12:49:53 -05001936 lepp_queue_t *eq = priv->eq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001937
Chris Metcalfd91c6412011-03-01 12:49:53 -05001938 struct sk_buff *olds[8];
1939 unsigned int wanted = 8;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001940 unsigned int i, nolds = 0;
1941
1942 unsigned int cmd_size = sizeof(lepp_cmd_t);
1943
1944 unsigned int cmd_head, cmd_tail, cmd_next;
1945 unsigned int comp_tail;
1946
Chris Metcalf815d3ba2013-08-01 11:36:42 -04001947 lepp_cmd_t cmds[1 + MAX_SKB_FRAGS];
Chris Metcalfe5a06932010-11-01 17:00:37 -04001948
Chris Metcalfe5a06932010-11-01 17:00:37 -04001949
1950 /*
1951 * This is paranoia, since we think that if the link doesn't come
1952 * up, telling Linux we have no carrier will keep it from trying
1953 * to transmit. If it does, though, we can't execute this routine,
1954 * since data structures we depend on aren't set up yet.
1955 */
1956 if (!info->registered)
1957 return NETDEV_TX_BUSY;
1958
1959
1960 /* Save the timestamp. */
1961 dev->trans_start = jiffies;
1962
1963
1964#ifdef TILE_NET_PARANOIA
1965#if CHIP_HAS_CBOX_HOME_MAP()
1966 if (hash_default) {
1967 HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)data);
1968 if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001969 panic("Non-HFH egress buffer! VA=%p Mode=%d PTE=%llx",
1970 data, hv_pte_get_mode(pte), hv_pte_val(pte));
Chris Metcalfe5a06932010-11-01 17:00:37 -04001971 }
1972#endif
1973#endif
1974
1975
1976#ifdef TILE_NET_DUMP_PACKETS
1977 /* ISSUE: Does not dump the "frags". */
1978 dump_packet(data, skb_headlen(skb), "tx");
1979#endif /* TILE_NET_DUMP_PACKETS */
1980
1981
1982 if (sh->gso_size != 0)
1983 return tile_net_tx_tso(skb, dev);
1984
1985
1986 /* Prepare the commands. */
1987
1988 num_frags = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
1989
1990 for (i = 0; i < num_frags; i++) {
1991
1992 bool final = (i == num_frags - 1);
1993
1994 lepp_cmd_t cmd = {
1995 .cpa_lo = frags[i].cpa_lo,
1996 .cpa_hi = frags[i].cpa_hi,
1997 .length = frags[i].length,
1998 .hash_for_home = frags[i].hash_for_home,
1999 .send_completion = final,
2000 .end_of_packet = final
2001 };
2002
2003 if (i == 0 && skb->ip_summed == CHECKSUM_PARTIAL) {
2004 cmd.compute_checksum = 1;
2005 cmd.checksum_data.bits.start_byte = csum_start;
2006 cmd.checksum_data.bits.count = len - csum_start;
2007 cmd.checksum_data.bits.destination_byte =
2008 csum_start + skb->csum_offset;
2009 }
2010
2011 cmds[i] = cmd;
2012 }
2013
2014
2015 /* Prefetch and wait, to minimize time spent holding the spinlock. */
2016 prefetch_L1(&eq->comp_tail);
2017 prefetch_L1(&eq->cmd_tail);
2018 mb();
2019
2020
2021 /* Enqueue the commands. */
2022
Chris Metcalfd91c6412011-03-01 12:49:53 -05002023 spin_lock_irqsave(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002024
Chris Metcalf92795672012-03-30 19:23:35 -04002025 /* Handle completions if needed to make room. */
2026 /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05002027 if (lepp_num_free_comp_slots(eq) == 0) {
2028 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
2029 if (nolds == 0) {
2030busy:
2031 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
2032 return NETDEV_TX_BUSY;
2033 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04002034 }
2035
2036 cmd_head = eq->cmd_head;
2037 cmd_tail = eq->cmd_tail;
2038
Chris Metcalfe5a06932010-11-01 17:00:37 -04002039 /* Copy the commands, or fail. */
Chris Metcalf92795672012-03-30 19:23:35 -04002040 /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002041 for (i = 0; i < num_frags; i++) {
2042
2043 /* Prepare to advance, detecting full queue. */
2044 cmd_next = cmd_tail + cmd_size;
2045 if (cmd_tail < cmd_head && cmd_next >= cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002046 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002047 if (cmd_next > LEPP_CMD_LIMIT) {
2048 cmd_next = 0;
2049 if (cmd_next == cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002050 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002051 }
2052
2053 /* Copy the command. */
2054 *(lepp_cmd_t *)&eq->cmds[cmd_tail] = cmds[i];
2055
2056 /* Advance. */
2057 cmd_tail = cmd_next;
2058 }
2059
2060 /* Record "skb" for eventual freeing. */
2061 comp_tail = eq->comp_tail;
2062 eq->comps[comp_tail] = skb;
2063 LEPP_QINC(comp_tail);
2064 eq->comp_tail = comp_tail;
2065
2066 /* Flush before allowing LEPP to handle the command. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05002067 /* ISSUE: Is this the optimal location for the flush? */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002068 __insn_mf();
2069
2070 eq->cmd_tail = cmd_tail;
2071
Chris Metcalfd91c6412011-03-01 12:49:53 -05002072 /* NOTE: Using "4" here is more efficient than "0" or "2", */
2073 /* and, strangely, more efficient than pre-checking the number */
2074 /* of available completions, and comparing it to 4. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002075 if (nolds == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002076 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
2077
2078 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002079
2080 /* Handle completions. */
2081 for (i = 0; i < nolds; i++)
2082 kfree_skb(olds[i]);
2083
2084 /* HACK: Track "expanded" size for short packets (e.g. 42 < 60). */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002085 u64_stats_update_begin(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002086 stats->tx_packets++;
2087 stats->tx_bytes += ((len >= ETH_ZLEN) ? len : ETH_ZLEN);
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002088 u64_stats_update_end(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002089
2090 /* Make sure the egress timer is scheduled. */
2091 tile_net_schedule_egress_timer(info);
2092
2093 return NETDEV_TX_OK;
2094}
2095
2096
2097/*
2098 * Deal with a transmit timeout.
2099 */
2100static void tile_net_tx_timeout(struct net_device *dev)
2101{
2102 PDEBUG("tile_net_tx_timeout()\n");
2103 PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
2104 jiffies - dev->trans_start);
2105
2106 /* XXX: ISSUE: This doesn't seem useful for us. */
2107 netif_wake_queue(dev);
2108}
2109
2110
2111/*
2112 * Ioctl commands.
2113 */
2114static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2115{
2116 return -EOPNOTSUPP;
2117}
2118
2119
2120/*
2121 * Get System Network Statistics.
2122 *
2123 * Returns the address of the device statistics structure.
2124 */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002125static struct rtnl_link_stats64 *tile_net_get_stats64(struct net_device *dev,
2126 struct rtnl_link_stats64 *stats)
Chris Metcalfe5a06932010-11-01 17:00:37 -04002127{
2128 struct tile_net_priv *priv = netdev_priv(dev);
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002129 u64 rx_packets = 0, tx_packets = 0;
2130 u64 rx_bytes = 0, tx_bytes = 0;
2131 u64 rx_errors = 0, rx_dropped = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002132 int i;
2133
2134 for_each_online_cpu(i) {
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002135 struct tile_net_stats_t *cpu_stats;
2136 u64 trx_packets, ttx_packets, trx_bytes, ttx_bytes;
2137 u64 trx_errors, trx_dropped;
2138 unsigned int start;
2139
2140 if (priv->cpu[i] == NULL)
2141 continue;
2142 cpu_stats = &priv->cpu[i]->stats;
2143
2144 do {
2145 start = u64_stats_fetch_begin_bh(&cpu_stats->syncp);
2146 trx_packets = cpu_stats->rx_packets;
2147 ttx_packets = cpu_stats->tx_packets;
2148 trx_bytes = cpu_stats->rx_bytes;
2149 ttx_bytes = cpu_stats->tx_bytes;
2150 trx_errors = cpu_stats->rx_errors;
2151 trx_dropped = cpu_stats->rx_dropped;
2152 } while (u64_stats_fetch_retry_bh(&cpu_stats->syncp, start));
2153
2154 rx_packets += trx_packets;
2155 tx_packets += ttx_packets;
2156 rx_bytes += trx_bytes;
2157 tx_bytes += ttx_bytes;
2158 rx_errors += trx_errors;
2159 rx_dropped += trx_dropped;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002160 }
2161
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002162 stats->rx_packets = rx_packets;
2163 stats->tx_packets = tx_packets;
2164 stats->rx_bytes = rx_bytes;
2165 stats->tx_bytes = tx_bytes;
2166 stats->rx_errors = rx_errors;
2167 stats->rx_dropped = rx_dropped;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002168
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002169 return stats;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002170}
2171
2172
2173/*
2174 * Change the "mtu".
2175 *
2176 * The "change_mtu" method is usually not needed.
2177 * If you need it, it must be like this.
2178 */
2179static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
2180{
2181 PDEBUG("tile_net_change_mtu()\n");
2182
2183 /* Check ranges. */
2184 if ((new_mtu < 68) || (new_mtu > 1500))
2185 return -EINVAL;
2186
2187 /* Accept the value. */
2188 dev->mtu = new_mtu;
2189
2190 return 0;
2191}
2192
2193
2194/*
2195 * Change the Ethernet Address of the NIC.
2196 *
2197 * The hypervisor driver does not support changing MAC address. However,
2198 * the IPP does not do anything with the MAC address, so the address which
2199 * gets used on outgoing packets, and which is accepted on incoming packets,
2200 * is completely up to the NetIO program or kernel driver which is actually
2201 * handling them.
2202 *
2203 * Returns 0 on success, negative on failure.
2204 */
2205static int tile_net_set_mac_address(struct net_device *dev, void *p)
2206{
2207 struct sockaddr *addr = p;
2208
2209 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002210 return -EADDRNOTAVAIL;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002211
2212 /* ISSUE: Note that "dev_addr" is now a pointer. */
2213 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2214
2215 return 0;
2216}
2217
2218
2219/*
2220 * Obtain the MAC address from the hypervisor.
2221 * This must be done before opening the device.
2222 */
2223static int tile_net_get_mac(struct net_device *dev)
2224{
2225 struct tile_net_priv *priv = netdev_priv(dev);
2226
2227 char hv_dev_name[32];
2228 int len;
2229
2230 __netio_getset_offset_t offset = { .word = NETIO_IPP_PARAM_OFF };
2231
2232 int ret;
2233
2234 /* For example, "xgbe0". */
2235 strcpy(hv_dev_name, dev->name);
2236 len = strlen(hv_dev_name);
2237
2238 /* For example, "xgbe/0". */
2239 hv_dev_name[len] = hv_dev_name[len - 1];
2240 hv_dev_name[len - 1] = '/';
2241 len++;
2242
2243 /* For example, "xgbe/0/native_hash". */
2244 strcpy(hv_dev_name + len, hash_default ? "/native_hash" : "/native");
2245
2246 /* Get the hypervisor handle for this device. */
2247 priv->hv_devhdl = hv_dev_open((HV_VirtAddr)hv_dev_name, 0);
2248 PDEBUG("hv_dev_open(%s) returned %d %p\n",
2249 hv_dev_name, priv->hv_devhdl, &priv->hv_devhdl);
2250 if (priv->hv_devhdl < 0) {
2251 if (priv->hv_devhdl == HV_ENODEV)
2252 printk(KERN_DEBUG "Ignoring unconfigured device %s\n",
2253 hv_dev_name);
2254 else
2255 printk(KERN_DEBUG "hv_dev_open(%s) returned %d\n",
2256 hv_dev_name, priv->hv_devhdl);
2257 return -1;
2258 }
2259
2260 /*
2261 * Read the hardware address from the hypervisor.
2262 * ISSUE: Note that "dev_addr" is now a pointer.
2263 */
2264 offset.bits.class = NETIO_PARAM;
2265 offset.bits.addr = NETIO_PARAM_MAC;
2266 ret = hv_dev_pread(priv->hv_devhdl, 0,
2267 (HV_VirtAddr)dev->dev_addr, dev->addr_len,
2268 offset.word);
2269 PDEBUG("hv_dev_pread(NETIO_PARAM_MAC) returned %d\n", ret);
2270 if (ret <= 0) {
2271 printk(KERN_DEBUG "hv_dev_pread(NETIO_PARAM_MAC) %s failed\n",
2272 dev->name);
2273 /*
2274 * Since the device is configured by the hypervisor but we
2275 * can't get its MAC address, we are most likely running
2276 * the simulator, so let's generate a random MAC address.
2277 */
Danny Kukawka7ce5d222012-02-15 06:45:40 +00002278 eth_hw_addr_random(dev);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002279 }
2280
2281 return 0;
2282}
2283
Chris Metcalf92795672012-03-30 19:23:35 -04002284
2285#ifdef CONFIG_NET_POLL_CONTROLLER
2286/*
2287 * Polling 'interrupt' - used by things like netconsole to send skbs
2288 * without having to re-enable interrupts. It's not called while
2289 * the interrupt routine is executing.
2290 */
2291static void tile_net_netpoll(struct net_device *dev)
2292{
2293 struct tile_net_priv *priv = netdev_priv(dev);
2294 disable_percpu_irq(priv->intr_id);
2295 tile_net_handle_ingress_interrupt(priv->intr_id, dev);
2296 enable_percpu_irq(priv->intr_id, 0);
2297}
2298#endif
2299
2300
stephen hemmingere5686ad2012-01-05 19:10:25 +00002301static const struct net_device_ops tile_net_ops = {
Chris Metcalfe5a06932010-11-01 17:00:37 -04002302 .ndo_open = tile_net_open,
2303 .ndo_stop = tile_net_stop,
2304 .ndo_start_xmit = tile_net_tx,
2305 .ndo_do_ioctl = tile_net_ioctl,
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002306 .ndo_get_stats64 = tile_net_get_stats64,
Chris Metcalfe5a06932010-11-01 17:00:37 -04002307 .ndo_change_mtu = tile_net_change_mtu,
2308 .ndo_tx_timeout = tile_net_tx_timeout,
Chris Metcalf92795672012-03-30 19:23:35 -04002309 .ndo_set_mac_address = tile_net_set_mac_address,
2310#ifdef CONFIG_NET_POLL_CONTROLLER
2311 .ndo_poll_controller = tile_net_netpoll,
2312#endif
Chris Metcalfe5a06932010-11-01 17:00:37 -04002313};
2314
2315
2316/*
2317 * The setup function.
2318 *
2319 * This uses ether_setup() to assign various fields in dev, including
2320 * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
2321 */
2322static void tile_net_setup(struct net_device *dev)
2323{
Chris Metcalfa8eaed52013-08-01 11:36:42 -04002324 netdev_features_t features = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002325
2326 ether_setup(dev);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002327 dev->netdev_ops = &tile_net_ops;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002328 dev->watchdog_timeo = TILE_NET_TIMEOUT;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002329 dev->tx_queue_len = TILE_NET_TX_QUEUE_LEN;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002330 dev->mtu = TILE_NET_MTU;
Chris Metcalfa8eaed52013-08-01 11:36:42 -04002331
2332 features |= NETIF_F_LLTX;
2333 features |= NETIF_F_HW_CSUM;
2334 features |= NETIF_F_SG;
Chris Metcalf815d3ba2013-08-01 11:36:42 -04002335
2336 /* We support TSO iff the HV supports sufficient frags. */
2337 if (LEPP_MAX_FRAGS >= 1 + MAX_SKB_FRAGS)
2338 features |= NETIF_F_TSO;
Chris Metcalfa8eaed52013-08-01 11:36:42 -04002339
2340 /* We can't support HIGHDMA without hash_default, since we need
2341 * to be able to finv() with a VA if we don't have hash_default.
2342 */
2343 if (hash_default)
2344 features |= NETIF_F_HIGHDMA;
2345
2346 dev->hw_features |= features;
2347 dev->vlan_features |= features;
2348 dev->features |= features;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002349}
2350
2351
2352/*
2353 * Allocate the device structure, register the device, and obtain the
2354 * MAC address from the hypervisor.
2355 */
2356static struct net_device *tile_net_dev_init(const char *name)
2357{
2358 int ret;
2359 struct net_device *dev;
2360 struct tile_net_priv *priv;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002361
2362 /*
2363 * Allocate the device structure. This allocates "priv", calls
2364 * tile_net_setup(), and saves "name". Normally, "name" is a
2365 * template, instantiated by register_netdev(), but not for us.
2366 */
2367 dev = alloc_netdev(sizeof(*priv), name, tile_net_setup);
2368 if (!dev) {
2369 pr_err("alloc_netdev(%s) failed\n", name);
2370 return NULL;
2371 }
2372
2373 priv = netdev_priv(dev);
2374
2375 /* Initialize "priv". */
2376
2377 memset(priv, 0, sizeof(*priv));
2378
2379 /* Save "dev" for "tile_net_open_retry()". */
2380 priv->dev = dev;
2381
2382 INIT_DELAYED_WORK(&priv->retry_work, tile_net_open_retry);
2383
Chris Metcalfd91c6412011-03-01 12:49:53 -05002384 spin_lock_init(&priv->eq_lock);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002385
Chris Metcalfd91c6412011-03-01 12:49:53 -05002386 /* Allocate "eq". */
2387 priv->eq_pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, EQ_ORDER);
2388 if (!priv->eq_pages) {
Chris Metcalfe5a06932010-11-01 17:00:37 -04002389 free_netdev(dev);
2390 return NULL;
2391 }
Chris Metcalfd91c6412011-03-01 12:49:53 -05002392 priv->eq = page_address(priv->eq_pages);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002393
2394 /* Register the network device. */
2395 ret = register_netdev(dev);
2396 if (ret) {
2397 pr_err("register_netdev %s failed %d\n", dev->name, ret);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002398 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002399 free_netdev(dev);
2400 return NULL;
2401 }
2402
2403 /* Get the MAC address. */
2404 ret = tile_net_get_mac(dev);
2405 if (ret < 0) {
2406 unregister_netdev(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002407 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002408 free_netdev(dev);
2409 return NULL;
2410 }
2411
2412 return dev;
2413}
2414
2415
2416/*
2417 * Module cleanup.
Chris Metcalfd91c6412011-03-01 12:49:53 -05002418 *
2419 * FIXME: If compiled as a module, this module cannot be "unloaded",
2420 * because the "ingress interrupt handler" is registered permanently.
Chris Metcalfe5a06932010-11-01 17:00:37 -04002421 */
2422static void tile_net_cleanup(void)
2423{
2424 int i;
2425
2426 for (i = 0; i < TILE_NET_DEVS; i++) {
2427 if (tile_net_devs[i]) {
2428 struct net_device *dev = tile_net_devs[i];
2429 struct tile_net_priv *priv = netdev_priv(dev);
2430 unregister_netdev(dev);
Chris Metcalfd07bd862011-05-02 16:36:48 -04002431 finv_buffer_remote(priv->eq, EQ_SIZE, 0);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002432 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002433 free_netdev(dev);
2434 }
2435 }
2436}
2437
2438
2439/*
2440 * Module initialization.
2441 */
2442static int tile_net_init_module(void)
2443{
Chris Metcalf92795672012-03-30 19:23:35 -04002444 pr_info("Tilera Network Driver\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04002445
2446 tile_net_devs[0] = tile_net_dev_init("xgbe0");
2447 tile_net_devs[1] = tile_net_dev_init("xgbe1");
2448 tile_net_devs[2] = tile_net_dev_init("gbe0");
2449 tile_net_devs[3] = tile_net_dev_init("gbe1");
2450
2451 return 0;
2452}
2453
2454
Chris Metcalfd91c6412011-03-01 12:49:53 -05002455module_init(tile_net_init_module);
2456module_exit(tile_net_cleanup);
2457
2458
Chris Metcalfe5a06932010-11-01 17:00:37 -04002459#ifndef MODULE
Chris Metcalfd91c6412011-03-01 12:49:53 -05002460
Chris Metcalfe5a06932010-11-01 17:00:37 -04002461/*
2462 * The "network_cpus" boot argument specifies the cpus that are dedicated
2463 * to handle ingress packets.
2464 *
2465 * The parameter should be in the form "network_cpus=m-n[,x-y]", where
2466 * m, n, x, y are integer numbers that represent the cpus that can be
2467 * neither a dedicated cpu nor a dataplane cpu.
2468 */
2469static int __init network_cpus_setup(char *str)
2470{
2471 int rc = cpulist_parse_crop(str, &network_cpus_map);
2472 if (rc != 0) {
2473 pr_warning("network_cpus=%s: malformed cpu list\n",
2474 str);
2475 } else {
2476
2477 /* Remove dedicated cpus. */
2478 cpumask_and(&network_cpus_map, &network_cpus_map,
2479 cpu_possible_mask);
2480
2481
2482 if (cpumask_empty(&network_cpus_map)) {
2483 pr_warning("Ignoring network_cpus='%s'.\n",
2484 str);
2485 } else {
2486 char buf[1024];
2487 cpulist_scnprintf(buf, sizeof(buf), &network_cpus_map);
2488 pr_info("Linux network CPUs: %s\n", buf);
2489 network_cpus_used = true;
2490 }
2491 }
2492
2493 return 0;
2494}
2495__setup("network_cpus=", network_cpus_setup);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002496
Chris Metcalfe5a06932010-11-01 17:00:37 -04002497#endif