1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * VMware VMCI Driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 VMware, Inc. All rights reserved.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/vmw_vmci_defs.h>
9*4882a593Smuzhiyun #include <linux/vmw_vmci_api.h>
10*4882a593Smuzhiyun #include <linux/completion.h>
11*4882a593Smuzhiyun #include <linux/hash.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/list.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/sched.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "vmci_datagram.h"
19*4882a593Smuzhiyun #include "vmci_doorbell.h"
20*4882a593Smuzhiyun #include "vmci_resource.h"
21*4882a593Smuzhiyun #include "vmci_driver.h"
22*4882a593Smuzhiyun #include "vmci_route.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define VMCI_DOORBELL_INDEX_BITS 6
26*4882a593Smuzhiyun #define VMCI_DOORBELL_INDEX_TABLE_SIZE (1 << VMCI_DOORBELL_INDEX_BITS)
27*4882a593Smuzhiyun #define VMCI_DOORBELL_HASH(_idx) hash_32(_idx, VMCI_DOORBELL_INDEX_BITS)
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun * DoorbellEntry describes the a doorbell notification handle allocated by the
31*4882a593Smuzhiyun * host.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun struct dbell_entry {
34*4882a593Smuzhiyun struct vmci_resource resource;
35*4882a593Smuzhiyun struct hlist_node node;
36*4882a593Smuzhiyun struct work_struct work;
37*4882a593Smuzhiyun vmci_callback notify_cb;
38*4882a593Smuzhiyun void *client_data;
39*4882a593Smuzhiyun u32 idx;
40*4882a593Smuzhiyun u32 priv_flags;
41*4882a593Smuzhiyun bool run_delayed;
42*4882a593Smuzhiyun atomic_t active; /* Only used by guest personality */
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* The VMCI index table keeps track of currently registered doorbells. */
46*4882a593Smuzhiyun struct dbell_index_table {
47*4882a593Smuzhiyun spinlock_t lock; /* Index table lock */
48*4882a593Smuzhiyun struct hlist_head entries[VMCI_DOORBELL_INDEX_TABLE_SIZE];
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun static struct dbell_index_table vmci_doorbell_it = {
52*4882a593Smuzhiyun .lock = __SPIN_LOCK_UNLOCKED(vmci_doorbell_it.lock),
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * The max_notify_idx is one larger than the currently known bitmap index in
57*4882a593Smuzhiyun * use, and is used to determine how much of the bitmap needs to be scanned.
58*4882a593Smuzhiyun */
59*4882a593Smuzhiyun static u32 max_notify_idx;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * The notify_idx_count is used for determining whether there are free entries
63*4882a593Smuzhiyun * within the bitmap (if notify_idx_count + 1 < max_notify_idx).
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun static u32 notify_idx_count;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * The last_notify_idx_reserved is used to track the last index handed out - in
69*4882a593Smuzhiyun * the case where multiple handles share a notification index, we hand out
70*4882a593Smuzhiyun * indexes round robin based on last_notify_idx_reserved.
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun static u32 last_notify_idx_reserved;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* This is a one entry cache used to by the index allocation. */
75*4882a593Smuzhiyun static u32 last_notify_idx_released = PAGE_SIZE;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * Utility function that retrieves the privilege flags associated
80*4882a593Smuzhiyun * with a given doorbell handle. For guest endpoints, the
81*4882a593Smuzhiyun * privileges are determined by the context ID, but for host
82*4882a593Smuzhiyun * endpoints privileges are associated with the complete
83*4882a593Smuzhiyun * handle. Hypervisor endpoints are not yet supported.
84*4882a593Smuzhiyun */
vmci_dbell_get_priv_flags(struct vmci_handle handle,u32 * priv_flags)85*4882a593Smuzhiyun int vmci_dbell_get_priv_flags(struct vmci_handle handle, u32 *priv_flags)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun if (priv_flags == NULL || handle.context == VMCI_INVALID_ID)
88*4882a593Smuzhiyun return VMCI_ERROR_INVALID_ARGS;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (handle.context == VMCI_HOST_CONTEXT_ID) {
91*4882a593Smuzhiyun struct dbell_entry *entry;
92*4882a593Smuzhiyun struct vmci_resource *resource;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun resource = vmci_resource_by_handle(handle,
95*4882a593Smuzhiyun VMCI_RESOURCE_TYPE_DOORBELL);
96*4882a593Smuzhiyun if (!resource)
97*4882a593Smuzhiyun return VMCI_ERROR_NOT_FOUND;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun entry = container_of(resource, struct dbell_entry, resource);
100*4882a593Smuzhiyun *priv_flags = entry->priv_flags;
101*4882a593Smuzhiyun vmci_resource_put(resource);
102*4882a593Smuzhiyun } else if (handle.context == VMCI_HYPERVISOR_CONTEXT_ID) {
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Hypervisor endpoints for notifications are not
105*4882a593Smuzhiyun * supported (yet).
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun return VMCI_ERROR_INVALID_ARGS;
108*4882a593Smuzhiyun } else {
109*4882a593Smuzhiyun *priv_flags = vmci_context_get_priv_flags(handle.context);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return VMCI_SUCCESS;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun * Find doorbell entry by bitmap index.
117*4882a593Smuzhiyun */
dbell_index_table_find(u32 idx)118*4882a593Smuzhiyun static struct dbell_entry *dbell_index_table_find(u32 idx)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun u32 bucket = VMCI_DOORBELL_HASH(idx);
121*4882a593Smuzhiyun struct dbell_entry *dbell;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun hlist_for_each_entry(dbell, &vmci_doorbell_it.entries[bucket],
124*4882a593Smuzhiyun node) {
125*4882a593Smuzhiyun if (idx == dbell->idx)
126*4882a593Smuzhiyun return dbell;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun return NULL;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /*
133*4882a593Smuzhiyun * Add the given entry to the index table. This willi take a reference to the
134*4882a593Smuzhiyun * entry's resource so that the entry is not deleted before it is removed from
135*4882a593Smuzhiyun * the * table.
136*4882a593Smuzhiyun */
dbell_index_table_add(struct dbell_entry * entry)137*4882a593Smuzhiyun static void dbell_index_table_add(struct dbell_entry *entry)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun u32 bucket;
140*4882a593Smuzhiyun u32 new_notify_idx;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun vmci_resource_get(&entry->resource);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun spin_lock_bh(&vmci_doorbell_it.lock);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /*
147*4882a593Smuzhiyun * Below we try to allocate an index in the notification
148*4882a593Smuzhiyun * bitmap with "not too much" sharing between resources. If we
149*4882a593Smuzhiyun * use less that the full bitmap, we either add to the end if
150*4882a593Smuzhiyun * there are no unused flags within the currently used area,
151*4882a593Smuzhiyun * or we search for unused ones. If we use the full bitmap, we
152*4882a593Smuzhiyun * allocate the index round robin.
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun if (max_notify_idx < PAGE_SIZE || notify_idx_count < PAGE_SIZE) {
155*4882a593Smuzhiyun if (last_notify_idx_released < max_notify_idx &&
156*4882a593Smuzhiyun !dbell_index_table_find(last_notify_idx_released)) {
157*4882a593Smuzhiyun new_notify_idx = last_notify_idx_released;
158*4882a593Smuzhiyun last_notify_idx_released = PAGE_SIZE;
159*4882a593Smuzhiyun } else {
160*4882a593Smuzhiyun bool reused = false;
161*4882a593Smuzhiyun new_notify_idx = last_notify_idx_reserved;
162*4882a593Smuzhiyun if (notify_idx_count + 1 < max_notify_idx) {
163*4882a593Smuzhiyun do {
164*4882a593Smuzhiyun if (!dbell_index_table_find
165*4882a593Smuzhiyun (new_notify_idx)) {
166*4882a593Smuzhiyun reused = true;
167*4882a593Smuzhiyun break;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun new_notify_idx = (new_notify_idx + 1) %
170*4882a593Smuzhiyun max_notify_idx;
171*4882a593Smuzhiyun } while (new_notify_idx !=
172*4882a593Smuzhiyun last_notify_idx_released);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun if (!reused) {
175*4882a593Smuzhiyun new_notify_idx = max_notify_idx;
176*4882a593Smuzhiyun max_notify_idx++;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun } else {
180*4882a593Smuzhiyun new_notify_idx = (last_notify_idx_reserved + 1) % PAGE_SIZE;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun last_notify_idx_reserved = new_notify_idx;
184*4882a593Smuzhiyun notify_idx_count++;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun entry->idx = new_notify_idx;
187*4882a593Smuzhiyun bucket = VMCI_DOORBELL_HASH(entry->idx);
188*4882a593Smuzhiyun hlist_add_head(&entry->node, &vmci_doorbell_it.entries[bucket]);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun spin_unlock_bh(&vmci_doorbell_it.lock);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * Remove the given entry from the index table. This will release() the
195*4882a593Smuzhiyun * entry's resource.
196*4882a593Smuzhiyun */
dbell_index_table_remove(struct dbell_entry * entry)197*4882a593Smuzhiyun static void dbell_index_table_remove(struct dbell_entry *entry)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun spin_lock_bh(&vmci_doorbell_it.lock);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun hlist_del_init(&entry->node);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun notify_idx_count--;
204*4882a593Smuzhiyun if (entry->idx == max_notify_idx - 1) {
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun * If we delete an entry with the maximum known
207*4882a593Smuzhiyun * notification index, we take the opportunity to
208*4882a593Smuzhiyun * prune the current max. As there might be other
209*4882a593Smuzhiyun * unused indices immediately below, we lower the
210*4882a593Smuzhiyun * maximum until we hit an index in use.
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun while (max_notify_idx > 0 &&
213*4882a593Smuzhiyun !dbell_index_table_find(max_notify_idx - 1))
214*4882a593Smuzhiyun max_notify_idx--;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun last_notify_idx_released = entry->idx;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun spin_unlock_bh(&vmci_doorbell_it.lock);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun vmci_resource_put(&entry->resource);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun * Creates a link between the given doorbell handle and the given
226*4882a593Smuzhiyun * index in the bitmap in the device backend. A notification state
227*4882a593Smuzhiyun * is created in hypervisor.
228*4882a593Smuzhiyun */
dbell_link(struct vmci_handle handle,u32 notify_idx)229*4882a593Smuzhiyun static int dbell_link(struct vmci_handle handle, u32 notify_idx)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun struct vmci_doorbell_link_msg link_msg;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun link_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
234*4882a593Smuzhiyun VMCI_DOORBELL_LINK);
235*4882a593Smuzhiyun link_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
236*4882a593Smuzhiyun link_msg.hdr.payload_size = sizeof(link_msg) - VMCI_DG_HEADERSIZE;
237*4882a593Smuzhiyun link_msg.handle = handle;
238*4882a593Smuzhiyun link_msg.notify_idx = notify_idx;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun return vmci_send_datagram(&link_msg.hdr);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * Unlinks the given doorbell handle from an index in the bitmap in
245*4882a593Smuzhiyun * the device backend. The notification state is destroyed in hypervisor.
246*4882a593Smuzhiyun */
dbell_unlink(struct vmci_handle handle)247*4882a593Smuzhiyun static int dbell_unlink(struct vmci_handle handle)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun struct vmci_doorbell_unlink_msg unlink_msg;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun unlink_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
252*4882a593Smuzhiyun VMCI_DOORBELL_UNLINK);
253*4882a593Smuzhiyun unlink_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
254*4882a593Smuzhiyun unlink_msg.hdr.payload_size = sizeof(unlink_msg) - VMCI_DG_HEADERSIZE;
255*4882a593Smuzhiyun unlink_msg.handle = handle;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun return vmci_send_datagram(&unlink_msg.hdr);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /*
261*4882a593Smuzhiyun * Notify another guest or the host. We send a datagram down to the
262*4882a593Smuzhiyun * host via the hypervisor with the notification info.
263*4882a593Smuzhiyun */
dbell_notify_as_guest(struct vmci_handle handle,u32 priv_flags)264*4882a593Smuzhiyun static int dbell_notify_as_guest(struct vmci_handle handle, u32 priv_flags)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun struct vmci_doorbell_notify_msg notify_msg;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun notify_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
269*4882a593Smuzhiyun VMCI_DOORBELL_NOTIFY);
270*4882a593Smuzhiyun notify_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
271*4882a593Smuzhiyun notify_msg.hdr.payload_size = sizeof(notify_msg) - VMCI_DG_HEADERSIZE;
272*4882a593Smuzhiyun notify_msg.handle = handle;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun return vmci_send_datagram(¬ify_msg.hdr);
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /*
278*4882a593Smuzhiyun * Calls the specified callback in a delayed context.
279*4882a593Smuzhiyun */
dbell_delayed_dispatch(struct work_struct * work)280*4882a593Smuzhiyun static void dbell_delayed_dispatch(struct work_struct *work)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun struct dbell_entry *entry = container_of(work,
283*4882a593Smuzhiyun struct dbell_entry, work);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun entry->notify_cb(entry->client_data);
286*4882a593Smuzhiyun vmci_resource_put(&entry->resource);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * Dispatches a doorbell notification to the host context.
291*4882a593Smuzhiyun */
vmci_dbell_host_context_notify(u32 src_cid,struct vmci_handle handle)292*4882a593Smuzhiyun int vmci_dbell_host_context_notify(u32 src_cid, struct vmci_handle handle)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun struct dbell_entry *entry;
295*4882a593Smuzhiyun struct vmci_resource *resource;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun if (vmci_handle_is_invalid(handle)) {
298*4882a593Smuzhiyun pr_devel("Notifying an invalid doorbell (handle=0x%x:0x%x)\n",
299*4882a593Smuzhiyun handle.context, handle.resource);
300*4882a593Smuzhiyun return VMCI_ERROR_INVALID_ARGS;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun resource = vmci_resource_by_handle(handle,
304*4882a593Smuzhiyun VMCI_RESOURCE_TYPE_DOORBELL);
305*4882a593Smuzhiyun if (!resource) {
306*4882a593Smuzhiyun pr_devel("Notifying an unknown doorbell (handle=0x%x:0x%x)\n",
307*4882a593Smuzhiyun handle.context, handle.resource);
308*4882a593Smuzhiyun return VMCI_ERROR_NOT_FOUND;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun entry = container_of(resource, struct dbell_entry, resource);
312*4882a593Smuzhiyun if (entry->run_delayed) {
313*4882a593Smuzhiyun if (!schedule_work(&entry->work))
314*4882a593Smuzhiyun vmci_resource_put(resource);
315*4882a593Smuzhiyun } else {
316*4882a593Smuzhiyun entry->notify_cb(entry->client_data);
317*4882a593Smuzhiyun vmci_resource_put(resource);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun return VMCI_SUCCESS;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /*
324*4882a593Smuzhiyun * Register the notification bitmap with the host.
325*4882a593Smuzhiyun */
vmci_dbell_register_notification_bitmap(u64 bitmap_ppn)326*4882a593Smuzhiyun bool vmci_dbell_register_notification_bitmap(u64 bitmap_ppn)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun int result;
329*4882a593Smuzhiyun struct vmci_notify_bm_set_msg bitmap_set_msg = { };
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun bitmap_set_msg.hdr.dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
332*4882a593Smuzhiyun VMCI_SET_NOTIFY_BITMAP);
333*4882a593Smuzhiyun bitmap_set_msg.hdr.src = VMCI_ANON_SRC_HANDLE;
334*4882a593Smuzhiyun bitmap_set_msg.hdr.payload_size = sizeof(bitmap_set_msg) -
335*4882a593Smuzhiyun VMCI_DG_HEADERSIZE;
336*4882a593Smuzhiyun if (vmci_use_ppn64())
337*4882a593Smuzhiyun bitmap_set_msg.bitmap_ppn64 = bitmap_ppn;
338*4882a593Smuzhiyun else
339*4882a593Smuzhiyun bitmap_set_msg.bitmap_ppn32 = (u32) bitmap_ppn;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun result = vmci_send_datagram(&bitmap_set_msg.hdr);
342*4882a593Smuzhiyun if (result != VMCI_SUCCESS) {
343*4882a593Smuzhiyun pr_devel("Failed to register (PPN=%llu) as notification bitmap (error=%d)\n",
344*4882a593Smuzhiyun bitmap_ppn, result);
345*4882a593Smuzhiyun return false;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun return true;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun /*
351*4882a593Smuzhiyun * Executes or schedules the handlers for a given notify index.
352*4882a593Smuzhiyun */
dbell_fire_entries(u32 notify_idx)353*4882a593Smuzhiyun static void dbell_fire_entries(u32 notify_idx)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun u32 bucket = VMCI_DOORBELL_HASH(notify_idx);
356*4882a593Smuzhiyun struct dbell_entry *dbell;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun spin_lock_bh(&vmci_doorbell_it.lock);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun hlist_for_each_entry(dbell, &vmci_doorbell_it.entries[bucket], node) {
361*4882a593Smuzhiyun if (dbell->idx == notify_idx &&
362*4882a593Smuzhiyun atomic_read(&dbell->active) == 1) {
363*4882a593Smuzhiyun if (dbell->run_delayed) {
364*4882a593Smuzhiyun vmci_resource_get(&dbell->resource);
365*4882a593Smuzhiyun if (!schedule_work(&dbell->work))
366*4882a593Smuzhiyun vmci_resource_put(&dbell->resource);
367*4882a593Smuzhiyun } else {
368*4882a593Smuzhiyun dbell->notify_cb(dbell->client_data);
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun spin_unlock_bh(&vmci_doorbell_it.lock);
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /*
377*4882a593Smuzhiyun * Scans the notification bitmap, collects pending notifications,
378*4882a593Smuzhiyun * resets the bitmap and invokes appropriate callbacks.
379*4882a593Smuzhiyun */
vmci_dbell_scan_notification_entries(u8 * bitmap)380*4882a593Smuzhiyun void vmci_dbell_scan_notification_entries(u8 *bitmap)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun u32 idx;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun for (idx = 0; idx < max_notify_idx; idx++) {
385*4882a593Smuzhiyun if (bitmap[idx] & 0x1) {
386*4882a593Smuzhiyun bitmap[idx] &= ~1;
387*4882a593Smuzhiyun dbell_fire_entries(idx);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun * vmci_doorbell_create() - Creates a doorbell
394*4882a593Smuzhiyun * @handle: A handle used to track the resource. Can be invalid.
395*4882a593Smuzhiyun * @flags: Flag that determines context of callback.
396*4882a593Smuzhiyun * @priv_flags: Privileges flags.
397*4882a593Smuzhiyun * @notify_cb: The callback to be ivoked when the doorbell fires.
398*4882a593Smuzhiyun * @client_data: A parameter to be passed to the callback.
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * Creates a doorbell with the given callback. If the handle is
401*4882a593Smuzhiyun * VMCI_INVALID_HANDLE, a free handle will be assigned, if
402*4882a593Smuzhiyun * possible. The callback can be run immediately (potentially with
403*4882a593Smuzhiyun * locks held - the default) or delayed (in a kernel thread) by
404*4882a593Smuzhiyun * specifying the flag VMCI_FLAG_DELAYED_CB. If delayed execution
405*4882a593Smuzhiyun * is selected, a given callback may not be run if the kernel is
406*4882a593Smuzhiyun * unable to allocate memory for the delayed execution (highly
407*4882a593Smuzhiyun * unlikely).
408*4882a593Smuzhiyun */
vmci_doorbell_create(struct vmci_handle * handle,u32 flags,u32 priv_flags,vmci_callback notify_cb,void * client_data)409*4882a593Smuzhiyun int vmci_doorbell_create(struct vmci_handle *handle,
410*4882a593Smuzhiyun u32 flags,
411*4882a593Smuzhiyun u32 priv_flags,
412*4882a593Smuzhiyun vmci_callback notify_cb, void *client_data)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun struct dbell_entry *entry;
415*4882a593Smuzhiyun struct vmci_handle new_handle;
416*4882a593Smuzhiyun int result;
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun if (!handle || !notify_cb || flags & ~VMCI_FLAG_DELAYED_CB ||
419*4882a593Smuzhiyun priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS)
420*4882a593Smuzhiyun return VMCI_ERROR_INVALID_ARGS;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun entry = kmalloc(sizeof(*entry), GFP_KERNEL);
423*4882a593Smuzhiyun if (entry == NULL) {
424*4882a593Smuzhiyun pr_warn("Failed allocating memory for datagram entry\n");
425*4882a593Smuzhiyun return VMCI_ERROR_NO_MEM;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (vmci_handle_is_invalid(*handle)) {
429*4882a593Smuzhiyun u32 context_id = vmci_get_context_id();
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun if (context_id == VMCI_INVALID_ID) {
432*4882a593Smuzhiyun pr_warn("Failed to get context ID\n");
433*4882a593Smuzhiyun result = VMCI_ERROR_NO_RESOURCES;
434*4882a593Smuzhiyun goto free_mem;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* Let resource code allocate a free ID for us */
438*4882a593Smuzhiyun new_handle = vmci_make_handle(context_id, VMCI_INVALID_ID);
439*4882a593Smuzhiyun } else {
440*4882a593Smuzhiyun bool valid_context = false;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun * Validate the handle. We must do both of the checks below
444*4882a593Smuzhiyun * because we can be acting as both a host and a guest at the
445*4882a593Smuzhiyun * same time. We always allow the host context ID, since the
446*4882a593Smuzhiyun * host functionality is in practice always there with the
447*4882a593Smuzhiyun * unified driver.
448*4882a593Smuzhiyun */
449*4882a593Smuzhiyun if (handle->context == VMCI_HOST_CONTEXT_ID ||
450*4882a593Smuzhiyun (vmci_guest_code_active() &&
451*4882a593Smuzhiyun vmci_get_context_id() == handle->context)) {
452*4882a593Smuzhiyun valid_context = true;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun if (!valid_context || handle->resource == VMCI_INVALID_ID) {
456*4882a593Smuzhiyun pr_devel("Invalid argument (handle=0x%x:0x%x)\n",
457*4882a593Smuzhiyun handle->context, handle->resource);
458*4882a593Smuzhiyun result = VMCI_ERROR_INVALID_ARGS;
459*4882a593Smuzhiyun goto free_mem;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun new_handle = *handle;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun entry->idx = 0;
466*4882a593Smuzhiyun INIT_HLIST_NODE(&entry->node);
467*4882a593Smuzhiyun entry->priv_flags = priv_flags;
468*4882a593Smuzhiyun INIT_WORK(&entry->work, dbell_delayed_dispatch);
469*4882a593Smuzhiyun entry->run_delayed = flags & VMCI_FLAG_DELAYED_CB;
470*4882a593Smuzhiyun entry->notify_cb = notify_cb;
471*4882a593Smuzhiyun entry->client_data = client_data;
472*4882a593Smuzhiyun atomic_set(&entry->active, 0);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun result = vmci_resource_add(&entry->resource,
475*4882a593Smuzhiyun VMCI_RESOURCE_TYPE_DOORBELL,
476*4882a593Smuzhiyun new_handle);
477*4882a593Smuzhiyun if (result != VMCI_SUCCESS) {
478*4882a593Smuzhiyun pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d\n",
479*4882a593Smuzhiyun new_handle.context, new_handle.resource, result);
480*4882a593Smuzhiyun goto free_mem;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun new_handle = vmci_resource_handle(&entry->resource);
484*4882a593Smuzhiyun if (vmci_guest_code_active()) {
485*4882a593Smuzhiyun dbell_index_table_add(entry);
486*4882a593Smuzhiyun result = dbell_link(new_handle, entry->idx);
487*4882a593Smuzhiyun if (VMCI_SUCCESS != result)
488*4882a593Smuzhiyun goto destroy_resource;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun atomic_set(&entry->active, 1);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun *handle = new_handle;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun return result;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun destroy_resource:
498*4882a593Smuzhiyun dbell_index_table_remove(entry);
499*4882a593Smuzhiyun vmci_resource_remove(&entry->resource);
500*4882a593Smuzhiyun free_mem:
501*4882a593Smuzhiyun kfree(entry);
502*4882a593Smuzhiyun return result;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(vmci_doorbell_create);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /*
507*4882a593Smuzhiyun * vmci_doorbell_destroy() - Destroy a doorbell.
508*4882a593Smuzhiyun * @handle: The handle tracking the resource.
509*4882a593Smuzhiyun *
510*4882a593Smuzhiyun * Destroys a doorbell previously created with vmcii_doorbell_create. This
511*4882a593Smuzhiyun * operation may block waiting for a callback to finish.
512*4882a593Smuzhiyun */
vmci_doorbell_destroy(struct vmci_handle handle)513*4882a593Smuzhiyun int vmci_doorbell_destroy(struct vmci_handle handle)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun struct dbell_entry *entry;
516*4882a593Smuzhiyun struct vmci_resource *resource;
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun if (vmci_handle_is_invalid(handle))
519*4882a593Smuzhiyun return VMCI_ERROR_INVALID_ARGS;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun resource = vmci_resource_by_handle(handle,
522*4882a593Smuzhiyun VMCI_RESOURCE_TYPE_DOORBELL);
523*4882a593Smuzhiyun if (!resource) {
524*4882a593Smuzhiyun pr_devel("Failed to destroy doorbell (handle=0x%x:0x%x)\n",
525*4882a593Smuzhiyun handle.context, handle.resource);
526*4882a593Smuzhiyun return VMCI_ERROR_NOT_FOUND;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun entry = container_of(resource, struct dbell_entry, resource);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun if (!hlist_unhashed(&entry->node)) {
532*4882a593Smuzhiyun int result;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun dbell_index_table_remove(entry);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun result = dbell_unlink(handle);
537*4882a593Smuzhiyun if (VMCI_SUCCESS != result) {
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /*
540*4882a593Smuzhiyun * The only reason this should fail would be
541*4882a593Smuzhiyun * an inconsistency between guest and
542*4882a593Smuzhiyun * hypervisor state, where the guest believes
543*4882a593Smuzhiyun * it has an active registration whereas the
544*4882a593Smuzhiyun * hypervisor doesn't. One case where this may
545*4882a593Smuzhiyun * happen is if a doorbell is unregistered
546*4882a593Smuzhiyun * following a hibernation at a time where the
547*4882a593Smuzhiyun * doorbell state hasn't been restored on the
548*4882a593Smuzhiyun * hypervisor side yet. Since the handle has
549*4882a593Smuzhiyun * now been removed in the guest, we just
550*4882a593Smuzhiyun * print a warning and return success.
551*4882a593Smuzhiyun */
552*4882a593Smuzhiyun pr_devel("Unlink of doorbell (handle=0x%x:0x%x) unknown by hypervisor (error=%d)\n",
553*4882a593Smuzhiyun handle.context, handle.resource, result);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /*
558*4882a593Smuzhiyun * Now remove the resource from the table. It might still be in use
559*4882a593Smuzhiyun * after this, in a callback or still on the delayed work queue.
560*4882a593Smuzhiyun */
561*4882a593Smuzhiyun vmci_resource_put(&entry->resource);
562*4882a593Smuzhiyun vmci_resource_remove(&entry->resource);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun kfree(entry);
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun return VMCI_SUCCESS;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(vmci_doorbell_destroy);
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun /*
571*4882a593Smuzhiyun * vmci_doorbell_notify() - Ring the doorbell (and hide in the bushes).
572*4882a593Smuzhiyun * @dst: The handlle identifying the doorbell resource
573*4882a593Smuzhiyun * @priv_flags: Priviledge flags.
574*4882a593Smuzhiyun *
575*4882a593Smuzhiyun * Generates a notification on the doorbell identified by the
576*4882a593Smuzhiyun * handle. For host side generation of notifications, the caller
577*4882a593Smuzhiyun * can specify what the privilege of the calling side is.
578*4882a593Smuzhiyun */
vmci_doorbell_notify(struct vmci_handle dst,u32 priv_flags)579*4882a593Smuzhiyun int vmci_doorbell_notify(struct vmci_handle dst, u32 priv_flags)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun int retval;
582*4882a593Smuzhiyun enum vmci_route route;
583*4882a593Smuzhiyun struct vmci_handle src;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun if (vmci_handle_is_invalid(dst) ||
586*4882a593Smuzhiyun (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS))
587*4882a593Smuzhiyun return VMCI_ERROR_INVALID_ARGS;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun src = VMCI_INVALID_HANDLE;
590*4882a593Smuzhiyun retval = vmci_route(&src, &dst, false, &route);
591*4882a593Smuzhiyun if (retval < VMCI_SUCCESS)
592*4882a593Smuzhiyun return retval;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun if (VMCI_ROUTE_AS_HOST == route)
595*4882a593Smuzhiyun return vmci_ctx_notify_dbell(VMCI_HOST_CONTEXT_ID,
596*4882a593Smuzhiyun dst, priv_flags);
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun if (VMCI_ROUTE_AS_GUEST == route)
599*4882a593Smuzhiyun return dbell_notify_as_guest(dst, priv_flags);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun pr_warn("Unknown route (%d) for doorbell\n", route);
602*4882a593Smuzhiyun return VMCI_ERROR_DST_UNREACHABLE;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(vmci_doorbell_notify);
605