interface.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/events.h>
  34. #include <xen/grant_table.h>
  35. #include <linux/kthread.h>
  36. static struct kmem_cache *blkif_cachep;
  37. blkif_t *blkif_alloc(domid_t domid)
  38. {
  39. blkif_t *blkif;
  40. blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL);
  41. if (!blkif)
  42. return ERR_PTR(-ENOMEM);
  43. memset(blkif, 0, sizeof(*blkif));
  44. blkif->domid = domid;
  45. spin_lock_init(&blkif->blk_ring_lock);
  46. atomic_set(&blkif->refcnt, 1);
  47. init_waitqueue_head(&blkif->wq);
  48. blkif->st_print = jiffies;
  49. init_waitqueue_head(&blkif->waiting_to_free);
  50. return blkif;
  51. }
  52. static int map_frontend_page(blkif_t *blkif, unsigned long shared_page)
  53. {
  54. struct gnttab_map_grant_ref op;
  55. gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
  56. GNTMAP_host_map, shared_page, blkif->domid);
  57. if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
  58. BUG();
  59. if (op.status) {
  60. DPRINTK(" Grant table operation failure !\n");
  61. return op.status;
  62. }
  63. blkif->shmem_ref = shared_page;
  64. blkif->shmem_handle = op.handle;
  65. return 0;
  66. }
  67. static void unmap_frontend_page(blkif_t *blkif)
  68. {
  69. struct gnttab_unmap_grant_ref op;
  70. gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
  71. GNTMAP_host_map, blkif->shmem_handle);
  72. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  73. BUG();
  74. }
  75. int blkif_map(blkif_t *blkif, unsigned long shared_page, unsigned int evtchn)
  76. {
  77. int err;
  78. /* Already connected through? */
  79. if (blkif->irq)
  80. return 0;
  81. if ( (blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE)) == NULL )
  82. return -ENOMEM;
  83. err = map_frontend_page(blkif, shared_page);
  84. if (err) {
  85. free_vm_area(blkif->blk_ring_area);
  86. return err;
  87. }
  88. switch (blkif->blk_protocol) {
  89. case BLKIF_PROTOCOL_NATIVE:
  90. {
  91. struct blkif_sring *sring;
  92. sring = (struct blkif_sring *)blkif->blk_ring_area->addr;
  93. BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
  94. break;
  95. }
  96. case BLKIF_PROTOCOL_X86_32:
  97. {
  98. struct blkif_x86_32_sring *sring_x86_32;
  99. sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr;
  100. BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
  101. break;
  102. }
  103. case BLKIF_PROTOCOL_X86_64:
  104. {
  105. struct blkif_x86_64_sring *sring_x86_64;
  106. sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr;
  107. BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
  108. break;
  109. }
  110. default:
  111. BUG();
  112. }
  113. err = bind_interdomain_evtchn_to_irqhandler(
  114. blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif);
  115. if (err < 0)
  116. {
  117. unmap_frontend_page(blkif);
  118. free_vm_area(blkif->blk_ring_area);
  119. blkif->blk_rings.common.sring = NULL;
  120. return err;
  121. }
  122. blkif->irq = err;
  123. return 0;
  124. }
  125. void blkif_disconnect(blkif_t *blkif)
  126. {
  127. if (blkif->xenblkd) {
  128. kthread_stop(blkif->xenblkd);
  129. blkif->xenblkd = NULL;
  130. }
  131. atomic_dec(&blkif->refcnt);
  132. wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
  133. atomic_inc(&blkif->refcnt);
  134. if (blkif->irq) {
  135. unbind_from_irqhandler(blkif->irq, blkif);
  136. blkif->irq = 0;
  137. }
  138. if (blkif->blk_rings.common.sring) {
  139. unmap_frontend_page(blkif);
  140. free_vm_area(blkif->blk_ring_area);
  141. blkif->blk_rings.common.sring = NULL;
  142. }
  143. }
  144. void blkif_free(blkif_t *blkif)
  145. {
  146. if (!atomic_dec_and_test(&blkif->refcnt))
  147. BUG();
  148. kmem_cache_free(blkif_cachep, blkif);
  149. }
  150. int __init blkif_interface_init(void)
  151. {
  152. blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t),
  153. 0, 0, NULL);
  154. if (!blkif_cachep)
  155. return -ENOMEM;
  156. return 0;
  157. }