1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*-
* Copyright (c) 2009-2012,2016 Microsoft Corp.
* Copyright (c) 2012 NetApp Inc.
* Copyright (c) 2012 Citrix Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <dev/hyperv/include/hyperv_busdma.h>
#include <dev/hyperv/vmbus/hv_vmbus_priv.h>
#include <dev/hyperv/vmbus/vmbus_reg.h>
#include <dev/hyperv/vmbus/vmbus_var.h>
typedef void (*vmbus_chanmsg_proc_t)
(struct vmbus_softc *, const struct vmbus_message *);
static struct hv_vmbus_channel *hv_vmbus_allocate_channel(struct vmbus_softc *);
static void vmbus_channel_on_offer_internal(struct vmbus_softc *,
const hv_vmbus_channel_offer_channel *offer);
static void vmbus_chan_detach_task(void *, int);
static void vmbus_channel_on_offer(struct vmbus_softc *,
const struct vmbus_message *);
static void vmbus_channel_on_offer_rescind(struct vmbus_softc *,
const struct vmbus_message *);
static void vmbus_channel_on_offers_delivered(struct vmbus_softc *,
const struct vmbus_message *);
/**
* Channel message dispatch table
*/
static const vmbus_chanmsg_proc_t
vmbus_chanmsg_process[HV_CHANNEL_MESSAGE_COUNT] = {
[HV_CHANNEL_MESSAGE_OFFER_CHANNEL] =
vmbus_channel_on_offer,
[HV_CHANNEL_MESSAGE_RESCIND_CHANNEL_OFFER] =
vmbus_channel_on_offer_rescind,
[HV_CHANNEL_MESSAGE_ALL_OFFERS_DELIVERED] =
vmbus_channel_on_offers_delivered,
[HV_CHANNEL_MESSAGE_OPEN_CHANNEL_RESULT] =
vmbus_msghc_wakeup,
[HV_CHANNEL_MESSAGE_GPADL_CREATED] =
vmbus_msghc_wakeup,
[HV_CHANNEL_MESSAGE_GPADL_TORNDOWN] =
vmbus_msghc_wakeup,
[HV_CHANNEL_MESSAGE_VERSION_RESPONSE] =
vmbus_msghc_wakeup
};
/**
* @brief Allocate and initialize a vmbus channel object
*/
static struct hv_vmbus_channel *
hv_vmbus_allocate_channel(struct vmbus_softc *sc)
{
struct hv_vmbus_channel *channel;
channel = malloc(sizeof(*channel), M_DEVBUF, M_WAITOK | M_ZERO);
channel->vmbus_sc = sc;
mtx_init(&channel->sc_lock, "vmbus multi channel", NULL, MTX_DEF);
TAILQ_INIT(&channel->sc_list_anchor);
TASK_INIT(&channel->ch_detach_task, 0, vmbus_chan_detach_task, channel);
return (channel);
}
/**
* @brief Release the resources used by the vmbus channel object
*/
void
hv_vmbus_free_vmbus_channel(hv_vmbus_channel* channel)
{
mtx_destroy(&channel->sc_lock);
free(channel, M_DEVBUF);
}
/**
* @brief Process the offer by creating a channel/device
* associated with this offer
*/
static void
vmbus_channel_process_offer(hv_vmbus_channel *new_channel)
{
struct vmbus_softc *sc = new_channel->vmbus_sc;
hv_vmbus_channel* channel;
uint32_t relid;
relid = new_channel->ch_id;
/*
* Make sure this is a new offer
*/
mtx_lock(&sc->vmbus_chlist_lock);
if (relid == 0) {
/*
* XXX channel0 will not be processed; skip it.
*/
printf("VMBUS: got channel0 offer\n");
} else {
sc->vmbus_chmap[relid] = new_channel;
}
TAILQ_FOREACH(channel, &sc->vmbus_chlist, ch_link) {
if (memcmp(&channel->ch_guid_type, &new_channel->ch_guid_type,
sizeof(hv_guid)) == 0 &&
memcmp(&channel->ch_guid_inst, &new_channel->ch_guid_inst,
sizeof(hv_guid)) == 0)
break;
}
if (channel == NULL) {
/* Install the new primary channel */
TAILQ_INSERT_TAIL(&sc->vmbus_chlist, new_channel, ch_link);
}
mtx_unlock(&sc->vmbus_chlist_lock);
if (bootverbose) {
char logstr[64];
logstr[0] = '\0';
if (channel != NULL) {
snprintf(logstr, sizeof(logstr), ", primary chan%u",
channel->ch_id);
}
device_printf(sc->vmbus_dev, "chan%u subchanid%u offer%s\n",
new_channel->ch_id,
new_channel->ch_subidx, logstr);
}
if (channel != NULL) {
/*
* Check if this is a sub channel.
*/
if (new_channel->ch_subidx != 0) {
/*
* It is a sub channel offer, process it.
*/
new_channel->primary_channel = channel;
new_channel->ch_dev = channel->ch_dev;
mtx_lock(&channel->sc_lock);
TAILQ_INSERT_TAIL(&channel->sc_list_anchor,
new_channel, sc_list_entry);
mtx_unlock(&channel->sc_lock);
/*
* Insert the new channel to the end of the global
* channel list.
*
* NOTE:
* The new sub-channel MUST be inserted AFTER it's
* primary channel, so that the primary channel will
* be found in the above loop for its baby siblings.
*/
mtx_lock(&sc->vmbus_chlist_lock);
TAILQ_INSERT_TAIL(&sc->vmbus_chlist, new_channel,
ch_link);
mtx_unlock(&sc->vmbus_chlist_lock);
new_channel->state = HV_CHANNEL_OPEN_STATE;
/*
* Bump up sub-channel count and notify anyone that is
* interested in this sub-channel, after this sub-channel
* is setup.
*/
mtx_lock(&channel->sc_lock);
channel->subchan_cnt++;
mtx_unlock(&channel->sc_lock);
wakeup(channel);
return;
}
printf("VMBUS: duplicated primary channel%u\n",
new_channel->ch_id);
hv_vmbus_free_vmbus_channel(new_channel);
return;
}
new_channel->state = HV_CHANNEL_OPEN_STATE;
/*
* Add the new device to the bus. This will kick off device-driver
* binding which eventually invokes the device driver's AddDevice()
* method.
*
* NOTE:
* Error is ignored here; don't have much to do if error really
* happens.
*/
hv_vmbus_child_device_register(new_channel);
}
void
vmbus_channel_cpu_set(struct hv_vmbus_channel *chan, int cpu)
{
KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu %d", cpu));
if (chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WS2008 ||
chan->vmbus_sc->vmbus_version == VMBUS_VERSION_WIN7) {
/* Only cpu0 is supported */
cpu = 0;
}
chan->target_cpu = cpu;
chan->target_vcpu = VMBUS_PCPU_GET(chan->vmbus_sc, vcpuid, cpu);
if (bootverbose) {
printf("vmbus_chan%u: assigned to cpu%u [vcpu%u]\n",
chan->ch_id,
chan->target_cpu, chan->target_vcpu);
}
}
void
vmbus_channel_cpu_rr(struct hv_vmbus_channel *chan)
{
static uint32_t vmbus_chan_nextcpu;
int cpu;
cpu = atomic_fetchadd_int(&vmbus_chan_nextcpu, 1) % mp_ncpus;
vmbus_channel_cpu_set(chan, cpu);
}
static void
vmbus_channel_select_defcpu(struct hv_vmbus_channel *chan)
{
/*
* By default, pin the channel to cpu0. Devices having
* special channel-cpu mapping requirement should call
* vmbus_channel_cpu_{set,rr}().
*/
vmbus_channel_cpu_set(chan, 0);
}
/**
* @brief Handler for channel offers from Hyper-V/Azure
*
* Handler for channel offers from vmbus in parent partition.
*/
static void
vmbus_channel_on_offer(struct vmbus_softc *sc, const struct vmbus_message *msg)
{
const hv_vmbus_channel_offer_channel *offer;
/* New channel is offered by vmbus */
vmbus_scan_newchan(sc);
offer = (const hv_vmbus_channel_offer_channel *)msg->msg_data;
vmbus_channel_on_offer_internal(sc, offer);
}
static void
vmbus_channel_on_offer_internal(struct vmbus_softc *sc,
const hv_vmbus_channel_offer_channel *offer)
{
hv_vmbus_channel* new_channel;
/* Allocate the channel object and save this offer */
new_channel = hv_vmbus_allocate_channel(sc);
new_channel->ch_id = offer->child_rel_id;
new_channel->ch_subidx = offer->offer.sub_channel_index;
if (offer->monitor_allocated)
new_channel->ch_flags |= VMBUS_CHAN_FLAG_HASMNF;
new_channel->ch_guid_type = offer->offer.interface_type;
new_channel->ch_guid_inst = offer->offer.interface_instance;
/*
* By default we setup state to enable batched
* reading. A specific service can choose to
* disable this prior to opening the channel.
*/
new_channel->batched_reading = TRUE;
new_channel->ch_sigevt = hyperv_dmamem_alloc(
bus_get_dma_tag(sc->vmbus_dev),
HYPERCALL_SIGEVTIN_ALIGN, 0, sizeof(struct hypercall_sigevt_in),
&new_channel->ch_sigevt_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
if (new_channel->ch_sigevt == NULL) {
device_printf(sc->vmbus_dev, "sigevt alloc failed\n");
/* XXX */
mtx_destroy(&new_channel->sc_lock);
free(new_channel, M_DEVBUF);
return;
}
new_channel->ch_sigevt->hc_connid = VMBUS_CONNID_EVENT;
if (sc->vmbus_version != VMBUS_VERSION_WS2008) {
new_channel->is_dedicated_interrupt =
(offer->is_dedicated_interrupt != 0);
new_channel->ch_sigevt->hc_connid = offer->connection_id;
}
new_channel->monitor_group = (uint8_t) offer->monitor_id / 32;
new_channel->monitor_bit = (uint8_t) offer->monitor_id % 32;
/* Select default cpu for this channel. */
vmbus_channel_select_defcpu(new_channel);
vmbus_channel_process_offer(new_channel);
}
/**
* @brief Rescind offer handler.
*
* We queue a work item to process this offer
* synchronously.
*
* XXX pretty broken; need rework.
*/
static void
vmbus_channel_on_offer_rescind(struct vmbus_softc *sc,
const struct vmbus_message *msg)
{
const hv_vmbus_channel_rescind_offer *rescind;
hv_vmbus_channel* channel;
rescind = (const hv_vmbus_channel_rescind_offer *)msg->msg_data;
if (bootverbose) {
device_printf(sc->vmbus_dev, "chan%u rescind\n",
rescind->child_rel_id);
}
channel = sc->vmbus_chmap[rescind->child_rel_id];
if (channel == NULL)
return;
sc->vmbus_chmap[rescind->child_rel_id] = NULL;
taskqueue_enqueue(taskqueue_thread, &channel->ch_detach_task);
}
static void
vmbus_chan_detach_task(void *xchan, int pending __unused)
{
struct hv_vmbus_channel *chan = xchan;
if (HV_VMBUS_CHAN_ISPRIMARY(chan)) {
/* Only primary channel owns the device */
hv_vmbus_child_device_unregister(chan);
/* NOTE: DO NOT free primary channel for now */
} else {
struct vmbus_softc *sc = chan->vmbus_sc;
struct hv_vmbus_channel *pri_chan = chan->primary_channel;
struct vmbus_chanmsg_chfree *req;
struct vmbus_msghc *mh;
int error;
mh = vmbus_msghc_get(sc, sizeof(*req));
if (mh == NULL) {
device_printf(sc->vmbus_dev,
"can not get msg hypercall for chfree(chan%u)\n",
chan->ch_id);
goto remove;
}
req = vmbus_msghc_dataptr(mh);
req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHFREE;
req->chm_chanid = chan->ch_id;
error = vmbus_msghc_exec_noresult(mh);
vmbus_msghc_put(sc, mh);
if (error) {
device_printf(sc->vmbus_dev,
"chfree(chan%u) failed: %d",
chan->ch_id, error);
/* NOTE: Move on! */
} else {
if (bootverbose) {
device_printf(sc->vmbus_dev, "chan%u freed\n",
chan->ch_id);
}
}
remove:
mtx_lock(&sc->vmbus_chlist_lock);
TAILQ_REMOVE(&sc->vmbus_chlist, chan, ch_link);
mtx_unlock(&sc->vmbus_chlist_lock);
mtx_lock(&pri_chan->sc_lock);
TAILQ_REMOVE(&pri_chan->sc_list_anchor, chan, sc_list_entry);
KASSERT(pri_chan->subchan_cnt > 0,
("invalid subchan_cnt %d", pri_chan->subchan_cnt));
pri_chan->subchan_cnt--;
mtx_unlock(&pri_chan->sc_lock);
wakeup(pri_chan);
hv_vmbus_free_vmbus_channel(chan);
}
}
/**
*
* @brief Invoked when all offers have been delivered.
*/
static void
vmbus_channel_on_offers_delivered(struct vmbus_softc *sc,
const struct vmbus_message *msg __unused)
{
/* No more new channels for the channel request. */
vmbus_scan_done(sc);
}
/**
* @brief Release channels that are unattached/unconnected (i.e., no drivers associated)
*/
void
hv_vmbus_release_unattached_channels(struct vmbus_softc *sc)
{
hv_vmbus_channel *channel;
mtx_lock(&sc->vmbus_chlist_lock);
while (!TAILQ_EMPTY(&sc->vmbus_chlist)) {
channel = TAILQ_FIRST(&sc->vmbus_chlist);
TAILQ_REMOVE(&sc->vmbus_chlist, channel, ch_link);
if (HV_VMBUS_CHAN_ISPRIMARY(channel)) {
/* Only primary channel owns the device */
hv_vmbus_child_device_unregister(channel);
}
hv_vmbus_free_vmbus_channel(channel);
}
bzero(sc->vmbus_chmap,
sizeof(struct hv_vmbus_channel *) * VMBUS_CHAN_MAX);
mtx_unlock(&sc->vmbus_chlist_lock);
}
/**
* @brief Select the best outgoing channel
*
* The channel whose vcpu binding is closest to the currect vcpu will
* be selected.
* If no multi-channel, always select primary channel
*
* @param primary - primary channel
*/
struct hv_vmbus_channel *
vmbus_select_outgoing_channel(struct hv_vmbus_channel *primary)
{
hv_vmbus_channel *new_channel = NULL;
hv_vmbus_channel *outgoing_channel = primary;
int old_cpu_distance = 0;
int new_cpu_distance = 0;
int cur_vcpu = 0;
int smp_pro_id = PCPU_GET(cpuid);
if (TAILQ_EMPTY(&primary->sc_list_anchor)) {
return outgoing_channel;
}
if (smp_pro_id >= MAXCPU) {
return outgoing_channel;
}
cur_vcpu = VMBUS_PCPU_GET(primary->vmbus_sc, vcpuid, smp_pro_id);
TAILQ_FOREACH(new_channel, &primary->sc_list_anchor, sc_list_entry) {
if (new_channel->state != HV_CHANNEL_OPENED_STATE){
continue;
}
if (new_channel->target_vcpu == cur_vcpu){
return new_channel;
}
old_cpu_distance = ((outgoing_channel->target_vcpu > cur_vcpu) ?
(outgoing_channel->target_vcpu - cur_vcpu) :
(cur_vcpu - outgoing_channel->target_vcpu));
new_cpu_distance = ((new_channel->target_vcpu > cur_vcpu) ?
(new_channel->target_vcpu - cur_vcpu) :
(cur_vcpu - new_channel->target_vcpu));
if (old_cpu_distance < new_cpu_distance) {
continue;
}
outgoing_channel = new_channel;
}
return(outgoing_channel);
}
struct hv_vmbus_channel **
vmbus_get_subchan(struct hv_vmbus_channel *pri_chan, int subchan_cnt)
{
struct hv_vmbus_channel **ret, *chan;
int i;
ret = malloc(subchan_cnt * sizeof(struct hv_vmbus_channel *), M_TEMP,
M_WAITOK);
mtx_lock(&pri_chan->sc_lock);
while (pri_chan->subchan_cnt < subchan_cnt)
mtx_sleep(pri_chan, &pri_chan->sc_lock, 0, "subch", 0);
i = 0;
TAILQ_FOREACH(chan, &pri_chan->sc_list_anchor, sc_list_entry) {
/* TODO: refcnt chan */
ret[i] = chan;
++i;
if (i == subchan_cnt)
break;
}
KASSERT(i == subchan_cnt, ("invalid subchan count %d, should be %d",
pri_chan->subchan_cnt, subchan_cnt));
mtx_unlock(&pri_chan->sc_lock);
return ret;
}
void
vmbus_rel_subchan(struct hv_vmbus_channel **subchan, int subchan_cnt __unused)
{
free(subchan, M_TEMP);
}
void
vmbus_drain_subchan(struct hv_vmbus_channel *pri_chan)
{
mtx_lock(&pri_chan->sc_lock);
while (pri_chan->subchan_cnt > 0)
mtx_sleep(pri_chan, &pri_chan->sc_lock, 0, "dsubch", 0);
mtx_unlock(&pri_chan->sc_lock);
}
void
vmbus_chan_msgproc(struct vmbus_softc *sc, const struct vmbus_message *msg)
{
vmbus_chanmsg_proc_t msg_proc;
uint32_t msg_type;
msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
if (msg_type >= HV_CHANNEL_MESSAGE_COUNT) {
device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
msg_type);
return;
}
msg_proc = vmbus_chanmsg_process[msg_type];
if (msg_proc != NULL)
msg_proc(sc, msg);
}
|