linux/arch/um/drivers/vector_kern.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (C) 2017 - 2019 Cambridge Greys Limited
   4 * Copyright (C) 2011 - 2014 Cisco Systems Inc
   5 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
   6 * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
   7 * James Leu (jleu@mindspring.net).
   8 * Copyright (C) 2001 by various other people who didn't put their name here.
   9 */
  10
  11#include <linux/memblock.h>
  12#include <linux/etherdevice.h>
  13#include <linux/ethtool.h>
  14#include <linux/inetdevice.h>
  15#include <linux/init.h>
  16#include <linux/list.h>
  17#include <linux/netdevice.h>
  18#include <linux/platform_device.h>
  19#include <linux/rtnetlink.h>
  20#include <linux/skbuff.h>
  21#include <linux/slab.h>
  22#include <linux/interrupt.h>
  23#include <linux/firmware.h>
  24#include <linux/fs.h>
  25#include <uapi/linux/filter.h>
  26#include <init.h>
  27#include <irq_kern.h>
  28#include <irq_user.h>
  29#include <net_kern.h>
  30#include <os.h>
  31#include "mconsole_kern.h"
  32#include "vector_user.h"
  33#include "vector_kern.h"
  34
  35/*
  36 * Adapted from network devices with the following major changes:
  37 * All transports are static - simplifies the code significantly
  38 * Multiple FDs/IRQs per device
  39 * Vector IO optionally used for read/write, falling back to legacy
  40 * based on configuration and/or availability
  41 * Configuration is no longer positional - L2TPv3 and GRE require up to
  42 * 10 parameters, passing this as positional is not fit for purpose.
  43 * Only socket transports are supported
  44 */
  45
  46
  47#define DRIVER_NAME "uml-vector"
  48struct vector_cmd_line_arg {
  49        struct list_head list;
  50        int unit;
  51        char *arguments;
  52};
  53
  54struct vector_device {
  55        struct list_head list;
  56        struct net_device *dev;
  57        struct platform_device pdev;
  58        int unit;
  59        int opened;
  60};
  61
  62static LIST_HEAD(vec_cmd_line);
  63
  64static DEFINE_SPINLOCK(vector_devices_lock);
  65static LIST_HEAD(vector_devices);
  66
  67static int driver_registered;
  68
  69static void vector_eth_configure(int n, struct arglist *def);
  70
  71/* Argument accessors to set variables (and/or set default values)
  72 * mtu, buffer sizing, default headroom, etc
  73 */
  74
  75#define DEFAULT_HEADROOM 2
  76#define SAFETY_MARGIN 32
  77#define DEFAULT_VECTOR_SIZE 64
  78#define TX_SMALL_PACKET 128
  79#define MAX_IOV_SIZE (MAX_SKB_FRAGS + 1)
  80#define MAX_ITERATIONS 64
  81
  82static const struct {
  83        const char string[ETH_GSTRING_LEN];
  84} ethtool_stats_keys[] = {
  85        { "rx_queue_max" },
  86        { "rx_queue_running_average" },
  87        { "tx_queue_max" },
  88        { "tx_queue_running_average" },
  89        { "rx_encaps_errors" },
  90        { "tx_timeout_count" },
  91        { "tx_restart_queue" },
  92        { "tx_kicks" },
  93        { "tx_flow_control_xon" },
  94        { "tx_flow_control_xoff" },
  95        { "rx_csum_offload_good" },
  96        { "rx_csum_offload_errors"},
  97        { "sg_ok"},
  98        { "sg_linearized"},
  99};
 100
 101#define VECTOR_NUM_STATS        ARRAY_SIZE(ethtool_stats_keys)
 102
 103static void vector_reset_stats(struct vector_private *vp)
 104{
 105        vp->estats.rx_queue_max = 0;
 106        vp->estats.rx_queue_running_average = 0;
 107        vp->estats.tx_queue_max = 0;
 108        vp->estats.tx_queue_running_average = 0;
 109        vp->estats.rx_encaps_errors = 0;
 110        vp->estats.tx_timeout_count = 0;
 111        vp->estats.tx_restart_queue = 0;
 112        vp->estats.tx_kicks = 0;
 113        vp->estats.tx_flow_control_xon = 0;
 114        vp->estats.tx_flow_control_xoff = 0;
 115        vp->estats.sg_ok = 0;
 116        vp->estats.sg_linearized = 0;
 117}
 118
 119static int get_mtu(struct arglist *def)
 120{
 121        char *mtu = uml_vector_fetch_arg(def, "mtu");
 122        long result;
 123
 124        if (mtu != NULL) {
 125                if (kstrtoul(mtu, 10, &result) == 0)
 126                        if ((result < (1 << 16) - 1) && (result >= 576))
 127                                return result;
 128        }
 129        return ETH_MAX_PACKET;
 130}
 131
 132static char *get_bpf_file(struct arglist *def)
 133{
 134        return uml_vector_fetch_arg(def, "bpffile");
 135}
 136
 137static bool get_bpf_flash(struct arglist *def)
 138{
 139        char *allow = uml_vector_fetch_arg(def, "bpfflash");
 140        long result;
 141
 142        if (allow != NULL) {
 143                if (kstrtoul(allow, 10, &result) == 0)
 144                        return (allow > 0);
 145        }
 146        return false;
 147}
 148
 149static int get_depth(struct arglist *def)
 150{
 151        char *mtu = uml_vector_fetch_arg(def, "depth");
 152        long result;
 153
 154        if (mtu != NULL) {
 155                if (kstrtoul(mtu, 10, &result) == 0)
 156                        return result;
 157        }
 158        return DEFAULT_VECTOR_SIZE;
 159}
 160
 161static int get_headroom(struct arglist *def)
 162{
 163        char *mtu = uml_vector_fetch_arg(def, "headroom");
 164        long result;
 165
 166        if (mtu != NULL) {
 167                if (kstrtoul(mtu, 10, &result) == 0)
 168                        return result;
 169        }
 170        return DEFAULT_HEADROOM;
 171}
 172
 173static int get_req_size(struct arglist *def)
 174{
 175        char *gro = uml_vector_fetch_arg(def, "gro");
 176        long result;
 177
 178        if (gro != NULL) {
 179                if (kstrtoul(gro, 10, &result) == 0) {
 180                        if (result > 0)
 181                                return 65536;
 182                }
 183        }
 184        return get_mtu(def) + ETH_HEADER_OTHER +
 185                get_headroom(def) + SAFETY_MARGIN;
 186}
 187
 188
 189static int get_transport_options(struct arglist *def)
 190{
 191        char *transport = uml_vector_fetch_arg(def, "transport");
 192        char *vector = uml_vector_fetch_arg(def, "vec");
 193
 194        int vec_rx = VECTOR_RX;
 195        int vec_tx = VECTOR_TX;
 196        long parsed;
 197        int result = 0;
 198
 199        if (transport == NULL)
 200                return -EINVAL;
 201
 202        if (vector != NULL) {
 203                if (kstrtoul(vector, 10, &parsed) == 0) {
 204                        if (parsed == 0) {
 205                                vec_rx = 0;
 206                                vec_tx = 0;
 207                        }
 208                }
 209        }
 210
 211        if (get_bpf_flash(def))
 212                result = VECTOR_BPF_FLASH;
 213
 214        if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
 215                return result;
 216        if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
 217                return (result | vec_rx | VECTOR_BPF);
 218        if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
 219                return (result | vec_rx | vec_tx | VECTOR_QDISC_BYPASS);
 220        return (result | vec_rx | vec_tx);
 221}
 222
 223
 224/* A mini-buffer for packet drop read
 225 * All of our supported transports are datagram oriented and we always
 226 * read using recvmsg or recvmmsg. If we pass a buffer which is smaller
 227 * than the packet size it still counts as full packet read and will
 228 * clean the incoming stream to keep sigio/epoll happy
 229 */
 230
 231#define DROP_BUFFER_SIZE 32
 232
 233static char *drop_buffer;
 234
 235/* Array backed queues optimized for bulk enqueue/dequeue and
 236 * 1:N (small values of N) or 1:1 enqueuer/dequeuer ratios.
 237 * For more details and full design rationale see
 238 * http://foswiki.cambridgegreys.com/Main/EatYourTailAndEnjoyIt
 239 */
 240
 241
 242/*
 243 * Advance the mmsg queue head by n = advance. Resets the queue to
 244 * maximum enqueue/dequeue-at-once capacity if possible. Called by
 245 * dequeuers. Caller must hold the head_lock!
 246 */
 247
 248static int vector_advancehead(struct vector_queue *qi, int advance)
 249{
 250        int queue_depth;
 251
 252        qi->head =
 253                (qi->head + advance)
 254                        % qi->max_depth;
 255
 256
 257        spin_lock(&qi->tail_lock);
 258        qi->queue_depth -= advance;
 259
 260        /* we are at 0, use this to
 261         * reset head and tail so we can use max size vectors
 262         */
 263
 264        if (qi->queue_depth == 0) {
 265                qi->head = 0;
 266                qi->tail = 0;
 267        }
 268        queue_depth = qi->queue_depth;
 269        spin_unlock(&qi->tail_lock);
 270        return queue_depth;
 271}
 272
 273/*      Advance the queue tail by n = advance.
 274 *      This is called by enqueuers which should hold the
 275 *      head lock already
 276 */
 277
 278static int vector_advancetail(struct vector_queue *qi, int advance)
 279{
 280        int queue_depth;
 281
 282        qi->tail =
 283                (qi->tail + advance)
 284                        % qi->max_depth;
 285        spin_lock(&qi->head_lock);
 286        qi->queue_depth += advance;
 287        queue_depth = qi->queue_depth;
 288        spin_unlock(&qi->head_lock);
 289        return queue_depth;
 290}
 291
 292static int prep_msg(struct vector_private *vp,
 293        struct sk_buff *skb,
 294        struct iovec *iov)
 295{
 296        int iov_index = 0;
 297        int nr_frags, frag;
 298        skb_frag_t *skb_frag;
 299
 300        nr_frags = skb_shinfo(skb)->nr_frags;
 301        if (nr_frags > MAX_IOV_SIZE) {
 302                if (skb_linearize(skb) != 0)
 303                        goto drop;
 304        }
 305        if (vp->header_size > 0) {
 306                iov[iov_index].iov_len = vp->header_size;
 307                vp->form_header(iov[iov_index].iov_base, skb, vp);
 308                iov_index++;
 309        }
 310        iov[iov_index].iov_base = skb->data;
 311        if (nr_frags > 0) {
 312                iov[iov_index].iov_len = skb->len - skb->data_len;
 313                vp->estats.sg_ok++;
 314        } else
 315                iov[iov_index].iov_len = skb->len;
 316        iov_index++;
 317        for (frag = 0; frag < nr_frags; frag++) {
 318                skb_frag = &skb_shinfo(skb)->frags[frag];
 319                iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
 320                iov[iov_index].iov_len = skb_frag_size(skb_frag);
 321                iov_index++;
 322        }
 323        return iov_index;
 324drop:
 325        return -1;
 326}
 327/*
 328 * Generic vector enqueue with support for forming headers using transport
 329 * specific callback. Allows GRE, L2TPv3, RAW and other transports
 330 * to use a common enqueue procedure in vector mode
 331 */
 332
 333static int vector_enqueue(struct vector_queue *qi, struct sk_buff *skb)
 334{
 335        struct vector_private *vp = netdev_priv(qi->dev);
 336        int queue_depth;
 337        int packet_len;
 338        struct mmsghdr *mmsg_vector = qi->mmsg_vector;
 339        int iov_count;
 340
 341        spin_lock(&qi->tail_lock);
 342        spin_lock(&qi->head_lock);
 343        queue_depth = qi->queue_depth;
 344        spin_unlock(&qi->head_lock);
 345
 346        if (skb)
 347                packet_len = skb->len;
 348
 349        if (queue_depth < qi->max_depth) {
 350
 351                *(qi->skbuff_vector + qi->tail) = skb;
 352                mmsg_vector += qi->tail;
 353                iov_count = prep_msg(
 354                        vp,
 355                        skb,
 356                        mmsg_vector->msg_hdr.msg_iov
 357                );
 358                if (iov_count < 1)
 359                        goto drop;
 360                mmsg_vector->msg_hdr.msg_iovlen = iov_count;
 361                mmsg_vector->msg_hdr.msg_name = vp->fds->remote_addr;
 362                mmsg_vector->msg_hdr.msg_namelen = vp->fds->remote_addr_size;
 363                queue_depth = vector_advancetail(qi, 1);
 364        } else
 365                goto drop;
 366        spin_unlock(&qi->tail_lock);
 367        return queue_depth;
 368drop:
 369        qi->dev->stats.tx_dropped++;
 370        if (skb != NULL) {
 371                packet_len = skb->len;
 372                dev_consume_skb_any(skb);
 373                netdev_completed_queue(qi->dev, 1, packet_len);
 374        }
 375        spin_unlock(&qi->tail_lock);
 376        return queue_depth;
 377}
 378
 379static int consume_vector_skbs(struct vector_queue *qi, int count)
 380{
 381        struct sk_buff *skb;
 382        int skb_index;
 383        int bytes_compl = 0;
 384
 385        for (skb_index = qi->head; skb_index < qi->head + count; skb_index++) {
 386                skb = *(qi->skbuff_vector + skb_index);
 387                /* mark as empty to ensure correct destruction if
 388                 * needed
 389                 */
 390                bytes_compl += skb->len;
 391                *(qi->skbuff_vector + skb_index) = NULL;
 392                dev_consume_skb_any(skb);
 393        }
 394        qi->dev->stats.tx_bytes += bytes_compl;
 395        qi->dev->stats.tx_packets += count;
 396        netdev_completed_queue(qi->dev, count, bytes_compl);
 397        return vector_advancehead(qi, count);
 398}
 399
 400/*
 401 * Generic vector deque via sendmmsg with support for forming headers
 402 * using transport specific callback. Allows GRE, L2TPv3, RAW and
 403 * other transports to use a common dequeue procedure in vector mode
 404 */
 405
 406
 407static int vector_send(struct vector_queue *qi)
 408{
 409        struct vector_private *vp = netdev_priv(qi->dev);
 410        struct mmsghdr *send_from;
 411        int result = 0, send_len, queue_depth = qi->max_depth;
 412
 413        if (spin_trylock(&qi->head_lock)) {
 414                if (spin_trylock(&qi->tail_lock)) {
 415                        /* update queue_depth to current value */
 416                        queue_depth = qi->queue_depth;
 417                        spin_unlock(&qi->tail_lock);
 418                        while (queue_depth > 0) {
 419                                /* Calculate the start of the vector */
 420                                send_len = queue_depth;
 421                                send_from = qi->mmsg_vector;
 422                                send_from += qi->head;
 423                                /* Adjust vector size if wraparound */
 424                                if (send_len + qi->head > qi->max_depth)
 425                                        send_len = qi->max_depth - qi->head;
 426                                /* Try to TX as many packets as possible */
 427                                if (send_len > 0) {
 428                                        result = uml_vector_sendmmsg(
 429                                                 vp->fds->tx_fd,
 430                                                 send_from,
 431                                                 send_len,
 432                                                 0
 433                                        );
 434                                        vp->in_write_poll =
 435                                                (result != send_len);
 436                                }
 437                                /* For some of the sendmmsg error scenarios
 438                                 * we may end being unsure in the TX success
 439                                 * for all packets. It is safer to declare
 440                                 * them all TX-ed and blame the network.
 441                                 */
 442                                if (result < 0) {
 443                                        if (net_ratelimit())
 444                                                netdev_err(vp->dev, "sendmmsg err=%i\n",
 445                                                        result);
 446                                        vp->in_error = true;
 447                                        result = send_len;
 448                                }
 449                                if (result > 0) {
 450                                        queue_depth =
 451                                                consume_vector_skbs(qi, result);
 452                                        /* This is equivalent to an TX IRQ.
 453                                         * Restart the upper layers to feed us
 454                                         * more packets.
 455                                         */
 456                                        if (result > vp->estats.tx_queue_max)
 457                                                vp->estats.tx_queue_max = result;
 458                                        vp->estats.tx_queue_running_average =
 459                                                (vp->estats.tx_queue_running_average + result) >> 1;
 460                                }
 461                                netif_trans_update(qi->dev);
 462                                netif_wake_queue(qi->dev);
 463                                /* if TX is busy, break out of the send loop,
 464                                 *  poll write IRQ will reschedule xmit for us
 465                                 */
 466                                if (result != send_len) {
 467                                        vp->estats.tx_restart_queue++;
 468                                        break;
 469                                }
 470                        }
 471                }
 472                spin_unlock(&qi->head_lock);
 473        } else {
 474                tasklet_schedule(&vp->tx_poll);
 475        }
 476        return queue_depth;
 477}
 478
 479/* Queue destructor. Deliberately stateless so we can use
 480 * it in queue cleanup if initialization fails.
 481 */
 482
 483static void destroy_queue(struct vector_queue *qi)
 484{
 485        int i;
 486        struct iovec *iov;
 487        struct vector_private *vp = netdev_priv(qi->dev);
 488        struct mmsghdr *mmsg_vector;
 489
 490        if (qi == NULL)
 491                return;
 492        /* deallocate any skbuffs - we rely on any unused to be
 493         * set to NULL.
 494         */
 495        if (qi->skbuff_vector != NULL) {
 496                for (i = 0; i < qi->max_depth; i++) {
 497                        if (*(qi->skbuff_vector + i) != NULL)
 498                                dev_kfree_skb_any(*(qi->skbuff_vector + i));
 499                }
 500                kfree(qi->skbuff_vector);
 501        }
 502        /* deallocate matching IOV structures including header buffs */
 503        if (qi->mmsg_vector != NULL) {
 504                mmsg_vector = qi->mmsg_vector;
 505                for (i = 0; i < qi->max_depth; i++) {
 506                        iov = mmsg_vector->msg_hdr.msg_iov;
 507                        if (iov != NULL) {
 508                                if ((vp->header_size > 0) &&
 509                                        (iov->iov_base != NULL))
 510                                        kfree(iov->iov_base);
 511                                kfree(iov);
 512                        }
 513                        mmsg_vector++;
 514                }
 515                kfree(qi->mmsg_vector);
 516        }
 517        kfree(qi);
 518}
 519
 520/*
 521 * Queue constructor. Create a queue with a given side.
 522 */
 523static struct vector_queue *create_queue(
 524        struct vector_private *vp,
 525        int max_size,
 526        int header_size,
 527        int num_extra_frags)
 528{
 529        struct vector_queue *result;
 530        int i;
 531        struct iovec *iov;
 532        struct mmsghdr *mmsg_vector;
 533
 534        result = kmalloc(sizeof(struct vector_queue), GFP_KERNEL);
 535        if (result == NULL)
 536                return NULL;
 537        result->max_depth = max_size;
 538        result->dev = vp->dev;
 539        result->mmsg_vector = kmalloc(
 540                (sizeof(struct mmsghdr) * max_size), GFP_KERNEL);
 541        if (result->mmsg_vector == NULL)
 542                goto out_mmsg_fail;
 543        result->skbuff_vector = kmalloc(
 544                (sizeof(void *) * max_size), GFP_KERNEL);
 545        if (result->skbuff_vector == NULL)
 546                goto out_skb_fail;
 547
 548        /* further failures can be handled safely by destroy_queue*/
 549
 550        mmsg_vector = result->mmsg_vector;
 551        for (i = 0; i < max_size; i++) {
 552                /* Clear all pointers - we use non-NULL as marking on
 553                 * what to free on destruction
 554                 */
 555                *(result->skbuff_vector + i) = NULL;
 556                mmsg_vector->msg_hdr.msg_iov = NULL;
 557                mmsg_vector++;
 558        }
 559        mmsg_vector = result->mmsg_vector;
 560        result->max_iov_frags = num_extra_frags;
 561        for (i = 0; i < max_size; i++) {
 562                if (vp->header_size > 0)
 563                        iov = kmalloc_array(3 + num_extra_frags,
 564                                            sizeof(struct iovec),
 565                                            GFP_KERNEL
 566                        );
 567                else
 568                        iov = kmalloc_array(2 + num_extra_frags,
 569                                            sizeof(struct iovec),
 570                                            GFP_KERNEL
 571                        );
 572                if (iov == NULL)
 573                        goto out_fail;
 574                mmsg_vector->msg_hdr.msg_iov = iov;
 575                mmsg_vector->msg_hdr.msg_iovlen = 1;
 576                mmsg_vector->msg_hdr.msg_control = NULL;
 577                mmsg_vector->msg_hdr.msg_controllen = 0;
 578                mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT;
 579                mmsg_vector->msg_hdr.msg_name = NULL;
 580                mmsg_vector->msg_hdr.msg_namelen = 0;
 581                if (vp->header_size > 0) {
 582                        iov->iov_base = kmalloc(header_size, GFP_KERNEL);
 583                        if (iov->iov_base == NULL)
 584                                goto out_fail;
 585                        iov->iov_len = header_size;
 586                        mmsg_vector->msg_hdr.msg_iovlen = 2;
 587                        iov++;
 588                }
 589                iov->iov_base = NULL;
 590                iov->iov_len = 0;
 591                mmsg_vector++;
 592        }
 593        spin_lock_init(&result->head_lock);
 594        spin_lock_init(&result->tail_lock);
 595        result->queue_depth = 0;
 596        result->head = 0;
 597        result->tail = 0;
 598        return result;
 599out_skb_fail:
 600        kfree(result->mmsg_vector);
 601out_mmsg_fail:
 602        kfree(result);
 603        return NULL;
 604out_fail:
 605        destroy_queue(result);
 606        return NULL;
 607}
 608
 609/*
 610 * We do not use the RX queue as a proper wraparound queue for now
 611 * This is not necessary because the consumption via netif_rx()
 612 * happens in-line. While we can try using the return code of
 613 * netif_rx() for flow control there are no drivers doing this today.
 614 * For this RX specific use we ignore the tail/head locks and
 615 * just read into a prepared queue filled with skbuffs.
 616 */
 617
 618static struct sk_buff *prep_skb(
 619        struct vector_private *vp,
 620        struct user_msghdr *msg)
 621{
 622        int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN;
 623        struct sk_buff *result;
 624        int iov_index = 0, len;
 625        struct iovec *iov = msg->msg_iov;
 626        int err, nr_frags, frag;
 627        skb_frag_t *skb_frag;
 628
 629        if (vp->req_size <= linear)
 630                len = linear;
 631        else
 632                len = vp->req_size;
 633        result = alloc_skb_with_frags(
 634                linear,
 635                len - vp->max_packet,
 636                3,
 637                &err,
 638                GFP_ATOMIC
 639        );
 640        if (vp->header_size > 0)
 641                iov_index++;
 642        if (result == NULL) {
 643                iov[iov_index].iov_base = NULL;
 644                iov[iov_index].iov_len = 0;
 645                goto done;
 646        }
 647        skb_reserve(result, vp->headroom);
 648        result->dev = vp->dev;
 649        skb_put(result, vp->max_packet);
 650        result->data_len = len - vp->max_packet;
 651        result->len += len - vp->max_packet;
 652        skb_reset_mac_header(result);
 653        result->ip_summed = CHECKSUM_NONE;
 654        iov[iov_index].iov_base = result->data;
 655        iov[iov_index].iov_len = vp->max_packet;
 656        iov_index++;
 657
 658        nr_frags = skb_shinfo(result)->nr_frags;
 659        for (frag = 0; frag < nr_frags; frag++) {
 660                skb_frag = &skb_shinfo(result)->frags[frag];
 661                iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
 662                if (iov[iov_index].iov_base != NULL)
 663                        iov[iov_index].iov_len = skb_frag_size(skb_frag);
 664                else
 665                        iov[iov_index].iov_len = 0;
 666                iov_index++;
 667        }
 668done:
 669        msg->msg_iovlen = iov_index;
 670        return result;
 671}
 672
 673
 674/* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs*/
 675
 676static void prep_queue_for_rx(struct vector_queue *qi)
 677{
 678        struct vector_private *vp = netdev_priv(qi->dev);
 679        struct mmsghdr *mmsg_vector = qi->mmsg_vector;
 680        void **skbuff_vector = qi->skbuff_vector;
 681        int i;
 682
 683        if (qi->queue_depth == 0)
 684                return;
 685        for (i = 0; i < qi->queue_depth; i++) {
 686                /* it is OK if allocation fails - recvmmsg with NULL data in
 687                 * iov argument still performs an RX, just drops the packet
 688                 * This allows us stop faffing around with a "drop buffer"
 689                 */
 690
 691                *skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr);
 692                skbuff_vector++;
 693                mmsg_vector++;
 694        }
 695        qi->queue_depth = 0;
 696}
 697
 698static struct vector_device *find_device(int n)
 699{
 700        struct vector_device *device;
 701        struct list_head *ele;
 702
 703        spin_lock(&vector_devices_lock);
 704        list_for_each(ele, &vector_devices) {
 705                device = list_entry(ele, struct vector_device, list);
 706                if (device->unit == n)
 707                        goto out;
 708        }
 709        device = NULL;
 710 out:
 711        spin_unlock(&vector_devices_lock);
 712        return device;
 713}
 714
 715static int vector_parse(char *str, int *index_out, char **str_out,
 716                        char **error_out)
 717{
 718        int n, len, err;
 719        char *start = str;
 720
 721        len = strlen(str);
 722
 723        while ((*str != ':') && (strlen(str) > 1))
 724                str++;
 725        if (*str != ':') {
 726                *error_out = "Expected ':' after device number";
 727                return -EINVAL;
 728        }
 729        *str = '\0';
 730
 731        err = kstrtouint(start, 0, &n);
 732        if (err < 0) {
 733                *error_out = "Bad device number";
 734                return err;
 735        }
 736
 737        str++;
 738        if (find_device(n)) {
 739                *error_out = "Device already configured";
 740                return -EINVAL;
 741        }
 742
 743        *index_out = n;
 744        *str_out = str;
 745        return 0;
 746}
 747
 748static int vector_config(char *str, char **error_out)
 749{
 750        int err, n;
 751        char *params;
 752        struct arglist *parsed;
 753
 754        err = vector_parse(str, &n, &params, error_out);
 755        if (err != 0)
 756                return err;
 757
 758        /* This string is broken up and the pieces used by the underlying
 759         * driver. We should copy it to make sure things do not go wrong
 760         * later.
 761         */
 762
 763        params = kstrdup(params, GFP_KERNEL);
 764        if (params == NULL) {
 765                *error_out = "vector_config failed to strdup string";
 766                return -ENOMEM;
 767        }
 768
 769        parsed = uml_parse_vector_ifspec(params);
 770
 771        if (parsed == NULL) {
 772                *error_out = "vector_config failed to parse parameters";
 773                return -EINVAL;
 774        }
 775
 776        vector_eth_configure(n, parsed);
 777        return 0;
 778}
 779
 780static int vector_id(char **str, int *start_out, int *end_out)
 781{
 782        char *end;
 783        int n;
 784
 785        n = simple_strtoul(*str, &end, 0);
 786        if ((*end != '\0') || (end == *str))
 787                return -1;
 788
 789        *start_out = n;
 790        *end_out = n;
 791        *str = end;
 792        return n;
 793}
 794
 795static int vector_remove(int n, char **error_out)
 796{
 797        struct vector_device *vec_d;
 798        struct net_device *dev;
 799        struct vector_private *vp;
 800
 801        vec_d = find_device(n);
 802        if (vec_d == NULL)
 803                return -ENODEV;
 804        dev = vec_d->dev;
 805        vp = netdev_priv(dev);
 806        if (vp->fds != NULL)
 807                return -EBUSY;
 808        unregister_netdev(dev);
 809        platform_device_unregister(&vec_d->pdev);
 810        return 0;
 811}
 812
 813/*
 814 * There is no shared per-transport initialization code, so
 815 * we will just initialize each interface one by one and
 816 * add them to a list
 817 */
 818
 819static struct platform_driver uml_net_driver = {
 820        .driver = {
 821                .name = DRIVER_NAME,
 822        },
 823};
 824
 825
 826static void vector_device_release(struct device *dev)
 827{
 828        struct vector_device *device = dev_get_drvdata(dev);
 829        struct net_device *netdev = device->dev;
 830
 831        list_del(&device->list);
 832        kfree(device);
 833        free_netdev(netdev);
 834}
 835
 836/* Bog standard recv using recvmsg - not used normally unless the user
 837 * explicitly specifies not to use recvmmsg vector RX.
 838 */
 839
 840static int vector_legacy_rx(struct vector_private *vp)
 841{
 842        int pkt_len;
 843        struct user_msghdr hdr;
 844        struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */
 845        int iovpos = 0;
 846        struct sk_buff *skb;
 847        int header_check;
 848
 849        hdr.msg_name = NULL;
 850        hdr.msg_namelen = 0;
 851        hdr.msg_iov = (struct iovec *) &iov;
 852        hdr.msg_control = NULL;
 853        hdr.msg_controllen = 0;
 854        hdr.msg_flags = 0;
 855
 856        if (vp->header_size > 0) {
 857                iov[0].iov_base = vp->header_rxbuffer;
 858                iov[0].iov_len = vp->header_size;
 859        }
 860
 861        skb = prep_skb(vp, &hdr);
 862
 863        if (skb == NULL) {
 864                /* Read a packet into drop_buffer and don't do
 865                 * anything with it.
 866                 */
 867                iov[iovpos].iov_base = drop_buffer;
 868                iov[iovpos].iov_len = DROP_BUFFER_SIZE;
 869                hdr.msg_iovlen = 1;
 870                vp->dev->stats.rx_dropped++;
 871        }
 872
 873        pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0);
 874        if (pkt_len < 0) {
 875                vp->in_error = true;
 876                return pkt_len;
 877        }
 878
 879        if (skb != NULL) {
 880                if (pkt_len > vp->header_size) {
 881                        if (vp->header_size > 0) {
 882                                header_check = vp->verify_header(
 883                                        vp->header_rxbuffer, skb, vp);
 884                                if (header_check < 0) {
 885                                        dev_kfree_skb_irq(skb);
 886                                        vp->dev->stats.rx_dropped++;
 887                                        vp->estats.rx_encaps_errors++;
 888                                        return 0;
 889                                }
 890                                if (header_check > 0) {
 891                                        vp->estats.rx_csum_offload_good++;
 892                                        skb->ip_summed = CHECKSUM_UNNECESSARY;
 893                                }
 894                        }
 895                        pskb_trim(skb, pkt_len - vp->rx_header_size);
 896                        skb->protocol = eth_type_trans(skb, skb->dev);
 897                        vp->dev->stats.rx_bytes += skb->len;
 898                        vp->dev->stats.rx_packets++;
 899                        netif_rx(skb);
 900                } else {
 901                        dev_kfree_skb_irq(skb);
 902                }
 903        }
 904        return pkt_len;
 905}
 906
 907/*
 908 * Packet at a time TX which falls back to vector TX if the
 909 * underlying transport is busy.
 910 */
 911
 912
 913
 914static int writev_tx(struct vector_private *vp, struct sk_buff *skb)
 915{
 916        struct iovec iov[3 + MAX_IOV_SIZE];
 917        int iov_count, pkt_len = 0;
 918
 919        iov[0].iov_base = vp->header_txbuffer;
 920        iov_count = prep_msg(vp, skb, (struct iovec *) &iov);
 921
 922        if (iov_count < 1)
 923                goto drop;
 924
 925        pkt_len = uml_vector_writev(
 926                vp->fds->tx_fd,
 927                (struct iovec *) &iov,
 928                iov_count
 929        );
 930
 931        if (pkt_len < 0)
 932                goto drop;
 933
 934        netif_trans_update(vp->dev);
 935        netif_wake_queue(vp->dev);
 936
 937        if (pkt_len > 0) {
 938                vp->dev->stats.tx_bytes += skb->len;
 939                vp->dev->stats.tx_packets++;
 940        } else {
 941                vp->dev->stats.tx_dropped++;
 942        }
 943        consume_skb(skb);
 944        return pkt_len;
 945drop:
 946        vp->dev->stats.tx_dropped++;
 947        consume_skb(skb);
 948        if (pkt_len < 0)
 949                vp->in_error = true;
 950        return pkt_len;
 951}
 952
 953/*
 954 * Receive as many messages as we can in one call using the special
 955 * mmsg vector matched to an skb vector which we prepared earlier.
 956 */
 957
 958static int vector_mmsg_rx(struct vector_private *vp)
 959{
 960        int packet_count, i;
 961        struct vector_queue *qi = vp->rx_queue;
 962        struct sk_buff *skb;
 963        struct mmsghdr *mmsg_vector = qi->mmsg_vector;
 964        void **skbuff_vector = qi->skbuff_vector;
 965        int header_check;
 966
 967        /* Refresh the vector and make sure it is with new skbs and the
 968         * iovs are updated to point to them.
 969         */
 970
 971        prep_queue_for_rx(qi);
 972
 973        /* Fire the Lazy Gun - get as many packets as we can in one go. */
 974
 975        packet_count = uml_vector_recvmmsg(
 976                vp->fds->rx_fd, qi->mmsg_vector, qi->max_depth, 0);
 977
 978        if (packet_count < 0)
 979                vp->in_error = true;
 980
 981        if (packet_count <= 0)
 982                return packet_count;
 983
 984        /* We treat packet processing as enqueue, buffer refresh as dequeue
 985         * The queue_depth tells us how many buffers have been used and how
 986         * many do we need to prep the next time prep_queue_for_rx() is called.
 987         */
 988
 989        qi->queue_depth = packet_count;
 990
 991        for (i = 0; i < packet_count; i++) {
 992                skb = (*skbuff_vector);
 993                if (mmsg_vector->msg_len > vp->header_size) {
 994                        if (vp->header_size > 0) {
 995                                header_check = vp->verify_header(
 996                                        mmsg_vector->msg_hdr.msg_iov->iov_base,
 997                                        skb,
 998                                        vp
 999                                );
1000                                if (header_check < 0) {
1001                                /* Overlay header failed to verify - discard.
1002                                 * We can actually keep this skb and reuse it,
1003                                 * but that will make the prep logic too
1004                                 * complex.
1005                                 */
1006                                        dev_kfree_skb_irq(skb);
1007                                        vp->estats.rx_encaps_errors++;
1008                                        continue;
1009                                }
1010                                if (header_check > 0) {
1011                                        vp->estats.rx_csum_offload_good++;
1012                                        skb->ip_summed = CHECKSUM_UNNECESSARY;
1013                                }
1014                        }
1015                        pskb_trim(skb,
1016                                mmsg_vector->msg_len - vp->rx_header_size);
1017                        skb->protocol = eth_type_trans(skb, skb->dev);
1018                        /*
1019                         * We do not need to lock on updating stats here
1020                         * The interrupt loop is non-reentrant.
1021                         */
1022                        vp->dev->stats.rx_bytes += skb->len;
1023                        vp->dev->stats.rx_packets++;
1024                        netif_rx(skb);
1025                } else {
1026                        /* Overlay header too short to do anything - discard.
1027                         * We can actually keep this skb and reuse it,
1028                         * but that will make the prep logic too complex.
1029                         */
1030                        if (skb != NULL)
1031                                dev_kfree_skb_irq(skb);
1032                }
1033                (*skbuff_vector) = NULL;
1034                /* Move to the next buffer element */
1035                mmsg_vector++;
1036                skbuff_vector++;
1037        }
1038        if (packet_count > 0) {
1039                if (vp->estats.rx_queue_max < packet_count)
1040                        vp->estats.rx_queue_max = packet_count;
1041                vp->estats.rx_queue_running_average =
1042                        (vp->estats.rx_queue_running_average + packet_count) >> 1;
1043        }
1044        return packet_count;
1045}
1046
1047static void vector_rx(struct vector_private *vp)
1048{
1049        int err;
1050        int iter = 0;
1051
1052        if ((vp->options & VECTOR_RX) > 0)
1053                while (((err = vector_mmsg_rx(vp)) > 0) && (iter < MAX_ITERATIONS))
1054                        iter++;
1055        else
1056                while (((err = vector_legacy_rx(vp)) > 0) && (iter < MAX_ITERATIONS))
1057                        iter++;
1058        if ((err != 0) && net_ratelimit())
1059                netdev_err(vp->dev, "vector_rx: error(%d)\n", err);
1060        if (iter == MAX_ITERATIONS)
1061                netdev_err(vp->dev, "vector_rx: device stuck, remote end may have closed the connection\n");
1062}
1063
1064static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
1065{
1066        struct vector_private *vp = netdev_priv(dev);
1067        int queue_depth = 0;
1068
1069        if (vp->in_error) {
1070                deactivate_fd(vp->fds->rx_fd, vp->rx_irq);
1071                if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0))
1072                        deactivate_fd(vp->fds->tx_fd, vp->tx_irq);
1073                return NETDEV_TX_BUSY;
1074        }
1075
1076        if ((vp->options & VECTOR_TX) == 0) {
1077                writev_tx(vp, skb);
1078                return NETDEV_TX_OK;
1079        }
1080
1081        /* We do BQL only in the vector path, no point doing it in
1082         * packet at a time mode as there is no device queue
1083         */
1084
1085        netdev_sent_queue(vp->dev, skb->len);
1086        queue_depth = vector_enqueue(vp->tx_queue, skb);
1087
1088        /* if the device queue is full, stop the upper layers and
1089         * flush it.
1090         */
1091
1092        if (queue_depth >= vp->tx_queue->max_depth - 1) {
1093                vp->estats.tx_kicks++;
1094                netif_stop_queue(dev);
1095                vector_send(vp->tx_queue);
1096                return NETDEV_TX_OK;
1097        }
1098        if (netdev_xmit_more()) {
1099                mod_timer(&vp->tl, vp->coalesce);
1100                return NETDEV_TX_OK;
1101        }
1102        if (skb->len < TX_SMALL_PACKET) {
1103                vp->estats.tx_kicks++;
1104                vector_send(vp->tx_queue);
1105        } else
1106                tasklet_schedule(&vp->tx_poll);
1107        return NETDEV_TX_OK;
1108}
1109
1110static irqreturn_t vector_rx_interrupt(int irq, void *dev_id)
1111{
1112        struct net_device *dev = dev_id;
1113        struct vector_private *vp = netdev_priv(dev);
1114
1115        if (!netif_running(dev))
1116                return IRQ_NONE;
1117        vector_rx(vp);
1118        return IRQ_HANDLED;
1119
1120}
1121
1122static irqreturn_t vector_tx_interrupt(int irq, void *dev_id)
1123{
1124        struct net_device *dev = dev_id;
1125        struct vector_private *vp = netdev_priv(dev);
1126
1127        if (!netif_running(dev))
1128                return IRQ_NONE;
1129        /* We need to pay attention to it only if we got
1130         * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise
1131         * we ignore it. In the future, it may be worth
1132         * it to improve the IRQ controller a bit to make
1133         * tweaking the IRQ mask less costly
1134         */
1135
1136        if (vp->in_write_poll)
1137                tasklet_schedule(&vp->tx_poll);
1138        return IRQ_HANDLED;
1139
1140}
1141
1142static int irq_rr;
1143
1144static int vector_net_close(struct net_device *dev)
1145{
1146        struct vector_private *vp = netdev_priv(dev);
1147        unsigned long flags;
1148
1149        netif_stop_queue(dev);
1150        del_timer(&vp->tl);
1151
1152        if (vp->fds == NULL)
1153                return 0;
1154
1155        /* Disable and free all IRQS */
1156        if (vp->rx_irq > 0) {
1157                um_free_irq(vp->rx_irq, dev);
1158                vp->rx_irq = 0;
1159        }
1160        if (vp->tx_irq > 0) {
1161                um_free_irq(vp->tx_irq, dev);
1162                vp->tx_irq = 0;
1163        }
1164        tasklet_kill(&vp->tx_poll);
1165        if (vp->fds->rx_fd > 0) {
1166                if (vp->bpf)
1167                        uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
1168                os_close_file(vp->fds->rx_fd);
1169                vp->fds->rx_fd = -1;
1170        }
1171        if (vp->fds->tx_fd > 0) {
1172                os_close_file(vp->fds->tx_fd);
1173                vp->fds->tx_fd = -1;
1174        }
1175        if (vp->bpf != NULL)
1176                kfree(vp->bpf->filter);
1177        kfree(vp->bpf);
1178        vp->bpf = NULL;
1179        kfree(vp->fds->remote_addr);
1180        kfree(vp->transport_data);
1181        kfree(vp->header_rxbuffer);
1182        kfree(vp->header_txbuffer);
1183        if (vp->rx_queue != NULL)
1184                destroy_queue(vp->rx_queue);
1185        if (vp->tx_queue != NULL)
1186                destroy_queue(vp->tx_queue);
1187        kfree(vp->fds);
1188        vp->fds = NULL;
1189        spin_lock_irqsave(&vp->lock, flags);
1190        vp->opened = false;
1191        vp->in_error = false;
1192        spin_unlock_irqrestore(&vp->lock, flags);
1193        return 0;
1194}
1195
1196/* TX tasklet */
1197
1198static void vector_tx_poll(struct tasklet_struct *t)
1199{
1200        struct vector_private *vp = from_tasklet(vp, t, tx_poll);
1201
1202        vp->estats.tx_kicks++;
1203        vector_send(vp->tx_queue);
1204}
1205static void vector_reset_tx(struct work_struct *work)
1206{
1207        struct vector_private *vp =
1208                container_of(work, struct vector_private, reset_tx);
1209        netdev_reset_queue(vp->dev);
1210        netif_start_queue(vp->dev);
1211        netif_wake_queue(vp->dev);
1212}
1213
1214static int vector_net_open(struct net_device *dev)
1215{
1216        struct vector_private *vp = netdev_priv(dev);
1217        unsigned long flags;
1218        int err = -EINVAL;
1219        struct vector_device *vdevice;
1220
1221        spin_lock_irqsave(&vp->lock, flags);
1222        if (vp->opened) {
1223                spin_unlock_irqrestore(&vp->lock, flags);
1224                return -ENXIO;
1225        }
1226        vp->opened = true;
1227        spin_unlock_irqrestore(&vp->lock, flags);
1228
1229        vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed));
1230
1231        vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
1232
1233        if (vp->fds == NULL)
1234                goto out_close;
1235
1236        if (build_transport_data(vp) < 0)
1237                goto out_close;
1238
1239        if ((vp->options & VECTOR_RX) > 0) {
1240                vp->rx_queue = create_queue(
1241                        vp,
1242                        get_depth(vp->parsed),
1243                        vp->rx_header_size,
1244                        MAX_IOV_SIZE
1245                );
1246                vp->rx_queue->queue_depth = get_depth(vp->parsed);
1247        } else {
1248                vp->header_rxbuffer = kmalloc(
1249                        vp->rx_header_size,
1250                        GFP_KERNEL
1251                );
1252                if (vp->header_rxbuffer == NULL)
1253                        goto out_close;
1254        }
1255        if ((vp->options & VECTOR_TX) > 0) {
1256                vp->tx_queue = create_queue(
1257                        vp,
1258                        get_depth(vp->parsed),
1259                        vp->header_size,
1260                        MAX_IOV_SIZE
1261                );
1262        } else {
1263                vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL);
1264                if (vp->header_txbuffer == NULL)
1265                        goto out_close;
1266        }
1267
1268        /* READ IRQ */
1269        err = um_request_irq(
1270                irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd,
1271                        IRQ_READ, vector_rx_interrupt,
1272                        IRQF_SHARED, dev->name, dev);
1273        if (err < 0) {
1274                netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err);
1275                err = -ENETUNREACH;
1276                goto out_close;
1277        }
1278        vp->rx_irq = irq_rr + VECTOR_BASE_IRQ;
1279        dev->irq = irq_rr + VECTOR_BASE_IRQ;
1280        irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
1281
1282        /* WRITE IRQ - we need it only if we have vector TX */
1283        if ((vp->options & VECTOR_TX) > 0) {
1284                err = um_request_irq(
1285                        irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd,
1286                                IRQ_WRITE, vector_tx_interrupt,
1287                                IRQF_SHARED, dev->name, dev);
1288                if (err < 0) {
1289                        netdev_err(dev,
1290                                "vector_open: failed to get tx irq(%d)\n", err);
1291                        err = -ENETUNREACH;
1292                        goto out_close;
1293                }
1294                vp->tx_irq = irq_rr + VECTOR_BASE_IRQ;
1295                irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
1296        }
1297
1298        if ((vp->options & VECTOR_QDISC_BYPASS) != 0) {
1299                if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
1300                        vp->options |= VECTOR_BPF;
1301        }
1302        if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL))
1303                vp->bpf = uml_vector_default_bpf(dev->dev_addr);
1304
1305        if (vp->bpf != NULL)
1306                uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
1307
1308        netif_start_queue(dev);
1309
1310        /* clear buffer - it can happen that the host side of the interface
1311         * is full when we get here. In this case, new data is never queued,
1312         * SIGIOs never arrive, and the net never works.
1313         */
1314
1315        vector_rx(vp);
1316
1317        vector_reset_stats(vp);
1318        vdevice = find_device(vp->unit);
1319        vdevice->opened = 1;
1320
1321        if ((vp->options & VECTOR_TX) != 0)
1322                add_timer(&vp->tl);
1323        return 0;
1324out_close:
1325        vector_net_close(dev);
1326        return err;
1327}
1328
1329
1330static void vector_net_set_multicast_list(struct net_device *dev)
1331{
1332        /* TODO: - we can do some BPF games here */
1333        return;
1334}
1335
1336static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
1337{
1338        struct vector_private *vp = netdev_priv(dev);
1339
1340        vp->estats.tx_timeout_count++;
1341        netif_trans_update(dev);
1342        schedule_work(&vp->reset_tx);
1343}
1344
1345static netdev_features_t vector_fix_features(struct net_device *dev,
1346        netdev_features_t features)
1347{
1348        features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
1349        return features;
1350}
1351
1352static int vector_set_features(struct net_device *dev,
1353        netdev_features_t features)
1354{
1355        struct vector_private *vp = netdev_priv(dev);
1356        /* Adjust buffer sizes for GSO/GRO. Unfortunately, there is
1357         * no way to negotiate it on raw sockets, so we can change
1358         * only our side.
1359         */
1360        if (features & NETIF_F_GRO)
1361                /* All new frame buffers will be GRO-sized */
1362                vp->req_size = 65536;
1363        else
1364                /* All new frame buffers will be normal sized */
1365                vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN;
1366        return 0;
1367}
1368
1369#ifdef CONFIG_NET_POLL_CONTROLLER
1370static void vector_net_poll_controller(struct net_device *dev)
1371{
1372        disable_irq(dev->irq);
1373        vector_rx_interrupt(dev->irq, dev);
1374        enable_irq(dev->irq);
1375}
1376#endif
1377
1378static void vector_net_get_drvinfo(struct net_device *dev,
1379                                struct ethtool_drvinfo *info)
1380{
1381        strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1382}
1383
1384static int vector_net_load_bpf_flash(struct net_device *dev,
1385                                struct ethtool_flash *efl)
1386{
1387        struct vector_private *vp = netdev_priv(dev);
1388        struct vector_device *vdevice;
1389        const struct firmware *fw;
1390        int result = 0;
1391
1392        if (!(vp->options & VECTOR_BPF_FLASH)) {
1393                netdev_err(dev, "loading firmware not permitted: %s\n", efl->data);
1394                return -1;
1395        }
1396
1397        spin_lock(&vp->lock);
1398
1399        if (vp->bpf != NULL) {
1400                if (vp->opened)
1401                        uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
1402                kfree(vp->bpf->filter);
1403                vp->bpf->filter = NULL;
1404        } else {
1405                vp->bpf = kmalloc(sizeof(struct sock_fprog), GFP_ATOMIC);
1406                if (vp->bpf == NULL) {
1407                        netdev_err(dev, "failed to allocate memory for firmware\n");
1408                        goto flash_fail;
1409                }
1410        }
1411
1412        vdevice = find_device(vp->unit);
1413
1414        if (request_firmware(&fw, efl->data, &vdevice->pdev.dev))
1415                goto flash_fail;
1416
1417        vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC);
1418        if (!vp->bpf->filter)
1419                goto free_buffer;
1420
1421        vp->bpf->len = fw->size / sizeof(struct sock_filter);
1422        release_firmware(fw);
1423
1424        if (vp->opened)
1425                result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
1426
1427        spin_unlock(&vp->lock);
1428
1429        return result;
1430
1431free_buffer:
1432        release_firmware(fw);
1433
1434flash_fail:
1435        spin_unlock(&vp->lock);
1436        if (vp->bpf != NULL)
1437                kfree(vp->bpf->filter);
1438        kfree(vp->bpf);
1439        vp->bpf = NULL;
1440        return -1;
1441}
1442
1443static void vector_get_ringparam(struct net_device *netdev,
1444                                struct ethtool_ringparam *ring)
1445{
1446        struct vector_private *vp = netdev_priv(netdev);
1447
1448        ring->rx_max_pending = vp->rx_queue->max_depth;
1449        ring->tx_max_pending = vp->tx_queue->max_depth;
1450        ring->rx_pending = vp->rx_queue->max_depth;
1451        ring->tx_pending = vp->tx_queue->max_depth;
1452}
1453
1454static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
1455{
1456        switch (stringset) {
1457        case ETH_SS_TEST:
1458                *buf = '\0';
1459                break;
1460        case ETH_SS_STATS:
1461                memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
1462                break;
1463        default:
1464                WARN_ON(1);
1465                break;
1466        }
1467}
1468
1469static int vector_get_sset_count(struct net_device *dev, int sset)
1470{
1471        switch (sset) {
1472        case ETH_SS_TEST:
1473                return 0;
1474        case ETH_SS_STATS:
1475                return VECTOR_NUM_STATS;
1476        default:
1477                return -EOPNOTSUPP;
1478        }
1479}
1480
1481static void vector_get_ethtool_stats(struct net_device *dev,
1482        struct ethtool_stats *estats,
1483        u64 *tmp_stats)
1484{
1485        struct vector_private *vp = netdev_priv(dev);
1486
1487        memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats));
1488}
1489
1490static int vector_get_coalesce(struct net_device *netdev,
1491                               struct ethtool_coalesce *ec,
1492                               struct kernel_ethtool_coalesce *kernel_coal,
1493                               struct netlink_ext_ack *extack)
1494{
1495        struct vector_private *vp = netdev_priv(netdev);
1496
1497        ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ;
1498        return 0;
1499}
1500
1501static int vector_set_coalesce(struct net_device *netdev,
1502                               struct ethtool_coalesce *ec,
1503                               struct kernel_ethtool_coalesce *kernel_coal,
1504                               struct netlink_ext_ack *extack)
1505{
1506        struct vector_private *vp = netdev_priv(netdev);
1507
1508        vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000;
1509        if (vp->coalesce == 0)
1510                vp->coalesce = 1;
1511        return 0;
1512}
1513
1514static const struct ethtool_ops vector_net_ethtool_ops = {
1515        .supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS,
1516        .get_drvinfo    = vector_net_get_drvinfo,
1517        .get_link       = ethtool_op_get_link,
1518        .get_ts_info    = ethtool_op_get_ts_info,
1519        .get_ringparam  = vector_get_ringparam,
1520        .get_strings    = vector_get_strings,
1521        .get_sset_count = vector_get_sset_count,
1522        .get_ethtool_stats = vector_get_ethtool_stats,
1523        .get_coalesce   = vector_get_coalesce,
1524        .set_coalesce   = vector_set_coalesce,
1525        .flash_device   = vector_net_load_bpf_flash,
1526};
1527
1528
1529static const struct net_device_ops vector_netdev_ops = {
1530        .ndo_open               = vector_net_open,
1531        .ndo_stop               = vector_net_close,
1532        .ndo_start_xmit         = vector_net_start_xmit,
1533        .ndo_set_rx_mode        = vector_net_set_multicast_list,
1534        .ndo_tx_timeout         = vector_net_tx_timeout,
1535        .ndo_set_mac_address    = eth_mac_addr,
1536        .ndo_validate_addr      = eth_validate_addr,
1537        .ndo_fix_features       = vector_fix_features,
1538        .ndo_set_features       = vector_set_features,
1539#ifdef CONFIG_NET_POLL_CONTROLLER
1540        .ndo_poll_controller = vector_net_poll_controller,
1541#endif
1542};
1543
1544
1545static void vector_timer_expire(struct timer_list *t)
1546{
1547        struct vector_private *vp = from_timer(vp, t, tl);
1548
1549        vp->estats.tx_kicks++;
1550        vector_send(vp->tx_queue);
1551}
1552
1553static void vector_eth_configure(
1554                int n,
1555                struct arglist *def
1556        )
1557{
1558        struct vector_device *device;
1559        struct net_device *dev;
1560        struct vector_private *vp;
1561        int err;
1562
1563        device = kzalloc(sizeof(*device), GFP_KERNEL);
1564        if (device == NULL) {
1565                printk(KERN_ERR "eth_configure failed to allocate struct "
1566                                 "vector_device\n");
1567                return;
1568        }
1569        dev = alloc_etherdev(sizeof(struct vector_private));
1570        if (dev == NULL) {
1571                printk(KERN_ERR "eth_configure: failed to allocate struct "
1572                                 "net_device for vec%d\n", n);
1573                goto out_free_device;
1574        }
1575
1576        dev->mtu = get_mtu(def);
1577
1578        INIT_LIST_HEAD(&device->list);
1579        device->unit = n;
1580
1581        /* If this name ends up conflicting with an existing registered
1582         * netdevice, that is OK, register_netdev{,ice}() will notice this
1583         * and fail.
1584         */
1585        snprintf(dev->name, sizeof(dev->name), "vec%d", n);
1586        uml_net_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac"));
1587        vp = netdev_priv(dev);
1588
1589        /* sysfs register */
1590        if (!driver_registered) {
1591                platform_driver_register(&uml_net_driver);
1592                driver_registered = 1;
1593        }
1594        device->pdev.id = n;
1595        device->pdev.name = DRIVER_NAME;
1596        device->pdev.dev.release = vector_device_release;
1597        dev_set_drvdata(&device->pdev.dev, device);
1598        if (platform_device_register(&device->pdev))
1599                goto out_free_netdev;
1600        SET_NETDEV_DEV(dev, &device->pdev.dev);
1601
1602        device->dev = dev;
1603
1604        *vp = ((struct vector_private)
1605                {
1606                .list                   = LIST_HEAD_INIT(vp->list),
1607                .dev                    = dev,
1608                .unit                   = n,
1609                .options                = get_transport_options(def),
1610                .rx_irq                 = 0,
1611                .tx_irq                 = 0,
1612                .parsed                 = def,
1613                .max_packet             = get_mtu(def) + ETH_HEADER_OTHER,
1614                /* TODO - we need to calculate headroom so that ip header
1615                 * is 16 byte aligned all the time
1616                 */
1617                .headroom               = get_headroom(def),
1618                .form_header            = NULL,
1619                .verify_header          = NULL,
1620                .header_rxbuffer        = NULL,
1621                .header_txbuffer        = NULL,
1622                .header_size            = 0,
1623                .rx_header_size         = 0,
1624                .rexmit_scheduled       = false,
1625                .opened                 = false,
1626                .transport_data         = NULL,
1627                .in_write_poll          = false,
1628                .coalesce               = 2,
1629                .req_size               = get_req_size(def),
1630                .in_error               = false,
1631                .bpf                    = NULL
1632        });
1633
1634        dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
1635        tasklet_setup(&vp->tx_poll, vector_tx_poll);
1636        INIT_WORK(&vp->reset_tx, vector_reset_tx);
1637
1638        timer_setup(&vp->tl, vector_timer_expire, 0);
1639        spin_lock_init(&vp->lock);
1640
1641        /* FIXME */
1642        dev->netdev_ops = &vector_netdev_ops;
1643        dev->ethtool_ops = &vector_net_ethtool_ops;
1644        dev->watchdog_timeo = (HZ >> 1);
1645        /* primary IRQ - fixme */
1646        dev->irq = 0; /* we will adjust this once opened */
1647
1648        rtnl_lock();
1649        err = register_netdevice(dev);
1650        rtnl_unlock();
1651        if (err)
1652                goto out_undo_user_init;
1653
1654        spin_lock(&vector_devices_lock);
1655        list_add(&device->list, &vector_devices);
1656        spin_unlock(&vector_devices_lock);
1657
1658        return;
1659
1660out_undo_user_init:
1661        return;
1662out_free_netdev:
1663        free_netdev(dev);
1664out_free_device:
1665        kfree(device);
1666}
1667
1668
1669
1670
1671/*
1672 * Invoked late in the init
1673 */
1674
1675static int __init vector_init(void)
1676{
1677        struct list_head *ele;
1678        struct vector_cmd_line_arg *def;
1679        struct arglist *parsed;
1680
1681        list_for_each(ele, &vec_cmd_line) {
1682                def = list_entry(ele, struct vector_cmd_line_arg, list);
1683                parsed = uml_parse_vector_ifspec(def->arguments);
1684                if (parsed != NULL)
1685                        vector_eth_configure(def->unit, parsed);
1686        }
1687        return 0;
1688}
1689
1690
1691/* Invoked at initial argument parsing, only stores
1692 * arguments until a proper vector_init is called
1693 * later
1694 */
1695
1696static int __init vector_setup(char *str)
1697{
1698        char *error;
1699        int n, err;
1700        struct vector_cmd_line_arg *new;
1701
1702        err = vector_parse(str, &n, &str, &error);
1703        if (err) {
1704                printk(KERN_ERR "vector_setup - Couldn't parse '%s' : %s\n",
1705                                 str, error);
1706                return 1;
1707        }
1708        new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
1709        if (!new)
1710                panic("%s: Failed to allocate %zu bytes\n", __func__,
1711                      sizeof(*new));
1712        INIT_LIST_HEAD(&new->list);
1713        new->unit = n;
1714        new->arguments = str;
1715        list_add_tail(&new->list, &vec_cmd_line);
1716        return 1;
1717}
1718
1719__setup("vec", vector_setup);
1720__uml_help(vector_setup,
1721"vec[0-9]+:<option>=<value>,<option>=<value>\n"
1722"        Configure a vector io network device.\n\n"
1723);
1724
1725late_initcall(vector_init);
1726
1727static struct mc_device vector_mc = {
1728        .list           = LIST_HEAD_INIT(vector_mc.list),
1729        .name           = "vec",
1730        .config         = vector_config,
1731        .get_config     = NULL,
1732        .id             = vector_id,
1733        .remove         = vector_remove,
1734};
1735
1736#ifdef CONFIG_INET
1737static int vector_inetaddr_event(
1738        struct notifier_block *this,
1739        unsigned long event,
1740        void *ptr)
1741{
1742        return NOTIFY_DONE;
1743}
1744
1745static struct notifier_block vector_inetaddr_notifier = {
1746        .notifier_call          = vector_inetaddr_event,
1747};
1748
1749static void inet_register(void)
1750{
1751        register_inetaddr_notifier(&vector_inetaddr_notifier);
1752}
1753#else
1754static inline void inet_register(void)
1755{
1756}
1757#endif
1758
1759static int vector_net_init(void)
1760{
1761        mconsole_register_dev(&vector_mc);
1762        inet_register();
1763        return 0;
1764}
1765
1766__initcall(vector_net_init);
1767
1768
1769
1770