aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2007-10-21 21:20:02 -0400
committerRusty Russell <rusty@rustcorp.com.au>2007-10-23 01:49:55 -0400
commit0ca49ca946409f87a8cd0b14d5acb6dea58de6f3 (patch)
tree5f5927f1b0bf46998f4132d3628ae4c51e5ccf5a /drivers
parent0a8a69dd77ddbd4513b21363021ecde7e1025502 (diff)
Remove old lguest bus and drivers.
This gets rid of the lguest bus, drivers and DMA mechanism, to make way for a generic virtio mechanism. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/block/Makefile1
-rw-r--r--drivers/block/lguest_blk.c421
-rw-r--r--drivers/char/Makefile1
-rw-r--r--drivers/char/hvc_lguest.c177
-rw-r--r--drivers/lguest/Kconfig10
-rw-r--r--drivers/lguest/Makefile3
-rw-r--r--drivers/lguest/lguest_bus.c220
-rw-r--r--drivers/lguest/x86/core.c1
-rw-r--r--drivers/net/Makefile1
-rw-r--r--drivers/net/lguest_net.c550
10 files changed, 0 insertions, 1385 deletions
diff --git a/drivers/block/Makefile b/drivers/block/Makefile
index d199eba7a080..7691505a2e12 100644
--- a/drivers/block/Makefile
+++ b/drivers/block/Makefile
@@ -32,4 +32,3 @@ obj-$(CONFIG_BLK_DEV_SX8) += sx8.o
32obj-$(CONFIG_BLK_DEV_UB) += ub.o 32obj-$(CONFIG_BLK_DEV_UB) += ub.o
33 33
34obj-$(CONFIG_XEN_BLKDEV_FRONTEND) += xen-blkfront.o 34obj-$(CONFIG_XEN_BLKDEV_FRONTEND) += xen-blkfront.o
35obj-$(CONFIG_LGUEST_BLOCK) += lguest_blk.o
diff --git a/drivers/block/lguest_blk.c b/drivers/block/lguest_blk.c
deleted file mode 100644
index fa8e42341b87..000000000000
--- a/drivers/block/lguest_blk.c
+++ /dev/null
@@ -1,421 +0,0 @@
1/*D:400
2 * The Guest block driver
3 *
4 * This is a simple block driver, which appears as /dev/lgba, lgbb, lgbc etc.
5 * The mechanism is simple: we place the information about the request in the
6 * device page, then use SEND_DMA (containing the data for a write, or an empty
7 * "ping" DMA for a read).
8 :*/
9/* Copyright 2006 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25//#define DEBUG
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/blkdev.h>
29#include <linux/interrupt.h>
30#include <linux/lguest_bus.h>
31
32static char next_block_index = 'a';
33
34/*D:420 Here is the structure which holds all the information we need about
35 * each Guest block device.
36 *
37 * I'm sure at this stage, you're wondering "hey, where was the adventure I was
38 * promised?" and thinking "Rusty sucks, I shall say nasty things about him on
39 * my blog". I think Real adventures have boring bits, too, and you're in the
40 * middle of one. But it gets better. Just not quite yet. */
41struct blockdev
42{
43 /* The block queue infrastructure wants a spinlock: it is held while it
44 * calls our block request function. We grab it in our interrupt
45 * handler so the responses don't mess with new requests. */
46 spinlock_t lock;
47
48 /* The disk structure registered with kernel. */
49 struct gendisk *disk;
50
51 /* The major device number for this disk, and the interrupt. We only
52 * really keep them here for completeness; we'd need them if we
53 * supported device unplugging. */
54 int major;
55 int irq;
56
57 /* The physical address of this device's memory page */
58 unsigned long phys_addr;
59 /* The mapped memory page for convenient acces. */
60 struct lguest_block_page *lb_page;
61
62 /* We only have a single request outstanding at a time: this is it. */
63 struct lguest_dma dma;
64 struct request *req;
65};
66
67/*D:495 We originally used end_request() throughout the driver, but it turns
68 * out that end_request() is deprecated, and doesn't actually end the request
69 * (which seems like a good reason to deprecate it!). It simply ends the first
70 * bio. So if we had 3 bios in a "struct request" we would do all 3,
71 * end_request(), do 2, end_request(), do 1 and end_request(): twice as much
72 * work as we needed to do.
73 *
74 * This reinforced to me that I do not understand the block layer.
75 *
76 * Nonetheless, Jens Axboe gave me this nice helper to end all chunks of a
77 * request. This improved disk speed by 130%. */
78static void end_entire_request(struct request *req, int uptodate)
79{
80 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
81 BUG();
82 add_disk_randomness(req->rq_disk);
83 blkdev_dequeue_request(req);
84 end_that_request_last(req, uptodate);
85}
86
87/* I'm told there are only two stories in the world worth telling: love and
88 * hate. So there used to be a love scene here like this:
89 *
90 * Launcher: We could make beautiful I/O together, you and I.
91 * Guest: My, that's a big disk!
92 *
93 * Unfortunately, it was just too raunchy for our otherwise-gentle tale. */
94
95/*D:490 This is the interrupt handler, called when a block read or write has
96 * been completed for us. */
97static irqreturn_t lgb_irq(int irq, void *_bd)
98{
99 /* We handed our "struct blockdev" as the argument to request_irq(), so
100 * it is passed through to us here. This tells us which device we're
101 * dealing with in case we have more than one. */
102 struct blockdev *bd = _bd;
103 unsigned long flags;
104
105 /* We weren't doing anything? Strange, but could happen if we shared
106 * interrupts (we don't!). */
107 if (!bd->req) {
108 pr_debug("No work!\n");
109 return IRQ_NONE;
110 }
111
112 /* Not done yet? That's equally strange. */
113 if (!bd->lb_page->result) {
114 pr_debug("No result!\n");
115 return IRQ_NONE;
116 }
117
118 /* We have to grab the lock before ending the request. */
119 spin_lock_irqsave(&bd->lock, flags);
120 /* "result" is 1 for success, 2 for failure: end_entire_request() wants
121 * to know whether this succeeded or not. */
122 end_entire_request(bd->req, bd->lb_page->result == 1);
123 /* Clear out request, it's done. */
124 bd->req = NULL;
125 /* Reset incoming DMA for next time. */
126 bd->dma.used_len = 0;
127 /* Ready for more reads or writes */
128 blk_start_queue(bd->disk->queue);
129 spin_unlock_irqrestore(&bd->lock, flags);
130
131 /* The interrupt was for us, we dealt with it. */
132 return IRQ_HANDLED;
133}
134
135/*D:480 The block layer's "struct request" contains a number of "struct bio"s,
136 * each of which contains "struct bio_vec"s, each of which contains a page, an
137 * offset and a length.
138 *
139 * Fortunately there are iterators to help us walk through the "struct
140 * request". Even more fortunately, there were plenty of places to steal the
141 * code from. We pack the "struct request" into our "struct lguest_dma" and
142 * return the total length. */
143static unsigned int req_to_dma(struct request *req, struct lguest_dma *dma)
144{
145 unsigned int i = 0, len = 0;
146 struct req_iterator iter;
147 struct bio_vec *bvec;
148
149 rq_for_each_segment(bvec, req, iter) {
150 /* We told the block layer not to give us too many. */
151 BUG_ON(i == LGUEST_MAX_DMA_SECTIONS);
152 /* If we had a zero-length segment, it would look like
153 * the end of the data referred to by the "struct
154 * lguest_dma", so make sure that doesn't happen. */
155 BUG_ON(!bvec->bv_len);
156 /* Convert page & offset to a physical address */
157 dma->addr[i] = page_to_phys(bvec->bv_page)
158 + bvec->bv_offset;
159 dma->len[i] = bvec->bv_len;
160 len += bvec->bv_len;
161 i++;
162 }
163 /* If the array isn't full, we mark the end with a 0 length */
164 if (i < LGUEST_MAX_DMA_SECTIONS)
165 dma->len[i] = 0;
166 return len;
167}
168
169/* This creates an empty DMA, useful for prodding the Host without sending data
170 * (ie. when we want to do a read) */
171static void empty_dma(struct lguest_dma *dma)
172{
173 dma->len[0] = 0;
174}
175
176/*D:470 Setting up a request is fairly easy: */
177static void setup_req(struct blockdev *bd,
178 int type, struct request *req, struct lguest_dma *dma)
179{
180 /* The type is 1 (write) or 0 (read). */
181 bd->lb_page->type = type;
182 /* The sector on disk where the read or write starts. */
183 bd->lb_page->sector = req->sector;
184 /* The result is initialized to 0 (unfinished). */
185 bd->lb_page->result = 0;
186 /* The current request (so we can end it in the interrupt handler). */
187 bd->req = req;
188 /* The number of bytes: returned as a side-effect of req_to_dma(),
189 * which packs the block layer's "struct request" into our "struct
190 * lguest_dma" */
191 bd->lb_page->bytes = req_to_dma(req, dma);
192}
193
194/*D:450 Write is pretty straightforward: we pack the request into a "struct
195 * lguest_dma", then use SEND_DMA to send the request. */
196static void do_write(struct blockdev *bd, struct request *req)
197{
198 struct lguest_dma send;
199
200 pr_debug("lgb: WRITE sector %li\n", (long)req->sector);
201 setup_req(bd, 1, req, &send);
202
203 lguest_send_dma(bd->phys_addr, &send);
204}
205
206/* Read is similar to write, except we pack the request into our receive
207 * "struct lguest_dma" and send through an empty DMA just to tell the Host that
208 * there's a request pending. */
209static void do_read(struct blockdev *bd, struct request *req)
210{
211 struct lguest_dma ping;
212
213 pr_debug("lgb: READ sector %li\n", (long)req->sector);
214 setup_req(bd, 0, req, &bd->dma);
215
216 empty_dma(&ping);
217 lguest_send_dma(bd->phys_addr, &ping);
218}
219
220/*D:440 This where requests come in: we get handed the request queue and are
221 * expected to pull a "struct request" off it until we've finished them or
222 * we're waiting for a reply: */
223static void do_lgb_request(struct request_queue *q)
224{
225 struct blockdev *bd;
226 struct request *req;
227
228again:
229 /* This sometimes returns NULL even on the very first time around. I
230 * wonder if it's something to do with letting elves handle the request
231 * queue... */
232 req = elv_next_request(q);
233 if (!req)
234 return;
235
236 /* We attached the struct blockdev to the disk: get it back */
237 bd = req->rq_disk->private_data;
238 /* Sometimes we get repeated requests after blk_stop_queue(), but we
239 * can only handle one at a time. */
240 if (bd->req)
241 return;
242
243 /* We only do reads and writes: no tricky business! */
244 if (!blk_fs_request(req)) {
245 pr_debug("Got non-command 0x%08x\n", req->cmd_type);
246 req->errors++;
247 end_entire_request(req, 0);
248 goto again;
249 }
250
251 if (rq_data_dir(req) == WRITE)
252 do_write(bd, req);
253 else
254 do_read(bd, req);
255
256 /* We've put out the request, so stop any more coming in until we get
257 * an interrupt, which takes us to lgb_irq() to re-enable the queue. */
258 blk_stop_queue(q);
259}
260
261/*D:430 This is the "struct block_device_operations" we attach to the disk at
262 * the end of lguestblk_probe(). It doesn't seem to want much. */
263static struct block_device_operations lguestblk_fops = {
264 .owner = THIS_MODULE,
265};
266
267/*D:425 Setting up a disk device seems to involve a lot of code. I'm not sure
268 * quite why. I do know that the IDE code sent two or three of the maintainers
269 * insane, perhaps this is the fringe of the same disease?
270 *
271 * As in the console code, the probe function gets handed the generic
272 * lguest_device from lguest_bus.c: */
273static int lguestblk_probe(struct lguest_device *lgdev)
274{
275 struct blockdev *bd;
276 int err;
277 int irqflags = IRQF_SHARED;
278
279 /* First we allocate our own "struct blockdev" and initialize the easy
280 * fields. */
281 bd = kmalloc(sizeof(*bd), GFP_KERNEL);
282 if (!bd)
283 return -ENOMEM;
284
285 spin_lock_init(&bd->lock);
286 bd->irq = lgdev_irq(lgdev);
287 bd->req = NULL;
288 bd->dma.used_len = 0;
289 bd->dma.len[0] = 0;
290 /* The descriptor in the lguest_devices array provided by the Host
291 * gives the Guest the physical page number of the device's page. */
292 bd->phys_addr = (lguest_devices[lgdev->index].pfn << PAGE_SHIFT);
293
294 /* We use lguest_map() to get a pointer to the device page */
295 bd->lb_page = lguest_map(bd->phys_addr, 1);
296 if (!bd->lb_page) {
297 err = -ENOMEM;
298 goto out_free_bd;
299 }
300
301 /* We need a major device number: 0 means "assign one dynamically". */
302 bd->major = register_blkdev(0, "lguestblk");
303 if (bd->major < 0) {
304 err = bd->major;
305 goto out_unmap;
306 }
307
308 /* This allocates a "struct gendisk" where we pack all the information
309 * about the disk which the rest of Linux sees. The argument is the
310 * number of minor devices desired: we need one minor for the main
311 * disk, and one for each partition. Of course, we can't possibly know
312 * how many partitions are on the disk (add_disk does that).
313 */
314 bd->disk = alloc_disk(16);
315 if (!bd->disk) {
316 err = -ENOMEM;
317 goto out_unregister_blkdev;
318 }
319
320 /* Every disk needs a queue for requests to come in: we set up the
321 * queue with a callback function (the core of our driver) and the lock
322 * to use. */
323 bd->disk->queue = blk_init_queue(do_lgb_request, &bd->lock);
324 if (!bd->disk->queue) {
325 err = -ENOMEM;
326 goto out_put_disk;
327 }
328
329 /* We can only handle a certain number of pointers in our SEND_DMA
330 * call, so we set that with blk_queue_max_hw_segments(). This is not
331 * to be confused with blk_queue_max_phys_segments() of course! I
332 * know, who could possibly confuse the two?
333 *
334 * Well, it's simple to tell them apart: this one seems to work and the
335 * other one didn't. */
336 blk_queue_max_hw_segments(bd->disk->queue, LGUEST_MAX_DMA_SECTIONS);
337
338 /* Due to technical limitations of our Host (and simple coding) we
339 * can't have a single buffer which crosses a page boundary. Tell it
340 * here. This means that our maximum request size is 16
341 * (LGUEST_MAX_DMA_SECTIONS) pages. */
342 blk_queue_segment_boundary(bd->disk->queue, PAGE_SIZE-1);
343
344 /* We name our disk: this becomes the device name when udev does its
345 * magic thing and creates the device node, such as /dev/lgba.
346 * next_block_index is a global which starts at 'a'. Unfortunately
347 * this simple increment logic means that the 27th disk will be called
348 * "/dev/lgb{". In that case, I recommend having at least 29 disks, so
349 * your /dev directory will be balanced. */
350 sprintf(bd->disk->disk_name, "lgb%c", next_block_index++);
351
352 /* We look to the device descriptor again to see if this device's
353 * interrupts are expected to be random. If they are, we tell the irq
354 * subsystem. At the moment this bit is always set. */
355 if (lguest_devices[lgdev->index].features & LGUEST_DEVICE_F_RANDOMNESS)
356 irqflags |= IRQF_SAMPLE_RANDOM;
357
358 /* Now we have the name and irqflags, we can request the interrupt; we
359 * give it the "struct blockdev" we have set up to pass to lgb_irq()
360 * when there is an interrupt. */
361 err = request_irq(bd->irq, lgb_irq, irqflags, bd->disk->disk_name, bd);
362 if (err)
363 goto out_cleanup_queue;
364
365 /* We bind our one-entry DMA pool to the key for this block device so
366 * the Host can reply to our requests. The key is equal to the
367 * physical address of the device's page, which is conveniently
368 * unique. */
369 err = lguest_bind_dma(bd->phys_addr, &bd->dma, 1, bd->irq);
370 if (err)
371 goto out_free_irq;
372
373 /* We finish our disk initialization and add the disk to the system. */
374 bd->disk->major = bd->major;
375 bd->disk->first_minor = 0;
376 bd->disk->private_data = bd;
377 bd->disk->fops = &lguestblk_fops;
378 /* This is initialized to the disk size by the Launcher. */
379 set_capacity(bd->disk, bd->lb_page->num_sectors);
380 add_disk(bd->disk);
381
382 printk(KERN_INFO "%s: device %i at major %d\n",
383 bd->disk->disk_name, lgdev->index, bd->major);
384
385 /* We don't need to keep the "struct blockdev" around, but if we ever
386 * implemented device removal, we'd need this. */
387 lgdev->private = bd;
388 return 0;
389
390out_free_irq:
391 free_irq(bd->irq, bd);
392out_cleanup_queue:
393 blk_cleanup_queue(bd->disk->queue);
394out_put_disk:
395 put_disk(bd->disk);
396out_unregister_blkdev:
397 unregister_blkdev(bd->major, "lguestblk");
398out_unmap:
399 lguest_unmap(bd->lb_page);
400out_free_bd:
401 kfree(bd);
402 return err;
403}
404
405/*D:410 The boilerplate code for registering the lguest block driver is just
406 * like the console: */
407static struct lguest_driver lguestblk_drv = {
408 .name = "lguestblk",
409 .owner = THIS_MODULE,
410 .device_type = LGUEST_DEVICE_T_BLOCK,
411 .probe = lguestblk_probe,
412};
413
414static __init int lguestblk_init(void)
415{
416 return register_lguest_driver(&lguestblk_drv);
417}
418module_init(lguestblk_init);
419
420MODULE_DESCRIPTION("Lguest block driver");
421MODULE_LICENSE("GPL");
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 057c8bbd7723..07304d50e0cb 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -42,7 +42,6 @@ obj-$(CONFIG_SYNCLINK_GT) += synclink_gt.o
42obj-$(CONFIG_N_HDLC) += n_hdlc.o 42obj-$(CONFIG_N_HDLC) += n_hdlc.o
43obj-$(CONFIG_AMIGA_BUILTIN_SERIAL) += amiserial.o 43obj-$(CONFIG_AMIGA_BUILTIN_SERIAL) += amiserial.o
44obj-$(CONFIG_SX) += sx.o generic_serial.o 44obj-$(CONFIG_SX) += sx.o generic_serial.o
45obj-$(CONFIG_LGUEST_GUEST) += hvc_lguest.o
46obj-$(CONFIG_RIO) += rio/ generic_serial.o 45obj-$(CONFIG_RIO) += rio/ generic_serial.o
47obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o hvsi.o 46obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o hvsi.o
48obj-$(CONFIG_HVC_ISERIES) += hvc_iseries.o 47obj-$(CONFIG_HVC_ISERIES) += hvc_iseries.o
diff --git a/drivers/char/hvc_lguest.c b/drivers/char/hvc_lguest.c
deleted file mode 100644
index efccb2155830..000000000000
--- a/drivers/char/hvc_lguest.c
+++ /dev/null
@@ -1,177 +0,0 @@
1/*D:300
2 * The Guest console driver
3 *
4 * This is a trivial console driver: we use lguest's DMA mechanism to send
5 * bytes out, and register a DMA buffer to receive bytes in. It is assumed to
6 * be present and available from the very beginning of boot.
7 *
8 * Writing console drivers is one of the few remaining Dark Arts in Linux.
9 * Fortunately for us, the path of virtual consoles has been well-trodden by
10 * the PowerPC folks, who wrote "hvc_console.c" to generically support any
11 * virtual console. We use that infrastructure which only requires us to write
12 * the basic put_chars and get_chars functions and call the right register
13 * functions.
14 :*/
15
16/*M:002 The console can be flooded: while the Guest is processing input the
17 * Host can send more. Buffering in the Host could alleviate this, but it is a
18 * difficult problem in general. :*/
19/* Copyright (C) 2006 Rusty Russell, IBM Corporation
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 */
35#include <linux/err.h>
36#include <linux/init.h>
37#include <linux/lguest_bus.h>
38#include <asm/paravirt.h>
39#include "hvc_console.h"
40
41/*D:340 This is our single console input buffer, with associated "struct
42 * lguest_dma" referring to it. Note the 0-terminated length array, and the
43 * use of physical address for the buffer itself. */
44static char inbuf[256];
45static struct lguest_dma cons_input = { .used_len = 0,
46 .addr[0] = __pa(inbuf),
47 .len[0] = sizeof(inbuf),
48 .len[1] = 0 };
49
50/*D:310 The put_chars() callback is pretty straightforward.
51 *
52 * First we put the pointer and length in a "struct lguest_dma": we only have
53 * one pointer, so we set the second length to 0. Then we use SEND_DMA to send
54 * the data to (Host) buffers attached to the console key. Usually a device's
55 * key is a physical address within the device's memory, but because the
56 * console device doesn't have any associated physical memory, we use the
57 * LGUEST_CONSOLE_DMA_KEY constant (aka 0). */
58static int put_chars(u32 vtermno, const char *buf, int count)
59{
60 struct lguest_dma dma;
61
62 /* FIXME: DMA buffers in a "struct lguest_dma" are not allowed
63 * to go over page boundaries. This never seems to happen,
64 * but if it did we'd need to fix this code. */
65 dma.len[0] = count;
66 dma.len[1] = 0;
67 dma.addr[0] = __pa(buf);
68
69 lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma);
70 /* We're expected to return the amount of data we wrote: all of it. */
71 return count;
72}
73
74/*D:350 get_chars() is the callback from the hvc_console infrastructure when
75 * an interrupt is received.
76 *
77 * Firstly we see if our buffer has been filled: if not, we return. The rest
78 * of the code deals with the fact that the hvc_console() infrastructure only
79 * asks us for 16 bytes at a time. We keep a "cons_offset" variable for
80 * partially-read buffers. */
81static int get_chars(u32 vtermno, char *buf, int count)
82{
83 static int cons_offset;
84
85 /* Nothing left to see here... */
86 if (!cons_input.used_len)
87 return 0;
88
89 /* You want more than we have to give? Well, try wanting less! */
90 if (cons_input.used_len - cons_offset < count)
91 count = cons_input.used_len - cons_offset;
92
93 /* Copy across to their buffer and increment offset. */
94 memcpy(buf, inbuf + cons_offset, count);
95 cons_offset += count;
96
97 /* Finished? Zero offset, and reset cons_input so Host will use it
98 * again. */
99 if (cons_offset == cons_input.used_len) {
100 cons_offset = 0;
101 cons_input.used_len = 0;
102 }
103 return count;
104}
105/*:*/
106
107static struct hv_ops lguest_cons = {
108 .get_chars = get_chars,
109 .put_chars = put_chars,
110};
111
112/*D:320 Console drivers are initialized very early so boot messages can go
113 * out. At this stage, the console is output-only. Our driver checks we're a
114 * Guest, and if so hands hvc_instantiate() the console number (0), priority
115 * (0), and the struct hv_ops containing the put_chars() function. */
116static int __init cons_init(void)
117{
118 if (strcmp(pv_info.name, "lguest") != 0)
119 return 0;
120
121 return hvc_instantiate(0, 0, &lguest_cons);
122}
123console_initcall(cons_init);
124
125/*D:370 To set up and manage our virtual console, we call hvc_alloc() and
126 * stash the result in the private pointer of the "struct lguest_device".
127 * Since we never remove the console device we never need this pointer again,
128 * but using ->private is considered good form, and you never know who's going
129 * to copy your driver.
130 *
131 * Once the console is set up, we bind our input buffer ready for input. */
132static int lguestcons_probe(struct lguest_device *lgdev)
133{
134 int err;
135
136 /* The first argument of hvc_alloc() is the virtual console number, so
137 * we use zero. The second argument is the interrupt number.
138 *
139 * The third argument is a "struct hv_ops" containing the put_chars()
140 * and get_chars() pointers. The final argument is the output buffer
141 * size: we use 256 and expect the Host to have room for us to send
142 * that much. */
143 lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256);
144 if (IS_ERR(lgdev->private))
145 return PTR_ERR(lgdev->private);
146
147 /* We bind a single DMA buffer at key LGUEST_CONSOLE_DMA_KEY.
148 * "cons_input" is that statically-initialized global DMA buffer we saw
149 * above, and we also give the interrupt we want. */
150 err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1,
151 lgdev_irq(lgdev));
152 if (err)
153 printk("lguest console: failed to bind buffer.\n");
154 return err;
155}
156/* Note the use of lgdev_irq() for the interrupt number. We tell hvc_alloc()
157 * to expect input when this interrupt is triggered, and then tell
158 * lguest_bind_dma() that is the interrupt to send us when input comes in. */
159
160/*D:360 From now on the console driver follows standard Guest driver form:
161 * register_lguest_driver() registers the device type and probe function, and
162 * the probe function sets up the device.
163 *
164 * The standard "struct lguest_driver": */
165static struct lguest_driver lguestcons_drv = {
166 .name = "lguestcons",
167 .owner = THIS_MODULE,
168 .device_type = LGUEST_DEVICE_T_CONSOLE,
169 .probe = lguestcons_probe,
170};
171
172/* The standard init function */
173static int __init hvc_lguest_init(void)
174{
175 return register_lguest_driver(&lguestcons_drv);
176}
177module_init(hvc_lguest_init);
diff --git a/drivers/lguest/Kconfig b/drivers/lguest/Kconfig
index 3ec5cc803a09..7eb9ecff8f4a 100644
--- a/drivers/lguest/Kconfig
+++ b/drivers/lguest/Kconfig
@@ -17,13 +17,3 @@ config LGUEST_GUEST
17 The guest needs code built-in, even if the host has lguest 17 The guest needs code built-in, even if the host has lguest
18 support as a module. The drivers are tiny, so we build them 18 support as a module. The drivers are tiny, so we build them
19 in too. 19 in too.
20
21config LGUEST_NET
22 tristate
23 default y
24 depends on LGUEST_GUEST && NET
25
26config LGUEST_BLOCK
27 tristate
28 default y
29 depends on LGUEST_GUEST && BLOCK
diff --git a/drivers/lguest/Makefile b/drivers/lguest/Makefile
index d330f5b8c456..8c28236ee1ae 100644
--- a/drivers/lguest/Makefile
+++ b/drivers/lguest/Makefile
@@ -1,6 +1,3 @@
1# Guest requires the bus driver.
2obj-$(CONFIG_LGUEST_GUEST) += lguest_bus.o
3
4# Host requires the other files, which can be a module. 1# Host requires the other files, which can be a module.
5obj-$(CONFIG_LGUEST) += lg.o 2obj-$(CONFIG_LGUEST) += lg.o
6lg-y = core.o hypercalls.o page_tables.o interrupts_and_traps.o \ 3lg-y = core.o hypercalls.o page_tables.o interrupts_and_traps.o \
diff --git a/drivers/lguest/lguest_bus.c b/drivers/lguest/lguest_bus.c
deleted file mode 100644
index 2e9a202be44e..000000000000
--- a/drivers/lguest/lguest_bus.c
+++ /dev/null
@@ -1,220 +0,0 @@
1/*P:050 Lguest guests use a very simple bus for devices. It's a simple array
2 * of device descriptors contained just above the top of normal memory. The
3 * lguest bus is 80% tedious boilerplate code. :*/
4#include <linux/init.h>
5#include <linux/bootmem.h>
6#include <linux/lguest_bus.h>
7#include <asm/io.h>
8#include <asm/paravirt.h>
9
10struct lguest_device_desc *lguest_devices;
11
12static ssize_t type_show(struct device *_dev,
13 struct device_attribute *attr, char *buf)
14{
15 struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
16 return sprintf(buf, "%hu", lguest_devices[dev->index].type);
17}
18static ssize_t features_show(struct device *_dev,
19 struct device_attribute *attr, char *buf)
20{
21 struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
22 return sprintf(buf, "%hx", lguest_devices[dev->index].features);
23}
24static ssize_t pfn_show(struct device *_dev,
25 struct device_attribute *attr, char *buf)
26{
27 struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
28 return sprintf(buf, "%u", lguest_devices[dev->index].pfn);
29}
30static ssize_t status_show(struct device *_dev,
31 struct device_attribute *attr, char *buf)
32{
33 struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
34 return sprintf(buf, "%hx", lguest_devices[dev->index].status);
35}
36static ssize_t status_store(struct device *_dev, struct device_attribute *attr,
37 const char *buf, size_t count)
38{
39 struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
40 if (sscanf(buf, "%hi", &lguest_devices[dev->index].status) != 1)
41 return -EINVAL;
42 return count;
43}
44static struct device_attribute lguest_dev_attrs[] = {
45 __ATTR_RO(type),
46 __ATTR_RO(features),
47 __ATTR_RO(pfn),
48 __ATTR(status, 0644, status_show, status_store),
49 __ATTR_NULL
50};
51
52/*D:130 The generic bus infrastructure requires a function which says whether a
53 * device matches a driver. For us, it is simple: "struct lguest_driver"
54 * contains a "device_type" field which indicates what type of device it can
55 * handle, so we just cast the args and compare: */
56static int lguest_dev_match(struct device *_dev, struct device_driver *_drv)
57{
58 struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
59 struct lguest_driver *drv = container_of(_drv,struct lguest_driver,drv);
60
61 return (drv->device_type == lguest_devices[dev->index].type);
62}
63/*:*/
64
65struct lguest_bus {
66 struct bus_type bus;
67 struct device dev;
68};
69
70static struct lguest_bus lguest_bus = {
71 .bus = {
72 .name = "lguest",
73 .match = lguest_dev_match,
74 .dev_attrs = lguest_dev_attrs,
75 },
76 .dev = {
77 .parent = NULL,
78 .bus_id = "lguest",
79 }
80};
81
82/*D:140 This is the callback which occurs once the bus infrastructure matches
83 * up a device and driver, ie. in response to add_lguest_device() calling
84 * device_register(), or register_lguest_driver() calling driver_register().
85 *
86 * At the moment it's always the latter: the devices are added first, since
87 * scan_devices() is called from a "core_initcall", and the drivers themselves
88 * called later as a normal "initcall". But it would work the other way too.
89 *
90 * So now we have the happy couple, we add the status bit to indicate that we
91 * found a driver. If the driver truly loves the device, it will return
92 * happiness from its probe function (ok, perhaps this wasn't my greatest
93 * analogy), and we set the final "driver ok" bit so the Host sees it's all
94 * green. */
95static int lguest_dev_probe(struct device *_dev)
96{
97 int ret;
98 struct lguest_device*dev = container_of(_dev,struct lguest_device,dev);
99 struct lguest_driver*drv = container_of(dev->dev.driver,
100 struct lguest_driver, drv);
101
102 lguest_devices[dev->index].status |= LGUEST_DEVICE_S_DRIVER;
103 ret = drv->probe(dev);
104 if (ret == 0)
105 lguest_devices[dev->index].status |= LGUEST_DEVICE_S_DRIVER_OK;
106 return ret;
107}
108
109/* The last part of the bus infrastructure is the function lguest drivers use
110 * to register themselves. Firstly, we do nothing if there's no lguest bus
111 * (ie. this is not a Guest), otherwise we fill in the embedded generic "struct
112 * driver" fields and call the generic driver_register(). */
113int register_lguest_driver(struct lguest_driver *drv)
114{
115 if (!lguest_devices)
116 return 0;
117
118 drv->drv.bus = &lguest_bus.bus;
119 drv->drv.name = drv->name;
120 drv->drv.owner = drv->owner;
121 drv->drv.probe = lguest_dev_probe;
122
123 return driver_register(&drv->drv);
124}
125
126/* At the moment we build all the drivers into the kernel because they're so
127 * simple: 8144 bytes for all three of them as I type this. And as the console
128 * really needs to be built in, it's actually only 3527 bytes for the network
129 * and block drivers.
130 *
131 * If they get complex it will make sense for them to be modularized, so we
132 * need to explicitly export the symbol.
133 *
134 * I don't think non-GPL modules make sense, so it's a GPL-only export.
135 */
136EXPORT_SYMBOL_GPL(register_lguest_driver);
137
138/*D:120 This is the core of the lguest bus: actually adding a new device.
139 * It's a separate function because it's neater that way, and because an
140 * earlier version of the code supported hotplug and unplug. They were removed
141 * early on because they were never used.
142 *
143 * As Andrew Tridgell says, "Untested code is buggy code".
144 *
145 * It's worth reading this carefully: we start with an index into the array of
146 * "struct lguest_device_desc"s indicating the device which is new: */
147static void add_lguest_device(unsigned int index)
148{
149 struct lguest_device *new;
150
151 /* Each "struct lguest_device_desc" has a "status" field, which the
152 * Guest updates as the device is probed. In the worst case, the Host
153 * can look at these bits to tell what part of device setup failed,
154 * even if the console isn't available. */
155 lguest_devices[index].status |= LGUEST_DEVICE_S_ACKNOWLEDGE;
156 new = kmalloc(sizeof(struct lguest_device), GFP_KERNEL);
157 if (!new) {
158 printk(KERN_EMERG "Cannot allocate lguest device %u\n", index);
159 lguest_devices[index].status |= LGUEST_DEVICE_S_FAILED;
160 return;
161 }
162
163 /* The "struct lguest_device" setup is pretty straight-forward example
164 * code. */
165 new->index = index;
166 new->private = NULL;
167 memset(&new->dev, 0, sizeof(new->dev));
168 new->dev.parent = &lguest_bus.dev;
169 new->dev.bus = &lguest_bus.bus;
170 sprintf(new->dev.bus_id, "%u", index);
171
172 /* device_register() causes the bus infrastructure to look for a
173 * matching driver. */
174 if (device_register(&new->dev) != 0) {
175 printk(KERN_EMERG "Cannot register lguest device %u\n", index);
176 lguest_devices[index].status |= LGUEST_DEVICE_S_FAILED;
177 kfree(new);
178 }
179}
180
181/*D:110 scan_devices() simply iterates through the device array. The type 0
182 * is reserved to mean "no device", and anything else means we have found a
183 * device: add it. */
184static void scan_devices(void)
185{
186 unsigned int i;
187
188 for (i = 0; i < LGUEST_MAX_DEVICES; i++)
189 if (lguest_devices[i].type)
190 add_lguest_device(i);
191}
192
193/*D:100 Fairly early in boot, lguest_bus_init() is called to set up the lguest
194 * bus. We check that we are a Guest by checking paravirt_ops.name: there are
195 * other ways of checking, but this seems most obvious to me.
196 *
197 * So we can access the array of "struct lguest_device_desc"s easily, we map
198 * that memory and store the pointer in the global "lguest_devices". Then we
199 * register the bus with the core. Doing two registrations seems clunky to me,
200 * but it seems to be the correct sysfs incantation.
201 *
202 * Finally we call scan_devices() which adds all the devices found in the
203 * "struct lguest_device_desc" array. */
204static int __init lguest_bus_init(void)
205{
206 if (strcmp(pv_info.name, "lguest") != 0)
207 return 0;
208
209 /* Devices are in a single page above top of "normal" mem */
210 lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);
211
212 if (bus_register(&lguest_bus.bus) != 0
213 || device_register(&lguest_bus.dev) != 0)
214 panic("lguest bus registration failed");
215
216 scan_devices();
217 return 0;
218}
219/* Do this after core stuff, before devices. */
220postcore_initcall(lguest_bus_init);
diff --git a/drivers/lguest/x86/core.c b/drivers/lguest/x86/core.c
index 39f64c95de18..ef976ccb4192 100644
--- a/drivers/lguest/x86/core.c
+++ b/drivers/lguest/x86/core.c
@@ -29,7 +29,6 @@
29#include <linux/cpu.h> 29#include <linux/cpu.h>
30#include <linux/lguest.h> 30#include <linux/lguest.h>
31#include <linux/lguest_launcher.h> 31#include <linux/lguest_launcher.h>
32#include <linux/lguest_bus.h>
33#include <asm/paravirt.h> 32#include <asm/paravirt.h>
34#include <asm/param.h> 33#include <asm/param.h>
35#include <asm/page.h> 34#include <asm/page.h>
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index 6745feb690ff..593262065c9b 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -183,7 +183,6 @@ obj-$(CONFIG_ZORRO8390) += zorro8390.o
183obj-$(CONFIG_HPLANCE) += hplance.o 7990.o 183obj-$(CONFIG_HPLANCE) += hplance.o 7990.o
184obj-$(CONFIG_MVME147_NET) += mvme147.o 7990.o 184obj-$(CONFIG_MVME147_NET) += mvme147.o 7990.o
185obj-$(CONFIG_EQUALIZER) += eql.o 185obj-$(CONFIG_EQUALIZER) += eql.o
186obj-$(CONFIG_LGUEST_NET) += lguest_net.o
187obj-$(CONFIG_MIPS_JAZZ_SONIC) += jazzsonic.o 186obj-$(CONFIG_MIPS_JAZZ_SONIC) += jazzsonic.o
188obj-$(CONFIG_MIPS_AU1X00_ENET) += au1000_eth.o 187obj-$(CONFIG_MIPS_AU1X00_ENET) += au1000_eth.o
189obj-$(CONFIG_MIPS_SIM_NET) += mipsnet.o 188obj-$(CONFIG_MIPS_SIM_NET) += mipsnet.o
diff --git a/drivers/net/lguest_net.c b/drivers/net/lguest_net.c
deleted file mode 100644
index e255476f224f..000000000000
--- a/drivers/net/lguest_net.c
+++ /dev/null
@@ -1,550 +0,0 @@
1/*D:500
2 * The Guest network driver.
3 *
4 * 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
11 *
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
38/*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
57/*D:530 The "struct lguestnet_info" contains all the information we need to
58 * know about the network device. */
59struct lguestnet_info
60{
61 /* The mapped device page(s) (an array of "struct lguest_net"). */
62 struct lguest_net *peer;
63 /* The physical address of the device page(s) */
64 unsigned long peer_phys;
65 /* The size of the device page(s). */
66 unsigned long mapsize;
67
68 /* The lguest_device I come from */
69 struct lguest_device *lgdev;
70
71 /* My peerid (ie. my slot in the array). */
72 unsigned int me;
73
74 /* Receive queue: the network packets waiting to be filled. */
75 struct sk_buff *skb[NUM_SKBS];
76 struct lguest_dma dma[NUM_SKBS];
77};
78/*:*/
79
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
86/*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. */
90static unsigned long peer_key(struct lguestnet_info *info, unsigned peernum)
91{
92 return info->peer_phys + 4 * peernum;
93}
94
95/* 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. */
103static void skb_to_dma(const struct sk_buff *skb, unsigned int headlen,
104 struct lguest_dma *dma)
105{
106 unsigned int i, seg;
107
108 /* 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: */
111 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 }
116
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. */
121 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) {
125 /* We will end up sending a truncated packet should
126 * this ever happen. Plus, a cool log message! */
127 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 }
133
134 /* If after all that we didn't use the entire "struct lguest_dma"
135 * array, we terminate it with a 0 length. */
136 if (seg < LGUEST_MAX_DMA_SECTIONS)
137 dma->len[seg] = 0;
138}
139
140/*
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 */
156#define PROMISC_BIT 0x01
157
158/* 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. */
162static 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
172/* A simple test function to see if a peer wants to see all packets.*/
173static int promisc(struct lguestnet_info *info, unsigned int peer)
174{
175 return info->peer[peer].mac[0] & PROMISC_BIT;
176}
177
178/* Another simple function to see if a peer's advertised ethernet address
179 * matches a packet's destination ethernet address. */
180static 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
189/* This is the function which actually sends a packet once we've decided a
190 * peer wants it: */
191static 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
198 /* We use our handy "struct lguest_dma" packing function to prepare
199 * the skb for sending. */
200 skb_to_dma(skb, skb_headlen(skb), &dma);
201 pr_debug("xfer length %04x (%u)\n", htons(skb->len), skb->len);
202
203 /* This is the actual send call which copies the packet. */
204 lguest_send_dma(peer_key(info, peernum), &dma);
205
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. */
209 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 {
215 /* On success we update the stats. */
216 dev->stats.tx_bytes += skb->len;
217 dev->stats.tx_packets++;
218 }
219}
220
221/* 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. */
224static int unused_peer(const struct lguest_net peer[], unsigned int num)
225{
226 return peer[num].mac[0] == 0;
227}
228
229/* Finally, here is the routine which handles an outgoing packet. It's called
230 * "start_xmit" for traditional reasons. */
231static 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);
236 /* Extract the destination ethernet address from the packet. */
237 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
238 DECLARE_MAC_BUF(mac);
239
240 pr_debug("%s: xmit %s\n", dev->name, print_mac(mac, dest));
241
242 /* 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. */
249 broadcast = is_multicast_ether_addr(dest);
250
251 /* Look through all the published ethernet addresses to see if we
252 * should send this packet. */
253 for (i = 0; i < info->mapsize/sizeof(struct lguest_net); i++) {
254 /* We don't send to ourselves (we actually can't SEND_DMA to
255 * ourselves anyway), and don't send to unused slots.*/
256 if (i == info->me || unused_peer(info->peer, i))
257 continue;
258
259 /* 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. */
262 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);
267 /* Our routine which actually does the transfer. */
268 transfer_packet(dev, skb, i);
269 }
270
271 /* An xmit routine is expected to dispose of the packet, so we do. */
272 dev_kfree_skb(skb);
273
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! */
277 return 0;
278}
279
280/*D:560
281 * Packet receiving.
282 *
283 * First, here's a helper routine which fills one of our array of receive
284 * buffers: */
285static int fill_slot(struct net_device *dev, unsigned int slot)
286{
287 struct lguestnet_info *info = netdev_priv(dev);
288
289 /* We can receive ETH_DATA_LEN (1500) byte packets, plus a standard
290 * ethernet header of ETH_HLEN (14) bytes. */
291 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
297 /* 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. */
300 skb_to_dma(info->skb[slot], ETH_HLEN + ETH_DATA_LEN, &info->dma[slot]);
301
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. */
309 wmb();
310 /* Writing 0 to "used_len" tells the Host it can use this receive
311 * buffer now. */
312 info->dma[slot].used_len = 0;
313 return 0;
314}
315
316/* 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: */
318static 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
324 /* Look through our entire receive array for an entry which has data
325 * in it. */
326 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
334 /* We've found one! Remember the skb (we grabbed the length
335 * above), and immediately refill the slot we've taken it
336 * from. */
337 done++;
338 skb = info->skb[i];
339 fill_slot(dev, i);
340
341 /* 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. */
344 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
351 /* 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). */
355 skb_put(skb, length);
356
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. */
361 skb->protocol = eth_type_trans(skb, dev);
362
363 /* If this device doesn't need checksums for sending, we also
364 * don't need to check the packets when they come in. */
365 if (dev->features & NETIF_F_NO_CSUM)
366 skb->ip_summed = CHECKSUM_UNNECESSARY;
367
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. */
372 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
373 ntohs(skb->protocol), skb->len, skb->pkt_type);
374
375 /* Update the packet and byte counts (visible from ifconfig,
376 * and good for debugging). */
377 dev->stats.rx_bytes += skb->len;
378 dev->stats.rx_packets++;
379
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. */
383 netif_rx(skb);
384 }
385
386 /* If we found any packets, we assume the interrupt was for us. */
387 return done ? IRQ_HANDLED : IRQ_NONE;
388}
389
390/*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. */
393static int lguestnet_open(struct net_device *dev)
394{
395 int i;
396 struct lguestnet_info *info = netdev_priv(dev);
397
398 /* Copy our MAC address into the device page, so others on the network
399 * can find us. */
400 memcpy(info->peer[info->me].mac, dev->dev_addr, ETH_ALEN);
401
402 /* We might already be in promisc mode (dev->flags & IFF_PROMISC). Our
403 * set_multicast callback handles this already, so we call it now. */
404 lguestnet_set_multicast(dev);
405
406 /* 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. */
409 for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
410 if (fill_slot(dev, i) != 0)
411 goto cleanup;
412 }
413
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. */
417 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}
427/*:*/
428
429/* The close routine is called when the device is no longer in use: we clean up
430 * elegantly. */
431static int lguestnet_close(struct net_device *dev)
432{
433 unsigned int i;
434 struct lguestnet_info *info = netdev_priv(dev);
435
436 /* Clear all trace of our existence out of the device memory by setting
437 * the slot which held our MAC address to 0 (unused). */
438 memset(&info->peer[info->me], 0, sizeof(info->peer[info->me]));
439
440 /* Unregister our array of receive buffers */
441 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
447/*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. :*/
450static 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 /* Ethernet defaults with some changes */
464 ether_setup(dev);
465 dev->set_mac_address = NULL;
466 random_ether_addr(dev->dev_addr);
467
468 dev->open = lguestnet_open;
469 dev->stop = lguestnet_close;
470 dev->hard_start_xmit = lguestnet_start_xmit;
471
472 /* We don't actually support multicast yet, but turning on/off
473 * promisc also calls dev->set_multicast_list. */
474 dev->set_multicast_list = lguestnet_set_multicast;
475 SET_NETDEV_DEV(dev, &lgdev->dev);
476
477 /* The network code complains if you have "scatter-gather" capability
478 * if you don't also handle checksums (it seem that would be
479 * "illogical"). So we use a lie of omission and don't tell it that we
480 * can handle scattered packets unless we also don't want checksums,
481 * even though to us they're completely independent. */
482 if (desc->features & LGUEST_NET_F_NOCSUM)
483 dev->features = NETIF_F_SG|NETIF_F_NO_CSUM;
484
485 info = netdev_priv(dev);
486 info->mapsize = PAGE_SIZE * desc->num_pages;
487 info->peer_phys = ((unsigned long)desc->pfn << PAGE_SHIFT);
488 info->lgdev = lgdev;
489 info->peer = lguest_map(info->peer_phys, desc->num_pages);
490 if (!info->peer) {
491 err = -ENOMEM;
492 goto free;
493 }
494
495 /* This stores our peerid (upper bits reserved for future). */
496 info->me = (desc->features & (info->mapsize-1));
497
498 err = register_netdev(dev);
499 if (err) {
500 pr_debug("lguestnet: registering device failed\n");
501 goto unmap;
502 }
503
504 if (lguest_devices[lgdev->index].features & LGUEST_DEVICE_F_RANDOMNESS)
505 irqf |= IRQF_SAMPLE_RANDOM;
506 if (request_irq(lgdev_irq(lgdev), lguestnet_rcv, irqf, "lguestnet",
507 dev) != 0) {
508 pr_debug("lguestnet: cannot get irq %i\n", lgdev_irq(lgdev));
509 goto unregister;
510 }
511
512 pr_debug("lguestnet: registered device %s\n", dev->name);
513 /* Finally, we put the "struct net_device" in the generic "struct
514 * lguest_device"s private pointer. Again, it's not necessary, but
515 * makes sure the cool kernel kids don't tease us. */
516 lgdev->private = dev;
517 return 0;
518
519unregister:
520 unregister_netdev(dev);
521unmap:
522 lguest_unmap(info->peer);
523free:
524 free_netdev(dev);
525 return err;
526}
527
528static struct lguest_driver lguestnet_drv = {
529 .name = "lguestnet",
530 .owner = THIS_MODULE,
531 .device_type = LGUEST_DEVICE_T_NET,
532 .probe = lguestnet_probe,
533};
534
535static __init int lguestnet_init(void)
536{
537 return register_lguest_driver(&lguestnet_drv);
538}
539module_init(lguestnet_init);
540
541MODULE_DESCRIPTION("Lguest network driver");
542MODULE_LICENSE("GPL");
543
544/*D:580
545 * This is the last of the Drivers, and with this we have covered the many and
546 * wonderous and fine (and boring) details of the Guest.
547 *
548 * "make Launcher" beckons, where we answer questions like "Where do Guests
549 * come from?", and "What do you do when someone asks for optimization?"
550 */