blob: 4ad548d599d59753a7a0a09d0d22e2ef1d8f1c60 [file] [log] [blame]
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +03003 * (C) 2007-2008 Samuel Thibault.
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +03004 * (C) Copyright 2020 EPAM Systems Inc.
5 */
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006
7#define LOG_CATEGORY UCLASS_PVBLOCK
8
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +03009#include <blk.h>
10#include <common.h>
11#include <dm.h>
12#include <dm/device-internal.h>
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +030013#include <malloc.h>
14#include <part.h>
15
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030016#include <asm/armv8/mmu.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030018#include <asm/io.h>
19#include <asm/xen/system.h>
20
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +030021#include <linux/bug.h>
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030022#include <linux/compat.h>
23
24#include <xen/events.h>
25#include <xen/gnttab.h>
26#include <xen/hvm.h>
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +030027#include <xen/xenbus.h>
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +030028
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030029#include <xen/interface/io/ring.h>
30#include <xen/interface/io/blkif.h>
31#include <xen/interface/io/protocols.h>
32
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +030033#define DRV_NAME "pvblock"
34#define DRV_NAME_BLK "pvblock_blk"
35
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030036#define O_RDONLY 00
37#define O_RDWR 02
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +030038#define WAIT_RING_TO_MS 10
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030039
40struct blkfront_info {
41 u64 sectors;
42 unsigned int sector_size;
43 int mode;
44 int info;
45 int barrier;
46 int flush;
47};
48
49/**
50 * struct blkfront_dev - Struct representing blkfront device
51 * @dom: Domain id
52 * @ring: Front_ring structure
53 * @ring_ref: The grant reference, allowing us to grant access
54 * to the ring to the other end/domain
55 * @evtchn: Event channel used to signal ring events
56 * @handle: Events handle
57 * @nodename: Device XenStore path in format "device/vbd/" + @devid
58 * @backend: Backend XenStore path
59 * @info: Private data
60 * @devid: Device id
61 */
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +030062struct blkfront_dev {
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +030063 domid_t dom;
64
65 struct blkif_front_ring ring;
66 grant_ref_t ring_ref;
67 evtchn_port_t evtchn;
68 blkif_vdev_t handle;
69
70 char *nodename;
71 char *backend;
72 struct blkfront_info info;
73 unsigned int devid;
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +030074 u8 *bounce_buffer;
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +030075};
76
Simon Glass8a8d24b2020-12-03 16:55:23 -070077struct blkfront_plat {
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +030078 unsigned int devid;
79};
80
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +030081/**
82 * struct blkfront_aiocb - AIO сontrol block
83 * @aio_dev: Blockfront device
84 * @aio_buf: Memory buffer, which must be sector-aligned for
85 * @aio_dev sector
86 * @aio_nbytes: Size of AIO, which must be less than @aio_dev
87 * sector-sized amounts
88 * @aio_offset: Offset, which must not go beyond @aio_dev
89 * sector-aligned location
90 * @data: Data used to receiving response from ring
91 * @gref: Array of grant references
92 * @n: Number of segments
93 * @aio_cb: Represents one I/O request.
94 */
95struct blkfront_aiocb {
96 struct blkfront_dev *aio_dev;
97 u8 *aio_buf;
98 size_t aio_nbytes;
99 off_t aio_offset;
100 void *data;
101
102 grant_ref_t gref[BLKIF_MAX_SEGMENTS_PER_REQUEST];
103 int n;
104
105 void (*aio_cb)(struct blkfront_aiocb *aiocb, int ret);
106};
107
108static void blkfront_sync(struct blkfront_dev *dev);
109
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300110static void free_blkfront(struct blkfront_dev *dev)
111{
112 mask_evtchn(dev->evtchn);
113 free(dev->backend);
114
115 gnttab_end_access(dev->ring_ref);
116 free(dev->ring.sring);
117
118 unbind_evtchn(dev->evtchn);
119
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300120 free(dev->bounce_buffer);
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300121 free(dev->nodename);
122 free(dev);
123}
124
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300125static int init_blkfront(unsigned int devid, struct blkfront_dev *dev)
126{
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300127 xenbus_transaction_t xbt;
128 char *err = NULL;
129 char *message = NULL;
130 struct blkif_sring *s;
131 int retry = 0;
132 char *msg = NULL;
133 char *c;
134 char nodename[32];
135 char path[ARRAY_SIZE(nodename) + strlen("/backend-id") + 1];
136
137 sprintf(nodename, "device/vbd/%d", devid);
138
139 memset(dev, 0, sizeof(*dev));
140 dev->nodename = strdup(nodename);
141 dev->devid = devid;
142
143 snprintf(path, sizeof(path), "%s/backend-id", nodename);
144 dev->dom = xenbus_read_integer(path);
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300145 evtchn_alloc_unbound(dev->dom, NULL, dev, &dev->evtchn);
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300146
147 s = (struct blkif_sring *)memalign(PAGE_SIZE, PAGE_SIZE);
148 if (!s) {
149 printf("Failed to allocate shared ring\n");
150 goto error;
151 }
152
153 SHARED_RING_INIT(s);
154 FRONT_RING_INIT(&dev->ring, s, PAGE_SIZE);
155
156 dev->ring_ref = gnttab_grant_access(dev->dom, virt_to_pfn(s), 0);
157
158again:
159 err = xenbus_transaction_start(&xbt);
160 if (err) {
161 printf("starting transaction\n");
162 free(err);
163 }
164
165 err = xenbus_printf(xbt, nodename, "ring-ref", "%u", dev->ring_ref);
166 if (err) {
167 message = "writing ring-ref";
168 goto abort_transaction;
169 }
170 err = xenbus_printf(xbt, nodename, "event-channel", "%u", dev->evtchn);
171 if (err) {
172 message = "writing event-channel";
173 goto abort_transaction;
174 }
175 err = xenbus_printf(xbt, nodename, "protocol", "%s",
176 XEN_IO_PROTO_ABI_NATIVE);
177 if (err) {
178 message = "writing protocol";
179 goto abort_transaction;
180 }
181
182 snprintf(path, sizeof(path), "%s/state", nodename);
183 err = xenbus_switch_state(xbt, path, XenbusStateConnected);
184 if (err) {
185 message = "switching state";
186 goto abort_transaction;
187 }
188
189 err = xenbus_transaction_end(xbt, 0, &retry);
190 free(err);
191 if (retry) {
192 goto again;
193 printf("completing transaction\n");
194 }
195
196 goto done;
197
198abort_transaction:
199 free(err);
200 err = xenbus_transaction_end(xbt, 1, &retry);
201 printf("Abort transaction %s\n", message);
202 goto error;
203
204done:
205 snprintf(path, sizeof(path), "%s/backend", nodename);
206 msg = xenbus_read(XBT_NIL, path, &dev->backend);
207 if (msg) {
208 printf("Error %s when reading the backend path %s\n",
209 msg, path);
210 goto error;
211 }
212
213 dev->handle = strtoul(strrchr(nodename, '/') + 1, NULL, 0);
214
215 {
216 XenbusState state;
217 char path[strlen(dev->backend) +
218 strlen("/feature-flush-cache") + 1];
219
220 snprintf(path, sizeof(path), "%s/mode", dev->backend);
221 msg = xenbus_read(XBT_NIL, path, &c);
222 if (msg) {
223 printf("Error %s when reading the mode\n", msg);
224 goto error;
225 }
226 if (*c == 'w')
227 dev->info.mode = O_RDWR;
228 else
229 dev->info.mode = O_RDONLY;
230 free(c);
231
232 snprintf(path, sizeof(path), "%s/state", dev->backend);
233
234 msg = NULL;
235 state = xenbus_read_integer(path);
236 while (!msg && state < XenbusStateConnected)
237 msg = xenbus_wait_for_state_change(path, &state);
238 if (msg || state != XenbusStateConnected) {
239 printf("backend not available, state=%d\n", state);
240 goto error;
241 }
242
243 snprintf(path, sizeof(path), "%s/info", dev->backend);
244 dev->info.info = xenbus_read_integer(path);
245
246 snprintf(path, sizeof(path), "%s/sectors", dev->backend);
247 /*
248 * FIXME: read_integer returns an int, so disk size
249 * limited to 1TB for now
250 */
251 dev->info.sectors = xenbus_read_integer(path);
252
253 snprintf(path, sizeof(path), "%s/sector-size", dev->backend);
254 dev->info.sector_size = xenbus_read_integer(path);
255
256 snprintf(path, sizeof(path), "%s/feature-barrier",
257 dev->backend);
258 dev->info.barrier = xenbus_read_integer(path);
259
260 snprintf(path, sizeof(path), "%s/feature-flush-cache",
261 dev->backend);
262 dev->info.flush = xenbus_read_integer(path);
263 }
264 unmask_evtchn(dev->evtchn);
265
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300266 dev->bounce_buffer = memalign(dev->info.sector_size,
267 dev->info.sector_size);
268 if (!dev->bounce_buffer) {
269 printf("Failed to allocate bouncing buffer\n");
270 goto error;
271 }
272
273 debug("%llu sectors of %u bytes, bounce buffer at %p\n",
274 dev->info.sectors, dev->info.sector_size,
275 dev->bounce_buffer);
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300276
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300277 return 0;
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300278
279error:
280 free(msg);
281 free(err);
282 free_blkfront(dev);
283 return -ENODEV;
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300284}
285
286static void shutdown_blkfront(struct blkfront_dev *dev)
287{
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300288 char *err = NULL, *err2;
289 XenbusState state;
290
291 char path[strlen(dev->backend) + strlen("/state") + 1];
292 char nodename[strlen(dev->nodename) + strlen("/event-channel") + 1];
293
294 debug("Close " DRV_NAME ", device ID %d\n", dev->devid);
295
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300296 blkfront_sync(dev);
297
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300298 snprintf(path, sizeof(path), "%s/state", dev->backend);
299 snprintf(nodename, sizeof(nodename), "%s/state", dev->nodename);
300
Anastasiia Lukianenko3337b292020-08-21 12:10:04 +0300301 err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosing);
302 if (err) {
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300303 printf("%s: error changing state to %d: %s\n", __func__,
304 XenbusStateClosing, err);
305 goto close;
306 }
307
308 state = xenbus_read_integer(path);
309 while (!err && state < XenbusStateClosing)
310 err = xenbus_wait_for_state_change(path, &state);
311 free(err);
312
Anastasiia Lukianenko3337b292020-08-21 12:10:04 +0300313 err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed);
314 if (err) {
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300315 printf("%s: error changing state to %d: %s\n", __func__,
316 XenbusStateClosed, err);
317 goto close;
318 }
319
320 state = xenbus_read_integer(path);
321 while (state < XenbusStateClosed) {
322 err = xenbus_wait_for_state_change(path, &state);
323 free(err);
324 }
325
Anastasiia Lukianenko3337b292020-08-21 12:10:04 +0300326 err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising);
327 if (err) {
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300328 printf("%s: error changing state to %d: %s\n", __func__,
329 XenbusStateInitialising, err);
330 goto close;
331 }
332
333 state = xenbus_read_integer(path);
334 while (!err &&
335 (state < XenbusStateInitWait || state >= XenbusStateClosed))
336 err = xenbus_wait_for_state_change(path, &state);
337
338close:
339 free(err);
340
341 snprintf(nodename, sizeof(nodename), "%s/ring-ref", dev->nodename);
342 err2 = xenbus_rm(XBT_NIL, nodename);
343 free(err2);
344 snprintf(nodename, sizeof(nodename), "%s/event-channel", dev->nodename);
345 err2 = xenbus_rm(XBT_NIL, nodename);
346 free(err2);
347
348 if (!err)
349 free_blkfront(dev);
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300350}
351
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300352/**
353 * blkfront_aio_poll() - AIO polling function.
354 * @dev: Blkfront device
355 *
356 * Here we receive response from the ring and check its status. This happens
357 * until we read all data from the ring. We read the data from consumed pointer
358 * to the response pointer. Then increase consumed pointer to make it clear that
359 * the data has been read.
360 *
361 * Return: Number of consumed bytes.
362 */
363static int blkfront_aio_poll(struct blkfront_dev *dev)
364{
365 RING_IDX rp, cons;
366 struct blkif_response *rsp;
367 int more;
368 int nr_consumed;
369
370moretodo:
371 rp = dev->ring.sring->rsp_prod;
372 rmb(); /* Ensure we see queued responses up to 'rp'. */
373 cons = dev->ring.rsp_cons;
374
375 nr_consumed = 0;
376 while ((cons != rp)) {
377 struct blkfront_aiocb *aiocbp;
378 int status;
379
380 rsp = RING_GET_RESPONSE(&dev->ring, cons);
381 nr_consumed++;
382
383 aiocbp = (void *)(uintptr_t)rsp->id;
384 status = rsp->status;
385
386 switch (rsp->operation) {
387 case BLKIF_OP_READ:
388 case BLKIF_OP_WRITE:
389 {
390 int j;
391
392 if (status != BLKIF_RSP_OKAY)
393 printf("%s error %d on %s at offset %llu, num bytes %llu\n",
394 rsp->operation == BLKIF_OP_READ ?
395 "read" : "write",
396 status, aiocbp->aio_dev->nodename,
397 (unsigned long long)aiocbp->aio_offset,
398 (unsigned long long)aiocbp->aio_nbytes);
399
400 for (j = 0; j < aiocbp->n; j++)
401 gnttab_end_access(aiocbp->gref[j]);
402
403 break;
404 }
405
406 case BLKIF_OP_WRITE_BARRIER:
407 if (status != BLKIF_RSP_OKAY)
408 printf("write barrier error %d\n", status);
409 break;
410 case BLKIF_OP_FLUSH_DISKCACHE:
411 if (status != BLKIF_RSP_OKAY)
412 printf("flush error %d\n", status);
413 break;
414
415 default:
416 printf("unrecognized block operation %d response (status %d)\n",
417 rsp->operation, status);
418 break;
419 }
420
421 dev->ring.rsp_cons = ++cons;
422 /* Nota: callback frees aiocbp itself */
423 if (aiocbp && aiocbp->aio_cb)
424 aiocbp->aio_cb(aiocbp, status ? -EIO : 0);
425 if (dev->ring.rsp_cons != cons)
426 /* We reentered, we must not continue here */
427 break;
428 }
429
430 RING_FINAL_CHECK_FOR_RESPONSES(&dev->ring, more);
431 if (more)
432 goto moretodo;
433
434 return nr_consumed;
435}
436
437static void blkfront_wait_slot(struct blkfront_dev *dev)
438{
439 /* Wait for a slot */
440 if (RING_FULL(&dev->ring)) {
441 while (true) {
442 blkfront_aio_poll(dev);
443 if (!RING_FULL(&dev->ring))
444 break;
445 wait_event_timeout(NULL, !RING_FULL(&dev->ring),
446 WAIT_RING_TO_MS);
447 }
448 }
449}
450
451/**
452 * blkfront_aio_poll() - Issue an aio.
453 * @aiocbp: AIO control block structure
454 * @write: Describes is it read or write operation
455 * 0 - read
456 * 1 - write
457 *
458 * We check whether the AIO parameters meet the requirements of the device.
459 * Then receive request from ring and define its arguments. After this we
460 * grant access to the grant references. The last step is notifying about AIO
461 * via event channel.
462 */
463static void blkfront_aio(struct blkfront_aiocb *aiocbp, int write)
464{
465 struct blkfront_dev *dev = aiocbp->aio_dev;
466 struct blkif_request *req;
467 RING_IDX i;
468 int notify;
469 int n, j;
470 uintptr_t start, end;
471
472 /* Can't io at non-sector-aligned location */
473 BUG_ON(aiocbp->aio_offset & (dev->info.sector_size - 1));
474 /* Can't io non-sector-sized amounts */
475 BUG_ON(aiocbp->aio_nbytes & (dev->info.sector_size - 1));
476 /* Can't io non-sector-aligned buffer */
477 BUG_ON(((uintptr_t)aiocbp->aio_buf & (dev->info.sector_size - 1)));
478
479 start = (uintptr_t)aiocbp->aio_buf & PAGE_MASK;
480 end = ((uintptr_t)aiocbp->aio_buf + aiocbp->aio_nbytes +
481 PAGE_SIZE - 1) & PAGE_MASK;
482 n = (end - start) / PAGE_SIZE;
483 aiocbp->n = n;
484
485 BUG_ON(n > BLKIF_MAX_SEGMENTS_PER_REQUEST);
486
487 blkfront_wait_slot(dev);
488 i = dev->ring.req_prod_pvt;
489 req = RING_GET_REQUEST(&dev->ring, i);
490
491 req->operation = write ? BLKIF_OP_WRITE : BLKIF_OP_READ;
492 req->nr_segments = n;
493 req->handle = dev->handle;
494 req->id = (uintptr_t)aiocbp;
495 req->sector_number = aiocbp->aio_offset / dev->info.sector_size;
496
497 for (j = 0; j < n; j++) {
498 req->seg[j].first_sect = 0;
499 req->seg[j].last_sect = PAGE_SIZE / dev->info.sector_size - 1;
500 }
501 req->seg[0].first_sect = ((uintptr_t)aiocbp->aio_buf & ~PAGE_MASK) /
502 dev->info.sector_size;
503 req->seg[n - 1].last_sect = (((uintptr_t)aiocbp->aio_buf +
504 aiocbp->aio_nbytes - 1) & ~PAGE_MASK) / dev->info.sector_size;
505 for (j = 0; j < n; j++) {
506 uintptr_t data = start + j * PAGE_SIZE;
507
508 if (!write) {
509 /* Trigger CoW if needed */
510 *(char *)(data + (req->seg[j].first_sect *
511 dev->info.sector_size)) = 0;
512 barrier();
513 }
514 req->seg[j].gref = gnttab_grant_access(dev->dom,
515 virt_to_pfn((void *)data),
516 write);
517 aiocbp->gref[j] = req->seg[j].gref;
518 }
519
520 dev->ring.req_prod_pvt = i + 1;
521
522 wmb();
523 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->ring, notify);
524
525 if (notify)
526 notify_remote_via_evtchn(dev->evtchn);
527}
528
529static void blkfront_aio_cb(struct blkfront_aiocb *aiocbp, int ret)
530{
531 aiocbp->data = (void *)1;
532 aiocbp->aio_cb = NULL;
533}
534
535static void blkfront_io(struct blkfront_aiocb *aiocbp, int write)
536{
537 aiocbp->aio_cb = blkfront_aio_cb;
538 blkfront_aio(aiocbp, write);
539 aiocbp->data = NULL;
540
541 while (true) {
542 blkfront_aio_poll(aiocbp->aio_dev);
543 if (aiocbp->data)
544 break;
545 cpu_relax();
546 }
547}
548
549static void blkfront_push_operation(struct blkfront_dev *dev, u8 op,
550 uint64_t id)
551{
552 struct blkif_request *req;
553 int notify, i;
554
555 blkfront_wait_slot(dev);
556 i = dev->ring.req_prod_pvt;
557 req = RING_GET_REQUEST(&dev->ring, i);
558 req->operation = op;
559 req->nr_segments = 0;
560 req->handle = dev->handle;
561 req->id = id;
562 req->sector_number = 0;
563 dev->ring.req_prod_pvt = i + 1;
564 wmb();
565 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->ring, notify);
566 if (notify)
567 notify_remote_via_evtchn(dev->evtchn);
568}
569
570static void blkfront_sync(struct blkfront_dev *dev)
571{
572 if (dev->info.mode == O_RDWR) {
573 if (dev->info.barrier == 1)
574 blkfront_push_operation(dev,
575 BLKIF_OP_WRITE_BARRIER, 0);
576
577 if (dev->info.flush == 1)
578 blkfront_push_operation(dev,
579 BLKIF_OP_FLUSH_DISKCACHE, 0);
580 }
581
582 while (true) {
583 blkfront_aio_poll(dev);
584 if (RING_FREE_REQUESTS(&dev->ring) == RING_SIZE(&dev->ring))
585 break;
586 cpu_relax();
587 }
588}
589
590/**
591 * pvblock_iop() - Issue an aio.
592 * @udev: Pvblock device
593 * @blknr: Block number to read from / write to
594 * @blkcnt: Amount of blocks to read / write
595 * @buffer: Memory buffer with data to be read / write
596 * @write: Describes is it read or write operation
597 * 0 - read
598 * 1 - write
599 *
600 * Depending on the operation - reading or writing, data is read / written from the
601 * specified address (@buffer) to the sector (@blknr).
602 */
603static ulong pvblock_iop(struct udevice *udev, lbaint_t blknr,
604 lbaint_t blkcnt, void *buffer, int write)
605{
606 struct blkfront_dev *blk_dev = dev_get_priv(udev);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700607 struct blk_desc *desc = dev_get_uclass_plat(udev);
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300608 struct blkfront_aiocb aiocb;
609 lbaint_t blocks_todo;
610 bool unaligned;
611
612 if (blkcnt == 0)
613 return 0;
614
615 if ((blknr + blkcnt) > desc->lba) {
616 printf(DRV_NAME ": block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
617 blknr + blkcnt, desc->lba);
618 return 0;
619 }
620
621 unaligned = (uintptr_t)buffer & (blk_dev->info.sector_size - 1);
622
623 aiocb.aio_dev = blk_dev;
624 aiocb.aio_offset = blknr * desc->blksz;
625 aiocb.aio_cb = NULL;
626 aiocb.data = NULL;
627 blocks_todo = blkcnt;
628 do {
629 aiocb.aio_buf = unaligned ? blk_dev->bounce_buffer : buffer;
630
631 if (write && unaligned)
632 memcpy(blk_dev->bounce_buffer, buffer, desc->blksz);
633
634 aiocb.aio_nbytes = unaligned ? desc->blksz :
635 min((size_t)(BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE),
636 (size_t)(blocks_todo * desc->blksz));
637
638 blkfront_io(&aiocb, write);
639
640 if (!write && unaligned)
641 memcpy(buffer, blk_dev->bounce_buffer, desc->blksz);
642
643 aiocb.aio_offset += aiocb.aio_nbytes;
644 buffer += aiocb.aio_nbytes;
645 blocks_todo -= aiocb.aio_nbytes / desc->blksz;
646 } while (blocks_todo > 0);
647
648 return blkcnt;
649}
650
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300651ulong pvblock_blk_read(struct udevice *udev, lbaint_t blknr, lbaint_t blkcnt,
652 void *buffer)
653{
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300654 return pvblock_iop(udev, blknr, blkcnt, buffer, 0);
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300655}
656
657ulong pvblock_blk_write(struct udevice *udev, lbaint_t blknr, lbaint_t blkcnt,
658 const void *buffer)
659{
Anastasiia Lukianenko3a739cc2020-08-06 12:42:58 +0300660 return pvblock_iop(udev, blknr, blkcnt, (void *)buffer, 1);
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300661}
662
663static int pvblock_blk_bind(struct udevice *udev)
664{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700665 struct blk_desc *desc = dev_get_uclass_plat(udev);
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300666 int devnum;
667
Simon Glass8149b152022-09-17 09:00:09 -0600668 desc->uclass_id = UCLASS_PVBLOCK;
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300669 /*
670 * Initialize the devnum to -ENODEV. This is to make sure that
671 * blk_next_free_devnum() works as expected, since the default
672 * value 0 is a valid devnum.
673 */
674 desc->devnum = -ENODEV;
Simon Glasse33a5c62022-08-11 19:34:59 -0600675 devnum = blk_next_free_devnum(UCLASS_PVBLOCK);
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300676 if (devnum < 0)
677 return devnum;
678 desc->devnum = devnum;
679 desc->part_type = PART_TYPE_UNKNOWN;
680 desc->bdev = udev;
681
682 strncpy(desc->vendor, "Xen", sizeof(desc->vendor));
683 strncpy(desc->revision, "1", sizeof(desc->revision));
684 strncpy(desc->product, "Virtual disk", sizeof(desc->product));
685
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300686 return 0;
687}
688
689static int pvblock_blk_probe(struct udevice *udev)
690{
691 struct blkfront_dev *blk_dev = dev_get_priv(udev);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700692 struct blkfront_plat *plat = dev_get_plat(udev);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700693 struct blk_desc *desc = dev_get_uclass_plat(udev);
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300694 int ret, devid;
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300695
Simon Glasscaa4daa2020-12-03 16:55:18 -0700696 devid = plat->devid;
697 free(plat);
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300698
699 ret = init_blkfront(devid, blk_dev);
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300700 if (ret < 0)
701 return ret;
Anastasiia Lukianenko17c96f82020-08-06 12:42:57 +0300702
703 desc->blksz = blk_dev->info.sector_size;
704 desc->lba = blk_dev->info.sectors;
705 desc->log2blksz = LOG2(blk_dev->info.sector_size);
706
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300707 return 0;
708}
709
710static int pvblock_blk_remove(struct udevice *udev)
711{
712 struct blkfront_dev *blk_dev = dev_get_priv(udev);
713
714 shutdown_blkfront(blk_dev);
715 return 0;
716}
717
718static const struct blk_ops pvblock_blk_ops = {
719 .read = pvblock_blk_read,
720 .write = pvblock_blk_write,
721};
722
723U_BOOT_DRIVER(pvblock_blk) = {
724 .name = DRV_NAME_BLK,
725 .id = UCLASS_BLK,
726 .ops = &pvblock_blk_ops,
727 .bind = pvblock_blk_bind,
728 .probe = pvblock_blk_probe,
729 .remove = pvblock_blk_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700730 .priv_auto = sizeof(struct blkfront_dev),
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300731 .flags = DM_FLAG_OS_PREPARE,
732};
733
734/*******************************************************************************
735 * Para-virtual block device class
736 *******************************************************************************/
737
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300738typedef int (*enum_vbd_callback)(struct udevice *parent, unsigned int devid);
739
740static int on_new_vbd(struct udevice *parent, unsigned int devid)
741{
742 struct driver_info info;
743 struct udevice *udev;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700744 struct blkfront_plat *plat;
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300745 int ret;
746
747 debug("New " DRV_NAME_BLK ", device ID %d\n", devid);
748
Simon Glass8a8d24b2020-12-03 16:55:23 -0700749 plat = malloc(sizeof(struct blkfront_plat));
Simon Glasscaa4daa2020-12-03 16:55:18 -0700750 if (!plat) {
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300751 printf("Failed to allocate platform data\n");
752 return -ENOMEM;
753 }
754
Simon Glasscaa4daa2020-12-03 16:55:18 -0700755 plat->devid = devid;
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300756
757 info.name = DRV_NAME_BLK;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700758 info.plat = plat;
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300759
760 ret = device_bind_by_name(parent, false, &info, &udev);
761 if (ret < 0) {
762 printf("Failed to bind " DRV_NAME_BLK " to device with ID %d, ret: %d\n",
763 devid, ret);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700764 free(plat);
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300765 }
766 return ret;
767}
768
769static int xenbus_enumerate_vbd(struct udevice *udev, enum_vbd_callback clb)
770{
771 char **dirs, *msg;
772 int i, ret;
773
774 msg = xenbus_ls(XBT_NIL, "device/vbd", &dirs);
775 if (msg) {
776 printf("Failed to read device/vbd directory: %s\n", msg);
777 free(msg);
778 return -ENODEV;
779 }
780
781 for (i = 0; dirs[i]; i++) {
782 int devid;
783
784 sscanf(dirs[i], "%d", &devid);
785 ret = clb(udev, devid);
786 if (ret < 0)
787 goto fail;
788
789 free(dirs[i]);
790 }
791 ret = 0;
792
793fail:
794 for (; dirs[i]; i++)
795 free(dirs[i]);
796 free(dirs);
797 return ret;
798}
799
Anastasiia Lukianenko53d725c2020-08-06 12:42:59 +0300800static void print_pvblock_devices(void)
801{
802 struct udevice *udev;
803 bool first = true;
804 const char *class_name;
805
806 class_name = uclass_get_name(UCLASS_PVBLOCK);
Simon Glasse33a5c62022-08-11 19:34:59 -0600807 for (blk_first_device(UCLASS_PVBLOCK, &udev); udev;
Anastasiia Lukianenko53d725c2020-08-06 12:42:59 +0300808 blk_next_device(&udev), first = false) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700809 struct blk_desc *desc = dev_get_uclass_plat(udev);
Anastasiia Lukianenko53d725c2020-08-06 12:42:59 +0300810
811 if (!first)
812 puts(", ");
813 printf("%s: %d", class_name, desc->devnum);
814 }
815 printf("\n");
816}
817
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300818void pvblock_init(void)
819{
820 struct driver_info info;
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300821 int ret;
822
823 /*
824 * At this point Xen drivers have already initialized,
825 * so we can instantiate the class driver and enumerate
826 * virtual block devices.
827 */
828 info.name = DRV_NAME;
Michal Suchanek0b999d22022-10-22 16:33:05 +0200829 ret = device_bind_by_name(gd->dm_root, false, &info, NULL);
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300830 if (ret < 0)
831 printf("Failed to bind " DRV_NAME ", ret: %d\n", ret);
832
833 /* Bootstrap virtual block devices class driver */
Michal Suchanek0b999d22022-10-22 16:33:05 +0200834 uclass_probe_all(UCLASS_PVBLOCK);
Anastasiia Lukianenko53d725c2020-08-06 12:42:59 +0300835
836 print_pvblock_devices();
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300837}
838
839static int pvblock_probe(struct udevice *udev)
840{
Anastasiia Lukianenkoa9993132020-08-06 12:42:56 +0300841 struct uclass *uc;
842 int ret;
843
844 if (xenbus_enumerate_vbd(udev, on_new_vbd) < 0)
845 return -ENODEV;
846
847 ret = uclass_get(UCLASS_BLK, &uc);
848 if (ret)
849 return ret;
Michal Suchanek801f7112022-10-12 21:58:05 +0200850 uclass_foreach_dev_probe(UCLASS_BLK, udev);
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300851 return 0;
852}
853
854U_BOOT_DRIVER(pvblock_drv) = {
855 .name = DRV_NAME,
856 .id = UCLASS_PVBLOCK,
857 .probe = pvblock_probe,
858};
859
860UCLASS_DRIVER(pvblock) = {
861 .name = DRV_NAME,
862 .id = UCLASS_PVBLOCK,
863};