virtio_block.c (275048) | virtio_block.c (275647) |
---|---|
1/*- 2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * --- 19 unchanged lines hidden (view full) --- 28 * SUCH DAMAGE. 29 */ 30 31/* 32 * BERI virtio block backend driver 33 */ 34 35#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * --- 19 unchanged lines hidden (view full) --- 28 * SUCH DAMAGE. 29 */ 30 31/* 32 * BERI virtio block backend driver 33 */ 34 35#include <sys/cdefs.h> |
36__FBSDID("$FreeBSD: head/sys/dev/beri/virtio/virtio_block.c 275048 2014-11-25 15:58:59Z br $"); | 36__FBSDID("$FreeBSD: head/sys/dev/beri/virtio/virtio_block.c 275647 2014-12-09 16:39:21Z br $"); |
37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/bus.h> 41#include <sys/kernel.h> 42#include <sys/module.h> 43#include <sys/rman.h> 44#include <sys/conf.h> --- 24 unchanged lines hidden (view full) --- 69#include <dev/virtio/block/virtio_blk.h> 70#include <dev/virtio/virtio.h> 71#include <dev/virtio/virtio_ring.h> 72 73#include "pio_if.h" 74 75#define DPRINTF(fmt, ...) 76 | 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/bus.h> 41#include <sys/kernel.h> 42#include <sys/module.h> 43#include <sys/rman.h> 44#include <sys/conf.h> --- 24 unchanged lines hidden (view full) --- 69#include <dev/virtio/block/virtio_blk.h> 70#include <dev/virtio/virtio.h> 71#include <dev/virtio/virtio_ring.h> 72 73#include "pio_if.h" 74 75#define DPRINTF(fmt, ...) 76 |
77/* We use indirect descriptors */ 78#define NUM_DESCS 1 79#define NUM_QUEUES 1 80 81#define VTBLK_BLK_ID_BYTES 20 82#define VTBLK_MAXSEGS 256 83 |
|
77struct beri_vtblk_softc { 78 struct resource *res[1]; 79 bus_space_tag_t bst; 80 bus_space_handle_t bsh; 81 struct cdev *cdev; 82 device_t dev; 83 int opened; 84 device_t pio_recv; --- 196 unchanged lines hidden (view full) --- 281 KASSERT(queue == 0, ("we support single queue only")); 282 283 /* Process new descriptors */ 284 vq = &sc->vs_queues[queue]; 285 vq->vq_save_used = be16toh(vq->vq_used->idx); 286 while (vq_has_descs(vq)) 287 vtblk_proc(sc, vq); 288 | 84struct beri_vtblk_softc { 85 struct resource *res[1]; 86 bus_space_tag_t bst; 87 bus_space_handle_t bsh; 88 struct cdev *cdev; 89 device_t dev; 90 int opened; 91 device_t pio_recv; --- 196 unchanged lines hidden (view full) --- 288 KASSERT(queue == 0, ("we support single queue only")); 289 290 /* Process new descriptors */ 291 vq = &sc->vs_queues[queue]; 292 vq->vq_save_used = be16toh(vq->vq_used->idx); 293 while (vq_has_descs(vq)) 294 vtblk_proc(sc, vq); 295 |
289 /* Interrupt other side */ 290 PIO_SET(sc->pio_send, Q_INTR, 1); | 296 /* Interrupt the other side */ 297 if ((be16toh(vq->vq_avail->flags) & VRING_AVAIL_F_NO_INTERRUPT) == 0) { 298 reg = htobe32(VIRTIO_MMIO_INT_VRING); 299 WRITE4(sc, VIRTIO_MMIO_INTERRUPT_STATUS, reg); 300 PIO_SET(sc->pio_send, Q_INTR, 1); 301 } |
291 292 return (0); 293} 294 295static int 296vq_init(struct beri_vtblk_softc *sc) 297{ 298 struct vqueue_info *vq; 299 uint8_t *base; 300 int size; 301 int reg; 302 int pfn; 303 304 vq = &sc->vs_queues[0]; | 302 303 return (0); 304} 305 306static int 307vq_init(struct beri_vtblk_softc *sc) 308{ 309 struct vqueue_info *vq; 310 uint8_t *base; 311 int size; 312 int reg; 313 int pfn; 314 315 vq = &sc->vs_queues[0]; |
305 vq->vq_qsize = NUM_QUEUES; | 316 vq->vq_qsize = NUM_DESCS; |
306 307 reg = READ4(sc, VIRTIO_MMIO_QUEUE_PFN); 308 pfn = be32toh(reg); 309 vq->vq_pfn = pfn; 310 311 size = vring_size(vq->vq_qsize, VRING_ALIGN); 312 base = paddr_map(sc->beri_mem_offset, 313 (pfn << PAGE_SHIFT), size); --- 34 unchanged lines hidden (view full) --- 348 vtblk_notify(sc); 349 } 350 sx_xunlock(&sc->sc_mtx); 351 352 kthread_exit(); 353} 354 355static int | 317 318 reg = READ4(sc, VIRTIO_MMIO_QUEUE_PFN); 319 pfn = be32toh(reg); 320 vq->vq_pfn = pfn; 321 322 size = vring_size(vq->vq_qsize, VRING_ALIGN); 323 base = paddr_map(sc->beri_mem_offset, 324 (pfn << PAGE_SHIFT), size); --- 34 unchanged lines hidden (view full) --- 359 vtblk_notify(sc); 360 } 361 sx_xunlock(&sc->sc_mtx); 362 363 kthread_exit(); 364} 365 366static int |
356setup_pio(struct beri_vtblk_softc *sc, char *name, device_t *dev) 357{ 358 phandle_t pio_node; 359 struct fdt_ic *ic; 360 phandle_t xref; 361 phandle_t node; 362 363 if ((node = ofw_bus_get_node(sc->dev)) == -1) 364 return (ENXIO); 365 366 if (OF_searchencprop(node, name, &xref, 367 sizeof(xref)) == -1) { 368 return (ENXIO); 369 } 370 371 pio_node = OF_node_from_xref(xref); 372 SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) { 373 if (ic->iph == pio_node) { 374 *dev = ic->dev; 375 PIO_CONFIGURE(*dev, PIO_OUT_ALL, 376 PIO_UNMASK_ALL); 377 return (0); 378 } 379 } 380 381 return (ENXIO); 382} 383 384static int 385setup_offset(struct beri_vtblk_softc *sc) 386{ 387 pcell_t dts_value[2]; 388 phandle_t mem_node; 389 phandle_t xref; 390 phandle_t node; 391 int len; 392 393 if ((node = ofw_bus_get_node(sc->dev)) == -1) 394 return (ENXIO); 395 396 if (OF_searchencprop(node, "beri-mem", &xref, 397 sizeof(xref)) == -1) { 398 return (ENXIO); 399 } 400 401 mem_node = OF_node_from_xref(xref); 402 if ((len = OF_getproplen(mem_node, "reg")) <= 0) 403 return (ENXIO); 404 OF_getencprop(mem_node, "reg", dts_value, len); 405 sc->beri_mem_offset = dts_value[0]; 406 407 return (0); 408} 409 410static int | |
411backend_info(struct beri_vtblk_softc *sc) 412{ 413 struct virtio_blk_config *cfg; 414 uint32_t *s; 415 int reg; 416 int i; 417 418 /* Specify that we provide block device */ 419 reg = htobe32(VIRTIO_ID_BLOCK); 420 WRITE4(sc, VIRTIO_MMIO_DEVICE_ID, reg); 421 | 367backend_info(struct beri_vtblk_softc *sc) 368{ 369 struct virtio_blk_config *cfg; 370 uint32_t *s; 371 int reg; 372 int i; 373 374 /* Specify that we provide block device */ 375 reg = htobe32(VIRTIO_ID_BLOCK); 376 WRITE4(sc, VIRTIO_MMIO_DEVICE_ID, reg); 377 |
422 /* The number of queues we support */ 423 reg = htobe16(NUM_QUEUES); 424 WRITE2(sc, VIRTIO_MMIO_QUEUE_NUM, reg); | 378 /* Queue size */ 379 reg = htobe32(NUM_DESCS); 380 WRITE4(sc, VIRTIO_MMIO_QUEUE_NUM_MAX, reg); |
425 426 /* Our features */ 427 reg = htobe32(VIRTIO_RING_F_INDIRECT_DESC 428 | VIRTIO_BLK_F_BLK_SIZE 429 | VIRTIO_BLK_F_SEG_MAX); 430 WRITE4(sc, VIRTIO_MMIO_HOST_FEATURES, reg); 431 432 cfg = sc->cfg; --- 128 unchanged lines hidden (view full) --- 561 562 error = kthread_add(vtblk_thread, sc, NULL, &sc->vtblk_ktd, 563 0, 0, "beri_virtio_block"); 564 if (error) { 565 device_printf(dev, "cannot create kthread\n"); 566 return (ENXIO); 567 } 568 | 381 382 /* Our features */ 383 reg = htobe32(VIRTIO_RING_F_INDIRECT_DESC 384 | VIRTIO_BLK_F_BLK_SIZE 385 | VIRTIO_BLK_F_SEG_MAX); 386 WRITE4(sc, VIRTIO_MMIO_HOST_FEATURES, reg); 387 388 cfg = sc->cfg; --- 128 unchanged lines hidden (view full) --- 517 518 error = kthread_add(vtblk_thread, sc, NULL, &sc->vtblk_ktd, 519 0, 0, "beri_virtio_block"); 520 if (error) { 521 device_printf(dev, "cannot create kthread\n"); 522 return (ENXIO); 523 } 524 |
569 if (setup_offset(sc) != 0) | 525 if (setup_offset(dev, &sc->beri_mem_offset) != 0) |
570 return (ENXIO); | 526 return (ENXIO); |
571 if (setup_pio(sc, "pio-send", &sc->pio_send) != 0) | 527 if (setup_pio(dev, "pio-send", &sc->pio_send) != 0) |
572 return (ENXIO); | 528 return (ENXIO); |
573 if (setup_pio(sc, "pio-recv", &sc->pio_recv) != 0) | 529 if (setup_pio(dev, "pio-recv", &sc->pio_recv) != 0) |
574 return (ENXIO); 575 576 sc->cdev = make_dev(&beri_cdevsw, 0, UID_ROOT, GID_WHEEL, 577 S_IRWXU, "beri_vtblk"); 578 if (sc->cdev == NULL) { 579 device_printf(dev, "Failed to create character device.\n"); 580 return (ENXIO); 581 } --- 21 unchanged lines hidden --- | 530 return (ENXIO); 531 532 sc->cdev = make_dev(&beri_cdevsw, 0, UID_ROOT, GID_WHEEL, 533 S_IRWXU, "beri_vtblk"); 534 if (sc->cdev == NULL) { 535 device_printf(dev, "Failed to create character device.\n"); 536 return (ENXIO); 537 } --- 21 unchanged lines hidden --- |