162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * VMware VMCI Driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2012 VMware, Inc. All rights reserved. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/vmw_vmci_defs.h> 962306a36Sopenharmony_ci#include <linux/vmw_vmci_api.h> 1062306a36Sopenharmony_ci#include <linux/list.h> 1162306a36Sopenharmony_ci#include <linux/module.h> 1262306a36Sopenharmony_ci#include <linux/sched.h> 1362306a36Sopenharmony_ci#include <linux/slab.h> 1462306a36Sopenharmony_ci#include <linux/rculist.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include "vmci_driver.h" 1762306a36Sopenharmony_ci#include "vmci_event.h" 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define EVENT_MAGIC 0xEABE0000 2062306a36Sopenharmony_ci#define VMCI_EVENT_MAX_ATTEMPTS 10 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_cistruct vmci_subscription { 2362306a36Sopenharmony_ci u32 id; 2462306a36Sopenharmony_ci u32 event; 2562306a36Sopenharmony_ci vmci_event_cb callback; 2662306a36Sopenharmony_ci void *callback_data; 2762306a36Sopenharmony_ci struct list_head node; /* on one of subscriber lists */ 2862306a36Sopenharmony_ci}; 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_cistatic struct list_head subscriber_array[VMCI_EVENT_MAX]; 3162306a36Sopenharmony_cistatic DEFINE_MUTEX(subscriber_mutex); 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ciint __init vmci_event_init(void) 3462306a36Sopenharmony_ci{ 3562306a36Sopenharmony_ci int i; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci for (i = 0; i < VMCI_EVENT_MAX; i++) 3862306a36Sopenharmony_ci INIT_LIST_HEAD(&subscriber_array[i]); 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci return VMCI_SUCCESS; 4162306a36Sopenharmony_ci} 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_civoid vmci_event_exit(void) 4462306a36Sopenharmony_ci{ 4562306a36Sopenharmony_ci int e; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci /* We free all memory at exit. */ 4862306a36Sopenharmony_ci for (e = 0; e < VMCI_EVENT_MAX; e++) { 4962306a36Sopenharmony_ci struct vmci_subscription *cur, *p2; 5062306a36Sopenharmony_ci list_for_each_entry_safe(cur, p2, &subscriber_array[e], node) { 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci /* 5362306a36Sopenharmony_ci * We should never get here because all events 5462306a36Sopenharmony_ci * should have been unregistered before we try 5562306a36Sopenharmony_ci * to unload the driver module. 5662306a36Sopenharmony_ci */ 5762306a36Sopenharmony_ci pr_warn("Unexpected free events occurring\n"); 5862306a36Sopenharmony_ci list_del(&cur->node); 5962306a36Sopenharmony_ci kfree(cur); 6062306a36Sopenharmony_ci } 6162306a36Sopenharmony_ci } 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci/* 6562306a36Sopenharmony_ci * Find entry. Assumes subscriber_mutex is held. 6662306a36Sopenharmony_ci */ 6762306a36Sopenharmony_cistatic struct vmci_subscription *event_find(u32 sub_id) 6862306a36Sopenharmony_ci{ 6962306a36Sopenharmony_ci int e; 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci for (e = 0; e < VMCI_EVENT_MAX; e++) { 7262306a36Sopenharmony_ci struct vmci_subscription *cur; 7362306a36Sopenharmony_ci list_for_each_entry(cur, &subscriber_array[e], node) { 7462306a36Sopenharmony_ci if (cur->id == sub_id) 7562306a36Sopenharmony_ci return cur; 7662306a36Sopenharmony_ci } 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci return NULL; 7962306a36Sopenharmony_ci} 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci/* 8262306a36Sopenharmony_ci * Actually delivers the events to the subscribers. 8362306a36Sopenharmony_ci * The callback function for each subscriber is invoked. 8462306a36Sopenharmony_ci */ 8562306a36Sopenharmony_cistatic void event_deliver(struct vmci_event_msg *event_msg) 8662306a36Sopenharmony_ci{ 8762306a36Sopenharmony_ci struct vmci_subscription *cur; 8862306a36Sopenharmony_ci struct list_head *subscriber_list; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci rcu_read_lock(); 9162306a36Sopenharmony_ci subscriber_list = &subscriber_array[event_msg->event_data.event]; 9262306a36Sopenharmony_ci list_for_each_entry_rcu(cur, subscriber_list, node) { 9362306a36Sopenharmony_ci cur->callback(cur->id, &event_msg->event_data, 9462306a36Sopenharmony_ci cur->callback_data); 9562306a36Sopenharmony_ci } 9662306a36Sopenharmony_ci rcu_read_unlock(); 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci/* 10062306a36Sopenharmony_ci * Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all 10162306a36Sopenharmony_ci * subscribers for given event. 10262306a36Sopenharmony_ci */ 10362306a36Sopenharmony_ciint vmci_event_dispatch(struct vmci_datagram *msg) 10462306a36Sopenharmony_ci{ 10562306a36Sopenharmony_ci struct vmci_event_msg *event_msg = (struct vmci_event_msg *)msg; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci if (msg->payload_size < sizeof(u32) || 10862306a36Sopenharmony_ci msg->payload_size > sizeof(struct vmci_event_data_max)) 10962306a36Sopenharmony_ci return VMCI_ERROR_INVALID_ARGS; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci if (!VMCI_EVENT_VALID(event_msg->event_data.event)) 11262306a36Sopenharmony_ci return VMCI_ERROR_EVENT_UNKNOWN; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci event_deliver(event_msg); 11562306a36Sopenharmony_ci return VMCI_SUCCESS; 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci/* 11962306a36Sopenharmony_ci * vmci_event_subscribe() - Subscribe to a given event. 12062306a36Sopenharmony_ci * @event: The event to subscribe to. 12162306a36Sopenharmony_ci * @callback: The callback to invoke upon the event. 12262306a36Sopenharmony_ci * @callback_data: Data to pass to the callback. 12362306a36Sopenharmony_ci * @subscription_id: ID used to track subscription. Used with 12462306a36Sopenharmony_ci * vmci_event_unsubscribe() 12562306a36Sopenharmony_ci * 12662306a36Sopenharmony_ci * Subscribes to the provided event. The callback specified will be 12762306a36Sopenharmony_ci * fired from RCU critical section and therefore must not sleep. 12862306a36Sopenharmony_ci */ 12962306a36Sopenharmony_ciint vmci_event_subscribe(u32 event, 13062306a36Sopenharmony_ci vmci_event_cb callback, 13162306a36Sopenharmony_ci void *callback_data, 13262306a36Sopenharmony_ci u32 *new_subscription_id) 13362306a36Sopenharmony_ci{ 13462306a36Sopenharmony_ci struct vmci_subscription *sub; 13562306a36Sopenharmony_ci int attempts; 13662306a36Sopenharmony_ci int retval; 13762306a36Sopenharmony_ci bool have_new_id = false; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci if (!new_subscription_id) { 14062306a36Sopenharmony_ci pr_devel("%s: Invalid subscription (NULL)\n", __func__); 14162306a36Sopenharmony_ci return VMCI_ERROR_INVALID_ARGS; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci if (!VMCI_EVENT_VALID(event) || !callback) { 14562306a36Sopenharmony_ci pr_devel("%s: Failed to subscribe to event (type=%d) (callback=%p) (data=%p)\n", 14662306a36Sopenharmony_ci __func__, event, callback, callback_data); 14762306a36Sopenharmony_ci return VMCI_ERROR_INVALID_ARGS; 14862306a36Sopenharmony_ci } 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci sub = kzalloc(sizeof(*sub), GFP_KERNEL); 15162306a36Sopenharmony_ci if (!sub) 15262306a36Sopenharmony_ci return VMCI_ERROR_NO_MEM; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci sub->id = VMCI_EVENT_MAX; 15562306a36Sopenharmony_ci sub->event = event; 15662306a36Sopenharmony_ci sub->callback = callback; 15762306a36Sopenharmony_ci sub->callback_data = callback_data; 15862306a36Sopenharmony_ci INIT_LIST_HEAD(&sub->node); 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci mutex_lock(&subscriber_mutex); 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci /* Creation of a new event is always allowed. */ 16362306a36Sopenharmony_ci for (attempts = 0; attempts < VMCI_EVENT_MAX_ATTEMPTS; attempts++) { 16462306a36Sopenharmony_ci static u32 subscription_id; 16562306a36Sopenharmony_ci /* 16662306a36Sopenharmony_ci * We try to get an id a couple of time before 16762306a36Sopenharmony_ci * claiming we are out of resources. 16862306a36Sopenharmony_ci */ 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci /* Test for duplicate id. */ 17162306a36Sopenharmony_ci if (!event_find(++subscription_id)) { 17262306a36Sopenharmony_ci sub->id = subscription_id; 17362306a36Sopenharmony_ci have_new_id = true; 17462306a36Sopenharmony_ci break; 17562306a36Sopenharmony_ci } 17662306a36Sopenharmony_ci } 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci if (have_new_id) { 17962306a36Sopenharmony_ci list_add_rcu(&sub->node, &subscriber_array[event]); 18062306a36Sopenharmony_ci retval = VMCI_SUCCESS; 18162306a36Sopenharmony_ci } else { 18262306a36Sopenharmony_ci retval = VMCI_ERROR_NO_RESOURCES; 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci mutex_unlock(&subscriber_mutex); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci *new_subscription_id = sub->id; 18862306a36Sopenharmony_ci return retval; 18962306a36Sopenharmony_ci} 19062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(vmci_event_subscribe); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci/* 19362306a36Sopenharmony_ci * vmci_event_unsubscribe() - unsubscribe from an event. 19462306a36Sopenharmony_ci * @sub_id: A subscription ID as provided by vmci_event_subscribe() 19562306a36Sopenharmony_ci * 19662306a36Sopenharmony_ci * Unsubscribe from given event. Removes it from list and frees it. 19762306a36Sopenharmony_ci * Will return callback_data if requested by caller. 19862306a36Sopenharmony_ci */ 19962306a36Sopenharmony_ciint vmci_event_unsubscribe(u32 sub_id) 20062306a36Sopenharmony_ci{ 20162306a36Sopenharmony_ci struct vmci_subscription *s; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci mutex_lock(&subscriber_mutex); 20462306a36Sopenharmony_ci s = event_find(sub_id); 20562306a36Sopenharmony_ci if (s) 20662306a36Sopenharmony_ci list_del_rcu(&s->node); 20762306a36Sopenharmony_ci mutex_unlock(&subscriber_mutex); 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci if (!s) 21062306a36Sopenharmony_ci return VMCI_ERROR_NOT_FOUND; 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci kvfree_rcu_mightsleep(s); 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci return VMCI_SUCCESS; 21562306a36Sopenharmony_ci} 21662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(vmci_event_unsubscribe); 217