aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/char/virtio_console.c329
-rw-r--r--drivers/lguest/core.c2
-rw-r--r--drivers/net/virtio_net.c48
-rw-r--r--drivers/rpmsg/virtio_rpmsg_bus.c6
-rw-r--r--drivers/scsi/virtio_scsi.c24
-rw-r--r--drivers/virtio/virtio.c30
-rw-r--r--drivers/virtio/virtio_balloon.c7
-rw-r--r--drivers/virtio/virtio_mmio.c30
-rw-r--r--drivers/virtio/virtio_pci.c20
-rw-r--r--drivers/virtio/virtio_ring.c46
-rw-r--r--include/linux/virtio.h25
-rw-r--r--include/linux/virtio_scsi.h28
-rw-r--r--include/uapi/linux/virtio_ids.h1
-rw-r--r--mm/highmem.c1
-rw-r--r--net/9p/trans_virtio.c3
-rw-r--r--tools/lguest/lguest.c84
-rw-r--r--tools/virtio/virtio_test.c4
17 files changed, 412 insertions, 276 deletions
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index 90493d4ead1f..c594cb16c37b 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -37,8 +37,12 @@
37#include <linux/wait.h> 37#include <linux/wait.h>
38#include <linux/workqueue.h> 38#include <linux/workqueue.h>
39#include <linux/module.h> 39#include <linux/module.h>
40#include <linux/dma-mapping.h>
41#include <linux/kconfig.h>
40#include "../tty/hvc/hvc_console.h" 42#include "../tty/hvc/hvc_console.h"
41 43
44#define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
45
42/* 46/*
43 * This is a global struct for storing common data for all the devices 47 * This is a global struct for storing common data for all the devices
44 * this driver handles. 48 * this driver handles.
@@ -111,6 +115,21 @@ struct port_buffer {
111 size_t len; 115 size_t len;
112 /* offset in the buf from which to consume data */ 116 /* offset in the buf from which to consume data */
113 size_t offset; 117 size_t offset;
118
119 /* DMA address of buffer */
120 dma_addr_t dma;
121
122 /* Device we got DMA memory from */
123 struct device *dev;
124
125 /* List of pending dma buffers to free */
126 struct list_head list;
127
128 /* If sgpages == 0 then buf is used */
129 unsigned int sgpages;
130
131 /* sg is used if spages > 0. sg must be the last in is struct */
132 struct scatterlist sg[0];
114}; 133};
115 134
116/* 135/*
@@ -325,6 +344,11 @@ static bool is_console_port(struct port *port)
325 return false; 344 return false;
326} 345}
327 346
347static bool is_rproc_serial(const struct virtio_device *vdev)
348{
349 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
350}
351
328static inline bool use_multiport(struct ports_device *portdev) 352static inline bool use_multiport(struct ports_device *portdev)
329{ 353{
330 /* 354 /*
@@ -336,20 +360,110 @@ static inline bool use_multiport(struct ports_device *portdev)
336 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT); 360 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
337} 361}
338 362
339static void free_buf(struct port_buffer *buf) 363static DEFINE_SPINLOCK(dma_bufs_lock);
364static LIST_HEAD(pending_free_dma_bufs);
365
366static void free_buf(struct port_buffer *buf, bool can_sleep)
340{ 367{
341 kfree(buf->buf); 368 unsigned int i;
369
370 for (i = 0; i < buf->sgpages; i++) {
371 struct page *page = sg_page(&buf->sg[i]);
372 if (!page)
373 break;
374 put_page(page);
375 }
376
377 if (!buf->dev) {
378 kfree(buf->buf);
379 } else if (is_rproc_enabled) {
380 unsigned long flags;
381
382 /* dma_free_coherent requires interrupts to be enabled. */
383 if (!can_sleep) {
384 /* queue up dma-buffers to be freed later */
385 spin_lock_irqsave(&dma_bufs_lock, flags);
386 list_add_tail(&buf->list, &pending_free_dma_bufs);
387 spin_unlock_irqrestore(&dma_bufs_lock, flags);
388 return;
389 }
390 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
391
392 /* Release device refcnt and allow it to be freed */
393 put_device(buf->dev);
394 }
395
342 kfree(buf); 396 kfree(buf);
343} 397}
344 398
345static struct port_buffer *alloc_buf(size_t buf_size) 399static void reclaim_dma_bufs(void)
400{
401 unsigned long flags;
402 struct port_buffer *buf, *tmp;
403 LIST_HEAD(tmp_list);
404
405 if (list_empty(&pending_free_dma_bufs))
406 return;
407
408 /* Create a copy of the pending_free_dma_bufs while holding the lock */
409 spin_lock_irqsave(&dma_bufs_lock, flags);
410 list_cut_position(&tmp_list, &pending_free_dma_bufs,
411 pending_free_dma_bufs.prev);
412 spin_unlock_irqrestore(&dma_bufs_lock, flags);
413
414 /* Release the dma buffers, without irqs enabled */
415 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
416 list_del(&buf->list);
417 free_buf(buf, true);
418 }
419}
420
421static struct port_buffer *alloc_buf(struct virtqueue *vq, size_t buf_size,
422 int pages)
346{ 423{
347 struct port_buffer *buf; 424 struct port_buffer *buf;
348 425
349 buf = kmalloc(sizeof(*buf), GFP_KERNEL); 426 reclaim_dma_bufs();
427
428 /*
429 * Allocate buffer and the sg list. The sg list array is allocated
430 * directly after the port_buffer struct.
431 */
432 buf = kmalloc(sizeof(*buf) + sizeof(struct scatterlist) * pages,
433 GFP_KERNEL);
350 if (!buf) 434 if (!buf)
351 goto fail; 435 goto fail;
352 buf->buf = kzalloc(buf_size, GFP_KERNEL); 436
437 buf->sgpages = pages;
438 if (pages > 0) {
439 buf->dev = NULL;
440 buf->buf = NULL;
441 return buf;
442 }
443
444 if (is_rproc_serial(vq->vdev)) {
445 /*
446 * Allocate DMA memory from ancestor. When a virtio
447 * device is created by remoteproc, the DMA memory is
448 * associated with the grandparent device:
449 * vdev => rproc => platform-dev.
450 * The code here would have been less quirky if
451 * DMA_MEMORY_INCLUDES_CHILDREN had been supported
452 * in dma-coherent.c
453 */
454 if (!vq->vdev->dev.parent || !vq->vdev->dev.parent->parent)
455 goto free_buf;
456 buf->dev = vq->vdev->dev.parent->parent;
457
458 /* Increase device refcnt to avoid freeing it */
459 get_device(buf->dev);
460 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
461 GFP_KERNEL);
462 } else {
463 buf->dev = NULL;
464 buf->buf = kmalloc(buf_size, GFP_KERNEL);
465 }
466
353 if (!buf->buf) 467 if (!buf->buf)
354 goto free_buf; 468 goto free_buf;
355 buf->len = 0; 469 buf->len = 0;
@@ -396,6 +510,8 @@ static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
396 510
397 ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC); 511 ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC);
398 virtqueue_kick(vq); 512 virtqueue_kick(vq);
513 if (!ret)
514 ret = vq->num_free;
399 return ret; 515 return ret;
400} 516}
401 517
@@ -416,7 +532,7 @@ static void discard_port_data(struct port *port)
416 port->stats.bytes_discarded += buf->len - buf->offset; 532 port->stats.bytes_discarded += buf->len - buf->offset;
417 if (add_inbuf(port->in_vq, buf) < 0) { 533 if (add_inbuf(port->in_vq, buf) < 0) {
418 err++; 534 err++;
419 free_buf(buf); 535 free_buf(buf, false);
420 } 536 }
421 port->inbuf = NULL; 537 port->inbuf = NULL;
422 buf = get_inbuf(port); 538 buf = get_inbuf(port);
@@ -459,7 +575,7 @@ static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
459 vq = portdev->c_ovq; 575 vq = portdev->c_ovq;
460 576
461 sg_init_one(sg, &cpkt, sizeof(cpkt)); 577 sg_init_one(sg, &cpkt, sizeof(cpkt));
462 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) { 578 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) == 0) {
463 virtqueue_kick(vq); 579 virtqueue_kick(vq);
464 while (!virtqueue_get_buf(vq, &len)) 580 while (!virtqueue_get_buf(vq, &len))
465 cpu_relax(); 581 cpu_relax();
@@ -476,55 +592,29 @@ static ssize_t send_control_msg(struct port *port, unsigned int event,
476 return 0; 592 return 0;
477} 593}
478 594
479struct buffer_token {
480 union {
481 void *buf;
482 struct scatterlist *sg;
483 } u;
484 /* If sgpages == 0 then buf is used, else sg is used */
485 unsigned int sgpages;
486};
487
488static void reclaim_sg_pages(struct scatterlist *sg, unsigned int nrpages)
489{
490 int i;
491 struct page *page;
492
493 for (i = 0; i < nrpages; i++) {
494 page = sg_page(&sg[i]);
495 if (!page)
496 break;
497 put_page(page);
498 }
499 kfree(sg);
500}
501 595
502/* Callers must take the port->outvq_lock */ 596/* Callers must take the port->outvq_lock */
503static void reclaim_consumed_buffers(struct port *port) 597static void reclaim_consumed_buffers(struct port *port)
504{ 598{
505 struct buffer_token *tok; 599 struct port_buffer *buf;
506 unsigned int len; 600 unsigned int len;
507 601
508 if (!port->portdev) { 602 if (!port->portdev) {
509 /* Device has been unplugged. vqs are already gone. */ 603 /* Device has been unplugged. vqs are already gone. */
510 return; 604 return;
511 } 605 }
512 while ((tok = virtqueue_get_buf(port->out_vq, &len))) { 606 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
513 if (tok->sgpages) 607 free_buf(buf, false);
514 reclaim_sg_pages(tok->u.sg, tok->sgpages);
515 else
516 kfree(tok->u.buf);
517 kfree(tok);
518 port->outvq_full = false; 608 port->outvq_full = false;
519 } 609 }
520} 610}
521 611
522static ssize_t __send_to_port(struct port *port, struct scatterlist *sg, 612static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
523 int nents, size_t in_count, 613 int nents, size_t in_count,
524 struct buffer_token *tok, bool nonblock) 614 void *data, bool nonblock)
525{ 615{
526 struct virtqueue *out_vq; 616 struct virtqueue *out_vq;
527 ssize_t ret; 617 int err;
528 unsigned long flags; 618 unsigned long flags;
529 unsigned int len; 619 unsigned int len;
530 620
@@ -534,17 +624,17 @@ static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
534 624
535 reclaim_consumed_buffers(port); 625 reclaim_consumed_buffers(port);
536 626
537 ret = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC); 627 err = virtqueue_add_buf(out_vq, sg, nents, 0, data, GFP_ATOMIC);
538 628
539 /* Tell Host to go! */ 629 /* Tell Host to go! */
540 virtqueue_kick(out_vq); 630 virtqueue_kick(out_vq);
541 631
542 if (ret < 0) { 632 if (err) {
543 in_count = 0; 633 in_count = 0;
544 goto done; 634 goto done;
545 } 635 }
546 636
547 if (ret == 0) 637 if (out_vq->num_free == 0)
548 port->outvq_full = true; 638 port->outvq_full = true;
549 639
550 if (nonblock) 640 if (nonblock)
@@ -572,37 +662,6 @@ done:
572 return in_count; 662 return in_count;
573} 663}
574 664
575static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
576 bool nonblock)
577{
578 struct scatterlist sg[1];
579 struct buffer_token *tok;
580
581 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
582 if (!tok)
583 return -ENOMEM;
584 tok->sgpages = 0;
585 tok->u.buf = in_buf;
586
587 sg_init_one(sg, in_buf, in_count);
588
589 return __send_to_port(port, sg, 1, in_count, tok, nonblock);
590}
591
592static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents,
593 size_t in_count, bool nonblock)
594{
595 struct buffer_token *tok;
596
597 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
598 if (!tok)
599 return -ENOMEM;
600 tok->sgpages = nents;
601 tok->u.sg = sg;
602
603 return __send_to_port(port, sg, nents, in_count, tok, nonblock);
604}
605
606/* 665/*
607 * Give out the data that's requested from the buffer that we have 666 * Give out the data that's requested from the buffer that we have
608 * queued up. 667 * queued up.
@@ -748,9 +807,10 @@ static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
748 size_t count, loff_t *offp) 807 size_t count, loff_t *offp)
749{ 808{
750 struct port *port; 809 struct port *port;
751 char *buf; 810 struct port_buffer *buf;
752 ssize_t ret; 811 ssize_t ret;
753 bool nonblock; 812 bool nonblock;
813 struct scatterlist sg[1];
754 814
755 /* Userspace could be out to fool us */ 815 /* Userspace could be out to fool us */
756 if (!count) 816 if (!count)
@@ -766,11 +826,11 @@ static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
766 826
767 count = min((size_t)(32 * 1024), count); 827 count = min((size_t)(32 * 1024), count);
768 828
769 buf = kmalloc(count, GFP_KERNEL); 829 buf = alloc_buf(port->out_vq, count, 0);
770 if (!buf) 830 if (!buf)
771 return -ENOMEM; 831 return -ENOMEM;
772 832
773 ret = copy_from_user(buf, ubuf, count); 833 ret = copy_from_user(buf->buf, ubuf, count);
774 if (ret) { 834 if (ret) {
775 ret = -EFAULT; 835 ret = -EFAULT;
776 goto free_buf; 836 goto free_buf;
@@ -784,13 +844,14 @@ static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
784 * through to the host. 844 * through to the host.
785 */ 845 */
786 nonblock = true; 846 nonblock = true;
787 ret = send_buf(port, buf, count, nonblock); 847 sg_init_one(sg, buf->buf, count);
848 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
788 849
789 if (nonblock && ret > 0) 850 if (nonblock && ret > 0)
790 goto out; 851 goto out;
791 852
792free_buf: 853free_buf:
793 kfree(buf); 854 free_buf(buf, true);
794out: 855out:
795 return ret; 856 return ret;
796} 857}
@@ -856,6 +917,7 @@ static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
856 struct port *port = filp->private_data; 917 struct port *port = filp->private_data;
857 struct sg_list sgl; 918 struct sg_list sgl;
858 ssize_t ret; 919 ssize_t ret;
920 struct port_buffer *buf;
859 struct splice_desc sd = { 921 struct splice_desc sd = {
860 .total_len = len, 922 .total_len = len,
861 .flags = flags, 923 .flags = flags,
@@ -863,22 +925,34 @@ static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
863 .u.data = &sgl, 925 .u.data = &sgl,
864 }; 926 };
865 927
928 /*
929 * Rproc_serial does not yet support splice. To support splice
930 * pipe_to_sg() must allocate dma-buffers and copy content from
931 * regular pages to dma pages. And alloc_buf and free_buf must
932 * support allocating and freeing such a list of dma-buffers.
933 */
934 if (is_rproc_serial(port->out_vq->vdev))
935 return -EINVAL;
936
866 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK); 937 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
867 if (ret < 0) 938 if (ret < 0)
868 return ret; 939 return ret;
869 940
941 buf = alloc_buf(port->out_vq, 0, pipe->nrbufs);
942 if (!buf)
943 return -ENOMEM;
944
870 sgl.n = 0; 945 sgl.n = 0;
871 sgl.len = 0; 946 sgl.len = 0;
872 sgl.size = pipe->nrbufs; 947 sgl.size = pipe->nrbufs;
873 sgl.sg = kmalloc(sizeof(struct scatterlist) * sgl.size, GFP_KERNEL); 948 sgl.sg = buf->sg;
874 if (unlikely(!sgl.sg))
875 return -ENOMEM;
876
877 sg_init_table(sgl.sg, sgl.size); 949 sg_init_table(sgl.sg, sgl.size);
878 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg); 950 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
879 if (likely(ret > 0)) 951 if (likely(ret > 0))
880 ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true); 952 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
881 953
954 if (unlikely(ret <= 0))
955 free_buf(buf, true);
882 return ret; 956 return ret;
883} 957}
884 958
@@ -927,6 +1001,7 @@ static int port_fops_release(struct inode *inode, struct file *filp)
927 reclaim_consumed_buffers(port); 1001 reclaim_consumed_buffers(port);
928 spin_unlock_irq(&port->outvq_lock); 1002 spin_unlock_irq(&port->outvq_lock);
929 1003
1004 reclaim_dma_bufs();
930 /* 1005 /*
931 * Locks aren't necessary here as a port can't be opened after 1006 * Locks aren't necessary here as a port can't be opened after
932 * unplug, and if a port isn't unplugged, a kref would already 1007 * unplug, and if a port isn't unplugged, a kref would already
@@ -1031,6 +1106,7 @@ static const struct file_operations port_fops = {
1031static int put_chars(u32 vtermno, const char *buf, int count) 1106static int put_chars(u32 vtermno, const char *buf, int count)
1032{ 1107{
1033 struct port *port; 1108 struct port *port;
1109 struct scatterlist sg[1];
1034 1110
1035 if (unlikely(early_put_chars)) 1111 if (unlikely(early_put_chars))
1036 return early_put_chars(vtermno, buf, count); 1112 return early_put_chars(vtermno, buf, count);
@@ -1039,7 +1115,8 @@ static int put_chars(u32 vtermno, const char *buf, int count)
1039 if (!port) 1115 if (!port)
1040 return -EPIPE; 1116 return -EPIPE;
1041 1117
1042 return send_buf(port, (void *)buf, count, false); 1118 sg_init_one(sg, buf, count);
1119 return __send_to_port(port, sg, 1, count, (void *)buf, false);
1043} 1120}
1044 1121
1045/* 1122/*
@@ -1076,7 +1153,10 @@ static void resize_console(struct port *port)
1076 return; 1153 return;
1077 1154
1078 vdev = port->portdev->vdev; 1155 vdev = port->portdev->vdev;
1079 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) 1156
1157 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1158 if (!is_rproc_serial(vdev) &&
1159 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1080 hvc_resize(port->cons.hvc, port->cons.ws); 1160 hvc_resize(port->cons.hvc, port->cons.ws);
1081} 1161}
1082 1162
@@ -1260,7 +1340,7 @@ static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1260 1340
1261 nr_added_bufs = 0; 1341 nr_added_bufs = 0;
1262 do { 1342 do {
1263 buf = alloc_buf(PAGE_SIZE); 1343 buf = alloc_buf(vq, PAGE_SIZE, 0);
1264 if (!buf) 1344 if (!buf)
1265 break; 1345 break;
1266 1346
@@ -1268,7 +1348,7 @@ static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1268 ret = add_inbuf(vq, buf); 1348 ret = add_inbuf(vq, buf);
1269 if (ret < 0) { 1349 if (ret < 0) {
1270 spin_unlock_irq(lock); 1350 spin_unlock_irq(lock);
1271 free_buf(buf); 1351 free_buf(buf, true);
1272 break; 1352 break;
1273 } 1353 }
1274 nr_added_bufs++; 1354 nr_added_bufs++;
@@ -1356,10 +1436,18 @@ static int add_port(struct ports_device *portdev, u32 id)
1356 goto free_device; 1436 goto free_device;
1357 } 1437 }
1358 1438
1359 /* 1439 if (is_rproc_serial(port->portdev->vdev))
1360 * If we're not using multiport support, this has to be a console port 1440 /*
1361 */ 1441 * For rproc_serial assume remote processor is connected.
1362 if (!use_multiport(port->portdev)) { 1442 * rproc_serial does not want the console port, only
1443 * the generic port implementation.
1444 */
1445 port->host_connected = true;
1446 else if (!use_multiport(port->portdev)) {
1447 /*
1448 * If we're not using multiport support,
1449 * this has to be a console port.
1450 */
1363 err = init_port_console(port); 1451 err = init_port_console(port);
1364 if (err) 1452 if (err)
1365 goto free_inbufs; 1453 goto free_inbufs;
@@ -1392,7 +1480,7 @@ static int add_port(struct ports_device *portdev, u32 id)
1392 1480
1393free_inbufs: 1481free_inbufs:
1394 while ((buf = virtqueue_detach_unused_buf(port->in_vq))) 1482 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1395 free_buf(buf); 1483 free_buf(buf, true);
1396free_device: 1484free_device:
1397 device_destroy(pdrvdata.class, port->dev->devt); 1485 device_destroy(pdrvdata.class, port->dev->devt);
1398free_cdev: 1486free_cdev:
@@ -1434,7 +1522,11 @@ static void remove_port_data(struct port *port)
1434 1522
1435 /* Remove buffers we queued up for the Host to send us data in. */ 1523 /* Remove buffers we queued up for the Host to send us data in. */
1436 while ((buf = virtqueue_detach_unused_buf(port->in_vq))) 1524 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1437 free_buf(buf); 1525 free_buf(buf, true);
1526
1527 /* Free pending buffers from the out-queue. */
1528 while ((buf = virtqueue_detach_unused_buf(port->out_vq)))
1529 free_buf(buf, true);
1438} 1530}
1439 1531
1440/* 1532/*
@@ -1636,7 +1728,7 @@ static void control_work_handler(struct work_struct *work)
1636 if (add_inbuf(portdev->c_ivq, buf) < 0) { 1728 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1637 dev_warn(&portdev->vdev->dev, 1729 dev_warn(&portdev->vdev->dev,
1638 "Error adding buffer to queue\n"); 1730 "Error adding buffer to queue\n");
1639 free_buf(buf); 1731 free_buf(buf, false);
1640 } 1732 }
1641 } 1733 }
1642 spin_unlock(&portdev->cvq_lock); 1734 spin_unlock(&portdev->cvq_lock);
@@ -1832,10 +1924,10 @@ static void remove_controlq_data(struct ports_device *portdev)
1832 return; 1924 return;
1833 1925
1834 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len))) 1926 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1835 free_buf(buf); 1927 free_buf(buf, true);
1836 1928
1837 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq))) 1929 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1838 free_buf(buf); 1930 free_buf(buf, true);
1839} 1931}
1840 1932
1841/* 1933/*
@@ -1882,11 +1974,15 @@ static int virtcons_probe(struct virtio_device *vdev)
1882 1974
1883 multiport = false; 1975 multiport = false;
1884 portdev->config.max_nr_ports = 1; 1976 portdev->config.max_nr_ports = 1;
1885 if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT, 1977
1886 offsetof(struct virtio_console_config, 1978 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
1887 max_nr_ports), 1979 if (!is_rproc_serial(vdev) &&
1888 &portdev->config.max_nr_ports) == 0) 1980 virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1981 offsetof(struct virtio_console_config,
1982 max_nr_ports),
1983 &portdev->config.max_nr_ports) == 0) {
1889 multiport = true; 1984 multiport = true;
1985 }
1890 1986
1891 err = init_vqs(portdev); 1987 err = init_vqs(portdev);
1892 if (err < 0) { 1988 if (err < 0) {
@@ -1996,6 +2092,16 @@ static unsigned int features[] = {
1996 VIRTIO_CONSOLE_F_MULTIPORT, 2092 VIRTIO_CONSOLE_F_MULTIPORT,
1997}; 2093};
1998 2094
2095static struct virtio_device_id rproc_serial_id_table[] = {
2096#if IS_ENABLED(CONFIG_REMOTEPROC)
2097 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2098#endif
2099 { 0 },
2100};
2101
2102static unsigned int rproc_serial_features[] = {
2103};
2104
1999#ifdef CONFIG_PM 2105#ifdef CONFIG_PM
2000static int virtcons_freeze(struct virtio_device *vdev) 2106static int virtcons_freeze(struct virtio_device *vdev)
2001{ 2107{
@@ -2080,6 +2186,20 @@ static struct virtio_driver virtio_console = {
2080#endif 2186#endif
2081}; 2187};
2082 2188
2189/*
2190 * virtio_rproc_serial refers to __devinit function which causes
2191 * section mismatch warnings. So use __refdata to silence warnings.
2192 */
2193static struct virtio_driver __refdata virtio_rproc_serial = {
2194 .feature_table = rproc_serial_features,
2195 .feature_table_size = ARRAY_SIZE(rproc_serial_features),
2196 .driver.name = "virtio_rproc_serial",
2197 .driver.owner = THIS_MODULE,
2198 .id_table = rproc_serial_id_table,
2199 .probe = virtcons_probe,
2200 .remove = virtcons_remove,
2201};
2202
2083static int __init init(void) 2203static int __init init(void)
2084{ 2204{
2085 int err; 2205 int err;
@@ -2104,7 +2224,15 @@ static int __init init(void)
2104 pr_err("Error %d registering virtio driver\n", err); 2224 pr_err("Error %d registering virtio driver\n", err);
2105 goto free; 2225 goto free;
2106 } 2226 }
2227 err = register_virtio_driver(&virtio_rproc_serial);
2228 if (err < 0) {
2229 pr_err("Error %d registering virtio rproc serial driver\n",
2230 err);
2231 goto unregister;
2232 }
2107 return 0; 2233 return 0;
2234unregister:
2235 unregister_virtio_driver(&virtio_console);
2108free: 2236free:
2109 if (pdrvdata.debugfs_dir) 2237 if (pdrvdata.debugfs_dir)
2110 debugfs_remove_recursive(pdrvdata.debugfs_dir); 2238 debugfs_remove_recursive(pdrvdata.debugfs_dir);
@@ -2114,7 +2242,10 @@ free:
2114 2242
2115static void __exit fini(void) 2243static void __exit fini(void)
2116{ 2244{
2245 reclaim_dma_bufs();
2246
2117 unregister_virtio_driver(&virtio_console); 2247 unregister_virtio_driver(&virtio_console);
2248 unregister_virtio_driver(&virtio_rproc_serial);
2118 2249
2119 class_destroy(pdrvdata.class); 2250 class_destroy(pdrvdata.class);
2120 if (pdrvdata.debugfs_dir) 2251 if (pdrvdata.debugfs_dir)
diff --git a/drivers/lguest/core.c b/drivers/lguest/core.c
index b5fdcb78a75b..a5ebc0083d87 100644
--- a/drivers/lguest/core.c
+++ b/drivers/lguest/core.c
@@ -225,7 +225,7 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
225 * eventfd (ie. the appropriate virtqueue thread)? 225 * eventfd (ie. the appropriate virtqueue thread)?
226 */ 226 */
227 if (!send_notify_to_eventfd(cpu)) { 227 if (!send_notify_to_eventfd(cpu)) {
228 /* OK, we tell the main Laucher. */ 228 /* OK, we tell the main Launcher. */
229 if (put_user(cpu->pending_notify, user)) 229 if (put_user(cpu->pending_notify, user))
230 return -EFAULT; 230 return -EFAULT;
231 return sizeof(cpu->pending_notify); 231 return sizeof(cpu->pending_notify);
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 68d64f0313ea..a6fcf15adc4f 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -130,7 +130,6 @@ struct skb_vnet_hdr {
130 struct virtio_net_hdr hdr; 130 struct virtio_net_hdr hdr;
131 struct virtio_net_hdr_mrg_rxbuf mhdr; 131 struct virtio_net_hdr_mrg_rxbuf mhdr;
132 }; 132 };
133 unsigned int num_sg;
134}; 133};
135 134
136struct padded_vnet_hdr { 135struct padded_vnet_hdr {
@@ -530,10 +529,10 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
530 err = add_recvbuf_small(rq, gfp); 529 err = add_recvbuf_small(rq, gfp);
531 530
532 oom = err == -ENOMEM; 531 oom = err == -ENOMEM;
533 if (err < 0) 532 if (err)
534 break; 533 break;
535 ++rq->num; 534 ++rq->num;
536 } while (err > 0); 535 } while (rq->vq->num_free);
537 if (unlikely(rq->num > rq->max)) 536 if (unlikely(rq->num > rq->max))
538 rq->max = rq->num; 537 rq->max = rq->num;
539 virtqueue_kick(rq->vq); 538 virtqueue_kick(rq->vq);
@@ -640,10 +639,10 @@ static int virtnet_open(struct net_device *dev)
640 return 0; 639 return 0;
641} 640}
642 641
643static unsigned int free_old_xmit_skbs(struct send_queue *sq) 642static void free_old_xmit_skbs(struct send_queue *sq)
644{ 643{
645 struct sk_buff *skb; 644 struct sk_buff *skb;
646 unsigned int len, tot_sgs = 0; 645 unsigned int len;
647 struct virtnet_info *vi = sq->vq->vdev->priv; 646 struct virtnet_info *vi = sq->vq->vdev->priv;
648 struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 647 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
649 648
@@ -655,10 +654,8 @@ static unsigned int free_old_xmit_skbs(struct send_queue *sq)
655 stats->tx_packets++; 654 stats->tx_packets++;
656 u64_stats_update_end(&stats->tx_syncp); 655 u64_stats_update_end(&stats->tx_syncp);
657 656
658 tot_sgs += skb_vnet_hdr(skb)->num_sg;
659 dev_kfree_skb_any(skb); 657 dev_kfree_skb_any(skb);
660 } 658 }
661 return tot_sgs;
662} 659}
663 660
664static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) 661static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
@@ -666,6 +663,7 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
666 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); 663 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
667 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 664 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
668 struct virtnet_info *vi = sq->vq->vdev->priv; 665 struct virtnet_info *vi = sq->vq->vdev->priv;
666 unsigned num_sg;
669 667
670 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 668 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
671 669
@@ -704,8 +702,8 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
704 else 702 else
705 sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr); 703 sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
706 704
707 hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 705 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
708 return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg, 706 return virtqueue_add_buf(sq->vq, sq->sg, num_sg,
709 0, skb, GFP_ATOMIC); 707 0, skb, GFP_ATOMIC);
710} 708}
711 709
@@ -714,28 +712,20 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
714 struct virtnet_info *vi = netdev_priv(dev); 712 struct virtnet_info *vi = netdev_priv(dev);
715 int qnum = skb_get_queue_mapping(skb); 713 int qnum = skb_get_queue_mapping(skb);
716 struct send_queue *sq = &vi->sq[qnum]; 714 struct send_queue *sq = &vi->sq[qnum];
717 int capacity; 715 int err;
718 716
719 /* Free up any pending old buffers before queueing new ones. */ 717 /* Free up any pending old buffers before queueing new ones. */
720 free_old_xmit_skbs(sq); 718 free_old_xmit_skbs(sq);
721 719
722 /* Try to transmit */ 720 /* Try to transmit */
723 capacity = xmit_skb(sq, skb); 721 err = xmit_skb(sq, skb);
724 722
725 /* This can happen with OOM and indirect buffers. */ 723 /* This should not happen! */
726 if (unlikely(capacity < 0)) { 724 if (unlikely(err)) {
727 if (likely(capacity == -ENOMEM)) { 725 dev->stats.tx_fifo_errors++;
728 if (net_ratelimit()) 726 if (net_ratelimit())
729 dev_warn(&dev->dev, 727 dev_warn(&dev->dev,
730 "TXQ (%d) failure: out of memory\n", 728 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
731 qnum);
732 } else {
733 dev->stats.tx_fifo_errors++;
734 if (net_ratelimit())
735 dev_warn(&dev->dev,
736 "Unexpected TXQ (%d) failure: %d\n",
737 qnum, capacity);
738 }
739 dev->stats.tx_dropped++; 729 dev->stats.tx_dropped++;
740 kfree_skb(skb); 730 kfree_skb(skb);
741 return NETDEV_TX_OK; 731 return NETDEV_TX_OK;
@@ -748,12 +738,12 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
748 738
749 /* Apparently nice girls don't return TX_BUSY; stop the queue 739 /* Apparently nice girls don't return TX_BUSY; stop the queue
750 * before it gets out of hand. Naturally, this wastes entries. */ 740 * before it gets out of hand. Naturally, this wastes entries. */
751 if (capacity < 2+MAX_SKB_FRAGS) { 741 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
752 netif_stop_subqueue(dev, qnum); 742 netif_stop_subqueue(dev, qnum);
753 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 743 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
754 /* More just got used, free them then recheck. */ 744 /* More just got used, free them then recheck. */
755 capacity += free_old_xmit_skbs(sq); 745 free_old_xmit_skbs(sq);
756 if (capacity >= 2+MAX_SKB_FRAGS) { 746 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
757 netif_start_subqueue(dev, qnum); 747 netif_start_subqueue(dev, qnum);
758 virtqueue_disable_cb(sq->vq); 748 virtqueue_disable_cb(sq->vq);
759 } 749 }
diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 1859f71372e2..027096fe6a12 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -764,7 +764,7 @@ int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
764 764
765 /* add message to the remote processor's virtqueue */ 765 /* add message to the remote processor's virtqueue */
766 err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL); 766 err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
767 if (err < 0) { 767 if (err) {
768 /* 768 /*
769 * need to reclaim the buffer here, otherwise it's lost 769 * need to reclaim the buffer here, otherwise it's lost
770 * (memory won't leak, but rpmsg won't use it again for TX). 770 * (memory won't leak, but rpmsg won't use it again for TX).
@@ -776,8 +776,6 @@ int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
776 776
777 /* tell the remote processor it has a pending message to read */ 777 /* tell the remote processor it has a pending message to read */
778 virtqueue_kick(vrp->svq); 778 virtqueue_kick(vrp->svq);
779
780 err = 0;
781out: 779out:
782 mutex_unlock(&vrp->tx_lock); 780 mutex_unlock(&vrp->tx_lock);
783 return err; 781 return err;
@@ -980,7 +978,7 @@ static int rpmsg_probe(struct virtio_device *vdev)
980 978
981 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr, 979 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
982 GFP_KERNEL); 980 GFP_KERNEL);
983 WARN_ON(err < 0); /* sanity check; this can't really happen */ 981 WARN_ON(err); /* sanity check; this can't really happen */
984 } 982 }
985 983
986 /* suppress "tx-complete" interrupts */ 984 /* suppress "tx-complete" interrupts */
diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index dd8dc27fa32c..74ab67a169ec 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -215,7 +215,7 @@ static void virtscsi_ctrl_done(struct virtqueue *vq)
215static int virtscsi_kick_event(struct virtio_scsi *vscsi, 215static int virtscsi_kick_event(struct virtio_scsi *vscsi,
216 struct virtio_scsi_event_node *event_node) 216 struct virtio_scsi_event_node *event_node)
217{ 217{
218 int ret; 218 int err;
219 struct scatterlist sg; 219 struct scatterlist sg;
220 unsigned long flags; 220 unsigned long flags;
221 221
@@ -223,13 +223,14 @@ static int virtscsi_kick_event(struct virtio_scsi *vscsi,
223 223
224 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); 224 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
225 225
226 ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, GFP_ATOMIC); 226 err = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node,
227 if (ret >= 0) 227 GFP_ATOMIC);
228 if (!err)
228 virtqueue_kick(vscsi->event_vq.vq); 229 virtqueue_kick(vscsi->event_vq.vq);
229 230
230 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); 231 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
231 232
232 return ret; 233 return err;
233} 234}
234 235
235static int virtscsi_kick_event_all(struct virtio_scsi *vscsi) 236static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
@@ -410,22 +411,23 @@ static int virtscsi_kick_cmd(struct virtio_scsi_target_state *tgt,
410{ 411{
411 unsigned int out_num, in_num; 412 unsigned int out_num, in_num;
412 unsigned long flags; 413 unsigned long flags;
413 int ret; 414 int err;
415 bool needs_kick = false;
414 416
415 spin_lock_irqsave(&tgt->tgt_lock, flags); 417 spin_lock_irqsave(&tgt->tgt_lock, flags);
416 virtscsi_map_cmd(tgt, cmd, &out_num, &in_num, req_size, resp_size); 418 virtscsi_map_cmd(tgt, cmd, &out_num, &in_num, req_size, resp_size);
417 419
418 spin_lock(&vq->vq_lock); 420 spin_lock(&vq->vq_lock);
419 ret = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp); 421 err = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp);
420 spin_unlock(&tgt->tgt_lock); 422 spin_unlock(&tgt->tgt_lock);
421 if (ret >= 0) 423 if (!err)
422 ret = virtqueue_kick_prepare(vq->vq); 424 needs_kick = virtqueue_kick_prepare(vq->vq);
423 425
424 spin_unlock_irqrestore(&vq->vq_lock, flags); 426 spin_unlock_irqrestore(&vq->vq_lock, flags);
425 427
426 if (ret > 0) 428 if (needs_kick)
427 virtqueue_notify(vq->vq); 429 virtqueue_notify(vq->vq);
428 return ret; 430 return err;
429} 431}
430 432
431static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) 433static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
@@ -467,7 +469,7 @@ static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
467 469
468 if (virtscsi_kick_cmd(tgt, &vscsi->req_vq, cmd, 470 if (virtscsi_kick_cmd(tgt, &vscsi->req_vq, cmd,
469 sizeof cmd->req.cmd, sizeof cmd->resp.cmd, 471 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
470 GFP_ATOMIC) >= 0) 472 GFP_ATOMIC) == 0)
471 ret = 0; 473 ret = 0;
472 else 474 else
473 mempool_free(cmd, virtscsi_cmd_pool); 475 mempool_free(cmd, virtscsi_cmd_pool);
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 809b0de59c09..ee59b74768d9 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -10,33 +10,32 @@ static DEFINE_IDA(virtio_index_ida);
10static ssize_t device_show(struct device *_d, 10static ssize_t device_show(struct device *_d,
11 struct device_attribute *attr, char *buf) 11 struct device_attribute *attr, char *buf)
12{ 12{
13 struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 13 struct virtio_device *dev = dev_to_virtio(_d);
14 return sprintf(buf, "0x%04x\n", dev->id.device); 14 return sprintf(buf, "0x%04x\n", dev->id.device);
15} 15}
16static ssize_t vendor_show(struct device *_d, 16static ssize_t vendor_show(struct device *_d,
17 struct device_attribute *attr, char *buf) 17 struct device_attribute *attr, char *buf)
18{ 18{
19 struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 19 struct virtio_device *dev = dev_to_virtio(_d);
20 return sprintf(buf, "0x%04x\n", dev->id.vendor); 20 return sprintf(buf, "0x%04x\n", dev->id.vendor);
21} 21}
22static ssize_t status_show(struct device *_d, 22static ssize_t status_show(struct device *_d,
23 struct device_attribute *attr, char *buf) 23 struct device_attribute *attr, char *buf)
24{ 24{
25 struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 25 struct virtio_device *dev = dev_to_virtio(_d);
26 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev)); 26 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
27} 27}
28static ssize_t modalias_show(struct device *_d, 28static ssize_t modalias_show(struct device *_d,
29 struct device_attribute *attr, char *buf) 29 struct device_attribute *attr, char *buf)
30{ 30{
31 struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 31 struct virtio_device *dev = dev_to_virtio(_d);
32
33 return sprintf(buf, "virtio:d%08Xv%08X\n", 32 return sprintf(buf, "virtio:d%08Xv%08X\n",
34 dev->id.device, dev->id.vendor); 33 dev->id.device, dev->id.vendor);
35} 34}
36static ssize_t features_show(struct device *_d, 35static ssize_t features_show(struct device *_d,
37 struct device_attribute *attr, char *buf) 36 struct device_attribute *attr, char *buf)
38{ 37{
39 struct virtio_device *dev = container_of(_d, struct virtio_device, dev); 38 struct virtio_device *dev = dev_to_virtio(_d);
40 unsigned int i; 39 unsigned int i;
41 ssize_t len = 0; 40 ssize_t len = 0;
42 41
@@ -71,10 +70,10 @@ static inline int virtio_id_match(const struct virtio_device *dev,
71static int virtio_dev_match(struct device *_dv, struct device_driver *_dr) 70static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
72{ 71{
73 unsigned int i; 72 unsigned int i;
74 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev); 73 struct virtio_device *dev = dev_to_virtio(_dv);
75 const struct virtio_device_id *ids; 74 const struct virtio_device_id *ids;
76 75
77 ids = container_of(_dr, struct virtio_driver, driver)->id_table; 76 ids = drv_to_virtio(_dr)->id_table;
78 for (i = 0; ids[i].device; i++) 77 for (i = 0; ids[i].device; i++)
79 if (virtio_id_match(dev, &ids[i])) 78 if (virtio_id_match(dev, &ids[i]))
80 return 1; 79 return 1;
@@ -83,7 +82,7 @@ static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
83 82
84static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env) 83static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
85{ 84{
86 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev); 85 struct virtio_device *dev = dev_to_virtio(_dv);
87 86
88 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X", 87 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
89 dev->id.device, dev->id.vendor); 88 dev->id.device, dev->id.vendor);
@@ -98,8 +97,7 @@ void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
98 unsigned int fbit) 97 unsigned int fbit)
99{ 98{
100 unsigned int i; 99 unsigned int i;
101 struct virtio_driver *drv = container_of(vdev->dev.driver, 100 struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
102 struct virtio_driver, driver);
103 101
104 for (i = 0; i < drv->feature_table_size; i++) 102 for (i = 0; i < drv->feature_table_size; i++)
105 if (drv->feature_table[i] == fbit) 103 if (drv->feature_table[i] == fbit)
@@ -111,9 +109,8 @@ EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
111static int virtio_dev_probe(struct device *_d) 109static int virtio_dev_probe(struct device *_d)
112{ 110{
113 int err, i; 111 int err, i;
114 struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 112 struct virtio_device *dev = dev_to_virtio(_d);
115 struct virtio_driver *drv = container_of(dev->dev.driver, 113 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
116 struct virtio_driver, driver);
117 u32 device_features; 114 u32 device_features;
118 115
119 /* We have a driver! */ 116 /* We have a driver! */
@@ -152,9 +149,8 @@ static int virtio_dev_probe(struct device *_d)
152 149
153static int virtio_dev_remove(struct device *_d) 150static int virtio_dev_remove(struct device *_d)
154{ 151{
155 struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 152 struct virtio_device *dev = dev_to_virtio(_d);
156 struct virtio_driver *drv = container_of(dev->dev.driver, 153 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
157 struct virtio_driver, driver);
158 154
159 drv->remove(dev); 155 drv->remove(dev);
160 156
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 2a70558b36ea..d19fe3e323b4 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -139,10 +139,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
139 struct page *page = balloon_page_enqueue(vb_dev_info); 139 struct page *page = balloon_page_enqueue(vb_dev_info);
140 140
141 if (!page) { 141 if (!page) {
142 if (printk_ratelimit()) 142 dev_info_ratelimited(&vb->vdev->dev,
143 dev_printk(KERN_INFO, &vb->vdev->dev, 143 "Out of puff! Can't get %u pages\n",
144 "Out of puff! Can't get %u pages\n", 144 VIRTIO_BALLOON_PAGES_PER_PAGE);
145 VIRTIO_BALLOON_PAGES_PER_PAGE);
146 /* Sleep for at least 1/5 of a second before retry. */ 145 /* Sleep for at least 1/5 of a second before retry. */
147 msleep(200); 146 msleep(200);
148 break; 147 break;
diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c
index 6b1b7e184939..634f80bcdbd7 100644
--- a/drivers/virtio/virtio_mmio.c
+++ b/drivers/virtio/virtio_mmio.c
@@ -225,7 +225,7 @@ static void vm_notify(struct virtqueue *vq)
225 225
226 /* We write the queue's selector into the notification register to 226 /* We write the queue's selector into the notification register to
227 * signal the other end */ 227 * signal the other end */
228 writel(virtqueue_get_queue_index(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); 228 writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
229} 229}
230 230
231/* Notify all virtqueues on an interrupt. */ 231/* Notify all virtqueues on an interrupt. */
@@ -266,7 +266,7 @@ static void vm_del_vq(struct virtqueue *vq)
266 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); 266 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
267 struct virtio_mmio_vq_info *info = vq->priv; 267 struct virtio_mmio_vq_info *info = vq->priv;
268 unsigned long flags, size; 268 unsigned long flags, size;
269 unsigned int index = virtqueue_get_queue_index(vq); 269 unsigned int index = vq->index;
270 270
271 spin_lock_irqsave(&vm_dev->lock, flags); 271 spin_lock_irqsave(&vm_dev->lock, flags);
272 list_del(&info->node); 272 list_del(&info->node);
@@ -521,25 +521,33 @@ static int vm_cmdline_set(const char *device,
521 int err; 521 int err;
522 struct resource resources[2] = {}; 522 struct resource resources[2] = {};
523 char *str; 523 char *str;
524 long long int base; 524 long long int base, size;
525 unsigned int irq;
525 int processed, consumed = 0; 526 int processed, consumed = 0;
526 struct platform_device *pdev; 527 struct platform_device *pdev;
527 528
528 resources[0].flags = IORESOURCE_MEM; 529 /* Consume "size" part of the command line parameter */
529 resources[1].flags = IORESOURCE_IRQ; 530 size = memparse(device, &str);
530
531 resources[0].end = memparse(device, &str) - 1;
532 531
532 /* Get "@<base>:<irq>[:<id>]" chunks */
533 processed = sscanf(str, "@%lli:%u%n:%d%n", 533 processed = sscanf(str, "@%lli:%u%n:%d%n",
534 &base, &resources[1].start, &consumed, 534 &base, &irq, &consumed,
535 &vm_cmdline_id, &consumed); 535 &vm_cmdline_id, &consumed);
536 536
537 if (processed < 2 || processed > 3 || str[consumed]) 537 /*
538 * sscanf() must processes at least 2 chunks; also there
539 * must be no extra characters after the last chunk, so
540 * str[consumed] must be '\0'
541 */
542 if (processed < 2 || str[consumed])
538 return -EINVAL; 543 return -EINVAL;
539 544
545 resources[0].flags = IORESOURCE_MEM;
540 resources[0].start = base; 546 resources[0].start = base;
541 resources[0].end += base; 547 resources[0].end = base + size - 1;
542 resources[1].end = resources[1].start; 548
549 resources[1].flags = IORESOURCE_IRQ;
550 resources[1].start = resources[1].end = irq;
543 551
544 if (!vm_cmdline_parent_registered) { 552 if (!vm_cmdline_parent_registered) {
545 err = device_register(&vm_cmdline_parent); 553 err = device_register(&vm_cmdline_parent);
diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c
index c33aea36598a..e3ecc94591ad 100644
--- a/drivers/virtio/virtio_pci.c
+++ b/drivers/virtio/virtio_pci.c
@@ -203,8 +203,7 @@ static void vp_notify(struct virtqueue *vq)
203 203
204 /* we write the queue's selector into the notification register to 204 /* we write the queue's selector into the notification register to
205 * signal the other end */ 205 * signal the other end */
206 iowrite16(virtqueue_get_queue_index(vq), 206 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
207 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
208} 207}
209 208
210/* Handle a configuration change: Tell driver if it wants to know. */ 209/* Handle a configuration change: Tell driver if it wants to know. */
@@ -479,8 +478,7 @@ static void vp_del_vq(struct virtqueue *vq)
479 list_del(&info->node); 478 list_del(&info->node);
480 spin_unlock_irqrestore(&vp_dev->lock, flags); 479 spin_unlock_irqrestore(&vp_dev->lock, flags);
481 480
482 iowrite16(virtqueue_get_queue_index(vq), 481 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
483 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
484 482
485 if (vp_dev->msix_enabled) { 483 if (vp_dev->msix_enabled) {
486 iowrite16(VIRTIO_MSI_NO_VECTOR, 484 iowrite16(VIRTIO_MSI_NO_VECTOR,
@@ -830,16 +828,4 @@ static struct pci_driver virtio_pci_driver = {
830#endif 828#endif
831}; 829};
832 830
833static int __init virtio_pci_init(void) 831module_pci_driver(virtio_pci_driver);
834{
835 return pci_register_driver(&virtio_pci_driver);
836}
837
838module_init(virtio_pci_init);
839
840static void __exit virtio_pci_exit(void)
841{
842 pci_unregister_driver(&virtio_pci_driver);
843}
844
845module_exit(virtio_pci_exit);
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index e639584b2dbd..ffd7e7da5d3b 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -93,8 +93,6 @@ struct vring_virtqueue
93 /* Host publishes avail event idx */ 93 /* Host publishes avail event idx */
94 bool event; 94 bool event;
95 95
96 /* Number of free buffers */
97 unsigned int num_free;
98 /* Head of free buffer list. */ 96 /* Head of free buffer list. */
99 unsigned int free_head; 97 unsigned int free_head;
100 /* Number we've added since last sync. */ 98 /* Number we've added since last sync. */
@@ -106,9 +104,6 @@ struct vring_virtqueue
106 /* How to notify other side. FIXME: commonalize hcalls! */ 104 /* How to notify other side. FIXME: commonalize hcalls! */
107 void (*notify)(struct virtqueue *vq); 105 void (*notify)(struct virtqueue *vq);
108 106
109 /* Index of the queue */
110 int queue_index;
111
112#ifdef DEBUG 107#ifdef DEBUG
113 /* They're supposed to lock for us. */ 108 /* They're supposed to lock for us. */
114 unsigned int in_use; 109 unsigned int in_use;
@@ -135,6 +130,13 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
135 unsigned head; 130 unsigned head;
136 int i; 131 int i;
137 132
133 /*
134 * We require lowmem mappings for the descriptors because
135 * otherwise virt_to_phys will give us bogus addresses in the
136 * virtqueue.
137 */
138 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
139
138 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp); 140 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
139 if (!desc) 141 if (!desc)
140 return -ENOMEM; 142 return -ENOMEM;
@@ -160,7 +162,7 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
160 desc[i-1].next = 0; 162 desc[i-1].next = 0;
161 163
162 /* We're about to use a buffer */ 164 /* We're about to use a buffer */
163 vq->num_free--; 165 vq->vq.num_free--;
164 166
165 /* Use a single buffer which doesn't continue */ 167 /* Use a single buffer which doesn't continue */
166 head = vq->free_head; 168 head = vq->free_head;
@@ -174,13 +176,6 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
174 return head; 176 return head;
175} 177}
176 178
177int virtqueue_get_queue_index(struct virtqueue *_vq)
178{
179 struct vring_virtqueue *vq = to_vvq(_vq);
180 return vq->queue_index;
181}
182EXPORT_SYMBOL_GPL(virtqueue_get_queue_index);
183
184/** 179/**
185 * virtqueue_add_buf - expose buffer to other end 180 * virtqueue_add_buf - expose buffer to other end
186 * @vq: the struct virtqueue we're talking about. 181 * @vq: the struct virtqueue we're talking about.
@@ -193,10 +188,7 @@ EXPORT_SYMBOL_GPL(virtqueue_get_queue_index);
193 * Caller must ensure we don't call this with other virtqueue operations 188 * Caller must ensure we don't call this with other virtqueue operations
194 * at the same time (except where noted). 189 * at the same time (except where noted).
195 * 190 *
196 * Returns remaining capacity of queue or a negative error 191 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
197 * (ie. ENOSPC). Note that it only really makes sense to treat all
198 * positive return values as "available": indirect buffers mean that
199 * we can put an entire sg[] array inside a single queue entry.
200 */ 192 */
201int virtqueue_add_buf(struct virtqueue *_vq, 193int virtqueue_add_buf(struct virtqueue *_vq,
202 struct scatterlist sg[], 194 struct scatterlist sg[],
@@ -228,7 +220,7 @@ int virtqueue_add_buf(struct virtqueue *_vq,
228 220
229 /* If the host supports indirect descriptor tables, and we have multiple 221 /* If the host supports indirect descriptor tables, and we have multiple
230 * buffers, then go indirect. FIXME: tune this threshold */ 222 * buffers, then go indirect. FIXME: tune this threshold */
231 if (vq->indirect && (out + in) > 1 && vq->num_free) { 223 if (vq->indirect && (out + in) > 1 && vq->vq.num_free) {
232 head = vring_add_indirect(vq, sg, out, in, gfp); 224 head = vring_add_indirect(vq, sg, out, in, gfp);
233 if (likely(head >= 0)) 225 if (likely(head >= 0))
234 goto add_head; 226 goto add_head;
@@ -237,9 +229,9 @@ int virtqueue_add_buf(struct virtqueue *_vq,
237 BUG_ON(out + in > vq->vring.num); 229 BUG_ON(out + in > vq->vring.num);
238 BUG_ON(out + in == 0); 230 BUG_ON(out + in == 0);
239 231
240 if (vq->num_free < out + in) { 232 if (vq->vq.num_free < out + in) {
241 pr_debug("Can't add buf len %i - avail = %i\n", 233 pr_debug("Can't add buf len %i - avail = %i\n",
242 out + in, vq->num_free); 234 out + in, vq->vq.num_free);
243 /* FIXME: for historical reasons, we force a notify here if 235 /* FIXME: for historical reasons, we force a notify here if
244 * there are outgoing parts to the buffer. Presumably the 236 * there are outgoing parts to the buffer. Presumably the
245 * host should service the ring ASAP. */ 237 * host should service the ring ASAP. */
@@ -250,7 +242,7 @@ int virtqueue_add_buf(struct virtqueue *_vq,
250 } 242 }
251 243
252 /* We're about to use some buffers from the free list. */ 244 /* We're about to use some buffers from the free list. */
253 vq->num_free -= out + in; 245 vq->vq.num_free -= out + in;
254 246
255 head = vq->free_head; 247 head = vq->free_head;
256 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { 248 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
@@ -296,7 +288,7 @@ add_head:
296 pr_debug("Added buffer head %i to %p\n", head, vq); 288 pr_debug("Added buffer head %i to %p\n", head, vq);
297 END_USE(vq); 289 END_USE(vq);
298 290
299 return vq->num_free; 291 return 0;
300} 292}
301EXPORT_SYMBOL_GPL(virtqueue_add_buf); 293EXPORT_SYMBOL_GPL(virtqueue_add_buf);
302 294
@@ -393,13 +385,13 @@ static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
393 385
394 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { 386 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
395 i = vq->vring.desc[i].next; 387 i = vq->vring.desc[i].next;
396 vq->num_free++; 388 vq->vq.num_free++;
397 } 389 }
398 390
399 vq->vring.desc[i].next = vq->free_head; 391 vq->vring.desc[i].next = vq->free_head;
400 vq->free_head = head; 392 vq->free_head = head;
401 /* Plus final descriptor */ 393 /* Plus final descriptor */
402 vq->num_free++; 394 vq->vq.num_free++;
403} 395}
404 396
405static inline bool more_used(const struct vring_virtqueue *vq) 397static inline bool more_used(const struct vring_virtqueue *vq)
@@ -599,7 +591,7 @@ void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
599 return buf; 591 return buf;
600 } 592 }
601 /* That should have freed everything. */ 593 /* That should have freed everything. */
602 BUG_ON(vq->num_free != vq->vring.num); 594 BUG_ON(vq->vq.num_free != vq->vring.num);
603 595
604 END_USE(vq); 596 END_USE(vq);
605 return NULL; 597 return NULL;
@@ -653,12 +645,13 @@ struct virtqueue *vring_new_virtqueue(unsigned int index,
653 vq->vq.callback = callback; 645 vq->vq.callback = callback;
654 vq->vq.vdev = vdev; 646 vq->vq.vdev = vdev;
655 vq->vq.name = name; 647 vq->vq.name = name;
648 vq->vq.num_free = num;
649 vq->vq.index = index;
656 vq->notify = notify; 650 vq->notify = notify;
657 vq->weak_barriers = weak_barriers; 651 vq->weak_barriers = weak_barriers;
658 vq->broken = false; 652 vq->broken = false;
659 vq->last_used_idx = 0; 653 vq->last_used_idx = 0;
660 vq->num_added = 0; 654 vq->num_added = 0;
661 vq->queue_index = index;
662 list_add_tail(&vq->vq.list, &vdev->vqs); 655 list_add_tail(&vq->vq.list, &vdev->vqs);
663#ifdef DEBUG 656#ifdef DEBUG
664 vq->in_use = false; 657 vq->in_use = false;
@@ -673,7 +666,6 @@ struct virtqueue *vring_new_virtqueue(unsigned int index,
673 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 666 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
674 667
675 /* Put everything in free lists. */ 668 /* Put everything in free lists. */
676 vq->num_free = num;
677 vq->free_head = 0; 669 vq->free_head = 0;
678 for (i = 0; i < num-1; i++) { 670 for (i = 0; i < num-1; i++) {
679 vq->vring.desc[i].next = i+1; 671 vq->vring.desc[i].next = i+1;
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 533b1157f22e..cf8adb1f5b2c 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -16,12 +16,20 @@
16 * @name: the name of this virtqueue (mainly for debugging) 16 * @name: the name of this virtqueue (mainly for debugging)
17 * @vdev: the virtio device this queue was created for. 17 * @vdev: the virtio device this queue was created for.
18 * @priv: a pointer for the virtqueue implementation to use. 18 * @priv: a pointer for the virtqueue implementation to use.
19 * @index: the zero-based ordinal number for this queue.
20 * @num_free: number of elements we expect to be able to fit.
21 *
22 * A note on @num_free: with indirect buffers, each buffer needs one
23 * element in the queue, otherwise a buffer will need one element per
24 * sg element.
19 */ 25 */
20struct virtqueue { 26struct virtqueue {
21 struct list_head list; 27 struct list_head list;
22 void (*callback)(struct virtqueue *vq); 28 void (*callback)(struct virtqueue *vq);
23 const char *name; 29 const char *name;
24 struct virtio_device *vdev; 30 struct virtio_device *vdev;
31 unsigned int index;
32 unsigned int num_free;
25 void *priv; 33 void *priv;
26}; 34};
27 35
@@ -50,7 +58,11 @@ void *virtqueue_detach_unused_buf(struct virtqueue *vq);
50 58
51unsigned int virtqueue_get_vring_size(struct virtqueue *vq); 59unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
52 60
53int virtqueue_get_queue_index(struct virtqueue *vq); 61/* FIXME: Obsolete accessor, but required for virtio_net merge. */
62static inline unsigned int virtqueue_get_queue_index(struct virtqueue *vq)
63{
64 return vq->index;
65}
54 66
55/** 67/**
56 * virtio_device - representation of a device using virtio 68 * virtio_device - representation of a device using virtio
@@ -73,7 +85,11 @@ struct virtio_device {
73 void *priv; 85 void *priv;
74}; 86};
75 87
76#define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev) 88static inline struct virtio_device *dev_to_virtio(struct device *_dev)
89{
90 return container_of(_dev, struct virtio_device, dev);
91}
92
77int register_virtio_device(struct virtio_device *dev); 93int register_virtio_device(struct virtio_device *dev);
78void unregister_virtio_device(struct virtio_device *dev); 94void unregister_virtio_device(struct virtio_device *dev);
79 95
@@ -103,6 +119,11 @@ struct virtio_driver {
103#endif 119#endif
104}; 120};
105 121
122static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
123{
124 return container_of(drv, struct virtio_driver, driver);
125}
126
106int register_virtio_driver(struct virtio_driver *drv); 127int register_virtio_driver(struct virtio_driver *drv);
107void unregister_virtio_driver(struct virtio_driver *drv); 128void unregister_virtio_driver(struct virtio_driver *drv);
108#endif /* _LINUX_VIRTIO_H */ 129#endif /* _LINUX_VIRTIO_H */
diff --git a/include/linux/virtio_scsi.h b/include/linux/virtio_scsi.h
index d6b4440387b7..4195b97a3def 100644
--- a/include/linux/virtio_scsi.h
+++ b/include/linux/virtio_scsi.h
@@ -1,7 +1,31 @@
1/*
2 * This header is BSD licensed so anyone can use the definitions to implement
3 * compatible drivers/servers.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
1#ifndef _LINUX_VIRTIO_SCSI_H 27#ifndef _LINUX_VIRTIO_SCSI_H
2#define _LINUX_VIRTIO_SCSI_H 28#define _LINUX_VIRTIO_SCSI_H
3/* This header is BSD licensed so anyone can use the definitions to implement
4 * compatible drivers/servers. */
5 29
6#define VIRTIO_SCSI_CDB_SIZE 32 30#define VIRTIO_SCSI_CDB_SIZE 32
7#define VIRTIO_SCSI_SENSE_SIZE 96 31#define VIRTIO_SCSI_SENSE_SIZE 96
diff --git a/include/uapi/linux/virtio_ids.h b/include/uapi/linux/virtio_ids.h
index 270fb22c5811..a7630d04029f 100644
--- a/include/uapi/linux/virtio_ids.h
+++ b/include/uapi/linux/virtio_ids.h
@@ -37,5 +37,6 @@
37#define VIRTIO_ID_RPMSG 7 /* virtio remote processor messaging */ 37#define VIRTIO_ID_RPMSG 7 /* virtio remote processor messaging */
38#define VIRTIO_ID_SCSI 8 /* virtio scsi */ 38#define VIRTIO_ID_SCSI 8 /* virtio scsi */
39#define VIRTIO_ID_9P 9 /* 9p virtio console */ 39#define VIRTIO_ID_9P 9 /* 9p virtio console */
40#define VIRTIO_ID_RPROC_SERIAL 11 /* virtio remoteproc serial link */
40 41
41#endif /* _LINUX_VIRTIO_IDS_H */ 42#endif /* _LINUX_VIRTIO_IDS_H */
diff --git a/mm/highmem.c b/mm/highmem.c
index d999077431df..b32b70cdaed6 100644
--- a/mm/highmem.c
+++ b/mm/highmem.c
@@ -105,6 +105,7 @@ struct page *kmap_to_page(void *vaddr)
105 105
106 return virt_to_page(addr); 106 return virt_to_page(addr);
107} 107}
108EXPORT_SYMBOL(kmap_to_page);
108 109
109static void flush_all_zero_pkmaps(void) 110static void flush_all_zero_pkmaps(void)
110{ 111{
diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index 35b8911b1c8e..fd05c81cb348 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -39,6 +39,7 @@
39#include <linux/inet.h> 39#include <linux/inet.h>
40#include <linux/idr.h> 40#include <linux/idr.h>
41#include <linux/file.h> 41#include <linux/file.h>
42#include <linux/highmem.h>
42#include <linux/slab.h> 43#include <linux/slab.h>
43#include <net/9p/9p.h> 44#include <net/9p/9p.h>
44#include <linux/parser.h> 45#include <linux/parser.h>
@@ -325,7 +326,7 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
325 int count = nr_pages; 326 int count = nr_pages;
326 while (nr_pages) { 327 while (nr_pages) {
327 s = rest_of_page(data); 328 s = rest_of_page(data);
328 pages[index++] = virt_to_page(data); 329 pages[index++] = kmap_to_page(data);
329 data += s; 330 data += s;
330 nr_pages--; 331 nr_pages--;
331 } 332 }
diff --git a/tools/lguest/lguest.c b/tools/lguest/lguest.c
index fd2f9221b241..07a03452c227 100644
--- a/tools/lguest/lguest.c
+++ b/tools/lguest/lguest.c
@@ -179,29 +179,6 @@ static struct termios orig_term;
179#define wmb() __asm__ __volatile__("" : : : "memory") 179#define wmb() __asm__ __volatile__("" : : : "memory")
180#define mb() __asm__ __volatile__("" : : : "memory") 180#define mb() __asm__ __volatile__("" : : : "memory")
181 181
182/*
183 * Convert an iovec element to the given type.
184 *
185 * This is a fairly ugly trick: we need to know the size of the type and
186 * alignment requirement to check the pointer is kosher. It's also nice to
187 * have the name of the type in case we report failure.
188 *
189 * Typing those three things all the time is cumbersome and error prone, so we
190 * have a macro which sets them all up and passes to the real function.
191 */
192#define convert(iov, type) \
193 ((type *)_convert((iov), sizeof(type), __alignof__(type), #type))
194
195static void *_convert(struct iovec *iov, size_t size, size_t align,
196 const char *name)
197{
198 if (iov->iov_len != size)
199 errx(1, "Bad iovec size %zu for %s", iov->iov_len, name);
200 if ((unsigned long)iov->iov_base % align != 0)
201 errx(1, "Bad alignment %p for %s", iov->iov_base, name);
202 return iov->iov_base;
203}
204
205/* Wrapper for the last available index. Makes it easier to change. */ 182/* Wrapper for the last available index. Makes it easier to change. */
206#define lg_last_avail(vq) ((vq)->last_avail_idx) 183#define lg_last_avail(vq) ((vq)->last_avail_idx)
207 184
@@ -228,7 +205,8 @@ static bool iov_empty(const struct iovec iov[], unsigned int num_iov)
228} 205}
229 206
230/* Take len bytes from the front of this iovec. */ 207/* Take len bytes from the front of this iovec. */
231static void iov_consume(struct iovec iov[], unsigned num_iov, unsigned len) 208static void iov_consume(struct iovec iov[], unsigned num_iov,
209 void *dest, unsigned len)
232{ 210{
233 unsigned int i; 211 unsigned int i;
234 212
@@ -236,11 +214,16 @@ static void iov_consume(struct iovec iov[], unsigned num_iov, unsigned len)
236 unsigned int used; 214 unsigned int used;
237 215
238 used = iov[i].iov_len < len ? iov[i].iov_len : len; 216 used = iov[i].iov_len < len ? iov[i].iov_len : len;
217 if (dest) {
218 memcpy(dest, iov[i].iov_base, used);
219 dest += used;
220 }
239 iov[i].iov_base += used; 221 iov[i].iov_base += used;
240 iov[i].iov_len -= used; 222 iov[i].iov_len -= used;
241 len -= used; 223 len -= used;
242 } 224 }
243 assert(len == 0); 225 if (len != 0)
226 errx(1, "iovec too short!");
244} 227}
245 228
246/* The device virtqueue descriptors are followed by feature bitmasks. */ 229/* The device virtqueue descriptors are followed by feature bitmasks. */
@@ -864,7 +847,7 @@ static void console_output(struct virtqueue *vq)
864 warn("Write to stdout gave %i (%d)", len, errno); 847 warn("Write to stdout gave %i (%d)", len, errno);
865 break; 848 break;
866 } 849 }
867 iov_consume(iov, out, len); 850 iov_consume(iov, out, NULL, len);
868 } 851 }
869 852
870 /* 853 /*
@@ -1591,9 +1574,9 @@ static void blk_request(struct virtqueue *vq)
1591{ 1574{
1592 struct vblk_info *vblk = vq->dev->priv; 1575 struct vblk_info *vblk = vq->dev->priv;
1593 unsigned int head, out_num, in_num, wlen; 1576 unsigned int head, out_num, in_num, wlen;
1594 int ret; 1577 int ret, i;
1595 u8 *in; 1578 u8 *in;
1596 struct virtio_blk_outhdr *out; 1579 struct virtio_blk_outhdr out;
1597 struct iovec iov[vq->vring.num]; 1580 struct iovec iov[vq->vring.num];
1598 off64_t off; 1581 off64_t off;
1599 1582
@@ -1603,32 +1586,36 @@ static void blk_request(struct virtqueue *vq)
1603 */ 1586 */
1604 head = wait_for_vq_desc(vq, iov, &out_num, &in_num); 1587 head = wait_for_vq_desc(vq, iov, &out_num, &in_num);
1605 1588
1606 /* 1589 /* Copy the output header from the front of the iov (adjusts iov) */
1607 * Every block request should contain at least one output buffer 1590 iov_consume(iov, out_num, &out, sizeof(out));
1608 * (detailing the location on disk and the type of request) and one 1591
1609 * input buffer (to hold the result). 1592 /* Find and trim end of iov input array, for our status byte. */
1610 */ 1593 in = NULL;
1611 if (out_num == 0 || in_num == 0) 1594 for (i = out_num + in_num - 1; i >= out_num; i--) {
1612 errx(1, "Bad virtblk cmd %u out=%u in=%u", 1595 if (iov[i].iov_len > 0) {
1613 head, out_num, in_num); 1596 in = iov[i].iov_base + iov[i].iov_len - 1;
1597 iov[i].iov_len--;
1598 break;
1599 }
1600 }
1601 if (!in)
1602 errx(1, "Bad virtblk cmd with no room for status");
1614 1603
1615 out = convert(&iov[0], struct virtio_blk_outhdr);
1616 in = convert(&iov[out_num+in_num-1], u8);
1617 /* 1604 /*
1618 * For historical reasons, block operations are expressed in 512 byte 1605 * For historical reasons, block operations are expressed in 512 byte
1619 * "sectors". 1606 * "sectors".
1620 */ 1607 */
1621 off = out->sector * 512; 1608 off = out.sector * 512;
1622 1609
1623 /* 1610 /*
1624 * In general the virtio block driver is allowed to try SCSI commands. 1611 * In general the virtio block driver is allowed to try SCSI commands.
1625 * It'd be nice if we supported eject, for example, but we don't. 1612 * It'd be nice if we supported eject, for example, but we don't.
1626 */ 1613 */
1627 if (out->type & VIRTIO_BLK_T_SCSI_CMD) { 1614 if (out.type & VIRTIO_BLK_T_SCSI_CMD) {
1628 fprintf(stderr, "Scsi commands unsupported\n"); 1615 fprintf(stderr, "Scsi commands unsupported\n");
1629 *in = VIRTIO_BLK_S_UNSUPP; 1616 *in = VIRTIO_BLK_S_UNSUPP;
1630 wlen = sizeof(*in); 1617 wlen = sizeof(*in);
1631 } else if (out->type & VIRTIO_BLK_T_OUT) { 1618 } else if (out.type & VIRTIO_BLK_T_OUT) {
1632 /* 1619 /*
1633 * Write 1620 * Write
1634 * 1621 *
@@ -1636,10 +1623,10 @@ static void blk_request(struct virtqueue *vq)
1636 * if they try to write past end. 1623 * if they try to write past end.
1637 */ 1624 */
1638 if (lseek64(vblk->fd, off, SEEK_SET) != off) 1625 if (lseek64(vblk->fd, off, SEEK_SET) != off)
1639 err(1, "Bad seek to sector %llu", out->sector); 1626 err(1, "Bad seek to sector %llu", out.sector);
1640 1627
1641 ret = writev(vblk->fd, iov+1, out_num-1); 1628 ret = writev(vblk->fd, iov, out_num);
1642 verbose("WRITE to sector %llu: %i\n", out->sector, ret); 1629 verbose("WRITE to sector %llu: %i\n", out.sector, ret);
1643 1630
1644 /* 1631 /*
1645 * Grr... Now we know how long the descriptor they sent was, we 1632 * Grr... Now we know how long the descriptor they sent was, we
@@ -1655,7 +1642,7 @@ static void blk_request(struct virtqueue *vq)
1655 1642
1656 wlen = sizeof(*in); 1643 wlen = sizeof(*in);
1657 *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); 1644 *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR);
1658 } else if (out->type & VIRTIO_BLK_T_FLUSH) { 1645 } else if (out.type & VIRTIO_BLK_T_FLUSH) {
1659 /* Flush */ 1646 /* Flush */
1660 ret = fdatasync(vblk->fd); 1647 ret = fdatasync(vblk->fd);
1661 verbose("FLUSH fdatasync: %i\n", ret); 1648 verbose("FLUSH fdatasync: %i\n", ret);
@@ -1669,10 +1656,9 @@ static void blk_request(struct virtqueue *vq)
1669 * if they try to read past end. 1656 * if they try to read past end.
1670 */ 1657 */
1671 if (lseek64(vblk->fd, off, SEEK_SET) != off) 1658 if (lseek64(vblk->fd, off, SEEK_SET) != off)
1672 err(1, "Bad seek to sector %llu", out->sector); 1659 err(1, "Bad seek to sector %llu", out.sector);
1673 1660
1674 ret = readv(vblk->fd, iov+1, in_num-1); 1661 ret = readv(vblk->fd, iov + out_num, in_num);
1675 verbose("READ from sector %llu: %i\n", out->sector, ret);
1676 if (ret >= 0) { 1662 if (ret >= 0) {
1677 wlen = sizeof(*in) + ret; 1663 wlen = sizeof(*in) + ret;
1678 *in = VIRTIO_BLK_S_OK; 1664 *in = VIRTIO_BLK_S_OK;
@@ -1758,7 +1744,7 @@ static void rng_input(struct virtqueue *vq)
1758 len = readv(rng_info->rfd, iov, in_num); 1744 len = readv(rng_info->rfd, iov, in_num);
1759 if (len <= 0) 1745 if (len <= 0)
1760 err(1, "Read from /dev/random gave %i", len); 1746 err(1, "Read from /dev/random gave %i", len);
1761 iov_consume(iov, in_num, len); 1747 iov_consume(iov, in_num, NULL, len);
1762 totlen += len; 1748 totlen += len;
1763 } 1749 }
1764 1750
diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c
index 6d25dcd2e97a..fcc9aa25fd08 100644
--- a/tools/virtio/virtio_test.c
+++ b/tools/virtio/virtio_test.c
@@ -164,7 +164,7 @@ static void run_test(struct vdev_info *dev, struct vq_info *vq,
164 r = virtqueue_add_buf(vq->vq, &sl, 1, 0, 164 r = virtqueue_add_buf(vq->vq, &sl, 1, 0,
165 dev->buf + started, 165 dev->buf + started,
166 GFP_ATOMIC); 166 GFP_ATOMIC);
167 if (likely(r >= 0)) { 167 if (likely(r == 0)) {
168 ++started; 168 ++started;
169 virtqueue_kick(vq->vq); 169 virtqueue_kick(vq->vq);
170 } 170 }
@@ -177,7 +177,7 @@ static void run_test(struct vdev_info *dev, struct vq_info *vq,
177 r = 0; 177 r = 0;
178 } 178 }
179 179
180 } while (r >= 0); 180 } while (r == 0);
181 if (completed == completed_before) 181 if (completed == completed_before)
182 ++spurious; 182 ++spurious;
183 assert(completed <= bufs); 183 assert(completed <= bufs);