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