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