sunvnet.c 49.1 KB
Newer Older
1
2
/* sunvnet.c: Sun LDOM Virtual Network Driver.
 *
3
 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4
5
 */

6
7
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

8
9
10
11
12
13
14
15
16
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/etherdevice.h>
17
#include <linux/mutex.h>
18
#include <linux/highmem.h>
19
#include <linux/if_vlan.h>
20

21
22
23
24
#if IS_ENABLED(CONFIG_IPV6)
#include <linux/icmpv6.h>
#endif

25
#include <net/ip.h>
26
27
28
#include <net/icmp.h>
#include <net/route.h>

29
30
31
32
33
34
35
36
37
#include <asm/vio.h>
#include <asm/ldc.h>

#include "sunvnet.h"

#define DRV_MODULE_NAME		"sunvnet"
#define DRV_MODULE_VERSION	"1.0"
#define DRV_MODULE_RELDATE	"June 25, 2007"

38
static char version[] =
39
40
41
42
43
44
	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
MODULE_DESCRIPTION("Sun LDOM virtual network driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_MODULE_VERSION);

45
46
#define	VNET_MAX_TXQS		16

47
48
49
50
51
/* Heuristic for the number of times to exponentially backoff and
 * retry sending an LDC trigger when EAGAIN is encountered
 */
#define	VNET_MAX_RETRIES	10

52
static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
53
static void vnet_port_reset(struct vnet_port *port);
54

55
56
/* Ordered from largest major to lowest */
static struct vio_version vnet_versions[] = {
57
58
	{ .major = 1, .minor = 8 },
	{ .major = 1, .minor = 7 },
59
	{ .major = 1, .minor = 6 },
60
61
62
63
64
65
66
67
68
69
70
71
	{ .major = 1, .minor = 0 },
};

static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
{
	return vio_dring_avail(dr, VNET_TX_RING_SIZE);
}

static int vnet_handle_unknown(struct vnet_port *port, void *arg)
{
	struct vio_msg_tag *pkt = arg;

72
	pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
73
	       pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
74
	pr_err("Resetting connection\n");
75
76
77
78
79
80

	ldc_disconnect(port->vio.lp);

	return -ECONNRESET;
}

81
82
static int vnet_port_alloc_tx_ring(struct vnet_port *port);

83
84
85
86
87
static int vnet_send_attr(struct vio_driver_state *vio)
{
	struct vnet_port *port = to_vnet_port(vio);
	struct net_device *dev = port->vp->dev;
	struct vio_net_attr_info pkt;
88
	int framelen = ETH_FRAME_LEN;
89
90
91
92
93
	int i, err;

	err = vnet_port_alloc_tx_ring(to_vnet_port(vio));
	if (err)
		return err;
94
95
96
97
98
99

	memset(&pkt, 0, sizeof(pkt));
	pkt.tag.type = VIO_TYPE_CTRL;
	pkt.tag.stype = VIO_SUBTYPE_INFO;
	pkt.tag.stype_env = VIO_ATTR_INFO;
	pkt.tag.sid = vio_send_sid(vio);
100
101
102
103
	if (vio_version_before(vio, 1, 2))
		pkt.xfer_mode = VIO_DRING_MODE;
	else
		pkt.xfer_mode = VIO_NEW_DRING_MODE;
104
105
106
107
	pkt.addr_type = VNET_ADDR_ETHERMAC;
	pkt.ack_freq = 0;
	for (i = 0; i < 6; i++)
		pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	if (vio_version_after(vio, 1, 3)) {
		if (port->rmtu) {
			port->rmtu = min(VNET_MAXPACKET, port->rmtu);
			pkt.mtu = port->rmtu;
		} else {
			port->rmtu = VNET_MAXPACKET;
			pkt.mtu = port->rmtu;
		}
		if (vio_version_after_eq(vio, 1, 6))
			pkt.options = VIO_TX_DRING;
	} else if (vio_version_before(vio, 1, 3)) {
		pkt.mtu = framelen;
	} else { /* v1.3 */
		pkt.mtu = framelen + VLAN_HLEN;
	}

	pkt.cflags = 0;
David L Stevens's avatar
David L Stevens committed
125
126
127
128
129
130
131
132
	if (vio_version_after_eq(vio, 1, 7) && port->tso) {
		pkt.cflags |= VNET_LSO_IPV4_CAPAB;
		if (!port->tsolen)
			port->tsolen = VNET_MAXTSO;
		pkt.ipv4_lso_maxlen = port->tsolen;
	}

	pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
133
134

	viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
135
136
	       "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
	       "cflags[0x%04x] lso_max[%u]\n",
137
	       pkt.xfer_mode, pkt.addr_type,
138
139
140
141
	       (unsigned long long)pkt.addr,
	       pkt.ack_freq, pkt.plnk_updt, pkt.options,
	       (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);

142
143
144
145
146
147
148

	return vio_ldc_send(vio, &pkt, sizeof(pkt));
}

static int handle_attr_info(struct vio_driver_state *vio,
			    struct vio_net_attr_info *pkt)
{
149
150
151
152
153
154
155
	struct vnet_port *port = to_vnet_port(vio);
	u64	localmtu;
	u8	xfer_mode;

	viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
	       "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
	       " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
156
	       pkt->xfer_mode, pkt->addr_type,
157
158
159
160
	       (unsigned long long)pkt->addr,
	       pkt->ack_freq, pkt->plnk_updt, pkt->options,
	       (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
	       pkt->ipv4_lso_maxlen);
161
162
163

	pkt->tag.sid = vio_send_sid(vio);

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	xfer_mode = pkt->xfer_mode;
	/* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
	if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
		xfer_mode = VIO_NEW_DRING_MODE;

	/* MTU negotiation:
	 *	< v1.3 - ETH_FRAME_LEN exactly
	 *	> v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
	 *			pkt->mtu for ACK
	 *	= v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
	 */
	if (vio_version_before(vio, 1, 3)) {
		localmtu = ETH_FRAME_LEN;
	} else if (vio_version_after(vio, 1, 3)) {
		localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
		localmtu = min(pkt->mtu, localmtu);
		pkt->mtu = localmtu;
	} else { /* v1.3 */
		localmtu = ETH_FRAME_LEN + VLAN_HLEN;
	}
	port->rmtu = localmtu;

David L Stevens's avatar
David L Stevens committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	/* LSO negotiation */
	if (vio_version_after_eq(vio, 1, 7))
		port->tso &= !!(pkt->cflags & VNET_LSO_IPV4_CAPAB);
	else
		port->tso = false;
	if (port->tso) {
		if (!port->tsolen)
			port->tsolen = VNET_MAXTSO;
		port->tsolen = min(port->tsolen, pkt->ipv4_lso_maxlen);
		if (port->tsolen < VNET_MINTSO) {
			port->tso = false;
			port->tsolen = 0;
			pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
		}
		pkt->ipv4_lso_maxlen = port->tsolen;
	} else {
		pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
		pkt->ipv4_lso_maxlen = 0;
	}

206
207
208
209
210
211
212
	/* for version >= 1.6, ACK packet mode we support */
	if (vio_version_after_eq(vio, 1, 6)) {
		pkt->xfer_mode = VIO_NEW_DRING_MODE;
		pkt->options = VIO_TX_DRING;
	}

	if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
213
	    pkt->addr_type != VNET_ADDR_ETHERMAC ||
214
	    pkt->mtu != localmtu) {
215
216
217
218
219
220
221
222
		viodbg(HS, "SEND NET ATTR NACK\n");

		pkt->tag.stype = VIO_SUBTYPE_NACK;

		(void) vio_ldc_send(vio, pkt, sizeof(*pkt));

		return -ECONNRESET;
	} else {
223
224
225
226
227
228
229
230
		viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
		       "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
		       "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
		       pkt->xfer_mode, pkt->addr_type,
		       (unsigned long long)pkt->addr,
		       pkt->ack_freq, pkt->plnk_updt, pkt->options,
		       (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
		       pkt->ipv4_lso_maxlen);
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

		pkt->tag.stype = VIO_SUBTYPE_ACK;

		return vio_ldc_send(vio, pkt, sizeof(*pkt));
	}

}

static int handle_attr_ack(struct vio_driver_state *vio,
			   struct vio_net_attr_info *pkt)
{
	viodbg(HS, "GOT NET ATTR ACK\n");

	return 0;
}

static int handle_attr_nack(struct vio_driver_state *vio,
			    struct vio_net_attr_info *pkt)
{
	viodbg(HS, "GOT NET ATTR NACK\n");

	return -ECONNRESET;
}

static int vnet_handle_attr(struct vio_driver_state *vio, void *arg)
{
	struct vio_net_attr_info *pkt = arg;

	switch (pkt->tag.stype) {
	case VIO_SUBTYPE_INFO:
		return handle_attr_info(vio, pkt);

	case VIO_SUBTYPE_ACK:
		return handle_attr_ack(vio, pkt);

	case VIO_SUBTYPE_NACK:
		return handle_attr_nack(vio, pkt);

	default:
		return -ECONNRESET;
	}
}

static void vnet_handshake_complete(struct vio_driver_state *vio)
{
	struct vio_dring_state *dr;

	dr = &vio->drings[VIO_DRIVER_RX_RING];
	dr->snd_nxt = dr->rcv_nxt = 1;

	dr = &vio->drings[VIO_DRIVER_TX_RING];
	dr->snd_nxt = dr->rcv_nxt = 1;
}

/* The hypervisor interface that implements copying to/from imported
 * memory from another domain requires that copies are done to 8-byte
 * aligned buffers, and that the lengths of such copies are also 8-byte
 * multiples.
 *
 * So we align skb->data to an 8-byte multiple and pad-out the data
 * area so we can round the copy length up to the next multiple of
 * 8 for the copy.
 *
 * The transmitter puts the actual start of the packet 6 bytes into
 * the buffer it sends over, so that the IP headers after the ethernet
 * header are aligned properly.  These 6 bytes are not in the descriptor
 * length, they are simply implied.  This offset is represented using
 * the VNET_PACKET_SKIP macro.
 */
static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
					   unsigned int len)
{
	struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8);
	unsigned long addr, off;

	if (unlikely(!skb))
		return NULL;

	addr = (unsigned long) skb->data;
	off = ((addr + 7UL) & ~7UL) - addr;
	if (off)
		skb_reserve(skb, off);

	return skb;
}

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
static inline void vnet_fullcsum(struct sk_buff *skb)
{
	struct iphdr *iph = ip_hdr(skb);
	int offset = skb_transport_offset(skb);

	if (skb->protocol != htons(ETH_P_IP))
		return;
	if (iph->protocol != IPPROTO_TCP &&
	    iph->protocol != IPPROTO_UDP)
		return;
	skb->ip_summed = CHECKSUM_NONE;
	skb->csum_level = 1;
	skb->csum = 0;
	if (iph->protocol == IPPROTO_TCP) {
		struct tcphdr *ptcp = tcp_hdr(skb);

		ptcp->check = 0;
		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
		ptcp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
						skb->len - offset, IPPROTO_TCP,
						skb->csum);
	} else if (iph->protocol == IPPROTO_UDP) {
		struct udphdr *pudp = udp_hdr(skb);

		pudp->check = 0;
		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
		pudp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
						skb->len - offset, IPPROTO_UDP,
						skb->csum);
	}
}

static int vnet_rx_one(struct vnet_port *port, struct vio_net_desc *desc)
350
351
{
	struct net_device *dev = port->vp->dev;
352
	unsigned int len = desc->size;
353
354
	unsigned int copy_len;
	struct sk_buff *skb;
355
	int maxlen;
356
357
358
	int err;

	err = -EMSGSIZE;
359
360
361
362
363
	if (port->tso && port->tsolen > port->rmtu)
		maxlen = port->tsolen;
	else
		maxlen = port->rmtu;
	if (unlikely(len < ETH_ZLEN || len > maxlen)) {
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
		dev->stats.rx_length_errors++;
		goto out_dropped;
	}

	skb = alloc_and_align_skb(dev, len);
	err = -ENOMEM;
	if (unlikely(!skb)) {
		dev->stats.rx_missed_errors++;
		goto out_dropped;
	}

	copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
	skb_put(skb, copy_len);
	err = ldc_copy(port->vio.lp, LDC_COPY_IN,
		       skb->data, copy_len, 0,
379
		       desc->cookies, desc->ncookies);
380
381
382
383
384
385
386
387
388
	if (unlikely(err < 0)) {
		dev->stats.rx_frame_errors++;
		goto out_free_skb;
	}

	skb_pull(skb, VNET_PACKET_SKIP);
	skb_trim(skb, len);
	skb->protocol = eth_type_trans(skb, dev);

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
	if (vio_version_after_eq(&port->vio, 1, 8)) {
		struct vio_net_dext *dext = vio_net_ext(desc);

		if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM) {
			if (skb->protocol == ETH_P_IP) {
				struct iphdr *iph = (struct iphdr *)skb->data;

				iph->check = 0;
				ip_send_check(iph);
			}
		}
		if ((dext->flags & VNET_PKT_HCK_FULLCKSUM) &&
		    skb->ip_summed == CHECKSUM_NONE)
			vnet_fullcsum(skb);
		if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM_OK) {
			skb->ip_summed = CHECKSUM_PARTIAL;
			skb->csum_level = 0;
			if (dext->flags & VNET_PKT_HCK_FULLCKSUM_OK)
				skb->csum_level = 1;
		}
	}

411
412
	skb->ip_summed = port->switch_port ? CHECKSUM_NONE : CHECKSUM_PARTIAL;

413
414
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += len;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
415
	napi_gro_receive(&port->napi, skb);
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	return 0;

out_free_skb:
	kfree_skb(skb);

out_dropped:
	dev->stats.rx_dropped++;
	return err;
}

static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
			 u32 start, u32 end, u8 vio_dring_state)
{
	struct vio_dring_data hdr = {
		.tag = {
			.type		= VIO_TYPE_DATA,
			.stype		= VIO_SUBTYPE_ACK,
			.stype_env	= VIO_DRING_DATA,
			.sid		= vio_send_sid(&port->vio),
		},
		.dring_ident		= dr->ident,
		.start_idx		= start,
		.end_idx		= end,
		.state			= vio_dring_state,
	};
	int err, delay;
442
	int retries = 0;
443
444
445
446
447
448
449
450
451
452
453
454

	hdr.seq = dr->snd_nxt;
	delay = 1;
	do {
		err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
		if (err > 0) {
			dr->snd_nxt++;
			break;
		}
		udelay(delay);
		if ((delay <<= 1) > 128)
			delay = 128;
455
456
457
458
459
		if (retries++ > VNET_MAX_RETRIES) {
			pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
				port->raddr[0], port->raddr[1],
				port->raddr[2], port->raddr[3],
				port->raddr[4], port->raddr[5]);
460
			break;
461
		}
462
463
	} while (err == -EAGAIN);

464
465
466
467
468
469
470
471
	if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
		port->stop_rx_idx = end;
		port->stop_rx = true;
	} else {
		port->stop_rx_idx = 0;
		port->stop_rx = false;
	}

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	return err;
}

static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
					struct vio_dring_state *dr,
					u32 index)
{
	struct vio_net_desc *desc = port->vio.desc_buf;
	int err;

	err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
				  (index * dr->entry_size),
				  dr->cookies, dr->ncookies);
	if (err < 0)
		return ERR_PTR(err);

	return desc;
}

static int put_rx_desc(struct vnet_port *port,
		       struct vio_dring_state *dr,
		       struct vio_net_desc *desc,
		       u32 index)
{
	int err;

	err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
				  (index * dr->entry_size),
				  dr->cookies, dr->ncookies);
	if (err < 0)
		return err;

	return 0;
}

static int vnet_walk_rx_one(struct vnet_port *port,
			    struct vio_dring_state *dr,
			    u32 index, int *needs_ack)
{
	struct vio_net_desc *desc = get_rx_desc(port, dr, index);
	struct vio_driver_state *vio = &port->vio;
	int err;

Sowmini Varadhan's avatar
Sowmini Varadhan committed
515
	BUG_ON(desc == NULL);
516
517
518
	if (IS_ERR(desc))
		return PTR_ERR(desc);

519
520
521
522
523
	if (desc->hdr.state != VIO_DESC_READY)
		return 1;

	rmb();

524
	viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
525
526
527
528
529
	       desc->hdr.state, desc->hdr.ack,
	       desc->size, desc->ncookies,
	       desc->cookies[0].cookie_addr,
	       desc->cookies[0].cookie_size);

530
	err = vnet_rx_one(port, desc);
531
532
533
534
535
536
537
538
539
540
541
	if (err == -ECONNRESET)
		return err;
	desc->hdr.state = VIO_DESC_DONE;
	err = put_rx_desc(port, dr, desc, index);
	if (err < 0)
		return err;
	*needs_ack = desc->hdr.ack;
	return 0;
}

static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
Sowmini Varadhan's avatar
Sowmini Varadhan committed
542
			u32 start, u32 end, int *npkts, int budget)
543
544
545
{
	struct vio_driver_state *vio = &port->vio;
	int ack_start = -1, ack_end = -1;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
546
	bool send_ack = true;
547

548
549
	end = (end == (u32) -1) ? vio_dring_prev(dr, start)
				: vio_dring_next(dr, end);
550
551
552
553
554
555
556
557
558

	viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);

	while (start != end) {
		int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
		if (err == -ECONNRESET)
			return err;
		if (err != 0)
			break;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
559
		(*npkts)++;
560
561
562
		if (ack_start == -1)
			ack_start = start;
		ack_end = start;
563
		start = vio_dring_next(dr, start);
564
565
566
567
568
569
570
		if (ack && start != end) {
			err = vnet_send_ack(port, dr, ack_start, ack_end,
					    VIO_DRING_ACTIVE);
			if (err == -ECONNRESET)
				return err;
			ack_start = -1;
		}
Sowmini Varadhan's avatar
Sowmini Varadhan committed
571
572
573
574
		if ((*npkts) >= budget) {
			send_ack = false;
			break;
		}
575
576
	}
	if (unlikely(ack_start == -1))
577
		ack_start = ack_end = vio_dring_prev(dr, start);
Sowmini Varadhan's avatar
Sowmini Varadhan committed
578
579
580
581
582
583
584
585
586
	if (send_ack) {
		port->napi_resume = false;
		return vnet_send_ack(port, dr, ack_start, ack_end,
				     VIO_DRING_STOPPED);
	} else  {
		port->napi_resume = true;
		port->napi_stop_idx = ack_end;
		return 1;
	}
587
588
}

Sowmini Varadhan's avatar
Sowmini Varadhan committed
589
590
static int vnet_rx(struct vnet_port *port, void *msgbuf, int *npkts,
		   int budget)
591
592
593
594
595
{
	struct vio_dring_data *pkt = msgbuf;
	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
	struct vio_driver_state *vio = &port->vio;

596
	viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
597
598
599
600
601
	       pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);

	if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
		return 0;
	if (unlikely(pkt->seq != dr->rcv_nxt)) {
602
603
		pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
		       pkt->seq, dr->rcv_nxt);
604
605
606
		return 0;
	}

Sowmini Varadhan's avatar
Sowmini Varadhan committed
607
608
	if (!port->napi_resume)
		dr->rcv_nxt++;
609
610
611

	/* XXX Validate pkt->start_idx and pkt->end_idx XXX */

Sowmini Varadhan's avatar
Sowmini Varadhan committed
612
613
	return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx,
			    npkts, budget);
614
615
616
617
618
619
620
621
622
623
624
625
}

static int idx_is_pending(struct vio_dring_state *dr, u32 end)
{
	u32 idx = dr->cons;
	int found = 0;

	while (idx != dr->prod) {
		if (idx == end) {
			found = 1;
			break;
		}
626
		idx = vio_dring_next(dr, idx);
627
628
629
630
631
632
633
634
635
636
637
	}
	return found;
}

static int vnet_ack(struct vnet_port *port, void *msgbuf)
{
	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
	struct vio_dring_data *pkt = msgbuf;
	struct net_device *dev;
	struct vnet *vp;
	u32 end;
638
	struct vio_net_desc *desc;
639
640
	struct netdev_queue *txq;

641
642
643
644
	if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
		return 0;

	end = pkt->end_idx;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
645
646
	vp = port->vp;
	dev = vp->dev;
647
648
649
650
651
652
	netif_tx_lock(dev);
	if (unlikely(!idx_is_pending(dr, end))) {
		netif_tx_unlock(dev);
		return 0;
	}

653
654
655
	/* sync for race conditions with vnet_start_xmit() and tell xmit it
	 * is time to send a trigger.
	 */
656
	dr->cons = vio_dring_next(dr, end);
657
	desc = vio_dring_entry(dr, dr->cons);
658
	if (desc->hdr.state == VIO_DESC_READY && !port->start_cons) {
659
660
661
662
663
664
665
666
667
668
669
		/* vnet_start_xmit() just populated this dring but missed
		 * sending the "start" LDC message to the consumer.
		 * Send a "start" trigger on its behalf.
		 */
		if (__vnet_tx_trigger(port, dr->cons) > 0)
			port->start_cons = false;
		else
			port->start_cons = true;
	} else {
		port->start_cons = true;
	}
Sowmini Varadhan's avatar
Sowmini Varadhan committed
670
	netif_tx_unlock(dev);
671

672
673
	txq = netdev_get_tx_queue(dev, port->q_index);
	if (unlikely(netif_tx_queue_stopped(txq) &&
674
675
676
677
678
679
680
681
682
683
684
685
		     vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
		return 1;

	return 0;
}

static int vnet_nack(struct vnet_port *port, void *msgbuf)
{
	/* XXX just reset or similar XXX */
	return 0;
}

686
687
688
689
690
static int handle_mcast(struct vnet_port *port, void *msgbuf)
{
	struct vio_net_mcast_info *pkt = msgbuf;

	if (pkt->tag.stype != VIO_SUBTYPE_ACK)
691
		pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
692
693
694
695
696
697
698
699
700
		       port->vp->dev->name,
		       pkt->tag.type,
		       pkt->tag.stype,
		       pkt->tag.stype_env,
		       pkt->tag.sid);

	return 0;
}

701
702
703
704
705
/* Got back a STOPPED LDC message on port. If the queue is stopped,
 * wake it up so that we'll send out another START message at the
 * next TX.
 */
static void maybe_tx_wakeup(struct vnet_port *port)
706
{
707
	struct netdev_queue *txq;
708

709
710
711
712
713
714
	txq = netdev_get_tx_queue(port->vp->dev, port->q_index);
	__netif_tx_lock(txq, smp_processor_id());
	if (likely(netif_tx_queue_stopped(txq))) {
		struct vio_dring_state *dr;

		dr = &port->vio.drings[VIO_DRIVER_TX_RING];
715
		netif_tx_wake_queue(txq);
716
	}
717
	__netif_tx_unlock(txq);
718
719
}

Sowmini Varadhan's avatar
Sowmini Varadhan committed
720
721
722
723
724
725
726
727
static inline bool port_is_up(struct vnet_port *vnet)
{
	struct vio_driver_state *vio = &vnet->vio;

	return !!(vio->hs_state & VIO_HS_COMPLETE);
}

static int vnet_event_napi(struct vnet_port *port, int budget)
728
729
730
{
	struct vio_driver_state *vio = &port->vio;
	int tx_wakeup, err;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
731
732
	int npkts = 0;
	int event = (port->rx_event & LDC_EVENT_RESET);
733

Sowmini Varadhan's avatar
Sowmini Varadhan committed
734
ldc_ctrl:
735
736
737
738
	if (unlikely(event == LDC_EVENT_RESET ||
		     event == LDC_EVENT_UP)) {
		vio_link_state_change(vio, event);

739
		if (event == LDC_EVENT_RESET) {
740
			vnet_port_reset(port);
741
			vio_port_up(vio);
742
		}
Sowmini Varadhan's avatar
Sowmini Varadhan committed
743
744
		port->rx_event = 0;
		return 0;
745
	}
Sowmini Varadhan's avatar
Sowmini Varadhan committed
746
747
748
749
750
751
752
753
	/* We may have multiple LDC events in rx_event. Unroll send_events() */
	event = (port->rx_event & LDC_EVENT_UP);
	port->rx_event &= ~(LDC_EVENT_RESET|LDC_EVENT_UP);
	if (event == LDC_EVENT_UP)
		goto ldc_ctrl;
	event = port->rx_event;
	if (!(event & LDC_EVENT_DATA_READY))
		return 0;
754

Sowmini Varadhan's avatar
Sowmini Varadhan committed
755
756
	/* we dont expect any other bits than RESET, UP, DATA_READY */
	BUG_ON(event != LDC_EVENT_DATA_READY);
757
758
759
760
761
762
763
764

	tx_wakeup = err = 0;
	while (1) {
		union {
			struct vio_msg_tag tag;
			u64 raw[8];
		} msgbuf;

Sowmini Varadhan's avatar
Sowmini Varadhan committed
765
766
767
768
769
770
771
772
773
774
		if (port->napi_resume) {
			struct vio_dring_data *pkt =
				(struct vio_dring_data *)&msgbuf;
			struct vio_dring_state *dr =
				&port->vio.drings[VIO_DRIVER_RX_RING];

			pkt->tag.type = VIO_TYPE_DATA;
			pkt->tag.stype = VIO_SUBTYPE_INFO;
			pkt->tag.stype_env = VIO_DRING_DATA;
			pkt->seq = dr->rcv_nxt;
775
			pkt->start_idx = vio_dring_next(dr, port->napi_stop_idx);
Sowmini Varadhan's avatar
Sowmini Varadhan committed
776
777
778
			pkt->end_idx = -1;
			goto napi_resume;
		}
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
		err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
		if (unlikely(err < 0)) {
			if (err == -ECONNRESET)
				vio_conn_reset(vio);
			break;
		}
		if (err == 0)
			break;
		viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
		       msgbuf.tag.type,
		       msgbuf.tag.stype,
		       msgbuf.tag.stype_env,
		       msgbuf.tag.sid);
		err = vio_validate_sid(vio, &msgbuf.tag);
		if (err < 0)
			break;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
795
napi_resume:
796
797
		if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
			if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
Sowmini Varadhan's avatar
Sowmini Varadhan committed
798
799
800
801
802
803
804
805
806
807
808
				if (!port_is_up(port)) {
					/* failures like handshake_failure()
					 * may have cleaned up dring, but
					 * NAPI polling may bring us here.
					 */
					err = -ECONNRESET;
					break;
				}
				err = vnet_rx(port, &msgbuf, &npkts, budget);
				if (npkts >= budget)
					break;
809
810
				if (npkts == 0)
					break;
811
812
813
814
815
816
817
818
			} else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
				err = vnet_ack(port, &msgbuf);
				if (err > 0)
					tx_wakeup |= err;
			} else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
				err = vnet_nack(port, &msgbuf);
			}
		} else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
819
820
821
822
			if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
				err = handle_mcast(port, &msgbuf);
			else
				err = vio_control_pkt_engine(vio, &msgbuf);
823
824
825
826
827
828
829
830
831
			if (err)
				break;
		} else {
			err = vnet_handle_unknown(port, &msgbuf);
		}
		if (err == -ECONNRESET)
			break;
	}
	if (unlikely(tx_wakeup && err != -ECONNRESET))
832
		maybe_tx_wakeup(port);
Sowmini Varadhan's avatar
Sowmini Varadhan committed
833
834
835
836
837
838
839
840
841
842
843
	return npkts;
}

static int vnet_poll(struct napi_struct *napi, int budget)
{
	struct vnet_port *port = container_of(napi, struct vnet_port, napi);
	struct vio_driver_state *vio = &port->vio;
	int processed = vnet_event_napi(port, budget);

	if (processed < budget) {
		napi_complete(napi);
844
		port->rx_event &= ~LDC_EVENT_DATA_READY;
Sowmini Varadhan's avatar
Sowmini Varadhan committed
845
846
847
848
849
850
851
852
853
854
855
856
857
		vio_set_intr(vio->vdev->rx_ino, HV_INTR_ENABLED);
	}
	return processed;
}

static void vnet_event(void *arg, int event)
{
	struct vnet_port *port = arg;
	struct vio_driver_state *vio = &port->vio;

	port->rx_event |= event;
	vio_set_intr(vio->vdev->rx_ino, HV_INTR_DISABLED);
	napi_schedule(&port->napi);
858

859
860
}

861
static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
862
863
864
865
866
867
868
869
870
871
{
	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
	struct vio_dring_data hdr = {
		.tag = {
			.type		= VIO_TYPE_DATA,
			.stype		= VIO_SUBTYPE_INFO,
			.stype_env	= VIO_DRING_DATA,
			.sid		= vio_send_sid(&port->vio),
		},
		.dring_ident		= dr->ident,
872
		.start_idx		= start,
873
874
875
		.end_idx		= (u32) -1,
	};
	int err, delay;
876
	int retries = 0;
877

878
879
880
881
882
883
884
885
886
	if (port->stop_rx) {
		err = vnet_send_ack(port,
				    &port->vio.drings[VIO_DRIVER_RX_RING],
				    port->stop_rx_idx, -1,
				    VIO_DRING_STOPPED);
		if (err <= 0)
			return err;
	}

887
888
889
890
891
892
893
894
895
896
897
	hdr.seq = dr->snd_nxt;
	delay = 1;
	do {
		err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
		if (err > 0) {
			dr->snd_nxt++;
			break;
		}
		udelay(delay);
		if ((delay <<= 1) > 128)
			delay = 128;
898
899
		if (retries++ > VNET_MAX_RETRIES)
			break;
900
901
902
903
904
905
906
907
908
909
910
	} while (err == -EAGAIN);

	return err;
}

struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
{
	unsigned int hash = vnet_hashfn(skb->data);
	struct hlist_head *hp = &vp->port_hash[hash];
	struct vnet_port *port;

911
	hlist_for_each_entry_rcu(port, hp, hash) {
912
913
		if (!port_is_up(port))
			continue;
914
		if (ether_addr_equal(port->raddr, skb->data))
915
916
			return port;
	}
917
	list_for_each_entry_rcu(port, &vp->port_list, list) {
918
919
920
921
922
923
924
		if (!port->switch_port)
			continue;
		if (!port_is_up(port))
			continue;
		return port;
	}
	return NULL;
925
926
}

927
928
929
930
931
932
933
934
935
static struct sk_buff *vnet_clean_tx_ring(struct vnet_port *port,
					  unsigned *pending)
{
	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
	struct sk_buff *skb = NULL;
	int i, txi;

	*pending = 0;

936
	txi = dr->prod;
937
938
939
	for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
		struct vio_net_desc *d;

940
941
942
		--txi;
		if (txi < 0)
			txi = VNET_TX_RING_SIZE-1;
943

944
		d = vio_dring_entry(dr, txi);
945

946
		if (d->hdr.state == VIO_DESC_READY) {
947
			(*pending)++;
948
			continue;
949
		}
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
		if (port->tx_bufs[txi].skb) {
			if (d->hdr.state != VIO_DESC_DONE)
				pr_notice("invalid ring buffer state %d\n",
					  d->hdr.state);
			BUG_ON(port->tx_bufs[txi].skb->next);

			port->tx_bufs[txi].skb->next = skb;
			skb = port->tx_bufs[txi].skb;
			port->tx_bufs[txi].skb = NULL;

			ldc_unmap(port->vio.lp,
				  port->tx_bufs[txi].cookies,
				  port->tx_bufs[txi].ncookies);
		} else if (d->hdr.state == VIO_DESC_FREE)
			break;
		d->hdr.state = VIO_DESC_FREE;
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
	}
	return skb;
}

static inline void vnet_free_skbs(struct sk_buff *skb)
{
	struct sk_buff *next;

	while (skb) {
		next = skb->next;
		skb->next = NULL;
		dev_kfree_skb(skb);
		skb = next;
	}
}

static void vnet_clean_timer_expire(unsigned long port0)
{
	struct vnet_port *port = (struct vnet_port *)port0;
	struct sk_buff *freeskbs;
	unsigned pending;

988
	netif_tx_lock(port->vp->dev);
989
	freeskbs = vnet_clean_tx_ring(port, &pending);
990
	netif_tx_unlock(port->vp->dev);
991
992
993
994
995
996
997
998
999
1000

	vnet_free_skbs(freeskbs);

	if (pending)
		(void)mod_timer(&port->clean_timer,
				jiffies + VNET_CLEAN_TIMEOUT);
	 else
		del_timer(&port->clean_timer);
}

1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
static inline int vnet_skb_map(struct ldc_channel *lp, struct sk_buff *skb,
			       struct ldc_trans_cookie *cookies, int ncookies,
			       unsigned int map_perm)
{
	int i, nc, err, blen;

	/* header */
	blen = skb_headlen(skb);
	if (blen < ETH_ZLEN)
		blen = ETH_ZLEN;
	blen += VNET_PACKET_SKIP;
	blen += 8 - (blen & 7);

	err = ldc_map_single(lp, skb->data-VNET_PACKET_SKIP, blen, cookies,
			     ncookies, map_perm);
	if (err < 0)
		return err;
	nc = err;

	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
		u8 *vaddr;

		if (nc < ncookies) {
			vaddr = kmap_atomic(skb_frag_page(f));
			blen = skb_frag_size(f);
			blen += 8 - (blen & 7);
			err = ldc_map_single(lp, vaddr + f->page_offset,
					     blen, cookies + nc, ncookies - nc,
					     map_perm);
			kunmap_atomic(vaddr);
		} else {
			err = -EMSGSIZE;
		}

		if (err < 0) {
			ldc_unmap(lp, cookies, nc);
			return err;
		}
		nc += err;
	}
	return nc;
}

static inline struct sk_buff *vnet_skb_shape(struct sk_buff *skb, int ncookies)
1046
1047
{
	struct sk_buff *nskb;
1048
	int i, len, pad, docopy;
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058

	len = skb->len;
	pad = 0;
	if (len < ETH_ZLEN) {
		pad += ETH_ZLEN - skb->len;
		len += pad;
	}
	len += VNET_PACKET_SKIP;
	pad += 8 - (len & 7);

1059
1060
1061
1062
1063
1064
1065
	/* make sure we have enough cookies and alignment in every frag */
	docopy = skb_shinfo(skb)->nr_frags >= ncookies;
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
		skb_frag_t *f = &skb_shinfo(skb)->frags[i];

		docopy |= f->page_offset & 7;
	}
1066
1067
	if (((unsigned long)skb->data & 7) != VNET_PACKET_SKIP ||
	    skb_tailroom(skb) < pad ||
1068
	    skb_headroom(skb) < VNET_PACKET_SKIP || docopy) {
1069
1070
		int start = 0, offset;
		__wsum csum;
1071

1072
1073
1074
1075
1076
1077
		len = skb->len > ETH_ZLEN ? skb->len : ETH_ZLEN;
		nskb = alloc_and_align_skb(skb->dev, len);
		if (nskb == NULL) {
			dev_kfree_skb(skb);
			return NULL;
		}
1078
		skb_reserve(nskb, VNET_PACKET_SKIP);
1079
1080
1081
1082
1083
1084
1085
1086
1087

		nskb->protocol = skb->protocol;
		offset = skb_mac_header(skb) - skb->data;
		skb_set_mac_header(nskb, offset);
		offset = skb_network_header(skb) - skb->data;
		skb_set_network_header(nskb, offset);
		offset = skb_transport_header(skb) - skb->data;
		skb_set_transport_header(nskb, offset);

1088
		offset = 0;
1089
1090
1091
		nskb->csum_offset = skb->csum_offset;
		nskb->ip_summed = skb->ip_summed;

1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
		if (skb->ip_summed == CHECKSUM_PARTIAL)
			start = skb_checksum_start_offset(skb);
		if (start) {
			struct iphdr *iph = ip_hdr(nskb);
			int offset = start + nskb->csum_offset;

			if (skb_copy_bits(skb, 0, nskb->data, start)) {
				dev_kfree_skb(nskb);
				dev_kfree_skb(skb);
				return NULL;
			}
			*(__sum16 *)(skb->data + offset) = 0;
			csum = skb_copy_and_csum_bits(skb, start,
						      nskb->data + start,
						      skb->len - start, 0);
			if (iph->protocol == IPPROTO_TCP ||
			    iph->protocol == IPPROTO_UDP) {
				csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
							 skb->len - start,
							 iph->protocol, csum);
			}
			*(__sum16 *)(nskb->data + offset) = csum;

			nskb->ip_summed = CHECKSUM_NONE;
		} else if (skb_copy_bits(skb, 0, nskb->data, skb->len)) {
1117
1118
1119
1120
1121
			dev_kfree_skb(nskb);
			dev_kfree_skb(skb);
			return NULL;
		}
		(void)skb_put(nskb, skb->len);
David L Stevens's avatar
David L Stevens committed
1122
1123
1124
1125
		if (skb_is_gso(skb)) {
			skb_shinfo(nskb)->gso_size = skb_shinfo(skb)->gso_size;
			skb_shinfo(nskb)->gso_type = skb_shinfo(skb)->gso_type;
		}
1126
		nskb->queue_mapping = skb->queue_mapping;
1127
1128
1129
1130
1131
1132
		dev_kfree_skb(skb);
		skb = nskb;
	}
	return skb;
}

1133
1134
1135
1136
1137
1138
1139
static u16
vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
		  void *accel_priv, select_queue_fallback_t fallback)
{
	struct vnet *vp = netdev_priv(dev);
	struct vnet_port *port = __tx_port_find(vp, skb);

1140
1141
	if (port == NULL)
		return 0;
1142
1143
1144
	return port->q_index;
}

David L Stevens's avatar
David L Stevens committed
1145
1146
1147
1148
1149
1150
1151
static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev);

static int vnet_handle_offloads(struct vnet_port *port, struct sk_buff *skb)
{
	struct net_device *dev = port->vp->dev;
	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
	struct sk_buff *segs;
David L Stevens's avatar
David L Stevens committed
1152
	int maclen, datalen;
David L Stevens's avatar
David L Stevens committed
1153
	int status;
David L Stevens's avatar
David L Stevens committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
	int gso_size, gso_type, gso_segs;
	int hlen = skb_transport_header(skb) - skb_mac_header(skb);
	int proto = IPPROTO_IP;

	if (skb->protocol == htons(ETH_P_IP))
		proto = ip_hdr(skb)->protocol;
	else if (skb->protocol == htons(ETH_P_IPV6))
		proto = ipv6_hdr(skb)->nexthdr;

	if (proto == IPPROTO_TCP)
		hlen += tcp_hdr(skb)->doff * 4;
	else if (proto == IPPROTO_UDP)
		hlen += sizeof(struct udphdr);
	else {
		pr_err("vnet_handle_offloads GSO with unknown transport "
		       "protocol %d tproto %d\n", skb->protocol, proto);
		hlen = 128; /* XXX */
	}
	datalen = port->tsolen - hlen;

	gso_size = skb_shinfo(skb)->gso_size;
	gso_type = skb_shinfo(skb)->gso_type;
	gso_segs = skb_shinfo(skb)->gso_segs;

	if (port->tso && gso_size < datalen)
		gso_segs = DIV_ROUND_UP(skb->len - hlen, datalen);
David L Stevens's avatar
David L Stevens committed
1180

David L Stevens's avatar
David L Stevens committed
1181
	if (unlikely(vnet_tx_dring_avail(dr) < gso_segs)) {
David L Stevens's avatar
David L Stevens committed
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
		struct netdev_queue *txq;

		txq  = netdev_get_tx_queue(dev, port->q_index);
		netif_tx_stop_queue(txq);
		if (vnet_tx_dring_avail(dr) < skb_shinfo(skb)->gso_segs)
			return NETDEV_TX_BUSY;
		netif_tx_wake_queue(txq);
	}

	maclen = skb_network_header(skb) - skb_mac_header(skb);
	skb_pull(skb, maclen);

David L Stevens's avatar
David L Stevens committed
1194
	if (port->tso && gso_size < datalen) {
1195
1196
1197
		if (skb_unclone(skb, GFP_ATOMIC))
			goto out_dropped;

David L Stevens's avatar
David L Stevens committed
1198
1199
1200
		/* segment to TSO size */
		skb_shinfo(skb)->gso_size = datalen;
		skb_shinfo(skb)->gso_segs = gso_segs;
David L Stevens's avatar
David L Stevens committed
1201
	}
1202
1203
1204
	segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
	if (IS_ERR(segs))
		goto out_dropped;
David L Stevens's avatar
David L Stevens committed
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214

	skb_push(skb, maclen);
	skb_reset_mac_header(skb);

	status = 0;
	while (segs) {
		struct sk_buff *curr = segs;

		segs = segs->next;
		curr->next = NULL;
David L Stevens's avatar
David L Stevens committed
1215
1216
1217
1218
1219
1220
1221
		if (port->tso && curr->len > dev->mtu) {
			skb_shinfo(curr)->gso_size = gso_size;
			skb_shinfo(curr)->gso_type = gso_type;
			skb_shinfo(curr)->gso_segs =
				DIV_ROUND_UP(curr->len - hlen, gso_size);
		} else
			skb_shinfo(curr)->gso_size = 0;
David L Stevens's avatar
David L Stevens committed
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241

		skb_push(curr, maclen);
		skb_reset_mac_header(curr);
		memcpy(skb_mac_header(curr), skb_mac_header(skb),
		       maclen);
		curr->csum_start = skb_transport_header(curr) - curr->head;
		if (ip_hdr(curr)->protocol == IPPROTO_TCP)
			curr->csum_offset = offsetof(struct tcphdr, check);
		else if (ip_hdr(curr)->protocol == IPPROTO_UDP)
			curr->csum_offset = offsetof(struct udphdr, check);

		if (!(status & NETDEV_TX_MASK))
			status = vnet_start_xmit(curr, dev);
		if (status & NETDEV_TX_MASK)
			dev_kfree_skb_any(curr);
	}

	if (!(status & NETDEV_TX_MASK))
		dev_kfree_skb_any(skb);
	return status;
1242
1243
1244
1245
out_dropped:
	dev->stats.tx_dropped++;
	dev_kfree_skb_any(skb);
	return NETDEV_TX_OK;
David L Stevens's avatar
David L Stevens committed
1246
1247
}

1248
1249
1250
static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct vnet *vp = netdev_priv(dev);
1251
	struct vnet_port *port = NULL;
1252
1253
1254
	struct vio_dring_state *dr;
	struct vio_net_desc *d;
	unsigned int len;
1255
1256
1257
	struct sk_buff *freeskbs = NULL;
	int i, err, txi;
	unsigned pending = 0;
1258
	struct netdev_queue *txq;
1259

1260
	rcu_read_lock();
1261
	port = __tx_port_find(vp, skb);
1262
1263
	if (unlikely(!port)) {
		rcu_read_unlock();
1264
		goto out_dropped;
1265
	}
1266

David L Stevens's avatar
David L Stevens committed
1267
	if (skb_is_gso(skb) && skb->len > port->tsolen) {
David L Stevens's avatar
David L Stevens committed
1268
1269
1270
1271
1272
		err = vnet_handle_offloads(port, skb);
		rcu_read_unlock();
		return err;
	}

David L Stevens's avatar
David L Stevens committed
1273
	if (!skb_is_gso(skb) && skb->len > port->rmtu) {
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
		unsigned long localmtu = port->rmtu - ETH_HLEN;

		if (vio_version_after_eq(&port->vio, 1, 3))
			localmtu -= VLAN_HLEN;

		if (skb->protocol == htons(ETH_P_IP)) {
			struct flowi4 fl4;
			struct rtable *rt = NULL;

			memset(&fl4, 0, sizeof(fl4));
			fl4.flowi4_oif = dev->ifindex;
			fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
			fl4.daddr = ip_hdr(skb)->daddr;
			fl4.saddr = ip_hdr(skb)->saddr;

			rt = ip_route_output_key(dev_net(dev), &fl4);
1290
			rcu_read_unlock();
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
			if (!IS_ERR(rt)) {
				skb_dst_set(skb, &rt->dst);
				icmp_send(skb, ICMP_DEST_UNREACH,
					  ICMP_FRAG_NEEDED,
					  htonl(localmtu));
			}
		}
#if IS_ENABLED(CONFIG_IPV6)
		else if (skb->protocol == htons(ETH_P_IPV6))
			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
#endif
1302
		goto out_dropped;
1303
	}
1304

1305
1306
	skb = vnet_skb_shape(skb, 2);

1307
	if (unlikely(!skb))
1308
1309
		goto out_dropped;

1310
1311
1312
	if (skb->ip_summed == CHECKSUM_PARTIAL)
		vnet_fullcsum(skb);

1313
	dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1314
1315
	i = skb_get_queue_mapping(skb);
	txq = netdev_get_tx_queue(dev, i);
1316
	if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
1317
1318
		if (!netif_tx_queue_stopped(txq)) {
			netif_tx_stop_queue(txq);
1319
1320

			/* This is a hard error, log it. */
1321
			netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1322
1323
			dev->stats.tx_errors++;
		}
1324
		rcu_read_unlock();
1325
1326
1327
1328
1329
		return NETDEV_TX_BUSY;
	}

	d = vio_dring_cur(dr);

1330
1331
1332
1333
1334
	txi = dr->prod;

	freeskbs = vnet_clean_tx_ring(port, &pending);

	BUG_ON(port->tx_bufs[txi].skb);
1335
1336

	len = skb->len;
1337
	if (len < ETH_ZLEN)
1338
		len = ETH_ZLEN;
1339

1340
1341
	err = vnet_skb_map(port->vio.lp, skb, port->tx_bufs[txi].cookies, 2,
			   (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
1342
1343
	if (err < 0) {
		netdev_info(dev, "tx buffer map error %d\n", err);
1344
		goto out_dropped;
1345
	}
1346
1347
1348

	port->tx_bufs[txi].skb = skb;
	skb = NULL;
1349
	port->tx_bufs[txi].ncookies = err;
1350

1351
1352
1353
1354
1355
1356
1357
1358
1359
	/* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
	 * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
	 * the protocol itself does not require it as long as the peer
	 * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
	 *
	 * An ACK for every packet in the ring is expensive as the
	 * sending of LDC messages is slow and affects performance.
	 */
	d->hdr.ack = VIO_ACK_DISABLE;
1360
	d->size = len;
1361
	d->ncookies = port->tx_bufs[txi].ncookies;
1362
	for (i = 0; i < d->ncookies; i++)
1363
		d->cookies[i] = port->tx_bufs[txi].cookies[i];
1364
1365
1366
1367
	if (vio_version_after_eq(&port->vio, 1, 7)) {
		struct vio_net_dext *dext = vio_net_ext(d);

		memset(dext, 0, sizeof(*dext));
David L Stevens's avatar
David L Stevens committed
1368
1369
1370
1371
1372
		if (skb_is_gso(port->tx_bufs[txi].skb)) {
			dext->ipv4_lso_mss = skb_shinfo(port->tx_bufs[txi].skb)
					     ->gso_size;
			dext->flags |= VNET_PKT_IPV4_LSO;
		}
1373
1374
1375
1376
1377
1378
		if (vio_version_after_eq(&port->vio, 1, 8) &&
		    !port->switch_port) {
			dext->flags |= VNET_PKT_HCK_IPV4_HDRCKSUM_OK;
			dext->flags |= VNET_PKT_HCK_FULLCKSUM_OK;
		}
	}
1379
1380
1381
1382
1383
1384
1385
1386

	/* This has to be a non-SMP write barrier because we are writing
	 * to memory which is shared with the peer LDOM.
	 */
	wmb();

	d->hdr.state = VIO_DESC_READY;

1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
	/* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
	 * to notify the consumer that some descriptors are READY.
	 * After that "start" trigger, no additional triggers are needed until
	 * a DRING_STOPPED is received from the consumer. The dr->cons field
	 * (set up by vnet_ack()) has the value of the next dring index
	 * that has not yet been ack-ed. We send a "start" trigger here
	 * if, and only if, start_cons is true (reset it afterward). Conversely,
	 * vnet_ack() should check if the dring corresponding to cons
	 * is marked READY, but start_cons was false.
	 * If so, vnet_ack() should send out the missed "start" trigger.
	 *
	 * Note that the wmb() above makes sure the cookies et al. are
	 * not globally visible before the VIO_DESC_READY, and that the
	 * stores are ordered correctly by the compiler. The consumer will
	 * not proceed until the VIO_DESC_READY is visible assuring that
	 * the consumer does not observe anything related to descriptors
	 * out of order. The HV trap from the LDC start trigger is the
	 * producer to consumer announcement that work is available to the
	 * consumer
	 */
	if (!port->start_cons)
		goto ldc_start_done; /* previous trigger suffices */

	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
1415
		skb = port->tx_bufs[txi].skb;
		port->tx_bufs[txi].skb = NULL;
1416
		dev->stats.tx_carrier_errors++;
1417
		goto out_dropped;
1418
1419
	}

1420
1421
1422
ldc_start_done:
	port->start_cons = false;

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
1434
	(void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
	rcu_read_unlock();
1435

1436
1437
	vnet_free_skbs(freeskbs);

1438
1439
1440
	return NETDEV_TX_OK;

out_dropped: