interface.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /******************************************************************************
  2. * arch/xen/drivers/blkif/backend/interface.c
  3. *
  4. * Block-device interface management.
  5. *
  6. * Copyright (c) 2004, Keir Fraser
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. */
  32. #include "common.h"
  33. #include <xen/evtchn.h>
  34. #include <linux/kthread.h>
  35. static kmem_cache_t *blkif_cachep;
  36. blkif_t *blkif_alloc(domid_t domid)
  37. {
  38. blkif_t *blkif;
  39. blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL);
  40. if (!blkif)
  41. return ERR_PTR(-ENOMEM);
  42. memset(blkif, 0, sizeof(*blkif));
  43. blkif->domid = domid;
  44. spin_lock_init(&blkif->blk_ring_lock);
  45. atomic_set(&blkif->refcnt, 1);
  46. init_waitqueue_head(&blkif->wq);
  47. blkif->st_print = jiffies;
  48. init_waitqueue_head(&blkif->waiting_to_free);
  49. return blkif;
  50. }
  51. static int map_frontend_page(blkif_t *blkif, unsigned long shared_page)
  52. {
  53. struct gnttab_map_grant_ref op;
  54. gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
  55. GNTMAP_host_map, shared_page, blkif->domid);
  56. if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
  57. BUG();
  58. if (op.status) {
  59. DPRINTK(" Grant table operation failure !\n");
  60. return op.status;
  61. }
  62. blkif->shmem_ref = shared_page;
  63. blkif->shmem_handle = op.handle;
  64. return 0;
  65. }
  66. static void unmap_frontend_page(blkif_t *blkif)
  67. {
  68. struct gnttab_unmap_grant_ref op;
  69. gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
  70. GNTMAP_host_map, blkif->shmem_handle);
  71. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  72. BUG();
  73. }
  74. int blkif_map(blkif_t *blkif, unsigned long shared_page, unsigned int evtchn)
  75. {
  76. int err;
  77. /* Already connected through? */
  78. if (blkif->irq)
  79. return 0;
  80. if ( (blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE)) == NULL )
  81. return -ENOMEM;
  82. err = map_frontend_page(blkif, shared_page);
  83. if (err) {
  84. free_vm_area(blkif->blk_ring_area);
  85. return err;
  86. }
  87. switch (blkif->blk_protocol) {
  88. case BLKIF_PROTOCOL_NATIVE:
  89. {
  90. blkif_sring_t *sring;
  91. sring = (blkif_sring_t *)blkif->blk_ring_area->addr;
  92. BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
  93. break;
  94. }
  95. case BLKIF_PROTOCOL_X86_32:
  96. {
  97. blkif_x86_32_sring_t *sring_x86_32;
  98. sring_x86_32 = (blkif_x86_32_sring_t *)blkif->blk_ring_area->addr;
  99. BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
  100. break;
  101. }
  102. case BLKIF_PROTOCOL_X86_64:
  103. {
  104. blkif_x86_64_sring_t *sring_x86_64;
  105. sring_x86_64 = (blkif_x86_64_sring_t *)blkif->blk_ring_area->addr;
  106. BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
  107. break;
  108. }
  109. default:
  110. BUG();
  111. }
  112. err = bind_interdomain_evtchn_to_irqhandler(
  113. blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif);
  114. if (err < 0)
  115. {
  116. unmap_frontend_page(blkif);
  117. free_vm_area(blkif->blk_ring_area);
  118. blkif->blk_rings.common.sring = NULL;
  119. return err;
  120. }
  121. blkif->irq = err;
  122. return 0;
  123. }
  124. void blkif_disconnect(blkif_t *blkif)
  125. {
  126. if (blkif->xenblkd) {
  127. kthread_stop(blkif->xenblkd);
  128. blkif->xenblkd = NULL;
  129. }
  130. atomic_dec(&blkif->refcnt);
  131. wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
  132. atomic_inc(&blkif->refcnt);
  133. if (blkif->irq) {
  134. unbind_from_irqhandler(blkif->irq, blkif);
  135. blkif->irq = 0;
  136. }
  137. if (blkif->blk_rings.common.sring) {
  138. unmap_frontend_page(blkif);
  139. free_vm_area(blkif->blk_ring_area);
  140. blkif->blk_rings.common.sring = NULL;
  141. }
  142. }
  143. void blkif_free(blkif_t *blkif)
  144. {
  145. if (!atomic_dec_and_test(&blkif->refcnt))
  146. BUG();
  147. kmem_cache_free(blkif_cachep, blkif);
  148. }
  149. void __init blkif_interface_init(void)
  150. {
  151. blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t),
  152. 0, 0, NULL, NULL);
  153. }