1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) 2007-2008 Samuel Thibault.
4 * (C) Copyright 2020 EPAM Systems Inc.
9 #include <dm/device-internal.h>
13 #include <asm/armv8/mmu.h>
15 #include <asm/xen/system.h>
17 #include <linux/bug.h>
18 #include <linux/compat.h>
20 #include <xen/events.h>
21 #include <xen/gnttab.h>
23 #include <xen/xenbus.h>
25 #include <xen/interface/io/ring.h>
26 #include <xen/interface/io/blkif.h>
27 #include <xen/interface/io/protocols.h>
29 #define DRV_NAME "pvblock"
30 #define DRV_NAME_BLK "pvblock_blk"
34 #define WAIT_RING_TO_MS 10
36 struct blkfront_info {
38 unsigned int sector_size;
46 * struct blkfront_dev - Struct representing blkfront device
48 * @ring: Front_ring structure
49 * @ring_ref: The grant reference, allowing us to grant access
50 * to the ring to the other end/domain
51 * @evtchn: Event channel used to signal ring events
52 * @handle: Events handle
53 * @nodename: Device XenStore path in format "device/vbd/" + @devid
54 * @backend: Backend XenStore path
61 struct blkif_front_ring ring;
68 struct blkfront_info info;
73 struct blkfront_platdata {
78 * struct blkfront_aiocb - AIO сontrol block
79 * @aio_dev: Blockfront device
80 * @aio_buf: Memory buffer, which must be sector-aligned for
82 * @aio_nbytes: Size of AIO, which must be less than @aio_dev
83 * sector-sized amounts
84 * @aio_offset: Offset, which must not go beyond @aio_dev
85 * sector-aligned location
86 * @data: Data used to receiving response from ring
87 * @gref: Array of grant references
88 * @n: Number of segments
89 * @aio_cb: Represents one I/O request.
91 struct blkfront_aiocb {
92 struct blkfront_dev *aio_dev;
98 grant_ref_t gref[BLKIF_MAX_SEGMENTS_PER_REQUEST];
101 void (*aio_cb)(struct blkfront_aiocb *aiocb, int ret);
104 static void blkfront_sync(struct blkfront_dev *dev);
106 static void free_blkfront(struct blkfront_dev *dev)
108 mask_evtchn(dev->evtchn);
111 gnttab_end_access(dev->ring_ref);
112 free(dev->ring.sring);
114 unbind_evtchn(dev->evtchn);
116 free(dev->bounce_buffer);
121 static int init_blkfront(unsigned int devid, struct blkfront_dev *dev)
123 xenbus_transaction_t xbt;
125 char *message = NULL;
126 struct blkif_sring *s;
131 char path[ARRAY_SIZE(nodename) + strlen("/backend-id") + 1];
133 sprintf(nodename, "device/vbd/%d", devid);
135 memset(dev, 0, sizeof(*dev));
136 dev->nodename = strdup(nodename);
139 snprintf(path, sizeof(path), "%s/backend-id", nodename);
140 dev->dom = xenbus_read_integer(path);
141 evtchn_alloc_unbound(dev->dom, NULL, dev, &dev->evtchn);
143 s = (struct blkif_sring *)memalign(PAGE_SIZE, PAGE_SIZE);
145 printf("Failed to allocate shared ring\n");
150 FRONT_RING_INIT(&dev->ring, s, PAGE_SIZE);
152 dev->ring_ref = gnttab_grant_access(dev->dom, virt_to_pfn(s), 0);
155 err = xenbus_transaction_start(&xbt);
157 printf("starting transaction\n");
161 err = xenbus_printf(xbt, nodename, "ring-ref", "%u", dev->ring_ref);
163 message = "writing ring-ref";
164 goto abort_transaction;
166 err = xenbus_printf(xbt, nodename, "event-channel", "%u", dev->evtchn);
168 message = "writing event-channel";
169 goto abort_transaction;
171 err = xenbus_printf(xbt, nodename, "protocol", "%s",
172 XEN_IO_PROTO_ABI_NATIVE);
174 message = "writing protocol";
175 goto abort_transaction;
178 snprintf(path, sizeof(path), "%s/state", nodename);
179 err = xenbus_switch_state(xbt, path, XenbusStateConnected);
181 message = "switching state";
182 goto abort_transaction;
185 err = xenbus_transaction_end(xbt, 0, &retry);
189 printf("completing transaction\n");
196 err = xenbus_transaction_end(xbt, 1, &retry);
197 printf("Abort transaction %s\n", message);
201 snprintf(path, sizeof(path), "%s/backend", nodename);
202 msg = xenbus_read(XBT_NIL, path, &dev->backend);
204 printf("Error %s when reading the backend path %s\n",
209 dev->handle = strtoul(strrchr(nodename, '/') + 1, NULL, 0);
213 char path[strlen(dev->backend) +
214 strlen("/feature-flush-cache") + 1];
216 snprintf(path, sizeof(path), "%s/mode", dev->backend);
217 msg = xenbus_read(XBT_NIL, path, &c);
219 printf("Error %s when reading the mode\n", msg);
223 dev->info.mode = O_RDWR;
225 dev->info.mode = O_RDONLY;
228 snprintf(path, sizeof(path), "%s/state", dev->backend);
231 state = xenbus_read_integer(path);
232 while (!msg && state < XenbusStateConnected)
233 msg = xenbus_wait_for_state_change(path, &state);
234 if (msg || state != XenbusStateConnected) {
235 printf("backend not available, state=%d\n", state);
239 snprintf(path, sizeof(path), "%s/info", dev->backend);
240 dev->info.info = xenbus_read_integer(path);
242 snprintf(path, sizeof(path), "%s/sectors", dev->backend);
244 * FIXME: read_integer returns an int, so disk size
245 * limited to 1TB for now
247 dev->info.sectors = xenbus_read_integer(path);
249 snprintf(path, sizeof(path), "%s/sector-size", dev->backend);
250 dev->info.sector_size = xenbus_read_integer(path);
252 snprintf(path, sizeof(path), "%s/feature-barrier",
254 dev->info.barrier = xenbus_read_integer(path);
256 snprintf(path, sizeof(path), "%s/feature-flush-cache",
258 dev->info.flush = xenbus_read_integer(path);
260 unmask_evtchn(dev->evtchn);
262 dev->bounce_buffer = memalign(dev->info.sector_size,
263 dev->info.sector_size);
264 if (!dev->bounce_buffer) {
265 printf("Failed to allocate bouncing buffer\n");
269 debug("%llu sectors of %u bytes, bounce buffer at %p\n",
270 dev->info.sectors, dev->info.sector_size,
282 static void shutdown_blkfront(struct blkfront_dev *dev)
284 char *err = NULL, *err2;
287 char path[strlen(dev->backend) + strlen("/state") + 1];
288 char nodename[strlen(dev->nodename) + strlen("/event-channel") + 1];
290 debug("Close " DRV_NAME ", device ID %d\n", dev->devid);
294 snprintf(path, sizeof(path), "%s/state", dev->backend);
295 snprintf(nodename, sizeof(nodename), "%s/state", dev->nodename);
297 err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosing);
299 printf("%s: error changing state to %d: %s\n", __func__,
300 XenbusStateClosing, err);
304 state = xenbus_read_integer(path);
305 while (!err && state < XenbusStateClosing)
306 err = xenbus_wait_for_state_change(path, &state);
309 err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed);
311 printf("%s: error changing state to %d: %s\n", __func__,
312 XenbusStateClosed, err);
316 state = xenbus_read_integer(path);
317 while (state < XenbusStateClosed) {
318 err = xenbus_wait_for_state_change(path, &state);
322 err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising);
324 printf("%s: error changing state to %d: %s\n", __func__,
325 XenbusStateInitialising, err);
329 state = xenbus_read_integer(path);
331 (state < XenbusStateInitWait || state >= XenbusStateClosed))
332 err = xenbus_wait_for_state_change(path, &state);
337 snprintf(nodename, sizeof(nodename), "%s/ring-ref", dev->nodename);
338 err2 = xenbus_rm(XBT_NIL, nodename);
340 snprintf(nodename, sizeof(nodename), "%s/event-channel", dev->nodename);
341 err2 = xenbus_rm(XBT_NIL, nodename);
349 * blkfront_aio_poll() - AIO polling function.
350 * @dev: Blkfront device
352 * Here we receive response from the ring and check its status. This happens
353 * until we read all data from the ring. We read the data from consumed pointer
354 * to the response pointer. Then increase consumed pointer to make it clear that
355 * the data has been read.
357 * Return: Number of consumed bytes.
359 static int blkfront_aio_poll(struct blkfront_dev *dev)
362 struct blkif_response *rsp;
367 rp = dev->ring.sring->rsp_prod;
368 rmb(); /* Ensure we see queued responses up to 'rp'. */
369 cons = dev->ring.rsp_cons;
372 while ((cons != rp)) {
373 struct blkfront_aiocb *aiocbp;
376 rsp = RING_GET_RESPONSE(&dev->ring, cons);
379 aiocbp = (void *)(uintptr_t)rsp->id;
380 status = rsp->status;
382 switch (rsp->operation) {
388 if (status != BLKIF_RSP_OKAY)
389 printf("%s error %d on %s at offset %llu, num bytes %llu\n",
390 rsp->operation == BLKIF_OP_READ ?
392 status, aiocbp->aio_dev->nodename,
393 (unsigned long long)aiocbp->aio_offset,
394 (unsigned long long)aiocbp->aio_nbytes);
396 for (j = 0; j < aiocbp->n; j++)
397 gnttab_end_access(aiocbp->gref[j]);
402 case BLKIF_OP_WRITE_BARRIER:
403 if (status != BLKIF_RSP_OKAY)
404 printf("write barrier error %d\n", status);
406 case BLKIF_OP_FLUSH_DISKCACHE:
407 if (status != BLKIF_RSP_OKAY)
408 printf("flush error %d\n", status);
412 printf("unrecognized block operation %d response (status %d)\n",
413 rsp->operation, status);
417 dev->ring.rsp_cons = ++cons;
418 /* Nota: callback frees aiocbp itself */
419 if (aiocbp && aiocbp->aio_cb)
420 aiocbp->aio_cb(aiocbp, status ? -EIO : 0);
421 if (dev->ring.rsp_cons != cons)
422 /* We reentered, we must not continue here */
426 RING_FINAL_CHECK_FOR_RESPONSES(&dev->ring, more);
433 static void blkfront_wait_slot(struct blkfront_dev *dev)
435 /* Wait for a slot */
436 if (RING_FULL(&dev->ring)) {
438 blkfront_aio_poll(dev);
439 if (!RING_FULL(&dev->ring))
441 wait_event_timeout(NULL, !RING_FULL(&dev->ring),
448 * blkfront_aio_poll() - Issue an aio.
449 * @aiocbp: AIO control block structure
450 * @write: Describes is it read or write operation
454 * We check whether the AIO parameters meet the requirements of the device.
455 * Then receive request from ring and define its arguments. After this we
456 * grant access to the grant references. The last step is notifying about AIO
459 static void blkfront_aio(struct blkfront_aiocb *aiocbp, int write)
461 struct blkfront_dev *dev = aiocbp->aio_dev;
462 struct blkif_request *req;
466 uintptr_t start, end;
468 /* Can't io at non-sector-aligned location */
469 BUG_ON(aiocbp->aio_offset & (dev->info.sector_size - 1));
470 /* Can't io non-sector-sized amounts */
471 BUG_ON(aiocbp->aio_nbytes & (dev->info.sector_size - 1));
472 /* Can't io non-sector-aligned buffer */
473 BUG_ON(((uintptr_t)aiocbp->aio_buf & (dev->info.sector_size - 1)));
475 start = (uintptr_t)aiocbp->aio_buf & PAGE_MASK;
476 end = ((uintptr_t)aiocbp->aio_buf + aiocbp->aio_nbytes +
477 PAGE_SIZE - 1) & PAGE_MASK;
478 n = (end - start) / PAGE_SIZE;
481 BUG_ON(n > BLKIF_MAX_SEGMENTS_PER_REQUEST);
483 blkfront_wait_slot(dev);
484 i = dev->ring.req_prod_pvt;
485 req = RING_GET_REQUEST(&dev->ring, i);
487 req->operation = write ? BLKIF_OP_WRITE : BLKIF_OP_READ;
488 req->nr_segments = n;
489 req->handle = dev->handle;
490 req->id = (uintptr_t)aiocbp;
491 req->sector_number = aiocbp->aio_offset / dev->info.sector_size;
493 for (j = 0; j < n; j++) {
494 req->seg[j].first_sect = 0;
495 req->seg[j].last_sect = PAGE_SIZE / dev->info.sector_size - 1;
497 req->seg[0].first_sect = ((uintptr_t)aiocbp->aio_buf & ~PAGE_MASK) /
498 dev->info.sector_size;
499 req->seg[n - 1].last_sect = (((uintptr_t)aiocbp->aio_buf +
500 aiocbp->aio_nbytes - 1) & ~PAGE_MASK) / dev->info.sector_size;
501 for (j = 0; j < n; j++) {
502 uintptr_t data = start + j * PAGE_SIZE;
505 /* Trigger CoW if needed */
506 *(char *)(data + (req->seg[j].first_sect *
507 dev->info.sector_size)) = 0;
510 req->seg[j].gref = gnttab_grant_access(dev->dom,
511 virt_to_pfn((void *)data),
513 aiocbp->gref[j] = req->seg[j].gref;
516 dev->ring.req_prod_pvt = i + 1;
519 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->ring, notify);
522 notify_remote_via_evtchn(dev->evtchn);
525 static void blkfront_aio_cb(struct blkfront_aiocb *aiocbp, int ret)
527 aiocbp->data = (void *)1;
528 aiocbp->aio_cb = NULL;
531 static void blkfront_io(struct blkfront_aiocb *aiocbp, int write)
533 aiocbp->aio_cb = blkfront_aio_cb;
534 blkfront_aio(aiocbp, write);
538 blkfront_aio_poll(aiocbp->aio_dev);
545 static void blkfront_push_operation(struct blkfront_dev *dev, u8 op,
548 struct blkif_request *req;
551 blkfront_wait_slot(dev);
552 i = dev->ring.req_prod_pvt;
553 req = RING_GET_REQUEST(&dev->ring, i);
555 req->nr_segments = 0;
556 req->handle = dev->handle;
558 req->sector_number = 0;
559 dev->ring.req_prod_pvt = i + 1;
561 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->ring, notify);
563 notify_remote_via_evtchn(dev->evtchn);
566 static void blkfront_sync(struct blkfront_dev *dev)
568 if (dev->info.mode == O_RDWR) {
569 if (dev->info.barrier == 1)
570 blkfront_push_operation(dev,
571 BLKIF_OP_WRITE_BARRIER, 0);
573 if (dev->info.flush == 1)
574 blkfront_push_operation(dev,
575 BLKIF_OP_FLUSH_DISKCACHE, 0);
579 blkfront_aio_poll(dev);
580 if (RING_FREE_REQUESTS(&dev->ring) == RING_SIZE(&dev->ring))
587 * pvblock_iop() - Issue an aio.
588 * @udev: Pvblock device
589 * @blknr: Block number to read from / write to
590 * @blkcnt: Amount of blocks to read / write
591 * @buffer: Memory buffer with data to be read / write
592 * @write: Describes is it read or write operation
596 * Depending on the operation - reading or writing, data is read / written from the
597 * specified address (@buffer) to the sector (@blknr).
599 static ulong pvblock_iop(struct udevice *udev, lbaint_t blknr,
600 lbaint_t blkcnt, void *buffer, int write)
602 struct blkfront_dev *blk_dev = dev_get_priv(udev);
603 struct blk_desc *desc = dev_get_uclass_platdata(udev);
604 struct blkfront_aiocb aiocb;
605 lbaint_t blocks_todo;
611 if ((blknr + blkcnt) > desc->lba) {
612 printf(DRV_NAME ": block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
613 blknr + blkcnt, desc->lba);
617 unaligned = (uintptr_t)buffer & (blk_dev->info.sector_size - 1);
619 aiocb.aio_dev = blk_dev;
620 aiocb.aio_offset = blknr * desc->blksz;
623 blocks_todo = blkcnt;
625 aiocb.aio_buf = unaligned ? blk_dev->bounce_buffer : buffer;
627 if (write && unaligned)
628 memcpy(blk_dev->bounce_buffer, buffer, desc->blksz);
630 aiocb.aio_nbytes = unaligned ? desc->blksz :
631 min((size_t)(BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE),
632 (size_t)(blocks_todo * desc->blksz));
634 blkfront_io(&aiocb, write);
636 if (!write && unaligned)
637 memcpy(buffer, blk_dev->bounce_buffer, desc->blksz);
639 aiocb.aio_offset += aiocb.aio_nbytes;
640 buffer += aiocb.aio_nbytes;
641 blocks_todo -= aiocb.aio_nbytes / desc->blksz;
642 } while (blocks_todo > 0);
647 ulong pvblock_blk_read(struct udevice *udev, lbaint_t blknr, lbaint_t blkcnt,
650 return pvblock_iop(udev, blknr, blkcnt, buffer, 0);
653 ulong pvblock_blk_write(struct udevice *udev, lbaint_t blknr, lbaint_t blkcnt,
656 return pvblock_iop(udev, blknr, blkcnt, (void *)buffer, 1);
659 static int pvblock_blk_bind(struct udevice *udev)
661 struct blk_desc *desc = dev_get_uclass_platdata(udev);
664 desc->if_type = IF_TYPE_PVBLOCK;
666 * Initialize the devnum to -ENODEV. This is to make sure that
667 * blk_next_free_devnum() works as expected, since the default
668 * value 0 is a valid devnum.
670 desc->devnum = -ENODEV;
671 devnum = blk_next_free_devnum(IF_TYPE_PVBLOCK);
674 desc->devnum = devnum;
675 desc->part_type = PART_TYPE_UNKNOWN;
678 strncpy(desc->vendor, "Xen", sizeof(desc->vendor));
679 strncpy(desc->revision, "1", sizeof(desc->revision));
680 strncpy(desc->product, "Virtual disk", sizeof(desc->product));
685 static int pvblock_blk_probe(struct udevice *udev)
687 struct blkfront_dev *blk_dev = dev_get_priv(udev);
688 struct blkfront_platdata *platdata = dev_get_platdata(udev);
689 struct blk_desc *desc = dev_get_uclass_platdata(udev);
692 devid = platdata->devid;
695 ret = init_blkfront(devid, blk_dev);
699 desc->blksz = blk_dev->info.sector_size;
700 desc->lba = blk_dev->info.sectors;
701 desc->log2blksz = LOG2(blk_dev->info.sector_size);
706 static int pvblock_blk_remove(struct udevice *udev)
708 struct blkfront_dev *blk_dev = dev_get_priv(udev);
710 shutdown_blkfront(blk_dev);
714 static const struct blk_ops pvblock_blk_ops = {
715 .read = pvblock_blk_read,
716 .write = pvblock_blk_write,
719 U_BOOT_DRIVER(pvblock_blk) = {
720 .name = DRV_NAME_BLK,
722 .ops = &pvblock_blk_ops,
723 .bind = pvblock_blk_bind,
724 .probe = pvblock_blk_probe,
725 .remove = pvblock_blk_remove,
726 .priv_auto = sizeof(struct blkfront_dev),
727 .flags = DM_FLAG_OS_PREPARE,
730 /*******************************************************************************
731 * Para-virtual block device class
732 *******************************************************************************/
734 typedef int (*enum_vbd_callback)(struct udevice *parent, unsigned int devid);
736 static int on_new_vbd(struct udevice *parent, unsigned int devid)
738 struct driver_info info;
739 struct udevice *udev;
740 struct blkfront_platdata *platdata;
743 debug("New " DRV_NAME_BLK ", device ID %d\n", devid);
745 platdata = malloc(sizeof(struct blkfront_platdata));
747 printf("Failed to allocate platform data\n");
751 platdata->devid = devid;
753 info.name = DRV_NAME_BLK;
754 info.platdata = platdata;
756 ret = device_bind_by_name(parent, false, &info, &udev);
758 printf("Failed to bind " DRV_NAME_BLK " to device with ID %d, ret: %d\n",
765 static int xenbus_enumerate_vbd(struct udevice *udev, enum_vbd_callback clb)
770 msg = xenbus_ls(XBT_NIL, "device/vbd", &dirs);
772 printf("Failed to read device/vbd directory: %s\n", msg);
777 for (i = 0; dirs[i]; i++) {
780 sscanf(dirs[i], "%d", &devid);
781 ret = clb(udev, devid);
796 static void print_pvblock_devices(void)
798 struct udevice *udev;
800 const char *class_name;
802 class_name = uclass_get_name(UCLASS_PVBLOCK);
803 for (blk_first_device(IF_TYPE_PVBLOCK, &udev); udev;
804 blk_next_device(&udev), first = false) {
805 struct blk_desc *desc = dev_get_uclass_platdata(udev);
809 printf("%s: %d", class_name, desc->devnum);
814 void pvblock_init(void)
816 struct driver_info info;
817 struct udevice *udev;
822 * At this point Xen drivers have already initialized,
823 * so we can instantiate the class driver and enumerate
824 * virtual block devices.
826 info.name = DRV_NAME;
827 ret = device_bind_by_name(gd->dm_root, false, &info, &udev);
829 printf("Failed to bind " DRV_NAME ", ret: %d\n", ret);
831 /* Bootstrap virtual block devices class driver */
832 ret = uclass_get(UCLASS_PVBLOCK, &uc);
835 uclass_foreach_dev_probe(UCLASS_PVBLOCK, udev);
837 print_pvblock_devices();
840 static int pvblock_probe(struct udevice *udev)
845 if (xenbus_enumerate_vbd(udev, on_new_vbd) < 0)
848 ret = uclass_get(UCLASS_BLK, &uc);
851 uclass_foreach_dev_probe(UCLASS_BLK, udev) {
858 U_BOOT_DRIVER(pvblock_drv) = {
860 .id = UCLASS_PVBLOCK,
861 .probe = pvblock_probe,
864 UCLASS_DRIVER(pvblock) = {
866 .id = UCLASS_PVBLOCK,