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 | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 29 | |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 30 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 31 | |
K. Y. Srinivasan | 6fdf3b2 | 2012-12-01 06:46:32 -0800 | [diff] [blame^] | 32 | void hv_begin_read(struct hv_ring_buffer_info *rbi) |
| 33 | { |
| 34 | rbi->ring_buffer->interrupt_mask = 1; |
| 35 | smp_mb(); |
| 36 | } |
| 37 | |
| 38 | u32 hv_end_read(struct hv_ring_buffer_info *rbi) |
| 39 | { |
| 40 | u32 read; |
| 41 | u32 write; |
| 42 | |
| 43 | rbi->ring_buffer->interrupt_mask = 0; |
| 44 | smp_mb(); |
| 45 | |
| 46 | /* |
| 47 | * Now check to see if the ring buffer is still empty. |
| 48 | * If it is not, we raced and we need to process new |
| 49 | * incoming messages. |
| 50 | */ |
| 51 | hv_get_ringbuffer_availbytes(rbi, &read, &write); |
| 52 | |
| 53 | return read; |
| 54 | } |
| 55 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 56 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 57 | /* |
| 58 | * hv_get_next_write_location() |
| 59 | * |
| 60 | * Get the next write location for the specified ring buffer |
| 61 | * |
| 62 | */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 63 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 64 | hv_get_next_write_location(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 65 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 66 | u32 next = ring_info->ring_buffer->write_index; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 67 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 68 | return next; |
| 69 | } |
| 70 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 71 | /* |
| 72 | * hv_set_next_write_location() |
| 73 | * |
| 74 | * Set the next write location for the specified ring buffer |
| 75 | * |
| 76 | */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 77 | static inline void |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 78 | hv_set_next_write_location(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 79 | u32 next_write_location) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 80 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 81 | ring_info->ring_buffer->write_index = next_write_location; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 82 | } |
| 83 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 84 | /* |
| 85 | * hv_get_next_read_location() |
| 86 | * |
| 87 | * Get the next read location for the specified ring buffer |
| 88 | */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 89 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 90 | hv_get_next_read_location(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 91 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 92 | u32 next = ring_info->ring_buffer->read_index; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 93 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 94 | return next; |
| 95 | } |
| 96 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 97 | /* |
| 98 | * hv_get_next_readlocation_withoffset() |
| 99 | * |
| 100 | * Get the next read location + offset for the specified ring buffer. |
| 101 | * This allows the caller to skip |
| 102 | */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 103 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 104 | hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | 1ac5864 | 2010-11-08 14:04:47 -0800 | [diff] [blame] | 105 | u32 offset) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 106 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 107 | u32 next = ring_info->ring_buffer->read_index; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 108 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 109 | next += offset; |
| 110 | next %= ring_info->ring_datasize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 111 | |
| 112 | return next; |
| 113 | } |
| 114 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 115 | /* |
| 116 | * |
| 117 | * hv_set_next_read_location() |
| 118 | * |
| 119 | * Set the next read location for the specified ring buffer |
| 120 | * |
| 121 | */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 122 | static inline void |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 123 | hv_set_next_read_location(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 124 | u32 next_read_location) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 125 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 126 | ring_info->ring_buffer->read_index = next_read_location; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 130 | /* |
| 131 | * |
| 132 | * hv_get_ring_buffer() |
| 133 | * |
| 134 | * Get the start of the ring buffer |
| 135 | */ |
Greg Kroah-Hartman | 8282c40 | 2009-07-14 15:06:28 -0700 | [diff] [blame] | 136 | static inline void * |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 137 | hv_get_ring_buffer(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 138 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 139 | return (void *)ring_info->ring_buffer->buffer; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 143 | /* |
| 144 | * |
| 145 | * hv_get_ring_buffersize() |
| 146 | * |
| 147 | * Get the size of the ring buffer |
| 148 | */ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 149 | static inline u32 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 150 | hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 151 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 152 | return ring_info->ring_datasize; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 153 | } |
| 154 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 155 | /* |
| 156 | * |
| 157 | * hv_get_ring_bufferindices() |
| 158 | * |
| 159 | * Get the read and write indices as u64 of the specified ring buffer |
| 160 | * |
| 161 | */ |
Greg Kroah-Hartman | 5947143 | 2009-07-14 15:10:26 -0700 | [diff] [blame] | 162 | static inline u64 |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 163 | hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 164 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 165 | return (u64)ring_info->ring_buffer->write_index << 32; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 166 | } |
| 167 | |
K. Y. Srinivasan | 8f1136a | 2011-05-10 07:55:31 -0700 | [diff] [blame] | 168 | /* |
| 169 | * |
| 170 | * hv_copyfrom_ringbuffer() |
| 171 | * |
| 172 | * Helper routine to copy to source from ring buffer. |
| 173 | * Assume there is enough room. Handles wrap-around in src case only!! |
| 174 | * |
| 175 | */ |
| 176 | static u32 hv_copyfrom_ringbuffer( |
| 177 | struct hv_ring_buffer_info *ring_info, |
| 178 | void *dest, |
| 179 | u32 destlen, |
| 180 | u32 start_read_offset) |
| 181 | { |
| 182 | void *ring_buffer = hv_get_ring_buffer(ring_info); |
| 183 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); |
| 184 | |
| 185 | u32 frag_len; |
| 186 | |
| 187 | /* wrap-around detected at the src */ |
| 188 | if (destlen > ring_buffer_size - start_read_offset) { |
| 189 | frag_len = ring_buffer_size - start_read_offset; |
| 190 | |
| 191 | memcpy(dest, ring_buffer + start_read_offset, frag_len); |
| 192 | memcpy(dest + frag_len, ring_buffer, destlen - frag_len); |
| 193 | } else |
| 194 | |
| 195 | memcpy(dest, ring_buffer + start_read_offset, destlen); |
| 196 | |
| 197 | |
| 198 | start_read_offset += destlen; |
| 199 | start_read_offset %= ring_buffer_size; |
| 200 | |
| 201 | return start_read_offset; |
| 202 | } |
| 203 | |
| 204 | |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 205 | /* |
| 206 | * |
| 207 | * hv_copyto_ringbuffer() |
| 208 | * |
| 209 | * Helper routine to copy from source to ring buffer. |
| 210 | * Assume there is enough room. Handles wrap-around in dest case only!! |
| 211 | * |
| 212 | */ |
| 213 | static u32 hv_copyto_ringbuffer( |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 214 | struct hv_ring_buffer_info *ring_info, |
| 215 | u32 start_write_offset, |
| 216 | void *src, |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 217 | u32 srclen) |
| 218 | { |
| 219 | void *ring_buffer = hv_get_ring_buffer(ring_info); |
| 220 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); |
| 221 | u32 frag_len; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 222 | |
K. Y. Srinivasan | 7581578 | 2011-05-10 07:55:32 -0700 | [diff] [blame] | 223 | /* wrap-around detected! */ |
| 224 | if (srclen > ring_buffer_size - start_write_offset) { |
| 225 | frag_len = ring_buffer_size - start_write_offset; |
| 226 | memcpy(ring_buffer + start_write_offset, src, frag_len); |
| 227 | memcpy(ring_buffer, src + frag_len, srclen - frag_len); |
| 228 | } else |
| 229 | memcpy(ring_buffer + start_write_offset, src, srclen); |
| 230 | |
| 231 | start_write_offset += srclen; |
| 232 | start_write_offset %= ring_buffer_size; |
| 233 | |
| 234 | return start_write_offset; |
| 235 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 236 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 237 | /* |
| 238 | * |
| 239 | * hv_ringbuffer_get_debuginfo() |
| 240 | * |
| 241 | * Get various debug metrics for the specified ring buffer |
| 242 | * |
| 243 | */ |
K. Y. Srinivasan | a75b61d | 2011-05-10 07:55:28 -0700 | [diff] [blame] | 244 | 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] | 245 | struct hv_ring_buffer_debug_info *debug_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 246 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 247 | u32 bytes_avail_towrite; |
| 248 | u32 bytes_avail_toread; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 249 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 250 | if (ring_info->ring_buffer) { |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 251 | hv_get_ringbuffer_availbytes(ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 252 | &bytes_avail_toread, |
| 253 | &bytes_avail_towrite); |
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 | debug_info->bytes_avail_toread = bytes_avail_toread; |
| 256 | debug_info->bytes_avail_towrite = bytes_avail_towrite; |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 257 | debug_info->current_read_index = |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 258 | ring_info->ring_buffer->read_index; |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 259 | debug_info->current_write_index = |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 260 | ring_info->ring_buffer->write_index; |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 261 | debug_info->current_interrupt_mask = |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 262 | ring_info->ring_buffer->interrupt_mask; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 267 | /* |
| 268 | * |
| 269 | * hv_get_ringbuffer_interrupt_mask() |
| 270 | * |
| 271 | * Get the interrupt mask for the specified ring buffer |
| 272 | * |
| 273 | */ |
K. Y. Srinivasan | decc49d | 2011-05-10 07:55:26 -0700 | [diff] [blame] | 274 | u32 hv_get_ringbuffer_interrupt_mask(struct hv_ring_buffer_info *rbi) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 275 | { |
Haiyang Zhang | 82f8bd4 | 2010-11-08 14:04:45 -0800 | [diff] [blame] | 276 | return rbi->ring_buffer->interrupt_mask; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 277 | } |
| 278 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 279 | /* |
| 280 | * |
| 281 | * hv_ringbuffer_init() |
| 282 | * |
| 283 | *Initialize the ring buffer |
| 284 | * |
| 285 | */ |
K. Y. Srinivasan | 72a95cb | 2011-05-10 07:55:21 -0700 | [diff] [blame] | 286 | int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 287 | void *buffer, u32 buflen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 288 | { |
Greg Kroah-Hartman | 4a1b3ac | 2010-07-27 11:47:08 -0700 | [diff] [blame] | 289 | if (sizeof(struct hv_ring_buffer) != PAGE_SIZE) |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 290 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 291 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 292 | memset(ring_info, 0, sizeof(struct hv_ring_buffer_info)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 293 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 294 | ring_info->ring_buffer = (struct hv_ring_buffer *)buffer; |
| 295 | ring_info->ring_buffer->read_index = |
| 296 | ring_info->ring_buffer->write_index = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 297 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 298 | ring_info->ring_size = buflen; |
| 299 | ring_info->ring_datasize = buflen - sizeof(struct hv_ring_buffer); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 300 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 301 | spin_lock_init(&ring_info->ring_lock); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 306 | /* |
| 307 | * |
| 308 | * hv_ringbuffer_cleanup() |
| 309 | * |
| 310 | * Cleanup the ring buffer |
| 311 | * |
| 312 | */ |
K. Y. Srinivasan | 2dba688 | 2011-05-10 07:55:22 -0700 | [diff] [blame] | 313 | void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 314 | { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 315 | } |
| 316 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 317 | /* |
| 318 | * |
| 319 | * hv_ringbuffer_write() |
| 320 | * |
| 321 | * Write to the ring buffer |
| 322 | * |
| 323 | */ |
K. Y. Srinivasan | 633c4dc | 2011-05-10 07:55:23 -0700 | [diff] [blame] | 324 | int hv_ringbuffer_write(struct hv_ring_buffer_info *outring_info, |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 325 | struct scatterlist *sglist, u32 sgcount) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 326 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 327 | int i = 0; |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 328 | u32 bytes_avail_towrite; |
| 329 | u32 bytes_avail_toread; |
| 330 | u32 totalbytes_towrite = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 331 | |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 332 | struct scatterlist *sg; |
K. Y. Srinivasan | 66a6054 | 2011-05-10 07:55:33 -0700 | [diff] [blame] | 333 | u32 next_write_location; |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 334 | u64 prev_indices = 0; |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 335 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 336 | |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 337 | for_each_sg(sglist, sg, sgcount, i) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 338 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 339 | totalbytes_towrite += sg->length; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 342 | totalbytes_towrite += sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 343 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 344 | spin_lock_irqsave(&outring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 345 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 346 | hv_get_ringbuffer_availbytes(outring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 347 | &bytes_avail_toread, |
| 348 | &bytes_avail_towrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 349 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 350 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 351 | /* If there is only room for the packet, assume it is full. */ |
| 352 | /* Otherwise, the next time around, we think the ring buffer */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 353 | /* is empty since the read index == write index */ |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 354 | if (bytes_avail_towrite <= totalbytes_towrite) { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 355 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 356 | return -EAGAIN; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 359 | /* Write to the ring buffer */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 360 | next_write_location = hv_get_next_write_location(outring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 361 | |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 362 | for_each_sg(sglist, sg, sgcount, i) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 363 | { |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 364 | next_write_location = hv_copyto_ringbuffer(outring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 365 | next_write_location, |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 366 | sg_virt(sg), |
| 367 | sg->length); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 370 | /* Set previous packet start */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 371 | prev_indices = hv_get_ring_bufferindices(outring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 372 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 373 | next_write_location = hv_copyto_ringbuffer(outring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 374 | next_write_location, |
| 375 | &prev_indices, |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 376 | sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 377 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 378 | /* Make sure we flush all writes before updating the writeIndex */ |
K. Y. Srinivasan | e690b5a | 2011-06-06 15:49:53 -0700 | [diff] [blame] | 379 | smp_wmb(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 380 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 381 | /* Now, update the write location */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 382 | hv_set_next_write_location(outring_info, next_write_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 383 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 384 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 385 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 390 | /* |
| 391 | * |
| 392 | * hv_ringbuffer_peek() |
| 393 | * |
| 394 | * Read without advancing the read index |
| 395 | * |
| 396 | */ |
K. Y. Srinivasan | a89186c | 2011-05-10 07:55:24 -0700 | [diff] [blame] | 397 | int hv_ringbuffer_peek(struct hv_ring_buffer_info *Inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 398 | void *Buffer, u32 buflen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 399 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 400 | u32 bytes_avail_towrite; |
| 401 | u32 bytes_avail_toread; |
| 402 | u32 next_read_location = 0; |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 403 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 404 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 405 | spin_lock_irqsave(&Inring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 406 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 407 | hv_get_ringbuffer_availbytes(Inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 408 | &bytes_avail_toread, |
| 409 | &bytes_avail_towrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 410 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 411 | /* Make sure there is something to read */ |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 412 | if (bytes_avail_toread < buflen) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 413 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 414 | spin_unlock_irqrestore(&Inring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 415 | |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 416 | return -EAGAIN; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 419 | /* Convert to byte offset */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 420 | next_read_location = hv_get_next_read_location(Inring_info); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 421 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 422 | next_read_location = hv_copyfrom_ringbuffer(Inring_info, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 423 | Buffer, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 424 | buflen, |
| 425 | next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 426 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 427 | spin_unlock_irqrestore(&Inring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | |
K. Y. Srinivasan | b2a5a58 | 2011-05-10 07:55:30 -0700 | [diff] [blame] | 433 | /* |
| 434 | * |
| 435 | * hv_ringbuffer_read() |
| 436 | * |
| 437 | * Read and advance the read index |
| 438 | * |
| 439 | */ |
K. Y. Srinivasan | 38397c8 | 2011-05-10 07:55:25 -0700 | [diff] [blame] | 440 | int hv_ringbuffer_read(struct hv_ring_buffer_info *inring_info, void *buffer, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 441 | u32 buflen, u32 offset) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 442 | { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 443 | u32 bytes_avail_towrite; |
| 444 | u32 bytes_avail_toread; |
| 445 | u32 next_read_location = 0; |
| 446 | u64 prev_indices = 0; |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 447 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 448 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 449 | if (buflen <= 0) |
Bill Pemberton | a16e148 | 2010-05-05 15:27:50 -0400 | [diff] [blame] | 450 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 451 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 452 | spin_lock_irqsave(&inring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 453 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 454 | hv_get_ringbuffer_availbytes(inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 455 | &bytes_avail_toread, |
| 456 | &bytes_avail_towrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 457 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 458 | /* Make sure there is something to read */ |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 459 | if (bytes_avail_toread < buflen) { |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 460 | spin_unlock_irqrestore(&inring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 461 | |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 462 | return -EAGAIN; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Haiyang Zhang | 1ac5864 | 2010-11-08 14:04:47 -0800 | [diff] [blame] | 465 | next_read_location = |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 466 | hv_get_next_readlocation_withoffset(inring_info, offset); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 467 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 468 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 469 | buffer, |
| 470 | buflen, |
| 471 | next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 472 | |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 473 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 474 | &prev_indices, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 475 | sizeof(u64), |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 476 | next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 477 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 478 | /* Make sure all reads are done before we update the read index since */ |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 479 | /* the writer may start writing to the read area once the read index */ |
| 480 | /*is updated */ |
K. Y. Srinivasan | ef0d5b2 | 2011-06-06 15:49:51 -0700 | [diff] [blame] | 481 | smp_mb(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 482 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 483 | /* Update the read index */ |
K. Y. Srinivasan | 2b8a912 | 2011-05-10 07:55:29 -0700 | [diff] [blame] | 484 | hv_set_next_read_location(inring_info, next_read_location); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 485 | |
Haiyang Zhang | fc8c72e | 2010-11-08 14:04:46 -0800 | [diff] [blame] | 486 | spin_unlock_irqrestore(&inring_info->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 487 | |
| 488 | return 0; |
| 489 | } |