1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
8 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/mm.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/hyperv.h>
18 #include <linux/uio.h>
19 #include <linux/interrupt.h>
20 #include <linux/set_memory.h>
21 #include <asm/page.h>
22 #include <asm/mem_encrypt.h>
23 #include <asm/mshyperv.h>
24
25 #include "hyperv_vmbus.h"
26
27 /*
28 * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses
29 *
30 * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does.
31 *
32 * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header
33 * (because of the alignment requirement), however, the hypervisor only
34 * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a
35 * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a
36 * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the
37 * total size that the guest uses minus twice of the gap size.
38 */
hv_gpadl_size(enum hv_gpadl_type type,u32 size)39 static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size)
40 {
41 switch (type) {
42 case HV_GPADL_BUFFER:
43 return size;
44 case HV_GPADL_RING:
45 /* The size of a ringbuffer must be page-aligned */
46 BUG_ON(size % PAGE_SIZE);
47 /*
48 * Two things to notice here:
49 * 1) We're processing two ring buffers as a unit
50 * 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in
51 * the first guest-size page of each of the two ring buffers.
52 * So we effectively subtract out two guest-size pages, and add
53 * back two Hyper-V size pages.
54 */
55 return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
56 }
57 BUG();
58 return 0;
59 }
60
61 /*
62 * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of
63 * HV_HYP_PAGE) in a ring gpadl based on the
64 * offset in the guest
65 *
66 * @offset: the offset (in bytes) where the send ringbuffer starts in the
67 * virtual address space of the guest
68 */
hv_ring_gpadl_send_hvpgoffset(u32 offset)69 static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset)
70 {
71
72 /*
73 * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the
74 * header (because of the alignment requirement), however, the
75 * hypervisor only uses the first HV_HYP_PAGE_SIZE as the header,
76 * therefore leaving a (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap.
77 *
78 * And to calculate the effective send offset in gpadl, we need to
79 * substract this gap.
80 */
81 return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT;
82 }
83
84 /*
85 * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in
86 * the gpadl
87 *
88 * @type: the type of the gpadl
89 * @kbuffer: the pointer to the gpadl in the guest
90 * @size: the total size (in bytes) of the gpadl
91 * @send_offset: the offset (in bytes) where the send ringbuffer starts in the
92 * virtual address space of the guest
93 * @i: the index
94 */
hv_gpadl_hvpfn(enum hv_gpadl_type type,void * kbuffer,u32 size,u32 send_offset,int i)95 static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer,
96 u32 size, u32 send_offset, int i)
97 {
98 int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset);
99 unsigned long delta = 0UL;
100
101 switch (type) {
102 case HV_GPADL_BUFFER:
103 break;
104 case HV_GPADL_RING:
105 if (i == 0)
106 delta = 0;
107 else if (i <= send_idx)
108 delta = PAGE_SIZE - HV_HYP_PAGE_SIZE;
109 else
110 delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
111 break;
112 default:
113 BUG();
114 break;
115 }
116
117 return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i));
118 }
119
120 /*
121 * vmbus_setevent- Trigger an event notification on the specified
122 * channel.
123 */
vmbus_setevent(struct vmbus_channel * channel)124 void vmbus_setevent(struct vmbus_channel *channel)
125 {
126 struct hv_monitor_page *monitorpage;
127
128 trace_vmbus_setevent(channel);
129
130 /*
131 * For channels marked as in "low latency" mode
132 * bypass the monitor page mechanism.
133 */
134 if (channel->offermsg.monitor_allocated && !channel->low_latency) {
135 vmbus_send_interrupt(channel->offermsg.child_relid);
136
137 /* Get the child to parent monitor page */
138 monitorpage = vmbus_connection.monitor_pages[1];
139
140 sync_set_bit(channel->monitor_bit,
141 (unsigned long *)&monitorpage->trigger_group
142 [channel->monitor_grp].pending);
143
144 } else {
145 vmbus_set_event(channel);
146 }
147 }
148 EXPORT_SYMBOL_GPL(vmbus_setevent);
149
150 /* vmbus_free_ring - drop mapping of ring buffer */
vmbus_free_ring(struct vmbus_channel * channel)151 void vmbus_free_ring(struct vmbus_channel *channel)
152 {
153 hv_ringbuffer_cleanup(&channel->outbound);
154 hv_ringbuffer_cleanup(&channel->inbound);
155
156 if (channel->ringbuffer_page) {
157 /* In a CoCo VM leak the memory if it didn't get re-encrypted */
158 if (!channel->ringbuffer_gpadlhandle.decrypted)
159 __free_pages(channel->ringbuffer_page,
160 get_order(channel->ringbuffer_pagecount
161 << PAGE_SHIFT));
162 channel->ringbuffer_page = NULL;
163 }
164 }
165 EXPORT_SYMBOL_GPL(vmbus_free_ring);
166
167 /* vmbus_alloc_ring - allocate and map pages for ring buffer */
vmbus_alloc_ring(struct vmbus_channel * newchannel,u32 send_size,u32 recv_size)168 int vmbus_alloc_ring(struct vmbus_channel *newchannel,
169 u32 send_size, u32 recv_size)
170 {
171 struct page *page;
172 int order;
173
174 if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
175 return -EINVAL;
176
177 /* Allocate the ring buffer */
178 order = get_order(send_size + recv_size);
179 page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
180 GFP_KERNEL|__GFP_ZERO, order);
181
182 if (!page)
183 page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
184
185 if (!page)
186 return -ENOMEM;
187
188 newchannel->ringbuffer_page = page;
189 newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
190 newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
191
192 return 0;
193 }
194 EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
195
196 /* Used for Hyper-V Socket: a guest client's connect() to the host */
vmbus_send_tl_connect_request(const guid_t * shv_guest_servie_id,const guid_t * shv_host_servie_id)197 int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
198 const guid_t *shv_host_servie_id)
199 {
200 struct vmbus_channel_tl_connect_request conn_msg;
201 int ret;
202
203 memset(&conn_msg, 0, sizeof(conn_msg));
204 conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
205 conn_msg.guest_endpoint_id = *shv_guest_servie_id;
206 conn_msg.host_service_id = *shv_host_servie_id;
207
208 ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
209
210 trace_vmbus_send_tl_connect_request(&conn_msg, ret);
211
212 return ret;
213 }
214 EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
215
send_modifychannel_without_ack(struct vmbus_channel * channel,u32 target_vp)216 static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp)
217 {
218 struct vmbus_channel_modifychannel msg;
219 int ret;
220
221 memset(&msg, 0, sizeof(msg));
222 msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
223 msg.child_relid = channel->offermsg.child_relid;
224 msg.target_vp = target_vp;
225
226 ret = vmbus_post_msg(&msg, sizeof(msg), true);
227 trace_vmbus_send_modifychannel(&msg, ret);
228
229 return ret;
230 }
231
send_modifychannel_with_ack(struct vmbus_channel * channel,u32 target_vp)232 static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp)
233 {
234 struct vmbus_channel_modifychannel *msg;
235 struct vmbus_channel_msginfo *info;
236 unsigned long flags;
237 int ret;
238
239 info = kzalloc(sizeof(struct vmbus_channel_msginfo) +
240 sizeof(struct vmbus_channel_modifychannel),
241 GFP_KERNEL);
242 if (!info)
243 return -ENOMEM;
244
245 init_completion(&info->waitevent);
246 info->waiting_channel = channel;
247
248 msg = (struct vmbus_channel_modifychannel *)info->msg;
249 msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL;
250 msg->child_relid = channel->offermsg.child_relid;
251 msg->target_vp = target_vp;
252
253 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
254 list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list);
255 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
256
257 ret = vmbus_post_msg(msg, sizeof(*msg), true);
258 trace_vmbus_send_modifychannel(msg, ret);
259 if (ret != 0) {
260 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
261 list_del(&info->msglistentry);
262 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
263 goto free_info;
264 }
265
266 /*
267 * Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on
268 * the mutex and be unable to signal the completion.
269 *
270 * See the caller target_cpu_store() for information about the usage of the
271 * mutex.
272 */
273 mutex_unlock(&vmbus_connection.channel_mutex);
274 wait_for_completion(&info->waitevent);
275 mutex_lock(&vmbus_connection.channel_mutex);
276
277 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
278 list_del(&info->msglistentry);
279 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
280
281 if (info->response.modify_response.status)
282 ret = -EAGAIN;
283
284 free_info:
285 kfree(info);
286 return ret;
287 }
288
289 /*
290 * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
291 *
292 * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3
293 * or later is negotiated, Hyper-V always sends an ACK in response to such a
294 * message. For VMbus version 5.2 and earlier, it never sends an ACK. With-
295 * out an ACK, we can not know when the host will stop interrupting the "old"
296 * vCPU and start interrupting the "new" vCPU for the given channel.
297 *
298 * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
299 * VERSION_WIN10_V4_1.
300 */
vmbus_send_modifychannel(struct vmbus_channel * channel,u32 target_vp)301 int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp)
302 {
303 if (vmbus_proto_version >= VERSION_WIN10_V5_3)
304 return send_modifychannel_with_ack(channel, target_vp);
305 return send_modifychannel_without_ack(channel, target_vp);
306 }
307 EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
308
309 /*
310 * create_gpadl_header - Creates a gpadl for the specified buffer
311 */
create_gpadl_header(enum hv_gpadl_type type,void * kbuffer,u32 size,u32 send_offset,struct vmbus_channel_msginfo ** msginfo)312 static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,
313 u32 size, u32 send_offset,
314 struct vmbus_channel_msginfo **msginfo)
315 {
316 int i;
317 int pagecount;
318 struct vmbus_channel_gpadl_header *gpadl_header;
319 struct vmbus_channel_gpadl_body *gpadl_body;
320 struct vmbus_channel_msginfo *msgheader;
321 struct vmbus_channel_msginfo *msgbody = NULL;
322 u32 msgsize;
323
324 int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
325
326 pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT;
327
328 /* do we need a gpadl body msg */
329 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
330 sizeof(struct vmbus_channel_gpadl_header) -
331 sizeof(struct gpa_range);
332 pfncount = pfnsize / sizeof(u64);
333
334 if (pagecount > pfncount) {
335 /* we need a gpadl body */
336 /* fill in the header */
337 msgsize = sizeof(struct vmbus_channel_msginfo) +
338 sizeof(struct vmbus_channel_gpadl_header) +
339 sizeof(struct gpa_range) + pfncount * sizeof(u64);
340 msgheader = kzalloc(msgsize, GFP_KERNEL);
341 if (!msgheader)
342 goto nomem;
343
344 INIT_LIST_HEAD(&msgheader->submsglist);
345 msgheader->msgsize = msgsize;
346
347 gpadl_header = (struct vmbus_channel_gpadl_header *)
348 msgheader->msg;
349 gpadl_header->rangecount = 1;
350 gpadl_header->range_buflen = sizeof(struct gpa_range) +
351 pagecount * sizeof(u64);
352 gpadl_header->range[0].byte_offset = 0;
353 gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
354 for (i = 0; i < pfncount; i++)
355 gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
356 type, kbuffer, size, send_offset, i);
357 *msginfo = msgheader;
358
359 pfnsum = pfncount;
360 pfnleft = pagecount - pfncount;
361
362 /* how many pfns can we fit */
363 pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
364 sizeof(struct vmbus_channel_gpadl_body);
365 pfncount = pfnsize / sizeof(u64);
366
367 /* fill in the body */
368 while (pfnleft) {
369 if (pfnleft > pfncount)
370 pfncurr = pfncount;
371 else
372 pfncurr = pfnleft;
373
374 msgsize = sizeof(struct vmbus_channel_msginfo) +
375 sizeof(struct vmbus_channel_gpadl_body) +
376 pfncurr * sizeof(u64);
377 msgbody = kzalloc(msgsize, GFP_KERNEL);
378
379 if (!msgbody) {
380 struct vmbus_channel_msginfo *pos = NULL;
381 struct vmbus_channel_msginfo *tmp = NULL;
382 /*
383 * Free up all the allocated messages.
384 */
385 list_for_each_entry_safe(pos, tmp,
386 &msgheader->submsglist,
387 msglistentry) {
388
389 list_del(&pos->msglistentry);
390 kfree(pos);
391 }
392
393 goto nomem;
394 }
395
396 msgbody->msgsize = msgsize;
397 gpadl_body =
398 (struct vmbus_channel_gpadl_body *)msgbody->msg;
399
400 /*
401 * Gpadl is u32 and we are using a pointer which could
402 * be 64-bit
403 * This is governed by the guest/host protocol and
404 * so the hypervisor guarantees that this is ok.
405 */
406 for (i = 0; i < pfncurr; i++)
407 gpadl_body->pfn[i] = hv_gpadl_hvpfn(type,
408 kbuffer, size, send_offset, pfnsum + i);
409
410 /* add to msg header */
411 list_add_tail(&msgbody->msglistentry,
412 &msgheader->submsglist);
413 pfnsum += pfncurr;
414 pfnleft -= pfncurr;
415 }
416 } else {
417 /* everything fits in a header */
418 msgsize = sizeof(struct vmbus_channel_msginfo) +
419 sizeof(struct vmbus_channel_gpadl_header) +
420 sizeof(struct gpa_range) + pagecount * sizeof(u64);
421 msgheader = kzalloc(msgsize, GFP_KERNEL);
422 if (msgheader == NULL)
423 goto nomem;
424
425 INIT_LIST_HEAD(&msgheader->submsglist);
426 msgheader->msgsize = msgsize;
427
428 gpadl_header = (struct vmbus_channel_gpadl_header *)
429 msgheader->msg;
430 gpadl_header->rangecount = 1;
431 gpadl_header->range_buflen = sizeof(struct gpa_range) +
432 pagecount * sizeof(u64);
433 gpadl_header->range[0].byte_offset = 0;
434 gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
435 for (i = 0; i < pagecount; i++)
436 gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
437 type, kbuffer, size, send_offset, i);
438
439 *msginfo = msgheader;
440 }
441
442 return 0;
443 nomem:
444 kfree(msgheader);
445 kfree(msgbody);
446 return -ENOMEM;
447 }
448
449 /*
450 * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer
451 *
452 * @channel: a channel
453 * @type: the type of the corresponding GPADL, only meaningful for the guest.
454 * @kbuffer: from kmalloc or vmalloc
455 * @size: page-size multiple
456 * @send_offset: the offset (in bytes) where the send ring buffer starts,
457 * should be 0 for BUFFER type gpadl
458 * @gpadl_handle: some funky thing
459 */
__vmbus_establish_gpadl(struct vmbus_channel * channel,enum hv_gpadl_type type,void * kbuffer,u32 size,u32 send_offset,struct vmbus_gpadl * gpadl)460 static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
461 enum hv_gpadl_type type, void *kbuffer,
462 u32 size, u32 send_offset,
463 struct vmbus_gpadl *gpadl)
464 {
465 struct vmbus_channel_gpadl_header *gpadlmsg;
466 struct vmbus_channel_gpadl_body *gpadl_body;
467 struct vmbus_channel_msginfo *msginfo = NULL;
468 struct vmbus_channel_msginfo *submsginfo, *tmp;
469 struct list_head *curr;
470 u32 next_gpadl_handle;
471 unsigned long flags;
472 int ret = 0;
473
474 next_gpadl_handle =
475 (atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
476
477 ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
478 if (ret) {
479 gpadl->decrypted = false;
480 return ret;
481 }
482
483 /*
484 * Set the "decrypted" flag to true for the set_memory_decrypted()
485 * success case. In the failure case, the encryption state of the
486 * memory is unknown. Leave "decrypted" as true to ensure the
487 * memory will be leaked instead of going back on the free list.
488 */
489 gpadl->decrypted = true;
490 ret = set_memory_decrypted((unsigned long)kbuffer,
491 PFN_UP(size));
492 if (ret) {
493 dev_warn(&channel->device_obj->device,
494 "Failed to set host visibility for new GPADL %d.\n",
495 ret);
496 return ret;
497 }
498
499 init_completion(&msginfo->waitevent);
500 msginfo->waiting_channel = channel;
501
502 gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
503 gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
504 gpadlmsg->child_relid = channel->offermsg.child_relid;
505 gpadlmsg->gpadl = next_gpadl_handle;
506
507
508 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
509 list_add_tail(&msginfo->msglistentry,
510 &vmbus_connection.chn_msg_list);
511
512 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
513
514 if (channel->rescind) {
515 ret = -ENODEV;
516 goto cleanup;
517 }
518
519 ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
520 sizeof(*msginfo), true);
521
522 trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
523
524 if (ret != 0)
525 goto cleanup;
526
527 list_for_each(curr, &msginfo->submsglist) {
528 submsginfo = (struct vmbus_channel_msginfo *)curr;
529 gpadl_body =
530 (struct vmbus_channel_gpadl_body *)submsginfo->msg;
531
532 gpadl_body->header.msgtype =
533 CHANNELMSG_GPADL_BODY;
534 gpadl_body->gpadl = next_gpadl_handle;
535
536 ret = vmbus_post_msg(gpadl_body,
537 submsginfo->msgsize - sizeof(*submsginfo),
538 true);
539
540 trace_vmbus_establish_gpadl_body(gpadl_body, ret);
541
542 if (ret != 0)
543 goto cleanup;
544
545 }
546 wait_for_completion(&msginfo->waitevent);
547
548 if (msginfo->response.gpadl_created.creation_status != 0) {
549 pr_err("Failed to establish GPADL: err = 0x%x\n",
550 msginfo->response.gpadl_created.creation_status);
551
552 ret = -EDQUOT;
553 goto cleanup;
554 }
555
556 if (channel->rescind) {
557 ret = -ENODEV;
558 goto cleanup;
559 }
560
561 /* At this point, we received the gpadl created msg */
562 gpadl->gpadl_handle = gpadlmsg->gpadl;
563 gpadl->buffer = kbuffer;
564 gpadl->size = size;
565
566
567 cleanup:
568 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
569 list_del(&msginfo->msglistentry);
570 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
571 list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
572 msglistentry) {
573 kfree(submsginfo);
574 }
575
576 kfree(msginfo);
577
578 if (ret) {
579 /*
580 * If set_memory_encrypted() fails, the decrypted flag is
581 * left as true so the memory is leaked instead of being
582 * put back on the free list.
583 */
584 if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)))
585 gpadl->decrypted = false;
586 }
587
588 return ret;
589 }
590
591 /*
592 * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
593 *
594 * @channel: a channel
595 * @kbuffer: from kmalloc or vmalloc
596 * @size: page-size multiple
597 * @gpadl_handle: some funky thing
598 */
vmbus_establish_gpadl(struct vmbus_channel * channel,void * kbuffer,u32 size,struct vmbus_gpadl * gpadl)599 int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
600 u32 size, struct vmbus_gpadl *gpadl)
601 {
602 return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
603 0U, gpadl);
604 }
605 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
606
607 /**
608 * request_arr_init - Allocates memory for the requestor array. Each slot
609 * keeps track of the next available slot in the array. Initially, each
610 * slot points to the next one (as in a Linked List). The last slot
611 * does not point to anything, so its value is U64_MAX by default.
612 * @size The size of the array
613 */
request_arr_init(u32 size)614 static u64 *request_arr_init(u32 size)
615 {
616 int i;
617 u64 *req_arr;
618
619 req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
620 if (!req_arr)
621 return NULL;
622
623 for (i = 0; i < size - 1; i++)
624 req_arr[i] = i + 1;
625
626 /* Last slot (no more available slots) */
627 req_arr[i] = U64_MAX;
628
629 return req_arr;
630 }
631
632 /*
633 * vmbus_alloc_requestor - Initializes @rqstor's fields.
634 * Index 0 is the first free slot
635 * @size: Size of the requestor array
636 */
vmbus_alloc_requestor(struct vmbus_requestor * rqstor,u32 size)637 static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
638 {
639 u64 *rqst_arr;
640 unsigned long *bitmap;
641
642 rqst_arr = request_arr_init(size);
643 if (!rqst_arr)
644 return -ENOMEM;
645
646 bitmap = bitmap_zalloc(size, GFP_KERNEL);
647 if (!bitmap) {
648 kfree(rqst_arr);
649 return -ENOMEM;
650 }
651
652 rqstor->req_arr = rqst_arr;
653 rqstor->req_bitmap = bitmap;
654 rqstor->size = size;
655 rqstor->next_request_id = 0;
656 spin_lock_init(&rqstor->req_lock);
657
658 return 0;
659 }
660
661 /*
662 * vmbus_free_requestor - Frees memory allocated for @rqstor
663 * @rqstor: Pointer to the requestor struct
664 */
vmbus_free_requestor(struct vmbus_requestor * rqstor)665 static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
666 {
667 kfree(rqstor->req_arr);
668 bitmap_free(rqstor->req_bitmap);
669 }
670
__vmbus_open(struct vmbus_channel * newchannel,void * userdata,u32 userdatalen,void (* onchannelcallback)(void * context),void * context)671 static int __vmbus_open(struct vmbus_channel *newchannel,
672 void *userdata, u32 userdatalen,
673 void (*onchannelcallback)(void *context), void *context)
674 {
675 struct vmbus_channel_open_channel *open_msg;
676 struct vmbus_channel_msginfo *open_info = NULL;
677 struct page *page = newchannel->ringbuffer_page;
678 u32 send_pages, recv_pages;
679 unsigned long flags;
680 int err;
681
682 if (userdatalen > MAX_USER_DEFINED_BYTES)
683 return -EINVAL;
684
685 send_pages = newchannel->ringbuffer_send_offset;
686 recv_pages = newchannel->ringbuffer_pagecount - send_pages;
687
688 if (newchannel->state != CHANNEL_OPEN_STATE)
689 return -EINVAL;
690
691 /* Create and init requestor */
692 if (newchannel->rqstor_size) {
693 if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size))
694 return -ENOMEM;
695 }
696
697 newchannel->state = CHANNEL_OPENING_STATE;
698 newchannel->onchannel_callback = onchannelcallback;
699 newchannel->channel_callback_context = context;
700
701 if (!newchannel->max_pkt_size)
702 newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
703
704 /* Establish the gpadl for the ring buffer */
705 newchannel->ringbuffer_gpadlhandle.gpadl_handle = 0;
706
707 err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
708 page_address(newchannel->ringbuffer_page),
709 (send_pages + recv_pages) << PAGE_SHIFT,
710 newchannel->ringbuffer_send_offset << PAGE_SHIFT,
711 &newchannel->ringbuffer_gpadlhandle);
712 if (err)
713 goto error_clean_ring;
714
715 err = hv_ringbuffer_init(&newchannel->outbound,
716 page, send_pages, 0);
717 if (err)
718 goto error_free_gpadl;
719
720 err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
721 recv_pages, newchannel->max_pkt_size);
722 if (err)
723 goto error_free_gpadl;
724
725 /* Create and init the channel open message */
726 open_info = kzalloc(sizeof(*open_info) +
727 sizeof(struct vmbus_channel_open_channel),
728 GFP_KERNEL);
729 if (!open_info) {
730 err = -ENOMEM;
731 goto error_free_gpadl;
732 }
733
734 init_completion(&open_info->waitevent);
735 open_info->waiting_channel = newchannel;
736
737 open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
738 open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
739 open_msg->openid = newchannel->offermsg.child_relid;
740 open_msg->child_relid = newchannel->offermsg.child_relid;
741 open_msg->ringbuffer_gpadlhandle
742 = newchannel->ringbuffer_gpadlhandle.gpadl_handle;
743 /*
744 * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
745 * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
746 * here we calculate it into HV_HYP_PAGE.
747 */
748 open_msg->downstream_ringbuffer_pageoffset =
749 hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT);
750 open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu);
751
752 if (userdatalen)
753 memcpy(open_msg->userdata, userdata, userdatalen);
754
755 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
756 list_add_tail(&open_info->msglistentry,
757 &vmbus_connection.chn_msg_list);
758 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
759
760 if (newchannel->rescind) {
761 err = -ENODEV;
762 goto error_clean_msglist;
763 }
764
765 err = vmbus_post_msg(open_msg,
766 sizeof(struct vmbus_channel_open_channel), true);
767
768 trace_vmbus_open(open_msg, err);
769
770 if (err != 0)
771 goto error_clean_msglist;
772
773 wait_for_completion(&open_info->waitevent);
774
775 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
776 list_del(&open_info->msglistentry);
777 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
778
779 if (newchannel->rescind) {
780 err = -ENODEV;
781 goto error_free_info;
782 }
783
784 if (open_info->response.open_result.status) {
785 err = -EAGAIN;
786 goto error_free_info;
787 }
788
789 newchannel->state = CHANNEL_OPENED_STATE;
790 kfree(open_info);
791 return 0;
792
793 error_clean_msglist:
794 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
795 list_del(&open_info->msglistentry);
796 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
797 error_free_info:
798 kfree(open_info);
799 error_free_gpadl:
800 vmbus_teardown_gpadl(newchannel, &newchannel->ringbuffer_gpadlhandle);
801 error_clean_ring:
802 hv_ringbuffer_cleanup(&newchannel->outbound);
803 hv_ringbuffer_cleanup(&newchannel->inbound);
804 vmbus_free_requestor(&newchannel->requestor);
805 newchannel->state = CHANNEL_OPEN_STATE;
806 return err;
807 }
808
809 /*
810 * vmbus_connect_ring - Open the channel but reuse ring buffer
811 */
vmbus_connect_ring(struct vmbus_channel * newchannel,void (* onchannelcallback)(void * context),void * context)812 int vmbus_connect_ring(struct vmbus_channel *newchannel,
813 void (*onchannelcallback)(void *context), void *context)
814 {
815 return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
816 }
817 EXPORT_SYMBOL_GPL(vmbus_connect_ring);
818
819 /*
820 * vmbus_open - Open the specified channel.
821 */
vmbus_open(struct vmbus_channel * newchannel,u32 send_ringbuffer_size,u32 recv_ringbuffer_size,void * userdata,u32 userdatalen,void (* onchannelcallback)(void * context),void * context)822 int vmbus_open(struct vmbus_channel *newchannel,
823 u32 send_ringbuffer_size, u32 recv_ringbuffer_size,
824 void *userdata, u32 userdatalen,
825 void (*onchannelcallback)(void *context), void *context)
826 {
827 int err;
828
829 err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
830 recv_ringbuffer_size);
831 if (err)
832 return err;
833
834 err = __vmbus_open(newchannel, userdata, userdatalen,
835 onchannelcallback, context);
836 if (err)
837 vmbus_free_ring(newchannel);
838
839 return err;
840 }
841 EXPORT_SYMBOL_GPL(vmbus_open);
842
843 /*
844 * vmbus_teardown_gpadl -Teardown the specified GPADL handle
845 */
vmbus_teardown_gpadl(struct vmbus_channel * channel,struct vmbus_gpadl * gpadl)846 int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpadl)
847 {
848 struct vmbus_channel_gpadl_teardown *msg;
849 struct vmbus_channel_msginfo *info;
850 unsigned long flags;
851 int ret;
852
853 info = kzalloc(sizeof(*info) +
854 sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
855 if (!info)
856 return -ENOMEM;
857
858 init_completion(&info->waitevent);
859 info->waiting_channel = channel;
860
861 msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
862
863 msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
864 msg->child_relid = channel->offermsg.child_relid;
865 msg->gpadl = gpadl->gpadl_handle;
866
867 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
868 list_add_tail(&info->msglistentry,
869 &vmbus_connection.chn_msg_list);
870 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
871
872 if (channel->rescind)
873 goto post_msg_err;
874
875 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
876 true);
877
878 trace_vmbus_teardown_gpadl(msg, ret);
879
880 if (ret)
881 goto post_msg_err;
882
883 wait_for_completion(&info->waitevent);
884
885 gpadl->gpadl_handle = 0;
886
887 post_msg_err:
888 /*
889 * If the channel has been rescinded;
890 * we will be awakened by the rescind
891 * handler; set the error code to zero so we don't leak memory.
892 */
893 if (channel->rescind)
894 ret = 0;
895
896 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
897 list_del(&info->msglistentry);
898 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
899
900 kfree(info);
901
902 ret = set_memory_encrypted((unsigned long)gpadl->buffer,
903 PFN_UP(gpadl->size));
904 if (ret)
905 pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret);
906
907 gpadl->decrypted = ret;
908
909 return ret;
910 }
911 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
912
vmbus_reset_channel_cb(struct vmbus_channel * channel)913 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
914 {
915 unsigned long flags;
916
917 /*
918 * vmbus_on_event(), running in the per-channel tasklet, can race
919 * with vmbus_close_internal() in the case of SMP guest, e.g., when
920 * the former is accessing channel->inbound.ring_buffer, the latter
921 * could be freeing the ring_buffer pages, so here we must stop it
922 * first.
923 *
924 * vmbus_chan_sched() might call the netvsc driver callback function
925 * that ends up scheduling NAPI work that accesses the ring buffer.
926 * At this point, we have to ensure that any such work is completed
927 * and that the channel ring buffer is no longer being accessed, cf.
928 * the calls to napi_disable() in netvsc_device_remove().
929 */
930 tasklet_disable(&channel->callback_event);
931
932 /* See the inline comments in vmbus_chan_sched(). */
933 spin_lock_irqsave(&channel->sched_lock, flags);
934 channel->onchannel_callback = NULL;
935 spin_unlock_irqrestore(&channel->sched_lock, flags);
936
937 channel->sc_creation_callback = NULL;
938
939 /* Re-enable tasklet for use on re-open */
940 tasklet_enable(&channel->callback_event);
941 }
942
vmbus_close_internal(struct vmbus_channel * channel)943 static int vmbus_close_internal(struct vmbus_channel *channel)
944 {
945 struct vmbus_channel_close_channel *msg;
946 int ret;
947
948 vmbus_reset_channel_cb(channel);
949
950 /*
951 * In case a device driver's probe() fails (e.g.,
952 * util_probe() -> vmbus_open() returns -ENOMEM) and the device is
953 * rescinded later (e.g., we dynamically disable an Integrated Service
954 * in Hyper-V Manager), the driver's remove() invokes vmbus_close():
955 * here we should skip most of the below cleanup work.
956 */
957 if (channel->state != CHANNEL_OPENED_STATE)
958 return -EINVAL;
959
960 channel->state = CHANNEL_OPEN_STATE;
961
962 /* Send a closing message */
963
964 msg = &channel->close_msg.msg;
965
966 msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
967 msg->child_relid = channel->offermsg.child_relid;
968
969 ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
970 true);
971
972 trace_vmbus_close_internal(msg, ret);
973
974 if (ret) {
975 pr_err("Close failed: close post msg return is %d\n", ret);
976 /*
977 * If we failed to post the close msg,
978 * it is perhaps better to leak memory.
979 */
980 }
981
982 /* Tear down the gpadl for the channel's ring buffer */
983 else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
984 ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer_gpadlhandle);
985 if (ret) {
986 pr_err("Close failed: teardown gpadl return %d\n", ret);
987 /*
988 * If we failed to teardown gpadl,
989 * it is perhaps better to leak memory.
990 */
991 }
992 }
993
994 if (!ret)
995 vmbus_free_requestor(&channel->requestor);
996
997 return ret;
998 }
999
1000 /* disconnect ring - close all channels */
vmbus_disconnect_ring(struct vmbus_channel * channel)1001 int vmbus_disconnect_ring(struct vmbus_channel *channel)
1002 {
1003 struct vmbus_channel *cur_channel, *tmp;
1004 int ret;
1005
1006 if (channel->primary_channel != NULL)
1007 return -EINVAL;
1008
1009 list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
1010 if (cur_channel->rescind)
1011 wait_for_completion(&cur_channel->rescind_event);
1012
1013 mutex_lock(&vmbus_connection.channel_mutex);
1014 if (vmbus_close_internal(cur_channel) == 0) {
1015 vmbus_free_ring(cur_channel);
1016
1017 if (cur_channel->rescind)
1018 hv_process_channel_removal(cur_channel);
1019 }
1020 mutex_unlock(&vmbus_connection.channel_mutex);
1021 }
1022
1023 /*
1024 * Now close the primary.
1025 */
1026 mutex_lock(&vmbus_connection.channel_mutex);
1027 ret = vmbus_close_internal(channel);
1028 mutex_unlock(&vmbus_connection.channel_mutex);
1029
1030 return ret;
1031 }
1032 EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
1033
1034 /*
1035 * vmbus_close - Close the specified channel
1036 */
vmbus_close(struct vmbus_channel * channel)1037 void vmbus_close(struct vmbus_channel *channel)
1038 {
1039 if (vmbus_disconnect_ring(channel) == 0)
1040 vmbus_free_ring(channel);
1041 }
1042 EXPORT_SYMBOL_GPL(vmbus_close);
1043
1044 /**
1045 * vmbus_sendpacket_getid() - Send the specified buffer on the given channel
1046 * @channel: Pointer to vmbus_channel structure
1047 * @buffer: Pointer to the buffer you want to send the data from.
1048 * @bufferlen: Maximum size of what the buffer holds.
1049 * @requestid: Identifier of the request
1050 * @trans_id: Identifier of the transaction associated to this request, if
1051 * the send is successful; undefined, otherwise.
1052 * @type: Type of packet that is being sent e.g. negotiate, time
1053 * packet etc.
1054 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
1055 *
1056 * Sends data in @buffer directly to Hyper-V via the vmbus.
1057 * This will send the data unparsed to Hyper-V.
1058 *
1059 * Mainly used by Hyper-V drivers.
1060 */
vmbus_sendpacket_getid(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u64 requestid,u64 * trans_id,enum vmbus_packet_type type,u32 flags)1061 int vmbus_sendpacket_getid(struct vmbus_channel *channel, void *buffer,
1062 u32 bufferlen, u64 requestid, u64 *trans_id,
1063 enum vmbus_packet_type type, u32 flags)
1064 {
1065 struct vmpacket_descriptor desc;
1066 u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
1067 u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1068 struct kvec bufferlist[3];
1069 u64 aligned_data = 0;
1070 int num_vecs = ((bufferlen != 0) ? 3 : 1);
1071
1072
1073 /* Setup the descriptor */
1074 desc.type = type; /* VmbusPacketTypeDataInBand; */
1075 desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
1076 /* in 8-bytes granularity */
1077 desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
1078 desc.len8 = (u16)(packetlen_aligned >> 3);
1079 desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
1080
1081 bufferlist[0].iov_base = &desc;
1082 bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
1083 bufferlist[1].iov_base = buffer;
1084 bufferlist[1].iov_len = bufferlen;
1085 bufferlist[2].iov_base = &aligned_data;
1086 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1087
1088 return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid, trans_id);
1089 }
1090 EXPORT_SYMBOL(vmbus_sendpacket_getid);
1091
1092 /**
1093 * vmbus_sendpacket() - Send the specified buffer on the given channel
1094 * @channel: Pointer to vmbus_channel structure
1095 * @buffer: Pointer to the buffer you want to send the data from.
1096 * @bufferlen: Maximum size of what the buffer holds.
1097 * @requestid: Identifier of the request
1098 * @type: Type of packet that is being sent e.g. negotiate, time
1099 * packet etc.
1100 * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
1101 *
1102 * Sends data in @buffer directly to Hyper-V via the vmbus.
1103 * This will send the data unparsed to Hyper-V.
1104 *
1105 * Mainly used by Hyper-V drivers.
1106 */
vmbus_sendpacket(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u64 requestid,enum vmbus_packet_type type,u32 flags)1107 int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer,
1108 u32 bufferlen, u64 requestid,
1109 enum vmbus_packet_type type, u32 flags)
1110 {
1111 return vmbus_sendpacket_getid(channel, buffer, bufferlen,
1112 requestid, NULL, type, flags);
1113 }
1114 EXPORT_SYMBOL(vmbus_sendpacket);
1115
1116 /*
1117 * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
1118 * packets using a GPADL Direct packet type. This interface allows you
1119 * to control notifying the host. This will be useful for sending
1120 * batched data. Also the sender can control the send flags
1121 * explicitly.
1122 */
vmbus_sendpacket_pagebuffer(struct vmbus_channel * channel,struct hv_page_buffer pagebuffers[],u32 pagecount,void * buffer,u32 bufferlen,u64 requestid)1123 int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
1124 struct hv_page_buffer pagebuffers[],
1125 u32 pagecount, void *buffer, u32 bufferlen,
1126 u64 requestid)
1127 {
1128 int i;
1129 struct vmbus_channel_packet_page_buffer desc;
1130 u32 descsize;
1131 u32 packetlen;
1132 u32 packetlen_aligned;
1133 struct kvec bufferlist[3];
1134 u64 aligned_data = 0;
1135
1136 if (pagecount > MAX_PAGE_BUFFER_COUNT)
1137 return -EINVAL;
1138
1139 /*
1140 * Adjust the size down since vmbus_channel_packet_page_buffer is the
1141 * largest size we support
1142 */
1143 descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
1144 ((MAX_PAGE_BUFFER_COUNT - pagecount) *
1145 sizeof(struct hv_page_buffer));
1146 packetlen = descsize + bufferlen;
1147 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1148
1149 /* Setup the descriptor */
1150 desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
1151 desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
1152 desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
1153 desc.length8 = (u16)(packetlen_aligned >> 3);
1154 desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
1155 desc.reserved = 0;
1156 desc.rangecount = pagecount;
1157
1158 for (i = 0; i < pagecount; i++) {
1159 desc.range[i].len = pagebuffers[i].len;
1160 desc.range[i].offset = pagebuffers[i].offset;
1161 desc.range[i].pfn = pagebuffers[i].pfn;
1162 }
1163
1164 bufferlist[0].iov_base = &desc;
1165 bufferlist[0].iov_len = descsize;
1166 bufferlist[1].iov_base = buffer;
1167 bufferlist[1].iov_len = bufferlen;
1168 bufferlist[2].iov_base = &aligned_data;
1169 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1170
1171 return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
1172 }
1173 EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
1174
1175 /*
1176 * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet
1177 * using a GPADL Direct packet type.
1178 * The buffer includes the vmbus descriptor.
1179 */
vmbus_sendpacket_mpb_desc(struct vmbus_channel * channel,struct vmbus_packet_mpb_array * desc,u32 desc_size,void * buffer,u32 bufferlen,u64 requestid)1180 int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
1181 struct vmbus_packet_mpb_array *desc,
1182 u32 desc_size,
1183 void *buffer, u32 bufferlen, u64 requestid)
1184 {
1185 u32 packetlen;
1186 u32 packetlen_aligned;
1187 struct kvec bufferlist[3];
1188 u64 aligned_data = 0;
1189
1190 packetlen = desc_size + bufferlen;
1191 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
1192
1193 /* Setup the descriptor */
1194 desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
1195 desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
1196 desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
1197 desc->length8 = (u16)(packetlen_aligned >> 3);
1198 desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
1199 desc->reserved = 0;
1200 desc->rangecount = 1;
1201
1202 bufferlist[0].iov_base = desc;
1203 bufferlist[0].iov_len = desc_size;
1204 bufferlist[1].iov_base = buffer;
1205 bufferlist[1].iov_len = bufferlen;
1206 bufferlist[2].iov_base = &aligned_data;
1207 bufferlist[2].iov_len = (packetlen_aligned - packetlen);
1208
1209 return hv_ringbuffer_write(channel, bufferlist, 3, requestid, NULL);
1210 }
1211 EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
1212
1213 /**
1214 * __vmbus_recvpacket() - Retrieve the user packet on the specified channel
1215 * @channel: Pointer to vmbus_channel structure
1216 * @buffer: Pointer to the buffer you want to receive the data into.
1217 * @bufferlen: Maximum size of what the buffer can hold.
1218 * @buffer_actual_len: The actual size of the data after it was received.
1219 * @requestid: Identifier of the request
1220 * @raw: true means keep the vmpacket_descriptor header in the received data.
1221 *
1222 * Receives directly from the hyper-v vmbus and puts the data it received
1223 * into Buffer. This will receive the data unparsed from hyper-v.
1224 *
1225 * Mainly used by Hyper-V drivers.
1226 */
1227 static inline int
__vmbus_recvpacket(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u32 * buffer_actual_len,u64 * requestid,bool raw)1228 __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1229 u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
1230 bool raw)
1231 {
1232 return hv_ringbuffer_read(channel, buffer, bufferlen,
1233 buffer_actual_len, requestid, raw);
1234
1235 }
1236
vmbus_recvpacket(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u32 * buffer_actual_len,u64 * requestid)1237 int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
1238 u32 bufferlen, u32 *buffer_actual_len,
1239 u64 *requestid)
1240 {
1241 return __vmbus_recvpacket(channel, buffer, bufferlen,
1242 buffer_actual_len, requestid, false);
1243 }
1244 EXPORT_SYMBOL(vmbus_recvpacket);
1245
1246 /*
1247 * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
1248 */
vmbus_recvpacket_raw(struct vmbus_channel * channel,void * buffer,u32 bufferlen,u32 * buffer_actual_len,u64 * requestid)1249 int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
1250 u32 bufferlen, u32 *buffer_actual_len,
1251 u64 *requestid)
1252 {
1253 return __vmbus_recvpacket(channel, buffer, bufferlen,
1254 buffer_actual_len, requestid, true);
1255 }
1256 EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
1257
1258 /*
1259 * vmbus_next_request_id - Returns a new request id. It is also
1260 * the index at which the guest memory address is stored.
1261 * Uses a spin lock to avoid race conditions.
1262 * @channel: Pointer to the VMbus channel struct
1263 * @rqst_add: Guest memory address to be stored in the array
1264 */
vmbus_next_request_id(struct vmbus_channel * channel,u64 rqst_addr)1265 u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
1266 {
1267 struct vmbus_requestor *rqstor = &channel->requestor;
1268 unsigned long flags;
1269 u64 current_id;
1270
1271 /* Check rqstor has been initialized */
1272 if (!channel->rqstor_size)
1273 return VMBUS_NO_RQSTOR;
1274
1275 lock_requestor(channel, flags);
1276 current_id = rqstor->next_request_id;
1277
1278 /* Requestor array is full */
1279 if (current_id >= rqstor->size) {
1280 unlock_requestor(channel, flags);
1281 return VMBUS_RQST_ERROR;
1282 }
1283
1284 rqstor->next_request_id = rqstor->req_arr[current_id];
1285 rqstor->req_arr[current_id] = rqst_addr;
1286
1287 /* The already held spin lock provides atomicity */
1288 bitmap_set(rqstor->req_bitmap, current_id, 1);
1289
1290 unlock_requestor(channel, flags);
1291
1292 /*
1293 * Cannot return an ID of 0, which is reserved for an unsolicited
1294 * message from Hyper-V; Hyper-V does not acknowledge (respond to)
1295 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED requests with ID of
1296 * 0 sent by the guest.
1297 */
1298 return current_id + 1;
1299 }
1300 EXPORT_SYMBOL_GPL(vmbus_next_request_id);
1301
1302 /* As in vmbus_request_addr_match() but without the requestor lock */
__vmbus_request_addr_match(struct vmbus_channel * channel,u64 trans_id,u64 rqst_addr)1303 u64 __vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
1304 u64 rqst_addr)
1305 {
1306 struct vmbus_requestor *rqstor = &channel->requestor;
1307 u64 req_addr;
1308
1309 /* Check rqstor has been initialized */
1310 if (!channel->rqstor_size)
1311 return VMBUS_NO_RQSTOR;
1312
1313 /* Hyper-V can send an unsolicited message with ID of 0 */
1314 if (!trans_id)
1315 return VMBUS_RQST_ERROR;
1316
1317 /* Data corresponding to trans_id is stored at trans_id - 1 */
1318 trans_id--;
1319
1320 /* Invalid trans_id */
1321 if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap))
1322 return VMBUS_RQST_ERROR;
1323
1324 req_addr = rqstor->req_arr[trans_id];
1325 if (rqst_addr == VMBUS_RQST_ADDR_ANY || req_addr == rqst_addr) {
1326 rqstor->req_arr[trans_id] = rqstor->next_request_id;
1327 rqstor->next_request_id = trans_id;
1328
1329 /* The already held spin lock provides atomicity */
1330 bitmap_clear(rqstor->req_bitmap, trans_id, 1);
1331 }
1332
1333 return req_addr;
1334 }
1335 EXPORT_SYMBOL_GPL(__vmbus_request_addr_match);
1336
1337 /*
1338 * vmbus_request_addr_match - Clears/removes @trans_id from the @channel's
1339 * requestor, provided the memory address stored at @trans_id equals @rqst_addr
1340 * (or provided @rqst_addr matches the sentinel value VMBUS_RQST_ADDR_ANY).
1341 *
1342 * Returns the memory address stored at @trans_id, or VMBUS_RQST_ERROR if
1343 * @trans_id is not contained in the requestor.
1344 *
1345 * Acquires and releases the requestor spin lock.
1346 */
vmbus_request_addr_match(struct vmbus_channel * channel,u64 trans_id,u64 rqst_addr)1347 u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id,
1348 u64 rqst_addr)
1349 {
1350 unsigned long flags;
1351 u64 req_addr;
1352
1353 lock_requestor(channel, flags);
1354 req_addr = __vmbus_request_addr_match(channel, trans_id, rqst_addr);
1355 unlock_requestor(channel, flags);
1356
1357 return req_addr;
1358 }
1359 EXPORT_SYMBOL_GPL(vmbus_request_addr_match);
1360
1361 /*
1362 * vmbus_request_addr - Returns the memory address stored at @trans_id
1363 * in @rqstor. Uses a spin lock to avoid race conditions.
1364 * @channel: Pointer to the VMbus channel struct
1365 * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
1366 * next request id.
1367 */
vmbus_request_addr(struct vmbus_channel * channel,u64 trans_id)1368 u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
1369 {
1370 return vmbus_request_addr_match(channel, trans_id, VMBUS_RQST_ADDR_ANY);
1371 }
1372 EXPORT_SYMBOL_GPL(vmbus_request_addr);
1373