1225c7b1fSRoland Dreier /* 2225c7b1fSRoland Dreier * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. 3225c7b1fSRoland Dreier * 4225c7b1fSRoland Dreier * This software is available to you under a choice of one of two 5225c7b1fSRoland Dreier * licenses. You may choose to be licensed under the terms of the GNU 6225c7b1fSRoland Dreier * General Public License (GPL) Version 2, available from the file 7225c7b1fSRoland Dreier * COPYING in the main directory of this source tree, or the 8225c7b1fSRoland Dreier * OpenIB.org BSD license below: 9225c7b1fSRoland Dreier * 10225c7b1fSRoland Dreier * Redistribution and use in source and binary forms, with or 11225c7b1fSRoland Dreier * without modification, are permitted provided that the following 12225c7b1fSRoland Dreier * conditions are met: 13225c7b1fSRoland Dreier * 14225c7b1fSRoland Dreier * - Redistributions of source code must retain the above 15225c7b1fSRoland Dreier * copyright notice, this list of conditions and the following 16225c7b1fSRoland Dreier * disclaimer. 17225c7b1fSRoland Dreier * 18225c7b1fSRoland Dreier * - Redistributions in binary form must reproduce the above 19225c7b1fSRoland Dreier * copyright notice, this list of conditions and the following 20225c7b1fSRoland Dreier * disclaimer in the documentation and/or other materials 21225c7b1fSRoland Dreier * provided with the distribution. 22225c7b1fSRoland Dreier * 23225c7b1fSRoland Dreier * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24225c7b1fSRoland Dreier * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25225c7b1fSRoland Dreier * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26225c7b1fSRoland Dreier * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27225c7b1fSRoland Dreier * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28225c7b1fSRoland Dreier * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29225c7b1fSRoland Dreier * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30225c7b1fSRoland Dreier * SOFTWARE. 31225c7b1fSRoland Dreier */ 32225c7b1fSRoland Dreier 33225c7b1fSRoland Dreier #include <linux/mlx4/qp.h> 34225c7b1fSRoland Dreier #include <linux/mlx4/srq.h> 35225c7b1fSRoland Dreier 36225c7b1fSRoland Dreier #include "mlx4_ib.h" 37225c7b1fSRoland Dreier #include "user.h" 38225c7b1fSRoland Dreier 39225c7b1fSRoland Dreier static void *get_wqe(struct mlx4_ib_srq *srq, int n) 40225c7b1fSRoland Dreier { 411c69fc2aSRoland Dreier return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift); 42225c7b1fSRoland Dreier } 43225c7b1fSRoland Dreier 44225c7b1fSRoland Dreier static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type) 45225c7b1fSRoland Dreier { 46225c7b1fSRoland Dreier struct ib_event event; 47225c7b1fSRoland Dreier struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq; 48225c7b1fSRoland Dreier 49225c7b1fSRoland Dreier if (ibsrq->event_handler) { 50225c7b1fSRoland Dreier event.device = ibsrq->device; 51225c7b1fSRoland Dreier event.element.srq = ibsrq; 52225c7b1fSRoland Dreier switch (type) { 53225c7b1fSRoland Dreier case MLX4_EVENT_TYPE_SRQ_LIMIT: 54225c7b1fSRoland Dreier event.event = IB_EVENT_SRQ_LIMIT_REACHED; 55225c7b1fSRoland Dreier break; 56225c7b1fSRoland Dreier case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR: 57225c7b1fSRoland Dreier event.event = IB_EVENT_SRQ_ERR; 58225c7b1fSRoland Dreier break; 59225c7b1fSRoland Dreier default: 60225c7b1fSRoland Dreier printk(KERN_WARNING "mlx4_ib: Unexpected event type %d " 61225c7b1fSRoland Dreier "on SRQ %06x\n", type, srq->srqn); 62225c7b1fSRoland Dreier return; 63225c7b1fSRoland Dreier } 64225c7b1fSRoland Dreier 65225c7b1fSRoland Dreier ibsrq->event_handler(&event, ibsrq->srq_context); 66225c7b1fSRoland Dreier } 67225c7b1fSRoland Dreier } 68225c7b1fSRoland Dreier 69225c7b1fSRoland Dreier struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd, 70225c7b1fSRoland Dreier struct ib_srq_init_attr *init_attr, 71225c7b1fSRoland Dreier struct ib_udata *udata) 72225c7b1fSRoland Dreier { 73225c7b1fSRoland Dreier struct mlx4_ib_dev *dev = to_mdev(pd->device); 74225c7b1fSRoland Dreier struct mlx4_ib_srq *srq; 75225c7b1fSRoland Dreier struct mlx4_wqe_srq_next_seg *next; 76225c7b1fSRoland Dreier int desc_size; 77225c7b1fSRoland Dreier int buf_size; 78225c7b1fSRoland Dreier int err; 79225c7b1fSRoland Dreier int i; 80225c7b1fSRoland Dreier 81225c7b1fSRoland Dreier /* Sanity check SRQ size before proceeding */ 82225c7b1fSRoland Dreier if (init_attr->attr.max_wr >= dev->dev->caps.max_srq_wqes || 83225c7b1fSRoland Dreier init_attr->attr.max_sge > dev->dev->caps.max_srq_sge) 84225c7b1fSRoland Dreier return ERR_PTR(-EINVAL); 85225c7b1fSRoland Dreier 86225c7b1fSRoland Dreier srq = kmalloc(sizeof *srq, GFP_KERNEL); 87225c7b1fSRoland Dreier if (!srq) 88225c7b1fSRoland Dreier return ERR_PTR(-ENOMEM); 89225c7b1fSRoland Dreier 90225c7b1fSRoland Dreier mutex_init(&srq->mutex); 91225c7b1fSRoland Dreier spin_lock_init(&srq->lock); 92225c7b1fSRoland Dreier srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1); 93225c7b1fSRoland Dreier srq->msrq.max_gs = init_attr->attr.max_sge; 94225c7b1fSRoland Dreier 95225c7b1fSRoland Dreier desc_size = max(32UL, 96225c7b1fSRoland Dreier roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) + 97225c7b1fSRoland Dreier srq->msrq.max_gs * 98225c7b1fSRoland Dreier sizeof (struct mlx4_wqe_data_seg))); 99225c7b1fSRoland Dreier srq->msrq.wqe_shift = ilog2(desc_size); 100225c7b1fSRoland Dreier 101225c7b1fSRoland Dreier buf_size = srq->msrq.max * desc_size; 102225c7b1fSRoland Dreier 103225c7b1fSRoland Dreier if (pd->uobject) { 104225c7b1fSRoland Dreier struct mlx4_ib_create_srq ucmd; 105225c7b1fSRoland Dreier 106225c7b1fSRoland Dreier if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) { 107225c7b1fSRoland Dreier err = -EFAULT; 108225c7b1fSRoland Dreier goto err_srq; 109225c7b1fSRoland Dreier } 110225c7b1fSRoland Dreier 111225c7b1fSRoland Dreier srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, 112225c7b1fSRoland Dreier buf_size, 0); 113225c7b1fSRoland Dreier if (IS_ERR(srq->umem)) { 114225c7b1fSRoland Dreier err = PTR_ERR(srq->umem); 115225c7b1fSRoland Dreier goto err_srq; 116225c7b1fSRoland Dreier } 117225c7b1fSRoland Dreier 118225c7b1fSRoland Dreier err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem), 119225c7b1fSRoland Dreier ilog2(srq->umem->page_size), &srq->mtt); 120225c7b1fSRoland Dreier if (err) 121225c7b1fSRoland Dreier goto err_buf; 122225c7b1fSRoland Dreier 123225c7b1fSRoland Dreier err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem); 124225c7b1fSRoland Dreier if (err) 125225c7b1fSRoland Dreier goto err_mtt; 126225c7b1fSRoland Dreier 127225c7b1fSRoland Dreier err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context), 128225c7b1fSRoland Dreier ucmd.db_addr, &srq->db); 129225c7b1fSRoland Dreier if (err) 130225c7b1fSRoland Dreier goto err_mtt; 131225c7b1fSRoland Dreier } else { 1326296883cSYevgeny Petrilin err = mlx4_db_alloc(dev->dev, &srq->db, 0); 133225c7b1fSRoland Dreier if (err) 134225c7b1fSRoland Dreier goto err_srq; 135225c7b1fSRoland Dreier 136225c7b1fSRoland Dreier *srq->db.db = 0; 137225c7b1fSRoland Dreier 138225c7b1fSRoland Dreier if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf)) { 139225c7b1fSRoland Dreier err = -ENOMEM; 140225c7b1fSRoland Dreier goto err_db; 141225c7b1fSRoland Dreier } 142225c7b1fSRoland Dreier 143225c7b1fSRoland Dreier srq->head = 0; 144225c7b1fSRoland Dreier srq->tail = srq->msrq.max - 1; 145225c7b1fSRoland Dreier srq->wqe_ctr = 0; 146225c7b1fSRoland Dreier 147225c7b1fSRoland Dreier for (i = 0; i < srq->msrq.max; ++i) { 148225c7b1fSRoland Dreier next = get_wqe(srq, i); 149225c7b1fSRoland Dreier next->next_wqe_index = 150225c7b1fSRoland Dreier cpu_to_be16((i + 1) & (srq->msrq.max - 1)); 151225c7b1fSRoland Dreier } 152225c7b1fSRoland Dreier 153225c7b1fSRoland Dreier err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift, 154225c7b1fSRoland Dreier &srq->mtt); 155225c7b1fSRoland Dreier if (err) 156225c7b1fSRoland Dreier goto err_buf; 157225c7b1fSRoland Dreier 158225c7b1fSRoland Dreier err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf); 159225c7b1fSRoland Dreier if (err) 160225c7b1fSRoland Dreier goto err_mtt; 161225c7b1fSRoland Dreier 162225c7b1fSRoland Dreier srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL); 163225c7b1fSRoland Dreier if (!srq->wrid) { 164225c7b1fSRoland Dreier err = -ENOMEM; 165225c7b1fSRoland Dreier goto err_mtt; 166225c7b1fSRoland Dreier } 167225c7b1fSRoland Dreier } 168225c7b1fSRoland Dreier 169225c7b1fSRoland Dreier err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, &srq->mtt, 170225c7b1fSRoland Dreier srq->db.dma, &srq->msrq); 171225c7b1fSRoland Dreier if (err) 172225c7b1fSRoland Dreier goto err_wrid; 173225c7b1fSRoland Dreier 174225c7b1fSRoland Dreier srq->msrq.event = mlx4_ib_srq_event; 175225c7b1fSRoland Dreier 176225c7b1fSRoland Dreier if (pd->uobject) 177225c7b1fSRoland Dreier if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) { 178225c7b1fSRoland Dreier err = -EFAULT; 179225c7b1fSRoland Dreier goto err_wrid; 180225c7b1fSRoland Dreier } 181225c7b1fSRoland Dreier 182225c7b1fSRoland Dreier init_attr->attr.max_wr = srq->msrq.max - 1; 183225c7b1fSRoland Dreier 184225c7b1fSRoland Dreier return &srq->ibsrq; 185225c7b1fSRoland Dreier 186225c7b1fSRoland Dreier err_wrid: 187225c7b1fSRoland Dreier if (pd->uobject) 188225c7b1fSRoland Dreier mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db); 189225c7b1fSRoland Dreier else 190225c7b1fSRoland Dreier kfree(srq->wrid); 191225c7b1fSRoland Dreier 192225c7b1fSRoland Dreier err_mtt: 193225c7b1fSRoland Dreier mlx4_mtt_cleanup(dev->dev, &srq->mtt); 194225c7b1fSRoland Dreier 195225c7b1fSRoland Dreier err_buf: 196225c7b1fSRoland Dreier if (pd->uobject) 197225c7b1fSRoland Dreier ib_umem_release(srq->umem); 198225c7b1fSRoland Dreier else 199225c7b1fSRoland Dreier mlx4_buf_free(dev->dev, buf_size, &srq->buf); 200225c7b1fSRoland Dreier 201225c7b1fSRoland Dreier err_db: 202225c7b1fSRoland Dreier if (!pd->uobject) 2036296883cSYevgeny Petrilin mlx4_db_free(dev->dev, &srq->db); 204225c7b1fSRoland Dreier 205225c7b1fSRoland Dreier err_srq: 206225c7b1fSRoland Dreier kfree(srq); 207225c7b1fSRoland Dreier 208225c7b1fSRoland Dreier return ERR_PTR(err); 209225c7b1fSRoland Dreier } 210225c7b1fSRoland Dreier 211225c7b1fSRoland Dreier int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 212225c7b1fSRoland Dreier enum ib_srq_attr_mask attr_mask, struct ib_udata *udata) 213225c7b1fSRoland Dreier { 214225c7b1fSRoland Dreier struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); 215225c7b1fSRoland Dreier struct mlx4_ib_srq *srq = to_msrq(ibsrq); 216225c7b1fSRoland Dreier int ret; 217225c7b1fSRoland Dreier 218225c7b1fSRoland Dreier /* We don't support resizing SRQs (yet?) */ 219225c7b1fSRoland Dreier if (attr_mask & IB_SRQ_MAX_WR) 220225c7b1fSRoland Dreier return -EINVAL; 221225c7b1fSRoland Dreier 222225c7b1fSRoland Dreier if (attr_mask & IB_SRQ_LIMIT) { 223225c7b1fSRoland Dreier if (attr->srq_limit >= srq->msrq.max) 224225c7b1fSRoland Dreier return -EINVAL; 225225c7b1fSRoland Dreier 226225c7b1fSRoland Dreier mutex_lock(&srq->mutex); 227225c7b1fSRoland Dreier ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit); 228225c7b1fSRoland Dreier mutex_unlock(&srq->mutex); 229225c7b1fSRoland Dreier 230225c7b1fSRoland Dreier if (ret) 231225c7b1fSRoland Dreier return ret; 232225c7b1fSRoland Dreier } 233225c7b1fSRoland Dreier 234225c7b1fSRoland Dreier return 0; 235225c7b1fSRoland Dreier } 236225c7b1fSRoland Dreier 23765541cb7SJack Morgenstein int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) 23865541cb7SJack Morgenstein { 23965541cb7SJack Morgenstein struct mlx4_ib_dev *dev = to_mdev(ibsrq->device); 24065541cb7SJack Morgenstein struct mlx4_ib_srq *srq = to_msrq(ibsrq); 24165541cb7SJack Morgenstein int ret; 24265541cb7SJack Morgenstein int limit_watermark; 24365541cb7SJack Morgenstein 24465541cb7SJack Morgenstein ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark); 24565541cb7SJack Morgenstein if (ret) 24665541cb7SJack Morgenstein return ret; 24765541cb7SJack Morgenstein 248d7dc3ccbSRoland Dreier srq_attr->srq_limit = limit_watermark; 24965541cb7SJack Morgenstein srq_attr->max_wr = srq->msrq.max - 1; 25065541cb7SJack Morgenstein srq_attr->max_sge = srq->msrq.max_gs; 25165541cb7SJack Morgenstein 25265541cb7SJack Morgenstein return 0; 25365541cb7SJack Morgenstein } 25465541cb7SJack Morgenstein 255225c7b1fSRoland Dreier int mlx4_ib_destroy_srq(struct ib_srq *srq) 256225c7b1fSRoland Dreier { 257225c7b1fSRoland Dreier struct mlx4_ib_dev *dev = to_mdev(srq->device); 258225c7b1fSRoland Dreier struct mlx4_ib_srq *msrq = to_msrq(srq); 259225c7b1fSRoland Dreier 260225c7b1fSRoland Dreier mlx4_srq_free(dev->dev, &msrq->msrq); 261225c7b1fSRoland Dreier mlx4_mtt_cleanup(dev->dev, &msrq->mtt); 262225c7b1fSRoland Dreier 263225c7b1fSRoland Dreier if (srq->uobject) { 264225c7b1fSRoland Dreier mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db); 265225c7b1fSRoland Dreier ib_umem_release(msrq->umem); 266225c7b1fSRoland Dreier } else { 267225c7b1fSRoland Dreier kfree(msrq->wrid); 268225c7b1fSRoland Dreier mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift, 269225c7b1fSRoland Dreier &msrq->buf); 2706296883cSYevgeny Petrilin mlx4_db_free(dev->dev, &msrq->db); 271225c7b1fSRoland Dreier } 272225c7b1fSRoland Dreier 273225c7b1fSRoland Dreier kfree(msrq); 274225c7b1fSRoland Dreier 275225c7b1fSRoland Dreier return 0; 276225c7b1fSRoland Dreier } 277225c7b1fSRoland Dreier 278225c7b1fSRoland Dreier void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index) 279225c7b1fSRoland Dreier { 280225c7b1fSRoland Dreier struct mlx4_wqe_srq_next_seg *next; 281225c7b1fSRoland Dreier 282225c7b1fSRoland Dreier /* always called with interrupts disabled. */ 283225c7b1fSRoland Dreier spin_lock(&srq->lock); 284225c7b1fSRoland Dreier 285225c7b1fSRoland Dreier next = get_wqe(srq, srq->tail); 286225c7b1fSRoland Dreier next->next_wqe_index = cpu_to_be16(wqe_index); 287225c7b1fSRoland Dreier srq->tail = wqe_index; 288225c7b1fSRoland Dreier 289225c7b1fSRoland Dreier spin_unlock(&srq->lock); 290225c7b1fSRoland Dreier } 291225c7b1fSRoland Dreier 292225c7b1fSRoland Dreier int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, 293225c7b1fSRoland Dreier struct ib_recv_wr **bad_wr) 294225c7b1fSRoland Dreier { 295225c7b1fSRoland Dreier struct mlx4_ib_srq *srq = to_msrq(ibsrq); 296225c7b1fSRoland Dreier struct mlx4_wqe_srq_next_seg *next; 297225c7b1fSRoland Dreier struct mlx4_wqe_data_seg *scat; 298225c7b1fSRoland Dreier unsigned long flags; 299225c7b1fSRoland Dreier int err = 0; 300225c7b1fSRoland Dreier int nreq; 301225c7b1fSRoland Dreier int i; 302225c7b1fSRoland Dreier 303225c7b1fSRoland Dreier spin_lock_irqsave(&srq->lock, flags); 304225c7b1fSRoland Dreier 305225c7b1fSRoland Dreier for (nreq = 0; wr; ++nreq, wr = wr->next) { 306225c7b1fSRoland Dreier if (unlikely(wr->num_sge > srq->msrq.max_gs)) { 307225c7b1fSRoland Dreier err = -EINVAL; 308225c7b1fSRoland Dreier *bad_wr = wr; 309225c7b1fSRoland Dreier break; 310225c7b1fSRoland Dreier } 311225c7b1fSRoland Dreier 31256a8c8b6SRoland Dreier if (unlikely(srq->head == srq->tail)) { 31356a8c8b6SRoland Dreier err = -ENOMEM; 31456a8c8b6SRoland Dreier *bad_wr = wr; 31556a8c8b6SRoland Dreier break; 31656a8c8b6SRoland Dreier } 31756a8c8b6SRoland Dreier 318225c7b1fSRoland Dreier srq->wrid[srq->head] = wr->wr_id; 319225c7b1fSRoland Dreier 320225c7b1fSRoland Dreier next = get_wqe(srq, srq->head); 321225c7b1fSRoland Dreier srq->head = be16_to_cpu(next->next_wqe_index); 322225c7b1fSRoland Dreier scat = (struct mlx4_wqe_data_seg *) (next + 1); 323225c7b1fSRoland Dreier 324225c7b1fSRoland Dreier for (i = 0; i < wr->num_sge; ++i) { 325225c7b1fSRoland Dreier scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length); 326225c7b1fSRoland Dreier scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey); 327225c7b1fSRoland Dreier scat[i].addr = cpu_to_be64(wr->sg_list[i].addr); 328225c7b1fSRoland Dreier } 329225c7b1fSRoland Dreier 330225c7b1fSRoland Dreier if (i < srq->msrq.max_gs) { 331225c7b1fSRoland Dreier scat[i].byte_count = 0; 332225c7b1fSRoland Dreier scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY); 333225c7b1fSRoland Dreier scat[i].addr = 0; 334225c7b1fSRoland Dreier } 335225c7b1fSRoland Dreier } 336225c7b1fSRoland Dreier 337225c7b1fSRoland Dreier if (likely(nreq)) { 338225c7b1fSRoland Dreier srq->wqe_ctr += nreq; 339225c7b1fSRoland Dreier 340225c7b1fSRoland Dreier /* 341225c7b1fSRoland Dreier * Make sure that descriptors are written before 342225c7b1fSRoland Dreier * doorbell record. 343225c7b1fSRoland Dreier */ 344225c7b1fSRoland Dreier wmb(); 345225c7b1fSRoland Dreier 346225c7b1fSRoland Dreier *srq->db.db = cpu_to_be32(srq->wqe_ctr); 347225c7b1fSRoland Dreier } 348225c7b1fSRoland Dreier 349225c7b1fSRoland Dreier spin_unlock_irqrestore(&srq->lock, flags); 350225c7b1fSRoland Dreier 351225c7b1fSRoland Dreier return err; 352225c7b1fSRoland Dreier } 353