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/systm.h>
59 #include <sys/mutex.h>
60 #include <net/if_arp.h>
62 #include <dev/hyperv/include/hyperv.h>
63 #include <dev/hyperv/netvsc/hv_net_vsc.h>
69 #define BUFFERSIZE sizeof(struct hv_kvp_msg)
72 #define kvp_hdr hdr.kvp_hdr
74 /* hv_kvp debug control */
75 static int hv_kvp_log = 0;
76 SYSCTL_INT(_dev, OID_AUTO, hv_kvp_log, CTLFLAG_RW, &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 /* character device prototypes */
90 static d_open_t hv_kvp_dev_open;
91 static d_close_t hv_kvp_dev_close;
92 static d_read_t hv_kvp_dev_daemon_read;
93 static d_write_t hv_kvp_dev_daemon_write;
94 static d_poll_t hv_kvp_dev_daemon_poll;
96 /* hv_kvp prototypes */
97 static int hv_kvp_req_in_progress(void);
98 static void hv_kvp_transaction_init(uint32_t, hv_vmbus_channel *, uint64_t, uint8_t *);
99 static void hv_kvp_send_msg_to_daemon(void);
100 static void hv_kvp_process_request(void *context);
102 /* hv_kvp character device structure */
103 static struct cdevsw hv_kvp_cdevsw =
105 .d_version = D_VERSION,
106 .d_open = hv_kvp_dev_open,
107 .d_close = hv_kvp_dev_close,
108 .d_read = hv_kvp_dev_daemon_read,
109 .d_write = hv_kvp_dev_daemon_write,
110 .d_poll = hv_kvp_dev_daemon_poll,
111 .d_name = "hv_kvp_dev",
113 static struct cdev *hv_kvp_dev;
114 static struct hv_kvp_msg *hv_kvp_dev_buf;
115 struct proc *daemon_task;
118 * Global state to track and synchronize multiple
119 * KVP transaction requests from the host.
123 /* Pre-allocated work item for queue */
124 hv_work_item work_item;
126 /* Unless specified the pending mutex should be
127 * used to alter the values of the following paramters:
132 struct mtx pending_mutex;
134 /* To track if transaction is active or not */
135 boolean_t req_in_progress;
136 /* Tracks if daemon did not reply back in time */
137 boolean_t req_timed_out;
138 /* Tracks if daemon is serving a request currently */
139 boolean_t daemon_busy;
140 /* Count of KVP requests from Hyper-V. */
141 uint64_t pending_reqs;
144 /* Length of host message */
145 uint32_t host_msg_len;
147 /* Pointer to channel */
148 hv_vmbus_channel *channelp;
150 /* Host message id */
151 uint64_t host_msg_id;
153 /* Current kvp message from the host */
154 struct hv_kvp_msg *host_kvp_msg;
156 /* Current kvp message for daemon */
157 struct hv_kvp_msg daemon_kvp_msg;
159 /* Rcv buffer for communicating with the host*/
162 /* Device semaphore to control communication */
163 struct sema dev_sema;
165 /* Indicates if daemon registered with driver */
166 boolean_t register_done;
168 /* Character device status */
169 boolean_t dev_accessed;
173 MALLOC_DECLARE(M_HV_KVP_DEV_BUF);
174 MALLOC_DEFINE(M_HV_KVP_DEV_BUF, "hv_kvp_dev buffer", "buffer for hv_kvp_dev module");
177 * hv_kvp low level functions
181 * Check if kvp transaction is in progres
184 hv_kvp_req_in_progress(void)
187 return (kvp_globals.req_in_progress);
192 * This routine is called whenever a message is received from the host
195 hv_kvp_transaction_init(uint32_t rcv_len, hv_vmbus_channel *rcv_channel,
196 uint64_t request_id, uint8_t *rcv_buf)
199 /* Store all the relevant message details in the global structure */
200 /* Do not need to use mutex for req_in_progress here */
201 kvp_globals.req_in_progress = true;
202 kvp_globals.host_msg_len = rcv_len;
203 kvp_globals.channelp = rcv_channel;
204 kvp_globals.host_msg_id = request_id;
205 kvp_globals.rcv_buf = rcv_buf;
206 kvp_globals.host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
207 sizeof(struct hv_vmbus_pipe_hdr) +
208 sizeof(struct hv_vmbus_icmsg_hdr)];
213 * hv_kvp - version neogtiation function
216 hv_kvp_negotiate_version(struct hv_vmbus_icmsg_hdr *icmsghdrp,
217 struct hv_vmbus_icmsg_negotiate *negop,
223 icmsghdrp->icmsgsize = 0x10;
225 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
226 sizeof(struct hv_vmbus_pipe_hdr) +
227 sizeof(struct hv_vmbus_icmsg_hdr)];
228 icframe_vercnt = negop->icframe_vercnt;
229 icmsg_vercnt = negop->icmsg_vercnt;
232 * Select the framework version number we will support
234 if ((icframe_vercnt >= 2) && (negop->icversion_data[1].major == 3)) {
236 if (icmsg_vercnt > 2)
245 negop->icframe_vercnt = 1;
246 negop->icmsg_vercnt = 1;
247 negop->icversion_data[0].major = icframe_vercnt;
248 negop->icversion_data[0].minor = 0;
249 negop->icversion_data[1].major = icmsg_vercnt;
250 negop->icversion_data[1].minor = 0;
255 * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
258 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
259 struct hv_kvp_ip_msg *host_ip_msg)
261 int err_ip, err_subnet, err_gway, err_dns, err_adap;
264 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
266 (char *)umsg->body.kvp_ip_val.ip_addr,
267 strlen((char *)umsg->body.kvp_ip_val.ip_addr),
270 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
272 (char *)umsg->body.kvp_ip_val.sub_net,
273 strlen((char *)umsg->body.kvp_ip_val.sub_net),
276 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
278 (char *)umsg->body.kvp_ip_val.gate_way,
279 strlen((char *)umsg->body.kvp_ip_val.gate_way),
282 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
284 (char *)umsg->body.kvp_ip_val.dns_addr,
285 strlen((char *)umsg->body.kvp_ip_val.dns_addr),
288 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
290 (char *)umsg->body.kvp_ip_val.adapter_id,
291 strlen((char *)umsg->body.kvp_ip_val.adapter_id),
295 host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
296 host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
298 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
303 * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
306 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
307 struct hv_kvp_msg *umsg)
309 int err_ip, err_subnet, err_gway, err_dns, err_adap;
312 struct hv_device *hv_dev; /* GUID Data Structure */
313 hn_softc_t *sc; /* hn softc structure */
315 unsigned char guid_instance[40];
316 char *guid_data = NULL;
319 struct guid_extract {
332 struct guid_extract *id;
337 utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
339 (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
344 /* Adapter ID : GUID */
345 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
347 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
352 if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
353 for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
354 sc = device_get_softc(devs[devcnt]);
356 /* Trying to find GUID of Network Device */
357 hv_dev = sc->hn_dev_obj;
359 for (guid_index = 0; guid_index < 16; guid_index++) {
360 sprintf(&guid_instance[guid_index * 2], "%02x",
361 hv_dev->device_id.data[guid_index]);
364 guid_data = (char *)guid_instance;
365 id = (struct guid_extract *)guid_data;
366 snprintf(buf, sizeof(buf), "{%.2s%.2s%.2s%.2s-%.2s%.2s-%.2s%.2s-%.4s-%s}",
367 id->a4, id->a3, id->a2, id->a1,
368 id->b2, id->b1, id->c2, id->c1, id->d, id->e);
370 sprintf(if_name, "%s%d", "hn", device_get_unit(devs[devcnt]));
372 if (strncmp(buf, (char *)umsg->body.kvp_ip_val.adapter_id, 39) == 0) {
373 strcpy((char *)umsg->body.kvp_ip_val.adapter_id, if_name);
380 /* Address Family , DHCP , SUBNET, Gateway, DNS */
381 umsg->kvp_hdr.operation = host_ip_msg->operation;
382 umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
383 umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
384 utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
385 (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
390 utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
391 (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
396 utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
397 (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
402 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
407 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
408 * Ensure utf16_utf8 takes care of the additional string terminating char!!
411 hv_kvp_convert_hostmsg_to_usermsg(void)
415 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)
416 kvp_globals.host_kvp_msg;
418 struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
419 struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
421 memset(umsg, 0, sizeof(struct hv_kvp_msg));
423 umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
424 umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
426 switch (umsg->kvp_hdr.operation) {
427 case HV_KVP_OP_SET_IP_INFO:
428 hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
431 case HV_KVP_OP_GET_IP_INFO:
432 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
434 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
435 MAX_ADAPTER_ID_SIZE, 1, &utf_err);
437 umsg->body.kvp_ip_val.addr_family =
438 host_ip_msg->kvp_ip_val.addr_family;
442 value_type = hmsg->body.kvp_set.data.value_type;
444 switch (value_type) {
446 umsg->body.kvp_set.data.value_size =
448 (char *)umsg->body.kvp_set.data.msg_value.value,
449 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
450 (uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
451 hmsg->body.kvp_set.data.value_size,
454 umsg->body.kvp_set.data.value_size =
455 umsg->body.kvp_set.data.value_size / 2;
459 umsg->body.kvp_set.data.value_size =
460 sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
461 hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
465 umsg->body.kvp_set.data.value_size =
466 sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
468 hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
472 umsg->body.kvp_set.data.key_size =
474 umsg->body.kvp_set.data.key,
475 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
476 (uint16_t *)hmsg->body.kvp_set.data.key,
477 hmsg->body.kvp_set.data.key_size,
481 umsg->body.kvp_set.data.key_size =
482 umsg->body.kvp_set.data.key_size / 2;
486 umsg->body.kvp_get.data.key_size =
487 utf16_to_utf8(umsg->body.kvp_get.data.key,
488 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
489 (uint16_t *)hmsg->body.kvp_get.data.key,
490 hmsg->body.kvp_get.data.key_size,
493 umsg->body.kvp_get.data.key_size =
494 umsg->body.kvp_get.data.key_size / 2;
497 case HV_KVP_OP_DELETE:
498 umsg->body.kvp_delete.key_size =
499 utf16_to_utf8(umsg->body.kvp_delete.key,
500 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
501 (uint16_t *)hmsg->body.kvp_delete.key,
502 hmsg->body.kvp_delete.key_size,
505 umsg->body.kvp_delete.key_size =
506 umsg->body.kvp_delete.key_size / 2;
509 case HV_KVP_OP_ENUMERATE:
510 umsg->body.kvp_enum_data.index =
511 hmsg->body.kvp_enum_data.index;
515 hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
516 __func__, umsg->kvp_hdr.operation);
522 * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
525 hv_kvp_convert_usermsg_to_hostmsg(void)
527 int hkey_len = 0, hvalue_len = 0, utf_err = 0;
528 struct hv_kvp_exchg_msg_value *host_exchg_data;
529 char *key_name, *value;
531 struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
532 struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
533 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
535 switch (hmsg->kvp_hdr.operation) {
536 case HV_KVP_OP_GET_IP_INFO:
537 return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
539 case HV_KVP_OP_SET_IP_INFO:
541 case HV_KVP_OP_DELETE:
542 return (KVP_SUCCESS);
544 case HV_KVP_OP_ENUMERATE:
545 host_exchg_data = &hmsg->body.kvp_enum_data.data;
546 key_name = umsg->body.kvp_enum_data.data.key;
547 hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
548 ((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
549 key_name, strlen(key_name),
552 host_exchg_data->key_size = 2 * (hkey_len + 1);
553 value = umsg->body.kvp_enum_data.data.msg_value.value;
554 hvalue_len = utf8_to_utf16(
555 (uint16_t *)host_exchg_data->msg_value.value,
556 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
557 value, strlen(value),
559 host_exchg_data->value_size = 2 * (hvalue_len + 1);
560 host_exchg_data->value_type = HV_REG_SZ;
562 if ((hkey_len < 0) || (hvalue_len < 0))
563 return (HV_KVP_E_FAIL);
565 return (KVP_SUCCESS);
568 host_exchg_data = &hmsg->body.kvp_get.data;
569 value = umsg->body.kvp_get.data.msg_value.value;
570 hvalue_len = utf8_to_utf16(
571 (uint16_t *)host_exchg_data->msg_value.value,
572 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
573 value, strlen(value),
575 /* Convert value size to uft16 */
576 host_exchg_data->value_size = 2 * (hvalue_len + 1);
577 /* Use values by string */
578 host_exchg_data->value_type = HV_REG_SZ;
580 if ((hkey_len < 0) || (hvalue_len < 0))
581 return (HV_KVP_E_FAIL);
583 return (KVP_SUCCESS);
586 return (HV_KVP_E_FAIL);
592 * Send the response back to the host.
595 hv_kvp_respond_host(int error)
597 struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
599 hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
600 &kvp_globals.rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
603 error = HV_KVP_E_FAIL;
605 hv_icmsg_hdrp->status = error;
606 hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION | HV_ICMSGHDRFLAG_RESPONSE;
608 error = hv_vmbus_channel_send_packet(kvp_globals.channelp,
610 kvp_globals.host_msg_len, kvp_globals.host_msg_id,
611 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
614 hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
620 * This is the main kvp kernel process that interacts with both user daemon
624 hv_kvp_send_msg_to_daemon(void)
626 /* Prepare kvp_msg to be sent to user */
627 hv_kvp_convert_hostmsg_to_usermsg();
629 /* Send the msg to user via function deamon_read - setting sema */
630 sema_post(&kvp_globals.dev_sema);
635 * Function to read the kvp request buffer from host
636 * and interact with daemon
639 hv_kvp_process_request(void *context)
642 hv_vmbus_channel *channel = context;
643 uint32_t recvlen = 0;
645 struct hv_vmbus_icmsg_hdr *icmsghdrp;
647 uint64_t pending_cnt = 1;
649 hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
650 kvp_buf = receive_buffer[HV_KVP];
651 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
652 &recvlen, &requestid);
655 * We start counting only after the daemon registers
656 * and therefore there could be requests pending in
657 * the VMBus that are not reflected in pending_cnt.
658 * Therefore we continue reading as long as either of
659 * the below conditions is true.
662 while ((pending_cnt>0) || ((ret == 0) && (recvlen > 0))) {
664 if ((ret == 0) && (recvlen>0)) {
666 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
667 &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
669 hv_kvp_transaction_init(recvlen, channel, requestid, kvp_buf);
670 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
671 hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
672 hv_kvp_respond_host(ret);
675 * It is ok to not acquire the mutex before setting
676 * req_in_progress here because negotiation is the
677 * first thing that happens and hence there is no
678 * chance of a race condition.
681 kvp_globals.req_in_progress = false;
682 hv_kvp_log_info("%s :version negotiated\n", __func__);
685 if (!kvp_globals.daemon_busy) {
687 hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
688 mtx_lock(&kvp_globals.pending_mutex);
689 kvp_globals.req_timed_out = false;
690 kvp_globals.daemon_busy = true;
691 mtx_unlock(&kvp_globals.pending_mutex);
693 hv_kvp_send_msg_to_daemon();
694 hv_kvp_log_info("%s: waiting for daemon\n", __func__);
697 /* Wait 5 seconds for daemon to respond back */
698 tsleep(&kvp_globals, 0, "kvpworkitem", 5 * hz);
699 hv_kvp_log_info("%s: came out of wait\n", __func__);
703 mtx_lock(&kvp_globals.pending_mutex);
705 /* Notice that once req_timed_out is set to true
706 * it will remain true until the next request is
707 * sent to the daemon. The response from daemon
708 * is forwarded to host only when this flag is
711 kvp_globals.req_timed_out = true;
714 * Cancel request if so need be.
716 if (hv_kvp_req_in_progress()) {
717 hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
718 hv_kvp_respond_host(HV_KVP_E_FAIL);
719 kvp_globals.req_in_progress = false;
723 * Decrement pending request count and
725 if (kvp_globals.pending_reqs>0) {
726 kvp_globals.pending_reqs = kvp_globals.pending_reqs - 1;
728 pending_cnt = kvp_globals.pending_reqs;
730 mtx_unlock(&kvp_globals.pending_mutex);
733 * Try reading next buffer
736 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
737 &recvlen, &requestid);
738 hv_kvp_log_info("%s: read: context %p, pending_cnt %llu ret =%d, recvlen=%d\n",
739 __func__, context, (unsigned long long)pending_cnt, ret, recvlen);
745 * Callback routine that gets called whenever there is a message from host
748 hv_kvp_callback(void *context)
750 uint64_t pending_cnt = 0;
752 if (kvp_globals.register_done == false) {
754 kvp_globals.channelp = context;
757 mtx_lock(&kvp_globals.pending_mutex);
758 kvp_globals.pending_reqs = kvp_globals.pending_reqs + 1;
759 pending_cnt = kvp_globals.pending_reqs;
760 mtx_unlock(&kvp_globals.pending_mutex);
761 if (pending_cnt == 1) {
762 hv_kvp_log_info("%s: Queuing work item\n", __func__);
764 service_table[HV_KVP].work_queue,
765 hv_kvp_process_request,
774 * This function is called by the hv_kvp_init -
775 * creates character device hv_kvp_dev
776 * allocates memory to hv_kvp_dev_buf
780 hv_kvp_dev_init(void)
784 /* initialize semaphore */
785 sema_init(&kvp_globals.dev_sema, 0, "hv_kvp device semaphore");
786 /* create character device */
787 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
800 * Malloc with M_WAITOK flag will never fail.
802 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_HV_KVP_DEV_BUF, M_WAITOK |
810 * This function is called by the hv_kvp_deinit -
811 * destroy character device
814 hv_kvp_dev_destroy(void)
817 if (daemon_task != NULL) {
818 PROC_LOCK(daemon_task);
819 kern_psignal(daemon_task, SIGKILL);
820 PROC_UNLOCK(daemon_task);
823 destroy_dev(hv_kvp_dev);
824 free(hv_kvp_dev_buf, M_HV_KVP_DEV_BUF);
830 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
834 hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
835 if (kvp_globals.dev_accessed)
838 daemon_task = curproc;
839 kvp_globals.dev_accessed = true;
840 kvp_globals.daemon_busy = false;
846 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
847 struct thread *td __unused)
850 hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
851 kvp_globals.dev_accessed = false;
852 kvp_globals.register_done = false;
858 * hv_kvp_daemon read invokes this function
859 * acts as a send to daemon
862 hv_kvp_dev_daemon_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
867 /* Check hv_kvp daemon registration status*/
868 if (!kvp_globals.register_done)
871 sema_wait(&kvp_globals.dev_sema);
873 memcpy(hv_kvp_dev_buf, &kvp_globals.daemon_kvp_msg, sizeof(struct hv_kvp_msg));
875 amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
876 BUFFERSIZE + 1 - uio->uio_offset);
878 if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
879 hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
886 * hv_kvp_daemon write invokes this function
887 * acts as a recieve from daemon
890 hv_kvp_dev_daemon_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
897 amt = MIN(uio->uio_resid, BUFFERSIZE);
898 error = uiomove(hv_kvp_dev_buf, amt, uio);
903 memcpy(&kvp_globals.daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
905 if (kvp_globals.register_done == false) {
906 if (kvp_globals.daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
908 kvp_globals.register_done = true;
909 if (kvp_globals.channelp) {
911 hv_kvp_callback(kvp_globals.channelp);
915 hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
920 mtx_lock(&kvp_globals.pending_mutex);
922 if(!kvp_globals.req_timed_out) {
924 hv_kvp_convert_usermsg_to_hostmsg();
925 hv_kvp_respond_host(KVP_SUCCESS);
926 wakeup(&kvp_globals);
927 kvp_globals.req_in_progress = false;
930 kvp_globals.daemon_busy = false;
931 mtx_unlock(&kvp_globals.pending_mutex);
939 * hv_kvp_daemon poll invokes this function to check if data is available
940 * for daemon to read.
943 hv_kvp_dev_daemon_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
947 mtx_lock(&kvp_globals.pending_mutex);
949 * We check global flag daemon_busy for the data availiability for
950 * userland to read. Deamon_busy is set to true before driver has data
951 * for daemon to read. It is set to false after daemon sends
952 * then response back to driver.
954 if (kvp_globals.daemon_busy == true)
956 mtx_unlock(&kvp_globals.pending_mutex);
963 * hv_kvp initialization function
964 * called from hv_util service.
968 hv_kvp_init(hv_vmbus_service *srv)
971 hv_work_queue *work_queue = NULL;
973 memset(&kvp_globals, 0, sizeof(kvp_globals));
975 work_queue = hv_work_queue_create("KVP Service");
976 if (work_queue == NULL) {
977 hv_kvp_log_info("%s: Work queue alloc failed\n", __func__);
979 hv_kvp_log_error("%s: ENOMEM\n", __func__);
982 srv->work_queue = work_queue;
984 error = hv_kvp_dev_init();
985 mtx_init(&kvp_globals.pending_mutex, "hv-kvp pending mutex",
987 kvp_globals.pending_reqs = 0;
998 hv_kvp_dev_destroy();
999 mtx_destroy(&kvp_globals.pending_mutex);