blob: cab57911a80e041e1f7b536324f1c56de6a44c19 [file] [log] [blame]
Rusty Russelle2c97842007-07-26 10:41:03 -07001/*D:500
2 * The Guest network driver.
Rusty Russelld503e2fa2007-07-19 01:49:28 -07003 *
Rusty Russelle2c97842007-07-26 10:41:03 -07004 * This is very simple a virtual network driver, and our last Guest driver.
5 * The only trick is that it can talk directly to multiple other recipients
6 * (ie. other Guests on the same network). It can also be used with only the
7 * Host on the network.
8 :*/
9
10/* Copyright 2006 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
Rusty Russelld503e2fa2007-07-19 01:49:28 -070011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26//#define DEBUG
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/module.h>
30#include <linux/mm_types.h>
31#include <linux/io.h>
32#include <linux/lguest_bus.h>
33
34#define SHARED_SIZE PAGE_SIZE
35#define MAX_LANS 4
36#define NUM_SKBS 8
37
Rusty Russellf56a3842007-07-26 10:41:05 -070038/*M:011 Network code master Jeff Garzik points out numerous shortcomings in
39 * this driver if it aspires to greatness.
40 *
41 * Firstly, it doesn't use "NAPI": the networking's New API, and is poorer for
42 * it. As he says "NAPI means system-wide load leveling, across multiple
43 * network interfaces. Lack of NAPI can mean competition at higher loads."
44 *
45 * He also points out that we don't implement set_mac_address, so users cannot
46 * change the devices hardware address. When I asked why one would want to:
47 * "Bonding, and situations where you /do/ want the MAC address to "leak" out
48 * of the host onto the wider net."
49 *
50 * Finally, he would like module unloading: "It is not unrealistic to think of
51 * [un|re|]loading the net support module in an lguest guest. And, adding
52 * module support makes the programmer more responsible, because they now have
53 * to learn to clean up after themselves. Any driver that cannot clean up
54 * after itself is an incomplete driver in my book."
55 :*/
56
Rusty Russelle2c97842007-07-26 10:41:03 -070057/*D:530 The "struct lguestnet_info" contains all the information we need to
58 * know about the network device. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070059struct lguestnet_info
60{
Rusty Russelle2c97842007-07-26 10:41:03 -070061 /* The mapped device page(s) (an array of "struct lguest_net"). */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070062 struct lguest_net *peer;
Rusty Russelle2c97842007-07-26 10:41:03 -070063 /* The physical address of the device page(s) */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070064 unsigned long peer_phys;
Rusty Russelle2c97842007-07-26 10:41:03 -070065 /* The size of the device page(s). */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070066 unsigned long mapsize;
67
68 /* The lguest_device I come from */
69 struct lguest_device *lgdev;
70
Rusty Russelle2c97842007-07-26 10:41:03 -070071 /* My peerid (ie. my slot in the array). */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070072 unsigned int me;
73
Rusty Russelle2c97842007-07-26 10:41:03 -070074 /* Receive queue: the network packets waiting to be filled. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070075 struct sk_buff *skb[NUM_SKBS];
76 struct lguest_dma dma[NUM_SKBS];
77};
Rusty Russelle2c97842007-07-26 10:41:03 -070078/*:*/
Rusty Russelld503e2fa2007-07-19 01:49:28 -070079
80/* How many bytes left in this page. */
81static unsigned int rest_of_page(void *data)
82{
83 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
84}
85
Rusty Russelle2c97842007-07-26 10:41:03 -070086/*D:570 Each peer (ie. Guest or Host) on the network binds their receive
87 * buffers to a different key: we simply use the physical address of the
88 * device's memory page plus the peer number. The Host insists that all keys
89 * be a multiple of 4, so we multiply the peer number by 4. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -070090static unsigned long peer_key(struct lguestnet_info *info, unsigned peernum)
91{
92 return info->peer_phys + 4 * peernum;
93}
94
Rusty Russelle2c97842007-07-26 10:41:03 -070095/* This is the routine which sets up a "struct lguest_dma" to point to a
96 * network packet, similar to req_to_dma() in lguest_blk.c. The structure of a
97 * "struct sk_buff" has grown complex over the years: it consists of a "head"
98 * linear section pointed to by "skb->data", and possibly an array of
99 * "fragments" in the case of a non-linear packet.
100 *
101 * Our receive buffers don't use fragments at all but outgoing skbs might, so
102 * we handle it. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700103static void skb_to_dma(const struct sk_buff *skb, unsigned int headlen,
104 struct lguest_dma *dma)
105{
106 unsigned int i, seg;
107
Rusty Russelle2c97842007-07-26 10:41:03 -0700108 /* First, we put the linear region into the "struct lguest_dma". Each
109 * entry can't go over a page boundary, so even though all our packets
110 * are 1514 bytes or less, we might need to use two entries here: */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700111 for (i = seg = 0; i < headlen; seg++, i += rest_of_page(skb->data+i)) {
112 dma->addr[seg] = virt_to_phys(skb->data + i);
113 dma->len[seg] = min((unsigned)(headlen - i),
114 rest_of_page(skb->data + i));
115 }
Rusty Russelle2c97842007-07-26 10:41:03 -0700116
117 /* Now we handle the fragments: at least they're guaranteed not to go
118 * over a page. skb_shinfo(skb) returns a pointer to the structure
119 * which tells us about the number of fragments and the fragment
120 * array. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700121 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, seg++) {
122 const skb_frag_t *f = &skb_shinfo(skb)->frags[i];
123 /* Should not happen with MTU less than 64k - 2 * PAGE_SIZE. */
124 if (seg == LGUEST_MAX_DMA_SECTIONS) {
Rusty Russelle2c97842007-07-26 10:41:03 -0700125 /* We will end up sending a truncated packet should
126 * this ever happen. Plus, a cool log message! */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700127 printk("Woah dude! Megapacket!\n");
128 break;
129 }
130 dma->addr[seg] = page_to_phys(f->page) + f->page_offset;
131 dma->len[seg] = f->size;
132 }
Rusty Russelle2c97842007-07-26 10:41:03 -0700133
134 /* If after all that we didn't use the entire "struct lguest_dma"
135 * array, we terminate it with a 0 length. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700136 if (seg < LGUEST_MAX_DMA_SECTIONS)
137 dma->len[seg] = 0;
138}
139
Rusty Russelle2c97842007-07-26 10:41:03 -0700140/*
141 * Packet transmission.
142 *
143 * Our packet transmission is a little unusual. A real network card would just
144 * send out the packet and leave the receivers to decide if they're interested.
145 * Instead, we look through the network device memory page and see if any of
146 * the ethernet addresses match the packet destination, and if so we send it to
147 * that Guest.
148 *
149 * This is made a little more complicated in two cases. The first case is
150 * broadcast packets: for that we send the packet to all Guests on the network,
151 * one at a time. The second case is "promiscuous" mode, where a Guest wants
152 * to see all the packets on the network. We need a way for the Guest to tell
153 * us it wants to see all packets, so it sets the "multicast" bit on its
154 * published MAC address, which is never valid in a real ethernet address.
155 */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700156#define PROMISC_BIT 0x01
157
Rusty Russelle2c97842007-07-26 10:41:03 -0700158/* This is the callback which is summoned whenever the network device's
159 * multicast or promiscuous state changes. If the card is in promiscuous mode,
160 * we advertise that in our ethernet address in the device's memory. We do the
161 * same if Linux wants any or all multicast traffic. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700162static void lguestnet_set_multicast(struct net_device *dev)
163{
164 struct lguestnet_info *info = netdev_priv(dev);
165
166 if ((dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) || dev->mc_count)
167 info->peer[info->me].mac[0] |= PROMISC_BIT;
168 else
169 info->peer[info->me].mac[0] &= ~PROMISC_BIT;
170}
171
Rusty Russelle2c97842007-07-26 10:41:03 -0700172/* A simple test function to see if a peer wants to see all packets.*/
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700173static int promisc(struct lguestnet_info *info, unsigned int peer)
174{
175 return info->peer[peer].mac[0] & PROMISC_BIT;
176}
177
Rusty Russelle2c97842007-07-26 10:41:03 -0700178/* Another simple function to see if a peer's advertised ethernet address
179 * matches a packet's destination ethernet address. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700180static int mac_eq(const unsigned char mac[ETH_ALEN],
181 struct lguestnet_info *info, unsigned int peer)
182{
183 /* Ignore multicast bit, which peer turns on to mean promisc. */
184 if ((info->peer[peer].mac[0] & (~PROMISC_BIT)) != mac[0])
185 return 0;
186 return memcmp(mac+1, info->peer[peer].mac+1, ETH_ALEN-1) == 0;
187}
188
Rusty Russelle2c97842007-07-26 10:41:03 -0700189/* This is the function which actually sends a packet once we've decided a
190 * peer wants it: */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700191static void transfer_packet(struct net_device *dev,
192 struct sk_buff *skb,
193 unsigned int peernum)
194{
195 struct lguestnet_info *info = netdev_priv(dev);
196 struct lguest_dma dma;
197
Rusty Russelle2c97842007-07-26 10:41:03 -0700198 /* We use our handy "struct lguest_dma" packing function to prepare
199 * the skb for sending. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700200 skb_to_dma(skb, skb_headlen(skb), &dma);
201 pr_debug("xfer length %04x (%u)\n", htons(skb->len), skb->len);
202
Rusty Russelle2c97842007-07-26 10:41:03 -0700203 /* This is the actual send call which copies the packet. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700204 lguest_send_dma(peer_key(info, peernum), &dma);
Rusty Russelle2c97842007-07-26 10:41:03 -0700205
206 /* Check that the entire packet was transmitted. If not, it could mean
207 * that the other Guest registered a short receive buffer, but this
208 * driver should never do that. More likely, the peer is dead. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700209 if (dma.used_len != skb->len) {
210 dev->stats.tx_carrier_errors++;
211 pr_debug("Bad xfer to peer %i: %i of %i (dma %p/%i)\n",
212 peernum, dma.used_len, skb->len,
213 (void *)dma.addr[0], dma.len[0]);
214 } else {
Rusty Russelle2c97842007-07-26 10:41:03 -0700215 /* On success we update the stats. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700216 dev->stats.tx_bytes += skb->len;
217 dev->stats.tx_packets++;
218 }
219}
220
Rusty Russelle2c97842007-07-26 10:41:03 -0700221/* Another helper function to tell is if a slot in the device memory is unused.
222 * Since we always set the Local Assignment bit in the ethernet address, the
223 * first byte can never be 0. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700224static int unused_peer(const struct lguest_net peer[], unsigned int num)
225{
226 return peer[num].mac[0] == 0;
227}
228
Rusty Russelle2c97842007-07-26 10:41:03 -0700229/* Finally, here is the routine which handles an outgoing packet. It's called
230 * "start_xmit" for traditional reasons. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700231static int lguestnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
232{
233 unsigned int i;
234 int broadcast;
235 struct lguestnet_info *info = netdev_priv(dev);
Rusty Russelle2c97842007-07-26 10:41:03 -0700236 /* Extract the destination ethernet address from the packet. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700237 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
238
239 pr_debug("%s: xmit %02x:%02x:%02x:%02x:%02x:%02x\n",
240 dev->name, dest[0],dest[1],dest[2],dest[3],dest[4],dest[5]);
241
Rusty Russelle2c97842007-07-26 10:41:03 -0700242 /* If it's a multicast packet, we broadcast to everyone. That's not
243 * very efficient, but there are very few applications which actually
244 * use multicast, which is a shame really.
245 *
246 * As etherdevice.h points out: "By definition the broadcast address is
247 * also a multicast address." So we don't have to test for broadcast
248 * packets separately. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700249 broadcast = is_multicast_ether_addr(dest);
Rusty Russelle2c97842007-07-26 10:41:03 -0700250
251 /* Look through all the published ethernet addresses to see if we
252 * should send this packet. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700253 for (i = 0; i < info->mapsize/sizeof(struct lguest_net); i++) {
Rusty Russelle2c97842007-07-26 10:41:03 -0700254 /* We don't send to ourselves (we actually can't SEND_DMA to
255 * ourselves anyway), and don't send to unused slots.*/
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700256 if (i == info->me || unused_peer(info->peer, i))
257 continue;
258
Rusty Russelle2c97842007-07-26 10:41:03 -0700259 /* If it's broadcast we send it. If they want every packet we
260 * send it. If the destination matches their address we send
261 * it. Otherwise we go to the next peer. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700262 if (!broadcast && !promisc(info, i) && !mac_eq(dest, info, i))
263 continue;
264
265 pr_debug("lguestnet %s: sending from %i to %i\n",
266 dev->name, info->me, i);
Rusty Russelle2c97842007-07-26 10:41:03 -0700267 /* Our routine which actually does the transfer. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700268 transfer_packet(dev, skb, i);
269 }
Rusty Russelle2c97842007-07-26 10:41:03 -0700270
271 /* An xmit routine is expected to dispose of the packet, so we do. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700272 dev_kfree_skb(skb);
Rusty Russelle2c97842007-07-26 10:41:03 -0700273
274 /* As per kernel convention, 0 means success. This is why I love
275 * networking: even if we never sent to anyone, that's still
276 * success! */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700277 return 0;
278}
279
Rusty Russelle2c97842007-07-26 10:41:03 -0700280/*D:560
281 * Packet receiving.
282 *
283 * First, here's a helper routine which fills one of our array of receive
284 * buffers: */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700285static int fill_slot(struct net_device *dev, unsigned int slot)
286{
287 struct lguestnet_info *info = netdev_priv(dev);
Rusty Russelle2c97842007-07-26 10:41:03 -0700288
289 /* We can receive ETH_DATA_LEN (1500) byte packets, plus a standard
290 * ethernet header of ETH_HLEN (14) bytes. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700291 info->skb[slot] = netdev_alloc_skb(dev, ETH_HLEN + ETH_DATA_LEN);
292 if (!info->skb[slot]) {
293 printk("%s: could not fill slot %i\n", dev->name, slot);
294 return -ENOMEM;
295 }
296
Rusty Russelle2c97842007-07-26 10:41:03 -0700297 /* skb_to_dma() is a helper which sets up the "struct lguest_dma" to
298 * point to the data in the skb: we also use it for sending out a
299 * packet. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700300 skb_to_dma(info->skb[slot], ETH_HLEN + ETH_DATA_LEN, &info->dma[slot]);
Rusty Russelle2c97842007-07-26 10:41:03 -0700301
302 /* This is a Write Memory Barrier: it ensures that the entry in the
303 * receive buffer array is written *before* we set the "used_len" entry
304 * to 0. If the Host were looking at the receive buffer array from a
305 * different CPU, it could potentially see "used_len = 0" and not see
306 * the updated receive buffer information. This would be a horribly
307 * nasty bug, so make sure the compiler and CPU know this has to happen
308 * first. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700309 wmb();
Rusty Russelle2c97842007-07-26 10:41:03 -0700310 /* Writing 0 to "used_len" tells the Host it can use this receive
311 * buffer now. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700312 info->dma[slot].used_len = 0;
313 return 0;
314}
315
Rusty Russelle2c97842007-07-26 10:41:03 -0700316/* This is the actual receive routine. When we receive an interrupt from the
317 * Host to tell us a packet has been delivered, we arrive here: */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700318static irqreturn_t lguestnet_rcv(int irq, void *dev_id)
319{
320 struct net_device *dev = dev_id;
321 struct lguestnet_info *info = netdev_priv(dev);
322 unsigned int i, done = 0;
323
Rusty Russelle2c97842007-07-26 10:41:03 -0700324 /* Look through our entire receive array for an entry which has data
325 * in it. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700326 for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
327 unsigned int length;
328 struct sk_buff *skb;
329
330 length = info->dma[i].used_len;
331 if (length == 0)
332 continue;
333
Rusty Russelle2c97842007-07-26 10:41:03 -0700334 /* We've found one! Remember the skb (we grabbed the length
335 * above), and immediately refill the slot we've taken it
336 * from. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700337 done++;
338 skb = info->skb[i];
339 fill_slot(dev, i);
340
Rusty Russelle2c97842007-07-26 10:41:03 -0700341 /* This shouldn't happen: micropackets could be sent by a
342 * badly-behaved Guest on the network, but the Host will never
343 * stuff more data in the buffer than the buffer length. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700344 if (length < ETH_HLEN || length > ETH_HLEN + ETH_DATA_LEN) {
345 pr_debug(KERN_WARNING "%s: unbelievable skb len: %i\n",
346 dev->name, length);
347 dev_kfree_skb(skb);
348 continue;
349 }
350
Rusty Russelle2c97842007-07-26 10:41:03 -0700351 /* skb_put(), what a great function! I've ranted about this
352 * function before (http://lkml.org/lkml/1999/9/26/24). You
353 * call it after you've added data to the end of an skb (in
354 * this case, it was the Host which wrote the data). */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700355 skb_put(skb, length);
Rusty Russelle2c97842007-07-26 10:41:03 -0700356
357 /* The ethernet header contains a protocol field: we use the
358 * standard helper to extract it, and place the result in
359 * skb->protocol. The helper also sets up skb->pkt_type and
360 * eats up the ethernet header from the front of the packet. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700361 skb->protocol = eth_type_trans(skb, dev);
Rusty Russelle2c97842007-07-26 10:41:03 -0700362
363 /* If this device doesn't need checksums for sending, we also
364 * don't need to check the packets when they come in. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700365 if (dev->features & NETIF_F_NO_CSUM)
366 skb->ip_summed = CHECKSUM_UNNECESSARY;
Rusty Russelle2c97842007-07-26 10:41:03 -0700367
368 /* As a last resort for debugging the driver or the lguest I/O
369 * subsystem, you can uncomment the "#define DEBUG" at the top
370 * of this file, which turns all the pr_debug() into printk()
371 * and floods the logs. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700372 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
373 ntohs(skb->protocol), skb->len, skb->pkt_type);
374
Rusty Russelle2c97842007-07-26 10:41:03 -0700375 /* Update the packet and byte counts (visible from ifconfig,
376 * and good for debugging). */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700377 dev->stats.rx_bytes += skb->len;
378 dev->stats.rx_packets++;
Rusty Russelle2c97842007-07-26 10:41:03 -0700379
380 /* Hand our fresh network packet into the stack's "network
381 * interface receive" routine. That will free the packet
382 * itself when it's finished. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700383 netif_rx(skb);
384 }
Rusty Russelle2c97842007-07-26 10:41:03 -0700385
386 /* If we found any packets, we assume the interrupt was for us. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700387 return done ? IRQ_HANDLED : IRQ_NONE;
388}
389
Rusty Russelle2c97842007-07-26 10:41:03 -0700390/*D:550 This is where we start: when the device is brought up by dhcpd or
391 * ifconfig. At this point we advertise our MAC address to the rest of the
392 * network, and register receive buffers ready for incoming packets. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700393static int lguestnet_open(struct net_device *dev)
394{
395 int i;
396 struct lguestnet_info *info = netdev_priv(dev);
397
Rusty Russelle2c97842007-07-26 10:41:03 -0700398 /* Copy our MAC address into the device page, so others on the network
399 * can find us. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700400 memcpy(info->peer[info->me].mac, dev->dev_addr, ETH_ALEN);
401
Rusty Russelle2c97842007-07-26 10:41:03 -0700402 /* We might already be in promisc mode (dev->flags & IFF_PROMISC). Our
403 * set_multicast callback handles this already, so we call it now. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700404 lguestnet_set_multicast(dev);
405
Rusty Russelle2c97842007-07-26 10:41:03 -0700406 /* Allocate packets and put them into our "struct lguest_dma" array.
407 * If we fail to allocate all the packets we could still limp along,
408 * but it's a sign of real stress so we should probably give up now. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700409 for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
410 if (fill_slot(dev, i) != 0)
411 goto cleanup;
412 }
Rusty Russelle2c97842007-07-26 10:41:03 -0700413
414 /* Finally we tell the Host where our array of "struct lguest_dma"
415 * receive buffers is, binding it to the key corresponding to the
416 * device's physical memory plus our peerid. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700417 if (lguest_bind_dma(peer_key(info,info->me), info->dma,
418 NUM_SKBS, lgdev_irq(info->lgdev)) != 0)
419 goto cleanup;
420 return 0;
421
422cleanup:
423 while (--i >= 0)
424 dev_kfree_skb(info->skb[i]);
425 return -ENOMEM;
426}
Rusty Russelle2c97842007-07-26 10:41:03 -0700427/*:*/
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700428
Rusty Russelle2c97842007-07-26 10:41:03 -0700429/* The close routine is called when the device is no longer in use: we clean up
430 * elegantly. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700431static int lguestnet_close(struct net_device *dev)
432{
433 unsigned int i;
434 struct lguestnet_info *info = netdev_priv(dev);
435
Rusty Russelle2c97842007-07-26 10:41:03 -0700436 /* Clear all trace of our existence out of the device memory by setting
437 * the slot which held our MAC address to 0 (unused). */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700438 memset(&info->peer[info->me], 0, sizeof(info->peer[info->me]));
439
Rusty Russelle2c97842007-07-26 10:41:03 -0700440 /* Unregister our array of receive buffers */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700441 lguest_unbind_dma(peer_key(info, info->me), info->dma);
442 for (i = 0; i < ARRAY_SIZE(info->dma); i++)
443 dev_kfree_skb(info->skb[i]);
444 return 0;
445}
446
Rusty Russelle2c97842007-07-26 10:41:03 -0700447/*D:510 The network device probe function is basically a standard ethernet
448 * device setup. It reads the "struct lguest_device_desc" and sets the "struct
449 * net_device". Oh, the line-by-line excitement! Let's skip over it. :*/
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700450static int lguestnet_probe(struct lguest_device *lgdev)
451{
452 int err, irqf = IRQF_SHARED;
453 struct net_device *dev;
454 struct lguestnet_info *info;
455 struct lguest_device_desc *desc = &lguest_devices[lgdev->index];
456
457 pr_debug("lguest_net: probing for device %i\n", lgdev->index);
458
459 dev = alloc_etherdev(sizeof(struct lguestnet_info));
460 if (!dev)
461 return -ENOMEM;
462
463 SET_MODULE_OWNER(dev);
464
465 /* Ethernet defaults with some changes */
466 ether_setup(dev);
467 dev->set_mac_address = NULL;
468
469 dev->dev_addr[0] = 0x02; /* set local assignment bit (IEEE802) */
470 dev->dev_addr[1] = 0x00;
471 memcpy(&dev->dev_addr[2], &lguest_data.guestid, 2);
472 dev->dev_addr[4] = 0x00;
473 dev->dev_addr[5] = 0x00;
474
475 dev->open = lguestnet_open;
476 dev->stop = lguestnet_close;
477 dev->hard_start_xmit = lguestnet_start_xmit;
478
Rusty Russelle2c97842007-07-26 10:41:03 -0700479 /* We don't actually support multicast yet, but turning on/off
480 * promisc also calls dev->set_multicast_list. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700481 dev->set_multicast_list = lguestnet_set_multicast;
482 SET_NETDEV_DEV(dev, &lgdev->dev);
Rusty Russelle2c97842007-07-26 10:41:03 -0700483
484 /* The network code complains if you have "scatter-gather" capability
485 * if you don't also handle checksums (it seem that would be
486 * "illogical"). So we use a lie of omission and don't tell it that we
487 * can handle scattered packets unless we also don't want checksums,
488 * even though to us they're completely independent. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700489 if (desc->features & LGUEST_NET_F_NOCSUM)
490 dev->features = NETIF_F_SG|NETIF_F_NO_CSUM;
491
492 info = netdev_priv(dev);
493 info->mapsize = PAGE_SIZE * desc->num_pages;
494 info->peer_phys = ((unsigned long)desc->pfn << PAGE_SHIFT);
495 info->lgdev = lgdev;
496 info->peer = lguest_map(info->peer_phys, desc->num_pages);
497 if (!info->peer) {
498 err = -ENOMEM;
499 goto free;
500 }
501
502 /* This stores our peerid (upper bits reserved for future). */
503 info->me = (desc->features & (info->mapsize-1));
504
505 err = register_netdev(dev);
506 if (err) {
507 pr_debug("lguestnet: registering device failed\n");
508 goto unmap;
509 }
510
511 if (lguest_devices[lgdev->index].features & LGUEST_DEVICE_F_RANDOMNESS)
512 irqf |= IRQF_SAMPLE_RANDOM;
513 if (request_irq(lgdev_irq(lgdev), lguestnet_rcv, irqf, "lguestnet",
514 dev) != 0) {
515 pr_debug("lguestnet: cannot get irq %i\n", lgdev_irq(lgdev));
516 goto unregister;
517 }
518
519 pr_debug("lguestnet: registered device %s\n", dev->name);
Rusty Russelle2c97842007-07-26 10:41:03 -0700520 /* Finally, we put the "struct net_device" in the generic "struct
521 * lguest_device"s private pointer. Again, it's not necessary, but
522 * makes sure the cool kernel kids don't tease us. */
Rusty Russelld503e2fa2007-07-19 01:49:28 -0700523 lgdev->private = dev;
524 return 0;
525
526unregister:
527 unregister_netdev(dev);
528unmap:
529 lguest_unmap(info->peer);
530free:
531 free_netdev(dev);
532 return err;
533}
534
535static struct lguest_driver lguestnet_drv = {
536 .name = "lguestnet",
537 .owner = THIS_MODULE,
538 .device_type = LGUEST_DEVICE_T_NET,
539 .probe = lguestnet_probe,
540};
541
542static __init int lguestnet_init(void)
543{
544 return register_lguest_driver(&lguestnet_drv);
545}
546module_init(lguestnet_init);
547
548MODULE_DESCRIPTION("Lguest network driver");
549MODULE_LICENSE("GPL");
Rusty Russelle2c97842007-07-26 10:41:03 -0700550
551/*D:580
552 * This is the last of the Drivers, and with this we have covered the many and
553 * wonderous and fine (and boring) details of the Guest.
554 *
555 * "make Launcher" beckons, where we answer questions like "Where do Guests
556 * come from?", and "What do you do when someone asks for optimization?"
557 */