Path: blob/master/drivers/infiniband/core/uverbs_main.c
37212 views
/*1* Copyright (c) 2005 Topspin Communications. All rights reserved.2* Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.3* Copyright (c) 2005 Mellanox Technologies. All rights reserved.4* Copyright (c) 2005 Voltaire, Inc. All rights reserved.5* Copyright (c) 2005 PathScale, Inc. All rights reserved.6*7* This software is available to you under a choice of one of two8* licenses. You may choose to be licensed under the terms of the GNU9* General Public License (GPL) Version 2, available from the file10* COPYING in the main directory of this source tree, or the11* OpenIB.org BSD license below:12*13* Redistribution and use in source and binary forms, with or14* without modification, are permitted provided that the following15* conditions are met:16*17* - Redistributions of source code must retain the above18* copyright notice, this list of conditions and the following19* disclaimer.20*21* - Redistributions in binary form must reproduce the above22* copyright notice, this list of conditions and the following23* disclaimer in the documentation and/or other materials24* provided with the distribution.25*26* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,27* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF28* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND29* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS30* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN31* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN32* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE33* SOFTWARE.34*/3536#include <linux/module.h>37#include <linux/init.h>38#include <linux/device.h>39#include <linux/err.h>40#include <linux/fs.h>41#include <linux/poll.h>42#include <linux/sched.h>43#include <linux/file.h>44#include <linux/cdev.h>45#include <linux/anon_inodes.h>46#include <linux/slab.h>4748#include <asm/uaccess.h>4950#include "uverbs.h"5152MODULE_AUTHOR("Roland Dreier");53MODULE_DESCRIPTION("InfiniBand userspace verbs access");54MODULE_LICENSE("Dual BSD/GPL");5556enum {57IB_UVERBS_MAJOR = 231,58IB_UVERBS_BASE_MINOR = 192,59IB_UVERBS_MAX_DEVICES = 3260};6162#define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)6364static struct class *uverbs_class;6566DEFINE_SPINLOCK(ib_uverbs_idr_lock);67DEFINE_IDR(ib_uverbs_pd_idr);68DEFINE_IDR(ib_uverbs_mr_idr);69DEFINE_IDR(ib_uverbs_mw_idr);70DEFINE_IDR(ib_uverbs_ah_idr);71DEFINE_IDR(ib_uverbs_cq_idr);72DEFINE_IDR(ib_uverbs_qp_idr);73DEFINE_IDR(ib_uverbs_srq_idr);7475static DEFINE_SPINLOCK(map_lock);76static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);7778static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,79const char __user *buf, int in_len,80int out_len) = {81[IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,82[IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,83[IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,84[IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,85[IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,86[IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,87[IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,88[IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,89[IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,90[IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,91[IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,92[IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,93[IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,94[IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,95[IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,96[IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,97[IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,98[IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,99[IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,100[IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,101[IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,102[IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,103[IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,104[IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,105[IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,106[IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,107[IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,108[IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,109};110111static void ib_uverbs_add_one(struct ib_device *device);112static void ib_uverbs_remove_one(struct ib_device *device);113114static void ib_uverbs_release_dev(struct kref *ref)115{116struct ib_uverbs_device *dev =117container_of(ref, struct ib_uverbs_device, ref);118119complete(&dev->comp);120}121122static void ib_uverbs_release_event_file(struct kref *ref)123{124struct ib_uverbs_event_file *file =125container_of(ref, struct ib_uverbs_event_file, ref);126127kfree(file);128}129130void ib_uverbs_release_ucq(struct ib_uverbs_file *file,131struct ib_uverbs_event_file *ev_file,132struct ib_ucq_object *uobj)133{134struct ib_uverbs_event *evt, *tmp;135136if (ev_file) {137spin_lock_irq(&ev_file->lock);138list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {139list_del(&evt->list);140kfree(evt);141}142spin_unlock_irq(&ev_file->lock);143144kref_put(&ev_file->ref, ib_uverbs_release_event_file);145}146147spin_lock_irq(&file->async_file->lock);148list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {149list_del(&evt->list);150kfree(evt);151}152spin_unlock_irq(&file->async_file->lock);153}154155void ib_uverbs_release_uevent(struct ib_uverbs_file *file,156struct ib_uevent_object *uobj)157{158struct ib_uverbs_event *evt, *tmp;159160spin_lock_irq(&file->async_file->lock);161list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {162list_del(&evt->list);163kfree(evt);164}165spin_unlock_irq(&file->async_file->lock);166}167168static void ib_uverbs_detach_umcast(struct ib_qp *qp,169struct ib_uqp_object *uobj)170{171struct ib_uverbs_mcast_entry *mcast, *tmp;172173list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {174ib_detach_mcast(qp, &mcast->gid, mcast->lid);175list_del(&mcast->list);176kfree(mcast);177}178}179180static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,181struct ib_ucontext *context)182{183struct ib_uobject *uobj, *tmp;184185if (!context)186return 0;187188context->closing = 1;189190list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {191struct ib_ah *ah = uobj->object;192193idr_remove_uobj(&ib_uverbs_ah_idr, uobj);194ib_destroy_ah(ah);195kfree(uobj);196}197198list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {199struct ib_qp *qp = uobj->object;200struct ib_uqp_object *uqp =201container_of(uobj, struct ib_uqp_object, uevent.uobject);202203idr_remove_uobj(&ib_uverbs_qp_idr, uobj);204ib_uverbs_detach_umcast(qp, uqp);205ib_destroy_qp(qp);206ib_uverbs_release_uevent(file, &uqp->uevent);207kfree(uqp);208}209210list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {211struct ib_cq *cq = uobj->object;212struct ib_uverbs_event_file *ev_file = cq->cq_context;213struct ib_ucq_object *ucq =214container_of(uobj, struct ib_ucq_object, uobject);215216idr_remove_uobj(&ib_uverbs_cq_idr, uobj);217ib_destroy_cq(cq);218ib_uverbs_release_ucq(file, ev_file, ucq);219kfree(ucq);220}221222list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {223struct ib_srq *srq = uobj->object;224struct ib_uevent_object *uevent =225container_of(uobj, struct ib_uevent_object, uobject);226227idr_remove_uobj(&ib_uverbs_srq_idr, uobj);228ib_destroy_srq(srq);229ib_uverbs_release_uevent(file, uevent);230kfree(uevent);231}232233/* XXX Free MWs */234235list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {236struct ib_mr *mr = uobj->object;237238idr_remove_uobj(&ib_uverbs_mr_idr, uobj);239ib_dereg_mr(mr);240kfree(uobj);241}242243list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {244struct ib_pd *pd = uobj->object;245246idr_remove_uobj(&ib_uverbs_pd_idr, uobj);247ib_dealloc_pd(pd);248kfree(uobj);249}250251return context->device->dealloc_ucontext(context);252}253254static void ib_uverbs_release_file(struct kref *ref)255{256struct ib_uverbs_file *file =257container_of(ref, struct ib_uverbs_file, ref);258259module_put(file->device->ib_dev->owner);260kref_put(&file->device->ref, ib_uverbs_release_dev);261262kfree(file);263}264265static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,266size_t count, loff_t *pos)267{268struct ib_uverbs_event_file *file = filp->private_data;269struct ib_uverbs_event *event;270int eventsz;271int ret = 0;272273spin_lock_irq(&file->lock);274275while (list_empty(&file->event_list)) {276spin_unlock_irq(&file->lock);277278if (filp->f_flags & O_NONBLOCK)279return -EAGAIN;280281if (wait_event_interruptible(file->poll_wait,282!list_empty(&file->event_list)))283return -ERESTARTSYS;284285spin_lock_irq(&file->lock);286}287288event = list_entry(file->event_list.next, struct ib_uverbs_event, list);289290if (file->is_async)291eventsz = sizeof (struct ib_uverbs_async_event_desc);292else293eventsz = sizeof (struct ib_uverbs_comp_event_desc);294295if (eventsz > count) {296ret = -EINVAL;297event = NULL;298} else {299list_del(file->event_list.next);300if (event->counter) {301++(*event->counter);302list_del(&event->obj_list);303}304}305306spin_unlock_irq(&file->lock);307308if (event) {309if (copy_to_user(buf, event, eventsz))310ret = -EFAULT;311else312ret = eventsz;313}314315kfree(event);316317return ret;318}319320static unsigned int ib_uverbs_event_poll(struct file *filp,321struct poll_table_struct *wait)322{323unsigned int pollflags = 0;324struct ib_uverbs_event_file *file = filp->private_data;325326poll_wait(filp, &file->poll_wait, wait);327328spin_lock_irq(&file->lock);329if (!list_empty(&file->event_list))330pollflags = POLLIN | POLLRDNORM;331spin_unlock_irq(&file->lock);332333return pollflags;334}335336static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)337{338struct ib_uverbs_event_file *file = filp->private_data;339340return fasync_helper(fd, filp, on, &file->async_queue);341}342343static int ib_uverbs_event_close(struct inode *inode, struct file *filp)344{345struct ib_uverbs_event_file *file = filp->private_data;346struct ib_uverbs_event *entry, *tmp;347348spin_lock_irq(&file->lock);349file->is_closed = 1;350list_for_each_entry_safe(entry, tmp, &file->event_list, list) {351if (entry->counter)352list_del(&entry->obj_list);353kfree(entry);354}355spin_unlock_irq(&file->lock);356357if (file->is_async) {358ib_unregister_event_handler(&file->uverbs_file->event_handler);359kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);360}361kref_put(&file->ref, ib_uverbs_release_event_file);362363return 0;364}365366static const struct file_operations uverbs_event_fops = {367.owner = THIS_MODULE,368.read = ib_uverbs_event_read,369.poll = ib_uverbs_event_poll,370.release = ib_uverbs_event_close,371.fasync = ib_uverbs_event_fasync,372.llseek = no_llseek,373};374375void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)376{377struct ib_uverbs_event_file *file = cq_context;378struct ib_ucq_object *uobj;379struct ib_uverbs_event *entry;380unsigned long flags;381382if (!file)383return;384385spin_lock_irqsave(&file->lock, flags);386if (file->is_closed) {387spin_unlock_irqrestore(&file->lock, flags);388return;389}390391entry = kmalloc(sizeof *entry, GFP_ATOMIC);392if (!entry) {393spin_unlock_irqrestore(&file->lock, flags);394return;395}396397uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);398399entry->desc.comp.cq_handle = cq->uobject->user_handle;400entry->counter = &uobj->comp_events_reported;401402list_add_tail(&entry->list, &file->event_list);403list_add_tail(&entry->obj_list, &uobj->comp_list);404spin_unlock_irqrestore(&file->lock, flags);405406wake_up_interruptible(&file->poll_wait);407kill_fasync(&file->async_queue, SIGIO, POLL_IN);408}409410static void ib_uverbs_async_handler(struct ib_uverbs_file *file,411__u64 element, __u64 event,412struct list_head *obj_list,413u32 *counter)414{415struct ib_uverbs_event *entry;416unsigned long flags;417418spin_lock_irqsave(&file->async_file->lock, flags);419if (file->async_file->is_closed) {420spin_unlock_irqrestore(&file->async_file->lock, flags);421return;422}423424entry = kmalloc(sizeof *entry, GFP_ATOMIC);425if (!entry) {426spin_unlock_irqrestore(&file->async_file->lock, flags);427return;428}429430entry->desc.async.element = element;431entry->desc.async.event_type = event;432entry->counter = counter;433434list_add_tail(&entry->list, &file->async_file->event_list);435if (obj_list)436list_add_tail(&entry->obj_list, obj_list);437spin_unlock_irqrestore(&file->async_file->lock, flags);438439wake_up_interruptible(&file->async_file->poll_wait);440kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);441}442443void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)444{445struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,446struct ib_ucq_object, uobject);447448ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,449event->event, &uobj->async_list,450&uobj->async_events_reported);451}452453void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)454{455struct ib_uevent_object *uobj;456457uobj = container_of(event->element.qp->uobject,458struct ib_uevent_object, uobject);459460ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,461event->event, &uobj->event_list,462&uobj->events_reported);463}464465void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)466{467struct ib_uevent_object *uobj;468469uobj = container_of(event->element.srq->uobject,470struct ib_uevent_object, uobject);471472ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,473event->event, &uobj->event_list,474&uobj->events_reported);475}476477void ib_uverbs_event_handler(struct ib_event_handler *handler,478struct ib_event *event)479{480struct ib_uverbs_file *file =481container_of(handler, struct ib_uverbs_file, event_handler);482483ib_uverbs_async_handler(file, event->element.port_num, event->event,484NULL, NULL);485}486487struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,488int is_async)489{490struct ib_uverbs_event_file *ev_file;491struct file *filp;492493ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);494if (!ev_file)495return ERR_PTR(-ENOMEM);496497kref_init(&ev_file->ref);498spin_lock_init(&ev_file->lock);499INIT_LIST_HEAD(&ev_file->event_list);500init_waitqueue_head(&ev_file->poll_wait);501ev_file->uverbs_file = uverbs_file;502ev_file->async_queue = NULL;503ev_file->is_async = is_async;504ev_file->is_closed = 0;505506filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,507ev_file, O_RDONLY);508if (IS_ERR(filp))509kfree(ev_file);510511return filp;512}513514/*515* Look up a completion event file by FD. If lookup is successful,516* takes a ref to the event file struct that it returns; if517* unsuccessful, returns NULL.518*/519struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)520{521struct ib_uverbs_event_file *ev_file = NULL;522struct file *filp;523524filp = fget(fd);525if (!filp)526return NULL;527528if (filp->f_op != &uverbs_event_fops)529goto out;530531ev_file = filp->private_data;532if (ev_file->is_async) {533ev_file = NULL;534goto out;535}536537kref_get(&ev_file->ref);538539out:540fput(filp);541return ev_file;542}543544static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,545size_t count, loff_t *pos)546{547struct ib_uverbs_file *file = filp->private_data;548struct ib_uverbs_cmd_hdr hdr;549550if (count < sizeof hdr)551return -EINVAL;552553if (copy_from_user(&hdr, buf, sizeof hdr))554return -EFAULT;555556if (hdr.in_words * 4 != count)557return -EINVAL;558559if (hdr.command < 0 ||560hdr.command >= ARRAY_SIZE(uverbs_cmd_table) ||561!uverbs_cmd_table[hdr.command])562return -EINVAL;563564if (!file->ucontext &&565hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)566return -EINVAL;567568if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command)))569return -ENOSYS;570571return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,572hdr.in_words * 4, hdr.out_words * 4);573}574575static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)576{577struct ib_uverbs_file *file = filp->private_data;578579if (!file->ucontext)580return -ENODEV;581else582return file->device->ib_dev->mmap(file->ucontext, vma);583}584585/*586* ib_uverbs_open() does not need the BKL:587*588* - the ib_uverbs_device structures are properly reference counted and589* everything else is purely local to the file being created, so590* races against other open calls are not a problem;591* - there is no ioctl method to race against;592* - the open method will either immediately run -ENXIO, or all593* required initialization will be done.594*/595static int ib_uverbs_open(struct inode *inode, struct file *filp)596{597struct ib_uverbs_device *dev;598struct ib_uverbs_file *file;599int ret;600601dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);602if (dev)603kref_get(&dev->ref);604else605return -ENXIO;606607if (!try_module_get(dev->ib_dev->owner)) {608ret = -ENODEV;609goto err;610}611612file = kmalloc(sizeof *file, GFP_KERNEL);613if (!file) {614ret = -ENOMEM;615goto err_module;616}617618file->device = dev;619file->ucontext = NULL;620file->async_file = NULL;621kref_init(&file->ref);622mutex_init(&file->mutex);623624filp->private_data = file;625626return nonseekable_open(inode, filp);627628err_module:629module_put(dev->ib_dev->owner);630631err:632kref_put(&dev->ref, ib_uverbs_release_dev);633return ret;634}635636static int ib_uverbs_close(struct inode *inode, struct file *filp)637{638struct ib_uverbs_file *file = filp->private_data;639640ib_uverbs_cleanup_ucontext(file, file->ucontext);641642if (file->async_file)643kref_put(&file->async_file->ref, ib_uverbs_release_event_file);644645kref_put(&file->ref, ib_uverbs_release_file);646647return 0;648}649650static const struct file_operations uverbs_fops = {651.owner = THIS_MODULE,652.write = ib_uverbs_write,653.open = ib_uverbs_open,654.release = ib_uverbs_close,655.llseek = no_llseek,656};657658static const struct file_operations uverbs_mmap_fops = {659.owner = THIS_MODULE,660.write = ib_uverbs_write,661.mmap = ib_uverbs_mmap,662.open = ib_uverbs_open,663.release = ib_uverbs_close,664.llseek = no_llseek,665};666667static struct ib_client uverbs_client = {668.name = "uverbs",669.add = ib_uverbs_add_one,670.remove = ib_uverbs_remove_one671};672673static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,674char *buf)675{676struct ib_uverbs_device *dev = dev_get_drvdata(device);677678if (!dev)679return -ENODEV;680681return sprintf(buf, "%s\n", dev->ib_dev->name);682}683static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);684685static ssize_t show_dev_abi_version(struct device *device,686struct device_attribute *attr, char *buf)687{688struct ib_uverbs_device *dev = dev_get_drvdata(device);689690if (!dev)691return -ENODEV;692693return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);694}695static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);696697static CLASS_ATTR_STRING(abi_version, S_IRUGO,698__stringify(IB_USER_VERBS_ABI_VERSION));699700static dev_t overflow_maj;701static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);702703/*704* If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by705* requesting a new major number and doubling the number of max devices we706* support. It's stupid, but simple.707*/708static int find_overflow_devnum(void)709{710int ret;711712if (!overflow_maj) {713ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,714"infiniband_verbs");715if (ret) {716printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");717return ret;718}719}720721ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);722if (ret >= IB_UVERBS_MAX_DEVICES)723return -1;724725return ret;726}727728static void ib_uverbs_add_one(struct ib_device *device)729{730int devnum;731dev_t base;732struct ib_uverbs_device *uverbs_dev;733734if (!device->alloc_ucontext)735return;736737uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);738if (!uverbs_dev)739return;740741kref_init(&uverbs_dev->ref);742init_completion(&uverbs_dev->comp);743744spin_lock(&map_lock);745devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);746if (devnum >= IB_UVERBS_MAX_DEVICES) {747spin_unlock(&map_lock);748devnum = find_overflow_devnum();749if (devnum < 0)750goto err;751752spin_lock(&map_lock);753uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;754base = devnum + overflow_maj;755set_bit(devnum, overflow_map);756} else {757uverbs_dev->devnum = devnum;758base = devnum + IB_UVERBS_BASE_DEV;759set_bit(devnum, dev_map);760}761spin_unlock(&map_lock);762763uverbs_dev->ib_dev = device;764uverbs_dev->num_comp_vectors = device->num_comp_vectors;765766cdev_init(&uverbs_dev->cdev, NULL);767uverbs_dev->cdev.owner = THIS_MODULE;768uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;769kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);770if (cdev_add(&uverbs_dev->cdev, base, 1))771goto err_cdev;772773uverbs_dev->dev = device_create(uverbs_class, device->dma_device,774uverbs_dev->cdev.dev, uverbs_dev,775"uverbs%d", uverbs_dev->devnum);776if (IS_ERR(uverbs_dev->dev))777goto err_cdev;778779if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))780goto err_class;781if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))782goto err_class;783784ib_set_client_data(device, &uverbs_client, uverbs_dev);785786return;787788err_class:789device_destroy(uverbs_class, uverbs_dev->cdev.dev);790791err_cdev:792cdev_del(&uverbs_dev->cdev);793if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)794clear_bit(devnum, dev_map);795else796clear_bit(devnum, overflow_map);797798err:799kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);800wait_for_completion(&uverbs_dev->comp);801kfree(uverbs_dev);802return;803}804805static void ib_uverbs_remove_one(struct ib_device *device)806{807struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);808809if (!uverbs_dev)810return;811812dev_set_drvdata(uverbs_dev->dev, NULL);813device_destroy(uverbs_class, uverbs_dev->cdev.dev);814cdev_del(&uverbs_dev->cdev);815816if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)817clear_bit(uverbs_dev->devnum, dev_map);818else819clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);820821kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);822wait_for_completion(&uverbs_dev->comp);823kfree(uverbs_dev);824}825826static char *uverbs_devnode(struct device *dev, mode_t *mode)827{828if (mode)829*mode = 0666;830return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));831}832833static int __init ib_uverbs_init(void)834{835int ret;836837ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,838"infiniband_verbs");839if (ret) {840printk(KERN_ERR "user_verbs: couldn't register device number\n");841goto out;842}843844uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");845if (IS_ERR(uverbs_class)) {846ret = PTR_ERR(uverbs_class);847printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");848goto out_chrdev;849}850851uverbs_class->devnode = uverbs_devnode;852853ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);854if (ret) {855printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");856goto out_class;857}858859ret = ib_register_client(&uverbs_client);860if (ret) {861printk(KERN_ERR "user_verbs: couldn't register client\n");862goto out_class;863}864865return 0;866867out_class:868class_destroy(uverbs_class);869870out_chrdev:871unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);872873out:874return ret;875}876877static void __exit ib_uverbs_cleanup(void)878{879ib_unregister_client(&uverbs_client);880class_destroy(uverbs_class);881unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);882if (overflow_maj)883unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);884idr_destroy(&ib_uverbs_pd_idr);885idr_destroy(&ib_uverbs_mr_idr);886idr_destroy(&ib_uverbs_mw_idr);887idr_destroy(&ib_uverbs_ah_idr);888idr_destroy(&ib_uverbs_cq_idr);889idr_destroy(&ib_uverbs_qp_idr);890idr_destroy(&ib_uverbs_srq_idr);891}892893module_init(ib_uverbs_init);894module_exit(ib_uverbs_cleanup);895896897