blob: f66ac2000c001c541c8ae7bee6e488af07db2692 [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
92/* HACK: Define to support GSO. */
93/* ISSUE: This may actually hurt performance of the TCP blaster. */
94/* #define TILE_NET_GSO */
95
96/* Define this to collapse "duplicate" acks. */
97/* #define IGNORE_DUP_ACKS */
98
99/* HACK: Define this to verify incoming packets. */
100/* #define TILE_NET_VERIFY_INGRESS */
101
102/* Use 3000 to enable the Linux Traffic Control (QoS) layer, else 0. */
103#define TILE_NET_TX_QUEUE_LEN 0
104
105/* Define to dump packets (prints out the whole packet on tx and rx). */
106/* #define TILE_NET_DUMP_PACKETS */
107
108/* Define to enable debug spew (all PDEBUG's are enabled). */
109/* #define TILE_NET_DEBUG */
110
111
112/* Define to activate paranoia checks. */
113/* #define TILE_NET_PARANOIA */
114
115/* Default transmit lockup timeout period, in jiffies. */
116#define TILE_NET_TIMEOUT (5 * HZ)
117
118/* Default retry interval for bringing up the NetIO interface, in jiffies. */
119#define TILE_NET_RETRY_INTERVAL (5 * HZ)
120
121/* Number of ports (xgbe0, xgbe1, gbe0, gbe1). */
122#define TILE_NET_DEVS 4
123
124
125
126/* Paranoia. */
127#if NET_IP_ALIGN != LIPP_PACKET_PADDING
128#error "NET_IP_ALIGN must match LIPP_PACKET_PADDING."
129#endif
130
131
132/* Debug print. */
133#ifdef TILE_NET_DEBUG
134#define PDEBUG(fmt, args...) net_printk(fmt, ## args)
135#else
136#define PDEBUG(fmt, args...)
137#endif
138
139
140MODULE_AUTHOR("Tilera");
141MODULE_LICENSE("GPL");
142
Chris Metcalfd91c6412011-03-01 12:49:53 -0500143
Chris Metcalfe5a06932010-11-01 17:00:37 -0400144/*
145 * Queue of incoming packets for a specific cpu and device.
146 *
147 * Includes a pointer to the "system" data, and the actual "user" data.
148 */
149struct tile_netio_queue {
150 netio_queue_impl_t *__system_part;
151 netio_queue_user_impl_t __user_part;
152
153};
154
155
156/*
157 * Statistics counters for a specific cpu and device.
158 */
159struct tile_net_stats_t {
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400160 struct u64_stats_sync syncp;
161 u64 rx_packets; /* total packets received */
162 u64 tx_packets; /* total packets transmitted */
163 u64 rx_bytes; /* total bytes received */
164 u64 tx_bytes; /* total bytes transmitted */
165 u64 rx_errors; /* packets truncated or marked bad by hw */
166 u64 rx_dropped; /* packets not for us or intf not up */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400167};
168
169
170/*
171 * Info for a specific cpu and device.
172 *
173 * ISSUE: There is a "dev" pointer in "napi" as well.
174 */
175struct tile_net_cpu {
176 /* The NAPI struct. */
177 struct napi_struct napi;
178 /* Packet queue. */
179 struct tile_netio_queue queue;
180 /* Statistics. */
181 struct tile_net_stats_t stats;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500182 /* True iff NAPI is enabled. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400183 bool napi_enabled;
Linus Torvalds8a9ea322011-10-25 13:25:22 +0200184 /* True if this tile has successfully registered with the IPP. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400185 bool registered;
186 /* True if the link was down last time we tried to register. */
187 bool link_down;
188 /* True if "egress_timer" is scheduled. */
189 bool egress_timer_scheduled;
190 /* Number of small sk_buffs which must still be provided. */
191 unsigned int num_needed_small_buffers;
192 /* Number of large sk_buffs which must still be provided. */
193 unsigned int num_needed_large_buffers;
194 /* A timer for handling egress completions. */
195 struct timer_list egress_timer;
196};
197
198
199/*
200 * Info for a specific device.
201 */
202struct tile_net_priv {
203 /* Our network device. */
204 struct net_device *dev;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500205 /* Pages making up the egress queue. */
206 struct page *eq_pages;
207 /* Address of the actual egress queue. */
208 lepp_queue_t *eq;
209 /* Protects "eq". */
210 spinlock_t eq_lock;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400211 /* The hypervisor handle for this interface. */
212 int hv_devhdl;
213 /* The intr bit mask that IDs this device. */
214 u32 intr_id;
215 /* True iff "tile_net_open_aux()" has succeeded. */
Chris Metcalfd91c6412011-03-01 12:49:53 -0500216 bool partly_opened;
217 /* True iff the device is "active". */
218 bool active;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400219 /* Effective network cpus. */
220 struct cpumask network_cpus_map;
221 /* Number of network cpus. */
222 int network_cpus_count;
223 /* Credits per network cpu. */
224 int network_cpus_credits;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400225 /* For NetIO bringup retries. */
226 struct delayed_work retry_work;
227 /* Quick access to per cpu data. */
228 struct tile_net_cpu *cpu[NR_CPUS];
229};
230
Chris Metcalfd91c6412011-03-01 12:49:53 -0500231/* Log2 of the number of small pages needed for the egress queue. */
232#define EQ_ORDER get_order(sizeof(lepp_queue_t))
233/* Size of the egress queue's pages. */
234#define EQ_SIZE (1 << (PAGE_SHIFT + EQ_ORDER))
Chris Metcalfe5a06932010-11-01 17:00:37 -0400235
236/*
237 * The actual devices (xgbe0, xgbe1, gbe0, gbe1).
238 */
239static struct net_device *tile_net_devs[TILE_NET_DEVS];
240
241/*
242 * The "tile_net_cpu" structures for each device.
243 */
244static DEFINE_PER_CPU(struct tile_net_cpu, hv_xgbe0);
245static DEFINE_PER_CPU(struct tile_net_cpu, hv_xgbe1);
246static DEFINE_PER_CPU(struct tile_net_cpu, hv_gbe0);
247static DEFINE_PER_CPU(struct tile_net_cpu, hv_gbe1);
248
249
250/*
251 * True if "network_cpus" was specified.
252 */
253static bool network_cpus_used;
254
255/*
256 * The actual cpus in "network_cpus".
257 */
258static struct cpumask network_cpus_map;
259
260
261
262#ifdef TILE_NET_DEBUG
263/*
264 * printk with extra stuff.
265 *
266 * We print the CPU we're running in brackets.
267 */
268static void net_printk(char *fmt, ...)
269{
270 int i;
271 int len;
272 va_list args;
273 static char buf[256];
274
275 len = sprintf(buf, "tile_net[%2.2d]: ", smp_processor_id());
276 va_start(args, fmt);
277 i = vscnprintf(buf + len, sizeof(buf) - len - 1, fmt, args);
278 va_end(args);
279 buf[255] = '\0';
280 pr_notice(buf);
281}
282#endif
283
284
285#ifdef TILE_NET_DUMP_PACKETS
286/*
287 * Dump a packet.
288 */
289static void dump_packet(unsigned char *data, unsigned long length, char *s)
290{
Chris Metcalfd91c6412011-03-01 12:49:53 -0500291 int my_cpu = smp_processor_id();
292
Chris Metcalfe5a06932010-11-01 17:00:37 -0400293 unsigned long i;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500294 char buf[128];
295
Chris Metcalfe5a06932010-11-01 17:00:37 -0400296 static unsigned int count;
297
298 pr_info("dump_packet(data %p, length 0x%lx s %s count 0x%x)\n",
299 data, length, s, count++);
300
301 pr_info("\n");
302
303 for (i = 0; i < length; i++) {
304 if ((i & 0xf) == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -0500305 sprintf(buf, "[%02d] %8.8lx:", my_cpu, i);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400306 sprintf(buf + strlen(buf), " %2.2x", data[i]);
Chris Metcalfd91c6412011-03-01 12:49:53 -0500307 if ((i & 0xf) == 0xf || i == length - 1) {
308 strcat(buf, "\n");
309 pr_info("%s", buf);
310 }
Chris Metcalfe5a06932010-11-01 17:00:37 -0400311 }
312}
313#endif
314
315
316/*
317 * Provide support for the __netio_fastio1() swint
318 * (see <hv/drv_xgbe_intf.h> for how it is used).
319 *
320 * The fastio swint2 call may clobber all the caller-saved registers.
321 * It rarely clobbers memory, but we allow for the possibility in
322 * the signature just to be on the safe side.
323 *
324 * Also, gcc doesn't seem to allow an input operand to be
325 * clobbered, so we fake it with dummy outputs.
326 *
327 * This function can't be static because of the way it is declared
328 * in the netio header.
329 */
330inline int __netio_fastio1(u32 fastio_index, u32 arg0)
331{
332 long result, clobber_r1, clobber_r10;
333 asm volatile("swint2"
334 : "=R00" (result),
335 "=R01" (clobber_r1), "=R10" (clobber_r10)
336 : "R10" (fastio_index), "R01" (arg0)
337 : "memory", "r2", "r3", "r4",
338 "r5", "r6", "r7", "r8", "r9",
339 "r11", "r12", "r13", "r14",
340 "r15", "r16", "r17", "r18", "r19",
341 "r20", "r21", "r22", "r23", "r24",
342 "r25", "r26", "r27", "r28", "r29");
343 return result;
344}
345
346
Chris Metcalf92795672012-03-30 19:23:35 -0400347static void tile_net_return_credit(struct tile_net_cpu *info)
348{
349 struct tile_netio_queue *queue = &info->queue;
350 netio_queue_user_impl_t *qup = &queue->__user_part;
351
352 /* Return four credits after every fourth packet. */
353 if (--qup->__receive_credit_remaining == 0) {
354 u32 interval = qup->__receive_credit_interval;
355 qup->__receive_credit_remaining = interval;
356 __netio_fastio_return_credits(qup->__fastio_index, interval);
357 }
358}
359
360
361
Chris Metcalfe5a06932010-11-01 17:00:37 -0400362/*
363 * Provide a linux buffer to LIPP.
364 */
365static void tile_net_provide_linux_buffer(struct tile_net_cpu *info,
366 void *va, bool small)
367{
368 struct tile_netio_queue *queue = &info->queue;
369
370 /* Convert "va" and "small" to "linux_buffer_t". */
371 unsigned int buffer = ((unsigned int)(__pa(va) >> 7) << 1) + small;
372
373 __netio_fastio_free_buffer(queue->__user_part.__fastio_index, buffer);
374}
375
376
377/*
378 * Provide a linux buffer for LIPP.
Chris Metcalfd91c6412011-03-01 12:49:53 -0500379 *
380 * Note that the ACTUAL allocation for each buffer is a "struct sk_buff",
381 * plus a chunk of memory that includes not only the requested bytes, but
382 * also NET_SKB_PAD bytes of initial padding, and a "struct skb_shared_info".
383 *
384 * Note that "struct skb_shared_info" is 88 bytes with 64K pages and
385 * 268 bytes with 4K pages (since the frags[] array needs 18 entries).
386 *
387 * Without jumbo packets, the maximum packet size will be 1536 bytes,
388 * and we use 2 bytes (NET_IP_ALIGN) of padding. ISSUE: If we told
389 * the hardware to clip at 1518 bytes instead of 1536 bytes, then we
390 * could save an entire cache line, but in practice, we don't need it.
391 *
392 * Since CPAs are 38 bits, and we can only encode the high 31 bits in
393 * a "linux_buffer_t", the low 7 bits must be zero, and thus, we must
394 * align the actual "va" mod 128.
395 *
396 * We assume that the underlying "head" will be aligned mod 64. Note
397 * that in practice, we have seen "head" NOT aligned mod 128 even when
398 * using 2048 byte allocations, which is surprising.
399 *
400 * If "head" WAS always aligned mod 128, we could change LIPP to
401 * assume that the low SIX bits are zero, and the 7th bit is one, that
402 * is, align the actual "va" mod 128 plus 64, which would be "free".
403 *
404 * For now, the actual "head" pointer points at NET_SKB_PAD bytes of
405 * padding, plus 28 or 92 bytes of extra padding, plus the sk_buff
406 * pointer, plus the NET_IP_ALIGN padding, plus 126 or 1536 bytes for
407 * the actual packet, plus 62 bytes of empty padding, plus some
408 * padding and the "struct skb_shared_info".
409 *
410 * With 64K pages, a large buffer thus needs 32+92+4+2+1536+62+88
411 * bytes, or 1816 bytes, which fits comfortably into 2048 bytes.
412 *
413 * With 64K pages, a small buffer thus needs 32+92+4+2+126+88
414 * bytes, or 344 bytes, which means we are wasting 64+ bytes, and
415 * could presumably increase the size of small buffers.
416 *
417 * With 4K pages, a large buffer thus needs 32+92+4+2+1536+62+268
418 * bytes, or 1996 bytes, which fits comfortably into 2048 bytes.
419 *
420 * With 4K pages, a small buffer thus needs 32+92+4+2+126+268
421 * bytes, or 524 bytes, which is annoyingly wasteful.
422 *
423 * Maybe we should increase LIPP_SMALL_PACKET_SIZE to 192?
424 *
425 * ISSUE: Maybe we should increase "NET_SKB_PAD" to 64?
Chris Metcalfe5a06932010-11-01 17:00:37 -0400426 */
427static bool tile_net_provide_needed_buffer(struct tile_net_cpu *info,
428 bool small)
429{
Chris Metcalfd91c6412011-03-01 12:49:53 -0500430#if TILE_NET_MTU <= 1536
431 /* Without "jumbo", 2 + 1536 should be sufficient. */
432 unsigned int large_size = NET_IP_ALIGN + 1536;
433#else
434 /* ISSUE: This has not been tested. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400435 unsigned int large_size = NET_IP_ALIGN + TILE_NET_MTU + 100;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500436#endif
Chris Metcalfe5a06932010-11-01 17:00:37 -0400437
Chris Metcalfd91c6412011-03-01 12:49:53 -0500438 /* Avoid "false sharing" with last cache line. */
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000439 /* ISSUE: This is already done by "netdev_alloc_skb()". */
Chris Metcalfd91c6412011-03-01 12:49:53 -0500440 unsigned int len =
Chris Metcalfe5a06932010-11-01 17:00:37 -0400441 (((small ? LIPP_SMALL_PACKET_SIZE : large_size) +
442 CHIP_L2_LINE_SIZE() - 1) & -CHIP_L2_LINE_SIZE());
443
Chris Metcalfd91c6412011-03-01 12:49:53 -0500444 unsigned int padding = 128 - NET_SKB_PAD;
445 unsigned int align;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400446
447 struct sk_buff *skb;
448 void *va;
449
450 struct sk_buff **skb_ptr;
451
Chris Metcalfd91c6412011-03-01 12:49:53 -0500452 /* Request 96 extra bytes for alignment purposes. */
Chris Metcalf00a62d42012-04-02 13:17:37 -0400453 skb = netdev_alloc_skb(info->napi.dev, len + padding);
Chris Metcalfd91c6412011-03-01 12:49:53 -0500454 if (skb == NULL)
455 return false;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400456
Chris Metcalfd91c6412011-03-01 12:49:53 -0500457 /* Skip 32 or 96 bytes to align "data" mod 128. */
458 align = -(long)skb->data & (128 - 1);
459 BUG_ON(align > padding);
460 skb_reserve(skb, align);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400461
Chris Metcalfd91c6412011-03-01 12:49:53 -0500462 /* This address is given to IPP. */
463 va = skb->data;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400464
Chris Metcalfd91c6412011-03-01 12:49:53 -0500465 /* Buffers must not span a huge page. */
466 BUG_ON(((((long)va & ~HPAGE_MASK) + len) & HPAGE_MASK) != 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400467
Chris Metcalfd91c6412011-03-01 12:49:53 -0500468#ifdef TILE_NET_PARANOIA
469#if CHIP_HAS_CBOX_HOME_MAP()
470 if (hash_default) {
471 HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)va);
472 if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
473 panic("Non-HFH ingress buffer! VA=%p Mode=%d PTE=%llx",
474 va, hv_pte_get_mode(pte), hv_pte_val(pte));
Chris Metcalfe5a06932010-11-01 17:00:37 -0400475 }
Chris Metcalfd91c6412011-03-01 12:49:53 -0500476#endif
477#endif
478
479 /* Invalidate the packet buffer. */
480 if (!hash_default)
481 __inv_buffer(va, len);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400482
483 /* Skip two bytes to satisfy LIPP assumptions. */
484 /* Note that this aligns IP on a 16 byte boundary. */
485 /* ISSUE: Do this when the packet arrives? */
486 skb_reserve(skb, NET_IP_ALIGN);
487
488 /* Save a back-pointer to 'skb'. */
489 skb_ptr = va - sizeof(*skb_ptr);
490 *skb_ptr = skb;
491
Chris Metcalfe5a06932010-11-01 17:00:37 -0400492 /* Make sure "skb_ptr" has been flushed. */
493 __insn_mf();
494
Chris Metcalfe5a06932010-11-01 17:00:37 -0400495 /* Provide the new buffer. */
496 tile_net_provide_linux_buffer(info, va, small);
497
498 return true;
499}
500
501
502/*
503 * Provide linux buffers for LIPP.
504 */
505static void tile_net_provide_needed_buffers(struct tile_net_cpu *info)
506{
507 while (info->num_needed_small_buffers != 0) {
508 if (!tile_net_provide_needed_buffer(info, true))
509 goto oops;
510 info->num_needed_small_buffers--;
511 }
512
513 while (info->num_needed_large_buffers != 0) {
514 if (!tile_net_provide_needed_buffer(info, false))
515 goto oops;
516 info->num_needed_large_buffers--;
517 }
518
519 return;
520
521oops:
522
523 /* Add a description to the page allocation failure dump. */
524 pr_notice("Could not provide a linux buffer to LIPP.\n");
525}
526
527
528/*
529 * Grab some LEPP completions, and store them in "comps", of size
530 * "comps_size", and return the number of completions which were
531 * stored, so the caller can free them.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400532 */
Chris Metcalfd91c6412011-03-01 12:49:53 -0500533static unsigned int tile_net_lepp_grab_comps(lepp_queue_t *eq,
Chris Metcalfe5a06932010-11-01 17:00:37 -0400534 struct sk_buff *comps[],
535 unsigned int comps_size,
Chris Metcalfd91c6412011-03-01 12:49:53 -0500536 unsigned int min_size)
Chris Metcalfe5a06932010-11-01 17:00:37 -0400537{
Chris Metcalfe5a06932010-11-01 17:00:37 -0400538 unsigned int n = 0;
539
Chris Metcalfd91c6412011-03-01 12:49:53 -0500540 unsigned int comp_head = eq->comp_head;
541 unsigned int comp_busy = eq->comp_busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400542
543 while (comp_head != comp_busy && n < comps_size) {
544 comps[n++] = eq->comps[comp_head];
545 LEPP_QINC(comp_head);
546 }
547
Chris Metcalfd91c6412011-03-01 12:49:53 -0500548 if (n < min_size)
549 return 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400550
551 eq->comp_head = comp_head;
552
Chris Metcalfe5a06932010-11-01 17:00:37 -0400553 return n;
554}
555
556
557/*
Chris Metcalfd91c6412011-03-01 12:49:53 -0500558 * Free some comps, and return true iff there are still some pending.
559 */
560static bool tile_net_lepp_free_comps(struct net_device *dev, bool all)
561{
562 struct tile_net_priv *priv = netdev_priv(dev);
563
564 lepp_queue_t *eq = priv->eq;
565
566 struct sk_buff *olds[64];
567 unsigned int wanted = 64;
568 unsigned int i, n;
569 bool pending;
570
571 spin_lock(&priv->eq_lock);
572
573 if (all)
574 eq->comp_busy = eq->comp_tail;
575
576 n = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
577
578 pending = (eq->comp_head != eq->comp_tail);
579
580 spin_unlock(&priv->eq_lock);
581
582 for (i = 0; i < n; i++)
583 kfree_skb(olds[i]);
584
585 return pending;
586}
587
588
589/*
Chris Metcalfe5a06932010-11-01 17:00:37 -0400590 * Make sure the egress timer is scheduled.
591 *
592 * Note that we use "schedule if not scheduled" logic instead of the more
593 * obvious "reschedule" logic, because "reschedule" is fairly expensive.
594 */
595static void tile_net_schedule_egress_timer(struct tile_net_cpu *info)
596{
597 if (!info->egress_timer_scheduled) {
598 mod_timer_pinned(&info->egress_timer, jiffies + 1);
599 info->egress_timer_scheduled = true;
600 }
601}
602
603
604/*
605 * The "function" for "info->egress_timer".
606 *
607 * This timer will reschedule itself as long as there are any pending
608 * completions expected (on behalf of any tile).
609 *
610 * ISSUE: Realistically, will the timer ever stop scheduling itself?
611 *
612 * ISSUE: This timer is almost never actually needed, so just use a global
613 * timer that can run on any tile.
614 *
615 * ISSUE: Maybe instead track number of expected completions, and free
616 * only that many, resetting to zero if "pending" is ever false.
617 */
618static void tile_net_handle_egress_timer(unsigned long arg)
619{
620 struct tile_net_cpu *info = (struct tile_net_cpu *)arg;
621 struct net_device *dev = info->napi.dev;
622
Chris Metcalfe5a06932010-11-01 17:00:37 -0400623 /* The timer is no longer scheduled. */
624 info->egress_timer_scheduled = false;
625
Chris Metcalfd91c6412011-03-01 12:49:53 -0500626 /* Free comps, and reschedule timer if more are pending. */
627 if (tile_net_lepp_free_comps(dev, false))
Chris Metcalfe5a06932010-11-01 17:00:37 -0400628 tile_net_schedule_egress_timer(info);
629}
630
631
632#ifdef IGNORE_DUP_ACKS
633
634/*
635 * Help detect "duplicate" ACKs. These are sequential packets (for a
636 * given flow) which are exactly 66 bytes long, sharing everything but
637 * ID=2@0x12, Hsum=2@0x18, Ack=4@0x2a, WinSize=2@0x30, Csum=2@0x32,
638 * Tstamps=10@0x38. The ID's are +1, the Hsum's are -1, the Ack's are
639 * +N, and the Tstamps are usually identical.
640 *
641 * NOTE: Apparently truly duplicate acks (with identical "ack" values),
642 * should not be collapsed, as they are used for some kind of flow control.
643 */
644static bool is_dup_ack(char *s1, char *s2, unsigned int len)
645{
646 int i;
647
648 unsigned long long ignorable = 0;
649
650 /* Identification. */
651 ignorable |= (1ULL << 0x12);
652 ignorable |= (1ULL << 0x13);
653
654 /* Header checksum. */
655 ignorable |= (1ULL << 0x18);
656 ignorable |= (1ULL << 0x19);
657
658 /* ACK. */
659 ignorable |= (1ULL << 0x2a);
660 ignorable |= (1ULL << 0x2b);
661 ignorable |= (1ULL << 0x2c);
662 ignorable |= (1ULL << 0x2d);
663
664 /* WinSize. */
665 ignorable |= (1ULL << 0x30);
666 ignorable |= (1ULL << 0x31);
667
668 /* Checksum. */
669 ignorable |= (1ULL << 0x32);
670 ignorable |= (1ULL << 0x33);
671
672 for (i = 0; i < len; i++, ignorable >>= 1) {
673
674 if ((ignorable & 1) || (s1[i] == s2[i]))
675 continue;
676
677#ifdef TILE_NET_DEBUG
678 /* HACK: Mention non-timestamp diffs. */
679 if (i < 0x38 && i != 0x2f &&
680 net_ratelimit())
681 pr_info("Diff at 0x%x\n", i);
682#endif
683
684 return false;
685 }
686
687#ifdef TILE_NET_NO_SUPPRESS_DUP_ACKS
688 /* HACK: Do not suppress truly duplicate ACKs. */
689 /* ISSUE: Is this actually necessary or helpful? */
690 if (s1[0x2a] == s2[0x2a] &&
691 s1[0x2b] == s2[0x2b] &&
692 s1[0x2c] == s2[0x2c] &&
693 s1[0x2d] == s2[0x2d]) {
694 return false;
695 }
696#endif
697
698 return true;
699}
700
701#endif
702
703
704
Chris Metcalfd91c6412011-03-01 12:49:53 -0500705static void tile_net_discard_aux(struct tile_net_cpu *info, int index)
706{
707 struct tile_netio_queue *queue = &info->queue;
708 netio_queue_impl_t *qsp = queue->__system_part;
709 netio_queue_user_impl_t *qup = &queue->__user_part;
710
711 int index2_aux = index + sizeof(netio_pkt_t);
712 int index2 =
713 ((index2_aux ==
714 qsp->__packet_receive_queue.__last_packet_plus_one) ?
715 0 : index2_aux);
716
717 netio_pkt_t *pkt = (netio_pkt_t *)((unsigned long) &qsp[1] + index);
718
719 /* Extract the "linux_buffer_t". */
720 unsigned int buffer = pkt->__packet.word;
721
722 /* Convert "linux_buffer_t" to "va". */
723 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
724
725 /* Acquire the associated "skb". */
726 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
727 struct sk_buff *skb = *skb_ptr;
728
729 kfree_skb(skb);
730
731 /* Consume this packet. */
732 qup->__packet_receive_read = index2;
733}
734
735
Chris Metcalfe5a06932010-11-01 17:00:37 -0400736/*
Chris Metcalfd91c6412011-03-01 12:49:53 -0500737 * Like "tile_net_poll()", but just discard packets.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400738 */
739static void tile_net_discard_packets(struct net_device *dev)
740{
741 struct tile_net_priv *priv = netdev_priv(dev);
742 int my_cpu = smp_processor_id();
743 struct tile_net_cpu *info = priv->cpu[my_cpu];
744 struct tile_netio_queue *queue = &info->queue;
745 netio_queue_impl_t *qsp = queue->__system_part;
746 netio_queue_user_impl_t *qup = &queue->__user_part;
747
748 while (qup->__packet_receive_read !=
749 qsp->__packet_receive_queue.__packet_write) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400750 int index = qup->__packet_receive_read;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500751 tile_net_discard_aux(info, index);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400752 }
753}
754
755
756/*
757 * Handle the next packet. Return true if "processed", false if "filtered".
758 */
759static bool tile_net_poll_aux(struct tile_net_cpu *info, int index)
760{
761 struct net_device *dev = info->napi.dev;
762
763 struct tile_netio_queue *queue = &info->queue;
764 netio_queue_impl_t *qsp = queue->__system_part;
765 netio_queue_user_impl_t *qup = &queue->__user_part;
766 struct tile_net_stats_t *stats = &info->stats;
767
768 int filter;
769
770 int index2_aux = index + sizeof(netio_pkt_t);
771 int index2 =
772 ((index2_aux ==
773 qsp->__packet_receive_queue.__last_packet_plus_one) ?
774 0 : index2_aux);
775
776 netio_pkt_t *pkt = (netio_pkt_t *)((unsigned long) &qsp[1] + index);
777
778 netio_pkt_metadata_t *metadata = NETIO_PKT_METADATA(pkt);
779
Chris Metcalfd91c6412011-03-01 12:49:53 -0500780 /* Extract the packet size. FIXME: Shouldn't the second line */
781 /* get subtracted? Mostly moot, since it should be "zero". */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400782 unsigned long len =
783 (NETIO_PKT_CUSTOM_LENGTH(pkt) +
784 NET_IP_ALIGN - NETIO_PACKET_PADDING);
785
786 /* Extract the "linux_buffer_t". */
787 unsigned int buffer = pkt->__packet.word;
788
789 /* Extract "small" (vs "large"). */
790 bool small = ((buffer & 1) != 0);
791
792 /* Convert "linux_buffer_t" to "va". */
793 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
794
795 /* Extract the packet data pointer. */
796 /* Compare to "NETIO_PKT_CUSTOM_DATA(pkt)". */
797 unsigned char *buf = va + NET_IP_ALIGN;
798
Chris Metcalfe5a06932010-11-01 17:00:37 -0400799 /* Invalidate the packet buffer. */
800 if (!hash_default)
801 __inv_buffer(buf, len);
802
803 /* ISSUE: Is this needed? */
804 dev->last_rx = jiffies;
805
806#ifdef TILE_NET_DUMP_PACKETS
807 dump_packet(buf, len, "rx");
808#endif /* TILE_NET_DUMP_PACKETS */
809
810#ifdef TILE_NET_VERIFY_INGRESS
811 if (!NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt) &&
812 NETIO_PKT_L4_CSUM_CALCULATED_M(metadata, pkt)) {
Chris Metcalfd91c6412011-03-01 12:49:53 -0500813 /* Bug 6624: Includes UDP packets with a "zero" checksum. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400814 pr_warning("Bad L4 checksum on %d byte packet.\n", len);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400815 }
816 if (!NETIO_PKT_L3_CSUM_CORRECT_M(metadata, pkt) &&
817 NETIO_PKT_L3_CSUM_CALCULATED_M(metadata, pkt)) {
818 dump_packet(buf, len, "rx");
819 panic("Bad L3 checksum.");
820 }
821 switch (NETIO_PKT_STATUS_M(metadata, pkt)) {
822 case NETIO_PKT_STATUS_OVERSIZE:
823 if (len >= 64) {
824 dump_packet(buf, len, "rx");
825 panic("Unexpected OVERSIZE.");
826 }
827 break;
828 case NETIO_PKT_STATUS_BAD:
Chris Metcalfd91c6412011-03-01 12:49:53 -0500829 pr_warning("Unexpected BAD %ld byte packet.\n", len);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400830 }
831#endif
832
833 filter = 0;
834
Chris Metcalfd91c6412011-03-01 12:49:53 -0500835 /* ISSUE: Filter TCP packets with "bad" checksums? */
836
Chris Metcalfe5a06932010-11-01 17:00:37 -0400837 if (!(dev->flags & IFF_UP)) {
838 /* Filter packets received before we're up. */
839 filter = 1;
Chris Metcalfd91c6412011-03-01 12:49:53 -0500840 } else if (NETIO_PKT_STATUS_M(metadata, pkt) == NETIO_PKT_STATUS_BAD) {
841 /* Filter "truncated" packets. */
842 filter = 1;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400843 } else if (!(dev->flags & IFF_PROMISC)) {
Chris Metcalfd91c6412011-03-01 12:49:53 -0500844 /* FIXME: Implement HW multicast filter. */
845 if (!is_multicast_ether_addr(buf)) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400846 /* Filter packets not for our address. */
847 const u8 *mine = dev->dev_addr;
Joe Perches2e42e472012-05-09 17:17:46 +0000848 filter = !ether_addr_equal(mine, buf);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400849 }
850 }
851
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400852 u64_stats_update_begin(&stats->syncp);
853
Chris Metcalfe5a06932010-11-01 17:00:37 -0400854 if (filter) {
855
856 /* ISSUE: Update "drop" statistics? */
857
858 tile_net_provide_linux_buffer(info, va, small);
859
860 } else {
861
862 /* Acquire the associated "skb". */
863 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
864 struct sk_buff *skb = *skb_ptr;
865
866 /* Paranoia. */
867 if (skb->data != buf)
868 panic("Corrupt linux buffer from LIPP! "
869 "VA=%p, skb=%p, skb->data=%p\n",
870 va, skb, skb->data);
871
872 /* Encode the actual packet length. */
873 skb_put(skb, len);
874
875 /* NOTE: This call also sets "skb->dev = dev". */
876 skb->protocol = eth_type_trans(skb, dev);
877
Chris Metcalfd91c6412011-03-01 12:49:53 -0500878 /* Avoid recomputing "good" TCP/UDP checksums. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400879 if (NETIO_PKT_L4_CSUM_CORRECT_M(metadata, pkt))
880 skb->ip_summed = CHECKSUM_UNNECESSARY;
881
882 netif_receive_skb(skb);
883
884 stats->rx_packets++;
885 stats->rx_bytes += len;
Chris Metcalfe5a06932010-11-01 17:00:37 -0400886 }
887
Chris Metcalfd68e2d32013-07-25 12:41:15 -0400888 u64_stats_update_end(&stats->syncp);
889
Chris Metcalf92795672012-03-30 19:23:35 -0400890 /* ISSUE: It would be nice to defer this until the packet has */
891 /* actually been processed. */
892 tile_net_return_credit(info);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400893
894 /* Consume this packet. */
895 qup->__packet_receive_read = index2;
896
897 return !filter;
898}
899
900
901/*
902 * Handle some packets for the given device on the current CPU.
903 *
Chris Metcalfd91c6412011-03-01 12:49:53 -0500904 * If "tile_net_stop()" is called on some other tile while this
905 * function is running, we will return, hopefully before that
906 * other tile asks us to call "napi_disable()".
907 *
908 * The "rotting packet" race condition occurs if a packet arrives
909 * during the extremely narrow window between the queue appearing to
910 * be empty, and the ingress interrupt being re-enabled. This happens
911 * a LOT under heavy network load.
Chris Metcalfe5a06932010-11-01 17:00:37 -0400912 */
913static int tile_net_poll(struct napi_struct *napi, int budget)
914{
915 struct net_device *dev = napi->dev;
916 struct tile_net_priv *priv = netdev_priv(dev);
917 int my_cpu = smp_processor_id();
918 struct tile_net_cpu *info = priv->cpu[my_cpu];
919 struct tile_netio_queue *queue = &info->queue;
920 netio_queue_impl_t *qsp = queue->__system_part;
921 netio_queue_user_impl_t *qup = &queue->__user_part;
922
923 unsigned int work = 0;
924
Chris Metcalfd91c6412011-03-01 12:49:53 -0500925 while (priv->active) {
Chris Metcalfe5a06932010-11-01 17:00:37 -0400926 int index = qup->__packet_receive_read;
927 if (index == qsp->__packet_receive_queue.__packet_write)
928 break;
929
930 if (tile_net_poll_aux(info, index)) {
931 if (++work >= budget)
932 goto done;
933 }
934 }
935
936 napi_complete(&info->napi);
937
Chris Metcalfd91c6412011-03-01 12:49:53 -0500938 if (!priv->active)
939 goto done;
940
941 /* Re-enable the ingress interrupt. */
Chris Metcalf0c905472011-12-01 12:58:19 -0500942 enable_percpu_irq(priv->intr_id, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400943
Chris Metcalfd91c6412011-03-01 12:49:53 -0500944 /* HACK: Avoid the "rotting packet" problem (see above). */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400945 if (qup->__packet_receive_read !=
Chris Metcalfd91c6412011-03-01 12:49:53 -0500946 qsp->__packet_receive_queue.__packet_write) {
947 /* ISSUE: Sometimes this returns zero, presumably */
948 /* because an interrupt was handled for this tile. */
949 (void)napi_reschedule(&info->napi);
950 }
Chris Metcalfe5a06932010-11-01 17:00:37 -0400951
952done:
953
Chris Metcalfd91c6412011-03-01 12:49:53 -0500954 if (priv->active)
955 tile_net_provide_needed_buffers(info);
Chris Metcalfe5a06932010-11-01 17:00:37 -0400956
957 return work;
958}
959
960
961/*
962 * Handle an ingress interrupt for the given device on the current cpu.
Chris Metcalfd91c6412011-03-01 12:49:53 -0500963 *
964 * ISSUE: Sometimes this gets called after "disable_percpu_irq()" has
965 * been called! This is probably due to "pending hypervisor downcalls".
966 *
967 * ISSUE: Is there any race condition between the "napi_schedule()" here
968 * and the "napi_complete()" call above?
Chris Metcalfe5a06932010-11-01 17:00:37 -0400969 */
970static irqreturn_t tile_net_handle_ingress_interrupt(int irq, void *dev_ptr)
971{
972 struct net_device *dev = (struct net_device *)dev_ptr;
973 struct tile_net_priv *priv = netdev_priv(dev);
974 int my_cpu = smp_processor_id();
975 struct tile_net_cpu *info = priv->cpu[my_cpu];
976
Chris Metcalfd91c6412011-03-01 12:49:53 -0500977 /* Disable the ingress interrupt. */
Chris Metcalfe5a06932010-11-01 17:00:37 -0400978 disable_percpu_irq(priv->intr_id);
979
Chris Metcalfd91c6412011-03-01 12:49:53 -0500980 /* Ignore unwanted interrupts. */
981 if (!priv->active)
982 return IRQ_HANDLED;
983
984 /* ISSUE: Sometimes "info->napi_enabled" is false here. */
985
Chris Metcalfe5a06932010-11-01 17:00:37 -0400986 napi_schedule(&info->napi);
987
988 return IRQ_HANDLED;
989}
990
991
992/*
993 * One time initialization per interface.
994 */
995static int tile_net_open_aux(struct net_device *dev)
996{
997 struct tile_net_priv *priv = netdev_priv(dev);
998
999 int ret;
1000 int dummy;
1001 unsigned int epp_lotar;
1002
1003 /*
1004 * Find out where EPP memory should be homed.
1005 */
1006 ret = hv_dev_pread(priv->hv_devhdl, 0,
1007 (HV_VirtAddr)&epp_lotar, sizeof(epp_lotar),
1008 NETIO_EPP_SHM_OFF);
1009 if (ret < 0) {
1010 pr_err("could not read epp_shm_queue lotar.\n");
1011 return -EIO;
1012 }
1013
1014 /*
1015 * Home the page on the EPP.
1016 */
1017 {
1018 int epp_home = hv_lotar_to_cpu(epp_lotar);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001019 homecache_change_page_home(priv->eq_pages, EQ_ORDER, epp_home);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001020 }
1021
1022 /*
1023 * Register the EPP shared memory queue.
1024 */
1025 {
1026 netio_ipp_address_t ea = {
1027 .va = 0,
Chris Metcalfd91c6412011-03-01 12:49:53 -05001028 .pa = __pa(priv->eq),
Chris Metcalfe5a06932010-11-01 17:00:37 -04001029 .pte = hv_pte(0),
Chris Metcalfd91c6412011-03-01 12:49:53 -05001030 .size = EQ_SIZE,
Chris Metcalfe5a06932010-11-01 17:00:37 -04001031 };
1032 ea.pte = hv_pte_set_lotar(ea.pte, epp_lotar);
1033 ea.pte = hv_pte_set_mode(ea.pte, HV_PTE_MODE_CACHE_TILE_L3);
1034 ret = hv_dev_pwrite(priv->hv_devhdl, 0,
1035 (HV_VirtAddr)&ea,
1036 sizeof(ea),
1037 NETIO_EPP_SHM_OFF);
1038 if (ret < 0)
1039 return -EIO;
1040 }
1041
1042 /*
1043 * Start LIPP/LEPP.
1044 */
1045 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1046 sizeof(dummy), NETIO_IPP_START_SHIM_OFF) < 0) {
1047 pr_warning("Failed to start LIPP/LEPP.\n");
1048 return -EIO;
1049 }
1050
1051 return 0;
1052}
1053
1054
1055/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001056 * Register with hypervisor on the current CPU.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001057 *
1058 * Strangely, this function does important things even if it "fails",
1059 * which is especially common if the link is not up yet. Hopefully
1060 * these things are all "harmless" if done twice!
1061 */
1062static void tile_net_register(void *dev_ptr)
1063{
1064 struct net_device *dev = (struct net_device *)dev_ptr;
1065 struct tile_net_priv *priv = netdev_priv(dev);
1066 int my_cpu = smp_processor_id();
1067 struct tile_net_cpu *info;
1068
1069 struct tile_netio_queue *queue;
1070
1071 /* Only network cpus can receive packets. */
1072 int queue_id =
1073 cpumask_test_cpu(my_cpu, &priv->network_cpus_map) ? 0 : 255;
1074
1075 netio_input_config_t config = {
1076 .flags = 0,
1077 .num_receive_packets = priv->network_cpus_credits,
1078 .queue_id = queue_id
1079 };
1080
1081 int ret = 0;
1082 netio_queue_impl_t *queuep;
1083
1084 PDEBUG("tile_net_register(queue_id %d)\n", queue_id);
1085
1086 if (!strcmp(dev->name, "xgbe0"))
1087 info = &__get_cpu_var(hv_xgbe0);
1088 else if (!strcmp(dev->name, "xgbe1"))
1089 info = &__get_cpu_var(hv_xgbe1);
1090 else if (!strcmp(dev->name, "gbe0"))
1091 info = &__get_cpu_var(hv_gbe0);
1092 else if (!strcmp(dev->name, "gbe1"))
1093 info = &__get_cpu_var(hv_gbe1);
1094 else
1095 BUG();
1096
1097 /* Initialize the egress timer. */
1098 init_timer(&info->egress_timer);
1099 info->egress_timer.data = (long)info;
1100 info->egress_timer.function = tile_net_handle_egress_timer;
1101
1102 priv->cpu[my_cpu] = info;
1103
1104 /*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001105 * Register ourselves with LIPP. This does a lot of stuff,
1106 * including invoking the LIPP registration code.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001107 */
1108 ret = hv_dev_pwrite(priv->hv_devhdl, 0,
1109 (HV_VirtAddr)&config,
1110 sizeof(netio_input_config_t),
1111 NETIO_IPP_INPUT_REGISTER_OFF);
1112 PDEBUG("hv_dev_pwrite(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
1113 ret);
1114 if (ret < 0) {
Chris Metcalfd91c6412011-03-01 12:49:53 -05001115 if (ret != NETIO_LINK_DOWN) {
1116 printk(KERN_DEBUG "hv_dev_pwrite "
1117 "NETIO_IPP_INPUT_REGISTER_OFF failure %d\n",
1118 ret);
1119 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001120 info->link_down = (ret == NETIO_LINK_DOWN);
1121 return;
1122 }
1123
1124 /*
1125 * Get the pointer to our queue's system part.
1126 */
1127
1128 ret = hv_dev_pread(priv->hv_devhdl, 0,
1129 (HV_VirtAddr)&queuep,
1130 sizeof(netio_queue_impl_t *),
1131 NETIO_IPP_INPUT_REGISTER_OFF);
1132 PDEBUG("hv_dev_pread(NETIO_IPP_INPUT_REGISTER_OFF) returned %d\n",
1133 ret);
1134 PDEBUG("queuep %p\n", queuep);
1135 if (ret <= 0) {
1136 /* ISSUE: Shouldn't this be a fatal error? */
1137 pr_err("hv_dev_pread NETIO_IPP_INPUT_REGISTER_OFF failure\n");
1138 return;
1139 }
1140
1141 queue = &info->queue;
1142
1143 queue->__system_part = queuep;
1144
1145 memset(&queue->__user_part, 0, sizeof(netio_queue_user_impl_t));
1146
1147 /* This is traditionally "config.num_receive_packets / 2". */
1148 queue->__user_part.__receive_credit_interval = 4;
1149 queue->__user_part.__receive_credit_remaining =
1150 queue->__user_part.__receive_credit_interval;
1151
1152 /*
1153 * Get a fastio index from the hypervisor.
1154 * ISSUE: Shouldn't this check the result?
1155 */
1156 ret = hv_dev_pread(priv->hv_devhdl, 0,
1157 (HV_VirtAddr)&queue->__user_part.__fastio_index,
1158 sizeof(queue->__user_part.__fastio_index),
1159 NETIO_IPP_GET_FASTIO_OFF);
1160 PDEBUG("hv_dev_pread(NETIO_IPP_GET_FASTIO_OFF) returned %d\n", ret);
1161
Chris Metcalfe5a06932010-11-01 17:00:37 -04001162 /* Now we are registered. */
1163 info->registered = true;
1164}
1165
1166
1167/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001168 * Deregister with hypervisor on the current CPU.
1169 *
1170 * This simply discards all our credits, so no more packets will be
1171 * delivered to this tile. There may still be packets in our queue.
1172 *
1173 * Also, disable the ingress interrupt.
1174 */
1175static void tile_net_deregister(void *dev_ptr)
1176{
1177 struct net_device *dev = (struct net_device *)dev_ptr;
1178 struct tile_net_priv *priv = netdev_priv(dev);
1179 int my_cpu = smp_processor_id();
1180 struct tile_net_cpu *info = priv->cpu[my_cpu];
1181
1182 /* Disable the ingress interrupt. */
1183 disable_percpu_irq(priv->intr_id);
1184
1185 /* Do nothing else if not registered. */
1186 if (info == NULL || !info->registered)
1187 return;
1188
1189 {
1190 struct tile_netio_queue *queue = &info->queue;
1191 netio_queue_user_impl_t *qup = &queue->__user_part;
1192
1193 /* Discard all our credits. */
1194 __netio_fastio_return_credits(qup->__fastio_index, -1);
1195 }
1196}
1197
1198
1199/*
1200 * Unregister with hypervisor on the current CPU.
1201 *
1202 * Also, disable the ingress interrupt.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001203 */
1204static void tile_net_unregister(void *dev_ptr)
1205{
1206 struct net_device *dev = (struct net_device *)dev_ptr;
1207 struct tile_net_priv *priv = netdev_priv(dev);
1208 int my_cpu = smp_processor_id();
1209 struct tile_net_cpu *info = priv->cpu[my_cpu];
1210
Chris Metcalfd91c6412011-03-01 12:49:53 -05001211 int ret;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001212 int dummy = 0;
1213
Chris Metcalfd91c6412011-03-01 12:49:53 -05001214 /* Disable the ingress interrupt. */
1215 disable_percpu_irq(priv->intr_id);
1216
1217 /* Do nothing else if not registered. */
1218 if (info == NULL || !info->registered)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001219 return;
1220
Chris Metcalfd91c6412011-03-01 12:49:53 -05001221 /* Unregister ourselves with LIPP/LEPP. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001222 ret = hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1223 sizeof(dummy), NETIO_IPP_INPUT_UNREGISTER_OFF);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001224 if (ret < 0)
1225 panic("Failed to unregister with LIPP/LEPP!\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001226
Chris Metcalfd91c6412011-03-01 12:49:53 -05001227 /* Discard all packets still in our NetIO queue. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001228 tile_net_discard_packets(dev);
1229
1230 /* Reset state. */
1231 info->num_needed_small_buffers = 0;
1232 info->num_needed_large_buffers = 0;
1233
1234 /* Cancel egress timer. */
1235 del_timer(&info->egress_timer);
1236 info->egress_timer_scheduled = false;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001237}
1238
1239
1240/*
1241 * Helper function for "tile_net_stop()".
1242 *
1243 * Also used to handle registration failure in "tile_net_open_inner()",
Chris Metcalfd91c6412011-03-01 12:49:53 -05001244 * when the various extra steps in "tile_net_stop()" are not necessary.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001245 */
1246static void tile_net_stop_aux(struct net_device *dev)
1247{
1248 struct tile_net_priv *priv = netdev_priv(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001249 int i;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001250
1251 int dummy = 0;
1252
Chris Metcalfd91c6412011-03-01 12:49:53 -05001253 /*
1254 * Unregister all tiles, so LIPP will stop delivering packets.
1255 * Also, delete all the "napi" objects (sequentially, to protect
1256 * "dev->napi_list").
1257 */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001258 on_each_cpu(tile_net_unregister, (void *)dev, 1);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001259 for_each_online_cpu(i) {
1260 struct tile_net_cpu *info = priv->cpu[i];
1261 if (info != NULL && info->registered) {
1262 netif_napi_del(&info->napi);
1263 info->registered = false;
1264 }
1265 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001266
1267 /* Stop LIPP/LEPP. */
1268 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1269 sizeof(dummy), NETIO_IPP_STOP_SHIM_OFF) < 0)
1270 panic("Failed to stop LIPP/LEPP!\n");
1271
Rusty Russell3db1cd52011-12-19 13:56:45 +00001272 priv->partly_opened = false;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001273}
1274
1275
1276/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001277 * Disable NAPI for the given device on the current cpu.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001278 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001279static void tile_net_stop_disable(void *dev_ptr)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001280{
1281 struct net_device *dev = (struct net_device *)dev_ptr;
1282 struct tile_net_priv *priv = netdev_priv(dev);
1283 int my_cpu = smp_processor_id();
1284 struct tile_net_cpu *info = priv->cpu[my_cpu];
1285
Chris Metcalfe5a06932010-11-01 17:00:37 -04001286 /* Disable NAPI if needed. */
1287 if (info != NULL && info->napi_enabled) {
1288 napi_disable(&info->napi);
1289 info->napi_enabled = false;
1290 }
1291}
1292
1293
1294/*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001295 * Enable NAPI and the ingress interrupt for the given device
1296 * on the current cpu.
1297 *
1298 * ISSUE: Only do this for "network cpus"?
Chris Metcalfe5a06932010-11-01 17:00:37 -04001299 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001300static void tile_net_open_enable(void *dev_ptr)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001301{
1302 struct net_device *dev = (struct net_device *)dev_ptr;
1303 struct tile_net_priv *priv = netdev_priv(dev);
1304 int my_cpu = smp_processor_id();
1305 struct tile_net_cpu *info = priv->cpu[my_cpu];
1306
Chris Metcalfe5a06932010-11-01 17:00:37 -04001307 /* Enable NAPI. */
1308 napi_enable(&info->napi);
1309 info->napi_enabled = true;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001310
1311 /* Enable the ingress interrupt. */
Chris Metcalf0c905472011-12-01 12:58:19 -05001312 enable_percpu_irq(priv->intr_id, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001313}
1314
1315
1316/*
1317 * tile_net_open_inner does most of the work of bringing up the interface.
1318 * It's called from tile_net_open(), and also from tile_net_retry_open().
1319 * The return value is 0 if the interface was brought up, < 0 if
1320 * tile_net_open() should return the return value as an error, and > 0 if
1321 * tile_net_open() should return success and schedule a work item to
1322 * periodically retry the bringup.
1323 */
1324static int tile_net_open_inner(struct net_device *dev)
1325{
1326 struct tile_net_priv *priv = netdev_priv(dev);
1327 int my_cpu = smp_processor_id();
1328 struct tile_net_cpu *info;
1329 struct tile_netio_queue *queue;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001330 int result = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001331 int i;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001332 int dummy = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001333
1334 /*
1335 * First try to register just on the local CPU, and handle any
1336 * semi-expected "link down" failure specially. Note that we
1337 * do NOT call "tile_net_stop_aux()", unlike below.
1338 */
1339 tile_net_register(dev);
1340 info = priv->cpu[my_cpu];
1341 if (!info->registered) {
1342 if (info->link_down)
1343 return 1;
1344 return -EAGAIN;
1345 }
1346
1347 /*
1348 * Now register everywhere else. If any registration fails,
1349 * even for "link down" (which might not be possible), we
Chris Metcalfd91c6412011-03-01 12:49:53 -05001350 * clean up using "tile_net_stop_aux()". Also, add all the
1351 * "napi" objects (sequentially, to protect "dev->napi_list").
1352 * ISSUE: Only use "netif_napi_add()" for "network cpus"?
Chris Metcalfe5a06932010-11-01 17:00:37 -04001353 */
1354 smp_call_function(tile_net_register, (void *)dev, 1);
1355 for_each_online_cpu(i) {
Chris Metcalfd91c6412011-03-01 12:49:53 -05001356 struct tile_net_cpu *info = priv->cpu[i];
1357 if (info->registered)
1358 netif_napi_add(dev, &info->napi, tile_net_poll, 64);
1359 else
1360 result = -EAGAIN;
1361 }
1362 if (result != 0) {
1363 tile_net_stop_aux(dev);
1364 return result;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001365 }
1366
1367 queue = &info->queue;
1368
Chris Metcalfd91c6412011-03-01 12:49:53 -05001369 if (priv->intr_id == 0) {
1370 unsigned int irq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001371
Chris Metcalfd91c6412011-03-01 12:49:53 -05001372 /*
1373 * Acquire the irq allocated by the hypervisor. Every
1374 * queue gets the same irq. The "__intr_id" field is
1375 * "1 << irq", so we use "__ffs()" to extract "irq".
1376 */
1377 priv->intr_id = queue->__system_part->__intr_id;
1378 BUG_ON(priv->intr_id == 0);
1379 irq = __ffs(priv->intr_id);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001380
Chris Metcalfd91c6412011-03-01 12:49:53 -05001381 /*
1382 * Register the ingress interrupt handler for this
1383 * device, permanently.
1384 *
1385 * We used to call "free_irq()" in "tile_net_stop()",
1386 * and then re-register the handler here every time,
1387 * but that caused DNP errors in "handle_IRQ_event()"
1388 * because "desc->action" was NULL. See bug 9143.
1389 */
1390 tile_irq_activate(irq, TILE_IRQ_PERCPU);
1391 BUG_ON(request_irq(irq, tile_net_handle_ingress_interrupt,
1392 0, dev->name, (void *)dev) != 0);
1393 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001394
Chris Metcalfd91c6412011-03-01 12:49:53 -05001395 {
Chris Metcalfe5a06932010-11-01 17:00:37 -04001396 /* Allocate initial buffers. */
1397
1398 int max_buffers =
1399 priv->network_cpus_count * priv->network_cpus_credits;
1400
1401 info->num_needed_small_buffers =
1402 min(LIPP_SMALL_BUFFERS, max_buffers);
1403
1404 info->num_needed_large_buffers =
1405 min(LIPP_LARGE_BUFFERS, max_buffers);
1406
1407 tile_net_provide_needed_buffers(info);
1408
1409 if (info->num_needed_small_buffers != 0 ||
1410 info->num_needed_large_buffers != 0)
1411 panic("Insufficient memory for buffer stack!");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001412 }
1413
Chris Metcalfd91c6412011-03-01 12:49:53 -05001414 /* We are about to be active. */
1415 priv->active = true;
1416
1417 /* Make sure "active" is visible to all tiles. */
1418 mb();
1419
1420 /* On each tile, enable NAPI and the ingress interrupt. */
1421 on_each_cpu(tile_net_open_enable, (void *)dev, 1);
1422
1423 /* Start LIPP/LEPP and activate "ingress" at the shim. */
1424 if (hv_dev_pwrite(priv->hv_devhdl, 0, (HV_VirtAddr)&dummy,
1425 sizeof(dummy), NETIO_IPP_INPUT_INIT_OFF) < 0)
1426 panic("Failed to activate the LIPP Shim!\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04001427
1428 /* Start our transmit queue. */
1429 netif_start_queue(dev);
1430
1431 return 0;
1432}
1433
1434
1435/*
1436 * Called periodically to retry bringing up the NetIO interface,
1437 * if it doesn't come up cleanly during tile_net_open().
1438 */
1439static void tile_net_open_retry(struct work_struct *w)
1440{
1441 struct delayed_work *dw =
1442 container_of(w, struct delayed_work, work);
1443
1444 struct tile_net_priv *priv =
1445 container_of(dw, struct tile_net_priv, retry_work);
1446
1447 /*
1448 * Try to bring the NetIO interface up. If it fails, reschedule
1449 * ourselves to try again later; otherwise, tell Linux we now have
1450 * a working link. ISSUE: What if the return value is negative?
1451 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001452 if (tile_net_open_inner(priv->dev) != 0)
1453 schedule_delayed_work(&priv->retry_work,
1454 TILE_NET_RETRY_INTERVAL);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001455 else
1456 netif_carrier_on(priv->dev);
1457}
1458
1459
1460/*
1461 * Called when a network interface is made active.
1462 *
1463 * Returns 0 on success, negative value on failure.
1464 *
1465 * The open entry point is called when a network interface is made
1466 * active by the system (IFF_UP). At this point all resources needed
1467 * for transmit and receive operations are allocated, the interrupt
Chris Metcalfd91c6412011-03-01 12:49:53 -05001468 * handler is registered with the OS (if needed), the watchdog timer
1469 * is started, and the stack is notified that the interface is ready.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001470 *
1471 * If the actual link is not available yet, then we tell Linux that
1472 * we have no carrier, and we keep checking until the link comes up.
1473 */
1474static int tile_net_open(struct net_device *dev)
1475{
1476 int ret = 0;
1477 struct tile_net_priv *priv = netdev_priv(dev);
1478
1479 /*
1480 * We rely on priv->partly_opened to tell us if this is the
1481 * first time this interface is being brought up. If it is
1482 * set, the IPP was already initialized and should not be
1483 * initialized again.
1484 */
1485 if (!priv->partly_opened) {
1486
1487 int count;
1488 int credits;
1489
1490 /* Initialize LIPP/LEPP, and start the Shim. */
1491 ret = tile_net_open_aux(dev);
1492 if (ret < 0) {
1493 pr_err("tile_net_open_aux failed: %d\n", ret);
1494 return ret;
1495 }
1496
1497 /* Analyze the network cpus. */
1498
1499 if (network_cpus_used)
1500 cpumask_copy(&priv->network_cpus_map,
1501 &network_cpus_map);
1502 else
1503 cpumask_copy(&priv->network_cpus_map, cpu_online_mask);
1504
1505
1506 count = cpumask_weight(&priv->network_cpus_map);
1507
1508 /* Limit credits to available buffers, and apply min. */
1509 credits = max(16, (LIPP_LARGE_BUFFERS / count) & ~1);
1510
1511 /* Apply "GBE" max limit. */
1512 /* ISSUE: Use higher limit for XGBE? */
1513 credits = min(NETIO_MAX_RECEIVE_PKTS, credits);
1514
1515 priv->network_cpus_count = count;
1516 priv->network_cpus_credits = credits;
1517
1518#ifdef TILE_NET_DEBUG
1519 pr_info("Using %d network cpus, with %d credits each\n",
1520 priv->network_cpus_count, priv->network_cpus_credits);
1521#endif
1522
Rusty Russell3db1cd52011-12-19 13:56:45 +00001523 priv->partly_opened = true;
Chris Metcalfd91c6412011-03-01 12:49:53 -05001524
1525 } else {
1526 /* FIXME: Is this possible? */
1527 /* printk("Already partly opened.\n"); */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001528 }
1529
1530 /*
1531 * Attempt to bring up the link.
1532 */
1533 ret = tile_net_open_inner(dev);
1534 if (ret <= 0) {
1535 if (ret == 0)
1536 netif_carrier_on(dev);
1537 return ret;
1538 }
1539
1540 /*
1541 * We were unable to bring up the NetIO interface, but we want to
1542 * try again in a little bit. Tell Linux that we have no carrier
1543 * so it doesn't try to use the interface before the link comes up
1544 * and then remember to try again later.
1545 */
1546 netif_carrier_off(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05001547 schedule_delayed_work(&priv->retry_work, TILE_NET_RETRY_INTERVAL);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001548
1549 return 0;
1550}
1551
1552
Chris Metcalfd91c6412011-03-01 12:49:53 -05001553static int tile_net_drain_lipp_buffers(struct tile_net_priv *priv)
Chris Metcalfe5a06932010-11-01 17:00:37 -04001554{
Chris Metcalfd91c6412011-03-01 12:49:53 -05001555 int n = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001556
Chris Metcalfd91c6412011-03-01 12:49:53 -05001557 /* Drain all the LIPP buffers. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001558 while (true) {
Chris Metcalf92795672012-03-30 19:23:35 -04001559 unsigned int buffer;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001560
1561 /* NOTE: This should never fail. */
1562 if (hv_dev_pread(priv->hv_devhdl, 0, (HV_VirtAddr)&buffer,
1563 sizeof(buffer), NETIO_IPP_DRAIN_OFF) < 0)
1564 break;
1565
1566 /* Stop when done. */
1567 if (buffer == 0)
1568 break;
1569
1570 {
1571 /* Convert "linux_buffer_t" to "va". */
1572 void *va = __va((phys_addr_t)(buffer >> 1) << 7);
1573
1574 /* Acquire the associated "skb". */
1575 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
1576 struct sk_buff *skb = *skb_ptr;
1577
1578 kfree_skb(skb);
1579 }
Chris Metcalfd91c6412011-03-01 12:49:53 -05001580
1581 n++;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001582 }
1583
Chris Metcalfd91c6412011-03-01 12:49:53 -05001584 return n;
1585}
1586
1587
1588/*
1589 * Disables a network interface.
1590 *
1591 * Returns 0, this is not allowed to fail.
1592 *
1593 * The close entry point is called when an interface is de-activated
1594 * by the OS. The hardware is still under the drivers control, but
1595 * needs to be disabled. A global MAC reset is issued to stop the
1596 * hardware, and all transmit and receive resources are freed.
1597 *
1598 * ISSUE: How closely does "netif_running(dev)" mirror "priv->active"?
1599 *
1600 * Before we are called by "__dev_close()", "netif_running()" will
1601 * have been cleared, so no NEW calls to "tile_net_poll()" will be
1602 * made by "netpoll_poll_dev()".
1603 *
1604 * Often, this can cause some tiles to still have packets in their
1605 * queues, so we must call "tile_net_discard_packets()" later.
1606 *
1607 * Note that some other tile may still be INSIDE "tile_net_poll()",
1608 * and in fact, many will be, if there is heavy network load.
1609 *
1610 * Calling "on_each_cpu(tile_net_stop_disable, (void *)dev, 1)" when
1611 * any tile is still "napi_schedule()"'d will induce a horrible crash
1612 * when "msleep()" is called. This includes tiles which are inside
1613 * "tile_net_poll()" which have not yet called "napi_complete()".
1614 *
1615 * So, we must first try to wait long enough for other tiles to finish
1616 * with any current "tile_net_poll()" call, and, hopefully, to clear
1617 * the "scheduled" flag. ISSUE: It is unclear what happens to tiles
1618 * which have called "napi_schedule()" but which had not yet tried to
1619 * call "tile_net_poll()", or which exhausted their budget inside
1620 * "tile_net_poll()" just before this function was called.
1621 */
1622static int tile_net_stop(struct net_device *dev)
1623{
1624 struct tile_net_priv *priv = netdev_priv(dev);
1625
1626 PDEBUG("tile_net_stop()\n");
1627
1628 /* Start discarding packets. */
1629 priv->active = false;
1630
1631 /* Make sure "active" is visible to all tiles. */
1632 mb();
1633
1634 /*
1635 * On each tile, make sure no NEW packets get delivered, and
1636 * disable the ingress interrupt.
1637 *
1638 * Note that the ingress interrupt can fire AFTER this,
1639 * presumably due to packets which were recently delivered,
1640 * but it will have no effect.
1641 */
1642 on_each_cpu(tile_net_deregister, (void *)dev, 1);
1643
1644 /* Optimistically drain LIPP buffers. */
1645 (void)tile_net_drain_lipp_buffers(priv);
1646
1647 /* ISSUE: Only needed if not yet fully open. */
1648 cancel_delayed_work_sync(&priv->retry_work);
1649
1650 /* Can't transmit any more. */
1651 netif_stop_queue(dev);
1652
1653 /* Disable NAPI on each tile. */
1654 on_each_cpu(tile_net_stop_disable, (void *)dev, 1);
1655
1656 /*
1657 * Drain any remaining LIPP buffers. NOTE: This "printk()"
1658 * has never been observed, but in theory it could happen.
1659 */
1660 if (tile_net_drain_lipp_buffers(priv) != 0)
1661 printk("Had to drain some extra LIPP buffers!\n");
1662
Chris Metcalfe5a06932010-11-01 17:00:37 -04001663 /* Stop LIPP/LEPP. */
1664 tile_net_stop_aux(dev);
1665
Chris Metcalfe5a06932010-11-01 17:00:37 -04001666 /*
Chris Metcalfd91c6412011-03-01 12:49:53 -05001667 * ISSUE: It appears that, in practice anyway, by the time we
1668 * get here, there are no pending completions, but just in case,
1669 * we free (all of) them anyway.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001670 */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001671 while (tile_net_lepp_free_comps(dev, true))
1672 /* loop */;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001673
Chris Metcalfd07bd862011-05-02 16:36:48 -04001674 /* Wipe the EPP queue, and wait till the stores hit the EPP. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001675 memset(priv->eq, 0, sizeof(lepp_queue_t));
Chris Metcalfd07bd862011-05-02 16:36:48 -04001676 mb();
Chris Metcalfe5a06932010-11-01 17:00:37 -04001677
1678 return 0;
1679}
1680
1681
1682/*
1683 * Prepare the "frags" info for the resulting LEPP command.
1684 *
1685 * If needed, flush the memory used by the frags.
1686 */
1687static unsigned int tile_net_tx_frags(lepp_frag_t *frags,
1688 struct sk_buff *skb,
1689 void *b_data, unsigned int b_len)
1690{
1691 unsigned int i, n = 0;
1692
1693 struct skb_shared_info *sh = skb_shinfo(skb);
1694
1695 phys_addr_t cpa;
1696
1697 if (b_len != 0) {
1698
1699 if (!hash_default)
Chris Metcalf63b7ca62011-02-28 15:48:39 -05001700 finv_buffer_remote(b_data, b_len, 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001701
1702 cpa = __pa(b_data);
1703 frags[n].cpa_lo = cpa;
1704 frags[n].cpa_hi = cpa >> 32;
1705 frags[n].length = b_len;
1706 frags[n].hash_for_home = hash_default;
1707 n++;
1708 }
1709
1710 for (i = 0; i < sh->nr_frags; i++) {
1711
1712 skb_frag_t *f = &sh->frags[i];
Chris Metcalf781a5e92011-12-01 12:56:03 -05001713 unsigned long pfn = page_to_pfn(skb_frag_page(f));
Chris Metcalfe5a06932010-11-01 17:00:37 -04001714
1715 /* FIXME: Compute "hash_for_home" properly. */
1716 /* ISSUE: The hypervisor checks CHIP_HAS_REV1_DMA_PACKETS(). */
1717 int hash_for_home = hash_default;
1718
1719 /* FIXME: Hmmm. */
1720 if (!hash_default) {
1721 void *va = pfn_to_kaddr(pfn) + f->page_offset;
Chris Metcalf781a5e92011-12-01 12:56:03 -05001722 BUG_ON(PageHighMem(skb_frag_page(f)));
Chris Metcalf92795672012-03-30 19:23:35 -04001723 finv_buffer_remote(va, skb_frag_size(f), 0);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001724 }
1725
1726 cpa = ((phys_addr_t)pfn << PAGE_SHIFT) + f->page_offset;
1727 frags[n].cpa_lo = cpa;
1728 frags[n].cpa_hi = cpa >> 32;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001729 frags[n].length = skb_frag_size(f);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001730 frags[n].hash_for_home = hash_for_home;
1731 n++;
1732 }
1733
1734 return n;
1735}
1736
1737
1738/*
1739 * This function takes "skb", consisting of a header template and a
1740 * payload, and hands it to LEPP, to emit as one or more segments,
1741 * each consisting of a possibly modified header, plus a piece of the
1742 * payload, via a process known as "tcp segmentation offload".
1743 *
1744 * Usually, "data" will contain the header template, of size "sh_len",
1745 * and "sh->frags" will contain "skb->data_len" bytes of payload, and
1746 * there will be "sh->gso_segs" segments.
1747 *
1748 * Sometimes, if "sendfile()" requires copying, we will be called with
1749 * "data" containing the header and payload, with "frags" being empty.
1750 *
Chris Metcalf92795672012-03-30 19:23:35 -04001751 * Sometimes, for example when using NFS over TCP, a single segment can
1752 * span 3 fragments, which must be handled carefully in LEPP.
Chris Metcalfe5a06932010-11-01 17:00:37 -04001753 *
1754 * See "emulate_large_send_offload()" for some reference code, which
1755 * does not handle checksumming.
1756 *
1757 * ISSUE: How do we make sure that high memory DMA does not migrate?
1758 */
1759static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
1760{
1761 struct tile_net_priv *priv = netdev_priv(dev);
1762 int my_cpu = smp_processor_id();
1763 struct tile_net_cpu *info = priv->cpu[my_cpu];
1764 struct tile_net_stats_t *stats = &info->stats;
1765
1766 struct skb_shared_info *sh = skb_shinfo(skb);
1767
1768 unsigned char *data = skb->data;
1769
1770 /* The ip header follows the ethernet header. */
1771 struct iphdr *ih = ip_hdr(skb);
1772 unsigned int ih_len = ih->ihl * 4;
1773
1774 /* Note that "nh == ih", by definition. */
1775 unsigned char *nh = skb_network_header(skb);
1776 unsigned int eh_len = nh - data;
1777
1778 /* The tcp header follows the ip header. */
1779 struct tcphdr *th = (struct tcphdr *)(nh + ih_len);
1780 unsigned int th_len = th->doff * 4;
1781
1782 /* The total number of header bytes. */
1783 /* NOTE: This may be less than skb_headlen(skb). */
1784 unsigned int sh_len = eh_len + ih_len + th_len;
1785
1786 /* The number of payload bytes at "skb->data + sh_len". */
1787 /* This is non-zero for sendfile() without HIGHDMA. */
1788 unsigned int b_len = skb_headlen(skb) - sh_len;
1789
1790 /* The total number of payload bytes. */
1791 unsigned int d_len = b_len + skb->data_len;
1792
1793 /* The maximum payload size. */
1794 unsigned int p_len = sh->gso_size;
1795
1796 /* The total number of segments. */
1797 unsigned int num_segs = sh->gso_segs;
1798
1799 /* The temporary copy of the command. */
1800 u32 cmd_body[(LEPP_MAX_CMD_SIZE + 3) / 4];
1801 lepp_tso_cmd_t *cmd = (lepp_tso_cmd_t *)cmd_body;
1802
1803 /* Analyze the "frags". */
1804 unsigned int num_frags =
1805 tile_net_tx_frags(cmd->frags, skb, data + sh_len, b_len);
1806
1807 /* The size of the command, including frags and header. */
1808 size_t cmd_size = LEPP_TSO_CMD_SIZE(num_frags, sh_len);
1809
1810 /* The command header. */
1811 lepp_tso_cmd_t cmd_init = {
1812 .tso = true,
1813 .header_size = sh_len,
1814 .ip_offset = eh_len,
1815 .tcp_offset = eh_len + ih_len,
1816 .payload_size = p_len,
1817 .num_frags = num_frags,
1818 };
1819
1820 unsigned long irqflags;
1821
Chris Metcalfd91c6412011-03-01 12:49:53 -05001822 lepp_queue_t *eq = priv->eq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001823
Chris Metcalfd91c6412011-03-01 12:49:53 -05001824 struct sk_buff *olds[8];
1825 unsigned int wanted = 8;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001826 unsigned int i, nolds = 0;
1827
1828 unsigned int cmd_head, cmd_tail, cmd_next;
1829 unsigned int comp_tail;
1830
Chris Metcalfe5a06932010-11-01 17:00:37 -04001831
1832 /* Paranoia. */
1833 BUG_ON(skb->protocol != htons(ETH_P_IP));
1834 BUG_ON(ih->protocol != IPPROTO_TCP);
1835 BUG_ON(skb->ip_summed != CHECKSUM_PARTIAL);
1836 BUG_ON(num_frags > LEPP_MAX_FRAGS);
1837 /*--BUG_ON(num_segs != (d_len + (p_len - 1)) / p_len); */
1838 BUG_ON(num_segs <= 1);
1839
1840
1841 /* Finish preparing the command. */
1842
1843 /* Copy the command header. */
1844 *cmd = cmd_init;
1845
1846 /* Copy the "header". */
1847 memcpy(&cmd->frags[num_frags], data, sh_len);
1848
1849
1850 /* Prefetch and wait, to minimize time spent holding the spinlock. */
1851 prefetch_L1(&eq->comp_tail);
1852 prefetch_L1(&eq->cmd_tail);
1853 mb();
1854
1855
1856 /* Enqueue the command. */
1857
Chris Metcalfd91c6412011-03-01 12:49:53 -05001858 spin_lock_irqsave(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001859
Chris Metcalf92795672012-03-30 19:23:35 -04001860 /* Handle completions if needed to make room. */
1861 /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001862 if (lepp_num_free_comp_slots(eq) == 0) {
1863 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
1864 if (nolds == 0) {
1865busy:
1866 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
1867 return NETDEV_TX_BUSY;
1868 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04001869 }
1870
1871 cmd_head = eq->cmd_head;
1872 cmd_tail = eq->cmd_tail;
1873
Chris Metcalfe5a06932010-11-01 17:00:37 -04001874 /* Prepare to advance, detecting full queue. */
Chris Metcalf92795672012-03-30 19:23:35 -04001875 /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001876 cmd_next = cmd_tail + cmd_size;
1877 if (cmd_tail < cmd_head && cmd_next >= cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001878 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001879 if (cmd_next > LEPP_CMD_LIMIT) {
1880 cmd_next = 0;
1881 if (cmd_next == cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001882 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001883 }
1884
1885 /* Copy the command. */
1886 memcpy(&eq->cmds[cmd_tail], cmd, cmd_size);
1887
1888 /* Advance. */
1889 cmd_tail = cmd_next;
1890
1891 /* Record "skb" for eventual freeing. */
1892 comp_tail = eq->comp_tail;
1893 eq->comps[comp_tail] = skb;
1894 LEPP_QINC(comp_tail);
1895 eq->comp_tail = comp_tail;
1896
1897 /* Flush before allowing LEPP to handle the command. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05001898 /* ISSUE: Is this the optimal location for the flush? */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001899 __insn_mf();
1900
1901 eq->cmd_tail = cmd_tail;
1902
Chris Metcalfd91c6412011-03-01 12:49:53 -05001903 /* NOTE: Using "4" here is more efficient than "0" or "2", */
1904 /* and, strangely, more efficient than pre-checking the number */
1905 /* of available completions, and comparing it to 4. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04001906 if (nolds == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001907 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
1908
1909 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001910
1911 /* Handle completions. */
1912 for (i = 0; i < nolds; i++)
1913 kfree_skb(olds[i]);
1914
1915 /* Update stats. */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04001916 u64_stats_update_begin(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001917 stats->tx_packets += num_segs;
1918 stats->tx_bytes += (num_segs * sh_len) + d_len;
Chris Metcalfd68e2d32013-07-25 12:41:15 -04001919 u64_stats_update_end(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001920
1921 /* Make sure the egress timer is scheduled. */
1922 tile_net_schedule_egress_timer(info);
1923
1924 return NETDEV_TX_OK;
1925}
1926
1927
1928/*
1929 * Transmit a packet (called by the kernel via "hard_start_xmit" hook).
1930 */
1931static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
1932{
1933 struct tile_net_priv *priv = netdev_priv(dev);
1934 int my_cpu = smp_processor_id();
1935 struct tile_net_cpu *info = priv->cpu[my_cpu];
1936 struct tile_net_stats_t *stats = &info->stats;
1937
1938 unsigned long irqflags;
1939
1940 struct skb_shared_info *sh = skb_shinfo(skb);
1941
1942 unsigned int len = skb->len;
1943 unsigned char *data = skb->data;
1944
Shan Wei96339d62011-04-22 19:07:41 +08001945 unsigned int csum_start = skb_checksum_start_offset(skb);
Chris Metcalfe5a06932010-11-01 17:00:37 -04001946
1947 lepp_frag_t frags[LEPP_MAX_FRAGS];
1948
1949 unsigned int num_frags;
1950
Chris Metcalfd91c6412011-03-01 12:49:53 -05001951 lepp_queue_t *eq = priv->eq;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001952
Chris Metcalfd91c6412011-03-01 12:49:53 -05001953 struct sk_buff *olds[8];
1954 unsigned int wanted = 8;
Chris Metcalfe5a06932010-11-01 17:00:37 -04001955 unsigned int i, nolds = 0;
1956
1957 unsigned int cmd_size = sizeof(lepp_cmd_t);
1958
1959 unsigned int cmd_head, cmd_tail, cmd_next;
1960 unsigned int comp_tail;
1961
1962 lepp_cmd_t cmds[LEPP_MAX_FRAGS];
1963
Chris Metcalfe5a06932010-11-01 17:00:37 -04001964
1965 /*
1966 * This is paranoia, since we think that if the link doesn't come
1967 * up, telling Linux we have no carrier will keep it from trying
1968 * to transmit. If it does, though, we can't execute this routine,
1969 * since data structures we depend on aren't set up yet.
1970 */
1971 if (!info->registered)
1972 return NETDEV_TX_BUSY;
1973
1974
1975 /* Save the timestamp. */
1976 dev->trans_start = jiffies;
1977
1978
1979#ifdef TILE_NET_PARANOIA
1980#if CHIP_HAS_CBOX_HOME_MAP()
1981 if (hash_default) {
1982 HV_PTE pte = *virt_to_pte(current->mm, (unsigned long)data);
1983 if (hv_pte_get_mode(pte) != HV_PTE_MODE_CACHE_HASH_L3)
Chris Metcalfd91c6412011-03-01 12:49:53 -05001984 panic("Non-HFH egress buffer! VA=%p Mode=%d PTE=%llx",
1985 data, hv_pte_get_mode(pte), hv_pte_val(pte));
Chris Metcalfe5a06932010-11-01 17:00:37 -04001986 }
1987#endif
1988#endif
1989
1990
1991#ifdef TILE_NET_DUMP_PACKETS
1992 /* ISSUE: Does not dump the "frags". */
1993 dump_packet(data, skb_headlen(skb), "tx");
1994#endif /* TILE_NET_DUMP_PACKETS */
1995
1996
1997 if (sh->gso_size != 0)
1998 return tile_net_tx_tso(skb, dev);
1999
2000
2001 /* Prepare the commands. */
2002
2003 num_frags = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
2004
2005 for (i = 0; i < num_frags; i++) {
2006
2007 bool final = (i == num_frags - 1);
2008
2009 lepp_cmd_t cmd = {
2010 .cpa_lo = frags[i].cpa_lo,
2011 .cpa_hi = frags[i].cpa_hi,
2012 .length = frags[i].length,
2013 .hash_for_home = frags[i].hash_for_home,
2014 .send_completion = final,
2015 .end_of_packet = final
2016 };
2017
2018 if (i == 0 && skb->ip_summed == CHECKSUM_PARTIAL) {
2019 cmd.compute_checksum = 1;
2020 cmd.checksum_data.bits.start_byte = csum_start;
2021 cmd.checksum_data.bits.count = len - csum_start;
2022 cmd.checksum_data.bits.destination_byte =
2023 csum_start + skb->csum_offset;
2024 }
2025
2026 cmds[i] = cmd;
2027 }
2028
2029
2030 /* Prefetch and wait, to minimize time spent holding the spinlock. */
2031 prefetch_L1(&eq->comp_tail);
2032 prefetch_L1(&eq->cmd_tail);
2033 mb();
2034
2035
2036 /* Enqueue the commands. */
2037
Chris Metcalfd91c6412011-03-01 12:49:53 -05002038 spin_lock_irqsave(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002039
Chris Metcalf92795672012-03-30 19:23:35 -04002040 /* Handle completions if needed to make room. */
2041 /* NOTE: Return NETDEV_TX_BUSY if there is still no room. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05002042 if (lepp_num_free_comp_slots(eq) == 0) {
2043 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 0);
2044 if (nolds == 0) {
2045busy:
2046 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
2047 return NETDEV_TX_BUSY;
2048 }
Chris Metcalfe5a06932010-11-01 17:00:37 -04002049 }
2050
2051 cmd_head = eq->cmd_head;
2052 cmd_tail = eq->cmd_tail;
2053
Chris Metcalfe5a06932010-11-01 17:00:37 -04002054 /* Copy the commands, or fail. */
Chris Metcalf92795672012-03-30 19:23:35 -04002055 /* NOTE: Return NETDEV_TX_BUSY if the queue is full. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002056 for (i = 0; i < num_frags; i++) {
2057
2058 /* Prepare to advance, detecting full queue. */
2059 cmd_next = cmd_tail + cmd_size;
2060 if (cmd_tail < cmd_head && cmd_next >= cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002061 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002062 if (cmd_next > LEPP_CMD_LIMIT) {
2063 cmd_next = 0;
2064 if (cmd_next == cmd_head)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002065 goto busy;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002066 }
2067
2068 /* Copy the command. */
2069 *(lepp_cmd_t *)&eq->cmds[cmd_tail] = cmds[i];
2070
2071 /* Advance. */
2072 cmd_tail = cmd_next;
2073 }
2074
2075 /* Record "skb" for eventual freeing. */
2076 comp_tail = eq->comp_tail;
2077 eq->comps[comp_tail] = skb;
2078 LEPP_QINC(comp_tail);
2079 eq->comp_tail = comp_tail;
2080
2081 /* Flush before allowing LEPP to handle the command. */
Chris Metcalfd91c6412011-03-01 12:49:53 -05002082 /* ISSUE: Is this the optimal location for the flush? */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002083 __insn_mf();
2084
2085 eq->cmd_tail = cmd_tail;
2086
Chris Metcalfd91c6412011-03-01 12:49:53 -05002087 /* NOTE: Using "4" here is more efficient than "0" or "2", */
2088 /* and, strangely, more efficient than pre-checking the number */
2089 /* of available completions, and comparing it to 4. */
Chris Metcalfe5a06932010-11-01 17:00:37 -04002090 if (nolds == 0)
Chris Metcalfd91c6412011-03-01 12:49:53 -05002091 nolds = tile_net_lepp_grab_comps(eq, olds, wanted, 4);
2092
2093 spin_unlock_irqrestore(&priv->eq_lock, irqflags);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002094
2095 /* Handle completions. */
2096 for (i = 0; i < nolds; i++)
2097 kfree_skb(olds[i]);
2098
2099 /* HACK: Track "expanded" size for short packets (e.g. 42 < 60). */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002100 u64_stats_update_begin(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002101 stats->tx_packets++;
2102 stats->tx_bytes += ((len >= ETH_ZLEN) ? len : ETH_ZLEN);
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002103 u64_stats_update_end(&stats->syncp);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002104
2105 /* Make sure the egress timer is scheduled. */
2106 tile_net_schedule_egress_timer(info);
2107
2108 return NETDEV_TX_OK;
2109}
2110
2111
2112/*
2113 * Deal with a transmit timeout.
2114 */
2115static void tile_net_tx_timeout(struct net_device *dev)
2116{
2117 PDEBUG("tile_net_tx_timeout()\n");
2118 PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
2119 jiffies - dev->trans_start);
2120
2121 /* XXX: ISSUE: This doesn't seem useful for us. */
2122 netif_wake_queue(dev);
2123}
2124
2125
2126/*
2127 * Ioctl commands.
2128 */
2129static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2130{
2131 return -EOPNOTSUPP;
2132}
2133
2134
2135/*
2136 * Get System Network Statistics.
2137 *
2138 * Returns the address of the device statistics structure.
2139 */
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002140static struct rtnl_link_stats64 *tile_net_get_stats64(struct net_device *dev,
2141 struct rtnl_link_stats64 *stats)
Chris Metcalfe5a06932010-11-01 17:00:37 -04002142{
2143 struct tile_net_priv *priv = netdev_priv(dev);
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002144 u64 rx_packets = 0, tx_packets = 0;
2145 u64 rx_bytes = 0, tx_bytes = 0;
2146 u64 rx_errors = 0, rx_dropped = 0;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002147 int i;
2148
2149 for_each_online_cpu(i) {
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002150 struct tile_net_stats_t *cpu_stats;
2151 u64 trx_packets, ttx_packets, trx_bytes, ttx_bytes;
2152 u64 trx_errors, trx_dropped;
2153 unsigned int start;
2154
2155 if (priv->cpu[i] == NULL)
2156 continue;
2157 cpu_stats = &priv->cpu[i]->stats;
2158
2159 do {
2160 start = u64_stats_fetch_begin_bh(&cpu_stats->syncp);
2161 trx_packets = cpu_stats->rx_packets;
2162 ttx_packets = cpu_stats->tx_packets;
2163 trx_bytes = cpu_stats->rx_bytes;
2164 ttx_bytes = cpu_stats->tx_bytes;
2165 trx_errors = cpu_stats->rx_errors;
2166 trx_dropped = cpu_stats->rx_dropped;
2167 } while (u64_stats_fetch_retry_bh(&cpu_stats->syncp, start));
2168
2169 rx_packets += trx_packets;
2170 tx_packets += ttx_packets;
2171 rx_bytes += trx_bytes;
2172 tx_bytes += ttx_bytes;
2173 rx_errors += trx_errors;
2174 rx_dropped += trx_dropped;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002175 }
2176
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002177 stats->rx_packets = rx_packets;
2178 stats->tx_packets = tx_packets;
2179 stats->rx_bytes = rx_bytes;
2180 stats->tx_bytes = tx_bytes;
2181 stats->rx_errors = rx_errors;
2182 stats->rx_dropped = rx_dropped;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002183
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002184 return stats;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002185}
2186
2187
2188/*
2189 * Change the "mtu".
2190 *
2191 * The "change_mtu" method is usually not needed.
2192 * If you need it, it must be like this.
2193 */
2194static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
2195{
2196 PDEBUG("tile_net_change_mtu()\n");
2197
2198 /* Check ranges. */
2199 if ((new_mtu < 68) || (new_mtu > 1500))
2200 return -EINVAL;
2201
2202 /* Accept the value. */
2203 dev->mtu = new_mtu;
2204
2205 return 0;
2206}
2207
2208
2209/*
2210 * Change the Ethernet Address of the NIC.
2211 *
2212 * The hypervisor driver does not support changing MAC address. However,
2213 * the IPP does not do anything with the MAC address, so the address which
2214 * gets used on outgoing packets, and which is accepted on incoming packets,
2215 * is completely up to the NetIO program or kernel driver which is actually
2216 * handling them.
2217 *
2218 * Returns 0 on success, negative on failure.
2219 */
2220static int tile_net_set_mac_address(struct net_device *dev, void *p)
2221{
2222 struct sockaddr *addr = p;
2223
2224 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002225 return -EADDRNOTAVAIL;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002226
2227 /* ISSUE: Note that "dev_addr" is now a pointer. */
2228 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2229
2230 return 0;
2231}
2232
2233
2234/*
2235 * Obtain the MAC address from the hypervisor.
2236 * This must be done before opening the device.
2237 */
2238static int tile_net_get_mac(struct net_device *dev)
2239{
2240 struct tile_net_priv *priv = netdev_priv(dev);
2241
2242 char hv_dev_name[32];
2243 int len;
2244
2245 __netio_getset_offset_t offset = { .word = NETIO_IPP_PARAM_OFF };
2246
2247 int ret;
2248
2249 /* For example, "xgbe0". */
2250 strcpy(hv_dev_name, dev->name);
2251 len = strlen(hv_dev_name);
2252
2253 /* For example, "xgbe/0". */
2254 hv_dev_name[len] = hv_dev_name[len - 1];
2255 hv_dev_name[len - 1] = '/';
2256 len++;
2257
2258 /* For example, "xgbe/0/native_hash". */
2259 strcpy(hv_dev_name + len, hash_default ? "/native_hash" : "/native");
2260
2261 /* Get the hypervisor handle for this device. */
2262 priv->hv_devhdl = hv_dev_open((HV_VirtAddr)hv_dev_name, 0);
2263 PDEBUG("hv_dev_open(%s) returned %d %p\n",
2264 hv_dev_name, priv->hv_devhdl, &priv->hv_devhdl);
2265 if (priv->hv_devhdl < 0) {
2266 if (priv->hv_devhdl == HV_ENODEV)
2267 printk(KERN_DEBUG "Ignoring unconfigured device %s\n",
2268 hv_dev_name);
2269 else
2270 printk(KERN_DEBUG "hv_dev_open(%s) returned %d\n",
2271 hv_dev_name, priv->hv_devhdl);
2272 return -1;
2273 }
2274
2275 /*
2276 * Read the hardware address from the hypervisor.
2277 * ISSUE: Note that "dev_addr" is now a pointer.
2278 */
2279 offset.bits.class = NETIO_PARAM;
2280 offset.bits.addr = NETIO_PARAM_MAC;
2281 ret = hv_dev_pread(priv->hv_devhdl, 0,
2282 (HV_VirtAddr)dev->dev_addr, dev->addr_len,
2283 offset.word);
2284 PDEBUG("hv_dev_pread(NETIO_PARAM_MAC) returned %d\n", ret);
2285 if (ret <= 0) {
2286 printk(KERN_DEBUG "hv_dev_pread(NETIO_PARAM_MAC) %s failed\n",
2287 dev->name);
2288 /*
2289 * Since the device is configured by the hypervisor but we
2290 * can't get its MAC address, we are most likely running
2291 * the simulator, so let's generate a random MAC address.
2292 */
Danny Kukawka7ce5d222012-02-15 06:45:40 +00002293 eth_hw_addr_random(dev);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002294 }
2295
2296 return 0;
2297}
2298
Chris Metcalf92795672012-03-30 19:23:35 -04002299
2300#ifdef CONFIG_NET_POLL_CONTROLLER
2301/*
2302 * Polling 'interrupt' - used by things like netconsole to send skbs
2303 * without having to re-enable interrupts. It's not called while
2304 * the interrupt routine is executing.
2305 */
2306static void tile_net_netpoll(struct net_device *dev)
2307{
2308 struct tile_net_priv *priv = netdev_priv(dev);
2309 disable_percpu_irq(priv->intr_id);
2310 tile_net_handle_ingress_interrupt(priv->intr_id, dev);
2311 enable_percpu_irq(priv->intr_id, 0);
2312}
2313#endif
2314
2315
stephen hemmingere5686ad2012-01-05 19:10:25 +00002316static const struct net_device_ops tile_net_ops = {
Chris Metcalfe5a06932010-11-01 17:00:37 -04002317 .ndo_open = tile_net_open,
2318 .ndo_stop = tile_net_stop,
2319 .ndo_start_xmit = tile_net_tx,
2320 .ndo_do_ioctl = tile_net_ioctl,
Chris Metcalfd68e2d32013-07-25 12:41:15 -04002321 .ndo_get_stats64 = tile_net_get_stats64,
Chris Metcalfe5a06932010-11-01 17:00:37 -04002322 .ndo_change_mtu = tile_net_change_mtu,
2323 .ndo_tx_timeout = tile_net_tx_timeout,
Chris Metcalf92795672012-03-30 19:23:35 -04002324 .ndo_set_mac_address = tile_net_set_mac_address,
2325#ifdef CONFIG_NET_POLL_CONTROLLER
2326 .ndo_poll_controller = tile_net_netpoll,
2327#endif
Chris Metcalfe5a06932010-11-01 17:00:37 -04002328};
2329
2330
2331/*
2332 * The setup function.
2333 *
2334 * This uses ether_setup() to assign various fields in dev, including
2335 * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
2336 */
2337static void tile_net_setup(struct net_device *dev)
2338{
2339 PDEBUG("tile_net_setup()\n");
2340
2341 ether_setup(dev);
2342
2343 dev->netdev_ops = &tile_net_ops;
2344
2345 dev->watchdog_timeo = TILE_NET_TIMEOUT;
2346
2347 /* We want lockless xmit. */
2348 dev->features |= NETIF_F_LLTX;
2349
2350 /* We support hardware tx checksums. */
2351 dev->features |= NETIF_F_HW_CSUM;
2352
2353 /* We support scatter/gather. */
2354 dev->features |= NETIF_F_SG;
2355
2356 /* We support TSO. */
2357 dev->features |= NETIF_F_TSO;
2358
2359#ifdef TILE_NET_GSO
2360 /* We support GSO. */
2361 dev->features |= NETIF_F_GSO;
2362#endif
2363
2364 if (hash_default)
2365 dev->features |= NETIF_F_HIGHDMA;
2366
2367 /* ISSUE: We should support NETIF_F_UFO. */
2368
2369 dev->tx_queue_len = TILE_NET_TX_QUEUE_LEN;
2370
2371 dev->mtu = TILE_NET_MTU;
2372}
2373
2374
2375/*
2376 * Allocate the device structure, register the device, and obtain the
2377 * MAC address from the hypervisor.
2378 */
2379static struct net_device *tile_net_dev_init(const char *name)
2380{
2381 int ret;
2382 struct net_device *dev;
2383 struct tile_net_priv *priv;
Chris Metcalfe5a06932010-11-01 17:00:37 -04002384
2385 /*
2386 * Allocate the device structure. This allocates "priv", calls
2387 * tile_net_setup(), and saves "name". Normally, "name" is a
2388 * template, instantiated by register_netdev(), but not for us.
2389 */
2390 dev = alloc_netdev(sizeof(*priv), name, tile_net_setup);
2391 if (!dev) {
2392 pr_err("alloc_netdev(%s) failed\n", name);
2393 return NULL;
2394 }
2395
2396 priv = netdev_priv(dev);
2397
2398 /* Initialize "priv". */
2399
2400 memset(priv, 0, sizeof(*priv));
2401
2402 /* Save "dev" for "tile_net_open_retry()". */
2403 priv->dev = dev;
2404
2405 INIT_DELAYED_WORK(&priv->retry_work, tile_net_open_retry);
2406
Chris Metcalfd91c6412011-03-01 12:49:53 -05002407 spin_lock_init(&priv->eq_lock);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002408
Chris Metcalfd91c6412011-03-01 12:49:53 -05002409 /* Allocate "eq". */
2410 priv->eq_pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, EQ_ORDER);
2411 if (!priv->eq_pages) {
Chris Metcalfe5a06932010-11-01 17:00:37 -04002412 free_netdev(dev);
2413 return NULL;
2414 }
Chris Metcalfd91c6412011-03-01 12:49:53 -05002415 priv->eq = page_address(priv->eq_pages);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002416
2417 /* Register the network device. */
2418 ret = register_netdev(dev);
2419 if (ret) {
2420 pr_err("register_netdev %s failed %d\n", dev->name, ret);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002421 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002422 free_netdev(dev);
2423 return NULL;
2424 }
2425
2426 /* Get the MAC address. */
2427 ret = tile_net_get_mac(dev);
2428 if (ret < 0) {
2429 unregister_netdev(dev);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002430 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002431 free_netdev(dev);
2432 return NULL;
2433 }
2434
2435 return dev;
2436}
2437
2438
2439/*
2440 * Module cleanup.
Chris Metcalfd91c6412011-03-01 12:49:53 -05002441 *
2442 * FIXME: If compiled as a module, this module cannot be "unloaded",
2443 * because the "ingress interrupt handler" is registered permanently.
Chris Metcalfe5a06932010-11-01 17:00:37 -04002444 */
2445static void tile_net_cleanup(void)
2446{
2447 int i;
2448
2449 for (i = 0; i < TILE_NET_DEVS; i++) {
2450 if (tile_net_devs[i]) {
2451 struct net_device *dev = tile_net_devs[i];
2452 struct tile_net_priv *priv = netdev_priv(dev);
2453 unregister_netdev(dev);
Chris Metcalfd07bd862011-05-02 16:36:48 -04002454 finv_buffer_remote(priv->eq, EQ_SIZE, 0);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002455 __free_pages(priv->eq_pages, EQ_ORDER);
Chris Metcalfe5a06932010-11-01 17:00:37 -04002456 free_netdev(dev);
2457 }
2458 }
2459}
2460
2461
2462/*
2463 * Module initialization.
2464 */
2465static int tile_net_init_module(void)
2466{
Chris Metcalf92795672012-03-30 19:23:35 -04002467 pr_info("Tilera Network Driver\n");
Chris Metcalfe5a06932010-11-01 17:00:37 -04002468
2469 tile_net_devs[0] = tile_net_dev_init("xgbe0");
2470 tile_net_devs[1] = tile_net_dev_init("xgbe1");
2471 tile_net_devs[2] = tile_net_dev_init("gbe0");
2472 tile_net_devs[3] = tile_net_dev_init("gbe1");
2473
2474 return 0;
2475}
2476
2477
Chris Metcalfd91c6412011-03-01 12:49:53 -05002478module_init(tile_net_init_module);
2479module_exit(tile_net_cleanup);
2480
2481
Chris Metcalfe5a06932010-11-01 17:00:37 -04002482#ifndef MODULE
Chris Metcalfd91c6412011-03-01 12:49:53 -05002483
Chris Metcalfe5a06932010-11-01 17:00:37 -04002484/*
2485 * The "network_cpus" boot argument specifies the cpus that are dedicated
2486 * to handle ingress packets.
2487 *
2488 * The parameter should be in the form "network_cpus=m-n[,x-y]", where
2489 * m, n, x, y are integer numbers that represent the cpus that can be
2490 * neither a dedicated cpu nor a dataplane cpu.
2491 */
2492static int __init network_cpus_setup(char *str)
2493{
2494 int rc = cpulist_parse_crop(str, &network_cpus_map);
2495 if (rc != 0) {
2496 pr_warning("network_cpus=%s: malformed cpu list\n",
2497 str);
2498 } else {
2499
2500 /* Remove dedicated cpus. */
2501 cpumask_and(&network_cpus_map, &network_cpus_map,
2502 cpu_possible_mask);
2503
2504
2505 if (cpumask_empty(&network_cpus_map)) {
2506 pr_warning("Ignoring network_cpus='%s'.\n",
2507 str);
2508 } else {
2509 char buf[1024];
2510 cpulist_scnprintf(buf, sizeof(buf), &network_cpus_map);
2511 pr_info("Linux network CPUs: %s\n", buf);
2512 network_cpus_used = true;
2513 }
2514 }
2515
2516 return 0;
2517}
2518__setup("network_cpus=", network_cpus_setup);
Chris Metcalfd91c6412011-03-01 12:49:53 -05002519
Chris Metcalfe5a06932010-11-01 17:00:37 -04002520#endif