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/sysctl.h>
50 #include <sys/kthread.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
54 #include <sys/endian.h>
55 #include <sys/_null.h>
56 #include <sys/signal.h>
57 #include <sys/syslog.h>
58 #include <sys/mutex.h>
59 #include <net/if_arp.h>
61 #include <dev/hyperv/include/hyperv.h>
62 #include <dev/hyperv/netvsc/hv_net_vsc.h>
68 #define BUFFERSIZE sizeof(struct hv_kvp_msg)
71 #define kvp_hdr hdr.kvp_hdr
73 /* hv_kvp debug control */
74 static int hv_kvp_log = 0;
75 SYSCTL_INT(_dev, OID_AUTO, hv_kvp_log, CTLFLAG_RW, &hv_kvp_log, 0,
78 #define hv_kvp_log_error(...) do { \
80 log(LOG_ERR, "hv_kvp: " __VA_ARGS__); \
83 #define hv_kvp_log_info(...) do { \
85 log(LOG_INFO, "hv_kvp: " __VA_ARGS__); \
88 /* character device prototypes */
89 static d_open_t hv_kvp_dev_open;
90 static d_close_t hv_kvp_dev_close;
91 static d_read_t hv_kvp_dev_daemon_read;
92 static d_write_t hv_kvp_dev_daemon_write;
93 static d_poll_t hv_kvp_dev_daemon_poll;
95 /* hv_kvp prototypes */
96 static int hv_kvp_req_in_progress(void);
97 static void hv_kvp_transaction_init(uint32_t, hv_vmbus_channel *, uint64_t, uint8_t *);
98 static void hv_kvp_send_msg_to_daemon(void);
99 static void hv_kvp_process_request(void *context);
101 /* hv_kvp character device structure */
102 static struct cdevsw hv_kvp_cdevsw =
104 .d_version = D_VERSION,
105 .d_open = hv_kvp_dev_open,
106 .d_close = hv_kvp_dev_close,
107 .d_read = hv_kvp_dev_daemon_read,
108 .d_write = hv_kvp_dev_daemon_write,
109 .d_poll = hv_kvp_dev_daemon_poll,
110 .d_name = "hv_kvp_dev",
112 static struct cdev *hv_kvp_dev;
113 static struct hv_kvp_msg *hv_kvp_dev_buf;
114 struct proc *daemon_task;
117 * Global state to track and synchronize multiple
118 * KVP transaction requests from the host.
122 /* Pre-allocated work item for queue */
123 hv_work_item work_item;
125 /* Unless specified the pending mutex should be
126 * used to alter the values of the following paramters:
131 struct mtx pending_mutex;
133 /* To track if transaction is active or not */
134 boolean_t req_in_progress;
135 /* Tracks if daemon did not reply back in time */
136 boolean_t req_timed_out;
137 /* Tracks if daemon is serving a request currently */
138 boolean_t daemon_busy;
139 /* Count of KVP requests from Hyper-V. */
140 uint64_t pending_reqs;
143 /* Length of host message */
144 uint32_t host_msg_len;
146 /* Pointer to channel */
147 hv_vmbus_channel *channelp;
149 /* Host message id */
150 uint64_t host_msg_id;
152 /* Current kvp message from the host */
153 struct hv_kvp_msg *host_kvp_msg;
155 /* Current kvp message for daemon */
156 struct hv_kvp_msg daemon_kvp_msg;
158 /* Rcv buffer for communicating with the host*/
161 /* Device semaphore to control communication */
162 struct sema dev_sema;
164 /* Indicates if daemon registered with driver */
165 boolean_t register_done;
167 /* Character device status */
168 boolean_t dev_accessed;
172 MALLOC_DECLARE(M_HV_KVP_DEV_BUF);
173 MALLOC_DEFINE(M_HV_KVP_DEV_BUF, "hv_kvp_dev buffer", "buffer for hv_kvp_dev module");
176 * hv_kvp low level functions
180 * Check if kvp transaction is in progres
183 hv_kvp_req_in_progress(void)
186 return (kvp_globals.req_in_progress);
191 * This routine is called whenever a message is received from the host
194 hv_kvp_transaction_init(uint32_t rcv_len, hv_vmbus_channel *rcv_channel,
195 uint64_t request_id, uint8_t *rcv_buf)
198 /* Store all the relevant message details in the global structure */
199 /* Do not need to use mutex for req_in_progress here */
200 kvp_globals.req_in_progress = true;
201 kvp_globals.host_msg_len = rcv_len;
202 kvp_globals.channelp = rcv_channel;
203 kvp_globals.host_msg_id = request_id;
204 kvp_globals.rcv_buf = rcv_buf;
205 kvp_globals.host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
206 sizeof(struct hv_vmbus_pipe_hdr) +
207 sizeof(struct hv_vmbus_icmsg_hdr)];
212 * hv_kvp - version neogtiation function
215 hv_kvp_negotiate_version(struct hv_vmbus_icmsg_hdr *icmsghdrp,
216 struct hv_vmbus_icmsg_negotiate *negop,
222 icmsghdrp->icmsgsize = 0x10;
224 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
225 sizeof(struct hv_vmbus_pipe_hdr) +
226 sizeof(struct hv_vmbus_icmsg_hdr)];
227 icframe_vercnt = negop->icframe_vercnt;
228 icmsg_vercnt = negop->icmsg_vercnt;
231 * Select the framework version number we will support
233 if ((icframe_vercnt >= 2) && (negop->icversion_data[1].major == 3)) {
235 if (icmsg_vercnt >= 2)
244 negop->icframe_vercnt = 1;
245 negop->icmsg_vercnt = 1;
246 negop->icversion_data[0].major = icframe_vercnt;
247 negop->icversion_data[0].minor = 0;
248 negop->icversion_data[1].major = icmsg_vercnt;
249 negop->icversion_data[1].minor = 0;
254 * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
257 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
258 struct hv_kvp_ip_msg *host_ip_msg)
260 int err_ip, err_subnet, err_gway, err_dns, err_adap;
263 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
265 (char *)umsg->body.kvp_ip_val.ip_addr,
266 strlen((char *)umsg->body.kvp_ip_val.ip_addr),
269 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
271 (char *)umsg->body.kvp_ip_val.sub_net,
272 strlen((char *)umsg->body.kvp_ip_val.sub_net),
275 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
277 (char *)umsg->body.kvp_ip_val.gate_way,
278 strlen((char *)umsg->body.kvp_ip_val.gate_way),
281 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
283 (char *)umsg->body.kvp_ip_val.dns_addr,
284 strlen((char *)umsg->body.kvp_ip_val.dns_addr),
287 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
289 (char *)umsg->body.kvp_ip_val.adapter_id,
290 strlen((char *)umsg->body.kvp_ip_val.adapter_id),
294 host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
295 host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
297 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
302 * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
305 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
306 struct hv_kvp_msg *umsg)
308 int err_ip, err_subnet, err_gway, err_dns, err_adap;
311 struct hv_device *hv_dev; /* GUID Data Structure */
312 hn_softc_t *sc; /* hn softc structure */
314 unsigned char guid_instance[40];
315 char *guid_data = NULL;
318 struct guid_extract {
331 struct guid_extract *id;
336 utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
338 (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
343 /* Adapter ID : GUID */
344 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
346 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
351 if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
352 for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
353 sc = device_get_softc(devs[devcnt]);
355 /* Trying to find GUID of Network Device */
356 hv_dev = sc->hn_dev_obj;
358 for (guid_index = 0; guid_index < 16; guid_index++) {
359 sprintf(&guid_instance[guid_index * 2], "%02x",
360 hv_dev->device_id.data[guid_index]);
363 guid_data = (char *)guid_instance;
364 id = (struct guid_extract *)guid_data;
365 snprintf(buf, sizeof(buf), "{%.2s%.2s%.2s%.2s-%.2s%.2s-%.2s%.2s-%.4s-%s}",
366 id->a4, id->a3, id->a2, id->a1,
367 id->b2, id->b1, id->c2, id->c1, id->d, id->e);
369 sprintf(if_name, "%s%d", "hn", device_get_unit(devs[devcnt]));
371 if (strncmp(buf, (char *)umsg->body.kvp_ip_val.adapter_id, 39) == 0) {
372 strcpy((char *)umsg->body.kvp_ip_val.adapter_id, if_name);
379 /* Address Family , DHCP , SUBNET, Gateway, DNS */
380 umsg->kvp_hdr.operation = host_ip_msg->operation;
381 umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
382 umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
383 utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
384 (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
389 utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
390 (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
395 utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
396 (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
401 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
406 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
407 * Ensure utf16_utf8 takes care of the additional string terminating char!!
410 hv_kvp_convert_hostmsg_to_usermsg(void)
414 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)
415 kvp_globals.host_kvp_msg;
417 struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
418 struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
420 memset(umsg, 0, sizeof(struct hv_kvp_msg));
422 umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
423 umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
425 switch (umsg->kvp_hdr.operation) {
426 case HV_KVP_OP_SET_IP_INFO:
427 hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
430 case HV_KVP_OP_GET_IP_INFO:
431 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
433 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
434 MAX_ADAPTER_ID_SIZE, 1, &utf_err);
436 umsg->body.kvp_ip_val.addr_family =
437 host_ip_msg->kvp_ip_val.addr_family;
441 value_type = hmsg->body.kvp_set.data.value_type;
443 switch (value_type) {
445 umsg->body.kvp_set.data.value_size =
447 (char *)umsg->body.kvp_set.data.msg_value.value,
448 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
449 (uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
450 hmsg->body.kvp_set.data.value_size,
453 umsg->body.kvp_set.data.value_size =
454 umsg->body.kvp_set.data.value_size / 2;
458 umsg->body.kvp_set.data.value_size =
459 sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
460 hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
464 umsg->body.kvp_set.data.value_size =
465 sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
467 hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
471 umsg->body.kvp_set.data.key_size =
473 umsg->body.kvp_set.data.key,
474 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
475 (uint16_t *)hmsg->body.kvp_set.data.key,
476 hmsg->body.kvp_set.data.key_size,
480 umsg->body.kvp_set.data.key_size =
481 umsg->body.kvp_set.data.key_size / 2;
485 umsg->body.kvp_get.data.key_size =
486 utf16_to_utf8(umsg->body.kvp_get.data.key,
487 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
488 (uint16_t *)hmsg->body.kvp_get.data.key,
489 hmsg->body.kvp_get.data.key_size,
492 umsg->body.kvp_get.data.key_size =
493 umsg->body.kvp_get.data.key_size / 2;
496 case HV_KVP_OP_DELETE:
497 umsg->body.kvp_delete.key_size =
498 utf16_to_utf8(umsg->body.kvp_delete.key,
499 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
500 (uint16_t *)hmsg->body.kvp_delete.key,
501 hmsg->body.kvp_delete.key_size,
504 umsg->body.kvp_delete.key_size =
505 umsg->body.kvp_delete.key_size / 2;
508 case HV_KVP_OP_ENUMERATE:
509 umsg->body.kvp_enum_data.index =
510 hmsg->body.kvp_enum_data.index;
514 hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
515 __func__, umsg->kvp_hdr.operation);
521 * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
524 hv_kvp_convert_usermsg_to_hostmsg(void)
526 int hkey_len = 0, hvalue_len = 0, utf_err = 0;
527 struct hv_kvp_exchg_msg_value *host_exchg_data;
528 char *key_name, *value;
530 struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
531 struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
532 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
534 switch (hmsg->kvp_hdr.operation) {
535 case HV_KVP_OP_GET_IP_INFO:
536 return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
538 case HV_KVP_OP_SET_IP_INFO:
540 case HV_KVP_OP_DELETE:
541 return (KVP_SUCCESS);
543 case HV_KVP_OP_ENUMERATE:
544 host_exchg_data = &hmsg->body.kvp_enum_data.data;
545 key_name = umsg->body.kvp_enum_data.data.key;
546 hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
547 ((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
548 key_name, strlen(key_name),
551 host_exchg_data->key_size = 2 * (hkey_len + 1);
552 value = umsg->body.kvp_enum_data.data.msg_value.value;
553 hvalue_len = utf8_to_utf16(
554 (uint16_t *)host_exchg_data->msg_value.value,
555 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
556 value, strlen(value),
558 host_exchg_data->value_size = 2 * (hvalue_len + 1);
559 host_exchg_data->value_type = HV_REG_SZ;
561 if ((hkey_len < 0) || (hvalue_len < 0))
562 return (HV_KVP_E_FAIL);
564 return (KVP_SUCCESS);
567 host_exchg_data = &hmsg->body.kvp_get.data;
568 value = umsg->body.kvp_get.data.msg_value.value;
569 hvalue_len = utf8_to_utf16(
570 (uint16_t *)host_exchg_data->msg_value.value,
571 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
572 value, strlen(value),
574 /* Convert value size to uft16 */
575 host_exchg_data->value_size = 2 * (hvalue_len + 1);
576 /* Use values by string */
577 host_exchg_data->value_type = HV_REG_SZ;
579 if ((hkey_len < 0) || (hvalue_len < 0))
580 return (HV_KVP_E_FAIL);
582 return (KVP_SUCCESS);
585 return (HV_KVP_E_FAIL);
591 * Send the response back to the host.
594 hv_kvp_respond_host(int error)
596 struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
598 hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
599 &kvp_globals.rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
602 error = HV_KVP_E_FAIL;
604 hv_icmsg_hdrp->status = error;
605 hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION | HV_ICMSGHDRFLAG_RESPONSE;
607 error = hv_vmbus_channel_send_packet(kvp_globals.channelp,
609 kvp_globals.host_msg_len, kvp_globals.host_msg_id,
610 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
613 hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
619 * This is the main kvp kernel process that interacts with both user daemon
623 hv_kvp_send_msg_to_daemon(void)
625 /* Prepare kvp_msg to be sent to user */
626 hv_kvp_convert_hostmsg_to_usermsg();
628 /* Send the msg to user via function deamon_read - setting sema */
629 sema_post(&kvp_globals.dev_sema);
634 * Function to read the kvp request buffer from host
635 * and interact with daemon
638 hv_kvp_process_request(void *context)
641 hv_vmbus_channel *channel = context;
642 uint32_t recvlen = 0;
644 struct hv_vmbus_icmsg_hdr *icmsghdrp;
646 uint64_t pending_cnt = 1;
648 hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
649 kvp_buf = receive_buffer[HV_KVP];
650 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
651 &recvlen, &requestid);
654 * We start counting only after the daemon registers
655 * and therefore there could be requests pending in
656 * the VMBus that are not reflected in pending_cnt.
657 * Therefore we continue reading as long as either of
658 * the below conditions is true.
661 while ((pending_cnt>0) || ((ret == 0) && (recvlen > 0))) {
663 if ((ret == 0) && (recvlen>0)) {
665 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
666 &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
668 hv_kvp_transaction_init(recvlen, channel, requestid, kvp_buf);
669 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
670 hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
671 hv_kvp_respond_host(ret);
674 * It is ok to not acquire the mutex before setting
675 * req_in_progress here because negotiation is the
676 * first thing that happens and hence there is no
677 * chance of a race condition.
680 kvp_globals.req_in_progress = false;
681 hv_kvp_log_info("%s :version negotiated\n", __func__);
684 if (!kvp_globals.daemon_busy) {
686 hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
687 mtx_lock(&kvp_globals.pending_mutex);
688 kvp_globals.req_timed_out = false;
689 kvp_globals.daemon_busy = true;
690 mtx_unlock(&kvp_globals.pending_mutex);
692 hv_kvp_send_msg_to_daemon();
693 hv_kvp_log_info("%s: waiting for daemon\n", __func__);
696 /* Wait 5 seconds for daemon to respond back */
697 tsleep(&kvp_globals, 0, "kvpworkitem", 5 * hz);
698 hv_kvp_log_info("%s: came out of wait\n", __func__);
702 mtx_lock(&kvp_globals.pending_mutex);
704 /* Notice that once req_timed_out is set to true
705 * it will remain true until the next request is
706 * sent to the daemon. The response from daemon
707 * is forwarded to host only when this flag is
710 kvp_globals.req_timed_out = true;
713 * Cancel request if so need be.
715 if (hv_kvp_req_in_progress()) {
716 hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
717 hv_kvp_respond_host(HV_KVP_E_FAIL);
718 kvp_globals.req_in_progress = false;
722 * Decrement pending request count and
724 if (kvp_globals.pending_reqs>0) {
725 kvp_globals.pending_reqs = kvp_globals.pending_reqs - 1;
727 pending_cnt = kvp_globals.pending_reqs;
729 mtx_unlock(&kvp_globals.pending_mutex);
732 * Try reading next buffer
735 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
736 &recvlen, &requestid);
737 hv_kvp_log_info("%s: read: context %p, pending_cnt %ju ret =%d, recvlen=%d\n",
738 __func__, context, pending_cnt, ret, recvlen);
744 * Callback routine that gets called whenever there is a message from host
747 hv_kvp_callback(void *context)
749 uint64_t pending_cnt = 0;
751 if (kvp_globals.register_done == false) {
753 kvp_globals.channelp = context;
756 mtx_lock(&kvp_globals.pending_mutex);
757 kvp_globals.pending_reqs = kvp_globals.pending_reqs + 1;
758 pending_cnt = kvp_globals.pending_reqs;
759 mtx_unlock(&kvp_globals.pending_mutex);
760 if (pending_cnt == 1) {
761 hv_kvp_log_info("%s: Queuing work item\n", __func__);
763 service_table[HV_KVP].work_queue,
764 hv_kvp_process_request,
773 * This function is called by the hv_kvp_init -
774 * creates character device hv_kvp_dev
775 * allocates memory to hv_kvp_dev_buf
779 hv_kvp_dev_init(void)
783 /* initialize semaphore */
784 sema_init(&kvp_globals.dev_sema, 0, "hv_kvp device semaphore");
785 /* create character device */
786 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
799 * Malloc with M_WAITOK flag will never fail.
801 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_HV_KVP_DEV_BUF, M_WAITOK |
809 * This function is called by the hv_kvp_deinit -
810 * destroy character device
813 hv_kvp_dev_destroy(void)
816 if (daemon_task != NULL) {
817 PROC_LOCK(daemon_task);
818 kern_psignal(daemon_task, SIGKILL);
819 PROC_UNLOCK(daemon_task);
822 destroy_dev(hv_kvp_dev);
823 free(hv_kvp_dev_buf, M_HV_KVP_DEV_BUF);
829 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
833 hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
834 if (kvp_globals.dev_accessed)
837 daemon_task = curproc;
838 kvp_globals.dev_accessed = true;
839 kvp_globals.daemon_busy = false;
845 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
846 struct thread *td __unused)
849 hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
850 kvp_globals.dev_accessed = false;
851 kvp_globals.register_done = false;
857 * hv_kvp_daemon read invokes this function
858 * acts as a send to daemon
861 hv_kvp_dev_daemon_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
866 /* Check hv_kvp daemon registration status*/
867 if (!kvp_globals.register_done)
870 sema_wait(&kvp_globals.dev_sema);
872 memcpy(hv_kvp_dev_buf, &kvp_globals.daemon_kvp_msg, sizeof(struct hv_kvp_msg));
874 amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
875 BUFFERSIZE + 1 - uio->uio_offset);
877 if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
878 hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
885 * hv_kvp_daemon write invokes this function
886 * acts as a recieve from daemon
889 hv_kvp_dev_daemon_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
896 amt = MIN(uio->uio_resid, BUFFERSIZE);
897 error = uiomove(hv_kvp_dev_buf, amt, uio);
902 memcpy(&kvp_globals.daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
904 if (kvp_globals.register_done == false) {
905 if (kvp_globals.daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
907 kvp_globals.register_done = true;
908 if (kvp_globals.channelp) {
910 hv_kvp_callback(kvp_globals.channelp);
914 hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
919 mtx_lock(&kvp_globals.pending_mutex);
921 if(!kvp_globals.req_timed_out) {
923 hv_kvp_convert_usermsg_to_hostmsg();
924 hv_kvp_respond_host(KVP_SUCCESS);
925 wakeup(&kvp_globals);
926 kvp_globals.req_in_progress = false;
929 kvp_globals.daemon_busy = false;
930 mtx_unlock(&kvp_globals.pending_mutex);
938 * hv_kvp_daemon poll invokes this function to check if data is available
939 * for daemon to read.
942 hv_kvp_dev_daemon_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
946 mtx_lock(&kvp_globals.pending_mutex);
948 * We check global flag daemon_busy for the data availiability for
949 * userland to read. Deamon_busy is set to true before driver has data
950 * for daemon to read. It is set to false after daemon sends
951 * then response back to driver.
953 if (kvp_globals.daemon_busy == true)
955 mtx_unlock(&kvp_globals.pending_mutex);
962 * hv_kvp initialization function
963 * called from hv_util service.
967 hv_kvp_init(hv_vmbus_service *srv)
970 hv_work_queue *work_queue = NULL;
972 memset(&kvp_globals, 0, sizeof(kvp_globals));
974 work_queue = hv_work_queue_create("KVP Service");
975 if (work_queue == NULL) {
976 hv_kvp_log_info("%s: Work queue alloc failed\n", __func__);
978 hv_kvp_log_error("%s: ENOMEM\n", __func__);
981 srv->work_queue = work_queue;
983 error = hv_kvp_dev_init();
984 mtx_init(&kvp_globals.pending_mutex, "hv-kvp pending mutex",
986 kvp_globals.pending_reqs = 0;
997 hv_kvp_dev_destroy();
998 mtx_destroy(&kvp_globals.pending_mutex);