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>
70 #define BUFFERSIZE sizeof(struct hv_kvp_msg)
73 #define kvp_hdr hdr.kvp_hdr
75 /* hv_kvp debug control */
76 static int hv_kvp_log = 0;
77 SYSCTL_INT(_dev, OID_AUTO, hv_kvp_log, CTLFLAG_RW, &hv_kvp_log, 0,
80 #define hv_kvp_log_error(...) do { \
82 log(LOG_ERR, "hv_kvp: " __VA_ARGS__); \
85 #define hv_kvp_log_info(...) do { \
87 log(LOG_INFO, "hv_kvp: " __VA_ARGS__); \
90 /* character device prototypes */
91 static d_open_t hv_kvp_dev_open;
92 static d_close_t hv_kvp_dev_close;
93 static d_read_t hv_kvp_dev_daemon_read;
94 static d_write_t hv_kvp_dev_daemon_write;
95 static d_poll_t hv_kvp_dev_daemon_poll;
97 /* hv_kvp prototypes */
98 static int hv_kvp_req_in_progress(void);
99 static void hv_kvp_transaction_init(uint32_t, hv_vmbus_channel *, uint64_t, uint8_t *);
100 static void hv_kvp_send_msg_to_daemon(void);
101 static void hv_kvp_process_request(void *context);
103 /* hv_kvp character device structure */
104 static struct cdevsw hv_kvp_cdevsw =
106 .d_version = D_VERSION,
107 .d_open = hv_kvp_dev_open,
108 .d_close = hv_kvp_dev_close,
109 .d_read = hv_kvp_dev_daemon_read,
110 .d_write = hv_kvp_dev_daemon_write,
111 .d_poll = hv_kvp_dev_daemon_poll,
112 .d_name = "hv_kvp_dev",
114 static struct cdev *hv_kvp_dev;
115 static struct hv_kvp_msg *hv_kvp_dev_buf;
116 struct proc *daemon_task;
118 static struct selinfo hv_kvp_selinfo;
121 * Global state to track and synchronize multiple
122 * KVP transaction requests from the host.
126 /* Pre-allocated work item for queue */
127 hv_work_item work_item;
129 /* Unless specified the pending mutex should be
130 * used to alter the values of the following paramters:
135 struct mtx pending_mutex;
137 /* To track if transaction is active or not */
138 boolean_t req_in_progress;
139 /* Tracks if daemon did not reply back in time */
140 boolean_t req_timed_out;
141 /* Tracks if daemon is serving a request currently */
142 boolean_t daemon_busy;
143 /* Count of KVP requests from Hyper-V. */
144 uint64_t pending_reqs;
147 /* Length of host message */
148 uint32_t host_msg_len;
150 /* Pointer to channel */
151 hv_vmbus_channel *channelp;
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;
176 MALLOC_DECLARE(M_HV_KVP_DEV_BUF);
177 MALLOC_DEFINE(M_HV_KVP_DEV_BUF, "hv_kvp_dev buffer", "buffer for hv_kvp_dev module");
180 * hv_kvp low level functions
184 * Check if kvp transaction is in progres
187 hv_kvp_req_in_progress(void)
190 return (kvp_globals.req_in_progress);
195 * This routine is called whenever a message is received from the host
198 hv_kvp_transaction_init(uint32_t rcv_len, hv_vmbus_channel *rcv_channel,
199 uint64_t request_id, uint8_t *rcv_buf)
202 /* Store all the relevant message details in the global structure */
203 /* Do not need to use mutex for req_in_progress here */
204 kvp_globals.req_in_progress = true;
205 kvp_globals.host_msg_len = rcv_len;
206 kvp_globals.channelp = rcv_channel;
207 kvp_globals.host_msg_id = request_id;
208 kvp_globals.rcv_buf = rcv_buf;
209 kvp_globals.host_kvp_msg = (struct hv_kvp_msg *)&rcv_buf[
210 sizeof(struct hv_vmbus_pipe_hdr) +
211 sizeof(struct hv_vmbus_icmsg_hdr)];
216 * hv_kvp - version neogtiation function
219 hv_kvp_negotiate_version(struct hv_vmbus_icmsg_hdr *icmsghdrp,
220 struct hv_vmbus_icmsg_negotiate *negop,
226 icmsghdrp->icmsgsize = 0x10;
228 negop = (struct hv_vmbus_icmsg_negotiate *)&buf[
229 sizeof(struct hv_vmbus_pipe_hdr) +
230 sizeof(struct hv_vmbus_icmsg_hdr)];
231 icframe_vercnt = negop->icframe_vercnt;
232 icmsg_vercnt = negop->icmsg_vercnt;
235 * Select the framework version number we will support
237 if ((icframe_vercnt >= 2) && (negop->icversion_data[1].major == 3)) {
239 if (icmsg_vercnt > 2)
248 negop->icframe_vercnt = 1;
249 negop->icmsg_vercnt = 1;
250 negop->icversion_data[0].major = icframe_vercnt;
251 negop->icversion_data[0].minor = 0;
252 negop->icversion_data[1].major = icmsg_vercnt;
253 negop->icversion_data[1].minor = 0;
258 * Convert ip related info in umsg from utf8 to utf16 and store in hmsg
261 hv_kvp_convert_utf8_ipinfo_to_utf16(struct hv_kvp_msg *umsg,
262 struct hv_kvp_ip_msg *host_ip_msg)
264 int err_ip, err_subnet, err_gway, err_dns, err_adap;
267 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
269 (char *)umsg->body.kvp_ip_val.ip_addr,
270 strlen((char *)umsg->body.kvp_ip_val.ip_addr),
273 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
275 (char *)umsg->body.kvp_ip_val.sub_net,
276 strlen((char *)umsg->body.kvp_ip_val.sub_net),
279 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
281 (char *)umsg->body.kvp_ip_val.gate_way,
282 strlen((char *)umsg->body.kvp_ip_val.gate_way),
285 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
287 (char *)umsg->body.kvp_ip_val.dns_addr,
288 strlen((char *)umsg->body.kvp_ip_val.dns_addr),
291 utf8_to_utf16((uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
293 (char *)umsg->body.kvp_ip_val.adapter_id,
294 strlen((char *)umsg->body.kvp_ip_val.adapter_id),
298 host_ip_msg->kvp_ip_val.dhcp_enabled = umsg->body.kvp_ip_val.dhcp_enabled;
299 host_ip_msg->kvp_ip_val.addr_family = umsg->body.kvp_ip_val.addr_family;
301 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
306 * Convert ip related info in hmsg from utf16 to utf8 and store in umsg
309 hv_kvp_convert_utf16_ipinfo_to_utf8(struct hv_kvp_ip_msg *host_ip_msg,
310 struct hv_kvp_msg *umsg)
312 int err_ip, err_subnet, err_gway, err_dns, err_adap;
315 struct hv_device *hv_dev; /* GUID Data Structure */
316 hn_softc_t *sc; /* hn softc structure */
318 unsigned char guid_instance[40];
319 char *guid_data = NULL;
322 struct guid_extract {
335 struct guid_extract *id;
340 utf16_to_utf8((char *)umsg->body.kvp_ip_val.ip_addr,
342 (uint16_t *)host_ip_msg->kvp_ip_val.ip_addr,
347 /* Adapter ID : GUID */
348 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
350 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
355 if (devclass_get_devices(devclass_find("hn"), &devs, &devcnt) == 0) {
356 for (devcnt = devcnt - 1; devcnt >= 0; devcnt--) {
357 sc = device_get_softc(devs[devcnt]);
359 /* Trying to find GUID of Network Device */
360 hv_dev = sc->hn_dev_obj;
362 for (guid_index = 0; guid_index < 16; guid_index++) {
363 sprintf(&guid_instance[guid_index * 2], "%02x",
364 hv_dev->device_id.data[guid_index]);
367 guid_data = (char *)guid_instance;
368 id = (struct guid_extract *)guid_data;
369 snprintf(buf, sizeof(buf), "{%.2s%.2s%.2s%.2s-%.2s%.2s-%.2s%.2s-%.4s-%s}",
370 id->a4, id->a3, id->a2, id->a1,
371 id->b2, id->b1, id->c2, id->c1, id->d, id->e);
373 sprintf(if_name, "%s%d", "hn", device_get_unit(devs[devcnt]));
375 if (strncmp(buf, (char *)umsg->body.kvp_ip_val.adapter_id, 39) == 0) {
376 strcpy((char *)umsg->body.kvp_ip_val.adapter_id, if_name);
383 /* Address Family , DHCP , SUBNET, Gateway, DNS */
384 umsg->kvp_hdr.operation = host_ip_msg->operation;
385 umsg->body.kvp_ip_val.addr_family = host_ip_msg->kvp_ip_val.addr_family;
386 umsg->body.kvp_ip_val.dhcp_enabled = host_ip_msg->kvp_ip_val.dhcp_enabled;
387 utf16_to_utf8((char *)umsg->body.kvp_ip_val.sub_net, MAX_IP_ADDR_SIZE,
388 (uint16_t *)host_ip_msg->kvp_ip_val.sub_net,
393 utf16_to_utf8((char *)umsg->body.kvp_ip_val.gate_way, MAX_GATEWAY_SIZE,
394 (uint16_t *)host_ip_msg->kvp_ip_val.gate_way,
399 utf16_to_utf8((char *)umsg->body.kvp_ip_val.dns_addr, MAX_IP_ADDR_SIZE,
400 (uint16_t *)host_ip_msg->kvp_ip_val.dns_addr,
405 return (err_ip | err_subnet | err_gway | err_dns | err_adap);
410 * Prepare a user kvp msg based on host kvp msg (utf16 to utf8)
411 * Ensure utf16_utf8 takes care of the additional string terminating char!!
414 hv_kvp_convert_hostmsg_to_usermsg(void)
418 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)
419 kvp_globals.host_kvp_msg;
421 struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
422 struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
424 memset(umsg, 0, sizeof(struct hv_kvp_msg));
426 umsg->kvp_hdr.operation = hmsg->kvp_hdr.operation;
427 umsg->kvp_hdr.pool = hmsg->kvp_hdr.pool;
429 switch (umsg->kvp_hdr.operation) {
430 case HV_KVP_OP_SET_IP_INFO:
431 hv_kvp_convert_utf16_ipinfo_to_utf8(host_ip_msg, umsg);
434 case HV_KVP_OP_GET_IP_INFO:
435 utf16_to_utf8((char *)umsg->body.kvp_ip_val.adapter_id,
437 (uint16_t *)host_ip_msg->kvp_ip_val.adapter_id,
438 MAX_ADAPTER_ID_SIZE, 1, &utf_err);
440 umsg->body.kvp_ip_val.addr_family =
441 host_ip_msg->kvp_ip_val.addr_family;
445 value_type = hmsg->body.kvp_set.data.value_type;
447 switch (value_type) {
449 umsg->body.kvp_set.data.value_size =
451 (char *)umsg->body.kvp_set.data.msg_value.value,
452 HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1,
453 (uint16_t *)hmsg->body.kvp_set.data.msg_value.value,
454 hmsg->body.kvp_set.data.value_size,
457 umsg->body.kvp_set.data.value_size =
458 umsg->body.kvp_set.data.value_size / 2;
462 umsg->body.kvp_set.data.value_size =
463 sprintf(umsg->body.kvp_set.data.msg_value.value, "%d",
464 hmsg->body.kvp_set.data.msg_value.value_u32) + 1;
468 umsg->body.kvp_set.data.value_size =
469 sprintf(umsg->body.kvp_set.data.msg_value.value, "%llu",
471 hmsg->body.kvp_set.data.msg_value.value_u64) + 1;
475 umsg->body.kvp_set.data.key_size =
477 umsg->body.kvp_set.data.key,
478 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
479 (uint16_t *)hmsg->body.kvp_set.data.key,
480 hmsg->body.kvp_set.data.key_size,
484 umsg->body.kvp_set.data.key_size =
485 umsg->body.kvp_set.data.key_size / 2;
489 umsg->body.kvp_get.data.key_size =
490 utf16_to_utf8(umsg->body.kvp_get.data.key,
491 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
492 (uint16_t *)hmsg->body.kvp_get.data.key,
493 hmsg->body.kvp_get.data.key_size,
496 umsg->body.kvp_get.data.key_size =
497 umsg->body.kvp_get.data.key_size / 2;
500 case HV_KVP_OP_DELETE:
501 umsg->body.kvp_delete.key_size =
502 utf16_to_utf8(umsg->body.kvp_delete.key,
503 HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1,
504 (uint16_t *)hmsg->body.kvp_delete.key,
505 hmsg->body.kvp_delete.key_size,
508 umsg->body.kvp_delete.key_size =
509 umsg->body.kvp_delete.key_size / 2;
512 case HV_KVP_OP_ENUMERATE:
513 umsg->body.kvp_enum_data.index =
514 hmsg->body.kvp_enum_data.index;
518 hv_kvp_log_info("%s: daemon_kvp_msg: Invalid operation : %d\n",
519 __func__, umsg->kvp_hdr.operation);
525 * Prepare a host kvp msg based on user kvp msg (utf8 to utf16)
528 hv_kvp_convert_usermsg_to_hostmsg(void)
530 int hkey_len = 0, hvalue_len = 0, utf_err = 0;
531 struct hv_kvp_exchg_msg_value *host_exchg_data;
532 char *key_name, *value;
534 struct hv_kvp_msg *umsg = &kvp_globals.daemon_kvp_msg;
535 struct hv_kvp_msg *hmsg = kvp_globals.host_kvp_msg;
536 struct hv_kvp_ip_msg *host_ip_msg = (struct hv_kvp_ip_msg *)hmsg;
538 switch (hmsg->kvp_hdr.operation) {
539 case HV_KVP_OP_GET_IP_INFO:
540 return (hv_kvp_convert_utf8_ipinfo_to_utf16(umsg, host_ip_msg));
542 case HV_KVP_OP_SET_IP_INFO:
544 case HV_KVP_OP_DELETE:
545 return (KVP_SUCCESS);
547 case HV_KVP_OP_ENUMERATE:
548 host_exchg_data = &hmsg->body.kvp_enum_data.data;
549 key_name = umsg->body.kvp_enum_data.data.key;
550 hkey_len = utf8_to_utf16((uint16_t *)host_exchg_data->key,
551 ((HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2),
552 key_name, strlen(key_name),
555 host_exchg_data->key_size = 2 * (hkey_len + 1);
556 value = umsg->body.kvp_enum_data.data.msg_value.value;
557 hvalue_len = utf8_to_utf16(
558 (uint16_t *)host_exchg_data->msg_value.value,
559 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
560 value, strlen(value),
562 host_exchg_data->value_size = 2 * (hvalue_len + 1);
563 host_exchg_data->value_type = HV_REG_SZ;
565 if ((hkey_len < 0) || (hvalue_len < 0))
566 return (HV_KVP_E_FAIL);
568 return (KVP_SUCCESS);
571 host_exchg_data = &hmsg->body.kvp_get.data;
572 value = umsg->body.kvp_get.data.msg_value.value;
573 hvalue_len = utf8_to_utf16(
574 (uint16_t *)host_exchg_data->msg_value.value,
575 ((HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2),
576 value, strlen(value),
578 /* Convert value size to uft16 */
579 host_exchg_data->value_size = 2 * (hvalue_len + 1);
580 /* Use values by string */
581 host_exchg_data->value_type = HV_REG_SZ;
583 if ((hkey_len < 0) || (hvalue_len < 0))
584 return (HV_KVP_E_FAIL);
586 return (KVP_SUCCESS);
589 return (HV_KVP_E_FAIL);
595 * Send the response back to the host.
598 hv_kvp_respond_host(int error)
600 struct hv_vmbus_icmsg_hdr *hv_icmsg_hdrp;
602 hv_icmsg_hdrp = (struct hv_vmbus_icmsg_hdr *)
603 &kvp_globals.rcv_buf[sizeof(struct hv_vmbus_pipe_hdr)];
606 error = HV_KVP_E_FAIL;
608 hv_icmsg_hdrp->status = error;
609 hv_icmsg_hdrp->icflags = HV_ICMSGHDRFLAG_TRANSACTION | HV_ICMSGHDRFLAG_RESPONSE;
611 error = hv_vmbus_channel_send_packet(kvp_globals.channelp,
613 kvp_globals.host_msg_len, kvp_globals.host_msg_id,
614 HV_VMBUS_PACKET_TYPE_DATA_IN_BAND, 0);
617 hv_kvp_log_info("%s: hv_kvp_respond_host: sendpacket error:%d\n",
623 * This is the main kvp kernel process that interacts with both user daemon
627 hv_kvp_send_msg_to_daemon(void)
629 /* Prepare kvp_msg to be sent to user */
630 hv_kvp_convert_hostmsg_to_usermsg();
632 /* Send the msg to user via function deamon_read - setting sema */
633 sema_post(&kvp_globals.dev_sema);
635 /* We should wake up the daemon, in case it's doing poll() */
636 selwakeup(&hv_kvp_selinfo);
641 * Function to read the kvp request buffer from host
642 * and interact with daemon
645 hv_kvp_process_request(void *context)
648 hv_vmbus_channel *channel = context;
649 uint32_t recvlen = 0;
651 struct hv_vmbus_icmsg_hdr *icmsghdrp;
653 uint64_t pending_cnt = 1;
655 hv_kvp_log_info("%s: entering hv_kvp_process_request\n", __func__);
656 kvp_buf = receive_buffer[HV_KVP];
657 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
658 &recvlen, &requestid);
661 * We start counting only after the daemon registers
662 * and therefore there could be requests pending in
663 * the VMBus that are not reflected in pending_cnt.
664 * Therefore we continue reading as long as either of
665 * the below conditions is true.
668 while ((pending_cnt>0) || ((ret == 0) && (recvlen > 0))) {
670 if ((ret == 0) && (recvlen>0)) {
672 icmsghdrp = (struct hv_vmbus_icmsg_hdr *)
673 &kvp_buf[sizeof(struct hv_vmbus_pipe_hdr)];
675 hv_kvp_transaction_init(recvlen, channel, requestid, kvp_buf);
676 if (icmsghdrp->icmsgtype == HV_ICMSGTYPE_NEGOTIATE) {
677 hv_kvp_negotiate_version(icmsghdrp, NULL, kvp_buf);
678 hv_kvp_respond_host(ret);
681 * It is ok to not acquire the mutex before setting
682 * req_in_progress here because negotiation is the
683 * first thing that happens and hence there is no
684 * chance of a race condition.
687 kvp_globals.req_in_progress = false;
688 hv_kvp_log_info("%s :version negotiated\n", __func__);
691 if (!kvp_globals.daemon_busy) {
693 hv_kvp_log_info("%s: issuing qury to daemon\n", __func__);
694 mtx_lock(&kvp_globals.pending_mutex);
695 kvp_globals.req_timed_out = false;
696 kvp_globals.daemon_busy = true;
697 mtx_unlock(&kvp_globals.pending_mutex);
699 hv_kvp_send_msg_to_daemon();
700 hv_kvp_log_info("%s: waiting for daemon\n", __func__);
703 /* Wait 5 seconds for daemon to respond back */
704 tsleep(&kvp_globals, 0, "kvpworkitem", 5 * hz);
705 hv_kvp_log_info("%s: came out of wait\n", __func__);
709 mtx_lock(&kvp_globals.pending_mutex);
711 /* Notice that once req_timed_out is set to true
712 * it will remain true until the next request is
713 * sent to the daemon. The response from daemon
714 * is forwarded to host only when this flag is
717 kvp_globals.req_timed_out = true;
720 * Cancel request if so need be.
722 if (hv_kvp_req_in_progress()) {
723 hv_kvp_log_info("%s: request was still active after wait so failing\n", __func__);
724 hv_kvp_respond_host(HV_KVP_E_FAIL);
725 kvp_globals.req_in_progress = false;
729 * Decrement pending request count and
731 if (kvp_globals.pending_reqs>0) {
732 kvp_globals.pending_reqs = kvp_globals.pending_reqs - 1;
734 pending_cnt = kvp_globals.pending_reqs;
736 mtx_unlock(&kvp_globals.pending_mutex);
739 * Try reading next buffer
742 ret = hv_vmbus_channel_recv_packet(channel, kvp_buf, 2 * PAGE_SIZE,
743 &recvlen, &requestid);
744 hv_kvp_log_info("%s: read: context %p, pending_cnt %llu ret =%d, recvlen=%d\n",
745 __func__, context, (unsigned long long)pending_cnt, ret, recvlen);
751 * Callback routine that gets called whenever there is a message from host
754 hv_kvp_callback(void *context)
756 uint64_t pending_cnt = 0;
758 if (kvp_globals.register_done == false) {
760 kvp_globals.channelp = context;
763 mtx_lock(&kvp_globals.pending_mutex);
764 kvp_globals.pending_reqs = kvp_globals.pending_reqs + 1;
765 pending_cnt = kvp_globals.pending_reqs;
766 mtx_unlock(&kvp_globals.pending_mutex);
767 if (pending_cnt == 1) {
768 hv_kvp_log_info("%s: Queuing work item\n", __func__);
770 service_table[HV_KVP].work_queue,
771 hv_kvp_process_request,
780 * This function is called by the hv_kvp_init -
781 * creates character device hv_kvp_dev
782 * allocates memory to hv_kvp_dev_buf
786 hv_kvp_dev_init(void)
790 /* initialize semaphore */
791 sema_init(&kvp_globals.dev_sema, 0, "hv_kvp device semaphore");
792 /* create character device */
793 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
806 * Malloc with M_WAITOK flag will never fail.
808 hv_kvp_dev_buf = malloc(sizeof(*hv_kvp_dev_buf), M_HV_KVP_DEV_BUF, M_WAITOK |
816 * This function is called by the hv_kvp_deinit -
817 * destroy character device
820 hv_kvp_dev_destroy(void)
823 if (daemon_task != NULL) {
824 PROC_LOCK(daemon_task);
825 kern_psignal(daemon_task, SIGKILL);
826 PROC_UNLOCK(daemon_task);
829 destroy_dev(hv_kvp_dev);
830 free(hv_kvp_dev_buf, M_HV_KVP_DEV_BUF);
836 hv_kvp_dev_open(struct cdev *dev, int oflags, int devtype,
840 hv_kvp_log_info("%s: Opened device \"hv_kvp_device\" successfully.\n", __func__);
841 if (kvp_globals.dev_accessed)
844 daemon_task = curproc;
845 kvp_globals.dev_accessed = true;
846 kvp_globals.daemon_busy = false;
852 hv_kvp_dev_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
853 struct thread *td __unused)
856 hv_kvp_log_info("%s: Closing device \"hv_kvp_device\".\n", __func__);
857 kvp_globals.dev_accessed = false;
858 kvp_globals.register_done = false;
864 * hv_kvp_daemon read invokes this function
865 * acts as a send to daemon
868 hv_kvp_dev_daemon_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
873 /* Check hv_kvp daemon registration status*/
874 if (!kvp_globals.register_done)
877 sema_wait(&kvp_globals.dev_sema);
879 memcpy(hv_kvp_dev_buf, &kvp_globals.daemon_kvp_msg, sizeof(struct hv_kvp_msg));
881 amt = MIN(uio->uio_resid, uio->uio_offset >= BUFFERSIZE + 1 ? 0 :
882 BUFFERSIZE + 1 - uio->uio_offset);
884 if ((error = uiomove(hv_kvp_dev_buf, amt, uio)) != 0)
885 hv_kvp_log_info("%s: hv_kvp uiomove read failed!\n", __func__);
892 * hv_kvp_daemon write invokes this function
893 * acts as a recieve from daemon
896 hv_kvp_dev_daemon_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
903 amt = MIN(uio->uio_resid, BUFFERSIZE);
904 error = uiomove(hv_kvp_dev_buf, amt, uio);
909 memcpy(&kvp_globals.daemon_kvp_msg, hv_kvp_dev_buf, sizeof(struct hv_kvp_msg));
911 if (kvp_globals.register_done == false) {
912 if (kvp_globals.daemon_kvp_msg.kvp_hdr.operation == HV_KVP_OP_REGISTER) {
914 kvp_globals.register_done = true;
915 if (kvp_globals.channelp) {
917 hv_kvp_callback(kvp_globals.channelp);
921 hv_kvp_log_info("%s, KVP Registration Failed\n", __func__);
926 mtx_lock(&kvp_globals.pending_mutex);
928 if(!kvp_globals.req_timed_out) {
930 hv_kvp_convert_usermsg_to_hostmsg();
931 hv_kvp_respond_host(KVP_SUCCESS);
932 wakeup(&kvp_globals);
933 kvp_globals.req_in_progress = false;
936 kvp_globals.daemon_busy = false;
937 mtx_unlock(&kvp_globals.pending_mutex);
945 * hv_kvp_daemon poll invokes this function to check if data is available
946 * for daemon to read.
949 hv_kvp_dev_daemon_poll(struct cdev *dev __unused, int events, struct thread *td)
953 mtx_lock(&kvp_globals.pending_mutex);
955 * We check global flag daemon_busy for the data availiability for
956 * userland to read. Deamon_busy is set to true before driver has data
957 * for daemon to read. It is set to false after daemon sends
958 * then response back to driver.
960 if (kvp_globals.daemon_busy == true)
963 selrecord(td, &hv_kvp_selinfo);
965 mtx_unlock(&kvp_globals.pending_mutex);
972 * hv_kvp initialization function
973 * called from hv_util service.
977 hv_kvp_init(hv_vmbus_service *srv)
980 hv_work_queue *work_queue = NULL;
982 memset(&kvp_globals, 0, sizeof(kvp_globals));
984 work_queue = hv_work_queue_create("KVP Service");
985 if (work_queue == NULL) {
986 hv_kvp_log_info("%s: Work queue alloc failed\n", __func__);
988 hv_kvp_log_error("%s: ENOMEM\n", __func__);
991 srv->work_queue = work_queue;
993 error = hv_kvp_dev_init();
994 mtx_init(&kvp_globals.pending_mutex, "hv-kvp pending mutex",
996 kvp_globals.pending_reqs = 0;
1007 hv_kvp_dev_destroy();
1008 mtx_destroy(&kvp_globals.pending_mutex);