]>
Commit | Line | Data |
---|---|---|
3e7ee490 HJ |
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> | |
b2a5a585 | 21 | * K. Y. Srinivasan <kys@microsoft.com> |
3e7ee490 HJ |
22 | * |
23 | */ | |
0a46618d | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
3e7ee490 | 25 | |
a0086dc5 GKH |
26 | #include <linux/kernel.h> |
27 | #include <linux/mm.h> | |
46a97191 | 28 | #include <linux/hyperv.h> |
011a7c3c | 29 | #include <linux/uio.h> |
9988ce68 VK |
30 | #include <linux/vmalloc.h> |
31 | #include <linux/slab.h> | |
3f335ea2 | 32 | |
0f2a6619 | 33 | #include "hyperv_vmbus.h" |
3e7ee490 | 34 | |
6fdf3b21 S |
35 | void hv_begin_read(struct hv_ring_buffer_info *rbi) |
36 | { | |
37 | rbi->ring_buffer->interrupt_mask = 1; | |
dcd0eeca | 38 | virt_mb(); |
6fdf3b21 S |
39 | } |
40 | ||
41 | u32 hv_end_read(struct hv_ring_buffer_info *rbi) | |
42 | { | |
6fdf3b21 S |
43 | |
44 | rbi->ring_buffer->interrupt_mask = 0; | |
dcd0eeca | 45 | virt_mb(); |
6fdf3b21 S |
46 | |
47 | /* | |
48 | * Now check to see if the ring buffer is still empty. | |
49 | * If it is not, we raced and we need to process new | |
50 | * incoming messages. | |
51 | */ | |
a6341f00 | 52 | return hv_get_bytes_to_read(rbi); |
6fdf3b21 S |
53 | } |
54 | ||
98fa8cf4 S |
55 | /* |
56 | * When we write to the ring buffer, check if the host needs to | |
57 | * be signaled. Here is the details of this protocol: | |
58 | * | |
59 | * 1. The host guarantees that while it is draining the | |
60 | * ring buffer, it will set the interrupt_mask to | |
61 | * indicate it does not need to be interrupted when | |
62 | * new data is placed. | |
63 | * | |
64 | * 2. The host guarantees that it will completely drain | |
65 | * the ring buffer before exiting the read loop. Further, | |
66 | * once the ring buffer is empty, it will clear the | |
67 | * interrupt_mask and re-check to see if new data has | |
68 | * arrived. | |
1f6ee4e7 S |
69 | * |
70 | * KYS: Oct. 30, 2016: | |
71 | * It looks like Windows hosts have logic to deal with DOS attacks that | |
72 | * can be triggered if it receives interrupts when it is not expecting | |
73 | * the interrupt. The host expects interrupts only when the ring | |
74 | * transitions from empty to non-empty (or full to non full on the guest | |
75 | * to host ring). | |
76 | * So, base the signaling decision solely on the ring state until the | |
77 | * host logic is fixed. | |
98fa8cf4 S |
78 | */ |
79 | ||
b103a56f | 80 | static void hv_signal_on_write(u32 old_write, struct vmbus_channel *channel) |
98fa8cf4 | 81 | { |
1f6ee4e7 S |
82 | struct hv_ring_buffer_info *rbi = &channel->outbound; |
83 | ||
dcd0eeca | 84 | virt_mb(); |
d45faaee | 85 | if (READ_ONCE(rbi->ring_buffer->interrupt_mask)) |
1f6ee4e7 | 86 | return; |
98fa8cf4 | 87 | |
e91e84fa | 88 | /* check interrupt_mask before read_index */ |
dcd0eeca | 89 | virt_rmb(); |
98fa8cf4 S |
90 | /* |
91 | * This is the only case we need to signal when the | |
92 | * ring transitions from being empty to non-empty. | |
93 | */ | |
d45faaee | 94 | if (old_write == READ_ONCE(rbi->ring_buffer->read_index)) |
1f6ee4e7 | 95 | vmbus_setevent(channel); |
98fa8cf4 | 96 | |
1f6ee4e7 | 97 | return; |
98fa8cf4 S |
98 | } |
99 | ||
822f18d4 | 100 | /* Get the next write location for the specified ring buffer. */ |
4d643114 | 101 | static inline u32 |
2b8a912e | 102 | hv_get_next_write_location(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 103 | { |
fc8c72eb | 104 | u32 next = ring_info->ring_buffer->write_index; |
3e7ee490 | 105 | |
3e7ee490 HJ |
106 | return next; |
107 | } | |
108 | ||
822f18d4 | 109 | /* Set the next write location for the specified ring buffer. */ |
3e7ee490 | 110 | static inline void |
2b8a912e | 111 | hv_set_next_write_location(struct hv_ring_buffer_info *ring_info, |
fc8c72eb | 112 | u32 next_write_location) |
3e7ee490 | 113 | { |
fc8c72eb | 114 | ring_info->ring_buffer->write_index = next_write_location; |
3e7ee490 HJ |
115 | } |
116 | ||
822f18d4 | 117 | /* Get the next read location for the specified ring buffer. */ |
4d643114 | 118 | static inline u32 |
2b8a912e | 119 | hv_get_next_read_location(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 120 | { |
fc8c72eb | 121 | u32 next = ring_info->ring_buffer->read_index; |
3e7ee490 | 122 | |
3e7ee490 HJ |
123 | return next; |
124 | } | |
125 | ||
b2a5a585 | 126 | /* |
b2a5a585 | 127 | * Get the next read location + offset for the specified ring buffer. |
822f18d4 | 128 | * This allows the caller to skip. |
b2a5a585 | 129 | */ |
4d643114 | 130 | static inline u32 |
2b8a912e | 131 | hv_get_next_readlocation_withoffset(struct hv_ring_buffer_info *ring_info, |
1ac58644 | 132 | u32 offset) |
3e7ee490 | 133 | { |
fc8c72eb | 134 | u32 next = ring_info->ring_buffer->read_index; |
3e7ee490 | 135 | |
fc8c72eb HZ |
136 | next += offset; |
137 | next %= ring_info->ring_datasize; | |
3e7ee490 HJ |
138 | |
139 | return next; | |
140 | } | |
141 | ||
822f18d4 | 142 | /* Set the next read location for the specified ring buffer. */ |
3e7ee490 | 143 | static inline void |
2b8a912e | 144 | hv_set_next_read_location(struct hv_ring_buffer_info *ring_info, |
fc8c72eb | 145 | u32 next_read_location) |
3e7ee490 | 146 | { |
fc8c72eb | 147 | ring_info->ring_buffer->read_index = next_read_location; |
ab028db4 | 148 | ring_info->priv_read_index = next_read_location; |
3e7ee490 HJ |
149 | } |
150 | ||
822f18d4 | 151 | /* Get the size of the ring buffer. */ |
4d643114 | 152 | static inline u32 |
2b8a912e | 153 | hv_get_ring_buffersize(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 154 | { |
fc8c72eb | 155 | return ring_info->ring_datasize; |
3e7ee490 HJ |
156 | } |
157 | ||
822f18d4 | 158 | /* Get the read and write indices as u64 of the specified ring buffer. */ |
59471438 | 159 | static inline u64 |
2b8a912e | 160 | hv_get_ring_bufferindices(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 161 | { |
fc8c72eb | 162 | return (u64)ring_info->ring_buffer->write_index << 32; |
3e7ee490 HJ |
163 | } |
164 | ||
8f1136ae | 165 | /* |
8f1136ae S |
166 | * Helper routine to copy to source from ring buffer. |
167 | * Assume there is enough room. Handles wrap-around in src case only!! | |
8f1136ae S |
168 | */ |
169 | static u32 hv_copyfrom_ringbuffer( | |
170 | struct hv_ring_buffer_info *ring_info, | |
171 | void *dest, | |
172 | u32 destlen, | |
173 | u32 start_read_offset) | |
174 | { | |
175 | void *ring_buffer = hv_get_ring_buffer(ring_info); | |
176 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); | |
177 | ||
f24f0b49 | 178 | memcpy(dest, ring_buffer + start_read_offset, destlen); |
8f1136ae S |
179 | |
180 | start_read_offset += destlen; | |
181 | start_read_offset %= ring_buffer_size; | |
182 | ||
183 | return start_read_offset; | |
184 | } | |
185 | ||
186 | ||
7581578d | 187 | /* |
7581578d S |
188 | * Helper routine to copy from source to ring buffer. |
189 | * Assume there is enough room. Handles wrap-around in dest case only!! | |
7581578d S |
190 | */ |
191 | static u32 hv_copyto_ringbuffer( | |
fc8c72eb HZ |
192 | struct hv_ring_buffer_info *ring_info, |
193 | u32 start_write_offset, | |
194 | void *src, | |
7581578d S |
195 | u32 srclen) |
196 | { | |
197 | void *ring_buffer = hv_get_ring_buffer(ring_info); | |
198 | u32 ring_buffer_size = hv_get_ring_buffersize(ring_info); | |
f24f0b49 VK |
199 | |
200 | memcpy(ring_buffer + start_write_offset, src, srclen); | |
3e7ee490 | 201 | |
7581578d S |
202 | start_write_offset += srclen; |
203 | start_write_offset %= ring_buffer_size; | |
204 | ||
205 | return start_write_offset; | |
206 | } | |
3e7ee490 | 207 | |
822f18d4 | 208 | /* Get various debug metrics for the specified ring buffer. */ |
a75b61d5 | 209 | void hv_ringbuffer_get_debuginfo(struct hv_ring_buffer_info *ring_info, |
80682b7a | 210 | struct hv_ring_buffer_debug_info *debug_info) |
3e7ee490 | 211 | { |
fc8c72eb HZ |
212 | u32 bytes_avail_towrite; |
213 | u32 bytes_avail_toread; | |
3e7ee490 | 214 | |
fc8c72eb | 215 | if (ring_info->ring_buffer) { |
2b8a912e | 216 | hv_get_ringbuffer_availbytes(ring_info, |
fc8c72eb HZ |
217 | &bytes_avail_toread, |
218 | &bytes_avail_towrite); | |
3e7ee490 | 219 | |
fc8c72eb HZ |
220 | debug_info->bytes_avail_toread = bytes_avail_toread; |
221 | debug_info->bytes_avail_towrite = bytes_avail_towrite; | |
82f8bd40 | 222 | debug_info->current_read_index = |
fc8c72eb | 223 | ring_info->ring_buffer->read_index; |
82f8bd40 | 224 | debug_info->current_write_index = |
fc8c72eb | 225 | ring_info->ring_buffer->write_index; |
82f8bd40 | 226 | debug_info->current_interrupt_mask = |
fc8c72eb | 227 | ring_info->ring_buffer->interrupt_mask; |
3e7ee490 HJ |
228 | } |
229 | } | |
230 | ||
822f18d4 | 231 | /* Initialize the ring buffer. */ |
72a95cbc | 232 | int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, |
9988ce68 | 233 | struct page *pages, u32 page_cnt) |
3e7ee490 | 234 | { |
9988ce68 VK |
235 | int i; |
236 | struct page **pages_wraparound; | |
237 | ||
238 | BUILD_BUG_ON((sizeof(struct hv_ring_buffer) != PAGE_SIZE)); | |
3e7ee490 | 239 | |
fc8c72eb | 240 | memset(ring_info, 0, sizeof(struct hv_ring_buffer_info)); |
3e7ee490 | 241 | |
9988ce68 VK |
242 | /* |
243 | * First page holds struct hv_ring_buffer, do wraparound mapping for | |
244 | * the rest. | |
245 | */ | |
246 | pages_wraparound = kzalloc(sizeof(struct page *) * (page_cnt * 2 - 1), | |
247 | GFP_KERNEL); | |
248 | if (!pages_wraparound) | |
249 | return -ENOMEM; | |
250 | ||
251 | pages_wraparound[0] = pages; | |
252 | for (i = 0; i < 2 * (page_cnt - 1); i++) | |
253 | pages_wraparound[i + 1] = &pages[i % (page_cnt - 1) + 1]; | |
254 | ||
255 | ring_info->ring_buffer = (struct hv_ring_buffer *) | |
256 | vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, PAGE_KERNEL); | |
257 | ||
258 | kfree(pages_wraparound); | |
259 | ||
260 | ||
261 | if (!ring_info->ring_buffer) | |
262 | return -ENOMEM; | |
263 | ||
fc8c72eb HZ |
264 | ring_info->ring_buffer->read_index = |
265 | ring_info->ring_buffer->write_index = 0; | |
3e7ee490 | 266 | |
822f18d4 | 267 | /* Set the feature bit for enabling flow control. */ |
046c7911 S |
268 | ring_info->ring_buffer->feature_bits.value = 1; |
269 | ||
9988ce68 VK |
270 | ring_info->ring_size = page_cnt << PAGE_SHIFT; |
271 | ring_info->ring_datasize = ring_info->ring_size - | |
272 | sizeof(struct hv_ring_buffer); | |
3e7ee490 | 273 | |
fc8c72eb | 274 | spin_lock_init(&ring_info->ring_lock); |
3e7ee490 HJ |
275 | |
276 | return 0; | |
277 | } | |
278 | ||
822f18d4 | 279 | /* Cleanup the ring buffer. */ |
2dba688b | 280 | void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info) |
3e7ee490 | 281 | { |
9988ce68 | 282 | vunmap(ring_info->ring_buffer); |
3e7ee490 HJ |
283 | } |
284 | ||
822f18d4 | 285 | /* Write to the ring buffer. */ |
1f6ee4e7 | 286 | int hv_ringbuffer_write(struct vmbus_channel *channel, |
b103a56f | 287 | struct kvec *kv_list, u32 kv_count, bool lock) |
3e7ee490 | 288 | { |
4408f531 | 289 | int i = 0; |
fc8c72eb | 290 | u32 bytes_avail_towrite; |
fc8c72eb | 291 | u32 totalbytes_towrite = 0; |
3e7ee490 | 292 | |
66a60543 | 293 | u32 next_write_location; |
98fa8cf4 | 294 | u32 old_write; |
fc8c72eb | 295 | u64 prev_indices = 0; |
fe760e4d | 296 | unsigned long flags = 0; |
1f6ee4e7 | 297 | struct hv_ring_buffer_info *outring_info = &channel->outbound; |
3e7ee490 | 298 | |
e7e97dd8 S |
299 | if (channel->rescind) |
300 | return -ENODEV; | |
301 | ||
011a7c3c S |
302 | for (i = 0; i < kv_count; i++) |
303 | totalbytes_towrite += kv_list[i].iov_len; | |
3e7ee490 | 304 | |
fc8c72eb | 305 | totalbytes_towrite += sizeof(u64); |
3e7ee490 | 306 | |
fe760e4d S |
307 | if (lock) |
308 | spin_lock_irqsave(&outring_info->ring_lock, flags); | |
3e7ee490 | 309 | |
a6341f00 | 310 | bytes_avail_towrite = hv_get_bytes_to_write(outring_info); |
3e7ee490 | 311 | |
822f18d4 VK |
312 | /* |
313 | * If there is only room for the packet, assume it is full. | |
314 | * Otherwise, the next time around, we think the ring buffer | |
315 | * is empty since the read index == write index. | |
316 | */ | |
fc8c72eb | 317 | if (bytes_avail_towrite <= totalbytes_towrite) { |
fe760e4d S |
318 | if (lock) |
319 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); | |
d2598f01 | 320 | return -EAGAIN; |
3e7ee490 HJ |
321 | } |
322 | ||
454f18a9 | 323 | /* Write to the ring buffer */ |
2b8a912e | 324 | next_write_location = hv_get_next_write_location(outring_info); |
3e7ee490 | 325 | |
98fa8cf4 S |
326 | old_write = next_write_location; |
327 | ||
011a7c3c | 328 | for (i = 0; i < kv_count; i++) { |
2b8a912e | 329 | next_write_location = hv_copyto_ringbuffer(outring_info, |
fc8c72eb | 330 | next_write_location, |
011a7c3c S |
331 | kv_list[i].iov_base, |
332 | kv_list[i].iov_len); | |
3e7ee490 HJ |
333 | } |
334 | ||
454f18a9 | 335 | /* Set previous packet start */ |
2b8a912e | 336 | prev_indices = hv_get_ring_bufferindices(outring_info); |
3e7ee490 | 337 | |
2b8a912e | 338 | next_write_location = hv_copyto_ringbuffer(outring_info, |
fc8c72eb HZ |
339 | next_write_location, |
340 | &prev_indices, | |
b219b3f7 | 341 | sizeof(u64)); |
3e7ee490 | 342 | |
98fa8cf4 | 343 | /* Issue a full memory barrier before updating the write index */ |
dcd0eeca | 344 | virt_mb(); |
3e7ee490 | 345 | |
454f18a9 | 346 | /* Now, update the write location */ |
2b8a912e | 347 | hv_set_next_write_location(outring_info, next_write_location); |
3e7ee490 | 348 | |
3e7ee490 | 349 | |
fe760e4d S |
350 | if (lock) |
351 | spin_unlock_irqrestore(&outring_info->ring_lock, flags); | |
98fa8cf4 | 352 | |
b103a56f | 353 | hv_signal_on_write(old_write, channel); |
e7e97dd8 S |
354 | |
355 | if (channel->rescind) | |
356 | return -ENODEV; | |
357 | ||
3e7ee490 HJ |
358 | return 0; |
359 | } | |
360 | ||
3372592a | 361 | int hv_ringbuffer_read(struct vmbus_channel *channel, |
940b68e2 | 362 | void *buffer, u32 buflen, u32 *buffer_actual_len, |
3372592a | 363 | u64 *requestid, bool raw) |
3e7ee490 | 364 | { |
fc8c72eb HZ |
365 | u32 bytes_avail_toread; |
366 | u32 next_read_location = 0; | |
367 | u64 prev_indices = 0; | |
940b68e2 VK |
368 | struct vmpacket_descriptor desc; |
369 | u32 offset; | |
370 | u32 packetlen; | |
371 | int ret = 0; | |
3372592a | 372 | struct hv_ring_buffer_info *inring_info = &channel->inbound; |
3e7ee490 | 373 | |
fc8c72eb | 374 | if (buflen <= 0) |
a16e1485 | 375 | return -EINVAL; |
3e7ee490 | 376 | |
3e7ee490 | 377 | |
940b68e2 VK |
378 | *buffer_actual_len = 0; |
379 | *requestid = 0; | |
380 | ||
a6341f00 | 381 | bytes_avail_toread = hv_get_bytes_to_read(inring_info); |
454f18a9 | 382 | /* Make sure there is something to read */ |
940b68e2 VK |
383 | if (bytes_avail_toread < sizeof(desc)) { |
384 | /* | |
385 | * No error is set when there is even no header, drivers are | |
386 | * supposed to analyze buffer_actual_len. | |
387 | */ | |
3eba9a77 | 388 | return ret; |
940b68e2 | 389 | } |
3e7ee490 | 390 | |
433e19cf | 391 | init_cached_read_index(channel); |
940b68e2 VK |
392 | next_read_location = hv_get_next_read_location(inring_info); |
393 | next_read_location = hv_copyfrom_ringbuffer(inring_info, &desc, | |
394 | sizeof(desc), | |
395 | next_read_location); | |
396 | ||
397 | offset = raw ? 0 : (desc.offset8 << 3); | |
398 | packetlen = (desc.len8 << 3) - offset; | |
399 | *buffer_actual_len = packetlen; | |
400 | *requestid = desc.trans_id; | |
401 | ||
3eba9a77 S |
402 | if (bytes_avail_toread < packetlen + offset) |
403 | return -EAGAIN; | |
940b68e2 | 404 | |
3eba9a77 S |
405 | if (packetlen > buflen) |
406 | return -ENOBUFS; | |
3e7ee490 | 407 | |
1ac58644 | 408 | next_read_location = |
2b8a912e | 409 | hv_get_next_readlocation_withoffset(inring_info, offset); |
3e7ee490 | 410 | |
2b8a912e | 411 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
fc8c72eb | 412 | buffer, |
940b68e2 | 413 | packetlen, |
fc8c72eb | 414 | next_read_location); |
3e7ee490 | 415 | |
2b8a912e | 416 | next_read_location = hv_copyfrom_ringbuffer(inring_info, |
fc8c72eb | 417 | &prev_indices, |
4408f531 | 418 | sizeof(u64), |
fc8c72eb | 419 | next_read_location); |
3e7ee490 | 420 | |
822f18d4 VK |
421 | /* |
422 | * Make sure all reads are done before we update the read index since | |
423 | * the writer may start writing to the read area once the read index | |
424 | * is updated. | |
425 | */ | |
dcd0eeca | 426 | virt_mb(); |
3e7ee490 | 427 | |
454f18a9 | 428 | /* Update the read index */ |
2b8a912e | 429 | hv_set_next_read_location(inring_info, next_read_location); |
3e7ee490 | 430 | |
3372592a | 431 | hv_signal_on_read(channel); |
c2b8e520 | 432 | |
940b68e2 | 433 | return ret; |
b5f53dde | 434 | } |