mic_virtio.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC Host driver.
  19. *
  20. */
  21. #ifndef MIC_VIRTIO_H
  22. #define MIC_VIRTIO_H
  23. #include <linux/virtio_config.h>
  24. #include <linux/mic_ioctl.h>
  25. /*
  26. * Note on endianness.
  27. * 1. Host can be both BE or LE
  28. * 2. Guest/card is LE. Host uses le_to_cpu to access desc/avail
  29. * rings and ioreadXX/iowriteXX to access used ring.
  30. * 3. Device page exposed by host to guest contains LE values. Guest
  31. * accesses these using ioreadXX/iowriteXX etc. This way in general we
  32. * obey the virtio spec according to which guest works with native
  33. * endianness and host is aware of guest endianness and does all
  34. * required endianness conversion.
  35. * 4. Data provided from user space to guest (in ADD_DEVICE and
  36. * CONFIG_CHANGE ioctl's) is not interpreted by the driver and should be
  37. * in guest endianness.
  38. */
  39. /**
  40. * struct mic_vringh - Virtio ring host information.
  41. *
  42. * @vring: The MIC vring used for setting up user space mappings.
  43. * @vrh: The host VRINGH used for accessing the card vrings.
  44. * @riov: The VRINGH read kernel IOV.
  45. * @wiov: The VRINGH write kernel IOV.
  46. * @vr_mutex: Mutex for synchronizing access to the VRING.
  47. * @buf: Temporary kernel buffer used to copy in/out data
  48. * from/to the card via DMA.
  49. * @buf_da: dma address of buf.
  50. * @mvdev: Back pointer to MIC virtio device for vringh_notify(..).
  51. * @head: The VRINGH head index address passed to vringh_getdesc_kern(..).
  52. */
  53. struct mic_vringh {
  54. struct mic_vring vring;
  55. struct vringh vrh;
  56. struct vringh_kiov riov;
  57. struct vringh_kiov wiov;
  58. struct mutex vr_mutex;
  59. void *buf;
  60. dma_addr_t buf_da;
  61. struct mic_vdev *mvdev;
  62. u16 head;
  63. };
  64. /**
  65. * struct mic_vdev - Host information for a card Virtio device.
  66. *
  67. * @virtio_id - Virtio device id.
  68. * @waitq - Waitqueue to allow ring3 apps to poll.
  69. * @mdev - Back pointer to host MIC device.
  70. * @poll_wake - Used for waking up threads blocked in poll.
  71. * @out_bytes - Debug stats for number of bytes copied from host to card.
  72. * @in_bytes - Debug stats for number of bytes copied from card to host.
  73. * @out_bytes_dma - Debug stats for number of bytes copied from host to card
  74. * using DMA.
  75. * @in_bytes_dma - Debug stats for number of bytes copied from card to host
  76. * using DMA.
  77. * @tx_len_unaligned - Debug stats for number of bytes copied to the card where
  78. * the transfer length did not have the required DMA alignment.
  79. * @tx_dst_unaligned - Debug stats for number of bytes copied where the
  80. * destination address on the card did not have the required DMA alignment.
  81. * @mvr - Store per VRING data structures.
  82. * @virtio_bh_work - Work struct used to schedule virtio bottom half handling.
  83. * @dd - Virtio device descriptor.
  84. * @dc - Virtio device control fields.
  85. * @list - List of Virtio devices.
  86. * @virtio_db - The doorbell used by the card to interrupt the host.
  87. * @virtio_cookie - The cookie returned while requesting interrupts.
  88. */
  89. struct mic_vdev {
  90. int virtio_id;
  91. wait_queue_head_t waitq;
  92. struct mic_device *mdev;
  93. int poll_wake;
  94. unsigned long out_bytes;
  95. unsigned long in_bytes;
  96. unsigned long out_bytes_dma;
  97. unsigned long in_bytes_dma;
  98. unsigned long tx_len_unaligned;
  99. unsigned long tx_dst_unaligned;
  100. struct mic_vringh mvr[MIC_MAX_VRINGS];
  101. struct work_struct virtio_bh_work;
  102. struct mic_device_desc *dd;
  103. struct mic_device_ctrl *dc;
  104. struct list_head list;
  105. int virtio_db;
  106. struct mic_irq *virtio_cookie;
  107. };
  108. void mic_virtio_uninit(struct mic_device *mdev);
  109. int mic_virtio_add_device(struct mic_vdev *mvdev,
  110. void __user *argp);
  111. void mic_virtio_del_device(struct mic_vdev *mvdev);
  112. int mic_virtio_config_change(struct mic_vdev *mvdev,
  113. void __user *argp);
  114. int mic_virtio_copy_desc(struct mic_vdev *mvdev,
  115. struct mic_copy_desc *request);
  116. void mic_virtio_reset_devices(struct mic_device *mdev);
  117. void mic_bh_handler(struct work_struct *work);
  118. /* Helper API to obtain the MIC PCIe device */
  119. static inline struct device *mic_dev(struct mic_vdev *mvdev)
  120. {
  121. return mvdev->mdev->sdev->parent;
  122. }
  123. /* Helper API to check if a virtio device is initialized */
  124. static inline int mic_vdev_inited(struct mic_vdev *mvdev)
  125. {
  126. /* Device has not been created yet */
  127. if (!mvdev->dd || !mvdev->dd->type) {
  128. dev_err(mic_dev(mvdev), "%s %d err %d\n",
  129. __func__, __LINE__, -EINVAL);
  130. return -EINVAL;
  131. }
  132. /* Device has been removed/deleted */
  133. if (mvdev->dd->type == -1) {
  134. dev_err(mic_dev(mvdev), "%s %d err %d\n",
  135. __func__, __LINE__, -ENODEV);
  136. return -ENODEV;
  137. }
  138. return 0;
  139. }
  140. /* Helper API to check if a virtio device is running */
  141. static inline bool mic_vdevup(struct mic_vdev *mvdev)
  142. {
  143. return !!mvdev->dd->status;
  144. }
  145. #endif