18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/kref.h>
348c2ecf20Sopenharmony_ci#include <linux/slab.h>
358c2ecf20Sopenharmony_ci#include <rdma/ib_umem.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include "mlx5_ib.h"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct mlx5_ib_user_db_page {
408c2ecf20Sopenharmony_ci	struct list_head	list;
418c2ecf20Sopenharmony_ci	struct ib_umem	       *umem;
428c2ecf20Sopenharmony_ci	unsigned long		user_virt;
438c2ecf20Sopenharmony_ci	int			refcnt;
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciint mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context,
478c2ecf20Sopenharmony_ci			struct ib_udata *udata, unsigned long virt,
488c2ecf20Sopenharmony_ci			struct mlx5_db *db)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct mlx5_ib_user_db_page *page;
518c2ecf20Sopenharmony_ci	int err = 0;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	mutex_lock(&context->db_page_mutex);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	list_for_each_entry(page, &context->db_page_list, list)
568c2ecf20Sopenharmony_ci		if (page->user_virt == (virt & PAGE_MASK))
578c2ecf20Sopenharmony_ci			goto found;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	page = kmalloc(sizeof(*page), GFP_KERNEL);
608c2ecf20Sopenharmony_ci	if (!page) {
618c2ecf20Sopenharmony_ci		err = -ENOMEM;
628c2ecf20Sopenharmony_ci		goto out;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	page->user_virt = (virt & PAGE_MASK);
668c2ecf20Sopenharmony_ci	page->refcnt    = 0;
678c2ecf20Sopenharmony_ci	page->umem = ib_umem_get(context->ibucontext.device, virt & PAGE_MASK,
688c2ecf20Sopenharmony_ci				 PAGE_SIZE, 0);
698c2ecf20Sopenharmony_ci	if (IS_ERR(page->umem)) {
708c2ecf20Sopenharmony_ci		err = PTR_ERR(page->umem);
718c2ecf20Sopenharmony_ci		kfree(page);
728c2ecf20Sopenharmony_ci		goto out;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	list_add(&page->list, &context->db_page_list);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cifound:
788c2ecf20Sopenharmony_ci	db->dma = sg_dma_address(page->umem->sg_head.sgl) + (virt & ~PAGE_MASK);
798c2ecf20Sopenharmony_ci	db->u.user_page = page;
808c2ecf20Sopenharmony_ci	++page->refcnt;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ciout:
838c2ecf20Sopenharmony_ci	mutex_unlock(&context->db_page_mutex);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return err;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_civoid mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	mutex_lock(&context->db_page_mutex);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (!--db->u.user_page->refcnt) {
938c2ecf20Sopenharmony_ci		list_del(&db->u.user_page->list);
948c2ecf20Sopenharmony_ci		ib_umem_release(db->u.user_page->umem);
958c2ecf20Sopenharmony_ci		kfree(db->u.user_page);
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	mutex_unlock(&context->db_page_mutex);
998c2ecf20Sopenharmony_ci}
100