]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hyperv/vmbus/hv_channel.c
Update llvm, clang and lldb to release_38 branch r260756.
[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 static void     VmbusProcessChannelEvent(void* channel, int pending);
55
56 /**
57  *  @brief Trigger an event notification on the specified channel
58  */
59 static void
60 vmbus_channel_set_event(hv_vmbus_channel *channel)
61 {
62         hv_vmbus_monitor_page *monitor_page;
63
64         if (channel->offer_msg.monitor_allocated) {
65                 /* Each uint32_t represents 32 channels */
66                 synch_set_bit((channel->offer_msg.child_rel_id & 31),
67                         ((uint32_t *)hv_vmbus_g_connection.send_interrupt_page
68                                 + ((channel->offer_msg.child_rel_id >> 5))));
69
70                 monitor_page = (hv_vmbus_monitor_page *)
71                         hv_vmbus_g_connection.monitor_page_2;
72
73                 synch_set_bit(channel->monitor_bit,
74                         (uint32_t *)&monitor_page->
75                                 trigger_group[channel->monitor_group].u.pending);
76         } else {
77                 hv_vmbus_set_event(channel);
78         }
79
80 }
81
82 /**
83  * @brief Open the specified channel
84  */
85 int
86 hv_vmbus_channel_open(
87         hv_vmbus_channel*               new_channel,
88         uint32_t                        send_ring_buffer_size,
89         uint32_t                        recv_ring_buffer_size,
90         void*                           user_data,
91         uint32_t                        user_data_len,
92         hv_vmbus_pfn_channel_callback   pfn_on_channel_callback,
93         void*                           context)
94 {
95
96         int ret = 0;
97         void *in, *out;
98         hv_vmbus_channel_open_channel*  open_msg;
99         hv_vmbus_channel_msg_info*      open_info;
100
101         mtx_lock(&new_channel->sc_lock);
102         if (new_channel->state == HV_CHANNEL_OPEN_STATE) {
103             new_channel->state = HV_CHANNEL_OPENING_STATE;
104         } else {
105             mtx_unlock(&new_channel->sc_lock);
106             if(bootverbose)
107                 printf("VMBUS: Trying to open channel <%p> which in "
108                     "%d state.\n", new_channel, new_channel->state);
109             return (EINVAL);
110         }
111         mtx_unlock(&new_channel->sc_lock);
112
113         new_channel->on_channel_callback = pfn_on_channel_callback;
114         new_channel->channel_callback_context = context;
115
116         new_channel->rxq = hv_vmbus_g_context.hv_event_queue[new_channel->target_cpu];
117         TASK_INIT(&new_channel->channel_task, 0, VmbusProcessChannelEvent, new_channel);
118
119         /* Allocate the ring buffer */
120         out = contigmalloc((send_ring_buffer_size + recv_ring_buffer_size),
121             M_DEVBUF, M_ZERO, 0UL, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
122         KASSERT(out != NULL,
123             ("Error VMBUS: contigmalloc failed to allocate Ring Buffer!"));
124         if (out == NULL)
125                 return (ENOMEM);
126
127         in = ((uint8_t *) out + send_ring_buffer_size);
128
129         new_channel->ring_buffer_pages = out;
130         new_channel->ring_buffer_page_count = (send_ring_buffer_size +
131             recv_ring_buffer_size) >> PAGE_SHIFT;
132         new_channel->ring_buffer_size = send_ring_buffer_size +
133             recv_ring_buffer_size;
134
135         hv_vmbus_ring_buffer_init(
136                 &new_channel->outbound,
137                 out,
138                 send_ring_buffer_size);
139
140         hv_vmbus_ring_buffer_init(
141                 &new_channel->inbound,
142                 in,
143                 recv_ring_buffer_size);
144
145         /**
146          * Establish the gpadl for the ring buffer
147          */
148         new_channel->ring_buffer_gpadl_handle = 0;
149
150         ret = hv_vmbus_channel_establish_gpadl(new_channel,
151                 new_channel->outbound.ring_buffer,
152                 send_ring_buffer_size + recv_ring_buffer_size,
153                 &new_channel->ring_buffer_gpadl_handle);
154
155         /**
156          * Create and init the channel open message
157          */
158         open_info = (hv_vmbus_channel_msg_info*) malloc(
159                 sizeof(hv_vmbus_channel_msg_info) +
160                         sizeof(hv_vmbus_channel_open_channel),
161                 M_DEVBUF,
162                 M_NOWAIT);
163         KASSERT(open_info != NULL,
164             ("Error VMBUS: malloc failed to allocate Open Channel message!"));
165
166         if (open_info == NULL)
167                 return (ENOMEM);
168
169         sema_init(&open_info->wait_sema, 0, "Open Info Sema");
170
171         open_msg = (hv_vmbus_channel_open_channel*) open_info->msg;
172         open_msg->header.message_type = HV_CHANNEL_MESSAGE_OPEN_CHANNEL;
173         open_msg->open_id = new_channel->offer_msg.child_rel_id;
174         open_msg->child_rel_id = new_channel->offer_msg.child_rel_id;
175         open_msg->ring_buffer_gpadl_handle =
176                 new_channel->ring_buffer_gpadl_handle;
177         open_msg->downstream_ring_buffer_page_offset = send_ring_buffer_size
178                 >> PAGE_SHIFT;
179         open_msg->target_vcpu = new_channel->target_vcpu;
180
181         if (user_data_len)
182                 memcpy(open_msg->user_data, user_data, user_data_len);
183
184         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
185         TAILQ_INSERT_TAIL(
186                 &hv_vmbus_g_connection.channel_msg_anchor,
187                 open_info,
188                 msg_list_entry);
189         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
190
191         ret = hv_vmbus_post_message(
192                 open_msg, sizeof(hv_vmbus_channel_open_channel));
193
194         if (ret != 0)
195             goto cleanup;
196
197         ret = sema_timedwait(&open_info->wait_sema, 500); /* KYS 5 seconds */
198
199         if (ret) {
200             if(bootverbose)
201                 printf("VMBUS: channel <%p> open timeout.\n", new_channel);
202             goto cleanup;
203         }
204
205         if (open_info->response.open_result.status == 0) {
206             new_channel->state = HV_CHANNEL_OPENED_STATE;
207             if(bootverbose)
208                 printf("VMBUS: channel <%p> open success.\n", new_channel);
209         } else {
210             if(bootverbose)
211                 printf("Error VMBUS: channel <%p> open failed - %d!\n",
212                         new_channel, open_info->response.open_result.status);
213         }
214
215         cleanup:
216         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
217         TAILQ_REMOVE(
218                 &hv_vmbus_g_connection.channel_msg_anchor,
219                 open_info,
220                 msg_list_entry);
221         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
222         sema_destroy(&open_info->wait_sema);
223         free(open_info, M_DEVBUF);
224
225         return (ret);
226 }
227
228 /**
229  * @brief Create a gpadl for the specified buffer
230  */
231 static int
232 vmbus_channel_create_gpadl_header(
233         void*                           contig_buffer,
234         uint32_t                        size,   /* page-size multiple */
235         hv_vmbus_channel_msg_info**     msg_info,
236         uint32_t*                       message_count)
237 {
238         int                             i;
239         int                             page_count;
240         unsigned long long              pfn;
241         uint32_t                        msg_size;
242         hv_vmbus_channel_gpadl_header*  gpa_header;
243         hv_vmbus_channel_gpadl_body*    gpadl_body;
244         hv_vmbus_channel_msg_info*      msg_header;
245         hv_vmbus_channel_msg_info*      msg_body;
246
247         int pfnSum, pfnCount, pfnLeft, pfnCurr, pfnSize;
248
249         page_count = size >> PAGE_SHIFT;
250         pfn = hv_get_phys_addr(contig_buffer) >> PAGE_SHIFT;
251
252         /*do we need a gpadl body msg */
253         pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
254             - sizeof(hv_vmbus_channel_gpadl_header)
255             - sizeof(hv_gpa_range);
256         pfnCount = pfnSize / sizeof(uint64_t);
257
258         if (page_count > pfnCount) { /* if(we need a gpadl body)        */
259             /* fill in the header               */
260             msg_size = sizeof(hv_vmbus_channel_msg_info)
261                 + sizeof(hv_vmbus_channel_gpadl_header)
262                 + sizeof(hv_gpa_range)
263                 + pfnCount * sizeof(uint64_t);
264             msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
265             KASSERT(
266                 msg_header != NULL,
267                 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
268             if (msg_header == NULL)
269                 return (ENOMEM);
270
271             TAILQ_INIT(&msg_header->sub_msg_list_anchor);
272             msg_header->message_size = msg_size;
273
274             gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
275             gpa_header->range_count = 1;
276             gpa_header->range_buf_len = sizeof(hv_gpa_range)
277                 + page_count * sizeof(uint64_t);
278             gpa_header->range[0].byte_offset = 0;
279             gpa_header->range[0].byte_count = size;
280             for (i = 0; i < pfnCount; i++) {
281                 gpa_header->range[0].pfn_array[i] = pfn + i;
282             }
283             *msg_info = msg_header;
284             *message_count = 1;
285
286             pfnSum = pfnCount;
287             pfnLeft = page_count - pfnCount;
288
289             /*
290              *  figure out how many pfns we can fit
291              */
292             pfnSize = HV_MAX_SIZE_CHANNEL_MESSAGE
293                 - sizeof(hv_vmbus_channel_gpadl_body);
294             pfnCount = pfnSize / sizeof(uint64_t);
295
296             /*
297              * fill in the body
298              */
299             while (pfnLeft) {
300                 if (pfnLeft > pfnCount) {
301                     pfnCurr = pfnCount;
302                 } else {
303                     pfnCurr = pfnLeft;
304                 }
305
306                 msg_size = sizeof(hv_vmbus_channel_msg_info) +
307                     sizeof(hv_vmbus_channel_gpadl_body) +
308                     pfnCurr * sizeof(uint64_t);
309                 msg_body = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
310                 KASSERT(
311                     msg_body != NULL,
312                     ("Error VMBUS: malloc failed to allocate Gpadl msg_body!"));
313                 if (msg_body == NULL)
314                     return (ENOMEM);
315
316                 msg_body->message_size = msg_size;
317                 (*message_count)++;
318                 gpadl_body =
319                     (hv_vmbus_channel_gpadl_body*) msg_body->msg;
320                 /*
321                  * gpadl_body->gpadl = kbuffer;
322                  */
323                 for (i = 0; i < pfnCurr; i++) {
324                     gpadl_body->pfn[i] = pfn + pfnSum + i;
325                 }
326
327                 TAILQ_INSERT_TAIL(
328                     &msg_header->sub_msg_list_anchor,
329                     msg_body,
330                     msg_list_entry);
331                 pfnSum += pfnCurr;
332                 pfnLeft -= pfnCurr;
333             }
334         } else { /* else everything fits in a header */
335
336             msg_size = sizeof(hv_vmbus_channel_msg_info) +
337                 sizeof(hv_vmbus_channel_gpadl_header) +
338                 sizeof(hv_gpa_range) +
339                 page_count * sizeof(uint64_t);
340             msg_header = malloc(msg_size, M_DEVBUF, M_NOWAIT | M_ZERO);
341             KASSERT(
342                 msg_header != NULL,
343                 ("Error VMBUS: malloc failed to allocate Gpadl Message!"));
344             if (msg_header == NULL)
345                 return (ENOMEM);
346
347             msg_header->message_size = msg_size;
348
349             gpa_header = (hv_vmbus_channel_gpadl_header*) msg_header->msg;
350             gpa_header->range_count = 1;
351             gpa_header->range_buf_len = sizeof(hv_gpa_range) +
352                 page_count * sizeof(uint64_t);
353             gpa_header->range[0].byte_offset = 0;
354             gpa_header->range[0].byte_count = size;
355             for (i = 0; i < page_count; i++) {
356                 gpa_header->range[0].pfn_array[i] = pfn + i;
357             }
358
359             *msg_info = msg_header;
360             *message_count = 1;
361         }
362
363         return (0);
364 }
365
366 /**
367  * @brief Establish a GPADL for the specified buffer
368  */
369 int
370 hv_vmbus_channel_establish_gpadl(
371         hv_vmbus_channel*       channel,
372         void*                   contig_buffer,
373         uint32_t                size, /* page-size multiple */
374         uint32_t*               gpadl_handle)
375
376 {
377         int ret = 0;
378         hv_vmbus_channel_gpadl_header*  gpadl_msg;
379         hv_vmbus_channel_gpadl_body*    gpadl_body;
380         hv_vmbus_channel_msg_info*      msg_info;
381         hv_vmbus_channel_msg_info*      sub_msg_info;
382         uint32_t                        msg_count;
383         hv_vmbus_channel_msg_info*      curr;
384         uint32_t                        next_gpadl_handle;
385
386         next_gpadl_handle = hv_vmbus_g_connection.next_gpadl_handle;
387         atomic_add_int((int*) &hv_vmbus_g_connection.next_gpadl_handle, 1);
388
389         ret = vmbus_channel_create_gpadl_header(
390                 contig_buffer, size, &msg_info, &msg_count);
391
392         if(ret != 0) { /* if(allocation failed) return immediately */
393             /* reverse atomic_add_int above */
394             atomic_subtract_int((int*)
395                     &hv_vmbus_g_connection.next_gpadl_handle, 1);
396             return ret;
397         }
398
399         sema_init(&msg_info->wait_sema, 0, "Open Info Sema");
400         gpadl_msg = (hv_vmbus_channel_gpadl_header*) msg_info->msg;
401         gpadl_msg->header.message_type = HV_CHANNEL_MESSAGEL_GPADL_HEADER;
402         gpadl_msg->child_rel_id = channel->offer_msg.child_rel_id;
403         gpadl_msg->gpadl = next_gpadl_handle;
404
405         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
406         TAILQ_INSERT_TAIL(
407                 &hv_vmbus_g_connection.channel_msg_anchor,
408                 msg_info,
409                 msg_list_entry);
410
411         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
412
413         ret = hv_vmbus_post_message(
414                 gpadl_msg,
415                 msg_info->message_size -
416                     (uint32_t) sizeof(hv_vmbus_channel_msg_info));
417
418         if (ret != 0)
419             goto cleanup;
420
421         if (msg_count > 1) {
422             TAILQ_FOREACH(curr,
423                     &msg_info->sub_msg_list_anchor, msg_list_entry) {
424                 sub_msg_info = curr;
425                 gpadl_body =
426                     (hv_vmbus_channel_gpadl_body*) sub_msg_info->msg;
427
428                 gpadl_body->header.message_type =
429                     HV_CHANNEL_MESSAGE_GPADL_BODY;
430                 gpadl_body->gpadl = next_gpadl_handle;
431
432                 ret = hv_vmbus_post_message(
433                         gpadl_body,
434                         sub_msg_info->message_size
435                             - (uint32_t) sizeof(hv_vmbus_channel_msg_info));
436                  /* if (the post message failed) give up and clean up */
437                 if(ret != 0)
438                     goto cleanup;
439             }
440         }
441
442         ret = sema_timedwait(&msg_info->wait_sema, 500); /* KYS 5 seconds*/
443         if (ret != 0)
444             goto cleanup;
445
446         *gpadl_handle = gpadl_msg->gpadl;
447
448 cleanup:
449
450         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
451         TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
452                 msg_info, msg_list_entry);
453         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
454
455         sema_destroy(&msg_info->wait_sema);
456         free(msg_info, M_DEVBUF);
457
458         return (ret);
459 }
460
461 /**
462  * @brief Teardown the specified GPADL handle
463  */
464 int
465 hv_vmbus_channel_teardown_gpdal(
466         hv_vmbus_channel*       channel,
467         uint32_t                gpadl_handle)
468 {
469         int                                     ret = 0;
470         hv_vmbus_channel_gpadl_teardown*        msg;
471         hv_vmbus_channel_msg_info*              info;
472
473         info = (hv_vmbus_channel_msg_info *)
474                 malloc( sizeof(hv_vmbus_channel_msg_info) +
475                         sizeof(hv_vmbus_channel_gpadl_teardown),
476                                 M_DEVBUF, M_NOWAIT);
477         KASSERT(info != NULL,
478             ("Error VMBUS: malloc failed to allocate Gpadl Teardown Msg!"));
479         if (info == NULL) {
480             ret = ENOMEM;
481             goto cleanup;
482         }
483
484         sema_init(&info->wait_sema, 0, "Open Info Sema");
485
486         msg = (hv_vmbus_channel_gpadl_teardown*) info->msg;
487
488         msg->header.message_type = HV_CHANNEL_MESSAGE_GPADL_TEARDOWN;
489         msg->child_rel_id = channel->offer_msg.child_rel_id;
490         msg->gpadl = gpadl_handle;
491
492         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
493         TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_msg_anchor,
494                         info, msg_list_entry);
495         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
496
497         ret = hv_vmbus_post_message(msg,
498                         sizeof(hv_vmbus_channel_gpadl_teardown));
499         if (ret != 0) 
500             goto cleanup;
501         
502         ret = sema_timedwait(&info->wait_sema, 500); /* KYS 5 seconds */
503
504 cleanup:
505         /*
506          * Received a torndown response
507          */
508         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
509         TAILQ_REMOVE(&hv_vmbus_g_connection.channel_msg_anchor,
510                         info, msg_list_entry);
511         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
512         sema_destroy(&info->wait_sema);
513         free(info, M_DEVBUF);
514
515         return (ret);
516 }
517
518 static void
519 hv_vmbus_channel_close_internal(hv_vmbus_channel *channel)
520 {
521         int ret = 0;
522         struct taskqueue *rxq = channel->rxq;
523         hv_vmbus_channel_close_channel* msg;
524         hv_vmbus_channel_msg_info* info;
525
526         channel->state = HV_CHANNEL_OPEN_STATE;
527         channel->sc_creation_callback = NULL;
528
529         /*
530          * set rxq to NULL to avoid more requests be scheduled
531          */
532         channel->rxq = NULL;
533         taskqueue_drain(rxq, &channel->channel_task);
534         /*
535          * Grab the lock to prevent race condition when a packet received
536          * and unloading driver is in the process.
537          */
538         mtx_lock(&channel->inbound_lock);
539         channel->on_channel_callback = NULL;
540         mtx_unlock(&channel->inbound_lock);
541
542         /**
543          * Send a closing message
544          */
545         info = (hv_vmbus_channel_msg_info *)
546                 malloc( sizeof(hv_vmbus_channel_msg_info) +
547                         sizeof(hv_vmbus_channel_close_channel),
548                                 M_DEVBUF, M_NOWAIT);
549         KASSERT(info != NULL, ("VMBUS: malloc failed hv_vmbus_channel_close!"));
550         if(info == NULL)
551             return;
552
553         msg = (hv_vmbus_channel_close_channel*) info->msg;
554         msg->header.message_type = HV_CHANNEL_MESSAGE_CLOSE_CHANNEL;
555         msg->child_rel_id = channel->offer_msg.child_rel_id;
556
557         ret = hv_vmbus_post_message(
558                 msg, sizeof(hv_vmbus_channel_close_channel));
559
560         /* Tear down the gpadl for the channel's ring buffer */
561         if (channel->ring_buffer_gpadl_handle) {
562                 hv_vmbus_channel_teardown_gpdal(channel,
563                         channel->ring_buffer_gpadl_handle);
564         }
565
566         /* TODO: Send a msg to release the childRelId */
567
568         /* cleanup the ring buffers for this channel */
569         hv_ring_buffer_cleanup(&channel->outbound);
570         hv_ring_buffer_cleanup(&channel->inbound);
571
572         contigfree(channel->ring_buffer_pages, channel->ring_buffer_size,
573             M_DEVBUF);
574
575         free(info, M_DEVBUF);
576 }
577
578 /**
579  * @brief Close the specified channel
580  */
581 void
582 hv_vmbus_channel_close(hv_vmbus_channel *channel)
583 {
584         hv_vmbus_channel*       sub_channel;
585
586         if (channel->primary_channel != NULL) {
587                 /*
588                  * We only close multi-channels when the primary is
589                  * closed.
590                  */
591                 return;
592         }
593
594         /*
595          * Close all multi-channels first.
596          */
597         TAILQ_FOREACH(sub_channel, &channel->sc_list_anchor,
598             sc_list_entry) {
599                 if (sub_channel->state != HV_CHANNEL_OPENED_STATE)
600                         continue;
601                 hv_vmbus_channel_close_internal(sub_channel);
602         }
603         /*
604          * Then close the primary channel.
605          */
606         hv_vmbus_channel_close_internal(channel);
607 }
608
609 /**
610  * @brief Send the specified buffer on the given channel
611  */
612 int
613 hv_vmbus_channel_send_packet(
614         hv_vmbus_channel*       channel,
615         void*                   buffer,
616         uint32_t                buffer_len,
617         uint64_t                request_id,
618         hv_vmbus_packet_type    type,
619         uint32_t                flags)
620 {
621         int                     ret = 0;
622         hv_vm_packet_descriptor desc;
623         uint32_t                packet_len;
624         uint64_t                aligned_data;
625         uint32_t                packet_len_aligned;
626         boolean_t               need_sig;
627         hv_vmbus_sg_buffer_list buffer_list[3];
628
629         packet_len = sizeof(hv_vm_packet_descriptor) + buffer_len;
630         packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
631         aligned_data = 0;
632
633         /* Setup the descriptor */
634         desc.type = type;   /* HV_VMBUS_PACKET_TYPE_DATA_IN_BAND;             */
635         desc.flags = flags; /* HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED */
636                             /* in 8-bytes granularity */
637         desc.data_offset8 = sizeof(hv_vm_packet_descriptor) >> 3;
638         desc.length8 = (uint16_t) (packet_len_aligned >> 3);
639         desc.transaction_id = request_id;
640
641         buffer_list[0].data = &desc;
642         buffer_list[0].length = sizeof(hv_vm_packet_descriptor);
643
644         buffer_list[1].data = buffer;
645         buffer_list[1].length = buffer_len;
646
647         buffer_list[2].data = &aligned_data;
648         buffer_list[2].length = packet_len_aligned - packet_len;
649
650         ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
651             &need_sig);
652
653         /* TODO: We should determine if this is optional */
654         if (ret == 0 && need_sig) {
655                 vmbus_channel_set_event(channel);
656         }
657
658         return (ret);
659 }
660
661 /**
662  * @brief Send a range of single-page buffer packets using
663  * a GPADL Direct packet type
664  */
665 int
666 hv_vmbus_channel_send_packet_pagebuffer(
667         hv_vmbus_channel*       channel,
668         hv_vmbus_page_buffer    page_buffers[],
669         uint32_t                page_count,
670         void*                   buffer,
671         uint32_t                buffer_len,
672         uint64_t                request_id)
673 {
674
675         int                                     ret = 0;
676         boolean_t                               need_sig;
677         uint32_t                                packet_len;
678         uint32_t                                page_buflen;
679         uint32_t                                packetLen_aligned;
680         hv_vmbus_sg_buffer_list                 buffer_list[4];
681         hv_vmbus_channel_packet_page_buffer     desc;
682         uint32_t                                descSize;
683         uint64_t                                alignedData = 0;
684
685         if (page_count > HV_MAX_PAGE_BUFFER_COUNT)
686                 return (EINVAL);
687
688         /*
689          * Adjust the size down since hv_vmbus_channel_packet_page_buffer
690          *  is the largest size we support
691          */
692         descSize = __offsetof(hv_vmbus_channel_packet_page_buffer, range);
693         page_buflen = sizeof(hv_vmbus_page_buffer) * page_count;
694         packet_len = descSize + page_buflen + buffer_len;
695         packetLen_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
696
697         /* Setup the descriptor */
698         desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
699         desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
700         /* in 8-bytes granularity */
701         desc.data_offset8 = (descSize + page_buflen) >> 3;
702         desc.length8 = (uint16_t) (packetLen_aligned >> 3);
703         desc.transaction_id = request_id;
704         desc.range_count = page_count;
705
706         buffer_list[0].data = &desc;
707         buffer_list[0].length = descSize;
708
709         buffer_list[1].data = page_buffers;
710         buffer_list[1].length = page_buflen;
711
712         buffer_list[2].data = buffer;
713         buffer_list[2].length = buffer_len;
714
715         buffer_list[3].data = &alignedData;
716         buffer_list[3].length = packetLen_aligned - packet_len;
717
718         ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 4,
719             &need_sig);
720
721         /* TODO: We should determine if this is optional */
722         if (ret == 0 && need_sig) {
723                 vmbus_channel_set_event(channel);
724         }
725
726         return (ret);
727 }
728
729 /**
730  * @brief Send a multi-page buffer packet using a GPADL Direct packet type
731  */
732 int
733 hv_vmbus_channel_send_packet_multipagebuffer(
734         hv_vmbus_channel*               channel,
735         hv_vmbus_multipage_buffer*      multi_page_buffer,
736         void*                           buffer,
737         uint32_t                        buffer_len,
738         uint64_t                        request_id)
739 {
740
741         int                     ret = 0;
742         uint32_t                desc_size;
743         boolean_t               need_sig;
744         uint32_t                packet_len;
745         uint32_t                packet_len_aligned;
746         uint32_t                pfn_count;
747         uint64_t                aligned_data = 0;
748         hv_vmbus_sg_buffer_list buffer_list[3];
749         hv_vmbus_channel_packet_multipage_buffer desc;
750
751         pfn_count =
752             HV_NUM_PAGES_SPANNED(
753                     multi_page_buffer->offset,
754                     multi_page_buffer->length);
755
756         if ((pfn_count == 0) || (pfn_count > HV_MAX_MULTIPAGE_BUFFER_COUNT))
757             return (EINVAL);
758         /*
759          * Adjust the size down since hv_vmbus_channel_packet_multipage_buffer
760          * is the largest size we support
761          */
762         desc_size =
763             sizeof(hv_vmbus_channel_packet_multipage_buffer) -
764                     ((HV_MAX_MULTIPAGE_BUFFER_COUNT - pfn_count) *
765                         sizeof(uint64_t));
766         packet_len = desc_size + buffer_len;
767         packet_len_aligned = HV_ALIGN_UP(packet_len, sizeof(uint64_t));
768
769         /*
770          * Setup the descriptor
771          */
772         desc.type = HV_VMBUS_PACKET_TYPE_DATA_USING_GPA_DIRECT;
773         desc.flags = HV_VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
774         desc.data_offset8 = desc_size >> 3; /* in 8-bytes granularity */
775         desc.length8 = (uint16_t) (packet_len_aligned >> 3);
776         desc.transaction_id = request_id;
777         desc.range_count = 1;
778
779         desc.range.length = multi_page_buffer->length;
780         desc.range.offset = multi_page_buffer->offset;
781
782         memcpy(desc.range.pfn_array, multi_page_buffer->pfn_array,
783                 pfn_count * sizeof(uint64_t));
784
785         buffer_list[0].data = &desc;
786         buffer_list[0].length = desc_size;
787
788         buffer_list[1].data = buffer;
789         buffer_list[1].length = buffer_len;
790
791         buffer_list[2].data = &aligned_data;
792         buffer_list[2].length = packet_len_aligned - packet_len;
793
794         ret = hv_ring_buffer_write(&channel->outbound, buffer_list, 3,
795             &need_sig);
796
797         /* TODO: We should determine if this is optional */
798         if (ret == 0 && need_sig) {
799             vmbus_channel_set_event(channel);
800         }
801
802         return (ret);
803 }
804
805 /**
806  * @brief Retrieve the user packet on the specified channel
807  */
808 int
809 hv_vmbus_channel_recv_packet(
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                user_len;
818         uint32_t                packet_len;
819         hv_vm_packet_descriptor desc;
820
821         *buffer_actual_len = 0;
822         *request_id = 0;
823
824         ret = hv_ring_buffer_peek(&channel->inbound, &desc,
825                 sizeof(hv_vm_packet_descriptor));
826         if (ret != 0)
827                 return (0);
828
829         packet_len = desc.length8 << 3;
830         user_len = packet_len - (desc.data_offset8 << 3);
831
832         *buffer_actual_len = user_len;
833
834         if (user_len > buffer_len)
835                 return (EINVAL);
836
837         *request_id = desc.transaction_id;
838
839         /* Copy over the packet to the user buffer */
840         ret = hv_ring_buffer_read(&channel->inbound, Buffer, user_len,
841                 (desc.data_offset8 << 3));
842
843         return (0);
844 }
845
846 /**
847  * @brief Retrieve the raw packet on the specified channel
848  */
849 int
850 hv_vmbus_channel_recv_packet_raw(
851         hv_vmbus_channel*       channel,
852         void*                   buffer,
853         uint32_t                buffer_len,
854         uint32_t*               buffer_actual_len,
855         uint64_t*               request_id)
856 {
857         int             ret;
858         uint32_t        packetLen;
859         uint32_t        userLen;
860         hv_vm_packet_descriptor desc;
861
862         *buffer_actual_len = 0;
863         *request_id = 0;
864
865         ret = hv_ring_buffer_peek(
866                 &channel->inbound, &desc,
867                 sizeof(hv_vm_packet_descriptor));
868
869         if (ret != 0)
870             return (0);
871
872         packetLen = desc.length8 << 3;
873         userLen = packetLen - (desc.data_offset8 << 3);
874
875         *buffer_actual_len = packetLen;
876
877         if (packetLen > buffer_len)
878             return (ENOBUFS);
879
880         *request_id = desc.transaction_id;
881
882         /* Copy over the entire packet to the user buffer */
883         ret = hv_ring_buffer_read(&channel->inbound, buffer, packetLen, 0);
884
885         return (0);
886 }
887
888
889 /**
890  * Process a channel event notification
891  */
892 static void
893 VmbusProcessChannelEvent(void* context, int pending)
894 {
895         void* arg;
896         uint32_t bytes_to_read;
897         hv_vmbus_channel* channel = (hv_vmbus_channel*)context;
898         boolean_t is_batched_reading;
899
900         /**
901          * Find the channel based on this relid and invokes
902          * the channel callback to process the event
903          */
904
905         if (channel == NULL) {
906                 return;
907         }
908         /**
909          * To deal with the race condition where we might
910          * receive a packet while the relevant driver is
911          * being unloaded, dispatch the callback while
912          * holding the channel lock. The unloading driver
913          * will acquire the same channel lock to set the
914          * callback to NULL. This closes the window.
915          */
916
917         /*
918          * Disable the lock due to newly added WITNESS check in r277723.
919          * Will seek other way to avoid race condition.
920          * -- whu
921          */
922         // mtx_lock(&channel->inbound_lock);
923         if (channel->on_channel_callback != NULL) {
924                 arg = channel->channel_callback_context;
925                 is_batched_reading = channel->batched_reading;
926                 /*
927                  * Optimize host to guest signaling by ensuring:
928                  * 1. While reading the channel, we disable interrupts from
929                  *    host.
930                  * 2. Ensure that we process all posted messages from the host
931                  *    before returning from this callback.
932                  * 3. Once we return, enable signaling from the host. Once this
933                  *    state is set we check to see if additional packets are
934                  *    available to read. In this case we repeat the process.
935                  */
936                 do {
937                         if (is_batched_reading)
938                                 hv_ring_buffer_read_begin(&channel->inbound);
939
940                         channel->on_channel_callback(arg);
941
942                         if (is_batched_reading)
943                                 bytes_to_read =
944                                     hv_ring_buffer_read_end(&channel->inbound);
945                         else
946                                 bytes_to_read = 0;
947                 } while (is_batched_reading && (bytes_to_read != 0));
948         }
949         // mtx_unlock(&channel->inbound_lock);
950 }