1 /*- 2 * Copyright (c) 2009-2012 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * Copyright (c) 2016 Mike Belopuhov <mike@esdenera.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * The OpenBSD port was done under funding by Esdenera Networks GmbH. 32 */ 33 34 #include <sys/param.h> 35 36 /* Hyperv requires locked atomic operations */ 37 #ifndef MULTIPROCESSOR 38 #define _HYPERVMPATOMICS 39 #define MULTIPROCESSOR 40 #endif 41 #include <sys/atomic.h> 42 #ifdef _HYPERVMPATOMICS 43 #undef MULTIPROCESSOR 44 #undef _HYPERVMPATOMICS 45 #endif 46 47 #include <sys/systm.h> 48 #include <sys/proc.h> 49 #include <sys/signal.h> 50 #include <sys/signalvar.h> 51 #include <sys/malloc.h> 52 #include <sys/kernel.h> 53 #include <sys/device.h> 54 #include <sys/timetc.h> 55 #include <sys/task.h> 56 #include <sys/syslog.h> 57 58 #include <machine/bus.h> 59 #include <machine/cpu.h> 60 #include <machine/cpufunc.h> 61 62 #include <uvm/uvm_extern.h> 63 64 #include <machine/i82489var.h> 65 66 #include <dev/pv/pvvar.h> 67 #include <dev/pv/pvreg.h> 68 #include <dev/pv/hypervreg.h> 69 #include <dev/pv/hypervvar.h> 70 71 /* Command submission flags */ 72 #define HCF_SLEEPOK 0x0001 /* M_WAITOK */ 73 #define HCF_NOSLEEP 0x0002 /* M_NOWAIT */ 74 #define HCF_NOREPLY 0x0004 75 76 struct hv_softc *hv_sc; 77 78 int hv_match(struct device *, void *, void *); 79 void hv_attach(struct device *, struct device *, void *); 80 void hv_set_version(struct hv_softc *); 81 u_int hv_gettime(struct timecounter *); 82 int hv_init_hypercall(struct hv_softc *); 83 uint64_t hv_hypercall(struct hv_softc *, uint64_t, void *, void *); 84 int hv_init_interrupts(struct hv_softc *); 85 int hv_init_synic(struct hv_softc *); 86 int hv_cmd(struct hv_softc *, void *, size_t, void *, size_t, int); 87 int hv_start(struct hv_softc *, struct hv_msg *); 88 int hv_reply(struct hv_softc *, struct hv_msg *); 89 void hv_wait(struct hv_softc *, int (*done)(struct hv_softc *, 90 struct hv_msg *), struct hv_msg *, void *, const char *); 91 uint16_t hv_intr_signal(struct hv_softc *, void *); 92 void hv_intr(void); 93 void hv_event_intr(struct hv_softc *); 94 void hv_message_intr(struct hv_softc *); 95 int hv_vmbus_connect(struct hv_softc *); 96 void hv_channel_response(struct hv_softc *, struct vmbus_chanmsg_hdr *); 97 void hv_channel_offer(struct hv_softc *, struct vmbus_chanmsg_hdr *); 98 void hv_channel_rescind(struct hv_softc *, struct vmbus_chanmsg_hdr *); 99 void hv_channel_delivered(struct hv_softc *, struct vmbus_chanmsg_hdr *); 100 int hv_channel_scan(struct hv_softc *); 101 void hv_process_offer(struct hv_softc *, struct hv_offer *); 102 struct hv_channel * 103 hv_channel_lookup(struct hv_softc *, uint32_t); 104 int hv_channel_ring_create(struct hv_channel *, uint32_t); 105 void hv_channel_ring_destroy(struct hv_channel *); 106 void hv_channel_pause(struct hv_channel *); 107 uint hv_channel_unpause(struct hv_channel *); 108 uint hv_channel_ready(struct hv_channel *); 109 extern void hv_attach_icdevs(struct hv_softc *); 110 int hv_attach_devices(struct hv_softc *); 111 112 struct { 113 int hmd_response; 114 int hmd_request; 115 void (*hmd_handler)(struct hv_softc *, 116 struct vmbus_chanmsg_hdr *); 117 } hv_msg_dispatch[] = { 118 { 0, 0, NULL }, 119 { VMBUS_CHANMSG_CHOFFER, 0, hv_channel_offer }, 120 { VMBUS_CHANMSG_CHRESCIND, 0, hv_channel_rescind }, 121 { VMBUS_CHANMSG_CHREQUEST, VMBUS_CHANMSG_CHOFFER, 122 NULL }, 123 { VMBUS_CHANMSG_CHOFFER_DONE, 0, 124 hv_channel_delivered }, 125 { VMBUS_CHANMSG_CHOPEN, 0, NULL }, 126 { VMBUS_CHANMSG_CHOPEN_RESP, VMBUS_CHANMSG_CHOPEN, 127 hv_channel_response }, 128 { VMBUS_CHANMSG_CHCLOSE, 0, NULL }, 129 { VMBUS_CHANMSG_GPADL_CONN, 0, NULL }, 130 { VMBUS_CHANMSG_GPADL_SUBCONN, 0, NULL }, 131 { VMBUS_CHANMSG_GPADL_CONNRESP, VMBUS_CHANMSG_GPADL_CONN, 132 hv_channel_response }, 133 { VMBUS_CHANMSG_GPADL_DISCONN, 0, NULL }, 134 { VMBUS_CHANMSG_GPADL_DISCONNRESP, VMBUS_CHANMSG_GPADL_DISCONN, 135 hv_channel_response }, 136 { VMBUS_CHANMSG_CHFREE, 0, NULL }, 137 { VMBUS_CHANMSG_CONNECT, 0, NULL }, 138 { VMBUS_CHANMSG_CONNECT_RESP, VMBUS_CHANMSG_CONNECT, 139 hv_channel_response }, 140 { VMBUS_CHANMSG_DISCONNECT, 0, NULL }, 141 }; 142 143 struct timecounter hv_timecounter = { 144 hv_gettime, 0, 0xffffffff, 10000000, "hyperv", 9001, NULL, 0 145 }; 146 147 struct cfdriver hyperv_cd = { 148 NULL, "hyperv", DV_DULL 149 }; 150 151 const struct cfattach hyperv_ca = { 152 sizeof(struct hv_softc), hv_match, hv_attach 153 }; 154 155 const struct hv_guid hv_guid_network = { 156 { 0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46, 157 0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e } 158 }; 159 160 const struct hv_guid hv_guid_ide = { 161 { 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, 162 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 } 163 }; 164 165 const struct hv_guid hv_guid_scsi = { 166 { 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 167 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f } 168 }; 169 170 const struct hv_guid hv_guid_shutdown = { 171 { 0x31, 0x60, 0x0b, 0x0e, 0x13, 0x52, 0x34, 0x49, 172 0x81, 0x8b, 0x38, 0xd9, 0x0c, 0xed, 0x39, 0xdb } 173 }; 174 175 const struct hv_guid hv_guid_timesync = { 176 { 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49, 177 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } 178 }; 179 180 const struct hv_guid hv_guid_heartbeat = { 181 { 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e, 182 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d } 183 }; 184 185 const struct hv_guid hv_guid_kvp = { 186 { 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d, 187 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x03, 0xe6 } 188 }; 189 190 #ifdef HYPERV_DEBUG 191 const struct hv_guid hv_guid_vss = { 192 { 0x29, 0x2e, 0xfa, 0x35, 0x23, 0xea, 0x36, 0x42, 193 0x96, 0xae, 0x3a, 0x6e, 0xba, 0xcb, 0xa4, 0x40 } 194 }; 195 196 const struct hv_guid hv_guid_dynmem = { 197 { 0xdc, 0x74, 0x50, 0x52, 0x85, 0x89, 0xe2, 0x46, 198 0x80, 0x57, 0xa3, 0x07, 0xdc, 0x18, 0xa5, 0x02 } 199 }; 200 201 const struct hv_guid hv_guid_mouse = { 202 { 0x9e, 0xb6, 0xa8, 0xcf, 0x4a, 0x5b, 0xc0, 0x4c, 203 0xb9, 0x8b, 0x8b, 0xa1, 0xa1, 0xf3, 0xf9, 0x5a } 204 }; 205 206 const struct hv_guid hv_guid_kbd = { 207 { 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48, 208 0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84 } 209 }; 210 211 const struct hv_guid hv_guid_video = { 212 { 0x02, 0x78, 0x0a, 0xda, 0x77, 0xe3, 0xac, 0x4a, 213 0x8e, 0x77, 0x05, 0x58, 0xeb, 0x10, 0x73, 0xf8 } 214 }; 215 216 const struct hv_guid hv_guid_fc = { 217 { 0x4a, 0xcc, 0x9b, 0x2f, 0x69, 0x00, 0xf3, 0x4a, 218 0xb7, 0x6b, 0x6f, 0xd0, 0xbe, 0x52, 0x8c, 0xda } 219 }; 220 221 const struct hv_guid hv_guid_fcopy = { 222 { 0xe3, 0x4b, 0xd1, 0x34, 0xe4, 0xde, 0xc8, 0x41, 223 0x9a, 0xe7, 0x6b, 0x17, 0x49, 0x77, 0xc1, 0x92 } 224 }; 225 226 const struct hv_guid hv_guid_pcie = { 227 { 0x1d, 0xf6, 0xc4, 0x44, 0x44, 0x44, 0x00, 0x44, 228 0x9d, 0x52, 0x80, 0x2e, 0x27, 0xed, 0xe1, 0x9f } 229 }; 230 231 const struct hv_guid hv_guid_netdir = { 232 { 0x3d, 0xaf, 0x2e, 0x8c, 0xa7, 0x32, 0x09, 0x4b, 233 0xab, 0x99, 0xbd, 0x1f, 0x1c, 0x86, 0xb5, 0x01 } 234 }; 235 236 const struct hv_guid hv_guid_rdesktop = { 237 { 0xf4, 0xac, 0x6a, 0x27, 0x15, 0xac, 0x6c, 0x42, 238 0x98, 0xdd, 0x75, 0x21, 0xad, 0x3f, 0x01, 0xfe } 239 }; 240 241 /* Automatic Virtual Machine Activation (AVMA) Services */ 242 const struct hv_guid hv_guid_avma1 = { 243 { 0x55, 0xb2, 0x87, 0x44, 0x8c, 0xb8, 0x3f, 0x40, 244 0xbb, 0x51, 0xd1, 0xf6, 0x9c, 0xf1, 0x7f, 0x87 } 245 }; 246 247 const struct hv_guid hv_guid_avma2 = { 248 { 0xf4, 0xba, 0x75, 0x33, 0x15, 0x9e, 0x30, 0x4b, 249 0xb7, 0x65, 0x67, 0xac, 0xb1, 0x0d, 0x60, 0x7b } 250 }; 251 252 const struct hv_guid hv_guid_avma3 = { 253 { 0xa0, 0x1f, 0x22, 0x99, 0xad, 0x24, 0xe2, 0x11, 254 0xbe, 0x98, 0x00, 0x1a, 0xa0, 0x1b, 0xbf, 0x6e } 255 }; 256 257 const struct hv_guid hv_guid_avma4 = { 258 { 0x16, 0x57, 0xe6, 0xf8, 0xb3, 0x3c, 0x06, 0x4a, 259 0x9a, 0x60, 0x18, 0x89, 0xc5, 0xcc, 0xca, 0xb5 } 260 }; 261 #endif /* HYPERV_DEBUG */ 262 263 int 264 hv_match(struct device *parent, void *match, void *aux) 265 { 266 struct pv_attach_args *pva = aux; 267 struct pvbus_hv *hv = &pva->pva_hv[PVBUS_HYPERV]; 268 269 if ((hv->hv_major == 0 && hv->hv_minor == 0) || hv->hv_base == 0) 270 return (0); 271 272 return (1); 273 } 274 275 void 276 hv_attach(struct device *parent, struct device *self, void *aux) 277 { 278 struct hv_softc *sc = (struct hv_softc *)self; 279 struct pv_attach_args *pva = aux; 280 struct pvbus_hv *hv = &pva->pva_hv[PVBUS_HYPERV]; 281 282 sc->sc_pvbus = hv; 283 sc->sc_dmat = pva->pva_dmat; 284 285 if (!(hv->hv_features & CPUID_HV_MSR_HYPERCALL) || 286 !(hv->hv_features & CPUID_HV_MSR_SYNIC)) { 287 printf(": not functional\n"); 288 return; 289 } 290 291 DPRINTF("\n"); 292 293 hv_set_version(sc); 294 295 if (hv->hv_features & CPUID_HV_MSR_TIME_REFCNT) 296 tc_init(&hv_timecounter); 297 298 if (hv_init_hypercall(sc)) 299 return; 300 301 /* Wire it up to the global */ 302 hv_sc = sc; 303 304 if (hv_init_interrupts(sc)) 305 return; 306 307 if (hv_vmbus_connect(sc)) 308 return; 309 310 DPRINTF("%s", sc->sc_dev.dv_xname); 311 printf(": protocol %d.%d, features %#x\n", 312 VMBUS_VERSION_MAJOR(sc->sc_proto), 313 VMBUS_VERSION_MINOR(sc->sc_proto), 314 hv->hv_features); 315 316 if (hv_channel_scan(sc)) 317 return; 318 319 /* Attach heartbeat, KVP and other "internal" services */ 320 hv_attach_icdevs(sc); 321 322 /* Attach devices with external drivers */ 323 hv_attach_devices(sc); 324 } 325 326 void 327 hv_set_version(struct hv_softc *sc) 328 { 329 uint64_t ver; 330 331 /* OpenBSD build date */ 332 ver = MSR_HV_GUESTID_OSTYPE_OPENBSD; 333 ver |= (uint64_t)OpenBSD << MSR_HV_GUESTID_VERSION_SHIFT; 334 wrmsr(MSR_HV_GUEST_OS_ID, ver); 335 } 336 337 u_int 338 hv_gettime(struct timecounter *tc) 339 { 340 u_int now = rdmsr(MSR_HV_TIME_REF_COUNT); 341 342 return (now); 343 } 344 345 int 346 hv_init_hypercall(struct hv_softc *sc) 347 { 348 extern void *hv_hypercall_page; 349 uint64_t msr; 350 paddr_t pa; 351 352 sc->sc_hc = &hv_hypercall_page; 353 354 if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_hc, &pa)) { 355 printf(": hypercall page PA extraction failed\n"); 356 return (-1); 357 } 358 359 msr = (atop(pa) << MSR_HV_HYPERCALL_PGSHIFT) | MSR_HV_HYPERCALL_ENABLE; 360 wrmsr(MSR_HV_HYPERCALL, msr); 361 362 if (!(rdmsr(MSR_HV_HYPERCALL) & MSR_HV_HYPERCALL_ENABLE)) { 363 printf(": failed to set up a hypercall page\n"); 364 return (-1); 365 } 366 367 return (0); 368 } 369 370 uint64_t 371 hv_hypercall(struct hv_softc *sc, uint64_t control, void *input, 372 void *output) 373 { 374 paddr_t input_pa = 0, output_pa = 0; 375 uint64_t status = 0; 376 377 if (input != NULL && 378 pmap_extract(pmap_kernel(), (vaddr_t)input, &input_pa) == 0) { 379 printf("%s: hypercall input PA extraction failed\n", 380 sc->sc_dev.dv_xname); 381 return (~HYPERCALL_STATUS_SUCCESS); 382 } 383 384 if (output != NULL && 385 pmap_extract(pmap_kernel(), (vaddr_t)output, &output_pa) == 0) { 386 printf("%s: hypercall output PA extraction failed\n", 387 sc->sc_dev.dv_xname); 388 return (~HYPERCALL_STATUS_SUCCESS); 389 } 390 391 #ifdef __amd64__ 392 __asm__ __volatile__ ("mov %0, %%r8" : : "r" (output_pa) : "r8"); 393 __asm__ __volatile__ ("call *%3" : "=a" (status) : "c" (control), 394 "d" (input_pa), "m" (sc->sc_hc)); 395 #else /* __i386__ */ 396 { 397 uint32_t control_hi = control >> 32; 398 uint32_t control_lo = control & 0xfffffffff; 399 uint32_t status_hi = 1; 400 uint32_t status_lo = 1; 401 402 __asm__ __volatile__ ("call *%8" : 403 "=d" (status_hi), "=a"(status_lo) : 404 "d" (control_hi), "a" (control_lo), 405 "b" (0), "c" (input_pa), "D" (0), "S" (output_pa), 406 "m" (sc->sc_hc)); 407 408 status = status_lo | ((uint64_t)status_hi << 32); 409 } 410 #endif /* __amd64__ */ 411 412 return (status); 413 } 414 415 int 416 hv_init_interrupts(struct hv_softc *sc) 417 { 418 struct cpu_info *ci = curcpu(); 419 int cpu = CPU_INFO_UNIT(ci); 420 421 sc->sc_idtvec = LAPIC_HYPERV_VECTOR; 422 423 TAILQ_INIT(&sc->sc_reqs); 424 mtx_init(&sc->sc_reqlck, IPL_NET); 425 426 TAILQ_INIT(&sc->sc_rsps); 427 mtx_init(&sc->sc_rsplck, IPL_NET); 428 429 sc->sc_simp[cpu] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait); 430 if (sc->sc_simp[cpu] == NULL) { 431 printf(": failed to allocate SIMP\n"); 432 return (-1); 433 } 434 435 sc->sc_siep[cpu] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait); 436 if (sc->sc_siep[cpu] == NULL) { 437 printf(": failed to allocate SIEP\n"); 438 km_free(sc->sc_simp[cpu], PAGE_SIZE, &kv_any, &kp_zero); 439 return (-1); 440 } 441 442 sc->sc_proto = VMBUS_VERSION_WS2008; 443 444 return (hv_init_synic(sc)); 445 } 446 447 int 448 hv_init_synic(struct hv_softc *sc) 449 { 450 struct cpu_info *ci = curcpu(); 451 int cpu = CPU_INFO_UNIT(ci); 452 uint64_t simp, siefp, sctrl, sint; 453 paddr_t pa; 454 455 /* 456 * Setup the Synic's message page 457 */ 458 if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_simp[cpu], &pa)) { 459 printf(": SIMP PA extraction failed\n"); 460 return (-1); 461 } 462 simp = rdmsr(MSR_HV_SIMP); 463 simp &= (1 << MSR_HV_SIMP_PGSHIFT) - 1; 464 simp |= (atop(pa) << MSR_HV_SIMP_PGSHIFT); 465 simp |= MSR_HV_SIMP_ENABLE; 466 wrmsr(MSR_HV_SIMP, simp); 467 468 /* 469 * Setup the Synic's event page 470 */ 471 if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_siep[cpu], &pa)) { 472 printf(": SIEP PA extraction failed\n"); 473 return (-1); 474 } 475 siefp = rdmsr(MSR_HV_SIEFP); 476 siefp &= (1<<MSR_HV_SIEFP_PGSHIFT) - 1; 477 siefp |= (atop(pa) << MSR_HV_SIEFP_PGSHIFT); 478 siefp |= MSR_HV_SIEFP_ENABLE; 479 wrmsr(MSR_HV_SIEFP, siefp); 480 481 /* 482 * Configure and unmask SINT for message and event flags 483 */ 484 sint = rdmsr(MSR_HV_SINT0 + VMBUS_SINT_MESSAGE); 485 sint = sc->sc_idtvec | MSR_HV_SINT_AUTOEOI | 486 (sint & MSR_HV_SINT_RSVD_MASK); 487 wrmsr(MSR_HV_SINT0 + VMBUS_SINT_MESSAGE, sint); 488 489 /* Enable the global synic bit */ 490 sctrl = rdmsr(MSR_HV_SCONTROL); 491 sctrl |= MSR_HV_SCTRL_ENABLE; 492 wrmsr(MSR_HV_SCONTROL, sctrl); 493 494 sc->sc_vcpus[cpu] = rdmsr(MSR_HV_VP_INDEX); 495 496 DPRINTF("vcpu%u: SIMP %#llx SIEFP %#llx SCTRL %#llx\n", 497 sc->sc_vcpus[cpu], simp, siefp, sctrl); 498 499 return (0); 500 } 501 502 int 503 hv_cmd(struct hv_softc *sc, void *cmd, size_t cmdlen, void *rsp, 504 size_t rsplen, int flags) 505 { 506 struct hv_msg msg; 507 int rv; 508 509 if (cmdlen > VMBUS_MSG_DSIZE_MAX) { 510 printf("%s: payload too large (%lu)\n", sc->sc_dev.dv_xname, 511 cmdlen); 512 return (EMSGSIZE); 513 } 514 515 memset(&msg, 0, sizeof(msg)); 516 517 msg.msg_req.hc_dsize = cmdlen; 518 memcpy(msg.msg_req.hc_data, cmd, cmdlen); 519 520 if (!(flags & HCF_NOREPLY)) { 521 msg.msg_rsp = rsp; 522 msg.msg_rsplen = rsplen; 523 } else 524 msg.msg_flags |= MSGF_NOQUEUE; 525 526 if (flags & HCF_NOSLEEP) 527 msg.msg_flags |= MSGF_NOSLEEP; 528 529 if ((rv = hv_start(sc, &msg)) != 0) 530 return (rv); 531 return (hv_reply(sc, &msg)); 532 } 533 534 int 535 hv_start(struct hv_softc *sc, struct hv_msg *msg) 536 { 537 const int delays[] = { 100, 100, 100, 500, 500, 5000, 5000, 5000 }; 538 const char *wchan = "hvstart"; 539 uint16_t status; 540 int i, s; 541 542 msg->msg_req.hc_connid = VMBUS_CONNID_MESSAGE; 543 msg->msg_req.hc_msgtype = 1; 544 545 if (!(msg->msg_flags & MSGF_NOQUEUE)) { 546 mtx_enter(&sc->sc_reqlck); 547 TAILQ_INSERT_TAIL(&sc->sc_reqs, msg, msg_entry); 548 mtx_leave(&sc->sc_reqlck); 549 } 550 551 for (i = 0; i < nitems(delays); i++) { 552 status = hv_hypercall(sc, HYPERCALL_POST_MESSAGE, 553 &msg->msg_req, NULL); 554 if (status == HYPERCALL_STATUS_SUCCESS) 555 break; 556 if (msg->msg_flags & MSGF_NOSLEEP) { 557 delay(delays[i]); 558 s = splnet(); 559 hv_intr(); 560 splx(s); 561 } else { 562 tsleep_nsec(wchan, PRIBIO, wchan, 563 USEC_TO_NSEC(delays[i])); 564 } 565 } 566 if (status != 0) { 567 printf("%s: posting vmbus message failed with %d\n", 568 sc->sc_dev.dv_xname, status); 569 if (!(msg->msg_flags & MSGF_NOQUEUE)) { 570 mtx_enter(&sc->sc_reqlck); 571 TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry); 572 mtx_leave(&sc->sc_reqlck); 573 } 574 return (EIO); 575 } 576 577 return (0); 578 } 579 580 static int 581 hv_reply_done(struct hv_softc *sc, struct hv_msg *msg) 582 { 583 struct hv_msg *m; 584 585 mtx_enter(&sc->sc_rsplck); 586 TAILQ_FOREACH(m, &sc->sc_rsps, msg_entry) { 587 if (m == msg) { 588 mtx_leave(&sc->sc_rsplck); 589 return (1); 590 } 591 } 592 mtx_leave(&sc->sc_rsplck); 593 return (0); 594 } 595 596 int 597 hv_reply(struct hv_softc *sc, struct hv_msg *msg) 598 { 599 if (msg->msg_flags & MSGF_NOQUEUE) 600 return (0); 601 602 hv_wait(sc, hv_reply_done, msg, msg, "hvreply"); 603 604 mtx_enter(&sc->sc_rsplck); 605 TAILQ_REMOVE(&sc->sc_rsps, msg, msg_entry); 606 mtx_leave(&sc->sc_rsplck); 607 608 return (0); 609 } 610 611 void 612 hv_wait(struct hv_softc *sc, int (*cond)(struct hv_softc *, struct hv_msg *), 613 struct hv_msg *msg, void *wchan, const char *wmsg) 614 { 615 int s; 616 617 KASSERT(cold ? msg->msg_flags & MSGF_NOSLEEP : 1); 618 619 while (!cond(sc, msg)) { 620 if (msg->msg_flags & MSGF_NOSLEEP) { 621 delay(1000); 622 s = splnet(); 623 hv_intr(); 624 splx(s); 625 } else { 626 tsleep_nsec(wchan, PRIBIO, wmsg ? wmsg : "hvwait", 627 USEC_TO_NSEC(1000)); 628 } 629 } 630 } 631 632 uint16_t 633 hv_intr_signal(struct hv_softc *sc, void *con) 634 { 635 uint64_t status; 636 637 status = hv_hypercall(sc, HYPERCALL_SIGNAL_EVENT, con, NULL); 638 return ((uint16_t)status); 639 } 640 641 void 642 hv_intr(void) 643 { 644 struct hv_softc *sc = hv_sc; 645 646 hv_event_intr(sc); 647 hv_message_intr(sc); 648 } 649 650 void 651 hv_event_intr(struct hv_softc *sc) 652 { 653 struct vmbus_evtflags *evt; 654 struct cpu_info *ci = curcpu(); 655 int cpu = CPU_INFO_UNIT(ci); 656 int bit, row, maxrow, chanid; 657 struct hv_channel *ch; 658 u_long *revents, pending; 659 660 evt = (struct vmbus_evtflags *)sc->sc_siep[cpu] + 661 VMBUS_SINT_MESSAGE; 662 if ((sc->sc_proto == VMBUS_VERSION_WS2008) || 663 (sc->sc_proto == VMBUS_VERSION_WIN7)) { 664 if (!test_bit(0, &evt->evt_flags[0])) 665 return; 666 clear_bit(0, &evt->evt_flags[0]); 667 maxrow = VMBUS_CHAN_MAX_COMPAT / VMBUS_EVTFLAG_LEN; 668 /* 669 * receive size is 1/2 page and divide that by 4 bytes 670 */ 671 revents = sc->sc_revents; 672 } else { 673 maxrow = nitems(evt->evt_flags); 674 /* 675 * On Host with Win8 or above, the event page can be 676 * checked directly to get the id of the channel 677 * that has the pending interrupt. 678 */ 679 revents = &evt->evt_flags[0]; 680 } 681 682 for (row = 0; row < maxrow; row++) { 683 if (revents[row] == 0) 684 continue; 685 pending = atomic_swap_ulong(&revents[row], 0); 686 for (bit = 0; pending > 0; pending >>= 1, bit++) { 687 if ((pending & 1) == 0) 688 continue; 689 chanid = (row * LONG_BIT) + bit; 690 /* vmbus channel protocol message */ 691 if (chanid == 0) 692 continue; 693 ch = hv_channel_lookup(sc, chanid); 694 if (ch == NULL) { 695 printf("%s: unhandled event on %d\n", 696 sc->sc_dev.dv_xname, chanid); 697 continue; 698 } 699 if (ch->ch_state != HV_CHANSTATE_OPENED) { 700 printf("%s: channel %d is not active\n", 701 sc->sc_dev.dv_xname, chanid); 702 continue; 703 } 704 ch->ch_evcnt.ec_count++; 705 hv_channel_schedule(ch); 706 } 707 } 708 } 709 710 void 711 hv_message_intr(struct hv_softc *sc) 712 { 713 struct vmbus_message *msg; 714 struct vmbus_chanmsg_hdr *hdr; 715 struct cpu_info *ci = curcpu(); 716 int cpu = CPU_INFO_UNIT(ci); 717 718 for (;;) { 719 msg = (struct vmbus_message *)sc->sc_simp[cpu] + 720 VMBUS_SINT_MESSAGE; 721 if (msg->msg_type == VMBUS_MSGTYPE_NONE) 722 break; 723 724 hdr = (struct vmbus_chanmsg_hdr *)msg->msg_data; 725 if (hdr->chm_type >= VMBUS_CHANMSG_COUNT) { 726 printf("%s: unhandled message type %u flags %#x\n", 727 sc->sc_dev.dv_xname, hdr->chm_type, 728 msg->msg_flags); 729 goto skip; 730 } 731 if (hv_msg_dispatch[hdr->chm_type].hmd_handler) 732 hv_msg_dispatch[hdr->chm_type].hmd_handler(sc, hdr); 733 else 734 printf("%s: unhandled message type %u\n", 735 sc->sc_dev.dv_xname, hdr->chm_type); 736 skip: 737 msg->msg_type = VMBUS_MSGTYPE_NONE; 738 virtio_membar_sync(); 739 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) 740 wrmsr(MSR_HV_EOM, 0); 741 } 742 } 743 744 void 745 hv_channel_response(struct hv_softc *sc, struct vmbus_chanmsg_hdr *rsphdr) 746 { 747 struct hv_msg *msg; 748 struct vmbus_chanmsg_hdr *reqhdr; 749 int req; 750 751 req = hv_msg_dispatch[rsphdr->chm_type].hmd_request; 752 mtx_enter(&sc->sc_reqlck); 753 TAILQ_FOREACH(msg, &sc->sc_reqs, msg_entry) { 754 reqhdr = (struct vmbus_chanmsg_hdr *)&msg->msg_req.hc_data; 755 if (reqhdr->chm_type == req) { 756 TAILQ_REMOVE(&sc->sc_reqs, msg, msg_entry); 757 break; 758 } 759 } 760 mtx_leave(&sc->sc_reqlck); 761 if (msg != NULL) { 762 memcpy(msg->msg_rsp, rsphdr, msg->msg_rsplen); 763 mtx_enter(&sc->sc_rsplck); 764 TAILQ_INSERT_TAIL(&sc->sc_rsps, msg, msg_entry); 765 mtx_leave(&sc->sc_rsplck); 766 wakeup(msg); 767 } 768 } 769 770 void 771 hv_channel_offer(struct hv_softc *sc, struct vmbus_chanmsg_hdr *hdr) 772 { 773 struct hv_offer *co; 774 775 co = malloc(sizeof(*co), M_DEVBUF, M_NOWAIT | M_ZERO); 776 if (co == NULL) { 777 printf("%s: failed to allocate an offer object\n", 778 sc->sc_dev.dv_xname); 779 return; 780 } 781 782 memcpy(&co->co_chan, hdr, sizeof(co->co_chan)); 783 784 mtx_enter(&sc->sc_offerlck); 785 SIMPLEQ_INSERT_TAIL(&sc->sc_offers, co, co_entry); 786 mtx_leave(&sc->sc_offerlck); 787 } 788 789 void 790 hv_channel_rescind(struct hv_softc *sc, struct vmbus_chanmsg_hdr *hdr) 791 { 792 const struct vmbus_chanmsg_chrescind *cmd; 793 794 cmd = (const struct vmbus_chanmsg_chrescind *)hdr; 795 printf("%s: revoking channel %u\n", sc->sc_dev.dv_xname, 796 cmd->chm_chanid); 797 } 798 799 void 800 hv_channel_delivered(struct hv_softc *sc, struct vmbus_chanmsg_hdr *hdr) 801 { 802 atomic_setbits_int(&sc->sc_flags, HSF_OFFERS_DELIVERED); 803 wakeup(&sc->sc_offers); 804 } 805 806 int 807 hv_vmbus_connect(struct hv_softc *sc) 808 { 809 const uint32_t versions[] = { 810 VMBUS_VERSION_WIN10, 811 VMBUS_VERSION_WIN8_1, VMBUS_VERSION_WIN8, 812 VMBUS_VERSION_WIN7, VMBUS_VERSION_WS2008 813 }; 814 struct vmbus_chanmsg_connect cmd; 815 struct vmbus_chanmsg_connect_resp rsp; 816 paddr_t epa, mpa1, mpa2; 817 int i; 818 819 sc->sc_events = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait); 820 if (sc->sc_events == NULL) { 821 printf(": failed to allocate channel port events page\n"); 822 goto errout; 823 } 824 if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_events, &epa)) { 825 printf(": channel port events page PA extraction failed\n"); 826 goto errout; 827 } 828 829 sc->sc_wevents = (u_long *)sc->sc_events; 830 sc->sc_revents = (u_long *)((caddr_t)sc->sc_events + (PAGE_SIZE >> 1)); 831 832 sc->sc_monitor[0] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait); 833 if (sc->sc_monitor[0] == NULL) { 834 printf(": failed to allocate monitor page 1\n"); 835 goto errout; 836 } 837 if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_monitor[0], &mpa1)) { 838 printf(": monitor page 1 PA extraction failed\n"); 839 goto errout; 840 } 841 842 sc->sc_monitor[1] = km_alloc(PAGE_SIZE, &kv_any, &kp_zero, &kd_nowait); 843 if (sc->sc_monitor[1] == NULL) { 844 printf(": failed to allocate monitor page 2\n"); 845 goto errout; 846 } 847 if (!pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_monitor[1], &mpa2)) { 848 printf(": monitor page 2 PA extraction failed\n"); 849 goto errout; 850 } 851 852 memset(&cmd, 0, sizeof(cmd)); 853 cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CONNECT; 854 cmd.chm_evtflags = (uint64_t)epa; 855 cmd.chm_mnf1 = (uint64_t)mpa1; 856 cmd.chm_mnf2 = (uint64_t)mpa2; 857 858 memset(&rsp, 0, sizeof(rsp)); 859 860 for (i = 0; i < nitems(versions); i++) { 861 cmd.chm_ver = versions[i]; 862 if (hv_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp), 863 HCF_NOSLEEP)) { 864 DPRINTF("%s: CONNECT failed\n", 865 sc->sc_dev.dv_xname); 866 goto errout; 867 } 868 if (rsp.chm_done) { 869 sc->sc_flags |= HSF_CONNECTED; 870 sc->sc_proto = versions[i]; 871 sc->sc_handle = VMBUS_GPADL_START; 872 break; 873 } 874 } 875 if (i == nitems(versions)) { 876 printf("%s: failed to negotiate protocol version\n", 877 sc->sc_dev.dv_xname); 878 goto errout; 879 } 880 881 return (0); 882 883 errout: 884 if (sc->sc_events) { 885 km_free(sc->sc_events, PAGE_SIZE, &kv_any, &kp_zero); 886 sc->sc_events = NULL; 887 sc->sc_wevents = NULL; 888 sc->sc_revents = NULL; 889 } 890 if (sc->sc_monitor[0]) { 891 km_free(sc->sc_monitor[0], PAGE_SIZE, &kv_any, &kp_zero); 892 sc->sc_monitor[0] = NULL; 893 } 894 if (sc->sc_monitor[1]) { 895 km_free(sc->sc_monitor[1], PAGE_SIZE, &kv_any, &kp_zero); 896 sc->sc_monitor[1] = NULL; 897 } 898 return (-1); 899 } 900 901 #ifdef HYPERV_DEBUG 902 static inline char * 903 guidprint(struct hv_guid *a) 904 { 905 /* 3 0 5 4 7 6 8 9 10 15 */ 906 /* 33221100-5544-7766-9988-FFEEDDCCBBAA */ 907 static char buf[16 * 2 + 4 + 1]; 908 int i, j = 0; 909 910 for (i = 3; i != -1; i -= 1, j += 2) 911 snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]); 912 buf[j++] = '-'; 913 for (i = 5; i != 3; i -= 1, j += 2) 914 snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]); 915 buf[j++] = '-'; 916 for (i = 7; i != 5; i -= 1, j += 2) 917 snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]); 918 buf[j++] = '-'; 919 for (i = 8; i < 10; i += 1, j += 2) 920 snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]); 921 buf[j++] = '-'; 922 for (i = 10; i < 16; i += 1, j += 2) 923 snprintf(&buf[j], 3, "%02x", (uint8_t)a->data[i]); 924 return (&buf[0]); 925 } 926 #endif /* HYPERV_DEBUG */ 927 928 void 929 hv_guid_sprint(struct hv_guid *guid, char *str, size_t size) 930 { 931 const struct { 932 const struct hv_guid *guid; 933 const char *ident; 934 } map[] = { 935 { &hv_guid_network, "network" }, 936 { &hv_guid_ide, "ide" }, 937 { &hv_guid_scsi, "scsi" }, 938 { &hv_guid_shutdown, "shutdown" }, 939 { &hv_guid_timesync, "timesync" }, 940 { &hv_guid_heartbeat, "heartbeat" }, 941 { &hv_guid_kvp, "kvp" }, 942 #ifdef HYPERV_DEBUG 943 { &hv_guid_vss, "vss" }, 944 { &hv_guid_dynmem, "dynamic-memory" }, 945 { &hv_guid_mouse, "mouse" }, 946 { &hv_guid_kbd, "keyboard" }, 947 { &hv_guid_video, "video" }, 948 { &hv_guid_fc, "fiber-channel" }, 949 { &hv_guid_fcopy, "file-copy" }, 950 { &hv_guid_pcie, "pcie-passthrough" }, 951 { &hv_guid_netdir, "network-direct" }, 952 { &hv_guid_rdesktop, "remote-desktop" }, 953 { &hv_guid_avma1, "avma-1" }, 954 { &hv_guid_avma2, "avma-2" }, 955 { &hv_guid_avma3, "avma-3" }, 956 { &hv_guid_avma4, "avma-4" }, 957 #endif 958 }; 959 int i; 960 961 for (i = 0; i < nitems(map); i++) { 962 if (memcmp(guid, map[i].guid, sizeof(*guid)) == 0) { 963 strlcpy(str, map[i].ident, size); 964 return; 965 } 966 } 967 #ifdef HYPERV_DEBUG 968 strlcpy(str, guidprint(guid), size); 969 #endif 970 } 971 972 static int 973 hv_channel_scan_done(struct hv_softc *sc, struct hv_msg *msg __unused) 974 { 975 return (sc->sc_flags & HSF_OFFERS_DELIVERED); 976 } 977 978 int 979 hv_channel_scan(struct hv_softc *sc) 980 { 981 struct vmbus_chanmsg_hdr hdr; 982 struct vmbus_chanmsg_choffer rsp; 983 struct hv_offer *co; 984 985 SIMPLEQ_INIT(&sc->sc_offers); 986 mtx_init(&sc->sc_offerlck, IPL_NET); 987 988 memset(&hdr, 0, sizeof(hdr)); 989 hdr.chm_type = VMBUS_CHANMSG_CHREQUEST; 990 991 if (hv_cmd(sc, &hdr, sizeof(hdr), &rsp, sizeof(rsp), 992 HCF_NOSLEEP | HCF_NOREPLY)) { 993 DPRINTF("%s: CHREQUEST failed\n", sc->sc_dev.dv_xname); 994 return (-1); 995 } 996 997 hv_wait(sc, hv_channel_scan_done, (struct hv_msg *)&hdr, 998 &sc->sc_offers, "hvscan"); 999 1000 TAILQ_INIT(&sc->sc_channels); 1001 mtx_init(&sc->sc_channelck, IPL_NET); 1002 1003 mtx_enter(&sc->sc_offerlck); 1004 while (!SIMPLEQ_EMPTY(&sc->sc_offers)) { 1005 co = SIMPLEQ_FIRST(&sc->sc_offers); 1006 SIMPLEQ_REMOVE_HEAD(&sc->sc_offers, co_entry); 1007 mtx_leave(&sc->sc_offerlck); 1008 1009 hv_process_offer(sc, co); 1010 free(co, M_DEVBUF, sizeof(*co)); 1011 1012 mtx_enter(&sc->sc_offerlck); 1013 } 1014 mtx_leave(&sc->sc_offerlck); 1015 1016 return (0); 1017 } 1018 1019 void 1020 hv_process_offer(struct hv_softc *sc, struct hv_offer *co) 1021 { 1022 struct hv_channel *ch, *nch; 1023 1024 nch = malloc(sizeof(*nch), M_DEVBUF, M_ZERO | M_NOWAIT); 1025 if (nch == NULL) { 1026 printf("%s: failed to allocate memory for the channel\n", 1027 sc->sc_dev.dv_xname); 1028 return; 1029 } 1030 nch->ch_sc = sc; 1031 hv_guid_sprint(&co->co_chan.chm_chtype, nch->ch_ident, 1032 sizeof(nch->ch_ident)); 1033 1034 /* 1035 * By default we setup state to enable batched reading. 1036 * A specific service can choose to disable this prior 1037 * to opening the channel. 1038 */ 1039 nch->ch_flags |= CHF_BATCHED; 1040 1041 KASSERT((((vaddr_t)&nch->ch_monprm) & 0x7) == 0); 1042 memset(&nch->ch_monprm, 0, sizeof(nch->ch_monprm)); 1043 nch->ch_monprm.mp_connid = VMBUS_CONNID_EVENT; 1044 1045 if (sc->sc_proto != VMBUS_VERSION_WS2008) 1046 nch->ch_monprm.mp_connid = co->co_chan.chm_connid; 1047 1048 if (co->co_chan.chm_flags1 & VMBUS_CHOFFER_FLAG1_HASMNF) { 1049 nch->ch_mgroup = co->co_chan.chm_montrig / VMBUS_MONTRIG_LEN; 1050 nch->ch_mindex = co->co_chan.chm_montrig % VMBUS_MONTRIG_LEN; 1051 nch->ch_flags |= CHF_MONITOR; 1052 } 1053 1054 nch->ch_id = co->co_chan.chm_chanid; 1055 1056 memcpy(&nch->ch_type, &co->co_chan.chm_chtype, sizeof(ch->ch_type)); 1057 memcpy(&nch->ch_inst, &co->co_chan.chm_chinst, sizeof(ch->ch_inst)); 1058 1059 mtx_enter(&sc->sc_channelck); 1060 TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) { 1061 if (!memcmp(&ch->ch_type, &nch->ch_type, sizeof(ch->ch_type)) && 1062 !memcmp(&ch->ch_inst, &nch->ch_inst, sizeof(ch->ch_inst))) 1063 break; 1064 } 1065 if (ch != NULL) { 1066 if (co->co_chan.chm_subidx == 0) { 1067 printf("%s: unknown offer \"%s\"\n", 1068 sc->sc_dev.dv_xname, nch->ch_ident); 1069 mtx_leave(&sc->sc_channelck); 1070 free(nch, M_DEVBUF, sizeof(*nch)); 1071 return; 1072 } 1073 #ifdef HYPERV_DEBUG 1074 printf("%s: subchannel %u for \"%s\"\n", sc->sc_dev.dv_xname, 1075 co->co_chan.chm_subidx, ch->ch_ident); 1076 #endif 1077 mtx_leave(&sc->sc_channelck); 1078 free(nch, M_DEVBUF, sizeof(*nch)); 1079 return; 1080 } 1081 1082 nch->ch_state = HV_CHANSTATE_OFFERED; 1083 1084 TAILQ_INSERT_TAIL(&sc->sc_channels, nch, ch_entry); 1085 mtx_leave(&sc->sc_channelck); 1086 1087 #ifdef HYPERV_DEBUG 1088 printf("%s: channel %u: \"%s\"", sc->sc_dev.dv_xname, nch->ch_id, 1089 nch->ch_ident); 1090 if (nch->ch_flags & CHF_MONITOR) 1091 printf(", monitor %u\n", co->co_chan.chm_montrig); 1092 else 1093 printf("\n"); 1094 #endif 1095 } 1096 1097 struct hv_channel * 1098 hv_channel_lookup(struct hv_softc *sc, uint32_t relid) 1099 { 1100 struct hv_channel *ch; 1101 1102 TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) { 1103 if (ch->ch_id == relid) 1104 return (ch); 1105 } 1106 return (NULL); 1107 } 1108 1109 int 1110 hv_channel_ring_create(struct hv_channel *ch, uint32_t buflen) 1111 { 1112 struct hv_softc *sc = ch->ch_sc; 1113 1114 buflen = roundup(buflen, PAGE_SIZE) + sizeof(struct vmbus_bufring); 1115 ch->ch_ring = km_alloc(2 * buflen, &kv_any, &kp_zero, cold ? 1116 &kd_nowait : &kd_waitok); 1117 if (ch->ch_ring == NULL) { 1118 printf("%s: failed to allocate channel ring\n", 1119 sc->sc_dev.dv_xname); 1120 return (-1); 1121 } 1122 ch->ch_ring_size = 2 * buflen; 1123 1124 memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd)); 1125 ch->ch_wrd.rd_ring = (struct vmbus_bufring *)ch->ch_ring; 1126 ch->ch_wrd.rd_size = buflen; 1127 ch->ch_wrd.rd_dsize = buflen - sizeof(struct vmbus_bufring); 1128 mtx_init(&ch->ch_wrd.rd_lock, IPL_NET); 1129 1130 memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd)); 1131 ch->ch_rrd.rd_ring = (struct vmbus_bufring *)((uint8_t *)ch->ch_ring + 1132 buflen); 1133 ch->ch_rrd.rd_size = buflen; 1134 ch->ch_rrd.rd_dsize = buflen - sizeof(struct vmbus_bufring); 1135 mtx_init(&ch->ch_rrd.rd_lock, IPL_NET); 1136 1137 if (hv_handle_alloc(ch, ch->ch_ring, 2 * buflen, &ch->ch_ring_gpadl)) { 1138 printf("%s: failed to obtain a PA handle for the ring\n", 1139 sc->sc_dev.dv_xname); 1140 hv_channel_ring_destroy(ch); 1141 return (-1); 1142 } 1143 1144 return (0); 1145 } 1146 1147 void 1148 hv_channel_ring_destroy(struct hv_channel *ch) 1149 { 1150 km_free(ch->ch_ring, ch->ch_ring_size, &kv_any, &kp_zero); 1151 ch->ch_ring = NULL; 1152 hv_handle_free(ch, ch->ch_ring_gpadl); 1153 1154 memset(&ch->ch_wrd, 0, sizeof(ch->ch_wrd)); 1155 memset(&ch->ch_rrd, 0, sizeof(ch->ch_rrd)); 1156 } 1157 1158 int 1159 hv_channel_open(struct hv_channel *ch, size_t buflen, void *udata, 1160 size_t udatalen, void (*handler)(void *), void *arg) 1161 { 1162 struct hv_softc *sc = ch->ch_sc; 1163 struct vmbus_chanmsg_chopen cmd; 1164 struct vmbus_chanmsg_chopen_resp rsp; 1165 int rv; 1166 1167 if (ch->ch_ring == NULL && 1168 hv_channel_ring_create(ch, buflen)) { 1169 DPRINTF("%s: failed to create channel ring\n", 1170 sc->sc_dev.dv_xname); 1171 return (-1); 1172 } 1173 1174 memset(&cmd, 0, sizeof(cmd)); 1175 cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHOPEN; 1176 cmd.chm_openid = ch->ch_id; 1177 cmd.chm_chanid = ch->ch_id; 1178 cmd.chm_gpadl = ch->ch_ring_gpadl; 1179 cmd.chm_txbr_pgcnt = ch->ch_wrd.rd_size >> PAGE_SHIFT; 1180 cmd.chm_vcpuid = ch->ch_vcpu; 1181 1182 if (udata && udatalen > 0) 1183 memcpy(cmd.chm_udata, udata, udatalen); 1184 1185 memset(&rsp, 0, sizeof(rsp)); 1186 1187 ch->ch_handler = handler; 1188 ch->ch_ctx = arg; 1189 1190 ch->ch_state = HV_CHANSTATE_OPENED; 1191 1192 rv = hv_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp), 1193 cold ? HCF_NOSLEEP : HCF_SLEEPOK); 1194 if (rv) { 1195 hv_channel_ring_destroy(ch); 1196 DPRINTF("%s: CHOPEN failed with %d\n", 1197 sc->sc_dev.dv_xname, rv); 1198 ch->ch_handler = NULL; 1199 ch->ch_ctx = NULL; 1200 ch->ch_state = HV_CHANSTATE_OFFERED; 1201 return (-1); 1202 } 1203 1204 return (0); 1205 } 1206 1207 int 1208 hv_channel_close(struct hv_channel *ch) 1209 { 1210 struct hv_softc *sc = ch->ch_sc; 1211 struct vmbus_chanmsg_chclose cmd; 1212 int rv; 1213 1214 memset(&cmd, 0, sizeof(cmd)); 1215 cmd.chm_hdr.chm_type = VMBUS_CHANMSG_CHCLOSE; 1216 cmd.chm_chanid = ch->ch_id; 1217 1218 ch->ch_state = HV_CHANSTATE_CLOSING; 1219 rv = hv_cmd(sc, &cmd, sizeof(cmd), NULL, 0, HCF_NOREPLY); 1220 if (rv) { 1221 DPRINTF("%s: CHCLOSE failed with %d\n", 1222 sc->sc_dev.dv_xname, rv); 1223 return (-1); 1224 } 1225 ch->ch_state = HV_CHANSTATE_CLOSED; 1226 hv_channel_ring_destroy(ch); 1227 return (0); 1228 } 1229 1230 static inline void 1231 hv_channel_setevent(struct hv_softc *sc, struct hv_channel *ch) 1232 { 1233 struct vmbus_mon_trig *mtg; 1234 1235 /* Each uint32_t represents 32 channels */ 1236 set_bit(ch->ch_id, sc->sc_wevents); 1237 if (ch->ch_flags & CHF_MONITOR) { 1238 mtg = &sc->sc_monitor[1]->mnf_trigs[ch->ch_mgroup]; 1239 set_bit(ch->ch_mindex, &mtg->mt_pending); 1240 } else 1241 hv_intr_signal(sc, &ch->ch_monprm); 1242 } 1243 1244 void 1245 hv_channel_intr(void *arg) 1246 { 1247 struct hv_channel *ch = arg; 1248 1249 if (hv_channel_ready(ch)) 1250 ch->ch_handler(ch->ch_ctx); 1251 1252 if (hv_channel_unpause(ch) == 0) 1253 return; 1254 1255 hv_channel_pause(ch); 1256 hv_channel_schedule(ch); 1257 } 1258 1259 int 1260 hv_channel_setdeferred(struct hv_channel *ch, const char *name) 1261 { 1262 ch->ch_taskq = taskq_create(name, 1, IPL_NET, TASKQ_MPSAFE); 1263 if (ch->ch_taskq == NULL) 1264 return (-1); 1265 task_set(&ch->ch_task, hv_channel_intr, ch); 1266 return (0); 1267 } 1268 1269 void 1270 hv_channel_schedule(struct hv_channel *ch) 1271 { 1272 if (ch->ch_handler) { 1273 if (!cold && (ch->ch_flags & CHF_BATCHED)) { 1274 hv_channel_pause(ch); 1275 task_add(ch->ch_taskq, &ch->ch_task); 1276 } else 1277 ch->ch_handler(ch->ch_ctx); 1278 } 1279 } 1280 1281 static inline void 1282 hv_ring_put(struct hv_ring_data *wrd, uint8_t *data, uint32_t datalen) 1283 { 1284 int left = MIN(datalen, wrd->rd_dsize - wrd->rd_prod); 1285 1286 memcpy(&wrd->rd_ring->br_data[wrd->rd_prod], data, left); 1287 memcpy(&wrd->rd_ring->br_data[0], data + left, datalen - left); 1288 wrd->rd_prod += datalen; 1289 if (wrd->rd_prod >= wrd->rd_dsize) 1290 wrd->rd_prod -= wrd->rd_dsize; 1291 } 1292 1293 static inline void 1294 hv_ring_get(struct hv_ring_data *rrd, uint8_t *data, uint32_t datalen, 1295 int peek) 1296 { 1297 int left = MIN(datalen, rrd->rd_dsize - rrd->rd_cons); 1298 1299 memcpy(data, &rrd->rd_ring->br_data[rrd->rd_cons], left); 1300 memcpy(data + left, &rrd->rd_ring->br_data[0], datalen - left); 1301 if (!peek) { 1302 rrd->rd_cons += datalen; 1303 if (rrd->rd_cons >= rrd->rd_dsize) 1304 rrd->rd_cons -= rrd->rd_dsize; 1305 } 1306 } 1307 1308 static inline void 1309 hv_ring_avail(struct hv_ring_data *rd, uint32_t *towrite, uint32_t *toread) 1310 { 1311 uint32_t ridx = rd->rd_ring->br_rindex; 1312 uint32_t widx = rd->rd_ring->br_windex; 1313 uint32_t r, w; 1314 1315 if (widx >= ridx) 1316 w = rd->rd_dsize - (widx - ridx); 1317 else 1318 w = ridx - widx; 1319 r = rd->rd_dsize - w; 1320 if (towrite) 1321 *towrite = w; 1322 if (toread) 1323 *toread = r; 1324 } 1325 1326 int 1327 hv_ring_write(struct hv_ring_data *wrd, struct iovec *iov, int iov_cnt, 1328 int *needsig) 1329 { 1330 uint64_t indices = 0; 1331 uint32_t avail, oprod, datalen = sizeof(indices); 1332 int i; 1333 1334 for (i = 0; i < iov_cnt; i++) 1335 datalen += iov[i].iov_len; 1336 1337 KASSERT(datalen <= wrd->rd_dsize); 1338 1339 hv_ring_avail(wrd, &avail, NULL); 1340 if (avail <= datalen) { 1341 DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen); 1342 return (EAGAIN); 1343 } 1344 1345 oprod = wrd->rd_prod; 1346 1347 for (i = 0; i < iov_cnt; i++) 1348 hv_ring_put(wrd, iov[i].iov_base, iov[i].iov_len); 1349 1350 indices = (uint64_t)oprod << 32; 1351 hv_ring_put(wrd, (uint8_t *)&indices, sizeof(indices)); 1352 1353 virtio_membar_sync(); 1354 wrd->rd_ring->br_windex = wrd->rd_prod; 1355 virtio_membar_sync(); 1356 1357 /* Signal when the ring transitions from being empty to non-empty */ 1358 if (wrd->rd_ring->br_imask == 0 && 1359 wrd->rd_ring->br_rindex == oprod) 1360 *needsig = 1; 1361 else 1362 *needsig = 0; 1363 1364 return (0); 1365 } 1366 1367 int 1368 hv_channel_send(struct hv_channel *ch, void *data, uint32_t datalen, 1369 uint64_t rid, int type, uint32_t flags) 1370 { 1371 struct hv_softc *sc = ch->ch_sc; 1372 struct vmbus_chanpkt cp; 1373 struct iovec iov[3]; 1374 uint32_t pktlen, pktlen_aligned; 1375 uint64_t zeropad = 0; 1376 int rv, needsig = 0; 1377 1378 pktlen = sizeof(cp) + datalen; 1379 pktlen_aligned = roundup(pktlen, sizeof(uint64_t)); 1380 1381 cp.cp_hdr.cph_type = type; 1382 cp.cp_hdr.cph_flags = flags; 1383 VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp)); 1384 VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned); 1385 cp.cp_hdr.cph_tid = rid; 1386 1387 iov[0].iov_base = &cp; 1388 iov[0].iov_len = sizeof(cp); 1389 1390 iov[1].iov_base = data; 1391 iov[1].iov_len = datalen; 1392 1393 iov[2].iov_base = &zeropad; 1394 iov[2].iov_len = pktlen_aligned - pktlen; 1395 1396 mtx_enter(&ch->ch_wrd.rd_lock); 1397 rv = hv_ring_write(&ch->ch_wrd, iov, 3, &needsig); 1398 mtx_leave(&ch->ch_wrd.rd_lock); 1399 if (rv == 0 && needsig) 1400 hv_channel_setevent(sc, ch); 1401 1402 return (rv); 1403 } 1404 1405 int 1406 hv_channel_send_sgl(struct hv_channel *ch, struct vmbus_gpa *sgl, 1407 uint32_t nsge, void *data, uint32_t datalen, uint64_t rid) 1408 { 1409 struct hv_softc *sc = ch->ch_sc; 1410 struct vmbus_chanpkt_sglist cp; 1411 struct iovec iov[4]; 1412 uint32_t buflen, pktlen, pktlen_aligned; 1413 uint64_t zeropad = 0; 1414 int rv, needsig = 0; 1415 1416 buflen = sizeof(struct vmbus_gpa) * nsge; 1417 pktlen = sizeof(cp) + datalen + buflen; 1418 pktlen_aligned = roundup(pktlen, sizeof(uint64_t)); 1419 1420 cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA; 1421 cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC; 1422 VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen); 1423 VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned); 1424 cp.cp_hdr.cph_tid = rid; 1425 cp.cp_gpa_cnt = nsge; 1426 cp.cp_rsvd = 0; 1427 1428 iov[0].iov_base = &cp; 1429 iov[0].iov_len = sizeof(cp); 1430 1431 iov[1].iov_base = sgl; 1432 iov[1].iov_len = buflen; 1433 1434 iov[2].iov_base = data; 1435 iov[2].iov_len = datalen; 1436 1437 iov[3].iov_base = &zeropad; 1438 iov[3].iov_len = pktlen_aligned - pktlen; 1439 1440 mtx_enter(&ch->ch_wrd.rd_lock); 1441 rv = hv_ring_write(&ch->ch_wrd, iov, 4, &needsig); 1442 mtx_leave(&ch->ch_wrd.rd_lock); 1443 if (rv == 0 && needsig) 1444 hv_channel_setevent(sc, ch); 1445 1446 return (rv); 1447 } 1448 1449 int 1450 hv_channel_send_prpl(struct hv_channel *ch, struct vmbus_gpa_range *prpl, 1451 uint32_t nprp, void *data, uint32_t datalen, uint64_t rid) 1452 { 1453 struct hv_softc *sc = ch->ch_sc; 1454 struct vmbus_chanpkt_prplist cp; 1455 struct iovec iov[4]; 1456 uint32_t buflen, pktlen, pktlen_aligned; 1457 uint64_t zeropad = 0; 1458 int rv, needsig = 0; 1459 1460 buflen = sizeof(struct vmbus_gpa_range) * (nprp + 1); 1461 pktlen = sizeof(cp) + datalen + buflen; 1462 pktlen_aligned = roundup(pktlen, sizeof(uint64_t)); 1463 1464 cp.cp_hdr.cph_type = VMBUS_CHANPKT_TYPE_GPA; 1465 cp.cp_hdr.cph_flags = VMBUS_CHANPKT_FLAG_RC; 1466 VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_hlen, sizeof(cp) + buflen); 1467 VMBUS_CHANPKT_SETLEN(cp.cp_hdr.cph_tlen, pktlen_aligned); 1468 cp.cp_hdr.cph_tid = rid; 1469 cp.cp_range_cnt = 1; 1470 cp.cp_rsvd = 0; 1471 1472 iov[0].iov_base = &cp; 1473 iov[0].iov_len = sizeof(cp); 1474 1475 iov[1].iov_base = prpl; 1476 iov[1].iov_len = buflen; 1477 1478 iov[2].iov_base = data; 1479 iov[2].iov_len = datalen; 1480 1481 iov[3].iov_base = &zeropad; 1482 iov[3].iov_len = pktlen_aligned - pktlen; 1483 1484 mtx_enter(&ch->ch_wrd.rd_lock); 1485 rv = hv_ring_write(&ch->ch_wrd, iov, 4, &needsig); 1486 mtx_leave(&ch->ch_wrd.rd_lock); 1487 if (rv == 0 && needsig) 1488 hv_channel_setevent(sc, ch); 1489 1490 return (rv); 1491 } 1492 1493 int 1494 hv_ring_peek(struct hv_ring_data *rrd, void *data, uint32_t datalen) 1495 { 1496 uint32_t avail; 1497 1498 KASSERT(datalen <= rrd->rd_dsize); 1499 1500 hv_ring_avail(rrd, NULL, &avail); 1501 if (avail < datalen) 1502 return (EAGAIN); 1503 1504 hv_ring_get(rrd, (uint8_t *)data, datalen, 1); 1505 return (0); 1506 } 1507 1508 int 1509 hv_ring_read(struct hv_ring_data *rrd, void *data, uint32_t datalen, 1510 uint32_t offset) 1511 { 1512 uint64_t indices; 1513 uint32_t avail; 1514 1515 KASSERT(datalen <= rrd->rd_dsize); 1516 1517 hv_ring_avail(rrd, NULL, &avail); 1518 if (avail < datalen) { 1519 DPRINTF("%s: avail %u datalen %u\n", __func__, avail, datalen); 1520 return (EAGAIN); 1521 } 1522 1523 if (offset) { 1524 rrd->rd_cons += offset; 1525 if (rrd->rd_cons >= rrd->rd_dsize) 1526 rrd->rd_cons -= rrd->rd_dsize; 1527 } 1528 1529 hv_ring_get(rrd, (uint8_t *)data, datalen, 0); 1530 hv_ring_get(rrd, (uint8_t *)&indices, sizeof(indices), 0); 1531 1532 virtio_membar_sync(); 1533 rrd->rd_ring->br_rindex = rrd->rd_cons; 1534 1535 return (0); 1536 } 1537 1538 int 1539 hv_channel_recv(struct hv_channel *ch, void *data, uint32_t datalen, 1540 uint32_t *rlen, uint64_t *rid, int raw) 1541 { 1542 struct vmbus_chanpkt_hdr cph; 1543 uint32_t offset, pktlen; 1544 int rv; 1545 1546 *rlen = 0; 1547 1548 mtx_enter(&ch->ch_rrd.rd_lock); 1549 1550 if ((rv = hv_ring_peek(&ch->ch_rrd, &cph, sizeof(cph))) != 0) { 1551 mtx_leave(&ch->ch_rrd.rd_lock); 1552 return (rv); 1553 } 1554 1555 offset = raw ? 0 : VMBUS_CHANPKT_GETLEN(cph.cph_hlen); 1556 pktlen = VMBUS_CHANPKT_GETLEN(cph.cph_tlen) - offset; 1557 if (pktlen > datalen) { 1558 mtx_leave(&ch->ch_rrd.rd_lock); 1559 printf("%s: pktlen %u datalen %u\n", __func__, pktlen, datalen); 1560 return (EINVAL); 1561 } 1562 1563 rv = hv_ring_read(&ch->ch_rrd, data, pktlen, offset); 1564 if (rv == 0) { 1565 *rlen = pktlen; 1566 *rid = cph.cph_tid; 1567 } 1568 1569 mtx_leave(&ch->ch_rrd.rd_lock); 1570 1571 return (rv); 1572 } 1573 1574 static inline void 1575 hv_ring_mask(struct hv_ring_data *rd) 1576 { 1577 virtio_membar_sync(); 1578 rd->rd_ring->br_imask = 1; 1579 virtio_membar_sync(); 1580 } 1581 1582 static inline void 1583 hv_ring_unmask(struct hv_ring_data *rd) 1584 { 1585 virtio_membar_sync(); 1586 rd->rd_ring->br_imask = 0; 1587 virtio_membar_sync(); 1588 } 1589 1590 void 1591 hv_channel_pause(struct hv_channel *ch) 1592 { 1593 hv_ring_mask(&ch->ch_rrd); 1594 } 1595 1596 uint 1597 hv_channel_unpause(struct hv_channel *ch) 1598 { 1599 uint32_t avail; 1600 1601 hv_ring_unmask(&ch->ch_rrd); 1602 hv_ring_avail(&ch->ch_rrd, NULL, &avail); 1603 1604 return (avail); 1605 } 1606 1607 uint 1608 hv_channel_ready(struct hv_channel *ch) 1609 { 1610 uint32_t avail; 1611 1612 hv_ring_avail(&ch->ch_rrd, NULL, &avail); 1613 1614 return (avail); 1615 } 1616 1617 /* How many PFNs can be referenced by the header */ 1618 #define HV_NPFNHDR ((VMBUS_MSG_DSIZE_MAX - \ 1619 sizeof(struct vmbus_chanmsg_gpadl_conn)) / sizeof(uint64_t)) 1620 1621 /* How many PFNs can be referenced by the body */ 1622 #define HV_NPFNBODY ((VMBUS_MSG_DSIZE_MAX - \ 1623 sizeof(struct vmbus_chanmsg_gpadl_subconn)) / sizeof(uint64_t)) 1624 1625 int 1626 hv_handle_alloc(struct hv_channel *ch, void *buffer, uint32_t buflen, 1627 uint32_t *handle) 1628 { 1629 struct hv_softc *sc = ch->ch_sc; 1630 struct vmbus_chanmsg_gpadl_conn *hdr; 1631 struct vmbus_chanmsg_gpadl_subconn *cmd; 1632 struct vmbus_chanmsg_gpadl_connresp rsp; 1633 struct hv_msg *msg; 1634 int i, j, last, left, rv; 1635 int bodylen = 0, ncmds = 0, pfn = 0; 1636 int waitflag = cold ? M_NOWAIT : M_WAITOK; 1637 uint64_t *frames; 1638 paddr_t pa; 1639 caddr_t body; 1640 /* Total number of pages to reference */ 1641 int total = atop(buflen); 1642 /* Number of pages that will fit the header */ 1643 int inhdr = MIN(total, HV_NPFNHDR); 1644 1645 KASSERT((buflen & (PAGE_SIZE - 1)) == 0); 1646 1647 if ((msg = malloc(sizeof(*msg), M_DEVBUF, M_ZERO | waitflag)) == NULL) 1648 return (ENOMEM); 1649 1650 /* Prepare array of frame addresses */ 1651 if ((frames = mallocarray(total, sizeof(*frames), M_DEVBUF, M_ZERO | 1652 waitflag)) == NULL) { 1653 free(msg, M_DEVBUF, sizeof(*msg)); 1654 return (ENOMEM); 1655 } 1656 for (i = 0; i < total; i++) { 1657 if (!pmap_extract(pmap_kernel(), (vaddr_t)buffer + 1658 PAGE_SIZE * i, &pa)) { 1659 free(msg, M_DEVBUF, sizeof(*msg)); 1660 free(frames, M_DEVBUF, total * sizeof(*frames)); 1661 return (EFAULT); 1662 } 1663 frames[i] = atop(pa); 1664 } 1665 1666 msg->msg_req.hc_dsize = sizeof(struct vmbus_chanmsg_gpadl_conn) + 1667 inhdr * sizeof(uint64_t); 1668 hdr = (struct vmbus_chanmsg_gpadl_conn *)msg->msg_req.hc_data; 1669 msg->msg_rsp = &rsp; 1670 msg->msg_rsplen = sizeof(rsp); 1671 if (waitflag == M_NOWAIT) 1672 msg->msg_flags = MSGF_NOSLEEP; 1673 1674 left = total - inhdr; 1675 1676 /* Allocate additional gpadl_body structures if required */ 1677 if (left > 0) { 1678 ncmds = MAX(1, left / HV_NPFNBODY + left % HV_NPFNBODY); 1679 bodylen = ncmds * VMBUS_MSG_DSIZE_MAX; 1680 body = malloc(bodylen, M_DEVBUF, M_ZERO | waitflag); 1681 if (body == NULL) { 1682 free(msg, M_DEVBUF, sizeof(*msg)); 1683 free(frames, M_DEVBUF, atop(buflen) * sizeof(*frames)); 1684 return (ENOMEM); 1685 } 1686 } 1687 1688 *handle = atomic_inc_int_nv(&sc->sc_handle); 1689 1690 hdr->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_CONN; 1691 hdr->chm_chanid = ch->ch_id; 1692 hdr->chm_gpadl = *handle; 1693 1694 /* Single range for a contiguous buffer */ 1695 hdr->chm_range_cnt = 1; 1696 hdr->chm_range_len = sizeof(struct vmbus_gpa_range) + total * 1697 sizeof(uint64_t); 1698 hdr->chm_range.gpa_ofs = 0; 1699 hdr->chm_range.gpa_len = buflen; 1700 1701 /* Fit as many pages as possible into the header */ 1702 for (i = 0; i < inhdr; i++) 1703 hdr->chm_range.gpa_page[i] = frames[pfn++]; 1704 1705 for (i = 0; i < ncmds; i++) { 1706 cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body + 1707 VMBUS_MSG_DSIZE_MAX * i); 1708 cmd->chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_SUBCONN; 1709 cmd->chm_gpadl = *handle; 1710 last = MIN(left, HV_NPFNBODY); 1711 for (j = 0; j < last; j++) 1712 cmd->chm_gpa_page[j] = frames[pfn++]; 1713 left -= last; 1714 } 1715 1716 rv = hv_start(sc, msg); 1717 if (rv != 0) { 1718 DPRINTF("%s: GPADL_CONN failed\n", sc->sc_dev.dv_xname); 1719 goto out; 1720 } 1721 for (i = 0; i < ncmds; i++) { 1722 int cmdlen = sizeof(*cmd); 1723 cmd = (struct vmbus_chanmsg_gpadl_subconn *)(body + 1724 VMBUS_MSG_DSIZE_MAX * i); 1725 /* Last element can be short */ 1726 if (i == ncmds - 1) 1727 cmdlen += last * sizeof(uint64_t); 1728 else 1729 cmdlen += HV_NPFNBODY * sizeof(uint64_t); 1730 rv = hv_cmd(sc, cmd, cmdlen, NULL, 0, waitflag | HCF_NOREPLY); 1731 if (rv != 0) { 1732 DPRINTF("%s: GPADL_SUBCONN (iteration %d/%d) failed " 1733 "with %d\n", sc->sc_dev.dv_xname, i, ncmds, rv); 1734 goto out; 1735 } 1736 } 1737 rv = hv_reply(sc, msg); 1738 if (rv != 0) 1739 DPRINTF("%s: GPADL allocation failed with %d\n", 1740 sc->sc_dev.dv_xname, rv); 1741 1742 out: 1743 free(msg, M_DEVBUF, sizeof(*msg)); 1744 free(frames, M_DEVBUF, total * sizeof(*frames)); 1745 if (bodylen > 0) 1746 free(body, M_DEVBUF, bodylen); 1747 if (rv != 0) 1748 return (rv); 1749 1750 KASSERT(*handle == rsp.chm_gpadl); 1751 1752 return (0); 1753 } 1754 1755 void 1756 hv_handle_free(struct hv_channel *ch, uint32_t handle) 1757 { 1758 struct hv_softc *sc = ch->ch_sc; 1759 struct vmbus_chanmsg_gpadl_disconn cmd; 1760 struct vmbus_chanmsg_gpadl_disconn rsp; 1761 int rv; 1762 1763 memset(&cmd, 0, sizeof(cmd)); 1764 cmd.chm_hdr.chm_type = VMBUS_CHANMSG_GPADL_DISCONN; 1765 cmd.chm_chanid = ch->ch_id; 1766 cmd.chm_gpadl = handle; 1767 1768 rv = hv_cmd(sc, &cmd, sizeof(cmd), &rsp, sizeof(rsp), cold ? 1769 HCF_NOSLEEP : 0); 1770 if (rv) 1771 DPRINTF("%s: GPADL_DISCONN failed with %d\n", 1772 sc->sc_dev.dv_xname, rv); 1773 } 1774 1775 static int 1776 hv_attach_print(void *aux, const char *name) 1777 { 1778 struct hv_attach_args *aa = aux; 1779 1780 if (name) 1781 printf("\"%s\" at %s", aa->aa_ident, name); 1782 1783 return (UNCONF); 1784 } 1785 1786 int 1787 hv_attach_devices(struct hv_softc *sc) 1788 { 1789 struct hv_dev *dv; 1790 struct hv_channel *ch; 1791 1792 SLIST_INIT(&sc->sc_devs); 1793 mtx_init(&sc->sc_devlck, IPL_NET); 1794 1795 TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) { 1796 if (ch->ch_state != HV_CHANSTATE_OFFERED) 1797 continue; 1798 if (!(ch->ch_flags & CHF_MONITOR)) 1799 continue; 1800 dv = malloc(sizeof(*dv), M_DEVBUF, M_ZERO | M_NOWAIT); 1801 if (dv == NULL) { 1802 printf("%s: failed to allocate device object\n", 1803 sc->sc_dev.dv_xname); 1804 return (-1); 1805 } 1806 dv->dv_aa.aa_parent = sc; 1807 dv->dv_aa.aa_type = &ch->ch_type; 1808 dv->dv_aa.aa_inst = &ch->ch_inst; 1809 dv->dv_aa.aa_ident = ch->ch_ident; 1810 dv->dv_aa.aa_chan = ch; 1811 dv->dv_aa.aa_dmat = sc->sc_dmat; 1812 mtx_enter(&sc->sc_devlck); 1813 SLIST_INSERT_HEAD(&sc->sc_devs, dv, dv_entry); 1814 mtx_leave(&sc->sc_devlck); 1815 config_found((struct device *)sc, &dv->dv_aa, hv_attach_print); 1816 } 1817 return (0); 1818 } 1819 1820 void 1821 hv_evcount_attach(struct hv_channel *ch, const char *name) 1822 { 1823 struct hv_softc *sc = ch->ch_sc; 1824 1825 evcount_attach(&ch->ch_evcnt, name, &sc->sc_idtvec); 1826 } 1827