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