blob: 5c5fb59adf765475e5cbfa82fa42c6145c0ebfc4 [file] [log] [blame]
David S. Miller4c521e42007-07-09 22:23:51 -07001/* sunvnet.c: Sun LDOM Virtual Network Driver.
2 *
David S. Miller3d452e52008-09-01 01:48:52 -07003 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
David S. Miller4c521e42007-07-09 22:23:51 -07004 */
5
Joe Perches4d5870e2010-08-17 07:55:05 +00006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
David S. Miller4c521e42007-07-09 22:23:51 -07008#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>
David S. Miller9184a042007-07-17 22:19:10 -070017#include <linux/mutex.h>
David L Stevense4defc72014-09-29 19:47:59 -040018#include <linux/if_vlan.h>
David S. Miller4c521e42007-07-09 22:23:51 -070019
David L Stevensa2b78e92014-09-29 19:48:24 -040020#if IS_ENABLED(CONFIG_IPV6)
21#include <linux/icmpv6.h>
22#endif
23
24#include <net/icmp.h>
25#include <net/route.h>
26
David S. Miller4c521e42007-07-09 22:23:51 -070027#include <asm/vio.h>
28#include <asm/ldc.h>
29
30#include "sunvnet.h"
31
32#define DRV_MODULE_NAME "sunvnet"
David S. Miller4c521e42007-07-09 22:23:51 -070033#define DRV_MODULE_VERSION "1.0"
34#define DRV_MODULE_RELDATE "June 25, 2007"
35
Bill Pembertonf73d12b2012-12-03 09:24:02 -050036static char version[] =
David S. Miller4c521e42007-07-09 22:23:51 -070037 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
38MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
39MODULE_DESCRIPTION("Sun LDOM virtual network driver");
40MODULE_LICENSE("GPL");
41MODULE_VERSION(DRV_MODULE_VERSION);
42
Sowmini Varadhand51bffd2014-10-30 12:46:09 -040043#define VNET_MAX_TXQS 16
44
Sowmini Varadhanadddc322014-08-12 10:33:10 -040045/* Heuristic for the number of times to exponentially backoff and
46 * retry sending an LDC trigger when EAGAIN is encountered
47 */
48#define VNET_MAX_RETRIES 10
49
Sowmini Varadhand1015642014-09-11 09:57:22 -040050static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
51
David S. Miller4c521e42007-07-09 22:23:51 -070052/* Ordered from largest major to lowest */
53static struct vio_version vnet_versions[] = {
David L Stevense4defc72014-09-29 19:47:59 -040054 { .major = 1, .minor = 6 },
David S. Miller4c521e42007-07-09 22:23:51 -070055 { .major = 1, .minor = 0 },
56};
57
58static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
59{
60 return vio_dring_avail(dr, VNET_TX_RING_SIZE);
61}
62
63static int vnet_handle_unknown(struct vnet_port *port, void *arg)
64{
65 struct vio_msg_tag *pkt = arg;
66
Joe Perches4d5870e2010-08-17 07:55:05 +000067 pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
David S. Miller4c521e42007-07-09 22:23:51 -070068 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
Joe Perches4d5870e2010-08-17 07:55:05 +000069 pr_err("Resetting connection\n");
David S. Miller4c521e42007-07-09 22:23:51 -070070
71 ldc_disconnect(port->vio.lp);
72
73 return -ECONNRESET;
74}
75
76static int vnet_send_attr(struct vio_driver_state *vio)
77{
78 struct vnet_port *port = to_vnet_port(vio);
79 struct net_device *dev = port->vp->dev;
80 struct vio_net_attr_info pkt;
David L Stevense4defc72014-09-29 19:47:59 -040081 int framelen = ETH_FRAME_LEN;
David S. Miller4c521e42007-07-09 22:23:51 -070082 int i;
83
84 memset(&pkt, 0, sizeof(pkt));
85 pkt.tag.type = VIO_TYPE_CTRL;
86 pkt.tag.stype = VIO_SUBTYPE_INFO;
87 pkt.tag.stype_env = VIO_ATTR_INFO;
88 pkt.tag.sid = vio_send_sid(vio);
David L Stevense4defc72014-09-29 19:47:59 -040089 if (vio_version_before(vio, 1, 2))
90 pkt.xfer_mode = VIO_DRING_MODE;
91 else
92 pkt.xfer_mode = VIO_NEW_DRING_MODE;
David S. Miller4c521e42007-07-09 22:23:51 -070093 pkt.addr_type = VNET_ADDR_ETHERMAC;
94 pkt.ack_freq = 0;
95 for (i = 0; i < 6; i++)
96 pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
David L Stevense4defc72014-09-29 19:47:59 -040097 if (vio_version_after(vio, 1, 3)) {
98 if (port->rmtu) {
99 port->rmtu = min(VNET_MAXPACKET, port->rmtu);
100 pkt.mtu = port->rmtu;
101 } else {
102 port->rmtu = VNET_MAXPACKET;
103 pkt.mtu = port->rmtu;
104 }
105 if (vio_version_after_eq(vio, 1, 6))
106 pkt.options = VIO_TX_DRING;
107 } else if (vio_version_before(vio, 1, 3)) {
108 pkt.mtu = framelen;
109 } else { /* v1.3 */
110 pkt.mtu = framelen + VLAN_HLEN;
111 }
112
113 pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
114 pkt.cflags = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700115
116 viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
David L Stevense4defc72014-09-29 19:47:59 -0400117 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
118 "cflags[0x%04x] lso_max[%u]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700119 pkt.xfer_mode, pkt.addr_type,
David L Stevense4defc72014-09-29 19:47:59 -0400120 (unsigned long long)pkt.addr,
121 pkt.ack_freq, pkt.plnk_updt, pkt.options,
122 (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);
123
David S. Miller4c521e42007-07-09 22:23:51 -0700124
125 return vio_ldc_send(vio, &pkt, sizeof(pkt));
126}
127
128static int handle_attr_info(struct vio_driver_state *vio,
129 struct vio_net_attr_info *pkt)
130{
David L Stevense4defc72014-09-29 19:47:59 -0400131 struct vnet_port *port = to_vnet_port(vio);
132 u64 localmtu;
133 u8 xfer_mode;
134
135 viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
136 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
137 " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700138 pkt->xfer_mode, pkt->addr_type,
David L Stevense4defc72014-09-29 19:47:59 -0400139 (unsigned long long)pkt->addr,
140 pkt->ack_freq, pkt->plnk_updt, pkt->options,
141 (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
142 pkt->ipv4_lso_maxlen);
David S. Miller4c521e42007-07-09 22:23:51 -0700143
144 pkt->tag.sid = vio_send_sid(vio);
145
David L Stevense4defc72014-09-29 19:47:59 -0400146 xfer_mode = pkt->xfer_mode;
147 /* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
148 if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
149 xfer_mode = VIO_NEW_DRING_MODE;
150
151 /* MTU negotiation:
152 * < v1.3 - ETH_FRAME_LEN exactly
153 * > v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
154 * pkt->mtu for ACK
155 * = v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
156 */
157 if (vio_version_before(vio, 1, 3)) {
158 localmtu = ETH_FRAME_LEN;
159 } else if (vio_version_after(vio, 1, 3)) {
160 localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
161 localmtu = min(pkt->mtu, localmtu);
162 pkt->mtu = localmtu;
163 } else { /* v1.3 */
164 localmtu = ETH_FRAME_LEN + VLAN_HLEN;
165 }
166 port->rmtu = localmtu;
167
168 /* for version >= 1.6, ACK packet mode we support */
169 if (vio_version_after_eq(vio, 1, 6)) {
170 pkt->xfer_mode = VIO_NEW_DRING_MODE;
171 pkt->options = VIO_TX_DRING;
172 }
173
174 if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
David S. Miller4c521e42007-07-09 22:23:51 -0700175 pkt->addr_type != VNET_ADDR_ETHERMAC ||
David L Stevense4defc72014-09-29 19:47:59 -0400176 pkt->mtu != localmtu) {
David S. Miller4c521e42007-07-09 22:23:51 -0700177 viodbg(HS, "SEND NET ATTR NACK\n");
178
179 pkt->tag.stype = VIO_SUBTYPE_NACK;
180
181 (void) vio_ldc_send(vio, pkt, sizeof(*pkt));
182
183 return -ECONNRESET;
184 } else {
David L Stevense4defc72014-09-29 19:47:59 -0400185 viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
186 "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
187 "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
188 pkt->xfer_mode, pkt->addr_type,
189 (unsigned long long)pkt->addr,
190 pkt->ack_freq, pkt->plnk_updt, pkt->options,
191 (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
192 pkt->ipv4_lso_maxlen);
David S. Miller4c521e42007-07-09 22:23:51 -0700193
194 pkt->tag.stype = VIO_SUBTYPE_ACK;
195
196 return vio_ldc_send(vio, pkt, sizeof(*pkt));
197 }
198
199}
200
201static int handle_attr_ack(struct vio_driver_state *vio,
202 struct vio_net_attr_info *pkt)
203{
204 viodbg(HS, "GOT NET ATTR ACK\n");
205
206 return 0;
207}
208
209static int handle_attr_nack(struct vio_driver_state *vio,
210 struct vio_net_attr_info *pkt)
211{
212 viodbg(HS, "GOT NET ATTR NACK\n");
213
214 return -ECONNRESET;
215}
216
217static int vnet_handle_attr(struct vio_driver_state *vio, void *arg)
218{
219 struct vio_net_attr_info *pkt = arg;
220
221 switch (pkt->tag.stype) {
222 case VIO_SUBTYPE_INFO:
223 return handle_attr_info(vio, pkt);
224
225 case VIO_SUBTYPE_ACK:
226 return handle_attr_ack(vio, pkt);
227
228 case VIO_SUBTYPE_NACK:
229 return handle_attr_nack(vio, pkt);
230
231 default:
232 return -ECONNRESET;
233 }
234}
235
236static void vnet_handshake_complete(struct vio_driver_state *vio)
237{
238 struct vio_dring_state *dr;
239
240 dr = &vio->drings[VIO_DRIVER_RX_RING];
241 dr->snd_nxt = dr->rcv_nxt = 1;
242
243 dr = &vio->drings[VIO_DRIVER_TX_RING];
244 dr->snd_nxt = dr->rcv_nxt = 1;
245}
246
247/* The hypervisor interface that implements copying to/from imported
248 * memory from another domain requires that copies are done to 8-byte
249 * aligned buffers, and that the lengths of such copies are also 8-byte
250 * multiples.
251 *
252 * So we align skb->data to an 8-byte multiple and pad-out the data
253 * area so we can round the copy length up to the next multiple of
254 * 8 for the copy.
255 *
256 * The transmitter puts the actual start of the packet 6 bytes into
257 * the buffer it sends over, so that the IP headers after the ethernet
258 * header are aligned properly. These 6 bytes are not in the descriptor
259 * length, they are simply implied. This offset is represented using
260 * the VNET_PACKET_SKIP macro.
261 */
262static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
263 unsigned int len)
264{
265 struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8);
266 unsigned long addr, off;
267
268 if (unlikely(!skb))
269 return NULL;
270
271 addr = (unsigned long) skb->data;
272 off = ((addr + 7UL) & ~7UL) - addr;
273 if (off)
274 skb_reserve(skb, off);
275
276 return skb;
277}
278
279static int vnet_rx_one(struct vnet_port *port, unsigned int len,
280 struct ldc_trans_cookie *cookies, int ncookies)
281{
282 struct net_device *dev = port->vp->dev;
283 unsigned int copy_len;
284 struct sk_buff *skb;
285 int err;
286
287 err = -EMSGSIZE;
David L Stevense4defc72014-09-29 19:47:59 -0400288 if (unlikely(len < ETH_ZLEN || len > port->rmtu)) {
David S. Miller4c521e42007-07-09 22:23:51 -0700289 dev->stats.rx_length_errors++;
290 goto out_dropped;
291 }
292
293 skb = alloc_and_align_skb(dev, len);
294 err = -ENOMEM;
295 if (unlikely(!skb)) {
296 dev->stats.rx_missed_errors++;
297 goto out_dropped;
298 }
299
300 copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
301 skb_put(skb, copy_len);
302 err = ldc_copy(port->vio.lp, LDC_COPY_IN,
303 skb->data, copy_len, 0,
304 cookies, ncookies);
305 if (unlikely(err < 0)) {
306 dev->stats.rx_frame_errors++;
307 goto out_free_skb;
308 }
309
310 skb_pull(skb, VNET_PACKET_SKIP);
311 skb_trim(skb, len);
312 skb->protocol = eth_type_trans(skb, dev);
313
314 dev->stats.rx_packets++;
315 dev->stats.rx_bytes += len;
Sowmini Varadhan69088822014-10-25 15:12:12 -0400316 napi_gro_receive(&port->napi, skb);
David S. Miller4c521e42007-07-09 22:23:51 -0700317 return 0;
318
319out_free_skb:
320 kfree_skb(skb);
321
322out_dropped:
323 dev->stats.rx_dropped++;
324 return err;
325}
326
327static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
328 u32 start, u32 end, u8 vio_dring_state)
329{
330 struct vio_dring_data hdr = {
331 .tag = {
332 .type = VIO_TYPE_DATA,
333 .stype = VIO_SUBTYPE_ACK,
334 .stype_env = VIO_DRING_DATA,
335 .sid = vio_send_sid(&port->vio),
336 },
337 .dring_ident = dr->ident,
338 .start_idx = start,
339 .end_idx = end,
340 .state = vio_dring_state,
341 };
342 int err, delay;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400343 int retries = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700344
345 hdr.seq = dr->snd_nxt;
346 delay = 1;
347 do {
348 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
349 if (err > 0) {
350 dr->snd_nxt++;
351 break;
352 }
353 udelay(delay);
354 if ((delay <<= 1) > 128)
355 delay = 128;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400356 if (retries++ > VNET_MAX_RETRIES) {
357 pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
358 port->raddr[0], port->raddr[1],
359 port->raddr[2], port->raddr[3],
360 port->raddr[4], port->raddr[5]);
Sowmini Varadhand1015642014-09-11 09:57:22 -0400361 break;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400362 }
David S. Miller4c521e42007-07-09 22:23:51 -0700363 } while (err == -EAGAIN);
364
Sowmini Varadhand1015642014-09-11 09:57:22 -0400365 if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
366 port->stop_rx_idx = end;
367 port->stop_rx = true;
368 } else {
369 port->stop_rx_idx = 0;
370 port->stop_rx = false;
371 }
372
David S. Miller4c521e42007-07-09 22:23:51 -0700373 return err;
374}
375
376static u32 next_idx(u32 idx, struct vio_dring_state *dr)
377{
378 if (++idx == dr->num_entries)
379 idx = 0;
380 return idx;
381}
382
383static u32 prev_idx(u32 idx, struct vio_dring_state *dr)
384{
385 if (idx == 0)
386 idx = dr->num_entries - 1;
387 else
388 idx--;
389
390 return idx;
391}
392
393static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
394 struct vio_dring_state *dr,
395 u32 index)
396{
397 struct vio_net_desc *desc = port->vio.desc_buf;
398 int err;
399
400 err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
401 (index * dr->entry_size),
402 dr->cookies, dr->ncookies);
403 if (err < 0)
404 return ERR_PTR(err);
405
406 return desc;
407}
408
409static int put_rx_desc(struct vnet_port *port,
410 struct vio_dring_state *dr,
411 struct vio_net_desc *desc,
412 u32 index)
413{
414 int err;
415
416 err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
417 (index * dr->entry_size),
418 dr->cookies, dr->ncookies);
419 if (err < 0)
420 return err;
421
422 return 0;
423}
424
425static int vnet_walk_rx_one(struct vnet_port *port,
426 struct vio_dring_state *dr,
427 u32 index, int *needs_ack)
428{
429 struct vio_net_desc *desc = get_rx_desc(port, dr, index);
430 struct vio_driver_state *vio = &port->vio;
431 int err;
432
Sowmini Varadhan69088822014-10-25 15:12:12 -0400433 BUG_ON(desc == NULL);
David S. Miller4c521e42007-07-09 22:23:51 -0700434 if (IS_ERR(desc))
435 return PTR_ERR(desc);
436
David L Stevens78dcff72014-09-08 16:23:01 -0400437 if (desc->hdr.state != VIO_DESC_READY)
438 return 1;
439
440 rmb();
441
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800442 viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700443 desc->hdr.state, desc->hdr.ack,
444 desc->size, desc->ncookies,
445 desc->cookies[0].cookie_addr,
446 desc->cookies[0].cookie_size);
447
David S. Miller4c521e42007-07-09 22:23:51 -0700448 err = vnet_rx_one(port, desc->size, desc->cookies, desc->ncookies);
449 if (err == -ECONNRESET)
450 return err;
451 desc->hdr.state = VIO_DESC_DONE;
452 err = put_rx_desc(port, dr, desc, index);
453 if (err < 0)
454 return err;
455 *needs_ack = desc->hdr.ack;
456 return 0;
457}
458
459static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
Sowmini Varadhan69088822014-10-25 15:12:12 -0400460 u32 start, u32 end, int *npkts, int budget)
David S. Miller4c521e42007-07-09 22:23:51 -0700461{
462 struct vio_driver_state *vio = &port->vio;
463 int ack_start = -1, ack_end = -1;
Sowmini Varadhan69088822014-10-25 15:12:12 -0400464 bool send_ack = true;
David S. Miller4c521e42007-07-09 22:23:51 -0700465
466 end = (end == (u32) -1) ? prev_idx(start, dr) : next_idx(end, dr);
467
468 viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
469
470 while (start != end) {
471 int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
472 if (err == -ECONNRESET)
473 return err;
474 if (err != 0)
475 break;
Sowmini Varadhan69088822014-10-25 15:12:12 -0400476 (*npkts)++;
David S. Miller4c521e42007-07-09 22:23:51 -0700477 if (ack_start == -1)
478 ack_start = start;
479 ack_end = start;
480 start = next_idx(start, dr);
481 if (ack && start != end) {
482 err = vnet_send_ack(port, dr, ack_start, ack_end,
483 VIO_DRING_ACTIVE);
484 if (err == -ECONNRESET)
485 return err;
486 ack_start = -1;
487 }
Sowmini Varadhan69088822014-10-25 15:12:12 -0400488 if ((*npkts) >= budget) {
489 send_ack = false;
490 break;
491 }
David S. Miller4c521e42007-07-09 22:23:51 -0700492 }
493 if (unlikely(ack_start == -1))
494 ack_start = ack_end = prev_idx(start, dr);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400495 if (send_ack) {
496 port->napi_resume = false;
497 return vnet_send_ack(port, dr, ack_start, ack_end,
498 VIO_DRING_STOPPED);
499 } else {
500 port->napi_resume = true;
501 port->napi_stop_idx = ack_end;
502 return 1;
503 }
David S. Miller4c521e42007-07-09 22:23:51 -0700504}
505
Sowmini Varadhan69088822014-10-25 15:12:12 -0400506static int vnet_rx(struct vnet_port *port, void *msgbuf, int *npkts,
507 int budget)
David S. Miller4c521e42007-07-09 22:23:51 -0700508{
509 struct vio_dring_data *pkt = msgbuf;
510 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
511 struct vio_driver_state *vio = &port->vio;
512
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800513 viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700514 pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
515
516 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
517 return 0;
518 if (unlikely(pkt->seq != dr->rcv_nxt)) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000519 pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
520 pkt->seq, dr->rcv_nxt);
David S. Miller4c521e42007-07-09 22:23:51 -0700521 return 0;
522 }
523
Sowmini Varadhan69088822014-10-25 15:12:12 -0400524 if (!port->napi_resume)
525 dr->rcv_nxt++;
David S. Miller4c521e42007-07-09 22:23:51 -0700526
527 /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
528
Sowmini Varadhan69088822014-10-25 15:12:12 -0400529 return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx,
530 npkts, budget);
David S. Miller4c521e42007-07-09 22:23:51 -0700531}
532
533static int idx_is_pending(struct vio_dring_state *dr, u32 end)
534{
535 u32 idx = dr->cons;
536 int found = 0;
537
538 while (idx != dr->prod) {
539 if (idx == end) {
540 found = 1;
541 break;
542 }
543 idx = next_idx(idx, dr);
544 }
545 return found;
546}
547
548static int vnet_ack(struct vnet_port *port, void *msgbuf)
549{
550 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
551 struct vio_dring_data *pkt = msgbuf;
552 struct net_device *dev;
553 struct vnet *vp;
554 u32 end;
Sowmini Varadhand1015642014-09-11 09:57:22 -0400555 struct vio_net_desc *desc;
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400556 struct netdev_queue *txq;
557
David S. Miller4c521e42007-07-09 22:23:51 -0700558 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
559 return 0;
560
561 end = pkt->end_idx;
562 if (unlikely(!idx_is_pending(dr, end)))
563 return 0;
564
Sowmini Varadhan69088822014-10-25 15:12:12 -0400565 vp = port->vp;
566 dev = vp->dev;
Sowmini Varadhand1015642014-09-11 09:57:22 -0400567 /* sync for race conditions with vnet_start_xmit() and tell xmit it
568 * is time to send a trigger.
569 */
Sowmini Varadhan69088822014-10-25 15:12:12 -0400570 netif_tx_lock(dev);
David S. Miller4c521e42007-07-09 22:23:51 -0700571 dr->cons = next_idx(end, dr);
Sowmini Varadhand1015642014-09-11 09:57:22 -0400572 desc = vio_dring_entry(dr, dr->cons);
573 if (desc->hdr.state == VIO_DESC_READY && port->start_cons) {
574 /* vnet_start_xmit() just populated this dring but missed
575 * sending the "start" LDC message to the consumer.
576 * Send a "start" trigger on its behalf.
577 */
578 if (__vnet_tx_trigger(port, dr->cons) > 0)
579 port->start_cons = false;
580 else
581 port->start_cons = true;
582 } else {
583 port->start_cons = true;
584 }
Sowmini Varadhan69088822014-10-25 15:12:12 -0400585 netif_tx_unlock(dev);
Sowmini Varadhand1015642014-09-11 09:57:22 -0400586
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400587 txq = netdev_get_tx_queue(dev, port->q_index);
588 if (unlikely(netif_tx_queue_stopped(txq) &&
David S. Miller4c521e42007-07-09 22:23:51 -0700589 vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
590 return 1;
591
592 return 0;
593}
594
595static int vnet_nack(struct vnet_port *port, void *msgbuf)
596{
597 /* XXX just reset or similar XXX */
598 return 0;
599}
600
David S. Miller028ebff2007-07-20 02:30:25 -0700601static int handle_mcast(struct vnet_port *port, void *msgbuf)
602{
603 struct vio_net_mcast_info *pkt = msgbuf;
604
605 if (pkt->tag.stype != VIO_SUBTYPE_ACK)
Joe Perches4d5870e2010-08-17 07:55:05 +0000606 pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
David S. Miller028ebff2007-07-20 02:30:25 -0700607 port->vp->dev->name,
608 pkt->tag.type,
609 pkt->tag.stype,
610 pkt->tag.stype_env,
611 pkt->tag.sid);
612
613 return 0;
614}
615
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400616/* Got back a STOPPED LDC message on port. If the queue is stopped,
617 * wake it up so that we'll send out another START message at the
618 * next TX.
619 */
620static void maybe_tx_wakeup(struct vnet_port *port)
David S. Miller4c521e42007-07-09 22:23:51 -0700621{
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400622 struct netdev_queue *txq;
David S. Miller4c521e42007-07-09 22:23:51 -0700623
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400624 txq = netdev_get_tx_queue(port->vp->dev, port->q_index);
625 __netif_tx_lock(txq, smp_processor_id());
626 if (likely(netif_tx_queue_stopped(txq))) {
627 struct vio_dring_state *dr;
David S. Miller4c521e42007-07-09 22:23:51 -0700628
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400629 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
Sowmini Varadhan6c3ce8a2014-11-06 14:51:02 -0500630 netif_tx_wake_queue(txq);
David S. Miller4c521e42007-07-09 22:23:51 -0700631 }
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400632 __netif_tx_unlock(txq);
David S. Miller4c521e42007-07-09 22:23:51 -0700633}
634
Sowmini Varadhan69088822014-10-25 15:12:12 -0400635static inline bool port_is_up(struct vnet_port *vnet)
David S. Miller4c521e42007-07-09 22:23:51 -0700636{
Sowmini Varadhan69088822014-10-25 15:12:12 -0400637 struct vio_driver_state *vio = &vnet->vio;
638
639 return !!(vio->hs_state & VIO_HS_COMPLETE);
640}
641
642static int vnet_event_napi(struct vnet_port *port, int budget)
643{
David S. Miller4c521e42007-07-09 22:23:51 -0700644 struct vio_driver_state *vio = &port->vio;
David S. Miller4c521e42007-07-09 22:23:51 -0700645 int tx_wakeup, err;
Sowmini Varadhan69088822014-10-25 15:12:12 -0400646 int npkts = 0;
647 int event = (port->rx_event & LDC_EVENT_RESET);
David S. Miller4c521e42007-07-09 22:23:51 -0700648
Sowmini Varadhan69088822014-10-25 15:12:12 -0400649ldc_ctrl:
David S. Miller4c521e42007-07-09 22:23:51 -0700650 if (unlikely(event == LDC_EVENT_RESET ||
651 event == LDC_EVENT_UP)) {
652 vio_link_state_change(vio, event);
David S. Miller4c521e42007-07-09 22:23:51 -0700653
David L Stevense4defc72014-09-29 19:47:59 -0400654 if (event == LDC_EVENT_RESET) {
655 port->rmtu = 0;
David S. Millerd762acd2007-07-18 00:07:39 -0700656 vio_port_up(vio);
David L Stevense4defc72014-09-29 19:47:59 -0400657 }
Sowmini Varadhan69088822014-10-25 15:12:12 -0400658 port->rx_event = 0;
659 return 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700660 }
Sowmini Varadhan69088822014-10-25 15:12:12 -0400661 /* We may have multiple LDC events in rx_event. Unroll send_events() */
662 event = (port->rx_event & LDC_EVENT_UP);
663 port->rx_event &= ~(LDC_EVENT_RESET|LDC_EVENT_UP);
664 if (event == LDC_EVENT_UP)
665 goto ldc_ctrl;
666 event = port->rx_event;
667 if (!(event & LDC_EVENT_DATA_READY))
668 return 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700669
Sowmini Varadhan69088822014-10-25 15:12:12 -0400670 /* we dont expect any other bits than RESET, UP, DATA_READY */
671 BUG_ON(event != LDC_EVENT_DATA_READY);
David S. Miller4c521e42007-07-09 22:23:51 -0700672
673 tx_wakeup = err = 0;
674 while (1) {
675 union {
676 struct vio_msg_tag tag;
677 u64 raw[8];
678 } msgbuf;
679
Sowmini Varadhan69088822014-10-25 15:12:12 -0400680 if (port->napi_resume) {
681 struct vio_dring_data *pkt =
682 (struct vio_dring_data *)&msgbuf;
683 struct vio_dring_state *dr =
684 &port->vio.drings[VIO_DRIVER_RX_RING];
685
686 pkt->tag.type = VIO_TYPE_DATA;
687 pkt->tag.stype = VIO_SUBTYPE_INFO;
688 pkt->tag.stype_env = VIO_DRING_DATA;
689 pkt->seq = dr->rcv_nxt;
690 pkt->start_idx = next_idx(port->napi_stop_idx, dr);
691 pkt->end_idx = -1;
692 goto napi_resume;
693 }
David S. Miller4c521e42007-07-09 22:23:51 -0700694 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
695 if (unlikely(err < 0)) {
696 if (err == -ECONNRESET)
697 vio_conn_reset(vio);
698 break;
699 }
700 if (err == 0)
701 break;
702 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
703 msgbuf.tag.type,
704 msgbuf.tag.stype,
705 msgbuf.tag.stype_env,
706 msgbuf.tag.sid);
707 err = vio_validate_sid(vio, &msgbuf.tag);
708 if (err < 0)
709 break;
Sowmini Varadhan69088822014-10-25 15:12:12 -0400710napi_resume:
David S. Miller4c521e42007-07-09 22:23:51 -0700711 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
712 if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
Sowmini Varadhan69088822014-10-25 15:12:12 -0400713 if (!port_is_up(port)) {
714 /* failures like handshake_failure()
715 * may have cleaned up dring, but
716 * NAPI polling may bring us here.
717 */
718 err = -ECONNRESET;
719 break;
720 }
721 err = vnet_rx(port, &msgbuf, &npkts, budget);
722 if (npkts >= budget)
723 break;
Sowmini Varadhan8c4ee3e2014-11-06 14:51:08 -0500724 if (npkts == 0)
725 break;
David S. Miller4c521e42007-07-09 22:23:51 -0700726 } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
727 err = vnet_ack(port, &msgbuf);
728 if (err > 0)
729 tx_wakeup |= err;
730 } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
731 err = vnet_nack(port, &msgbuf);
732 }
733 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
David S. Miller028ebff2007-07-20 02:30:25 -0700734 if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
735 err = handle_mcast(port, &msgbuf);
736 else
737 err = vio_control_pkt_engine(vio, &msgbuf);
David S. Miller4c521e42007-07-09 22:23:51 -0700738 if (err)
739 break;
740 } else {
741 err = vnet_handle_unknown(port, &msgbuf);
742 }
743 if (err == -ECONNRESET)
744 break;
745 }
David S. Miller4c521e42007-07-09 22:23:51 -0700746 if (unlikely(tx_wakeup && err != -ECONNRESET))
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400747 maybe_tx_wakeup(port);
Sowmini Varadhan69088822014-10-25 15:12:12 -0400748 return npkts;
749}
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -0400750
Sowmini Varadhan69088822014-10-25 15:12:12 -0400751static int vnet_poll(struct napi_struct *napi, int budget)
752{
753 struct vnet_port *port = container_of(napi, struct vnet_port, napi);
754 struct vio_driver_state *vio = &port->vio;
755 int processed = vnet_event_napi(port, budget);
756
757 if (processed < budget) {
758 napi_complete(napi);
Sowmini Varadhan7bd68bf2014-10-30 12:45:58 -0400759 port->rx_event &= ~LDC_EVENT_DATA_READY;
Sowmini Varadhan69088822014-10-25 15:12:12 -0400760 vio_set_intr(vio->vdev->rx_ino, HV_INTR_ENABLED);
761 }
762 return processed;
763}
764
765static void vnet_event(void *arg, int event)
766{
767 struct vnet_port *port = arg;
768 struct vio_driver_state *vio = &port->vio;
769
770 port->rx_event |= event;
771 vio_set_intr(vio->vdev->rx_ino, HV_INTR_DISABLED);
772 napi_schedule(&port->napi);
773
David S. Miller4c521e42007-07-09 22:23:51 -0700774}
775
Sowmini Varadhand1015642014-09-11 09:57:22 -0400776static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
David S. Miller4c521e42007-07-09 22:23:51 -0700777{
778 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
779 struct vio_dring_data hdr = {
780 .tag = {
781 .type = VIO_TYPE_DATA,
782 .stype = VIO_SUBTYPE_INFO,
783 .stype_env = VIO_DRING_DATA,
784 .sid = vio_send_sid(&port->vio),
785 },
786 .dring_ident = dr->ident,
Sowmini Varadhand1015642014-09-11 09:57:22 -0400787 .start_idx = start,
David S. Miller4c521e42007-07-09 22:23:51 -0700788 .end_idx = (u32) -1,
789 };
790 int err, delay;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400791 int retries = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700792
Sowmini Varadhand1015642014-09-11 09:57:22 -0400793 if (port->stop_rx) {
794 err = vnet_send_ack(port,
795 &port->vio.drings[VIO_DRIVER_RX_RING],
796 port->stop_rx_idx, -1,
797 VIO_DRING_STOPPED);
798 if (err <= 0)
799 return err;
800 }
801
David S. Miller4c521e42007-07-09 22:23:51 -0700802 hdr.seq = dr->snd_nxt;
803 delay = 1;
804 do {
805 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
806 if (err > 0) {
807 dr->snd_nxt++;
808 break;
809 }
810 udelay(delay);
811 if ((delay <<= 1) > 128)
812 delay = 128;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400813 if (retries++ > VNET_MAX_RETRIES)
814 break;
David S. Miller4c521e42007-07-09 22:23:51 -0700815 } while (err == -EAGAIN);
816
817 return err;
818}
819
820struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
821{
822 unsigned int hash = vnet_hashfn(skb->data);
823 struct hlist_head *hp = &vp->port_hash[hash];
David S. Miller4c521e42007-07-09 22:23:51 -0700824 struct vnet_port *port;
825
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400826 hlist_for_each_entry_rcu(port, hp, hash) {
David L Stevens8266f5f2014-07-25 10:30:11 -0400827 if (!port_is_up(port))
828 continue;
Joe Perches2e42e472012-05-09 17:17:46 +0000829 if (ether_addr_equal(port->raddr, skb->data))
David S. Miller4c521e42007-07-09 22:23:51 -0700830 return port;
831 }
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400832 list_for_each_entry_rcu(port, &vp->port_list, list) {
David L Stevens8266f5f2014-07-25 10:30:11 -0400833 if (!port->switch_port)
834 continue;
835 if (!port_is_up(port))
836 continue;
837 return port;
838 }
839 return NULL;
David S. Miller4c521e42007-07-09 22:23:51 -0700840}
841
David L Stevens8e845f4c2014-09-29 19:48:11 -0400842static struct sk_buff *vnet_clean_tx_ring(struct vnet_port *port,
843 unsigned *pending)
844{
845 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
846 struct sk_buff *skb = NULL;
847 int i, txi;
848
849 *pending = 0;
850
851 txi = dr->prod-1;
852 if (txi < 0)
853 txi = VNET_TX_RING_SIZE-1;
854
855 for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
856 struct vio_net_desc *d;
857
858 d = vio_dring_entry(dr, txi);
859
860 if (d->hdr.state == VIO_DESC_DONE) {
861 if (port->tx_bufs[txi].skb) {
862 BUG_ON(port->tx_bufs[txi].skb->next);
863
864 port->tx_bufs[txi].skb->next = skb;
865 skb = port->tx_bufs[txi].skb;
866 port->tx_bufs[txi].skb = NULL;
867
868 ldc_unmap(port->vio.lp,
869 port->tx_bufs[txi].cookies,
870 port->tx_bufs[txi].ncookies);
871 }
872 d->hdr.state = VIO_DESC_FREE;
873 } else if (d->hdr.state == VIO_DESC_READY) {
874 (*pending)++;
875 } else if (d->hdr.state == VIO_DESC_FREE) {
876 break;
877 }
878 --txi;
879 if (txi < 0)
880 txi = VNET_TX_RING_SIZE-1;
881 }
882 return skb;
883}
884
885static inline void vnet_free_skbs(struct sk_buff *skb)
886{
887 struct sk_buff *next;
888
889 while (skb) {
890 next = skb->next;
891 skb->next = NULL;
892 dev_kfree_skb(skb);
893 skb = next;
894 }
895}
896
897static void vnet_clean_timer_expire(unsigned long port0)
898{
899 struct vnet_port *port = (struct vnet_port *)port0;
900 struct sk_buff *freeskbs;
901 unsigned pending;
David L Stevens8e845f4c2014-09-29 19:48:11 -0400902
Sowmini Varadhan13b13dd2014-10-25 15:12:31 -0400903 netif_tx_lock(port->vp->dev);
David L Stevens8e845f4c2014-09-29 19:48:11 -0400904 freeskbs = vnet_clean_tx_ring(port, &pending);
Sowmini Varadhan13b13dd2014-10-25 15:12:31 -0400905 netif_tx_unlock(port->vp->dev);
David L Stevens8e845f4c2014-09-29 19:48:11 -0400906
907 vnet_free_skbs(freeskbs);
908
909 if (pending)
910 (void)mod_timer(&port->clean_timer,
911 jiffies + VNET_CLEAN_TIMEOUT);
912 else
913 del_timer(&port->clean_timer);
914}
915
916static inline struct sk_buff *vnet_skb_shape(struct sk_buff *skb, void **pstart,
917 int *plen)
918{
919 struct sk_buff *nskb;
920 int len, pad;
921
922 len = skb->len;
923 pad = 0;
924 if (len < ETH_ZLEN) {
925 pad += ETH_ZLEN - skb->len;
926 len += pad;
927 }
928 len += VNET_PACKET_SKIP;
929 pad += 8 - (len & 7);
930 len += 8 - (len & 7);
931
932 if (((unsigned long)skb->data & 7) != VNET_PACKET_SKIP ||
933 skb_tailroom(skb) < pad ||
934 skb_headroom(skb) < VNET_PACKET_SKIP) {
935 nskb = alloc_and_align_skb(skb->dev, skb->len);
936 skb_reserve(nskb, VNET_PACKET_SKIP);
937 if (skb_copy_bits(skb, 0, nskb->data, skb->len)) {
938 dev_kfree_skb(nskb);
939 dev_kfree_skb(skb);
940 return NULL;
941 }
942 (void)skb_put(nskb, skb->len);
943 dev_kfree_skb(skb);
944 skb = nskb;
945 }
946
947 *pstart = skb->data - VNET_PACKET_SKIP;
948 *plen = len;
949 return skb;
950}
951
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400952static u16
953vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
954 void *accel_priv, select_queue_fallback_t fallback)
955{
956 struct vnet *vp = netdev_priv(dev);
957 struct vnet_port *port = __tx_port_find(vp, skb);
958
959 return port->q_index;
960}
961
David S. Miller4c521e42007-07-09 22:23:51 -0700962static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
963{
964 struct vnet *vp = netdev_priv(dev);
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400965 struct vnet_port *port = NULL;
David S. Miller4c521e42007-07-09 22:23:51 -0700966 struct vio_dring_state *dr;
967 struct vio_net_desc *d;
David S. Miller4c521e42007-07-09 22:23:51 -0700968 unsigned int len;
David L Stevens8e845f4c2014-09-29 19:48:11 -0400969 struct sk_buff *freeskbs = NULL;
970 int i, err, txi;
971 void *start = NULL;
972 int nlen = 0;
973 unsigned pending = 0;
Sowmini Varadhand51bffd2014-10-30 12:46:09 -0400974 struct netdev_queue *txq;
David S. Miller4c521e42007-07-09 22:23:51 -0700975
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400976 skb = vnet_skb_shape(skb, &start, &nlen);
977 if (unlikely(!skb))
David S. Miller4c521e42007-07-09 22:23:51 -0700978 goto out_dropped;
979
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400980 rcu_read_lock();
Sowmini Varadhan13b13dd2014-10-25 15:12:31 -0400981 port = __tx_port_find(vp, skb);
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -0400982 if (unlikely(!port))
David L Stevens8e845f4c2014-09-29 19:48:11 -0400983 goto out_dropped;
984
David L Stevensa2b78e92014-09-29 19:48:24 -0400985 if (skb->len > port->rmtu) {
986 unsigned long localmtu = port->rmtu - ETH_HLEN;
987
988 if (vio_version_after_eq(&port->vio, 1, 3))
989 localmtu -= VLAN_HLEN;
990
991 if (skb->protocol == htons(ETH_P_IP)) {
992 struct flowi4 fl4;
993 struct rtable *rt = NULL;
994
995 memset(&fl4, 0, sizeof(fl4));
996 fl4.flowi4_oif = dev->ifindex;
997 fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
998 fl4.daddr = ip_hdr(skb)->daddr;
999 fl4.saddr = ip_hdr(skb)->saddr;
1000
1001 rt = ip_route_output_key(dev_net(dev), &fl4);
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001002 rcu_read_unlock();
David L Stevensa2b78e92014-09-29 19:48:24 -04001003 if (!IS_ERR(rt)) {
1004 skb_dst_set(skb, &rt->dst);
1005 icmp_send(skb, ICMP_DEST_UNREACH,
1006 ICMP_FRAG_NEEDED,
1007 htonl(localmtu));
1008 }
1009 }
1010#if IS_ENABLED(CONFIG_IPV6)
1011 else if (skb->protocol == htons(ETH_P_IPV6))
1012 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, localmtu);
1013#endif
David L Stevens42db6722014-09-29 19:48:18 -04001014 goto out_dropped;
David L Stevensa2b78e92014-09-29 19:48:24 -04001015 }
David L Stevens42db6722014-09-29 19:48:18 -04001016
David S. Miller4c521e42007-07-09 22:23:51 -07001017 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001018 i = skb_get_queue_mapping(skb);
1019 txq = netdev_get_tx_queue(dev, i);
Dwight Engend0aedcd2014-09-19 09:43:02 -04001020 if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001021 if (!netif_tx_queue_stopped(txq)) {
1022 netif_tx_stop_queue(txq);
David S. Miller4c521e42007-07-09 22:23:51 -07001023
1024 /* This is a hard error, log it. */
Joe Perches4d5870e2010-08-17 07:55:05 +00001025 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
David S. Miller4c521e42007-07-09 22:23:51 -07001026 dev->stats.tx_errors++;
1027 }
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001028 rcu_read_unlock();
David S. Miller4c521e42007-07-09 22:23:51 -07001029 return NETDEV_TX_BUSY;
1030 }
1031
1032 d = vio_dring_cur(dr);
1033
David L Stevens8e845f4c2014-09-29 19:48:11 -04001034 txi = dr->prod;
1035
1036 freeskbs = vnet_clean_tx_ring(port, &pending);
1037
1038 BUG_ON(port->tx_bufs[txi].skb);
David S. Miller4c521e42007-07-09 22:23:51 -07001039
1040 len = skb->len;
David L Stevens8e845f4c2014-09-29 19:48:11 -04001041 if (len < ETH_ZLEN)
David S. Miller4c521e42007-07-09 22:23:51 -07001042 len = ETH_ZLEN;
David L Stevens8e845f4c2014-09-29 19:48:11 -04001043
1044 port->tx_bufs[txi].skb = skb;
1045 skb = NULL;
1046
1047 err = ldc_map_single(port->vio.lp, start, nlen,
David L Stevens42db6722014-09-29 19:48:18 -04001048 port->tx_bufs[txi].cookies, VNET_MAXCOOKIES,
David L Stevens8e845f4c2014-09-29 19:48:11 -04001049 (LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_RW));
1050 if (err < 0) {
1051 netdev_info(dev, "tx buffer map error %d\n", err);
Sowmini Varadhan13b13dd2014-10-25 15:12:31 -04001052 goto out_dropped;
David S. Miller4c521e42007-07-09 22:23:51 -07001053 }
David L Stevens8e845f4c2014-09-29 19:48:11 -04001054 port->tx_bufs[txi].ncookies = err;
David S. Miller4c521e42007-07-09 22:23:51 -07001055
Sowmini Varadhan1f6394e2014-08-12 10:27:24 -04001056 /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
1057 * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
1058 * the protocol itself does not require it as long as the peer
1059 * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
1060 *
1061 * An ACK for every packet in the ring is expensive as the
1062 * sending of LDC messages is slow and affects performance.
1063 */
1064 d->hdr.ack = VIO_ACK_DISABLE;
David S. Miller4c521e42007-07-09 22:23:51 -07001065 d->size = len;
David L Stevens8e845f4c2014-09-29 19:48:11 -04001066 d->ncookies = port->tx_bufs[txi].ncookies;
David S. Miller4c521e42007-07-09 22:23:51 -07001067 for (i = 0; i < d->ncookies; i++)
David L Stevens8e845f4c2014-09-29 19:48:11 -04001068 d->cookies[i] = port->tx_bufs[txi].cookies[i];
David S. Miller4c521e42007-07-09 22:23:51 -07001069
1070 /* This has to be a non-SMP write barrier because we are writing
1071 * to memory which is shared with the peer LDOM.
1072 */
1073 wmb();
1074
1075 d->hdr.state = VIO_DESC_READY;
1076
Sowmini Varadhand1015642014-09-11 09:57:22 -04001077 /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
1078 * to notify the consumer that some descriptors are READY.
1079 * After that "start" trigger, no additional triggers are needed until
1080 * a DRING_STOPPED is received from the consumer. The dr->cons field
1081 * (set up by vnet_ack()) has the value of the next dring index
1082 * that has not yet been ack-ed. We send a "start" trigger here
1083 * if, and only if, start_cons is true (reset it afterward). Conversely,
1084 * vnet_ack() should check if the dring corresponding to cons
1085 * is marked READY, but start_cons was false.
1086 * If so, vnet_ack() should send out the missed "start" trigger.
1087 *
1088 * Note that the wmb() above makes sure the cookies et al. are
1089 * not globally visible before the VIO_DESC_READY, and that the
1090 * stores are ordered correctly by the compiler. The consumer will
1091 * not proceed until the VIO_DESC_READY is visible assuring that
1092 * the consumer does not observe anything related to descriptors
1093 * out of order. The HV trap from the LDC start trigger is the
1094 * producer to consumer announcement that work is available to the
1095 * consumer
1096 */
1097 if (!port->start_cons)
1098 goto ldc_start_done; /* previous trigger suffices */
1099
1100 err = __vnet_tx_trigger(port, dr->cons);
David S. Miller4c521e42007-07-09 22:23:51 -07001101 if (unlikely(err < 0)) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001102 netdev_info(dev, "TX trigger error %d\n", err);
David S. Miller4c521e42007-07-09 22:23:51 -07001103 d->hdr.state = VIO_DESC_FREE;
1104 dev->stats.tx_carrier_errors++;
Sowmini Varadhan13b13dd2014-10-25 15:12:31 -04001105 goto out_dropped;
David S. Miller4c521e42007-07-09 22:23:51 -07001106 }
1107
Sowmini Varadhand1015642014-09-11 09:57:22 -04001108ldc_start_done:
1109 port->start_cons = false;
1110
David S. Miller4c521e42007-07-09 22:23:51 -07001111 dev->stats.tx_packets++;
David L Stevens8e845f4c2014-09-29 19:48:11 -04001112 dev->stats.tx_bytes += port->tx_bufs[txi].skb->len;
David S. Miller4c521e42007-07-09 22:23:51 -07001113
1114 dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
Dwight Engend0aedcd2014-09-19 09:43:02 -04001115 if (unlikely(vnet_tx_dring_avail(dr) < 1)) {
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001116 netif_tx_stop_queue(txq);
David S. Miller4c521e42007-07-09 22:23:51 -07001117 if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001118 netif_tx_wake_queue(txq);
David S. Miller4c521e42007-07-09 22:23:51 -07001119 }
1120
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001121 (void)mod_timer(&port->clean_timer, jiffies + VNET_CLEAN_TIMEOUT);
1122 rcu_read_unlock();
David S. Miller4c521e42007-07-09 22:23:51 -07001123
David L Stevens8e845f4c2014-09-29 19:48:11 -04001124 vnet_free_skbs(freeskbs);
1125
David S. Miller4c521e42007-07-09 22:23:51 -07001126 return NETDEV_TX_OK;
1127
David S. Miller4c521e42007-07-09 22:23:51 -07001128out_dropped:
David L Stevens8e845f4c2014-09-29 19:48:11 -04001129 if (pending)
1130 (void)mod_timer(&port->clean_timer,
1131 jiffies + VNET_CLEAN_TIMEOUT);
David L Stevensa29c9c42014-10-01 11:05:27 -04001132 else if (port)
David L Stevens8e845f4c2014-09-29 19:48:11 -04001133 del_timer(&port->clean_timer);
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001134 if (port)
1135 rcu_read_unlock();
1136 if (skb)
1137 dev_kfree_skb(skb);
1138 vnet_free_skbs(freeskbs);
David S. Miller4c521e42007-07-09 22:23:51 -07001139 dev->stats.tx_dropped++;
1140 return NETDEV_TX_OK;
1141}
1142
1143static void vnet_tx_timeout(struct net_device *dev)
1144{
1145 /* XXX Implement me XXX */
1146}
1147
1148static int vnet_open(struct net_device *dev)
1149{
1150 netif_carrier_on(dev);
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001151 netif_tx_start_all_queues(dev);
David S. Miller4c521e42007-07-09 22:23:51 -07001152
1153 return 0;
1154}
1155
1156static int vnet_close(struct net_device *dev)
1157{
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001158 netif_tx_stop_all_queues(dev);
David S. Miller4c521e42007-07-09 22:23:51 -07001159 netif_carrier_off(dev);
1160
1161 return 0;
1162}
1163
David S. Miller028ebff2007-07-20 02:30:25 -07001164static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
1165{
1166 struct vnet_mcast_entry *m;
1167
1168 for (m = vp->mcast_list; m; m = m->next) {
dingtianhong00fa4ce2013-12-30 15:41:24 +08001169 if (ether_addr_equal(m->addr, addr))
David S. Miller028ebff2007-07-20 02:30:25 -07001170 return m;
1171 }
1172 return NULL;
1173}
1174
1175static void __update_mc_list(struct vnet *vp, struct net_device *dev)
1176{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001177 struct netdev_hw_addr *ha;
David S. Miller028ebff2007-07-20 02:30:25 -07001178
Jiri Pirko22bedad32010-04-01 21:22:57 +00001179 netdev_for_each_mc_addr(ha, dev) {
David S. Miller028ebff2007-07-20 02:30:25 -07001180 struct vnet_mcast_entry *m;
1181
Jiri Pirko22bedad32010-04-01 21:22:57 +00001182 m = __vnet_mc_find(vp, ha->addr);
David S. Miller028ebff2007-07-20 02:30:25 -07001183 if (m) {
1184 m->hit = 1;
1185 continue;
1186 }
1187
1188 if (!m) {
1189 m = kzalloc(sizeof(*m), GFP_ATOMIC);
1190 if (!m)
1191 continue;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001192 memcpy(m->addr, ha->addr, ETH_ALEN);
David S. Miller028ebff2007-07-20 02:30:25 -07001193 m->hit = 1;
1194
1195 m->next = vp->mcast_list;
1196 vp->mcast_list = m;
1197 }
1198 }
1199}
1200
1201static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
1202{
1203 struct vio_net_mcast_info info;
1204 struct vnet_mcast_entry *m, **pp;
1205 int n_addrs;
1206
1207 memset(&info, 0, sizeof(info));
1208
1209 info.tag.type = VIO_TYPE_CTRL;
1210 info.tag.stype = VIO_SUBTYPE_INFO;
1211 info.tag.stype_env = VNET_MCAST_INFO;
1212 info.tag.sid = vio_send_sid(&port->vio);
1213 info.set = 1;
1214
1215 n_addrs = 0;
1216 for (m = vp->mcast_list; m; m = m->next) {
1217 if (m->sent)
1218 continue;
1219 m->sent = 1;
1220 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1221 m->addr, ETH_ALEN);
1222 if (++n_addrs == VNET_NUM_MCAST) {
1223 info.count = n_addrs;
1224
1225 (void) vio_ldc_send(&port->vio, &info,
1226 sizeof(info));
1227 n_addrs = 0;
1228 }
1229 }
1230 if (n_addrs) {
1231 info.count = n_addrs;
1232 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1233 }
1234
1235 info.set = 0;
1236
1237 n_addrs = 0;
1238 pp = &vp->mcast_list;
1239 while ((m = *pp) != NULL) {
1240 if (m->hit) {
1241 m->hit = 0;
1242 pp = &m->next;
1243 continue;
1244 }
1245
1246 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1247 m->addr, ETH_ALEN);
1248 if (++n_addrs == VNET_NUM_MCAST) {
1249 info.count = n_addrs;
1250 (void) vio_ldc_send(&port->vio, &info,
1251 sizeof(info));
1252 n_addrs = 0;
1253 }
1254
1255 *pp = m->next;
1256 kfree(m);
1257 }
1258 if (n_addrs) {
1259 info.count = n_addrs;
1260 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1261 }
1262}
1263
David S. Miller4c521e42007-07-09 22:23:51 -07001264static void vnet_set_rx_mode(struct net_device *dev)
1265{
David S. Miller028ebff2007-07-20 02:30:25 -07001266 struct vnet *vp = netdev_priv(dev);
1267 struct vnet_port *port;
David S. Miller028ebff2007-07-20 02:30:25 -07001268
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001269 rcu_read_lock();
1270 list_for_each_entry_rcu(port, &vp->port_list, list) {
David S. Miller028ebff2007-07-20 02:30:25 -07001271
1272 if (port->switch_port) {
1273 __update_mc_list(vp, dev);
1274 __send_mc_list(vp, port);
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001275 break;
David S. Miller028ebff2007-07-20 02:30:25 -07001276 }
1277 }
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001278 rcu_read_unlock();
David S. Miller4c521e42007-07-09 22:23:51 -07001279}
1280
1281static int vnet_change_mtu(struct net_device *dev, int new_mtu)
1282{
David L Stevens42db6722014-09-29 19:48:18 -04001283 if (new_mtu < 68 || new_mtu > 65535)
David S. Miller4c521e42007-07-09 22:23:51 -07001284 return -EINVAL;
1285
1286 dev->mtu = new_mtu;
1287 return 0;
1288}
1289
1290static int vnet_set_mac_addr(struct net_device *dev, void *p)
1291{
1292 return -EINVAL;
1293}
1294
1295static void vnet_get_drvinfo(struct net_device *dev,
1296 struct ethtool_drvinfo *info)
1297{
Jiri Pirko7826d432013-01-06 00:44:26 +00001298 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1299 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
David S. Miller4c521e42007-07-09 22:23:51 -07001300}
1301
1302static u32 vnet_get_msglevel(struct net_device *dev)
1303{
1304 struct vnet *vp = netdev_priv(dev);
1305 return vp->msg_enable;
1306}
1307
1308static void vnet_set_msglevel(struct net_device *dev, u32 value)
1309{
1310 struct vnet *vp = netdev_priv(dev);
1311 vp->msg_enable = value;
1312}
1313
1314static const struct ethtool_ops vnet_ethtool_ops = {
1315 .get_drvinfo = vnet_get_drvinfo,
1316 .get_msglevel = vnet_get_msglevel,
1317 .set_msglevel = vnet_set_msglevel,
1318 .get_link = ethtool_op_get_link,
David S. Miller4c521e42007-07-09 22:23:51 -07001319};
1320
1321static void vnet_port_free_tx_bufs(struct vnet_port *port)
1322{
1323 struct vio_dring_state *dr;
1324 int i;
1325
1326 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1327 if (dr->base) {
1328 ldc_free_exp_dring(port->vio.lp, dr->base,
1329 (dr->entry_size * dr->num_entries),
1330 dr->cookies, dr->ncookies);
1331 dr->base = NULL;
1332 dr->entry_size = 0;
1333 dr->num_entries = 0;
1334 dr->pending = 0;
1335 dr->ncookies = 0;
1336 }
1337
1338 for (i = 0; i < VNET_TX_RING_SIZE; i++) {
David L Stevens8e845f4c2014-09-29 19:48:11 -04001339 struct vio_net_desc *d;
1340 void *skb = port->tx_bufs[i].skb;
David S. Miller4c521e42007-07-09 22:23:51 -07001341
David L Stevens8e845f4c2014-09-29 19:48:11 -04001342 if (!skb)
David S. Miller4c521e42007-07-09 22:23:51 -07001343 continue;
1344
David L Stevens8e845f4c2014-09-29 19:48:11 -04001345 d = vio_dring_entry(dr, i);
1346 if (d->hdr.state == VIO_DESC_READY)
1347 pr_warn("active transmit buffers freed\n");
1348
David S. Miller4c521e42007-07-09 22:23:51 -07001349 ldc_unmap(port->vio.lp,
1350 port->tx_bufs[i].cookies,
1351 port->tx_bufs[i].ncookies);
David L Stevens8e845f4c2014-09-29 19:48:11 -04001352 dev_kfree_skb(skb);
1353 port->tx_bufs[i].skb = NULL;
1354 d->hdr.state = VIO_DESC_FREE;
David S. Miller4c521e42007-07-09 22:23:51 -07001355 }
1356}
1357
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001358static int vnet_port_alloc_tx_bufs(struct vnet_port *port)
David S. Miller4c521e42007-07-09 22:23:51 -07001359{
1360 struct vio_dring_state *dr;
1361 unsigned long len;
1362 int i, err, ncookies;
1363 void *dring;
1364
David S. Miller4c521e42007-07-09 22:23:51 -07001365 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1366
1367 len = (VNET_TX_RING_SIZE *
1368 (sizeof(struct vio_net_desc) +
1369 (sizeof(struct ldc_trans_cookie) * 2)));
1370
1371 ncookies = VIO_MAX_RING_COOKIES;
1372 dring = ldc_alloc_exp_dring(port->vio.lp, len,
1373 dr->cookies, &ncookies,
1374 (LDC_MAP_SHADOW |
1375 LDC_MAP_DIRECT |
1376 LDC_MAP_RW));
1377 if (IS_ERR(dring)) {
1378 err = PTR_ERR(dring);
1379 goto err_out;
1380 }
1381
1382 dr->base = dring;
1383 dr->entry_size = (sizeof(struct vio_net_desc) +
1384 (sizeof(struct ldc_trans_cookie) * 2));
1385 dr->num_entries = VNET_TX_RING_SIZE;
1386 dr->prod = dr->cons = 0;
Sowmini Varadhand1015642014-09-11 09:57:22 -04001387 port->start_cons = true; /* need an initial trigger */
David S. Miller4c521e42007-07-09 22:23:51 -07001388 dr->pending = VNET_TX_RING_SIZE;
1389 dr->ncookies = ncookies;
1390
David L Stevens8e845f4c2014-09-29 19:48:11 -04001391 for (i = 0; i < VNET_TX_RING_SIZE; ++i) {
1392 struct vio_net_desc *d;
1393
1394 d = vio_dring_entry(dr, i);
1395 d->hdr.state = VIO_DESC_FREE;
1396 }
David S. Miller4c521e42007-07-09 22:23:51 -07001397 return 0;
1398
1399err_out:
1400 vnet_port_free_tx_bufs(port);
1401
1402 return err;
1403}
1404
Sowmini Varadhan69088822014-10-25 15:12:12 -04001405#ifdef CONFIG_NET_POLL_CONTROLLER
1406static void vnet_poll_controller(struct net_device *dev)
1407{
1408 struct vnet *vp = netdev_priv(dev);
1409 struct vnet_port *port;
1410 unsigned long flags;
1411
1412 spin_lock_irqsave(&vp->lock, flags);
1413 if (!list_empty(&vp->port_list)) {
1414 port = list_entry(vp->port_list.next, struct vnet_port, list);
1415 napi_schedule(&port->napi);
1416 }
1417 spin_unlock_irqrestore(&vp->lock, flags);
1418}
1419#endif
David S. Miller9184a042007-07-17 22:19:10 -07001420static LIST_HEAD(vnet_list);
1421static DEFINE_MUTEX(vnet_list_mutex);
1422
David S. Miller8fd17f22009-03-20 00:51:22 -07001423static const struct net_device_ops vnet_ops = {
1424 .ndo_open = vnet_open,
1425 .ndo_stop = vnet_close,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001426 .ndo_set_rx_mode = vnet_set_rx_mode,
David S. Miller8fd17f22009-03-20 00:51:22 -07001427 .ndo_set_mac_address = vnet_set_mac_addr,
Ben Hutchings240c1022009-07-09 17:54:35 +00001428 .ndo_validate_addr = eth_validate_addr,
David S. Miller8fd17f22009-03-20 00:51:22 -07001429 .ndo_tx_timeout = vnet_tx_timeout,
1430 .ndo_change_mtu = vnet_change_mtu,
1431 .ndo_start_xmit = vnet_start_xmit,
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001432 .ndo_select_queue = vnet_select_queue,
Sowmini Varadhan69088822014-10-25 15:12:12 -04001433#ifdef CONFIG_NET_POLL_CONTROLLER
1434 .ndo_poll_controller = vnet_poll_controller,
1435#endif
David S. Miller8fd17f22009-03-20 00:51:22 -07001436};
1437
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001438static struct vnet *vnet_new(const u64 *local_mac)
David S. Miller9184a042007-07-17 22:19:10 -07001439{
1440 struct net_device *dev;
1441 struct vnet *vp;
1442 int err, i;
1443
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001444 dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
Joe Perches41de8d42012-01-29 13:47:52 +00001445 if (!dev)
David S. Miller9184a042007-07-17 22:19:10 -07001446 return ERR_PTR(-ENOMEM);
David L Stevens8e845f4c2014-09-29 19:48:11 -04001447 dev->needed_headroom = VNET_PACKET_SKIP + 8;
1448 dev->needed_tailroom = 8;
David S. Miller9184a042007-07-17 22:19:10 -07001449
1450 for (i = 0; i < ETH_ALEN; i++)
1451 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
1452
David S. Miller9184a042007-07-17 22:19:10 -07001453 vp = netdev_priv(dev);
1454
1455 spin_lock_init(&vp->lock);
1456 vp->dev = dev;
1457
1458 INIT_LIST_HEAD(&vp->port_list);
1459 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
1460 INIT_HLIST_HEAD(&vp->port_hash[i]);
1461 INIT_LIST_HEAD(&vp->list);
1462 vp->local_mac = *local_mac;
1463
David S. Miller8fd17f22009-03-20 00:51:22 -07001464 dev->netdev_ops = &vnet_ops;
David S. Miller9184a042007-07-17 22:19:10 -07001465 dev->ethtool_ops = &vnet_ethtool_ops;
1466 dev->watchdog_timeo = VNET_TX_TIMEOUT;
David S. Miller9184a042007-07-17 22:19:10 -07001467
1468 err = register_netdev(dev);
1469 if (err) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001470 pr_err("Cannot register net device, aborting\n");
David S. Miller9184a042007-07-17 22:19:10 -07001471 goto err_out_free_dev;
1472 }
1473
Joe Perches4d5870e2010-08-17 07:55:05 +00001474 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
David S. Miller9184a042007-07-17 22:19:10 -07001475
1476 list_add(&vp->list, &vnet_list);
1477
1478 return vp;
1479
1480err_out_free_dev:
1481 free_netdev(dev);
1482
1483 return ERR_PTR(err);
1484}
1485
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001486static struct vnet *vnet_find_or_create(const u64 *local_mac)
David S. Miller9184a042007-07-17 22:19:10 -07001487{
1488 struct vnet *iter, *vp;
1489
1490 mutex_lock(&vnet_list_mutex);
1491 vp = NULL;
1492 list_for_each_entry(iter, &vnet_list, list) {
1493 if (iter->local_mac == *local_mac) {
1494 vp = iter;
1495 break;
1496 }
1497 }
1498 if (!vp)
1499 vp = vnet_new(local_mac);
1500 mutex_unlock(&vnet_list_mutex);
1501
1502 return vp;
1503}
1504
Sowmini Varadhana4b70a02014-07-16 10:02:26 -04001505static void vnet_cleanup(void)
1506{
1507 struct vnet *vp;
1508 struct net_device *dev;
1509
1510 mutex_lock(&vnet_list_mutex);
1511 while (!list_empty(&vnet_list)) {
1512 vp = list_first_entry(&vnet_list, struct vnet, list);
1513 list_del(&vp->list);
1514 dev = vp->dev;
1515 /* vio_unregister_driver() should have cleaned up port_list */
1516 BUG_ON(!list_empty(&vp->port_list));
1517 unregister_netdev(dev);
1518 free_netdev(dev);
1519 }
1520 mutex_unlock(&vnet_list_mutex);
1521}
1522
David S. Miller9184a042007-07-17 22:19:10 -07001523static const char *local_mac_prop = "local-mac-address";
1524
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001525static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
David S. Miller9184a042007-07-17 22:19:10 -07001526 u64 port_node)
1527{
1528 const u64 *local_mac = NULL;
1529 u64 a;
1530
1531 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
1532 u64 target = mdesc_arc_target(hp, a);
1533 const char *name;
1534
1535 name = mdesc_get_property(hp, target, "name", NULL);
1536 if (!name || strcmp(name, "network"))
1537 continue;
1538
1539 local_mac = mdesc_get_property(hp, target,
1540 local_mac_prop, NULL);
1541 if (local_mac)
1542 break;
1543 }
1544 if (!local_mac)
1545 return ERR_PTR(-ENODEV);
1546
1547 return vnet_find_or_create(local_mac);
1548}
1549
David S. Miller4c521e42007-07-09 22:23:51 -07001550static struct ldc_channel_config vnet_ldc_cfg = {
1551 .event = vnet_event,
1552 .mtu = 64,
1553 .mode = LDC_MODE_UNRELIABLE,
1554};
1555
1556static struct vio_driver_ops vnet_vio_ops = {
1557 .send_attr = vnet_send_attr,
1558 .handle_attr = vnet_handle_attr,
1559 .handshake_complete = vnet_handshake_complete,
1560};
1561
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001562static void print_version(void)
David S. Miller9184a042007-07-17 22:19:10 -07001563{
Joe Perches4d5870e2010-08-17 07:55:05 +00001564 printk_once(KERN_INFO "%s", version);
David S. Miller9184a042007-07-17 22:19:10 -07001565}
1566
David S. Miller4c521e42007-07-09 22:23:51 -07001567const char *remote_macaddr_prop = "remote-mac-address";
1568
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001569static void
1570vnet_port_add_txq(struct vnet_port *port)
1571{
1572 struct vnet *vp = port->vp;
1573 int n;
1574
1575 n = vp->nports++;
1576 n = n & (VNET_MAX_TXQS - 1);
1577 port->q_index = n;
1578 netif_tx_wake_queue(netdev_get_tx_queue(vp->dev, port->q_index));
1579}
1580
1581static void
1582vnet_port_rm_txq(struct vnet_port *port)
1583{
1584 port->vp->nports--;
1585 netif_tx_stop_queue(netdev_get_tx_queue(port->vp->dev, port->q_index));
1586}
1587
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001588static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller4c521e42007-07-09 22:23:51 -07001589{
David S. Miller43fdf272007-07-12 13:47:50 -07001590 struct mdesc_handle *hp;
David S. Miller4c521e42007-07-09 22:23:51 -07001591 struct vnet_port *port;
1592 unsigned long flags;
1593 struct vnet *vp;
1594 const u64 *rmac;
1595 int len, i, err, switch_port;
1596
David S. Miller9184a042007-07-17 22:19:10 -07001597 print_version();
David S. Miller4c521e42007-07-09 22:23:51 -07001598
David S. Miller43fdf272007-07-12 13:47:50 -07001599 hp = mdesc_grab();
1600
David S. Miller9184a042007-07-17 22:19:10 -07001601 vp = vnet_find_parent(hp, vdev->mp);
1602 if (IS_ERR(vp)) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001603 pr_err("Cannot find port parent vnet\n");
David S. Miller9184a042007-07-17 22:19:10 -07001604 err = PTR_ERR(vp);
1605 goto err_out_put_mdesc;
1606 }
1607
David S. Miller43fdf272007-07-12 13:47:50 -07001608 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
1609 err = -ENODEV;
David S. Miller4c521e42007-07-09 22:23:51 -07001610 if (!rmac) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001611 pr_err("Port lacks %s property\n", remote_macaddr_prop);
David S. Miller43fdf272007-07-12 13:47:50 -07001612 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -07001613 }
1614
1615 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -07001616 err = -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001617 if (!port)
David S. Miller43fdf272007-07-12 13:47:50 -07001618 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -07001619
1620 for (i = 0; i < ETH_ALEN; i++)
1621 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
1622
1623 port->vp = vp;
1624
David S. Miller43fdf272007-07-12 13:47:50 -07001625 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
David S. Miller4c521e42007-07-09 22:23:51 -07001626 vnet_versions, ARRAY_SIZE(vnet_versions),
1627 &vnet_vio_ops, vp->dev->name);
1628 if (err)
1629 goto err_out_free_port;
1630
1631 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
1632 if (err)
1633 goto err_out_free_port;
1634
Sowmini Varadhan69088822014-10-25 15:12:12 -04001635 netif_napi_add(port->vp->dev, &port->napi, vnet_poll, NAPI_POLL_WEIGHT);
1636
David S. Miller4c521e42007-07-09 22:23:51 -07001637 err = vnet_port_alloc_tx_bufs(port);
1638 if (err)
1639 goto err_out_free_ldc;
1640
1641 INIT_HLIST_NODE(&port->hash);
1642 INIT_LIST_HEAD(&port->list);
1643
1644 switch_port = 0;
David S. Miller43fdf272007-07-12 13:47:50 -07001645 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL) != NULL)
David S. Miller4c521e42007-07-09 22:23:51 -07001646 switch_port = 1;
David S. Miller028ebff2007-07-20 02:30:25 -07001647 port->switch_port = switch_port;
David S. Miller4c521e42007-07-09 22:23:51 -07001648
1649 spin_lock_irqsave(&vp->lock, flags);
1650 if (switch_port)
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001651 list_add_rcu(&port->list, &vp->port_list);
David S. Miller4c521e42007-07-09 22:23:51 -07001652 else
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001653 list_add_tail_rcu(&port->list, &vp->port_list);
1654 hlist_add_head_rcu(&port->hash,
1655 &vp->port_hash[vnet_hashfn(port->raddr)]);
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001656 vnet_port_add_txq(port);
David S. Miller4c521e42007-07-09 22:23:51 -07001657 spin_unlock_irqrestore(&vp->lock, flags);
1658
1659 dev_set_drvdata(&vdev->dev, port);
1660
Joe Perches4d5870e2010-08-17 07:55:05 +00001661 pr_info("%s: PORT ( remote-mac %pM%s )\n",
1662 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
David S. Miller4c521e42007-07-09 22:23:51 -07001663
David L Stevens8e845f4c2014-09-29 19:48:11 -04001664 setup_timer(&port->clean_timer, vnet_clean_timer_expire,
1665 (unsigned long)port);
1666
Sowmini Varadhan69088822014-10-25 15:12:12 -04001667 napi_enable(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -07001668 vio_port_up(&port->vio);
1669
David S. Miller43fdf272007-07-12 13:47:50 -07001670 mdesc_release(hp);
1671
David S. Miller4c521e42007-07-09 22:23:51 -07001672 return 0;
1673
1674err_out_free_ldc:
Sowmini Varadhan69088822014-10-25 15:12:12 -04001675 netif_napi_del(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -07001676 vio_ldc_free(&port->vio);
1677
1678err_out_free_port:
1679 kfree(port);
1680
David S. Miller43fdf272007-07-12 13:47:50 -07001681err_out_put_mdesc:
1682 mdesc_release(hp);
David S. Miller4c521e42007-07-09 22:23:51 -07001683 return err;
1684}
1685
1686static int vnet_port_remove(struct vio_dev *vdev)
1687{
1688 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
1689
1690 if (port) {
David S. Miller4c521e42007-07-09 22:23:51 -07001691
1692 del_timer_sync(&port->vio.timer);
1693
Sowmini Varadhan69088822014-10-25 15:12:12 -04001694 napi_disable(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -07001695
Sowmini Varadhan2a968dd2014-10-25 15:12:20 -04001696 list_del_rcu(&port->list);
1697 hlist_del_rcu(&port->hash);
1698
1699 synchronize_rcu();
1700 del_timer_sync(&port->clean_timer);
Sowmini Varadhand51bffd2014-10-30 12:46:09 -04001701 vnet_port_rm_txq(port);
Sowmini Varadhan69088822014-10-25 15:12:12 -04001702 netif_napi_del(&port->napi);
David S. Miller4c521e42007-07-09 22:23:51 -07001703 vnet_port_free_tx_bufs(port);
1704 vio_ldc_free(&port->vio);
1705
1706 dev_set_drvdata(&vdev->dev, NULL);
1707
1708 kfree(port);
Dave Kleikampaabb9872013-07-01 16:49:22 -05001709
David S. Miller4c521e42007-07-09 22:23:51 -07001710 }
1711 return 0;
1712}
1713
David S. Miller3d452e52008-09-01 01:48:52 -07001714static const struct vio_device_id vnet_port_match[] = {
David S. Miller4c521e42007-07-09 22:23:51 -07001715 {
1716 .type = "vnet-port",
1717 },
1718 {},
1719};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -07001720MODULE_DEVICE_TABLE(vio, vnet_port_match);
David S. Miller4c521e42007-07-09 22:23:51 -07001721
1722static struct vio_driver vnet_port_driver = {
1723 .id_table = vnet_port_match,
1724 .probe = vnet_port_probe,
1725 .remove = vnet_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001726 .name = "vnet_port",
David S. Miller4c521e42007-07-09 22:23:51 -07001727};
1728
David S. Miller4c521e42007-07-09 22:23:51 -07001729static int __init vnet_init(void)
1730{
David S. Miller9184a042007-07-17 22:19:10 -07001731 return vio_register_driver(&vnet_port_driver);
David S. Miller4c521e42007-07-09 22:23:51 -07001732}
1733
1734static void __exit vnet_exit(void)
1735{
1736 vio_unregister_driver(&vnet_port_driver);
Sowmini Varadhana4b70a02014-07-16 10:02:26 -04001737 vnet_cleanup();
David S. Miller4c521e42007-07-09 22:23:51 -07001738}
1739
1740module_init(vnet_init);
1741module_exit(vnet_exit);