Path: blob/master/drivers/infiniband/core/user_mad.c
37212 views
/*1* Copyright (c) 2004 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Voltaire, Inc. All rights reserved.3* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.4* Copyright (c) 2008 Cisco. All rights reserved.5*6* This software is available to you under a choice of one of two7* licenses. You may choose to be licensed under the terms of the GNU8* General Public License (GPL) Version 2, available from the file9* COPYING in the main directory of this source tree, or the10* OpenIB.org BSD license below:11*12* Redistribution and use in source and binary forms, with or13* without modification, are permitted provided that the following14* conditions are met:15*16* - Redistributions of source code must retain the above17* copyright notice, this list of conditions and the following18* disclaimer.19*20* - Redistributions in binary form must reproduce the above21* copyright notice, this list of conditions and the following22* disclaimer in the documentation and/or other materials23* provided with the distribution.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,26* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF27* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND28* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS29* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN30* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN31* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE32* SOFTWARE.33*/3435#include <linux/module.h>36#include <linux/init.h>37#include <linux/device.h>38#include <linux/err.h>39#include <linux/fs.h>40#include <linux/cdev.h>41#include <linux/dma-mapping.h>42#include <linux/poll.h>43#include <linux/mutex.h>44#include <linux/kref.h>45#include <linux/compat.h>46#include <linux/sched.h>47#include <linux/semaphore.h>48#include <linux/slab.h>4950#include <asm/uaccess.h>5152#include <rdma/ib_mad.h>53#include <rdma/ib_user_mad.h>5455MODULE_AUTHOR("Roland Dreier");56MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");57MODULE_LICENSE("Dual BSD/GPL");5859enum {60IB_UMAD_MAX_PORTS = 64,61IB_UMAD_MAX_AGENTS = 32,6263IB_UMAD_MAJOR = 231,64IB_UMAD_MINOR_BASE = 065};6667/*68* Our lifetime rules for these structs are the following:69* device special file is opened, we take a reference on the70* ib_umad_port's struct ib_umad_device. We drop these71* references in the corresponding close().72*73* In addition to references coming from open character devices, there74* is one more reference to each ib_umad_device representing the75* module's reference taken when allocating the ib_umad_device in76* ib_umad_add_one().77*78* When destroying an ib_umad_device, we drop the module's reference.79*/8081struct ib_umad_port {82struct cdev cdev;83struct device *dev;8485struct cdev sm_cdev;86struct device *sm_dev;87struct semaphore sm_sem;8889struct mutex file_mutex;90struct list_head file_list;9192struct ib_device *ib_dev;93struct ib_umad_device *umad_dev;94int dev_num;95u8 port_num;96};9798struct ib_umad_device {99int start_port, end_port;100struct kref ref;101struct ib_umad_port port[0];102};103104struct ib_umad_file {105struct mutex mutex;106struct ib_umad_port *port;107struct list_head recv_list;108struct list_head send_list;109struct list_head port_list;110spinlock_t send_lock;111wait_queue_head_t recv_wait;112struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];113int agents_dead;114u8 use_pkey_index;115u8 already_used;116};117118struct ib_umad_packet {119struct ib_mad_send_buf *msg;120struct ib_mad_recv_wc *recv_wc;121struct list_head list;122int length;123struct ib_user_mad mad;124};125126static struct class *umad_class;127128static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);129130static DEFINE_SPINLOCK(port_lock);131static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS);132133static void ib_umad_add_one(struct ib_device *device);134static void ib_umad_remove_one(struct ib_device *device);135136static void ib_umad_release_dev(struct kref *ref)137{138struct ib_umad_device *dev =139container_of(ref, struct ib_umad_device, ref);140141kfree(dev);142}143144static int hdr_size(struct ib_umad_file *file)145{146return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :147sizeof (struct ib_user_mad_hdr_old);148}149150/* caller must hold file->mutex */151static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)152{153return file->agents_dead ? NULL : file->agent[id];154}155156static int queue_packet(struct ib_umad_file *file,157struct ib_mad_agent *agent,158struct ib_umad_packet *packet)159{160int ret = 1;161162mutex_lock(&file->mutex);163164for (packet->mad.hdr.id = 0;165packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;166packet->mad.hdr.id++)167if (agent == __get_agent(file, packet->mad.hdr.id)) {168list_add_tail(&packet->list, &file->recv_list);169wake_up_interruptible(&file->recv_wait);170ret = 0;171break;172}173174mutex_unlock(&file->mutex);175176return ret;177}178179static void dequeue_send(struct ib_umad_file *file,180struct ib_umad_packet *packet)181{182spin_lock_irq(&file->send_lock);183list_del(&packet->list);184spin_unlock_irq(&file->send_lock);185}186187static void send_handler(struct ib_mad_agent *agent,188struct ib_mad_send_wc *send_wc)189{190struct ib_umad_file *file = agent->context;191struct ib_umad_packet *packet = send_wc->send_buf->context[0];192193dequeue_send(file, packet);194ib_destroy_ah(packet->msg->ah);195ib_free_send_mad(packet->msg);196197if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {198packet->length = IB_MGMT_MAD_HDR;199packet->mad.hdr.status = ETIMEDOUT;200if (!queue_packet(file, agent, packet))201return;202}203kfree(packet);204}205206static void recv_handler(struct ib_mad_agent *agent,207struct ib_mad_recv_wc *mad_recv_wc)208{209struct ib_umad_file *file = agent->context;210struct ib_umad_packet *packet;211212if (mad_recv_wc->wc->status != IB_WC_SUCCESS)213goto err1;214215packet = kzalloc(sizeof *packet, GFP_KERNEL);216if (!packet)217goto err1;218219packet->length = mad_recv_wc->mad_len;220packet->recv_wc = mad_recv_wc;221222packet->mad.hdr.status = 0;223packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;224packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);225packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);226packet->mad.hdr.sl = mad_recv_wc->wc->sl;227packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;228packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;229packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);230if (packet->mad.hdr.grh_present) {231struct ib_ah_attr ah_attr;232233ib_init_ah_from_wc(agent->device, agent->port_num,234mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,235&ah_attr);236237packet->mad.hdr.gid_index = ah_attr.grh.sgid_index;238packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit;239packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class;240memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16);241packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label);242}243244if (queue_packet(file, agent, packet))245goto err2;246return;247248err2:249kfree(packet);250err1:251ib_free_recv_mad(mad_recv_wc);252}253254static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,255struct ib_umad_packet *packet, size_t count)256{257struct ib_mad_recv_buf *recv_buf;258int left, seg_payload, offset, max_seg_payload;259260/* We need enough room to copy the first (or only) MAD segment. */261recv_buf = &packet->recv_wc->recv_buf;262if ((packet->length <= sizeof (*recv_buf->mad) &&263count < hdr_size(file) + packet->length) ||264(packet->length > sizeof (*recv_buf->mad) &&265count < hdr_size(file) + sizeof (*recv_buf->mad)))266return -EINVAL;267268if (copy_to_user(buf, &packet->mad, hdr_size(file)))269return -EFAULT;270271buf += hdr_size(file);272seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));273if (copy_to_user(buf, recv_buf->mad, seg_payload))274return -EFAULT;275276if (seg_payload < packet->length) {277/*278* Multipacket RMPP MAD message. Copy remainder of message.279* Note that last segment may have a shorter payload.280*/281if (count < hdr_size(file) + packet->length) {282/*283* The buffer is too small, return the first RMPP segment,284* which includes the RMPP message length.285*/286return -ENOSPC;287}288offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);289max_seg_payload = sizeof (struct ib_mad) - offset;290291for (left = packet->length - seg_payload, buf += seg_payload;292left; left -= seg_payload, buf += seg_payload) {293recv_buf = container_of(recv_buf->list.next,294struct ib_mad_recv_buf, list);295seg_payload = min(left, max_seg_payload);296if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,297seg_payload))298return -EFAULT;299}300}301return hdr_size(file) + packet->length;302}303304static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,305struct ib_umad_packet *packet, size_t count)306{307ssize_t size = hdr_size(file) + packet->length;308309if (count < size)310return -EINVAL;311312if (copy_to_user(buf, &packet->mad, hdr_size(file)))313return -EFAULT;314315buf += hdr_size(file);316317if (copy_to_user(buf, packet->mad.data, packet->length))318return -EFAULT;319320return size;321}322323static ssize_t ib_umad_read(struct file *filp, char __user *buf,324size_t count, loff_t *pos)325{326struct ib_umad_file *file = filp->private_data;327struct ib_umad_packet *packet;328ssize_t ret;329330if (count < hdr_size(file))331return -EINVAL;332333mutex_lock(&file->mutex);334335while (list_empty(&file->recv_list)) {336mutex_unlock(&file->mutex);337338if (filp->f_flags & O_NONBLOCK)339return -EAGAIN;340341if (wait_event_interruptible(file->recv_wait,342!list_empty(&file->recv_list)))343return -ERESTARTSYS;344345mutex_lock(&file->mutex);346}347348packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);349list_del(&packet->list);350351mutex_unlock(&file->mutex);352353if (packet->recv_wc)354ret = copy_recv_mad(file, buf, packet, count);355else356ret = copy_send_mad(file, buf, packet, count);357358if (ret < 0) {359/* Requeue packet */360mutex_lock(&file->mutex);361list_add(&packet->list, &file->recv_list);362mutex_unlock(&file->mutex);363} else {364if (packet->recv_wc)365ib_free_recv_mad(packet->recv_wc);366kfree(packet);367}368return ret;369}370371static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)372{373int left, seg;374375/* Copy class specific header */376if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&377copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,378msg->hdr_len - IB_MGMT_RMPP_HDR))379return -EFAULT;380381/* All headers are in place. Copy data segments. */382for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;383seg++, left -= msg->seg_size, buf += msg->seg_size) {384if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,385min(left, msg->seg_size)))386return -EFAULT;387}388return 0;389}390391static int same_destination(struct ib_user_mad_hdr *hdr1,392struct ib_user_mad_hdr *hdr2)393{394if (!hdr1->grh_present && !hdr2->grh_present)395return (hdr1->lid == hdr2->lid);396397if (hdr1->grh_present && hdr2->grh_present)398return !memcmp(hdr1->gid, hdr2->gid, 16);399400return 0;401}402403static int is_duplicate(struct ib_umad_file *file,404struct ib_umad_packet *packet)405{406struct ib_umad_packet *sent_packet;407struct ib_mad_hdr *sent_hdr, *hdr;408409hdr = (struct ib_mad_hdr *) packet->mad.data;410list_for_each_entry(sent_packet, &file->send_list, list) {411sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;412413if ((hdr->tid != sent_hdr->tid) ||414(hdr->mgmt_class != sent_hdr->mgmt_class))415continue;416417/*418* No need to be overly clever here. If two new operations have419* the same TID, reject the second as a duplicate. This is more420* restrictive than required by the spec.421*/422if (!ib_response_mad((struct ib_mad *) hdr)) {423if (!ib_response_mad((struct ib_mad *) sent_hdr))424return 1;425continue;426} else if (!ib_response_mad((struct ib_mad *) sent_hdr))427continue;428429if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))430return 1;431}432433return 0;434}435436static ssize_t ib_umad_write(struct file *filp, const char __user *buf,437size_t count, loff_t *pos)438{439struct ib_umad_file *file = filp->private_data;440struct ib_umad_packet *packet;441struct ib_mad_agent *agent;442struct ib_ah_attr ah_attr;443struct ib_ah *ah;444struct ib_rmpp_mad *rmpp_mad;445__be64 *tid;446int ret, data_len, hdr_len, copy_offset, rmpp_active;447448if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)449return -EINVAL;450451packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);452if (!packet)453return -ENOMEM;454455if (copy_from_user(&packet->mad, buf, hdr_size(file))) {456ret = -EFAULT;457goto err;458}459460if (packet->mad.hdr.id < 0 ||461packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {462ret = -EINVAL;463goto err;464}465466buf += hdr_size(file);467468if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {469ret = -EFAULT;470goto err;471}472473mutex_lock(&file->mutex);474475agent = __get_agent(file, packet->mad.hdr.id);476if (!agent) {477ret = -EINVAL;478goto err_up;479}480481memset(&ah_attr, 0, sizeof ah_attr);482ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);483ah_attr.sl = packet->mad.hdr.sl;484ah_attr.src_path_bits = packet->mad.hdr.path_bits;485ah_attr.port_num = file->port->port_num;486if (packet->mad.hdr.grh_present) {487ah_attr.ah_flags = IB_AH_GRH;488memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);489ah_attr.grh.sgid_index = packet->mad.hdr.gid_index;490ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);491ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;492ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;493}494495ah = ib_create_ah(agent->qp->pd, &ah_attr);496if (IS_ERR(ah)) {497ret = PTR_ERR(ah);498goto err_up;499}500501rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;502hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);503if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) {504copy_offset = IB_MGMT_MAD_HDR;505rmpp_active = 0;506} else {507copy_offset = IB_MGMT_RMPP_HDR;508rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &509IB_MGMT_RMPP_FLAG_ACTIVE;510}511512data_len = count - hdr_size(file) - hdr_len;513packet->msg = ib_create_send_mad(agent,514be32_to_cpu(packet->mad.hdr.qpn),515packet->mad.hdr.pkey_index, rmpp_active,516hdr_len, data_len, GFP_KERNEL);517if (IS_ERR(packet->msg)) {518ret = PTR_ERR(packet->msg);519goto err_ah;520}521522packet->msg->ah = ah;523packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;524packet->msg->retries = packet->mad.hdr.retries;525packet->msg->context[0] = packet;526527/* Copy MAD header. Any RMPP header is already in place. */528memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);529530if (!rmpp_active) {531if (copy_from_user(packet->msg->mad + copy_offset,532buf + copy_offset,533hdr_len + data_len - copy_offset)) {534ret = -EFAULT;535goto err_msg;536}537} else {538ret = copy_rmpp_mad(packet->msg, buf);539if (ret)540goto err_msg;541}542543/*544* Set the high-order part of the transaction ID to make MADs from545* different agents unique, and allow routing responses back to the546* original requestor.547*/548if (!ib_response_mad(packet->msg->mad)) {549tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;550*tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |551(be64_to_cpup(tid) & 0xffffffff));552rmpp_mad->mad_hdr.tid = *tid;553}554555spin_lock_irq(&file->send_lock);556ret = is_duplicate(file, packet);557if (!ret)558list_add_tail(&packet->list, &file->send_list);559spin_unlock_irq(&file->send_lock);560if (ret) {561ret = -EINVAL;562goto err_msg;563}564565ret = ib_post_send_mad(packet->msg, NULL);566if (ret)567goto err_send;568569mutex_unlock(&file->mutex);570return count;571572err_send:573dequeue_send(file, packet);574err_msg:575ib_free_send_mad(packet->msg);576err_ah:577ib_destroy_ah(ah);578err_up:579mutex_unlock(&file->mutex);580err:581kfree(packet);582return ret;583}584585static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)586{587struct ib_umad_file *file = filp->private_data;588589/* we will always be able to post a MAD send */590unsigned int mask = POLLOUT | POLLWRNORM;591592poll_wait(filp, &file->recv_wait, wait);593594if (!list_empty(&file->recv_list))595mask |= POLLIN | POLLRDNORM;596597return mask;598}599600static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,601int compat_method_mask)602{603struct ib_user_mad_reg_req ureq;604struct ib_mad_reg_req req;605struct ib_mad_agent *agent = NULL;606int agent_id;607int ret;608609mutex_lock(&file->port->file_mutex);610mutex_lock(&file->mutex);611612if (!file->port->ib_dev) {613ret = -EPIPE;614goto out;615}616617if (copy_from_user(&ureq, arg, sizeof ureq)) {618ret = -EFAULT;619goto out;620}621622if (ureq.qpn != 0 && ureq.qpn != 1) {623ret = -EINVAL;624goto out;625}626627for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)628if (!__get_agent(file, agent_id))629goto found;630631ret = -ENOMEM;632goto out;633634found:635if (ureq.mgmt_class) {636req.mgmt_class = ureq.mgmt_class;637req.mgmt_class_version = ureq.mgmt_class_version;638memcpy(req.oui, ureq.oui, sizeof req.oui);639640if (compat_method_mask) {641u32 *umm = (u32 *) ureq.method_mask;642int i;643644for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)645req.method_mask[i] =646umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);647} else648memcpy(req.method_mask, ureq.method_mask,649sizeof req.method_mask);650}651652agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,653ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,654ureq.mgmt_class ? &req : NULL,655ureq.rmpp_version,656send_handler, recv_handler, file);657if (IS_ERR(agent)) {658ret = PTR_ERR(agent);659agent = NULL;660goto out;661}662663if (put_user(agent_id,664(u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {665ret = -EFAULT;666goto out;667}668669if (!file->already_used) {670file->already_used = 1;671if (!file->use_pkey_index) {672printk(KERN_WARNING "user_mad: process %s did not enable "673"P_Key index support.\n", current->comm);674printk(KERN_WARNING "user_mad: Documentation/infiniband/user_mad.txt "675"has info on the new ABI.\n");676}677}678679file->agent[agent_id] = agent;680ret = 0;681682out:683mutex_unlock(&file->mutex);684685if (ret && agent)686ib_unregister_mad_agent(agent);687688mutex_unlock(&file->port->file_mutex);689690return ret;691}692693static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)694{695struct ib_mad_agent *agent = NULL;696u32 id;697int ret = 0;698699if (get_user(id, arg))700return -EFAULT;701702mutex_lock(&file->port->file_mutex);703mutex_lock(&file->mutex);704705if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {706ret = -EINVAL;707goto out;708}709710agent = file->agent[id];711file->agent[id] = NULL;712713out:714mutex_unlock(&file->mutex);715716if (agent)717ib_unregister_mad_agent(agent);718719mutex_unlock(&file->port->file_mutex);720721return ret;722}723724static long ib_umad_enable_pkey(struct ib_umad_file *file)725{726int ret = 0;727728mutex_lock(&file->mutex);729if (file->already_used)730ret = -EINVAL;731else732file->use_pkey_index = 1;733mutex_unlock(&file->mutex);734735return ret;736}737738static long ib_umad_ioctl(struct file *filp, unsigned int cmd,739unsigned long arg)740{741switch (cmd) {742case IB_USER_MAD_REGISTER_AGENT:743return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);744case IB_USER_MAD_UNREGISTER_AGENT:745return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);746case IB_USER_MAD_ENABLE_PKEY:747return ib_umad_enable_pkey(filp->private_data);748default:749return -ENOIOCTLCMD;750}751}752753#ifdef CONFIG_COMPAT754static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,755unsigned long arg)756{757switch (cmd) {758case IB_USER_MAD_REGISTER_AGENT:759return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);760case IB_USER_MAD_UNREGISTER_AGENT:761return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));762case IB_USER_MAD_ENABLE_PKEY:763return ib_umad_enable_pkey(filp->private_data);764default:765return -ENOIOCTLCMD;766}767}768#endif769770/*771* ib_umad_open() does not need the BKL:772*773* - the ib_umad_port structures are properly reference counted, and774* everything else is purely local to the file being created, so775* races against other open calls are not a problem;776* - the ioctl method does not affect any global state outside of the777* file structure being operated on;778*/779static int ib_umad_open(struct inode *inode, struct file *filp)780{781struct ib_umad_port *port;782struct ib_umad_file *file;783int ret;784785port = container_of(inode->i_cdev, struct ib_umad_port, cdev);786if (port)787kref_get(&port->umad_dev->ref);788else789return -ENXIO;790791mutex_lock(&port->file_mutex);792793if (!port->ib_dev) {794ret = -ENXIO;795goto out;796}797798file = kzalloc(sizeof *file, GFP_KERNEL);799if (!file) {800kref_put(&port->umad_dev->ref, ib_umad_release_dev);801ret = -ENOMEM;802goto out;803}804805mutex_init(&file->mutex);806spin_lock_init(&file->send_lock);807INIT_LIST_HEAD(&file->recv_list);808INIT_LIST_HEAD(&file->send_list);809init_waitqueue_head(&file->recv_wait);810811file->port = port;812filp->private_data = file;813814list_add_tail(&file->port_list, &port->file_list);815816ret = nonseekable_open(inode, filp);817818out:819mutex_unlock(&port->file_mutex);820return ret;821}822823static int ib_umad_close(struct inode *inode, struct file *filp)824{825struct ib_umad_file *file = filp->private_data;826struct ib_umad_device *dev = file->port->umad_dev;827struct ib_umad_packet *packet, *tmp;828int already_dead;829int i;830831mutex_lock(&file->port->file_mutex);832mutex_lock(&file->mutex);833834already_dead = file->agents_dead;835file->agents_dead = 1;836837list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {838if (packet->recv_wc)839ib_free_recv_mad(packet->recv_wc);840kfree(packet);841}842843list_del(&file->port_list);844845mutex_unlock(&file->mutex);846847if (!already_dead)848for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)849if (file->agent[i])850ib_unregister_mad_agent(file->agent[i]);851852mutex_unlock(&file->port->file_mutex);853854kfree(file);855kref_put(&dev->ref, ib_umad_release_dev);856857return 0;858}859860static const struct file_operations umad_fops = {861.owner = THIS_MODULE,862.read = ib_umad_read,863.write = ib_umad_write,864.poll = ib_umad_poll,865.unlocked_ioctl = ib_umad_ioctl,866#ifdef CONFIG_COMPAT867.compat_ioctl = ib_umad_compat_ioctl,868#endif869.open = ib_umad_open,870.release = ib_umad_close,871.llseek = no_llseek,872};873874static int ib_umad_sm_open(struct inode *inode, struct file *filp)875{876struct ib_umad_port *port;877struct ib_port_modify props = {878.set_port_cap_mask = IB_PORT_SM879};880int ret;881882port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);883if (port)884kref_get(&port->umad_dev->ref);885else886return -ENXIO;887888if (filp->f_flags & O_NONBLOCK) {889if (down_trylock(&port->sm_sem)) {890ret = -EAGAIN;891goto fail;892}893} else {894if (down_interruptible(&port->sm_sem)) {895ret = -ERESTARTSYS;896goto fail;897}898}899900ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);901if (ret) {902up(&port->sm_sem);903goto fail;904}905906filp->private_data = port;907908return nonseekable_open(inode, filp);909910fail:911kref_put(&port->umad_dev->ref, ib_umad_release_dev);912return ret;913}914915static int ib_umad_sm_close(struct inode *inode, struct file *filp)916{917struct ib_umad_port *port = filp->private_data;918struct ib_port_modify props = {919.clr_port_cap_mask = IB_PORT_SM920};921int ret = 0;922923mutex_lock(&port->file_mutex);924if (port->ib_dev)925ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);926mutex_unlock(&port->file_mutex);927928up(&port->sm_sem);929930kref_put(&port->umad_dev->ref, ib_umad_release_dev);931932return ret;933}934935static const struct file_operations umad_sm_fops = {936.owner = THIS_MODULE,937.open = ib_umad_sm_open,938.release = ib_umad_sm_close,939.llseek = no_llseek,940};941942static struct ib_client umad_client = {943.name = "umad",944.add = ib_umad_add_one,945.remove = ib_umad_remove_one946};947948static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,949char *buf)950{951struct ib_umad_port *port = dev_get_drvdata(dev);952953if (!port)954return -ENODEV;955956return sprintf(buf, "%s\n", port->ib_dev->name);957}958static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);959960static ssize_t show_port(struct device *dev, struct device_attribute *attr,961char *buf)962{963struct ib_umad_port *port = dev_get_drvdata(dev);964965if (!port)966return -ENODEV;967968return sprintf(buf, "%d\n", port->port_num);969}970static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);971972static CLASS_ATTR_STRING(abi_version, S_IRUGO,973__stringify(IB_USER_MAD_ABI_VERSION));974975static dev_t overflow_maj;976static DECLARE_BITMAP(overflow_map, IB_UMAD_MAX_PORTS);977static int find_overflow_devnum(void)978{979int ret;980981if (!overflow_maj) {982ret = alloc_chrdev_region(&overflow_maj, 0, IB_UMAD_MAX_PORTS * 2,983"infiniband_mad");984if (ret) {985printk(KERN_ERR "user_mad: couldn't register dynamic device number\n");986return ret;987}988}989990ret = find_first_zero_bit(overflow_map, IB_UMAD_MAX_PORTS);991if (ret >= IB_UMAD_MAX_PORTS)992return -1;993994return ret;995}996997static int ib_umad_init_port(struct ib_device *device, int port_num,998struct ib_umad_port *port)999{1000int devnum;1001dev_t base;10021003spin_lock(&port_lock);1004devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);1005if (devnum >= IB_UMAD_MAX_PORTS) {1006spin_unlock(&port_lock);1007devnum = find_overflow_devnum();1008if (devnum < 0)1009return -1;10101011spin_lock(&port_lock);1012port->dev_num = devnum + IB_UMAD_MAX_PORTS;1013base = devnum + overflow_maj;1014set_bit(devnum, overflow_map);1015} else {1016port->dev_num = devnum;1017base = devnum + base_dev;1018set_bit(devnum, dev_map);1019}1020spin_unlock(&port_lock);10211022port->ib_dev = device;1023port->port_num = port_num;1024sema_init(&port->sm_sem, 1);1025mutex_init(&port->file_mutex);1026INIT_LIST_HEAD(&port->file_list);10271028cdev_init(&port->cdev, &umad_fops);1029port->cdev.owner = THIS_MODULE;1030kobject_set_name(&port->cdev.kobj, "umad%d", port->dev_num);1031if (cdev_add(&port->cdev, base, 1))1032goto err_cdev;10331034port->dev = device_create(umad_class, device->dma_device,1035port->cdev.dev, port,1036"umad%d", port->dev_num);1037if (IS_ERR(port->dev))1038goto err_cdev;10391040if (device_create_file(port->dev, &dev_attr_ibdev))1041goto err_dev;1042if (device_create_file(port->dev, &dev_attr_port))1043goto err_dev;10441045base += IB_UMAD_MAX_PORTS;1046cdev_init(&port->sm_cdev, &umad_sm_fops);1047port->sm_cdev.owner = THIS_MODULE;1048kobject_set_name(&port->sm_cdev.kobj, "issm%d", port->dev_num);1049if (cdev_add(&port->sm_cdev, base, 1))1050goto err_sm_cdev;10511052port->sm_dev = device_create(umad_class, device->dma_device,1053port->sm_cdev.dev, port,1054"issm%d", port->dev_num);1055if (IS_ERR(port->sm_dev))1056goto err_sm_cdev;10571058if (device_create_file(port->sm_dev, &dev_attr_ibdev))1059goto err_sm_dev;1060if (device_create_file(port->sm_dev, &dev_attr_port))1061goto err_sm_dev;10621063return 0;10641065err_sm_dev:1066device_destroy(umad_class, port->sm_cdev.dev);10671068err_sm_cdev:1069cdev_del(&port->sm_cdev);10701071err_dev:1072device_destroy(umad_class, port->cdev.dev);10731074err_cdev:1075cdev_del(&port->cdev);1076if (port->dev_num < IB_UMAD_MAX_PORTS)1077clear_bit(devnum, dev_map);1078else1079clear_bit(devnum, overflow_map);10801081return -1;1082}10831084static void ib_umad_kill_port(struct ib_umad_port *port)1085{1086struct ib_umad_file *file;1087int id;10881089dev_set_drvdata(port->dev, NULL);1090dev_set_drvdata(port->sm_dev, NULL);10911092device_destroy(umad_class, port->cdev.dev);1093device_destroy(umad_class, port->sm_cdev.dev);10941095cdev_del(&port->cdev);1096cdev_del(&port->sm_cdev);10971098mutex_lock(&port->file_mutex);10991100port->ib_dev = NULL;11011102list_for_each_entry(file, &port->file_list, port_list) {1103mutex_lock(&file->mutex);1104file->agents_dead = 1;1105mutex_unlock(&file->mutex);11061107for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)1108if (file->agent[id])1109ib_unregister_mad_agent(file->agent[id]);1110}11111112mutex_unlock(&port->file_mutex);11131114if (port->dev_num < IB_UMAD_MAX_PORTS)1115clear_bit(port->dev_num, dev_map);1116else1117clear_bit(port->dev_num - IB_UMAD_MAX_PORTS, overflow_map);1118}11191120static void ib_umad_add_one(struct ib_device *device)1121{1122struct ib_umad_device *umad_dev;1123int s, e, i;11241125if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)1126return;11271128if (device->node_type == RDMA_NODE_IB_SWITCH)1129s = e = 0;1130else {1131s = 1;1132e = device->phys_port_cnt;1133}11341135umad_dev = kzalloc(sizeof *umad_dev +1136(e - s + 1) * sizeof (struct ib_umad_port),1137GFP_KERNEL);1138if (!umad_dev)1139return;11401141kref_init(&umad_dev->ref);11421143umad_dev->start_port = s;1144umad_dev->end_port = e;11451146for (i = s; i <= e; ++i) {1147umad_dev->port[i - s].umad_dev = umad_dev;11481149if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))1150goto err;1151}11521153ib_set_client_data(device, &umad_client, umad_dev);11541155return;11561157err:1158while (--i >= s)1159ib_umad_kill_port(&umad_dev->port[i - s]);11601161kref_put(&umad_dev->ref, ib_umad_release_dev);1162}11631164static void ib_umad_remove_one(struct ib_device *device)1165{1166struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);1167int i;11681169if (!umad_dev)1170return;11711172for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)1173ib_umad_kill_port(&umad_dev->port[i]);11741175kref_put(&umad_dev->ref, ib_umad_release_dev);1176}11771178static char *umad_devnode(struct device *dev, mode_t *mode)1179{1180return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));1181}11821183static int __init ib_umad_init(void)1184{1185int ret;11861187ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,1188"infiniband_mad");1189if (ret) {1190printk(KERN_ERR "user_mad: couldn't register device number\n");1191goto out;1192}11931194umad_class = class_create(THIS_MODULE, "infiniband_mad");1195if (IS_ERR(umad_class)) {1196ret = PTR_ERR(umad_class);1197printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");1198goto out_chrdev;1199}12001201umad_class->devnode = umad_devnode;12021203ret = class_create_file(umad_class, &class_attr_abi_version.attr);1204if (ret) {1205printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");1206goto out_class;1207}12081209ret = ib_register_client(&umad_client);1210if (ret) {1211printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");1212goto out_class;1213}12141215return 0;12161217out_class:1218class_destroy(umad_class);12191220out_chrdev:1221unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);12221223out:1224return ret;1225}12261227static void __exit ib_umad_cleanup(void)1228{1229ib_unregister_client(&umad_client);1230class_destroy(umad_class);1231unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);1232if (overflow_maj)1233unregister_chrdev_region(overflow_maj, IB_UMAD_MAX_PORTS * 2);1234}12351236module_init(ib_umad_init);1237module_exit(ib_umad_cleanup);123812391240