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