aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/net/Kconfig6
-rw-r--r--drivers/net/Makefile1
-rw-r--r--drivers/net/sunvnet.c1156
-rw-r--r--drivers/net/sunvnet.h70
4 files changed, 1233 insertions, 0 deletions
diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index d17d64eb7065..7903f9c7839e 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -604,6 +604,12 @@ config CASSINI
604 Support for the Sun Cassini chip, aka Sun GigaSwift Ethernet. See also 604 Support for the Sun Cassini chip, aka Sun GigaSwift Ethernet. See also
605 <http://www.sun.com/products-n-solutions/hardware/docs/pdf/817-4341-10.pdf> 605 <http://www.sun.com/products-n-solutions/hardware/docs/pdf/817-4341-10.pdf>
606 606
607config SUNVNET
608 tristate "Sun Virtual Network support"
609 depends on SUN_LDOMS
610 help
611 Support for virtual network devices under Sun Logical Domains.
612
607config NET_VENDOR_3COM 613config NET_VENDOR_3COM
608 bool "3COM cards" 614 bool "3COM cards"
609 depends on ISA || EISA || MCA || PCI 615 depends on ISA || EISA || MCA || PCI
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index c26b8674213c..b95b1b237a26 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -34,6 +34,7 @@ obj-$(CONFIG_SUNBMAC) += sunbmac.o
34obj-$(CONFIG_MYRI_SBUS) += myri_sbus.o 34obj-$(CONFIG_MYRI_SBUS) += myri_sbus.o
35obj-$(CONFIG_SUNGEM) += sungem.o sungem_phy.o 35obj-$(CONFIG_SUNGEM) += sungem.o sungem_phy.o
36obj-$(CONFIG_CASSINI) += cassini.o 36obj-$(CONFIG_CASSINI) += cassini.o
37obj-$(CONFIG_SUNVNET) += sunvnet.o
37 38
38obj-$(CONFIG_MACE) += mace.o 39obj-$(CONFIG_MACE) += mace.o
39obj-$(CONFIG_BMAC) += bmac.o 40obj-$(CONFIG_BMAC) += bmac.o
diff --git a/drivers/net/sunvnet.c b/drivers/net/sunvnet.c
new file mode 100644
index 000000000000..d764e4ccba56
--- /dev/null
+++ b/drivers/net/sunvnet.c
@@ -0,0 +1,1156 @@
1/* sunvnet.c: Sun LDOM Virtual Network Driver.
2 *
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/slab.h>
10#include <linux/delay.h>
11#include <linux/init.h>
12#include <linux/netdevice.h>
13#include <linux/ethtool.h>
14#include <linux/etherdevice.h>
15
16#include <asm/vio.h>
17#include <asm/ldc.h>
18
19#include "sunvnet.h"
20
21#define DRV_MODULE_NAME "sunvnet"
22#define PFX DRV_MODULE_NAME ": "
23#define DRV_MODULE_VERSION "1.0"
24#define DRV_MODULE_RELDATE "June 25, 2007"
25
26static char version[] __devinitdata =
27 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
28MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
29MODULE_DESCRIPTION("Sun LDOM virtual network driver");
30MODULE_LICENSE("GPL");
31MODULE_VERSION(DRV_MODULE_VERSION);
32
33/* Ordered from largest major to lowest */
34static struct vio_version vnet_versions[] = {
35 { .major = 1, .minor = 0 },
36};
37
38static inline u32 vnet_tx_dring_avail(struct vio_dring_state *dr)
39{
40 return vio_dring_avail(dr, VNET_TX_RING_SIZE);
41}
42
43static int vnet_handle_unknown(struct vnet_port *port, void *arg)
44{
45 struct vio_msg_tag *pkt = arg;
46
47 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
48 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
49 printk(KERN_ERR PFX "Resetting connection.\n");
50
51 ldc_disconnect(port->vio.lp);
52
53 return -ECONNRESET;
54}
55
56static int vnet_send_attr(struct vio_driver_state *vio)
57{
58 struct vnet_port *port = to_vnet_port(vio);
59 struct net_device *dev = port->vp->dev;
60 struct vio_net_attr_info pkt;
61 int i;
62
63 memset(&pkt, 0, sizeof(pkt));
64 pkt.tag.type = VIO_TYPE_CTRL;
65 pkt.tag.stype = VIO_SUBTYPE_INFO;
66 pkt.tag.stype_env = VIO_ATTR_INFO;
67 pkt.tag.sid = vio_send_sid(vio);
68 pkt.xfer_mode = VIO_DRING_MODE;
69 pkt.addr_type = VNET_ADDR_ETHERMAC;
70 pkt.ack_freq = 0;
71 for (i = 0; i < 6; i++)
72 pkt.addr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
73 pkt.mtu = ETH_FRAME_LEN;
74
75 viodbg(HS, "SEND NET ATTR xmode[0x%x] atype[0x%x] addr[%llx] "
76 "ackfreq[%u] mtu[%llu]\n",
77 pkt.xfer_mode, pkt.addr_type,
78 (unsigned long long) pkt.addr,
79 pkt.ack_freq,
80 (unsigned long long) pkt.mtu);
81
82 return vio_ldc_send(vio, &pkt, sizeof(pkt));
83}
84
85static int handle_attr_info(struct vio_driver_state *vio,
86 struct vio_net_attr_info *pkt)
87{
88 viodbg(HS, "GOT NET ATTR INFO xmode[0x%x] atype[0x%x] addr[%llx] "
89 "ackfreq[%u] mtu[%llu]\n",
90 pkt->xfer_mode, pkt->addr_type,
91 (unsigned long long) pkt->addr,
92 pkt->ack_freq,
93 (unsigned long long) pkt->mtu);
94
95 pkt->tag.sid = vio_send_sid(vio);
96
97 if (pkt->xfer_mode != VIO_DRING_MODE ||
98 pkt->addr_type != VNET_ADDR_ETHERMAC ||
99 pkt->mtu != ETH_FRAME_LEN) {
100 viodbg(HS, "SEND NET ATTR NACK\n");
101
102 pkt->tag.stype = VIO_SUBTYPE_NACK;
103
104 (void) vio_ldc_send(vio, pkt, sizeof(*pkt));
105
106 return -ECONNRESET;
107 } else {
108 viodbg(HS, "SEND NET ATTR ACK\n");
109
110 pkt->tag.stype = VIO_SUBTYPE_ACK;
111
112 return vio_ldc_send(vio, pkt, sizeof(*pkt));
113 }
114
115}
116
117static int handle_attr_ack(struct vio_driver_state *vio,
118 struct vio_net_attr_info *pkt)
119{
120 viodbg(HS, "GOT NET ATTR ACK\n");
121
122 return 0;
123}
124
125static int handle_attr_nack(struct vio_driver_state *vio,
126 struct vio_net_attr_info *pkt)
127{
128 viodbg(HS, "GOT NET ATTR NACK\n");
129
130 return -ECONNRESET;
131}
132
133static int vnet_handle_attr(struct vio_driver_state *vio, void *arg)
134{
135 struct vio_net_attr_info *pkt = arg;
136
137 switch (pkt->tag.stype) {
138 case VIO_SUBTYPE_INFO:
139 return handle_attr_info(vio, pkt);
140
141 case VIO_SUBTYPE_ACK:
142 return handle_attr_ack(vio, pkt);
143
144 case VIO_SUBTYPE_NACK:
145 return handle_attr_nack(vio, pkt);
146
147 default:
148 return -ECONNRESET;
149 }
150}
151
152static void vnet_handshake_complete(struct vio_driver_state *vio)
153{
154 struct vio_dring_state *dr;
155
156 dr = &vio->drings[VIO_DRIVER_RX_RING];
157 dr->snd_nxt = dr->rcv_nxt = 1;
158
159 dr = &vio->drings[VIO_DRIVER_TX_RING];
160 dr->snd_nxt = dr->rcv_nxt = 1;
161}
162
163/* The hypervisor interface that implements copying to/from imported
164 * memory from another domain requires that copies are done to 8-byte
165 * aligned buffers, and that the lengths of such copies are also 8-byte
166 * multiples.
167 *
168 * So we align skb->data to an 8-byte multiple and pad-out the data
169 * area so we can round the copy length up to the next multiple of
170 * 8 for the copy.
171 *
172 * The transmitter puts the actual start of the packet 6 bytes into
173 * the buffer it sends over, so that the IP headers after the ethernet
174 * header are aligned properly. These 6 bytes are not in the descriptor
175 * length, they are simply implied. This offset is represented using
176 * the VNET_PACKET_SKIP macro.
177 */
178static struct sk_buff *alloc_and_align_skb(struct net_device *dev,
179 unsigned int len)
180{
181 struct sk_buff *skb = netdev_alloc_skb(dev, len+VNET_PACKET_SKIP+8+8);
182 unsigned long addr, off;
183
184 if (unlikely(!skb))
185 return NULL;
186
187 addr = (unsigned long) skb->data;
188 off = ((addr + 7UL) & ~7UL) - addr;
189 if (off)
190 skb_reserve(skb, off);
191
192 return skb;
193}
194
195static int vnet_rx_one(struct vnet_port *port, unsigned int len,
196 struct ldc_trans_cookie *cookies, int ncookies)
197{
198 struct net_device *dev = port->vp->dev;
199 unsigned int copy_len;
200 struct sk_buff *skb;
201 int err;
202
203 err = -EMSGSIZE;
204 if (unlikely(len < ETH_ZLEN || len > ETH_FRAME_LEN)) {
205 dev->stats.rx_length_errors++;
206 goto out_dropped;
207 }
208
209 skb = alloc_and_align_skb(dev, len);
210 err = -ENOMEM;
211 if (unlikely(!skb)) {
212 dev->stats.rx_missed_errors++;
213 goto out_dropped;
214 }
215
216 copy_len = (len + VNET_PACKET_SKIP + 7U) & ~7U;
217 skb_put(skb, copy_len);
218 err = ldc_copy(port->vio.lp, LDC_COPY_IN,
219 skb->data, copy_len, 0,
220 cookies, ncookies);
221 if (unlikely(err < 0)) {
222 dev->stats.rx_frame_errors++;
223 goto out_free_skb;
224 }
225
226 skb_pull(skb, VNET_PACKET_SKIP);
227 skb_trim(skb, len);
228 skb->protocol = eth_type_trans(skb, dev);
229
230 dev->stats.rx_packets++;
231 dev->stats.rx_bytes += len;
232
233 netif_rx(skb);
234
235 return 0;
236
237out_free_skb:
238 kfree_skb(skb);
239
240out_dropped:
241 dev->stats.rx_dropped++;
242 return err;
243}
244
245static int vnet_send_ack(struct vnet_port *port, struct vio_dring_state *dr,
246 u32 start, u32 end, u8 vio_dring_state)
247{
248 struct vio_dring_data hdr = {
249 .tag = {
250 .type = VIO_TYPE_DATA,
251 .stype = VIO_SUBTYPE_ACK,
252 .stype_env = VIO_DRING_DATA,
253 .sid = vio_send_sid(&port->vio),
254 },
255 .dring_ident = dr->ident,
256 .start_idx = start,
257 .end_idx = end,
258 .state = vio_dring_state,
259 };
260 int err, delay;
261
262 hdr.seq = dr->snd_nxt;
263 delay = 1;
264 do {
265 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
266 if (err > 0) {
267 dr->snd_nxt++;
268 break;
269 }
270 udelay(delay);
271 if ((delay <<= 1) > 128)
272 delay = 128;
273 } while (err == -EAGAIN);
274
275 return err;
276}
277
278static u32 next_idx(u32 idx, struct vio_dring_state *dr)
279{
280 if (++idx == dr->num_entries)
281 idx = 0;
282 return idx;
283}
284
285static u32 prev_idx(u32 idx, struct vio_dring_state *dr)
286{
287 if (idx == 0)
288 idx = dr->num_entries - 1;
289 else
290 idx--;
291
292 return idx;
293}
294
295static struct vio_net_desc *get_rx_desc(struct vnet_port *port,
296 struct vio_dring_state *dr,
297 u32 index)
298{
299 struct vio_net_desc *desc = port->vio.desc_buf;
300 int err;
301
302 err = ldc_get_dring_entry(port->vio.lp, desc, dr->entry_size,
303 (index * dr->entry_size),
304 dr->cookies, dr->ncookies);
305 if (err < 0)
306 return ERR_PTR(err);
307
308 return desc;
309}
310
311static int put_rx_desc(struct vnet_port *port,
312 struct vio_dring_state *dr,
313 struct vio_net_desc *desc,
314 u32 index)
315{
316 int err;
317
318 err = ldc_put_dring_entry(port->vio.lp, desc, dr->entry_size,
319 (index * dr->entry_size),
320 dr->cookies, dr->ncookies);
321 if (err < 0)
322 return err;
323
324 return 0;
325}
326
327static int vnet_walk_rx_one(struct vnet_port *port,
328 struct vio_dring_state *dr,
329 u32 index, int *needs_ack)
330{
331 struct vio_net_desc *desc = get_rx_desc(port, dr, index);
332 struct vio_driver_state *vio = &port->vio;
333 int err;
334
335 if (IS_ERR(desc))
336 return PTR_ERR(desc);
337
338 viodbg(DATA, "vio_walk_rx_one desc[%02x:%02x:%08x:%08x:%lx:%lx]\n",
339 desc->hdr.state, desc->hdr.ack,
340 desc->size, desc->ncookies,
341 desc->cookies[0].cookie_addr,
342 desc->cookies[0].cookie_size);
343
344 if (desc->hdr.state != VIO_DESC_READY)
345 return 1;
346 err = vnet_rx_one(port, desc->size, desc->cookies, desc->ncookies);
347 if (err == -ECONNRESET)
348 return err;
349 desc->hdr.state = VIO_DESC_DONE;
350 err = put_rx_desc(port, dr, desc, index);
351 if (err < 0)
352 return err;
353 *needs_ack = desc->hdr.ack;
354 return 0;
355}
356
357static int vnet_walk_rx(struct vnet_port *port, struct vio_dring_state *dr,
358 u32 start, u32 end)
359{
360 struct vio_driver_state *vio = &port->vio;
361 int ack_start = -1, ack_end = -1;
362
363 end = (end == (u32) -1) ? prev_idx(start, dr) : next_idx(end, dr);
364
365 viodbg(DATA, "vnet_walk_rx start[%08x] end[%08x]\n", start, end);
366
367 while (start != end) {
368 int ack = 0, err = vnet_walk_rx_one(port, dr, start, &ack);
369 if (err == -ECONNRESET)
370 return err;
371 if (err != 0)
372 break;
373 if (ack_start == -1)
374 ack_start = start;
375 ack_end = start;
376 start = next_idx(start, dr);
377 if (ack && start != end) {
378 err = vnet_send_ack(port, dr, ack_start, ack_end,
379 VIO_DRING_ACTIVE);
380 if (err == -ECONNRESET)
381 return err;
382 ack_start = -1;
383 }
384 }
385 if (unlikely(ack_start == -1))
386 ack_start = ack_end = prev_idx(start, dr);
387 return vnet_send_ack(port, dr, ack_start, ack_end, VIO_DRING_STOPPED);
388}
389
390static int vnet_rx(struct vnet_port *port, void *msgbuf)
391{
392 struct vio_dring_data *pkt = msgbuf;
393 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_RX_RING];
394 struct vio_driver_state *vio = &port->vio;
395
396 viodbg(DATA, "vnet_rx stype_env[%04x] seq[%016lx] rcv_nxt[%016lx]\n",
397 pkt->tag.stype_env, pkt->seq, dr->rcv_nxt);
398
399 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
400 return 0;
401 if (unlikely(pkt->seq != dr->rcv_nxt)) {
402 printk(KERN_ERR PFX "RX out of sequence seq[0x%lx] "
403 "rcv_nxt[0x%lx]\n", pkt->seq, dr->rcv_nxt);
404 return 0;
405 }
406
407 dr->rcv_nxt++;
408
409 /* XXX Validate pkt->start_idx and pkt->end_idx XXX */
410
411 return vnet_walk_rx(port, dr, pkt->start_idx, pkt->end_idx);
412}
413
414static int idx_is_pending(struct vio_dring_state *dr, u32 end)
415{
416 u32 idx = dr->cons;
417 int found = 0;
418
419 while (idx != dr->prod) {
420 if (idx == end) {
421 found = 1;
422 break;
423 }
424 idx = next_idx(idx, dr);
425 }
426 return found;
427}
428
429static int vnet_ack(struct vnet_port *port, void *msgbuf)
430{
431 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
432 struct vio_dring_data *pkt = msgbuf;
433 struct net_device *dev;
434 struct vnet *vp;
435 u32 end;
436
437 if (unlikely(pkt->tag.stype_env != VIO_DRING_DATA))
438 return 0;
439
440 end = pkt->end_idx;
441 if (unlikely(!idx_is_pending(dr, end)))
442 return 0;
443
444 dr->cons = next_idx(end, dr);
445
446 vp = port->vp;
447 dev = vp->dev;
448 if (unlikely(netif_queue_stopped(dev) &&
449 vnet_tx_dring_avail(dr) >= VNET_TX_WAKEUP_THRESH(dr)))
450 return 1;
451
452 return 0;
453}
454
455static int vnet_nack(struct vnet_port *port, void *msgbuf)
456{
457 /* XXX just reset or similar XXX */
458 return 0;
459}
460
461static void maybe_tx_wakeup(struct vnet *vp)
462{
463 struct net_device *dev = vp->dev;
464
465 netif_tx_lock(dev);
466 if (likely(netif_queue_stopped(dev))) {
467 struct vnet_port *port;
468 int wake = 1;
469
470 list_for_each_entry(port, &vp->port_list, list) {
471 struct vio_dring_state *dr;
472
473 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
474 if (vnet_tx_dring_avail(dr) <
475 VNET_TX_WAKEUP_THRESH(dr)) {
476 wake = 0;
477 break;
478 }
479 }
480 if (wake)
481 netif_wake_queue(dev);
482 }
483 netif_tx_unlock(dev);
484}
485
486static void vnet_event(void *arg, int event)
487{
488 struct vnet_port *port = arg;
489 struct vio_driver_state *vio = &port->vio;
490 unsigned long flags;
491 int tx_wakeup, err;
492
493 spin_lock_irqsave(&vio->lock, flags);
494
495 if (unlikely(event == LDC_EVENT_RESET ||
496 event == LDC_EVENT_UP)) {
497 vio_link_state_change(vio, event);
498 spin_unlock_irqrestore(&vio->lock, flags);
499
500 return;
501 }
502
503 if (unlikely(event != LDC_EVENT_DATA_READY)) {
504 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
505 spin_unlock_irqrestore(&vio->lock, flags);
506 return;
507 }
508
509 tx_wakeup = err = 0;
510 while (1) {
511 union {
512 struct vio_msg_tag tag;
513 u64 raw[8];
514 } msgbuf;
515
516 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
517 if (unlikely(err < 0)) {
518 if (err == -ECONNRESET)
519 vio_conn_reset(vio);
520 break;
521 }
522 if (err == 0)
523 break;
524 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
525 msgbuf.tag.type,
526 msgbuf.tag.stype,
527 msgbuf.tag.stype_env,
528 msgbuf.tag.sid);
529 err = vio_validate_sid(vio, &msgbuf.tag);
530 if (err < 0)
531 break;
532
533 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
534 if (msgbuf.tag.stype == VIO_SUBTYPE_INFO) {
535 err = vnet_rx(port, &msgbuf);
536 } else if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) {
537 err = vnet_ack(port, &msgbuf);
538 if (err > 0)
539 tx_wakeup |= err;
540 } else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) {
541 err = vnet_nack(port, &msgbuf);
542 }
543 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
544 err = vio_control_pkt_engine(vio, &msgbuf);
545 if (err)
546 break;
547 } else {
548 err = vnet_handle_unknown(port, &msgbuf);
549 }
550 if (err == -ECONNRESET)
551 break;
552 }
553 spin_unlock(&vio->lock);
554 if (unlikely(tx_wakeup && err != -ECONNRESET))
555 maybe_tx_wakeup(port->vp);
556 local_irq_restore(flags);
557}
558
559static int __vnet_tx_trigger(struct vnet_port *port)
560{
561 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
562 struct vio_dring_data hdr = {
563 .tag = {
564 .type = VIO_TYPE_DATA,
565 .stype = VIO_SUBTYPE_INFO,
566 .stype_env = VIO_DRING_DATA,
567 .sid = vio_send_sid(&port->vio),
568 },
569 .dring_ident = dr->ident,
570 .start_idx = dr->prod,
571 .end_idx = (u32) -1,
572 };
573 int err, delay;
574
575 hdr.seq = dr->snd_nxt;
576 delay = 1;
577 do {
578 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
579 if (err > 0) {
580 dr->snd_nxt++;
581 break;
582 }
583 udelay(delay);
584 if ((delay <<= 1) > 128)
585 delay = 128;
586 } while (err == -EAGAIN);
587
588 return err;
589}
590
591struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
592{
593 unsigned int hash = vnet_hashfn(skb->data);
594 struct hlist_head *hp = &vp->port_hash[hash];
595 struct hlist_node *n;
596 struct vnet_port *port;
597
598 hlist_for_each_entry(port, n, hp, hash) {
599 if (!compare_ether_addr(port->raddr, skb->data))
600 return port;
601 }
602 port = NULL;
603 if (!list_empty(&vp->port_list))
604 port = list_entry(vp->port_list.next, struct vnet_port, list);
605
606 return port;
607}
608
609struct vnet_port *tx_port_find(struct vnet *vp, struct sk_buff *skb)
610{
611 struct vnet_port *ret;
612 unsigned long flags;
613
614 spin_lock_irqsave(&vp->lock, flags);
615 ret = __tx_port_find(vp, skb);
616 spin_unlock_irqrestore(&vp->lock, flags);
617
618 return ret;
619}
620
621static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
622{
623 struct vnet *vp = netdev_priv(dev);
624 struct vnet_port *port = tx_port_find(vp, skb);
625 struct vio_dring_state *dr;
626 struct vio_net_desc *d;
627 unsigned long flags;
628 unsigned int len;
629 void *tx_buf;
630 int i, err;
631
632 if (unlikely(!port))
633 goto out_dropped;
634
635 spin_lock_irqsave(&port->vio.lock, flags);
636
637 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
638 if (unlikely(vnet_tx_dring_avail(dr) < 2)) {
639 if (!netif_queue_stopped(dev)) {
640 netif_stop_queue(dev);
641
642 /* This is a hard error, log it. */
643 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
644 "queue awake!\n", dev->name);
645 dev->stats.tx_errors++;
646 }
647 spin_unlock_irqrestore(&port->vio.lock, flags);
648 return NETDEV_TX_BUSY;
649 }
650
651 d = vio_dring_cur(dr);
652
653 tx_buf = port->tx_bufs[dr->prod].buf;
654 skb_copy_from_linear_data(skb, tx_buf + VNET_PACKET_SKIP, skb->len);
655
656 len = skb->len;
657 if (len < ETH_ZLEN) {
658 len = ETH_ZLEN;
659 memset(tx_buf+VNET_PACKET_SKIP+skb->len, 0, len - skb->len);
660 }
661
662 d->hdr.ack = VIO_ACK_ENABLE;
663 d->size = len;
664 d->ncookies = port->tx_bufs[dr->prod].ncookies;
665 for (i = 0; i < d->ncookies; i++)
666 d->cookies[i] = port->tx_bufs[dr->prod].cookies[i];
667
668 /* This has to be a non-SMP write barrier because we are writing
669 * to memory which is shared with the peer LDOM.
670 */
671 wmb();
672
673 d->hdr.state = VIO_DESC_READY;
674
675 err = __vnet_tx_trigger(port);
676 if (unlikely(err < 0)) {
677 printk(KERN_INFO PFX "%s: TX trigger error %d\n",
678 dev->name, err);
679 d->hdr.state = VIO_DESC_FREE;
680 dev->stats.tx_carrier_errors++;
681 goto out_dropped_unlock;
682 }
683
684 dev->stats.tx_packets++;
685 dev->stats.tx_bytes += skb->len;
686
687 dr->prod = (dr->prod + 1) & (VNET_TX_RING_SIZE - 1);
688 if (unlikely(vnet_tx_dring_avail(dr) < 2)) {
689 netif_stop_queue(dev);
690 if (vnet_tx_dring_avail(dr) > VNET_TX_WAKEUP_THRESH(dr))
691 netif_wake_queue(dev);
692 }
693
694 spin_unlock_irqrestore(&port->vio.lock, flags);
695
696 dev_kfree_skb(skb);
697
698 dev->trans_start = jiffies;
699 return NETDEV_TX_OK;
700
701out_dropped_unlock:
702 spin_unlock_irqrestore(&port->vio.lock, flags);
703
704out_dropped:
705 dev_kfree_skb(skb);
706 dev->stats.tx_dropped++;
707 return NETDEV_TX_OK;
708}
709
710static void vnet_tx_timeout(struct net_device *dev)
711{
712 /* XXX Implement me XXX */
713}
714
715static int vnet_open(struct net_device *dev)
716{
717 netif_carrier_on(dev);
718 netif_start_queue(dev);
719
720 return 0;
721}
722
723static int vnet_close(struct net_device *dev)
724{
725 netif_stop_queue(dev);
726 netif_carrier_off(dev);
727
728 return 0;
729}
730
731static void vnet_set_rx_mode(struct net_device *dev)
732{
733 /* XXX Implement multicast support XXX */
734}
735
736static int vnet_change_mtu(struct net_device *dev, int new_mtu)
737{
738 if (new_mtu != ETH_DATA_LEN)
739 return -EINVAL;
740
741 dev->mtu = new_mtu;
742 return 0;
743}
744
745static int vnet_set_mac_addr(struct net_device *dev, void *p)
746{
747 return -EINVAL;
748}
749
750static void vnet_get_drvinfo(struct net_device *dev,
751 struct ethtool_drvinfo *info)
752{
753 strcpy(info->driver, DRV_MODULE_NAME);
754 strcpy(info->version, DRV_MODULE_VERSION);
755}
756
757static u32 vnet_get_msglevel(struct net_device *dev)
758{
759 struct vnet *vp = netdev_priv(dev);
760 return vp->msg_enable;
761}
762
763static void vnet_set_msglevel(struct net_device *dev, u32 value)
764{
765 struct vnet *vp = netdev_priv(dev);
766 vp->msg_enable = value;
767}
768
769static const struct ethtool_ops vnet_ethtool_ops = {
770 .get_drvinfo = vnet_get_drvinfo,
771 .get_msglevel = vnet_get_msglevel,
772 .set_msglevel = vnet_set_msglevel,
773 .get_link = ethtool_op_get_link,
774 .get_perm_addr = ethtool_op_get_perm_addr,
775};
776
777static void vnet_port_free_tx_bufs(struct vnet_port *port)
778{
779 struct vio_dring_state *dr;
780 int i;
781
782 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
783 if (dr->base) {
784 ldc_free_exp_dring(port->vio.lp, dr->base,
785 (dr->entry_size * dr->num_entries),
786 dr->cookies, dr->ncookies);
787 dr->base = NULL;
788 dr->entry_size = 0;
789 dr->num_entries = 0;
790 dr->pending = 0;
791 dr->ncookies = 0;
792 }
793
794 for (i = 0; i < VNET_TX_RING_SIZE; i++) {
795 void *buf = port->tx_bufs[i].buf;
796
797 if (!buf)
798 continue;
799
800 ldc_unmap(port->vio.lp,
801 port->tx_bufs[i].cookies,
802 port->tx_bufs[i].ncookies);
803
804 kfree(buf);
805 port->tx_bufs[i].buf = NULL;
806 }
807}
808
809static int __devinit vnet_port_alloc_tx_bufs(struct vnet_port *port)
810{
811 struct vio_dring_state *dr;
812 unsigned long len;
813 int i, err, ncookies;
814 void *dring;
815
816 for (i = 0; i < VNET_TX_RING_SIZE; i++) {
817 void *buf = kzalloc(ETH_FRAME_LEN + 8, GFP_KERNEL);
818 int map_len = (ETH_FRAME_LEN + 7) & ~7;
819
820 err = -ENOMEM;
821 if (!buf) {
822 printk(KERN_ERR "TX buffer allocation failure\n");
823 goto err_out;
824 }
825 err = -EFAULT;
826 if ((unsigned long)buf & (8UL - 1)) {
827 printk(KERN_ERR "TX buffer misaligned\n");
828 kfree(buf);
829 goto err_out;
830 }
831
832 err = ldc_map_single(port->vio.lp, buf, map_len,
833 port->tx_bufs[i].cookies, 2,
834 (LDC_MAP_SHADOW |
835 LDC_MAP_DIRECT |
836 LDC_MAP_RW));
837 if (err < 0) {
838 kfree(buf);
839 goto err_out;
840 }
841 port->tx_bufs[i].buf = buf;
842 port->tx_bufs[i].ncookies = err;
843 }
844
845 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
846
847 len = (VNET_TX_RING_SIZE *
848 (sizeof(struct vio_net_desc) +
849 (sizeof(struct ldc_trans_cookie) * 2)));
850
851 ncookies = VIO_MAX_RING_COOKIES;
852 dring = ldc_alloc_exp_dring(port->vio.lp, len,
853 dr->cookies, &ncookies,
854 (LDC_MAP_SHADOW |
855 LDC_MAP_DIRECT |
856 LDC_MAP_RW));
857 if (IS_ERR(dring)) {
858 err = PTR_ERR(dring);
859 goto err_out;
860 }
861
862 dr->base = dring;
863 dr->entry_size = (sizeof(struct vio_net_desc) +
864 (sizeof(struct ldc_trans_cookie) * 2));
865 dr->num_entries = VNET_TX_RING_SIZE;
866 dr->prod = dr->cons = 0;
867 dr->pending = VNET_TX_RING_SIZE;
868 dr->ncookies = ncookies;
869
870 return 0;
871
872err_out:
873 vnet_port_free_tx_bufs(port);
874
875 return err;
876}
877
878static struct ldc_channel_config vnet_ldc_cfg = {
879 .event = vnet_event,
880 .mtu = 64,
881 .mode = LDC_MODE_UNRELIABLE,
882};
883
884static struct vio_driver_ops vnet_vio_ops = {
885 .send_attr = vnet_send_attr,
886 .handle_attr = vnet_handle_attr,
887 .handshake_complete = vnet_handshake_complete,
888};
889
890const char *remote_macaddr_prop = "remote-mac-address";
891
892static int __devinit vnet_port_probe(struct vio_dev *vdev,
893 const struct vio_device_id *id)
894{
895 struct mdesc_node *endp;
896 struct vnet_port *port;
897 unsigned long flags;
898 struct vnet *vp;
899 const u64 *rmac;
900 int len, i, err, switch_port;
901
902 vp = dev_get_drvdata(vdev->dev.parent);
903 if (!vp) {
904 printk(KERN_ERR PFX "Cannot find port parent vnet.\n");
905 return -ENODEV;
906 }
907
908 rmac = md_get_property(vdev->mp, remote_macaddr_prop, &len);
909 if (!rmac) {
910 printk(KERN_ERR PFX "Port lacks %s property.\n",
911 remote_macaddr_prop);
912 return -ENODEV;
913 }
914
915 endp = vio_find_endpoint(vdev);
916 if (!endp) {
917 printk(KERN_ERR PFX "Port lacks channel-endpoint.\n");
918 return -ENODEV;
919 }
920
921 port = kzalloc(sizeof(*port), GFP_KERNEL);
922 if (!port) {
923 printk(KERN_ERR PFX "Cannot allocate vnet_port.\n");
924 return -ENOMEM;
925 }
926
927 for (i = 0; i < ETH_ALEN; i++)
928 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
929
930 port->vp = vp;
931
932 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK, endp,
933 vnet_versions, ARRAY_SIZE(vnet_versions),
934 &vnet_vio_ops, vp->dev->name);
935 if (err)
936 goto err_out_free_port;
937
938 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
939 if (err)
940 goto err_out_free_port;
941
942 err = vnet_port_alloc_tx_bufs(port);
943 if (err)
944 goto err_out_free_ldc;
945
946 INIT_HLIST_NODE(&port->hash);
947 INIT_LIST_HEAD(&port->list);
948
949 switch_port = 0;
950 if (md_get_property(vdev->mp, "switch-port", NULL) != NULL)
951 switch_port = 1;
952
953 spin_lock_irqsave(&vp->lock, flags);
954 if (switch_port)
955 list_add(&port->list, &vp->port_list);
956 else
957 list_add_tail(&port->list, &vp->port_list);
958 hlist_add_head(&port->hash, &vp->port_hash[vnet_hashfn(port->raddr)]);
959 spin_unlock_irqrestore(&vp->lock, flags);
960
961 dev_set_drvdata(&vdev->dev, port);
962
963 printk(KERN_INFO "%s: PORT ( remote-mac ", vp->dev->name);
964 for (i = 0; i < 6; i++)
965 printk("%2.2x%c", port->raddr[i], i == 5 ? ' ' : ':');
966 if (switch_port)
967 printk("switch-port ");
968 printk(")\n");
969
970 vio_port_up(&port->vio);
971
972 return 0;
973
974err_out_free_ldc:
975 vio_ldc_free(&port->vio);
976
977err_out_free_port:
978 kfree(port);
979
980 return err;
981}
982
983static int vnet_port_remove(struct vio_dev *vdev)
984{
985 struct vnet_port *port = dev_get_drvdata(&vdev->dev);
986
987 if (port) {
988 struct vnet *vp = port->vp;
989 unsigned long flags;
990
991 del_timer_sync(&port->vio.timer);
992
993 spin_lock_irqsave(&vp->lock, flags);
994 list_del(&port->list);
995 hlist_del(&port->hash);
996 spin_unlock_irqrestore(&vp->lock, flags);
997
998 vnet_port_free_tx_bufs(port);
999 vio_ldc_free(&port->vio);
1000
1001 dev_set_drvdata(&vdev->dev, NULL);
1002
1003 kfree(port);
1004 }
1005 return 0;
1006}
1007
1008static struct vio_device_id vnet_port_match[] = {
1009 {
1010 .type = "vnet-port",
1011 },
1012 {},
1013};
1014MODULE_DEVICE_TABLE(vio, vnet_match);
1015
1016static struct vio_driver vnet_port_driver = {
1017 .id_table = vnet_port_match,
1018 .probe = vnet_port_probe,
1019 .remove = vnet_port_remove,
1020 .driver = {
1021 .name = "vnet_port",
1022 .owner = THIS_MODULE,
1023 }
1024};
1025
1026const char *local_mac_prop = "local-mac-address";
1027
1028static int __devinit vnet_probe(struct vio_dev *vdev,
1029 const struct vio_device_id *id)
1030{
1031 static int vnet_version_printed;
1032 struct net_device *dev;
1033 struct vnet *vp;
1034 const u64 *mac;
1035 int err, i, len;
1036
1037 if (vnet_version_printed++ == 0)
1038 printk(KERN_INFO "%s", version);
1039
1040 mac = md_get_property(vdev->mp, local_mac_prop, &len);
1041 if (!mac) {
1042 printk(KERN_ERR PFX "vnet lacks %s property.\n",
1043 local_mac_prop);
1044 err = -ENODEV;
1045 goto err_out;
1046 }
1047
1048 dev = alloc_etherdev(sizeof(*vp));
1049 if (!dev) {
1050 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
1051 err = -ENOMEM;
1052 goto err_out;
1053 }
1054
1055 for (i = 0; i < ETH_ALEN; i++)
1056 dev->dev_addr[i] = (*mac >> (5 - i) * 8) & 0xff;
1057
1058 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
1059
1060 SET_NETDEV_DEV(dev, &vdev->dev);
1061
1062 vp = netdev_priv(dev);
1063
1064 spin_lock_init(&vp->lock);
1065 vp->dev = dev;
1066 vp->vdev = vdev;
1067
1068 INIT_LIST_HEAD(&vp->port_list);
1069 for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
1070 INIT_HLIST_HEAD(&vp->port_hash[i]);
1071
1072 dev->open = vnet_open;
1073 dev->stop = vnet_close;
1074 dev->set_multicast_list = vnet_set_rx_mode;
1075 dev->set_mac_address = vnet_set_mac_addr;
1076 dev->tx_timeout = vnet_tx_timeout;
1077 dev->ethtool_ops = &vnet_ethtool_ops;
1078 dev->watchdog_timeo = VNET_TX_TIMEOUT;
1079 dev->change_mtu = vnet_change_mtu;
1080 dev->hard_start_xmit = vnet_start_xmit;
1081
1082 err = register_netdev(dev);
1083 if (err) {
1084 printk(KERN_ERR PFX "Cannot register net device, "
1085 "aborting.\n");
1086 goto err_out_free_dev;
1087 }
1088
1089 printk(KERN_INFO "%s: Sun LDOM vnet ", dev->name);
1090
1091 for (i = 0; i < 6; i++)
1092 printk("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
1093
1094 dev_set_drvdata(&vdev->dev, vp);
1095
1096 return 0;
1097
1098err_out_free_dev:
1099 free_netdev(dev);
1100
1101err_out:
1102 return err;
1103}
1104
1105static int vnet_remove(struct vio_dev *vdev)
1106{
1107
1108 struct vnet *vp = dev_get_drvdata(&vdev->dev);
1109
1110 if (vp) {
1111 /* XXX unregister port, or at least check XXX */
1112 unregister_netdevice(vp->dev);
1113 dev_set_drvdata(&vdev->dev, NULL);
1114 }
1115 return 0;
1116}
1117
1118static struct vio_device_id vnet_match[] = {
1119 {
1120 .type = "network",
1121 },
1122 {},
1123};
1124MODULE_DEVICE_TABLE(vio, vnet_match);
1125
1126static struct vio_driver vnet_driver = {
1127 .id_table = vnet_match,
1128 .probe = vnet_probe,
1129 .remove = vnet_remove,
1130 .driver = {
1131 .name = "vnet",
1132 .owner = THIS_MODULE,
1133 }
1134};
1135
1136static int __init vnet_init(void)
1137{
1138 int err = vio_register_driver(&vnet_driver);
1139
1140 if (!err) {
1141 err = vio_register_driver(&vnet_port_driver);
1142 if (err)
1143 vio_unregister_driver(&vnet_driver);
1144 }
1145
1146 return err;
1147}
1148
1149static void __exit vnet_exit(void)
1150{
1151 vio_unregister_driver(&vnet_port_driver);
1152 vio_unregister_driver(&vnet_driver);
1153}
1154
1155module_init(vnet_init);
1156module_exit(vnet_exit);
diff --git a/drivers/net/sunvnet.h b/drivers/net/sunvnet.h
new file mode 100644
index 000000000000..1c887302d46d
--- /dev/null
+++ b/drivers/net/sunvnet.h
@@ -0,0 +1,70 @@
1#ifndef _SUNVNET_H
2#define _SUNVNET_H
3
4#define DESC_NCOOKIES(entry_size) \
5 ((entry_size) - sizeof(struct vio_net_desc))
6
7/* length of time before we decide the hardware is borked,
8 * and dev->tx_timeout() should be called to fix the problem
9 */
10#define VNET_TX_TIMEOUT (5 * HZ)
11
12#define VNET_TX_RING_SIZE 512
13#define VNET_TX_WAKEUP_THRESH(dr) ((dr)->pending / 4)
14
15/* VNET packets are sent in buffers with the first 6 bytes skipped
16 * so that after the ethernet header the IPv4/IPv6 headers are aligned
17 * properly.
18 */
19#define VNET_PACKET_SKIP 6
20
21struct vnet_tx_entry {
22 void *buf;
23 unsigned int ncookies;
24 struct ldc_trans_cookie cookies[2];
25};
26
27struct vnet;
28struct vnet_port {
29 struct vio_driver_state vio;
30
31 struct hlist_node hash;
32 u8 raddr[ETH_ALEN];
33
34 struct vnet *vp;
35
36 struct vnet_tx_entry tx_bufs[VNET_TX_RING_SIZE];
37
38 struct list_head list;
39};
40
41static inline struct vnet_port *to_vnet_port(struct vio_driver_state *vio)
42{
43 return container_of(vio, struct vnet_port, vio);
44}
45
46#define VNET_PORT_HASH_SIZE 16
47#define VNET_PORT_HASH_MASK (VNET_PORT_HASH_SIZE - 1)
48
49static inline unsigned int vnet_hashfn(u8 *mac)
50{
51 unsigned int val = mac[4] ^ mac[5];
52
53 return val & (VNET_PORT_HASH_MASK);
54}
55
56struct vnet {
57 /* Protects port_list and port_hash. */
58 spinlock_t lock;
59
60 struct net_device *dev;
61
62 u32 msg_enable;
63 struct vio_dev *vdev;
64
65 struct list_head port_list;
66
67 struct hlist_head port_hash[VNET_PORT_HASH_SIZE];
68};
69
70#endif /* _SUNVNET_H */