blob: 34b43b400ab312ed07137dfb564fee3581072907 [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);
775
Chris Metcalfd91c6412011-03-01 12:49:53 -0500776 /* Extract the packet size. FIXME: Shouldn't the second line */
777 /* get subtracted? Mostly moot, since it should be "zero". */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400778 unsigned long len =
779 (NETIO_PKT_CUSTOM_LENGTH(pkt) +
780 NET_IP_ALIGN - NETIO_PACKET_PADDING);
781
782 /* Extract the "linux_buffer_t". */
783 unsigned int buffer = pkt->__packet.word;
784
785 /* Extract "small" (vs "large"). */
786 bool small = ((buffer & 1) != 0);
787
788 /* Convert "linux_buffer_t" to "va". */
789 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
790
791 /* Extract the packet data pointer. */
792 /* Compare to "NETIO_PKT_CUSTOM_DATA(pkt)". */
793 unsigned char *buf = va + NET_IP_ALIGN;
794
Chris Metcalfe5a06932010-11-01 17:00:37 -0400795 /* Invalidate the packet buffer. */
796 if (!hash_default)
797 __inv_buffer(buf, len);
798
799 /* ISSUE: Is this needed? */
800 dev->last_rx = jiffies;
801
802#ifdef TILE_NET_DUMP_PACKETS
803 dump_packet(buf, len, "rx");
804#endif /* TILE_NET_DUMP_PACKETS */
805
806#ifdef TILE_NET_VERIFY_INGRESS
807 if (!NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt) &&
808 NETIO_PKT_L4_CSUM_CALCULATED_M(metadata, pkt)) {
Chris Metcalfd91c6412011-03-01 12:49:53 -0500809 /* Bug 6624: Includes UDP packets with a "zero" checksum. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400810 pr_warning("Bad L4 checksum on %d byte packet.\n", len);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400811 }
812 if (!NETIO_PKT_L3_CSUM_CORRECT_M(metadata, pkt) &&
813 NETIO_PKT_L3_CSUM_CALCULATED_M(metadata, pkt)) {
814 dump_packet(buf, len, "rx");
815 panic("Bad L3 checksum.");
816 }
817 switch (NETIO_PKT_STATUS_M(metadata, pkt)) {
818 case NETIO_PKT_STATUS_OVERSIZE:
819 if (len >= 64) {
820 dump_packet(buf, len, "rx");
821 panic("Unexpected OVERSIZE.");
822 }
823 break;
824 case NETIO_PKT_STATUS_BAD:
Chris Metcalfd91c6412011-03-01 12:49:53 -0500825 pr_warning("Unexpected BAD %ld byte packet.\n", len);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400826 }
827#endif
828
829 filter = 0;
830
Chris Metcalfd91c6412011-03-01 12:49:53 -0500831 /* ISSUE: Filter TCP packets with "bad" checksums? */
832
Chris Metcalfe5a06932010-11-01 17:00:37 -0400833 if (!(dev->flags & IFF_UP)) {
834 /* Filter packets received before we're up. */
835 filter = 1;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500836 } else if (NETIO_PKT_STATUS_M(metadata, pkt) == NETIO_PKT_STATUS_BAD) {
837 /* Filter "truncated" packets. */
838 filter = 1;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400839 } else if (!(dev->flags & IFF_PROMISC)) {
Chris Metcalfd91c6412011-03-01 12:49:53 -0500840 /* FIXME: Implement HW multicast filter. */
841 if (!is_multicast_ether_addr(buf)) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400842 /* Filter packets not for our address. */
843 const u8 *mine = dev->dev_addr;
Joe Perches2e42e472012-05-09 17:17:46 +0000844 filter = !ether_addr_equal(mine, buf);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400845 }
846 }
847
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400848 u64_stats_update_begin(&stats->syncp);
849
Chris Metcalfe5a06932010-11-01 17:00:37 -0400850 if (filter) {
851
852 /* ISSUE: Update "drop" statistics? */
853
854 tile_net_provide_linux_buffer(info, va, small);
855
856 } else {
857
858 /* Acquire the associated "skb". */
859 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
860 struct sk_buff *skb = *skb_ptr;
861
862 /* Paranoia. */
863 if (skb->data != buf)
864 panic("Corrupt linux buffer from LIPP! "
865 "VA=%p, skb=%p, skb->data=%p\n",
866 va, skb, skb->data);
867
868 /* Encode the actual packet length. */
869 skb_put(skb, len);
870
871 /* NOTE: This call also sets "skb->dev = dev". */
872 skb->protocol = eth_type_trans(skb, dev);
873
Chris Metcalfd91c6412011-03-01 12:49:53 -0500874 /* Avoid recomputing "good" TCP/UDP checksums. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400875 if (NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt))
876 skb->ip_summed = CHECKSUM_UNNECESSARY;
877
878 netif_receive_skb(skb);
879
880 stats->rx_packets++;
881 stats->rx_bytes += len;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400882 }
883
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400884 u64_stats_update_end(&stats->syncp);
885
Chris Metcalf92795672012-03-30 19:23:35 -0400886 /* ISSUE: It would be nice to defer this until the packet has */
887 /* actually been processed. */
888 tile_net_return_credit(info);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400889
890 /* Consume this packet. */
891 qup->__packet_receive_read = index2;
892
893 return !filter;
894}
895
896
897/*
898 * Handle some packets for the given device on the current CPU.
899 *
Chris Metcalfd91c6412011-03-01 12:49:53 -0500900 * If "tile_net_stop()" is called on some other tile while this
901 * function is running, we will return, hopefully before that
902 * other tile asks us to call "napi_disable()".
903 *
904 * The "rotting packet" race condition occurs if a packet arrives
905 * during the extremely narrow window between the queue appearing to
906 * be empty, and the ingress interrupt being re-enabled. This happens
907 * a LOT under heavy network load.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400908 */
909static int tile_net_poll(struct napi_struct *napi, int budget)
910{
911 struct net_device *dev = napi->dev;
912 struct tile_net_priv *priv = netdev_priv(dev);
913 int my_cpu = smp_processor_id();
914 struct tile_net_cpu *info = priv->cpu[my_cpu];
915 struct tile_netio_queue *queue = &info->queue;
916 netio_queue_impl_t *qsp = queue->__system_part;
917 netio_queue_user_impl_t *qup = &queue->__user_part;
918
919 unsigned int work = 0;
920
Chris Metcalfd91c6412011-03-01 12:49:53 -0500921 while (priv->active) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400922 int index = qup->__packet_receive_read;
923 if (index == qsp->__packet_receive_queue.__packet_write)
924 break;
925
926 if (tile_net_poll_aux(info, index)) {
927 if (++work >= budget)
928 goto done;
929 }
930 }
931
932 napi_complete(&info->napi);
933
Chris Metcalfd91c6412011-03-01 12:49:53 -0500934 if (!priv->active)
935 goto done;
936
937 /* Re-enable the ingress interrupt. */
Chris Metcalf0c905472011-12-01 12:58:19 -0500938 enable_percpu_irq(priv->intr_id, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400939
Chris Metcalfd91c6412011-03-01 12:49:53 -0500940 /* HACK: Avoid the "rotting packet" problem (see above). */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400941 if (qup->__packet_receive_read !=
Chris Metcalfd91c6412011-03-01 12:49:53 -0500942 qsp->__packet_receive_queue.__packet_write) {
943 /* ISSUE: Sometimes this returns zero, presumably */
944 /* because an interrupt was handled for this tile. */
945 (void)napi_reschedule(&info->napi);
946 }
Chris Metcalfe5a06932010-11-01 17:00:37 -0400947
948done:
949
Chris Metcalfd91c6412011-03-01 12:49:53 -0500950 if (priv->active)
951 tile_net_provide_needed_buffers(info);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400952
953 return work;
954}
955
956
957/*
958 * Handle an ingress interrupt for the given device on the current cpu.
Chris Metcalfd91c6412011-03-01 12:49:53 -0500959 *
960 * ISSUE: Sometimes this gets called after "disable_percpu_irq()" has
961 * been called! This is probably due to "pending hypervisor downcalls".
962 *
963 * ISSUE: Is there any race condition between the "napi_schedule()" here
964 * and the "napi_complete()" call above?
Chris Metcalfe5a06932010-11-01 17:00:37 -0400965 */
966static irqreturn_t tile_net_handle_ingress_interrupt(int irq, void *dev_ptr)
967{
968 struct net_device *dev = (struct net_device *)dev_ptr;
969 struct tile_net_priv *priv = netdev_priv(dev);
970 int my_cpu = smp_processor_id();
971 struct tile_net_cpu *info = priv->cpu[my_cpu];
972
Chris Metcalfd91c6412011-03-01 12:49:53 -0500973 /* Disable the ingress interrupt. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400974 disable_percpu_irq(priv->intr_id);
975
Chris Metcalfd91c6412011-03-01 12:49:53 -0500976 /* Ignore unwanted interrupts. */
977 if (!priv->active)
978 return IRQ_HANDLED;
979
980 /* ISSUE: Sometimes "info->napi_enabled" is false here. */
981
Chris Metcalfe5a06932010-11-01 17:00:37 -0400982 napi_schedule(&info->napi);
983
984 return IRQ_HANDLED;
985}
986
987
988/*
989 * One time initialization per interface.
990 */
991static int tile_net_open_aux(struct net_device *dev)
992{
993 struct tile_net_priv *priv = netdev_priv(dev);
994
995 int ret;
996 int dummy;
997 unsigned int epp_lotar;
998
999 /*
1000 * Find out where EPP memory should be homed.
1001 */
1002 ret = hv_dev_pread(priv->hv_devhdl, 0,
1003 (HV_VirtAddr)&epp_lotar, sizeof(epp_lotar),
1004 NETIO_EPP_SHM_OFF);
1005 if (ret < 0) {
1006 pr_err("could not read epp_shm_queue lotar.\n");
1007 return -EIO;
1008 }
1009
1010 /*
1011 * Home the page on the EPP.
1012 */
1013 {
1014 int epp_home = hv_lotar_to_cpu(epp_lotar);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001015 homecache_change_page_home(priv->eq_pages, EQ_ORDER, epp_home);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001016 }
1017
1018 /*
1019 * Register the EPP shared memory queue.
1020 */
1021 {
1022 netio_ipp_address_t ea = {
1023 .va = 0,
Chris Metcalfd91c6412011-03-01 12:49:53 -05001024 .pa = __pa(priv->eq),
Chris Metcalfe5a06932010-11-01 17:00:37 -04001025 .pte = hv_pte(0),
Chris Metcalfd91c6412011-03-01 12:49:53 -05001026 .size = EQ_SIZE,
Chris Metcalfe5a06932010-11-01 17:00:37 -04001027 };
1028 ea.pte = hv_pte_set_lotar(ea.pte, epp_lotar);
1029 ea.pte = hv_pte_set_mode(ea.pte, HV_PTE_MODE_CACHE_TILE_L3);
1030 ret = hv_dev_pwrite(priv->hv_devhdl, 0,
1031 (HV_VirtAddr)&ea,
1032 sizeof(ea),
1033 NETIO_EPP_SHM_OFF);
1034 if (ret < 0)
1035 return -EIO;
1036 }
1037
1038 /*
1039 * Start LIPP/LEPP.
1040 */
1041 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1042 sizeof(dummy), NETIO_IPP_START_SHIM_OFF) < 0) {
1043 pr_warning("Failed to start LIPP/LEPP.\n");
1044 return -EIO;
1045 }
1046
1047 return 0;
1048}
1049
1050
1051/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001052 * Register with hypervisor on the current CPU.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001053 *
1054 * Strangely, this function does important things even if it "fails",
1055 * which is especially common if the link is not up yet. Hopefully
1056 * these things are all "harmless" if done twice!
1057 */
1058static void tile_net_register(void *dev_ptr)
1059{
1060 struct net_device *dev = (struct net_device *)dev_ptr;
1061 struct tile_net_priv *priv = netdev_priv(dev);
1062 int my_cpu = smp_processor_id();
1063 struct tile_net_cpu *info;
1064
1065 struct tile_netio_queue *queue;
1066
1067 /* Only network cpus can receive packets. */
1068 int queue_id =
1069 cpumask_test_cpu(my_cpu, &priv->network_cpus_map) ? 0 : 255;
1070
1071 netio_input_config_t config = {
1072 .flags = 0,
1073 .num_receive_packets = priv->network_cpus_credits,
1074 .queue_id = queue_id
1075 };
1076
1077 int ret = 0;
1078 netio_queue_impl_t *queuep;
1079
1080 PDEBUG("tile_net_register(queue_id %d)\n", queue_id);
1081
1082 if (!strcmp(dev->name, "xgbe0"))
1083 info = &__get_cpu_var(hv_xgbe0);
1084 else if (!strcmp(dev->name, "xgbe1"))
1085 info = &__get_cpu_var(hv_xgbe1);
1086 else if (!strcmp(dev->name, "gbe0"))
1087 info = &__get_cpu_var(hv_gbe0);
1088 else if (!strcmp(dev->name, "gbe1"))
1089 info = &__get_cpu_var(hv_gbe1);
1090 else
1091 BUG();
1092
1093 /* Initialize the egress timer. */
1094 init_timer(&info->egress_timer);
1095 info->egress_timer.data = (long)info;
1096 info->egress_timer.function = tile_net_handle_egress_timer;
1097
1098 priv->cpu[my_cpu] = info;
1099
1100 /*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001101 * Register ourselves with LIPP. This does a lot of stuff,
1102 * including invoking the LIPP registration code.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001103 */
1104 ret = hv_dev_pwrite(priv->hv_devhdl, 0,
1105 (HV_VirtAddr)&config,
1106 sizeof(netio_input_config_t),
1107 NETIO_IPP_INPUT_REGISTER_OFF);
1108 PDEBUG("hv_dev_pwrite(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
1109 ret);
1110 if (ret < 0) {
Chris Metcalfd91c6412011-03-01 12:49:53 -05001111 if (ret != NETIO_LINK_DOWN) {
1112 printk(KERN_DEBUG "hv_dev_pwrite "
1113 "NETIO_IPP_INPUT_REGISTER_OFF failure %d\n",
1114 ret);
1115 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001116 info->link_down = (ret == NETIO_LINK_DOWN);
1117 return;
1118 }
1119
1120 /*
1121 * Get the pointer to our queue's system part.
1122 */
1123
1124 ret = hv_dev_pread(priv->hv_devhdl, 0,
1125 (HV_VirtAddr)&queuep,
1126 sizeof(netio_queue_impl_t *),
1127 NETIO_IPP_INPUT_REGISTER_OFF);
1128 PDEBUG("hv_dev_pread(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
1129 ret);
1130 PDEBUG("queuep %p\n", queuep);
1131 if (ret <= 0) {
1132 /* ISSUE: Shouldn't this be a fatal error? */
1133 pr_err("hv_dev_pread NETIO_IPP_INPUT_REGISTER_OFF failure\n");
1134 return;
1135 }
1136
1137 queue = &info->queue;
1138
1139 queue->__system_part = queuep;
1140
1141 memset(&queue->__user_part, 0, sizeof(netio_queue_user_impl_t));
1142
1143 /* This is traditionally "config.num_receive_packets / 2". */
1144 queue->__user_part.__receive_credit_interval = 4;
1145 queue->__user_part.__receive_credit_remaining =
1146 queue->__user_part.__receive_credit_interval;
1147
1148 /*
1149 * Get a fastio index from the hypervisor.
1150 * ISSUE: Shouldn't this check the result?
1151 */
1152 ret = hv_dev_pread(priv->hv_devhdl, 0,
1153 (HV_VirtAddr)&queue->__user_part.__fastio_index,
1154 sizeof(queue->__user_part.__fastio_index),
1155 NETIO_IPP_GET_FASTIO_OFF);
1156 PDEBUG("hv_dev_pread(NETIO_IPP_GET_FASTIO_OFF) returned %d\n", ret);
1157
Chris Metcalfe5a06932010-11-01 17:00:37 -04001158 /* Now we are registered. */
1159 info->registered = true;
1160}
1161
1162
1163/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001164 * Deregister with hypervisor on the current CPU.
1165 *
1166 * This simply discards all our credits, so no more packets will be
1167 * delivered to this tile. There may still be packets in our queue.
1168 *
1169 * Also, disable the ingress interrupt.
1170 */
1171static void tile_net_deregister(void *dev_ptr)
1172{
1173 struct net_device *dev = (struct net_device *)dev_ptr;
1174 struct tile_net_priv *priv = netdev_priv(dev);
1175 int my_cpu = smp_processor_id();
1176 struct tile_net_cpu *info = priv->cpu[my_cpu];
1177
1178 /* Disable the ingress interrupt. */
1179 disable_percpu_irq(priv->intr_id);
1180
1181 /* Do nothing else if not registered. */
1182 if (info == NULL || !info->registered)
1183 return;
1184
1185 {
1186 struct tile_netio_queue *queue = &info->queue;
1187 netio_queue_user_impl_t *qup = &queue->__user_part;
1188
1189 /* Discard all our credits. */
1190 __netio_fastio_return_credits(qup->__fastio_index, -1);
1191 }
1192}
1193
1194
1195/*
1196 * Unregister with hypervisor on the current CPU.
1197 *
1198 * Also, disable the ingress interrupt.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001199 */
1200static void tile_net_unregister(void *dev_ptr)
1201{
1202 struct net_device *dev = (struct net_device *)dev_ptr;
1203 struct tile_net_priv *priv = netdev_priv(dev);
1204 int my_cpu = smp_processor_id();
1205 struct tile_net_cpu *info = priv->cpu[my_cpu];
1206
Chris Metcalfd91c6412011-03-01 12:49:53 -05001207 int ret;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001208 int dummy = 0;
1209
Chris Metcalfd91c6412011-03-01 12:49:53 -05001210 /* Disable the ingress interrupt. */
1211 disable_percpu_irq(priv->intr_id);
1212
1213 /* Do nothing else if not registered. */
1214 if (info == NULL || !info->registered)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001215 return;
1216
Chris Metcalfd91c6412011-03-01 12:49:53 -05001217 /* Unregister ourselves with LIPP/LEPP. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001218 ret = hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1219 sizeof(dummy), NETIO_IPP_INPUT_UNREGISTER_OFF);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001220 if (ret < 0)
1221 panic("Failed to unregister with LIPP/LEPP!\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001222
Chris Metcalfd91c6412011-03-01 12:49:53 -05001223 /* Discard all packets still in our NetIO queue. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001224 tile_net_discard_packets(dev);
1225
1226 /* Reset state. */
1227 info->num_needed_small_buffers = 0;
1228 info->num_needed_large_buffers = 0;
1229
1230 /* Cancel egress timer. */
1231 del_timer(&info->egress_timer);
1232 info->egress_timer_scheduled = false;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001233}
1234
1235
1236/*
1237 * Helper function for "tile_net_stop()".
1238 *
1239 * Also used to handle registration failure in "tile_net_open_inner()",
Chris Metcalfd91c6412011-03-01 12:49:53 -05001240 * when the various extra steps in "tile_net_stop()" are not necessary.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001241 */
1242static void tile_net_stop_aux(struct net_device *dev)
1243{
1244 struct tile_net_priv *priv = netdev_priv(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001245 int i;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001246
1247 int dummy = 0;
1248
Chris Metcalfd91c6412011-03-01 12:49:53 -05001249 /*
1250 * Unregister all tiles, so LIPP will stop delivering packets.
1251 * Also, delete all the "napi" objects (sequentially, to protect
1252 * "dev->napi_list").
1253 */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001254 on_each_cpu(tile_net_unregister, (void *)dev, 1);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001255 for_each_online_cpu(i) {
1256 struct tile_net_cpu *info = priv->cpu[i];
1257 if (info != NULL && info->registered) {
1258 netif_napi_del(&info->napi);
1259 info->registered = false;
1260 }
1261 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001262
1263 /* Stop LIPP/LEPP. */
1264 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1265 sizeof(dummy), NETIO_IPP_STOP_SHIM_OFF) < 0)
1266 panic("Failed to stop LIPP/LEPP!\n");
1267
Rusty Russell3db1cd52011-12-19 13:56:45 +00001268 priv->partly_opened = false;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001269}
1270
1271
1272/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001273 * Disable NAPI for the given device on the current cpu.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001274 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001275static void tile_net_stop_disable(void *dev_ptr)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001276{
1277 struct net_device *dev = (struct net_device *)dev_ptr;
1278 struct tile_net_priv *priv = netdev_priv(dev);
1279 int my_cpu = smp_processor_id();
1280 struct tile_net_cpu *info = priv->cpu[my_cpu];
1281
Chris Metcalfe5a06932010-11-01 17:00:37 -04001282 /* Disable NAPI if needed. */
1283 if (info != NULL && info->napi_enabled) {
1284 napi_disable(&info->napi);
1285 info->napi_enabled = false;
1286 }
1287}
1288
1289
1290/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001291 * Enable NAPI and the ingress interrupt for the given device
1292 * on the current cpu.
1293 *
1294 * ISSUE: Only do this for "network cpus"?
Chris Metcalfe5a06932010-11-01 17:00:37 -04001295 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001296static void tile_net_open_enable(void *dev_ptr)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001297{
1298 struct net_device *dev = (struct net_device *)dev_ptr;
1299 struct tile_net_priv *priv = netdev_priv(dev);
1300 int my_cpu = smp_processor_id();
1301 struct tile_net_cpu *info = priv->cpu[my_cpu];
1302
Chris Metcalfe5a06932010-11-01 17:00:37 -04001303 /* Enable NAPI. */
1304 napi_enable(&info->napi);
1305 info->napi_enabled = true;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001306
1307 /* Enable the ingress interrupt. */
Chris Metcalf0c905472011-12-01 12:58:19 -05001308 enable_percpu_irq(priv->intr_id, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001309}
1310
1311
1312/*
1313 * tile_net_open_inner does most of the work of bringing up the interface.
1314 * It's called from tile_net_open(), and also from tile_net_retry_open().
1315 * The return value is 0 if the interface was brought up, < 0 if
1316 * tile_net_open() should return the return value as an error, and > 0 if
1317 * tile_net_open() should return success and schedule a work item to
1318 * periodically retry the bringup.
1319 */
1320static int tile_net_open_inner(struct net_device *dev)
1321{
1322 struct tile_net_priv *priv = netdev_priv(dev);
1323 int my_cpu = smp_processor_id();
1324 struct tile_net_cpu *info;
1325 struct tile_netio_queue *queue;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001326 int result = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001327 int i;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001328 int dummy = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001329
1330 /*
1331 * First try to register just on the local CPU, and handle any
1332 * semi-expected "link down" failure specially. Note that we
1333 * do NOT call "tile_net_stop_aux()", unlike below.
1334 */
1335 tile_net_register(dev);
1336 info = priv->cpu[my_cpu];
1337 if (!info->registered) {
1338 if (info->link_down)
1339 return 1;
1340 return -EAGAIN;
1341 }
1342
1343 /*
1344 * Now register everywhere else. If any registration fails,
1345 * even for "link down" (which might not be possible), we
Chris Metcalfd91c6412011-03-01 12:49:53 -05001346 * clean up using "tile_net_stop_aux()". Also, add all the
1347 * "napi" objects (sequentially, to protect "dev->napi_list").
1348 * ISSUE: Only use "netif_napi_add()" for "network cpus"?
Chris Metcalfe5a06932010-11-01 17:00:37 -04001349 */
1350 smp_call_function(tile_net_register, (void *)dev, 1);
1351 for_each_online_cpu(i) {
Chris Metcalfd91c6412011-03-01 12:49:53 -05001352 struct tile_net_cpu *info = priv->cpu[i];
1353 if (info->registered)
1354 netif_napi_add(dev, &info->napi, tile_net_poll, 64);
1355 else
1356 result = -EAGAIN;
1357 }
1358 if (result != 0) {
1359 tile_net_stop_aux(dev);
1360 return result;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001361 }
1362
1363 queue = &info->queue;
1364
Chris Metcalfd91c6412011-03-01 12:49:53 -05001365 if (priv->intr_id == 0) {
1366 unsigned int irq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001367
Chris Metcalfd91c6412011-03-01 12:49:53 -05001368 /*
1369 * Acquire the irq allocated by the hypervisor. Every
1370 * queue gets the same irq. The "__intr_id" field is
1371 * "1 << irq", so we use "__ffs()" to extract "irq".
1372 */
1373 priv->intr_id = queue->__system_part->__intr_id;
1374 BUG_ON(priv->intr_id == 0);
1375 irq = __ffs(priv->intr_id);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001376
Chris Metcalfd91c6412011-03-01 12:49:53 -05001377 /*
1378 * Register the ingress interrupt handler for this
1379 * device, permanently.
1380 *
1381 * We used to call "free_irq()" in "tile_net_stop()",
1382 * and then re-register the handler here every time,
1383 * but that caused DNP errors in "handle_IRQ_event()"
1384 * because "desc->action" was NULL. See bug 9143.
1385 */
1386 tile_irq_activate(irq, TILE_IRQ_PERCPU);
1387 BUG_ON(request_irq(irq, tile_net_handle_ingress_interrupt,
1388 0, dev->name, (void *)dev) != 0);
1389 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001390
Chris Metcalfd91c6412011-03-01 12:49:53 -05001391 {
Chris Metcalfe5a06932010-11-01 17:00:37 -04001392 /* Allocate initial buffers. */
1393
1394 int max_buffers =
1395 priv->network_cpus_count * priv->network_cpus_credits;
1396
1397 info->num_needed_small_buffers =
1398 min(LIPP_SMALL_BUFFERS, max_buffers);
1399
1400 info->num_needed_large_buffers =
1401 min(LIPP_LARGE_BUFFERS, max_buffers);
1402
1403 tile_net_provide_needed_buffers(info);
1404
1405 if (info->num_needed_small_buffers != 0 ||
1406 info->num_needed_large_buffers != 0)
1407 panic("Insufficient memory for buffer stack!");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001408 }
1409
Chris Metcalfd91c6412011-03-01 12:49:53 -05001410 /* We are about to be active. */
1411 priv->active = true;
1412
1413 /* Make sure "active" is visible to all tiles. */
1414 mb();
1415
1416 /* On each tile, enable NAPI and the ingress interrupt. */
1417 on_each_cpu(tile_net_open_enable, (void *)dev, 1);
1418
1419 /* Start LIPP/LEPP and activate "ingress" at the shim. */
1420 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1421 sizeof(dummy), NETIO_IPP_INPUT_INIT_OFF) < 0)
1422 panic("Failed to activate the LIPP Shim!\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001423
1424 /* Start our transmit queue. */
1425 netif_start_queue(dev);
1426
1427 return 0;
1428}
1429
1430
1431/*
1432 * Called periodically to retry bringing up the NetIO interface,
1433 * if it doesn't come up cleanly during tile_net_open().
1434 */
1435static void tile_net_open_retry(struct work_struct *w)
1436{
1437 struct delayed_work *dw =
1438 container_of(w, struct delayed_work, work);
1439
1440 struct tile_net_priv *priv =
1441 container_of(dw, struct tile_net_priv, retry_work);
1442
1443 /*
1444 * Try to bring the NetIO interface up. If it fails, reschedule
1445 * ourselves to try again later; otherwise, tell Linux we now have
1446 * a working link. ISSUE: What if the return value is negative?
1447 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001448 if (tile_net_open_inner(priv->dev) != 0)
1449 schedule_delayed_work(&priv->retry_work,
1450 TILE_NET_RETRY_INTERVAL);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001451 else
1452 netif_carrier_on(priv->dev);
1453}
1454
1455
1456/*
1457 * Called when a network interface is made active.
1458 *
1459 * Returns 0 on success, negative value on failure.
1460 *
1461 * The open entry point is called when a network interface is made
1462 * active by the system (IFF_UP). At this point all resources needed
1463 * for transmit and receive operations are allocated, the interrupt
Chris Metcalfd91c6412011-03-01 12:49:53 -05001464 * handler is registered with the OS (if needed), the watchdog timer
1465 * is started, and the stack is notified that the interface is ready.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001466 *
1467 * If the actual link is not available yet, then we tell Linux that
1468 * we have no carrier, and we keep checking until the link comes up.
1469 */
1470static int tile_net_open(struct net_device *dev)
1471{
1472 int ret = 0;
1473 struct tile_net_priv *priv = netdev_priv(dev);
1474
1475 /*
1476 * We rely on priv->partly_opened to tell us if this is the
1477 * first time this interface is being brought up. If it is
1478 * set, the IPP was already initialized and should not be
1479 * initialized again.
1480 */
1481 if (!priv->partly_opened) {
1482
1483 int count;
1484 int credits;
1485
1486 /* Initialize LIPP/LEPP, and start the Shim. */
1487 ret = tile_net_open_aux(dev);
1488 if (ret < 0) {
1489 pr_err("tile_net_open_aux failed: %d\n", ret);
1490 return ret;
1491 }
1492
1493 /* Analyze the network cpus. */
1494
1495 if (network_cpus_used)
1496 cpumask_copy(&priv->network_cpus_map,
1497 &network_cpus_map);
1498 else
1499 cpumask_copy(&priv->network_cpus_map, cpu_online_mask);
1500
1501
1502 count = cpumask_weight(&priv->network_cpus_map);
1503
1504 /* Limit credits to available buffers, and apply min. */
1505 credits = max(16, (LIPP_LARGE_BUFFERS / count) & ~1);
1506
1507 /* Apply "GBE" max limit. */
1508 /* ISSUE: Use higher limit for XGBE? */
1509 credits = min(NETIO_MAX_RECEIVE_PKTS, credits);
1510
1511 priv->network_cpus_count = count;
1512 priv->network_cpus_credits = credits;
1513
1514#ifdef TILE_NET_DEBUG
1515 pr_info("Using %d network cpus, with %d credits each\n",
1516 priv->network_cpus_count, priv->network_cpus_credits);
1517#endif
1518
Rusty Russell3db1cd52011-12-19 13:56:45 +00001519 priv->partly_opened = true;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001520
1521 } else {
1522 /* FIXME: Is this possible? */
1523 /* printk("Already partly opened.\n"); */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001524 }
1525
1526 /*
1527 * Attempt to bring up the link.
1528 */
1529 ret = tile_net_open_inner(dev);
1530 if (ret <= 0) {
1531 if (ret == 0)
1532 netif_carrier_on(dev);
1533 return ret;
1534 }
1535
1536 /*
1537 * We were unable to bring up the NetIO interface, but we want to
1538 * try again in a little bit. Tell Linux that we have no carrier
1539 * so it doesn't try to use the interface before the link comes up
1540 * and then remember to try again later.
1541 */
1542 netif_carrier_off(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001543 schedule_delayed_work(&priv->retry_work, TILE_NET_RETRY_INTERVAL);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001544
1545 return 0;
1546}
1547
1548
Chris Metcalfd91c6412011-03-01 12:49:53 -05001549static int tile_net_drain_lipp_buffers(struct tile_net_priv *priv)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001550{
Chris Metcalfd91c6412011-03-01 12:49:53 -05001551 int n = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001552
Chris Metcalfd91c6412011-03-01 12:49:53 -05001553 /* Drain all the LIPP buffers. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001554 while (true) {
Chris Metcalf92795672012-03-30 19:23:35 -04001555 unsigned int buffer;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001556
1557 /* NOTE: This should never fail. */
1558 if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&buffer,
1559 sizeof(buffer), NETIO_IPP_DRAIN_OFF) < 0)
1560 break;
1561
1562 /* Stop when done. */
1563 if (buffer == 0)
1564 break;
1565
1566 {
1567 /* Convert "linux_buffer_t" to "va". */
1568 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
1569
1570 /* Acquire the associated "skb". */
1571 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
1572 struct sk_buff *skb = *skb_ptr;
1573
1574 kfree_skb(skb);
1575 }
Chris Metcalfd91c6412011-03-01 12:49:53 -05001576
1577 n++;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001578 }
1579
Chris Metcalfd91c6412011-03-01 12:49:53 -05001580 return n;
1581}
1582
1583
1584/*
1585 * Disables a network interface.
1586 *
1587 * Returns 0, this is not allowed to fail.
1588 *
1589 * The close entry point is called when an interface is de-activated
1590 * by the OS. The hardware is still under the drivers control, but
1591 * needs to be disabled. A global MAC reset is issued to stop the
1592 * hardware, and all transmit and receive resources are freed.
1593 *
1594 * ISSUE: How closely does "netif_running(dev)" mirror "priv->active"?
1595 *
1596 * Before we are called by "__dev_close()", "netif_running()" will
1597 * have been cleared, so no NEW calls to "tile_net_poll()" will be
1598 * made by "netpoll_poll_dev()".
1599 *
1600 * Often, this can cause some tiles to still have packets in their
1601 * queues, so we must call "tile_net_discard_packets()" later.
1602 *
1603 * Note that some other tile may still be INSIDE "tile_net_poll()",
1604 * and in fact, many will be, if there is heavy network load.
1605 *
1606 * Calling "on_each_cpu(tile_net_stop_disable, (void *)dev, 1)" when
1607 * any tile is still "napi_schedule()"'d will induce a horrible crash
1608 * when "msleep()" is called. This includes tiles which are inside
1609 * "tile_net_poll()" which have not yet called "napi_complete()".
1610 *
1611 * So, we must first try to wait long enough for other tiles to finish
1612 * with any current "tile_net_poll()" call, and, hopefully, to clear
1613 * the "scheduled" flag. ISSUE: It is unclear what happens to tiles
1614 * which have called "napi_schedule()" but which had not yet tried to
1615 * call "tile_net_poll()", or which exhausted their budget inside
1616 * "tile_net_poll()" just before this function was called.
1617 */
1618static int tile_net_stop(struct net_device *dev)
1619{
1620 struct tile_net_priv *priv = netdev_priv(dev);
1621
1622 PDEBUG("tile_net_stop()\n");
1623
1624 /* Start discarding packets. */
1625 priv->active = false;
1626
1627 /* Make sure "active" is visible to all tiles. */
1628 mb();
1629
1630 /*
1631 * On each tile, make sure no NEW packets get delivered, and
1632 * disable the ingress interrupt.
1633 *
1634 * Note that the ingress interrupt can fire AFTER this,
1635 * presumably due to packets which were recently delivered,
1636 * but it will have no effect.
1637 */
1638 on_each_cpu(tile_net_deregister, (void *)dev, 1);
1639
1640 /* Optimistically drain LIPP buffers. */
1641 (void)tile_net_drain_lipp_buffers(priv);
1642
1643 /* ISSUE: Only needed if not yet fully open. */
1644 cancel_delayed_work_sync(&priv->retry_work);
1645
1646 /* Can't transmit any more. */
1647 netif_stop_queue(dev);
1648
1649 /* Disable NAPI on each tile. */
1650 on_each_cpu(tile_net_stop_disable, (void *)dev, 1);
1651
1652 /*
1653 * Drain any remaining LIPP buffers. NOTE: This "printk()"
1654 * has never been observed, but in theory it could happen.
1655 */
1656 if (tile_net_drain_lipp_buffers(priv) != 0)
1657 printk("Had to drain some extra LIPP buffers!\n");
1658
Chris Metcalfe5a06932010-11-01 17:00:37 -04001659 /* Stop LIPP/LEPP. */
1660 tile_net_stop_aux(dev);
1661
Chris Metcalfe5a06932010-11-01 17:00:37 -04001662 /*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001663 * ISSUE: It appears that, in practice anyway, by the time we
1664 * get here, there are no pending completions, but just in case,
1665 * we free (all of) them anyway.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001666 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001667 while (tile_net_lepp_free_comps(dev, true))
1668 /* loop */;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001669
Chris Metcalfd07bd862011-05-02 16:36:48 -04001670 /* Wipe the EPP queue, and wait till the stores hit the EPP. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001671 memset(priv->eq, 0, sizeof(lepp_queue_t));
Chris Metcalfd07bd862011-05-02 16:36:48 -04001672 mb();
Chris Metcalfe5a06932010-11-01 17:00:37 -04001673
1674 return 0;
1675}
1676
1677
1678/*
1679 * Prepare the "frags" info for the resulting LEPP command.
1680 *
1681 * If needed, flush the memory used by the frags.
1682 */
1683static unsigned int tile_net_tx_frags(lepp_frag_t *frags,
1684 struct sk_buff *skb,
1685 void *b_data, unsigned int b_len)
1686{
1687 unsigned int i, n = 0;
1688
1689 struct skb_shared_info *sh = skb_shinfo(skb);
1690
1691 phys_addr_t cpa;
1692
1693 if (b_len != 0) {
1694
1695 if (!hash_default)
Chris Metcalf63b7ca62011-02-28 15:48:39 -05001696 finv_buffer_remote(b_data, b_len, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001697
1698 cpa = __pa(b_data);
1699 frags[n].cpa_lo = cpa;
1700 frags[n].cpa_hi = cpa >> 32;
1701 frags[n].length = b_len;
1702 frags[n].hash_for_home = hash_default;
1703 n++;
1704 }
1705
1706 for (i = 0; i < sh->nr_frags; i++) {
1707
1708 skb_frag_t *f = &sh->frags[i];
Chris Metcalf781a5e92011-12-01 12:56:03 -05001709 unsigned long pfn = page_to_pfn(skb_frag_page(f));
Chris Metcalfe5a06932010-11-01 17:00:37 -04001710
1711 /* FIXME: Compute "hash_for_home" properly. */
1712 /* ISSUE: The hypervisor checks CHIP_HAS_REV1_DMA_PACKETS(). */
1713 int hash_for_home = hash_default;
1714
1715 /* FIXME: Hmmm. */
1716 if (!hash_default) {
1717 void *va = pfn_to_kaddr(pfn) + f->page_offset;
Chris Metcalf781a5e92011-12-01 12:56:03 -05001718 BUG_ON(PageHighMem(skb_frag_page(f)));
Chris Metcalf92795672012-03-30 19:23:35 -04001719 finv_buffer_remote(va, skb_frag_size(f), 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001720 }
1721
1722 cpa = ((phys_addr_t)pfn << PAGE_SHIFT) + f->page_offset;
1723 frags[n].cpa_lo = cpa;
1724 frags[n].cpa_hi = cpa >> 32;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001725 frags[n].length = skb_frag_size(f);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001726 frags[n].hash_for_home = hash_for_home;
1727 n++;
1728 }
1729
1730 return n;
1731}
1732
1733
1734/*
1735 * This function takes "skb", consisting of a header template and a
1736 * payload, and hands it to LEPP, to emit as one or more segments,
1737 * each consisting of a possibly modified header, plus a piece of the
1738 * payload, via a process known as "tcp segmentation offload".
1739 *
1740 * Usually, "data" will contain the header template, of size "sh_len",
1741 * and "sh->frags" will contain "skb->data_len" bytes of payload, and
1742 * there will be "sh->gso_segs" segments.
1743 *
1744 * Sometimes, if "sendfile()" requires copying, we will be called with
1745 * "data" containing the header and payload, with "frags" being empty.
1746 *
Chris Metcalf92795672012-03-30 19:23:35 -04001747 * Sometimes, for example when using NFS over TCP, a single segment can
1748 * span 3 fragments, which must be handled carefully in LEPP.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001749 *
1750 * See "emulate_large_send_offload()" for some reference code, which
1751 * does not handle checksumming.
1752 *
1753 * ISSUE: How do we make sure that high memory DMA does not migrate?
1754 */
1755static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
1756{
1757 struct tile_net_priv *priv = netdev_priv(dev);
1758 int my_cpu = smp_processor_id();
1759 struct tile_net_cpu *info = priv->cpu[my_cpu];
1760 struct tile_net_stats_t *stats = &info->stats;
1761
1762 struct skb_shared_info *sh = skb_shinfo(skb);
1763
1764 unsigned char *data = skb->data;
1765
1766 /* The ip header follows the ethernet header. */
1767 struct iphdr *ih = ip_hdr(skb);
1768 unsigned int ih_len = ih->ihl * 4;
1769
1770 /* Note that "nh == ih", by definition. */
1771 unsigned char *nh = skb_network_header(skb);
1772 unsigned int eh_len = nh - data;
1773
1774 /* The tcp header follows the ip header. */
1775 struct tcphdr *th = (struct tcphdr *)(nh + ih_len);
1776 unsigned int th_len = th->doff * 4;
1777
1778 /* The total number of header bytes. */
1779 /* NOTE: This may be less than skb_headlen(skb). */
1780 unsigned int sh_len = eh_len + ih_len + th_len;
1781
1782 /* The number of payload bytes at "skb->data + sh_len". */
1783 /* This is non-zero for sendfile() without HIGHDMA. */
1784 unsigned int b_len = skb_headlen(skb) - sh_len;
1785
1786 /* The total number of payload bytes. */
1787 unsigned int d_len = b_len + skb->data_len;
1788
1789 /* The maximum payload size. */
1790 unsigned int p_len = sh->gso_size;
1791
1792 /* The total number of segments. */
1793 unsigned int num_segs = sh->gso_segs;
1794
1795 /* The temporary copy of the command. */
1796 u32 cmd_body[(LEPP_MAX_CMD_SIZE + 3) / 4];
1797 lepp_tso_cmd_t *cmd = (lepp_tso_cmd_t *)cmd_body;
1798
1799 /* Analyze the "frags". */
1800 unsigned int num_frags =
1801 tile_net_tx_frags(cmd->frags, skb, data + sh_len, b_len);
1802
1803 /* The size of the command, including frags and header. */
1804 size_t cmd_size = LEPP_TSO_CMD_SIZE(num_frags, sh_len);
1805
1806 /* The command header. */
1807 lepp_tso_cmd_t cmd_init = {
1808 .tso = true,
1809 .header_size = sh_len,
1810 .ip_offset = eh_len,
1811 .tcp_offset = eh_len + ih_len,
1812 .payload_size = p_len,
1813 .num_frags = num_frags,
1814 };
1815
1816 unsigned long irqflags;
1817
Chris Metcalfd91c6412011-03-01 12:49:53 -05001818 lepp_queue_t *eq = priv->eq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001819
Chris Metcalfd91c6412011-03-01 12:49:53 -05001820 struct sk_buff *olds[8];
1821 unsigned int wanted = 8;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001822 unsigned int i, nolds = 0;
1823
1824 unsigned int cmd_head, cmd_tail, cmd_next;
1825 unsigned int comp_tail;
1826
Chris Metcalfe5a06932010-11-01 17:00:37 -04001827
1828 /* Paranoia. */
1829 BUG_ON(skb->protocol != htons(ETH_P_IP));
1830 BUG_ON(ih->protocol != IPPROTO_TCP);
1831 BUG_ON(skb->ip_summed != CHECKSUM_PARTIAL);
1832 BUG_ON(num_frags > LEPP_MAX_FRAGS);
1833 /*--BUG_ON(num_segs != (d_len + (p_len - 1)) / p_len); */
1834 BUG_ON(num_segs <= 1);
1835
1836
1837 /* Finish preparing the command. */
1838
1839 /* Copy the command header. */
1840 *cmd = cmd_init;
1841
1842 /* Copy the "header". */
1843 memcpy(&cmd->frags[num_frags], data, sh_len);
1844
1845
1846 /* Prefetch and wait, to minimize time spent holding the spinlock. */
1847 prefetch_L1(&eq->comp_tail);
1848 prefetch_L1(&eq->cmd_tail);
1849 mb();
1850
1851
1852 /* Enqueue the command. */
1853
Chris Metcalfd91c6412011-03-01 12:49:53 -05001854 spin_lock_irqsave(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001855
Chris Metcalf92795672012-03-30 19:23:35 -04001856 /* Handle completions if needed to make room. */
1857 /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001858 if (lepp_num_free_comp_slots(eq) == 0) {
1859 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
1860 if (nolds == 0) {
1861busy:
1862 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
1863 return NETDEV_TX_BUSY;
1864 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001865 }
1866
1867 cmd_head = eq->cmd_head;
1868 cmd_tail = eq->cmd_tail;
1869
Chris Metcalfe5a06932010-11-01 17:00:37 -04001870 /* Prepare to advance, detecting full queue. */
Chris Metcalf92795672012-03-30 19:23:35 -04001871 /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001872 cmd_next = cmd_tail + cmd_size;
1873 if (cmd_tail < cmd_head && cmd_next >= cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001874 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001875 if (cmd_next > LEPP_CMD_LIMIT) {
1876 cmd_next = 0;
1877 if (cmd_next == cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001878 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001879 }
1880
1881 /* Copy the command. */
1882 memcpy(&eq->cmds[cmd_tail], cmd, cmd_size);
1883
1884 /* Advance. */
1885 cmd_tail = cmd_next;
1886
1887 /* Record "skb" for eventual freeing. */
1888 comp_tail = eq->comp_tail;
1889 eq->comps[comp_tail] = skb;
1890 LEPP_QINC(comp_tail);
1891 eq->comp_tail = comp_tail;
1892
1893 /* Flush before allowing LEPP to handle the command. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001894 /* ISSUE: Is this the optimal location for the flush? */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001895 __insn_mf();
1896
1897 eq->cmd_tail = cmd_tail;
1898
Chris Metcalfd91c6412011-03-01 12:49:53 -05001899 /* NOTE: Using "4" here is more efficient than "0" or "2", */
1900 /* and, strangely, more efficient than pre-checking the number */
1901 /* of available completions, and comparing it to 4. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001902 if (nolds == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001903 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
1904
1905 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001906
1907 /* Handle completions. */
1908 for (i = 0; i < nolds; i++)
1909 kfree_skb(olds[i]);
1910
1911 /* Update stats. */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04001912 u64_stats_update_begin(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001913 stats->tx_packets += num_segs;
1914 stats->tx_bytes += (num_segs * sh_len) + d_len;
Chris Metcalfd68e2d32013-07-25 12:41:15 -04001915 u64_stats_update_end(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001916
1917 /* Make sure the egress timer is scheduled. */
1918 tile_net_schedule_egress_timer(info);
1919
1920 return NETDEV_TX_OK;
1921}
1922
1923
1924/*
1925 * Transmit a packet (called by the kernel via "hard_start_xmit" hook).
1926 */
1927static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
1928{
1929 struct tile_net_priv *priv = netdev_priv(dev);
1930 int my_cpu = smp_processor_id();
1931 struct tile_net_cpu *info = priv->cpu[my_cpu];
1932 struct tile_net_stats_t *stats = &info->stats;
1933
1934 unsigned long irqflags;
1935
1936 struct skb_shared_info *sh = skb_shinfo(skb);
1937
1938 unsigned int len = skb->len;
1939 unsigned char *data = skb->data;
1940
Shan Wei96339d62011-04-22 19:07:41 +08001941 unsigned int csum_start = skb_checksum_start_offset(skb);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001942
1943 lepp_frag_t frags[LEPP_MAX_FRAGS];
1944
1945 unsigned int num_frags;
1946
Chris Metcalfd91c6412011-03-01 12:49:53 -05001947 lepp_queue_t *eq = priv->eq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001948
Chris Metcalfd91c6412011-03-01 12:49:53 -05001949 struct sk_buff *olds[8];
1950 unsigned int wanted = 8;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001951 unsigned int i, nolds = 0;
1952
1953 unsigned int cmd_size = sizeof(lepp_cmd_t);
1954
1955 unsigned int cmd_head, cmd_tail, cmd_next;
1956 unsigned int comp_tail;
1957
1958 lepp_cmd_t cmds[LEPP_MAX_FRAGS];
1959
Chris Metcalfe5a06932010-11-01 17:00:37 -04001960
1961 /*
1962 * This is paranoia, since we think that if the link doesn't come
1963 * up, telling Linux we have no carrier will keep it from trying
1964 * to transmit. If it does, though, we can't execute this routine,
1965 * since data structures we depend on aren't set up yet.
1966 */
1967 if (!info->registered)
1968 return NETDEV_TX_BUSY;
1969
1970
1971 /* Save the timestamp. */
1972 dev->trans_start = jiffies;
1973
1974
1975#ifdef TILE_NET_PARANOIA
1976#if CHIP_HAS_CBOX_HOME_MAP()
1977 if (hash_default) {
1978 HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)data);
1979 if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001980 panic("Non-HFH egress buffer! VA=%p Mode=%d PTE=%llx",
1981 data, hv_pte_get_mode(pte), hv_pte_val(pte));
Chris Metcalfe5a06932010-11-01 17:00:37 -04001982 }
1983#endif
1984#endif
1985
1986
1987#ifdef TILE_NET_DUMP_PACKETS
1988 /* ISSUE: Does not dump the "frags". */
1989 dump_packet(data, skb_headlen(skb), "tx");
1990#endif /* TILE_NET_DUMP_PACKETS */
1991
1992
1993 if (sh->gso_size != 0)
1994 return tile_net_tx_tso(skb, dev);
1995
1996
1997 /* Prepare the commands. */
1998
1999 num_frags = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
2000
2001 for (i = 0; i < num_frags; i++) {
2002
2003 bool final = (i == num_frags - 1);
2004
2005 lepp_cmd_t cmd = {
2006 .cpa_lo = frags[i].cpa_lo,
2007 .cpa_hi = frags[i].cpa_hi,
2008 .length = frags[i].length,
2009 .hash_for_home = frags[i].hash_for_home,
2010 .send_completion = final,
2011 .end_of_packet = final
2012 };
2013
2014 if (i == 0 && skb->ip_summed == CHECKSUM_PARTIAL) {
2015 cmd.compute_checksum = 1;
2016 cmd.checksum_data.bits.start_byte = csum_start;
2017 cmd.checksum_data.bits.count = len - csum_start;
2018 cmd.checksum_data.bits.destination_byte =
2019 csum_start + skb->csum_offset;
2020 }
2021
2022 cmds[i] = cmd;
2023 }
2024
2025
2026 /* Prefetch and wait, to minimize time spent holding the spinlock. */
2027 prefetch_L1(&eq->comp_tail);
2028 prefetch_L1(&eq->cmd_tail);
2029 mb();
2030
2031
2032 /* Enqueue the commands. */
2033
Chris Metcalfd91c6412011-03-01 12:49:53 -05002034 spin_lock_irqsave(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002035
Chris Metcalf92795672012-03-30 19:23:35 -04002036 /* Handle completions if needed to make room. */
2037 /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05002038 if (lepp_num_free_comp_slots(eq) == 0) {
2039 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
2040 if (nolds == 0) {
2041busy:
2042 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
2043 return NETDEV_TX_BUSY;
2044 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04002045 }
2046
2047 cmd_head = eq->cmd_head;
2048 cmd_tail = eq->cmd_tail;
2049
Chris Metcalfe5a06932010-11-01 17:00:37 -04002050 /* Copy the commands, or fail. */
Chris Metcalf92795672012-03-30 19:23:35 -04002051 /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002052 for (i = 0; i < num_frags; i++) {
2053
2054 /* Prepare to advance, detecting full queue. */
2055 cmd_next = cmd_tail + cmd_size;
2056 if (cmd_tail < cmd_head && cmd_next >= cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002057 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002058 if (cmd_next > LEPP_CMD_LIMIT) {
2059 cmd_next = 0;
2060 if (cmd_next == cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002061 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002062 }
2063
2064 /* Copy the command. */
2065 *(lepp_cmd_t *)&eq->cmds[cmd_tail] = cmds[i];
2066
2067 /* Advance. */
2068 cmd_tail = cmd_next;
2069 }
2070
2071 /* Record "skb" for eventual freeing. */
2072 comp_tail = eq->comp_tail;
2073 eq->comps[comp_tail] = skb;
2074 LEPP_QINC(comp_tail);
2075 eq->comp_tail = comp_tail;
2076
2077 /* Flush before allowing LEPP to handle the command. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05002078 /* ISSUE: Is this the optimal location for the flush? */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002079 __insn_mf();
2080
2081 eq->cmd_tail = cmd_tail;
2082
Chris Metcalfd91c6412011-03-01 12:49:53 -05002083 /* NOTE: Using "4" here is more efficient than "0" or "2", */
2084 /* and, strangely, more efficient than pre-checking the number */
2085 /* of available completions, and comparing it to 4. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002086 if (nolds == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002087 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
2088
2089 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002090
2091 /* Handle completions. */
2092 for (i = 0; i < nolds; i++)
2093 kfree_skb(olds[i]);
2094
2095 /* HACK: Track "expanded" size for short packets (e.g. 42 < 60). */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002096 u64_stats_update_begin(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002097 stats->tx_packets++;
2098 stats->tx_bytes += ((len >= ETH_ZLEN) ? len : ETH_ZLEN);
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002099 u64_stats_update_end(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002100
2101 /* Make sure the egress timer is scheduled. */
2102 tile_net_schedule_egress_timer(info);
2103
2104 return NETDEV_TX_OK;
2105}
2106
2107
2108/*
2109 * Deal with a transmit timeout.
2110 */
2111static void tile_net_tx_timeout(struct net_device *dev)
2112{
2113 PDEBUG("tile_net_tx_timeout()\n");
2114 PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
2115 jiffies - dev->trans_start);
2116
2117 /* XXX: ISSUE: This doesn't seem useful for us. */
2118 netif_wake_queue(dev);
2119}
2120
2121
2122/*
2123 * Ioctl commands.
2124 */
2125static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2126{
2127 return -EOPNOTSUPP;
2128}
2129
2130
2131/*
2132 * Get System Network Statistics.
2133 *
2134 * Returns the address of the device statistics structure.
2135 */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002136static struct rtnl_link_stats64 *tile_net_get_stats64(struct net_device *dev,
2137 struct rtnl_link_stats64 *stats)
Chris Metcalfe5a06932010-11-01 17:00:37 -04002138{
2139 struct tile_net_priv *priv = netdev_priv(dev);
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002140 u64 rx_packets = 0, tx_packets = 0;
2141 u64 rx_bytes = 0, tx_bytes = 0;
2142 u64 rx_errors = 0, rx_dropped = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002143 int i;
2144
2145 for_each_online_cpu(i) {
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002146 struct tile_net_stats_t *cpu_stats;
2147 u64 trx_packets, ttx_packets, trx_bytes, ttx_bytes;
2148 u64 trx_errors, trx_dropped;
2149 unsigned int start;
2150
2151 if (priv->cpu[i] == NULL)
2152 continue;
2153 cpu_stats = &priv->cpu[i]->stats;
2154
2155 do {
2156 start = u64_stats_fetch_begin_bh(&cpu_stats->syncp);
2157 trx_packets = cpu_stats->rx_packets;
2158 ttx_packets = cpu_stats->tx_packets;
2159 trx_bytes = cpu_stats->rx_bytes;
2160 ttx_bytes = cpu_stats->tx_bytes;
2161 trx_errors = cpu_stats->rx_errors;
2162 trx_dropped = cpu_stats->rx_dropped;
2163 } while (u64_stats_fetch_retry_bh(&cpu_stats->syncp, start));
2164
2165 rx_packets += trx_packets;
2166 tx_packets += ttx_packets;
2167 rx_bytes += trx_bytes;
2168 tx_bytes += ttx_bytes;
2169 rx_errors += trx_errors;
2170 rx_dropped += trx_dropped;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002171 }
2172
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002173 stats->rx_packets = rx_packets;
2174 stats->tx_packets = tx_packets;
2175 stats->rx_bytes = rx_bytes;
2176 stats->tx_bytes = tx_bytes;
2177 stats->rx_errors = rx_errors;
2178 stats->rx_dropped = rx_dropped;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002179
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002180 return stats;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002181}
2182
2183
2184/*
2185 * Change the "mtu".
2186 *
2187 * The "change_mtu" method is usually not needed.
2188 * If you need it, it must be like this.
2189 */
2190static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
2191{
2192 PDEBUG("tile_net_change_mtu()\n");
2193
2194 /* Check ranges. */
2195 if ((new_mtu < 68) || (new_mtu > 1500))
2196 return -EINVAL;
2197
2198 /* Accept the value. */
2199 dev->mtu = new_mtu;
2200
2201 return 0;
2202}
2203
2204
2205/*
2206 * Change the Ethernet Address of the NIC.
2207 *
2208 * The hypervisor driver does not support changing MAC address. However,
2209 * the IPP does not do anything with the MAC address, so the address which
2210 * gets used on outgoing packets, and which is accepted on incoming packets,
2211 * is completely up to the NetIO program or kernel driver which is actually
2212 * handling them.
2213 *
2214 * Returns 0 on success, negative on failure.
2215 */
2216static int tile_net_set_mac_address(struct net_device *dev, void *p)
2217{
2218 struct sockaddr *addr = p;
2219
2220 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002221 return -EADDRNOTAVAIL;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002222
2223 /* ISSUE: Note that "dev_addr" is now a pointer. */
2224 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2225
2226 return 0;
2227}
2228
2229
2230/*
2231 * Obtain the MAC address from the hypervisor.
2232 * This must be done before opening the device.
2233 */
2234static int tile_net_get_mac(struct net_device *dev)
2235{
2236 struct tile_net_priv *priv = netdev_priv(dev);
2237
2238 char hv_dev_name[32];
2239 int len;
2240
2241 __netio_getset_offset_t offset = { .word = NETIO_IPP_PARAM_OFF };
2242
2243 int ret;
2244
2245 /* For example, "xgbe0". */
2246 strcpy(hv_dev_name, dev->name);
2247 len = strlen(hv_dev_name);
2248
2249 /* For example, "xgbe/0". */
2250 hv_dev_name[len] = hv_dev_name[len - 1];
2251 hv_dev_name[len - 1] = '/';
2252 len++;
2253
2254 /* For example, "xgbe/0/native_hash". */
2255 strcpy(hv_dev_name + len, hash_default ? "/native_hash" : "/native");
2256
2257 /* Get the hypervisor handle for this device. */
2258 priv->hv_devhdl = hv_dev_open((HV_VirtAddr)hv_dev_name, 0);
2259 PDEBUG("hv_dev_open(%s) returned %d %p\n",
2260 hv_dev_name, priv->hv_devhdl, &priv->hv_devhdl);
2261 if (priv->hv_devhdl < 0) {
2262 if (priv->hv_devhdl == HV_ENODEV)
2263 printk(KERN_DEBUG "Ignoring unconfigured device %s\n",
2264 hv_dev_name);
2265 else
2266 printk(KERN_DEBUG "hv_dev_open(%s) returned %d\n",
2267 hv_dev_name, priv->hv_devhdl);
2268 return -1;
2269 }
2270
2271 /*
2272 * Read the hardware address from the hypervisor.
2273 * ISSUE: Note that "dev_addr" is now a pointer.
2274 */
2275 offset.bits.class = NETIO_PARAM;
2276 offset.bits.addr = NETIO_PARAM_MAC;
2277 ret = hv_dev_pread(priv->hv_devhdl, 0,
2278 (HV_VirtAddr)dev->dev_addr, dev->addr_len,
2279 offset.word);
2280 PDEBUG("hv_dev_pread(NETIO_PARAM_MAC) returned %d\n", ret);
2281 if (ret <= 0) {
2282 printk(KERN_DEBUG "hv_dev_pread(NETIO_PARAM_MAC) %s failed\n",
2283 dev->name);
2284 /*
2285 * Since the device is configured by the hypervisor but we
2286 * can't get its MAC address, we are most likely running
2287 * the simulator, so let's generate a random MAC address.
2288 */
Danny Kukawka7ce5d222012-02-15 06:45:40 +00002289 eth_hw_addr_random(dev);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002290 }
2291
2292 return 0;
2293}
2294
Chris Metcalf92795672012-03-30 19:23:35 -04002295
2296#ifdef CONFIG_NET_POLL_CONTROLLER
2297/*
2298 * Polling 'interrupt' - used by things like netconsole to send skbs
2299 * without having to re-enable interrupts. It's not called while
2300 * the interrupt routine is executing.
2301 */
2302static void tile_net_netpoll(struct net_device *dev)
2303{
2304 struct tile_net_priv *priv = netdev_priv(dev);
2305 disable_percpu_irq(priv->intr_id);
2306 tile_net_handle_ingress_interrupt(priv->intr_id, dev);
2307 enable_percpu_irq(priv->intr_id, 0);
2308}
2309#endif
2310
2311
stephen hemmingere5686ad2012-01-05 19:10:25 +00002312static const struct net_device_ops tile_net_ops = {
Chris Metcalfe5a06932010-11-01 17:00:37 -04002313 .ndo_open = tile_net_open,
2314 .ndo_stop = tile_net_stop,
2315 .ndo_start_xmit = tile_net_tx,
2316 .ndo_do_ioctl = tile_net_ioctl,
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002317 .ndo_get_stats64 = tile_net_get_stats64,
Chris Metcalfe5a06932010-11-01 17:00:37 -04002318 .ndo_change_mtu = tile_net_change_mtu,
2319 .ndo_tx_timeout = tile_net_tx_timeout,
Chris Metcalf92795672012-03-30 19:23:35 -04002320 .ndo_set_mac_address = tile_net_set_mac_address,
2321#ifdef CONFIG_NET_POLL_CONTROLLER
2322 .ndo_poll_controller = tile_net_netpoll,
2323#endif
Chris Metcalfe5a06932010-11-01 17:00:37 -04002324};
2325
2326
2327/*
2328 * The setup function.
2329 *
2330 * This uses ether_setup() to assign various fields in dev, including
2331 * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
2332 */
2333static void tile_net_setup(struct net_device *dev)
2334{
Chris Metcalfa8eaed52013-08-01 11:36:42 -04002335 netdev_features_t features = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002336
2337 ether_setup(dev);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002338 dev->netdev_ops = &tile_net_ops;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002339 dev->watchdog_timeo = TILE_NET_TIMEOUT;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002340 dev->tx_queue_len = TILE_NET_TX_QUEUE_LEN;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002341 dev->mtu = TILE_NET_MTU;
Chris Metcalfa8eaed52013-08-01 11:36:42 -04002342
2343 features |= NETIF_F_LLTX;
2344 features |= NETIF_F_HW_CSUM;
2345 features |= NETIF_F_SG;
2346 features |= NETIF_F_TSO;
2347
2348 /* We can't support HIGHDMA without hash_default, since we need
2349 * to be able to finv() with a VA if we don't have hash_default.
2350 */
2351 if (hash_default)
2352 features |= NETIF_F_HIGHDMA;
2353
2354 dev->hw_features |= features;
2355 dev->vlan_features |= features;
2356 dev->features |= features;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002357}
2358
2359
2360/*
2361 * Allocate the device structure, register the device, and obtain the
2362 * MAC address from the hypervisor.
2363 */
2364static struct net_device *tile_net_dev_init(const char *name)
2365{
2366 int ret;
2367 struct net_device *dev;
2368 struct tile_net_priv *priv;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002369
2370 /*
2371 * Allocate the device structure. This allocates "priv", calls
2372 * tile_net_setup(), and saves "name". Normally, "name" is a
2373 * template, instantiated by register_netdev(), but not for us.
2374 */
2375 dev = alloc_netdev(sizeof(*priv), name, tile_net_setup);
2376 if (!dev) {
2377 pr_err("alloc_netdev(%s) failed\n", name);
2378 return NULL;
2379 }
2380
2381 priv = netdev_priv(dev);
2382
2383 /* Initialize "priv". */
2384
2385 memset(priv, 0, sizeof(*priv));
2386
2387 /* Save "dev" for "tile_net_open_retry()". */
2388 priv->dev = dev;
2389
2390 INIT_DELAYED_WORK(&priv->retry_work, tile_net_open_retry);
2391
Chris Metcalfd91c6412011-03-01 12:49:53 -05002392 spin_lock_init(&priv->eq_lock);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002393
Chris Metcalfd91c6412011-03-01 12:49:53 -05002394 /* Allocate "eq". */
2395 priv->eq_pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, EQ_ORDER);
2396 if (!priv->eq_pages) {
Chris Metcalfe5a06932010-11-01 17:00:37 -04002397 free_netdev(dev);
2398 return NULL;
2399 }
Chris Metcalfd91c6412011-03-01 12:49:53 -05002400 priv->eq = page_address(priv->eq_pages);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002401
2402 /* Register the network device. */
2403 ret = register_netdev(dev);
2404 if (ret) {
2405 pr_err("register_netdev %s failed %d\n", dev->name, ret);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002406 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002407 free_netdev(dev);
2408 return NULL;
2409 }
2410
2411 /* Get the MAC address. */
2412 ret = tile_net_get_mac(dev);
2413 if (ret < 0) {
2414 unregister_netdev(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002415 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002416 free_netdev(dev);
2417 return NULL;
2418 }
2419
2420 return dev;
2421}
2422
2423
2424/*
2425 * Module cleanup.
Chris Metcalfd91c6412011-03-01 12:49:53 -05002426 *
2427 * FIXME: If compiled as a module, this module cannot be "unloaded",
2428 * because the "ingress interrupt handler" is registered permanently.
Chris Metcalfe5a06932010-11-01 17:00:37 -04002429 */
2430static void tile_net_cleanup(void)
2431{
2432 int i;
2433
2434 for (i = 0; i < TILE_NET_DEVS; i++) {
2435 if (tile_net_devs[i]) {
2436 struct net_device *dev = tile_net_devs[i];
2437 struct tile_net_priv *priv = netdev_priv(dev);
2438 unregister_netdev(dev);
Chris Metcalfd07bd862011-05-02 16:36:48 -04002439 finv_buffer_remote(priv->eq, EQ_SIZE, 0);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002440 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002441 free_netdev(dev);
2442 }
2443 }
2444}
2445
2446
2447/*
2448 * Module initialization.
2449 */
2450static int tile_net_init_module(void)
2451{
Chris Metcalf92795672012-03-30 19:23:35 -04002452 pr_info("Tilera Network Driver\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04002453
2454 tile_net_devs[0] = tile_net_dev_init("xgbe0");
2455 tile_net_devs[1] = tile_net_dev_init("xgbe1");
2456 tile_net_devs[2] = tile_net_dev_init("gbe0");
2457 tile_net_devs[3] = tile_net_dev_init("gbe1");
2458
2459 return 0;
2460}
2461
2462
Chris Metcalfd91c6412011-03-01 12:49:53 -05002463module_init(tile_net_init_module);
2464module_exit(tile_net_cleanup);
2465
2466
Chris Metcalfe5a06932010-11-01 17:00:37 -04002467#ifndef MODULE
Chris Metcalfd91c6412011-03-01 12:49:53 -05002468
Chris Metcalfe5a06932010-11-01 17:00:37 -04002469/*
2470 * The "network_cpus" boot argument specifies the cpus that are dedicated
2471 * to handle ingress packets.
2472 *
2473 * The parameter should be in the form "network_cpus=m-n[,x-y]", where
2474 * m, n, x, y are integer numbers that represent the cpus that can be
2475 * neither a dedicated cpu nor a dataplane cpu.
2476 */
2477static int __init network_cpus_setup(char *str)
2478{
2479 int rc = cpulist_parse_crop(str, &network_cpus_map);
2480 if (rc != 0) {
2481 pr_warning("network_cpus=%s: malformed cpu list\n",
2482 str);
2483 } else {
2484
2485 /* Remove dedicated cpus. */
2486 cpumask_and(&network_cpus_map, &network_cpus_map,
2487 cpu_possible_mask);
2488
2489
2490 if (cpumask_empty(&network_cpus_map)) {
2491 pr_warning("Ignoring network_cpus='%s'.\n",
2492 str);
2493 } else {
2494 char buf[1024];
2495 cpulist_scnprintf(buf, sizeof(buf), &network_cpus_map);
2496 pr_info("Linux network CPUs: %s\n", buf);
2497 network_cpus_used = true;
2498 }
2499 }
2500
2501 return 0;
2502}
2503__setup("network_cpus=", network_cpus_setup);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002504
Chris Metcalfe5a06932010-11-01 17:00:37 -04002505#endif