/*
* VMware VMCI Driver
*
* Copyright (C) 2012 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <linux/vmw_vmci_defs.h>
#include <linux/vmw_vmci_api.h>
#include <linux/highmem.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include "vmci_queue_pair.h"
#include "vmci_datagram.h"
#include "vmci_doorbell.h"
#include "vmci_context.h"
#include "vmci_driver.h"
#include "vmci_event.h"
/*
* List of current VMCI contexts. Contexts can be added by
* vmci_ctx_create() and removed via vmci_ctx_destroy().
* These, along with context lookup, are protected by the
* list structure's lock.
*/
static struct {
struct list_head head;
spinlock_t lock; /* Spinlock for context list operations */
} ctx_list = {
.head = LIST_HEAD_INIT(ctx_list.head),
.lock = __SPIN_LOCK_UNLOCKED(ctx_list.lock),
};
/* Used by contexts that did not set up notify flag pointers */
static bool ctx_dummy_notify;
static void ctx_signal_notify(struct vmci_ctx *context)
{
*context->notify = true;
}
static void ctx_clear_notify(struct vmci_ctx *context)
{
*context->notify = false;
}
/*
* If nothing requires the attention of the guest, clears both
* notify flag and call.
*/
static void ctx_clear_notify_call(struct vmci_ctx *context)
{
if (context->pending_datagrams == 0 &&
vmci_handle_arr_get_size(context->pending_doorbell_array) == 0)
ctx_clear_notify(context);
}
/*
* Sets the context's notify flag iff datagrams are pending for this
* context. Called from vmci_setup_notify().
*/
void vmci_ctx_check_signal_notify(struct vmci_ctx *context)
{
spin_lock(&context->lock);
if (context->pending_datagrams)
ctx_signal_notify(context);
spin_unlock(&context->lock);
}
/*
* Allocates and initializes a VMCI context.
*/
struct vmci_ctx *vmci_ctx_create(u32 cid, u32 priv_flags,
uintptr_t event_hnd,
int user_version,
const struct cred *cred)
{
struct vmci_ctx *context;
int error;
if (cid == VMCI_INVALID_ID) {
pr_devel("Invalid context ID for VMCI context\n");
error = -EINVAL;
goto err_out;
}
if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS) {
pr_devel("Invalid flag (flags=0x%x) for VMCI context\n",
priv_flags);
error = -EINVAL;
goto err_out;
}
if (user_version == 0) {
pr_devel("Invalid suer_version %d\n", user_version);
error = -EINVAL;
goto err_out;
}
context = kzalloc(sizeof(*context), GFP_KERNEL);
if (!context) {
pr_warn("Failed to allocate memory for VMCI context\n");
error = -EINVAL;
goto err_out;
}
kref_init(&context->kref);
spin_lock_init(&context->lock);
INIT_LIST_HEAD(&context->list_item);
INIT_LIST_HEAD(&context->datagram_queue);