notify.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2014 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. */
  24. #include <nvif/client.h>
  25. #include <nvif/driver.h>
  26. #include <nvif/notify.h>
  27. #include <nvif/object.h>
  28. #include <nvif/ioctl.h>
  29. #include <nvif/event.h>
  30. static inline int
  31. nvif_notify_put_(struct nvif_notify *notify)
  32. {
  33. struct nvif_object *object = notify->object;
  34. struct {
  35. struct nvif_ioctl_v0 ioctl;
  36. struct nvif_ioctl_ntfy_put_v0 ntfy;
  37. } args = {
  38. .ioctl.type = NVIF_IOCTL_V0_NTFY_PUT,
  39. .ntfy.index = notify->index,
  40. };
  41. if (atomic_inc_return(&notify->putcnt) != 1)
  42. return 0;
  43. return nvif_object_ioctl(object, &args, sizeof(args), NULL);
  44. }
  45. int
  46. nvif_notify_put(struct nvif_notify *notify)
  47. {
  48. if (likely(notify->object) &&
  49. test_and_clear_bit(NVIF_NOTIFY_USER, &notify->flags)) {
  50. int ret = nvif_notify_put_(notify);
  51. if (test_bit(NVIF_NOTIFY_WORK, &notify->flags))
  52. flush_work(&notify->work);
  53. return ret;
  54. }
  55. return 0;
  56. }
  57. static inline int
  58. nvif_notify_get_(struct nvif_notify *notify)
  59. {
  60. struct nvif_object *object = notify->object;
  61. struct {
  62. struct nvif_ioctl_v0 ioctl;
  63. struct nvif_ioctl_ntfy_get_v0 ntfy;
  64. } args = {
  65. .ioctl.type = NVIF_IOCTL_V0_NTFY_GET,
  66. .ntfy.index = notify->index,
  67. };
  68. if (atomic_dec_return(&notify->putcnt) != 0)
  69. return 0;
  70. return nvif_object_ioctl(object, &args, sizeof(args), NULL);
  71. }
  72. int
  73. nvif_notify_get(struct nvif_notify *notify)
  74. {
  75. if (likely(notify->object) &&
  76. !test_and_set_bit(NVIF_NOTIFY_USER, &notify->flags))
  77. return nvif_notify_get_(notify);
  78. return 0;
  79. }
  80. static inline int
  81. nvif_notify_func(struct nvif_notify *notify, bool keep)
  82. {
  83. int ret = notify->func(notify);
  84. if (ret == NVIF_NOTIFY_KEEP ||
  85. !test_and_clear_bit(NVIF_NOTIFY_USER, &notify->flags)) {
  86. if (!keep)
  87. atomic_dec(&notify->putcnt);
  88. else
  89. nvif_notify_get_(notify);
  90. }
  91. return ret;
  92. }
  93. static void
  94. nvif_notify_work(struct work_struct *work)
  95. {
  96. struct nvif_notify *notify = container_of(work, typeof(*notify), work);
  97. nvif_notify_func(notify, true);
  98. }
  99. int
  100. nvif_notify(const void *header, u32 length, const void *data, u32 size)
  101. {
  102. struct nvif_notify *notify = NULL;
  103. const union {
  104. struct nvif_notify_rep_v0 v0;
  105. } *args = header;
  106. int ret = NVIF_NOTIFY_DROP;
  107. if (length == sizeof(args->v0) && args->v0.version == 0) {
  108. if (WARN_ON(args->v0.route))
  109. return NVIF_NOTIFY_DROP;
  110. notify = (void *)(unsigned long)args->v0.token;
  111. }
  112. if (!WARN_ON(notify == NULL)) {
  113. struct nvif_client *client = notify->object->client;
  114. if (!WARN_ON(notify->size != size)) {
  115. atomic_inc(&notify->putcnt);
  116. if (test_bit(NVIF_NOTIFY_WORK, &notify->flags)) {
  117. memcpy((void *)notify->data, data, size);
  118. schedule_work(&notify->work);
  119. return NVIF_NOTIFY_DROP;
  120. }
  121. notify->data = data;
  122. ret = nvif_notify_func(notify, client->driver->keep);
  123. notify->data = NULL;
  124. }
  125. }
  126. return ret;
  127. }
  128. int
  129. nvif_notify_fini(struct nvif_notify *notify)
  130. {
  131. struct nvif_object *object = notify->object;
  132. struct {
  133. struct nvif_ioctl_v0 ioctl;
  134. struct nvif_ioctl_ntfy_del_v0 ntfy;
  135. } args = {
  136. .ioctl.type = NVIF_IOCTL_V0_NTFY_DEL,
  137. .ntfy.index = notify->index,
  138. };
  139. int ret = nvif_notify_put(notify);
  140. if (ret >= 0 && object) {
  141. ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
  142. if (ret == 0) {
  143. notify->object = NULL;
  144. kfree((void *)notify->data);
  145. }
  146. }
  147. return ret;
  148. }
  149. int
  150. nvif_notify_init(struct nvif_object *object, int (*func)(struct nvif_notify *),
  151. bool work, u8 event, void *data, u32 size, u32 reply,
  152. struct nvif_notify *notify)
  153. {
  154. struct {
  155. struct nvif_ioctl_v0 ioctl;
  156. struct nvif_ioctl_ntfy_new_v0 ntfy;
  157. struct nvif_notify_req_v0 req;
  158. } *args;
  159. int ret = -ENOMEM;
  160. notify->object = object;
  161. notify->flags = 0;
  162. atomic_set(&notify->putcnt, 1);
  163. notify->func = func;
  164. notify->data = NULL;
  165. notify->size = reply;
  166. if (work) {
  167. INIT_WORK(&notify->work, nvif_notify_work);
  168. set_bit(NVIF_NOTIFY_WORK, &notify->flags);
  169. notify->data = kmalloc(notify->size, GFP_KERNEL);
  170. if (!notify->data)
  171. goto done;
  172. }
  173. if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
  174. goto done;
  175. args->ioctl.version = 0;
  176. args->ioctl.type = NVIF_IOCTL_V0_NTFY_NEW;
  177. args->ntfy.version = 0;
  178. args->ntfy.event = event;
  179. args->req.version = 0;
  180. args->req.reply = notify->size;
  181. args->req.route = 0;
  182. args->req.token = (unsigned long)(void *)notify;
  183. memcpy(args->req.data, data, size);
  184. ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
  185. notify->index = args->ntfy.index;
  186. kfree(args);
  187. done:
  188. if (ret)
  189. nvif_notify_fini(notify);
  190. return ret;
  191. }