Deleted Added
full compact
blkfront.c (181643) blkfront.c (181805)
1/*-
2 * All rights reserved.
3 *
4 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
5 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
8 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
10 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
11 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
14 * SUCH DAMAGE.
15 *
16 */
17
18/*
19 * XenoBSD block device driver
20 */
21
22#include <sys/cdefs.h>
1/*-
2 * All rights reserved.
3 *
4 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
5 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
8 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
10 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
11 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
14 * SUCH DAMAGE.
15 *
16 */
17
18/*
19 * XenoBSD block device driver
20 */
21
22#include <sys/cdefs.h>
23__FBSDID("$FreeBSD: head/sys/dev/xen/blkfront/blkfront.c 181643 2008-08-12 20:01:57Z kmacy $");
23__FBSDID("$FreeBSD: head/sys/dev/xen/blkfront/blkfront.c 181805 2008-08-17 23:33:33Z kmacy $");
24
25#include <sys/param.h>
26#include <sys/systm.h>
27#include <sys/malloc.h>
28#include <sys/kernel.h>
29#include <vm/vm.h>
30#include <vm/pmap.h>
31
32#include <sys/bio.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/module.h>
36
37#include <machine/bus.h>
38#include <sys/rman.h>
39#include <machine/resource.h>
40#include <machine/intr_machdep.h>
41#include <machine/vmparam.h>
42
43#include <machine/xen/hypervisor.h>
44#include <machine/xen/xen-os.h>
45#include <machine/xen/xen_intr.h>
46#include <machine/xen/xenbus.h>
47#include <machine/xen/evtchn.h>
48#include <xen/interface/grant_table.h>
49
50#include <geom/geom_disk.h>
51#include <machine/xen/xenfunc.h>
52#include <xen/gnttab.h>
53
54#include <dev/xen/blkfront/block.h>
55
56#define ASSERT(S) KASSERT(S, (#S))
57/* prototypes */
58struct xb_softc;
59static void xb_startio(struct xb_softc *sc);
60static void connect(struct blkfront_info *);
61static void blkfront_closing(struct xenbus_device *);
62static int blkfront_remove(struct xenbus_device *);
63static int talk_to_backend(struct xenbus_device *, struct blkfront_info *);
64static int setup_blkring(struct xenbus_device *, struct blkfront_info *);
65static void blkif_int(void *);
66#if 0
67static void blkif_restart_queue(void *arg);
68#endif
69static void blkif_recover(struct blkfront_info *);
70static void blkif_completion(struct blk_shadow *);
71static void blkif_free(struct blkfront_info *, int);
72
73#define GRANT_INVALID_REF 0
74#define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
75
76LIST_HEAD(xb_softc_list_head, xb_softc) xbsl_head;
77
78/* Control whether runtime update of vbds is enabled. */
79#define ENABLE_VBD_UPDATE 0
80
81#if ENABLE_VBD_UPDATE
82static void vbd_update(void);
83#endif
84
85
86#define BLKIF_STATE_DISCONNECTED 0
87#define BLKIF_STATE_CONNECTED 1
88#define BLKIF_STATE_SUSPENDED 2
89
90#ifdef notyet
91static char *blkif_state_name[] = {
92 [BLKIF_STATE_DISCONNECTED] = "disconnected",
93 [BLKIF_STATE_CONNECTED] = "connected",
94 [BLKIF_STATE_SUSPENDED] = "closed",
95};
96
97static char * blkif_status_name[] = {
98 [BLKIF_INTERFACE_STATUS_CLOSED] = "closed",
99 [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
100 [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected",
101 [BLKIF_INTERFACE_STATUS_CHANGED] = "changed",
102};
103#endif
104#define WPRINTK(fmt, args...) printf("[XEN] " fmt, ##args)
105#if 0
106#define DPRINTK(fmt, args...) printf("[XEN] %s:%d" fmt ".\n", __FUNCTION__, __LINE__,##args)
107#else
108#define DPRINTK(fmt, args...)
109#endif
110
111static grant_ref_t gref_head;
112#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
113 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
114
115static void kick_pending_request_queues(struct blkfront_info *);
116static int blkif_open(struct disk *dp);
117static int blkif_close(struct disk *dp);
118static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td);
119static int blkif_queue_request(struct bio *bp);
120static void xb_strategy(struct bio *bp);
121
122
123
124/* XXX move to xb_vbd.c when VBD update support is added */
125#define MAX_VBDS 64
126
127#define XBD_SECTOR_SIZE 512 /* XXX: assume for now */
128#define XBD_SECTOR_SHFT 9
129
130static struct mtx blkif_io_lock;
131
24
25#include <sys/param.h>
26#include <sys/systm.h>
27#include <sys/malloc.h>
28#include <sys/kernel.h>
29#include <vm/vm.h>
30#include <vm/pmap.h>
31
32#include <sys/bio.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/module.h>
36
37#include <machine/bus.h>
38#include <sys/rman.h>
39#include <machine/resource.h>
40#include <machine/intr_machdep.h>
41#include <machine/vmparam.h>
42
43#include <machine/xen/hypervisor.h>
44#include <machine/xen/xen-os.h>
45#include <machine/xen/xen_intr.h>
46#include <machine/xen/xenbus.h>
47#include <machine/xen/evtchn.h>
48#include <xen/interface/grant_table.h>
49
50#include <geom/geom_disk.h>
51#include <machine/xen/xenfunc.h>
52#include <xen/gnttab.h>
53
54#include <dev/xen/blkfront/block.h>
55
56#define ASSERT(S) KASSERT(S, (#S))
57/* prototypes */
58struct xb_softc;
59static void xb_startio(struct xb_softc *sc);
60static void connect(struct blkfront_info *);
61static void blkfront_closing(struct xenbus_device *);
62static int blkfront_remove(struct xenbus_device *);
63static int talk_to_backend(struct xenbus_device *, struct blkfront_info *);
64static int setup_blkring(struct xenbus_device *, struct blkfront_info *);
65static void blkif_int(void *);
66#if 0
67static void blkif_restart_queue(void *arg);
68#endif
69static void blkif_recover(struct blkfront_info *);
70static void blkif_completion(struct blk_shadow *);
71static void blkif_free(struct blkfront_info *, int);
72
73#define GRANT_INVALID_REF 0
74#define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, PAGE_SIZE)
75
76LIST_HEAD(xb_softc_list_head, xb_softc) xbsl_head;
77
78/* Control whether runtime update of vbds is enabled. */
79#define ENABLE_VBD_UPDATE 0
80
81#if ENABLE_VBD_UPDATE
82static void vbd_update(void);
83#endif
84
85
86#define BLKIF_STATE_DISCONNECTED 0
87#define BLKIF_STATE_CONNECTED 1
88#define BLKIF_STATE_SUSPENDED 2
89
90#ifdef notyet
91static char *blkif_state_name[] = {
92 [BLKIF_STATE_DISCONNECTED] = "disconnected",
93 [BLKIF_STATE_CONNECTED] = "connected",
94 [BLKIF_STATE_SUSPENDED] = "closed",
95};
96
97static char * blkif_status_name[] = {
98 [BLKIF_INTERFACE_STATUS_CLOSED] = "closed",
99 [BLKIF_INTERFACE_STATUS_DISCONNECTED] = "disconnected",
100 [BLKIF_INTERFACE_STATUS_CONNECTED] = "connected",
101 [BLKIF_INTERFACE_STATUS_CHANGED] = "changed",
102};
103#endif
104#define WPRINTK(fmt, args...) printf("[XEN] " fmt, ##args)
105#if 0
106#define DPRINTK(fmt, args...) printf("[XEN] %s:%d" fmt ".\n", __FUNCTION__, __LINE__,##args)
107#else
108#define DPRINTK(fmt, args...)
109#endif
110
111static grant_ref_t gref_head;
112#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
113 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
114
115static void kick_pending_request_queues(struct blkfront_info *);
116static int blkif_open(struct disk *dp);
117static int blkif_close(struct disk *dp);
118static int blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td);
119static int blkif_queue_request(struct bio *bp);
120static void xb_strategy(struct bio *bp);
121
122
123
124/* XXX move to xb_vbd.c when VBD update support is added */
125#define MAX_VBDS 64
126
127#define XBD_SECTOR_SIZE 512 /* XXX: assume for now */
128#define XBD_SECTOR_SHFT 9
129
130static struct mtx blkif_io_lock;
131
132static unsigned long
133pfn_to_mfn(unsigned long pfn)
132static vm_paddr_t
133pfn_to_mfn(vm_paddr_t pfn)
134{
135 return (phystomach(pfn << PAGE_SHIFT) >> PAGE_SHIFT);
136}
137
138
139int
140xlvbd_add(blkif_sector_t capacity, int unit, uint16_t vdisk_info, uint16_t sector_size,
141 struct blkfront_info *info)
142{
143 struct xb_softc *sc;
144 int error = 0;
145
146 sc = (struct xb_softc *)malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
147 sc->xb_unit = unit;
148 sc->xb_info = info;
149 info->sc = sc;
150
151 memset(&sc->xb_disk, 0, sizeof(sc->xb_disk));
152 sc->xb_disk = disk_alloc();
153 sc->xb_disk->d_unit = unit;
154 sc->xb_disk->d_open = blkif_open;
155 sc->xb_disk->d_close = blkif_close;
156 sc->xb_disk->d_ioctl = blkif_ioctl;
157 sc->xb_disk->d_strategy = xb_strategy;
158 sc->xb_disk->d_name = "xbd";
159 sc->xb_disk->d_drv1 = sc;
160 sc->xb_disk->d_sectorsize = sector_size;
161
162 /* XXX */
163 sc->xb_disk->d_mediasize = capacity << XBD_SECTOR_SHFT;
164#if 0
165 sc->xb_disk->d_maxsize = DFLTPHYS;
166#else /* XXX: xen can't handle large single i/o requests */
167 sc->xb_disk->d_maxsize = 4096;
168#endif
169#ifdef notyet
170 XENPRINTF("attaching device 0x%x unit %d capacity %llu\n",
171 xb_diskinfo[sc->xb_unit].device, sc->xb_unit,
172 sc->xb_disk->d_mediasize);
173#endif
174 sc->xb_disk->d_flags = 0;
175 disk_create(sc->xb_disk, DISK_VERSION_00);
176 bioq_init(&sc->xb_bioq);
177
178 return error;
179}
180
181void
182xlvbd_del(struct blkfront_info *info)
183{
184 struct xb_softc *sc;
185
186 sc = info->sc;
187 disk_destroy(sc->xb_disk);
188}
189/************************ end VBD support *****************/
190
191/*
192 * Read/write routine for a buffer. Finds the proper unit, place it on
193 * the sortq and kick the controller.
194 */
195static void
196xb_strategy(struct bio *bp)
197{
198 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
199
200 /* bogus disk? */
201 if (sc == NULL) {
202 bp->bio_error = EINVAL;
203 bp->bio_flags |= BIO_ERROR;
204 goto bad;
205 }
206
207 DPRINTK("");
208
209 /*
210 * Place it in the queue of disk activities for this disk
211 */
212 mtx_lock(&blkif_io_lock);
213 bioq_disksort(&sc->xb_bioq, bp);
214
215 xb_startio(sc);
216 mtx_unlock(&blkif_io_lock);
217 return;
218
219 bad:
220 /*
221 * Correctly set the bio to indicate a failed tranfer.
222 */
223 bp->bio_resid = bp->bio_bcount;
224 biodone(bp);
225 return;
226}
227
228
229/* Setup supplies the backend dir, virtual device.
230
231We place an event channel and shared frame entries.
232We watch backend to wait if it's ok. */
233static int blkfront_probe(struct xenbus_device *dev,
234 const struct xenbus_device_id *id)
235{
236 int err, vdevice, i;
237 struct blkfront_info *info;
238
239 /* FIXME: Use dynamic device id if this is not set. */
240 err = xenbus_scanf(XBT_NIL, dev->nodename,
241 "virtual-device", "%i", &vdevice);
242 if (err != 1) {
243 xenbus_dev_fatal(dev, err, "reading virtual-device");
134{
135 return (phystomach(pfn << PAGE_SHIFT) >> PAGE_SHIFT);
136}
137
138
139int
140xlvbd_add(blkif_sector_t capacity, int unit, uint16_t vdisk_info, uint16_t sector_size,
141 struct blkfront_info *info)
142{
143 struct xb_softc *sc;
144 int error = 0;
145
146 sc = (struct xb_softc *)malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
147 sc->xb_unit = unit;
148 sc->xb_info = info;
149 info->sc = sc;
150
151 memset(&sc->xb_disk, 0, sizeof(sc->xb_disk));
152 sc->xb_disk = disk_alloc();
153 sc->xb_disk->d_unit = unit;
154 sc->xb_disk->d_open = blkif_open;
155 sc->xb_disk->d_close = blkif_close;
156 sc->xb_disk->d_ioctl = blkif_ioctl;
157 sc->xb_disk->d_strategy = xb_strategy;
158 sc->xb_disk->d_name = "xbd";
159 sc->xb_disk->d_drv1 = sc;
160 sc->xb_disk->d_sectorsize = sector_size;
161
162 /* XXX */
163 sc->xb_disk->d_mediasize = capacity << XBD_SECTOR_SHFT;
164#if 0
165 sc->xb_disk->d_maxsize = DFLTPHYS;
166#else /* XXX: xen can't handle large single i/o requests */
167 sc->xb_disk->d_maxsize = 4096;
168#endif
169#ifdef notyet
170 XENPRINTF("attaching device 0x%x unit %d capacity %llu\n",
171 xb_diskinfo[sc->xb_unit].device, sc->xb_unit,
172 sc->xb_disk->d_mediasize);
173#endif
174 sc->xb_disk->d_flags = 0;
175 disk_create(sc->xb_disk, DISK_VERSION_00);
176 bioq_init(&sc->xb_bioq);
177
178 return error;
179}
180
181void
182xlvbd_del(struct blkfront_info *info)
183{
184 struct xb_softc *sc;
185
186 sc = info->sc;
187 disk_destroy(sc->xb_disk);
188}
189/************************ end VBD support *****************/
190
191/*
192 * Read/write routine for a buffer. Finds the proper unit, place it on
193 * the sortq and kick the controller.
194 */
195static void
196xb_strategy(struct bio *bp)
197{
198 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
199
200 /* bogus disk? */
201 if (sc == NULL) {
202 bp->bio_error = EINVAL;
203 bp->bio_flags |= BIO_ERROR;
204 goto bad;
205 }
206
207 DPRINTK("");
208
209 /*
210 * Place it in the queue of disk activities for this disk
211 */
212 mtx_lock(&blkif_io_lock);
213 bioq_disksort(&sc->xb_bioq, bp);
214
215 xb_startio(sc);
216 mtx_unlock(&blkif_io_lock);
217 return;
218
219 bad:
220 /*
221 * Correctly set the bio to indicate a failed tranfer.
222 */
223 bp->bio_resid = bp->bio_bcount;
224 biodone(bp);
225 return;
226}
227
228
229/* Setup supplies the backend dir, virtual device.
230
231We place an event channel and shared frame entries.
232We watch backend to wait if it's ok. */
233static int blkfront_probe(struct xenbus_device *dev,
234 const struct xenbus_device_id *id)
235{
236 int err, vdevice, i;
237 struct blkfront_info *info;
238
239 /* FIXME: Use dynamic device id if this is not set. */
240 err = xenbus_scanf(XBT_NIL, dev->nodename,
241 "virtual-device", "%i", &vdevice);
242 if (err != 1) {
243 xenbus_dev_fatal(dev, err, "reading virtual-device");
244 return err;
244 printf("couldn't find virtual device");
245 return (err);
245 }
246
247 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT|M_ZERO);
248 if (info == NULL) {
249 xenbus_dev_fatal(dev, ENOMEM, "allocating info structure");
250 return ENOMEM;
251 }
252
253 /*
254 * XXX debug only
255 */
256 for (i = 0; i < sizeof(*info); i++)
257 if (((uint8_t *)info)[i] != 0)
258 panic("non-null memory");
259
260 info->shadow_free = 0;
261 info->xbdev = dev;
262 info->vdevice = vdevice;
263 info->connected = BLKIF_STATE_DISCONNECTED;
264
265 /* work queue needed ? */
266 for (i = 0; i < BLK_RING_SIZE; i++)
267 info->shadow[i].req.id = i+1;
268 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
269
270 /* Front end dir is a number, which is used as the id. */
271 info->handle = strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
272 dev->dev_driver_data = info;
273
274 err = talk_to_backend(dev, info);
275 if (err) {
276 free(info, M_DEVBUF);
277 dev->dev_driver_data = NULL;
278 return err;
279 }
280
281 return 0;
282}
283
284
285static int blkfront_resume(struct xenbus_device *dev)
286{
287 struct blkfront_info *info = dev->dev_driver_data;
288 int err;
289
290 DPRINTK("blkfront_resume: %s\n", dev->nodename);
291
292 blkif_free(info, 1);
293
294 err = talk_to_backend(dev, info);
295 if (!err)
296 blkif_recover(info);
297
298 return err;
299}
300
301/* Common code used when first setting up, and when resuming. */
302static int talk_to_backend(struct xenbus_device *dev,
303 struct blkfront_info *info)
304{
305 const char *message = NULL;
306 struct xenbus_transaction xbt;
307 int err;
308
309 /* Create shared ring, alloc event channel. */
310 err = setup_blkring(dev, info);
311 if (err)
312 goto out;
313
314 again:
315 err = xenbus_transaction_start(&xbt);
316 if (err) {
317 xenbus_dev_fatal(dev, err, "starting transaction");
318 goto destroy_blkring;
319 }
320
321 err = xenbus_printf(xbt, dev->nodename,
322 "ring-ref","%u", info->ring_ref);
323 if (err) {
324 message = "writing ring-ref";
325 goto abort_transaction;
326 }
327 err = xenbus_printf(xbt, dev->nodename,
328 "event-channel", "%u", irq_to_evtchn_port(info->irq));
329 if (err) {
330 message = "writing event-channel";
331 goto abort_transaction;
332 }
333
334 err = xenbus_transaction_end(xbt, 0);
335 if (err) {
336 if (err == -EAGAIN)
337 goto again;
338 xenbus_dev_fatal(dev, err, "completing transaction");
339 goto destroy_blkring;
340 }
341 xenbus_switch_state(dev, XenbusStateInitialised);
342
343 return 0;
344
345 abort_transaction:
346 xenbus_transaction_end(xbt, 1);
347 if (message)
348 xenbus_dev_fatal(dev, err, "%s", message);
349 destroy_blkring:
350 blkif_free(info, 0);
351 out:
352 return err;
353}
354
355static int
356setup_blkring(struct xenbus_device *dev, struct blkfront_info *info)
357{
358 blkif_sring_t *sring;
359 int err;
360
361 info->ring_ref = GRANT_INVALID_REF;
362
363 sring = (blkif_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
364 if (sring == NULL) {
365 xenbus_dev_fatal(dev, ENOMEM, "allocating shared ring");
366 return ENOMEM;
367 }
368 SHARED_RING_INIT(sring);
369 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
370
371 err = xenbus_grant_ring(dev, (vtomach(info->ring.sring) >> PAGE_SHIFT));
372 if (err < 0) {
373 free(sring, M_DEVBUF);
374 info->ring.sring = NULL;
375 goto fail;
376 }
377 info->ring_ref = err;
378
379 err = bind_listening_port_to_irqhandler(dev->otherend_id,
380 "xbd", (driver_intr_t *)blkif_int, info,
381 INTR_TYPE_BIO | INTR_MPSAFE, NULL);
382 if (err <= 0) {
383 xenbus_dev_fatal(dev, err,
384 "bind_evtchn_to_irqhandler failed");
385 goto fail;
386 }
387 info->irq = err;
388
389 return 0;
390 fail:
391 blkif_free(info, 0);
392 return err;
393}
394
395
396/**
397 * Callback received when the backend's state changes.
398 */
399static void backend_changed(struct xenbus_device *dev,
400 XenbusState backend_state)
401{
402 struct blkfront_info *info = dev->dev_driver_data;
403
404 DPRINTK("blkfront:backend_changed.\n");
405
406 switch (backend_state) {
407 case XenbusStateUnknown:
408 case XenbusStateInitialising:
409 case XenbusStateInitWait:
410 case XenbusStateInitialised:
411 case XenbusStateClosed:
412 break;
413
414 case XenbusStateConnected:
415 connect(info);
416 break;
417
418 case XenbusStateClosing:
419 if (info->users > 0)
420 xenbus_dev_error(dev, -EBUSY,
421 "Device in use; refusing to close");
422 else
423 blkfront_closing(dev);
424#ifdef notyet
425 bd = bdget(info->dev);
426 if (bd == NULL)
427 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
428
429 down(&bd->bd_sem);
430 if (info->users > 0)
431 xenbus_dev_error(dev, -EBUSY,
432 "Device in use; refusing to close");
433 else
434 blkfront_closing(dev);
435 up(&bd->bd_sem);
436 bdput(bd);
437#endif
438 }
439}
440
441/*
442** Invoked when the backend is finally 'ready' (and has told produced
443** the details about the physical device - #sectors, size, etc).
444*/
445static void
446connect(struct blkfront_info *info)
447{
448 unsigned long sectors, sector_size;
449 unsigned int binfo;
450 int err;
451
452 if( (info->connected == BLKIF_STATE_CONNECTED) ||
453 (info->connected == BLKIF_STATE_SUSPENDED) )
454 return;
455
456 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
457
458 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
459 "sectors", "%lu", &sectors,
460 "info", "%u", &binfo,
461 "sector-size", "%lu", &sector_size,
462 NULL);
463 if (err) {
464 xenbus_dev_fatal(info->xbdev, err,
465 "reading backend fields at %s",
466 info->xbdev->otherend);
467 return;
468 }
469 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
470 "feature-barrier", "%lu", &info->feature_barrier,
471 NULL);
472 if (err)
473 info->feature_barrier = 0;
474
475 xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
476
477 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
478
479 /* Kick pending requests. */
480 mtx_lock(&blkif_io_lock);
481 info->connected = BLKIF_STATE_CONNECTED;
482 kick_pending_request_queues(info);
483 mtx_unlock(&blkif_io_lock);
484
485#if 0
486 add_disk(info->gd);
487#endif
488}
489
490/**
491 * Handle the change of state of the backend to Closing. We must delete our
492 * device-layer structures now, to ensure that writes are flushed through to
493 * the backend. Once is this done, we can switch to Closed in
494 * acknowledgement.
495 */
496static void blkfront_closing(struct xenbus_device *dev)
497{
498 struct blkfront_info *info = dev->dev_driver_data;
499
500 DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
501
502 if (info->mi) {
503 DPRINTK("Calling xlvbd_del\n");
504 xlvbd_del(info);
505 info->mi = NULL;
506 }
507
508 xenbus_switch_state(dev, XenbusStateClosed);
509}
510
511
512static int blkfront_remove(struct xenbus_device *dev)
513{
514 struct blkfront_info *info = dev->dev_driver_data;
515
516 DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
517
518 blkif_free(info, 0);
519
520 free(info, M_DEVBUF);
521
522 return 0;
523}
524
525
526static inline int
527GET_ID_FROM_FREELIST(struct blkfront_info *info)
528{
529 unsigned long nfree = info->shadow_free;
530
531 KASSERT(nfree <= BLK_RING_SIZE, ("free %lu > RING_SIZE", nfree));
532 info->shadow_free = info->shadow[nfree].req.id;
533 info->shadow[nfree].req.id = 0x0fffffee; /* debug */
534 return nfree;
535}
536
537static inline void
538ADD_ID_TO_FREELIST(struct blkfront_info *info, unsigned long id)
539{
540 info->shadow[id].req.id = info->shadow_free;
541 info->shadow[id].request = 0;
542 info->shadow_free = id;
543}
544
545static inline void
546flush_requests(struct blkfront_info *info)
547{
548 int notify;
549
550 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
551
552 if (notify)
553 notify_remote_via_irq(info->irq);
554}
555
556static void
557kick_pending_request_queues(struct blkfront_info *info)
558{
559 /* XXX check if we can't simplify */
560#if 0
561 if (!RING_FULL(&info->ring)) {
562 /* Re-enable calldowns. */
563 blk_start_queue(info->rq);
564 /* Kick things off immediately. */
565 do_blkif_request(info->rq);
566 }
567#endif
568 if (!RING_FULL(&info->ring)) {
569#if 0
570 sc = LIST_FIRST(&xbsl_head);
571 LIST_REMOVE(sc, entry);
572 /* Re-enable calldowns. */
573 blk_start_queue(di->rq);
574#endif
575 /* Kick things off immediately. */
576 xb_startio(info->sc);
577 }
578}
579
580#if 0
581/* XXX */
582static void blkif_restart_queue(void *arg)
583{
584 struct blkfront_info *info = (struct blkfront_info *)arg;
585
586 mtx_lock(&blkif_io_lock);
587 kick_pending_request_queues(info);
588 mtx_unlock(&blkif_io_lock);
589}
590#endif
591
592static void blkif_restart_queue_callback(void *arg)
593{
594#if 0
595 struct blkfront_info *info = (struct blkfront_info *)arg;
596 /* XXX BSD equiv ? */
597
598 schedule_work(&info->work);
599#endif
600}
601
602static int
603blkif_open(struct disk *dp)
604{
605 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
606
607 if (sc == NULL) {
608 printk("xb%d: not found", sc->xb_unit);
609 return (ENXIO);
610 }
611
612 sc->xb_flags |= XB_OPEN;
613 sc->xb_info->users++;
614 return (0);
615}
616
617static int
618blkif_close(struct disk *dp)
619{
620 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
621
622 if (sc == NULL)
623 return (ENXIO);
624 sc->xb_flags &= ~XB_OPEN;
625 if (--(sc->xb_info->users) == 0) {
626 /* Check whether we have been instructed to close. We will
627 have ignored this request initially, as the device was
628 still mounted. */
629 struct xenbus_device * dev = sc->xb_info->xbdev;
630 XenbusState state = xenbus_read_driver_state(dev->otherend);
631
632 if (state == XenbusStateClosing)
633 blkfront_closing(dev);
634 }
635 return (0);
636}
637
638static int
639blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
640{
641 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
642
643 if (sc == NULL)
644 return (ENXIO);
645
646 return (ENOTTY);
647}
648
649
650/*
651 * blkif_queue_request
652 *
653 * request block io
654 *
655 * id: for guest use only.
656 * operation: BLKIF_OP_{READ,WRITE,PROBE}
657 * buffer: buffer to read/write into. this should be a
658 * virtual address in the guest os.
659 */
660static int blkif_queue_request(struct bio *bp)
661{
662 caddr_t alignbuf;
246 }
247
248 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT|M_ZERO);
249 if (info == NULL) {
250 xenbus_dev_fatal(dev, ENOMEM, "allocating info structure");
251 return ENOMEM;
252 }
253
254 /*
255 * XXX debug only
256 */
257 for (i = 0; i < sizeof(*info); i++)
258 if (((uint8_t *)info)[i] != 0)
259 panic("non-null memory");
260
261 info->shadow_free = 0;
262 info->xbdev = dev;
263 info->vdevice = vdevice;
264 info->connected = BLKIF_STATE_DISCONNECTED;
265
266 /* work queue needed ? */
267 for (i = 0; i < BLK_RING_SIZE; i++)
268 info->shadow[i].req.id = i+1;
269 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
270
271 /* Front end dir is a number, which is used as the id. */
272 info->handle = strtoul(strrchr(dev->nodename,'/')+1, NULL, 0);
273 dev->dev_driver_data = info;
274
275 err = talk_to_backend(dev, info);
276 if (err) {
277 free(info, M_DEVBUF);
278 dev->dev_driver_data = NULL;
279 return err;
280 }
281
282 return 0;
283}
284
285
286static int blkfront_resume(struct xenbus_device *dev)
287{
288 struct blkfront_info *info = dev->dev_driver_data;
289 int err;
290
291 DPRINTK("blkfront_resume: %s\n", dev->nodename);
292
293 blkif_free(info, 1);
294
295 err = talk_to_backend(dev, info);
296 if (!err)
297 blkif_recover(info);
298
299 return err;
300}
301
302/* Common code used when first setting up, and when resuming. */
303static int talk_to_backend(struct xenbus_device *dev,
304 struct blkfront_info *info)
305{
306 const char *message = NULL;
307 struct xenbus_transaction xbt;
308 int err;
309
310 /* Create shared ring, alloc event channel. */
311 err = setup_blkring(dev, info);
312 if (err)
313 goto out;
314
315 again:
316 err = xenbus_transaction_start(&xbt);
317 if (err) {
318 xenbus_dev_fatal(dev, err, "starting transaction");
319 goto destroy_blkring;
320 }
321
322 err = xenbus_printf(xbt, dev->nodename,
323 "ring-ref","%u", info->ring_ref);
324 if (err) {
325 message = "writing ring-ref";
326 goto abort_transaction;
327 }
328 err = xenbus_printf(xbt, dev->nodename,
329 "event-channel", "%u", irq_to_evtchn_port(info->irq));
330 if (err) {
331 message = "writing event-channel";
332 goto abort_transaction;
333 }
334
335 err = xenbus_transaction_end(xbt, 0);
336 if (err) {
337 if (err == -EAGAIN)
338 goto again;
339 xenbus_dev_fatal(dev, err, "completing transaction");
340 goto destroy_blkring;
341 }
342 xenbus_switch_state(dev, XenbusStateInitialised);
343
344 return 0;
345
346 abort_transaction:
347 xenbus_transaction_end(xbt, 1);
348 if (message)
349 xenbus_dev_fatal(dev, err, "%s", message);
350 destroy_blkring:
351 blkif_free(info, 0);
352 out:
353 return err;
354}
355
356static int
357setup_blkring(struct xenbus_device *dev, struct blkfront_info *info)
358{
359 blkif_sring_t *sring;
360 int err;
361
362 info->ring_ref = GRANT_INVALID_REF;
363
364 sring = (blkif_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
365 if (sring == NULL) {
366 xenbus_dev_fatal(dev, ENOMEM, "allocating shared ring");
367 return ENOMEM;
368 }
369 SHARED_RING_INIT(sring);
370 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
371
372 err = xenbus_grant_ring(dev, (vtomach(info->ring.sring) >> PAGE_SHIFT));
373 if (err < 0) {
374 free(sring, M_DEVBUF);
375 info->ring.sring = NULL;
376 goto fail;
377 }
378 info->ring_ref = err;
379
380 err = bind_listening_port_to_irqhandler(dev->otherend_id,
381 "xbd", (driver_intr_t *)blkif_int, info,
382 INTR_TYPE_BIO | INTR_MPSAFE, NULL);
383 if (err <= 0) {
384 xenbus_dev_fatal(dev, err,
385 "bind_evtchn_to_irqhandler failed");
386 goto fail;
387 }
388 info->irq = err;
389
390 return 0;
391 fail:
392 blkif_free(info, 0);
393 return err;
394}
395
396
397/**
398 * Callback received when the backend's state changes.
399 */
400static void backend_changed(struct xenbus_device *dev,
401 XenbusState backend_state)
402{
403 struct blkfront_info *info = dev->dev_driver_data;
404
405 DPRINTK("blkfront:backend_changed.\n");
406
407 switch (backend_state) {
408 case XenbusStateUnknown:
409 case XenbusStateInitialising:
410 case XenbusStateInitWait:
411 case XenbusStateInitialised:
412 case XenbusStateClosed:
413 break;
414
415 case XenbusStateConnected:
416 connect(info);
417 break;
418
419 case XenbusStateClosing:
420 if (info->users > 0)
421 xenbus_dev_error(dev, -EBUSY,
422 "Device in use; refusing to close");
423 else
424 blkfront_closing(dev);
425#ifdef notyet
426 bd = bdget(info->dev);
427 if (bd == NULL)
428 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
429
430 down(&bd->bd_sem);
431 if (info->users > 0)
432 xenbus_dev_error(dev, -EBUSY,
433 "Device in use; refusing to close");
434 else
435 blkfront_closing(dev);
436 up(&bd->bd_sem);
437 bdput(bd);
438#endif
439 }
440}
441
442/*
443** Invoked when the backend is finally 'ready' (and has told produced
444** the details about the physical device - #sectors, size, etc).
445*/
446static void
447connect(struct blkfront_info *info)
448{
449 unsigned long sectors, sector_size;
450 unsigned int binfo;
451 int err;
452
453 if( (info->connected == BLKIF_STATE_CONNECTED) ||
454 (info->connected == BLKIF_STATE_SUSPENDED) )
455 return;
456
457 DPRINTK("blkfront.c:connect:%s.\n", info->xbdev->otherend);
458
459 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
460 "sectors", "%lu", &sectors,
461 "info", "%u", &binfo,
462 "sector-size", "%lu", &sector_size,
463 NULL);
464 if (err) {
465 xenbus_dev_fatal(info->xbdev, err,
466 "reading backend fields at %s",
467 info->xbdev->otherend);
468 return;
469 }
470 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
471 "feature-barrier", "%lu", &info->feature_barrier,
472 NULL);
473 if (err)
474 info->feature_barrier = 0;
475
476 xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
477
478 (void)xenbus_switch_state(info->xbdev, XenbusStateConnected);
479
480 /* Kick pending requests. */
481 mtx_lock(&blkif_io_lock);
482 info->connected = BLKIF_STATE_CONNECTED;
483 kick_pending_request_queues(info);
484 mtx_unlock(&blkif_io_lock);
485
486#if 0
487 add_disk(info->gd);
488#endif
489}
490
491/**
492 * Handle the change of state of the backend to Closing. We must delete our
493 * device-layer structures now, to ensure that writes are flushed through to
494 * the backend. Once is this done, we can switch to Closed in
495 * acknowledgement.
496 */
497static void blkfront_closing(struct xenbus_device *dev)
498{
499 struct blkfront_info *info = dev->dev_driver_data;
500
501 DPRINTK("blkfront_closing: %s removed\n", dev->nodename);
502
503 if (info->mi) {
504 DPRINTK("Calling xlvbd_del\n");
505 xlvbd_del(info);
506 info->mi = NULL;
507 }
508
509 xenbus_switch_state(dev, XenbusStateClosed);
510}
511
512
513static int blkfront_remove(struct xenbus_device *dev)
514{
515 struct blkfront_info *info = dev->dev_driver_data;
516
517 DPRINTK("blkfront_remove: %s removed\n", dev->nodename);
518
519 blkif_free(info, 0);
520
521 free(info, M_DEVBUF);
522
523 return 0;
524}
525
526
527static inline int
528GET_ID_FROM_FREELIST(struct blkfront_info *info)
529{
530 unsigned long nfree = info->shadow_free;
531
532 KASSERT(nfree <= BLK_RING_SIZE, ("free %lu > RING_SIZE", nfree));
533 info->shadow_free = info->shadow[nfree].req.id;
534 info->shadow[nfree].req.id = 0x0fffffee; /* debug */
535 return nfree;
536}
537
538static inline void
539ADD_ID_TO_FREELIST(struct blkfront_info *info, unsigned long id)
540{
541 info->shadow[id].req.id = info->shadow_free;
542 info->shadow[id].request = 0;
543 info->shadow_free = id;
544}
545
546static inline void
547flush_requests(struct blkfront_info *info)
548{
549 int notify;
550
551 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
552
553 if (notify)
554 notify_remote_via_irq(info->irq);
555}
556
557static void
558kick_pending_request_queues(struct blkfront_info *info)
559{
560 /* XXX check if we can't simplify */
561#if 0
562 if (!RING_FULL(&info->ring)) {
563 /* Re-enable calldowns. */
564 blk_start_queue(info->rq);
565 /* Kick things off immediately. */
566 do_blkif_request(info->rq);
567 }
568#endif
569 if (!RING_FULL(&info->ring)) {
570#if 0
571 sc = LIST_FIRST(&xbsl_head);
572 LIST_REMOVE(sc, entry);
573 /* Re-enable calldowns. */
574 blk_start_queue(di->rq);
575#endif
576 /* Kick things off immediately. */
577 xb_startio(info->sc);
578 }
579}
580
581#if 0
582/* XXX */
583static void blkif_restart_queue(void *arg)
584{
585 struct blkfront_info *info = (struct blkfront_info *)arg;
586
587 mtx_lock(&blkif_io_lock);
588 kick_pending_request_queues(info);
589 mtx_unlock(&blkif_io_lock);
590}
591#endif
592
593static void blkif_restart_queue_callback(void *arg)
594{
595#if 0
596 struct blkfront_info *info = (struct blkfront_info *)arg;
597 /* XXX BSD equiv ? */
598
599 schedule_work(&info->work);
600#endif
601}
602
603static int
604blkif_open(struct disk *dp)
605{
606 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
607
608 if (sc == NULL) {
609 printk("xb%d: not found", sc->xb_unit);
610 return (ENXIO);
611 }
612
613 sc->xb_flags |= XB_OPEN;
614 sc->xb_info->users++;
615 return (0);
616}
617
618static int
619blkif_close(struct disk *dp)
620{
621 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
622
623 if (sc == NULL)
624 return (ENXIO);
625 sc->xb_flags &= ~XB_OPEN;
626 if (--(sc->xb_info->users) == 0) {
627 /* Check whether we have been instructed to close. We will
628 have ignored this request initially, as the device was
629 still mounted. */
630 struct xenbus_device * dev = sc->xb_info->xbdev;
631 XenbusState state = xenbus_read_driver_state(dev->otherend);
632
633 if (state == XenbusStateClosing)
634 blkfront_closing(dev);
635 }
636 return (0);
637}
638
639static int
640blkif_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
641{
642 struct xb_softc *sc = (struct xb_softc *)dp->d_drv1;
643
644 if (sc == NULL)
645 return (ENXIO);
646
647 return (ENOTTY);
648}
649
650
651/*
652 * blkif_queue_request
653 *
654 * request block io
655 *
656 * id: for guest use only.
657 * operation: BLKIF_OP_{READ,WRITE,PROBE}
658 * buffer: buffer to read/write into. this should be a
659 * virtual address in the guest os.
660 */
661static int blkif_queue_request(struct bio *bp)
662{
663 caddr_t alignbuf;
663 unsigned long buffer_ma;
664 vm_paddr_t buffer_ma;
664 blkif_request_t *ring_req;
665 unsigned long id;
665 blkif_request_t *ring_req;
666 unsigned long id;
666 unsigned int fsect, lsect;
667 uint64_t fsect, lsect;
667 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
668 struct blkfront_info *info = sc->xb_info;
669 int ref;
670
671 if (unlikely(sc->xb_info->connected != BLKIF_STATE_CONNECTED))
672 return 1;
673
674 if (gnttab_alloc_grant_references(
675 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
676 gnttab_request_free_callback(
677 &info->callback,
678 blkif_restart_queue_callback,
679 info,
680 BLKIF_MAX_SEGMENTS_PER_REQUEST);
681 return 1;
682 }
683
684 /* Check if the buffer is properly aligned */
685 if ((vm_offset_t)bp->bio_data & PAGE_MASK) {
686 int align = (bp->bio_bcount < PAGE_SIZE/2) ? XBD_SECTOR_SIZE :
687 PAGE_SIZE;
688 caddr_t newbuf = malloc(bp->bio_bcount + align, M_DEVBUF,
689 M_NOWAIT);
690
691 alignbuf = (char *)roundup2((u_long)newbuf, align);
692
693 /* save a copy of the current buffer */
694 bp->bio_driver1 = newbuf;
695 bp->bio_driver2 = alignbuf;
696
697 /* Copy the data for a write */
698 if (bp->bio_cmd == BIO_WRITE)
699 bcopy(bp->bio_data, alignbuf, bp->bio_bcount);
700 } else
701 alignbuf = bp->bio_data;
702
703 /* Fill out a communications ring structure. */
704 ring_req = RING_GET_REQUEST(&info->ring,
705 info->ring.req_prod_pvt);
706 id = GET_ID_FROM_FREELIST(info);
707 info->shadow[id].request = (unsigned long)bp;
708
709 ring_req->id = id;
710 ring_req->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ :
711 BLKIF_OP_WRITE;
712
713 ring_req->sector_number= (blkif_sector_t)bp->bio_pblkno;
714 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk;
715
716 ring_req->nr_segments = 0; /* XXX not doing scatter/gather since buffer
717 * chaining is not supported.
718 */
719
720 buffer_ma = vtomach(alignbuf);
721 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
722 lsect = fsect + (bp->bio_bcount >> XBD_SECTOR_SHFT) - 1;
723 /* install a grant reference. */
724 ref = gnttab_claim_grant_reference(&gref_head);
725 KASSERT( ref != -ENOSPC, ("grant_reference failed") );
726
727 gnttab_grant_foreign_access_ref(
728 ref,
729 info->xbdev->otherend_id,
730 buffer_ma >> PAGE_SHIFT,
731 ring_req->operation & 1 ); /* ??? */
732 info->shadow[id].frame[ring_req->nr_segments] =
733 buffer_ma >> PAGE_SHIFT;
734
735 ring_req->seg[ring_req->nr_segments] =
736 (struct blkif_request_segment) {
737 .gref = ref,
738 .first_sect = fsect,
739 .last_sect = lsect };
740
741 ring_req->nr_segments++;
742 KASSERT((buffer_ma & (XBD_SECTOR_SIZE-1)) == 0,
743 ("XEN buffer must be sector aligned"));
744 KASSERT(lsect <= 7,
745 ("XEN disk driver data cannot cross a page boundary"));
746
747 buffer_ma &= ~PAGE_MASK;
748
749 info->ring.req_prod_pvt++;
750
751 /* Keep a private copy so we can reissue requests when recovering. */
752 info->shadow[id].req = *ring_req;
753
754 gnttab_free_grant_references(gref_head);
755
756 return 0;
757}
758
759
760
761/*
762 * Dequeue buffers and place them in the shared communication ring.
763 * Return when no more requests can be accepted or all buffers have
764 * been queued.
765 *
766 * Signal XEN once the ring has been filled out.
767 */
768static void
769xb_startio(struct xb_softc *sc)
770{
771 struct bio *bp;
772 int queued = 0;
773 struct blkfront_info *info = sc->xb_info;
774 DPRINTK("");
775
776 mtx_assert(&blkif_io_lock, MA_OWNED);
777
778 while ((bp = bioq_takefirst(&sc->xb_bioq)) != NULL) {
779
780 if (RING_FULL(&info->ring))
781 goto wait;
782
783 if (blkif_queue_request(bp)) {
784 wait:
785 bioq_insert_head(&sc->xb_bioq, bp);
786 break;
787 }
788 queued++;
789 }
790
791 if (queued != 0)
792 flush_requests(sc->xb_info);
793}
794
795static void
796blkif_int(void *xsc)
797{
798 struct xb_softc *sc = NULL;
799 struct bio *bp;
800 blkif_response_t *bret;
801 RING_IDX i, rp;
802 struct blkfront_info *info = xsc;
803 DPRINTK("");
804
805 TRACE_ENTER;
806
807 mtx_lock(&blkif_io_lock);
808
809 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
810 mtx_unlock(&blkif_io_lock);
811 return;
812 }
813
814 again:
815 rp = info->ring.sring->rsp_prod;
816 rmb(); /* Ensure we see queued responses up to 'rp'. */
817
818 for (i = info->ring.rsp_cons; i != rp; i++) {
819 unsigned long id;
820
821 bret = RING_GET_RESPONSE(&info->ring, i);
822 id = bret->id;
823 bp = (struct bio *)info->shadow[id].request;
824
825 blkif_completion(&info->shadow[id]);
826
827 ADD_ID_TO_FREELIST(info, id);
828
829 switch (bret->operation) {
830 case BLKIF_OP_READ:
831 /* had an unaligned buffer that needs to be copied */
832 if (bp->bio_driver1)
833 bcopy(bp->bio_driver2, bp->bio_data, bp->bio_bcount);
834 /* FALLTHROUGH */
835 case BLKIF_OP_WRITE:
836
837 /* free the copy buffer */
838 if (bp->bio_driver1) {
839 free(bp->bio_driver1, M_DEVBUF);
840 bp->bio_driver1 = NULL;
841 }
842
843 if ( unlikely(bret->status != BLKIF_RSP_OKAY) ) {
668 struct xb_softc *sc = (struct xb_softc *)bp->bio_disk->d_drv1;
669 struct blkfront_info *info = sc->xb_info;
670 int ref;
671
672 if (unlikely(sc->xb_info->connected != BLKIF_STATE_CONNECTED))
673 return 1;
674
675 if (gnttab_alloc_grant_references(
676 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
677 gnttab_request_free_callback(
678 &info->callback,
679 blkif_restart_queue_callback,
680 info,
681 BLKIF_MAX_SEGMENTS_PER_REQUEST);
682 return 1;
683 }
684
685 /* Check if the buffer is properly aligned */
686 if ((vm_offset_t)bp->bio_data & PAGE_MASK) {
687 int align = (bp->bio_bcount < PAGE_SIZE/2) ? XBD_SECTOR_SIZE :
688 PAGE_SIZE;
689 caddr_t newbuf = malloc(bp->bio_bcount + align, M_DEVBUF,
690 M_NOWAIT);
691
692 alignbuf = (char *)roundup2((u_long)newbuf, align);
693
694 /* save a copy of the current buffer */
695 bp->bio_driver1 = newbuf;
696 bp->bio_driver2 = alignbuf;
697
698 /* Copy the data for a write */
699 if (bp->bio_cmd == BIO_WRITE)
700 bcopy(bp->bio_data, alignbuf, bp->bio_bcount);
701 } else
702 alignbuf = bp->bio_data;
703
704 /* Fill out a communications ring structure. */
705 ring_req = RING_GET_REQUEST(&info->ring,
706 info->ring.req_prod_pvt);
707 id = GET_ID_FROM_FREELIST(info);
708 info->shadow[id].request = (unsigned long)bp;
709
710 ring_req->id = id;
711 ring_req->operation = (bp->bio_cmd == BIO_READ) ? BLKIF_OP_READ :
712 BLKIF_OP_WRITE;
713
714 ring_req->sector_number= (blkif_sector_t)bp->bio_pblkno;
715 ring_req->handle = (blkif_vdev_t)(uintptr_t)sc->xb_disk;
716
717 ring_req->nr_segments = 0; /* XXX not doing scatter/gather since buffer
718 * chaining is not supported.
719 */
720
721 buffer_ma = vtomach(alignbuf);
722 fsect = (buffer_ma & PAGE_MASK) >> XBD_SECTOR_SHFT;
723 lsect = fsect + (bp->bio_bcount >> XBD_SECTOR_SHFT) - 1;
724 /* install a grant reference. */
725 ref = gnttab_claim_grant_reference(&gref_head);
726 KASSERT( ref != -ENOSPC, ("grant_reference failed") );
727
728 gnttab_grant_foreign_access_ref(
729 ref,
730 info->xbdev->otherend_id,
731 buffer_ma >> PAGE_SHIFT,
732 ring_req->operation & 1 ); /* ??? */
733 info->shadow[id].frame[ring_req->nr_segments] =
734 buffer_ma >> PAGE_SHIFT;
735
736 ring_req->seg[ring_req->nr_segments] =
737 (struct blkif_request_segment) {
738 .gref = ref,
739 .first_sect = fsect,
740 .last_sect = lsect };
741
742 ring_req->nr_segments++;
743 KASSERT((buffer_ma & (XBD_SECTOR_SIZE-1)) == 0,
744 ("XEN buffer must be sector aligned"));
745 KASSERT(lsect <= 7,
746 ("XEN disk driver data cannot cross a page boundary"));
747
748 buffer_ma &= ~PAGE_MASK;
749
750 info->ring.req_prod_pvt++;
751
752 /* Keep a private copy so we can reissue requests when recovering. */
753 info->shadow[id].req = *ring_req;
754
755 gnttab_free_grant_references(gref_head);
756
757 return 0;
758}
759
760
761
762/*
763 * Dequeue buffers and place them in the shared communication ring.
764 * Return when no more requests can be accepted or all buffers have
765 * been queued.
766 *
767 * Signal XEN once the ring has been filled out.
768 */
769static void
770xb_startio(struct xb_softc *sc)
771{
772 struct bio *bp;
773 int queued = 0;
774 struct blkfront_info *info = sc->xb_info;
775 DPRINTK("");
776
777 mtx_assert(&blkif_io_lock, MA_OWNED);
778
779 while ((bp = bioq_takefirst(&sc->xb_bioq)) != NULL) {
780
781 if (RING_FULL(&info->ring))
782 goto wait;
783
784 if (blkif_queue_request(bp)) {
785 wait:
786 bioq_insert_head(&sc->xb_bioq, bp);
787 break;
788 }
789 queued++;
790 }
791
792 if (queued != 0)
793 flush_requests(sc->xb_info);
794}
795
796static void
797blkif_int(void *xsc)
798{
799 struct xb_softc *sc = NULL;
800 struct bio *bp;
801 blkif_response_t *bret;
802 RING_IDX i, rp;
803 struct blkfront_info *info = xsc;
804 DPRINTK("");
805
806 TRACE_ENTER;
807
808 mtx_lock(&blkif_io_lock);
809
810 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
811 mtx_unlock(&blkif_io_lock);
812 return;
813 }
814
815 again:
816 rp = info->ring.sring->rsp_prod;
817 rmb(); /* Ensure we see queued responses up to 'rp'. */
818
819 for (i = info->ring.rsp_cons; i != rp; i++) {
820 unsigned long id;
821
822 bret = RING_GET_RESPONSE(&info->ring, i);
823 id = bret->id;
824 bp = (struct bio *)info->shadow[id].request;
825
826 blkif_completion(&info->shadow[id]);
827
828 ADD_ID_TO_FREELIST(info, id);
829
830 switch (bret->operation) {
831 case BLKIF_OP_READ:
832 /* had an unaligned buffer that needs to be copied */
833 if (bp->bio_driver1)
834 bcopy(bp->bio_driver2, bp->bio_data, bp->bio_bcount);
835 /* FALLTHROUGH */
836 case BLKIF_OP_WRITE:
837
838 /* free the copy buffer */
839 if (bp->bio_driver1) {
840 free(bp->bio_driver1, M_DEVBUF);
841 bp->bio_driver1 = NULL;
842 }
843
844 if ( unlikely(bret->status != BLKIF_RSP_OKAY) ) {
844 XENPRINTF("Bad return from blkdev data request: %x\n",
845 printf("Bad return from blkdev data request: %x\n",
845 bret->status);
846 bp->bio_flags |= BIO_ERROR;
847 }
848
849 sc = (struct xb_softc *)bp->bio_disk->d_drv1;
850
851 if (bp->bio_flags & BIO_ERROR)
852 bp->bio_error = EIO;
853 else
854 bp->bio_resid = 0;
855
856 biodone(bp);
857 break;
858 default:
859 panic("received invalid operation");
860 break;
861 }
862 }
863
864 info->ring.rsp_cons = i;
865
866 if (i != info->ring.req_prod_pvt) {
867 int more_to_do;
868 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
869 if (more_to_do)
870 goto again;
871 } else {
872 info->ring.sring->rsp_event = i + 1;
873 }
874
875 kick_pending_request_queues(info);
876
877 mtx_unlock(&blkif_io_lock);
878}
879
880static void
881blkif_free(struct blkfront_info *info, int suspend)
882{
883
884/* Prevent new requests being issued until we fix things up. */
885 mtx_lock(&blkif_io_lock);
886 info->connected = suspend ?
887 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
888 mtx_unlock(&blkif_io_lock);
889
890 /* Free resources associated with old device channel. */
891 if (info->ring_ref != GRANT_INVALID_REF) {
892 gnttab_end_foreign_access(info->ring_ref, 0,
893 info->ring.sring);
894 info->ring_ref = GRANT_INVALID_REF;
895 info->ring.sring = NULL;
896 }
897 if (info->irq)
898 unbind_from_irqhandler(info->irq, info);
899 info->irq = 0;
900
901}
902
903static void
904blkif_completion(struct blk_shadow *s)
905{
906 int i;
907
908 for (i = 0; i < s->req.nr_segments; i++)
909 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
910}
911
912static void
913blkif_recover(struct blkfront_info *info)
914{
915 int i, j;
916 blkif_request_t *req;
917 struct blk_shadow *copy;
918
919 /* Stage 1: Make a safe copy of the shadow state. */
920 copy = (struct blk_shadow *)malloc(sizeof(info->shadow), M_DEVBUF, M_NOWAIT|M_ZERO);
921 PANIC_IF(copy == NULL);
922 memcpy(copy, info->shadow, sizeof(info->shadow));
923
924 /* Stage 2: Set up free list. */
925 memset(&info->shadow, 0, sizeof(info->shadow));
926 for (i = 0; i < BLK_RING_SIZE; i++)
927 info->shadow[i].req.id = i+1;
928 info->shadow_free = info->ring.req_prod_pvt;
929 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
930
931 /* Stage 3: Find pending requests and requeue them. */
932 for (i = 0; i < BLK_RING_SIZE; i++) {
933 /* Not in use? */
934 if (copy[i].request == 0)
935 continue;
936
937 /* Grab a request slot and copy shadow state into it. */
938 req = RING_GET_REQUEST(
939 &info->ring, info->ring.req_prod_pvt);
940 *req = copy[i].req;
941
942 /* We get a new request id, and must reset the shadow state. */
943 req->id = GET_ID_FROM_FREELIST(info);
944 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
945
946 /* Rewrite any grant references invalidated by suspend/resume. */
947 for (j = 0; j < req->nr_segments; j++)
948 gnttab_grant_foreign_access_ref(
949 req->seg[j].gref,
950 info->xbdev->otherend_id,
951 pfn_to_mfn(info->shadow[req->id].frame[j]),
952 0 /* assume not readonly */);
953
954 info->shadow[req->id].req = *req;
955
956 info->ring.req_prod_pvt++;
957 }
958
959 free(copy, M_DEVBUF);
960
961 xenbus_switch_state(info->xbdev, XenbusStateConnected);
962
963 /* Now safe for us to use the shared ring */
964 mtx_lock(&blkif_io_lock);
965 info->connected = BLKIF_STATE_CONNECTED;
966 mtx_unlock(&blkif_io_lock);
967
968 /* Send off requeued requests */
969 mtx_lock(&blkif_io_lock);
970 flush_requests(info);
971
972 /* Kick any other new requests queued since we resumed */
973 kick_pending_request_queues(info);
974 mtx_unlock(&blkif_io_lock);
975}
976
977static int
978blkfront_is_ready(struct xenbus_device *dev)
979{
980 struct blkfront_info *info = dev->dev_driver_data;
981
982 return info->is_ready;
983}
984
985static struct xenbus_device_id blkfront_ids[] = {
986 { "vbd" },
987 { "" }
988};
989
990
991static struct xenbus_driver blkfront = {
992 .name = "vbd",
993 .ids = blkfront_ids,
994 .probe = blkfront_probe,
995 .remove = blkfront_remove,
996 .resume = blkfront_resume,
997 .otherend_changed = backend_changed,
998 .is_ready = blkfront_is_ready,
999};
1000
1001
1002
1003static void
1004xenbus_init(void)
1005{
1006 xenbus_register_frontend(&blkfront);
1007}
1008
1009MTX_SYSINIT(ioreq, &blkif_io_lock, "BIO LOCK", MTX_NOWITNESS); /* XXX how does one enroll a lock? */
1010SYSINIT(xbdev, SI_SUB_PSEUDO, SI_ORDER_SECOND, xenbus_init, NULL);
1011
1012
1013/*
1014 * Local variables:
1015 * mode: C
1016 * c-set-style: "BSD"
1017 * c-basic-offset: 8
1018 * tab-width: 4
1019 * indent-tabs-mode: t
1020 * End:
1021 */
846 bret->status);
847 bp->bio_flags |= BIO_ERROR;
848 }
849
850 sc = (struct xb_softc *)bp->bio_disk->d_drv1;
851
852 if (bp->bio_flags & BIO_ERROR)
853 bp->bio_error = EIO;
854 else
855 bp->bio_resid = 0;
856
857 biodone(bp);
858 break;
859 default:
860 panic("received invalid operation");
861 break;
862 }
863 }
864
865 info->ring.rsp_cons = i;
866
867 if (i != info->ring.req_prod_pvt) {
868 int more_to_do;
869 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
870 if (more_to_do)
871 goto again;
872 } else {
873 info->ring.sring->rsp_event = i + 1;
874 }
875
876 kick_pending_request_queues(info);
877
878 mtx_unlock(&blkif_io_lock);
879}
880
881static void
882blkif_free(struct blkfront_info *info, int suspend)
883{
884
885/* Prevent new requests being issued until we fix things up. */
886 mtx_lock(&blkif_io_lock);
887 info->connected = suspend ?
888 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
889 mtx_unlock(&blkif_io_lock);
890
891 /* Free resources associated with old device channel. */
892 if (info->ring_ref != GRANT_INVALID_REF) {
893 gnttab_end_foreign_access(info->ring_ref, 0,
894 info->ring.sring);
895 info->ring_ref = GRANT_INVALID_REF;
896 info->ring.sring = NULL;
897 }
898 if (info->irq)
899 unbind_from_irqhandler(info->irq, info);
900 info->irq = 0;
901
902}
903
904static void
905blkif_completion(struct blk_shadow *s)
906{
907 int i;
908
909 for (i = 0; i < s->req.nr_segments; i++)
910 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
911}
912
913static void
914blkif_recover(struct blkfront_info *info)
915{
916 int i, j;
917 blkif_request_t *req;
918 struct blk_shadow *copy;
919
920 /* Stage 1: Make a safe copy of the shadow state. */
921 copy = (struct blk_shadow *)malloc(sizeof(info->shadow), M_DEVBUF, M_NOWAIT|M_ZERO);
922 PANIC_IF(copy == NULL);
923 memcpy(copy, info->shadow, sizeof(info->shadow));
924
925 /* Stage 2: Set up free list. */
926 memset(&info->shadow, 0, sizeof(info->shadow));
927 for (i = 0; i < BLK_RING_SIZE; i++)
928 info->shadow[i].req.id = i+1;
929 info->shadow_free = info->ring.req_prod_pvt;
930 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
931
932 /* Stage 3: Find pending requests and requeue them. */
933 for (i = 0; i < BLK_RING_SIZE; i++) {
934 /* Not in use? */
935 if (copy[i].request == 0)
936 continue;
937
938 /* Grab a request slot and copy shadow state into it. */
939 req = RING_GET_REQUEST(
940 &info->ring, info->ring.req_prod_pvt);
941 *req = copy[i].req;
942
943 /* We get a new request id, and must reset the shadow state. */
944 req->id = GET_ID_FROM_FREELIST(info);
945 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
946
947 /* Rewrite any grant references invalidated by suspend/resume. */
948 for (j = 0; j < req->nr_segments; j++)
949 gnttab_grant_foreign_access_ref(
950 req->seg[j].gref,
951 info->xbdev->otherend_id,
952 pfn_to_mfn(info->shadow[req->id].frame[j]),
953 0 /* assume not readonly */);
954
955 info->shadow[req->id].req = *req;
956
957 info->ring.req_prod_pvt++;
958 }
959
960 free(copy, M_DEVBUF);
961
962 xenbus_switch_state(info->xbdev, XenbusStateConnected);
963
964 /* Now safe for us to use the shared ring */
965 mtx_lock(&blkif_io_lock);
966 info->connected = BLKIF_STATE_CONNECTED;
967 mtx_unlock(&blkif_io_lock);
968
969 /* Send off requeued requests */
970 mtx_lock(&blkif_io_lock);
971 flush_requests(info);
972
973 /* Kick any other new requests queued since we resumed */
974 kick_pending_request_queues(info);
975 mtx_unlock(&blkif_io_lock);
976}
977
978static int
979blkfront_is_ready(struct xenbus_device *dev)
980{
981 struct blkfront_info *info = dev->dev_driver_data;
982
983 return info->is_ready;
984}
985
986static struct xenbus_device_id blkfront_ids[] = {
987 { "vbd" },
988 { "" }
989};
990
991
992static struct xenbus_driver blkfront = {
993 .name = "vbd",
994 .ids = blkfront_ids,
995 .probe = blkfront_probe,
996 .remove = blkfront_remove,
997 .resume = blkfront_resume,
998 .otherend_changed = backend_changed,
999 .is_ready = blkfront_is_ready,
1000};
1001
1002
1003
1004static void
1005xenbus_init(void)
1006{
1007 xenbus_register_frontend(&blkfront);
1008}
1009
1010MTX_SYSINIT(ioreq, &blkif_io_lock, "BIO LOCK", MTX_NOWITNESS); /* XXX how does one enroll a lock? */
1011SYSINIT(xbdev, SI_SUB_PSEUDO, SI_ORDER_SECOND, xenbus_init, NULL);
1012
1013
1014/*
1015 * Local variables:
1016 * mode: C
1017 * c-set-style: "BSD"
1018 * c-basic-offset: 8
1019 * tab-width: 4
1020 * indent-tabs-mode: t
1021 * End:
1022 */