]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hyperv/vmbus/hv_channel_mgmt.c
Merge ^/head r296369 through r296409.
[FreeBSD/FreeBSD.git] / sys / dev / hyperv / vmbus / hv_channel_mgmt.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/param.h>
30 #include <sys/mbuf.h>
31
32 #include "hv_vmbus_priv.h"
33
34 /*
35  * Internal functions
36  */
37
38 static void vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr);
39 static void vmbus_channel_on_offer_internal(void* context);
40 static void vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr);
41 static void vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr);
42 static void vmbus_channel_on_offer_rescind_internal(void* context);
43 static void vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr);
44 static void vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr);
45 static void vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr);
46 static void vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr);
47
48 /**
49  * Channel message dispatch table
50  */
51 hv_vmbus_channel_msg_table_entry
52     g_channel_message_table[HV_CHANNEL_MESSAGE_COUNT] = {
53         { HV_CHANNEL_MESSAGE_INVALID,
54                 NULL },
55         { HV_CHANNEL_MESSAGE_OFFER_CHANNEL,
56                 vmbus_channel_on_offer },
57         { HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER,
58                 vmbus_channel_on_offer_rescind },
59         { HV_CHANNEL_MESSAGE_REQUEST_OFFERS,
60                 NULL },
61         { HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED,
62                 vmbus_channel_on_offers_delivered },
63         { HV_CHANNEL_MESSAGE_OPEN_CHANNEL,
64                 NULL },
65         { HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT,
66                 vmbus_channel_on_open_result },
67         { HV_CHANNEL_MESSAGE_CLOSE_CHANNEL,
68                 NULL },
69         { HV_CHANNEL_MESSAGEL_GPADL_HEADER,
70                 NULL },
71         { HV_CHANNEL_MESSAGE_GPADL_BODY,
72                 NULL },
73         { HV_CHANNEL_MESSAGE_GPADL_CREATED,
74                 vmbus_channel_on_gpadl_created },
75         { HV_CHANNEL_MESSAGE_GPADL_TEARDOWN,
76                 NULL },
77         { HV_CHANNEL_MESSAGE_GPADL_TORNDOWN,
78                 vmbus_channel_on_gpadl_torndown },
79         { HV_CHANNEL_MESSAGE_REL_ID_RELEASED,
80                 NULL },
81         { HV_CHANNEL_MESSAGE_INITIATED_CONTACT,
82                 NULL },
83         { HV_CHANNEL_MESSAGE_VERSION_RESPONSE,
84                 vmbus_channel_on_version_response },
85         { HV_CHANNEL_MESSAGE_UNLOAD,
86                 NULL }
87 };
88
89 typedef struct hv_work_item {
90         struct task     work;
91         void            (*callback)(void *);
92         void*           context;
93 } hv_work_item;
94
95 /**
96  * Implementation of the work abstraction.
97  */
98 static void
99 work_item_callback(void *work, int pending)
100 {
101         struct hv_work_item *w = (struct hv_work_item *)work;
102
103         w->callback(w->context);
104
105         free(w, M_DEVBUF);
106 }
107
108 /**
109  * @brief Create work item
110  */
111 static int
112 hv_queue_work_item(
113         void (*callback)(void *), void *context)
114 {
115         struct hv_work_item *w = malloc(sizeof(struct hv_work_item),
116                                         M_DEVBUF, M_NOWAIT);
117         KASSERT(w != NULL, ("Error VMBUS: Failed to allocate WorkItem\n"));
118         if (w == NULL)
119             return (ENOMEM);
120
121         w->callback = callback;
122         w->context = context;
123
124         TASK_INIT(&w->work, 0, work_item_callback, w);
125
126         return (taskqueue_enqueue(taskqueue_thread, &w->work));
127 }
128
129
130 /**
131  * @brief Allocate and initialize a vmbus channel object
132  */
133 hv_vmbus_channel*
134 hv_vmbus_allocate_channel(void)
135 {
136         hv_vmbus_channel* channel;
137
138         channel = (hv_vmbus_channel*) malloc(
139                                         sizeof(hv_vmbus_channel),
140                                         M_DEVBUF,
141                                         M_WAITOK | M_ZERO);
142
143         mtx_init(&channel->sc_lock, "vmbus multi channel", NULL, MTX_DEF);
144         TAILQ_INIT(&channel->sc_list_anchor);
145
146         return (channel);
147 }
148
149 /**
150  * @brief Release the resources used by the vmbus channel object
151  */
152 void
153 hv_vmbus_free_vmbus_channel(hv_vmbus_channel* channel)
154 {
155         mtx_destroy(&channel->sc_lock);
156         free(channel, M_DEVBUF);
157 }
158
159 /**
160  * @brief Process the offer by creating a channel/device
161  * associated with this offer
162  */
163 static void
164 vmbus_channel_process_offer(hv_vmbus_channel *new_channel)
165 {
166         boolean_t               f_new;
167         hv_vmbus_channel*       channel;
168         int                     ret;
169         uint32_t                relid;
170
171         f_new = TRUE;
172         channel = NULL;
173         relid = new_channel->offer_msg.child_rel_id;
174         /*
175          * Make sure this is a new offer
176          */
177         mtx_lock(&hv_vmbus_g_connection.channel_lock);
178         hv_vmbus_g_connection.channels[relid] = new_channel;
179
180         TAILQ_FOREACH(channel, &hv_vmbus_g_connection.channel_anchor,
181             list_entry)
182         {
183                 if (memcmp(&channel->offer_msg.offer.interface_type,
184                     &new_channel->offer_msg.offer.interface_type,
185                     sizeof(hv_guid)) == 0 &&
186                     memcmp(&channel->offer_msg.offer.interface_instance,
187                     &new_channel->offer_msg.offer.interface_instance,
188                     sizeof(hv_guid)) == 0) {
189                         f_new = FALSE;
190                         break;
191                 }
192         }
193
194         if (f_new) {
195                 /* Insert at tail */
196                 TAILQ_INSERT_TAIL(
197                     &hv_vmbus_g_connection.channel_anchor,
198                     new_channel,
199                     list_entry);
200         }
201         mtx_unlock(&hv_vmbus_g_connection.channel_lock);
202
203         /*XXX add new channel to percpu_list */
204
205         if (!f_new) {
206                 /*
207                  * Check if this is a sub channel.
208                  */
209                 if (new_channel->offer_msg.offer.sub_channel_index != 0) {
210                         /*
211                          * It is a sub channel offer, process it.
212                          */
213                         new_channel->primary_channel = channel;
214                         new_channel->device = channel->device;
215                         mtx_lock(&channel->sc_lock);
216                         TAILQ_INSERT_TAIL(
217                             &channel->sc_list_anchor,
218                             new_channel,
219                             sc_list_entry);
220                         mtx_unlock(&channel->sc_lock);
221
222                         /* Insert new channel into channel_anchor. */
223                         printf("VMBUS get multi-channel offer, rel=%u,sub=%u\n",
224                             new_channel->offer_msg.child_rel_id,
225                             new_channel->offer_msg.offer.sub_channel_index);    
226                         mtx_lock(&hv_vmbus_g_connection.channel_lock);
227                         TAILQ_INSERT_TAIL(&hv_vmbus_g_connection.channel_anchor,
228                             new_channel, list_entry);                           
229                         mtx_unlock(&hv_vmbus_g_connection.channel_lock);
230
231                         if(bootverbose)
232                                 printf("VMBUS: new multi-channel offer <%p>, "
233                                     "its primary channel is <%p>.\n",
234                                     new_channel, new_channel->primary_channel);
235
236                         /*XXX add it to percpu_list */
237
238                         new_channel->state = HV_CHANNEL_OPEN_STATE;
239                         if (channel->sc_creation_callback != NULL) {
240                                 channel->sc_creation_callback(new_channel);
241                         }
242                         return;
243                 }
244
245             hv_vmbus_free_vmbus_channel(new_channel);
246             return;
247         }
248
249         new_channel->state = HV_CHANNEL_OPEN_STATE;
250
251         /*
252          * Start the process of binding this offer to the driver
253          * (We need to set the device field before calling
254          * hv_vmbus_child_device_add())
255          */
256         new_channel->device = hv_vmbus_child_device_create(
257             new_channel->offer_msg.offer.interface_type,
258             new_channel->offer_msg.offer.interface_instance, new_channel);
259
260         /*
261          * Add the new device to the bus. This will kick off device-driver
262          * binding which eventually invokes the device driver's AddDevice()
263          * method.
264          */
265         ret = hv_vmbus_child_device_register(new_channel->device);
266         if (ret != 0) {
267                 mtx_lock(&hv_vmbus_g_connection.channel_lock);
268                 TAILQ_REMOVE(
269                     &hv_vmbus_g_connection.channel_anchor,
270                     new_channel,
271                     list_entry);
272                 mtx_unlock(&hv_vmbus_g_connection.channel_lock);
273                 hv_vmbus_free_vmbus_channel(new_channel);
274         }
275 }
276
277 void
278 vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu)
279 {
280         KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
281
282         chan->target_cpu = cpu;
283         chan->target_vcpu = hv_vmbus_g_context.hv_vcpu_index[cpu];
284
285         if (bootverbose) {
286                 printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
287                     chan->offer_msg.child_rel_id,
288                     chan->target_cpu, chan->target_vcpu);
289         }
290 }
291
292 /**
293  * Array of device guids that are performance critical. We try to distribute
294  * the interrupt load for these devices across all online cpus. 
295  */
296 static const hv_guid high_perf_devices[] = {
297         {HV_NIC_GUID, },
298         {HV_IDE_GUID, },
299         {HV_SCSI_GUID, },
300 };
301
302 enum {
303         PERF_CHN_NIC = 0,
304         PERF_CHN_IDE,
305         PERF_CHN_SCSI,
306         MAX_PERF_CHN,
307 };
308
309 /*
310  * We use this static number to distribute the channel interrupt load.
311  */
312 static uint32_t next_vcpu;
313
314 /**
315  * Starting with Win8, we can statically distribute the incoming
316  * channel interrupt load by binding a channel to VCPU. We
317  * implement here a simple round robin scheme for distributing
318  * the interrupt load.
319  * We will bind channels that are not performance critical to cpu 0 and
320  * performance critical channels (IDE, SCSI and Network) will be uniformly
321  * distributed across all available CPUs.
322  */
323 static void
324 vmbus_channel_select_defcpu(struct hv_vmbus_channel *channel)
325 {
326         uint32_t current_cpu;
327         int i;
328         boolean_t is_perf_channel = FALSE;
329         const hv_guid *guid = &channel->offer_msg.offer.interface_type;
330
331         for (i = PERF_CHN_NIC; i < MAX_PERF_CHN; i++) {
332                 if (memcmp(guid->data, high_perf_devices[i].data,
333                     sizeof(hv_guid)) == 0) {
334                         is_perf_channel = TRUE;
335                         break;
336                 }
337         }
338
339         if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
340             (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7) ||
341             (!is_perf_channel)) {
342                 /* Stick to cpu0 */
343                 vmbus_channel_cpu_set(channel, 0);
344                 return;
345         }
346         /* mp_ncpus should have the number cpus currently online */
347         current_cpu = (++next_vcpu % mp_ncpus);
348         vmbus_channel_cpu_set(channel, current_cpu);
349 }
350
351 /**
352  * @brief Handler for channel offers from Hyper-V/Azure
353  *
354  * Handler for channel offers from vmbus in parent partition. We ignore
355  * all offers except network and storage offers. For each network and storage
356  * offers, we create a channel object and queue a work item to the channel
357  * object to process the offer synchronously
358  */
359 static void
360 vmbus_channel_on_offer(hv_vmbus_channel_msg_header* hdr)
361 {
362         hv_vmbus_channel_offer_channel* offer;
363         hv_vmbus_channel_offer_channel* copied;
364
365         offer = (hv_vmbus_channel_offer_channel*) hdr;
366
367         hv_guid *guidType;
368         hv_guid *guidInstance;
369
370         guidType = &offer->offer.interface_type;
371         guidInstance = &offer->offer.interface_instance;
372
373         // copy offer data
374         copied = malloc(sizeof(*copied), M_DEVBUF, M_NOWAIT);
375         if (copied == NULL) {
376                 printf("fail to allocate memory\n");
377                 return;
378         }
379
380         memcpy(copied, hdr, sizeof(*copied));
381         hv_queue_work_item(vmbus_channel_on_offer_internal, copied);
382 }
383
384 static void
385 vmbus_channel_on_offer_internal(void* context)
386 {
387         hv_vmbus_channel* new_channel;
388
389         hv_vmbus_channel_offer_channel* offer = (hv_vmbus_channel_offer_channel*)context;
390         /* Allocate the channel object and save this offer */
391         new_channel = hv_vmbus_allocate_channel();
392
393         /*
394          * By default we setup state to enable batched
395          * reading. A specific service can choose to
396          * disable this prior to opening the channel.
397          */
398         new_channel->batched_reading = TRUE;
399
400         new_channel->signal_event_param =
401             (hv_vmbus_input_signal_event *)
402             (HV_ALIGN_UP((unsigned long)
403                 &new_channel->signal_event_buffer,
404                 HV_HYPERCALL_PARAM_ALIGN));
405
406         new_channel->signal_event_param->connection_id.as_uint32_t = 0; 
407         new_channel->signal_event_param->connection_id.u.id =
408             HV_VMBUS_EVENT_CONNECTION_ID;
409         new_channel->signal_event_param->flag_number = 0;
410         new_channel->signal_event_param->rsvd_z = 0;
411
412         if (hv_vmbus_protocal_version != HV_VMBUS_VERSION_WS2008) {
413                 new_channel->is_dedicated_interrupt =
414                     (offer->is_dedicated_interrupt != 0);
415                 new_channel->signal_event_param->connection_id.u.id =
416                     offer->connection_id;
417         }
418
419         memcpy(&new_channel->offer_msg, offer,
420             sizeof(hv_vmbus_channel_offer_channel));
421         new_channel->monitor_group = (uint8_t) offer->monitor_id / 32;
422         new_channel->monitor_bit = (uint8_t) offer->monitor_id % 32;
423
424         /* Select default cpu for this channel. */
425         vmbus_channel_select_defcpu(new_channel);
426
427         vmbus_channel_process_offer(new_channel);
428
429         free(offer, M_DEVBUF);
430 }
431
432 /**
433  * @brief Rescind offer handler.
434  *
435  * We queue a work item to process this offer
436  * synchronously
437  */
438 static void
439 vmbus_channel_on_offer_rescind(hv_vmbus_channel_msg_header* hdr)
440 {
441         hv_vmbus_channel_rescind_offer* rescind;
442         hv_vmbus_channel*               channel;
443
444         rescind = (hv_vmbus_channel_rescind_offer*) hdr;
445
446         channel = hv_vmbus_g_connection.channels[rescind->child_rel_id];
447         if (channel == NULL)
448             return;
449
450         hv_queue_work_item(vmbus_channel_on_offer_rescind_internal, channel);
451         hv_vmbus_g_connection.channels[rescind->child_rel_id] = NULL;
452 }
453
454 static void
455 vmbus_channel_on_offer_rescind_internal(void *context)
456 {
457         hv_vmbus_channel*               channel;
458
459         channel = (hv_vmbus_channel*)context;
460         if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
461                 /* Only primary channel owns the hv_device */
462                 hv_vmbus_child_device_unregister(channel->device);
463         }
464 }
465
466 /**
467  *
468  * @brief Invoked when all offers have been delivered.
469  */
470 static void
471 vmbus_channel_on_offers_delivered(hv_vmbus_channel_msg_header* hdr)
472 {
473 }
474
475 /**
476  * @brief Open result handler.
477  *
478  * This is invoked when we received a response
479  * to our channel open request. Find the matching request, copy the
480  * response and signal the requesting thread.
481  */
482 static void
483 vmbus_channel_on_open_result(hv_vmbus_channel_msg_header* hdr)
484 {
485         hv_vmbus_channel_open_result*   result;
486         hv_vmbus_channel_msg_info*      msg_info;
487         hv_vmbus_channel_msg_header*    requestHeader;
488         hv_vmbus_channel_open_channel*  openMsg;
489
490         result = (hv_vmbus_channel_open_result*) hdr;
491
492         /*
493          * Find the open msg, copy the result and signal/unblock the wait event
494          */
495         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
496
497         TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
498             msg_list_entry) {
499             requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
500
501             if (requestHeader->message_type ==
502                     HV_CHANNEL_MESSAGE_OPEN_CHANNEL) {
503                 openMsg = (hv_vmbus_channel_open_channel*) msg_info->msg;
504                 if (openMsg->child_rel_id == result->child_rel_id
505                     && openMsg->open_id == result->open_id) {
506                     memcpy(&msg_info->response.open_result, result,
507                         sizeof(hv_vmbus_channel_open_result));
508                     sema_post(&msg_info->wait_sema);
509                     break;
510                 }
511             }
512         }
513         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
514
515 }
516
517 /**
518  * @brief GPADL created handler.
519  *
520  * This is invoked when we received a response
521  * to our gpadl create request. Find the matching request, copy the
522  * response and signal the requesting thread.
523  */
524 static void
525 vmbus_channel_on_gpadl_created(hv_vmbus_channel_msg_header* hdr)
526 {
527         hv_vmbus_channel_gpadl_created*         gpadl_created;
528         hv_vmbus_channel_msg_info*              msg_info;
529         hv_vmbus_channel_msg_header*            request_header;
530         hv_vmbus_channel_gpadl_header*          gpadl_header;
531
532         gpadl_created = (hv_vmbus_channel_gpadl_created*) hdr;
533
534         /* Find the establish msg, copy the result and signal/unblock
535          * the wait event
536          */
537         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
538         TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
539                 msg_list_entry) {
540             request_header = (hv_vmbus_channel_msg_header*) msg_info->msg;
541             if (request_header->message_type ==
542                     HV_CHANNEL_MESSAGEL_GPADL_HEADER) {
543                 gpadl_header =
544                     (hv_vmbus_channel_gpadl_header*) request_header;
545
546                 if ((gpadl_created->child_rel_id == gpadl_header->child_rel_id)
547                     && (gpadl_created->gpadl == gpadl_header->gpadl)) {
548                     memcpy(&msg_info->response.gpadl_created,
549                         gpadl_created,
550                         sizeof(hv_vmbus_channel_gpadl_created));
551                     sema_post(&msg_info->wait_sema);
552                     break;
553                 }
554             }
555         }
556         mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
557 }
558
559 /**
560  * @brief GPADL torndown handler.
561  *
562  * This is invoked when we received a respons
563  * to our gpadl teardown request. Find the matching request, copy the
564  * response and signal the requesting thread
565  */
566 static void
567 vmbus_channel_on_gpadl_torndown(hv_vmbus_channel_msg_header* hdr)
568 {
569         hv_vmbus_channel_gpadl_torndown*        gpadl_torndown;
570         hv_vmbus_channel_msg_info*              msg_info;
571         hv_vmbus_channel_msg_header*            requestHeader;
572         hv_vmbus_channel_gpadl_teardown*        gpadlTeardown;
573
574         gpadl_torndown = (hv_vmbus_channel_gpadl_torndown*)hdr;
575
576         /*
577          * Find the open msg, copy the result and signal/unblock the
578          * wait event.
579          */
580
581         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
582
583         TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
584                 msg_list_entry) {
585             requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
586
587             if (requestHeader->message_type
588                     == HV_CHANNEL_MESSAGE_GPADL_TEARDOWN) {
589                 gpadlTeardown =
590                     (hv_vmbus_channel_gpadl_teardown*) requestHeader;
591
592                 if (gpadl_torndown->gpadl == gpadlTeardown->gpadl) {
593                     memcpy(&msg_info->response.gpadl_torndown,
594                         gpadl_torndown,
595                         sizeof(hv_vmbus_channel_gpadl_torndown));
596                     sema_post(&msg_info->wait_sema);
597                     break;
598                 }
599             }
600         }
601     mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
602 }
603
604 /**
605  * @brief Version response handler.
606  *
607  * This is invoked when we received a response
608  * to our initiate contact request. Find the matching request, copy th
609  * response and signal the requesting thread.
610  */
611 static void
612 vmbus_channel_on_version_response(hv_vmbus_channel_msg_header* hdr)
613 {
614         hv_vmbus_channel_msg_info*              msg_info;
615         hv_vmbus_channel_msg_header*            requestHeader;
616         hv_vmbus_channel_initiate_contact*      initiate;
617         hv_vmbus_channel_version_response*      versionResponse;
618
619         versionResponse = (hv_vmbus_channel_version_response*)hdr;
620
621         mtx_lock_spin(&hv_vmbus_g_connection.channel_msg_lock);
622         TAILQ_FOREACH(msg_info, &hv_vmbus_g_connection.channel_msg_anchor,
623             msg_list_entry) {
624             requestHeader = (hv_vmbus_channel_msg_header*) msg_info->msg;
625             if (requestHeader->message_type
626                 == HV_CHANNEL_MESSAGE_INITIATED_CONTACT) {
627                 initiate =
628                     (hv_vmbus_channel_initiate_contact*) requestHeader;
629                 memcpy(&msg_info->response.version_response,
630                     versionResponse,
631                     sizeof(hv_vmbus_channel_version_response));
632                 sema_post(&msg_info->wait_sema);
633             }
634         }
635     mtx_unlock_spin(&hv_vmbus_g_connection.channel_msg_lock);
636
637 }
638
639 /**
640  *  @brief Send a request to get all our pending offers.
641  */
642 int
643 hv_vmbus_request_channel_offers(void)
644 {
645         int                             ret;
646         hv_vmbus_channel_msg_header*    msg;
647         hv_vmbus_channel_msg_info*      msg_info;
648
649         msg_info = (hv_vmbus_channel_msg_info *)
650             malloc(sizeof(hv_vmbus_channel_msg_info)
651                     + sizeof(hv_vmbus_channel_msg_header), M_DEVBUF, M_NOWAIT);
652
653         if (msg_info == NULL) {
654             if(bootverbose)
655                 printf("Error VMBUS: malloc failed for Request Offers\n");
656             return (ENOMEM);
657         }
658
659         msg = (hv_vmbus_channel_msg_header*) msg_info->msg;
660         msg->message_type = HV_CHANNEL_MESSAGE_REQUEST_OFFERS;
661
662         ret = hv_vmbus_post_message(msg, sizeof(hv_vmbus_channel_msg_header));
663
664         free(msg_info, M_DEVBUF);
665
666         return (ret);
667 }
668
669 /**
670  * @brief Release channels that are unattached/unconnected (i.e., no drivers associated)
671  */
672 void
673 hv_vmbus_release_unattached_channels(void) 
674 {
675         hv_vmbus_channel *channel;
676
677         mtx_lock(&hv_vmbus_g_connection.channel_lock);
678
679         while (!TAILQ_EMPTY(&hv_vmbus_g_connection.channel_anchor)) {
680             channel = TAILQ_FIRST(&hv_vmbus_g_connection.channel_anchor);
681             TAILQ_REMOVE(&hv_vmbus_g_connection.channel_anchor,
682                             channel, list_entry);
683
684             if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
685                 /* Only primary channel owns the hv_device */
686                 hv_vmbus_child_device_unregister(channel->device);
687             }
688             hv_vmbus_free_vmbus_channel(channel);
689         }
690         bzero(hv_vmbus_g_connection.channels, 
691                 sizeof(hv_vmbus_channel*) * HV_CHANNEL_MAX_COUNT);
692         mtx_unlock(&hv_vmbus_g_connection.channel_lock);
693 }
694
695 /**
696  * @brief Select the best outgoing channel
697  * 
698  * The channel whose vcpu binding is closest to the currect vcpu will
699  * be selected.
700  * If no multi-channel, always select primary channel
701  * 
702  * @param primary - primary channel
703  */
704 struct hv_vmbus_channel *
705 vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary)
706 {
707         hv_vmbus_channel *new_channel = NULL;
708         hv_vmbus_channel *outgoing_channel = primary;
709         int old_cpu_distance = 0;
710         int new_cpu_distance = 0;
711         int cur_vcpu = 0;
712         int smp_pro_id = PCPU_GET(cpuid);
713
714         if (TAILQ_EMPTY(&primary->sc_list_anchor)) {
715                 return outgoing_channel;
716         }
717
718         if (smp_pro_id >= MAXCPU) {
719                 return outgoing_channel;
720         }
721
722         cur_vcpu = hv_vmbus_g_context.hv_vcpu_index[smp_pro_id];
723         
724         TAILQ_FOREACH(new_channel, &primary->sc_list_anchor, sc_list_entry) {
725                 if (new_channel->state != HV_CHANNEL_OPENED_STATE){
726                         continue;
727                 }
728
729                 if (new_channel->target_vcpu == cur_vcpu){
730                         return new_channel;
731                 }
732
733                 old_cpu_distance = ((outgoing_channel->target_vcpu > cur_vcpu) ?
734                     (outgoing_channel->target_vcpu - cur_vcpu) :
735                     (cur_vcpu - outgoing_channel->target_vcpu));
736
737                 new_cpu_distance = ((new_channel->target_vcpu > cur_vcpu) ?
738                     (new_channel->target_vcpu - cur_vcpu) :
739                     (cur_vcpu - new_channel->target_vcpu));
740
741                 if (old_cpu_distance < new_cpu_distance) {
742                         continue;
743                 }
744
745                 outgoing_channel = new_channel;
746         }
747
748         return(outgoing_channel);
749 }