18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2005 Cisco Systems. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/slab.h>
348c2ecf20Sopenharmony_ci#include <linux/string.h>
358c2ecf20Sopenharmony_ci#include <linux/sched.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <asm/io.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <rdma/uverbs_ioctl.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include "mthca_dev.h"
428c2ecf20Sopenharmony_ci#include "mthca_cmd.h"
438c2ecf20Sopenharmony_ci#include "mthca_memfree.h"
448c2ecf20Sopenharmony_ci#include "mthca_wqe.h"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum {
478c2ecf20Sopenharmony_ci	MTHCA_MAX_DIRECT_SRQ_SIZE = 4 * PAGE_SIZE
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistruct mthca_tavor_srq_context {
518c2ecf20Sopenharmony_ci	__be64 wqe_base_ds;	/* low 6 bits is descriptor size */
528c2ecf20Sopenharmony_ci	__be32 state_pd;
538c2ecf20Sopenharmony_ci	__be32 lkey;
548c2ecf20Sopenharmony_ci	__be32 uar;
558c2ecf20Sopenharmony_ci	__be16 limit_watermark;
568c2ecf20Sopenharmony_ci	__be16 wqe_cnt;
578c2ecf20Sopenharmony_ci	u32    reserved[2];
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistruct mthca_arbel_srq_context {
618c2ecf20Sopenharmony_ci	__be32 state_logsize_srqn;
628c2ecf20Sopenharmony_ci	__be32 lkey;
638c2ecf20Sopenharmony_ci	__be32 db_index;
648c2ecf20Sopenharmony_ci	__be32 logstride_usrpage;
658c2ecf20Sopenharmony_ci	__be64 wqe_base;
668c2ecf20Sopenharmony_ci	__be32 eq_pd;
678c2ecf20Sopenharmony_ci	__be16 limit_watermark;
688c2ecf20Sopenharmony_ci	__be16 wqe_cnt;
698c2ecf20Sopenharmony_ci	u16    reserved1;
708c2ecf20Sopenharmony_ci	__be16 wqe_counter;
718c2ecf20Sopenharmony_ci	u32    reserved2[3];
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void *get_wqe(struct mthca_srq *srq, int n)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	if (srq->is_direct)
778c2ecf20Sopenharmony_ci		return srq->queue.direct.buf + (n << srq->wqe_shift);
788c2ecf20Sopenharmony_ci	else
798c2ecf20Sopenharmony_ci		return srq->queue.page_list[(n << srq->wqe_shift) >> PAGE_SHIFT].buf +
808c2ecf20Sopenharmony_ci			((n << srq->wqe_shift) & (PAGE_SIZE - 1));
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/*
848c2ecf20Sopenharmony_ci * Return a pointer to the location within a WQE that we're using as a
858c2ecf20Sopenharmony_ci * link when the WQE is in the free list.  We use the imm field
868c2ecf20Sopenharmony_ci * because in the Tavor case, posting a WQE may overwrite the next
878c2ecf20Sopenharmony_ci * segment of the previous WQE, but a receive WQE will never touch the
888c2ecf20Sopenharmony_ci * imm field.  This avoids corrupting our free list if the previous
898c2ecf20Sopenharmony_ci * WQE has already completed and been put on the free list when we
908c2ecf20Sopenharmony_ci * post the next WQE.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistatic inline int *wqe_to_link(void *wqe)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	return (int *) (wqe + offsetof(struct mthca_next_seg, imm));
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic void mthca_tavor_init_srq_context(struct mthca_dev *dev,
988c2ecf20Sopenharmony_ci					 struct mthca_pd *pd,
998c2ecf20Sopenharmony_ci					 struct mthca_srq *srq,
1008c2ecf20Sopenharmony_ci					 struct mthca_tavor_srq_context *context,
1018c2ecf20Sopenharmony_ci					 struct ib_udata *udata)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct mthca_ucontext *ucontext = rdma_udata_to_drv_context(
1048c2ecf20Sopenharmony_ci		udata, struct mthca_ucontext, ibucontext);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	memset(context, 0, sizeof *context);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	context->wqe_base_ds = cpu_to_be64(1 << (srq->wqe_shift - 4));
1098c2ecf20Sopenharmony_ci	context->state_pd    = cpu_to_be32(pd->pd_num);
1108c2ecf20Sopenharmony_ci	context->lkey        = cpu_to_be32(srq->mr.ibmr.lkey);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (udata)
1138c2ecf20Sopenharmony_ci		context->uar = cpu_to_be32(ucontext->uar.index);
1148c2ecf20Sopenharmony_ci	else
1158c2ecf20Sopenharmony_ci		context->uar = cpu_to_be32(dev->driver_uar.index);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic void mthca_arbel_init_srq_context(struct mthca_dev *dev,
1198c2ecf20Sopenharmony_ci					 struct mthca_pd *pd,
1208c2ecf20Sopenharmony_ci					 struct mthca_srq *srq,
1218c2ecf20Sopenharmony_ci					 struct mthca_arbel_srq_context *context,
1228c2ecf20Sopenharmony_ci					 struct ib_udata *udata)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct mthca_ucontext *ucontext = rdma_udata_to_drv_context(
1258c2ecf20Sopenharmony_ci		udata, struct mthca_ucontext, ibucontext);
1268c2ecf20Sopenharmony_ci	int logsize, max;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	memset(context, 0, sizeof *context);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * Put max in a temporary variable to work around gcc bug
1328c2ecf20Sopenharmony_ci	 * triggered by ilog2() on sparc64.
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	max = srq->max;
1358c2ecf20Sopenharmony_ci	logsize = ilog2(max);
1368c2ecf20Sopenharmony_ci	context->state_logsize_srqn = cpu_to_be32(logsize << 24 | srq->srqn);
1378c2ecf20Sopenharmony_ci	context->lkey = cpu_to_be32(srq->mr.ibmr.lkey);
1388c2ecf20Sopenharmony_ci	context->db_index = cpu_to_be32(srq->db_index);
1398c2ecf20Sopenharmony_ci	context->logstride_usrpage = cpu_to_be32((srq->wqe_shift - 4) << 29);
1408c2ecf20Sopenharmony_ci	if (udata)
1418c2ecf20Sopenharmony_ci		context->logstride_usrpage |= cpu_to_be32(ucontext->uar.index);
1428c2ecf20Sopenharmony_ci	else
1438c2ecf20Sopenharmony_ci		context->logstride_usrpage |= cpu_to_be32(dev->driver_uar.index);
1448c2ecf20Sopenharmony_ci	context->eq_pd = cpu_to_be32(MTHCA_EQ_ASYNC << 24 | pd->pd_num);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic void mthca_free_srq_buf(struct mthca_dev *dev, struct mthca_srq *srq)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	mthca_buf_free(dev, srq->max << srq->wqe_shift, &srq->queue,
1508c2ecf20Sopenharmony_ci		       srq->is_direct, &srq->mr);
1518c2ecf20Sopenharmony_ci	kfree(srq->wrid);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int mthca_alloc_srq_buf(struct mthca_dev *dev, struct mthca_pd *pd,
1558c2ecf20Sopenharmony_ci			       struct mthca_srq *srq, struct ib_udata *udata)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct mthca_data_seg *scatter;
1588c2ecf20Sopenharmony_ci	void *wqe;
1598c2ecf20Sopenharmony_ci	int err;
1608c2ecf20Sopenharmony_ci	int i;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (udata)
1638c2ecf20Sopenharmony_ci		return 0;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	srq->wrid = kmalloc_array(srq->max, sizeof(u64), GFP_KERNEL);
1668c2ecf20Sopenharmony_ci	if (!srq->wrid)
1678c2ecf20Sopenharmony_ci		return -ENOMEM;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	err = mthca_buf_alloc(dev, srq->max << srq->wqe_shift,
1708c2ecf20Sopenharmony_ci			      MTHCA_MAX_DIRECT_SRQ_SIZE,
1718c2ecf20Sopenharmony_ci			      &srq->queue, &srq->is_direct, pd, 1, &srq->mr);
1728c2ecf20Sopenharmony_ci	if (err) {
1738c2ecf20Sopenharmony_ci		kfree(srq->wrid);
1748c2ecf20Sopenharmony_ci		return err;
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/*
1788c2ecf20Sopenharmony_ci	 * Now initialize the SRQ buffer so that all of the WQEs are
1798c2ecf20Sopenharmony_ci	 * linked into the list of free WQEs.  In addition, set the
1808c2ecf20Sopenharmony_ci	 * scatter list L_Keys to the sentry value of 0x100.
1818c2ecf20Sopenharmony_ci	 */
1828c2ecf20Sopenharmony_ci	for (i = 0; i < srq->max; ++i) {
1838c2ecf20Sopenharmony_ci		struct mthca_next_seg *next;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci		next = wqe = get_wqe(srq, i);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci		if (i < srq->max - 1) {
1888c2ecf20Sopenharmony_ci			*wqe_to_link(wqe) = i + 1;
1898c2ecf20Sopenharmony_ci			next->nda_op = htonl(((i + 1) << srq->wqe_shift) | 1);
1908c2ecf20Sopenharmony_ci		} else {
1918c2ecf20Sopenharmony_ci			*wqe_to_link(wqe) = -1;
1928c2ecf20Sopenharmony_ci			next->nda_op = 0;
1938c2ecf20Sopenharmony_ci		}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		for (scatter = wqe + sizeof (struct mthca_next_seg);
1968c2ecf20Sopenharmony_ci		     (void *) scatter < wqe + (1 << srq->wqe_shift);
1978c2ecf20Sopenharmony_ci		     ++scatter)
1988c2ecf20Sopenharmony_ci			scatter->lkey = cpu_to_be32(MTHCA_INVAL_LKEY);
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	srq->last = get_wqe(srq, srq->max - 1);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return 0;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciint mthca_alloc_srq(struct mthca_dev *dev, struct mthca_pd *pd,
2078c2ecf20Sopenharmony_ci		    struct ib_srq_attr *attr, struct mthca_srq *srq,
2088c2ecf20Sopenharmony_ci		    struct ib_udata *udata)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct mthca_mailbox *mailbox;
2118c2ecf20Sopenharmony_ci	int ds;
2128c2ecf20Sopenharmony_ci	int err;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* Sanity check SRQ size before proceeding */
2158c2ecf20Sopenharmony_ci	if (attr->max_wr  > dev->limits.max_srq_wqes ||
2168c2ecf20Sopenharmony_ci	    attr->max_sge > dev->limits.max_srq_sge)
2178c2ecf20Sopenharmony_ci		return -EINVAL;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	srq->max      = attr->max_wr;
2208c2ecf20Sopenharmony_ci	srq->max_gs   = attr->max_sge;
2218c2ecf20Sopenharmony_ci	srq->counter  = 0;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (mthca_is_memfree(dev))
2248c2ecf20Sopenharmony_ci		srq->max = roundup_pow_of_two(srq->max + 1);
2258c2ecf20Sopenharmony_ci	else
2268c2ecf20Sopenharmony_ci		srq->max = srq->max + 1;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	ds = max(64UL,
2298c2ecf20Sopenharmony_ci		 roundup_pow_of_two(sizeof (struct mthca_next_seg) +
2308c2ecf20Sopenharmony_ci				    srq->max_gs * sizeof (struct mthca_data_seg)));
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	if (!mthca_is_memfree(dev) && (ds > dev->limits.max_desc_sz))
2338c2ecf20Sopenharmony_ci		return -EINVAL;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	srq->wqe_shift = ilog2(ds);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	srq->srqn = mthca_alloc(&dev->srq_table.alloc);
2388c2ecf20Sopenharmony_ci	if (srq->srqn == -1)
2398c2ecf20Sopenharmony_ci		return -ENOMEM;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	if (mthca_is_memfree(dev)) {
2428c2ecf20Sopenharmony_ci		err = mthca_table_get(dev, dev->srq_table.table, srq->srqn);
2438c2ecf20Sopenharmony_ci		if (err)
2448c2ecf20Sopenharmony_ci			goto err_out;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		if (!udata) {
2478c2ecf20Sopenharmony_ci			srq->db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_SRQ,
2488c2ecf20Sopenharmony_ci						       srq->srqn, &srq->db);
2498c2ecf20Sopenharmony_ci			if (srq->db_index < 0) {
2508c2ecf20Sopenharmony_ci				err = -ENOMEM;
2518c2ecf20Sopenharmony_ci				goto err_out_icm;
2528c2ecf20Sopenharmony_ci			}
2538c2ecf20Sopenharmony_ci		}
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
2578c2ecf20Sopenharmony_ci	if (IS_ERR(mailbox)) {
2588c2ecf20Sopenharmony_ci		err = PTR_ERR(mailbox);
2598c2ecf20Sopenharmony_ci		goto err_out_db;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	err = mthca_alloc_srq_buf(dev, pd, srq, udata);
2638c2ecf20Sopenharmony_ci	if (err)
2648c2ecf20Sopenharmony_ci		goto err_out_mailbox;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	spin_lock_init(&srq->lock);
2678c2ecf20Sopenharmony_ci	srq->refcount = 1;
2688c2ecf20Sopenharmony_ci	init_waitqueue_head(&srq->wait);
2698c2ecf20Sopenharmony_ci	mutex_init(&srq->mutex);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (mthca_is_memfree(dev))
2728c2ecf20Sopenharmony_ci		mthca_arbel_init_srq_context(dev, pd, srq, mailbox->buf, udata);
2738c2ecf20Sopenharmony_ci	else
2748c2ecf20Sopenharmony_ci		mthca_tavor_init_srq_context(dev, pd, srq, mailbox->buf, udata);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	err = mthca_SW2HW_SRQ(dev, mailbox, srq->srqn);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (err) {
2798c2ecf20Sopenharmony_ci		mthca_warn(dev, "SW2HW_SRQ failed (%d)\n", err);
2808c2ecf20Sopenharmony_ci		goto err_out_free_buf;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	spin_lock_irq(&dev->srq_table.lock);
2848c2ecf20Sopenharmony_ci	if (mthca_array_set(&dev->srq_table.srq,
2858c2ecf20Sopenharmony_ci			    srq->srqn & (dev->limits.num_srqs - 1),
2868c2ecf20Sopenharmony_ci			    srq)) {
2878c2ecf20Sopenharmony_ci		spin_unlock_irq(&dev->srq_table.lock);
2888c2ecf20Sopenharmony_ci		goto err_out_free_srq;
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci	spin_unlock_irq(&dev->srq_table.lock);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	mthca_free_mailbox(dev, mailbox);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	srq->first_free = 0;
2958c2ecf20Sopenharmony_ci	srq->last_free  = srq->max - 1;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	attr->max_wr    = srq->max - 1;
2988c2ecf20Sopenharmony_ci	attr->max_sge   = srq->max_gs;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	return 0;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cierr_out_free_srq:
3038c2ecf20Sopenharmony_ci	err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn);
3048c2ecf20Sopenharmony_ci	if (err)
3058c2ecf20Sopenharmony_ci		mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cierr_out_free_buf:
3088c2ecf20Sopenharmony_ci	if (!udata)
3098c2ecf20Sopenharmony_ci		mthca_free_srq_buf(dev, srq);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cierr_out_mailbox:
3128c2ecf20Sopenharmony_ci	mthca_free_mailbox(dev, mailbox);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cierr_out_db:
3158c2ecf20Sopenharmony_ci	if (!udata && mthca_is_memfree(dev))
3168c2ecf20Sopenharmony_ci		mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cierr_out_icm:
3198c2ecf20Sopenharmony_ci	mthca_table_put(dev, dev->srq_table.table, srq->srqn);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cierr_out:
3228c2ecf20Sopenharmony_ci	mthca_free(&dev->srq_table.alloc, srq->srqn);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	return err;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic inline int get_srq_refcount(struct mthca_dev *dev, struct mthca_srq *srq)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	int c;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	spin_lock_irq(&dev->srq_table.lock);
3328c2ecf20Sopenharmony_ci	c = srq->refcount;
3338c2ecf20Sopenharmony_ci	spin_unlock_irq(&dev->srq_table.lock);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	return c;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_civoid mthca_free_srq(struct mthca_dev *dev, struct mthca_srq *srq)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	struct mthca_mailbox *mailbox;
3418c2ecf20Sopenharmony_ci	int err;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
3448c2ecf20Sopenharmony_ci	if (IS_ERR(mailbox)) {
3458c2ecf20Sopenharmony_ci		mthca_warn(dev, "No memory for mailbox to free SRQ.\n");
3468c2ecf20Sopenharmony_ci		return;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	err = mthca_HW2SW_SRQ(dev, mailbox, srq->srqn);
3508c2ecf20Sopenharmony_ci	if (err)
3518c2ecf20Sopenharmony_ci		mthca_warn(dev, "HW2SW_SRQ failed (%d)\n", err);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	spin_lock_irq(&dev->srq_table.lock);
3548c2ecf20Sopenharmony_ci	mthca_array_clear(&dev->srq_table.srq,
3558c2ecf20Sopenharmony_ci			  srq->srqn & (dev->limits.num_srqs - 1));
3568c2ecf20Sopenharmony_ci	--srq->refcount;
3578c2ecf20Sopenharmony_ci	spin_unlock_irq(&dev->srq_table.lock);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	wait_event(srq->wait, !get_srq_refcount(dev, srq));
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (!srq->ibsrq.uobject) {
3628c2ecf20Sopenharmony_ci		mthca_free_srq_buf(dev, srq);
3638c2ecf20Sopenharmony_ci		if (mthca_is_memfree(dev))
3648c2ecf20Sopenharmony_ci			mthca_free_db(dev, MTHCA_DB_TYPE_SRQ, srq->db_index);
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	mthca_table_put(dev, dev->srq_table.table, srq->srqn);
3688c2ecf20Sopenharmony_ci	mthca_free(&dev->srq_table.alloc, srq->srqn);
3698c2ecf20Sopenharmony_ci	mthca_free_mailbox(dev, mailbox);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ciint mthca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
3738c2ecf20Sopenharmony_ci		     enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct mthca_dev *dev = to_mdev(ibsrq->device);
3768c2ecf20Sopenharmony_ci	struct mthca_srq *srq = to_msrq(ibsrq);
3778c2ecf20Sopenharmony_ci	int ret = 0;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* We don't support resizing SRQs (yet?) */
3808c2ecf20Sopenharmony_ci	if (attr_mask & IB_SRQ_MAX_WR)
3818c2ecf20Sopenharmony_ci		return -EINVAL;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (attr_mask & IB_SRQ_LIMIT) {
3848c2ecf20Sopenharmony_ci		u32 max_wr = mthca_is_memfree(dev) ? srq->max - 1 : srq->max;
3858c2ecf20Sopenharmony_ci		if (attr->srq_limit > max_wr)
3868c2ecf20Sopenharmony_ci			return -EINVAL;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		mutex_lock(&srq->mutex);
3898c2ecf20Sopenharmony_ci		ret = mthca_ARM_SRQ(dev, srq->srqn, attr->srq_limit);
3908c2ecf20Sopenharmony_ci		mutex_unlock(&srq->mutex);
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	return ret;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ciint mthca_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	struct mthca_dev *dev = to_mdev(ibsrq->device);
3998c2ecf20Sopenharmony_ci	struct mthca_srq *srq = to_msrq(ibsrq);
4008c2ecf20Sopenharmony_ci	struct mthca_mailbox *mailbox;
4018c2ecf20Sopenharmony_ci	struct mthca_arbel_srq_context *arbel_ctx;
4028c2ecf20Sopenharmony_ci	struct mthca_tavor_srq_context *tavor_ctx;
4038c2ecf20Sopenharmony_ci	int err;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
4068c2ecf20Sopenharmony_ci	if (IS_ERR(mailbox))
4078c2ecf20Sopenharmony_ci		return PTR_ERR(mailbox);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	err = mthca_QUERY_SRQ(dev, srq->srqn, mailbox);
4108c2ecf20Sopenharmony_ci	if (err)
4118c2ecf20Sopenharmony_ci		goto out;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (mthca_is_memfree(dev)) {
4148c2ecf20Sopenharmony_ci		arbel_ctx = mailbox->buf;
4158c2ecf20Sopenharmony_ci		srq_attr->srq_limit = be16_to_cpu(arbel_ctx->limit_watermark);
4168c2ecf20Sopenharmony_ci	} else {
4178c2ecf20Sopenharmony_ci		tavor_ctx = mailbox->buf;
4188c2ecf20Sopenharmony_ci		srq_attr->srq_limit = be16_to_cpu(tavor_ctx->limit_watermark);
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	srq_attr->max_wr  = srq->max - 1;
4228c2ecf20Sopenharmony_ci	srq_attr->max_sge = srq->max_gs;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ciout:
4258c2ecf20Sopenharmony_ci	mthca_free_mailbox(dev, mailbox);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	return err;
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_civoid mthca_srq_event(struct mthca_dev *dev, u32 srqn,
4318c2ecf20Sopenharmony_ci		     enum ib_event_type event_type)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	struct mthca_srq *srq;
4348c2ecf20Sopenharmony_ci	struct ib_event event;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	spin_lock(&dev->srq_table.lock);
4378c2ecf20Sopenharmony_ci	srq = mthca_array_get(&dev->srq_table.srq, srqn & (dev->limits.num_srqs - 1));
4388c2ecf20Sopenharmony_ci	if (srq)
4398c2ecf20Sopenharmony_ci		++srq->refcount;
4408c2ecf20Sopenharmony_ci	spin_unlock(&dev->srq_table.lock);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	if (!srq) {
4438c2ecf20Sopenharmony_ci		mthca_warn(dev, "Async event for bogus SRQ %08x\n", srqn);
4448c2ecf20Sopenharmony_ci		return;
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	if (!srq->ibsrq.event_handler)
4488c2ecf20Sopenharmony_ci		goto out;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	event.device      = &dev->ib_dev;
4518c2ecf20Sopenharmony_ci	event.event       = event_type;
4528c2ecf20Sopenharmony_ci	event.element.srq = &srq->ibsrq;
4538c2ecf20Sopenharmony_ci	srq->ibsrq.event_handler(&event, srq->ibsrq.srq_context);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ciout:
4568c2ecf20Sopenharmony_ci	spin_lock(&dev->srq_table.lock);
4578c2ecf20Sopenharmony_ci	if (!--srq->refcount)
4588c2ecf20Sopenharmony_ci		wake_up(&srq->wait);
4598c2ecf20Sopenharmony_ci	spin_unlock(&dev->srq_table.lock);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci/*
4638c2ecf20Sopenharmony_ci * This function must be called with IRQs disabled.
4648c2ecf20Sopenharmony_ci */
4658c2ecf20Sopenharmony_civoid mthca_free_srq_wqe(struct mthca_srq *srq, u32 wqe_addr)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	int ind;
4688c2ecf20Sopenharmony_ci	struct mthca_next_seg *last_free;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ind = wqe_addr >> srq->wqe_shift;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	spin_lock(&srq->lock);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	last_free = get_wqe(srq, srq->last_free);
4758c2ecf20Sopenharmony_ci	*wqe_to_link(last_free) = ind;
4768c2ecf20Sopenharmony_ci	last_free->nda_op = htonl((ind << srq->wqe_shift) | 1);
4778c2ecf20Sopenharmony_ci	*wqe_to_link(get_wqe(srq, ind)) = -1;
4788c2ecf20Sopenharmony_ci	srq->last_free = ind;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	spin_unlock(&srq->lock);
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ciint mthca_tavor_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
4848c2ecf20Sopenharmony_ci			      const struct ib_recv_wr **bad_wr)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	struct mthca_dev *dev = to_mdev(ibsrq->device);
4878c2ecf20Sopenharmony_ci	struct mthca_srq *srq = to_msrq(ibsrq);
4888c2ecf20Sopenharmony_ci	unsigned long flags;
4898c2ecf20Sopenharmony_ci	int err = 0;
4908c2ecf20Sopenharmony_ci	int first_ind;
4918c2ecf20Sopenharmony_ci	int ind;
4928c2ecf20Sopenharmony_ci	int next_ind;
4938c2ecf20Sopenharmony_ci	int nreq;
4948c2ecf20Sopenharmony_ci	int i;
4958c2ecf20Sopenharmony_ci	void *wqe;
4968c2ecf20Sopenharmony_ci	void *prev_wqe;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&srq->lock, flags);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	first_ind = srq->first_free;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	for (nreq = 0; wr; wr = wr->next) {
5038c2ecf20Sopenharmony_ci		ind       = srq->first_free;
5048c2ecf20Sopenharmony_ci		wqe       = get_wqe(srq, ind);
5058c2ecf20Sopenharmony_ci		next_ind  = *wqe_to_link(wqe);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci		if (unlikely(next_ind < 0)) {
5088c2ecf20Sopenharmony_ci			mthca_err(dev, "SRQ %06x full\n", srq->srqn);
5098c2ecf20Sopenharmony_ci			err = -ENOMEM;
5108c2ecf20Sopenharmony_ci			*bad_wr = wr;
5118c2ecf20Sopenharmony_ci			break;
5128c2ecf20Sopenharmony_ci		}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci		prev_wqe  = srq->last;
5158c2ecf20Sopenharmony_ci		srq->last = wqe;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		((struct mthca_next_seg *) wqe)->ee_nds = 0;
5188c2ecf20Sopenharmony_ci		/* flags field will always remain 0 */
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci		wqe += sizeof (struct mthca_next_seg);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci		if (unlikely(wr->num_sge > srq->max_gs)) {
5238c2ecf20Sopenharmony_ci			err = -EINVAL;
5248c2ecf20Sopenharmony_ci			*bad_wr = wr;
5258c2ecf20Sopenharmony_ci			srq->last = prev_wqe;
5268c2ecf20Sopenharmony_ci			break;
5278c2ecf20Sopenharmony_ci		}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		for (i = 0; i < wr->num_sge; ++i) {
5308c2ecf20Sopenharmony_ci			mthca_set_data_seg(wqe, wr->sg_list + i);
5318c2ecf20Sopenharmony_ci			wqe += sizeof (struct mthca_data_seg);
5328c2ecf20Sopenharmony_ci		}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		if (i < srq->max_gs)
5358c2ecf20Sopenharmony_ci			mthca_set_data_seg_inval(wqe);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		((struct mthca_next_seg *) prev_wqe)->ee_nds =
5388c2ecf20Sopenharmony_ci			cpu_to_be32(MTHCA_NEXT_DBD);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		srq->wrid[ind]  = wr->wr_id;
5418c2ecf20Sopenharmony_ci		srq->first_free = next_ind;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		++nreq;
5448c2ecf20Sopenharmony_ci		if (unlikely(nreq == MTHCA_TAVOR_MAX_WQES_PER_RECV_DB)) {
5458c2ecf20Sopenharmony_ci			nreq = 0;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci			/*
5488c2ecf20Sopenharmony_ci			 * Make sure that descriptors are written
5498c2ecf20Sopenharmony_ci			 * before doorbell is rung.
5508c2ecf20Sopenharmony_ci			 */
5518c2ecf20Sopenharmony_ci			wmb();
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci			mthca_write64(first_ind << srq->wqe_shift, srq->srqn << 8,
5548c2ecf20Sopenharmony_ci				      dev->kar + MTHCA_RECEIVE_DOORBELL,
5558c2ecf20Sopenharmony_ci				      MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci			first_ind = srq->first_free;
5588c2ecf20Sopenharmony_ci		}
5598c2ecf20Sopenharmony_ci	}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	if (likely(nreq)) {
5628c2ecf20Sopenharmony_ci		/*
5638c2ecf20Sopenharmony_ci		 * Make sure that descriptors are written before
5648c2ecf20Sopenharmony_ci		 * doorbell is rung.
5658c2ecf20Sopenharmony_ci		 */
5668c2ecf20Sopenharmony_ci		wmb();
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci		mthca_write64(first_ind << srq->wqe_shift, (srq->srqn << 8) | nreq,
5698c2ecf20Sopenharmony_ci			      dev->kar + MTHCA_RECEIVE_DOORBELL,
5708c2ecf20Sopenharmony_ci			      MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&srq->lock, flags);
5748c2ecf20Sopenharmony_ci	return err;
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ciint mthca_arbel_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
5788c2ecf20Sopenharmony_ci			      const struct ib_recv_wr **bad_wr)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	struct mthca_dev *dev = to_mdev(ibsrq->device);
5818c2ecf20Sopenharmony_ci	struct mthca_srq *srq = to_msrq(ibsrq);
5828c2ecf20Sopenharmony_ci	unsigned long flags;
5838c2ecf20Sopenharmony_ci	int err = 0;
5848c2ecf20Sopenharmony_ci	int ind;
5858c2ecf20Sopenharmony_ci	int next_ind;
5868c2ecf20Sopenharmony_ci	int nreq;
5878c2ecf20Sopenharmony_ci	int i;
5888c2ecf20Sopenharmony_ci	void *wqe;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&srq->lock, flags);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	for (nreq = 0; wr; ++nreq, wr = wr->next) {
5938c2ecf20Sopenharmony_ci		ind       = srq->first_free;
5948c2ecf20Sopenharmony_ci		wqe       = get_wqe(srq, ind);
5958c2ecf20Sopenharmony_ci		next_ind  = *wqe_to_link(wqe);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		if (unlikely(next_ind < 0)) {
5988c2ecf20Sopenharmony_ci			mthca_err(dev, "SRQ %06x full\n", srq->srqn);
5998c2ecf20Sopenharmony_ci			err = -ENOMEM;
6008c2ecf20Sopenharmony_ci			*bad_wr = wr;
6018c2ecf20Sopenharmony_ci			break;
6028c2ecf20Sopenharmony_ci		}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci		((struct mthca_next_seg *) wqe)->ee_nds = 0;
6058c2ecf20Sopenharmony_ci		/* flags field will always remain 0 */
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci		wqe += sizeof (struct mthca_next_seg);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci		if (unlikely(wr->num_sge > srq->max_gs)) {
6108c2ecf20Sopenharmony_ci			err = -EINVAL;
6118c2ecf20Sopenharmony_ci			*bad_wr = wr;
6128c2ecf20Sopenharmony_ci			break;
6138c2ecf20Sopenharmony_ci		}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci		for (i = 0; i < wr->num_sge; ++i) {
6168c2ecf20Sopenharmony_ci			mthca_set_data_seg(wqe, wr->sg_list + i);
6178c2ecf20Sopenharmony_ci			wqe += sizeof (struct mthca_data_seg);
6188c2ecf20Sopenharmony_ci		}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci		if (i < srq->max_gs)
6218c2ecf20Sopenharmony_ci			mthca_set_data_seg_inval(wqe);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci		srq->wrid[ind]  = wr->wr_id;
6248c2ecf20Sopenharmony_ci		srq->first_free = next_ind;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	if (likely(nreq)) {
6288c2ecf20Sopenharmony_ci		srq->counter += nreq;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci		/*
6318c2ecf20Sopenharmony_ci		 * Make sure that descriptors are written before
6328c2ecf20Sopenharmony_ci		 * we write doorbell record.
6338c2ecf20Sopenharmony_ci		 */
6348c2ecf20Sopenharmony_ci		wmb();
6358c2ecf20Sopenharmony_ci		*srq->db = cpu_to_be32(srq->counter);
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&srq->lock, flags);
6398c2ecf20Sopenharmony_ci	return err;
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ciint mthca_max_srq_sge(struct mthca_dev *dev)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	if (mthca_is_memfree(dev))
6458c2ecf20Sopenharmony_ci		return dev->limits.max_sg;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	/*
6488c2ecf20Sopenharmony_ci	 * SRQ allocations are based on powers of 2 for Tavor,
6498c2ecf20Sopenharmony_ci	 * (although they only need to be multiples of 16 bytes).
6508c2ecf20Sopenharmony_ci	 *
6518c2ecf20Sopenharmony_ci	 * Therefore, we need to base the max number of sg entries on
6528c2ecf20Sopenharmony_ci	 * the largest power of 2 descriptor size that is <= to the
6538c2ecf20Sopenharmony_ci	 * actual max WQE descriptor size, rather than return the
6548c2ecf20Sopenharmony_ci	 * max_sg value given by the firmware (which is based on WQE
6558c2ecf20Sopenharmony_ci	 * sizes as multiples of 16, not powers of 2).
6568c2ecf20Sopenharmony_ci	 *
6578c2ecf20Sopenharmony_ci	 * If SRQ implementation is changed for Tavor to be based on
6588c2ecf20Sopenharmony_ci	 * multiples of 16, the calculation below can be deleted and
6598c2ecf20Sopenharmony_ci	 * the FW max_sg value returned.
6608c2ecf20Sopenharmony_ci	 */
6618c2ecf20Sopenharmony_ci	return min_t(int, dev->limits.max_sg,
6628c2ecf20Sopenharmony_ci		     ((1 << (fls(dev->limits.max_desc_sz) - 1)) -
6638c2ecf20Sopenharmony_ci		      sizeof (struct mthca_next_seg)) /
6648c2ecf20Sopenharmony_ci		     sizeof (struct mthca_data_seg));
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ciint mthca_init_srq_table(struct mthca_dev *dev)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	int err;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
6728c2ecf20Sopenharmony_ci		return 0;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	spin_lock_init(&dev->srq_table.lock);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	err = mthca_alloc_init(&dev->srq_table.alloc,
6778c2ecf20Sopenharmony_ci			       dev->limits.num_srqs,
6788c2ecf20Sopenharmony_ci			       dev->limits.num_srqs - 1,
6798c2ecf20Sopenharmony_ci			       dev->limits.reserved_srqs);
6808c2ecf20Sopenharmony_ci	if (err)
6818c2ecf20Sopenharmony_ci		return err;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	err = mthca_array_init(&dev->srq_table.srq,
6848c2ecf20Sopenharmony_ci			       dev->limits.num_srqs);
6858c2ecf20Sopenharmony_ci	if (err)
6868c2ecf20Sopenharmony_ci		mthca_alloc_cleanup(&dev->srq_table.alloc);
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	return err;
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_civoid mthca_cleanup_srq_table(struct mthca_dev *dev)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	if (!(dev->mthca_flags & MTHCA_FLAG_SRQ))
6948c2ecf20Sopenharmony_ci		return;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	mthca_array_cleanup(&dev->srq_table.srq, dev->limits.num_srqs);
6978c2ecf20Sopenharmony_ci	mthca_alloc_cleanup(&dev->srq_table.alloc);
6988c2ecf20Sopenharmony_ci}
699