Path: blob/master/drivers/infiniband/hw/ipath/ipath_srq.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.2* Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/err.h>34#include <linux/slab.h>35#include <linux/vmalloc.h>3637#include "ipath_verbs.h"3839/**40* ipath_post_srq_receive - post a receive on a shared receive queue41* @ibsrq: the SRQ to post the receive on42* @wr: the list of work requests to post43* @bad_wr: the first WR to cause a problem is put here44*45* This may be called from interrupt context.46*/47int ipath_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,48struct ib_recv_wr **bad_wr)49{50struct ipath_srq *srq = to_isrq(ibsrq);51struct ipath_rwq *wq;52unsigned long flags;53int ret;5455for (; wr; wr = wr->next) {56struct ipath_rwqe *wqe;57u32 next;58int i;5960if ((unsigned) wr->num_sge > srq->rq.max_sge) {61*bad_wr = wr;62ret = -EINVAL;63goto bail;64}6566spin_lock_irqsave(&srq->rq.lock, flags);67wq = srq->rq.wq;68next = wq->head + 1;69if (next >= srq->rq.size)70next = 0;71if (next == wq->tail) {72spin_unlock_irqrestore(&srq->rq.lock, flags);73*bad_wr = wr;74ret = -ENOMEM;75goto bail;76}7778wqe = get_rwqe_ptr(&srq->rq, wq->head);79wqe->wr_id = wr->wr_id;80wqe->num_sge = wr->num_sge;81for (i = 0; i < wr->num_sge; i++)82wqe->sg_list[i] = wr->sg_list[i];83/* Make sure queue entry is written before the head index. */84smp_wmb();85wq->head = next;86spin_unlock_irqrestore(&srq->rq.lock, flags);87}88ret = 0;8990bail:91return ret;92}9394/**95* ipath_create_srq - create a shared receive queue96* @ibpd: the protection domain of the SRQ to create97* @srq_init_attr: the attributes of the SRQ98* @udata: data from libipathverbs when creating a user SRQ99*/100struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,101struct ib_srq_init_attr *srq_init_attr,102struct ib_udata *udata)103{104struct ipath_ibdev *dev = to_idev(ibpd->device);105struct ipath_srq *srq;106u32 sz;107struct ib_srq *ret;108109if (srq_init_attr->attr.max_wr == 0) {110ret = ERR_PTR(-EINVAL);111goto done;112}113114if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||115(srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {116ret = ERR_PTR(-EINVAL);117goto done;118}119120srq = kmalloc(sizeof(*srq), GFP_KERNEL);121if (!srq) {122ret = ERR_PTR(-ENOMEM);123goto done;124}125126/*127* Need to use vmalloc() if we want to support large #s of entries.128*/129srq->rq.size = srq_init_attr->attr.max_wr + 1;130srq->rq.max_sge = srq_init_attr->attr.max_sge;131sz = sizeof(struct ib_sge) * srq->rq.max_sge +132sizeof(struct ipath_rwqe);133srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);134if (!srq->rq.wq) {135ret = ERR_PTR(-ENOMEM);136goto bail_srq;137}138139/*140* Return the address of the RWQ as the offset to mmap.141* See ipath_mmap() for details.142*/143if (udata && udata->outlen >= sizeof(__u64)) {144int err;145u32 s = sizeof(struct ipath_rwq) + srq->rq.size * sz;146147srq->ip =148ipath_create_mmap_info(dev, s,149ibpd->uobject->context,150srq->rq.wq);151if (!srq->ip) {152ret = ERR_PTR(-ENOMEM);153goto bail_wq;154}155156err = ib_copy_to_udata(udata, &srq->ip->offset,157sizeof(srq->ip->offset));158if (err) {159ret = ERR_PTR(err);160goto bail_ip;161}162} else163srq->ip = NULL;164165/*166* ib_create_srq() will initialize srq->ibsrq.167*/168spin_lock_init(&srq->rq.lock);169srq->rq.wq->head = 0;170srq->rq.wq->tail = 0;171srq->limit = srq_init_attr->attr.srq_limit;172173spin_lock(&dev->n_srqs_lock);174if (dev->n_srqs_allocated == ib_ipath_max_srqs) {175spin_unlock(&dev->n_srqs_lock);176ret = ERR_PTR(-ENOMEM);177goto bail_ip;178}179180dev->n_srqs_allocated++;181spin_unlock(&dev->n_srqs_lock);182183if (srq->ip) {184spin_lock_irq(&dev->pending_lock);185list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);186spin_unlock_irq(&dev->pending_lock);187}188189ret = &srq->ibsrq;190goto done;191192bail_ip:193kfree(srq->ip);194bail_wq:195vfree(srq->rq.wq);196bail_srq:197kfree(srq);198done:199return ret;200}201202/**203* ipath_modify_srq - modify a shared receive queue204* @ibsrq: the SRQ to modify205* @attr: the new attributes of the SRQ206* @attr_mask: indicates which attributes to modify207* @udata: user data for ipathverbs.so208*/209int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,210enum ib_srq_attr_mask attr_mask,211struct ib_udata *udata)212{213struct ipath_srq *srq = to_isrq(ibsrq);214struct ipath_rwq *wq;215int ret = 0;216217if (attr_mask & IB_SRQ_MAX_WR) {218struct ipath_rwq *owq;219struct ipath_rwqe *p;220u32 sz, size, n, head, tail;221222/* Check that the requested sizes are below the limits. */223if ((attr->max_wr > ib_ipath_max_srq_wrs) ||224((attr_mask & IB_SRQ_LIMIT) ?225attr->srq_limit : srq->limit) > attr->max_wr) {226ret = -EINVAL;227goto bail;228}229230sz = sizeof(struct ipath_rwqe) +231srq->rq.max_sge * sizeof(struct ib_sge);232size = attr->max_wr + 1;233wq = vmalloc_user(sizeof(struct ipath_rwq) + size * sz);234if (!wq) {235ret = -ENOMEM;236goto bail;237}238239/* Check that we can write the offset to mmap. */240if (udata && udata->inlen >= sizeof(__u64)) {241__u64 offset_addr;242__u64 offset = 0;243244ret = ib_copy_from_udata(&offset_addr, udata,245sizeof(offset_addr));246if (ret)247goto bail_free;248udata->outbuf =249(void __user *) (unsigned long) offset_addr;250ret = ib_copy_to_udata(udata, &offset,251sizeof(offset));252if (ret)253goto bail_free;254}255256spin_lock_irq(&srq->rq.lock);257/*258* validate head pointer value and compute259* the number of remaining WQEs.260*/261owq = srq->rq.wq;262head = owq->head;263if (head >= srq->rq.size)264head = 0;265tail = owq->tail;266if (tail >= srq->rq.size)267tail = 0;268n = head;269if (n < tail)270n += srq->rq.size - tail;271else272n -= tail;273if (size <= n) {274ret = -EINVAL;275goto bail_unlock;276}277n = 0;278p = wq->wq;279while (tail != head) {280struct ipath_rwqe *wqe;281int i;282283wqe = get_rwqe_ptr(&srq->rq, tail);284p->wr_id = wqe->wr_id;285p->num_sge = wqe->num_sge;286for (i = 0; i < wqe->num_sge; i++)287p->sg_list[i] = wqe->sg_list[i];288n++;289p = (struct ipath_rwqe *)((char *) p + sz);290if (++tail >= srq->rq.size)291tail = 0;292}293srq->rq.wq = wq;294srq->rq.size = size;295wq->head = n;296wq->tail = 0;297if (attr_mask & IB_SRQ_LIMIT)298srq->limit = attr->srq_limit;299spin_unlock_irq(&srq->rq.lock);300301vfree(owq);302303if (srq->ip) {304struct ipath_mmap_info *ip = srq->ip;305struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);306u32 s = sizeof(struct ipath_rwq) + size * sz;307308ipath_update_mmap_info(dev, ip, s, wq);309310/*311* Return the offset to mmap.312* See ipath_mmap() for details.313*/314if (udata && udata->inlen >= sizeof(__u64)) {315ret = ib_copy_to_udata(udata, &ip->offset,316sizeof(ip->offset));317if (ret)318goto bail;319}320321spin_lock_irq(&dev->pending_lock);322if (list_empty(&ip->pending_mmaps))323list_add(&ip->pending_mmaps,324&dev->pending_mmaps);325spin_unlock_irq(&dev->pending_lock);326}327} else if (attr_mask & IB_SRQ_LIMIT) {328spin_lock_irq(&srq->rq.lock);329if (attr->srq_limit >= srq->rq.size)330ret = -EINVAL;331else332srq->limit = attr->srq_limit;333spin_unlock_irq(&srq->rq.lock);334}335goto bail;336337bail_unlock:338spin_unlock_irq(&srq->rq.lock);339bail_free:340vfree(wq);341bail:342return ret;343}344345int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)346{347struct ipath_srq *srq = to_isrq(ibsrq);348349attr->max_wr = srq->rq.size - 1;350attr->max_sge = srq->rq.max_sge;351attr->srq_limit = srq->limit;352return 0;353}354355/**356* ipath_destroy_srq - destroy a shared receive queue357* @ibsrq: the SRQ to destroy358*/359int ipath_destroy_srq(struct ib_srq *ibsrq)360{361struct ipath_srq *srq = to_isrq(ibsrq);362struct ipath_ibdev *dev = to_idev(ibsrq->device);363364spin_lock(&dev->n_srqs_lock);365dev->n_srqs_allocated--;366spin_unlock(&dev->n_srqs_lock);367if (srq->ip)368kref_put(&srq->ip->ref, ipath_release_mmap_info);369else370vfree(srq->rq.wq);371kfree(srq);372373return 0;374}375376377