2 * Copyright (c) 2014,2016 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Author: Sainath Varanasi.
30 * Email: bsdic@microsoft.com
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/reboot.h>
46 #include <sys/taskqueue.h>
47 #include <sys/selinfo.h>
48 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysproto.h>
55 #include <sys/endian.h>
56 #include <sys/_null.h>
58 #include <sys/signal.h>
59 #include <sys/syslog.h>
60 #include <sys/systm.h>
61 #include <sys/mutex.h>
63 #include <dev/hyperv/include/hyperv.h>
64 #include <dev/hyperv/include/vmbus.h>
65 #include <dev/hyperv/utilities/hv_utilreg.h>
66 #include <dev/hyperv/utilities/vmbus_icreg.h>
74 #define BUFFERSIZE sizeof(struct hv_kvp_msg)
75 #define kvp_hdr hdr.kvp_hdr
77 #define KVP_FWVER_MAJOR 3
78 #define KVP_FWVER VMBUS_IC_VERSION(KVP_FWVER_MAJOR, 0)
80 #define KVP_MSGVER_MAJOR 4
81 #define KVP_MSGVER VMBUS_IC_VERSION(KVP_MSGVER_MAJOR, 0)
83 /* hv_kvp debug control */
84 static int hv_kvp_log = 0;
86 #define hv_kvp_log_error(...) do { \
88 log(LOG_ERR, "hv_kvp: " __VA_ARGS__); \
91 #define hv_kvp_log_info(...) do { \
93 log(LOG_INFO, "hv_kvp: " __VA_ARGS__); \
96 static const struct vmbus_ic_desc vmbus_kvp_descs[] = {
98 .ic_guid = { .hv_guid = {
99 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
100 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6 } },
101 .ic_desc = "Hyper-V KVP"
106 /* character device prototypes */
107 static d_open_t hv_kvp_dev_open;
108 static d_close_t hv_kvp_dev_close;
109 static d_read_t hv_kvp_dev_daemon_read;
110 static d_write_t hv_kvp_dev_daemon_write;
111 static d_poll_t hv_kvp_dev_daemon_poll;
113 /* hv_kvp character device structure */
114 static struct cdevsw hv_kvp_cdevsw =
116 .d_version = D_VERSION,
117 .d_open = hv_kvp_dev_open,
118 .d_close = hv_kvp_dev_close,
119 .d_read = hv_kvp_dev_daemon_read,
120 .d_write = hv_kvp_dev_daemon_write,
121 .d_poll = hv_kvp_dev_daemon_poll,
122 .d_name = "hv_kvp_dev",
127 * Global state to track and synchronize multiple
128 * KVP transaction requests from the host.
130 typedef struct hv_kvp_sc {
131 struct hv_util_sc util_sc;
134 /* Unless specified the pending mutex should be
135 * used to alter the values of the following paramters:
139 struct mtx pending_mutex;
143 /* To track if transaction is active or not */
144 boolean_t req_in_progress;
145 /* Tracks if daemon did not reply back in time */
146 boolean_t req_timed_out;
147 /* Tracks if daemon is serving a request currently */
148 boolean_t daemon_busy;
150 /* Length of host message */
151 uint32_t host_msg_len;
153 /* Host message id */
154 uint64_t host_msg_id;
156 /* Current kvp message from the host */
157 struct hv_kvp_msg *host_kvp_msg;
159 /* Current kvp message for daemon */
160 struct hv_kvp_msg daemon_kvp_msg;
162 /* Rcv buffer for communicating with the host*/
165 /* Device semaphore to control communication */
166 struct sema dev_sema;
168 /* Indicates if daemon registered with driver */
169 boolean_t register_done;
171 /* Character device status */
172 boolean_t dev_accessed;
174 struct cdev *hv_kvp_dev;
176 struct proc *daemon_task;
178 struct selinfo hv_kvp_selinfo;
181 /* hv_kvp prototypes */
182 static int hv_kvp_req_in_progress(hv_kvp_sc *sc);
183 static void hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t, uint64_t, uint8_t *);
184 static void hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc);
185 static void hv_kvp_process_request(void *context, int pending);
188 * hv_kvp low level functions
192 * Check if kvp transaction is in progres
195 hv_kvp_req_in_progress(hv_kvp_sc *sc)
198 return (sc->req_in_progress);
203 * This routine is called whenever a message is received from the host
206 hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t rcv_len,
207 uint64_t request_id, uint8_t *rcv_buf)
210 /* Store all the relevant message details in the global structure */
211 /* Do not need to use mutex for req_in_progress here */
212 sc->req_in_progress = true;
213 sc->host_msg_len = rcv_len;
214 sc->host_msg_id = request_id;
215 sc->rcv_buf = rcv_buf;
216 sc->host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
217 sizeof(struct hv_vmbus_pipe_hdr) +
218 sizeof(struct hv_vmbus_icmsg_hdr)];
222 * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
225 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
226 struct hv_kvp_ip_msg *host_ip_msg)
228 int err_ip, err_subnet, err_gway, err_dns, err_adap;
231 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
233 (char *)umsg->body.kvp_ip_val.ip_addr,
234 strlen((char *)umsg->body.kvp_ip_val.ip_addr),
237 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
239 (char *)umsg->body.kvp_ip_val.sub_net,
240 strlen((char *)umsg->body.kvp_ip_val.sub_net),
243 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
245 (char *)umsg->body.kvp_ip_val.gate_way,
246 strlen((char *)umsg->body.kvp_ip_val.gate_way),
249 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
251 (char *)umsg->body.kvp_ip_val.dns_addr,
252 strlen((char *)umsg->body.kvp_ip_val.dns_addr),
255 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
257 (char *)umsg->body.kvp_ip_val.adapter_id,
258 strlen((char *)umsg->body.kvp_ip_val.adapter_id),
262 host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
263 host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
265 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
270 * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
273 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
274 struct hv_kvp_msg *umsg)
276 int err_ip, err_subnet, err_gway, err_dns, err_adap;
282 utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
284 (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
289 /* Adapter ID : GUID */
290 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
292 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
297 if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
298 for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
299 device_t dev = devs[devcnt];
300 struct vmbus_channel *chan;
301 char buf[HYPERV_GUID_STRLEN];
304 chan = vmbus_get_channel(dev);
305 n = hyperv_guid2str(vmbus_chan_guid_inst(chan), buf,
309 * The string in the 'kvp_ip_val.adapter_id' has
310 * braces around the GUID; skip the leading brace
311 * in 'kvp_ip_val.adapter_id'.
314 ((char *)&umsg->body.kvp_ip_val.adapter_id) + 1,
316 strlcpy((char *)umsg->body.kvp_ip_val.adapter_id,
317 device_get_nameunit(dev), MAX_ADAPTER_ID_SIZE);
324 /* Address Family , DHCP , SUBNET, Gateway, DNS */
325 umsg->kvp_hdr.operation = host_ip_msg->operation;
326 umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
327 umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
328 utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
329 (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
334 utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
335 (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
340 utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
341 (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
346 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
351 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
352 * Ensure utf16_utf8 takes care of the additional string terminating char!!
355 hv_kvp_convert_hostmsg_to_usermsg(struct hv_kvp_msg *hmsg, struct hv_kvp_msg *umsg)
359 struct hv_kvp_ip_msg *host_ip_msg;
361 host_ip_msg = (struct hv_kvp_ip_msg*)hmsg;
362 memset(umsg, 0, sizeof(struct hv_kvp_msg));
364 umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
365 umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
367 switch (umsg->kvp_hdr.operation) {
368 case HV_KVP_OP_SET_IP_INFO:
369 hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
372 case HV_KVP_OP_GET_IP_INFO:
373 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
375 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
376 MAX_ADAPTER_ID_SIZE, 1, &utf_err);
378 umsg->body.kvp_ip_val.addr_family =
379 host_ip_msg->kvp_ip_val.addr_family;
383 value_type = hmsg->body.kvp_set.data.value_type;
385 switch (value_type) {
387 umsg->body.kvp_set.data.value_size =
389 (char *)umsg->body.kvp_set.data.msg_value.value,
390 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
391 (uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
392 hmsg->body.kvp_set.data.value_size,
395 umsg->body.kvp_set.data.value_size =
396 umsg->body.kvp_set.data.value_size / 2;
400 umsg->body.kvp_set.data.value_size =
401 sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
402 hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
406 umsg->body.kvp_set.data.value_size =
407 sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
409 hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
413 umsg->body.kvp_set.data.key_size =
415 umsg->body.kvp_set.data.key,
416 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
417 (uint16_t *)hmsg->body.kvp_set.data.key,
418 hmsg->body.kvp_set.data.key_size,
422 umsg->body.kvp_set.data.key_size =
423 umsg->body.kvp_set.data.key_size / 2;
427 umsg->body.kvp_get.data.key_size =
428 utf16_to_utf8(umsg->body.kvp_get.data.key,
429 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
430 (uint16_t *)hmsg->body.kvp_get.data.key,
431 hmsg->body.kvp_get.data.key_size,
434 umsg->body.kvp_get.data.key_size =
435 umsg->body.kvp_get.data.key_size / 2;
438 case HV_KVP_OP_DELETE:
439 umsg->body.kvp_delete.key_size =
440 utf16_to_utf8(umsg->body.kvp_delete.key,
441 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
442 (uint16_t *)hmsg->body.kvp_delete.key,
443 hmsg->body.kvp_delete.key_size,
446 umsg->body.kvp_delete.key_size =
447 umsg->body.kvp_delete.key_size / 2;
450 case HV_KVP_OP_ENUMERATE:
451 umsg->body.kvp_enum_data.index =
452 hmsg->body.kvp_enum_data.index;
456 hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
457 __func__, umsg->kvp_hdr.operation);
463 * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
466 hv_kvp_convert_usermsg_to_hostmsg(struct hv_kvp_msg *umsg, struct hv_kvp_msg *hmsg)
468 int hkey_len = 0, hvalue_len = 0, utf_err = 0;
469 struct hv_kvp_exchg_msg_value *host_exchg_data;
470 char *key_name, *value;
472 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
474 switch (hmsg->kvp_hdr.operation) {
475 case HV_KVP_OP_GET_IP_INFO:
476 return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
478 case HV_KVP_OP_SET_IP_INFO:
480 case HV_KVP_OP_DELETE:
483 case HV_KVP_OP_ENUMERATE:
484 host_exchg_data = &hmsg->body.kvp_enum_data.data;
485 key_name = umsg->body.kvp_enum_data.data.key;
486 hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
487 ((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
488 key_name, strlen(key_name),
491 host_exchg_data->key_size = 2 * (hkey_len + 1);
492 value = umsg->body.kvp_enum_data.data.msg_value.value;
493 hvalue_len = utf8_to_utf16(
494 (uint16_t *)host_exchg_data->msg_value.value,
495 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
496 value, strlen(value),
498 host_exchg_data->value_size = 2 * (hvalue_len + 1);
499 host_exchg_data->value_type = HV_REG_SZ;
501 if ((hkey_len < 0) || (hvalue_len < 0))
507 host_exchg_data = &hmsg->body.kvp_get.data;
508 value = umsg->body.kvp_get.data.msg_value.value;
509 hvalue_len = utf8_to_utf16(
510 (uint16_t *)host_exchg_data->msg_value.value,
511 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
512 value, strlen(value),
514 /* Convert value size to uft16 */
515 host_exchg_data->value_size = 2 * (hvalue_len + 1);
516 /* Use values by string */
517 host_exchg_data->value_type = HV_REG_SZ;
519 if ((hkey_len < 0) || (hvalue_len < 0))
531 * Send the response back to the host.
534 hv_kvp_respond_host(hv_kvp_sc *sc, uint32_t error)
536 struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
538 hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
539 &sc->rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
541 hv_icmsg_hdrp->status = error;
542 hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION |
543 HV_ICMSGHDRFLAG_RESPONSE;
545 error = vmbus_chan_send(vmbus_get_channel(sc->dev),
546 VMBUS_CHANPKT_TYPE_INBAND, 0, sc->rcv_buf, sc->host_msg_len,
549 hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
555 * This is the main kvp kernel process that interacts with both user daemon
559 hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc)
561 struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
562 struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
564 /* Prepare kvp_msg to be sent to user */
565 hv_kvp_convert_hostmsg_to_usermsg(hmsg, umsg);
567 /* Send the msg to user via function deamon_read - setting sema */
568 sema_post(&sc->dev_sema);
570 /* We should wake up the daemon, in case it's doing poll() */
571 selwakeup(&sc->hv_kvp_selinfo);
576 * Function to read the kvp request buffer from host
577 * and interact with daemon
580 hv_kvp_process_request(void *context, int pending)
583 struct vmbus_channel *channel;
584 uint32_t recvlen = 0;
586 struct hv_vmbus_icmsg_hdr *icmsghdrp;
590 hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
592 sc = (hv_kvp_sc*)context;
593 kvp_buf = sc->util_sc.receive_buffer;;
594 channel = vmbus_get_channel(sc->dev);
596 recvlen = sc->util_sc.ic_buflen;
597 ret = vmbus_chan_recv(channel, kvp_buf, &recvlen, &requestid);
598 KASSERT(ret != ENOBUFS, ("hvkvp recvbuf is not large enough"));
599 /* XXX check recvlen to make sure that it contains enough data */
601 while ((ret == 0) && (recvlen > 0)) {
602 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
603 &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
605 hv_kvp_transaction_init(sc, recvlen, requestid, kvp_buf);
606 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
607 error = vmbus_ic_negomsg(&sc->util_sc,
608 kvp_buf, &recvlen, KVP_FWVER, KVP_MSGVER);
609 /* XXX handle vmbus_ic_negomsg failure. */
611 hv_kvp_respond_host(sc, HV_S_OK);
613 hv_kvp_respond_host(sc, HV_E_FAIL);
615 * It is ok to not acquire the mutex before setting
616 * req_in_progress here because negotiation is the
617 * first thing that happens and hence there is no
618 * chance of a race condition.
621 sc->req_in_progress = false;
622 hv_kvp_log_info("%s :version negotiated\n", __func__);
625 if (!sc->daemon_busy) {
627 hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
628 mtx_lock(&sc->pending_mutex);
629 sc->req_timed_out = false;
630 sc->daemon_busy = true;
631 mtx_unlock(&sc->pending_mutex);
633 hv_kvp_send_msg_to_daemon(sc);
634 hv_kvp_log_info("%s: waiting for daemon\n", __func__);
637 /* Wait 5 seconds for daemon to respond back */
638 tsleep(sc, 0, "kvpworkitem", 5 * hz);
639 hv_kvp_log_info("%s: came out of wait\n", __func__);
642 mtx_lock(&sc->pending_mutex);
644 /* Notice that once req_timed_out is set to true
645 * it will remain true until the next request is
646 * sent to the daemon. The response from daemon
647 * is forwarded to host only when this flag is
650 sc->req_timed_out = true;
653 * Cancel request if so need be.
655 if (hv_kvp_req_in_progress(sc)) {
656 hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
657 hv_kvp_respond_host(sc, HV_E_FAIL);
658 sc->req_in_progress = false;
661 mtx_unlock(&sc->pending_mutex);
664 * Try reading next buffer
666 recvlen = sc->util_sc.ic_buflen;
667 ret = vmbus_chan_recv(channel, kvp_buf, &recvlen, &requestid);
668 KASSERT(ret != ENOBUFS, ("hvkvp recvbuf is not large enough"));
669 /* XXX check recvlen to make sure that it contains enough data */
671 hv_kvp_log_info("%s: read: context %p, ret =%d, recvlen=%d\n",
672 __func__, context, ret, recvlen);
678 * Callback routine that gets called whenever there is a message from host
681 hv_kvp_callback(struct vmbus_channel *chan __unused, void *context)
683 hv_kvp_sc *sc = (hv_kvp_sc*)context;
685 The first request from host will not be handled until daemon is registered.
686 when callback is triggered without a registered daemon, callback just return.
687 When a new daemon gets regsitered, this callbcak is trigged from _write op.
689 if (sc->register_done) {
690 hv_kvp_log_info("%s: Queuing work item\n", __func__);
691 taskqueue_enqueue(taskqueue_thread, &sc->task);
696 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
699 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
701 hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
702 if (sc->dev_accessed)
705 sc->daemon_task = curproc;
706 sc->dev_accessed = true;
707 sc->daemon_busy = false;
713 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
714 struct thread *td __unused)
716 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
718 hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
719 sc->dev_accessed = false;
720 sc->register_done = false;
726 * hv_kvp_daemon read invokes this function
727 * acts as a send to daemon
730 hv_kvp_dev_daemon_read(struct cdev *dev, struct uio *uio, int ioflag __unused)
734 struct hv_kvp_msg *hv_kvp_dev_buf;
735 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
737 /* Read is not allowed util registering is done. */
738 if (!sc->register_done)
741 sema_wait(&sc->dev_sema);
743 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
744 memcpy(hv_kvp_dev_buf, &sc->daemon_kvp_msg, sizeof(struct hv_kvp_msg));
746 amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
747 BUFFERSIZE + 1 - uio->uio_offset);
749 if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
750 hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
752 free(hv_kvp_dev_buf, M_TEMP);
758 * hv_kvp_daemon write invokes this function
759 * acts as a recieve from daemon
762 hv_kvp_dev_daemon_write(struct cdev *dev, struct uio *uio, int ioflag __unused)
766 struct hv_kvp_msg *hv_kvp_dev_buf;
767 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
770 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
772 amt = MIN(uio->uio_resid, BUFFERSIZE);
773 error = uiomove(hv_kvp_dev_buf, amt, uio);
776 free(hv_kvp_dev_buf, M_TEMP);
779 memcpy(&sc->daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
781 free(hv_kvp_dev_buf, M_TEMP);
782 if (sc->register_done == false) {
783 if (sc->daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
784 sc->register_done = true;
785 hv_kvp_callback(vmbus_get_channel(sc->dev), dev->si_drv1);
788 hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
793 mtx_lock(&sc->pending_mutex);
795 if(!sc->req_timed_out) {
796 struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
797 struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
799 error = hv_kvp_convert_usermsg_to_hostmsg(umsg, hmsg);
800 hv_kvp_respond_host(sc, umsg->hdr.error);
802 sc->req_in_progress = false;
803 if (umsg->hdr.error != HV_S_OK)
804 hv_kvp_log_info("%s, Error 0x%x from daemon\n",
805 __func__, umsg->hdr.error);
807 hv_kvp_log_info("%s, Error from convert\n", __func__);
810 sc->daemon_busy = false;
811 mtx_unlock(&sc->pending_mutex);
819 * hv_kvp_daemon poll invokes this function to check if data is available
820 * for daemon to read.
823 hv_kvp_dev_daemon_poll(struct cdev *dev, int events, struct thread *td)
826 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
828 mtx_lock(&sc->pending_mutex);
830 * We check global flag daemon_busy for the data availiability for
831 * userland to read. Deamon_busy is set to true before driver has data
832 * for daemon to read. It is set to false after daemon sends
833 * then response back to driver.
835 if (sc->daemon_busy == true)
838 selrecord(td, &sc->hv_kvp_selinfo);
840 mtx_unlock(&sc->pending_mutex);
846 hv_kvp_probe(device_t dev)
849 return (vmbus_ic_probe(dev, vmbus_kvp_descs));
853 hv_kvp_attach(device_t dev)
856 struct sysctl_oid_list *child;
857 struct sysctl_ctx_list *ctx;
859 hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
862 sema_init(&sc->dev_sema, 0, "hv_kvp device semaphore");
863 mtx_init(&sc->pending_mutex, "hv-kvp pending mutex",
866 ctx = device_get_sysctl_ctx(dev);
867 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
869 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "hv_kvp_log",
870 CTLFLAG_RWTUN, &hv_kvp_log, 0, "Hyperv KVP service log level");
872 TASK_INIT(&sc->task, 0, hv_kvp_process_request, sc);
874 /* create character device */
875 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
886 sc->hv_kvp_dev->si_drv1 = sc;
888 return hv_util_attach(dev, hv_kvp_callback);
892 hv_kvp_detach(device_t dev)
894 hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
896 if (sc->daemon_task != NULL) {
897 PROC_LOCK(sc->daemon_task);
898 kern_psignal(sc->daemon_task, SIGKILL);
899 PROC_UNLOCK(sc->daemon_task);
902 destroy_dev(sc->hv_kvp_dev);
903 return hv_util_detach(dev);
906 static device_method_t kvp_methods[] = {
907 /* Device interface */
908 DEVMETHOD(device_probe, hv_kvp_probe),
909 DEVMETHOD(device_attach, hv_kvp_attach),
910 DEVMETHOD(device_detach, hv_kvp_detach),
914 static driver_t kvp_driver = { "hvkvp", kvp_methods, sizeof(hv_kvp_sc)};
916 static devclass_t kvp_devclass;
918 DRIVER_MODULE(hv_kvp, vmbus, kvp_driver, kvp_devclass, NULL, NULL);
919 MODULE_VERSION(hv_kvp, 1);
920 MODULE_DEPEND(hv_kvp, vmbus, 1, 1, 1);