Merge tag 'pci-v3.15-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[deliverable/linux.git] / drivers / net / ethernet / tile / tilegx.c
CommitLineData
e3d62d7e
CM
1/*
2 * Copyright 2012 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/irq.h>
26#include <linux/netdevice.h> /* struct device, and other headers */
27#include <linux/etherdevice.h> /* eth_type_trans */
28#include <linux/skbuff.h>
29#include <linux/ioctl.h>
30#include <linux/cdev.h>
31#include <linux/hugetlb.h>
32#include <linux/in6.h>
33#include <linux/timer.h>
34#include <linux/hrtimer.h>
35#include <linux/ktime.h>
36#include <linux/io.h>
37#include <linux/ctype.h>
38#include <linux/ip.h>
2c7d04a9 39#include <linux/ipv6.h>
e3d62d7e 40#include <linux/tcp.h>
9ab5ec59
CM
41#include <linux/net_tstamp.h>
42#include <linux/ptp_clock_kernel.h>
e3d62d7e
CM
43
44#include <asm/checksum.h>
45#include <asm/homecache.h>
46#include <gxio/mpipe.h>
47#include <arch/sim.h>
48
49/* Default transmit lockup timeout period, in jiffies. */
50#define TILE_NET_TIMEOUT (5 * HZ)
51
52/* The maximum number of distinct channels (idesc.channel is 5 bits). */
53#define TILE_NET_CHANNELS 32
54
55/* Maximum number of idescs to handle per "poll". */
56#define TILE_NET_BATCH 128
57
58/* Maximum number of packets to handle per "poll". */
59#define TILE_NET_WEIGHT 64
60
61/* Number of entries in each iqueue. */
62#define IQUEUE_ENTRIES 512
63
64/* Number of entries in each equeue. */
65#define EQUEUE_ENTRIES 2048
66
67/* Total header bytes per equeue slot. Must be big enough for 2 bytes
68 * of NET_IP_ALIGN alignment, plus 14 bytes (?) of L2 header, plus up to
69 * 60 bytes of actual TCP header. We round up to align to cache lines.
70 */
71#define HEADER_BYTES 128
72
73/* Maximum completions per cpu per device (must be a power of two).
74 * ISSUE: What is the right number here? If this is too small, then
75 * egress might block waiting for free space in a completions array.
76 * ISSUE: At the least, allocate these only for initialized echannels.
77 */
78#define TILE_NET_MAX_COMPS 64
79
80#define MAX_FRAGS (MAX_SKB_FRAGS + 1)
81
2628e8af
CM
82/* The "kinds" of buffer stacks (small/large/jumbo). */
83#define MAX_KINDS 3
84
e3d62d7e
CM
85/* Size of completions data to allocate.
86 * ISSUE: Probably more than needed since we don't use all the channels.
87 */
88#define COMPS_SIZE (TILE_NET_CHANNELS * sizeof(struct tile_net_comps))
89
90/* Size of NotifRing data to allocate. */
91#define NOTIF_RING_SIZE (IQUEUE_ENTRIES * sizeof(gxio_mpipe_idesc_t))
92
93/* Timeout to wake the per-device TX timer after we stop the queue.
94 * We don't want the timeout too short (adds overhead, and might end
95 * up causing stop/wake/stop/wake cycles) or too long (affects performance).
96 * For the 10 Gb NIC, 30 usec means roughly 30+ 1500-byte packets.
97 */
98#define TX_TIMER_DELAY_USEC 30
99
100/* Timeout to wake the per-cpu egress timer to free completions. */
101#define EGRESS_TIMER_DELAY_USEC 1000
102
103MODULE_AUTHOR("Tilera Corporation");
104MODULE_LICENSE("GPL");
105
106/* A "packet fragment" (a chunk of memory). */
107struct frag {
108 void *buf;
109 size_t length;
110};
111
112/* A single completion. */
113struct tile_net_comp {
114 /* The "complete_count" when the completion will be complete. */
115 s64 when;
116 /* The buffer to be freed when the completion is complete. */
117 struct sk_buff *skb;
118};
119
120/* The completions for a given cpu and echannel. */
121struct tile_net_comps {
122 /* The completions. */
123 struct tile_net_comp comp_queue[TILE_NET_MAX_COMPS];
124 /* The number of completions used. */
125 unsigned long comp_next;
126 /* The number of completions freed. */
127 unsigned long comp_last;
128};
129
130/* The transmit wake timer for a given cpu and echannel. */
131struct tile_net_tx_wake {
9b4c341b 132 int tx_queue_idx;
e3d62d7e
CM
133 struct hrtimer timer;
134 struct net_device *dev;
135};
136
137/* Info for a specific cpu. */
138struct tile_net_info {
e3d62d7e
CM
139 /* Our cpu. */
140 int my_cpu;
e3d62d7e
CM
141 /* A timer for handling egress completions. */
142 struct hrtimer egress_timer;
143 /* True if "egress_timer" is scheduled. */
144 bool egress_timer_scheduled;
f3286a3a
CM
145 struct info_mpipe {
146 /* Packet queue. */
147 gxio_mpipe_iqueue_t iqueue;
148 /* The NAPI struct. */
149 struct napi_struct napi;
150 /* Number of buffers (by kind) which must still be provided. */
151 unsigned int num_needed_buffers[MAX_KINDS];
152 /* instance id. */
153 int instance;
154 /* True if iqueue is valid. */
155 bool has_iqueue;
156 /* NAPI flags. */
157 bool napi_added;
158 bool napi_enabled;
159 /* Comps for each egress channel. */
160 struct tile_net_comps *comps_for_echannel[TILE_NET_CHANNELS];
161 /* Transmit wake timer for each egress channel. */
162 struct tile_net_tx_wake tx_wake[TILE_NET_CHANNELS];
163 } mpipe[NR_MPIPE_MAX];
e3d62d7e
CM
164};
165
166/* Info for egress on a particular egress channel. */
167struct tile_net_egress {
168 /* The "equeue". */
169 gxio_mpipe_equeue_t *equeue;
170 /* The headers for TSO. */
171 unsigned char *headers;
172};
173
174/* Info for a specific device. */
175struct tile_net_priv {
176 /* Our network device. */
177 struct net_device *dev;
178 /* The primary link. */
179 gxio_mpipe_link_t link;
180 /* The primary channel, if open, else -1. */
181 int channel;
182 /* The "loopify" egress link, if needed. */
183 gxio_mpipe_link_t loopify_link;
184 /* The "loopify" egress channel, if open, else -1. */
185 int loopify_channel;
186 /* The egress channel (channel or loopify_channel). */
187 int echannel;
f3286a3a
CM
188 /* mPIPE instance, 0 or 1. */
189 int instance;
9ab5ec59
CM
190 /* The timestamp config. */
191 struct hwtstamp_config stamp_cfg;
e3d62d7e
CM
192};
193
f3286a3a
CM
194static struct mpipe_data {
195 /* The ingress irq. */
196 int ingress_irq;
e3d62d7e 197
f3286a3a
CM
198 /* The "context" for all devices. */
199 gxio_mpipe_context_t context;
200
201 /* Egress info, indexed by "priv->echannel"
202 * (lazily created as needed).
203 */
204 struct tile_net_egress
205 egress_for_echannel[TILE_NET_CHANNELS];
206
207 /* Devices currently associated with each channel.
208 * NOTE: The array entry can become NULL after ifconfig down, but
209 * we do not free the underlying net_device structures, so it is
210 * safe to use a pointer after reading it from this array.
211 */
212 struct net_device
213 *tile_net_devs_for_channel[TILE_NET_CHANNELS];
214
215 /* The actual memory allocated for the buffer stacks. */
216 void *buffer_stack_vas[MAX_KINDS];
217
218 /* The amount of memory allocated for each buffer stack. */
219 size_t buffer_stack_bytes[MAX_KINDS];
220
221 /* The first buffer stack index
222 * (small = +0, large = +1, jumbo = +2).
223 */
224 int first_buffer_stack;
225
226 /* The buckets. */
227 int first_bucket;
228 int num_buckets;
229
9ab5ec59
CM
230 /* PTP-specific data. */
231 struct ptp_clock *ptp_clock;
232 struct ptp_clock_info caps;
233
234 /* Lock for ptp accessors. */
235 struct mutex ptp_lock;
9ab5ec59 236
f3286a3a
CM
237} mpipe_data[NR_MPIPE_MAX] = {
238 [0 ... (NR_MPIPE_MAX - 1)] {
239 .ingress_irq = -1,
240 .first_buffer_stack = -1,
241 .first_bucket = -1,
242 .num_buckets = 1
243 }
244};
e3d62d7e
CM
245
246/* A mutex for "tile_net_devs_for_channel". */
247static DEFINE_MUTEX(tile_net_devs_for_channel_mutex);
248
249/* The per-cpu info. */
250static DEFINE_PER_CPU(struct tile_net_info, per_cpu_info);
251
e3d62d7e 252
2628e8af 253/* The buffer size enums for each buffer stack.
e3d62d7e 254 * See arch/tile/include/gxio/mpipe.h for the set of possible values.
2628e8af
CM
255 * We avoid the "10384" size because it can induce "false chaining"
256 * on "cut-through" jumbo packets.
e3d62d7e 257 */
2628e8af
CM
258static gxio_mpipe_buffer_size_enum_t buffer_size_enums[MAX_KINDS] = {
259 GXIO_MPIPE_BUFFER_SIZE_128,
260 GXIO_MPIPE_BUFFER_SIZE_1664,
261 GXIO_MPIPE_BUFFER_SIZE_16384
262};
e3d62d7e 263
e3d62d7e
CM
264/* Text value of tile_net.cpus if passed as a module parameter. */
265static char *network_cpus_string;
266
267/* The actual cpus in "network_cpus". */
268static struct cpumask network_cpus_map;
269
4aa02644 270/* If "tile_net.loopify=LINK" was specified, this is "LINK". */
e3d62d7e
CM
271static char *loopify_link_name;
272
4aa02644
CM
273/* If "tile_net.custom" was specified, this is true. */
274static bool custom_flag;
e3d62d7e 275
2628e8af
CM
276/* If "tile_net.jumbo=NUM" was specified, this is "NUM". */
277static uint jumbo_num;
278
f3286a3a
CM
279/* Obtain mpipe instance from struct tile_net_priv given struct net_device. */
280static inline int mpipe_instance(struct net_device *dev)
281{
282 struct tile_net_priv *priv = netdev_priv(dev);
283 return priv->instance;
284}
285
e3d62d7e
CM
286/* The "tile_net.cpus" argument specifies the cpus that are dedicated
287 * to handle ingress packets.
288 *
289 * The parameter should be in the form "tile_net.cpus=m-n[,x-y]", where
290 * m, n, x, y are integer numbers that represent the cpus that can be
291 * neither a dedicated cpu nor a dataplane cpu.
292 */
293static bool network_cpus_init(void)
294{
295 char buf[1024];
296 int rc;
297
298 if (network_cpus_string == NULL)
299 return false;
300
301 rc = cpulist_parse_crop(network_cpus_string, &network_cpus_map);
302 if (rc != 0) {
303 pr_warn("tile_net.cpus=%s: malformed cpu list\n",
304 network_cpus_string);
305 return false;
306 }
307
308 /* Remove dedicated cpus. */
309 cpumask_and(&network_cpus_map, &network_cpus_map, cpu_possible_mask);
310
311 if (cpumask_empty(&network_cpus_map)) {
312 pr_warn("Ignoring empty tile_net.cpus='%s'.\n",
313 network_cpus_string);
314 return false;
315 }
316
317 cpulist_scnprintf(buf, sizeof(buf), &network_cpus_map);
318 pr_info("Linux network CPUs: %s\n", buf);
319 return true;
320}
321
322module_param_named(cpus, network_cpus_string, charp, 0444);
323MODULE_PARM_DESC(cpus, "cpulist of cores that handle network interrupts");
324
325/* The "tile_net.loopify=LINK" argument causes the named device to
326 * actually use "loop0" for ingress, and "loop1" for egress. This
327 * allows an app to sit between the actual link and linux, passing
328 * (some) packets along to linux, and forwarding (some) packets sent
329 * out by linux.
330 */
331module_param_named(loopify, loopify_link_name, charp, 0444);
332MODULE_PARM_DESC(loopify, "name the device to use loop0/1 for ingress/egress");
333
334/* The "tile_net.custom" argument causes us to ignore the "conventional"
335 * classifier metadata, in particular, the "l2_offset".
336 */
4aa02644 337module_param_named(custom, custom_flag, bool, 0444);
e3d62d7e
CM
338MODULE_PARM_DESC(custom, "indicates a (heavily) customized classifier");
339
2628e8af
CM
340/* The "tile_net.jumbo" argument causes us to support "jumbo" packets,
341 * and to allocate the given number of "jumbo" buffers.
342 */
343module_param_named(jumbo, jumbo_num, uint, 0444);
344MODULE_PARM_DESC(jumbo, "the number of buffers to support jumbo packets");
345
e3d62d7e
CM
346/* Atomically update a statistics field.
347 * Note that on TILE-Gx, this operation is fire-and-forget on the
348 * issuing core (single-cycle dispatch) and takes only a few cycles
349 * longer than a regular store when the request reaches the home cache.
350 * No expensive bus management overhead is required.
351 */
352static void tile_net_stats_add(unsigned long value, unsigned long *field)
353{
354 BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(unsigned long));
355 atomic_long_add(value, (atomic_long_t *)field);
356}
357
358/* Allocate and push a buffer. */
f3286a3a 359static bool tile_net_provide_buffer(int instance, int kind)
e3d62d7e 360{
f3286a3a 361 struct mpipe_data *md = &mpipe_data[instance];
2628e8af
CM
362 gxio_mpipe_buffer_size_enum_t bse = buffer_size_enums[kind];
363 size_t bs = gxio_mpipe_buffer_size_enum_to_buffer_size(bse);
e3d62d7e
CM
364 const unsigned long buffer_alignment = 128;
365 struct sk_buff *skb;
366 int len;
367
2628e8af 368 len = sizeof(struct sk_buff **) + buffer_alignment + bs;
e3d62d7e
CM
369 skb = dev_alloc_skb(len);
370 if (skb == NULL)
371 return false;
372
373 /* Make room for a back-pointer to 'skb' and guarantee alignment. */
374 skb_reserve(skb, sizeof(struct sk_buff **));
375 skb_reserve(skb, -(long)skb->data & (buffer_alignment - 1));
376
377 /* Save a back-pointer to 'skb'. */
378 *(struct sk_buff **)(skb->data - sizeof(struct sk_buff **)) = skb;
379
380 /* Make sure "skb" and the back-pointer have been flushed. */
381 wmb();
382
f3286a3a 383 gxio_mpipe_push_buffer(&md->context, md->first_buffer_stack + kind,
e3d62d7e
CM
384 (void *)va_to_tile_io_addr(skb->data));
385
386 return true;
387}
388
389/* Convert a raw mpipe buffer to its matching skb pointer. */
390static struct sk_buff *mpipe_buf_to_skb(void *va)
391{
392 /* Acquire the associated "skb". */
393 struct sk_buff **skb_ptr = va - sizeof(*skb_ptr);
394 struct sk_buff *skb = *skb_ptr;
395
396 /* Paranoia. */
397 if (skb->data != va) {
398 /* Panic here since there's a reasonable chance
399 * that corrupt buffers means generic memory
400 * corruption, with unpredictable system effects.
401 */
402 panic("Corrupt linux buffer! va=%p, skb=%p, skb->data=%p",
403 va, skb, skb->data);
404 }
405
406 return skb;
407}
408
f3286a3a 409static void tile_net_pop_all_buffers(int instance, int stack)
e3d62d7e 410{
f3286a3a
CM
411 struct mpipe_data *md = &mpipe_data[instance];
412
e3d62d7e
CM
413 for (;;) {
414 tile_io_addr_t addr =
f3286a3a
CM
415 (tile_io_addr_t)gxio_mpipe_pop_buffer(&md->context,
416 stack);
e3d62d7e
CM
417 if (addr == 0)
418 break;
419 dev_kfree_skb_irq(mpipe_buf_to_skb(tile_io_addr_to_va(addr)));
420 }
421}
422
423/* Provide linux buffers to mPIPE. */
424static void tile_net_provide_needed_buffers(void)
425{
426 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
f3286a3a
CM
427 int instance, kind;
428 for (instance = 0; instance < NR_MPIPE_MAX &&
429 info->mpipe[instance].has_iqueue; instance++) {
430 for (kind = 0; kind < MAX_KINDS; kind++) {
431 while (info->mpipe[instance].num_needed_buffers[kind]
432 != 0) {
433 if (!tile_net_provide_buffer(instance, kind)) {
434 pr_notice("Tile %d still needs"
435 " some buffers\n",
436 info->my_cpu);
437 return;
438 }
439 info->mpipe[instance].
440 num_needed_buffers[kind]--;
2628e8af 441 }
2628e8af 442 }
e3d62d7e 443 }
e3d62d7e
CM
444}
445
9ab5ec59
CM
446/* Get RX timestamp, and store it in the skb. */
447static void tile_rx_timestamp(struct tile_net_priv *priv, struct sk_buff *skb,
448 gxio_mpipe_idesc_t *idesc)
449{
9ab5ec59
CM
450 if (unlikely(priv->stamp_cfg.rx_filter != HWTSTAMP_FILTER_NONE)) {
451 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
452 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
453 shhwtstamps->hwtstamp = ktime_set(idesc->time_stamp_sec,
454 idesc->time_stamp_ns);
455 }
9ab5ec59
CM
456}
457
458/* Get TX timestamp, and store it in the skb. */
459static void tile_tx_timestamp(struct sk_buff *skb, int instance)
460{
9ab5ec59
CM
461 struct skb_shared_info *shtx = skb_shinfo(skb);
462 if (unlikely((shtx->tx_flags & SKBTX_HW_TSTAMP) != 0)) {
463 struct mpipe_data *md = &mpipe_data[instance];
464 struct skb_shared_hwtstamps shhwtstamps;
465 struct timespec ts;
466
467 shtx->tx_flags |= SKBTX_IN_PROGRESS;
468 gxio_mpipe_get_timestamp(&md->context, &ts);
469 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
470 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
471 skb_tstamp_tx(skb, &shhwtstamps);
472 }
9ab5ec59
CM
473}
474
475/* Use ioctl() to enable or disable TX or RX timestamping. */
6ab96d1e 476static int tile_hwtstamp_set(struct net_device *dev, struct ifreq *rq)
9ab5ec59 477{
9ab5ec59
CM
478 struct hwtstamp_config config;
479 struct tile_net_priv *priv = netdev_priv(dev);
480
481 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
482 return -EFAULT;
483
484 if (config.flags) /* reserved for future extensions */
485 return -EINVAL;
486
487 switch (config.tx_type) {
488 case HWTSTAMP_TX_OFF:
489 case HWTSTAMP_TX_ON:
490 break;
491 default:
492 return -ERANGE;
493 }
494
495 switch (config.rx_filter) {
496 case HWTSTAMP_FILTER_NONE:
497 break;
498 case HWTSTAMP_FILTER_ALL:
499 case HWTSTAMP_FILTER_SOME:
500 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
501 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
502 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
503 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
504 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
505 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
506 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
507 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
508 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
509 case HWTSTAMP_FILTER_PTP_V2_EVENT:
510 case HWTSTAMP_FILTER_PTP_V2_SYNC:
511 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
512 config.rx_filter = HWTSTAMP_FILTER_ALL;
513 break;
514 default:
515 return -ERANGE;
516 }
517
518 if (copy_to_user(rq->ifr_data, &config, sizeof(config)))
519 return -EFAULT;
520
521 priv->stamp_cfg = config;
522 return 0;
9ab5ec59
CM
523}
524
6ab96d1e
BH
525static int tile_hwtstamp_get(struct net_device *dev, struct ifreq *rq)
526{
6ab96d1e
BH
527 struct tile_net_priv *priv = netdev_priv(dev);
528
529 if (copy_to_user(rq->ifr_data, &priv->stamp_cfg,
530 sizeof(priv->stamp_cfg)))
531 return -EFAULT;
532
533 return 0;
6ab96d1e
BH
534}
535
e3d62d7e
CM
536static inline bool filter_packet(struct net_device *dev, void *buf)
537{
538 /* Filter packets received before we're up. */
539 if (dev == NULL || !(dev->flags & IFF_UP))
540 return true;
541
542 /* Filter out packets that aren't for us. */
543 if (!(dev->flags & IFF_PROMISC) &&
544 !is_multicast_ether_addr(buf) &&
7367d0b5 545 !ether_addr_equal(dev->dev_addr, buf))
e3d62d7e
CM
546 return true;
547
548 return false;
549}
550
551static void tile_net_receive_skb(struct net_device *dev, struct sk_buff *skb,
552 gxio_mpipe_idesc_t *idesc, unsigned long len)
553{
554 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
9ab5ec59
CM
555 struct tile_net_priv *priv = netdev_priv(dev);
556 int instance = priv->instance;
e3d62d7e
CM
557
558 /* Encode the actual packet length. */
559 skb_put(skb, len);
560
561 skb->protocol = eth_type_trans(skb, dev);
562
563 /* Acknowledge "good" hardware checksums. */
564 if (idesc->cs && idesc->csum_seed_val == 0xFFFF)
565 skb->ip_summed = CHECKSUM_UNNECESSARY;
566
9ab5ec59
CM
567 /* Get RX timestamp from idesc. */
568 tile_rx_timestamp(priv, skb, idesc);
569
f3286a3a 570 napi_gro_receive(&info->mpipe[instance].napi, skb);
e3d62d7e
CM
571
572 /* Update stats. */
ad018185
CM
573 tile_net_stats_add(1, &dev->stats.rx_packets);
574 tile_net_stats_add(len, &dev->stats.rx_bytes);
e3d62d7e
CM
575
576 /* Need a new buffer. */
2628e8af 577 if (idesc->size == buffer_size_enums[0])
f3286a3a 578 info->mpipe[instance].num_needed_buffers[0]++;
2628e8af 579 else if (idesc->size == buffer_size_enums[1])
f3286a3a 580 info->mpipe[instance].num_needed_buffers[1]++;
e3d62d7e 581 else
f3286a3a 582 info->mpipe[instance].num_needed_buffers[2]++;
e3d62d7e
CM
583}
584
585/* Handle a packet. Return true if "processed", false if "filtered". */
f3286a3a 586static bool tile_net_handle_packet(int instance, gxio_mpipe_idesc_t *idesc)
e3d62d7e
CM
587{
588 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
f3286a3a
CM
589 struct mpipe_data *md = &mpipe_data[instance];
590 struct net_device *dev = md->tile_net_devs_for_channel[idesc->channel];
e3d62d7e
CM
591 uint8_t l2_offset;
592 void *va;
593 void *buf;
594 unsigned long len;
595 bool filter;
596
2628e8af
CM
597 /* Drop packets for which no buffer was available (which can
598 * happen under heavy load), or for which the me/tr/ce flags
599 * are set (which can happen for jumbo cut-through packets,
600 * or with a customized classifier).
e3d62d7e 601 */
2628e8af
CM
602 if (idesc->be || idesc->me || idesc->tr || idesc->ce) {
603 if (dev)
ad018185 604 tile_net_stats_add(1, &dev->stats.rx_errors);
2628e8af 605 goto drop;
e3d62d7e
CM
606 }
607
608 /* Get the "l2_offset", if allowed. */
4aa02644 609 l2_offset = custom_flag ? 0 : gxio_mpipe_idesc_get_l2_offset(idesc);
e3d62d7e 610
2628e8af
CM
611 /* Get the VA (including NET_IP_ALIGN bytes of "headroom"). */
612 va = tile_io_addr_to_va((unsigned long)idesc->va);
e3d62d7e
CM
613
614 /* Get the actual packet start/length. */
615 buf = va + l2_offset;
616 len = idesc->l2_size - l2_offset;
617
618 /* Point "va" at the raw buffer. */
619 va -= NET_IP_ALIGN;
620
621 filter = filter_packet(dev, buf);
622 if (filter) {
2628e8af 623 if (dev)
ad018185 624 tile_net_stats_add(1, &dev->stats.rx_dropped);
2628e8af 625drop:
f3286a3a 626 gxio_mpipe_iqueue_drop(&info->mpipe[instance].iqueue, idesc);
e3d62d7e
CM
627 } else {
628 struct sk_buff *skb = mpipe_buf_to_skb(va);
629
630 /* Skip headroom, and any custom header. */
631 skb_reserve(skb, NET_IP_ALIGN + l2_offset);
632
633 tile_net_receive_skb(dev, skb, idesc, len);
634 }
635
f3286a3a 636 gxio_mpipe_iqueue_consume(&info->mpipe[instance].iqueue, idesc);
e3d62d7e
CM
637 return !filter;
638}
639
640/* Handle some packets for the current CPU.
641 *
642 * This function handles up to TILE_NET_BATCH idescs per call.
643 *
644 * ISSUE: Since we do not provide new buffers until this function is
645 * complete, we must initially provide enough buffers for each network
646 * cpu to fill its iqueue and also its batched idescs.
647 *
648 * ISSUE: The "rotting packet" race condition occurs if a packet
649 * arrives after the queue appears to be empty, and before the
650 * hypervisor interrupt is re-enabled.
651 */
652static int tile_net_poll(struct napi_struct *napi, int budget)
653{
654 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
655 unsigned int work = 0;
656 gxio_mpipe_idesc_t *idesc;
f3286a3a
CM
657 int instance, i, n;
658 struct mpipe_data *md;
659 struct info_mpipe *info_mpipe =
660 container_of(napi, struct info_mpipe, napi);
661
662 instance = info_mpipe->instance;
663 while ((n = gxio_mpipe_iqueue_try_peek(
664 &info_mpipe->iqueue,
665 &idesc)) > 0) {
e3d62d7e
CM
666 for (i = 0; i < n; i++) {
667 if (i == TILE_NET_BATCH)
668 goto done;
f3286a3a
CM
669 if (tile_net_handle_packet(instance,
670 idesc + i)) {
e3d62d7e
CM
671 if (++work >= budget)
672 goto done;
673 }
674 }
675 }
676
677 /* There are no packets left. */
f3286a3a 678 napi_complete(&info_mpipe->napi);
e3d62d7e 679
f3286a3a 680 md = &mpipe_data[instance];
e3d62d7e 681 /* Re-enable hypervisor interrupts. */
f3286a3a
CM
682 gxio_mpipe_enable_notif_ring_interrupt(
683 &md->context, info->mpipe[instance].iqueue.ring);
e3d62d7e
CM
684
685 /* HACK: Avoid the "rotting packet" problem. */
f3286a3a
CM
686 if (gxio_mpipe_iqueue_try_peek(&info_mpipe->iqueue, &idesc) > 0)
687 napi_schedule(&info_mpipe->napi);
e3d62d7e
CM
688
689 /* ISSUE: Handle completions? */
690
691done:
692 tile_net_provide_needed_buffers();
693
694 return work;
695}
696
f3286a3a
CM
697/* Handle an ingress interrupt from an instance on the current cpu. */
698static irqreturn_t tile_net_handle_ingress_irq(int irq, void *id)
e3d62d7e
CM
699{
700 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
f3286a3a 701 napi_schedule(&info->mpipe[(uint64_t)id].napi);
e3d62d7e
CM
702 return IRQ_HANDLED;
703}
704
705/* Free some completions. This must be called with interrupts blocked. */
706static int tile_net_free_comps(gxio_mpipe_equeue_t *equeue,
707 struct tile_net_comps *comps,
708 int limit, bool force_update)
709{
710 int n = 0;
711 while (comps->comp_last < comps->comp_next) {
712 unsigned int cid = comps->comp_last % TILE_NET_MAX_COMPS;
713 struct tile_net_comp *comp = &comps->comp_queue[cid];
714 if (!gxio_mpipe_equeue_is_complete(equeue, comp->when,
715 force_update || n == 0))
716 break;
717 dev_kfree_skb_irq(comp->skb);
718 comps->comp_last++;
719 if (++n == limit)
720 break;
721 }
722 return n;
723}
724
725/* Add a completion. This must be called with interrupts blocked.
726 * tile_net_equeue_try_reserve() will have ensured a free completion entry.
727 */
728static void add_comp(gxio_mpipe_equeue_t *equeue,
729 struct tile_net_comps *comps,
730 uint64_t when, struct sk_buff *skb)
731{
732 int cid = comps->comp_next % TILE_NET_MAX_COMPS;
733 comps->comp_queue[cid].when = when;
734 comps->comp_queue[cid].skb = skb;
735 comps->comp_next++;
736}
737
9b4c341b
CM
738static void tile_net_schedule_tx_wake_timer(struct net_device *dev,
739 int tx_queue_idx)
e3d62d7e 740{
9b4c341b 741 struct tile_net_info *info = &per_cpu(per_cpu_info, tx_queue_idx);
e3d62d7e 742 struct tile_net_priv *priv = netdev_priv(dev);
f3286a3a
CM
743 int instance = priv->instance;
744 struct tile_net_tx_wake *tx_wake =
745 &info->mpipe[instance].tx_wake[priv->echannel];
e3d62d7e 746
9b4c341b 747 hrtimer_start(&tx_wake->timer,
e3d62d7e
CM
748 ktime_set(0, TX_TIMER_DELAY_USEC * 1000UL),
749 HRTIMER_MODE_REL_PINNED);
750}
751
752static enum hrtimer_restart tile_net_handle_tx_wake_timer(struct hrtimer *t)
753{
754 struct tile_net_tx_wake *tx_wake =
755 container_of(t, struct tile_net_tx_wake, timer);
9b4c341b 756 netif_wake_subqueue(tx_wake->dev, tx_wake->tx_queue_idx);
e3d62d7e
CM
757 return HRTIMER_NORESTART;
758}
759
760/* Make sure the egress timer is scheduled. */
761static void tile_net_schedule_egress_timer(void)
762{
763 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
764
765 if (!info->egress_timer_scheduled) {
766 hrtimer_start(&info->egress_timer,
767 ktime_set(0, EGRESS_TIMER_DELAY_USEC * 1000UL),
768 HRTIMER_MODE_REL_PINNED);
769 info->egress_timer_scheduled = true;
770 }
771}
772
773/* The "function" for "info->egress_timer".
774 *
775 * This timer will reschedule itself as long as there are any pending
776 * completions expected for this tile.
777 */
778static enum hrtimer_restart tile_net_handle_egress_timer(struct hrtimer *t)
779{
780 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
781 unsigned long irqflags;
782 bool pending = false;
f3286a3a 783 int i, instance;
e3d62d7e
CM
784
785 local_irq_save(irqflags);
786
787 /* The timer is no longer scheduled. */
788 info->egress_timer_scheduled = false;
789
790 /* Free all possible comps for this tile. */
f3286a3a
CM
791 for (instance = 0; instance < NR_MPIPE_MAX &&
792 info->mpipe[instance].has_iqueue; instance++) {
793 for (i = 0; i < TILE_NET_CHANNELS; i++) {
794 struct tile_net_egress *egress =
795 &mpipe_data[instance].egress_for_echannel[i];
796 struct tile_net_comps *comps =
797 info->mpipe[instance].comps_for_echannel[i];
798 if (!egress || comps->comp_last >= comps->comp_next)
799 continue;
800 tile_net_free_comps(egress->equeue, comps, -1, true);
801 pending = pending ||
802 (comps->comp_last < comps->comp_next);
803 }
e3d62d7e
CM
804 }
805
806 /* Reschedule timer if needed. */
807 if (pending)
808 tile_net_schedule_egress_timer();
809
810 local_irq_restore(irqflags);
811
812 return HRTIMER_NORESTART;
813}
814
9ab5ec59
CM
815/* PTP clock operations. */
816
817static int ptp_mpipe_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
818{
819 int ret = 0;
820 struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
821 mutex_lock(&md->ptp_lock);
822 if (gxio_mpipe_adjust_timestamp_freq(&md->context, ppb))
823 ret = -EINVAL;
824 mutex_unlock(&md->ptp_lock);
825 return ret;
826}
827
828static int ptp_mpipe_adjtime(struct ptp_clock_info *ptp, s64 delta)
829{
830 int ret = 0;
831 struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
832 mutex_lock(&md->ptp_lock);
833 if (gxio_mpipe_adjust_timestamp(&md->context, delta))
834 ret = -EBUSY;
835 mutex_unlock(&md->ptp_lock);
836 return ret;
837}
838
839static int ptp_mpipe_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
840{
841 int ret = 0;
842 struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
843 mutex_lock(&md->ptp_lock);
844 if (gxio_mpipe_get_timestamp(&md->context, ts))
845 ret = -EBUSY;
846 mutex_unlock(&md->ptp_lock);
847 return ret;
848}
849
850static int ptp_mpipe_settime(struct ptp_clock_info *ptp,
851 const struct timespec *ts)
852{
853 int ret = 0;
854 struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
855 mutex_lock(&md->ptp_lock);
856 if (gxio_mpipe_set_timestamp(&md->context, ts))
857 ret = -EBUSY;
858 mutex_unlock(&md->ptp_lock);
859 return ret;
860}
861
862static int ptp_mpipe_enable(struct ptp_clock_info *ptp,
863 struct ptp_clock_request *request, int on)
864{
865 return -EOPNOTSUPP;
866}
867
868static struct ptp_clock_info ptp_mpipe_caps = {
869 .owner = THIS_MODULE,
870 .name = "mPIPE clock",
871 .max_adj = 999999999,
872 .n_ext_ts = 0,
873 .pps = 0,
874 .adjfreq = ptp_mpipe_adjfreq,
875 .adjtime = ptp_mpipe_adjtime,
876 .gettime = ptp_mpipe_gettime,
877 .settime = ptp_mpipe_settime,
878 .enable = ptp_mpipe_enable,
879};
880
9ab5ec59
CM
881/* Sync mPIPE's timestamp up with Linux system time and register PTP clock. */
882static void register_ptp_clock(struct net_device *dev, struct mpipe_data *md)
883{
9ab5ec59
CM
884 struct timespec ts;
885
886 getnstimeofday(&ts);
887 gxio_mpipe_set_timestamp(&md->context, &ts);
888
889 mutex_init(&md->ptp_lock);
890 md->caps = ptp_mpipe_caps;
891 md->ptp_clock = ptp_clock_register(&md->caps, NULL);
892 if (IS_ERR(md->ptp_clock))
893 netdev_err(dev, "ptp_clock_register failed %ld\n",
894 PTR_ERR(md->ptp_clock));
9ab5ec59
CM
895}
896
897/* Initialize PTP fields in a new device. */
898static void init_ptp_dev(struct tile_net_priv *priv)
899{
9ab5ec59
CM
900 priv->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
901 priv->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
9ab5ec59
CM
902}
903
f3286a3a
CM
904/* Helper functions for "tile_net_update()". */
905static void enable_ingress_irq(void *irq)
e3d62d7e 906{
f3286a3a
CM
907 enable_percpu_irq((long)irq, 0);
908}
909
910static void disable_ingress_irq(void *irq)
911{
912 disable_percpu_irq((long)irq);
e3d62d7e
CM
913}
914
915/* Helper function for tile_net_open() and tile_net_stop().
916 * Always called under tile_net_devs_for_channel_mutex.
917 */
918static int tile_net_update(struct net_device *dev)
919{
920 static gxio_mpipe_rules_t rules; /* too big to fit on the stack */
921 bool saw_channel = false;
f3286a3a
CM
922 int instance = mpipe_instance(dev);
923 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
924 int channel;
925 int rc;
926 int cpu;
927
f3286a3a
CM
928 saw_channel = false;
929 gxio_mpipe_rules_init(&rules, &md->context);
e3d62d7e
CM
930
931 for (channel = 0; channel < TILE_NET_CHANNELS; channel++) {
f3286a3a 932 if (md->tile_net_devs_for_channel[channel] == NULL)
e3d62d7e
CM
933 continue;
934 if (!saw_channel) {
935 saw_channel = true;
f3286a3a
CM
936 gxio_mpipe_rules_begin(&rules, md->first_bucket,
937 md->num_buckets, NULL);
e3d62d7e
CM
938 gxio_mpipe_rules_set_headroom(&rules, NET_IP_ALIGN);
939 }
940 gxio_mpipe_rules_add_channel(&rules, channel);
941 }
942
943 /* NOTE: This can fail if there is no classifier.
944 * ISSUE: Can anything else cause it to fail?
945 */
946 rc = gxio_mpipe_rules_commit(&rules);
947 if (rc != 0) {
f3286a3a
CM
948 netdev_warn(dev, "gxio_mpipe_rules_commit: mpipe[%d] %d\n",
949 instance, rc);
e3d62d7e
CM
950 return -EIO;
951 }
952
5e7a54a2
CM
953 /* Update all cpus, sequentially (to protect "netif_napi_add()").
954 * We use on_each_cpu to handle the IPI mask or unmask.
955 */
956 if (!saw_channel)
f3286a3a
CM
957 on_each_cpu(disable_ingress_irq,
958 (void *)(long)(md->ingress_irq), 1);
5e7a54a2
CM
959 for_each_online_cpu(cpu) {
960 struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
f3286a3a
CM
961
962 if (!info->mpipe[instance].has_iqueue)
5e7a54a2
CM
963 continue;
964 if (saw_channel) {
f3286a3a
CM
965 if (!info->mpipe[instance].napi_added) {
966 netif_napi_add(dev, &info->mpipe[instance].napi,
5e7a54a2 967 tile_net_poll, TILE_NET_WEIGHT);
f3286a3a 968 info->mpipe[instance].napi_added = true;
5e7a54a2 969 }
f3286a3a
CM
970 if (!info->mpipe[instance].napi_enabled) {
971 napi_enable(&info->mpipe[instance].napi);
972 info->mpipe[instance].napi_enabled = true;
5e7a54a2
CM
973 }
974 } else {
f3286a3a
CM
975 if (info->mpipe[instance].napi_enabled) {
976 napi_disable(&info->mpipe[instance].napi);
977 info->mpipe[instance].napi_enabled = false;
5e7a54a2
CM
978 }
979 /* FIXME: Drain the iqueue. */
980 }
981 }
982 if (saw_channel)
f3286a3a
CM
983 on_each_cpu(enable_ingress_irq,
984 (void *)(long)(md->ingress_irq), 1);
e3d62d7e
CM
985
986 /* HACK: Allow packets to flow in the simulator. */
987 if (saw_channel)
f3286a3a 988 sim_enable_mpipe_links(instance, -1);
e3d62d7e
CM
989
990 return 0;
991}
992
2628e8af
CM
993/* Initialize a buffer stack. */
994static int create_buffer_stack(struct net_device *dev,
995 int kind, size_t num_buffers)
e3d62d7e
CM
996{
997 pte_t hash_pte = pte_set_home((pte_t) { 0 }, PAGE_HOME_HASH);
f3286a3a
CM
998 int instance = mpipe_instance(dev);
999 struct mpipe_data *md = &mpipe_data[instance];
2628e8af 1000 size_t needed = gxio_mpipe_calc_buffer_stack_bytes(num_buffers);
f3286a3a 1001 int stack_idx = md->first_buffer_stack + kind;
2628e8af
CM
1002 void *va;
1003 int i, rc;
e3d62d7e 1004
2628e8af
CM
1005 /* Round up to 64KB and then use alloc_pages() so we get the
1006 * required 64KB alignment.
e3d62d7e 1007 */
f3286a3a
CM
1008 md->buffer_stack_bytes[kind] =
1009 ALIGN(needed, 64 * 1024);
e3d62d7e 1010
f3286a3a 1011 va = alloc_pages_exact(md->buffer_stack_bytes[kind], GFP_KERNEL);
2628e8af 1012 if (va == NULL) {
e3d62d7e 1013 netdev_err(dev,
2628e8af 1014 "Could not alloc %zd bytes for buffer stack %d\n",
f3286a3a 1015 md->buffer_stack_bytes[kind], kind);
e3d62d7e
CM
1016 return -ENOMEM;
1017 }
2628e8af
CM
1018
1019 /* Initialize the buffer stack. */
f3286a3a
CM
1020 rc = gxio_mpipe_init_buffer_stack(&md->context, stack_idx,
1021 buffer_size_enums[kind], va,
1022 md->buffer_stack_bytes[kind], 0);
e3d62d7e 1023 if (rc != 0) {
f3286a3a
CM
1024 netdev_err(dev, "gxio_mpipe_init_buffer_stack: mpipe[%d] %d\n",
1025 instance, rc);
1026 free_pages_exact(va, md->buffer_stack_bytes[kind]);
e3d62d7e
CM
1027 return rc;
1028 }
2628e8af 1029
f3286a3a 1030 md->buffer_stack_vas[kind] = va;
2628e8af 1031
f3286a3a 1032 rc = gxio_mpipe_register_client_memory(&md->context, stack_idx,
e3d62d7e
CM
1033 hash_pte, 0);
1034 if (rc != 0) {
f3286a3a
CM
1035 netdev_err(dev,
1036 "gxio_mpipe_register_client_memory: mpipe[%d] %d\n",
1037 instance, rc);
e3d62d7e
CM
1038 return rc;
1039 }
1040
2628e8af
CM
1041 /* Provide initial buffers. */
1042 for (i = 0; i < num_buffers; i++) {
f3286a3a 1043 if (!tile_net_provide_buffer(instance, kind)) {
2628e8af
CM
1044 netdev_err(dev, "Cannot allocate initial sk_bufs!\n");
1045 return -ENOMEM;
1046 }
e3d62d7e 1047 }
2628e8af
CM
1048
1049 return 0;
1050}
1051
1052/* Allocate and initialize mpipe buffer stacks, and register them in
1053 * the mPIPE TLBs, for small, large, and (possibly) jumbo packet sizes.
1054 * This routine supports tile_net_init_mpipe(), below.
1055 */
1056static int init_buffer_stacks(struct net_device *dev,
1057 int network_cpus_count)
1058{
1059 int num_kinds = MAX_KINDS - (jumbo_num == 0);
1060 size_t num_buffers;
1061 int rc;
f3286a3a
CM
1062 int instance = mpipe_instance(dev);
1063 struct mpipe_data *md = &mpipe_data[instance];
2628e8af
CM
1064
1065 /* Allocate the buffer stacks. */
f3286a3a 1066 rc = gxio_mpipe_alloc_buffer_stacks(&md->context, num_kinds, 0, 0);
2628e8af 1067 if (rc < 0) {
f3286a3a
CM
1068 netdev_err(dev,
1069 "gxio_mpipe_alloc_buffer_stacks: mpipe[%d] %d\n",
1070 instance, rc);
e3d62d7e
CM
1071 return rc;
1072 }
f3286a3a 1073 md->first_buffer_stack = rc;
e3d62d7e 1074
2628e8af
CM
1075 /* Enough small/large buffers to (normally) avoid buffer errors. */
1076 num_buffers =
1077 network_cpus_count * (IQUEUE_ENTRIES + TILE_NET_BATCH);
1078
1079 /* Allocate the small memory stack. */
1080 if (rc >= 0)
1081 rc = create_buffer_stack(dev, 0, num_buffers);
1082
1083 /* Allocate the large buffer stack. */
1084 if (rc >= 0)
1085 rc = create_buffer_stack(dev, 1, num_buffers);
1086
1087 /* Allocate the jumbo buffer stack if needed. */
1088 if (rc >= 0 && jumbo_num != 0)
1089 rc = create_buffer_stack(dev, 2, jumbo_num);
1090
1091 return rc;
e3d62d7e
CM
1092}
1093
1094/* Allocate per-cpu resources (memory for completions and idescs).
1095 * This routine supports tile_net_init_mpipe(), below.
1096 */
1097static int alloc_percpu_mpipe_resources(struct net_device *dev,
1098 int cpu, int ring)
1099{
1100 struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
1101 int order, i, rc;
f3286a3a
CM
1102 int instance = mpipe_instance(dev);
1103 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
1104 struct page *page;
1105 void *addr;
1106
1107 /* Allocate the "comps". */
1108 order = get_order(COMPS_SIZE);
1109 page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
1110 if (page == NULL) {
1111 netdev_err(dev, "Failed to alloc %zd bytes comps memory\n",
1112 COMPS_SIZE);
1113 return -ENOMEM;
1114 }
1115 addr = pfn_to_kaddr(page_to_pfn(page));
1116 memset(addr, 0, COMPS_SIZE);
1117 for (i = 0; i < TILE_NET_CHANNELS; i++)
f3286a3a 1118 info->mpipe[instance].comps_for_echannel[i] =
e3d62d7e
CM
1119 addr + i * sizeof(struct tile_net_comps);
1120
1121 /* If this is a network cpu, create an iqueue. */
1122 if (cpu_isset(cpu, network_cpus_map)) {
1123 order = get_order(NOTIF_RING_SIZE);
1124 page = homecache_alloc_pages(GFP_KERNEL, order, cpu);
1125 if (page == NULL) {
1126 netdev_err(dev,
1127 "Failed to alloc %zd bytes iqueue memory\n",
1128 NOTIF_RING_SIZE);
1129 return -ENOMEM;
1130 }
1131 addr = pfn_to_kaddr(page_to_pfn(page));
f3286a3a
CM
1132 rc = gxio_mpipe_iqueue_init(&info->mpipe[instance].iqueue,
1133 &md->context, ring++, addr,
1134 NOTIF_RING_SIZE, 0);
e3d62d7e
CM
1135 if (rc < 0) {
1136 netdev_err(dev,
1137 "gxio_mpipe_iqueue_init failed: %d\n", rc);
1138 return rc;
1139 }
f3286a3a 1140 info->mpipe[instance].has_iqueue = true;
e3d62d7e
CM
1141 }
1142
1143 return ring;
1144}
1145
1146/* Initialize NotifGroup and buckets.
1147 * This routine supports tile_net_init_mpipe(), below.
1148 */
1149static int init_notif_group_and_buckets(struct net_device *dev,
1150 int ring, int network_cpus_count)
1151{
1152 int group, rc;
f3286a3a
CM
1153 int instance = mpipe_instance(dev);
1154 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
1155
1156 /* Allocate one NotifGroup. */
f3286a3a 1157 rc = gxio_mpipe_alloc_notif_groups(&md->context, 1, 0, 0);
e3d62d7e 1158 if (rc < 0) {
f3286a3a
CM
1159 netdev_err(dev, "gxio_mpipe_alloc_notif_groups: mpipe[%d] %d\n",
1160 instance, rc);
e3d62d7e
CM
1161 return rc;
1162 }
1163 group = rc;
1164
1165 /* Initialize global num_buckets value. */
1166 if (network_cpus_count > 4)
f3286a3a 1167 md->num_buckets = 256;
e3d62d7e 1168 else if (network_cpus_count > 1)
f3286a3a 1169 md->num_buckets = 16;
e3d62d7e
CM
1170
1171 /* Allocate some buckets, and set global first_bucket value. */
f3286a3a 1172 rc = gxio_mpipe_alloc_buckets(&md->context, md->num_buckets, 0, 0);
e3d62d7e 1173 if (rc < 0) {
f3286a3a
CM
1174 netdev_err(dev, "gxio_mpipe_alloc_buckets: mpipe[%d] %d\n",
1175 instance, rc);
e3d62d7e
CM
1176 return rc;
1177 }
f3286a3a 1178 md->first_bucket = rc;
e3d62d7e
CM
1179
1180 /* Init group and buckets. */
1181 rc = gxio_mpipe_init_notif_group_and_buckets(
f3286a3a
CM
1182 &md->context, group, ring, network_cpus_count,
1183 md->first_bucket, md->num_buckets,
e3d62d7e
CM
1184 GXIO_MPIPE_BUCKET_STICKY_FLOW_LOCALITY);
1185 if (rc != 0) {
f3286a3a
CM
1186 netdev_err(dev, "gxio_mpipe_init_notif_group_and_buckets: "
1187 "mpipe[%d] %d\n", instance, rc);
e3d62d7e
CM
1188 return rc;
1189 }
1190
1191 return 0;
1192}
1193
1194/* Create an irq and register it, then activate the irq and request
1195 * interrupts on all cores. Note that "ingress_irq" being initialized
1196 * is how we know not to call tile_net_init_mpipe() again.
1197 * This routine supports tile_net_init_mpipe(), below.
1198 */
1199static int tile_net_setup_interrupts(struct net_device *dev)
1200{
f3286a3a
CM
1201 int cpu, rc, irq;
1202 int instance = mpipe_instance(dev);
1203 struct mpipe_data *md = &mpipe_data[instance];
1204
1205 irq = md->ingress_irq;
1206 if (irq < 0) {
1207 irq = create_irq();
1208 if (irq < 0) {
1209 netdev_err(dev,
1210 "create_irq failed: mpipe[%d] %d\n",
1211 instance, irq);
1212 return irq;
1213 }
1214 tile_irq_activate(irq, TILE_IRQ_PERCPU);
e3d62d7e 1215
f3286a3a
CM
1216 rc = request_irq(irq, tile_net_handle_ingress_irq,
1217 0, "tile_net", (void *)((uint64_t)instance));
1218
1219 if (rc != 0) {
1220 netdev_err(dev, "request_irq failed: mpipe[%d] %d\n",
1221 instance, rc);
1222 destroy_irq(irq);
1223 return rc;
1224 }
1225 md->ingress_irq = irq;
e3d62d7e
CM
1226 }
1227
1228 for_each_online_cpu(cpu) {
1229 struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
f3286a3a
CM
1230 if (info->mpipe[instance].has_iqueue) {
1231 gxio_mpipe_request_notif_ring_interrupt(&md->context,
1232 cpu_x(cpu), cpu_y(cpu), KERNEL_PL, irq,
1233 info->mpipe[instance].iqueue.ring);
e3d62d7e
CM
1234 }
1235 }
1236
1237 return 0;
1238}
1239
1240/* Undo any state set up partially by a failed call to tile_net_init_mpipe. */
f3286a3a 1241static void tile_net_init_mpipe_fail(int instance)
e3d62d7e 1242{
2628e8af 1243 int kind, cpu;
f3286a3a 1244 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
1245
1246 /* Do cleanups that require the mpipe context first. */
2628e8af 1247 for (kind = 0; kind < MAX_KINDS; kind++) {
f3286a3a
CM
1248 if (md->buffer_stack_vas[kind] != NULL) {
1249 tile_net_pop_all_buffers(instance,
1250 md->first_buffer_stack +
1251 kind);
2628e8af
CM
1252 }
1253 }
e3d62d7e
CM
1254
1255 /* Destroy mpipe context so the hardware no longer owns any memory. */
f3286a3a 1256 gxio_mpipe_destroy(&md->context);
e3d62d7e
CM
1257
1258 for_each_online_cpu(cpu) {
1259 struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
f3286a3a
CM
1260 free_pages(
1261 (unsigned long)(
1262 info->mpipe[instance].comps_for_echannel[0]),
1263 get_order(COMPS_SIZE));
1264 info->mpipe[instance].comps_for_echannel[0] = NULL;
1265 free_pages((unsigned long)(info->mpipe[instance].iqueue.idescs),
e3d62d7e 1266 get_order(NOTIF_RING_SIZE));
f3286a3a 1267 info->mpipe[instance].iqueue.idescs = NULL;
e3d62d7e
CM
1268 }
1269
2628e8af 1270 for (kind = 0; kind < MAX_KINDS; kind++) {
f3286a3a
CM
1271 if (md->buffer_stack_vas[kind] != NULL) {
1272 free_pages_exact(md->buffer_stack_vas[kind],
1273 md->buffer_stack_bytes[kind]);
1274 md->buffer_stack_vas[kind] = NULL;
2628e8af
CM
1275 }
1276 }
e3d62d7e 1277
f3286a3a
CM
1278 md->first_buffer_stack = -1;
1279 md->first_bucket = -1;
e3d62d7e
CM
1280}
1281
1282/* The first time any tilegx network device is opened, we initialize
1283 * the global mpipe state. If this step fails, we fail to open the
1284 * device, but if it succeeds, we never need to do it again, and since
1285 * tile_net can't be unloaded, we never undo it.
1286 *
1287 * Note that some resources in this path (buffer stack indices,
1288 * bindings from init_buffer_stack, etc.) are hypervisor resources
1289 * that are freed implicitly by gxio_mpipe_destroy().
1290 */
1291static int tile_net_init_mpipe(struct net_device *dev)
1292{
2628e8af 1293 int rc;
e3d62d7e
CM
1294 int cpu;
1295 int first_ring, ring;
f3286a3a
CM
1296 int instance = mpipe_instance(dev);
1297 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
1298 int network_cpus_count = cpus_weight(network_cpus_map);
1299
1300 if (!hash_default) {
1301 netdev_err(dev, "Networking requires hash_default!\n");
1302 return -EIO;
1303 }
1304
f3286a3a 1305 rc = gxio_mpipe_init(&md->context, instance);
e3d62d7e 1306 if (rc != 0) {
f3286a3a
CM
1307 netdev_err(dev, "gxio_mpipe_init: mpipe[%d] %d\n",
1308 instance, rc);
e3d62d7e
CM
1309 return -EIO;
1310 }
1311
1312 /* Set up the buffer stacks. */
2628e8af 1313 rc = init_buffer_stacks(dev, network_cpus_count);
e3d62d7e
CM
1314 if (rc != 0)
1315 goto fail;
1316
e3d62d7e 1317 /* Allocate one NotifRing for each network cpu. */
f3286a3a
CM
1318 rc = gxio_mpipe_alloc_notif_rings(&md->context,
1319 network_cpus_count, 0, 0);
e3d62d7e
CM
1320 if (rc < 0) {
1321 netdev_err(dev, "gxio_mpipe_alloc_notif_rings failed %d\n",
1322 rc);
1323 goto fail;
1324 }
1325
1326 /* Init NotifRings per-cpu. */
1327 first_ring = rc;
1328 ring = first_ring;
1329 for_each_online_cpu(cpu) {
1330 rc = alloc_percpu_mpipe_resources(dev, cpu, ring);
1331 if (rc < 0)
1332 goto fail;
1333 ring = rc;
1334 }
1335
1336 /* Initialize NotifGroup and buckets. */
1337 rc = init_notif_group_and_buckets(dev, first_ring, network_cpus_count);
1338 if (rc != 0)
1339 goto fail;
1340
1341 /* Create and enable interrupts. */
1342 rc = tile_net_setup_interrupts(dev);
1343 if (rc != 0)
1344 goto fail;
1345
9ab5ec59
CM
1346 /* Register PTP clock and set mPIPE timestamp, if configured. */
1347 register_ptp_clock(dev, md);
1348
e3d62d7e
CM
1349 return 0;
1350
1351fail:
f3286a3a 1352 tile_net_init_mpipe_fail(instance);
e3d62d7e
CM
1353 return rc;
1354}
1355
1356/* Create persistent egress info for a given egress channel.
1357 * Note that this may be shared between, say, "gbe0" and "xgbe0".
1358 * ISSUE: Defer header allocation until TSO is actually needed?
1359 */
1360static int tile_net_init_egress(struct net_device *dev, int echannel)
1361{
2628e8af 1362 static int ering = -1;
e3d62d7e
CM
1363 struct page *headers_page, *edescs_page, *equeue_page;
1364 gxio_mpipe_edesc_t *edescs;
1365 gxio_mpipe_equeue_t *equeue;
1366 unsigned char *headers;
1367 int headers_order, edescs_order, equeue_order;
1368 size_t edescs_size;
e3d62d7e 1369 int rc = -ENOMEM;
f3286a3a
CM
1370 int instance = mpipe_instance(dev);
1371 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
1372
1373 /* Only initialize once. */
f3286a3a 1374 if (md->egress_for_echannel[echannel].equeue != NULL)
e3d62d7e
CM
1375 return 0;
1376
1377 /* Allocate memory for the "headers". */
1378 headers_order = get_order(EQUEUE_ENTRIES * HEADER_BYTES);
1379 headers_page = alloc_pages(GFP_KERNEL, headers_order);
1380 if (headers_page == NULL) {
1381 netdev_warn(dev,
1382 "Could not alloc %zd bytes for TSO headers.\n",
1383 PAGE_SIZE << headers_order);
1384 goto fail;
1385 }
1386 headers = pfn_to_kaddr(page_to_pfn(headers_page));
1387
1388 /* Allocate memory for the "edescs". */
1389 edescs_size = EQUEUE_ENTRIES * sizeof(*edescs);
1390 edescs_order = get_order(edescs_size);
1391 edescs_page = alloc_pages(GFP_KERNEL, edescs_order);
1392 if (edescs_page == NULL) {
1393 netdev_warn(dev,
1394 "Could not alloc %zd bytes for eDMA ring.\n",
1395 edescs_size);
1396 goto fail_headers;
1397 }
1398 edescs = pfn_to_kaddr(page_to_pfn(edescs_page));
1399
1400 /* Allocate memory for the "equeue". */
1401 equeue_order = get_order(sizeof(*equeue));
1402 equeue_page = alloc_pages(GFP_KERNEL, equeue_order);
1403 if (equeue_page == NULL) {
1404 netdev_warn(dev,
1405 "Could not alloc %zd bytes for equeue info.\n",
1406 PAGE_SIZE << equeue_order);
1407 goto fail_edescs;
1408 }
1409 equeue = pfn_to_kaddr(page_to_pfn(equeue_page));
1410
2628e8af
CM
1411 /* Allocate an edma ring (using a one entry "free list"). */
1412 if (ering < 0) {
f3286a3a 1413 rc = gxio_mpipe_alloc_edma_rings(&md->context, 1, 0, 0);
2628e8af 1414 if (rc < 0) {
f3286a3a
CM
1415 netdev_warn(dev, "gxio_mpipe_alloc_edma_rings: "
1416 "mpipe[%d] %d\n", instance, rc);
2628e8af
CM
1417 goto fail_equeue;
1418 }
1419 ering = rc;
e3d62d7e 1420 }
e3d62d7e
CM
1421
1422 /* Initialize the equeue. */
f3286a3a 1423 rc = gxio_mpipe_equeue_init(equeue, &md->context, ering, echannel,
e3d62d7e
CM
1424 edescs, edescs_size, 0);
1425 if (rc != 0) {
f3286a3a
CM
1426 netdev_err(dev, "gxio_mpipe_equeue_init: mpipe[%d] %d\n",
1427 instance, rc);
e3d62d7e
CM
1428 goto fail_equeue;
1429 }
1430
2628e8af
CM
1431 /* Don't reuse the ering later. */
1432 ering = -1;
1433
1434 if (jumbo_num != 0) {
1435 /* Make sure "jumbo" packets can be egressed safely. */
1436 if (gxio_mpipe_equeue_set_snf_size(equeue, 10368) < 0) {
1437 /* ISSUE: There is no "gxio_mpipe_equeue_destroy()". */
1438 netdev_warn(dev, "Jumbo packets may not be egressed"
1439 " properly on channel %d\n", echannel);
1440 }
1441 }
1442
e3d62d7e 1443 /* Done. */
f3286a3a
CM
1444 md->egress_for_echannel[echannel].equeue = equeue;
1445 md->egress_for_echannel[echannel].headers = headers;
e3d62d7e
CM
1446 return 0;
1447
1448fail_equeue:
1449 __free_pages(equeue_page, equeue_order);
1450
1451fail_edescs:
1452 __free_pages(edescs_page, edescs_order);
1453
1454fail_headers:
1455 __free_pages(headers_page, headers_order);
1456
1457fail:
1458 return rc;
1459}
1460
1461/* Return channel number for a newly-opened link. */
1462static int tile_net_link_open(struct net_device *dev, gxio_mpipe_link_t *link,
1463 const char *link_name)
1464{
f3286a3a
CM
1465 int instance = mpipe_instance(dev);
1466 struct mpipe_data *md = &mpipe_data[instance];
1467 int rc = gxio_mpipe_link_open(link, &md->context, link_name, 0);
e3d62d7e 1468 if (rc < 0) {
f3286a3a
CM
1469 netdev_err(dev, "Failed to open '%s', mpipe[%d], %d\n",
1470 link_name, instance, rc);
e3d62d7e
CM
1471 return rc;
1472 }
2628e8af
CM
1473 if (jumbo_num != 0) {
1474 u32 attr = GXIO_MPIPE_LINK_RECEIVE_JUMBO;
1475 rc = gxio_mpipe_link_set_attr(link, attr, 1);
1476 if (rc != 0) {
1477 netdev_err(dev,
1478 "Cannot receive jumbo packets on '%s'\n",
1479 link_name);
1480 gxio_mpipe_link_close(link);
1481 return rc;
1482 }
1483 }
e3d62d7e
CM
1484 rc = gxio_mpipe_link_channel(link);
1485 if (rc < 0 || rc >= TILE_NET_CHANNELS) {
1486 netdev_err(dev, "gxio_mpipe_link_channel bad value: %d\n", rc);
1487 gxio_mpipe_link_close(link);
1488 return -EINVAL;
1489 }
1490 return rc;
1491}
1492
1493/* Help the kernel activate the given network interface. */
1494static int tile_net_open(struct net_device *dev)
1495{
1496 struct tile_net_priv *priv = netdev_priv(dev);
f3286a3a 1497 int cpu, rc, instance;
e3d62d7e
CM
1498
1499 mutex_lock(&tile_net_devs_for_channel_mutex);
1500
f3286a3a
CM
1501 /* Get the instance info. */
1502 rc = gxio_mpipe_link_instance(dev->name);
1155e964
WY
1503 if (rc < 0 || rc >= NR_MPIPE_MAX) {
1504 mutex_unlock(&tile_net_devs_for_channel_mutex);
f3286a3a 1505 return -EIO;
1155e964 1506 }
f3286a3a
CM
1507
1508 priv->instance = rc;
1509 instance = rc;
1510 if (!mpipe_data[rc].context.mmio_fast_base) {
1511 /* Do one-time initialization per instance the first time
1512 * any device is opened.
1513 */
e3d62d7e
CM
1514 rc = tile_net_init_mpipe(dev);
1515 if (rc != 0)
1516 goto fail;
1517 }
1518
1519 /* Determine if this is the "loopify" device. */
1520 if (unlikely((loopify_link_name != NULL) &&
1521 !strcmp(dev->name, loopify_link_name))) {
1522 rc = tile_net_link_open(dev, &priv->link, "loop0");
1523 if (rc < 0)
1524 goto fail;
1525 priv->channel = rc;
1526 rc = tile_net_link_open(dev, &priv->loopify_link, "loop1");
1527 if (rc < 0)
1528 goto fail;
1529 priv->loopify_channel = rc;
1530 priv->echannel = rc;
1531 } else {
1532 rc = tile_net_link_open(dev, &priv->link, dev->name);
1533 if (rc < 0)
1534 goto fail;
1535 priv->channel = rc;
1536 priv->echannel = rc;
1537 }
1538
1539 /* Initialize egress info (if needed). Once ever, per echannel. */
1540 rc = tile_net_init_egress(dev, priv->echannel);
1541 if (rc != 0)
1542 goto fail;
1543
f3286a3a 1544 mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = dev;
e3d62d7e
CM
1545
1546 rc = tile_net_update(dev);
1547 if (rc != 0)
1548 goto fail;
1549
1550 mutex_unlock(&tile_net_devs_for_channel_mutex);
1551
1552 /* Initialize the transmit wake timer for this device for each cpu. */
1553 for_each_online_cpu(cpu) {
1554 struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
1555 struct tile_net_tx_wake *tx_wake =
f3286a3a 1556 &info->mpipe[instance].tx_wake[priv->echannel];
e3d62d7e
CM
1557
1558 hrtimer_init(&tx_wake->timer, CLOCK_MONOTONIC,
1559 HRTIMER_MODE_REL);
9b4c341b 1560 tx_wake->tx_queue_idx = cpu;
e3d62d7e
CM
1561 tx_wake->timer.function = tile_net_handle_tx_wake_timer;
1562 tx_wake->dev = dev;
1563 }
1564
1565 for_each_online_cpu(cpu)
1566 netif_start_subqueue(dev, cpu);
1567 netif_carrier_on(dev);
1568 return 0;
1569
1570fail:
1571 if (priv->loopify_channel >= 0) {
1572 if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
1573 netdev_warn(dev, "Failed to close loopify link!\n");
1574 priv->loopify_channel = -1;
1575 }
1576 if (priv->channel >= 0) {
1577 if (gxio_mpipe_link_close(&priv->link) != 0)
1578 netdev_warn(dev, "Failed to close link!\n");
1579 priv->channel = -1;
1580 }
1581 priv->echannel = -1;
f3286a3a 1582 mpipe_data[instance].tile_net_devs_for_channel[priv->channel] = NULL;
e3d62d7e
CM
1583 mutex_unlock(&tile_net_devs_for_channel_mutex);
1584
1585 /* Don't return raw gxio error codes to generic Linux. */
1586 return (rc > -512) ? rc : -EIO;
1587}
1588
1589/* Help the kernel deactivate the given network interface. */
1590static int tile_net_stop(struct net_device *dev)
1591{
1592 struct tile_net_priv *priv = netdev_priv(dev);
1593 int cpu;
f3286a3a
CM
1594 int instance = priv->instance;
1595 struct mpipe_data *md = &mpipe_data[instance];
e3d62d7e
CM
1596
1597 for_each_online_cpu(cpu) {
1598 struct tile_net_info *info = &per_cpu(per_cpu_info, cpu);
1599 struct tile_net_tx_wake *tx_wake =
f3286a3a 1600 &info->mpipe[instance].tx_wake[priv->echannel];
e3d62d7e
CM
1601
1602 hrtimer_cancel(&tx_wake->timer);
1603 netif_stop_subqueue(dev, cpu);
1604 }
1605
1606 mutex_lock(&tile_net_devs_for_channel_mutex);
f3286a3a 1607 md->tile_net_devs_for_channel[priv->channel] = NULL;
e3d62d7e
CM
1608 (void)tile_net_update(dev);
1609 if (priv->loopify_channel >= 0) {
1610 if (gxio_mpipe_link_close(&priv->loopify_link) != 0)
1611 netdev_warn(dev, "Failed to close loopify link!\n");
1612 priv->loopify_channel = -1;
1613 }
1614 if (priv->channel >= 0) {
1615 if (gxio_mpipe_link_close(&priv->link) != 0)
1616 netdev_warn(dev, "Failed to close link!\n");
1617 priv->channel = -1;
1618 }
1619 priv->echannel = -1;
1620 mutex_unlock(&tile_net_devs_for_channel_mutex);
1621
1622 return 0;
1623}
1624
1625/* Determine the VA for a fragment. */
1626static inline void *tile_net_frag_buf(skb_frag_t *f)
1627{
1628 unsigned long pfn = page_to_pfn(skb_frag_page(f));
1629 return pfn_to_kaddr(pfn) + f->page_offset;
1630}
1631
1632/* Acquire a completion entry and an egress slot, or if we can't,
1633 * stop the queue and schedule the tx_wake timer.
1634 */
1635static s64 tile_net_equeue_try_reserve(struct net_device *dev,
9b4c341b 1636 int tx_queue_idx,
e3d62d7e
CM
1637 struct tile_net_comps *comps,
1638 gxio_mpipe_equeue_t *equeue,
1639 int num_edescs)
1640{
1641 /* Try to acquire a completion entry. */
1642 if (comps->comp_next - comps->comp_last < TILE_NET_MAX_COMPS - 1 ||
1643 tile_net_free_comps(equeue, comps, 32, false) != 0) {
1644
1645 /* Try to acquire an egress slot. */
1646 s64 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
1647 if (slot >= 0)
1648 return slot;
1649
1650 /* Freeing some completions gives the equeue time to drain. */
1651 tile_net_free_comps(equeue, comps, TILE_NET_MAX_COMPS, false);
1652
1653 slot = gxio_mpipe_equeue_try_reserve(equeue, num_edescs);
1654 if (slot >= 0)
1655 return slot;
1656 }
1657
1658 /* Still nothing; give up and stop the queue for a short while. */
9b4c341b
CM
1659 netif_stop_subqueue(dev, tx_queue_idx);
1660 tile_net_schedule_tx_wake_timer(dev, tx_queue_idx);
e3d62d7e
CM
1661 return -1;
1662}
1663
1664/* Determine how many edesc's are needed for TSO.
1665 *
1666 * Sometimes, if "sendfile()" requires copying, we will be called with
1667 * "data" containing the header and payload, with "frags" being empty.
1668 * Sometimes, for example when using NFS over TCP, a single segment can
1669 * span 3 fragments. This requires special care.
1670 */
1671static int tso_count_edescs(struct sk_buff *skb)
1672{
1673 struct skb_shared_info *sh = skb_shinfo(skb);
8388546e 1674 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3da3fff8 1675 unsigned int data_len = skb->len - sh_len;
e3d62d7e
CM
1676 unsigned int p_len = sh->gso_size;
1677 long f_id = -1; /* id of the current fragment */
3da3fff8
CM
1678 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
1679 long f_used = 0; /* bytes used from the current fragment */
e3d62d7e
CM
1680 long n; /* size of the current piece of payload */
1681 int num_edescs = 0;
1682 int segment;
1683
1684 for (segment = 0; segment < sh->gso_segs; segment++) {
1685
1686 unsigned int p_used = 0;
1687
1688 /* One edesc for header and for each piece of the payload. */
1689 for (num_edescs++; p_used < p_len; num_edescs++) {
1690
1691 /* Advance as needed. */
1692 while (f_used >= f_size) {
1693 f_id++;
3da3fff8 1694 f_size = skb_frag_size(&sh->frags[f_id]);
e3d62d7e
CM
1695 f_used = 0;
1696 }
1697
1698 /* Use bytes from the current fragment. */
1699 n = p_len - p_used;
1700 if (n > f_size - f_used)
1701 n = f_size - f_used;
1702 f_used += n;
1703 p_used += n;
1704 }
1705
1706 /* The last segment may be less than gso_size. */
1707 data_len -= p_len;
1708 if (data_len < p_len)
1709 p_len = data_len;
1710 }
1711
1712 return num_edescs;
1713}
1714
2c7d04a9 1715/* Prepare modified copies of the skbuff headers. */
e3d62d7e
CM
1716static void tso_headers_prepare(struct sk_buff *skb, unsigned char *headers,
1717 s64 slot)
1718{
1719 struct skb_shared_info *sh = skb_shinfo(skb);
1720 struct iphdr *ih;
2c7d04a9 1721 struct ipv6hdr *ih6;
e3d62d7e 1722 struct tcphdr *th;
8388546e 1723 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3da3fff8 1724 unsigned int data_len = skb->len - sh_len;
e3d62d7e 1725 unsigned char *data = skb->data;
8388546e 1726 unsigned int ih_off, th_off, p_len;
444fa88a
CM
1727 unsigned int isum_seed, tsum_seed, seq;
1728 unsigned int uninitialized_var(id);
2c7d04a9 1729 int is_ipv6;
e3d62d7e 1730 long f_id = -1; /* id of the current fragment */
3da3fff8
CM
1731 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
1732 long f_used = 0; /* bytes used from the current fragment */
e3d62d7e
CM
1733 long n; /* size of the current piece of payload */
1734 int segment;
1735
1736 /* Locate original headers and compute various lengths. */
2c7d04a9
CM
1737 is_ipv6 = skb_is_gso_v6(skb);
1738 if (is_ipv6) {
1739 ih6 = ipv6_hdr(skb);
1740 ih_off = skb_network_offset(skb);
1741 } else {
1742 ih = ip_hdr(skb);
1743 ih_off = skb_network_offset(skb);
1744 isum_seed = ((0xFFFF - ih->check) +
1745 (0xFFFF - ih->tot_len) +
1746 (0xFFFF - ih->id));
1747 id = ntohs(ih->id);
1748 }
1749
e3d62d7e 1750 th = tcp_hdr(skb);
e3d62d7e 1751 th_off = skb_transport_offset(skb);
e3d62d7e
CM
1752 p_len = sh->gso_size;
1753
3da3fff8 1754 tsum_seed = th->check + (0xFFFF ^ htons(skb->len));
e3d62d7e
CM
1755 seq = ntohl(th->seq);
1756
1757 /* Prepare all the headers. */
1758 for (segment = 0; segment < sh->gso_segs; segment++) {
1759 unsigned char *buf;
1760 unsigned int p_used = 0;
1761
1762 /* Copy to the header memory for this segment. */
1763 buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
1764 NET_IP_ALIGN;
1765 memcpy(buf, data, sh_len);
1766
1767 /* Update copied ip header. */
2c7d04a9
CM
1768 if (is_ipv6) {
1769 ih6 = (struct ipv6hdr *)(buf + ih_off);
1770 ih6->payload_len = htons(sh_len + p_len - ih_off -
1771 sizeof(*ih6));
1772 } else {
1773 ih = (struct iphdr *)(buf + ih_off);
1774 ih->tot_len = htons(sh_len + p_len - ih_off);
444fa88a 1775 ih->id = htons(id++);
2c7d04a9
CM
1776 ih->check = csum_long(isum_seed + ih->tot_len +
1777 ih->id) ^ 0xffff;
1778 }
e3d62d7e
CM
1779
1780 /* Update copied tcp header. */
1781 th = (struct tcphdr *)(buf + th_off);
1782 th->seq = htonl(seq);
1783 th->check = csum_long(tsum_seed + htons(sh_len + p_len));
1784 if (segment != sh->gso_segs - 1) {
1785 th->fin = 0;
1786 th->psh = 0;
1787 }
1788
1789 /* Skip past the header. */
1790 slot++;
1791
1792 /* Skip past the payload. */
1793 while (p_used < p_len) {
1794
1795 /* Advance as needed. */
1796 while (f_used >= f_size) {
1797 f_id++;
3da3fff8 1798 f_size = skb_frag_size(&sh->frags[f_id]);
e3d62d7e
CM
1799 f_used = 0;
1800 }
1801
1802 /* Use bytes from the current fragment. */
1803 n = p_len - p_used;
1804 if (n > f_size - f_used)
1805 n = f_size - f_used;
1806 f_used += n;
1807 p_used += n;
1808
1809 slot++;
1810 }
1811
e3d62d7e
CM
1812 seq += p_len;
1813
1814 /* The last segment may be less than gso_size. */
1815 data_len -= p_len;
1816 if (data_len < p_len)
1817 p_len = data_len;
1818 }
1819
1820 /* Flush the headers so they are ready for hardware DMA. */
1821 wmb();
1822}
1823
1824/* Pass all the data to mpipe for egress. */
1825static void tso_egress(struct net_device *dev, gxio_mpipe_equeue_t *equeue,
1826 struct sk_buff *skb, unsigned char *headers, s64 slot)
1827{
e3d62d7e 1828 struct skb_shared_info *sh = skb_shinfo(skb);
f3286a3a
CM
1829 int instance = mpipe_instance(dev);
1830 struct mpipe_data *md = &mpipe_data[instance];
8388546e 1831 unsigned int sh_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3da3fff8 1832 unsigned int data_len = skb->len - sh_len;
e3d62d7e
CM
1833 unsigned int p_len = sh->gso_size;
1834 gxio_mpipe_edesc_t edesc_head = { { 0 } };
1835 gxio_mpipe_edesc_t edesc_body = { { 0 } };
1836 long f_id = -1; /* id of the current fragment */
3da3fff8
CM
1837 long f_size = skb_headlen(skb) - sh_len; /* current fragment size */
1838 long f_used = 0; /* bytes used from the current fragment */
1839 void *f_data = skb->data + sh_len;
e3d62d7e
CM
1840 long n; /* size of the current piece of payload */
1841 unsigned long tx_packets = 0, tx_bytes = 0;
8388546e 1842 unsigned int csum_start;
e3d62d7e
CM
1843 int segment;
1844
1845 /* Prepare to egress the headers: set up header edesc. */
1846 csum_start = skb_checksum_start_offset(skb);
e3d62d7e
CM
1847 edesc_head.csum = 1;
1848 edesc_head.csum_start = csum_start;
1849 edesc_head.csum_dest = csum_start + skb->csum_offset;
1850 edesc_head.xfer_size = sh_len;
1851
1852 /* This is only used to specify the TLB. */
f3286a3a
CM
1853 edesc_head.stack_idx = md->first_buffer_stack;
1854 edesc_body.stack_idx = md->first_buffer_stack;
e3d62d7e
CM
1855
1856 /* Egress all the edescs. */
1857 for (segment = 0; segment < sh->gso_segs; segment++) {
e3d62d7e
CM
1858 unsigned char *buf;
1859 unsigned int p_used = 0;
1860
1861 /* Egress the header. */
1862 buf = headers + (slot % EQUEUE_ENTRIES) * HEADER_BYTES +
1863 NET_IP_ALIGN;
1864 edesc_head.va = va_to_tile_io_addr(buf);
1865 gxio_mpipe_equeue_put_at(equeue, edesc_head, slot);
1866 slot++;
1867
1868 /* Egress the payload. */
1869 while (p_used < p_len) {
3da3fff8 1870 void *va;
e3d62d7e
CM
1871
1872 /* Advance as needed. */
1873 while (f_used >= f_size) {
1874 f_id++;
3da3fff8 1875 f_size = skb_frag_size(&sh->frags[f_id]);
8388546e 1876 f_data = tile_net_frag_buf(&sh->frags[f_id]);
3da3fff8 1877 f_used = 0;
e3d62d7e
CM
1878 }
1879
3da3fff8
CM
1880 va = f_data + f_used;
1881
e3d62d7e
CM
1882 /* Use bytes from the current fragment. */
1883 n = p_len - p_used;
1884 if (n > f_size - f_used)
1885 n = f_size - f_used;
1886 f_used += n;
1887 p_used += n;
1888
1889 /* Egress a piece of the payload. */
3da3fff8 1890 edesc_body.va = va_to_tile_io_addr(va);
e3d62d7e
CM
1891 edesc_body.xfer_size = n;
1892 edesc_body.bound = !(p_used < p_len);
1893 gxio_mpipe_equeue_put_at(equeue, edesc_body, slot);
1894 slot++;
1895 }
1896
1897 tx_packets++;
1898 tx_bytes += sh_len + p_len;
1899
1900 /* The last segment may be less than gso_size. */
1901 data_len -= p_len;
1902 if (data_len < p_len)
1903 p_len = data_len;
1904 }
1905
1906 /* Update stats. */
ad018185
CM
1907 tile_net_stats_add(tx_packets, &dev->stats.tx_packets);
1908 tile_net_stats_add(tx_bytes, &dev->stats.tx_bytes);
e3d62d7e
CM
1909}
1910
1911/* Do "TSO" handling for egress.
1912 *
1913 * Normally drivers set NETIF_F_TSO only to support hardware TSO;
1914 * otherwise the stack uses scatter-gather to implement GSO in software.
1915 * On our testing, enabling GSO support (via NETIF_F_SG) drops network
1916 * performance down to around 7.5 Gbps on the 10G interfaces, although
1917 * also dropping cpu utilization way down, to under 8%. But
1918 * implementing "TSO" in the driver brings performance back up to line
1919 * rate, while dropping cpu usage even further, to less than 4%. In
1920 * practice, profiling of GSO shows that skb_segment() is what causes
1921 * the performance overheads; we benefit in the driver from using
1922 * preallocated memory to duplicate the TCP/IP headers.
1923 */
1924static int tile_net_tx_tso(struct sk_buff *skb, struct net_device *dev)
1925{
1926 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
1927 struct tile_net_priv *priv = netdev_priv(dev);
1928 int channel = priv->echannel;
f3286a3a
CM
1929 int instance = priv->instance;
1930 struct mpipe_data *md = &mpipe_data[instance];
1931 struct tile_net_egress *egress = &md->egress_for_echannel[channel];
1932 struct tile_net_comps *comps =
1933 info->mpipe[instance].comps_for_echannel[channel];
e3d62d7e
CM
1934 gxio_mpipe_equeue_t *equeue = egress->equeue;
1935 unsigned long irqflags;
1936 int num_edescs;
1937 s64 slot;
1938
1939 /* Determine how many mpipe edesc's are needed. */
1940 num_edescs = tso_count_edescs(skb);
1941
1942 local_irq_save(irqflags);
1943
1944 /* Try to acquire a completion entry and an egress slot. */
9b4c341b
CM
1945 slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
1946 equeue, num_edescs);
e3d62d7e
CM
1947 if (slot < 0) {
1948 local_irq_restore(irqflags);
1949 return NETDEV_TX_BUSY;
1950 }
1951
1952 /* Set up copies of header data properly. */
1953 tso_headers_prepare(skb, egress->headers, slot);
1954
1955 /* Actually pass the data to the network hardware. */
1956 tso_egress(dev, equeue, skb, egress->headers, slot);
1957
1958 /* Add a completion record. */
1959 add_comp(equeue, comps, slot + num_edescs - 1, skb);
1960
1961 local_irq_restore(irqflags);
1962
1963 /* Make sure the egress timer is scheduled. */
1964 tile_net_schedule_egress_timer();
1965
1966 return NETDEV_TX_OK;
1967}
1968
1969/* Analyze the body and frags for a transmit request. */
1970static unsigned int tile_net_tx_frags(struct frag *frags,
1971 struct sk_buff *skb,
1972 void *b_data, unsigned int b_len)
1973{
1974 unsigned int i, n = 0;
1975
1976 struct skb_shared_info *sh = skb_shinfo(skb);
1977
1978 if (b_len != 0) {
1979 frags[n].buf = b_data;
1980 frags[n++].length = b_len;
1981 }
1982
1983 for (i = 0; i < sh->nr_frags; i++) {
1984 skb_frag_t *f = &sh->frags[i];
1985 frags[n].buf = tile_net_frag_buf(f);
1986 frags[n++].length = skb_frag_size(f);
1987 }
1988
1989 return n;
1990}
1991
1992/* Help the kernel transmit a packet. */
1993static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
1994{
1995 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
1996 struct tile_net_priv *priv = netdev_priv(dev);
f3286a3a
CM
1997 int instance = priv->instance;
1998 struct mpipe_data *md = &mpipe_data[instance];
1999 struct tile_net_egress *egress =
2000 &md->egress_for_echannel[priv->echannel];
e3d62d7e
CM
2001 gxio_mpipe_equeue_t *equeue = egress->equeue;
2002 struct tile_net_comps *comps =
f3286a3a 2003 info->mpipe[instance].comps_for_echannel[priv->echannel];
e3d62d7e
CM
2004 unsigned int len = skb->len;
2005 unsigned char *data = skb->data;
2006 unsigned int num_edescs;
2007 struct frag frags[MAX_FRAGS];
2008 gxio_mpipe_edesc_t edescs[MAX_FRAGS];
2009 unsigned long irqflags;
2010 gxio_mpipe_edesc_t edesc = { { 0 } };
2011 unsigned int i;
2012 s64 slot;
2013
2014 if (skb_is_gso(skb))
2015 return tile_net_tx_tso(skb, dev);
2016
2017 num_edescs = tile_net_tx_frags(frags, skb, data, skb_headlen(skb));
2018
2019 /* This is only used to specify the TLB. */
f3286a3a 2020 edesc.stack_idx = md->first_buffer_stack;
e3d62d7e
CM
2021
2022 /* Prepare the edescs. */
2023 for (i = 0; i < num_edescs; i++) {
2024 edesc.xfer_size = frags[i].length;
2025 edesc.va = va_to_tile_io_addr(frags[i].buf);
2026 edescs[i] = edesc;
2027 }
2028
2029 /* Mark the final edesc. */
2030 edescs[num_edescs - 1].bound = 1;
2031
2032 /* Add checksum info to the initial edesc, if needed. */
2033 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2034 unsigned int csum_start = skb_checksum_start_offset(skb);
2035 edescs[0].csum = 1;
2036 edescs[0].csum_start = csum_start;
2037 edescs[0].csum_dest = csum_start + skb->csum_offset;
2038 }
2039
2040 local_irq_save(irqflags);
2041
2042 /* Try to acquire a completion entry and an egress slot. */
9b4c341b
CM
2043 slot = tile_net_equeue_try_reserve(dev, skb->queue_mapping, comps,
2044 equeue, num_edescs);
e3d62d7e
CM
2045 if (slot < 0) {
2046 local_irq_restore(irqflags);
2047 return NETDEV_TX_BUSY;
2048 }
2049
2050 for (i = 0; i < num_edescs; i++)
2051 gxio_mpipe_equeue_put_at(equeue, edescs[i], slot++);
2052
9ab5ec59
CM
2053 /* Store TX timestamp if needed. */
2054 tile_tx_timestamp(skb, instance);
2055
e3d62d7e
CM
2056 /* Add a completion record. */
2057 add_comp(equeue, comps, slot - 1, skb);
2058
2059 /* NOTE: Use ETH_ZLEN for short packets (e.g. 42 < 60). */
ad018185 2060 tile_net_stats_add(1, &dev->stats.tx_packets);
e3d62d7e 2061 tile_net_stats_add(max_t(unsigned int, len, ETH_ZLEN),
ad018185 2062 &dev->stats.tx_bytes);
e3d62d7e
CM
2063
2064 local_irq_restore(irqflags);
2065
2066 /* Make sure the egress timer is scheduled. */
2067 tile_net_schedule_egress_timer();
2068
2069 return NETDEV_TX_OK;
2070}
2071
2072/* Return subqueue id on this core (one per core). */
f663dd9a 2073static u16 tile_net_select_queue(struct net_device *dev, struct sk_buff *skb,
99932d4f 2074 void *accel_priv, select_queue_fallback_t fallback)
e3d62d7e
CM
2075{
2076 return smp_processor_id();
2077}
2078
2079/* Deal with a transmit timeout. */
2080static void tile_net_tx_timeout(struct net_device *dev)
2081{
2082 int cpu;
2083
2084 for_each_online_cpu(cpu)
2085 netif_wake_subqueue(dev, cpu);
2086}
2087
2088/* Ioctl commands. */
2089static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2090{
9ab5ec59 2091 if (cmd == SIOCSHWTSTAMP)
6ab96d1e
BH
2092 return tile_hwtstamp_set(dev, rq);
2093 if (cmd == SIOCGHWTSTAMP)
2094 return tile_hwtstamp_get(dev, rq);
9ab5ec59 2095
e3d62d7e
CM
2096 return -EOPNOTSUPP;
2097}
2098
e3d62d7e
CM
2099/* Change the MTU. */
2100static int tile_net_change_mtu(struct net_device *dev, int new_mtu)
2101{
2628e8af
CM
2102 if (new_mtu < 68)
2103 return -EINVAL;
2104 if (new_mtu > ((jumbo_num != 0) ? 9000 : 1500))
e3d62d7e
CM
2105 return -EINVAL;
2106 dev->mtu = new_mtu;
2107 return 0;
2108}
2109
2110/* Change the Ethernet address of the NIC.
2111 *
2112 * The hypervisor driver does not support changing MAC address. However,
2113 * the hardware does not do anything with the MAC address, so the address
2114 * which gets used on outgoing packets, and which is accepted on incoming
2115 * packets, is completely up to us.
2116 *
2117 * Returns 0 on success, negative on failure.
2118 */
2119static int tile_net_set_mac_address(struct net_device *dev, void *p)
2120{
2121 struct sockaddr *addr = p;
2122
2123 if (!is_valid_ether_addr(addr->sa_data))
2124 return -EINVAL;
2125 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2126 return 0;
2127}
2128
2129#ifdef CONFIG_NET_POLL_CONTROLLER
2130/* Polling 'interrupt' - used by things like netconsole to send skbs
2131 * without having to re-enable interrupts. It's not called while
2132 * the interrupt routine is executing.
2133 */
2134static void tile_net_netpoll(struct net_device *dev)
2135{
f3286a3a
CM
2136 int instance = mpipe_instance(dev);
2137 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
2138 struct mpipe_data *md = &mpipe_data[instance];
2139
2140 disable_percpu_irq(md->ingress_irq);
2141 napi_schedule(&info->mpipe[instance].napi);
2142 enable_percpu_irq(md->ingress_irq, 0);
e3d62d7e
CM
2143}
2144#endif
2145
2146static const struct net_device_ops tile_net_ops = {
2147 .ndo_open = tile_net_open,
2148 .ndo_stop = tile_net_stop,
2149 .ndo_start_xmit = tile_net_tx,
2150 .ndo_select_queue = tile_net_select_queue,
2151 .ndo_do_ioctl = tile_net_ioctl,
e3d62d7e
CM
2152 .ndo_change_mtu = tile_net_change_mtu,
2153 .ndo_tx_timeout = tile_net_tx_timeout,
2154 .ndo_set_mac_address = tile_net_set_mac_address,
2155#ifdef CONFIG_NET_POLL_CONTROLLER
2156 .ndo_poll_controller = tile_net_netpoll,
2157#endif
2158};
2159
2160/* The setup function.
2161 *
2162 * This uses ether_setup() to assign various fields in dev, including
2163 * setting IFF_BROADCAST and IFF_MULTICAST, then sets some extra fields.
2164 */
2165static void tile_net_setup(struct net_device *dev)
2166{
a8eaed55
CM
2167 netdev_features_t features = 0;
2168
e3d62d7e
CM
2169 ether_setup(dev);
2170 dev->netdev_ops = &tile_net_ops;
2171 dev->watchdog_timeo = TILE_NET_TIMEOUT;
e3d62d7e 2172 dev->mtu = 1500;
a8eaed55 2173
a8eaed55
CM
2174 features |= NETIF_F_HW_CSUM;
2175 features |= NETIF_F_SG;
2176 features |= NETIF_F_TSO;
2c7d04a9 2177 features |= NETIF_F_TSO6;
a8eaed55
CM
2178
2179 dev->hw_features |= features;
2180 dev->vlan_features |= features;
2181 dev->features |= features;
e3d62d7e
CM
2182}
2183
2184/* Allocate the device structure, register the device, and obtain the
2185 * MAC address from the hypervisor.
2186 */
2187static void tile_net_dev_init(const char *name, const uint8_t *mac)
2188{
2189 int ret;
2190 int i;
2191 int nz_addr = 0;
2192 struct net_device *dev;
2193 struct tile_net_priv *priv;
2194
2195 /* HACK: Ignore "loop" links. */
2196 if (strncmp(name, "loop", 4) == 0)
2197 return;
2198
2199 /* Allocate the device structure. Normally, "name" is a
2200 * template, instantiated by register_netdev(), but not for us.
2201 */
2202 dev = alloc_netdev_mqs(sizeof(*priv), name, tile_net_setup,
2203 NR_CPUS, 1);
2204 if (!dev) {
2205 pr_err("alloc_netdev_mqs(%s) failed\n", name);
2206 return;
2207 }
2208
2209 /* Initialize "priv". */
2210 priv = netdev_priv(dev);
2211 memset(priv, 0, sizeof(*priv));
2212 priv->dev = dev;
2213 priv->channel = -1;
2214 priv->loopify_channel = -1;
2215 priv->echannel = -1;
9ab5ec59 2216 init_ptp_dev(priv);
e3d62d7e
CM
2217
2218 /* Get the MAC address and set it in the device struct; this must
2219 * be done before the device is opened. If the MAC is all zeroes,
2220 * we use a random address, since we're probably on the simulator.
2221 */
2222 for (i = 0; i < 6; i++)
2223 nz_addr |= mac[i];
2224
2225 if (nz_addr) {
d458cdf7 2226 memcpy(dev->dev_addr, mac, ETH_ALEN);
e3d62d7e
CM
2227 dev->addr_len = 6;
2228 } else {
c8ab13fb 2229 eth_hw_addr_random(dev);
e3d62d7e
CM
2230 }
2231
2232 /* Register the network device. */
2233 ret = register_netdev(dev);
2234 if (ret) {
2235 netdev_err(dev, "register_netdev failed %d\n", ret);
2236 free_netdev(dev);
2237 return;
2238 }
2239}
2240
2241/* Per-cpu module initialization. */
2242static void tile_net_init_module_percpu(void *unused)
2243{
2244 struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
2245 int my_cpu = smp_processor_id();
f3286a3a 2246 int instance;
e3d62d7e 2247
f3286a3a
CM
2248 for (instance = 0; instance < NR_MPIPE_MAX; instance++) {
2249 info->mpipe[instance].has_iqueue = false;
2250 info->mpipe[instance].instance = instance;
2251 }
e3d62d7e
CM
2252 info->my_cpu = my_cpu;
2253
2254 /* Initialize the egress timer. */
2255 hrtimer_init(&info->egress_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2256 info->egress_timer.function = tile_net_handle_egress_timer;
2257}
2258
2259/* Module initialization. */
2260static int __init tile_net_init_module(void)
2261{
2262 int i;
2263 char name[GXIO_MPIPE_LINK_NAME_LEN];
2264 uint8_t mac[6];
2265
2266 pr_info("Tilera Network Driver\n");
2267
f3286a3a
CM
2268 BUILD_BUG_ON(NR_MPIPE_MAX != 2);
2269
e3d62d7e
CM
2270 mutex_init(&tile_net_devs_for_channel_mutex);
2271
2272 /* Initialize each CPU. */
2273 on_each_cpu(tile_net_init_module_percpu, NULL, 1);
2274
2275 /* Find out what devices we have, and initialize them. */
2276 for (i = 0; gxio_mpipe_link_enumerate_mac(i, name, mac) >= 0; i++)
2277 tile_net_dev_init(name, mac);
2278
2279 if (!network_cpus_init())
2280 network_cpus_map = *cpu_online_mask;
2281
2282 return 0;
2283}
2284
2285module_init(tile_net_init_module);
This page took 0.318745 seconds and 5 git commands to generate.