1 /* sunvnet.c: Sun LDOM Virtual Network Driver.
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 * Copyright (C) 2016 Oracle. All rights reserved.
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/etherdevice.h>
16 #include <linux/mutex.h>
17 #include <linux/highmem.h>
18 #include <linux/if_vlan.h>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/sunvnet.h>
22 #if IS_ENABLED(CONFIG_IPV6)
23 #include <linux/icmpv6.h>
28 #include <net/route.h>
33 #include "sunvnet_common.h"
35 /* Heuristic for the number of times to exponentially backoff and
36 * retry sending an LDC trigger when EAGAIN is encountered
38 #define VNET_MAX_RETRIES 10
40 static int __vnet_tx_trigger(struct vnet_port
*port
, u32 start
);
41 static void vnet_port_reset(struct vnet_port
*port
);
43 static inline u32
vnet_tx_dring_avail(struct vio_dring_state
*dr
)
45 return vio_dring_avail(dr
, VNET_TX_RING_SIZE
);
48 static int vnet_handle_unknown(struct vnet_port
*port
, void *arg
)
50 struct vio_msg_tag
*pkt
= arg
;
52 pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
53 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
54 pr_err("Resetting connection\n");
56 ldc_disconnect(port
->vio
.lp
);
61 static int vnet_port_alloc_tx_ring(struct vnet_port
*port
);
63 int sunvnet_send_attr_common(struct vio_driver_state
*vio
)
65 struct vnet_port
*port
= to_vnet_port(vio
);
66 struct net_device
*dev
= VNET_PORT_TO_NET_DEVICE(port
);
67 struct vio_net_attr_info pkt
;
68 int framelen
= ETH_FRAME_LEN
;
71 err
= vnet_port_alloc_tx_ring(to_vnet_port(vio
));
75 memset(&pkt
, 0, sizeof(pkt
));
76 pkt
.tag
.type
= VIO_TYPE_CTRL
;
77 pkt
.tag
.stype
= VIO_SUBTYPE_INFO
;
78 pkt
.tag
.stype_env
= VIO_ATTR_INFO
;
79 pkt
.tag
.sid
= vio_send_sid(vio
);
80 if (vio_version_before(vio
, 1, 2))
81 pkt
.xfer_mode
= VIO_DRING_MODE
;
83 pkt
.xfer_mode
= VIO_NEW_DRING_MODE
;
84 pkt
.addr_type
= VNET_ADDR_ETHERMAC
;
86 for (i
= 0; i
< 6; i
++)
87 pkt
.addr
|= (u64
)dev
->dev_addr
[i
] << ((5 - i
) * 8);
88 if (vio_version_after(vio
, 1, 3)) {
90 port
->rmtu
= min(VNET_MAXPACKET
, port
->rmtu
);
93 port
->rmtu
= VNET_MAXPACKET
;
96 if (vio_version_after_eq(vio
, 1, 6))
97 pkt
.options
= VIO_TX_DRING
;
98 } else if (vio_version_before(vio
, 1, 3)) {
101 pkt
.mtu
= framelen
+ VLAN_HLEN
;
105 if (vio_version_after_eq(vio
, 1, 7) && port
->tso
) {
106 pkt
.cflags
|= VNET_LSO_IPV4_CAPAB
;
108 port
->tsolen
= VNET_MAXTSO
;
109 pkt
.ipv4_lso_maxlen
= port
->tsolen
;
112 pkt
.plnk_updt
= PHYSLINK_UPDATE_NONE
;
114 viodbg(HS
, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
115 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
116 "cflags[0x%04x] lso_max[%u]\n",
117 pkt
.xfer_mode
, pkt
.addr_type
,
118 (unsigned long long)pkt
.addr
,
119 pkt
.ack_freq
, pkt
.plnk_updt
, pkt
.options
,
120 (unsigned long long)pkt
.mtu
, pkt
.cflags
, pkt
.ipv4_lso_maxlen
);
122 return vio_ldc_send(vio
, &pkt
, sizeof(pkt
));
124 EXPORT_SYMBOL_GPL(sunvnet_send_attr_common
);
126 static int handle_attr_info(struct vio_driver_state
*vio
,
127 struct vio_net_attr_info
*pkt
)
129 struct vnet_port
*port
= to_vnet_port(vio
);
133 viodbg(HS
, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
134 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
135 " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
136 pkt
->xfer_mode
, pkt
->addr_type
,
137 (unsigned long long)pkt
->addr
,
138 pkt
->ack_freq
, pkt
->plnk_updt
, pkt
->options
,
139 (unsigned long long)pkt
->mtu
, port
->rmtu
, pkt
->cflags
,
140 pkt
->ipv4_lso_maxlen
);
142 pkt
->tag
.sid
= vio_send_sid(vio
);
144 xfer_mode
= pkt
->xfer_mode
;
145 /* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
146 if (vio_version_before(vio
, 1, 2) && xfer_mode
== VIO_DRING_MODE
)
147 xfer_mode
= VIO_NEW_DRING_MODE
;
150 * < v1.3 - ETH_FRAME_LEN exactly
151 * > v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
153 * = v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
155 if (vio_version_before(vio
, 1, 3)) {
156 localmtu
= ETH_FRAME_LEN
;
157 } else if (vio_version_after(vio
, 1, 3)) {
158 localmtu
= port
->rmtu
? port
->rmtu
: VNET_MAXPACKET
;
159 localmtu
= min(pkt
->mtu
, localmtu
);
162 localmtu
= ETH_FRAME_LEN
+ VLAN_HLEN
;
164 port
->rmtu
= localmtu
;
166 /* LSO negotiation */
167 if (vio_version_after_eq(vio
, 1, 7))
168 port
->tso
&= !!(pkt
->cflags
& VNET_LSO_IPV4_CAPAB
);
173 port
->tsolen
= VNET_MAXTSO
;
174 port
->tsolen
= min(port
->tsolen
, pkt
->ipv4_lso_maxlen
);
175 if (port
->tsolen
< VNET_MINTSO
) {
178 pkt
->cflags
&= ~VNET_LSO_IPV4_CAPAB
;
180 pkt
->ipv4_lso_maxlen
= port
->tsolen
;
182 pkt
->cflags
&= ~VNET_LSO_IPV4_CAPAB
;
183 pkt
->ipv4_lso_maxlen
= 0;
186 /* for version >= 1.6, ACK packet mode we support */
187 if (vio_version_after_eq(vio
, 1, 6)) {
188 pkt
->xfer_mode
= VIO_NEW_DRING_MODE
;
189 pkt
->options
= VIO_TX_DRING
;
192 if (!(xfer_mode
| VIO_NEW_DRING_MODE
) ||
193 pkt
->addr_type
!= VNET_ADDR_ETHERMAC
||
194 pkt
->mtu
!= localmtu
) {
195 viodbg(HS
, "SEND NET ATTR NACK\n");
197 pkt
->tag
.stype
= VIO_SUBTYPE_NACK
;
199 (void)vio_ldc_send(vio
, pkt
, sizeof(*pkt
));
204 viodbg(HS
, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
205 "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
206 "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
207 pkt
->xfer_mode
, pkt
->addr_type
,
208 (unsigned long long)pkt
->addr
,
209 pkt
->ack_freq
, pkt
->plnk_updt
, pkt
->options
,
210 (unsigned long long)pkt
->mtu
, port
->rmtu
, pkt
->cflags
,
211 pkt
->ipv4_lso_maxlen
);
213 pkt
->tag
.stype
= VIO_SUBTYPE_ACK
;
215 return vio_ldc_send(vio
, pkt
, sizeof(*pkt
));
218 static int handle_attr_ack(struct vio_driver_state
*vio
,
219 struct vio_net_attr_info
*pkt
)
221 viodbg(HS
, "GOT NET ATTR ACK\n");
226 static int handle_attr_nack(struct vio_driver_state
*vio
,
227 struct vio_net_attr_info
*pkt
)
229 viodbg(HS
, "GOT NET ATTR NACK\n");
234 int sunvnet_handle_attr_common(struct vio_driver_state
*vio
, void *arg
)
236 struct vio_net_attr_info
*pkt
= arg
;
238 switch (pkt
->tag
.stype
) {
239 case VIO_SUBTYPE_INFO
:
240 return handle_attr_info(vio
, pkt
);
242 case VIO_SUBTYPE_ACK
:
243 return handle_attr_ack(vio
, pkt
);
245 case VIO_SUBTYPE_NACK
:
246 return handle_attr_nack(vio
, pkt
);
252 EXPORT_SYMBOL_GPL(sunvnet_handle_attr_common
);
254 void sunvnet_handshake_complete_common(struct vio_driver_state
*vio
)
256 struct vio_dring_state
*dr
;
258 dr
= &vio
->drings
[VIO_DRIVER_RX_RING
];
262 dr
= &vio
->drings
[VIO_DRIVER_TX_RING
];
266 EXPORT_SYMBOL_GPL(sunvnet_handshake_complete_common
);
268 /* The hypervisor interface that implements copying to/from imported
269 * memory from another domain requires that copies are done to 8-byte
270 * aligned buffers, and that the lengths of such copies are also 8-byte
273 * So we align skb->data to an 8-byte multiple and pad-out the data
274 * area so we can round the copy length up to the next multiple of
277 * The transmitter puts the actual start of the packet 6 bytes into
278 * the buffer it sends over, so that the IP headers after the ethernet
279 * header are aligned properly. These 6 bytes are not in the descriptor
280 * length, they are simply implied. This offset is represented using
281 * the VNET_PACKET_SKIP macro.
283 static struct sk_buff
*alloc_and_align_skb(struct net_device
*dev
,
287 unsigned long addr
, off
;
289 skb
= netdev_alloc_skb(dev
, len
+ VNET_PACKET_SKIP
+ 8 + 8);
293 addr
= (unsigned long)skb
->data
;
294 off
= ((addr
+ 7UL) & ~7UL) - addr
;
296 skb_reserve(skb
, off
);
301 static inline void vnet_fullcsum(struct sk_buff
*skb
)
303 struct iphdr
*iph
= ip_hdr(skb
);
304 int offset
= skb_transport_offset(skb
);
306 if (skb
->protocol
!= htons(ETH_P_IP
))
308 if (iph
->protocol
!= IPPROTO_TCP
&&
309 iph
->protocol
!= IPPROTO_UDP
)
311 skb
->ip_summed
= CHECKSUM_NONE
;
314 if (iph
->protocol
== IPPROTO_TCP
) {
315 struct tcphdr
*ptcp
= tcp_hdr(skb
);
318 skb
->csum
= skb_checksum(skb
, offset
, skb
->len
- offset
, 0);
319 ptcp
->check
= csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
320 skb
->len
- offset
, IPPROTO_TCP
,
322 } else if (iph
->protocol
== IPPROTO_UDP
) {
323 struct udphdr
*pudp
= udp_hdr(skb
);
326 skb
->csum
= skb_checksum(skb
, offset
, skb
->len
- offset
, 0);
327 pudp
->check
= csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
328 skb
->len
- offset
, IPPROTO_UDP
,
333 static int vnet_rx_one(struct vnet_port
*port
, struct vio_net_desc
*desc
)
335 struct net_device
*dev
= VNET_PORT_TO_NET_DEVICE(port
);
336 unsigned int len
= desc
->size
;
337 unsigned int copy_len
;
343 if (port
->tso
&& port
->tsolen
> port
->rmtu
)
344 maxlen
= port
->tsolen
;
347 if (unlikely(len
< ETH_ZLEN
|| len
> maxlen
)) {
348 dev
->stats
.rx_length_errors
++;
352 skb
= alloc_and_align_skb(dev
, len
);
354 if (unlikely(!skb
)) {
355 dev
->stats
.rx_missed_errors
++;
359 copy_len
= (len
+ VNET_PACKET_SKIP
+ 7U) & ~7U;
360 skb_put(skb
, copy_len
);
361 err
= ldc_copy(port
->vio
.lp
, LDC_COPY_IN
,
362 skb
->data
, copy_len
, 0,
363 desc
->cookies
, desc
->ncookies
);
364 if (unlikely(err
< 0)) {
365 dev
->stats
.rx_frame_errors
++;
369 skb_pull(skb
, VNET_PACKET_SKIP
);
371 skb
->protocol
= eth_type_trans(skb
, dev
);
373 if (vio_version_after_eq(&port
->vio
, 1, 8)) {
374 struct vio_net_dext
*dext
= vio_net_ext(desc
);
376 skb_reset_network_header(skb
);
378 if (dext
->flags
& VNET_PKT_HCK_IPV4_HDRCKSUM
) {
379 if (skb
->protocol
== ETH_P_IP
) {
380 struct iphdr
*iph
= ip_hdr(skb
);
386 if ((dext
->flags
& VNET_PKT_HCK_FULLCKSUM
) &&
387 skb
->ip_summed
== CHECKSUM_NONE
) {
388 if (skb
->protocol
== htons(ETH_P_IP
)) {
389 struct iphdr
*iph
= ip_hdr(skb
);
390 int ihl
= iph
->ihl
* 4;
392 skb_reset_transport_header(skb
);
393 skb_set_transport_header(skb
, ihl
);
397 if (dext
->flags
& VNET_PKT_HCK_IPV4_HDRCKSUM_OK
) {
398 skb
->ip_summed
= CHECKSUM_PARTIAL
;
400 if (dext
->flags
& VNET_PKT_HCK_FULLCKSUM_OK
)
405 skb
->ip_summed
= port
->switch_port
? CHECKSUM_NONE
: CHECKSUM_PARTIAL
;
407 dev
->stats
.rx_packets
++;
408 dev
->stats
.rx_bytes
+= len
;
409 napi_gro_receive(&port
->napi
, skb
);
416 dev
->stats
.rx_dropped
++;
420 static int vnet_send_ack(struct vnet_port
*port
, struct vio_dring_state
*dr
,
421 u32 start
, u32 end
, u8 vio_dring_state
)
423 struct vio_dring_data hdr
= {
425 .type
= VIO_TYPE_DATA
,
426 .stype
= VIO_SUBTYPE_ACK
,
427 .stype_env
= VIO_DRING_DATA
,
428 .sid
= vio_send_sid(&port
->vio
),
430 .dring_ident
= dr
->ident
,
433 .state
= vio_dring_state
,
438 hdr
.seq
= dr
->snd_nxt
;
441 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
447 if ((delay
<<= 1) > 128)
449 if (retries
++ > VNET_MAX_RETRIES
) {
450 pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
451 port
->raddr
[0], port
->raddr
[1],
452 port
->raddr
[2], port
->raddr
[3],
453 port
->raddr
[4], port
->raddr
[5]);
456 } while (err
== -EAGAIN
);
458 if (err
<= 0 && vio_dring_state
== VIO_DRING_STOPPED
) {
459 port
->stop_rx_idx
= end
;
460 port
->stop_rx
= true;
462 port
->stop_rx_idx
= 0;
463 port
->stop_rx
= false;
469 static struct vio_net_desc
*get_rx_desc(struct vnet_port
*port
,
470 struct vio_dring_state
*dr
,
473 struct vio_net_desc
*desc
= port
->vio
.desc_buf
;
476 err
= ldc_get_dring_entry(port
->vio
.lp
, desc
, dr
->entry_size
,
477 (index
* dr
->entry_size
),
478 dr
->cookies
, dr
->ncookies
);
485 static int put_rx_desc(struct vnet_port
*port
,
486 struct vio_dring_state
*dr
,
487 struct vio_net_desc
*desc
,
492 err
= ldc_put_dring_entry(port
->vio
.lp
, desc
, dr
->entry_size
,
493 (index
* dr
->entry_size
),
494 dr
->cookies
, dr
->ncookies
);
501 static int vnet_walk_rx_one(struct vnet_port
*port
,
502 struct vio_dring_state
*dr
,
503 u32 index
, int *needs_ack
)
505 struct vio_net_desc
*desc
= get_rx_desc(port
, dr
, index
);
506 struct vio_driver_state
*vio
= &port
->vio
;
511 return PTR_ERR(desc
);
513 if (desc
->hdr
.state
!= VIO_DESC_READY
)
518 viodbg(DATA
, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
519 desc
->hdr
.state
, desc
->hdr
.ack
,
520 desc
->size
, desc
->ncookies
,
521 desc
->cookies
[0].cookie_addr
,
522 desc
->cookies
[0].cookie_size
);
524 err
= vnet_rx_one(port
, desc
);
525 if (err
== -ECONNRESET
)
527 trace_vnet_rx_one(port
->vio
._local_sid
, port
->vio
._peer_sid
,
528 index
, desc
->hdr
.ack
);
529 desc
->hdr
.state
= VIO_DESC_DONE
;
530 err
= put_rx_desc(port
, dr
, desc
, index
);
533 *needs_ack
= desc
->hdr
.ack
;
537 static int vnet_walk_rx(struct vnet_port
*port
, struct vio_dring_state
*dr
,
538 u32 start
, u32 end
, int *npkts
, int budget
)
540 struct vio_driver_state
*vio
= &port
->vio
;
541 int ack_start
= -1, ack_end
= -1;
542 bool send_ack
= true;
544 end
= (end
== (u32
)-1) ? vio_dring_prev(dr
, start
)
545 : vio_dring_next(dr
, end
);
547 viodbg(DATA
, "vnet_walk_rx start[%08x] end[%08x]\n", start
, end
);
549 while (start
!= end
) {
550 int ack
= 0, err
= vnet_walk_rx_one(port
, dr
, start
, &ack
);
552 if (err
== -ECONNRESET
)
560 start
= vio_dring_next(dr
, start
);
561 if (ack
&& start
!= end
) {
562 err
= vnet_send_ack(port
, dr
, ack_start
, ack_end
,
564 if (err
== -ECONNRESET
)
568 if ((*npkts
) >= budget
) {
573 if (unlikely(ack_start
== -1)) {
574 ack_end
= vio_dring_prev(dr
, start
);
578 port
->napi_resume
= false;
579 trace_vnet_tx_send_stopped_ack(port
->vio
._local_sid
,
582 return vnet_send_ack(port
, dr
, ack_start
, ack_end
,
585 trace_vnet_tx_defer_stopped_ack(port
->vio
._local_sid
,
588 port
->napi_resume
= true;
589 port
->napi_stop_idx
= ack_end
;
594 static int vnet_rx(struct vnet_port
*port
, void *msgbuf
, int *npkts
,
597 struct vio_dring_data
*pkt
= msgbuf
;
598 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_RX_RING
];
599 struct vio_driver_state
*vio
= &port
->vio
;
601 viodbg(DATA
, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
602 pkt
->tag
.stype_env
, pkt
->seq
, dr
->rcv_nxt
);
604 if (unlikely(pkt
->tag
.stype_env
!= VIO_DRING_DATA
))
606 if (unlikely(pkt
->seq
!= dr
->rcv_nxt
)) {
607 pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
608 pkt
->seq
, dr
->rcv_nxt
);
612 if (!port
->napi_resume
)
615 /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
617 return vnet_walk_rx(port
, dr
, pkt
->start_idx
, pkt
->end_idx
,
621 static int idx_is_pending(struct vio_dring_state
*dr
, u32 end
)
626 while (idx
!= dr
->prod
) {
631 idx
= vio_dring_next(dr
, idx
);
636 static int vnet_ack(struct vnet_port
*port
, void *msgbuf
)
638 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
639 struct vio_dring_data
*pkt
= msgbuf
;
640 struct net_device
*dev
;
642 struct vio_net_desc
*desc
;
643 struct netdev_queue
*txq
;
645 if (unlikely(pkt
->tag
.stype_env
!= VIO_DRING_DATA
))
649 dev
= VNET_PORT_TO_NET_DEVICE(port
);
651 if (unlikely(!idx_is_pending(dr
, end
))) {
652 netif_tx_unlock(dev
);
656 /* sync for race conditions with vnet_start_xmit() and tell xmit it
657 * is time to send a trigger.
659 trace_vnet_rx_stopped_ack(port
->vio
._local_sid
,
660 port
->vio
._peer_sid
, end
);
661 dr
->cons
= vio_dring_next(dr
, end
);
662 desc
= vio_dring_entry(dr
, dr
->cons
);
663 if (desc
->hdr
.state
== VIO_DESC_READY
&& !port
->start_cons
) {
664 /* vnet_start_xmit() just populated this dring but missed
665 * sending the "start" LDC message to the consumer.
666 * Send a "start" trigger on its behalf.
668 if (__vnet_tx_trigger(port
, dr
->cons
) > 0)
669 port
->start_cons
= false;
671 port
->start_cons
= true;
673 port
->start_cons
= true;
675 netif_tx_unlock(dev
);
677 txq
= netdev_get_tx_queue(dev
, port
->q_index
);
678 if (unlikely(netif_tx_queue_stopped(txq
) &&
679 vnet_tx_dring_avail(dr
) >= VNET_TX_WAKEUP_THRESH(dr
)))
685 static int vnet_nack(struct vnet_port
*port
, void *msgbuf
)
687 /* XXX just reset or similar XXX */
691 static int handle_mcast(struct vnet_port
*port
, void *msgbuf
)
693 struct vio_net_mcast_info
*pkt
= msgbuf
;
694 struct net_device
*dev
= VNET_PORT_TO_NET_DEVICE(port
);
696 if (pkt
->tag
.stype
!= VIO_SUBTYPE_ACK
)
697 pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
707 /* Got back a STOPPED LDC message on port. If the queue is stopped,
708 * wake it up so that we'll send out another START message at the
711 static void maybe_tx_wakeup(struct vnet_port
*port
)
713 struct netdev_queue
*txq
;
715 txq
= netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port
),
717 __netif_tx_lock(txq
, smp_processor_id());
718 if (likely(netif_tx_queue_stopped(txq
))) {
719 struct vio_dring_state
*dr
;
721 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
722 netif_tx_wake_queue(txq
);
724 __netif_tx_unlock(txq
);
727 bool sunvnet_port_is_up_common(struct vnet_port
*vnet
)
729 struct vio_driver_state
*vio
= &vnet
->vio
;
731 return !!(vio
->hs_state
& VIO_HS_COMPLETE
);
733 EXPORT_SYMBOL_GPL(sunvnet_port_is_up_common
);
735 static int vnet_event_napi(struct vnet_port
*port
, int budget
)
737 struct vio_driver_state
*vio
= &port
->vio
;
740 int event
= (port
->rx_event
& LDC_EVENT_RESET
);
743 if (unlikely(event
== LDC_EVENT_RESET
||
744 event
== LDC_EVENT_UP
)) {
745 vio_link_state_change(vio
, event
);
747 if (event
== LDC_EVENT_RESET
) {
748 vnet_port_reset(port
);
754 /* We may have multiple LDC events in rx_event. Unroll send_events() */
755 event
= (port
->rx_event
& LDC_EVENT_UP
);
756 port
->rx_event
&= ~(LDC_EVENT_RESET
| LDC_EVENT_UP
);
757 if (event
== LDC_EVENT_UP
)
759 event
= port
->rx_event
;
760 if (!(event
& LDC_EVENT_DATA_READY
))
763 /* we dont expect any other bits than RESET, UP, DATA_READY */
764 BUG_ON(event
!= LDC_EVENT_DATA_READY
);
770 struct vio_msg_tag tag
;
774 if (port
->napi_resume
) {
775 struct vio_dring_data
*pkt
=
776 (struct vio_dring_data
*)&msgbuf
;
777 struct vio_dring_state
*dr
=
778 &port
->vio
.drings
[VIO_DRIVER_RX_RING
];
780 pkt
->tag
.type
= VIO_TYPE_DATA
;
781 pkt
->tag
.stype
= VIO_SUBTYPE_INFO
;
782 pkt
->tag
.stype_env
= VIO_DRING_DATA
;
783 pkt
->seq
= dr
->rcv_nxt
;
784 pkt
->start_idx
= vio_dring_next(dr
,
785 port
->napi_stop_idx
);
789 err
= ldc_read(vio
->lp
, &msgbuf
, sizeof(msgbuf
));
790 if (unlikely(err
< 0)) {
791 if (err
== -ECONNRESET
)
797 viodbg(DATA
, "TAG [%02x:%02x:%04x:%08x]\n",
800 msgbuf
.tag
.stype_env
,
802 err
= vio_validate_sid(vio
, &msgbuf
.tag
);
806 if (likely(msgbuf
.tag
.type
== VIO_TYPE_DATA
)) {
807 if (msgbuf
.tag
.stype
== VIO_SUBTYPE_INFO
) {
808 if (!sunvnet_port_is_up_common(port
)) {
809 /* failures like handshake_failure()
810 * may have cleaned up dring, but
811 * NAPI polling may bring us here.
816 err
= vnet_rx(port
, &msgbuf
, &npkts
, budget
);
821 } else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_ACK
) {
822 err
= vnet_ack(port
, &msgbuf
);
825 } else if (msgbuf
.tag
.stype
== VIO_SUBTYPE_NACK
) {
826 err
= vnet_nack(port
, &msgbuf
);
828 } else if (msgbuf
.tag
.type
== VIO_TYPE_CTRL
) {
829 if (msgbuf
.tag
.stype_env
== VNET_MCAST_INFO
)
830 err
= handle_mcast(port
, &msgbuf
);
832 err
= vio_control_pkt_engine(vio
, &msgbuf
);
836 err
= vnet_handle_unknown(port
, &msgbuf
);
838 if (err
== -ECONNRESET
)
841 if (unlikely(tx_wakeup
&& err
!= -ECONNRESET
))
842 maybe_tx_wakeup(port
);
846 int sunvnet_poll_common(struct napi_struct
*napi
, int budget
)
848 struct vnet_port
*port
= container_of(napi
, struct vnet_port
, napi
);
849 struct vio_driver_state
*vio
= &port
->vio
;
850 int processed
= vnet_event_napi(port
, budget
);
852 if (processed
< budget
) {
854 port
->rx_event
&= ~LDC_EVENT_DATA_READY
;
855 vio_set_intr(vio
->vdev
->rx_ino
, HV_INTR_ENABLED
);
859 EXPORT_SYMBOL_GPL(sunvnet_poll_common
);
861 void sunvnet_event_common(void *arg
, int event
)
863 struct vnet_port
*port
= arg
;
864 struct vio_driver_state
*vio
= &port
->vio
;
866 port
->rx_event
|= event
;
867 vio_set_intr(vio
->vdev
->rx_ino
, HV_INTR_DISABLED
);
868 napi_schedule(&port
->napi
);
870 EXPORT_SYMBOL_GPL(sunvnet_event_common
);
872 static int __vnet_tx_trigger(struct vnet_port
*port
, u32 start
)
874 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
875 struct vio_dring_data hdr
= {
877 .type
= VIO_TYPE_DATA
,
878 .stype
= VIO_SUBTYPE_INFO
,
879 .stype_env
= VIO_DRING_DATA
,
880 .sid
= vio_send_sid(&port
->vio
),
882 .dring_ident
= dr
->ident
,
890 trace_vnet_tx_pending_stopped_ack(port
->vio
._local_sid
,
892 port
->stop_rx_idx
, -1);
893 err
= vnet_send_ack(port
,
894 &port
->vio
.drings
[VIO_DRIVER_RX_RING
],
895 port
->stop_rx_idx
, -1,
901 hdr
.seq
= dr
->snd_nxt
;
904 err
= vio_ldc_send(&port
->vio
, &hdr
, sizeof(hdr
));
910 if ((delay
<<= 1) > 128)
912 if (retries
++ > VNET_MAX_RETRIES
)
914 } while (err
== -EAGAIN
);
915 trace_vnet_tx_trigger(port
->vio
._local_sid
,
916 port
->vio
._peer_sid
, start
, err
);
921 static struct sk_buff
*vnet_clean_tx_ring(struct vnet_port
*port
,
924 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
925 struct sk_buff
*skb
= NULL
;
931 for (i
= 0; i
< VNET_TX_RING_SIZE
; ++i
) {
932 struct vio_net_desc
*d
;
936 txi
= VNET_TX_RING_SIZE
- 1;
938 d
= vio_dring_entry(dr
, txi
);
940 if (d
->hdr
.state
== VIO_DESC_READY
) {
944 if (port
->tx_bufs
[txi
].skb
) {
945 if (d
->hdr
.state
!= VIO_DESC_DONE
)
946 pr_notice("invalid ring buffer state %d\n",
948 BUG_ON(port
->tx_bufs
[txi
].skb
->next
);
950 port
->tx_bufs
[txi
].skb
->next
= skb
;
951 skb
= port
->tx_bufs
[txi
].skb
;
952 port
->tx_bufs
[txi
].skb
= NULL
;
954 ldc_unmap(port
->vio
.lp
,
955 port
->tx_bufs
[txi
].cookies
,
956 port
->tx_bufs
[txi
].ncookies
);
957 } else if (d
->hdr
.state
== VIO_DESC_FREE
) {
960 d
->hdr
.state
= VIO_DESC_FREE
;
965 static inline void vnet_free_skbs(struct sk_buff
*skb
)
967 struct sk_buff
*next
;
977 void sunvnet_clean_timer_expire_common(unsigned long port0
)
979 struct vnet_port
*port
= (struct vnet_port
*)port0
;
980 struct sk_buff
*freeskbs
;
983 netif_tx_lock(VNET_PORT_TO_NET_DEVICE(port
));
984 freeskbs
= vnet_clean_tx_ring(port
, &pending
);
985 netif_tx_unlock(VNET_PORT_TO_NET_DEVICE(port
));
987 vnet_free_skbs(freeskbs
);
990 (void)mod_timer(&port
->clean_timer
,
991 jiffies
+ VNET_CLEAN_TIMEOUT
);
993 del_timer(&port
->clean_timer
);
995 EXPORT_SYMBOL_GPL(sunvnet_clean_timer_expire_common
);
997 static 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
)
1001 int i
, nc
, err
, blen
;
1004 blen
= skb_headlen(skb
);
1005 if (blen
< ETH_ZLEN
)
1007 blen
+= VNET_PACKET_SKIP
;
1008 blen
+= 8 - (blen
& 7);
1010 err
= ldc_map_single(lp
, skb
->data
- VNET_PACKET_SKIP
, blen
, cookies
,
1011 ncookies
, map_perm
);
1016 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1017 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
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
,
1027 kunmap_atomic(vaddr
);
1033 ldc_unmap(lp
, cookies
, nc
);
1041 static inline struct sk_buff
*vnet_skb_shape(struct sk_buff
*skb
, int ncookies
)
1043 struct sk_buff
*nskb
;
1044 int i
, len
, pad
, docopy
;
1048 if (len
< ETH_ZLEN
) {
1049 pad
+= ETH_ZLEN
- skb
->len
;
1052 len
+= VNET_PACKET_SKIP
;
1053 pad
+= 8 - (len
& 7);
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
];
1060 docopy
|= f
->page_offset
& 7;
1062 if (((unsigned long)skb
->data
& 7) != VNET_PACKET_SKIP
||
1063 skb_tailroom(skb
) < pad
||
1064 skb_headroom(skb
) < VNET_PACKET_SKIP
|| docopy
) {
1065 int start
= 0, offset
;
1068 len
= skb
->len
> ETH_ZLEN
? skb
->len
: ETH_ZLEN
;
1069 nskb
= alloc_and_align_skb(skb
->dev
, len
);
1074 skb_reserve(nskb
, VNET_PACKET_SKIP
);
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
);
1085 nskb
->csum_offset
= skb
->csum_offset
;
1086 nskb
->ip_summed
= skb
->ip_summed
;
1088 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1089 start
= skb_checksum_start_offset(skb
);
1091 struct iphdr
*iph
= ip_hdr(nskb
);
1092 int offset
= start
+ nskb
->csum_offset
;
1094 if (skb_copy_bits(skb
, 0, nskb
->data
, start
)) {
1095 dev_kfree_skb(nskb
);
1099 *(__sum16
*)(skb
->data
+ offset
) = 0;
1100 csum
= skb_copy_and_csum_bits(skb
, 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
,
1107 iph
->protocol
, csum
);
1109 *(__sum16
*)(nskb
->data
+ offset
) = csum
;
1111 nskb
->ip_summed
= CHECKSUM_NONE
;
1112 } else if (skb_copy_bits(skb
, 0, nskb
->data
, skb
->len
)) {
1113 dev_kfree_skb(nskb
);
1117 (void)skb_put(nskb
, skb
->len
);
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
;
1122 nskb
->queue_mapping
= skb
->queue_mapping
;
1129 static int vnet_handle_offloads(struct vnet_port
*port
, struct sk_buff
*skb
,
1130 struct vnet_port
*(*vnet_tx_port
)
1131 (struct sk_buff
*, struct net_device
*))
1133 struct net_device
*dev
= VNET_PORT_TO_NET_DEVICE(port
);
1134 struct vio_dring_state
*dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1135 struct sk_buff
*segs
;
1136 int maclen
, datalen
;
1138 int gso_size
, gso_type
, gso_segs
;
1139 int hlen
= skb_transport_header(skb
) - skb_mac_header(skb
);
1140 int proto
= IPPROTO_IP
;
1142 if (skb
->protocol
== htons(ETH_P_IP
))
1143 proto
= ip_hdr(skb
)->protocol
;
1144 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1145 proto
= ipv6_hdr(skb
)->nexthdr
;
1147 if (proto
== IPPROTO_TCP
) {
1148 hlen
+= tcp_hdr(skb
)->doff
* 4;
1149 } else if (proto
== IPPROTO_UDP
) {
1150 hlen
+= sizeof(struct udphdr
);
1152 pr_err("vnet_handle_offloads GSO with unknown transport "
1153 "protocol %d tproto %d\n", skb
->protocol
, proto
);
1154 hlen
= 128; /* XXX */
1156 datalen
= port
->tsolen
- hlen
;
1158 gso_size
= skb_shinfo(skb
)->gso_size
;
1159 gso_type
= skb_shinfo(skb
)->gso_type
;
1160 gso_segs
= skb_shinfo(skb
)->gso_segs
;
1162 if (port
->tso
&& gso_size
< datalen
)
1163 gso_segs
= DIV_ROUND_UP(skb
->len
- hlen
, datalen
);
1165 if (unlikely(vnet_tx_dring_avail(dr
) < gso_segs
)) {
1166 struct netdev_queue
*txq
;
1168 txq
= netdev_get_tx_queue(dev
, port
->q_index
);
1169 netif_tx_stop_queue(txq
);
1170 if (vnet_tx_dring_avail(dr
) < skb_shinfo(skb
)->gso_segs
)
1171 return NETDEV_TX_BUSY
;
1172 netif_tx_wake_queue(txq
);
1175 maclen
= skb_network_header(skb
) - skb_mac_header(skb
);
1176 skb_pull(skb
, maclen
);
1178 if (port
->tso
&& gso_size
< datalen
) {
1179 if (skb_unclone(skb
, GFP_ATOMIC
))
1182 /* segment to TSO size */
1183 skb_shinfo(skb
)->gso_size
= datalen
;
1184 skb_shinfo(skb
)->gso_segs
= gso_segs
;
1186 segs
= skb_gso_segment(skb
, dev
->features
& ~NETIF_F_TSO
);
1190 skb_push(skb
, maclen
);
1191 skb_reset_mac_header(skb
);
1195 struct sk_buff
*curr
= segs
;
1199 if (port
->tso
&& curr
->len
> dev
->mtu
) {
1200 skb_shinfo(curr
)->gso_size
= gso_size
;
1201 skb_shinfo(curr
)->gso_type
= gso_type
;
1202 skb_shinfo(curr
)->gso_segs
=
1203 DIV_ROUND_UP(curr
->len
- hlen
, gso_size
);
1205 skb_shinfo(curr
)->gso_size
= 0;
1208 skb_push(curr
, maclen
);
1209 skb_reset_mac_header(curr
);
1210 memcpy(skb_mac_header(curr
), skb_mac_header(skb
),
1212 curr
->csum_start
= skb_transport_header(curr
) - curr
->head
;
1213 if (ip_hdr(curr
)->protocol
== IPPROTO_TCP
)
1214 curr
->csum_offset
= offsetof(struct tcphdr
, check
);
1215 else if (ip_hdr(curr
)->protocol
== IPPROTO_UDP
)
1216 curr
->csum_offset
= offsetof(struct udphdr
, check
);
1218 if (!(status
& NETDEV_TX_MASK
))
1219 status
= sunvnet_start_xmit_common(curr
, dev
,
1221 if (status
& NETDEV_TX_MASK
)
1222 dev_kfree_skb_any(curr
);
1225 if (!(status
& NETDEV_TX_MASK
))
1226 dev_kfree_skb_any(skb
);
1229 dev
->stats
.tx_dropped
++;
1230 dev_kfree_skb_any(skb
);
1231 return NETDEV_TX_OK
;
1234 int sunvnet_start_xmit_common(struct sk_buff
*skb
, struct net_device
*dev
,
1235 struct vnet_port
*(*vnet_tx_port
)
1236 (struct sk_buff
*, struct net_device
*))
1238 struct vnet_port
*port
= NULL
;
1239 struct vio_dring_state
*dr
;
1240 struct vio_net_desc
*d
;
1242 struct sk_buff
*freeskbs
= NULL
;
1244 unsigned pending
= 0;
1245 struct netdev_queue
*txq
;
1248 port
= vnet_tx_port(skb
, dev
);
1249 if (unlikely(!port
)) {
1254 if (skb_is_gso(skb
) && skb
->len
> port
->tsolen
) {
1255 err
= vnet_handle_offloads(port
, skb
, vnet_tx_port
);
1260 if (!skb_is_gso(skb
) && skb
->len
> port
->rmtu
) {
1261 unsigned long localmtu
= port
->rmtu
- ETH_HLEN
;
1263 if (vio_version_after_eq(&port
->vio
, 1, 3))
1264 localmtu
-= VLAN_HLEN
;
1266 if (skb
->protocol
== htons(ETH_P_IP
)) {
1268 struct rtable
*rt
= NULL
;
1270 memset(&fl4
, 0, sizeof(fl4
));
1271 fl4
.flowi4_oif
= dev
->ifindex
;
1272 fl4
.flowi4_tos
= RT_TOS(ip_hdr(skb
)->tos
);
1273 fl4
.daddr
= ip_hdr(skb
)->daddr
;
1274 fl4
.saddr
= ip_hdr(skb
)->saddr
;
1276 rt
= ip_route_output_key(dev_net(dev
), &fl4
);
1279 skb_dst_set(skb
, &rt
->dst
);
1280 icmp_send(skb
, ICMP_DEST_UNREACH
,
1285 #if IS_ENABLED(CONFIG_IPV6)
1286 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1287 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, localmtu
);
1292 skb
= vnet_skb_shape(skb
, 2);
1297 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1300 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1301 i
= skb_get_queue_mapping(skb
);
1302 txq
= netdev_get_tx_queue(dev
, i
);
1303 if (unlikely(vnet_tx_dring_avail(dr
) < 1)) {
1304 if (!netif_tx_queue_stopped(txq
)) {
1305 netif_tx_stop_queue(txq
);
1307 /* This is a hard error, log it. */
1308 netdev_err(dev
, "BUG! Tx Ring full when queue awake!\n");
1309 dev
->stats
.tx_errors
++;
1312 return NETDEV_TX_BUSY
;
1315 d
= vio_dring_cur(dr
);
1319 freeskbs
= vnet_clean_tx_ring(port
, &pending
);
1321 BUG_ON(port
->tx_bufs
[txi
].skb
);
1327 err
= vnet_skb_map(port
->vio
.lp
, skb
, port
->tx_bufs
[txi
].cookies
, 2,
1328 (LDC_MAP_SHADOW
| LDC_MAP_DIRECT
| LDC_MAP_RW
));
1330 netdev_info(dev
, "tx buffer map error %d\n", err
);
1334 port
->tx_bufs
[txi
].skb
= skb
;
1336 port
->tx_bufs
[txi
].ncookies
= err
;
1338 /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
1339 * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
1340 * the protocol itself does not require it as long as the peer
1341 * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
1343 * An ACK for every packet in the ring is expensive as the
1344 * sending of LDC messages is slow and affects performance.
1346 d
->hdr
.ack
= VIO_ACK_DISABLE
;
1348 d
->ncookies
= port
->tx_bufs
[txi
].ncookies
;
1349 for (i
= 0; i
< d
->ncookies
; i
++)
1350 d
->cookies
[i
] = port
->tx_bufs
[txi
].cookies
[i
];
1351 if (vio_version_after_eq(&port
->vio
, 1, 7)) {
1352 struct vio_net_dext
*dext
= vio_net_ext(d
);
1354 memset(dext
, 0, sizeof(*dext
));
1355 if (skb_is_gso(port
->tx_bufs
[txi
].skb
)) {
1356 dext
->ipv4_lso_mss
= skb_shinfo(port
->tx_bufs
[txi
].skb
)
1358 dext
->flags
|= VNET_PKT_IPV4_LSO
;
1360 if (vio_version_after_eq(&port
->vio
, 1, 8) &&
1361 !port
->switch_port
) {
1362 dext
->flags
|= VNET_PKT_HCK_IPV4_HDRCKSUM_OK
;
1363 dext
->flags
|= VNET_PKT_HCK_FULLCKSUM_OK
;
1367 /* This has to be a non-SMP write barrier because we are writing
1368 * to memory which is shared with the peer LDOM.
1372 d
->hdr
.state
= VIO_DESC_READY
;
1374 /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
1375 * to notify the consumer that some descriptors are READY.
1376 * After that "start" trigger, no additional triggers are needed until
1377 * a DRING_STOPPED is received from the consumer. The dr->cons field
1378 * (set up by vnet_ack()) has the value of the next dring index
1379 * that has not yet been ack-ed. We send a "start" trigger here
1380 * if, and only if, start_cons is true (reset it afterward). Conversely,
1381 * vnet_ack() should check if the dring corresponding to cons
1382 * is marked READY, but start_cons was false.
1383 * If so, vnet_ack() should send out the missed "start" trigger.
1385 * Note that the dma_wmb() above makes sure the cookies et al. are
1386 * not globally visible before the VIO_DESC_READY, and that the
1387 * stores are ordered correctly by the compiler. The consumer will
1388 * not proceed until the VIO_DESC_READY is visible assuring that
1389 * the consumer does not observe anything related to descriptors
1390 * out of order. The HV trap from the LDC start trigger is the
1391 * producer to consumer announcement that work is available to the
1394 if (!port
->start_cons
) { /* previous trigger suffices */
1395 trace_vnet_skip_tx_trigger(port
->vio
._local_sid
,
1396 port
->vio
._peer_sid
, dr
->cons
);
1397 goto ldc_start_done
;
1400 err
= __vnet_tx_trigger(port
, dr
->cons
);
1401 if (unlikely(err
< 0)) {
1402 netdev_info(dev
, "TX trigger error %d\n", err
);
1403 d
->hdr
.state
= VIO_DESC_FREE
;
1404 skb
= port
->tx_bufs
[txi
].skb
;
1405 port
->tx_bufs
[txi
].skb
= NULL
;
1406 dev
->stats
.tx_carrier_errors
++;
1411 port
->start_cons
= false;
1413 dev
->stats
.tx_packets
++;
1414 dev
->stats
.tx_bytes
+= port
->tx_bufs
[txi
].skb
->len
;
1416 dr
->prod
= (dr
->prod
+ 1) & (VNET_TX_RING_SIZE
- 1);
1417 if (unlikely(vnet_tx_dring_avail(dr
) < 1)) {
1418 netif_tx_stop_queue(txq
);
1419 if (vnet_tx_dring_avail(dr
) > VNET_TX_WAKEUP_THRESH(dr
))
1420 netif_tx_wake_queue(txq
);
1423 (void)mod_timer(&port
->clean_timer
, jiffies
+ VNET_CLEAN_TIMEOUT
);
1426 vnet_free_skbs(freeskbs
);
1428 return NETDEV_TX_OK
;
1432 (void)mod_timer(&port
->clean_timer
,
1433 jiffies
+ VNET_CLEAN_TIMEOUT
);
1435 del_timer(&port
->clean_timer
);
1440 vnet_free_skbs(freeskbs
);
1441 dev
->stats
.tx_dropped
++;
1442 return NETDEV_TX_OK
;
1444 EXPORT_SYMBOL_GPL(sunvnet_start_xmit_common
);
1446 void sunvnet_tx_timeout_common(struct net_device
*dev
)
1448 /* XXX Implement me XXX */
1450 EXPORT_SYMBOL_GPL(sunvnet_tx_timeout_common
);
1452 int sunvnet_open_common(struct net_device
*dev
)
1454 netif_carrier_on(dev
);
1455 netif_tx_start_all_queues(dev
);
1459 EXPORT_SYMBOL_GPL(sunvnet_open_common
);
1461 int sunvnet_close_common(struct net_device
*dev
)
1463 netif_tx_stop_all_queues(dev
);
1464 netif_carrier_off(dev
);
1468 EXPORT_SYMBOL_GPL(sunvnet_close_common
);
1470 static struct vnet_mcast_entry
*__vnet_mc_find(struct vnet
*vp
, u8
*addr
)
1472 struct vnet_mcast_entry
*m
;
1474 for (m
= vp
->mcast_list
; m
; m
= m
->next
) {
1475 if (ether_addr_equal(m
->addr
, addr
))
1481 static void __update_mc_list(struct vnet
*vp
, struct net_device
*dev
)
1483 struct netdev_hw_addr
*ha
;
1485 netdev_for_each_mc_addr(ha
, dev
) {
1486 struct vnet_mcast_entry
*m
;
1488 m
= __vnet_mc_find(vp
, ha
->addr
);
1495 m
= kzalloc(sizeof(*m
), GFP_ATOMIC
);
1498 memcpy(m
->addr
, ha
->addr
, ETH_ALEN
);
1501 m
->next
= vp
->mcast_list
;
1507 static void __send_mc_list(struct vnet
*vp
, struct vnet_port
*port
)
1509 struct vio_net_mcast_info info
;
1510 struct vnet_mcast_entry
*m
, **pp
;
1513 memset(&info
, 0, sizeof(info
));
1515 info
.tag
.type
= VIO_TYPE_CTRL
;
1516 info
.tag
.stype
= VIO_SUBTYPE_INFO
;
1517 info
.tag
.stype_env
= VNET_MCAST_INFO
;
1518 info
.tag
.sid
= vio_send_sid(&port
->vio
);
1522 for (m
= vp
->mcast_list
; m
; m
= m
->next
) {
1526 memcpy(&info
.mcast_addr
[n_addrs
* ETH_ALEN
],
1528 if (++n_addrs
== VNET_NUM_MCAST
) {
1529 info
.count
= n_addrs
;
1531 (void)vio_ldc_send(&port
->vio
, &info
,
1537 info
.count
= n_addrs
;
1538 (void)vio_ldc_send(&port
->vio
, &info
, sizeof(info
));
1544 pp
= &vp
->mcast_list
;
1545 while ((m
= *pp
) != NULL
) {
1552 memcpy(&info
.mcast_addr
[n_addrs
* ETH_ALEN
],
1554 if (++n_addrs
== VNET_NUM_MCAST
) {
1555 info
.count
= n_addrs
;
1556 (void)vio_ldc_send(&port
->vio
, &info
,
1565 info
.count
= n_addrs
;
1566 (void)vio_ldc_send(&port
->vio
, &info
, sizeof(info
));
1570 void sunvnet_set_rx_mode_common(struct net_device
*dev
, struct vnet
*vp
)
1572 struct vnet_port
*port
;
1575 list_for_each_entry_rcu(port
, &vp
->port_list
, list
) {
1576 if (port
->switch_port
) {
1577 __update_mc_list(vp
, dev
);
1578 __send_mc_list(vp
, port
);
1584 EXPORT_SYMBOL_GPL(sunvnet_set_rx_mode_common
);
1586 int sunvnet_change_mtu_common(struct net_device
*dev
, int new_mtu
)
1588 if (new_mtu
< 68 || new_mtu
> 65535)
1594 EXPORT_SYMBOL_GPL(sunvnet_change_mtu_common
);
1596 int sunvnet_set_mac_addr_common(struct net_device
*dev
, void *p
)
1600 EXPORT_SYMBOL_GPL(sunvnet_set_mac_addr_common
);
1602 void sunvnet_port_free_tx_bufs_common(struct vnet_port
*port
)
1604 struct vio_dring_state
*dr
;
1607 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1612 for (i
= 0; i
< VNET_TX_RING_SIZE
; i
++) {
1613 struct vio_net_desc
*d
;
1614 void *skb
= port
->tx_bufs
[i
].skb
;
1619 d
= vio_dring_entry(dr
, i
);
1621 ldc_unmap(port
->vio
.lp
,
1622 port
->tx_bufs
[i
].cookies
,
1623 port
->tx_bufs
[i
].ncookies
);
1625 port
->tx_bufs
[i
].skb
= NULL
;
1626 d
->hdr
.state
= VIO_DESC_FREE
;
1628 ldc_free_exp_dring(port
->vio
.lp
, dr
->base
,
1629 (dr
->entry_size
* dr
->num_entries
),
1630 dr
->cookies
, dr
->ncookies
);
1633 dr
->num_entries
= 0;
1637 EXPORT_SYMBOL_GPL(sunvnet_port_free_tx_bufs_common
);
1639 static void vnet_port_reset(struct vnet_port
*port
)
1641 del_timer(&port
->clean_timer
);
1642 sunvnet_port_free_tx_bufs_common(port
);
1648 static int vnet_port_alloc_tx_ring(struct vnet_port
*port
)
1650 struct vio_dring_state
*dr
;
1651 unsigned long len
, elen
;
1652 int i
, err
, ncookies
;
1655 dr
= &port
->vio
.drings
[VIO_DRIVER_TX_RING
];
1657 elen
= sizeof(struct vio_net_desc
) +
1658 sizeof(struct ldc_trans_cookie
) * 2;
1659 if (vio_version_after_eq(&port
->vio
, 1, 7))
1660 elen
+= sizeof(struct vio_net_dext
);
1661 len
= VNET_TX_RING_SIZE
* elen
;
1663 ncookies
= VIO_MAX_RING_COOKIES
;
1664 dring
= ldc_alloc_exp_dring(port
->vio
.lp
, len
,
1665 dr
->cookies
, &ncookies
,
1669 if (IS_ERR(dring
)) {
1670 err
= PTR_ERR(dring
);
1675 dr
->entry_size
= elen
;
1676 dr
->num_entries
= VNET_TX_RING_SIZE
;
1679 port
->start_cons
= true; /* need an initial trigger */
1680 dr
->pending
= VNET_TX_RING_SIZE
;
1681 dr
->ncookies
= ncookies
;
1683 for (i
= 0; i
< VNET_TX_RING_SIZE
; ++i
) {
1684 struct vio_net_desc
*d
;
1686 d
= vio_dring_entry(dr
, i
);
1687 d
->hdr
.state
= VIO_DESC_FREE
;
1692 sunvnet_port_free_tx_bufs_common(port
);
1697 #ifdef CONFIG_NET_POLL_CONTROLLER
1698 void sunvnet_poll_controller_common(struct net_device
*dev
, struct vnet
*vp
)
1700 struct vnet_port
*port
;
1701 unsigned long flags
;
1703 spin_lock_irqsave(&vp
->lock
, flags
);
1704 if (!list_empty(&vp
->port_list
)) {
1705 port
= list_entry(vp
->port_list
.next
, struct vnet_port
, list
);
1706 napi_schedule(&port
->napi
);
1708 spin_unlock_irqrestore(&vp
->lock
, flags
);
1710 EXPORT_SYMBOL_GPL(sunvnet_poll_controller_common
);
1713 void sunvnet_port_add_txq_common(struct vnet_port
*port
)
1715 struct vnet
*vp
= port
->vp
;
1719 n
= n
& (VNET_MAX_TXQS
- 1);
1721 netif_tx_wake_queue(netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port
),
1724 EXPORT_SYMBOL_GPL(sunvnet_port_add_txq_common
);
1726 void sunvnet_port_rm_txq_common(struct vnet_port
*port
)
1729 netif_tx_stop_queue(netdev_get_tx_queue(VNET_PORT_TO_NET_DEVICE(port
),
1732 EXPORT_SYMBOL_GPL(sunvnet_port_rm_txq_common
);