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