aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2007-07-19 04:49:28 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-07-19 13:04:53 -0400
commitd503e2fa5aecef99675c5a81b61321a5407bf61f (patch)
treec00a98dfbc5a37ddfc359b11bf5fb8fc30a81754 /drivers
parent3f8c4d3f82c564e5e27c6375fe17544f694359dc (diff)
lguest: the net driver
Lguest net driver A simple net driver for lguest. [akpm@linux-foundation.org: include fix] Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Andi Kleen <ak@suse.de> Cc: Jeff Garzik <jeff@garzik.org> Acked-by: James Morris <jmorris@namei.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/net/Makefile1
-rw-r--r--drivers/net/lguest_net.c354
2 files changed, 355 insertions, 0 deletions
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index aaaa0a04bb46..336af0635df8 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -177,6 +177,7 @@ obj-$(CONFIG_ZORRO8390) += zorro8390.o
177obj-$(CONFIG_HPLANCE) += hplance.o 7990.o 177obj-$(CONFIG_HPLANCE) += hplance.o 7990.o
178obj-$(CONFIG_MVME147_NET) += mvme147.o 7990.o 178obj-$(CONFIG_MVME147_NET) += mvme147.o 7990.o
179obj-$(CONFIG_EQUALIZER) += eql.o 179obj-$(CONFIG_EQUALIZER) += eql.o
180obj-$(CONFIG_LGUEST_GUEST) += lguest_net.o
180obj-$(CONFIG_MIPS_JAZZ_SONIC) += jazzsonic.o 181obj-$(CONFIG_MIPS_JAZZ_SONIC) += jazzsonic.o
181obj-$(CONFIG_MIPS_AU1X00_ENET) += au1000_eth.o 182obj-$(CONFIG_MIPS_AU1X00_ENET) += au1000_eth.o
182obj-$(CONFIG_MIPS_SIM_NET) += mipsnet.o 183obj-$(CONFIG_MIPS_SIM_NET) += mipsnet.o
diff --git a/drivers/net/lguest_net.c b/drivers/net/lguest_net.c
new file mode 100644
index 000000000000..112778652f7d
--- /dev/null
+++ b/drivers/net/lguest_net.c
@@ -0,0 +1,354 @@
1/* A simple network driver for lguest.
2 *
3 * Copyright 2006 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19//#define DEBUG
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
22#include <linux/module.h>
23#include <linux/mm_types.h>
24#include <linux/io.h>
25#include <linux/lguest_bus.h>
26
27#define SHARED_SIZE PAGE_SIZE
28#define MAX_LANS 4
29#define NUM_SKBS 8
30
31struct lguestnet_info
32{
33 /* The shared page(s). */
34 struct lguest_net *peer;
35 unsigned long peer_phys;
36 unsigned long mapsize;
37
38 /* The lguest_device I come from */
39 struct lguest_device *lgdev;
40
41 /* My peerid. */
42 unsigned int me;
43
44 /* Receive queue. */
45 struct sk_buff *skb[NUM_SKBS];
46 struct lguest_dma dma[NUM_SKBS];
47};
48
49/* How many bytes left in this page. */
50static unsigned int rest_of_page(void *data)
51{
52 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
53}
54
55/* Simple convention: offset 4 * peernum. */
56static unsigned long peer_key(struct lguestnet_info *info, unsigned peernum)
57{
58 return info->peer_phys + 4 * peernum;
59}
60
61static void skb_to_dma(const struct sk_buff *skb, unsigned int headlen,
62 struct lguest_dma *dma)
63{
64 unsigned int i, seg;
65
66 for (i = seg = 0; i < headlen; seg++, i += rest_of_page(skb->data+i)) {
67 dma->addr[seg] = virt_to_phys(skb->data + i);
68 dma->len[seg] = min((unsigned)(headlen - i),
69 rest_of_page(skb->data + i));
70 }
71 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, seg++) {
72 const skb_frag_t *f = &skb_shinfo(skb)->frags[i];
73 /* Should not happen with MTU less than 64k - 2 * PAGE_SIZE. */
74 if (seg == LGUEST_MAX_DMA_SECTIONS) {
75 printk("Woah dude! Megapacket!\n");
76 break;
77 }
78 dma->addr[seg] = page_to_phys(f->page) + f->page_offset;
79 dma->len[seg] = f->size;
80 }
81 if (seg < LGUEST_MAX_DMA_SECTIONS)
82 dma->len[seg] = 0;
83}
84
85/* We overload multicast bit to show promiscuous mode. */
86#define PROMISC_BIT 0x01
87
88static void lguestnet_set_multicast(struct net_device *dev)
89{
90 struct lguestnet_info *info = netdev_priv(dev);
91
92 if ((dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) || dev->mc_count)
93 info->peer[info->me].mac[0] |= PROMISC_BIT;
94 else
95 info->peer[info->me].mac[0] &= ~PROMISC_BIT;
96}
97
98static int promisc(struct lguestnet_info *info, unsigned int peer)
99{
100 return info->peer[peer].mac[0] & PROMISC_BIT;
101}
102
103static int mac_eq(const unsigned char mac[ETH_ALEN],
104 struct lguestnet_info *info, unsigned int peer)
105{
106 /* Ignore multicast bit, which peer turns on to mean promisc. */
107 if ((info->peer[peer].mac[0] & (~PROMISC_BIT)) != mac[0])
108 return 0;
109 return memcmp(mac+1, info->peer[peer].mac+1, ETH_ALEN-1) == 0;
110}
111
112static void transfer_packet(struct net_device *dev,
113 struct sk_buff *skb,
114 unsigned int peernum)
115{
116 struct lguestnet_info *info = netdev_priv(dev);
117 struct lguest_dma dma;
118
119 skb_to_dma(skb, skb_headlen(skb), &dma);
120 pr_debug("xfer length %04x (%u)\n", htons(skb->len), skb->len);
121
122 lguest_send_dma(peer_key(info, peernum), &dma);
123 if (dma.used_len != skb->len) {
124 dev->stats.tx_carrier_errors++;
125 pr_debug("Bad xfer to peer %i: %i of %i (dma %p/%i)\n",
126 peernum, dma.used_len, skb->len,
127 (void *)dma.addr[0], dma.len[0]);
128 } else {
129 dev->stats.tx_bytes += skb->len;
130 dev->stats.tx_packets++;
131 }
132}
133
134static int unused_peer(const struct lguest_net peer[], unsigned int num)
135{
136 return peer[num].mac[0] == 0;
137}
138
139static int lguestnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
140{
141 unsigned int i;
142 int broadcast;
143 struct lguestnet_info *info = netdev_priv(dev);
144 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
145
146 pr_debug("%s: xmit %02x:%02x:%02x:%02x:%02x:%02x\n",
147 dev->name, dest[0],dest[1],dest[2],dest[3],dest[4],dest[5]);
148
149 broadcast = is_multicast_ether_addr(dest);
150 for (i = 0; i < info->mapsize/sizeof(struct lguest_net); i++) {
151 if (i == info->me || unused_peer(info->peer, i))
152 continue;
153
154 if (!broadcast && !promisc(info, i) && !mac_eq(dest, info, i))
155 continue;
156
157 pr_debug("lguestnet %s: sending from %i to %i\n",
158 dev->name, info->me, i);
159 transfer_packet(dev, skb, i);
160 }
161 dev_kfree_skb(skb);
162 return 0;
163}
164
165/* Find a new skb to put in this slot in shared mem. */
166static int fill_slot(struct net_device *dev, unsigned int slot)
167{
168 struct lguestnet_info *info = netdev_priv(dev);
169 /* Try to create and register a new one. */
170 info->skb[slot] = netdev_alloc_skb(dev, ETH_HLEN + ETH_DATA_LEN);
171 if (!info->skb[slot]) {
172 printk("%s: could not fill slot %i\n", dev->name, slot);
173 return -ENOMEM;
174 }
175
176 skb_to_dma(info->skb[slot], ETH_HLEN + ETH_DATA_LEN, &info->dma[slot]);
177 wmb();
178 /* Now we tell hypervisor it can use the slot. */
179 info->dma[slot].used_len = 0;
180 return 0;
181}
182
183static irqreturn_t lguestnet_rcv(int irq, void *dev_id)
184{
185 struct net_device *dev = dev_id;
186 struct lguestnet_info *info = netdev_priv(dev);
187 unsigned int i, done = 0;
188
189 for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
190 unsigned int length;
191 struct sk_buff *skb;
192
193 length = info->dma[i].used_len;
194 if (length == 0)
195 continue;
196
197 done++;
198 skb = info->skb[i];
199 fill_slot(dev, i);
200
201 if (length < ETH_HLEN || length > ETH_HLEN + ETH_DATA_LEN) {
202 pr_debug(KERN_WARNING "%s: unbelievable skb len: %i\n",
203 dev->name, length);
204 dev_kfree_skb(skb);
205 continue;
206 }
207
208 skb_put(skb, length);
209 skb->protocol = eth_type_trans(skb, dev);
210 /* This is a reliable transport. */
211 if (dev->features & NETIF_F_NO_CSUM)
212 skb->ip_summed = CHECKSUM_UNNECESSARY;
213 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
214 ntohs(skb->protocol), skb->len, skb->pkt_type);
215
216 dev->stats.rx_bytes += skb->len;
217 dev->stats.rx_packets++;
218 netif_rx(skb);
219 }
220 return done ? IRQ_HANDLED : IRQ_NONE;
221}
222
223static int lguestnet_open(struct net_device *dev)
224{
225 int i;
226 struct lguestnet_info *info = netdev_priv(dev);
227
228 /* Set up our MAC address */
229 memcpy(info->peer[info->me].mac, dev->dev_addr, ETH_ALEN);
230
231 /* Turn on promisc mode if needed */
232 lguestnet_set_multicast(dev);
233
234 for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
235 if (fill_slot(dev, i) != 0)
236 goto cleanup;
237 }
238 if (lguest_bind_dma(peer_key(info,info->me), info->dma,
239 NUM_SKBS, lgdev_irq(info->lgdev)) != 0)
240 goto cleanup;
241 return 0;
242
243cleanup:
244 while (--i >= 0)
245 dev_kfree_skb(info->skb[i]);
246 return -ENOMEM;
247}
248
249static int lguestnet_close(struct net_device *dev)
250{
251 unsigned int i;
252 struct lguestnet_info *info = netdev_priv(dev);
253
254 /* Clear all trace: others might deliver packets, we'll ignore it. */
255 memset(&info->peer[info->me], 0, sizeof(info->peer[info->me]));
256
257 /* Deregister sg lists. */
258 lguest_unbind_dma(peer_key(info, info->me), info->dma);
259 for (i = 0; i < ARRAY_SIZE(info->dma); i++)
260 dev_kfree_skb(info->skb[i]);
261 return 0;
262}
263
264static int lguestnet_probe(struct lguest_device *lgdev)
265{
266 int err, irqf = IRQF_SHARED;
267 struct net_device *dev;
268 struct lguestnet_info *info;
269 struct lguest_device_desc *desc = &lguest_devices[lgdev->index];
270
271 pr_debug("lguest_net: probing for device %i\n", lgdev->index);
272
273 dev = alloc_etherdev(sizeof(struct lguestnet_info));
274 if (!dev)
275 return -ENOMEM;
276
277 SET_MODULE_OWNER(dev);
278
279 /* Ethernet defaults with some changes */
280 ether_setup(dev);
281 dev->set_mac_address = NULL;
282
283 dev->dev_addr[0] = 0x02; /* set local assignment bit (IEEE802) */
284 dev->dev_addr[1] = 0x00;
285 memcpy(&dev->dev_addr[2], &lguest_data.guestid, 2);
286 dev->dev_addr[4] = 0x00;
287 dev->dev_addr[5] = 0x00;
288
289 dev->open = lguestnet_open;
290 dev->stop = lguestnet_close;
291 dev->hard_start_xmit = lguestnet_start_xmit;
292
293 /* Turning on/off promisc will call dev->set_multicast_list.
294 * We don't actually support multicast yet */
295 dev->set_multicast_list = lguestnet_set_multicast;
296 SET_NETDEV_DEV(dev, &lgdev->dev);
297 if (desc->features & LGUEST_NET_F_NOCSUM)
298 dev->features = NETIF_F_SG|NETIF_F_NO_CSUM;
299
300 info = netdev_priv(dev);
301 info->mapsize = PAGE_SIZE * desc->num_pages;
302 info->peer_phys = ((unsigned long)desc->pfn << PAGE_SHIFT);
303 info->lgdev = lgdev;
304 info->peer = lguest_map(info->peer_phys, desc->num_pages);
305 if (!info->peer) {
306 err = -ENOMEM;
307 goto free;
308 }
309
310 /* This stores our peerid (upper bits reserved for future). */
311 info->me = (desc->features & (info->mapsize-1));
312
313 err = register_netdev(dev);
314 if (err) {
315 pr_debug("lguestnet: registering device failed\n");
316 goto unmap;
317 }
318
319 if (lguest_devices[lgdev->index].features & LGUEST_DEVICE_F_RANDOMNESS)
320 irqf |= IRQF_SAMPLE_RANDOM;
321 if (request_irq(lgdev_irq(lgdev), lguestnet_rcv, irqf, "lguestnet",
322 dev) != 0) {
323 pr_debug("lguestnet: cannot get irq %i\n", lgdev_irq(lgdev));
324 goto unregister;
325 }
326
327 pr_debug("lguestnet: registered device %s\n", dev->name);
328 lgdev->private = dev;
329 return 0;
330
331unregister:
332 unregister_netdev(dev);
333unmap:
334 lguest_unmap(info->peer);
335free:
336 free_netdev(dev);
337 return err;
338}
339
340static struct lguest_driver lguestnet_drv = {
341 .name = "lguestnet",
342 .owner = THIS_MODULE,
343 .device_type = LGUEST_DEVICE_T_NET,
344 .probe = lguestnet_probe,
345};
346
347static __init int lguestnet_init(void)
348{
349 return register_lguest_driver(&lguestnet_drv);
350}
351module_init(lguestnet_init);
352
353MODULE_DESCRIPTION("Lguest network driver");
354MODULE_LICENSE("GPL");