Path: blob/master/drivers/infiniband/hw/qib/qib_srq.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 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 "qib_verbs.h"3839/**40* qib_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: A pointer to the first WR to cause a problem is put here44*45* This may be called from interrupt context.46*/47int qib_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,48struct ib_recv_wr **bad_wr)49{50struct qib_srq *srq = to_isrq(ibsrq);51struct qib_rwq *wq;52unsigned long flags;53int ret;5455for (; wr; wr = wr->next) {56struct qib_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* qib_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 libibverbs when creating a user SRQ99*/100struct ib_srq *qib_create_srq(struct ib_pd *ibpd,101struct ib_srq_init_attr *srq_init_attr,102struct ib_udata *udata)103{104struct qib_ibdev *dev = to_idev(ibpd->device);105struct qib_srq *srq;106u32 sz;107struct ib_srq *ret;108109if (srq_init_attr->attr.max_sge == 0 ||110srq_init_attr->attr.max_sge > ib_qib_max_srq_sges ||111srq_init_attr->attr.max_wr == 0 ||112srq_init_attr->attr.max_wr > ib_qib_max_srq_wrs) {113ret = ERR_PTR(-EINVAL);114goto done;115}116117srq = kmalloc(sizeof(*srq), GFP_KERNEL);118if (!srq) {119ret = ERR_PTR(-ENOMEM);120goto done;121}122123/*124* Need to use vmalloc() if we want to support large #s of entries.125*/126srq->rq.size = srq_init_attr->attr.max_wr + 1;127srq->rq.max_sge = srq_init_attr->attr.max_sge;128sz = sizeof(struct ib_sge) * srq->rq.max_sge +129sizeof(struct qib_rwqe);130srq->rq.wq = vmalloc_user(sizeof(struct qib_rwq) + srq->rq.size * sz);131if (!srq->rq.wq) {132ret = ERR_PTR(-ENOMEM);133goto bail_srq;134}135136/*137* Return the address of the RWQ as the offset to mmap.138* See qib_mmap() for details.139*/140if (udata && udata->outlen >= sizeof(__u64)) {141int err;142u32 s = sizeof(struct qib_rwq) + srq->rq.size * sz;143144srq->ip =145qib_create_mmap_info(dev, s, ibpd->uobject->context,146srq->rq.wq);147if (!srq->ip) {148ret = ERR_PTR(-ENOMEM);149goto bail_wq;150}151152err = ib_copy_to_udata(udata, &srq->ip->offset,153sizeof(srq->ip->offset));154if (err) {155ret = ERR_PTR(err);156goto bail_ip;157}158} else159srq->ip = NULL;160161/*162* ib_create_srq() will initialize srq->ibsrq.163*/164spin_lock_init(&srq->rq.lock);165srq->rq.wq->head = 0;166srq->rq.wq->tail = 0;167srq->limit = srq_init_attr->attr.srq_limit;168169spin_lock(&dev->n_srqs_lock);170if (dev->n_srqs_allocated == ib_qib_max_srqs) {171spin_unlock(&dev->n_srqs_lock);172ret = ERR_PTR(-ENOMEM);173goto bail_ip;174}175176dev->n_srqs_allocated++;177spin_unlock(&dev->n_srqs_lock);178179if (srq->ip) {180spin_lock_irq(&dev->pending_lock);181list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);182spin_unlock_irq(&dev->pending_lock);183}184185ret = &srq->ibsrq;186goto done;187188bail_ip:189kfree(srq->ip);190bail_wq:191vfree(srq->rq.wq);192bail_srq:193kfree(srq);194done:195return ret;196}197198/**199* qib_modify_srq - modify a shared receive queue200* @ibsrq: the SRQ to modify201* @attr: the new attributes of the SRQ202* @attr_mask: indicates which attributes to modify203* @udata: user data for libibverbs.so204*/205int qib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,206enum ib_srq_attr_mask attr_mask,207struct ib_udata *udata)208{209struct qib_srq *srq = to_isrq(ibsrq);210struct qib_rwq *wq;211int ret = 0;212213if (attr_mask & IB_SRQ_MAX_WR) {214struct qib_rwq *owq;215struct qib_rwqe *p;216u32 sz, size, n, head, tail;217218/* Check that the requested sizes are below the limits. */219if ((attr->max_wr > ib_qib_max_srq_wrs) ||220((attr_mask & IB_SRQ_LIMIT) ?221attr->srq_limit : srq->limit) > attr->max_wr) {222ret = -EINVAL;223goto bail;224}225226sz = sizeof(struct qib_rwqe) +227srq->rq.max_sge * sizeof(struct ib_sge);228size = attr->max_wr + 1;229wq = vmalloc_user(sizeof(struct qib_rwq) + size * sz);230if (!wq) {231ret = -ENOMEM;232goto bail;233}234235/* Check that we can write the offset to mmap. */236if (udata && udata->inlen >= sizeof(__u64)) {237__u64 offset_addr;238__u64 offset = 0;239240ret = ib_copy_from_udata(&offset_addr, udata,241sizeof(offset_addr));242if (ret)243goto bail_free;244udata->outbuf =245(void __user *) (unsigned long) offset_addr;246ret = ib_copy_to_udata(udata, &offset,247sizeof(offset));248if (ret)249goto bail_free;250}251252spin_lock_irq(&srq->rq.lock);253/*254* validate head and tail pointer values and compute255* the number of remaining WQEs.256*/257owq = srq->rq.wq;258head = owq->head;259tail = owq->tail;260if (head >= srq->rq.size || tail >= srq->rq.size) {261ret = -EINVAL;262goto bail_unlock;263}264n = head;265if (n < tail)266n += srq->rq.size - tail;267else268n -= tail;269if (size <= n) {270ret = -EINVAL;271goto bail_unlock;272}273n = 0;274p = wq->wq;275while (tail != head) {276struct qib_rwqe *wqe;277int i;278279wqe = get_rwqe_ptr(&srq->rq, tail);280p->wr_id = wqe->wr_id;281p->num_sge = wqe->num_sge;282for (i = 0; i < wqe->num_sge; i++)283p->sg_list[i] = wqe->sg_list[i];284n++;285p = (struct qib_rwqe *)((char *) p + sz);286if (++tail >= srq->rq.size)287tail = 0;288}289srq->rq.wq = wq;290srq->rq.size = size;291wq->head = n;292wq->tail = 0;293if (attr_mask & IB_SRQ_LIMIT)294srq->limit = attr->srq_limit;295spin_unlock_irq(&srq->rq.lock);296297vfree(owq);298299if (srq->ip) {300struct qib_mmap_info *ip = srq->ip;301struct qib_ibdev *dev = to_idev(srq->ibsrq.device);302u32 s = sizeof(struct qib_rwq) + size * sz;303304qib_update_mmap_info(dev, ip, s, wq);305306/*307* Return the offset to mmap.308* See qib_mmap() for details.309*/310if (udata && udata->inlen >= sizeof(__u64)) {311ret = ib_copy_to_udata(udata, &ip->offset,312sizeof(ip->offset));313if (ret)314goto bail;315}316317/*318* Put user mapping info onto the pending list319* unless it already is on the list.320*/321spin_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 qib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)346{347struct qib_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* qib_destroy_srq - destroy a shared receive queue357* @ibsrq: the SRQ to destroy358*/359int qib_destroy_srq(struct ib_srq *ibsrq)360{361struct qib_srq *srq = to_isrq(ibsrq);362struct qib_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, qib_release_mmap_info);369else370vfree(srq->rq.wq);371kfree(srq);372373return 0;374}375376377