2 * Copyright (c) 2014 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>
57 #include <sys/signal.h>
58 #include <sys/syslog.h>
59 #include <sys/systm.h>
60 #include <sys/mutex.h>
61 #include <net/if_arp.h>
63 #include <dev/hyperv/include/hyperv.h>
64 #include <dev/hyperv/netvsc/hv_net_vsc.h>
71 #define BUFFERSIZE sizeof(struct hv_kvp_msg)
74 #define kvp_hdr hdr.kvp_hdr
76 /* hv_kvp debug control */
77 static int hv_kvp_log = 0;
79 #define hv_kvp_log_error(...) do { \
81 log(LOG_ERR, "hv_kvp: " __VA_ARGS__); \
84 #define hv_kvp_log_info(...) do { \
86 log(LOG_INFO, "hv_kvp: " __VA_ARGS__); \
89 static hv_guid service_guid = { .data =
90 {0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
91 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6} };
93 /* character device prototypes */
94 static d_open_t hv_kvp_dev_open;
95 static d_close_t hv_kvp_dev_close;
96 static d_read_t hv_kvp_dev_daemon_read;
97 static d_write_t hv_kvp_dev_daemon_write;
98 static d_poll_t hv_kvp_dev_daemon_poll;
100 /* hv_kvp character device structure */
101 static struct cdevsw hv_kvp_cdevsw =
103 .d_version = D_VERSION,
104 .d_open = hv_kvp_dev_open,
105 .d_close = hv_kvp_dev_close,
106 .d_read = hv_kvp_dev_daemon_read,
107 .d_write = hv_kvp_dev_daemon_write,
108 .d_poll = hv_kvp_dev_daemon_poll,
109 .d_name = "hv_kvp_dev",
114 * Global state to track and synchronize multiple
115 * KVP transaction requests from the host.
117 typedef struct hv_kvp_sc {
118 struct hv_util_sc util_sc;
120 /* Unless specified the pending mutex should be
121 * used to alter the values of the following paramters:
125 struct mtx pending_mutex;
129 /* To track if transaction is active or not */
130 boolean_t req_in_progress;
131 /* Tracks if daemon did not reply back in time */
132 boolean_t req_timed_out;
133 /* Tracks if daemon is serving a request currently */
134 boolean_t daemon_busy;
136 /* Length of host message */
137 uint32_t host_msg_len;
139 /* Host message id */
140 uint64_t host_msg_id;
142 /* Current kvp message from the host */
143 struct hv_kvp_msg *host_kvp_msg;
145 /* Current kvp message for daemon */
146 struct hv_kvp_msg daemon_kvp_msg;
148 /* Rcv buffer for communicating with the host*/
151 /* Device semaphore to control communication */
152 struct sema dev_sema;
154 /* Indicates if daemon registered with driver */
155 boolean_t register_done;
157 /* Character device status */
158 boolean_t dev_accessed;
160 struct cdev *hv_kvp_dev;
162 struct proc *daemon_task;
164 struct selinfo hv_kvp_selinfo;
167 /* hv_kvp prototypes */
168 static int hv_kvp_req_in_progress(hv_kvp_sc *sc);
169 static void hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t, uint64_t, uint8_t *);
170 static void hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc);
171 static void hv_kvp_process_request(void *context, int pending);
174 * hv_kvp low level functions
178 * Check if kvp transaction is in progres
181 hv_kvp_req_in_progress(hv_kvp_sc *sc)
184 return (sc->req_in_progress);
189 * This routine is called whenever a message is received from the host
192 hv_kvp_transaction_init(hv_kvp_sc *sc, uint32_t rcv_len,
193 uint64_t request_id, uint8_t *rcv_buf)
196 /* Store all the relevant message details in the global structure */
197 /* Do not need to use mutex for req_in_progress here */
198 sc->req_in_progress = true;
199 sc->host_msg_len = rcv_len;
200 sc->host_msg_id = request_id;
201 sc->rcv_buf = rcv_buf;
202 sc->host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
203 sizeof(struct hv_vmbus_pipe_hdr) +
204 sizeof(struct hv_vmbus_icmsg_hdr)];
209 * hv_kvp - version neogtiation function
212 hv_kvp_negotiate_version(struct hv_vmbus_icmsg_hdr *icmsghdrp,
213 struct hv_vmbus_icmsg_negotiate *negop,
219 icmsghdrp->icmsgsize = 0x10;
221 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
222 sizeof(struct hv_vmbus_pipe_hdr) +
223 sizeof(struct hv_vmbus_icmsg_hdr)];
224 icframe_vercnt = negop->icframe_vercnt;
225 icmsg_vercnt = negop->icmsg_vercnt;
228 * Select the framework version number we will support
230 if ((icframe_vercnt >= 2) && (negop->icversion_data[1].major == 3)) {
232 if (icmsg_vercnt > 2)
241 negop->icframe_vercnt = 1;
242 negop->icmsg_vercnt = 1;
243 negop->icversion_data[0].major = icframe_vercnt;
244 negop->icversion_data[0].minor = 0;
245 negop->icversion_data[1].major = icmsg_vercnt;
246 negop->icversion_data[1].minor = 0;
251 * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
254 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
255 struct hv_kvp_ip_msg *host_ip_msg)
257 int err_ip, err_subnet, err_gway, err_dns, err_adap;
260 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
262 (char *)umsg->body.kvp_ip_val.ip_addr,
263 strlen((char *)umsg->body.kvp_ip_val.ip_addr),
266 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
268 (char *)umsg->body.kvp_ip_val.sub_net,
269 strlen((char *)umsg->body.kvp_ip_val.sub_net),
272 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
274 (char *)umsg->body.kvp_ip_val.gate_way,
275 strlen((char *)umsg->body.kvp_ip_val.gate_way),
278 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
280 (char *)umsg->body.kvp_ip_val.dns_addr,
281 strlen((char *)umsg->body.kvp_ip_val.dns_addr),
284 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
286 (char *)umsg->body.kvp_ip_val.adapter_id,
287 strlen((char *)umsg->body.kvp_ip_val.adapter_id),
291 host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
292 host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
294 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
299 * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
302 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
303 struct hv_kvp_msg *umsg)
305 int err_ip, err_subnet, err_gway, err_dns, err_adap;
307 struct hv_device *hv_dev; /* GUID Data Structure */
308 hn_softc_t *sc; /* hn softc structure */
316 utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
318 (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
323 /* Adapter ID : GUID */
324 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
326 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
331 if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
332 for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
333 sc = device_get_softc(devs[devcnt]);
335 /* Trying to find GUID of Network Device */
336 hv_dev = sc->hn_dev_obj;
338 snprintf_hv_guid(buf, sizeof(buf), &hv_dev->device_id);
339 sprintf(if_name, "%s%d", "hn", device_get_unit(devs[devcnt]));
341 if (strncmp(buf, (char *)umsg->body.kvp_ip_val.adapter_id, 39) == 0) {
342 strcpy((char *)umsg->body.kvp_ip_val.adapter_id, if_name);
349 /* Address Family , DHCP , SUBNET, Gateway, DNS */
350 umsg->kvp_hdr.operation = host_ip_msg->operation;
351 umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
352 umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
353 utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
354 (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
359 utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
360 (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
365 utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
366 (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
371 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
376 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
377 * Ensure utf16_utf8 takes care of the additional string terminating char!!
380 hv_kvp_convert_hostmsg_to_usermsg(struct hv_kvp_msg *hmsg, struct hv_kvp_msg *umsg)
384 struct hv_kvp_ip_msg *host_ip_msg;
386 host_ip_msg = (struct hv_kvp_ip_msg*)hmsg;
387 memset(umsg, 0, sizeof(struct hv_kvp_msg));
389 umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
390 umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
392 switch (umsg->kvp_hdr.operation) {
393 case HV_KVP_OP_SET_IP_INFO:
394 hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
397 case HV_KVP_OP_GET_IP_INFO:
398 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
400 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
401 MAX_ADAPTER_ID_SIZE, 1, &utf_err);
403 umsg->body.kvp_ip_val.addr_family =
404 host_ip_msg->kvp_ip_val.addr_family;
408 value_type = hmsg->body.kvp_set.data.value_type;
410 switch (value_type) {
412 umsg->body.kvp_set.data.value_size =
414 (char *)umsg->body.kvp_set.data.msg_value.value,
415 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
416 (uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
417 hmsg->body.kvp_set.data.value_size,
420 umsg->body.kvp_set.data.value_size =
421 umsg->body.kvp_set.data.value_size / 2;
425 umsg->body.kvp_set.data.value_size =
426 sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
427 hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
431 umsg->body.kvp_set.data.value_size =
432 sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
434 hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
438 umsg->body.kvp_set.data.key_size =
440 umsg->body.kvp_set.data.key,
441 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
442 (uint16_t *)hmsg->body.kvp_set.data.key,
443 hmsg->body.kvp_set.data.key_size,
447 umsg->body.kvp_set.data.key_size =
448 umsg->body.kvp_set.data.key_size / 2;
452 umsg->body.kvp_get.data.key_size =
453 utf16_to_utf8(umsg->body.kvp_get.data.key,
454 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
455 (uint16_t *)hmsg->body.kvp_get.data.key,
456 hmsg->body.kvp_get.data.key_size,
459 umsg->body.kvp_get.data.key_size =
460 umsg->body.kvp_get.data.key_size / 2;
463 case HV_KVP_OP_DELETE:
464 umsg->body.kvp_delete.key_size =
465 utf16_to_utf8(umsg->body.kvp_delete.key,
466 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
467 (uint16_t *)hmsg->body.kvp_delete.key,
468 hmsg->body.kvp_delete.key_size,
471 umsg->body.kvp_delete.key_size =
472 umsg->body.kvp_delete.key_size / 2;
475 case HV_KVP_OP_ENUMERATE:
476 umsg->body.kvp_enum_data.index =
477 hmsg->body.kvp_enum_data.index;
481 hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
482 __func__, umsg->kvp_hdr.operation);
488 * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
491 hv_kvp_convert_usermsg_to_hostmsg(struct hv_kvp_msg *umsg, struct hv_kvp_msg *hmsg)
493 int hkey_len = 0, hvalue_len = 0, utf_err = 0;
494 struct hv_kvp_exchg_msg_value *host_exchg_data;
495 char *key_name, *value;
497 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
499 switch (hmsg->kvp_hdr.operation) {
500 case HV_KVP_OP_GET_IP_INFO:
501 return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
503 case HV_KVP_OP_SET_IP_INFO:
505 case HV_KVP_OP_DELETE:
506 return (KVP_SUCCESS);
508 case HV_KVP_OP_ENUMERATE:
509 host_exchg_data = &hmsg->body.kvp_enum_data.data;
510 key_name = umsg->body.kvp_enum_data.data.key;
511 hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
512 ((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
513 key_name, strlen(key_name),
516 host_exchg_data->key_size = 2 * (hkey_len + 1);
517 value = umsg->body.kvp_enum_data.data.msg_value.value;
518 hvalue_len = utf8_to_utf16(
519 (uint16_t *)host_exchg_data->msg_value.value,
520 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
521 value, strlen(value),
523 host_exchg_data->value_size = 2 * (hvalue_len + 1);
524 host_exchg_data->value_type = HV_REG_SZ;
526 if ((hkey_len < 0) || (hvalue_len < 0))
527 return (HV_KVP_E_FAIL);
529 return (KVP_SUCCESS);
532 host_exchg_data = &hmsg->body.kvp_get.data;
533 value = umsg->body.kvp_get.data.msg_value.value;
534 hvalue_len = utf8_to_utf16(
535 (uint16_t *)host_exchg_data->msg_value.value,
536 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
537 value, strlen(value),
539 /* Convert value size to uft16 */
540 host_exchg_data->value_size = 2 * (hvalue_len + 1);
541 /* Use values by string */
542 host_exchg_data->value_type = HV_REG_SZ;
544 if ((hkey_len < 0) || (hvalue_len < 0))
545 return (HV_KVP_E_FAIL);
547 return (KVP_SUCCESS);
550 return (HV_KVP_E_FAIL);
556 * Send the response back to the host.
559 hv_kvp_respond_host(hv_kvp_sc *sc, int error)
561 struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
563 hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
564 &sc->rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
567 error = HV_KVP_E_FAIL;
569 hv_icmsg_hdrp->status = error;
570 hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION | HV_ICMSGHDRFLAG_RESPONSE;
572 error = hv_vmbus_channel_send_packet(sc->util_sc.hv_dev->channel,
574 sc->host_msg_len, sc->host_msg_id,
575 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
578 hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
584 * This is the main kvp kernel process that interacts with both user daemon
588 hv_kvp_send_msg_to_daemon(hv_kvp_sc *sc)
590 struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
591 struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
593 /* Prepare kvp_msg to be sent to user */
594 hv_kvp_convert_hostmsg_to_usermsg(hmsg, umsg);
596 /* Send the msg to user via function deamon_read - setting sema */
597 sema_post(&sc->dev_sema);
599 /* We should wake up the daemon, in case it's doing poll() */
600 selwakeup(&sc->hv_kvp_selinfo);
605 * Function to read the kvp request buffer from host
606 * and interact with daemon
609 hv_kvp_process_request(void *context, int pending)
612 hv_vmbus_channel *channel;
613 uint32_t recvlen = 0;
615 struct hv_vmbus_icmsg_hdr *icmsghdrp;
619 hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
621 sc = (hv_kvp_sc*)context;
622 kvp_buf = sc->util_sc.receive_buffer;;
623 channel = sc->util_sc.hv_dev->channel;
625 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
626 &recvlen, &requestid);
628 while ((ret == 0) && (recvlen > 0)) {
630 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
631 &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
633 hv_kvp_transaction_init(sc, recvlen, requestid, kvp_buf);
634 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
635 hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
636 hv_kvp_respond_host(sc, ret);
639 * It is ok to not acquire the mutex before setting
640 * req_in_progress here because negotiation is the
641 * first thing that happens and hence there is no
642 * chance of a race condition.
645 sc->req_in_progress = false;
646 hv_kvp_log_info("%s :version negotiated\n", __func__);
649 if (!sc->daemon_busy) {
651 hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
652 mtx_lock(&sc->pending_mutex);
653 sc->req_timed_out = false;
654 sc->daemon_busy = true;
655 mtx_unlock(&sc->pending_mutex);
657 hv_kvp_send_msg_to_daemon(sc);
658 hv_kvp_log_info("%s: waiting for daemon\n", __func__);
661 /* Wait 5 seconds for daemon to respond back */
662 tsleep(sc, 0, "kvpworkitem", 5 * hz);
663 hv_kvp_log_info("%s: came out of wait\n", __func__);
666 mtx_lock(&sc->pending_mutex);
668 /* Notice that once req_timed_out is set to true
669 * it will remain true until the next request is
670 * sent to the daemon. The response from daemon
671 * is forwarded to host only when this flag is
674 sc->req_timed_out = true;
677 * Cancel request if so need be.
679 if (hv_kvp_req_in_progress(sc)) {
680 hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
681 hv_kvp_respond_host(sc, HV_KVP_E_FAIL);
682 sc->req_in_progress = false;
685 mtx_unlock(&sc->pending_mutex);
688 * Try reading next buffer
691 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
692 &recvlen, &requestid);
693 hv_kvp_log_info("%s: read: context %p, ret =%d, recvlen=%d\n",
694 __func__, context, ret, recvlen);
700 * Callback routine that gets called whenever there is a message from host
703 hv_kvp_callback(void *context)
705 hv_kvp_sc *sc = (hv_kvp_sc*)context;
707 The first request from host will not be handled until daemon is registered.
708 when callback is triggered without a registered daemon, callback just return.
709 When a new daemon gets regsitered, this callbcak is trigged from _write op.
711 if (sc->register_done) {
712 hv_kvp_log_info("%s: Queuing work item\n", __func__);
713 taskqueue_enqueue(taskqueue_thread, &sc->task);
718 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
721 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
723 hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
724 if (sc->dev_accessed)
727 sc->daemon_task = curproc;
728 sc->dev_accessed = true;
729 sc->daemon_busy = false;
735 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
736 struct thread *td __unused)
738 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
740 hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
741 sc->dev_accessed = false;
742 sc->register_done = false;
748 * hv_kvp_daemon read invokes this function
749 * acts as a send to daemon
752 hv_kvp_dev_daemon_read(struct cdev *dev, struct uio *uio, int ioflag __unused)
756 struct hv_kvp_msg *hv_kvp_dev_buf;
757 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
759 /* Check hv_kvp daemon registration status*/
760 if (!sc->register_done)
763 sema_wait(&sc->dev_sema);
765 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
766 memcpy(hv_kvp_dev_buf, &sc->daemon_kvp_msg, sizeof(struct hv_kvp_msg));
768 amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
769 BUFFERSIZE + 1 - uio->uio_offset);
771 if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
772 hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
774 free(hv_kvp_dev_buf, M_TEMP);
780 * hv_kvp_daemon write invokes this function
781 * acts as a recieve from daemon
784 hv_kvp_dev_daemon_write(struct cdev *dev, struct uio *uio, int ioflag __unused)
788 struct hv_kvp_msg *hv_kvp_dev_buf;
789 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
792 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_TEMP, M_WAITOK);
794 amt = MIN(uio->uio_resid, BUFFERSIZE);
795 error = uiomove(hv_kvp_dev_buf, amt, uio);
798 free(hv_kvp_dev_buf, M_TEMP);
801 memcpy(&sc->daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
803 free(hv_kvp_dev_buf, M_TEMP);
804 if (sc->register_done == false) {
805 if (sc->daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
806 sc->register_done = true;
807 hv_kvp_callback(dev->si_drv1);
810 hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
815 mtx_lock(&sc->pending_mutex);
817 if(!sc->req_timed_out) {
818 struct hv_kvp_msg *hmsg = sc->host_kvp_msg;
819 struct hv_kvp_msg *umsg = &sc->daemon_kvp_msg;
821 hv_kvp_convert_usermsg_to_hostmsg(umsg, hmsg);
822 hv_kvp_respond_host(sc, KVP_SUCCESS);
824 sc->req_in_progress = false;
827 sc->daemon_busy = false;
828 mtx_unlock(&sc->pending_mutex);
836 * hv_kvp_daemon poll invokes this function to check if data is available
837 * for daemon to read.
840 hv_kvp_dev_daemon_poll(struct cdev *dev, int events, struct thread *td)
843 hv_kvp_sc *sc = (hv_kvp_sc*)dev->si_drv1;
845 mtx_lock(&sc->pending_mutex);
847 * We check global flag daemon_busy for the data availiability for
848 * userland to read. Deamon_busy is set to true before driver has data
849 * for daemon to read. It is set to false after daemon sends
850 * then response back to driver.
852 if (sc->daemon_busy == true)
855 selrecord(td, &sc->hv_kvp_selinfo);
857 mtx_unlock(&sc->pending_mutex);
863 hv_kvp_probe(device_t dev)
865 const char *p = vmbus_get_type(dev);
866 if (!memcmp(p, &service_guid, sizeof(hv_guid))) {
867 device_set_desc(dev, "Hyper-V KVP Service");
868 return BUS_PROBE_DEFAULT;
875 hv_kvp_attach(device_t dev)
878 struct sysctl_oid_list *child;
879 struct sysctl_ctx_list *ctx;
881 hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
883 sc->util_sc.callback = hv_kvp_callback;
884 sema_init(&sc->dev_sema, 0, "hv_kvp device semaphore");
885 mtx_init(&sc->pending_mutex, "hv-kvp pending mutex",
888 ctx = device_get_sysctl_ctx(dev);
889 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
891 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "hv_kvp_log",
892 CTLFLAG_RW, &hv_kvp_log, 0, "Hyperv KVP service log level");
894 TASK_INIT(&sc->task, 0, hv_kvp_process_request, sc);
896 /* create character device */
897 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
908 sc->hv_kvp_dev->si_drv1 = sc;
910 return hv_util_attach(dev);
914 hv_kvp_detach(device_t dev)
916 hv_kvp_sc *sc = (hv_kvp_sc*)device_get_softc(dev);
918 if (sc->daemon_task != NULL) {
919 PROC_LOCK(sc->daemon_task);
920 kern_psignal(sc->daemon_task, SIGKILL);
921 PROC_UNLOCK(sc->daemon_task);
924 destroy_dev(sc->hv_kvp_dev);
925 return hv_util_detach(dev);
928 static device_method_t kvp_methods[] = {
929 /* Device interface */
930 DEVMETHOD(device_probe, hv_kvp_probe),
931 DEVMETHOD(device_attach, hv_kvp_attach),
932 DEVMETHOD(device_detach, hv_kvp_detach),
936 static driver_t kvp_driver = { "hvkvp", kvp_methods, sizeof(hv_kvp_sc)};
938 static devclass_t kvp_devclass;
940 DRIVER_MODULE(hv_kvp, vmbus, kvp_driver, kvp_devclass, NULL, NULL);
941 MODULE_VERSION(hv_kvp, 1);
942 MODULE_DEPEND(hv_kvp, vmbus, 1, 1, 1);