2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
32 #include <sys/mutex.h>
33 #include <sys/sysctl.h>
35 #include "hv_vmbus_priv.h"
37 /* Amount of space to write to */
38 #define HV_BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r))? \
39 ((z) - ((w) - (r))):((r) - (w))
42 hv_rbi_sysctl_stats(SYSCTL_HANDLER_ARGS)
44 hv_vmbus_ring_buffer_info* rbi;
45 uint32_t read_index, write_index, interrupt_mask, sz;
46 uint32_t read_avail, write_avail;
49 rbi = (hv_vmbus_ring_buffer_info*)arg1;
50 read_index = rbi->ring_buffer->read_index;
51 write_index = rbi->ring_buffer->write_index;
52 interrupt_mask = rbi->ring_buffer->interrupt_mask;
53 sz = rbi->ring_data_size;
54 write_avail = HV_BYTES_AVAIL_TO_WRITE(read_index,
56 read_avail = sz - write_avail;
57 snprintf(rbi_stats, sizeof(rbi_stats),
63 read_index, write_index, interrupt_mask,
64 read_avail, write_avail);
66 return (sysctl_handle_string(oidp, rbi_stats,
67 sizeof(rbi_stats), req));
72 struct sysctl_ctx_list *ctx,
73 struct sysctl_oid_list *tree_node,
74 hv_vmbus_ring_buffer_info *rbi,
77 SYSCTL_ADD_PROC(ctx, tree_node, OID_AUTO,
79 CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE, rbi, 0,
80 hv_rbi_sysctl_stats, "A", desc);
83 * @brief Get number of bytes available to read and to write to
84 * for the specified ring buffer
87 get_ring_buffer_avail_bytes(
88 hv_vmbus_ring_buffer_info* rbi,
92 uint32_t read_loc, write_loc;
95 * Capture the read/write indices before they changed
97 read_loc = rbi->ring_buffer->read_index;
98 write_loc = rbi->ring_buffer->write_index;
100 *write = HV_BYTES_AVAIL_TO_WRITE(
101 read_loc, write_loc, rbi->ring_data_size);
102 *read = rbi->ring_data_size - *write;
106 * @brief Get the next write location for the specified ring buffer
108 static inline uint32_t
109 get_next_write_location(hv_vmbus_ring_buffer_info* ring_info)
111 uint32_t next = ring_info->ring_buffer->write_index;
116 * @brief Set the next write location for the specified ring buffer
119 set_next_write_location(
120 hv_vmbus_ring_buffer_info* ring_info,
121 uint32_t next_write_location)
123 ring_info->ring_buffer->write_index = next_write_location;
127 * @brief Get the next read location for the specified ring buffer
129 static inline uint32_t
130 get_next_read_location(hv_vmbus_ring_buffer_info* ring_info)
132 uint32_t next = ring_info->ring_buffer->read_index;
137 * @brief Get the next read location + offset for the specified ring buffer.
138 * This allows the caller to skip.
140 static inline uint32_t
141 get_next_read_location_with_offset(
142 hv_vmbus_ring_buffer_info* ring_info,
145 uint32_t next = ring_info->ring_buffer->read_index;
147 next %= ring_info->ring_data_size;
152 * @brief Set the next read location for the specified ring buffer
155 set_next_read_location(
156 hv_vmbus_ring_buffer_info* ring_info,
157 uint32_t next_read_location)
159 ring_info->ring_buffer->read_index = next_read_location;
163 * @brief Get the start of the ring buffer
166 get_ring_buffer(hv_vmbus_ring_buffer_info* ring_info)
168 return (void *) ring_info->ring_buffer->buffer;
172 * @brief Get the size of the ring buffer.
174 static inline uint32_t
175 get_ring_buffer_size(hv_vmbus_ring_buffer_info* ring_info)
177 return ring_info->ring_data_size;
181 * Get the read and write indices as uint64_t of the specified ring buffer.
183 static inline uint64_t
184 get_ring_buffer_indices(hv_vmbus_ring_buffer_info* ring_info)
186 return (uint64_t) ring_info->ring_buffer->write_index << 32;
190 hv_ring_buffer_read_begin(
191 hv_vmbus_ring_buffer_info* ring_info)
193 ring_info->ring_buffer->interrupt_mask = 1;
198 hv_ring_buffer_read_end(
199 hv_vmbus_ring_buffer_info* ring_info)
201 uint32_t read, write;
203 ring_info->ring_buffer->interrupt_mask = 0;
207 * Now check to see if the ring buffer is still empty.
208 * If it is not, we raced and we need to process new
211 get_ring_buffer_avail_bytes(ring_info, &read, &write);
217 * When we write to the ring buffer, check if the host needs to
218 * be signaled. Here is the details of this protocol:
220 * 1. The host guarantees that while it is draining the
221 * ring buffer, it will set the interrupt_mask to
222 * indicate it does not need to be interrupted when
223 * new data is placed.
225 * 2. The host guarantees that it will completely drain
226 * the ring buffer before exiting the read loop. Further,
227 * once the ring buffer is empty, it will clear the
228 * interrupt_mask and re-check to see if new data has
232 hv_ring_buffer_needsig_on_write(
233 uint32_t old_write_location,
234 hv_vmbus_ring_buffer_info* rbi)
237 if (rbi->ring_buffer->interrupt_mask)
240 /* Read memory barrier */
243 * This is the only case we need to signal when the
244 * ring transitions from being empty to non-empty.
246 if (old_write_location == rbi->ring_buffer->read_index)
252 static uint32_t copy_to_ring_buffer(
253 hv_vmbus_ring_buffer_info* ring_info,
254 uint32_t start_write_offset,
258 static uint32_t copy_from_ring_buffer(
259 hv_vmbus_ring_buffer_info* ring_info,
262 uint32_t start_read_offset);
266 * @brief Get the interrupt mask for the specified ring buffer.
269 hv_vmbus_get_ring_buffer_interrupt_mask(hv_vmbus_ring_buffer_info *rbi)
271 return rbi->ring_buffer->interrupt_mask;
275 * @brief Initialize the ring buffer.
278 hv_vmbus_ring_buffer_init(
279 hv_vmbus_ring_buffer_info* ring_info,
283 memset(ring_info, 0, sizeof(hv_vmbus_ring_buffer_info));
285 ring_info->ring_buffer = (hv_vmbus_ring_buffer*) buffer;
286 ring_info->ring_buffer->read_index =
287 ring_info->ring_buffer->write_index = 0;
289 ring_info->ring_size = buffer_len;
290 ring_info->ring_data_size = buffer_len - sizeof(hv_vmbus_ring_buffer);
292 mtx_init(&ring_info->ring_lock, "vmbus ring buffer", NULL, MTX_SPIN);
298 * @brief Cleanup the ring buffer.
300 void hv_ring_buffer_cleanup(hv_vmbus_ring_buffer_info* ring_info)
302 mtx_destroy(&ring_info->ring_lock);
306 * @brief Write to the ring buffer.
309 hv_ring_buffer_write(
310 hv_vmbus_ring_buffer_info* out_ring_info,
311 hv_vmbus_sg_buffer_list sg_buffers[],
312 uint32_t sg_buffer_count,
316 uint32_t byte_avail_to_write;
317 uint32_t byte_avail_to_read;
318 uint32_t old_write_location;
319 uint32_t total_bytes_to_write = 0;
321 volatile uint32_t next_write_location;
322 uint64_t prev_indices = 0;
324 for (i = 0; i < sg_buffer_count; i++) {
325 total_bytes_to_write += sg_buffers[i].length;
328 total_bytes_to_write += sizeof(uint64_t);
330 mtx_lock_spin(&out_ring_info->ring_lock);
332 get_ring_buffer_avail_bytes(out_ring_info, &byte_avail_to_read,
333 &byte_avail_to_write);
336 * If there is only room for the packet, assume it is full.
337 * Otherwise, the next time around, we think the ring buffer
338 * is empty since the read index == write index
341 if (byte_avail_to_write <= total_bytes_to_write) {
343 mtx_unlock_spin(&out_ring_info->ring_lock);
348 * Write to the ring buffer
350 next_write_location = get_next_write_location(out_ring_info);
352 old_write_location = next_write_location;
354 for (i = 0; i < sg_buffer_count; i++) {
355 next_write_location = copy_to_ring_buffer(out_ring_info,
356 next_write_location, (char *) sg_buffers[i].data,
357 sg_buffers[i].length);
361 * Set previous packet start
363 prev_indices = get_ring_buffer_indices(out_ring_info);
365 next_write_location = copy_to_ring_buffer(
366 out_ring_info, next_write_location,
367 (char *) &prev_indices, sizeof(uint64_t));
370 * Full memory barrier before upding the write index.
375 * Now, update the write location
377 set_next_write_location(out_ring_info, next_write_location);
379 mtx_unlock_spin(&out_ring_info->ring_lock);
381 *need_sig = hv_ring_buffer_needsig_on_write(old_write_location,
388 * @brief Read without advancing the read index.
392 hv_vmbus_ring_buffer_info* in_ring_info,
396 uint32_t bytesAvailToWrite;
397 uint32_t bytesAvailToRead;
398 uint32_t nextReadLocation = 0;
400 mtx_lock_spin(&in_ring_info->ring_lock);
402 get_ring_buffer_avail_bytes(in_ring_info, &bytesAvailToRead,
406 * Make sure there is something to read
408 if (bytesAvailToRead < buffer_len) {
409 mtx_unlock_spin(&in_ring_info->ring_lock);
414 * Convert to byte offset
416 nextReadLocation = get_next_read_location(in_ring_info);
418 nextReadLocation = copy_from_ring_buffer(
419 in_ring_info, (char *)buffer, buffer_len, nextReadLocation);
421 mtx_unlock_spin(&in_ring_info->ring_lock);
427 * @brief Read and advance the read index.
431 hv_vmbus_ring_buffer_info* in_ring_info,
436 uint32_t bytes_avail_to_write;
437 uint32_t bytes_avail_to_read;
438 uint32_t next_read_location = 0;
439 uint64_t prev_indices = 0;
444 mtx_lock_spin(&in_ring_info->ring_lock);
446 get_ring_buffer_avail_bytes(
447 in_ring_info, &bytes_avail_to_read,
448 &bytes_avail_to_write);
451 * Make sure there is something to read
453 if (bytes_avail_to_read < buffer_len) {
454 mtx_unlock_spin(&in_ring_info->ring_lock);
458 next_read_location = get_next_read_location_with_offset(
462 next_read_location = copy_from_ring_buffer(
468 next_read_location = copy_from_ring_buffer(
470 (char *) &prev_indices,
475 * Make sure all reads are done before we update the read index since
476 * the writer may start writing to the read area once the read index
482 * Update the read index
484 set_next_read_location(in_ring_info, next_read_location);
486 mtx_unlock_spin(&in_ring_info->ring_lock);
492 * @brief Helper routine to copy from source to ring buffer.
494 * Assume there is enough room. Handles wrap-around in dest case only!
498 hv_vmbus_ring_buffer_info* ring_info,
499 uint32_t start_write_offset,
503 char *ring_buffer = get_ring_buffer(ring_info);
504 uint32_t ring_buffer_size = get_ring_buffer_size(ring_info);
507 if (src_len > ring_buffer_size - start_write_offset) {
508 /* wrap-around detected! */
509 fragLen = ring_buffer_size - start_write_offset;
510 memcpy(ring_buffer + start_write_offset, src, fragLen);
511 memcpy(ring_buffer, src + fragLen, src_len - fragLen);
513 memcpy(ring_buffer + start_write_offset, src, src_len);
516 start_write_offset += src_len;
517 start_write_offset %= ring_buffer_size;
519 return (start_write_offset);
523 * @brief Helper routine to copy to source from ring buffer.
525 * Assume there is enough room. Handles wrap-around in src case only!
528 copy_from_ring_buffer(
529 hv_vmbus_ring_buffer_info* ring_info,
532 uint32_t start_read_offset)
535 char *ring_buffer = get_ring_buffer(ring_info);
536 uint32_t ring_buffer_size = get_ring_buffer_size(ring_info);
538 if (dest_len > ring_buffer_size - start_read_offset) {
539 /* wrap-around detected at the src */
540 fragLen = ring_buffer_size - start_read_offset;
541 memcpy(dest, ring_buffer + start_read_offset, fragLen);
542 memcpy(dest + fragLen, ring_buffer, dest_len - fragLen);
544 memcpy(dest, ring_buffer + start_read_offset, dest_len);
547 start_read_offset += dest_len;
548 start_read_offset %= ring_buffer_size;
550 return (start_read_offset);