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