Path: blob/master/drivers/infiniband/hw/mlx4/doorbell.c
15112 views
/*1* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*/3132#include <linux/slab.h>3334#include "mlx4_ib.h"3536struct mlx4_ib_user_db_page {37struct list_head list;38struct ib_umem *umem;39unsigned long user_virt;40int refcnt;41};4243int mlx4_ib_db_map_user(struct mlx4_ib_ucontext *context, unsigned long virt,44struct mlx4_db *db)45{46struct mlx4_ib_user_db_page *page;47struct ib_umem_chunk *chunk;48int err = 0;4950mutex_lock(&context->db_page_mutex);5152list_for_each_entry(page, &context->db_page_list, list)53if (page->user_virt == (virt & PAGE_MASK))54goto found;5556page = kmalloc(sizeof *page, GFP_KERNEL);57if (!page) {58err = -ENOMEM;59goto out;60}6162page->user_virt = (virt & PAGE_MASK);63page->refcnt = 0;64page->umem = ib_umem_get(&context->ibucontext, virt & PAGE_MASK,65PAGE_SIZE, 0, 0);66if (IS_ERR(page->umem)) {67err = PTR_ERR(page->umem);68kfree(page);69goto out;70}7172list_add(&page->list, &context->db_page_list);7374found:75chunk = list_entry(page->umem->chunk_list.next, struct ib_umem_chunk, list);76db->dma = sg_dma_address(chunk->page_list) + (virt & ~PAGE_MASK);77db->u.user_page = page;78++page->refcnt;7980out:81mutex_unlock(&context->db_page_mutex);8283return err;84}8586void mlx4_ib_db_unmap_user(struct mlx4_ib_ucontext *context, struct mlx4_db *db)87{88mutex_lock(&context->db_page_mutex);8990if (!--db->u.user_page->refcnt) {91list_del(&db->u.user_page->list);92ib_umem_release(db->u.user_page->umem);93kfree(db->u.user_page);94}9596mutex_unlock(&context->db_page_mutex);97}9899100