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