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