sunvnet: fix rx packet length check to allow for TSO
[deliverable/linux.git] / drivers / net / ethernet / sun / sunvnet.c
CommitLineData
4c521e42
DM
1/* sunvnet.c: Sun LDOM Virtual Network Driver.
2 *
3d452e55 3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4c521e42
DM
4 */
5
4d5870ec
JP
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
4c521e42
DM
8#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/delay.h>
13#include <linux/init.h>
14#include <linux/netdevice.h>
15#include <linux/ethtool.h>
16#include <linux/etherdevice.h>
9184a046 17#include <linux/mutex.h>
da38c564 18#include <linux/highmem.h>
e4defc77 19#include <linux/if_vlan.h>
4c521e42 20
a2b78e9b
DS
21#if IS_ENABLED(CONFIG_IPV6)
22#include <linux/icmpv6.h>
23#endif
24
6d0ba919 25#include <net/ip.h>
a2b78e9b
DS
26#include <net/icmp.h>
27#include <net/route.h>
28
4c521e42
DM
29#include <asm/vio.h>
30#include <asm/ldc.h>
31
32#include "sunvnet.h"
33
34#define DRV_MODULE_NAME "sunvnet"
4c521e42
DM
35#define DRV_MODULE_VERSION "1.0"
36#define DRV_MODULE_RELDATE "June 25, 2007"
37
f73d12bd 38static char version[] =
4c521e42
DM
39 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
40MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
41MODULE_DESCRIPTION("Sun LDOM virtual network driver");
42MODULE_LICENSE("GPL");
43MODULE_VERSION(DRV_MODULE_VERSION);
44
d51bffd1
SV
45#define VNET_MAX_TXQS 16
46
adddc32d
SV
47/* Heuristic for the number of times to exponentially backoff and
48 * retry sending an LDC trigger when EAGAIN is encountered
49 */
50#define VNET_MAX_RETRIES 10
51
d1015645
SV
52static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
53
4c521e42
DM
54/* Ordered from largest major to lowest */
55static struct vio_version vnet_versions[] = {
6d0ba919
DS
56 { .major = 1, .minor = 8 },
57 { .major = 1, .minor = 7 },
e4defc77 58 { .major = 1, .minor = 6 },
4c521e42
DM
59 { .major = 1, .minor = 0 },
60};
61
62static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
63{
64 return vio_dring_avail(dr, VNET_TX_RING_SIZE);
65}
66
67static int vnet_handle_unknown(struct vnet_port *port, void *arg)
68{
69 struct vio_msg_tag *pkt = arg;
70
4d5870ec 71 pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
4c521e42 72 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
4d5870ec 73 pr_err("Resetting connection\n");
4c521e42
DM
74
75 ldc_disconnect(port->vio.lp);
76
77 return -ECONNRESET;
78}
79
d6732489
DS
80static int vnet_port_alloc_tx_ring(struct vnet_port *port);
81
4c521e42
DM
82static int vnet_send_attr(struct vio_driver_state *vio)
83{
84 struct vnet_port *port = to_vnet_port(vio);
85 struct net_device *dev = port->vp->dev;
86 struct vio_net_attr_info pkt;
e4defc77 87 int framelen = ETH_FRAME_LEN;
d6732489
DS
88 int i, err;
89
90 err = vnet_port_alloc_tx_ring(to_vnet_port(vio));
91 if (err)
92 return err;
4c521e42
DM
93
94 memset(&pkt, 0, sizeof(pkt));
95 pkt.tag.type = VIO_TYPE_CTRL;
96 pkt.tag.stype = VIO_SUBTYPE_INFO;
97 pkt.tag.stype_env = VIO_ATTR_INFO;
98 pkt.tag.sid = vio_send_sid(vio);
e4defc77
DS
99 if (vio_version_before(vio, 1, 2))
100 pkt.xfer_mode = VIO_DRING_MODE;
101 else
102 pkt.xfer_mode = VIO_NEW_DRING_MODE;
4c521e42
DM
103 pkt.addr_type = VNET_ADDR_ETHERMAC;
104 pkt.ack_freq = 0;
105 for (i = 0; i < 6; i++)
106 pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
e4defc77
DS
107 if (vio_version_after(vio, 1, 3)) {
108 if (port->rmtu) {
109 port->rmtu = min(VNET_MAXPACKET, port->rmtu);
110 pkt.mtu = port->rmtu;
111 } else {
112 port->rmtu = VNET_MAXPACKET;
113 pkt.mtu = port->rmtu;
114 }
115 if (vio_version_after_eq(vio, 1, 6))
116 pkt.options = VIO_TX_DRING;
117 } else if (vio_version_before(vio, 1, 3)) {
118 pkt.mtu = framelen;
119 } else { /* v1.3 */
120 pkt.mtu = framelen + VLAN_HLEN;
121 }
122
e4defc77 123 pkt.cflags = 0;
368e36ed
DS
124 if (vio_version_after_eq(vio, 1, 7) && port->tso) {
125 pkt.cflags |= VNET_LSO_IPV4_CAPAB;
126 if (!port->tsolen)
127 port->tsolen = VNET_MAXTSO;
128 pkt.ipv4_lso_maxlen = port->tsolen;
129 }
130
131 pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
4c521e42
DM
132
133 viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
e4defc77
DS
134 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
135 "cflags[0x%04x] lso_max[%u]\n",
4c521e42 136 pkt.xfer_mode, pkt.addr_type,
e4defc77
DS
137 (unsigned long long)pkt.addr,
138 pkt.ack_freq, pkt.plnk_updt, pkt.options,
139 (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);
140
4c521e42
DM
141
142 return vio_ldc_send(vio, &pkt, sizeof(pkt));
143}
144
145static int handle_attr_info(struct vio_driver_state *vio,
146 struct vio_net_attr_info *pkt)
147{
e4defc77
DS
148 struct vnet_port *port = to_vnet_port(vio);
149 u64 localmtu;
150 u8 xfer_mode;
151
152 viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
153 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
154 " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
4c521e42 155 pkt->xfer_mode, pkt->addr_type,
e4defc77
DS
156 (unsigned long long)pkt->addr,
157 pkt->ack_freq, pkt->plnk_updt, pkt->options,
158 (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
159 pkt->ipv4_lso_maxlen);
4c521e42
DM
160
161 pkt->tag.sid = vio_send_sid(vio);
162
e4defc77
DS
163 xfer_mode = pkt->xfer_mode;
164 /* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
165 if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
166 xfer_mode = VIO_NEW_DRING_MODE;
167
168 /* MTU negotiation:
169 * < v1.3 - ETH_FRAME_LEN exactly
170 * > v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
171 * pkt->mtu for ACK
172 * = v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
173 */
174 if (vio_version_before(vio, 1, 3)) {
175 localmtu = ETH_FRAME_LEN;
176 } else if (vio_version_after(vio, 1, 3)) {
177 localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
178 localmtu = min(pkt->mtu, localmtu);
179 pkt->mtu = localmtu;
180 } else { /* v1.3 */
181 localmtu = ETH_FRAME_LEN + VLAN_HLEN;
182 }
183 port->rmtu = localmtu;
184
368e36ed
DS
185 /* LSO negotiation */
186 if (vio_version_after_eq(vio, 1, 7))
187 port->tso &= !!(pkt->cflags & VNET_LSO_IPV4_CAPAB);
188 else
189 port->tso = false;
190 if (port->tso) {
191 if (!port->tsolen)
192 port->tsolen = VNET_MAXTSO;
193 port->tsolen = min(port->tsolen, pkt->ipv4_lso_maxlen);
194 if (port->tsolen < VNET_MINTSO) {
195 port->tso = false;
196 port->tsolen = 0;
197 pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
198 }
199 pkt->ipv4_lso_maxlen = port->tsolen;
200 } else {
201 pkt->cflags &= ~VNET_LSO_IPV4_CAPAB;
202 pkt->ipv4_lso_maxlen = 0;
203 }
204
e4defc77
DS
205 /* for version >= 1.6, ACK packet mode we support */
206 if (vio_version_after_eq(vio, 1, 6)) {
207 pkt->xfer_mode = VIO_NEW_DRING_MODE;
208 pkt->options = VIO_TX_DRING;
209 }
210
211 if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
4c521e42 212 pkt->addr_type != VNET_ADDR_ETHERMAC ||
e4defc77 213 pkt->mtu != localmtu) {
4c521e42
DM
214 viodbg(HS, "SEND NET ATTR NACK\n");
215
216 pkt->tag.stype = VIO_SUBTYPE_NACK;
217
218 (void) vio_ldc_send(vio, pkt, sizeof(*pkt));
219
220 return -ECONNRESET;
221 } else {
e4defc77
DS
222 viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
223 "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
224 "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
225 pkt->xfer_mode, pkt->addr_type,
226 (unsigned long long)pkt->addr,
227 pkt->ack_freq, pkt->plnk_updt, pkt->options,
228 (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
229 pkt->ipv4_lso_maxlen);
4c521e42
DM
230
231 pkt->tag.stype = VIO_SUBTYPE_ACK;
232
233 return vio_ldc_send(vio, pkt, sizeof(*pkt));
234 }
235
236}
237
238static int handle_attr_ack(struct vio_driver_state *vio,
239 struct vio_net_attr_info *pkt)
240{
241 viodbg(HS, "GOT NET ATTR ACK\n");
242
243 return 0;
244}
245
246static int handle_attr_nack(struct vio_driver_state *vio,
247 struct vio_net_attr_info *pkt)
248{
249 viodbg(HS, "GOT NET ATTR NACK\n");
250
251 return -ECONNRESET;
252}
253
254static int vnet_handle_attr(struct vio_driver_state *vio, void *arg)
255{
256 struct vio_net_attr_info *pkt = arg;
257
258 switch (pkt->tag.stype) {
259 case VIO_SUBTYPE_INFO:
260 return handle_attr_info(vio, pkt);
261
262 case VIO_SUBTYPE_ACK:
263 return handle_attr_ack(vio, pkt);
264
265 case VIO_SUBTYPE_NACK:
266 return handle_attr_nack(vio, pkt);
267
268 default:
269 return -ECONNRESET;
270 }
271}
272
273static void vnet_handshake_complete(struct vio_driver_state *vio)
274{
275 struct vio_dring_state *dr;
276
277 dr = &vio->drings[VIO_DRIVER_RX_RING];
278 dr->snd_nxt = dr->rcv_nxt = 1;
279
280 dr = &vio->drings[VIO_DRIVER_TX_RING];
281 dr->snd_nxt = dr->rcv_nxt = 1;
282}
283
284/* The hypervisor interface that implements copying to/from imported
285 * memory from another domain requires that copies are done to 8-byte
286 * aligned buffers, and that the lengths of such copies are also 8-byte
287 * multiples.
288 *
289 * So we align skb->data to an 8-byte multiple and pad-out the data
290 * area so we can round the copy length up to the next multiple of
291 * 8 for the copy.
292 *
293 * The transmitter puts the actual start of the packet 6 bytes into
294 * the buffer it sends over, so that the IP headers after the ethernet
295 * header are aligned properly. These 6 bytes are not in the descriptor
296 * length, they are simply implied. This offset is represented using
297 * the VNET_PACKET_SKIP macro.
298 */
299static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
300 unsigned int len)
301{
302 struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8);
303 unsigned long addr, off;
304
305 if (unlikely(!skb))
306 return NULL;
307
308 addr = (unsigned long) skb->data;
309 off = ((addr + 7UL) & ~7UL) - addr;
310 if (off)
311 skb_reserve(skb, off);
312
313 return skb;
314}
315
6d0ba919
DS
316static inline void vnet_fullcsum(struct sk_buff *skb)
317{
318 struct iphdr *iph = ip_hdr(skb);
319 int offset = skb_transport_offset(skb);
320
321 if (skb->protocol != htons(ETH_P_IP))
322 return;
323 if (iph->protocol != IPPROTO_TCP &&
324 iph->protocol != IPPROTO_UDP)
325 return;
326 skb->ip_summed = CHECKSUM_NONE;
327 skb->csum_level = 1;
328 skb->csum = 0;
329 if (iph->protocol == IPPROTO_TCP) {
330 struct tcphdr *ptcp = tcp_hdr(skb);
331
332 ptcp->check = 0;
333 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
334 ptcp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
335 skb->len - offset, IPPROTO_TCP,
336 skb->csum);
337 } else if (iph->protocol == IPPROTO_UDP) {
338 struct udphdr *pudp = udp_hdr(skb);
339
340 pudp->check = 0;
341 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
342 pudp->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
343 skb->len - offset, IPPROTO_UDP,
344 skb->csum);
345 }
346}
347
348static int vnet_rx_one(struct vnet_port *port, struct vio_net_desc *desc)
4c521e42
DM
349{
350 struct net_device *dev = port->vp->dev;
6d0ba919 351 unsigned int len = desc->size;
4c521e42
DM
352 unsigned int copy_len;
353 struct sk_buff *skb;
86cfeab6 354 int maxlen;
4c521e42
DM
355 int err;
356
357 err = -EMSGSIZE;
86cfeab6
DS
358 if (port->tso && port->tsolen > port->rmtu)
359 maxlen = port->tsolen;
360 else
361 maxlen = port->rmtu;
362 if (unlikely(len < ETH_ZLEN || len > maxlen)) {
4c521e42
DM
363 dev->stats.rx_length_errors++;
364 goto out_dropped;
365 }
366
367 skb = alloc_and_align_skb(dev, len);
368 err = -ENOMEM;
369 if (unlikely(!skb)) {
370 dev->stats.rx_missed_errors++;
371 goto out_dropped;
372 }
373
374 copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
375 skb_put(skb, copy_len);
376 err = ldc_copy(port->vio.lp, LDC_COPY_IN,
377 skb->data, copy_len, 0,
6d0ba919 378 desc->cookies, desc->ncookies);
4c521e42
DM
379 if (unlikely(err < 0)) {
380 dev->stats.rx_frame_errors++;
381 goto out_free_skb;
382 }
383
384 skb_pull(skb, VNET_PACKET_SKIP);
385 skb_trim(skb, len);
386 skb->protocol = eth_type_trans(skb, dev);
387
6d0ba919
DS
388 if (vio_version_after_eq(&port->vio, 1, 8)) {
389 struct vio_net_dext *dext = vio_net_ext(desc);
390
391 if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM) {
392 if (skb->protocol == ETH_P_IP) {
393 struct iphdr *iph = (struct iphdr *)skb->data;
394
395 iph->check = 0;
396 ip_send_check(iph);
397 }
398 }
399 if ((dext->flags & VNET_PKT_HCK_FULLCKSUM) &&
400 skb->ip_summed == CHECKSUM_NONE)
401 vnet_fullcsum(skb);
402 if (dext->flags & VNET_PKT_HCK_IPV4_HDRCKSUM_OK) {
403 skb->ip_summed = CHECKSUM_PARTIAL;
404 skb->csum_level = 0;
405 if (dext->flags & VNET_PKT_HCK_FULLCKSUM_OK)
406 skb->csum_level = 1;
407 }
408 }
409
1b6b0a47
DS
410 skb->ip_summed = port->switch_port ? CHECKSUM_NONE : CHECKSUM_PARTIAL;
411
4c521e42
DM
412 dev->stats.rx_packets++;
413 dev->stats.rx_bytes += len;
69088822 414 napi_gro_receive(&port->napi, skb);
4c521e42
DM
415 return 0;
416
417out_free_skb:
418 kfree_skb(skb);
419
420out_dropped:
421 dev->stats.rx_dropped++;
422 return err;
423}
424
425static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
426 u32 start, u32 end, u8 vio_dring_state)
427{
428 struct vio_dring_data hdr = {
429 .tag = {
430 .type = VIO_TYPE_DATA,
431 .stype = VIO_SUBTYPE_ACK,
432 .stype_env = VIO_DRING_DATA,
433 .sid = vio_send_sid(&port->vio),
434 },
435 .dring_ident = dr->ident,
436 .start_idx = start,
437 .end_idx = end,
438 .state = vio_dring_state,
439 };
440 int err, delay;
adddc32d 441 int retries = 0;
4c521e42
DM
442
443 hdr.seq = dr->snd_nxt;
444 delay = 1;
445 do {
446 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
447 if (err > 0) {
448 dr->snd_nxt++;
449 break;
450 }
451 udelay(delay);
452 if ((delay <<= 1) > 128)
453 delay = 128;
adddc32d
SV
454 if (retries++ > VNET_MAX_RETRIES) {
455 pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
456 port->raddr[0], port->raddr[1],
457 port->raddr[2], port->raddr[3],
458 port->raddr[4], port->raddr[5]);
d1015645 459 break;
adddc32d 460 }
4c521e42
DM
461 } while (err == -EAGAIN);
462
d1015645
SV
463 if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
464 port->stop_rx_idx = end;
465 port->stop_rx = true;
466 } else {
467 port->stop_rx_idx = 0;
468 port->stop_rx = false;
469 }
470
4c521e42
DM
471 return err;
472}
473
4c521e42
DM
474static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
475 struct vio_dring_state *dr,
476 u32 index)
477{
478 struct vio_net_desc *desc = port->vio.desc_buf;
479 int err;
480
481 err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
482 (index * dr->entry_size),
483 dr->cookies, dr->ncookies);
484 if (err < 0)
485 return ERR_PTR(err);
486
487 return desc;
488}
489
490static int put_rx_desc(struct vnet_port *port,
491 struct vio_dring_state *dr,
492 struct vio_net_desc *desc,
493 u32 index)
494{
495 int err;
496
497 err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
498 (index * dr->entry_size),
499 dr->cookies, dr->ncookies);
500 if (err < 0)
501 return err;
502
503 return 0;
504}
505
506static int vnet_walk_rx_one(struct vnet_port *port,
507 struct vio_dring_state *dr,
508 u32 index, int *needs_ack)
509{
510 struct vio_net_desc *desc = get_rx_desc(port, dr, index);
511 struct vio_driver_state *vio = &port->vio;
512 int err;
513
69088822 514 BUG_ON(desc == NULL);
4c521e42
DM
515 if (IS_ERR(desc))
516 return PTR_ERR(desc);
517
78dcff7b
DS
518 if (desc->hdr.state != VIO_DESC_READY)
519 return 1;
520
521 rmb();
522
3f4528d6 523 viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
4c521e42
DM
524 desc->hdr.state, desc->hdr.ack,
525 desc->size, desc->ncookies,
526 desc->cookies[0].cookie_addr,
527 desc->cookies[0].cookie_size);
528
6d0ba919 529 err = vnet_rx_one(port, desc);
4c521e42
DM
530 if (err == -ECONNRESET)
531 return err;
532 desc->hdr.state = VIO_DESC_DONE;
533 err = put_rx_desc(port, dr, desc, index);
534 if (err < 0)
535 return err;
536 *needs_ack = desc->hdr.ack;
537 return 0;
538}
539
540static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
69088822 541 u32 start, u32 end, int *npkts, int budget)
4c521e42
DM
542{
543 struct vio_driver_state *vio = &port->vio;
544 int ack_start = -1, ack_end = -1;
69088822 545 bool send_ack = true;
4c521e42 546
fe47c3c2
DE
547 end = (end == (u32) -1) ? vio_dring_prev(dr, start)
548 : vio_dring_next(dr, end);
4c521e42
DM
549
550 viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
551
552 while (start != end) {
553 int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
554 if (err == -ECONNRESET)
555 return err;
556 if (err != 0)
557 break;
69088822 558 (*npkts)++;
4c521e42
DM
559 if (ack_start == -1)
560 ack_start = start;
561 ack_end = start;
fe47c3c2 562 start = vio_dring_next(dr, start);
4c521e42
DM
563 if (ack && start != end) {
564 err = vnet_send_ack(port, dr, ack_start, ack_end,
565 VIO_DRING_ACTIVE);
566 if (err == -ECONNRESET)
567 return err;
568 ack_start = -1;
569 }
69088822
SV
570 if ((*npkts) >= budget) {
571 send_ack = false;
572 break;
573 }
4c521e42
DM
574 }
575 if (unlikely(ack_start == -1))
fe47c3c2 576 ack_start = ack_end = vio_dring_prev(dr, start);
69088822
SV
577 if (send_ack) {
578 port->napi_resume = false;
579 return vnet_send_ack(port, dr, ack_start, ack_end,
580 VIO_DRING_STOPPED);
581 } else {
582 port->napi_resume = true;
583 port->napi_stop_idx = ack_end;
584 return 1;
585 }
4c521e42
DM
586}
587
69088822
SV
588static int vnet_rx(struct vnet_port *port, void *msgbuf, int *npkts,
589 int budget)
4c521e42
DM
590{
591 struct vio_dring_data *pkt = msgbuf;
592 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
593 struct vio_driver_state *vio = &port->vio;
594
3f4528d6 595 viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
4c521e42
DM
596 pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
597
598 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
599 return 0;
600 if (unlikely(pkt->seq != dr->rcv_nxt)) {
4d5870ec
JP
601 pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
602 pkt->seq, dr->rcv_nxt);
4c521e42
DM
603 return 0;
604 }
605
69088822
SV
606 if (!port->napi_resume)
607 dr->rcv_nxt++;
4c521e42
DM
608
609 /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
610
69088822
SV
611 return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx,
612 npkts, budget);
4c521e42
DM
613}
614
615static int idx_is_pending(struct vio_dring_state *dr, u32 end)
616{
617 u32 idx = dr->cons;
618 int found = 0;
619
620 while (idx != dr->prod) {
621 if (idx == end) {
622 found = 1;
623 break;
624 }
fe47c3c2 625 idx = vio_dring_next(dr, idx);
4c521e42
DM
626 }
627 return found;
628}
629
630static int vnet_ack(struct vnet_port *port, void *msgbuf)
631{
632 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
633 struct vio_dring_data *pkt = msgbuf;
634 struct net_device *dev;
635 struct vnet *vp;
636 u32 end;
d1015645 637 struct vio_net_desc *desc;
d51bffd1
SV
638 struct netdev_queue *txq;
639
4c521e42
DM
640 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
641 return 0;
642
643 end = pkt->end_idx;
69088822
SV
644 vp = port->vp;
645 dev = vp->dev;
b0cffed5
SV
646 netif_tx_lock(dev);
647 if (unlikely(!idx_is_pending(dr, end))) {
648 netif_tx_unlock(dev);
649 return 0;
650 }
651
d1015645
SV
652 /* sync for race conditions with vnet_start_xmit() and tell xmit it
653 * is time to send a trigger.
654 */
fe47c3c2 655 dr->cons = vio_dring_next(dr, end);
d1015645 656 desc = vio_dring_entry(dr, dr->cons);
777362d7 657 if (desc->hdr.state == VIO_DESC_READY && !port->start_cons) {
d1015645
SV
658 /* vnet_start_xmit() just populated this dring but missed
659 * sending the "start" LDC message to the consumer.
660 * Send a "start" trigger on its behalf.
661 */
662 if (__vnet_tx_trigger(port, dr->cons) > 0)
663 port->start_cons = false;
664 else
665 port->start_cons = true;
666 } else {
667 port->start_cons = true;
668 }
69088822 669 netif_tx_unlock(dev);
d1015645 670
d51bffd1
SV
671 txq = netdev_get_tx_queue(dev, port->q_index);
672 if (unlikely(netif_tx_queue_stopped(txq) &&
4c521e42
DM
673 vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
674 return 1;
675
676 return 0;
677}
678
679static int vnet_nack(struct vnet_port *port, void *msgbuf)
680{
681 /* XXX just reset or similar XXX */
682 return 0;
683}
684
028ebff2
DM
685static int handle_mcast(struct vnet_port *port, void *msgbuf)
686{
687 struct vio_net_mcast_info *pkt = msgbuf;
688
689 if (pkt->tag.stype != VIO_SUBTYPE_ACK)
4d5870ec 690 pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
028ebff2
DM
691 port->vp->dev->name,
692 pkt->tag.type,
693 pkt->tag.stype,
694 pkt->tag.stype_env,
695 pkt->tag.sid);
696
697 return 0;
698}
699
d51bffd1
SV
700/* Got back a STOPPED LDC message on port. If the queue is stopped,
701 * wake it up so that we'll send out another START message at the
702 * next TX.
703 */
704static void maybe_tx_wakeup(struct vnet_port *port)
4c521e42 705{
d51bffd1 706 struct netdev_queue *txq;
4c521e42 707
d51bffd1
SV
708 txq = netdev_get_tx_queue(port->vp->dev, port->q_index);
709 __netif_tx_lock(txq, smp_processor_id());
710 if (likely(netif_tx_queue_stopped(txq))) {
711 struct vio_dring_state *dr;
712
713 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
6c3ce8a3 714 netif_tx_wake_queue(txq);
4c521e42 715 }
d51bffd1 716 __netif_tx_unlock(txq);
4c521e42
DM
717}
718
69088822
SV
719static inline bool port_is_up(struct vnet_port *vnet)
720{
721 struct vio_driver_state *vio = &vnet->vio;
722
723 return !!(vio->hs_state & VIO_HS_COMPLETE);
724}
725
726static int vnet_event_napi(struct vnet_port *port, int budget)
4c521e42 727{
4c521e42 728 struct vio_driver_state *vio = &port->vio;
4c521e42 729 int tx_wakeup, err;
69088822
SV
730 int npkts = 0;
731 int event = (port->rx_event & LDC_EVENT_RESET);
4c521e42 732
69088822 733ldc_ctrl:
4c521e42
DM
734 if (unlikely(event == LDC_EVENT_RESET ||
735 event == LDC_EVENT_UP)) {
736 vio_link_state_change(vio, event);
4c521e42 737
e4defc77
DS
738 if (event == LDC_EVENT_RESET) {
739 port->rmtu = 0;
368e36ed
DS
740 port->tso = true;
741 port->tsolen = 0;
d762acdb 742 vio_port_up(vio);
e4defc77 743 }
69088822
SV
744 port->rx_event = 0;
745 return 0;
4c521e42 746 }
69088822
SV
747 /* We may have multiple LDC events in rx_event. Unroll send_events() */
748 event = (port->rx_event & LDC_EVENT_UP);
749 port->rx_event &= ~(LDC_EVENT_RESET|LDC_EVENT_UP);
750 if (event == LDC_EVENT_UP)
751 goto ldc_ctrl;
752 event = port->rx_event;
753 if (!(event & LDC_EVENT_DATA_READY))
754 return 0;
4c521e42 755
69088822
SV
756 /* we dont expect any other bits than RESET, UP, DATA_READY */
757 BUG_ON(event != LDC_EVENT_DATA_READY);
4c521e42
DM
758
759 tx_wakeup = err = 0;
760 while (1) {
761 union {
762 struct vio_msg_tag tag;
763 u64 raw[8];
764 } msgbuf;
765
69088822
SV
766 if (port->napi_resume) {
767 struct vio_dring_data *pkt =
768 (struct vio_dring_data *)&msgbuf;
769 struct vio_dring_state *dr =
770 &port->vio.drings[VIO_DRIVER_RX_RING];
771
772 pkt->tag.type = VIO_TYPE_DATA;
773 pkt->tag.stype = VIO_SUBTYPE_INFO;
774 pkt->tag.stype_env = VIO_DRING_DATA;
775 pkt->seq = dr->rcv_nxt;
fe47c3c2 776 pkt->start_idx = vio_dring_next(dr, port->napi_stop_idx);
69088822
SV
777 pkt->end_idx = -1;
778 goto napi_resume;
779 }
4c521e42
DM
780 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
781 if (unlikely(err < 0)) {
782 if (err == -ECONNRESET)
783 vio_conn_reset(vio);
784 break;
785 }
786 if (err == 0)
787 break;
788 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
789 msgbuf.tag.type,
790 msgbuf.tag.stype,
791 msgbuf.tag.stype_env,
792 msgbuf.tag.sid);
793 err = vio_validate_sid(vio, &msgbuf.tag);
794 if (err < 0)
795 break;
69088822 796napi_resume:
4c521e42
DM
797 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
798 if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
69088822
SV
799 if (!port_is_up(port)) {
800 /* failures like handshake_failure()
801 * may have cleaned up dring, but
802 * NAPI polling may bring us here.
803 */
804 err = -ECONNRESET;
805 break;
806 }
807 err = vnet_rx(port, &msgbuf, &npkts, budget);
808 if (npkts >= budget)
809 break;
8c4ee3e7
SV
810 if (npkts == 0)
811 break;
4c521e42
DM
812 } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
813 err = vnet_ack(port, &msgbuf);
814 if (err > 0)
815 tx_wakeup |= err;
816 } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
817 err = vnet_nack(port, &msgbuf);
818 }
819 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
028ebff2
DM
820 if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
821 err = handle_mcast(port, &msgbuf);
822 else
823 err = vio_control_pkt_engine(vio, &msgbuf);
4c521e42
DM
824 if (err)
825 break;
826 } else {
827 err = vnet_handle_unknown(port, &msgbuf);
828 }
829 if (err == -ECONNRESET)
830 break;
831 }
4c521e42 832 if (unlikely(tx_wakeup && err != -ECONNRESET))
d51bffd1 833 maybe_tx_wakeup(port);
69088822
SV
834 return npkts;
835}
836
837static int vnet_poll(struct napi_struct *napi, int budget)
838{
839 struct vnet_port *port = container_of(napi, struct vnet_port, napi);
840 struct vio_driver_state *vio = &port->vio;
841 int processed = vnet_event_napi(port, budget);
842
843 if (processed < budget) {
844 napi_complete(napi);
7bd68bfd 845 port->rx_event &= ~LDC_EVENT_DATA_READY;
69088822
SV
846 vio_set_intr(vio->vdev->rx_ino, HV_INTR_ENABLED);
847 }
848 return processed;
849}
850
851static void vnet_event(void *arg, int event)
852{
853 struct vnet_port *port = arg;
854 struct vio_driver_state *vio = &port->vio;
855
856 port->rx_event |= event;
857 vio_set_intr(vio->vdev->rx_ino, HV_INTR_DISABLED);
858 napi_schedule(&port->napi);
1d311ad2 859
4c521e42
DM
860}
861
d1015645 862static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
4c521e42
DM
863{
864 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
865 struct vio_dring_data hdr = {
866 .tag = {
867 .type = VIO_TYPE_DATA,
868 .stype = VIO_SUBTYPE_INFO,
869 .stype_env = VIO_DRING_DATA,
870 .sid = vio_send_sid(&port->vio),
871 },
872 .dring_ident = dr->ident,
d1015645 873 .start_idx = start,
4c521e42
DM
874 .end_idx = (u32) -1,
875 };
876 int err, delay;
adddc32d 877 int retries = 0;
4c521e42 878
d1015645
SV
879 if (port->stop_rx) {
880 err = vnet_send_ack(port,
881 &port->vio.drings[VIO_DRIVER_RX_RING],
882 port->stop_rx_idx, -1,
883 VIO_DRING_STOPPED);
884 if (err <= 0)
885 return err;
886 }
887
4c521e42
DM
888 hdr.seq = dr->snd_nxt;
889 delay = 1;
890 do {
891 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
892 if (err > 0) {
893 dr->snd_nxt++;
894 break;
895 }
896 udelay(delay);
897 if ((delay <<= 1) > 128)
898 delay = 128;
adddc32d
SV
899 if (retries++ > VNET_MAX_RETRIES)
900 break;
4c521e42
DM
901 } while (err == -EAGAIN);
902
903 return err;
904}
905
906struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
907{
908 unsigned int hash = vnet_hashfn(skb->data);
909 struct hlist_head *hp = &vp->port_hash[hash];
4c521e42
DM
910 struct vnet_port *port;
911
2a968dd8 912 hlist_for_each_entry_rcu(port, hp, hash) {
8266f5fc
DS
913 if (!port_is_up(port))
914 continue;
2e42e474 915 if (ether_addr_equal(port->raddr, skb->data))
4c521e42
DM
916 return port;
917 }
2a968dd8 918 list_for_each_entry_rcu(port, &vp->port_list, list) {
8266f5fc
DS
919 if (!port->switch_port)
920 continue;
921 if (!port_is_up(port))
922 continue;
923 return port;
924 }
925 return NULL;
4c521e42
DM
926}
927
8e845f4c
DS
928static struct sk_buff *vnet_clean_tx_ring(struct vnet_port *port,
929 unsigned *pending)
930{
931 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
932 struct sk_buff *skb = NULL;
933 int i, txi;
934
935 *pending = 0;
936
937 txi = dr->prod-1;
938 if (txi < 0)
939 txi = VNET_TX_RING_SIZE-1;
940
941 for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
942 struct vio_net_desc *d;
943
944 d = vio_dring_entry(dr, txi);
945
946 if (d->hdr.state == VIO_DESC_DONE) {
947 if (port->tx_bufs[txi].skb) {
948 BUG_ON(port->tx_bufs[txi].skb->next);
949
950 port->tx_bufs[txi].skb->next = skb;
951 skb = port->tx_bufs[txi].skb;
952 port->tx_bufs[txi].skb = NULL;
953
954 ldc_unmap(port->vio.lp,
955 port->tx_bufs[txi].cookies,
956 port->tx_bufs[txi].ncookies);
957 }
958 d->hdr.state = VIO_DESC_FREE;
959 } else if (d->hdr.state == VIO_DESC_READY) {
960 (*pending)++;
961 } else if (d->hdr.state == VIO_DESC_FREE) {
962 break;
963 }
964 --txi;
965 if (txi < 0)
966 txi = VNET_TX_RING_SIZE-1;
967 }
968 return skb;
969}
970
971static inline void vnet_free_skbs(struct sk_buff *skb)
972{
973 struct sk_buff *next;
974
975 while (skb) {
976 next = skb->next;
977 skb->next = NULL;
978 dev_kfree_skb(skb);
979 skb = next;
980 }
981}
982
983static void vnet_clean_timer_expire(unsigned long port0)
984{
985 struct vnet_port *port = (struct vnet_port *)port0;
986 struct sk_buff *freeskbs;
987 unsigned pending;
8e845f4c 988
13b13dd9 989 netif_tx_lock(port->vp->dev);
8e845f4c 990 freeskbs = vnet_clean_tx_ring(port, &pending);
13b13dd9 991 netif_tx_unlock(port->vp->dev);
8e845f4c
DS
992
993 vnet_free_skbs(freeskbs);
994
995 if (pending)
996 (void)mod_timer(&port->clean_timer,
997 jiffies + VNET_CLEAN_TIMEOUT);
998 else
999 del_timer(&port->clean_timer);
1000}
1001
da38c564
DS
1002static inline int vnet_skb_map(struct ldc_channel *lp, struct sk_buff *skb,
1003 struct ldc_trans_cookie *cookies, int ncookies,
1004 unsigned int map_perm)
1005{
1006 int i, nc, err, blen;
1007
1008 /* header */
1009 blen = skb_headlen(skb);
1010 if (blen < ETH_ZLEN)
1011 blen = ETH_ZLEN;
1012 blen += VNET_PACKET_SKIP;
1013 blen += 8 - (blen & 7);
1014
1015 err = ldc_map_single(lp, skb->data-VNET_PACKET_SKIP, blen, cookies,
1016 ncookies, map_perm);
1017 if (err < 0)
1018 return err;
1019 nc = err;
1020
1021 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1022 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1023 u8 *vaddr;
1024
1025 if (nc < ncookies) {
1026 vaddr = kmap_atomic(skb_frag_page(f));
1027 blen = skb_frag_size(f);
1028 blen += 8 - (blen & 7);
1029 err = ldc_map_single(lp, vaddr + f->page_offset,
1030 blen, cookies + nc, ncookies - nc,
1031 map_perm);
1032 kunmap_atomic(vaddr);
1033 } else {
1034 err = -EMSGSIZE;
1035 }
1036
1037 if (err < 0) {
1038 ldc_unmap(lp, cookies, nc);
1039 return err;
1040 }
1041 nc += err;
1042 }
1043 return nc;
1044}
1045
1046static inline struct sk_buff *vnet_skb_shape(struct sk_buff *skb, int ncookies)
8e845f4c
DS
1047{
1048 struct sk_buff *nskb;
da38c564 1049 int i, len, pad, docopy;
8e845f4c
DS
1050
1051 len = skb->len;
1052 pad = 0;
1053 if (len < ETH_ZLEN) {
1054 pad += ETH_ZLEN - skb->len;
1055 len += pad;
1056 }
1057 len += VNET_PACKET_SKIP;
1058 pad += 8 - (len & 7);
8e845f4c 1059
da38c564
DS
1060 /* make sure we have enough cookies and alignment in every frag */
1061 docopy = skb_shinfo(skb)->nr_frags >= ncookies;
1062 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1063 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1064
1065 docopy |= f->page_offset & 7;
1066 }
8e845f4c
DS
1067 if (((unsigned long)skb->data & 7) != VNET_PACKET_SKIP ||
1068 skb_tailroom(skb) < pad ||
da38c564 1069 skb_headroom(skb) < VNET_PACKET_SKIP || docopy) {
1b6b0a47
DS
1070 int start = 0, offset;
1071 __wsum csum;
6d0ba919 1072
da38c564
DS
1073 len = skb->len > ETH_ZLEN ? skb->len : ETH_ZLEN;
1074 nskb = alloc_and_align_skb(skb->dev, len);
1075 if (nskb == NULL) {
1076 dev_kfree_skb(skb);
1077 return NULL;
1078 }
8e845f4c 1079 skb_reserve(nskb, VNET_PACKET_SKIP);
6d0ba919
DS
1080
1081 nskb->protocol = skb->protocol;
1082 offset = skb_mac_header(skb) - skb->data;
1083 skb_set_mac_header(nskb, offset);
1084 offset = skb_network_header(skb) - skb->data;
1085 skb_set_network_header(nskb, offset);
1086 offset = skb_transport_header(skb) - skb->data;
1087 skb_set_transport_header(nskb, offset);
1088
1b6b0a47 1089 offset = 0;
6d0ba919
DS
1090 nskb->csum_offset = skb->csum_offset;
1091 nskb->ip_summed = skb->ip_summed;
1092
1b6b0a47
DS
1093 if (skb->ip_summed == CHECKSUM_PARTIAL)
1094 start = skb_checksum_start_offset(skb);
1095 if (start) {
1096 struct iphdr *iph = ip_hdr(nskb);
1097 int offset = start + nskb->csum_offset;
1098
1099 if (skb_copy_bits(skb, 0, nskb->data, start)) {
1100 dev_kfree_skb(nskb);
1101 dev_kfree_skb(skb);
1102 return NULL;
1103 }
1104 *(__sum16 *)(skb->data + offset) = 0;
1105 csum = skb_copy_and_csum_bits(skb, start,
1106 nskb->data + start,
1107 skb->len - start, 0);
1108 if (iph->protocol == IPPROTO_TCP ||
1109 iph->protocol == IPPROTO_UDP) {
1110 csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
1111 skb->len - start,
1112 iph->protocol, csum);
1113 }
1114 *(__sum16 *)(nskb->data + offset) = csum;
1115
1116 nskb->ip_summed = CHECKSUM_NONE;
1117 } else if (skb_copy_bits(skb, 0, nskb->data, skb->len)) {
8e845f4c
DS
1118 dev_kfree_skb(nskb);
1119 dev_kfree_skb(skb);
1120 return NULL;
1121 }
1122 (void)skb_put(nskb, skb->len);
9a72dd4d
DS
1123 if (skb_is_gso(skb)) {
1124 skb_shinfo(nskb)->gso_size = skb_shinfo(skb)->gso_size;
1125 skb_shinfo(nskb)->gso_type = skb_shinfo(skb)->gso_type;
1126 }
8e845f4c
DS
1127 dev_kfree_skb(skb);
1128 skb = nskb;
1129 }
8e845f4c
DS
1130 return skb;
1131}
1132
d51bffd1
SV
1133static u16
1134vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
1135 void *accel_priv, select_queue_fallback_t fallback)
1136{
1137 struct vnet *vp = netdev_priv(dev);
1138 struct vnet_port *port = __tx_port_find(vp, skb);
1139
c647cc3f
DS
1140 if (port == NULL)
1141 return 0;
d51bffd1
SV
1142 return port->q_index;
1143}
1144
9a72dd4d
DS
1145static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev);
1146
1147static int vnet_handle_offloads(struct vnet_port *port, struct sk_buff *skb)
1148{
1149 struct net_device *dev = port->vp->dev;
1150 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1151 struct sk_buff *segs;
368e36ed 1152 int maclen, datalen;
9a72dd4d 1153 int status;
368e36ed
DS
1154 int gso_size, gso_type, gso_segs;
1155 int hlen = skb_transport_header(skb) - skb_mac_header(skb);
1156 int proto = IPPROTO_IP;
1157
1158 if (skb->protocol == htons(ETH_P_IP))
1159 proto = ip_hdr(skb)->protocol;
1160 else if (skb->protocol == htons(ETH_P_IPV6))
1161 proto = ipv6_hdr(skb)->nexthdr;
1162
1163 if (proto == IPPROTO_TCP)
1164 hlen += tcp_hdr(skb)->doff * 4;
1165 else if (proto == IPPROTO_UDP)
1166 hlen += sizeof(struct udphdr);
1167 else {
1168 pr_err("vnet_handle_offloads GSO with unknown transport "
1169 "protocol %d tproto %d\n", skb->protocol, proto);
1170 hlen = 128; /* XXX */
1171 }
1172 datalen = port->tsolen - hlen;
1173
1174 gso_size = skb_shinfo(skb)->gso_size;
1175 gso_type = skb_shinfo(skb)->gso_type;
1176 gso_segs = skb_shinfo(skb)->gso_segs;
1177
1178 if (port->tso && gso_size < datalen)
1179 gso_segs = DIV_ROUND_UP(skb->len - hlen, datalen);
9a72dd4d 1180
368e36ed 1181 if (unlikely(vnet_tx_dring_avail(dr) < gso_segs)) {
9a72dd4d
DS
1182 struct netdev_queue *txq;
1183
1184 txq = netdev_get_tx_queue(dev, port->q_index);
1185 netif_tx_stop_queue(txq);
1186 if (vnet_tx_dring_avail(dr) < skb_shinfo(skb)->gso_segs)
1187 return NETDEV_TX_BUSY;
1188 netif_tx_wake_queue(txq);
1189 }
1190
1191 maclen = skb_network_header(skb) - skb_mac_header(skb);
1192 skb_pull(skb, maclen);
1193
368e36ed
DS
1194 if (port->tso && gso_size < datalen) {
1195 /* segment to TSO size */
1196 skb_shinfo(skb)->gso_size = datalen;
1197 skb_shinfo(skb)->gso_segs = gso_segs;
1198
1199 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
1200
1201 /* restore gso_size & gso_segs */
1202 skb_shinfo(skb)->gso_size = gso_size;
1203 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len - hlen,
1204 gso_size);
1205 } else
1206 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO);
9a72dd4d
DS
1207 if (IS_ERR(segs)) {
1208 dev->stats.tx_dropped++;
4f2ff8ef 1209 dev_kfree_skb_any(skb);
9a72dd4d
DS
1210 return NETDEV_TX_OK;
1211 }
1212
1213 skb_push(skb, maclen);
1214 skb_reset_mac_header(skb);
1215
1216 status = 0;
1217 while (segs) {
1218 struct sk_buff *curr = segs;
1219
1220 segs = segs->next;
1221 curr->next = NULL;
368e36ed
DS
1222 if (port->tso && curr->len > dev->mtu) {
1223 skb_shinfo(curr)->gso_size = gso_size;
1224 skb_shinfo(curr)->gso_type = gso_type;
1225 skb_shinfo(curr)->gso_segs =
1226 DIV_ROUND_UP(curr->len - hlen, gso_size);
1227 } else
1228 skb_shinfo(curr)->gso_size = 0;
9a72dd4d
DS
1229
1230 skb_push(curr, maclen);
1231 skb_reset_mac_header(curr);
1232 memcpy(skb_mac_header(curr), skb_mac_header(skb),
1233 maclen);
1234 curr->csum_start = skb_transport_header(curr) - curr->head;
1235 if (ip_hdr(curr)->protocol == IPPROTO_TCP)
1236 curr->csum_offset = offsetof(struct tcphdr, check);
1237 else if (ip_hdr(curr)->protocol == IPPROTO_UDP)
1238 curr->csum_offset = offsetof(struct udphdr, check);
1239
1240 if (!(status & NETDEV_TX_MASK))
1241 status = vnet_start_xmit(curr, dev);
1242 if (status & NETDEV_TX_MASK)
1243 dev_kfree_skb_any(curr);
1244 }
1245
1246 if (!(status & NETDEV_TX_MASK))
1247 dev_kfree_skb_any(skb);
1248 return status;
1249}
1250
4c521e42
DM
1251static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
1252{
1253 struct vnet *vp = netdev_priv(dev);
2a968dd8 1254 struct vnet_port *port = NULL;
4c521e42
DM
1255 struct vio_dring_state *dr;
1256 struct vio_net_desc *d;
4c521e42 1257 unsigned int len;
8e845f4c
DS
1258 struct sk_buff *freeskbs = NULL;
1259 int i, err, txi;
8e845f4c 1260 unsigned pending = 0;
d51bffd1 1261 struct netdev_queue *txq;
4c521e42 1262
2a968dd8 1263 rcu_read_lock();
13b13dd9 1264 port = __tx_port_find(vp, skb);
df20286a
SV
1265 if (unlikely(!port)) {
1266 rcu_read_unlock();
2a968dd8 1267 goto out_dropped;
df20286a 1268 }
2a968dd8 1269
368e36ed 1270 if (skb_is_gso(skb) && skb->len > port->tsolen) {
9a72dd4d
DS
1271 err = vnet_handle_offloads(port, skb);
1272 rcu_read_unlock();
1273 return err;
1274 }
1275
368e36ed 1276 if (!skb_is_gso(skb) && skb->len > port->rmtu) {
a2b78e9b
DS
1277 unsigned long localmtu = port->rmtu - ETH_HLEN;
1278
1279 if (vio_version_after_eq(&port->vio, 1, 3))
1280 localmtu -= VLAN_HLEN;
1281
1282 if (skb->protocol == htons(ETH_P_IP)) {
1283 struct flowi4 fl4;
1284 struct rtable *rt = NULL;
1285
1286 memset(&fl4, 0, sizeof(fl4));
1287 fl4.flowi4_oif = dev->ifindex;
1288 fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
1289 fl4.daddr = ip_hdr(skb)->daddr;
1290 fl4.saddr = ip_hdr(skb)->saddr;
1291
1292 rt = ip_route_output_key(dev_net(dev), &fl4);
2a968dd8 1293 rcu_read_unlock();
a2b78e9b
DS
1294 if (!IS_ERR(rt)) {
1295 skb_dst_set(skb, &rt->dst);
1296 icmp_send(skb, ICMP_DEST_UNREACH,
1297 ICMP_FRAG_NEEDED,
1298 htonl(localmtu));
1299 }
1300 }
1301#if IS_ENABLED(CONFIG_IPV6)
1302 else if (skb->protocol == htons(ETH_P_IPV6))
1303 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
1304#endif
42db672d 1305 goto out_dropped;
a2b78e9b 1306 }
42db672d 1307
da38c564
DS
1308 skb = vnet_skb_shape(skb, 2);
1309
9cdfe2c7 1310 if (unlikely(!skb))
da38c564 1311 goto out_dropped;
da38c564 1312
1b6b0a47
DS
1313 if (skb->ip_summed == CHECKSUM_PARTIAL)
1314 vnet_fullcsum(skb);
1315
4c521e42 1316 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
d51bffd1
SV
1317 i = skb_get_queue_mapping(skb);
1318 txq = netdev_get_tx_queue(dev, i);
d0aedcd4 1319 if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
d51bffd1
SV
1320 if (!netif_tx_queue_stopped(txq)) {
1321 netif_tx_stop_queue(txq);
4c521e42
DM
1322
1323 /* This is a hard error, log it. */
4d5870ec 1324 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
4c521e42
DM
1325 dev->stats.tx_errors++;
1326 }
2a968dd8 1327 rcu_read_unlock();
4c521e42
DM
1328 return NETDEV_TX_BUSY;
1329 }
1330
1331 d = vio_dring_cur(dr);
1332
8e845f4c
DS
1333 txi = dr->prod;
1334
1335 freeskbs = vnet_clean_tx_ring(port, &pending);
1336
1337 BUG_ON(port->tx_bufs[txi].skb);
4c521e42
DM
1338
1339 len = skb->len;
8e845f4c 1340 if (len < ETH_ZLEN)
4c521e42 1341 len = ETH_ZLEN;
8e845f4c 1342
da38c564
DS
1343 err = vnet_skb_map(port->vio.lp, skb, port->tx_bufs[txi].cookies, 2,
1344 (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
8e845f4c
DS
1345 if (err < 0) {
1346 netdev_info(dev, "tx buffer map error %d\n", err);
13b13dd9 1347 goto out_dropped;
4c521e42 1348 }
da38c564
DS
1349
1350 port->tx_bufs[txi].skb = skb;
1351 skb = NULL;
8e845f4c 1352 port->tx_bufs[txi].ncookies = err;
4c521e42 1353
1f6394e3
SV
1354 /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
1355 * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
1356 * the protocol itself does not require it as long as the peer
1357 * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
1358 *
1359 * An ACK for every packet in the ring is expensive as the
1360 * sending of LDC messages is slow and affects performance.
1361 */
1362 d->hdr.ack = VIO_ACK_DISABLE;
4c521e42 1363 d->size = len;
8e845f4c 1364 d->ncookies = port->tx_bufs[txi].ncookies;
4c521e42 1365 for (i = 0; i < d->ncookies; i++)
8e845f4c 1366 d->cookies[i] = port->tx_bufs[txi].cookies[i];
6d0ba919
DS
1367 if (vio_version_after_eq(&port->vio, 1, 7)) {
1368 struct vio_net_dext *dext = vio_net_ext(d);
1369
1370 memset(dext, 0, sizeof(*dext));
368e36ed
DS
1371 if (skb_is_gso(port->tx_bufs[txi].skb)) {
1372 dext->ipv4_lso_mss = skb_shinfo(port->tx_bufs[txi].skb)
1373 ->gso_size;
1374 dext->flags |= VNET_PKT_IPV4_LSO;
1375 }
6d0ba919
DS
1376 if (vio_version_after_eq(&port->vio, 1, 8) &&
1377 !port->switch_port) {
1378 dext->flags |= VNET_PKT_HCK_IPV4_HDRCKSUM_OK;
1379 dext->flags |= VNET_PKT_HCK_FULLCKSUM_OK;
1380 }
1381 }
4c521e42
DM
1382
1383 /* This has to be a non-SMP write barrier because we are writing
1384 * to memory which is shared with the peer LDOM.
1385 */
1386 wmb();
1387
1388 d->hdr.state = VIO_DESC_READY;
1389
d1015645
SV
1390 /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
1391 * to notify the consumer that some descriptors are READY.
1392 * After that "start" trigger, no additional triggers are needed until
1393 * a DRING_STOPPED is received from the consumer. The dr->cons field
1394 * (set up by vnet_ack()) has the value of the next dring index
1395 * that has not yet been ack-ed. We send a "start" trigger here
1396 * if, and only if, start_cons is true (reset it afterward). Conversely,
1397 * vnet_ack() should check if the dring corresponding to cons
1398 * is marked READY, but start_cons was false.
1399 * If so, vnet_ack() should send out the missed "start" trigger.
1400 *
1401 * Note that the wmb() above makes sure the cookies et al. are
1402 * not globally visible before the VIO_DESC_READY, and that the
1403 * stores are ordered correctly by the compiler. The consumer will
1404 * not proceed until the VIO_DESC_READY is visible assuring that
1405 * the consumer does not observe anything related to descriptors
1406 * out of order. The HV trap from the LDC start trigger is the
1407 * producer to consumer announcement that work is available to the
1408 * consumer
1409 */
1410 if (!port->start_cons)
1411 goto ldc_start_done; /* previous trigger suffices */
1412
1413 err = __vnet_tx_trigger(port, dr->cons);
4c521e42 1414 if (unlikely(err < 0)) {
4d5870ec 1415 netdev_info(dev, "TX trigger error %d\n", err);
4c521e42
DM
1416 d->hdr.state = VIO_DESC_FREE;
1417 dev->stats.tx_carrier_errors++;
13b13dd9 1418 goto out_dropped;
4c521e42
DM
1419 }
1420
d1015645
SV
1421ldc_start_done:
1422 port->start_cons = false;
1423
4c521e42 1424 dev->stats.tx_packets++;
8e845f4c 1425 dev->stats.tx_bytes += port->tx_bufs[txi].skb->len;
4c521e42
DM
1426
1427 dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
d0aedcd4 1428 if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
d51bffd1 1429 netif_tx_stop_queue(txq);
4c521e42 1430 if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
d51bffd1 1431 netif_tx_wake_queue(txq);
4c521e42
DM
1432 }
1433
2a968dd8
SV
1434 (void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
1435 rcu_read_unlock();
4c521e42 1436
8e845f4c
DS
1437 vnet_free_skbs(freeskbs);
1438
4c521e42
DM
1439 return NETDEV_TX_OK;
1440
4c521e42 1441out_dropped:
8e845f4c
DS
1442 if (pending)
1443 (void)mod_timer(&port->clean_timer,
1444 jiffies + VNET_CLEAN_TIMEOUT);
a29c9c43 1445 else if (port)
8e845f4c 1446 del_timer(&port->clean_timer);
2a968dd8
SV
1447 if (port)
1448 rcu_read_unlock();
1449 if (skb)
1450 dev_kfree_skb(skb);
1451 vnet_free_skbs(freeskbs);
4c521e42
DM
1452 dev->stats.tx_dropped++;
1453 return NETDEV_TX_OK;
1454}
1455
1456static void vnet_tx_timeout(struct net_device *dev)
1457{
1458 /* XXX Implement me XXX */
1459}
1460
1461static int vnet_open(struct net_device *dev)
1462{
1463 netif_carrier_on(dev);
d51bffd1 1464 netif_tx_start_all_queues(dev);
4c521e42
DM
1465
1466 return 0;
1467}
1468
1469static int vnet_close(struct net_device *dev)
1470{
d51bffd1 1471 netif_tx_stop_all_queues(dev);
4c521e42
DM
1472 netif_carrier_off(dev);
1473
1474 return 0;
1475}
1476
028ebff2
DM
1477static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
1478{
1479 struct vnet_mcast_entry *m;
1480
1481 for (m = vp->mcast_list; m; m = m->next) {
00fa4ce9 1482 if (ether_addr_equal(m->addr, addr))
028ebff2
DM
1483 return m;
1484 }
1485 return NULL;
1486}
1487
1488static void __update_mc_list(struct vnet *vp, struct net_device *dev)
1489{
22bedad3 1490 struct netdev_hw_addr *ha;
028ebff2 1491
22bedad3 1492 netdev_for_each_mc_addr(ha, dev) {
028ebff2
DM
1493 struct vnet_mcast_entry *m;
1494
22bedad3 1495 m = __vnet_mc_find(vp, ha->addr);
028ebff2
DM
1496 if (m) {
1497 m->hit = 1;
1498 continue;
1499 }
1500
1501 if (!m) {
1502 m = kzalloc(sizeof(*m), GFP_ATOMIC);
1503 if (!m)
1504 continue;
22bedad3 1505 memcpy(m->addr, ha->addr, ETH_ALEN);
028ebff2
DM
1506 m->hit = 1;
1507
1508 m->next = vp->mcast_list;
1509 vp->mcast_list = m;
1510 }
1511 }
1512}
1513
1514static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
1515{
1516 struct vio_net_mcast_info info;
1517 struct vnet_mcast_entry *m, **pp;
1518 int n_addrs;
1519
1520 memset(&info, 0, sizeof(info));
1521
1522 info.tag.type = VIO_TYPE_CTRL;
1523 info.tag.stype = VIO_SUBTYPE_INFO;
1524 info.tag.stype_env = VNET_MCAST_INFO;
1525 info.tag.sid = vio_send_sid(&port->vio);
1526 info.set = 1;
1527
1528 n_addrs = 0;
1529 for (m = vp->mcast_list; m; m = m->next) {
1530 if (m->sent)
1531 continue;
1532 m->sent = 1;
1533 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1534 m->addr, ETH_ALEN);
1535 if (++n_addrs == VNET_NUM_MCAST) {
1536 info.count = n_addrs;
1537
1538 (void) vio_ldc_send(&port->vio, &info,
1539 sizeof(info));
1540 n_addrs = 0;
1541 }
1542 }
1543 if (n_addrs) {
1544 info.count = n_addrs;
1545 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1546 }
1547
1548 info.set = 0;
1549
1550 n_addrs = 0;
1551 pp = &vp->mcast_list;
1552 while ((m = *pp) != NULL) {
1553 if (m->hit) {
1554 m->hit = 0;
1555 pp = &m->next;
1556 continue;
1557 }
1558
1559 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1560 m->addr, ETH_ALEN);
1561 if (++n_addrs == VNET_NUM_MCAST) {
1562 info.count = n_addrs;
1563 (void) vio_ldc_send(&port->vio, &info,
1564 sizeof(info));
1565 n_addrs = 0;
1566 }
1567
1568 *pp = m->next;
1569 kfree(m);
1570 }
1571 if (n_addrs) {
1572 info.count = n_addrs;
1573 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1574 }
1575}
1576
4c521e42
DM
1577static void vnet_set_rx_mode(struct net_device *dev)
1578{
028ebff2
DM
1579 struct vnet *vp = netdev_priv(dev);
1580 struct vnet_port *port;
028ebff2 1581
2a968dd8
SV
1582 rcu_read_lock();
1583 list_for_each_entry_rcu(port, &vp->port_list, list) {
028ebff2
DM
1584
1585 if (port->switch_port) {
1586 __update_mc_list(vp, dev);
1587 __send_mc_list(vp, port);
2a968dd8 1588 break;
028ebff2
DM
1589 }
1590 }
2a968dd8 1591 rcu_read_unlock();
4c521e42
DM
1592}
1593
1594static int vnet_change_mtu(struct net_device *dev, int new_mtu)
1595{
42db672d 1596 if (new_mtu < 68 || new_mtu > 65535)
4c521e42
DM
1597 return -EINVAL;
1598
1599 dev->mtu = new_mtu;
1600 return 0;
1601}
1602
1603static int vnet_set_mac_addr(struct net_device *dev, void *p)
1604{
1605 return -EINVAL;
1606}
1607
1608static void vnet_get_drvinfo(struct net_device *dev,
1609 struct ethtool_drvinfo *info)
1610{
7826d43f
JP
1611 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1612 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
4c521e42
DM
1613}
1614
1615static u32 vnet_get_msglevel(struct net_device *dev)
1616{
1617 struct vnet *vp = netdev_priv(dev);
1618 return vp->msg_enable;
1619}
1620
1621static void vnet_set_msglevel(struct net_device *dev, u32 value)
1622{
1623 struct vnet *vp = netdev_priv(dev);
1624 vp->msg_enable = value;
1625}
1626
1627static const struct ethtool_ops vnet_ethtool_ops = {
1628 .get_drvinfo = vnet_get_drvinfo,
1629 .get_msglevel = vnet_get_msglevel,
1630 .set_msglevel = vnet_set_msglevel,
1631 .get_link = ethtool_op_get_link,
4c521e42
DM
1632};
1633
1634static void vnet_port_free_tx_bufs(struct vnet_port *port)
1635{
1636 struct vio_dring_state *dr;
1637 int i;
1638
1639 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1640 if (dr->base) {
1641 ldc_free_exp_dring(port->vio.lp, dr->base,
1642 (dr->entry_size * dr->num_entries),
1643 dr->cookies, dr->ncookies);
1644 dr->base = NULL;
1645 dr->entry_size = 0;
1646 dr->num_entries = 0;
1647 dr->pending = 0;
1648 dr->ncookies = 0;
1649 }
1650
1651 for (i = 0; i < VNET_TX_RING_SIZE; i++) {
8e845f4c
DS
1652 struct vio_net_desc *d;
1653 void *skb = port->tx_bufs[i].skb;
4c521e42 1654
8e845f4c 1655 if (!skb)
4c521e42
DM
1656 continue;
1657
8e845f4c
DS
1658 d = vio_dring_entry(dr, i);
1659 if (d->hdr.state == VIO_DESC_READY)
1660 pr_warn("active transmit buffers freed\n");
1661
4c521e42
DM
1662 ldc_unmap(port->vio.lp,
1663 port->tx_bufs[i].cookies,
1664 port->tx_bufs[i].ncookies);
8e845f4c
DS
1665 dev_kfree_skb(skb);
1666 port->tx_bufs[i].skb = NULL;
1667 d->hdr.state = VIO_DESC_FREE;
4c521e42
DM
1668 }
1669}
1670
d6732489 1671static int vnet_port_alloc_tx_ring(struct vnet_port *port)
4c521e42
DM
1672{
1673 struct vio_dring_state *dr;
6d0ba919 1674 unsigned long len, elen;
4c521e42
DM
1675 int i, err, ncookies;
1676 void *dring;
1677
4c521e42
DM
1678 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1679
6d0ba919
DS
1680 elen = sizeof(struct vio_net_desc) +
1681 sizeof(struct ldc_trans_cookie) * 2;
1682 if (vio_version_after_eq(&port->vio, 1, 7))
1683 elen += sizeof(struct vio_net_dext);
1684 len = VNET_TX_RING_SIZE * elen;
4c521e42
DM
1685
1686 ncookies = VIO_MAX_RING_COOKIES;
1687 dring = ldc_alloc_exp_dring(port->vio.lp, len,
1688 dr->cookies, &ncookies,
1689 (LDC_MAP_SHADOW |
1690 LDC_MAP_DIRECT |
1691 LDC_MAP_RW));
1692 if (IS_ERR(dring)) {
1693 err = PTR_ERR(dring);
1694 goto err_out;
1695 }
1696
1697 dr->base = dring;
6d0ba919 1698 dr->entry_size = elen;
4c521e42
DM
1699 dr->num_entries = VNET_TX_RING_SIZE;
1700 dr->prod = dr->cons = 0;
d1015645 1701 port->start_cons = true; /* need an initial trigger */
4c521e42
DM
1702 dr->pending = VNET_TX_RING_SIZE;
1703 dr->ncookies = ncookies;
1704
8e845f4c
DS
1705 for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
1706 struct vio_net_desc *d;
1707
1708 d = vio_dring_entry(dr, i);
1709 d->hdr.state = VIO_DESC_FREE;
1710 }
4c521e42
DM
1711 return 0;
1712
1713err_out:
1714 vnet_port_free_tx_bufs(port);
1715
1716 return err;
1717}
1718
69088822
SV
1719#ifdef CONFIG_NET_POLL_CONTROLLER
1720static void vnet_poll_controller(struct net_device *dev)
1721{
1722 struct vnet *vp = netdev_priv(dev);
1723 struct vnet_port *port;
1724 unsigned long flags;
1725
1726 spin_lock_irqsave(&vp->lock, flags);
1727 if (!list_empty(&vp->port_list)) {
1728 port = list_entry(vp->port_list.next, struct vnet_port, list);
1729 napi_schedule(&port->napi);
1730 }
1731 spin_unlock_irqrestore(&vp->lock, flags);
1732}
1733#endif
9184a046
DM
1734static LIST_HEAD(vnet_list);
1735static DEFINE_MUTEX(vnet_list_mutex);
1736
8fd17f2e
DM
1737static const struct net_device_ops vnet_ops = {
1738 .ndo_open = vnet_open,
1739 .ndo_stop = vnet_close,
afc4b13d 1740 .ndo_set_rx_mode = vnet_set_rx_mode,
8fd17f2e 1741 .ndo_set_mac_address = vnet_set_mac_addr,
240c102d 1742 .ndo_validate_addr = eth_validate_addr,
8fd17f2e
DM
1743 .ndo_tx_timeout = vnet_tx_timeout,
1744 .ndo_change_mtu = vnet_change_mtu,
1745 .ndo_start_xmit = vnet_start_xmit,
d51bffd1 1746 .ndo_select_queue = vnet_select_queue,
69088822
SV
1747#ifdef CONFIG_NET_POLL_CONTROLLER
1748 .ndo_poll_controller = vnet_poll_controller,
1749#endif
8fd17f2e
DM
1750};
1751
f73d12bd 1752static struct vnet *vnet_new(const u64 *local_mac)
9184a046
DM
1753{
1754 struct net_device *dev;
1755 struct vnet *vp;
1756 int err, i;
1757
d51bffd1 1758 dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
41de8d4c 1759 if (!dev)
9184a046 1760 return ERR_PTR(-ENOMEM);
8e845f4c
DS
1761 dev->needed_headroom = VNET_PACKET_SKIP + 8;
1762 dev->needed_tailroom = 8;
9184a046
DM
1763
1764 for (i = 0; i < ETH_ALEN; i++)
1765 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
1766
9184a046
DM
1767 vp = netdev_priv(dev);
1768
1769 spin_lock_init(&vp->lock);
1770 vp->dev = dev;
1771
1772 INIT_LIST_HEAD(&vp->port_list);
1773 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
1774 INIT_HLIST_HEAD(&vp->port_hash[i]);
1775 INIT_LIST_HEAD(&vp->list);
1776 vp->local_mac = *local_mac;
1777
8fd17f2e 1778 dev->netdev_ops = &vnet_ops;
9184a046
DM
1779 dev->ethtool_ops = &vnet_ethtool_ops;
1780 dev->watchdog_timeo = VNET_TX_TIMEOUT;
9184a046 1781
368e36ed 1782 dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_GSO_SOFTWARE |
9a72dd4d 1783 NETIF_F_HW_CSUM | NETIF_F_SG;
da38c564
DS
1784 dev->features = dev->hw_features;
1785
9184a046
DM
1786 err = register_netdev(dev);
1787 if (err) {
4d5870ec 1788 pr_err("Cannot register net device, aborting\n");
9184a046
DM
1789 goto err_out_free_dev;
1790 }
1791
4d5870ec 1792 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
9184a046
DM
1793
1794 list_add(&vp->list, &vnet_list);
1795
1796 return vp;
1797
1798err_out_free_dev:
1799 free_netdev(dev);
1800
1801 return ERR_PTR(err);
1802}
1803
f73d12bd 1804static struct vnet *vnet_find_or_create(const u64 *local_mac)
9184a046
DM
1805{
1806 struct vnet *iter, *vp;
1807
1808 mutex_lock(&vnet_list_mutex);
1809 vp = NULL;
1810 list_for_each_entry(iter, &vnet_list, list) {
1811 if (iter->local_mac == *local_mac) {
1812 vp = iter;
1813 break;
1814 }
1815 }
1816 if (!vp)
1817 vp = vnet_new(local_mac);
1818 mutex_unlock(&vnet_list_mutex);
1819
1820 return vp;
1821}
1822
a4b70a07
SV
1823static void vnet_cleanup(void)
1824{
1825 struct vnet *vp;
1826 struct net_device *dev;
1827
1828 mutex_lock(&vnet_list_mutex);
1829 while (!list_empty(&vnet_list)) {
1830 vp = list_first_entry(&vnet_list, struct vnet, list);
1831 list_del(&vp->list);
1832 dev = vp->dev;
1833 /* vio_unregister_driver() should have cleaned up port_list */
1834 BUG_ON(!list_empty(&vp->port_list));
1835 unregister_netdev(dev);
1836 free_netdev(dev);
1837 }
1838 mutex_unlock(&vnet_list_mutex);
1839}
1840
9184a046
DM
1841static const char *local_mac_prop = "local-mac-address";
1842
f73d12bd 1843static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
9184a046
DM
1844 u64 port_node)
1845{
1846 const u64 *local_mac = NULL;
1847 u64 a;
1848
1849 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
1850 u64 target = mdesc_arc_target(hp, a);
1851 const char *name;
1852
1853 name = mdesc_get_property(hp, target, "name", NULL);
1854 if (!name || strcmp(name, "network"))
1855 continue;
1856
1857 local_mac = mdesc_get_property(hp, target,
1858 local_mac_prop, NULL);
1859 if (local_mac)
1860 break;
1861 }
1862 if (!local_mac)
1863 return ERR_PTR(-ENODEV);
1864
1865 return vnet_find_or_create(local_mac);
1866}
1867
4c521e42
DM
1868static struct ldc_channel_config vnet_ldc_cfg = {
1869 .event = vnet_event,
1870 .mtu = 64,
1871 .mode = LDC_MODE_UNRELIABLE,
1872};
1873
1874static struct vio_driver_ops vnet_vio_ops = {
1875 .send_attr = vnet_send_attr,
1876 .handle_attr = vnet_handle_attr,
1877 .handshake_complete = vnet_handshake_complete,
1878};
1879
f73d12bd 1880static void print_version(void)
9184a046 1881{
4d5870ec 1882 printk_once(KERN_INFO "%s", version);
9184a046
DM
1883}
1884
4c521e42
DM
1885const char *remote_macaddr_prop = "remote-mac-address";
1886
d51bffd1
SV
1887static void
1888vnet_port_add_txq(struct vnet_port *port)
1889{
1890 struct vnet *vp = port->vp;
1891 int n;
1892
1893 n = vp->nports++;
1894 n = n & (VNET_MAX_TXQS - 1);
1895 port->q_index = n;
1896 netif_tx_wake_queue(netdev_get_tx_queue(vp->dev, port->q_index));
1897}
1898
1899static void
1900vnet_port_rm_txq(struct vnet_port *port)
1901{
1902 port->vp->nports--;
1903 netif_tx_stop_queue(netdev_get_tx_queue(port->vp->dev, port->q_index));
1904}
1905
1dd06ae8 1906static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
4c521e42 1907{
43fdf274 1908 struct mdesc_handle *hp;
4c521e42
DM
1909 struct vnet_port *port;
1910 unsigned long flags;
1911 struct vnet *vp;
1912 const u64 *rmac;
1913 int len, i, err, switch_port;
1914
9184a046 1915 print_version();
4c521e42 1916
43fdf274
DM
1917 hp = mdesc_grab();
1918
9184a046
DM
1919 vp = vnet_find_parent(hp, vdev->mp);
1920 if (IS_ERR(vp)) {
4d5870ec 1921 pr_err("Cannot find port parent vnet\n");
9184a046
DM
1922 err = PTR_ERR(vp);
1923 goto err_out_put_mdesc;
1924 }
1925
43fdf274
DM
1926 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
1927 err = -ENODEV;
4c521e42 1928 if (!rmac) {
4d5870ec 1929 pr_err("Port lacks %s property\n", remote_macaddr_prop);
43fdf274 1930 goto err_out_put_mdesc;
4c521e42
DM
1931 }
1932
1933 port = kzalloc(sizeof(*port), GFP_KERNEL);
43fdf274 1934 err = -ENOMEM;
e404decb 1935 if (!port)
43fdf274 1936 goto err_out_put_mdesc;
4c521e42
DM
1937
1938 for (i = 0; i < ETH_ALEN; i++)
1939 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
1940
1941 port->vp = vp;
1942
43fdf274 1943 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
4c521e42
DM
1944 vnet_versions, ARRAY_SIZE(vnet_versions),
1945 &vnet_vio_ops, vp->dev->name);
1946 if (err)
1947 goto err_out_free_port;
1948
1949 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
1950 if (err)
1951 goto err_out_free_port;
1952
69088822
SV
1953 netif_napi_add(port->vp->dev, &port->napi, vnet_poll, NAPI_POLL_WEIGHT);
1954
4c521e42
DM
1955 INIT_HLIST_NODE(&port->hash);
1956 INIT_LIST_HEAD(&port->list);
1957
1958 switch_port = 0;
43fdf274 1959 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL) != NULL)
4c521e42 1960 switch_port = 1;
028ebff2 1961 port->switch_port = switch_port;
368e36ed
DS
1962 port->tso = true;
1963 port->tsolen = 0;
4c521e42
DM
1964
1965 spin_lock_irqsave(&vp->lock, flags);
1966 if (switch_port)
2a968dd8 1967 list_add_rcu(&port->list, &vp->port_list);
4c521e42 1968 else
2a968dd8
SV
1969 list_add_tail_rcu(&port->list, &vp->port_list);
1970 hlist_add_head_rcu(&port->hash,
1971 &vp->port_hash[vnet_hashfn(port->raddr)]);
d51bffd1 1972 vnet_port_add_txq(port);
4c521e42
DM
1973 spin_unlock_irqrestore(&vp->lock, flags);
1974
1975 dev_set_drvdata(&vdev->dev, port);
1976
4d5870ec
JP
1977 pr_info("%s: PORT ( remote-mac %pM%s )\n",
1978 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
4c521e42 1979
8e845f4c
DS
1980 setup_timer(&port->clean_timer, vnet_clean_timer_expire,
1981 (unsigned long)port);
1982
69088822 1983 napi_enable(&port->napi);
4c521e42
DM
1984 vio_port_up(&port->vio);
1985
43fdf274
DM
1986 mdesc_release(hp);
1987
4c521e42
DM
1988 return 0;
1989
4c521e42
DM
1990err_out_free_port:
1991 kfree(port);
1992
43fdf274
DM
1993err_out_put_mdesc:
1994 mdesc_release(hp);
4c521e42
DM
1995 return err;
1996}
1997
1998static int vnet_port_remove(struct vio_dev *vdev)
1999{
2000 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
2001
2002 if (port) {
4c521e42
DM
2003
2004 del_timer_sync(&port->vio.timer);
2005
69088822 2006 napi_disable(&port->napi);
4c521e42 2007
2a968dd8
SV
2008 list_del_rcu(&port->list);
2009 hlist_del_rcu(&port->hash);
2010
2011 synchronize_rcu();
2012 del_timer_sync(&port->clean_timer);
d51bffd1 2013 vnet_port_rm_txq(port);
69088822 2014 netif_napi_del(&port->napi);
4c521e42
DM
2015 vnet_port_free_tx_bufs(port);
2016 vio_ldc_free(&port->vio);
2017
2018 dev_set_drvdata(&vdev->dev, NULL);
2019
2020 kfree(port);
aabb9875 2021
4c521e42
DM
2022 }
2023 return 0;
2024}
2025
3d452e55 2026static const struct vio_device_id vnet_port_match[] = {
4c521e42
DM
2027 {
2028 .type = "vnet-port",
2029 },
2030 {},
2031};
da68e081 2032MODULE_DEVICE_TABLE(vio, vnet_port_match);
4c521e42
DM
2033
2034static struct vio_driver vnet_port_driver = {
2035 .id_table = vnet_port_match,
2036 .probe = vnet_port_probe,
2037 .remove = vnet_port_remove,
cb52d897 2038 .name = "vnet_port",
4c521e42
DM
2039};
2040
4c521e42
DM
2041static int __init vnet_init(void)
2042{
9184a046 2043 return vio_register_driver(&vnet_port_driver);
4c521e42
DM
2044}
2045
2046static void __exit vnet_exit(void)
2047{
2048 vio_unregister_driver(&vnet_port_driver);
a4b70a07 2049 vnet_cleanup();
4c521e42
DM
2050}
2051
2052module_init(vnet_init);
2053module_exit(vnet_exit);
This page took 1.011177 seconds and 5 git commands to generate.