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