]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hyperv/vmbus/hv_channel.c
Merge libucl 20140718 (fixes a bug in the parser)
[FreeBSD/FreeBSD.git] / sys / dev / hyperv / vmbus / hv_channel.c
1 /*-
2  * Copyright (c) 2009-2012 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
35 #include <sys/mbuf.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <machine/bus.h>
39 #include <vm/vm.h>
40 #include <vm/vm_param.h>
41 #include <vm/pmap.h>
42
43 #include "hv_vmbus_priv.h"
44
45 static int      vmbus_channel_create_gpadl_header(
46                         /* must be phys and virt contiguous*/
47                         void*                           contig_buffer,
48                         /* page-size multiple */
49                         uint32_t                        size,
50                         hv_vmbus_channel_msg_info**     msg_info,
51                         uint32_t*                       message_count);
52
53 static void     vmbus_channel_set_event(hv_vmbus_channel* channel);
54
55 /**
56  *  @brief Trigger an event notification on the specified channel
57  */
58 static void
59 vmbus_channel_set_event(hv_vmbus_channel *channel)
60 {
61         hv_vmbus_monitor_page *monitor_page;
62
63         if (channel->offer_msg.monitor_allocated) {
64                 /* Each uint32_t represents 32 channels */
65                 synch_set_bit((channel->offer_msg.child_rel_id & 31),
66                         ((uint32_t *)hv_vmbus_g_connection.send_interrupt_page
67                                 + ((channel->offer_msg.child_rel_id >> 5))));
68
69                 monitor_page = (hv_vmbus_monitor_page *)
70                         hv_vmbus_g_connection.monitor_pages;
71
72                 monitor_page++; /* Get the child to parent monitor page */
73
74                 synch_set_bit(channel->monitor_bit,
75                         (uint32_t *)&monitor_page->
76                                 trigger_group[channel->monitor_group].u.pending);
77         } else {
78                 hv_vmbus_set_event(channel->offer_msg.child_rel_id);
79         }
80
81 }
82
83 /**
84  * @brief Open the specified channel
85  */
86 int
87 hv_vmbus_channel_open(
88         hv_vmbus_channel*               new_channel,
89         uint32_t                        send_ring_buffer_size,
90         uint32_t                        recv_ring_buffer_size,
91         void*                           user_data,
92         uint32_t                        user_data_len,
93         hv_vmbus_pfn_channel_callback   pfn_on_channel_callback,
94         void*                           context)
95 {
96
97         int ret = 0;
98         void *in, *out;
99         hv_vmbus_channel_open_channel*  open_msg;
100         hv_vmbus_channel_msg_info*      open_info;
101
102         new_channel->on_channel_callback = pfn_on_channel_callback;
103         new_channel->channel_callback_context = context;
104
105         /* Allocate the ring buffer */
106         out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
107             M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
108         KASSERT(out != NULL,
109             ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
110         if (out == NULL)
111                 return (ENOMEM);
112
113         in = ((uint8_t *) out + send_ring_buffer_size);
114
115         new_channel->ring_buffer_pages = out;
116         new_channel->ring_buffer_page_count = (send_ring_buffer_size +
117             recv_ring_buffer_size) >> PAGE_SHIFT;
118         new_channel->ring_buffer_size = send_ring_buffer_size +
119             recv_ring_buffer_size;
120
121         hv_vmbus_ring_buffer_init(
122                 &new_channel->outbound,
123                 out,
124                 send_ring_buffer_size);
125
126         hv_vmbus_ring_buffer_init(
127                 &new_channel->inbound,
128                 in,
129                 recv_ring_buffer_size);
130
131         /**
132          * Establish the gpadl for the ring buffer
133          */
134         new_channel->ring_buffer_gpadl_handle = 0;
135
136         ret = hv_vmbus_channel_establish_gpadl(new_channel,
137                 new_channel->outbound.ring_buffer,
138                 send_ring_buffer_size + recv_ring_buffer_size,
139                 &new_channel->ring_buffer_gpadl_handle);
140
141         /**
142          * Create and init the channel open message
143          */
144         open_info = (hv_vmbus_channel_msg_info*) malloc(
145                 sizeof(hv_vmbus_channel_msg_info) +
146                         sizeof(hv_vmbus_channel_open_channel),
147                 M_DEVBUF,
148                 M_NOWAIT);
149         KASSERT(open_info != NULL,
150             ("Error VMBUS: malloc failed to allocate Open Channel message!"));
151
152         if (open_info == NULL)
153                 return (ENOMEM);
154
155         sema_init(&open_info->wait_sema, 0, "Open Info Sema");
156
157         open_msg = (hv_vmbus_channel_open_channel*) open_info->msg;
158         open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL;
159         open_msg->open_id = new_channel->offer_msg.child_rel_id;
160         open_msg->child_rel_id = new_channel->offer_msg.child_rel_id;
161         open_msg->ring_buffer_gpadl_handle =
162                 new_channel->ring_buffer_gpadl_handle;
163         open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size
164                 >> PAGE_SHIFT;
165         open_msg->server_context_area_gpadl_handle = 0;
166
167         if (user_data_len)
168                 memcpy(open_msg->user_data, user_data, user_data_len);
169
170         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
171         TAILQ_INSERT_TAIL(
172                 &hv_vmbus_g_connection.channel_msg_anchor,
173                 open_info,
174                 msg_list_entry);
175         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
176
177         ret = hv_vmbus_post_message(
178                 open_msg, sizeof(hv_vmbus_channel_open_channel));
179
180         if (ret != 0)
181             goto cleanup;
182
183         ret = sema_timedwait(&open_info->wait_sema, 500); /* KYS 5 seconds */
184
185         if (ret)
186             goto cleanup;
187
188         if (open_info->response.open_result.status == 0) {
189             if(bootverbose)
190                 printf("VMBUS: channel <%p> open success.\n", new_channel);
191         } else {
192             if(bootverbose)
193                 printf("Error VMBUS: channel <%p> open failed - %d!\n",
194                         new_channel, open_info->response.open_result.status);
195         }
196
197         cleanup:
198         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
199         TAILQ_REMOVE(
200                 &hv_vmbus_g_connection.channel_msg_anchor,
201                 open_info,
202                 msg_list_entry);
203         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
204         sema_destroy(&open_info->wait_sema);
205         free(open_info, M_DEVBUF);
206
207         return (ret);
208 }
209
210 /**
211  * @brief Create a gpadl for the specified buffer
212  */
213 static int
214 vmbus_channel_create_gpadl_header(
215         void*                           contig_buffer,
216         uint32_t                        size,   /* page-size multiple */
217         hv_vmbus_channel_msg_info**     msg_info,
218         uint32_t*                       message_count)
219 {
220         int                             i;
221         int                             page_count;
222         unsigned long long              pfn;
223         uint32_t                        msg_size;
224         hv_vmbus_channel_gpadl_header*  gpa_header;
225         hv_vmbus_channel_gpadl_body*    gpadl_body;
226         hv_vmbus_channel_msg_info*      msg_header;
227         hv_vmbus_channel_msg_info*      msg_body;
228
229         int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
230
231         page_count = size >> PAGE_SHIFT;
232         pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT;
233
234         /*do we need a gpadl body msg */
235         pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
236             - sizeof(hv_vmbus_channel_gpadl_header)
237             - sizeof(hv_gpa_range);
238         pfnCount = pfnSize / sizeof(uint64_t);
239
240         if (page_count > pfnCount) { /* if(we need a gpadl body)        */
241             /* fill in the header               */
242             msg_size = sizeof(hv_vmbus_channel_msg_info)
243                 + sizeof(hv_vmbus_channel_gpadl_header)
244                 + sizeof(hv_gpa_range)
245                 + pfnCount * sizeof(uint64_t);
246             msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
247             KASSERT(
248                 msg_header != NULL,
249                 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
250             if (msg_header == NULL)
251                 return (ENOMEM);
252
253             TAILQ_INIT(&msg_header->sub_msg_list_anchor);
254             msg_header->message_size = msg_size;
255
256             gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
257             gpa_header->range_count = 1;
258             gpa_header->range_buf_len = sizeof(hv_gpa_range)
259                 + page_count * sizeof(uint64_t);
260             gpa_header->range[0].byte_offset = 0;
261             gpa_header->range[0].byte_count = size;
262             for (i = 0; i < pfnCount; i++) {
263                 gpa_header->range[0].pfn_array[i] = pfn + i;
264             }
265             *msg_info = msg_header;
266             *message_count = 1;
267
268             pfnSum = pfnCount;
269             pfnLeft = page_count - pfnCount;
270
271             /*
272              *  figure out how many pfns we can fit
273              */
274             pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
275                 - sizeof(hv_vmbus_channel_gpadl_body);
276             pfnCount = pfnSize / sizeof(uint64_t);
277
278             /*
279              * fill in the body
280              */
281             while (pfnLeft) {
282                 if (pfnLeft > pfnCount) {
283                     pfnCurr = pfnCount;
284                 } else {
285                     pfnCurr = pfnLeft;
286                 }
287
288                 msg_size = sizeof(hv_vmbus_channel_msg_info) +
289                     sizeof(hv_vmbus_channel_gpadl_body) +
290                     pfnCurr * sizeof(uint64_t);
291                 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
292                 KASSERT(
293                     msg_body != NULL,
294                     ("Error VMBUS: malloc failed to allocate Gpadl msg_body!"));
295                 if (msg_body == NULL)
296                     return (ENOMEM);
297
298                 msg_body->message_size = msg_size;
299                 (*message_count)++;
300                 gpadl_body =
301                     (hv_vmbus_channel_gpadl_body*) msg_body->msg;
302                 /*
303                  * gpadl_body->gpadl = kbuffer;
304                  */
305                 for (i = 0; i < pfnCurr; i++) {
306                     gpadl_body->pfn[i] = pfn + pfnSum + i;
307                 }
308
309                 TAILQ_INSERT_TAIL(
310                     &msg_header->sub_msg_list_anchor,
311                     msg_body,
312                     msg_list_entry);
313                 pfnSum += pfnCurr;
314                 pfnLeft -= pfnCurr;
315             }
316         } else { /* else everything fits in a header */
317
318             msg_size = sizeof(hv_vmbus_channel_msg_info) +
319                 sizeof(hv_vmbus_channel_gpadl_header) +
320                 sizeof(hv_gpa_range) +
321                 page_count * sizeof(uint64_t);
322             msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
323             KASSERT(
324                 msg_header != NULL,
325                 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
326             if (msg_header == NULL)
327                 return (ENOMEM);
328
329             msg_header->message_size = msg_size;
330
331             gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
332             gpa_header->range_count = 1;
333             gpa_header->range_buf_len = sizeof(hv_gpa_range) +
334                 page_count * sizeof(uint64_t);
335             gpa_header->range[0].byte_offset = 0;
336             gpa_header->range[0].byte_count = size;
337             for (i = 0; i < page_count; i++) {
338                 gpa_header->range[0].pfn_array[i] = pfn + i;
339             }
340
341             *msg_info = msg_header;
342             *message_count = 1;
343         }
344
345         return (0);
346 }
347
348 /**
349  * @brief Establish a GPADL for the specified buffer
350  */
351 int
352 hv_vmbus_channel_establish_gpadl(
353         hv_vmbus_channel*       channel,
354         void*                   contig_buffer,
355         uint32_t                size, /* page-size multiple */
356         uint32_t*               gpadl_handle)
357
358 {
359         int ret = 0;
360         hv_vmbus_channel_gpadl_header*  gpadl_msg;
361         hv_vmbus_channel_gpadl_body*    gpadl_body;
362         hv_vmbus_channel_msg_info*      msg_info;
363         hv_vmbus_channel_msg_info*      sub_msg_info;
364         uint32_t                        msg_count;
365         hv_vmbus_channel_msg_info*      curr;
366         uint32_t                        next_gpadl_handle;
367
368         next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle;
369         atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1);
370
371         ret = vmbus_channel_create_gpadl_header(
372                 contig_buffer, size, &msg_info, &msg_count);
373
374         if(ret != 0) { /* if(allocation failed) return immediately */
375             /* reverse atomic_add_int above */
376             atomic_subtract_int((int*)
377                     &hv_vmbus_g_connection.next_gpadl_handle, 1);
378             return ret;
379         }
380
381         sema_init(&msg_info->wait_sema, 0, "Open Info Sema");
382         gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg;
383         gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER;
384         gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id;
385         gpadl_msg->gpadl = next_gpadl_handle;
386
387         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
388         TAILQ_INSERT_TAIL(
389                 &hv_vmbus_g_connection.channel_msg_anchor,
390                 msg_info,
391                 msg_list_entry);
392
393         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
394
395         ret = hv_vmbus_post_message(
396                 gpadl_msg,
397                 msg_info->message_size -
398                     (uint32_t) sizeof(hv_vmbus_channel_msg_info));
399
400         if (ret != 0)
401             goto cleanup;
402
403         if (msg_count > 1) {
404             TAILQ_FOREACH(curr,
405                     &msg_info->sub_msg_list_anchor, msg_list_entry) {
406                 sub_msg_info = curr;
407                 gpadl_body =
408                     (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg;
409
410                 gpadl_body->header.message_type =
411                     HV_CHANNEL_MESSAGE_GPADL_BODY;
412                 gpadl_body->gpadl = next_gpadl_handle;
413
414                 ret = hv_vmbus_post_message(
415                         gpadl_body,
416                         sub_msg_info->message_size
417                             - (uint32_t) sizeof(hv_vmbus_channel_msg_info));
418                  /* if (the post message failed) give up and clean up */
419                 if(ret != 0)
420                     goto cleanup;
421             }
422         }
423
424         ret = sema_timedwait(&msg_info->wait_sema, 500); /* KYS 5 seconds*/
425         if (ret != 0)
426             goto cleanup;
427
428         *gpadl_handle = gpadl_msg->gpadl;
429
430 cleanup:
431
432         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
433         TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
434                 msg_info, msg_list_entry);
435         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
436
437         sema_destroy(&msg_info->wait_sema);
438         free(msg_info, M_DEVBUF);
439
440         return (ret);
441 }
442
443 /**
444  * @brief Teardown the specified GPADL handle
445  */
446 int
447 hv_vmbus_channel_teardown_gpdal(
448         hv_vmbus_channel*       channel,
449         uint32_t                gpadl_handle)
450 {
451         int                                     ret = 0;
452         hv_vmbus_channel_gpadl_teardown*        msg;
453         hv_vmbus_channel_msg_info*              info;
454
455         info = (hv_vmbus_channel_msg_info *)
456                 malloc( sizeof(hv_vmbus_channel_msg_info) +
457                         sizeof(hv_vmbus_channel_gpadl_teardown),
458                                 M_DEVBUF, M_NOWAIT);
459         KASSERT(info != NULL,
460             ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!"));
461         if (info == NULL) {
462             ret = ENOMEM;
463             goto cleanup;
464         }
465
466         sema_init(&info->wait_sema, 0, "Open Info Sema");
467
468         msg = (hv_vmbus_channel_gpadl_teardown*) info->msg;
469
470         msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN;
471         msg->child_rel_id = channel->offer_msg.child_rel_id;
472         msg->gpadl = gpadl_handle;
473
474         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
475         TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor,
476                         info, msg_list_entry);
477         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
478
479         ret = hv_vmbus_post_message(msg,
480                         sizeof(hv_vmbus_channel_gpadl_teardown));
481         if (ret != 0) 
482             goto cleanup;
483         
484         ret = sema_timedwait(&info->wait_sema, 500); /* KYS 5 seconds */
485
486 cleanup:
487         /*
488          * Received a torndown response
489          */
490         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
491         TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
492                         info, msg_list_entry);
493         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
494         sema_destroy(&info->wait_sema);
495         free(info, M_DEVBUF);
496
497         return (ret);
498 }
499
500 /**
501  * @brief Close the specified channel
502  */
503 void
504 hv_vmbus_channel_close(hv_vmbus_channel *channel)
505 {
506         int ret = 0;
507         hv_vmbus_channel_close_channel* msg;
508         hv_vmbus_channel_msg_info* info;
509
510         mtx_lock(&channel->inbound_lock);
511         channel->on_channel_callback = NULL;
512         mtx_unlock(&channel->inbound_lock);
513
514         /**
515          * Send a closing message
516          */
517         info = (hv_vmbus_channel_msg_info *)
518                 malloc( sizeof(hv_vmbus_channel_msg_info) +
519                         sizeof(hv_vmbus_channel_close_channel),
520                                 M_DEVBUF, M_NOWAIT);
521         KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!"));
522         if(info == NULL)
523             return;
524
525         msg = (hv_vmbus_channel_close_channel*) info->msg;
526         msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL;
527         msg->child_rel_id = channel->offer_msg.child_rel_id;
528
529         ret = hv_vmbus_post_message(
530                 msg, sizeof(hv_vmbus_channel_close_channel));
531
532         /* Tear down the gpadl for the channel's ring buffer */
533         if (channel->ring_buffer_gpadl_handle) {
534                 hv_vmbus_channel_teardown_gpdal(channel,
535                         channel->ring_buffer_gpadl_handle);
536         }
537
538         /* TODO: Send a msg to release the childRelId */
539
540         /* cleanup the ring buffers for this channel */
541         hv_ring_buffer_cleanup(&channel->outbound);
542         hv_ring_buffer_cleanup(&channel->inbound);
543
544         contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
545             M_DEVBUF);
546
547         free(info, M_DEVBUF);
548
549         /*
550          *  If we are closing the channel during an error path in
551          *  opening the channel, don't free the channel
552          *  since the caller will free the channel
553          */
554         if (channel->state == HV_CHANNEL_OPEN_STATE) {
555                 mtx_lock_spin(&hv_vmbus_g_connection.channel_lock);
556                 TAILQ_REMOVE(
557                         &hv_vmbus_g_connection.channel_anchor,
558                         channel,
559                         list_entry);
560                 mtx_unlock_spin(&hv_vmbus_g_connection.channel_lock);
561
562                 hv_vmbus_free_vmbus_channel(channel);
563         }
564
565 }
566
567 /**
568  * @brief Send the specified buffer on the given channel
569  */
570 int
571 hv_vmbus_channel_send_packet(
572         hv_vmbus_channel*       channel,
573         void*                   buffer,
574         uint32_t                buffer_len,
575         uint64_t                request_id,
576         hv_vmbus_packet_type    type,
577         uint32_t                flags)
578 {
579         int                     ret = 0;
580         hv_vm_packet_descriptor desc;
581         uint32_t                packet_len;
582         uint64_t                aligned_data;
583         uint32_t                packet_len_aligned;
584         hv_vmbus_sg_buffer_list buffer_list[3];
585
586         packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
587         packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
588         aligned_data = 0;
589
590         /* Setup the descriptor */
591         desc.type = type;   /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND;             */
592         desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
593                             /* in 8-bytes granularity */
594         desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
595         desc.length8 = (uint16_t) (packet_len_aligned >> 3);
596         desc.transaction_id = request_id;
597
598         buffer_list[0].data = &desc;
599         buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
600
601         buffer_list[1].data = buffer;
602         buffer_list[1].length = buffer_len;
603
604         buffer_list[2].data = &aligned_data;
605         buffer_list[2].length = packet_len_aligned - packet_len;
606
607         ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3);
608
609         /* TODO: We should determine if this is optional */
610         if (ret == 0
611                 && !hv_vmbus_get_ring_buffer_interrupt_mask(
612                         &channel->outbound)) {
613                 vmbus_channel_set_event(channel);
614         }
615
616         return (ret);
617 }
618
619 /**
620  * @brief Send a range of single-page buffer packets using
621  * a GPADL Direct packet type
622  */
623 int
624 hv_vmbus_channel_send_packet_pagebuffer(
625         hv_vmbus_channel*       channel,
626         hv_vmbus_page_buffer    page_buffers[],
627         uint32_t                page_count,
628         void*                   buffer,
629         uint32_t                buffer_len,
630         uint64_t                request_id)
631 {
632
633         int                                     ret = 0;
634         int                                     i = 0;
635         uint32_t                                packet_len;
636         uint32_t                                packetLen_aligned;
637         hv_vmbus_sg_buffer_list                 buffer_list[3];
638         hv_vmbus_channel_packet_page_buffer     desc;
639         uint32_t                                descSize;
640         uint64_t                                alignedData = 0;
641
642         if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
643                 return (EINVAL);
644
645         /*
646          * Adjust the size down since hv_vmbus_channel_packet_page_buffer
647          *  is the largest size we support
648          */
649         descSize = sizeof(hv_vmbus_channel_packet_page_buffer) -
650                         ((HV_MAX_PAGE_BUFFER_COUNT - page_count) *
651                         sizeof(hv_vmbus_page_buffer));
652         packet_len = descSize + buffer_len;
653         packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
654
655         /* Setup the descriptor */
656         desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
657         desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
658         desc.data_offset8 = descSize >> 3; /* in 8-bytes granularity */
659         desc.length8 = (uint16_t) (packetLen_aligned >> 3);
660         desc.transaction_id = request_id;
661         desc.range_count = page_count;
662
663         for (i = 0; i < page_count; i++) {
664                 desc.range[i].length = page_buffers[i].length;
665                 desc.range[i].offset = page_buffers[i].offset;
666                 desc.range[i].pfn = page_buffers[i].pfn;
667         }
668
669         buffer_list[0].data = &desc;
670         buffer_list[0].length = descSize;
671
672         buffer_list[1].data = buffer;
673         buffer_list[1].length = buffer_len;
674
675         buffer_list[2].data = &alignedData;
676         buffer_list[2].length = packetLen_aligned - packet_len;
677
678         ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3);
679
680         /* TODO: We should determine if this is optional */
681         if (ret == 0 &&
682                 !hv_vmbus_get_ring_buffer_interrupt_mask(&channel->outbound)) {
683                 vmbus_channel_set_event(channel);
684         }
685
686         return (ret);
687 }
688
689 /**
690  * @brief Send a multi-page buffer packet using a GPADL Direct packet type
691  */
692 int
693 hv_vmbus_channel_send_packet_multipagebuffer(
694         hv_vmbus_channel*               channel,
695         hv_vmbus_multipage_buffer*      multi_page_buffer,
696         void*                           buffer,
697         uint32_t                        buffer_len,
698         uint64_t                        request_id)
699 {
700
701         int                     ret = 0;
702         uint32_t                desc_size;
703         uint32_t                packet_len;
704         uint32_t                packet_len_aligned;
705         uint32_t                pfn_count;
706         uint64_t                aligned_data = 0;
707         hv_vmbus_sg_buffer_list buffer_list[3];
708         hv_vmbus_channel_packet_multipage_buffer desc;
709
710         pfn_count =
711             HV_NUM_PAGES_SPANNED(
712                     multi_page_buffer->offset,
713                     multi_page_buffer->length);
714
715         if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
716             return (EINVAL);
717         /*
718          * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
719          * is the largest size we support
720          */
721         desc_size =
722             sizeof(hv_vmbus_channel_packet_multipage_buffer) -
723                     ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
724                         sizeof(uint64_t));
725         packet_len = desc_size + buffer_len;
726         packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
727
728         /*
729          * Setup the descriptor
730          */
731         desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
732         desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
733         desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
734         desc.length8 = (uint16_t) (packet_len_aligned >> 3);
735         desc.transaction_id = request_id;
736         desc.range_count = 1;
737
738         desc.range.length = multi_page_buffer->length;
739         desc.range.offset = multi_page_buffer->offset;
740
741         memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
742                 pfn_count * sizeof(uint64_t));
743
744         buffer_list[0].data = &desc;
745         buffer_list[0].length = desc_size;
746
747         buffer_list[1].data = buffer;
748         buffer_list[1].length = buffer_len;
749
750         buffer_list[2].data = &aligned_data;
751         buffer_list[2].length = packet_len_aligned - packet_len;
752
753         ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3);
754
755         /* TODO: We should determine if this is optional */
756         if (ret == 0 &&
757             !hv_vmbus_get_ring_buffer_interrupt_mask(&channel->outbound)) {
758             vmbus_channel_set_event(channel);
759         }
760
761         return (ret);
762 }
763
764 /**
765  * @brief Retrieve the user packet on the specified channel
766  */
767 int
768 hv_vmbus_channel_recv_packet(
769         hv_vmbus_channel*       channel,
770         void*                   Buffer,
771         uint32_t                buffer_len,
772         uint32_t*               buffer_actual_len,
773         uint64_t*               request_id)
774 {
775         int                     ret;
776         uint32_t                user_len;
777         uint32_t                packet_len;
778         hv_vm_packet_descriptor desc;
779
780         *buffer_actual_len = 0;
781         *request_id = 0;
782
783         ret = hv_ring_buffer_peek(&channel->inbound, &desc,
784                 sizeof(hv_vm_packet_descriptor));
785         if (ret != 0)
786                 return (0);
787
788         packet_len = desc.length8 << 3;
789         user_len = packet_len - (desc.data_offset8 << 3);
790
791         *buffer_actual_len = user_len;
792
793         if (user_len > buffer_len)
794                 return (EINVAL);
795
796         *request_id = desc.transaction_id;
797
798         /* Copy over the packet to the user buffer */
799         ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
800                 (desc.data_offset8 << 3));
801
802         return (0);
803 }
804
805 /**
806  * @brief Retrieve the raw packet on the specified channel
807  */
808 int
809 hv_vmbus_channel_recv_packet_raw(
810         hv_vmbus_channel*       channel,
811         void*                   buffer,
812         uint32_t                buffer_len,
813         uint32_t*               buffer_actual_len,
814         uint64_t*               request_id)
815 {
816         int             ret;
817         uint32_t        packetLen;
818         uint32_t        userLen;
819         hv_vm_packet_descriptor desc;
820
821         *buffer_actual_len = 0;
822         *request_id = 0;
823
824         ret = hv_ring_buffer_peek(
825                 &channel->inbound, &desc,
826                 sizeof(hv_vm_packet_descriptor));
827
828         if (ret != 0)
829             return (0);
830
831         packetLen = desc.length8 << 3;
832         userLen = packetLen - (desc.data_offset8 << 3);
833
834         *buffer_actual_len = packetLen;
835
836         if (packetLen > buffer_len)
837             return (ENOBUFS);
838
839         *request_id = desc.transaction_id;
840
841         /* Copy over the entire packet to the user buffer */
842         ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
843
844         return (0);
845 }