blob: b1abcadb42ff2fe7b14938ef6eeeaa36cd6c04d0 [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
20#include <asm/vio.h>
21#include <asm/ldc.h>
22
23#include "sunvnet.h"
24
25#define DRV_MODULE_NAME "sunvnet"
David S. Miller4c521e42007-07-09 22:23:51 -070026#define DRV_MODULE_VERSION "1.0"
27#define DRV_MODULE_RELDATE "June 25, 2007"
28
Bill Pembertonf73d12b2012-12-03 09:24:02 -050029static char version[] =
David S. Miller4c521e42007-07-09 22:23:51 -070030 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32MODULE_DESCRIPTION("Sun LDOM virtual network driver");
33MODULE_LICENSE("GPL");
34MODULE_VERSION(DRV_MODULE_VERSION);
35
Sowmini Varadhanadddc322014-08-12 10:33:10 -040036/* Heuristic for the number of times to exponentially backoff and
37 * retry sending an LDC trigger when EAGAIN is encountered
38 */
39#define VNET_MAX_RETRIES 10
40
Sowmini Varadhand1015642014-09-11 09:57:22 -040041static int __vnet_tx_trigger(struct vnet_port *port, u32 start);
42
David S. Miller4c521e42007-07-09 22:23:51 -070043/* Ordered from largest major to lowest */
44static struct vio_version vnet_versions[] = {
David L Stevense4defc72014-09-29 19:47:59 -040045 { .major = 1, .minor = 6 },
David S. Miller4c521e42007-07-09 22:23:51 -070046 { .major = 1, .minor = 0 },
47};
48
49static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
50{
51 return vio_dring_avail(dr, VNET_TX_RING_SIZE);
52}
53
54static int vnet_handle_unknown(struct vnet_port *port, void *arg)
55{
56 struct vio_msg_tag *pkt = arg;
57
Joe Perches4d5870e2010-08-17 07:55:05 +000058 pr_err("Received unknown msg [%02x:%02x:%04x:%08x]\n",
David S. Miller4c521e42007-07-09 22:23:51 -070059 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
Joe Perches4d5870e2010-08-17 07:55:05 +000060 pr_err("Resetting connection\n");
David S. Miller4c521e42007-07-09 22:23:51 -070061
62 ldc_disconnect(port->vio.lp);
63
64 return -ECONNRESET;
65}
66
67static int vnet_send_attr(struct vio_driver_state *vio)
68{
69 struct vnet_port *port = to_vnet_port(vio);
70 struct net_device *dev = port->vp->dev;
71 struct vio_net_attr_info pkt;
David L Stevense4defc72014-09-29 19:47:59 -040072 int framelen = ETH_FRAME_LEN;
David S. Miller4c521e42007-07-09 22:23:51 -070073 int i;
74
75 memset(&pkt, 0, sizeof(pkt));
76 pkt.tag.type = VIO_TYPE_CTRL;
77 pkt.tag.stype = VIO_SUBTYPE_INFO;
78 pkt.tag.stype_env = VIO_ATTR_INFO;
79 pkt.tag.sid = vio_send_sid(vio);
David L Stevense4defc72014-09-29 19:47:59 -040080 if (vio_version_before(vio, 1, 2))
81 pkt.xfer_mode = VIO_DRING_MODE;
82 else
83 pkt.xfer_mode = VIO_NEW_DRING_MODE;
David S. Miller4c521e42007-07-09 22:23:51 -070084 pkt.addr_type = VNET_ADDR_ETHERMAC;
85 pkt.ack_freq = 0;
86 for (i = 0; i < 6; i++)
87 pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
David L Stevense4defc72014-09-29 19:47:59 -040088 if (vio_version_after(vio, 1, 3)) {
89 if (port->rmtu) {
90 port->rmtu = min(VNET_MAXPACKET, port->rmtu);
91 pkt.mtu = port->rmtu;
92 } else {
93 port->rmtu = VNET_MAXPACKET;
94 pkt.mtu = port->rmtu;
95 }
96 if (vio_version_after_eq(vio, 1, 6))
97 pkt.options = VIO_TX_DRING;
98 } else if (vio_version_before(vio, 1, 3)) {
99 pkt.mtu = framelen;
100 } else { /* v1.3 */
101 pkt.mtu = framelen + VLAN_HLEN;
102 }
103
104 pkt.plnk_updt = PHYSLINK_UPDATE_NONE;
105 pkt.cflags = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700106
107 viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
David L Stevense4defc72014-09-29 19:47:59 -0400108 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
109 "cflags[0x%04x] lso_max[%u]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700110 pkt.xfer_mode, pkt.addr_type,
David L Stevense4defc72014-09-29 19:47:59 -0400111 (unsigned long long)pkt.addr,
112 pkt.ack_freq, pkt.plnk_updt, pkt.options,
113 (unsigned long long)pkt.mtu, pkt.cflags, pkt.ipv4_lso_maxlen);
114
David S. Miller4c521e42007-07-09 22:23:51 -0700115
116 return vio_ldc_send(vio, &pkt, sizeof(pkt));
117}
118
119static int handle_attr_info(struct vio_driver_state *vio,
120 struct vio_net_attr_info *pkt)
121{
David L Stevense4defc72014-09-29 19:47:59 -0400122 struct vnet_port *port = to_vnet_port(vio);
123 u64 localmtu;
124 u8 xfer_mode;
125
126 viodbg(HS, "GOT NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
127 "ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] mtu[%llu] "
128 " (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700129 pkt->xfer_mode, pkt->addr_type,
David L Stevense4defc72014-09-29 19:47:59 -0400130 (unsigned long long)pkt->addr,
131 pkt->ack_freq, pkt->plnk_updt, pkt->options,
132 (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
133 pkt->ipv4_lso_maxlen);
David S. Miller4c521e42007-07-09 22:23:51 -0700134
135 pkt->tag.sid = vio_send_sid(vio);
136
David L Stevense4defc72014-09-29 19:47:59 -0400137 xfer_mode = pkt->xfer_mode;
138 /* for version < 1.2, VIO_DRING_MODE = 0x3 and no bitmask */
139 if (vio_version_before(vio, 1, 2) && xfer_mode == VIO_DRING_MODE)
140 xfer_mode = VIO_NEW_DRING_MODE;
141
142 /* MTU negotiation:
143 * < v1.3 - ETH_FRAME_LEN exactly
144 * > v1.3 - MIN(pkt.mtu, VNET_MAXPACKET, port->rmtu) and change
145 * pkt->mtu for ACK
146 * = v1.3 - ETH_FRAME_LEN + VLAN_HLEN exactly
147 */
148 if (vio_version_before(vio, 1, 3)) {
149 localmtu = ETH_FRAME_LEN;
150 } else if (vio_version_after(vio, 1, 3)) {
151 localmtu = port->rmtu ? port->rmtu : VNET_MAXPACKET;
152 localmtu = min(pkt->mtu, localmtu);
153 pkt->mtu = localmtu;
154 } else { /* v1.3 */
155 localmtu = ETH_FRAME_LEN + VLAN_HLEN;
156 }
157 port->rmtu = localmtu;
158
159 /* for version >= 1.6, ACK packet mode we support */
160 if (vio_version_after_eq(vio, 1, 6)) {
161 pkt->xfer_mode = VIO_NEW_DRING_MODE;
162 pkt->options = VIO_TX_DRING;
163 }
164
165 if (!(xfer_mode | VIO_NEW_DRING_MODE) ||
David S. Miller4c521e42007-07-09 22:23:51 -0700166 pkt->addr_type != VNET_ADDR_ETHERMAC ||
David L Stevense4defc72014-09-29 19:47:59 -0400167 pkt->mtu != localmtu) {
David S. Miller4c521e42007-07-09 22:23:51 -0700168 viodbg(HS, "SEND NET ATTR NACK\n");
169
170 pkt->tag.stype = VIO_SUBTYPE_NACK;
171
172 (void) vio_ldc_send(vio, pkt, sizeof(*pkt));
173
174 return -ECONNRESET;
175 } else {
David L Stevense4defc72014-09-29 19:47:59 -0400176 viodbg(HS, "SEND NET ATTR ACK xmode[0x%x] atype[0x%x] "
177 "addr[%llx] ackfreq[%u] plnk_updt[0x%02x] opts[0x%02x] "
178 "mtu[%llu] (rmtu[%llu]) cflags[0x%04x] lso_max[%u]\n",
179 pkt->xfer_mode, pkt->addr_type,
180 (unsigned long long)pkt->addr,
181 pkt->ack_freq, pkt->plnk_updt, pkt->options,
182 (unsigned long long)pkt->mtu, port->rmtu, pkt->cflags,
183 pkt->ipv4_lso_maxlen);
David S. Miller4c521e42007-07-09 22:23:51 -0700184
185 pkt->tag.stype = VIO_SUBTYPE_ACK;
186
187 return vio_ldc_send(vio, pkt, sizeof(*pkt));
188 }
189
190}
191
192static int handle_attr_ack(struct vio_driver_state *vio,
193 struct vio_net_attr_info *pkt)
194{
195 viodbg(HS, "GOT NET ATTR ACK\n");
196
197 return 0;
198}
199
200static int handle_attr_nack(struct vio_driver_state *vio,
201 struct vio_net_attr_info *pkt)
202{
203 viodbg(HS, "GOT NET ATTR NACK\n");
204
205 return -ECONNRESET;
206}
207
208static int vnet_handle_attr(struct vio_driver_state *vio, void *arg)
209{
210 struct vio_net_attr_info *pkt = arg;
211
212 switch (pkt->tag.stype) {
213 case VIO_SUBTYPE_INFO:
214 return handle_attr_info(vio, pkt);
215
216 case VIO_SUBTYPE_ACK:
217 return handle_attr_ack(vio, pkt);
218
219 case VIO_SUBTYPE_NACK:
220 return handle_attr_nack(vio, pkt);
221
222 default:
223 return -ECONNRESET;
224 }
225}
226
227static void vnet_handshake_complete(struct vio_driver_state *vio)
228{
229 struct vio_dring_state *dr;
230
231 dr = &vio->drings[VIO_DRIVER_RX_RING];
232 dr->snd_nxt = dr->rcv_nxt = 1;
233
234 dr = &vio->drings[VIO_DRIVER_TX_RING];
235 dr->snd_nxt = dr->rcv_nxt = 1;
236}
237
238/* The hypervisor interface that implements copying to/from imported
239 * memory from another domain requires that copies are done to 8-byte
240 * aligned buffers, and that the lengths of such copies are also 8-byte
241 * multiples.
242 *
243 * So we align skb->data to an 8-byte multiple and pad-out the data
244 * area so we can round the copy length up to the next multiple of
245 * 8 for the copy.
246 *
247 * The transmitter puts the actual start of the packet 6 bytes into
248 * the buffer it sends over, so that the IP headers after the ethernet
249 * header are aligned properly. These 6 bytes are not in the descriptor
250 * length, they are simply implied. This offset is represented using
251 * the VNET_PACKET_SKIP macro.
252 */
253static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
254 unsigned int len)
255{
256 struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8);
257 unsigned long addr, off;
258
259 if (unlikely(!skb))
260 return NULL;
261
262 addr = (unsigned long) skb->data;
263 off = ((addr + 7UL) & ~7UL) - addr;
264 if (off)
265 skb_reserve(skb, off);
266
267 return skb;
268}
269
270static int vnet_rx_one(struct vnet_port *port, unsigned int len,
271 struct ldc_trans_cookie *cookies, int ncookies)
272{
273 struct net_device *dev = port->vp->dev;
274 unsigned int copy_len;
275 struct sk_buff *skb;
276 int err;
277
278 err = -EMSGSIZE;
David L Stevense4defc72014-09-29 19:47:59 -0400279 if (unlikely(len < ETH_ZLEN || len > port->rmtu)) {
David S. Miller4c521e42007-07-09 22:23:51 -0700280 dev->stats.rx_length_errors++;
281 goto out_dropped;
282 }
283
284 skb = alloc_and_align_skb(dev, len);
285 err = -ENOMEM;
286 if (unlikely(!skb)) {
287 dev->stats.rx_missed_errors++;
288 goto out_dropped;
289 }
290
291 copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
292 skb_put(skb, copy_len);
293 err = ldc_copy(port->vio.lp, LDC_COPY_IN,
294 skb->data, copy_len, 0,
295 cookies, ncookies);
296 if (unlikely(err < 0)) {
297 dev->stats.rx_frame_errors++;
298 goto out_free_skb;
299 }
300
301 skb_pull(skb, VNET_PACKET_SKIP);
302 skb_trim(skb, len);
303 skb->protocol = eth_type_trans(skb, dev);
304
305 dev->stats.rx_packets++;
306 dev->stats.rx_bytes += len;
307
308 netif_rx(skb);
309
310 return 0;
311
312out_free_skb:
313 kfree_skb(skb);
314
315out_dropped:
316 dev->stats.rx_dropped++;
317 return err;
318}
319
320static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
321 u32 start, u32 end, u8 vio_dring_state)
322{
323 struct vio_dring_data hdr = {
324 .tag = {
325 .type = VIO_TYPE_DATA,
326 .stype = VIO_SUBTYPE_ACK,
327 .stype_env = VIO_DRING_DATA,
328 .sid = vio_send_sid(&port->vio),
329 },
330 .dring_ident = dr->ident,
331 .start_idx = start,
332 .end_idx = end,
333 .state = vio_dring_state,
334 };
335 int err, delay;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400336 int retries = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700337
338 hdr.seq = dr->snd_nxt;
339 delay = 1;
340 do {
341 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
342 if (err > 0) {
343 dr->snd_nxt++;
344 break;
345 }
346 udelay(delay);
347 if ((delay <<= 1) > 128)
348 delay = 128;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400349 if (retries++ > VNET_MAX_RETRIES) {
350 pr_info("ECONNRESET %x:%x:%x:%x:%x:%x\n",
351 port->raddr[0], port->raddr[1],
352 port->raddr[2], port->raddr[3],
353 port->raddr[4], port->raddr[5]);
Sowmini Varadhand1015642014-09-11 09:57:22 -0400354 break;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400355 }
David S. Miller4c521e42007-07-09 22:23:51 -0700356 } while (err == -EAGAIN);
357
Sowmini Varadhand1015642014-09-11 09:57:22 -0400358 if (err <= 0 && vio_dring_state == VIO_DRING_STOPPED) {
359 port->stop_rx_idx = end;
360 port->stop_rx = true;
361 } else {
362 port->stop_rx_idx = 0;
363 port->stop_rx = false;
364 }
365
David S. Miller4c521e42007-07-09 22:23:51 -0700366 return err;
367}
368
369static u32 next_idx(u32 idx, struct vio_dring_state *dr)
370{
371 if (++idx == dr->num_entries)
372 idx = 0;
373 return idx;
374}
375
376static u32 prev_idx(u32 idx, struct vio_dring_state *dr)
377{
378 if (idx == 0)
379 idx = dr->num_entries - 1;
380 else
381 idx--;
382
383 return idx;
384}
385
386static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
387 struct vio_dring_state *dr,
388 u32 index)
389{
390 struct vio_net_desc *desc = port->vio.desc_buf;
391 int err;
392
393 err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
394 (index * dr->entry_size),
395 dr->cookies, dr->ncookies);
396 if (err < 0)
397 return ERR_PTR(err);
398
399 return desc;
400}
401
402static int put_rx_desc(struct vnet_port *port,
403 struct vio_dring_state *dr,
404 struct vio_net_desc *desc,
405 u32 index)
406{
407 int err;
408
409 err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
410 (index * dr->entry_size),
411 dr->cookies, dr->ncookies);
412 if (err < 0)
413 return err;
414
415 return 0;
416}
417
418static int vnet_walk_rx_one(struct vnet_port *port,
419 struct vio_dring_state *dr,
420 u32 index, int *needs_ack)
421{
422 struct vio_net_desc *desc = get_rx_desc(port, dr, index);
423 struct vio_driver_state *vio = &port->vio;
424 int err;
425
426 if (IS_ERR(desc))
427 return PTR_ERR(desc);
428
David L Stevens78dcff72014-09-08 16:23:01 -0400429 if (desc->hdr.state != VIO_DESC_READY)
430 return 1;
431
432 rmb();
433
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800434 viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%llx:%llx]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700435 desc->hdr.state, desc->hdr.ack,
436 desc->size, desc->ncookies,
437 desc->cookies[0].cookie_addr,
438 desc->cookies[0].cookie_size);
439
David S. Miller4c521e42007-07-09 22:23:51 -0700440 err = vnet_rx_one(port, desc->size, desc->cookies, desc->ncookies);
441 if (err == -ECONNRESET)
442 return err;
443 desc->hdr.state = VIO_DESC_DONE;
444 err = put_rx_desc(port, dr, desc, index);
445 if (err < 0)
446 return err;
447 *needs_ack = desc->hdr.ack;
448 return 0;
449}
450
451static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
452 u32 start, u32 end)
453{
454 struct vio_driver_state *vio = &port->vio;
455 int ack_start = -1, ack_end = -1;
456
457 end = (end == (u32) -1) ? prev_idx(start, dr) : next_idx(end, dr);
458
459 viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
460
461 while (start != end) {
462 int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
463 if (err == -ECONNRESET)
464 return err;
465 if (err != 0)
466 break;
467 if (ack_start == -1)
468 ack_start = start;
469 ack_end = start;
470 start = next_idx(start, dr);
471 if (ack && start != end) {
472 err = vnet_send_ack(port, dr, ack_start, ack_end,
473 VIO_DRING_ACTIVE);
474 if (err == -ECONNRESET)
475 return err;
476 ack_start = -1;
477 }
478 }
479 if (unlikely(ack_start == -1))
480 ack_start = ack_end = prev_idx(start, dr);
481 return vnet_send_ack(port, dr, ack_start, ack_end, VIO_DRING_STOPPED);
482}
483
484static int vnet_rx(struct vnet_port *port, void *msgbuf)
485{
486 struct vio_dring_data *pkt = msgbuf;
487 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
488 struct vio_driver_state *vio = &port->vio;
489
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800490 viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016llx] rcv_nxt[%016llx]\n",
David S. Miller4c521e42007-07-09 22:23:51 -0700491 pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
492
493 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
494 return 0;
495 if (unlikely(pkt->seq != dr->rcv_nxt)) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000496 pr_err("RX out of sequence seq[0x%llx] rcv_nxt[0x%llx]\n",
497 pkt->seq, dr->rcv_nxt);
David S. Miller4c521e42007-07-09 22:23:51 -0700498 return 0;
499 }
500
501 dr->rcv_nxt++;
502
503 /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
504
505 return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx);
506}
507
508static int idx_is_pending(struct vio_dring_state *dr, u32 end)
509{
510 u32 idx = dr->cons;
511 int found = 0;
512
513 while (idx != dr->prod) {
514 if (idx == end) {
515 found = 1;
516 break;
517 }
518 idx = next_idx(idx, dr);
519 }
520 return found;
521}
522
523static int vnet_ack(struct vnet_port *port, void *msgbuf)
524{
525 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
526 struct vio_dring_data *pkt = msgbuf;
527 struct net_device *dev;
528 struct vnet *vp;
529 u32 end;
Sowmini Varadhand1015642014-09-11 09:57:22 -0400530 struct vio_net_desc *desc;
David S. Miller4c521e42007-07-09 22:23:51 -0700531 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
532 return 0;
533
534 end = pkt->end_idx;
535 if (unlikely(!idx_is_pending(dr, end)))
536 return 0;
537
Sowmini Varadhand1015642014-09-11 09:57:22 -0400538 /* sync for race conditions with vnet_start_xmit() and tell xmit it
539 * is time to send a trigger.
540 */
David S. Miller4c521e42007-07-09 22:23:51 -0700541 dr->cons = next_idx(end, dr);
Sowmini Varadhand1015642014-09-11 09:57:22 -0400542 desc = vio_dring_entry(dr, dr->cons);
543 if (desc->hdr.state == VIO_DESC_READY && port->start_cons) {
544 /* vnet_start_xmit() just populated this dring but missed
545 * sending the "start" LDC message to the consumer.
546 * Send a "start" trigger on its behalf.
547 */
548 if (__vnet_tx_trigger(port, dr->cons) > 0)
549 port->start_cons = false;
550 else
551 port->start_cons = true;
552 } else {
553 port->start_cons = true;
554 }
555
David S. Miller4c521e42007-07-09 22:23:51 -0700556
557 vp = port->vp;
558 dev = vp->dev;
559 if (unlikely(netif_queue_stopped(dev) &&
560 vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
561 return 1;
562
563 return 0;
564}
565
566static int vnet_nack(struct vnet_port *port, void *msgbuf)
567{
568 /* XXX just reset or similar XXX */
569 return 0;
570}
571
David S. Miller028ebff2007-07-20 02:30:25 -0700572static int handle_mcast(struct vnet_port *port, void *msgbuf)
573{
574 struct vio_net_mcast_info *pkt = msgbuf;
575
576 if (pkt->tag.stype != VIO_SUBTYPE_ACK)
Joe Perches4d5870e2010-08-17 07:55:05 +0000577 pr_err("%s: Got unexpected MCAST reply [%02x:%02x:%04x:%08x]\n",
David S. Miller028ebff2007-07-20 02:30:25 -0700578 port->vp->dev->name,
579 pkt->tag.type,
580 pkt->tag.stype,
581 pkt->tag.stype_env,
582 pkt->tag.sid);
583
584 return 0;
585}
586
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -0400587static void maybe_tx_wakeup(unsigned long param)
David S. Miller4c521e42007-07-09 22:23:51 -0700588{
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -0400589 struct vnet *vp = (struct vnet *)param;
David S. Miller4c521e42007-07-09 22:23:51 -0700590 struct net_device *dev = vp->dev;
591
592 netif_tx_lock(dev);
593 if (likely(netif_queue_stopped(dev))) {
594 struct vnet_port *port;
595 int wake = 1;
596
597 list_for_each_entry(port, &vp->port_list, list) {
598 struct vio_dring_state *dr;
599
600 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
601 if (vnet_tx_dring_avail(dr) <
602 VNET_TX_WAKEUP_THRESH(dr)) {
603 wake = 0;
604 break;
605 }
606 }
607 if (wake)
608 netif_wake_queue(dev);
609 }
610 netif_tx_unlock(dev);
611}
612
613static void vnet_event(void *arg, int event)
614{
615 struct vnet_port *port = arg;
616 struct vio_driver_state *vio = &port->vio;
617 unsigned long flags;
618 int tx_wakeup, err;
619
620 spin_lock_irqsave(&vio->lock, flags);
621
622 if (unlikely(event == LDC_EVENT_RESET ||
623 event == LDC_EVENT_UP)) {
624 vio_link_state_change(vio, event);
625 spin_unlock_irqrestore(&vio->lock, flags);
626
David L Stevense4defc72014-09-29 19:47:59 -0400627 if (event == LDC_EVENT_RESET) {
628 port->rmtu = 0;
David S. Millerd762acd2007-07-18 00:07:39 -0700629 vio_port_up(vio);
David L Stevense4defc72014-09-29 19:47:59 -0400630 }
David S. Miller4c521e42007-07-09 22:23:51 -0700631 return;
632 }
633
634 if (unlikely(event != LDC_EVENT_DATA_READY)) {
Joe Perchesfe3881c2014-09-09 20:27:44 -0700635 pr_warn("Unexpected LDC event %d\n", event);
David S. Miller4c521e42007-07-09 22:23:51 -0700636 spin_unlock_irqrestore(&vio->lock, flags);
637 return;
638 }
639
640 tx_wakeup = err = 0;
641 while (1) {
642 union {
643 struct vio_msg_tag tag;
644 u64 raw[8];
645 } msgbuf;
646
647 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
648 if (unlikely(err < 0)) {
649 if (err == -ECONNRESET)
650 vio_conn_reset(vio);
651 break;
652 }
653 if (err == 0)
654 break;
655 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
656 msgbuf.tag.type,
657 msgbuf.tag.stype,
658 msgbuf.tag.stype_env,
659 msgbuf.tag.sid);
660 err = vio_validate_sid(vio, &msgbuf.tag);
661 if (err < 0)
662 break;
663
664 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
665 if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
666 err = vnet_rx(port, &msgbuf);
667 } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
668 err = vnet_ack(port, &msgbuf);
669 if (err > 0)
670 tx_wakeup |= err;
671 } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
672 err = vnet_nack(port, &msgbuf);
673 }
674 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
David S. Miller028ebff2007-07-20 02:30:25 -0700675 if (msgbuf.tag.stype_env == VNET_MCAST_INFO)
676 err = handle_mcast(port, &msgbuf);
677 else
678 err = vio_control_pkt_engine(vio, &msgbuf);
David S. Miller4c521e42007-07-09 22:23:51 -0700679 if (err)
680 break;
681 } else {
682 err = vnet_handle_unknown(port, &msgbuf);
683 }
684 if (err == -ECONNRESET)
685 break;
686 }
687 spin_unlock(&vio->lock);
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -0400688 /* Kick off a tasklet to wake the queue. We cannot call
689 * maybe_tx_wakeup directly here because we could deadlock on
690 * netif_tx_lock() with dev_watchdog()
691 */
David S. Miller4c521e42007-07-09 22:23:51 -0700692 if (unlikely(tx_wakeup && err != -ECONNRESET))
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -0400693 tasklet_schedule(&port->vp->vnet_tx_wakeup);
694
David S. Miller4c521e42007-07-09 22:23:51 -0700695 local_irq_restore(flags);
696}
697
Sowmini Varadhand1015642014-09-11 09:57:22 -0400698static int __vnet_tx_trigger(struct vnet_port *port, u32 start)
David S. Miller4c521e42007-07-09 22:23:51 -0700699{
700 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
701 struct vio_dring_data hdr = {
702 .tag = {
703 .type = VIO_TYPE_DATA,
704 .stype = VIO_SUBTYPE_INFO,
705 .stype_env = VIO_DRING_DATA,
706 .sid = vio_send_sid(&port->vio),
707 },
708 .dring_ident = dr->ident,
Sowmini Varadhand1015642014-09-11 09:57:22 -0400709 .start_idx = start,
David S. Miller4c521e42007-07-09 22:23:51 -0700710 .end_idx = (u32) -1,
711 };
712 int err, delay;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400713 int retries = 0;
David S. Miller4c521e42007-07-09 22:23:51 -0700714
Sowmini Varadhand1015642014-09-11 09:57:22 -0400715 if (port->stop_rx) {
716 err = vnet_send_ack(port,
717 &port->vio.drings[VIO_DRIVER_RX_RING],
718 port->stop_rx_idx, -1,
719 VIO_DRING_STOPPED);
720 if (err <= 0)
721 return err;
722 }
723
David S. Miller4c521e42007-07-09 22:23:51 -0700724 hdr.seq = dr->snd_nxt;
725 delay = 1;
726 do {
727 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
728 if (err > 0) {
729 dr->snd_nxt++;
730 break;
731 }
732 udelay(delay);
733 if ((delay <<= 1) > 128)
734 delay = 128;
Sowmini Varadhanadddc322014-08-12 10:33:10 -0400735 if (retries++ > VNET_MAX_RETRIES)
736 break;
David S. Miller4c521e42007-07-09 22:23:51 -0700737 } while (err == -EAGAIN);
738
739 return err;
740}
741
David L Stevens8266f5f2014-07-25 10:30:11 -0400742static inline bool port_is_up(struct vnet_port *vnet)
743{
744 struct vio_driver_state *vio = &vnet->vio;
745
746 return !!(vio->hs_state & VIO_HS_COMPLETE);
747}
748
David S. Miller4c521e42007-07-09 22:23:51 -0700749struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
750{
751 unsigned int hash = vnet_hashfn(skb->data);
752 struct hlist_head *hp = &vp->port_hash[hash];
David S. Miller4c521e42007-07-09 22:23:51 -0700753 struct vnet_port *port;
754
Sasha Levinb67bfe02013-02-27 17:06:00 -0800755 hlist_for_each_entry(port, hp, hash) {
David L Stevens8266f5f2014-07-25 10:30:11 -0400756 if (!port_is_up(port))
757 continue;
Joe Perches2e42e472012-05-09 17:17:46 +0000758 if (ether_addr_equal(port->raddr, skb->data))
David S. Miller4c521e42007-07-09 22:23:51 -0700759 return port;
760 }
David L Stevens8266f5f2014-07-25 10:30:11 -0400761 list_for_each_entry(port, &vp->port_list, list) {
762 if (!port->switch_port)
763 continue;
764 if (!port_is_up(port))
765 continue;
766 return port;
767 }
768 return NULL;
David S. Miller4c521e42007-07-09 22:23:51 -0700769}
770
771struct vnet_port *tx_port_find(struct vnet *vp, struct sk_buff *skb)
772{
773 struct vnet_port *ret;
774 unsigned long flags;
775
776 spin_lock_irqsave(&vp->lock, flags);
777 ret = __tx_port_find(vp, skb);
778 spin_unlock_irqrestore(&vp->lock, flags);
779
780 return ret;
781}
782
783static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
784{
785 struct vnet *vp = netdev_priv(dev);
786 struct vnet_port *port = tx_port_find(vp, skb);
787 struct vio_dring_state *dr;
788 struct vio_net_desc *d;
789 unsigned long flags;
790 unsigned int len;
791 void *tx_buf;
792 int i, err;
793
794 if (unlikely(!port))
795 goto out_dropped;
796
797 spin_lock_irqsave(&port->vio.lock, flags);
798
799 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
800 if (unlikely(vnet_tx_dring_avail(dr) < 2)) {
801 if (!netif_queue_stopped(dev)) {
802 netif_stop_queue(dev);
803
804 /* This is a hard error, log it. */
Joe Perches4d5870e2010-08-17 07:55:05 +0000805 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
David S. Miller4c521e42007-07-09 22:23:51 -0700806 dev->stats.tx_errors++;
807 }
808 spin_unlock_irqrestore(&port->vio.lock, flags);
809 return NETDEV_TX_BUSY;
810 }
811
812 d = vio_dring_cur(dr);
813
814 tx_buf = port->tx_bufs[dr->prod].buf;
815 skb_copy_from_linear_data(skb, tx_buf + VNET_PACKET_SKIP, skb->len);
816
817 len = skb->len;
818 if (len < ETH_ZLEN) {
819 len = ETH_ZLEN;
820 memset(tx_buf+VNET_PACKET_SKIP+skb->len, 0, len - skb->len);
821 }
822
Sowmini Varadhan1f6394e2014-08-12 10:27:24 -0400823 /* We don't rely on the ACKs to free the skb in vnet_start_xmit(),
824 * thus it is safe to not set VIO_ACK_ENABLE for each transmission:
825 * the protocol itself does not require it as long as the peer
826 * sends a VIO_SUBTYPE_ACK for VIO_DRING_STOPPED.
827 *
828 * An ACK for every packet in the ring is expensive as the
829 * sending of LDC messages is slow and affects performance.
830 */
831 d->hdr.ack = VIO_ACK_DISABLE;
David S. Miller4c521e42007-07-09 22:23:51 -0700832 d->size = len;
833 d->ncookies = port->tx_bufs[dr->prod].ncookies;
834 for (i = 0; i < d->ncookies; i++)
835 d->cookies[i] = port->tx_bufs[dr->prod].cookies[i];
836
837 /* This has to be a non-SMP write barrier because we are writing
838 * to memory which is shared with the peer LDOM.
839 */
840 wmb();
841
842 d->hdr.state = VIO_DESC_READY;
843
Sowmini Varadhand1015642014-09-11 09:57:22 -0400844 /* Exactly one ldc "start" trigger (for dr->cons) needs to be sent
845 * to notify the consumer that some descriptors are READY.
846 * After that "start" trigger, no additional triggers are needed until
847 * a DRING_STOPPED is received from the consumer. The dr->cons field
848 * (set up by vnet_ack()) has the value of the next dring index
849 * that has not yet been ack-ed. We send a "start" trigger here
850 * if, and only if, start_cons is true (reset it afterward). Conversely,
851 * vnet_ack() should check if the dring corresponding to cons
852 * is marked READY, but start_cons was false.
853 * If so, vnet_ack() should send out the missed "start" trigger.
854 *
855 * Note that the wmb() above makes sure the cookies et al. are
856 * not globally visible before the VIO_DESC_READY, and that the
857 * stores are ordered correctly by the compiler. The consumer will
858 * not proceed until the VIO_DESC_READY is visible assuring that
859 * the consumer does not observe anything related to descriptors
860 * out of order. The HV trap from the LDC start trigger is the
861 * producer to consumer announcement that work is available to the
862 * consumer
863 */
864 if (!port->start_cons)
865 goto ldc_start_done; /* previous trigger suffices */
866
867 err = __vnet_tx_trigger(port, dr->cons);
David S. Miller4c521e42007-07-09 22:23:51 -0700868 if (unlikely(err < 0)) {
Joe Perches4d5870e2010-08-17 07:55:05 +0000869 netdev_info(dev, "TX trigger error %d\n", err);
David S. Miller4c521e42007-07-09 22:23:51 -0700870 d->hdr.state = VIO_DESC_FREE;
871 dev->stats.tx_carrier_errors++;
872 goto out_dropped_unlock;
873 }
874
Sowmini Varadhand1015642014-09-11 09:57:22 -0400875ldc_start_done:
876 port->start_cons = false;
877
David S. Miller4c521e42007-07-09 22:23:51 -0700878 dev->stats.tx_packets++;
879 dev->stats.tx_bytes += skb->len;
880
881 dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
882 if (unlikely(vnet_tx_dring_avail(dr) < 2)) {
883 netif_stop_queue(dev);
884 if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
885 netif_wake_queue(dev);
886 }
887
888 spin_unlock_irqrestore(&port->vio.lock, flags);
889
890 dev_kfree_skb(skb);
891
David S. Miller4c521e42007-07-09 22:23:51 -0700892 return NETDEV_TX_OK;
893
894out_dropped_unlock:
895 spin_unlock_irqrestore(&port->vio.lock, flags);
896
897out_dropped:
898 dev_kfree_skb(skb);
899 dev->stats.tx_dropped++;
900 return NETDEV_TX_OK;
901}
902
903static void vnet_tx_timeout(struct net_device *dev)
904{
905 /* XXX Implement me XXX */
906}
907
908static int vnet_open(struct net_device *dev)
909{
910 netif_carrier_on(dev);
911 netif_start_queue(dev);
912
913 return 0;
914}
915
916static int vnet_close(struct net_device *dev)
917{
918 netif_stop_queue(dev);
919 netif_carrier_off(dev);
920
921 return 0;
922}
923
David S. Miller028ebff2007-07-20 02:30:25 -0700924static struct vnet_mcast_entry *__vnet_mc_find(struct vnet *vp, u8 *addr)
925{
926 struct vnet_mcast_entry *m;
927
928 for (m = vp->mcast_list; m; m = m->next) {
dingtianhong00fa4ce2013-12-30 15:41:24 +0800929 if (ether_addr_equal(m->addr, addr))
David S. Miller028ebff2007-07-20 02:30:25 -0700930 return m;
931 }
932 return NULL;
933}
934
935static void __update_mc_list(struct vnet *vp, struct net_device *dev)
936{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000937 struct netdev_hw_addr *ha;
David S. Miller028ebff2007-07-20 02:30:25 -0700938
Jiri Pirko22bedad32010-04-01 21:22:57 +0000939 netdev_for_each_mc_addr(ha, dev) {
David S. Miller028ebff2007-07-20 02:30:25 -0700940 struct vnet_mcast_entry *m;
941
Jiri Pirko22bedad32010-04-01 21:22:57 +0000942 m = __vnet_mc_find(vp, ha->addr);
David S. Miller028ebff2007-07-20 02:30:25 -0700943 if (m) {
944 m->hit = 1;
945 continue;
946 }
947
948 if (!m) {
949 m = kzalloc(sizeof(*m), GFP_ATOMIC);
950 if (!m)
951 continue;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000952 memcpy(m->addr, ha->addr, ETH_ALEN);
David S. Miller028ebff2007-07-20 02:30:25 -0700953 m->hit = 1;
954
955 m->next = vp->mcast_list;
956 vp->mcast_list = m;
957 }
958 }
959}
960
961static void __send_mc_list(struct vnet *vp, struct vnet_port *port)
962{
963 struct vio_net_mcast_info info;
964 struct vnet_mcast_entry *m, **pp;
965 int n_addrs;
966
967 memset(&info, 0, sizeof(info));
968
969 info.tag.type = VIO_TYPE_CTRL;
970 info.tag.stype = VIO_SUBTYPE_INFO;
971 info.tag.stype_env = VNET_MCAST_INFO;
972 info.tag.sid = vio_send_sid(&port->vio);
973 info.set = 1;
974
975 n_addrs = 0;
976 for (m = vp->mcast_list; m; m = m->next) {
977 if (m->sent)
978 continue;
979 m->sent = 1;
980 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
981 m->addr, ETH_ALEN);
982 if (++n_addrs == VNET_NUM_MCAST) {
983 info.count = n_addrs;
984
985 (void) vio_ldc_send(&port->vio, &info,
986 sizeof(info));
987 n_addrs = 0;
988 }
989 }
990 if (n_addrs) {
991 info.count = n_addrs;
992 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
993 }
994
995 info.set = 0;
996
997 n_addrs = 0;
998 pp = &vp->mcast_list;
999 while ((m = *pp) != NULL) {
1000 if (m->hit) {
1001 m->hit = 0;
1002 pp = &m->next;
1003 continue;
1004 }
1005
1006 memcpy(&info.mcast_addr[n_addrs * ETH_ALEN],
1007 m->addr, ETH_ALEN);
1008 if (++n_addrs == VNET_NUM_MCAST) {
1009 info.count = n_addrs;
1010 (void) vio_ldc_send(&port->vio, &info,
1011 sizeof(info));
1012 n_addrs = 0;
1013 }
1014
1015 *pp = m->next;
1016 kfree(m);
1017 }
1018 if (n_addrs) {
1019 info.count = n_addrs;
1020 (void) vio_ldc_send(&port->vio, &info, sizeof(info));
1021 }
1022}
1023
David S. Miller4c521e42007-07-09 22:23:51 -07001024static void vnet_set_rx_mode(struct net_device *dev)
1025{
David S. Miller028ebff2007-07-20 02:30:25 -07001026 struct vnet *vp = netdev_priv(dev);
1027 struct vnet_port *port;
1028 unsigned long flags;
1029
1030 spin_lock_irqsave(&vp->lock, flags);
1031 if (!list_empty(&vp->port_list)) {
1032 port = list_entry(vp->port_list.next, struct vnet_port, list);
1033
1034 if (port->switch_port) {
1035 __update_mc_list(vp, dev);
1036 __send_mc_list(vp, port);
1037 }
1038 }
1039 spin_unlock_irqrestore(&vp->lock, flags);
David S. Miller4c521e42007-07-09 22:23:51 -07001040}
1041
1042static int vnet_change_mtu(struct net_device *dev, int new_mtu)
1043{
1044 if (new_mtu != ETH_DATA_LEN)
1045 return -EINVAL;
1046
1047 dev->mtu = new_mtu;
1048 return 0;
1049}
1050
1051static int vnet_set_mac_addr(struct net_device *dev, void *p)
1052{
1053 return -EINVAL;
1054}
1055
1056static void vnet_get_drvinfo(struct net_device *dev,
1057 struct ethtool_drvinfo *info)
1058{
Jiri Pirko7826d432013-01-06 00:44:26 +00001059 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1060 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
David S. Miller4c521e42007-07-09 22:23:51 -07001061}
1062
1063static u32 vnet_get_msglevel(struct net_device *dev)
1064{
1065 struct vnet *vp = netdev_priv(dev);
1066 return vp->msg_enable;
1067}
1068
1069static void vnet_set_msglevel(struct net_device *dev, u32 value)
1070{
1071 struct vnet *vp = netdev_priv(dev);
1072 vp->msg_enable = value;
1073}
1074
1075static const struct ethtool_ops vnet_ethtool_ops = {
1076 .get_drvinfo = vnet_get_drvinfo,
1077 .get_msglevel = vnet_get_msglevel,
1078 .set_msglevel = vnet_set_msglevel,
1079 .get_link = ethtool_op_get_link,
David S. Miller4c521e42007-07-09 22:23:51 -07001080};
1081
1082static void vnet_port_free_tx_bufs(struct vnet_port *port)
1083{
1084 struct vio_dring_state *dr;
1085 int i;
1086
1087 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1088 if (dr->base) {
1089 ldc_free_exp_dring(port->vio.lp, dr->base,
1090 (dr->entry_size * dr->num_entries),
1091 dr->cookies, dr->ncookies);
1092 dr->base = NULL;
1093 dr->entry_size = 0;
1094 dr->num_entries = 0;
1095 dr->pending = 0;
1096 dr->ncookies = 0;
1097 }
1098
1099 for (i = 0; i < VNET_TX_RING_SIZE; i++) {
1100 void *buf = port->tx_bufs[i].buf;
1101
1102 if (!buf)
1103 continue;
1104
1105 ldc_unmap(port->vio.lp,
1106 port->tx_bufs[i].cookies,
1107 port->tx_bufs[i].ncookies);
1108
1109 kfree(buf);
1110 port->tx_bufs[i].buf = NULL;
1111 }
1112}
1113
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001114static int vnet_port_alloc_tx_bufs(struct vnet_port *port)
David S. Miller4c521e42007-07-09 22:23:51 -07001115{
1116 struct vio_dring_state *dr;
1117 unsigned long len;
1118 int i, err, ncookies;
1119 void *dring;
1120
1121 for (i = 0; i < VNET_TX_RING_SIZE; i++) {
David L Stevense4defc72014-09-29 19:47:59 -04001122 void *buf = kzalloc(VNET_MAXPACKET + 8, GFP_KERNEL);
1123 int map_len = (VNET_MAXPACKET + 7) & ~7;
David S. Miller4c521e42007-07-09 22:23:51 -07001124
1125 err = -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001126 if (!buf)
David S. Miller4c521e42007-07-09 22:23:51 -07001127 goto err_out;
Joe Perchese404dec2012-01-29 12:56:23 +00001128
David S. Miller4c521e42007-07-09 22:23:51 -07001129 err = -EFAULT;
1130 if ((unsigned long)buf & (8UL - 1)) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001131 pr_err("TX buffer misaligned\n");
David S. Miller4c521e42007-07-09 22:23:51 -07001132 kfree(buf);
1133 goto err_out;
1134 }
1135
1136 err = ldc_map_single(port->vio.lp, buf, map_len,
1137 port->tx_bufs[i].cookies, 2,
1138 (LDC_MAP_SHADOW |
1139 LDC_MAP_DIRECT |
1140 LDC_MAP_RW));
1141 if (err < 0) {
1142 kfree(buf);
1143 goto err_out;
1144 }
1145 port->tx_bufs[i].buf = buf;
1146 port->tx_bufs[i].ncookies = err;
1147 }
1148
1149 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1150
1151 len = (VNET_TX_RING_SIZE *
1152 (sizeof(struct vio_net_desc) +
1153 (sizeof(struct ldc_trans_cookie) * 2)));
1154
1155 ncookies = VIO_MAX_RING_COOKIES;
1156 dring = ldc_alloc_exp_dring(port->vio.lp, len,
1157 dr->cookies, &ncookies,
1158 (LDC_MAP_SHADOW |
1159 LDC_MAP_DIRECT |
1160 LDC_MAP_RW));
1161 if (IS_ERR(dring)) {
1162 err = PTR_ERR(dring);
1163 goto err_out;
1164 }
1165
1166 dr->base = dring;
1167 dr->entry_size = (sizeof(struct vio_net_desc) +
1168 (sizeof(struct ldc_trans_cookie) * 2));
1169 dr->num_entries = VNET_TX_RING_SIZE;
1170 dr->prod = dr->cons = 0;
Sowmini Varadhand1015642014-09-11 09:57:22 -04001171 port->start_cons = true; /* need an initial trigger */
David S. Miller4c521e42007-07-09 22:23:51 -07001172 dr->pending = VNET_TX_RING_SIZE;
1173 dr->ncookies = ncookies;
1174
1175 return 0;
1176
1177err_out:
1178 vnet_port_free_tx_bufs(port);
1179
1180 return err;
1181}
1182
David S. Miller9184a042007-07-17 22:19:10 -07001183static LIST_HEAD(vnet_list);
1184static DEFINE_MUTEX(vnet_list_mutex);
1185
David S. Miller8fd17f22009-03-20 00:51:22 -07001186static const struct net_device_ops vnet_ops = {
1187 .ndo_open = vnet_open,
1188 .ndo_stop = vnet_close,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001189 .ndo_set_rx_mode = vnet_set_rx_mode,
David S. Miller8fd17f22009-03-20 00:51:22 -07001190 .ndo_set_mac_address = vnet_set_mac_addr,
Ben Hutchings240c1022009-07-09 17:54:35 +00001191 .ndo_validate_addr = eth_validate_addr,
David S. Miller8fd17f22009-03-20 00:51:22 -07001192 .ndo_tx_timeout = vnet_tx_timeout,
1193 .ndo_change_mtu = vnet_change_mtu,
1194 .ndo_start_xmit = vnet_start_xmit,
1195};
1196
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001197static struct vnet *vnet_new(const u64 *local_mac)
David S. Miller9184a042007-07-17 22:19:10 -07001198{
1199 struct net_device *dev;
1200 struct vnet *vp;
1201 int err, i;
1202
1203 dev = alloc_etherdev(sizeof(*vp));
Joe Perches41de8d42012-01-29 13:47:52 +00001204 if (!dev)
David S. Miller9184a042007-07-17 22:19:10 -07001205 return ERR_PTR(-ENOMEM);
David S. Miller9184a042007-07-17 22:19:10 -07001206
1207 for (i = 0; i < ETH_ALEN; i++)
1208 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
1209
David S. Miller9184a042007-07-17 22:19:10 -07001210 vp = netdev_priv(dev);
1211
1212 spin_lock_init(&vp->lock);
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -04001213 tasklet_init(&vp->vnet_tx_wakeup, maybe_tx_wakeup, (unsigned long)vp);
David S. Miller9184a042007-07-17 22:19:10 -07001214 vp->dev = dev;
1215
1216 INIT_LIST_HEAD(&vp->port_list);
1217 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
1218 INIT_HLIST_HEAD(&vp->port_hash[i]);
1219 INIT_LIST_HEAD(&vp->list);
1220 vp->local_mac = *local_mac;
1221
David S. Miller8fd17f22009-03-20 00:51:22 -07001222 dev->netdev_ops = &vnet_ops;
David S. Miller9184a042007-07-17 22:19:10 -07001223 dev->ethtool_ops = &vnet_ethtool_ops;
1224 dev->watchdog_timeo = VNET_TX_TIMEOUT;
David S. Miller9184a042007-07-17 22:19:10 -07001225
1226 err = register_netdev(dev);
1227 if (err) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001228 pr_err("Cannot register net device, aborting\n");
David S. Miller9184a042007-07-17 22:19:10 -07001229 goto err_out_free_dev;
1230 }
1231
Joe Perches4d5870e2010-08-17 07:55:05 +00001232 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
David S. Miller9184a042007-07-17 22:19:10 -07001233
1234 list_add(&vp->list, &vnet_list);
1235
1236 return vp;
1237
1238err_out_free_dev:
1239 free_netdev(dev);
1240
1241 return ERR_PTR(err);
1242}
1243
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001244static struct vnet *vnet_find_or_create(const u64 *local_mac)
David S. Miller9184a042007-07-17 22:19:10 -07001245{
1246 struct vnet *iter, *vp;
1247
1248 mutex_lock(&vnet_list_mutex);
1249 vp = NULL;
1250 list_for_each_entry(iter, &vnet_list, list) {
1251 if (iter->local_mac == *local_mac) {
1252 vp = iter;
1253 break;
1254 }
1255 }
1256 if (!vp)
1257 vp = vnet_new(local_mac);
1258 mutex_unlock(&vnet_list_mutex);
1259
1260 return vp;
1261}
1262
Sowmini Varadhana4b70a02014-07-16 10:02:26 -04001263static void vnet_cleanup(void)
1264{
1265 struct vnet *vp;
1266 struct net_device *dev;
1267
1268 mutex_lock(&vnet_list_mutex);
1269 while (!list_empty(&vnet_list)) {
1270 vp = list_first_entry(&vnet_list, struct vnet, list);
1271 list_del(&vp->list);
1272 dev = vp->dev;
Sowmini Varadhan1d311ad2014-08-13 10:29:41 -04001273 tasklet_kill(&vp->vnet_tx_wakeup);
Sowmini Varadhana4b70a02014-07-16 10:02:26 -04001274 /* vio_unregister_driver() should have cleaned up port_list */
1275 BUG_ON(!list_empty(&vp->port_list));
1276 unregister_netdev(dev);
1277 free_netdev(dev);
1278 }
1279 mutex_unlock(&vnet_list_mutex);
1280}
1281
David S. Miller9184a042007-07-17 22:19:10 -07001282static const char *local_mac_prop = "local-mac-address";
1283
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001284static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
David S. Miller9184a042007-07-17 22:19:10 -07001285 u64 port_node)
1286{
1287 const u64 *local_mac = NULL;
1288 u64 a;
1289
1290 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
1291 u64 target = mdesc_arc_target(hp, a);
1292 const char *name;
1293
1294 name = mdesc_get_property(hp, target, "name", NULL);
1295 if (!name || strcmp(name, "network"))
1296 continue;
1297
1298 local_mac = mdesc_get_property(hp, target,
1299 local_mac_prop, NULL);
1300 if (local_mac)
1301 break;
1302 }
1303 if (!local_mac)
1304 return ERR_PTR(-ENODEV);
1305
1306 return vnet_find_or_create(local_mac);
1307}
1308
David S. Miller4c521e42007-07-09 22:23:51 -07001309static struct ldc_channel_config vnet_ldc_cfg = {
1310 .event = vnet_event,
1311 .mtu = 64,
1312 .mode = LDC_MODE_UNRELIABLE,
1313};
1314
1315static struct vio_driver_ops vnet_vio_ops = {
1316 .send_attr = vnet_send_attr,
1317 .handle_attr = vnet_handle_attr,
1318 .handshake_complete = vnet_handshake_complete,
1319};
1320
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001321static void print_version(void)
David S. Miller9184a042007-07-17 22:19:10 -07001322{
Joe Perches4d5870e2010-08-17 07:55:05 +00001323 printk_once(KERN_INFO "%s", version);
David S. Miller9184a042007-07-17 22:19:10 -07001324}
1325
David S. Miller4c521e42007-07-09 22:23:51 -07001326const char *remote_macaddr_prop = "remote-mac-address";
1327
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001328static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller4c521e42007-07-09 22:23:51 -07001329{
David S. Miller43fdf272007-07-12 13:47:50 -07001330 struct mdesc_handle *hp;
David S. Miller4c521e42007-07-09 22:23:51 -07001331 struct vnet_port *port;
1332 unsigned long flags;
1333 struct vnet *vp;
1334 const u64 *rmac;
1335 int len, i, err, switch_port;
1336
David S. Miller9184a042007-07-17 22:19:10 -07001337 print_version();
David S. Miller4c521e42007-07-09 22:23:51 -07001338
David S. Miller43fdf272007-07-12 13:47:50 -07001339 hp = mdesc_grab();
1340
David S. Miller9184a042007-07-17 22:19:10 -07001341 vp = vnet_find_parent(hp, vdev->mp);
1342 if (IS_ERR(vp)) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001343 pr_err("Cannot find port parent vnet\n");
David S. Miller9184a042007-07-17 22:19:10 -07001344 err = PTR_ERR(vp);
1345 goto err_out_put_mdesc;
1346 }
1347
David S. Miller43fdf272007-07-12 13:47:50 -07001348 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
1349 err = -ENODEV;
David S. Miller4c521e42007-07-09 22:23:51 -07001350 if (!rmac) {
Joe Perches4d5870e2010-08-17 07:55:05 +00001351 pr_err("Port lacks %s property\n", remote_macaddr_prop);
David S. Miller43fdf272007-07-12 13:47:50 -07001352 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -07001353 }
1354
1355 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -07001356 err = -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001357 if (!port)
David S. Miller43fdf272007-07-12 13:47:50 -07001358 goto err_out_put_mdesc;
David S. Miller4c521e42007-07-09 22:23:51 -07001359
1360 for (i = 0; i < ETH_ALEN; i++)
1361 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
1362
1363 port->vp = vp;
1364
David S. Miller43fdf272007-07-12 13:47:50 -07001365 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
David S. Miller4c521e42007-07-09 22:23:51 -07001366 vnet_versions, ARRAY_SIZE(vnet_versions),
1367 &vnet_vio_ops, vp->dev->name);
1368 if (err)
1369 goto err_out_free_port;
1370
1371 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
1372 if (err)
1373 goto err_out_free_port;
1374
1375 err = vnet_port_alloc_tx_bufs(port);
1376 if (err)
1377 goto err_out_free_ldc;
1378
1379 INIT_HLIST_NODE(&port->hash);
1380 INIT_LIST_HEAD(&port->list);
1381
1382 switch_port = 0;
David S. Miller43fdf272007-07-12 13:47:50 -07001383 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL) != NULL)
David S. Miller4c521e42007-07-09 22:23:51 -07001384 switch_port = 1;
David S. Miller028ebff2007-07-20 02:30:25 -07001385 port->switch_port = switch_port;
David S. Miller4c521e42007-07-09 22:23:51 -07001386
1387 spin_lock_irqsave(&vp->lock, flags);
1388 if (switch_port)
1389 list_add(&port->list, &vp->port_list);
1390 else
1391 list_add_tail(&port->list, &vp->port_list);
1392 hlist_add_head(&port->hash, &vp->port_hash[vnet_hashfn(port->raddr)]);
1393 spin_unlock_irqrestore(&vp->lock, flags);
1394
1395 dev_set_drvdata(&vdev->dev, port);
1396
Joe Perches4d5870e2010-08-17 07:55:05 +00001397 pr_info("%s: PORT ( remote-mac %pM%s )\n",
1398 vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
David S. Miller4c521e42007-07-09 22:23:51 -07001399
1400 vio_port_up(&port->vio);
1401
David S. Miller43fdf272007-07-12 13:47:50 -07001402 mdesc_release(hp);
1403
David S. Miller4c521e42007-07-09 22:23:51 -07001404 return 0;
1405
1406err_out_free_ldc:
1407 vio_ldc_free(&port->vio);
1408
1409err_out_free_port:
1410 kfree(port);
1411
David S. Miller43fdf272007-07-12 13:47:50 -07001412err_out_put_mdesc:
1413 mdesc_release(hp);
David S. Miller4c521e42007-07-09 22:23:51 -07001414 return err;
1415}
1416
1417static int vnet_port_remove(struct vio_dev *vdev)
1418{
1419 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
1420
1421 if (port) {
1422 struct vnet *vp = port->vp;
1423 unsigned long flags;
1424
1425 del_timer_sync(&port->vio.timer);
1426
1427 spin_lock_irqsave(&vp->lock, flags);
1428 list_del(&port->list);
1429 hlist_del(&port->hash);
1430 spin_unlock_irqrestore(&vp->lock, flags);
1431
1432 vnet_port_free_tx_bufs(port);
1433 vio_ldc_free(&port->vio);
1434
1435 dev_set_drvdata(&vdev->dev, NULL);
1436
1437 kfree(port);
Dave Kleikampaabb9872013-07-01 16:49:22 -05001438
David S. Miller4c521e42007-07-09 22:23:51 -07001439 }
1440 return 0;
1441}
1442
David S. Miller3d452e52008-09-01 01:48:52 -07001443static const struct vio_device_id vnet_port_match[] = {
David S. Miller4c521e42007-07-09 22:23:51 -07001444 {
1445 .type = "vnet-port",
1446 },
1447 {},
1448};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -07001449MODULE_DEVICE_TABLE(vio, vnet_port_match);
David S. Miller4c521e42007-07-09 22:23:51 -07001450
1451static struct vio_driver vnet_port_driver = {
1452 .id_table = vnet_port_match,
1453 .probe = vnet_port_probe,
1454 .remove = vnet_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001455 .name = "vnet_port",
David S. Miller4c521e42007-07-09 22:23:51 -07001456};
1457
David S. Miller4c521e42007-07-09 22:23:51 -07001458static int __init vnet_init(void)
1459{
David S. Miller9184a042007-07-17 22:19:10 -07001460 return vio_register_driver(&vnet_port_driver);
David S. Miller4c521e42007-07-09 22:23:51 -07001461}
1462
1463static void __exit vnet_exit(void)
1464{
1465 vio_unregister_driver(&vnet_port_driver);
Sowmini Varadhana4b70a02014-07-16 10:02:26 -04001466 vnet_cleanup();
David S. Miller4c521e42007-07-09 22:23:51 -07001467}
1468
1469module_init(vnet_init);
1470module_exit(vnet_exit);