uverbs_cmd.c

来自「底层驱动开发」· C语言 代码 · 共 1,266 行 · 第 1/3 页

C
1,266
字号
/* * Copyright (c) 2005 Topspin Communications.  All rights reserved. * Copyright (c) 2005 Cisco Systems.  All rights reserved. * * This software is available to you under a choice of one of two * licenses.  You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * *     Redistribution and use in source and binary forms, with or *     without modification, are permitted provided that the following *     conditions are met: * *      - Redistributions of source code must retain the above *        copyright notice, this list of conditions and the following *        disclaimer. * *      - Redistributions in binary form must reproduce the above *        copyright notice, this list of conditions and the following *        disclaimer in the documentation and/or other materials *        provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * $Id: uverbs_cmd.c 2708 2005-06-24 17:27:21Z roland $ */#include <asm/uaccess.h>#include "uverbs.h"#define INIT_UDATA(udata, ibuf, obuf, ilen, olen)			\	do {								\		(udata)->inbuf  = (void __user *) (ibuf);		\		(udata)->outbuf = (void __user *) (obuf);		\		(udata)->inlen  = (ilen);				\		(udata)->outlen = (olen);				\	} while (0)ssize_t ib_uverbs_query_params(struct ib_uverbs_file *file,			       const char __user *buf,			       int in_len, int out_len){	struct ib_uverbs_query_params      cmd;	struct ib_uverbs_query_params_resp resp;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	memset(&resp, 0, sizeof resp);	resp.num_cq_events = file->device->num_comp;	if (copy_to_user((void __user *) (unsigned long) cmd.response, &resp, sizeof resp))	    return -EFAULT;	return in_len;}ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,			      const char __user *buf,			      int in_len, int out_len){	struct ib_uverbs_get_context      cmd;	struct ib_uverbs_get_context_resp resp;	struct ib_udata                   udata;	struct ib_device                 *ibdev = file->device->ib_dev;	struct ib_ucontext		 *ucontext;	int i;	int ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	down(&file->mutex);	if (file->ucontext) {		ret = -EINVAL;		goto err;	}	INIT_UDATA(&udata, buf + sizeof cmd,		   (unsigned long) cmd.response + sizeof resp,		   in_len - sizeof cmd, out_len - sizeof resp);	ucontext = ibdev->alloc_ucontext(ibdev, &udata);	if (IS_ERR(ucontext))		return PTR_ERR(file->ucontext);	ucontext->device = ibdev;	INIT_LIST_HEAD(&ucontext->pd_list);	INIT_LIST_HEAD(&ucontext->mr_list);	INIT_LIST_HEAD(&ucontext->mw_list);	INIT_LIST_HEAD(&ucontext->cq_list);	INIT_LIST_HEAD(&ucontext->qp_list);	INIT_LIST_HEAD(&ucontext->srq_list);	INIT_LIST_HEAD(&ucontext->ah_list);	resp.async_fd = file->async_file.fd;	for (i = 0; i < file->device->num_comp; ++i)		if (copy_to_user((void __user *) (unsigned long) cmd.cq_fd_tab +				 i * sizeof (__u32),				 &file->comp_file[i].fd, sizeof (__u32))) {			ret = -EFAULT;			goto err_free;		}	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp)) {		ret = -EFAULT;		goto err_free;	}	file->ucontext = ucontext;	up(&file->mutex);	return in_len;err_free:	ibdev->dealloc_ucontext(ucontext);err:	up(&file->mutex);	return ret;}ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,			       const char __user *buf,			       int in_len, int out_len){	struct ib_uverbs_query_device      cmd;	struct ib_uverbs_query_device_resp resp;	struct ib_device_attr              attr;	int                                ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	ret = ib_query_device(file->device->ib_dev, &attr);	if (ret)		return ret;	memset(&resp, 0, sizeof resp);	resp.fw_ver 		       = attr.fw_ver;	resp.node_guid 		       = attr.node_guid;	resp.sys_image_guid 	       = attr.sys_image_guid;	resp.max_mr_size 	       = attr.max_mr_size;	resp.page_size_cap 	       = attr.page_size_cap;	resp.vendor_id 		       = attr.vendor_id;	resp.vendor_part_id 	       = attr.vendor_part_id;	resp.hw_ver 		       = attr.hw_ver;	resp.max_qp 		       = attr.max_qp;	resp.max_qp_wr 		       = attr.max_qp_wr;	resp.device_cap_flags 	       = attr.device_cap_flags;	resp.max_sge 		       = attr.max_sge;	resp.max_sge_rd 	       = attr.max_sge_rd;	resp.max_cq 		       = attr.max_cq;	resp.max_cqe 		       = attr.max_cqe;	resp.max_mr 		       = attr.max_mr;	resp.max_pd 		       = attr.max_pd;	resp.max_qp_rd_atom 	       = attr.max_qp_rd_atom;	resp.max_ee_rd_atom 	       = attr.max_ee_rd_atom;	resp.max_res_rd_atom 	       = attr.max_res_rd_atom;	resp.max_qp_init_rd_atom       = attr.max_qp_init_rd_atom;	resp.max_ee_init_rd_atom       = attr.max_ee_init_rd_atom;	resp.atomic_cap 	       = attr.atomic_cap;	resp.max_ee 		       = attr.max_ee;	resp.max_rdd 		       = attr.max_rdd;	resp.max_mw 		       = attr.max_mw;	resp.max_raw_ipv6_qp 	       = attr.max_raw_ipv6_qp;	resp.max_raw_ethy_qp 	       = attr.max_raw_ethy_qp;	resp.max_mcast_grp 	       = attr.max_mcast_grp;	resp.max_mcast_qp_attach       = attr.max_mcast_qp_attach;	resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;	resp.max_ah 		       = attr.max_ah;	resp.max_fmr 		       = attr.max_fmr;	resp.max_map_per_fmr 	       = attr.max_map_per_fmr;	resp.max_srq 		       = attr.max_srq;	resp.max_srq_wr 	       = attr.max_srq_wr;	resp.max_srq_sge 	       = attr.max_srq_sge;	resp.max_pkeys 		       = attr.max_pkeys;	resp.local_ca_ack_delay        = attr.local_ca_ack_delay;	resp.phys_port_cnt	       = file->device->ib_dev->phys_port_cnt;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp))		return -EFAULT;	return in_len;}ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,			     const char __user *buf,			     int in_len, int out_len){	struct ib_uverbs_query_port      cmd;	struct ib_uverbs_query_port_resp resp;	struct ib_port_attr              attr;	int                              ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);	if (ret)		return ret;	memset(&resp, 0, sizeof resp);	resp.state 	     = attr.state;	resp.max_mtu 	     = attr.max_mtu;	resp.active_mtu      = attr.active_mtu;	resp.gid_tbl_len     = attr.gid_tbl_len;	resp.port_cap_flags  = attr.port_cap_flags;	resp.max_msg_sz      = attr.max_msg_sz;	resp.bad_pkey_cntr   = attr.bad_pkey_cntr;	resp.qkey_viol_cntr  = attr.qkey_viol_cntr;	resp.pkey_tbl_len    = attr.pkey_tbl_len;	resp.lid 	     = attr.lid;	resp.sm_lid 	     = attr.sm_lid;	resp.lmc 	     = attr.lmc;	resp.max_vl_num      = attr.max_vl_num;	resp.sm_sl 	     = attr.sm_sl;	resp.subnet_timeout  = attr.subnet_timeout;	resp.init_type_reply = attr.init_type_reply;	resp.active_width    = attr.active_width;	resp.active_speed    = attr.active_speed;	resp.phys_state      = attr.phys_state;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp))		return -EFAULT;	return in_len;}ssize_t ib_uverbs_query_gid(struct ib_uverbs_file *file,			    const char __user *buf,			    int in_len, int out_len){	struct ib_uverbs_query_gid      cmd;	struct ib_uverbs_query_gid_resp resp;	int                             ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	memset(&resp, 0, sizeof resp);	ret = ib_query_gid(file->device->ib_dev, cmd.port_num, cmd.index,			   (union ib_gid *) resp.gid);	if (ret)		return ret;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp))		return -EFAULT;	return in_len;}ssize_t ib_uverbs_query_pkey(struct ib_uverbs_file *file,			     const char __user *buf,			     int in_len, int out_len){	struct ib_uverbs_query_pkey      cmd;	struct ib_uverbs_query_pkey_resp resp;	int                              ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	memset(&resp, 0, sizeof resp);	ret = ib_query_pkey(file->device->ib_dev, cmd.port_num, cmd.index,			    &resp.pkey);	if (ret)		return ret;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp))		return -EFAULT;	return in_len;}ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,			   const char __user *buf,			   int in_len, int out_len){	struct ib_uverbs_alloc_pd      cmd;	struct ib_uverbs_alloc_pd_resp resp;	struct ib_udata                udata;	struct ib_uobject             *uobj;	struct ib_pd                  *pd;	int                            ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	INIT_UDATA(&udata, buf + sizeof cmd,		   (unsigned long) cmd.response + sizeof resp,		   in_len - sizeof cmd, out_len - sizeof resp);	uobj = kmalloc(sizeof *uobj, GFP_KERNEL);	if (!uobj)		return -ENOMEM;	uobj->context = file->ucontext;	pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,					    file->ucontext, &udata);	if (IS_ERR(pd)) {		ret = PTR_ERR(pd);		goto err;	}	pd->device  = file->device->ib_dev;	pd->uobject = uobj;	atomic_set(&pd->usecnt, 0);retry:	if (!idr_pre_get(&ib_uverbs_pd_idr, GFP_KERNEL)) {		ret = -ENOMEM;		goto err_pd;	}	down(&ib_uverbs_idr_mutex);	ret = idr_get_new(&ib_uverbs_pd_idr, pd, &uobj->id);	up(&ib_uverbs_idr_mutex);	if (ret == -EAGAIN)		goto retry;	if (ret)		goto err_pd;	down(&file->mutex);	list_add_tail(&uobj->list, &file->ucontext->pd_list);	up(&file->mutex);	memset(&resp, 0, sizeof resp);	resp.pd_handle = uobj->id;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp)) {		ret = -EFAULT;		goto err_list;	}	return in_len;err_list: 	down(&file->mutex);	list_del(&uobj->list);	up(&file->mutex);	down(&ib_uverbs_idr_mutex);	idr_remove(&ib_uverbs_pd_idr, uobj->id);	up(&ib_uverbs_idr_mutex);err_pd:	ib_dealloc_pd(pd);err:	kfree(uobj);	return ret;}ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,			     const char __user *buf,			     int in_len, int out_len){	struct ib_uverbs_dealloc_pd cmd;	struct ib_pd               *pd;	struct ib_uobject          *uobj;	int                         ret = -EINVAL;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	down(&ib_uverbs_idr_mutex);	pd = idr_find(&ib_uverbs_pd_idr, cmd.pd_handle);	if (!pd || pd->uobject->context != file->ucontext)		goto out;	uobj = pd->uobject;	ret = ib_dealloc_pd(pd);	if (ret)		goto out;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?