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