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