1 /* sunvnet.c: Sun LDOM Virtual Network Driver.
2  *
3  * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4  * Copyright (C) 2016 Oracle. All rights reserved.
5  */
6 
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/etherdevice.h>
16 #include <linux/mutex.h>
17 #include <linux/highmem.h>
18 #include <linux/if_vlan.h>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/sunvnet.h>
21 
22 #if IS_ENABLED(CONFIG_IPV6)
23 #include <linux/icmpv6.h>
24 #endif
25 
26 #include <net/ip.h>
27 #include <net/icmp.h>
28 #include <net/route.h>
29 
30 #include <asm/vio.h>
31 #include <asm/ldc.h>
32 
33 #include "sunvnet_common.h"
34 
35 /* Heuristic for the number of times to exponentially backoff and
36  * retry sending an LDC trigger when EAGAIN is encountered
37  */
38 #define	VNET_MAX_RETRIES	10
39 
40 static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
41 static void vnet_port_reset(struct vnet_port *port);
42 
43 static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
44 {
45 	return vio_dring_avail(dr, VNET_TX_RING_SIZE);
46 }
47 
48 static int vnet_handle_unknown(struct vnet_port *port, void *arg)
49 {
50 	struct vio_msg_tag *pkt = arg;
51 
52 	pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
53 	       pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
54 	pr_err("Resetting connection\n");
55 
56 	ldc_disconnect(port->vio.lp);
57 
58 	return -ECONNRESET;
59 }
60 
61 static int vnet_port_alloc_tx_ring(struct vnet_port *port);
62 
63 int sunvnet_send_attr_common(struct vio_driver_state *vio)
64 {
65 	struct vnet_port *port = to_vnet_port(vio);
66 	struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
67 	struct vio_net_attr_info pkt;
68 	int framelen = ETH_FRAME_LEN;
69 	int i, err;
70 
71 	err = vnet_port_alloc_tx_ring(to_vnet_port(vio));
72 	if (err)
73 		return err;
74 
75 	memset(&pkt, 0, sizeof(pkt));
76 	pkt.tag.type = VIO_TYPE_CTRL;
77 	pkt.tag.stype = VIO_SUBTYPE_INFO;
78 	pkt.tag.stype_env = VIO_ATTR_INFO;
79 	pkt.tag.sid = vio_send_sid(vio);
80 	if (vio_version_before(vio, 1, 2))
81 		pkt.xfer_mode = VIO_DRING_MODE;
82 	else
83 		pkt.xfer_mode = VIO_NEW_DRING_MODE;
84 	pkt.addr_type = VNET_ADDR_ETHERMAC;
85 	pkt.ack_freq = 0;
86 	for (i = 0; i < 6; i++)
87 		pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
88 	if (vio_version_after(vio, 1, 3)) {
89 		if (port->rmtu) {
90 			port->rmtu = min(VNET_MAXPACKET, port->rmtu);
91 			pkt.mtu = port->rmtu;
92 		} else {
93 			port->rmtu = VNET_MAXPACKET;
94 			pkt.mtu = port->rmtu;
95 		}
96 		if (vio_version_after_eq(vio, 1, 6))
97 			pkt.options = VIO_TX_DRING;
98 	} else if (vio_version_before(vio, 1, 3)) {
99 		pkt.mtu = framelen;
100 	} else { /* v1.3 */
101 		pkt.mtu = framelen + VLAN_HLEN;
102 	}
103 
104 	pkt.cflags = 0;
105 	if (vio_version_after_eq(vio, 1, 7) && port->tso) {
106 		pkt.cflags |= VNET_LSO_IPV4_CAPAB;
107 		if (!port->tsolen)
108 			port->tsolen = VNET_MAXTSO;
109 		pkt.ipv4_lso_maxlen = port->tsolen;
110 	}
111 
112 	pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
113 
114 	viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
115 	       "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
116 	       "cflags[0x%04x] lso_max[%u]\n",
117 	       pkt.xfer_mode, pkt.addr_type,
118 	       (unsigned long long)pkt.addr,
119 	       pkt.ack_freq, pkt.plnk_updt, pkt.options,
120 	       (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);
121 
122 	return vio_ldc_send(vio, &pkt, sizeof(pkt));
123 }
124 EXPORT_SYMBOL_GPL(sunvnet_send_attr_common);
125 
126 static int handle_attr_info(struct vio_driver_state *vio,
127 			    struct vio_net_attr_info *pkt)
128 {
129 	struct vnet_port *port = to_vnet_port(vio);
130 	u64	localmtu;
131 	u8	xfer_mode;
132 
133 	viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
134 	       "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
135 	       " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
136 	       pkt->xfer_mode, pkt->addr_type,
137 	       (unsigned long long)pkt->addr,
138 	       pkt->ack_freq, pkt->plnk_updt, pkt->options,
139 	       (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
140 	       pkt->ipv4_lso_maxlen);
141 
142 	pkt->tag.sid = vio_send_sid(vio);
143 
144 	xfer_mode = pkt->xfer_mode;
145 	/* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
146 	if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
147 		xfer_mode = VIO_NEW_DRING_MODE;
148 
149 	/* MTU negotiation:
150 	 *	< v1.3 - ETH_FRAME_LEN exactly
151 	 *	> v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
152 	 *			pkt->mtu for ACK
153 	 *	= v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
154 	 */
155 	if (vio_version_before(vio, 1, 3)) {
156 		localmtu = ETH_FRAME_LEN;
157 	} else if (vio_version_after(vio, 1, 3)) {
158 		localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
159 		localmtu = min(pkt->mtu, localmtu);
160 		pkt->mtu = localmtu;
161 	} else { /* v1.3 */
162 		localmtu = ETH_FRAME_LEN + VLAN_HLEN;
163 	}
164 	port->rmtu = localmtu;
165 
166 	/* LSO negotiation */
167 	if (vio_version_after_eq(vio, 1, 7))
168 		port->tso &= !!(pkt->cflags & VNET_LSO_IPV4_CAPAB);
169 	else
170 		port->tso = false;
171 	if (port->tso) {
172 		if (!port->tsolen)
173 			port->tsolen = VNET_MAXTSO;
174 		port->tsolen = min(port->tsolen, pkt->ipv4_lso_maxlen);
175 		if (port->tsolen < VNET_MINTSO) {
176 			port->tso = false;
177 			port->tsolen = 0;
178 			pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
179 		}
180 		pkt->ipv4_lso_maxlen = port->tsolen;
181 	} else {
182 		pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
183 		pkt->ipv4_lso_maxlen = 0;
184 	}
185 
186 	/* for version >= 1.6, ACK packet mode we support */
187 	if (vio_version_after_eq(vio, 1, 6)) {
188 		pkt->xfer_mode = VIO_NEW_DRING_MODE;
189 		pkt->options = VIO_TX_DRING;
190 	}
191 
192 	if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
193 	    pkt->addr_type != VNET_ADDR_ETHERMAC ||
194 	    pkt->mtu != localmtu) {
195 		viodbg(HS, "SEND NET ATTR NACK\n");
196 
197 		pkt->tag.stype = VIO_SUBTYPE_NACK;
198 
199 		(void)vio_ldc_send(vio, pkt, sizeof(*pkt));
200 
201 		return -ECONNRESET;
202 	}
203 
204 	viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
205 	       "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
206 	       "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
207 	       pkt->xfer_mode, pkt->addr_type,
208 	       (unsigned long long)pkt->addr,
209 	       pkt->ack_freq, pkt->plnk_updt, pkt->options,
210 	       (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
211 	       pkt->ipv4_lso_maxlen);
212 
213 	pkt->tag.stype = VIO_SUBTYPE_ACK;
214 
215 	return vio_ldc_send(vio, pkt, sizeof(*pkt));
216 }
217 
218 static int handle_attr_ack(struct vio_driver_state *vio,
219 			   struct vio_net_attr_info *pkt)
220 {
221 	viodbg(HS, "GOT NET ATTR ACK\n");
222 
223 	return 0;
224 }
225 
226 static int handle_attr_nack(struct vio_driver_state *vio,
227 			    struct vio_net_attr_info *pkt)
228 {
229 	viodbg(HS, "GOT NET ATTR NACK\n");
230 
231 	return -ECONNRESET;
232 }
233 
234 int sunvnet_handle_attr_common(struct vio_driver_state *vio, void *arg)
235 {
236 	struct vio_net_attr_info *pkt = arg;
237 
238 	switch (pkt->tag.stype) {
239 	case VIO_SUBTYPE_INFO:
240 		return handle_attr_info(vio, pkt);
241 
242 	case VIO_SUBTYPE_ACK:
243 		return handle_attr_ack(vio, pkt);
244 
245 	case VIO_SUBTYPE_NACK:
246 		return handle_attr_nack(vio, pkt);
247 
248 	default:
249 		return -ECONNRESET;
250 	}
251 }
252 EXPORT_SYMBOL_GPL(sunvnet_handle_attr_common);
253 
254 void sunvnet_handshake_complete_common(struct vio_driver_state *vio)
255 {
256 	struct vio_dring_state *dr;
257 
258 	dr = &vio->drings[VIO_DRIVER_RX_RING];
259 	dr->rcv_nxt = 1;
260 	dr->snd_nxt = 1;
261 
262 	dr = &vio->drings[VIO_DRIVER_TX_RING];
263 	dr->rcv_nxt = 1;
264 	dr->snd_nxt = 1;
265 }
266 EXPORT_SYMBOL_GPL(sunvnet_handshake_complete_common);
267 
268 /* The hypervisor interface that implements copying to/from imported
269  * memory from another domain requires that copies are done to 8-byte
270  * aligned buffers, and that the lengths of such copies are also 8-byte
271  * multiples.
272  *
273  * So we align skb->data to an 8-byte multiple and pad-out the data
274  * area so we can round the copy length up to the next multiple of
275  * 8 for the copy.
276  *
277  * The transmitter puts the actual start of the packet 6 bytes into
278  * the buffer it sends over, so that the IP headers after the ethernet
279  * header are aligned properly.  These 6 bytes are not in the descriptor
280  * length, they are simply implied.  This offset is represented using
281  * the VNET_PACKET_SKIP macro.
282  */
283 static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
284 					   unsigned int len)
285 {
286 	struct sk_buff *skb;
287 	unsigned long addr, off;
288 
289 	skb = netdev_alloc_skb(dev, len + VNET_PACKET_SKIP + 8 + 8);
290 	if (unlikely(!skb))
291 		return NULL;
292 
293 	addr = (unsigned long)skb->data;
294 	off = ((addr + 7UL) & ~7UL) - addr;
295 	if (off)
296 		skb_reserve(skb, off);
297 
298 	return skb;
299 }
300 
301 static inline void vnet_fullcsum(struct sk_buff *skb)
302 {
303 	struct iphdr *iph = ip_hdr(skb);
304 	int offset = skb_transport_offset(skb);
305 
306 	if (skb->protocol != htons(ETH_P_IP))
307 		return;
308 	if (iph->protocol != IPPROTO_TCP &&
309 	    iph->protocol != IPPROTO_UDP)
310 		return;
311 	skb->ip_summed = CHECKSUM_NONE;
312 	skb->csum_level = 1;
313 	skb->csum = 0;
314 	if (iph->protocol == IPPROTO_TCP) {
315 		struct tcphdr *ptcp = tcp_hdr(skb);
316 
317 		ptcp->check = 0;
318 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
319 		ptcp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
320 						skb->len - offset, IPPROTO_TCP,
321 						skb->csum);
322 	} else if (iph->protocol == IPPROTO_UDP) {
323 		struct udphdr *pudp = udp_hdr(skb);
324 
325 		pudp->check = 0;
326 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
327 		pudp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
328 						skb->len - offset, IPPROTO_UDP,
329 						skb->csum);
330 	}
331 }
332 
333 static int vnet_rx_one(struct vnet_port *port, struct vio_net_desc *desc)
334 {
335 	struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
336 	unsigned int len = desc->size;
337 	unsigned int copy_len;
338 	struct sk_buff *skb;
339 	int maxlen;
340 	int err;
341 
342 	err = -EMSGSIZE;
343 	if (port->tso && port->tsolen > port->rmtu)
344 		maxlen = port->tsolen;
345 	else
346 		maxlen = port->rmtu;
347 	if (unlikely(len < ETH_ZLEN || len > maxlen)) {
348 		dev->stats.rx_length_errors++;
349 		goto out_dropped;
350 	}
351 
352 	skb = alloc_and_align_skb(dev, len);
353 	err = -ENOMEM;
354 	if (unlikely(!skb)) {
355 		dev->stats.rx_missed_errors++;
356 		goto out_dropped;
357 	}
358 
359 	copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
360 	skb_put(skb, copy_len);
361 	err = ldc_copy(port->vio.lp, LDC_COPY_IN,
362 		       skb->data, copy_len, 0,
363 		       desc->cookies, desc->ncookies);
364 	if (unlikely(err < 0)) {
365 		dev->stats.rx_frame_errors++;
366 		goto out_free_skb;
367 	}
368 
369 	skb_pull(skb, VNET_PACKET_SKIP);
370 	skb_trim(skb, len);
371 	skb->protocol = eth_type_trans(skb, dev);
372 
373 	if (vio_version_after_eq(&port->vio, 1, 8)) {
374 		struct vio_net_dext *dext = vio_net_ext(desc);
375 
376 		skb_reset_network_header(skb);
377 
378 		if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM) {
379 			if (skb->protocol == ETH_P_IP) {
380 				struct iphdr *iph = ip_hdr(skb);
381 
382 				iph->check = 0;
383 				ip_send_check(iph);
384 			}
385 		}
386 		if ((dext->flags & VNET_PKT_HCK_FULLCKSUM) &&
387 		    skb->ip_summed == CHECKSUM_NONE) {
388 			if (skb->protocol == htons(ETH_P_IP)) {
389 				struct iphdr *iph = ip_hdr(skb);
390 				int ihl = iph->ihl * 4;
391 
392 				skb_reset_transport_header(skb);
393 				skb_set_transport_header(skb, ihl);
394 				vnet_fullcsum(skb);
395 			}
396 		}
397 		if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM_OK) {
398 			skb->ip_summed = CHECKSUM_PARTIAL;
399 			skb->csum_level = 0;
400 			if (dext->flags & VNET_PKT_HCK_FULLCKSUM_OK)
401 				skb->csum_level = 1;
402 		}
403 	}
404 
405 	skb->ip_summed = port->switch_port ? CHECKSUM_NONE : CHECKSUM_PARTIAL;
406 
407 	dev->stats.rx_packets++;
408 	dev->stats.rx_bytes += len;
409 	napi_gro_receive(&port->napi, skb);
410 	return 0;
411 
412 out_free_skb:
413 	kfree_skb(skb);
414 
415 out_dropped:
416 	dev->stats.rx_dropped++;
417 	return err;
418 }
419 
420 static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
421 			 u32 start, u32 end, u8 vio_dring_state)
422 {
423 	struct vio_dring_data hdr = {
424 		.tag = {
425 			.type		= VIO_TYPE_DATA,
426 			.stype		= VIO_SUBTYPE_ACK,
427 			.stype_env	= VIO_DRING_DATA,
428 			.sid		= vio_send_sid(&port->vio),
429 		},
430 		.dring_ident		= dr->ident,
431 		.start_idx		= start,
432 		.end_idx		= end,
433 		.state			= vio_dring_state,
434 	};
435 	int err, delay;
436 	int retries = 0;
437 
438 	hdr.seq = dr->snd_nxt;
439 	delay = 1;
440 	do {
441 		err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
442 		if (err > 0) {
443 			dr->snd_nxt++;
444 			break;
445 		}
446 		udelay(delay);
447 		if ((delay <<= 1) > 128)
448 			delay = 128;
449 		if (retries++ > VNET_MAX_RETRIES) {
450 			pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
451 				port->raddr[0], port->raddr[1],
452 				port->raddr[2], port->raddr[3],
453 				port->raddr[4], port->raddr[5]);
454 			break;
455 		}
456 	} while (err == -EAGAIN);
457 
458 	if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
459 		port->stop_rx_idx = end;
460 		port->stop_rx = true;
461 	} else {
462 		port->stop_rx_idx = 0;
463 		port->stop_rx = false;
464 	}
465 
466 	return err;
467 }
468 
469 static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
470 					struct vio_dring_state *dr,
471 					u32 index)
472 {
473 	struct vio_net_desc *desc = port->vio.desc_buf;
474 	int err;
475 
476 	err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
477 				  (index * dr->entry_size),
478 				  dr->cookies, dr->ncookies);
479 	if (err < 0)
480 		return ERR_PTR(err);
481 
482 	return desc;
483 }
484 
485 static int put_rx_desc(struct vnet_port *port,
486 		       struct vio_dring_state *dr,
487 		       struct vio_net_desc *desc,
488 		       u32 index)
489 {
490 	int err;
491 
492 	err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
493 				  (index * dr->entry_size),
494 				  dr->cookies, dr->ncookies);
495 	if (err < 0)
496 		return err;
497 
498 	return 0;
499 }
500 
501 static int vnet_walk_rx_one(struct vnet_port *port,
502 			    struct vio_dring_state *dr,
503 			    u32 index, int *needs_ack)
504 {
505 	struct vio_net_desc *desc = get_rx_desc(port, dr, index);
506 	struct vio_driver_state *vio = &port->vio;
507 	int err;
508 
509 	BUG_ON(!desc);
510 	if (IS_ERR(desc))
511 		return PTR_ERR(desc);
512 
513 	if (desc->hdr.state != VIO_DESC_READY)
514 		return 1;
515 
516 	dma_rmb();
517 
518 	viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
519 	       desc->hdr.state, desc->hdr.ack,
520 	       desc->size, desc->ncookies,
521 	       desc->cookies[0].cookie_addr,
522 	       desc->cookies[0].cookie_size);
523 
524 	err = vnet_rx_one(port, desc);
525 	if (err == -ECONNRESET)
526 		return err;
527 	trace_vnet_rx_one(port->vio._local_sid, port->vio._peer_sid,
528 			  index, desc->hdr.ack);
529 	desc->hdr.state = VIO_DESC_DONE;
530 	err = put_rx_desc(port, dr, desc, index);
531 	if (err < 0)
532 		return err;
533 	*needs_ack = desc->hdr.ack;
534 	return 0;
535 }
536 
537 static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
538 			u32 start, u32 end, int *npkts, int budget)
539 {
540 	struct vio_driver_state *vio = &port->vio;
541 	int ack_start = -1, ack_end = -1;
542 	bool send_ack = true;
543 
544 	end = (end == (u32)-1) ? vio_dring_prev(dr, start)
545 			       : vio_dring_next(dr, end);
546 
547 	viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
548 
549 	while (start != end) {
550 		int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
551 
552 		if (err == -ECONNRESET)
553 			return err;
554 		if (err != 0)
555 			break;
556 		(*npkts)++;
557 		if (ack_start == -1)
558 			ack_start = start;
559 		ack_end = start;
560 		start = vio_dring_next(dr, start);
561 		if (ack && start != end) {
562 			err = vnet_send_ack(port, dr, ack_start, ack_end,
563 					    VIO_DRING_ACTIVE);
564 			if (err == -ECONNRESET)
565 				return err;
566 			ack_start = -1;
567 		}
568 		if ((*npkts) >= budget) {
569 			send_ack = false;
570 			break;
571 		}
572 	}
573 	if (unlikely(ack_start == -1)) {
574 		ack_end = vio_dring_prev(dr, start);
575 		ack_start = ack_end;
576 	}
577 	if (send_ack) {
578 		port->napi_resume = false;
579 		trace_vnet_tx_send_stopped_ack(port->vio._local_sid,
580 					       port->vio._peer_sid,
581 					       ack_end, *npkts);
582 		return vnet_send_ack(port, dr, ack_start, ack_end,
583 				     VIO_DRING_STOPPED);
584 	} else  {
585 		trace_vnet_tx_defer_stopped_ack(port->vio._local_sid,
586 						port->vio._peer_sid,
587 						ack_end, *npkts);
588 		port->napi_resume = true;
589 		port->napi_stop_idx = ack_end;
590 		return 1;
591 	}
592 }
593 
594 static int vnet_rx(struct vnet_port *port, void *msgbuf, int *npkts,
595 		   int budget)
596 {
597 	struct vio_dring_data *pkt = msgbuf;
598 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
599 	struct vio_driver_state *vio = &port->vio;
600 
601 	viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
602 	       pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
603 
604 	if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
605 		return 0;
606 	if (unlikely(pkt->seq != dr->rcv_nxt)) {
607 		pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
608 		       pkt->seq, dr->rcv_nxt);
609 		return 0;
610 	}
611 
612 	if (!port->napi_resume)
613 		dr->rcv_nxt++;
614 
615 	/* XXX Validate pkt->start_idx and pkt->end_idx XXX */
616 
617 	return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx,
618 			    npkts, budget);
619 }
620 
621 static int idx_is_pending(struct vio_dring_state *dr, u32 end)
622 {
623 	u32 idx = dr->cons;
624 	int found = 0;
625 
626 	while (idx != dr->prod) {
627 		if (idx == end) {
628 			found = 1;
629 			break;
630 		}
631 		idx = vio_dring_next(dr, idx);
632 	}
633 	return found;
634 }
635 
636 static int vnet_ack(struct vnet_port *port, void *msgbuf)
637 {
638 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
639 	struct vio_dring_data *pkt = msgbuf;
640 	struct net_device *dev;
641 	u32 end;
642 	struct vio_net_desc *desc;
643 	struct netdev_queue *txq;
644 
645 	if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
646 		return 0;
647 
648 	end = pkt->end_idx;
649 	dev = VNET_PORT_TO_NET_DEVICE(port);
650 	netif_tx_lock(dev);
651 	if (unlikely(!idx_is_pending(dr, end))) {
652 		netif_tx_unlock(dev);
653 		return 0;
654 	}
655 
656 	/* sync for race conditions with vnet_start_xmit() and tell xmit it
657 	 * is time to send a trigger.
658 	 */
659 	trace_vnet_rx_stopped_ack(port->vio._local_sid,
660 				  port->vio._peer_sid, end);
661 	dr->cons = vio_dring_next(dr, end);
662 	desc = vio_dring_entry(dr, dr->cons);
663 	if (desc->hdr.state == VIO_DESC_READY && !port->start_cons) {
664 		/* vnet_start_xmit() just populated this dring but missed
665 		 * sending the "start" LDC message to the consumer.
666 		 * Send a "start" trigger on its behalf.
667 		 */
668 		if (__vnet_tx_trigger(port, dr->cons) > 0)
669 			port->start_cons = false;
670 		else
671 			port->start_cons = true;
672 	} else {
673 		port->start_cons = true;
674 	}
675 	netif_tx_unlock(dev);
676 
677 	txq = netdev_get_tx_queue(dev, port->q_index);
678 	if (unlikely(netif_tx_queue_stopped(txq) &&
679 		     vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
680 		return 1;
681 
682 	return 0;
683 }
684 
685 static int vnet_nack(struct vnet_port *port, void *msgbuf)
686 {
687 	/* XXX just reset or similar XXX */
688 	return 0;
689 }
690 
691 static int handle_mcast(struct vnet_port *port, void *msgbuf)
692 {
693 	struct vio_net_mcast_info *pkt = msgbuf;
694 	struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
695 
696 	if (pkt->tag.stype != VIO_SUBTYPE_ACK)
697 		pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
698 		       dev->name,
699 		       pkt->tag.type,
700 		       pkt->tag.stype,
701 		       pkt->tag.stype_env,
702 		       pkt->tag.sid);
703 
704 	return 0;
705 }
706 
707 /* If the queue is stopped, wake it up so that we'll
708  * send out another START message at the next TX.
709  */
710 static void maybe_tx_wakeup(struct vnet_port *port)
711 {
712 	struct netdev_queue *txq;
713 
714 	txq = netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port),
715 				  port->q_index);
716 	__netif_tx_lock(txq, smp_processor_id());
717 	if (likely(netif_tx_queue_stopped(txq))) {
718 		struct vio_dring_state *dr;
719 
720 		dr = &port->vio.drings[VIO_DRIVER_TX_RING];
721 		netif_tx_wake_queue(txq);
722 	}
723 	__netif_tx_unlock(txq);
724 }
725 
726 bool sunvnet_port_is_up_common(struct vnet_port *vnet)
727 {
728 	struct vio_driver_state *vio = &vnet->vio;
729 
730 	return !!(vio->hs_state & VIO_HS_COMPLETE);
731 }
732 EXPORT_SYMBOL_GPL(sunvnet_port_is_up_common);
733 
734 static int vnet_event_napi(struct vnet_port *port, int budget)
735 {
736 	struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
737 	struct vio_driver_state *vio = &port->vio;
738 	int tx_wakeup, err;
739 	int npkts = 0;
740 	int event = (port->rx_event & LDC_EVENT_RESET);
741 
742 ldc_ctrl:
743 	if (unlikely(event == LDC_EVENT_RESET ||
744 		     event == LDC_EVENT_UP)) {
745 		vio_link_state_change(vio, event);
746 
747 		if (event == LDC_EVENT_RESET) {
748 			vnet_port_reset(port);
749 			vio_port_up(vio);
750 
751 			/* If the device is running but its tx queue was
752 			 * stopped (due to flow control), restart it.
753 			 * This is necessary since vnet_port_reset()
754 			 * clears the tx drings and thus we may never get
755 			 * back a VIO_TYPE_DATA ACK packet - which is
756 			 * the normal mechanism to restart the tx queue.
757 			 */
758 			if (netif_running(dev))
759 				maybe_tx_wakeup(port);
760 		}
761 		port->rx_event = 0;
762 		return 0;
763 	}
764 	/* We may have multiple LDC events in rx_event. Unroll send_events() */
765 	event = (port->rx_event & LDC_EVENT_UP);
766 	port->rx_event &= ~(LDC_EVENT_RESET | LDC_EVENT_UP);
767 	if (event == LDC_EVENT_UP)
768 		goto ldc_ctrl;
769 	event = port->rx_event;
770 	if (!(event & LDC_EVENT_DATA_READY))
771 		return 0;
772 
773 	/* we dont expect any other bits than RESET, UP, DATA_READY */
774 	BUG_ON(event != LDC_EVENT_DATA_READY);
775 
776 	err = 0;
777 	tx_wakeup = 0;
778 	while (1) {
779 		union {
780 			struct vio_msg_tag tag;
781 			u64 raw[8];
782 		} msgbuf;
783 
784 		if (port->napi_resume) {
785 			struct vio_dring_data *pkt =
786 				(struct vio_dring_data *)&msgbuf;
787 			struct vio_dring_state *dr =
788 				&port->vio.drings[VIO_DRIVER_RX_RING];
789 
790 			pkt->tag.type = VIO_TYPE_DATA;
791 			pkt->tag.stype = VIO_SUBTYPE_INFO;
792 			pkt->tag.stype_env = VIO_DRING_DATA;
793 			pkt->seq = dr->rcv_nxt;
794 			pkt->start_idx = vio_dring_next(dr,
795 							port->napi_stop_idx);
796 			pkt->end_idx = -1;
797 			goto napi_resume;
798 		}
799 		err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
800 		if (unlikely(err < 0)) {
801 			if (err == -ECONNRESET)
802 				vio_conn_reset(vio);
803 			break;
804 		}
805 		if (err == 0)
806 			break;
807 		viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
808 		       msgbuf.tag.type,
809 		       msgbuf.tag.stype,
810 		       msgbuf.tag.stype_env,
811 		       msgbuf.tag.sid);
812 		err = vio_validate_sid(vio, &msgbuf.tag);
813 		if (err < 0)
814 			break;
815 napi_resume:
816 		if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
817 			if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
818 				if (!sunvnet_port_is_up_common(port)) {
819 					/* failures like handshake_failure()
820 					 * may have cleaned up dring, but
821 					 * NAPI polling may bring us here.
822 					 */
823 					err = -ECONNRESET;
824 					break;
825 				}
826 				err = vnet_rx(port, &msgbuf, &npkts, budget);
827 				if (npkts >= budget)
828 					break;
829 				if (npkts == 0)
830 					break;
831 			} else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
832 				err = vnet_ack(port, &msgbuf);
833 				if (err > 0)
834 					tx_wakeup |= err;
835 			} else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
836 				err = vnet_nack(port, &msgbuf);
837 			}
838 		} else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
839 			if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
840 				err = handle_mcast(port, &msgbuf);
841 			else
842 				err = vio_control_pkt_engine(vio, &msgbuf);
843 			if (err)
844 				break;
845 		} else {
846 			err = vnet_handle_unknown(port, &msgbuf);
847 		}
848 		if (err == -ECONNRESET)
849 			break;
850 	}
851 	if (unlikely(tx_wakeup && err != -ECONNRESET))
852 		maybe_tx_wakeup(port);
853 	return npkts;
854 }
855 
856 int sunvnet_poll_common(struct napi_struct *napi, int budget)
857 {
858 	struct vnet_port *port = container_of(napi, struct vnet_port, napi);
859 	struct vio_driver_state *vio = &port->vio;
860 	int processed = vnet_event_napi(port, budget);
861 
862 	if (processed < budget) {
863 		napi_complete(napi);
864 		port->rx_event &= ~LDC_EVENT_DATA_READY;
865 		vio_set_intr(vio->vdev->rx_ino, HV_INTR_ENABLED);
866 	}
867 	return processed;
868 }
869 EXPORT_SYMBOL_GPL(sunvnet_poll_common);
870 
871 void sunvnet_event_common(void *arg, int event)
872 {
873 	struct vnet_port *port = arg;
874 	struct vio_driver_state *vio = &port->vio;
875 
876 	port->rx_event |= event;
877 	vio_set_intr(vio->vdev->rx_ino, HV_INTR_DISABLED);
878 	napi_schedule(&port->napi);
879 }
880 EXPORT_SYMBOL_GPL(sunvnet_event_common);
881 
882 static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
883 {
884 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
885 	struct vio_dring_data hdr = {
886 		.tag = {
887 			.type		= VIO_TYPE_DATA,
888 			.stype		= VIO_SUBTYPE_INFO,
889 			.stype_env	= VIO_DRING_DATA,
890 			.sid		= vio_send_sid(&port->vio),
891 		},
892 		.dring_ident		= dr->ident,
893 		.start_idx		= start,
894 		.end_idx		= (u32)-1,
895 	};
896 	int err, delay;
897 	int retries = 0;
898 
899 	if (port->stop_rx) {
900 		trace_vnet_tx_pending_stopped_ack(port->vio._local_sid,
901 						  port->vio._peer_sid,
902 						  port->stop_rx_idx, -1);
903 		err = vnet_send_ack(port,
904 				    &port->vio.drings[VIO_DRIVER_RX_RING],
905 				    port->stop_rx_idx, -1,
906 				    VIO_DRING_STOPPED);
907 		if (err <= 0)
908 			return err;
909 	}
910 
911 	hdr.seq = dr->snd_nxt;
912 	delay = 1;
913 	do {
914 		err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
915 		if (err > 0) {
916 			dr->snd_nxt++;
917 			break;
918 		}
919 		udelay(delay);
920 		if ((delay <<= 1) > 128)
921 			delay = 128;
922 		if (retries++ > VNET_MAX_RETRIES)
923 			break;
924 	} while (err == -EAGAIN);
925 	trace_vnet_tx_trigger(port->vio._local_sid,
926 			      port->vio._peer_sid, start, err);
927 
928 	return err;
929 }
930 
931 static struct sk_buff *vnet_clean_tx_ring(struct vnet_port *port,
932 					  unsigned *pending)
933 {
934 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
935 	struct sk_buff *skb = NULL;
936 	int i, txi;
937 
938 	*pending = 0;
939 
940 	txi = dr->prod;
941 	for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
942 		struct vio_net_desc *d;
943 
944 		--txi;
945 		if (txi < 0)
946 			txi = VNET_TX_RING_SIZE - 1;
947 
948 		d = vio_dring_entry(dr, txi);
949 
950 		if (d->hdr.state == VIO_DESC_READY) {
951 			(*pending)++;
952 			continue;
953 		}
954 		if (port->tx_bufs[txi].skb) {
955 			if (d->hdr.state != VIO_DESC_DONE)
956 				pr_notice("invalid ring buffer state %d\n",
957 					  d->hdr.state);
958 			BUG_ON(port->tx_bufs[txi].skb->next);
959 
960 			port->tx_bufs[txi].skb->next = skb;
961 			skb = port->tx_bufs[txi].skb;
962 			port->tx_bufs[txi].skb = NULL;
963 
964 			ldc_unmap(port->vio.lp,
965 				  port->tx_bufs[txi].cookies,
966 				  port->tx_bufs[txi].ncookies);
967 		} else if (d->hdr.state == VIO_DESC_FREE) {
968 			break;
969 		}
970 		d->hdr.state = VIO_DESC_FREE;
971 	}
972 	return skb;
973 }
974 
975 static inline void vnet_free_skbs(struct sk_buff *skb)
976 {
977 	struct sk_buff *next;
978 
979 	while (skb) {
980 		next = skb->next;
981 		skb->next = NULL;
982 		dev_kfree_skb(skb);
983 		skb = next;
984 	}
985 }
986 
987 void sunvnet_clean_timer_expire_common(unsigned long port0)
988 {
989 	struct vnet_port *port = (struct vnet_port *)port0;
990 	struct sk_buff *freeskbs;
991 	unsigned pending;
992 
993 	netif_tx_lock(VNET_PORT_TO_NET_DEVICE(port));
994 	freeskbs = vnet_clean_tx_ring(port, &pending);
995 	netif_tx_unlock(VNET_PORT_TO_NET_DEVICE(port));
996 
997 	vnet_free_skbs(freeskbs);
998 
999 	if (pending)
1000 		(void)mod_timer(&port->clean_timer,
1001 				jiffies + VNET_CLEAN_TIMEOUT);
1002 	 else
1003 		del_timer(&port->clean_timer);
1004 }
1005 EXPORT_SYMBOL_GPL(sunvnet_clean_timer_expire_common);
1006 
1007 static inline int vnet_skb_map(struct ldc_channel *lp, struct sk_buff *skb,
1008 			       struct ldc_trans_cookie *cookies, int ncookies,
1009 			       unsigned int map_perm)
1010 {
1011 	int i, nc, err, blen;
1012 
1013 	/* header */
1014 	blen = skb_headlen(skb);
1015 	if (blen < ETH_ZLEN)
1016 		blen = ETH_ZLEN;
1017 	blen += VNET_PACKET_SKIP;
1018 	blen += 8 - (blen & 7);
1019 
1020 	err = ldc_map_single(lp, skb->data - VNET_PACKET_SKIP, blen, cookies,
1021 			     ncookies, map_perm);
1022 	if (err < 0)
1023 		return err;
1024 	nc = err;
1025 
1026 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1027 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1028 		u8 *vaddr;
1029 
1030 		if (nc < ncookies) {
1031 			vaddr = kmap_atomic(skb_frag_page(f));
1032 			blen = skb_frag_size(f);
1033 			blen += 8 - (blen & 7);
1034 			err = ldc_map_single(lp, vaddr + f->page_offset,
1035 					     blen, cookies + nc, ncookies - nc,
1036 					     map_perm);
1037 			kunmap_atomic(vaddr);
1038 		} else {
1039 			err = -EMSGSIZE;
1040 		}
1041 
1042 		if (err < 0) {
1043 			ldc_unmap(lp, cookies, nc);
1044 			return err;
1045 		}
1046 		nc += err;
1047 	}
1048 	return nc;
1049 }
1050 
1051 static inline struct sk_buff *vnet_skb_shape(struct sk_buff *skb, int ncookies)
1052 {
1053 	struct sk_buff *nskb;
1054 	int i, len, pad, docopy;
1055 
1056 	len = skb->len;
1057 	pad = 0;
1058 	if (len < ETH_ZLEN) {
1059 		pad += ETH_ZLEN - skb->len;
1060 		len += pad;
1061 	}
1062 	len += VNET_PACKET_SKIP;
1063 	pad += 8 - (len & 7);
1064 
1065 	/* make sure we have enough cookies and alignment in every frag */
1066 	docopy = skb_shinfo(skb)->nr_frags >= ncookies;
1067 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1068 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1069 
1070 		docopy |= f->page_offset & 7;
1071 	}
1072 	if (((unsigned long)skb->data & 7) != VNET_PACKET_SKIP ||
1073 	    skb_tailroom(skb) < pad ||
1074 	    skb_headroom(skb) < VNET_PACKET_SKIP || docopy) {
1075 		int start = 0, offset;
1076 		__wsum csum;
1077 
1078 		len = skb->len > ETH_ZLEN ? skb->len : ETH_ZLEN;
1079 		nskb = alloc_and_align_skb(skb->dev, len);
1080 		if (!nskb) {
1081 			dev_kfree_skb(skb);
1082 			return NULL;
1083 		}
1084 		skb_reserve(nskb, VNET_PACKET_SKIP);
1085 
1086 		nskb->protocol = skb->protocol;
1087 		offset = skb_mac_header(skb) - skb->data;
1088 		skb_set_mac_header(nskb, offset);
1089 		offset = skb_network_header(skb) - skb->data;
1090 		skb_set_network_header(nskb, offset);
1091 		offset = skb_transport_header(skb) - skb->data;
1092 		skb_set_transport_header(nskb, offset);
1093 
1094 		offset = 0;
1095 		nskb->csum_offset = skb->csum_offset;
1096 		nskb->ip_summed = skb->ip_summed;
1097 
1098 		if (skb->ip_summed == CHECKSUM_PARTIAL)
1099 			start = skb_checksum_start_offset(skb);
1100 		if (start) {
1101 			struct iphdr *iph = ip_hdr(nskb);
1102 			int offset = start + nskb->csum_offset;
1103 
1104 			if (skb_copy_bits(skb, 0, nskb->data, start)) {
1105 				dev_kfree_skb(nskb);
1106 				dev_kfree_skb(skb);
1107 				return NULL;
1108 			}
1109 			*(__sum16 *)(skb->data + offset) = 0;
1110 			csum = skb_copy_and_csum_bits(skb, start,
1111 						      nskb->data + start,
1112 						      skb->len - start, 0);
1113 			if (iph->protocol == IPPROTO_TCP ||
1114 			    iph->protocol == IPPROTO_UDP) {
1115 				csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
1116 							 skb->len - start,
1117 							 iph->protocol, csum);
1118 			}
1119 			*(__sum16 *)(nskb->data + offset) = csum;
1120 
1121 			nskb->ip_summed = CHECKSUM_NONE;
1122 		} else if (skb_copy_bits(skb, 0, nskb->data, skb->len)) {
1123 			dev_kfree_skb(nskb);
1124 			dev_kfree_skb(skb);
1125 			return NULL;
1126 		}
1127 		(void)skb_put(nskb, skb->len);
1128 		if (skb_is_gso(skb)) {
1129 			skb_shinfo(nskb)->gso_size = skb_shinfo(skb)->gso_size;
1130 			skb_shinfo(nskb)->gso_type = skb_shinfo(skb)->gso_type;
1131 		}
1132 		nskb->queue_mapping = skb->queue_mapping;
1133 		dev_kfree_skb(skb);
1134 		skb = nskb;
1135 	}
1136 	return skb;
1137 }
1138 
1139 static int vnet_handle_offloads(struct vnet_port *port, struct sk_buff *skb,
1140 				struct vnet_port *(*vnet_tx_port)
1141 				(struct sk_buff *, struct net_device *))
1142 {
1143 	struct net_device *dev = VNET_PORT_TO_NET_DEVICE(port);
1144 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1145 	struct sk_buff *segs;
1146 	int maclen, datalen;
1147 	int status;
1148 	int gso_size, gso_type, gso_segs;
1149 	int hlen = skb_transport_header(skb) - skb_mac_header(skb);
1150 	int proto = IPPROTO_IP;
1151 
1152 	if (skb->protocol == htons(ETH_P_IP))
1153 		proto = ip_hdr(skb)->protocol;
1154 	else if (skb->protocol == htons(ETH_P_IPV6))
1155 		proto = ipv6_hdr(skb)->nexthdr;
1156 
1157 	if (proto == IPPROTO_TCP) {
1158 		hlen += tcp_hdr(skb)->doff * 4;
1159 	} else if (proto == IPPROTO_UDP) {
1160 		hlen += sizeof(struct udphdr);
1161 	} else {
1162 		pr_err("vnet_handle_offloads GSO with unknown transport "
1163 		       "protocol %d tproto %d\n", skb->protocol, proto);
1164 		hlen = 128; /* XXX */
1165 	}
1166 	datalen = port->tsolen - hlen;
1167 
1168 	gso_size = skb_shinfo(skb)->gso_size;
1169 	gso_type = skb_shinfo(skb)->gso_type;
1170 	gso_segs = skb_shinfo(skb)->gso_segs;
1171 
1172 	if (port->tso && gso_size < datalen)
1173 		gso_segs = DIV_ROUND_UP(skb->len - hlen, datalen);
1174 
1175 	if (unlikely(vnet_tx_dring_avail(dr) < gso_segs)) {
1176 		struct netdev_queue *txq;
1177 
1178 		txq  = netdev_get_tx_queue(dev, port->q_index);
1179 		netif_tx_stop_queue(txq);
1180 		if (vnet_tx_dring_avail(dr) < skb_shinfo(skb)->gso_segs)
1181 			return NETDEV_TX_BUSY;
1182 		netif_tx_wake_queue(txq);
1183 	}
1184 
1185 	maclen = skb_network_header(skb) - skb_mac_header(skb);
1186 	skb_pull(skb, maclen);
1187 
1188 	if (port->tso && gso_size < datalen) {
1189 		if (skb_unclone(skb, GFP_ATOMIC))
1190 			goto out_dropped;
1191 
1192 		/* segment to TSO size */
1193 		skb_shinfo(skb)->gso_size = datalen;
1194 		skb_shinfo(skb)->gso_segs = gso_segs;
1195 	}
1196 	segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
1197 	if (IS_ERR(segs))
1198 		goto out_dropped;
1199 
1200 	skb_push(skb, maclen);
1201 	skb_reset_mac_header(skb);
1202 
1203 	status = 0;
1204 	while (segs) {
1205 		struct sk_buff *curr = segs;
1206 
1207 		segs = segs->next;
1208 		curr->next = NULL;
1209 		if (port->tso && curr->len > dev->mtu) {
1210 			skb_shinfo(curr)->gso_size = gso_size;
1211 			skb_shinfo(curr)->gso_type = gso_type;
1212 			skb_shinfo(curr)->gso_segs =
1213 				DIV_ROUND_UP(curr->len - hlen, gso_size);
1214 		} else {
1215 			skb_shinfo(curr)->gso_size = 0;
1216 		}
1217 
1218 		skb_push(curr, maclen);
1219 		skb_reset_mac_header(curr);
1220 		memcpy(skb_mac_header(curr), skb_mac_header(skb),
1221 		       maclen);
1222 		curr->csum_start = skb_transport_header(curr) - curr->head;
1223 		if (ip_hdr(curr)->protocol == IPPROTO_TCP)
1224 			curr->csum_offset = offsetof(struct tcphdr, check);
1225 		else if (ip_hdr(curr)->protocol == IPPROTO_UDP)
1226 			curr->csum_offset = offsetof(struct udphdr, check);
1227 
1228 		if (!(status & NETDEV_TX_MASK))
1229 			status = sunvnet_start_xmit_common(curr, dev,
1230 							   vnet_tx_port);
1231 		if (status & NETDEV_TX_MASK)
1232 			dev_kfree_skb_any(curr);
1233 	}
1234 
1235 	if (!(status & NETDEV_TX_MASK))
1236 		dev_kfree_skb_any(skb);
1237 	return status;
1238 out_dropped:
1239 	dev->stats.tx_dropped++;
1240 	dev_kfree_skb_any(skb);
1241 	return NETDEV_TX_OK;
1242 }
1243 
1244 int sunvnet_start_xmit_common(struct sk_buff *skb, struct net_device *dev,
1245 			      struct vnet_port *(*vnet_tx_port)
1246 			      (struct sk_buff *, struct net_device *))
1247 {
1248 	struct vnet_port *port = NULL;
1249 	struct vio_dring_state *dr;
1250 	struct vio_net_desc *d;
1251 	unsigned int len;
1252 	struct sk_buff *freeskbs = NULL;
1253 	int i, err, txi;
1254 	unsigned pending = 0;
1255 	struct netdev_queue *txq;
1256 
1257 	rcu_read_lock();
1258 	port = vnet_tx_port(skb, dev);
1259 	if (unlikely(!port)) {
1260 		rcu_read_unlock();
1261 		goto out_dropped;
1262 	}
1263 
1264 	if (skb_is_gso(skb) && skb->len > port->tsolen) {
1265 		err = vnet_handle_offloads(port, skb, vnet_tx_port);
1266 		rcu_read_unlock();
1267 		return err;
1268 	}
1269 
1270 	if (!skb_is_gso(skb) && skb->len > port->rmtu) {
1271 		unsigned long localmtu = port->rmtu - ETH_HLEN;
1272 
1273 		if (vio_version_after_eq(&port->vio, 1, 3))
1274 			localmtu -= VLAN_HLEN;
1275 
1276 		if (skb->protocol == htons(ETH_P_IP)) {
1277 			struct flowi4 fl4;
1278 			struct rtable *rt = NULL;
1279 
1280 			memset(&fl4, 0, sizeof(fl4));
1281 			fl4.flowi4_oif = dev->ifindex;
1282 			fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
1283 			fl4.daddr = ip_hdr(skb)->daddr;
1284 			fl4.saddr = ip_hdr(skb)->saddr;
1285 
1286 			rt = ip_route_output_key(dev_net(dev), &fl4);
1287 			rcu_read_unlock();
1288 			if (!IS_ERR(rt)) {
1289 				skb_dst_set(skb, &rt->dst);
1290 				icmp_send(skb, ICMP_DEST_UNREACH,
1291 					  ICMP_FRAG_NEEDED,
1292 					  htonl(localmtu));
1293 			}
1294 		}
1295 #if IS_ENABLED(CONFIG_IPV6)
1296 		else if (skb->protocol == htons(ETH_P_IPV6))
1297 			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
1298 #endif
1299 		goto out_dropped;
1300 	}
1301 
1302 	skb = vnet_skb_shape(skb, 2);
1303 
1304 	if (unlikely(!skb))
1305 		goto out_dropped;
1306 
1307 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1308 		vnet_fullcsum(skb);
1309 
1310 	dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1311 	i = skb_get_queue_mapping(skb);
1312 	txq = netdev_get_tx_queue(dev, i);
1313 	if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
1314 		if (!netif_tx_queue_stopped(txq)) {
1315 			netif_tx_stop_queue(txq);
1316 
1317 			/* This is a hard error, log it. */
1318 			netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1319 			dev->stats.tx_errors++;
1320 		}
1321 		rcu_read_unlock();
1322 		return NETDEV_TX_BUSY;
1323 	}
1324 
1325 	d = vio_dring_cur(dr);
1326 
1327 	txi = dr->prod;
1328 
1329 	freeskbs = vnet_clean_tx_ring(port, &pending);
1330 
1331 	BUG_ON(port->tx_bufs[txi].skb);
1332 
1333 	len = skb->len;
1334 	if (len < ETH_ZLEN)
1335 		len = ETH_ZLEN;
1336 
1337 	err = vnet_skb_map(port->vio.lp, skb, port->tx_bufs[txi].cookies, 2,
1338 			   (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
1339 	if (err < 0) {
1340 		netdev_info(dev, "tx buffer map error %d\n", err);
1341 		goto out_dropped;
1342 	}
1343 
1344 	port->tx_bufs[txi].skb = skb;
1345 	skb = NULL;
1346 	port->tx_bufs[txi].ncookies = err;
1347 
1348 	/* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
1349 	 * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
1350 	 * the protocol itself does not require it as long as the peer
1351 	 * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
1352 	 *
1353 	 * An ACK for every packet in the ring is expensive as the
1354 	 * sending of LDC messages is slow and affects performance.
1355 	 */
1356 	d->hdr.ack = VIO_ACK_DISABLE;
1357 	d->size = len;
1358 	d->ncookies = port->tx_bufs[txi].ncookies;
1359 	for (i = 0; i < d->ncookies; i++)
1360 		d->cookies[i] = port->tx_bufs[txi].cookies[i];
1361 	if (vio_version_after_eq(&port->vio, 1, 7)) {
1362 		struct vio_net_dext *dext = vio_net_ext(d);
1363 
1364 		memset(dext, 0, sizeof(*dext));
1365 		if (skb_is_gso(port->tx_bufs[txi].skb)) {
1366 			dext->ipv4_lso_mss = skb_shinfo(port->tx_bufs[txi].skb)
1367 					     ->gso_size;
1368 			dext->flags |= VNET_PKT_IPV4_LSO;
1369 		}
1370 		if (vio_version_after_eq(&port->vio, 1, 8) &&
1371 		    !port->switch_port) {
1372 			dext->flags |= VNET_PKT_HCK_IPV4_HDRCKSUM_OK;
1373 			dext->flags |= VNET_PKT_HCK_FULLCKSUM_OK;
1374 		}
1375 	}
1376 
1377 	/* This has to be a non-SMP write barrier because we are writing
1378 	 * to memory which is shared with the peer LDOM.
1379 	 */
1380 	dma_wmb();
1381 
1382 	d->hdr.state = VIO_DESC_READY;
1383 
1384 	/* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
1385 	 * to notify the consumer that some descriptors are READY.
1386 	 * After that "start" trigger, no additional triggers are needed until
1387 	 * a DRING_STOPPED is received from the consumer. The dr->cons field
1388 	 * (set up by vnet_ack()) has the value of the next dring index
1389 	 * that has not yet been ack-ed. We send a "start" trigger here
1390 	 * if, and only if, start_cons is true (reset it afterward). Conversely,
1391 	 * vnet_ack() should check if the dring corresponding to cons
1392 	 * is marked READY, but start_cons was false.
1393 	 * If so, vnet_ack() should send out the missed "start" trigger.
1394 	 *
1395 	 * Note that the dma_wmb() above makes sure the cookies et al. are
1396 	 * not globally visible before the VIO_DESC_READY, and that the
1397 	 * stores are ordered correctly by the compiler. The consumer will
1398 	 * not proceed until the VIO_DESC_READY is visible assuring that
1399 	 * the consumer does not observe anything related to descriptors
1400 	 * out of order. The HV trap from the LDC start trigger is the
1401 	 * producer to consumer announcement that work is available to the
1402 	 * consumer
1403 	 */
1404 	if (!port->start_cons) { /* previous trigger suffices */
1405 		trace_vnet_skip_tx_trigger(port->vio._local_sid,
1406 					   port->vio._peer_sid, dr->cons);
1407 		goto ldc_start_done;
1408 	}
1409 
1410 	err = __vnet_tx_trigger(port, dr->cons);
1411 	if (unlikely(err < 0)) {
1412 		netdev_info(dev, "TX trigger error %d\n", err);
1413 		d->hdr.state = VIO_DESC_FREE;
1414 		skb = port->tx_bufs[txi].skb;
1415 		port->tx_bufs[txi].skb = NULL;
1416 		dev->stats.tx_carrier_errors++;
1417 		goto out_dropped;
1418 	}
1419 
1420 ldc_start_done:
1421 	port->start_cons = false;
1422 
1423 	dev->stats.tx_packets++;
1424 	dev->stats.tx_bytes += port->tx_bufs[txi].skb->len;
1425 
1426 	dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
1427 	if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
1428 		netif_tx_stop_queue(txq);
1429 		if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
1430 			netif_tx_wake_queue(txq);
1431 	}
1432 
1433 	(void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
1434 	rcu_read_unlock();
1435 
1436 	vnet_free_skbs(freeskbs);
1437 
1438 	return NETDEV_TX_OK;
1439 
1440 out_dropped:
1441 	if (pending)
1442 		(void)mod_timer(&port->clean_timer,
1443 				jiffies + VNET_CLEAN_TIMEOUT);
1444 	else if (port)
1445 		del_timer(&port->clean_timer);
1446 	if (port)
1447 		rcu_read_unlock();
1448 	if (skb)
1449 		dev_kfree_skb(skb);
1450 	vnet_free_skbs(freeskbs);
1451 	dev->stats.tx_dropped++;
1452 	return NETDEV_TX_OK;
1453 }
1454 EXPORT_SYMBOL_GPL(sunvnet_start_xmit_common);
1455 
1456 void sunvnet_tx_timeout_common(struct net_device *dev)
1457 {
1458 	/* XXX Implement me XXX */
1459 }
1460 EXPORT_SYMBOL_GPL(sunvnet_tx_timeout_common);
1461 
1462 int sunvnet_open_common(struct net_device *dev)
1463 {
1464 	netif_carrier_on(dev);
1465 	netif_tx_start_all_queues(dev);
1466 
1467 	return 0;
1468 }
1469 EXPORT_SYMBOL_GPL(sunvnet_open_common);
1470 
1471 int sunvnet_close_common(struct net_device *dev)
1472 {
1473 	netif_tx_stop_all_queues(dev);
1474 	netif_carrier_off(dev);
1475 
1476 	return 0;
1477 }
1478 EXPORT_SYMBOL_GPL(sunvnet_close_common);
1479 
1480 static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
1481 {
1482 	struct vnet_mcast_entry *m;
1483 
1484 	for (m = vp->mcast_list; m; m = m->next) {
1485 		if (ether_addr_equal(m->addr, addr))
1486 			return m;
1487 	}
1488 	return NULL;
1489 }
1490 
1491 static void __update_mc_list(struct vnet *vp, struct net_device *dev)
1492 {
1493 	struct netdev_hw_addr *ha;
1494 
1495 	netdev_for_each_mc_addr(ha, dev) {
1496 		struct vnet_mcast_entry *m;
1497 
1498 		m = __vnet_mc_find(vp, ha->addr);
1499 		if (m) {
1500 			m->hit = 1;
1501 			continue;
1502 		}
1503 
1504 		if (!m) {
1505 			m = kzalloc(sizeof(*m), GFP_ATOMIC);
1506 			if (!m)
1507 				continue;
1508 			memcpy(m->addr, ha->addr, ETH_ALEN);
1509 			m->hit = 1;
1510 
1511 			m->next = vp->mcast_list;
1512 			vp->mcast_list = m;
1513 		}
1514 	}
1515 }
1516 
1517 static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
1518 {
1519 	struct vio_net_mcast_info info;
1520 	struct vnet_mcast_entry *m, **pp;
1521 	int n_addrs;
1522 
1523 	memset(&info, 0, sizeof(info));
1524 
1525 	info.tag.type = VIO_TYPE_CTRL;
1526 	info.tag.stype = VIO_SUBTYPE_INFO;
1527 	info.tag.stype_env = VNET_MCAST_INFO;
1528 	info.tag.sid = vio_send_sid(&port->vio);
1529 	info.set = 1;
1530 
1531 	n_addrs = 0;
1532 	for (m = vp->mcast_list; m; m = m->next) {
1533 		if (m->sent)
1534 			continue;
1535 		m->sent = 1;
1536 		memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1537 		       m->addr, ETH_ALEN);
1538 		if (++n_addrs == VNET_NUM_MCAST) {
1539 			info.count = n_addrs;
1540 
1541 			(void)vio_ldc_send(&port->vio, &info,
1542 					   sizeof(info));
1543 			n_addrs = 0;
1544 		}
1545 	}
1546 	if (n_addrs) {
1547 		info.count = n_addrs;
1548 		(void)vio_ldc_send(&port->vio, &info, sizeof(info));
1549 	}
1550 
1551 	info.set = 0;
1552 
1553 	n_addrs = 0;
1554 	pp = &vp->mcast_list;
1555 	while ((m = *pp) != NULL) {
1556 		if (m->hit) {
1557 			m->hit = 0;
1558 			pp = &m->next;
1559 			continue;
1560 		}
1561 
1562 		memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1563 		       m->addr, ETH_ALEN);
1564 		if (++n_addrs == VNET_NUM_MCAST) {
1565 			info.count = n_addrs;
1566 			(void)vio_ldc_send(&port->vio, &info,
1567 					   sizeof(info));
1568 			n_addrs = 0;
1569 		}
1570 
1571 		*pp = m->next;
1572 		kfree(m);
1573 	}
1574 	if (n_addrs) {
1575 		info.count = n_addrs;
1576 		(void)vio_ldc_send(&port->vio, &info, sizeof(info));
1577 	}
1578 }
1579 
1580 void sunvnet_set_rx_mode_common(struct net_device *dev, struct vnet *vp)
1581 {
1582 	struct vnet_port *port;
1583 
1584 	rcu_read_lock();
1585 	list_for_each_entry_rcu(port, &vp->port_list, list) {
1586 		if (port->switch_port) {
1587 			__update_mc_list(vp, dev);
1588 			__send_mc_list(vp, port);
1589 			break;
1590 		}
1591 	}
1592 	rcu_read_unlock();
1593 }
1594 EXPORT_SYMBOL_GPL(sunvnet_set_rx_mode_common);
1595 
1596 int sunvnet_set_mac_addr_common(struct net_device *dev, void *p)
1597 {
1598 	return -EINVAL;
1599 }
1600 EXPORT_SYMBOL_GPL(sunvnet_set_mac_addr_common);
1601 
1602 void sunvnet_port_free_tx_bufs_common(struct vnet_port *port)
1603 {
1604 	struct vio_dring_state *dr;
1605 	int i;
1606 
1607 	dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1608 
1609 	if (!dr->base)
1610 		return;
1611 
1612 	for (i = 0; i < VNET_TX_RING_SIZE; i++) {
1613 		struct vio_net_desc *d;
1614 		void *skb = port->tx_bufs[i].skb;
1615 
1616 		if (!skb)
1617 			continue;
1618 
1619 		d = vio_dring_entry(dr, i);
1620 
1621 		ldc_unmap(port->vio.lp,
1622 			  port->tx_bufs[i].cookies,
1623 			  port->tx_bufs[i].ncookies);
1624 		dev_kfree_skb(skb);
1625 		port->tx_bufs[i].skb = NULL;
1626 		d->hdr.state = VIO_DESC_FREE;
1627 	}
1628 	ldc_free_exp_dring(port->vio.lp, dr->base,
1629 			   (dr->entry_size * dr->num_entries),
1630 			   dr->cookies, dr->ncookies);
1631 	dr->base = NULL;
1632 	dr->entry_size = 0;
1633 	dr->num_entries = 0;
1634 	dr->pending = 0;
1635 	dr->ncookies = 0;
1636 }
1637 EXPORT_SYMBOL_GPL(sunvnet_port_free_tx_bufs_common);
1638 
1639 static void vnet_port_reset(struct vnet_port *port)
1640 {
1641 	del_timer(&port->clean_timer);
1642 	sunvnet_port_free_tx_bufs_common(port);
1643 	port->rmtu = 0;
1644 	port->tso = true;
1645 	port->tsolen = 0;
1646 }
1647 
1648 static int vnet_port_alloc_tx_ring(struct vnet_port *port)
1649 {
1650 	struct vio_dring_state *dr;
1651 	unsigned long len, elen;
1652 	int i, err, ncookies;
1653 	void *dring;
1654 
1655 	dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1656 
1657 	elen = sizeof(struct vio_net_desc) +
1658 	       sizeof(struct ldc_trans_cookie) * 2;
1659 	if (vio_version_after_eq(&port->vio, 1, 7))
1660 		elen += sizeof(struct vio_net_dext);
1661 	len = VNET_TX_RING_SIZE * elen;
1662 
1663 	ncookies = VIO_MAX_RING_COOKIES;
1664 	dring = ldc_alloc_exp_dring(port->vio.lp, len,
1665 				    dr->cookies, &ncookies,
1666 				    (LDC_MAP_SHADOW |
1667 				     LDC_MAP_DIRECT |
1668 				     LDC_MAP_RW));
1669 	if (IS_ERR(dring)) {
1670 		err = PTR_ERR(dring);
1671 		goto err_out;
1672 	}
1673 
1674 	dr->base = dring;
1675 	dr->entry_size = elen;
1676 	dr->num_entries = VNET_TX_RING_SIZE;
1677 	dr->prod = 0;
1678 	dr->cons = 0;
1679 	port->start_cons  = true; /* need an initial trigger */
1680 	dr->pending = VNET_TX_RING_SIZE;
1681 	dr->ncookies = ncookies;
1682 
1683 	for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
1684 		struct vio_net_desc *d;
1685 
1686 		d = vio_dring_entry(dr, i);
1687 		d->hdr.state = VIO_DESC_FREE;
1688 	}
1689 	return 0;
1690 
1691 err_out:
1692 	sunvnet_port_free_tx_bufs_common(port);
1693 
1694 	return err;
1695 }
1696 
1697 #ifdef CONFIG_NET_POLL_CONTROLLER
1698 void sunvnet_poll_controller_common(struct net_device *dev, struct vnet *vp)
1699 {
1700 	struct vnet_port *port;
1701 	unsigned long flags;
1702 
1703 	spin_lock_irqsave(&vp->lock, flags);
1704 	if (!list_empty(&vp->port_list)) {
1705 		port = list_entry(vp->port_list.next, struct vnet_port, list);
1706 		napi_schedule(&port->napi);
1707 	}
1708 	spin_unlock_irqrestore(&vp->lock, flags);
1709 }
1710 EXPORT_SYMBOL_GPL(sunvnet_poll_controller_common);
1711 #endif
1712 
1713 void sunvnet_port_add_txq_common(struct vnet_port *port)
1714 {
1715 	struct vnet *vp = port->vp;
1716 	int n;
1717 
1718 	n = vp->nports++;
1719 	n = n & (VNET_MAX_TXQS - 1);
1720 	port->q_index = n;
1721 	netif_tx_wake_queue(netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port),
1722 						port->q_index));
1723 }
1724 EXPORT_SYMBOL_GPL(sunvnet_port_add_txq_common);
1725 
1726 void sunvnet_port_rm_txq_common(struct vnet_port *port)
1727 {
1728 	port->vp->nports--;
1729 	netif_tx_stop_queue(netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port),
1730 						port->q_index));
1731 }
1732 EXPORT_SYMBOL_GPL(sunvnet_port_rm_txq_common);
1733