Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 16 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 17 | * |
| 18 | * Authors: |
| 19 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 20 | * Hank Janssen <hjanssen@microsoft.com> |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 21 | * K. Y. Srinivasan <kys@microsoft.com> |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 22 | * |
| 23 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 25 | |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/mm.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 28 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 29 | #include <linux/uio.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 30 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 31 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 32 | |
K. Y. Srinivasan | 6fdf3b2 | 2012-12-01 06:46:32 -0800 | [diff] [blame] | 33 | void hv_begin_read(struct hv_ring_buffer_info *rbi) |
| 34 | { |
| 35 | rbi->ring_buffer->interrupt_mask = 1; |
K. Y. Srinivasan | dcd0eec | 2016-04-02 17:59:48 -0700 | [diff] [blame] | 36 | virt_mb(); |
K. Y. Srinivasan | 6fdf3b2 | 2012-12-01 06:46:32 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | u32 hv_end_read(struct hv_ring_buffer_info *rbi) |
| 40 | { |
K. Y. Srinivasan | 6fdf3b2 | 2012-12-01 06:46:32 -0800 | [diff] [blame] | 41 | |
| 42 | rbi->ring_buffer->interrupt_mask = 0; |
K. Y. Srinivasan | dcd0eec | 2016-04-02 17:59:48 -0700 | [diff] [blame] | 43 | virt_mb(); |
K. Y. Srinivasan | 6fdf3b2 | 2012-12-01 06:46:32 -0800 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Now check to see if the ring buffer is still empty. |
| 47 | * If it is not, we raced and we need to process new |
| 48 | * incoming messages. |
| 49 | */ |
K. Y. Srinivasan | a6341f0 | 2016-04-02 17:59:46 -0700 | [diff] [blame] | 50 | return hv_get_bytes_to_read(rbi); |
K. Y. Srinivasan | 6fdf3b2 | 2012-12-01 06:46:32 -0800 | [diff] [blame] | 51 | } |
| 52 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 53 | /* |
| 54 | * When we write to the ring buffer, check if the host needs to |
| 55 | * be signaled. Here is the details of this protocol: |
| 56 | * |
| 57 | * 1. The host guarantees that while it is draining the |
| 58 | * ring buffer, it will set the interrupt_mask to |
| 59 | * indicate it does not need to be interrupted when |
| 60 | * new data is placed. |
| 61 | * |
| 62 | * 2. The host guarantees that it will completely drain |
| 63 | * the ring buffer before exiting the read loop. Further, |
| 64 | * once the ring buffer is empty, it will clear the |
| 65 | * interrupt_mask and re-check to see if new data has |
| 66 | * arrived. |
| 67 | */ |
| 68 | |
| 69 | static bool hv_need_to_signal(u32 old_write, struct hv_ring_buffer_info *rbi) |
| 70 | { |
K. Y. Srinivasan | dcd0eec | 2016-04-02 17:59:48 -0700 | [diff] [blame] | 71 | virt_mb(); |
K. Y. Srinivasan | d45faae | 2016-04-02 17:59:47 -0700 | [diff] [blame] | 72 | if (READ_ONCE(rbi->ring_buffer->interrupt_mask)) |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 73 | return false; |
| 74 | |
Jason Wang | e91e84f | 2013-06-20 12:58:57 +0800 | [diff] [blame] | 75 | /* check interrupt_mask before read_index */ |
K. Y. Srinivasan | dcd0eec | 2016-04-02 17:59:48 -0700 | [diff] [blame] | 76 | virt_rmb(); |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 77 | /* |
| 78 | * This is the only case we need to signal when the |
| 79 | * ring transitions from being empty to non-empty. |
| 80 | */ |
K. Y. Srinivasan | d45faae | 2016-04-02 17:59:47 -0700 | [diff] [blame] | 81 | if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 82 | return true; |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 87 | /* Get the next write location for the specified ring buffer. */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 88 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 89 | hv_get_next_write_location(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 90 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 91 | u32 next = ring_info->ring_buffer->write_index; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 92 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 93 | return next; |
| 94 | } |
| 95 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 96 | /* Set the next write location for the specified ring buffer. */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 97 | static inline void |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 98 | hv_set_next_write_location(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 99 | u32 next_write_location) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 100 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 101 | ring_info->ring_buffer->write_index = next_write_location; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 104 | /* Get the next read location for the specified ring buffer. */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 105 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 106 | hv_get_next_read_location(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 107 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 108 | u32 next = ring_info->ring_buffer->read_index; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 109 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 110 | return next; |
| 111 | } |
| 112 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 113 | /* |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 114 | * Get the next read location + offset for the specified ring buffer. |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 115 | * This allows the caller to skip. |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 116 | */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 117 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 118 | hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | 1ac5864 | 2010-11-08 14:04:47 -0800 | [diff] [blame] | 119 | u32 offset) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 120 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 121 | u32 next = ring_info->ring_buffer->read_index; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 122 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 123 | next += offset; |
| 124 | next %= ring_info->ring_datasize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 125 | |
| 126 | return next; |
| 127 | } |
| 128 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 129 | /* Set the next read location for the specified ring buffer. */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 130 | static inline void |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 131 | hv_set_next_read_location(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 132 | u32 next_read_location) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 133 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 134 | ring_info->ring_buffer->read_index = next_read_location; |
K. Y. Srinivasan | ab028db | 2016-04-02 17:59:51 -0700 | [diff] [blame^] | 135 | ring_info->priv_read_index = next_read_location; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 138 | /* Get the size of the ring buffer. */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 139 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 140 | hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 141 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 142 | return ring_info->ring_datasize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 145 | /* Get the read and write indices as u64 of the specified ring buffer. */ |
Greg Kroah-Hartman | 5947143 | 2009-07-14 15:10:26 -0700 | [diff] [blame] | 146 | static inline u64 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 147 | hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 148 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 149 | return (u64)ring_info->ring_buffer->write_index << 32; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 150 | } |
| 151 | |
K. Y. Srinivasan | 8f1136a | 2011-05-10 07:55:31 -0700 | [diff] [blame] | 152 | /* |
K. Y. Srinivasan | 8f1136a | 2011-05-10 07:55:31 -0700 | [diff] [blame] | 153 | * Helper routine to copy to source from ring buffer. |
| 154 | * Assume there is enough room. Handles wrap-around in src case only!! |
K. Y. Srinivasan | 8f1136a | 2011-05-10 07:55:31 -0700 | [diff] [blame] | 155 | */ |
| 156 | static u32 hv_copyfrom_ringbuffer( |
| 157 | struct hv_ring_buffer_info *ring_info, |
| 158 | void *dest, |
| 159 | u32 destlen, |
| 160 | u32 start_read_offset) |
| 161 | { |
| 162 | void *ring_buffer = hv_get_ring_buffer(ring_info); |
| 163 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); |
| 164 | |
| 165 | u32 frag_len; |
| 166 | |
| 167 | /* wrap-around detected at the src */ |
| 168 | if (destlen > ring_buffer_size - start_read_offset) { |
| 169 | frag_len = ring_buffer_size - start_read_offset; |
| 170 | |
| 171 | memcpy(dest, ring_buffer + start_read_offset, frag_len); |
| 172 | memcpy(dest + frag_len, ring_buffer, destlen - frag_len); |
| 173 | } else |
| 174 | |
| 175 | memcpy(dest, ring_buffer + start_read_offset, destlen); |
| 176 | |
| 177 | |
| 178 | start_read_offset += destlen; |
| 179 | start_read_offset %= ring_buffer_size; |
| 180 | |
| 181 | return start_read_offset; |
| 182 | } |
| 183 | |
| 184 | |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 185 | /* |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 186 | * Helper routine to copy from source to ring buffer. |
| 187 | * Assume there is enough room. Handles wrap-around in dest case only!! |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 188 | */ |
| 189 | static u32 hv_copyto_ringbuffer( |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 190 | struct hv_ring_buffer_info *ring_info, |
| 191 | u32 start_write_offset, |
| 192 | void *src, |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 193 | u32 srclen) |
| 194 | { |
| 195 | void *ring_buffer = hv_get_ring_buffer(ring_info); |
| 196 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); |
| 197 | u32 frag_len; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 198 | |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 199 | /* wrap-around detected! */ |
| 200 | if (srclen > ring_buffer_size - start_write_offset) { |
| 201 | frag_len = ring_buffer_size - start_write_offset; |
| 202 | memcpy(ring_buffer + start_write_offset, src, frag_len); |
| 203 | memcpy(ring_buffer, src + frag_len, srclen - frag_len); |
| 204 | } else |
| 205 | memcpy(ring_buffer + start_write_offset, src, srclen); |
| 206 | |
| 207 | start_write_offset += srclen; |
| 208 | start_write_offset %= ring_buffer_size; |
| 209 | |
| 210 | return start_write_offset; |
| 211 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 212 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 213 | /* Get various debug metrics for the specified ring buffer. */ |
K. Y. Srinivasan | a75b61d | 2011-05-10 07:55:28 -0700 | [diff] [blame] | 214 | void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info, |
Greg Kroah-Hartman | 80682b7 | 2010-07-27 11:37:32 -0700 | [diff] [blame] | 215 | struct hv_ring_buffer_debug_info *debug_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 216 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 217 | u32 bytes_avail_towrite; |
| 218 | u32 bytes_avail_toread; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 219 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 220 | if (ring_info->ring_buffer) { |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 221 | hv_get_ringbuffer_availbytes(ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 222 | &bytes_avail_toread, |
| 223 | &bytes_avail_towrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 224 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 225 | debug_info->bytes_avail_toread = bytes_avail_toread; |
| 226 | debug_info->bytes_avail_towrite = bytes_avail_towrite; |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 227 | debug_info->current_read_index = |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 228 | ring_info->ring_buffer->read_index; |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 229 | debug_info->current_write_index = |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 230 | ring_info->ring_buffer->write_index; |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 231 | debug_info->current_interrupt_mask = |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 232 | ring_info->ring_buffer->interrupt_mask; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 236 | /* Initialize the ring buffer. */ |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 237 | int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 238 | void *buffer, u32 buflen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 239 | { |
Greg Kroah-Hartman | 4a1b3ac | 2010-07-27 11:47:08 -0700 | [diff] [blame] | 240 | if (sizeof(struct hv_ring_buffer) != PAGE_SIZE) |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 241 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 242 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 243 | memset(ring_info, 0, sizeof(struct hv_ring_buffer_info)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 244 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 245 | ring_info->ring_buffer = (struct hv_ring_buffer *)buffer; |
| 246 | ring_info->ring_buffer->read_index = |
| 247 | ring_info->ring_buffer->write_index = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 248 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 249 | /* Set the feature bit for enabling flow control. */ |
K. Y. Srinivasan | 046c791 | 2014-09-05 17:29:12 -0700 | [diff] [blame] | 250 | ring_info->ring_buffer->feature_bits.value = 1; |
| 251 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 252 | ring_info->ring_size = buflen; |
| 253 | ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 254 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 255 | spin_lock_init(&ring_info->ring_lock); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 260 | /* Cleanup the ring buffer. */ |
K. Y. Srinivasan | 2dba688 | 2011-05-10 07:55:22 -0700 | [diff] [blame] | 261 | void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 262 | { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 265 | /* Write to the ring buffer. */ |
K. Y. Srinivasan | 633c4dc | 2011-05-10 07:55:23 -0700 | [diff] [blame] | 266 | int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info, |
K. Y. Srinivasan | fe760e4 | 2016-01-27 22:29:45 -0800 | [diff] [blame] | 267 | struct kvec *kv_list, u32 kv_count, bool *signal, bool lock) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 268 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 269 | int i = 0; |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 270 | u32 bytes_avail_towrite; |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 271 | u32 totalbytes_towrite = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 272 | |
K. Y. Srinivasan | 66a6054 | 2011-05-10 07:55:33 -0700 | [diff] [blame] | 273 | u32 next_write_location; |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 274 | u32 old_write; |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 275 | u64 prev_indices = 0; |
K. Y. Srinivasan | fe760e4 | 2016-01-27 22:29:45 -0800 | [diff] [blame] | 276 | unsigned long flags = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 277 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 278 | for (i = 0; i < kv_count; i++) |
| 279 | totalbytes_towrite += kv_list[i].iov_len; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 280 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 281 | totalbytes_towrite += sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 282 | |
K. Y. Srinivasan | fe760e4 | 2016-01-27 22:29:45 -0800 | [diff] [blame] | 283 | if (lock) |
| 284 | spin_lock_irqsave(&outring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 285 | |
K. Y. Srinivasan | a6341f0 | 2016-04-02 17:59:46 -0700 | [diff] [blame] | 286 | bytes_avail_towrite = hv_get_bytes_to_write(outring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 287 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 288 | /* |
| 289 | * If there is only room for the packet, assume it is full. |
| 290 | * Otherwise, the next time around, we think the ring buffer |
| 291 | * is empty since the read index == write index. |
| 292 | */ |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 293 | if (bytes_avail_towrite <= totalbytes_towrite) { |
K. Y. Srinivasan | fe760e4 | 2016-01-27 22:29:45 -0800 | [diff] [blame] | 294 | if (lock) |
| 295 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 296 | return -EAGAIN; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 299 | /* Write to the ring buffer */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 300 | next_write_location = hv_get_next_write_location(outring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 301 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 302 | old_write = next_write_location; |
| 303 | |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 304 | for (i = 0; i < kv_count; i++) { |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 305 | next_write_location = hv_copyto_ringbuffer(outring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 306 | next_write_location, |
K. Y. Srinivasan | 011a7c3 | 2014-02-01 19:02:20 -0800 | [diff] [blame] | 307 | kv_list[i].iov_base, |
| 308 | kv_list[i].iov_len); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 311 | /* Set previous packet start */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 312 | prev_indices = hv_get_ring_bufferindices(outring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 313 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 314 | next_write_location = hv_copyto_ringbuffer(outring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 315 | next_write_location, |
| 316 | &prev_indices, |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 317 | sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 318 | |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 319 | /* Issue a full memory barrier before updating the write index */ |
K. Y. Srinivasan | dcd0eec | 2016-04-02 17:59:48 -0700 | [diff] [blame] | 320 | virt_mb(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 321 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 322 | /* Now, update the write location */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 323 | hv_set_next_write_location(outring_info, next_write_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 324 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 325 | |
K. Y. Srinivasan | fe760e4 | 2016-01-27 22:29:45 -0800 | [diff] [blame] | 326 | if (lock) |
| 327 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
K. Y. Srinivasan | 98fa8cf | 2012-12-01 06:46:36 -0800 | [diff] [blame] | 328 | |
| 329 | *signal = hv_need_to_signal(old_write, outring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 333 | int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, |
| 334 | void *buffer, u32 buflen, u32 *buffer_actual_len, |
| 335 | u64 *requestid, bool *signal, bool raw) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 336 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 337 | u32 bytes_avail_toread; |
| 338 | u32 next_read_location = 0; |
| 339 | u64 prev_indices = 0; |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 340 | struct vmpacket_descriptor desc; |
| 341 | u32 offset; |
| 342 | u32 packetlen; |
| 343 | int ret = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 344 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 345 | if (buflen <= 0) |
Bill Pemberton | a16e148 | 2010-05-05 15:27:50 -0400 | [diff] [blame] | 346 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 347 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 348 | |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 349 | *buffer_actual_len = 0; |
| 350 | *requestid = 0; |
| 351 | |
K. Y. Srinivasan | a6341f0 | 2016-04-02 17:59:46 -0700 | [diff] [blame] | 352 | bytes_avail_toread = hv_get_bytes_to_read(inring_info); |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 353 | /* Make sure there is something to read */ |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 354 | if (bytes_avail_toread < sizeof(desc)) { |
| 355 | /* |
| 356 | * No error is set when there is even no header, drivers are |
| 357 | * supposed to analyze buffer_actual_len. |
| 358 | */ |
K. Y. Srinivasan | 3eba9a7 | 2016-01-27 22:29:44 -0800 | [diff] [blame] | 359 | return ret; |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 360 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 361 | |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 362 | next_read_location = hv_get_next_read_location(inring_info); |
| 363 | next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc, |
| 364 | sizeof(desc), |
| 365 | next_read_location); |
| 366 | |
| 367 | offset = raw ? 0 : (desc.offset8 << 3); |
| 368 | packetlen = (desc.len8 << 3) - offset; |
| 369 | *buffer_actual_len = packetlen; |
| 370 | *requestid = desc.trans_id; |
| 371 | |
K. Y. Srinivasan | 3eba9a7 | 2016-01-27 22:29:44 -0800 | [diff] [blame] | 372 | if (bytes_avail_toread < packetlen + offset) |
| 373 | return -EAGAIN; |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 374 | |
K. Y. Srinivasan | 3eba9a7 | 2016-01-27 22:29:44 -0800 | [diff] [blame] | 375 | if (packetlen > buflen) |
| 376 | return -ENOBUFS; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 377 | |
Haiyang Zhang | 1ac5864 | 2010-11-08 14:04:47 -0800 | [diff] [blame] | 378 | next_read_location = |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 379 | hv_get_next_readlocation_withoffset(inring_info, offset); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 380 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 381 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 382 | buffer, |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 383 | packetlen, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 384 | next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 385 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 386 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 387 | &prev_indices, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 388 | sizeof(u64), |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 389 | next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 390 | |
Vitaly Kuznetsov | 822f18d | 2015-12-14 19:01:57 -0800 | [diff] [blame] | 391 | /* |
| 392 | * Make sure all reads are done before we update the read index since |
| 393 | * the writer may start writing to the read area once the read index |
| 394 | * is updated. |
| 395 | */ |
K. Y. Srinivasan | dcd0eec | 2016-04-02 17:59:48 -0700 | [diff] [blame] | 396 | virt_mb(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 397 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 398 | /* Update the read index */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 399 | hv_set_next_read_location(inring_info, next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 400 | |
K. Y. Srinivasan | a389fcf | 2016-04-02 16:17:38 -0700 | [diff] [blame] | 401 | *signal = hv_need_to_signal_on_read(inring_info); |
K. Y. Srinivasan | c2b8e52 | 2012-12-01 06:46:57 -0800 | [diff] [blame] | 402 | |
Vitaly Kuznetsov | 940b68e | 2015-12-14 19:02:01 -0800 | [diff] [blame] | 403 | return ret; |
Vitaly Kuznetsov | b5f53dd | 2015-12-14 19:01:59 -0800 | [diff] [blame] | 404 | } |