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