163fa15dbSBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 28700e3e7SMoni Shoua /* 38700e3e7SMoni Shoua * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 48700e3e7SMoni Shoua * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 58700e3e7SMoni Shoua */ 68700e3e7SMoni Shoua 70bbb3b74SBart Van Assche #include <linux/dma-mapping.h> 84d6f2859SYuval Shaia #include <net/addrconf.h> 989944450SShamir Rabinovitch #include <rdma/uverbs_ioctl.h> 100b1fbfb9SBob Pearson 118700e3e7SMoni Shoua #include "rxe.h" 128700e3e7SMoni Shoua #include "rxe_queue.h" 130b1e5b99SYonatan Cohen #include "rxe_hw_counters.h" 148700e3e7SMoni Shoua 158700e3e7SMoni Shoua static int rxe_query_device(struct ib_device *dev, 168700e3e7SMoni Shoua struct ib_device_attr *attr, 178700e3e7SMoni Shoua struct ib_udata *uhw) 188700e3e7SMoni Shoua { 198700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(dev); 208700e3e7SMoni Shoua 218700e3e7SMoni Shoua if (uhw->inlen || uhw->outlen) 228700e3e7SMoni Shoua return -EINVAL; 238700e3e7SMoni Shoua 248700e3e7SMoni Shoua *attr = rxe->attr; 258700e3e7SMoni Shoua return 0; 268700e3e7SMoni Shoua } 278700e3e7SMoni Shoua 288700e3e7SMoni Shoua static int rxe_query_port(struct ib_device *dev, 291fb7f897SMark Bloch u32 port_num, struct ib_port_attr *attr) 308700e3e7SMoni Shoua { 318700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(dev); 3259590b8aSYuval Shaia int rc; 338700e3e7SMoni Shoua 34c4550c63SOr Gerlitz /* *attr being zeroed by the caller, avoid zeroing it here */ 35d12faf2dSZhu Yanjun *attr = rxe->port.attr; 368700e3e7SMoni Shoua 378700e3e7SMoni Shoua mutex_lock(&rxe->usdev_lock); 38d4186194SYuval Shaia rc = ib_get_eth_speed(dev, port_num, &attr->active_speed, 39e404f945SParav Pandit &attr->active_width); 405736c7c4SAndrew Boyer 415736c7c4SAndrew Boyer if (attr->state == IB_PORT_ACTIVE) 4272a7720fSKamal Heib attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 435736c7c4SAndrew Boyer else if (dev_get_flags(rxe->ndev) & IFF_UP) 4472a7720fSKamal Heib attr->phys_state = IB_PORT_PHYS_STATE_POLLING; 455736c7c4SAndrew Boyer else 4672a7720fSKamal Heib attr->phys_state = IB_PORT_PHYS_STATE_DISABLED; 475736c7c4SAndrew Boyer 488700e3e7SMoni Shoua mutex_unlock(&rxe->usdev_lock); 498700e3e7SMoni Shoua 50d4186194SYuval Shaia return rc; 518700e3e7SMoni Shoua } 528700e3e7SMoni Shoua 538700e3e7SMoni Shoua static int rxe_query_pkey(struct ib_device *device, 541fb7f897SMark Bloch u32 port_num, u16 index, u16 *pkey) 558700e3e7SMoni Shoua { 5676251e15SKamal Heib if (index > 0) 578700e3e7SMoni Shoua return -EINVAL; 5876251e15SKamal Heib 5976251e15SKamal Heib *pkey = IB_DEFAULT_PKEY_FULL; 6076251e15SKamal Heib return 0; 618700e3e7SMoni Shoua } 628700e3e7SMoni Shoua 638700e3e7SMoni Shoua static int rxe_modify_device(struct ib_device *dev, 648700e3e7SMoni Shoua int mask, struct ib_device_modify *attr) 658700e3e7SMoni Shoua { 668700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(dev); 678700e3e7SMoni Shoua 68f3fceba5SKamal Heib if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID | 69f3fceba5SKamal Heib IB_DEVICE_MODIFY_NODE_DESC)) 70f3fceba5SKamal Heib return -EOPNOTSUPP; 71f3fceba5SKamal Heib 728700e3e7SMoni Shoua if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) 738700e3e7SMoni Shoua rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid); 748700e3e7SMoni Shoua 758700e3e7SMoni Shoua if (mask & IB_DEVICE_MODIFY_NODE_DESC) { 768700e3e7SMoni Shoua memcpy(rxe->ib_dev.node_desc, 778700e3e7SMoni Shoua attr->node_desc, sizeof(rxe->ib_dev.node_desc)); 788700e3e7SMoni Shoua } 798700e3e7SMoni Shoua 808700e3e7SMoni Shoua return 0; 818700e3e7SMoni Shoua } 828700e3e7SMoni Shoua 838700e3e7SMoni Shoua static int rxe_modify_port(struct ib_device *dev, 841fb7f897SMark Bloch u32 port_num, int mask, struct ib_port_modify *attr) 858700e3e7SMoni Shoua { 868700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(dev); 878700e3e7SMoni Shoua struct rxe_port *port; 888700e3e7SMoni Shoua 898700e3e7SMoni Shoua port = &rxe->port; 908700e3e7SMoni Shoua 918700e3e7SMoni Shoua port->attr.port_cap_flags |= attr->set_port_cap_mask; 928700e3e7SMoni Shoua port->attr.port_cap_flags &= ~attr->clr_port_cap_mask; 938700e3e7SMoni Shoua 948700e3e7SMoni Shoua if (mask & IB_PORT_RESET_QKEY_CNTR) 958700e3e7SMoni Shoua port->attr.qkey_viol_cntr = 0; 968700e3e7SMoni Shoua 978700e3e7SMoni Shoua return 0; 988700e3e7SMoni Shoua } 998700e3e7SMoni Shoua 1008700e3e7SMoni Shoua static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev, 1011fb7f897SMark Bloch u32 port_num) 1028700e3e7SMoni Shoua { 103420bd9e2SKamal Heib return IB_LINK_LAYER_ETHERNET; 1048700e3e7SMoni Shoua } 1058700e3e7SMoni Shoua 10691a42c5bSBob Pearson static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata) 1078700e3e7SMoni Shoua { 10891a42c5bSBob Pearson struct rxe_dev *rxe = to_rdev(ibuc->device); 10991a42c5bSBob Pearson struct rxe_ucontext *uc = to_ruc(ibuc); 1108700e3e7SMoni Shoua 11191a42c5bSBob Pearson return rxe_add_to_pool(&rxe->uc_pool, uc); 1128700e3e7SMoni Shoua } 1138700e3e7SMoni Shoua 114a2a074efSLeon Romanovsky static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) 1158700e3e7SMoni Shoua { 1168700e3e7SMoni Shoua struct rxe_ucontext *uc = to_ruc(ibuc); 1178700e3e7SMoni Shoua 118215d0a75SBob Pearson rxe_cleanup(uc); 1198700e3e7SMoni Shoua } 1208700e3e7SMoni Shoua 1211fb7f897SMark Bloch static int rxe_port_immutable(struct ib_device *dev, u32 port_num, 1228700e3e7SMoni Shoua struct ib_port_immutable *immutable) 1238700e3e7SMoni Shoua { 1248700e3e7SMoni Shoua int err; 1258700e3e7SMoni Shoua struct ib_port_attr attr; 1268700e3e7SMoni Shoua 127c4550c63SOr Gerlitz immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 128c4550c63SOr Gerlitz 129c4550c63SOr Gerlitz err = ib_query_port(dev, port_num, &attr); 1308700e3e7SMoni Shoua if (err) 1318700e3e7SMoni Shoua return err; 1328700e3e7SMoni Shoua 1338700e3e7SMoni Shoua immutable->pkey_tbl_len = attr.pkey_tbl_len; 1348700e3e7SMoni Shoua immutable->gid_tbl_len = attr.gid_tbl_len; 1358700e3e7SMoni Shoua immutable->max_mad_size = IB_MGMT_MAD_SIZE; 1368700e3e7SMoni Shoua 1378700e3e7SMoni Shoua return 0; 1388700e3e7SMoni Shoua } 1398700e3e7SMoni Shoua 140ff23dfa1SShamir Rabinovitch static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 1418700e3e7SMoni Shoua { 14221a428a0SLeon Romanovsky struct rxe_dev *rxe = to_rdev(ibpd->device); 14321a428a0SLeon Romanovsky struct rxe_pd *pd = to_rpd(ibpd); 1448700e3e7SMoni Shoua 14591a42c5bSBob Pearson return rxe_add_to_pool(&rxe->pd_pool, pd); 1468700e3e7SMoni Shoua } 1478700e3e7SMoni Shoua 14891a7c58fSLeon Romanovsky static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 1498700e3e7SMoni Shoua { 1508700e3e7SMoni Shoua struct rxe_pd *pd = to_rpd(ibpd); 1518700e3e7SMoni Shoua 152215d0a75SBob Pearson rxe_cleanup(pd); 15391a7c58fSLeon Romanovsky return 0; 1548700e3e7SMoni Shoua } 1558700e3e7SMoni Shoua 156fa5d010cSMaor Gottlieb static int rxe_create_ah(struct ib_ah *ibah, 157fa5d010cSMaor Gottlieb struct rdma_ah_init_attr *init_attr, 158fa5d010cSMaor Gottlieb struct ib_udata *udata) 159477864c8SMoni Shoua 1608700e3e7SMoni Shoua { 161d3456914SLeon Romanovsky struct rxe_dev *rxe = to_rdev(ibah->device); 162d3456914SLeon Romanovsky struct rxe_ah *ah = to_rah(ibah); 16373a54932SBob Pearson struct rxe_create_ah_resp __user *uresp = NULL; 16473a54932SBob Pearson int err; 16573a54932SBob Pearson 16673a54932SBob Pearson if (udata) { 16773a54932SBob Pearson /* test if new user provider */ 16873a54932SBob Pearson if (udata->outlen >= sizeof(*uresp)) 16973a54932SBob Pearson uresp = udata->outbuf; 17073a54932SBob Pearson ah->is_user = true; 17173a54932SBob Pearson } else { 17273a54932SBob Pearson ah->is_user = false; 17373a54932SBob Pearson } 1748700e3e7SMoni Shoua 175215d0a75SBob Pearson err = rxe_add_to_pool_ah(&rxe->ah_pool, ah, 176215d0a75SBob Pearson init_attr->flags & RDMA_CREATE_AH_SLEEPABLE); 177d3456914SLeon Romanovsky if (err) 178d3456914SLeon Romanovsky return err; 1798700e3e7SMoni Shoua 18073a54932SBob Pearson /* create index > 0 */ 18102827b67SBob Pearson ah->ah_num = ah->elem.index; 18273a54932SBob Pearson 18325fd735aSBob Pearson err = rxe_ah_chk_attr(ah, init_attr->ah_attr); 18425fd735aSBob Pearson if (err) { 18525fd735aSBob Pearson rxe_cleanup(ah); 18625fd735aSBob Pearson return err; 18725fd735aSBob Pearson } 18825fd735aSBob Pearson 18973a54932SBob Pearson if (uresp) { 19073a54932SBob Pearson /* only if new user provider */ 19173a54932SBob Pearson err = copy_to_user(&uresp->ah_num, &ah->ah_num, 19273a54932SBob Pearson sizeof(uresp->ah_num)); 19373a54932SBob Pearson if (err) { 194215d0a75SBob Pearson rxe_cleanup(ah); 19573a54932SBob Pearson return -EFAULT; 19673a54932SBob Pearson } 19773a54932SBob Pearson } else if (ah->is_user) { 19873a54932SBob Pearson /* only if old user provider */ 19973a54932SBob Pearson ah->ah_num = 0; 20073a54932SBob Pearson } 20173a54932SBob Pearson 202fa5d010cSMaor Gottlieb rxe_init_av(init_attr->ah_attr, &ah->av); 203215d0a75SBob Pearson rxe_finalize(ah); 204215d0a75SBob Pearson 205d3456914SLeon Romanovsky return 0; 2068700e3e7SMoni Shoua } 2078700e3e7SMoni Shoua 20890898850SDasaratharaman Chandramouli static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr) 2098700e3e7SMoni Shoua { 2108700e3e7SMoni Shoua int err; 2118700e3e7SMoni Shoua struct rxe_ah *ah = to_rah(ibah); 2128700e3e7SMoni Shoua 21325fd735aSBob Pearson err = rxe_ah_chk_attr(ah, attr); 2148700e3e7SMoni Shoua if (err) 2158700e3e7SMoni Shoua return err; 2168700e3e7SMoni Shoua 217fa407188SKamal Heib rxe_init_av(attr, &ah->av); 2188700e3e7SMoni Shoua return 0; 2198700e3e7SMoni Shoua } 2208700e3e7SMoni Shoua 22190898850SDasaratharaman Chandramouli static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr) 2228700e3e7SMoni Shoua { 2238700e3e7SMoni Shoua struct rxe_ah *ah = to_rah(ibah); 2248700e3e7SMoni Shoua 225eca7ddf9SDasaratharaman Chandramouli memset(attr, 0, sizeof(*attr)); 22644c58487SDasaratharaman Chandramouli attr->type = ibah->type; 2279c96f3d4SZhu Yanjun rxe_av_to_attr(&ah->av, attr); 2288700e3e7SMoni Shoua return 0; 2298700e3e7SMoni Shoua } 2308700e3e7SMoni Shoua 2319a9ebf8cSLeon Romanovsky static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags) 2328700e3e7SMoni Shoua { 2338700e3e7SMoni Shoua struct rxe_ah *ah = to_rah(ibah); 2348700e3e7SMoni Shoua 235215d0a75SBob Pearson rxe_cleanup_ah(ah, flags & RDMA_DESTROY_AH_SLEEPABLE); 236215d0a75SBob Pearson 2379a9ebf8cSLeon Romanovsky return 0; 2388700e3e7SMoni Shoua } 2398700e3e7SMoni Shoua 240d34ac5cdSBart Van Assche static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr) 2418700e3e7SMoni Shoua { 2428700e3e7SMoni Shoua int i; 2438700e3e7SMoni Shoua u32 length; 2448700e3e7SMoni Shoua struct rxe_recv_wqe *recv_wqe; 2458700e3e7SMoni Shoua int num_sge = ibwr->num_sge; 2465bcf5a59SBob Pearson int full; 2478700e3e7SMoni Shoua 248ae6e843fSBob Pearson full = queue_full(rq->queue, QUEUE_TYPE_TO_DRIVER); 249692373d1SYunsheng Lin if (unlikely(full)) 250692373d1SYunsheng Lin return -ENOMEM; 2518700e3e7SMoni Shoua 252692373d1SYunsheng Lin if (unlikely(num_sge > rq->max_sge)) 253692373d1SYunsheng Lin return -EINVAL; 2548700e3e7SMoni Shoua 2558700e3e7SMoni Shoua length = 0; 2568700e3e7SMoni Shoua for (i = 0; i < num_sge; i++) 2578700e3e7SMoni Shoua length += ibwr->sg_list[i].length; 2588700e3e7SMoni Shoua 259ae6e843fSBob Pearson recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_TO_DRIVER); 2608700e3e7SMoni Shoua recv_wqe->wr_id = ibwr->wr_id; 2618700e3e7SMoni Shoua 2628700e3e7SMoni Shoua memcpy(recv_wqe->dma.sge, ibwr->sg_list, 2638700e3e7SMoni Shoua num_sge * sizeof(struct ib_sge)); 2648700e3e7SMoni Shoua 2658700e3e7SMoni Shoua recv_wqe->dma.length = length; 2668700e3e7SMoni Shoua recv_wqe->dma.resid = length; 2678700e3e7SMoni Shoua recv_wqe->dma.num_sge = num_sge; 2688700e3e7SMoni Shoua recv_wqe->dma.cur_sge = 0; 2698700e3e7SMoni Shoua recv_wqe->dma.sge_offset = 0; 2708700e3e7SMoni Shoua 271ae6e843fSBob Pearson queue_advance_producer(rq->queue, QUEUE_TYPE_TO_DRIVER); 2725bcf5a59SBob Pearson 2738700e3e7SMoni Shoua return 0; 2748700e3e7SMoni Shoua } 2758700e3e7SMoni Shoua 27668e326deSLeon Romanovsky static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init, 2778700e3e7SMoni Shoua struct ib_udata *udata) 2788700e3e7SMoni Shoua { 2798700e3e7SMoni Shoua int err; 28068e326deSLeon Romanovsky struct rxe_dev *rxe = to_rdev(ibsrq->device); 28168e326deSLeon Romanovsky struct rxe_pd *pd = to_rpd(ibsrq->pd); 28268e326deSLeon Romanovsky struct rxe_srq *srq = to_rsrq(ibsrq); 2830c43ab37SJason Gunthorpe struct rxe_create_srq_resp __user *uresp = NULL; 2840c43ab37SJason Gunthorpe 2850c43ab37SJason Gunthorpe if (udata) { 2860c43ab37SJason Gunthorpe if (udata->outlen < sizeof(*uresp)) 28768e326deSLeon Romanovsky return -EINVAL; 2880c43ab37SJason Gunthorpe uresp = udata->outbuf; 2890c43ab37SJason Gunthorpe } 2908700e3e7SMoni Shoua 291b2a41678SBob Pearson if (init->srq_type != IB_SRQT_BASIC) 292b2a41678SBob Pearson return -EOPNOTSUPP; 293b2a41678SBob Pearson 2940b1fbfb9SBob Pearson err = rxe_srq_chk_init(rxe, init); 2958700e3e7SMoni Shoua if (err) 296b2a41678SBob Pearson return err; 2978700e3e7SMoni Shoua 29891a42c5bSBob Pearson err = rxe_add_to_pool(&rxe->srq_pool, srq); 29968e326deSLeon Romanovsky if (err) 300b2a41678SBob Pearson return err; 3018700e3e7SMoni Shoua 3023197706aSBob Pearson rxe_get(pd); 3038700e3e7SMoni Shoua srq->pd = pd; 3048700e3e7SMoni Shoua 305ff23dfa1SShamir Rabinovitch err = rxe_srq_from_init(rxe, srq, init, udata, uresp); 3068700e3e7SMoni Shoua if (err) 307215d0a75SBob Pearson goto err_cleanup; 3088700e3e7SMoni Shoua 30968e326deSLeon Romanovsky return 0; 3108700e3e7SMoni Shoua 311215d0a75SBob Pearson err_cleanup: 312215d0a75SBob Pearson rxe_cleanup(srq); 313215d0a75SBob Pearson 31468e326deSLeon Romanovsky return err; 3158700e3e7SMoni Shoua } 3168700e3e7SMoni Shoua 3178700e3e7SMoni Shoua static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr, 3188700e3e7SMoni Shoua enum ib_srq_attr_mask mask, 3198700e3e7SMoni Shoua struct ib_udata *udata) 3208700e3e7SMoni Shoua { 3218700e3e7SMoni Shoua int err; 3228700e3e7SMoni Shoua struct rxe_srq *srq = to_rsrq(ibsrq); 3238700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ibsrq->device); 3240c43ab37SJason Gunthorpe struct rxe_modify_srq_cmd ucmd = {}; 3250c43ab37SJason Gunthorpe 3260c43ab37SJason Gunthorpe if (udata) { 3270c43ab37SJason Gunthorpe if (udata->inlen < sizeof(ucmd)) 3280c43ab37SJason Gunthorpe return -EINVAL; 3290c43ab37SJason Gunthorpe 3300c43ab37SJason Gunthorpe err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)); 3310c43ab37SJason Gunthorpe if (err) 3320c43ab37SJason Gunthorpe return err; 3330c43ab37SJason Gunthorpe } 3348700e3e7SMoni Shoua 3358700e3e7SMoni Shoua err = rxe_srq_chk_attr(rxe, srq, attr, mask); 3368700e3e7SMoni Shoua if (err) 337b2a41678SBob Pearson return err; 3388700e3e7SMoni Shoua 339692373d1SYunsheng Lin return rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata); 3408700e3e7SMoni Shoua } 3418700e3e7SMoni Shoua 3428700e3e7SMoni Shoua static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr) 3438700e3e7SMoni Shoua { 3448700e3e7SMoni Shoua struct rxe_srq *srq = to_rsrq(ibsrq); 3458700e3e7SMoni Shoua 3468700e3e7SMoni Shoua if (srq->error) 3478700e3e7SMoni Shoua return -EINVAL; 3488700e3e7SMoni Shoua 3498700e3e7SMoni Shoua attr->max_wr = srq->rq.queue->buf->index_mask; 3508700e3e7SMoni Shoua attr->max_sge = srq->rq.max_sge; 3518700e3e7SMoni Shoua attr->srq_limit = srq->limit; 3528700e3e7SMoni Shoua return 0; 3538700e3e7SMoni Shoua } 3548700e3e7SMoni Shoua 355119181d1SLeon Romanovsky static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) 3568700e3e7SMoni Shoua { 3578700e3e7SMoni Shoua struct rxe_srq *srq = to_rsrq(ibsrq); 3588700e3e7SMoni Shoua 359215d0a75SBob Pearson rxe_cleanup(srq); 360119181d1SLeon Romanovsky return 0; 3618700e3e7SMoni Shoua } 3628700e3e7SMoni Shoua 363d34ac5cdSBart Van Assche static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr, 364d34ac5cdSBart Van Assche const struct ib_recv_wr **bad_wr) 3658700e3e7SMoni Shoua { 3668700e3e7SMoni Shoua int err = 0; 3678700e3e7SMoni Shoua struct rxe_srq *srq = to_rsrq(ibsrq); 368a099b085SBob Pearson unsigned long flags; 3698700e3e7SMoni Shoua 370a099b085SBob Pearson spin_lock_irqsave(&srq->rq.producer_lock, flags); 3718700e3e7SMoni Shoua 3728700e3e7SMoni Shoua while (wr) { 3738700e3e7SMoni Shoua err = post_one_recv(&srq->rq, wr); 3748700e3e7SMoni Shoua if (unlikely(err)) 3758700e3e7SMoni Shoua break; 3768700e3e7SMoni Shoua wr = wr->next; 3778700e3e7SMoni Shoua } 3788700e3e7SMoni Shoua 379a099b085SBob Pearson spin_unlock_irqrestore(&srq->rq.producer_lock, flags); 3808700e3e7SMoni Shoua 3818700e3e7SMoni Shoua if (err) 3828700e3e7SMoni Shoua *bad_wr = wr; 3838700e3e7SMoni Shoua 3848700e3e7SMoni Shoua return err; 3858700e3e7SMoni Shoua } 3868700e3e7SMoni Shoua 387514aee66SLeon Romanovsky static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, 3888700e3e7SMoni Shoua struct ib_udata *udata) 3898700e3e7SMoni Shoua { 3908700e3e7SMoni Shoua int err; 391514aee66SLeon Romanovsky struct rxe_dev *rxe = to_rdev(ibqp->device); 392514aee66SLeon Romanovsky struct rxe_pd *pd = to_rpd(ibqp->pd); 393514aee66SLeon Romanovsky struct rxe_qp *qp = to_rqp(ibqp); 3940c43ab37SJason Gunthorpe struct rxe_create_qp_resp __user *uresp = NULL; 3950c43ab37SJason Gunthorpe 3960c43ab37SJason Gunthorpe if (udata) { 3970c43ab37SJason Gunthorpe if (udata->outlen < sizeof(*uresp)) 398514aee66SLeon Romanovsky return -EINVAL; 3990c43ab37SJason Gunthorpe uresp = udata->outbuf; 4000c43ab37SJason Gunthorpe } 4018700e3e7SMoni Shoua 4021f11a761SJason Gunthorpe if (init->create_flags) 403514aee66SLeon Romanovsky return -EOPNOTSUPP; 4041f11a761SJason Gunthorpe 4058700e3e7SMoni Shoua err = rxe_qp_chk_init(rxe, init); 4068700e3e7SMoni Shoua if (err) 407514aee66SLeon Romanovsky return err; 4088700e3e7SMoni Shoua 4098700e3e7SMoni Shoua if (udata) { 410514aee66SLeon Romanovsky if (udata->inlen) 411514aee66SLeon Romanovsky return -EINVAL; 412514aee66SLeon Romanovsky 4135bcf5a59SBob Pearson qp->is_user = true; 4145bcf5a59SBob Pearson } else { 4155bcf5a59SBob Pearson qp->is_user = false; 4168700e3e7SMoni Shoua } 4178700e3e7SMoni Shoua 418514aee66SLeon Romanovsky err = rxe_add_to_pool(&rxe->qp_pool, qp); 4198700e3e7SMoni Shoua if (err) 420514aee66SLeon Romanovsky return err; 4218700e3e7SMoni Shoua 422514aee66SLeon Romanovsky err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); 423514aee66SLeon Romanovsky if (err) 424514aee66SLeon Romanovsky goto qp_init; 4258700e3e7SMoni Shoua 426215d0a75SBob Pearson rxe_finalize(qp); 427514aee66SLeon Romanovsky return 0; 428514aee66SLeon Romanovsky 429514aee66SLeon Romanovsky qp_init: 430215d0a75SBob Pearson rxe_cleanup(qp); 431514aee66SLeon Romanovsky return err; 4328700e3e7SMoni Shoua } 4338700e3e7SMoni Shoua 4348700e3e7SMoni Shoua static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 4358700e3e7SMoni Shoua int mask, struct ib_udata *udata) 4368700e3e7SMoni Shoua { 4378700e3e7SMoni Shoua int err; 4388700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ibqp->device); 4398700e3e7SMoni Shoua struct rxe_qp *qp = to_rqp(ibqp); 4408700e3e7SMoni Shoua 44126e990baSJason Gunthorpe if (mask & ~IB_QP_ATTR_STANDARD_BITS) 44226e990baSJason Gunthorpe return -EOPNOTSUPP; 44326e990baSJason Gunthorpe 4448700e3e7SMoni Shoua err = rxe_qp_chk_attr(rxe, qp, attr, mask); 4458700e3e7SMoni Shoua if (err) 446692373d1SYunsheng Lin return err; 4478700e3e7SMoni Shoua 4488700e3e7SMoni Shoua err = rxe_qp_from_attr(qp, attr, mask, udata); 4498700e3e7SMoni Shoua if (err) 450692373d1SYunsheng Lin return err; 4518700e3e7SMoni Shoua 452104f062fSZhu Yanjun if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH)) 453104f062fSZhu Yanjun qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label, 454104f062fSZhu Yanjun qp->ibqp.qp_num, 455104f062fSZhu Yanjun qp->attr.dest_qp_num); 456104f062fSZhu Yanjun 4578700e3e7SMoni Shoua return 0; 4588700e3e7SMoni Shoua } 4598700e3e7SMoni Shoua 4608700e3e7SMoni Shoua static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 4618700e3e7SMoni Shoua int mask, struct ib_qp_init_attr *init) 4628700e3e7SMoni Shoua { 4638700e3e7SMoni Shoua struct rxe_qp *qp = to_rqp(ibqp); 4648700e3e7SMoni Shoua 4658700e3e7SMoni Shoua rxe_qp_to_init(qp, init); 4668700e3e7SMoni Shoua rxe_qp_to_attr(qp, attr, mask); 4678700e3e7SMoni Shoua 4688700e3e7SMoni Shoua return 0; 4698700e3e7SMoni Shoua } 4708700e3e7SMoni Shoua 471c4367a26SShamir Rabinovitch static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) 4728700e3e7SMoni Shoua { 4738700e3e7SMoni Shoua struct rxe_qp *qp = to_rqp(ibqp); 474f9f48460SBob Pearson int ret; 475f9f48460SBob Pearson 476f9f48460SBob Pearson ret = rxe_qp_chk_destroy(qp); 477f9f48460SBob Pearson if (ret) 478f9f48460SBob Pearson return ret; 4798700e3e7SMoni Shoua 480215d0a75SBob Pearson rxe_cleanup(qp); 4818700e3e7SMoni Shoua return 0; 4828700e3e7SMoni Shoua } 4838700e3e7SMoni Shoua 484f696bf6dSBart Van Assche static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 4858700e3e7SMoni Shoua unsigned int mask, unsigned int length) 4868700e3e7SMoni Shoua { 4878700e3e7SMoni Shoua int num_sge = ibwr->num_sge; 4888700e3e7SMoni Shoua struct rxe_sq *sq = &qp->sq; 4898700e3e7SMoni Shoua 4908700e3e7SMoni Shoua if (unlikely(num_sge > sq->max_sge)) 491692373d1SYunsheng Lin return -EINVAL; 4928700e3e7SMoni Shoua 4938700e3e7SMoni Shoua if (unlikely(mask & WR_ATOMIC_MASK)) { 4948700e3e7SMoni Shoua if (length < 8) 495692373d1SYunsheng Lin return -EINVAL; 4968700e3e7SMoni Shoua 4978700e3e7SMoni Shoua if (atomic_wr(ibwr)->remote_addr & 0x7) 498692373d1SYunsheng Lin return -EINVAL; 4998700e3e7SMoni Shoua } 5008700e3e7SMoni Shoua 5018700e3e7SMoni Shoua if (unlikely((ibwr->send_flags & IB_SEND_INLINE) && 5028700e3e7SMoni Shoua (length > sq->max_inline))) 503692373d1SYunsheng Lin return -EINVAL; 5048700e3e7SMoni Shoua 5058700e3e7SMoni Shoua return 0; 5068700e3e7SMoni Shoua } 5078700e3e7SMoni Shoua 5088700e3e7SMoni Shoua static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr, 509f696bf6dSBart Van Assche const struct ib_send_wr *ibwr) 5108700e3e7SMoni Shoua { 5118700e3e7SMoni Shoua wr->wr_id = ibwr->wr_id; 5128700e3e7SMoni Shoua wr->opcode = ibwr->opcode; 5138700e3e7SMoni Shoua wr->send_flags = ibwr->send_flags; 5148700e3e7SMoni Shoua 5158700e3e7SMoni Shoua if (qp_type(qp) == IB_QPT_UD || 5168700e3e7SMoni Shoua qp_type(qp) == IB_QPT_GSI) { 5173b87e082SBob Pearson struct ib_ah *ibah = ud_wr(ibwr)->ah; 5183b87e082SBob Pearson 5198700e3e7SMoni Shoua wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn; 5208700e3e7SMoni Shoua wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey; 5213b87e082SBob Pearson wr->wr.ud.ah_num = to_rah(ibah)->ah_num; 5228700e3e7SMoni Shoua if (qp_type(qp) == IB_QPT_GSI) 5238700e3e7SMoni Shoua wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index; 5248700e3e7SMoni Shoua if (wr->opcode == IB_WR_SEND_WITH_IMM) 5258700e3e7SMoni Shoua wr->ex.imm_data = ibwr->ex.imm_data; 5268700e3e7SMoni Shoua } else { 5278700e3e7SMoni Shoua switch (wr->opcode) { 5288700e3e7SMoni Shoua case IB_WR_RDMA_WRITE_WITH_IMM: 5298700e3e7SMoni Shoua wr->ex.imm_data = ibwr->ex.imm_data; 530df561f66SGustavo A. R. Silva fallthrough; 5318700e3e7SMoni Shoua case IB_WR_RDMA_READ: 5328700e3e7SMoni Shoua case IB_WR_RDMA_WRITE: 5338700e3e7SMoni Shoua wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr; 5348700e3e7SMoni Shoua wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey; 5358700e3e7SMoni Shoua break; 5368700e3e7SMoni Shoua case IB_WR_SEND_WITH_IMM: 5378700e3e7SMoni Shoua wr->ex.imm_data = ibwr->ex.imm_data; 5388700e3e7SMoni Shoua break; 5398700e3e7SMoni Shoua case IB_WR_SEND_WITH_INV: 5408700e3e7SMoni Shoua wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 5418700e3e7SMoni Shoua break; 5428700e3e7SMoni Shoua case IB_WR_ATOMIC_CMP_AND_SWP: 5438700e3e7SMoni Shoua case IB_WR_ATOMIC_FETCH_AND_ADD: 5448700e3e7SMoni Shoua wr->wr.atomic.remote_addr = 5458700e3e7SMoni Shoua atomic_wr(ibwr)->remote_addr; 5468700e3e7SMoni Shoua wr->wr.atomic.compare_add = 5478700e3e7SMoni Shoua atomic_wr(ibwr)->compare_add; 5488700e3e7SMoni Shoua wr->wr.atomic.swap = atomic_wr(ibwr)->swap; 5498700e3e7SMoni Shoua wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey; 5508700e3e7SMoni Shoua break; 5518700e3e7SMoni Shoua case IB_WR_LOCAL_INV: 5528700e3e7SMoni Shoua wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey; 5538700e3e7SMoni Shoua break; 5548700e3e7SMoni Shoua case IB_WR_REG_MR: 5558700e3e7SMoni Shoua wr->wr.reg.mr = reg_wr(ibwr)->mr; 5568700e3e7SMoni Shoua wr->wr.reg.key = reg_wr(ibwr)->key; 5578700e3e7SMoni Shoua wr->wr.reg.access = reg_wr(ibwr)->access; 5588700e3e7SMoni Shoua break; 5598700e3e7SMoni Shoua default: 5608700e3e7SMoni Shoua break; 5618700e3e7SMoni Shoua } 5628700e3e7SMoni Shoua } 5638700e3e7SMoni Shoua } 5648700e3e7SMoni Shoua 565086f580cSBob Pearson static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe, 566086f580cSBob Pearson const struct ib_send_wr *ibwr) 567086f580cSBob Pearson { 568086f580cSBob Pearson struct ib_sge *sge = ibwr->sg_list; 569086f580cSBob Pearson u8 *p = wqe->dma.inline_data; 570086f580cSBob Pearson int i; 571086f580cSBob Pearson 572086f580cSBob Pearson for (i = 0; i < ibwr->num_sge; i++, sge++) { 573086f580cSBob Pearson memcpy(p, (void *)(uintptr_t)sge->addr, sge->length); 574086f580cSBob Pearson p += sge->length; 575086f580cSBob Pearson } 576086f580cSBob Pearson } 577086f580cSBob Pearson 578086f580cSBob Pearson static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 5798700e3e7SMoni Shoua unsigned int mask, unsigned int length, 5808700e3e7SMoni Shoua struct rxe_send_wqe *wqe) 5818700e3e7SMoni Shoua { 5828700e3e7SMoni Shoua int num_sge = ibwr->num_sge; 5838700e3e7SMoni Shoua 5848700e3e7SMoni Shoua init_send_wr(qp, &wqe->wr, ibwr); 5858700e3e7SMoni Shoua 586dc78074aSBob Pearson /* local operation */ 587886441fbSBob Pearson if (unlikely(mask & WR_LOCAL_OP_MASK)) { 588dc78074aSBob Pearson wqe->mask = mask; 589dc78074aSBob Pearson wqe->state = wqe_state_posted; 590086f580cSBob Pearson return; 591dc78074aSBob Pearson } 592dc78074aSBob Pearson 593086f580cSBob Pearson if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) 594086f580cSBob Pearson copy_inline_data_to_wqe(wqe, ibwr); 595086f580cSBob Pearson else 5968700e3e7SMoni Shoua memcpy(wqe->dma.sge, ibwr->sg_list, 5978700e3e7SMoni Shoua num_sge * sizeof(struct ib_sge)); 5988700e3e7SMoni Shoua 599a6544a62SBart Van Assche wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr : 600a6544a62SBart Van Assche mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0; 6018700e3e7SMoni Shoua wqe->mask = mask; 6028700e3e7SMoni Shoua wqe->dma.length = length; 6038700e3e7SMoni Shoua wqe->dma.resid = length; 6048700e3e7SMoni Shoua wqe->dma.num_sge = num_sge; 6058700e3e7SMoni Shoua wqe->dma.cur_sge = 0; 6068700e3e7SMoni Shoua wqe->dma.sge_offset = 0; 6078700e3e7SMoni Shoua wqe->state = wqe_state_posted; 6088700e3e7SMoni Shoua wqe->ssn = atomic_add_return(1, &qp->ssn); 6098700e3e7SMoni Shoua } 6108700e3e7SMoni Shoua 611f696bf6dSBart Van Assche static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr, 612e404f945SParav Pandit unsigned int mask, u32 length) 6138700e3e7SMoni Shoua { 6148700e3e7SMoni Shoua int err; 6158700e3e7SMoni Shoua struct rxe_sq *sq = &qp->sq; 6168700e3e7SMoni Shoua struct rxe_send_wqe *send_wqe; 617a099b085SBob Pearson unsigned long flags; 6185bcf5a59SBob Pearson int full; 6198700e3e7SMoni Shoua 6208700e3e7SMoni Shoua err = validate_send_wr(qp, ibwr, mask, length); 6218700e3e7SMoni Shoua if (err) 6228700e3e7SMoni Shoua return err; 6238700e3e7SMoni Shoua 624a099b085SBob Pearson spin_lock_irqsave(&qp->sq.sq_lock, flags); 6258700e3e7SMoni Shoua 626ae6e843fSBob Pearson full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER); 6275bcf5a59SBob Pearson 6285bcf5a59SBob Pearson if (unlikely(full)) { 629a099b085SBob Pearson spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 6305bcf5a59SBob Pearson return -ENOMEM; 6318700e3e7SMoni Shoua } 6328700e3e7SMoni Shoua 633ae6e843fSBob Pearson send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER); 634086f580cSBob Pearson init_send_wqe(qp, ibwr, mask, length, send_wqe); 6358700e3e7SMoni Shoua 636ae6e843fSBob Pearson queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER); 6375bcf5a59SBob Pearson 638a099b085SBob Pearson spin_unlock_irqrestore(&qp->sq.sq_lock, flags); 6398700e3e7SMoni Shoua 6408700e3e7SMoni Shoua return 0; 6418700e3e7SMoni Shoua } 6428700e3e7SMoni Shoua 643d34ac5cdSBart Van Assche static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr, 644d34ac5cdSBart Van Assche const struct ib_send_wr **bad_wr) 6458700e3e7SMoni Shoua { 6468700e3e7SMoni Shoua int err = 0; 6478700e3e7SMoni Shoua unsigned int mask; 6488700e3e7SMoni Shoua unsigned int length = 0; 6498700e3e7SMoni Shoua int i; 6505f0b2a60SMikhail Malygin struct ib_send_wr *next; 6518700e3e7SMoni Shoua 6528700e3e7SMoni Shoua while (wr) { 6538700e3e7SMoni Shoua mask = wr_opcode_mask(wr->opcode, qp); 6548700e3e7SMoni Shoua if (unlikely(!mask)) { 6558700e3e7SMoni Shoua err = -EINVAL; 6568700e3e7SMoni Shoua *bad_wr = wr; 6578700e3e7SMoni Shoua break; 6588700e3e7SMoni Shoua } 6598700e3e7SMoni Shoua 6608700e3e7SMoni Shoua if (unlikely((wr->send_flags & IB_SEND_INLINE) && 6618700e3e7SMoni Shoua !(mask & WR_INLINE_MASK))) { 6628700e3e7SMoni Shoua err = -EINVAL; 6638700e3e7SMoni Shoua *bad_wr = wr; 6648700e3e7SMoni Shoua break; 6658700e3e7SMoni Shoua } 6668700e3e7SMoni Shoua 6675f0b2a60SMikhail Malygin next = wr->next; 6685f0b2a60SMikhail Malygin 6698700e3e7SMoni Shoua length = 0; 6708700e3e7SMoni Shoua for (i = 0; i < wr->num_sge; i++) 6718700e3e7SMoni Shoua length += wr->sg_list[i].length; 6728700e3e7SMoni Shoua 6738700e3e7SMoni Shoua err = post_one_send(qp, wr, mask, length); 6748700e3e7SMoni Shoua 6758700e3e7SMoni Shoua if (err) { 6768700e3e7SMoni Shoua *bad_wr = wr; 6778700e3e7SMoni Shoua break; 6788700e3e7SMoni Shoua } 6795f0b2a60SMikhail Malygin wr = next; 6808700e3e7SMoni Shoua } 6818700e3e7SMoni Shoua 682dccb23f6SBob Pearson rxe_sched_task(&qp->req.task); 6836f301e06SBart Van Assche if (unlikely(qp->req.state == QP_STATE_ERROR)) 684dccb23f6SBob Pearson rxe_sched_task(&qp->comp.task); 6858700e3e7SMoni Shoua 6868700e3e7SMoni Shoua return err; 6878700e3e7SMoni Shoua } 6888700e3e7SMoni Shoua 689d34ac5cdSBart Van Assche static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr, 690d34ac5cdSBart Van Assche const struct ib_send_wr **bad_wr) 691063af595SParav Pandit { 692063af595SParav Pandit struct rxe_qp *qp = to_rqp(ibqp); 693063af595SParav Pandit 694063af595SParav Pandit if (unlikely(!qp->valid)) { 695063af595SParav Pandit *bad_wr = wr; 696063af595SParav Pandit return -EINVAL; 697063af595SParav Pandit } 698063af595SParav Pandit 699063af595SParav Pandit if (unlikely(qp->req.state < QP_STATE_READY)) { 700063af595SParav Pandit *bad_wr = wr; 701063af595SParav Pandit return -EINVAL; 702063af595SParav Pandit } 703063af595SParav Pandit 704063af595SParav Pandit if (qp->is_user) { 705063af595SParav Pandit /* Utilize process context to do protocol processing */ 706dccb23f6SBob Pearson rxe_run_task(&qp->req.task); 707063af595SParav Pandit return 0; 708063af595SParav Pandit } else 709063af595SParav Pandit return rxe_post_send_kernel(qp, wr, bad_wr); 710063af595SParav Pandit } 711063af595SParav Pandit 712d34ac5cdSBart Van Assche static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr, 713d34ac5cdSBart Van Assche const struct ib_recv_wr **bad_wr) 7148700e3e7SMoni Shoua { 7158700e3e7SMoni Shoua int err = 0; 7168700e3e7SMoni Shoua struct rxe_qp *qp = to_rqp(ibqp); 7178700e3e7SMoni Shoua struct rxe_rq *rq = &qp->rq; 718a099b085SBob Pearson unsigned long flags; 7198700e3e7SMoni Shoua 7208700e3e7SMoni Shoua if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) { 7218700e3e7SMoni Shoua *bad_wr = wr; 722692373d1SYunsheng Lin return -EINVAL; 7238700e3e7SMoni Shoua } 7248700e3e7SMoni Shoua 7258700e3e7SMoni Shoua if (unlikely(qp->srq)) { 7268700e3e7SMoni Shoua *bad_wr = wr; 727692373d1SYunsheng Lin return -EINVAL; 7288700e3e7SMoni Shoua } 7298700e3e7SMoni Shoua 730a099b085SBob Pearson spin_lock_irqsave(&rq->producer_lock, flags); 7318700e3e7SMoni Shoua 7328700e3e7SMoni Shoua while (wr) { 7338700e3e7SMoni Shoua err = post_one_recv(rq, wr); 7348700e3e7SMoni Shoua if (unlikely(err)) { 7358700e3e7SMoni Shoua *bad_wr = wr; 7368700e3e7SMoni Shoua break; 7378700e3e7SMoni Shoua } 7388700e3e7SMoni Shoua wr = wr->next; 7398700e3e7SMoni Shoua } 7408700e3e7SMoni Shoua 741a099b085SBob Pearson spin_unlock_irqrestore(&rq->producer_lock, flags); 7428700e3e7SMoni Shoua 74312171971SVijay Immanuel if (qp->resp.state == QP_STATE_ERROR) 744dccb23f6SBob Pearson rxe_sched_task(&qp->resp.task); 74512171971SVijay Immanuel 7468700e3e7SMoni Shoua return err; 7478700e3e7SMoni Shoua } 7488700e3e7SMoni Shoua 749e39afe3dSLeon Romanovsky static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 7508700e3e7SMoni Shoua struct ib_udata *udata) 7518700e3e7SMoni Shoua { 7528700e3e7SMoni Shoua int err; 753e39afe3dSLeon Romanovsky struct ib_device *dev = ibcq->device; 7548700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(dev); 755e39afe3dSLeon Romanovsky struct rxe_cq *cq = to_rcq(ibcq); 7560c43ab37SJason Gunthorpe struct rxe_create_cq_resp __user *uresp = NULL; 7570c43ab37SJason Gunthorpe 7580c43ab37SJason Gunthorpe if (udata) { 7590c43ab37SJason Gunthorpe if (udata->outlen < sizeof(*uresp)) 760e39afe3dSLeon Romanovsky return -EINVAL; 7610c43ab37SJason Gunthorpe uresp = udata->outbuf; 7620c43ab37SJason Gunthorpe } 7638700e3e7SMoni Shoua 7648700e3e7SMoni Shoua if (attr->flags) 7651c407cb5SJason Gunthorpe return -EOPNOTSUPP; 7668700e3e7SMoni Shoua 767b92ec0feSJason Gunthorpe err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector); 7688700e3e7SMoni Shoua if (err) 769e39afe3dSLeon Romanovsky return err; 7708700e3e7SMoni Shoua 771ff23dfa1SShamir Rabinovitch err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata, 772ff23dfa1SShamir Rabinovitch uresp); 7738700e3e7SMoni Shoua if (err) 774e39afe3dSLeon Romanovsky return err; 7758700e3e7SMoni Shoua 77691a42c5bSBob Pearson return rxe_add_to_pool(&rxe->cq_pool, cq); 7778700e3e7SMoni Shoua } 7788700e3e7SMoni Shoua 77943d781b9SLeon Romanovsky static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) 7808700e3e7SMoni Shoua { 7818700e3e7SMoni Shoua struct rxe_cq *cq = to_rcq(ibcq); 7828700e3e7SMoni Shoua 7834703b4f0SBob Pearson /* See IBA C11-17: The CI shall return an error if this Verb is 7844703b4f0SBob Pearson * invoked while a Work Queue is still associated with the CQ. 7854703b4f0SBob Pearson */ 7864703b4f0SBob Pearson if (atomic_read(&cq->num_wq)) 7874703b4f0SBob Pearson return -EINVAL; 7884703b4f0SBob Pearson 789bfc3ae05SAndrew Boyer rxe_cq_disable(cq); 790bfc3ae05SAndrew Boyer 791215d0a75SBob Pearson rxe_cleanup(cq); 79243d781b9SLeon Romanovsky return 0; 7938700e3e7SMoni Shoua } 7948700e3e7SMoni Shoua 7958700e3e7SMoni Shoua static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata) 7968700e3e7SMoni Shoua { 7978700e3e7SMoni Shoua int err; 7988700e3e7SMoni Shoua struct rxe_cq *cq = to_rcq(ibcq); 7998700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ibcq->device); 8000c43ab37SJason Gunthorpe struct rxe_resize_cq_resp __user *uresp = NULL; 8010c43ab37SJason Gunthorpe 8020c43ab37SJason Gunthorpe if (udata) { 8030c43ab37SJason Gunthorpe if (udata->outlen < sizeof(*uresp)) 8040c43ab37SJason Gunthorpe return -EINVAL; 8050c43ab37SJason Gunthorpe uresp = udata->outbuf; 8060c43ab37SJason Gunthorpe } 8078700e3e7SMoni Shoua 808b92ec0feSJason Gunthorpe err = rxe_cq_chk_attr(rxe, cq, cqe, 0); 8098700e3e7SMoni Shoua if (err) 8108700e3e7SMoni Shoua return err; 811692373d1SYunsheng Lin 812692373d1SYunsheng Lin return rxe_cq_resize_queue(cq, cqe, uresp, udata); 8138700e3e7SMoni Shoua } 8148700e3e7SMoni Shoua 8158700e3e7SMoni Shoua static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) 8168700e3e7SMoni Shoua { 8178700e3e7SMoni Shoua int i; 8188700e3e7SMoni Shoua struct rxe_cq *cq = to_rcq(ibcq); 8198700e3e7SMoni Shoua struct rxe_cqe *cqe; 820a099b085SBob Pearson unsigned long flags; 8218700e3e7SMoni Shoua 822a099b085SBob Pearson spin_lock_irqsave(&cq->cq_lock, flags); 8238700e3e7SMoni Shoua for (i = 0; i < num_entries; i++) { 824ae6e843fSBob Pearson cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER); 8258700e3e7SMoni Shoua if (!cqe) 8268700e3e7SMoni Shoua break; 8278700e3e7SMoni Shoua 8288700e3e7SMoni Shoua memcpy(wc++, &cqe->ibwc, sizeof(*wc)); 829ae6e843fSBob Pearson queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER); 8308700e3e7SMoni Shoua } 831a099b085SBob Pearson spin_unlock_irqrestore(&cq->cq_lock, flags); 8328700e3e7SMoni Shoua 8338700e3e7SMoni Shoua return i; 8348700e3e7SMoni Shoua } 8358700e3e7SMoni Shoua 8368700e3e7SMoni Shoua static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt) 8378700e3e7SMoni Shoua { 8388700e3e7SMoni Shoua struct rxe_cq *cq = to_rcq(ibcq); 8395bcf5a59SBob Pearson int count; 8405bcf5a59SBob Pearson 841ae6e843fSBob Pearson count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER); 8428700e3e7SMoni Shoua 8438700e3e7SMoni Shoua return (count > wc_cnt) ? wc_cnt : count; 8448700e3e7SMoni Shoua } 8458700e3e7SMoni Shoua 8468700e3e7SMoni Shoua static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags) 8478700e3e7SMoni Shoua { 8488700e3e7SMoni Shoua struct rxe_cq *cq = to_rcq(ibcq); 849accacb8fSAndrew Boyer int ret = 0; 8505bcf5a59SBob Pearson int empty; 851a099b085SBob Pearson unsigned long irq_flags; 8528700e3e7SMoni Shoua 853a099b085SBob Pearson spin_lock_irqsave(&cq->cq_lock, irq_flags); 8548700e3e7SMoni Shoua if (cq->notify != IB_CQ_NEXT_COMP) 8558700e3e7SMoni Shoua cq->notify = flags & IB_CQ_SOLICITED_MASK; 8568700e3e7SMoni Shoua 857ae6e843fSBob Pearson empty = queue_empty(cq->queue, QUEUE_TYPE_FROM_DRIVER); 8585bcf5a59SBob Pearson 8595bcf5a59SBob Pearson if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty) 860accacb8fSAndrew Boyer ret = 1; 861accacb8fSAndrew Boyer 862a099b085SBob Pearson spin_unlock_irqrestore(&cq->cq_lock, irq_flags); 863accacb8fSAndrew Boyer 864accacb8fSAndrew Boyer return ret; 8658700e3e7SMoni Shoua } 8668700e3e7SMoni Shoua 8678700e3e7SMoni Shoua static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access) 8688700e3e7SMoni Shoua { 8698700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ibpd->device); 8708700e3e7SMoni Shoua struct rxe_pd *pd = to_rpd(ibpd); 871364e282cSBob Pearson struct rxe_mr *mr; 872*72a03627SBob Pearson int err; 8738700e3e7SMoni Shoua 874*72a03627SBob Pearson mr = kzalloc(sizeof(*mr), GFP_KERNEL); 875*72a03627SBob Pearson if (!mr) { 876*72a03627SBob Pearson err = -ENOMEM; 877*72a03627SBob Pearson goto err_out; 878*72a03627SBob Pearson } 879*72a03627SBob Pearson 880*72a03627SBob Pearson err = rxe_add_to_pool(&rxe->mr_pool, mr); 881*72a03627SBob Pearson if (err) 882*72a03627SBob Pearson goto err_free; 8838700e3e7SMoni Shoua 8843197706aSBob Pearson rxe_get(pd); 88558651bbbSBob Pearson mr->ibmr.pd = ibpd; 8862778b72bSBob Pearson mr->ibmr.device = ibpd->device; 88758651bbbSBob Pearson 88858651bbbSBob Pearson rxe_mr_init_dma(access, mr); 889215d0a75SBob Pearson rxe_finalize(mr); 8908700e3e7SMoni Shoua return &mr->ibmr; 891*72a03627SBob Pearson 892*72a03627SBob Pearson err_free: 893*72a03627SBob Pearson kfree(mr); 894*72a03627SBob Pearson err_out: 895*72a03627SBob Pearson return ERR_PTR(err); 8968700e3e7SMoni Shoua } 8978700e3e7SMoni Shoua 8988700e3e7SMoni Shoua static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, 8998700e3e7SMoni Shoua u64 start, 9008700e3e7SMoni Shoua u64 length, 9018700e3e7SMoni Shoua u64 iova, 9028700e3e7SMoni Shoua int access, struct ib_udata *udata) 9038700e3e7SMoni Shoua { 9048700e3e7SMoni Shoua int err; 9058700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ibpd->device); 9068700e3e7SMoni Shoua struct rxe_pd *pd = to_rpd(ibpd); 907364e282cSBob Pearson struct rxe_mr *mr; 9088700e3e7SMoni Shoua 909*72a03627SBob Pearson mr = kzalloc(sizeof(*mr), GFP_KERNEL); 910*72a03627SBob Pearson if (!mr) { 911*72a03627SBob Pearson err = -ENOMEM; 912*72a03627SBob Pearson goto err_out; 913*72a03627SBob Pearson } 914*72a03627SBob Pearson 915*72a03627SBob Pearson err = rxe_add_to_pool(&rxe->mr_pool, mr); 916*72a03627SBob Pearson if (err) 917*72a03627SBob Pearson goto err_free; 9188700e3e7SMoni Shoua 9193197706aSBob Pearson rxe_get(pd); 92058651bbbSBob Pearson mr->ibmr.pd = ibpd; 9212778b72bSBob Pearson mr->ibmr.device = ibpd->device; 9228700e3e7SMoni Shoua 92358651bbbSBob Pearson err = rxe_mr_init_user(rxe, start, length, iova, access, mr); 9248700e3e7SMoni Shoua if (err) 925*72a03627SBob Pearson goto err_cleanup; 9268700e3e7SMoni Shoua 927215d0a75SBob Pearson rxe_finalize(mr); 9288700e3e7SMoni Shoua return &mr->ibmr; 9298700e3e7SMoni Shoua 930*72a03627SBob Pearson err_cleanup: 931215d0a75SBob Pearson rxe_cleanup(mr); 932*72a03627SBob Pearson err_free: 933*72a03627SBob Pearson kfree(mr); 934*72a03627SBob Pearson err_out: 9358700e3e7SMoni Shoua return ERR_PTR(err); 9368700e3e7SMoni Shoua } 9378700e3e7SMoni Shoua 938c4367a26SShamir Rabinovitch static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, 93942a3b153SGal Pressman u32 max_num_sg) 9408700e3e7SMoni Shoua { 9418700e3e7SMoni Shoua struct rxe_dev *rxe = to_rdev(ibpd->device); 9428700e3e7SMoni Shoua struct rxe_pd *pd = to_rpd(ibpd); 943364e282cSBob Pearson struct rxe_mr *mr; 9448700e3e7SMoni Shoua int err; 9458700e3e7SMoni Shoua 9468700e3e7SMoni Shoua if (mr_type != IB_MR_TYPE_MEM_REG) 9478700e3e7SMoni Shoua return ERR_PTR(-EINVAL); 9488700e3e7SMoni Shoua 949*72a03627SBob Pearson mr = kzalloc(sizeof(*mr), GFP_KERNEL); 950*72a03627SBob Pearson if (!mr) { 951*72a03627SBob Pearson err = -ENOMEM; 952*72a03627SBob Pearson goto err_out; 953*72a03627SBob Pearson } 954*72a03627SBob Pearson 955*72a03627SBob Pearson err = rxe_add_to_pool(&rxe->mr_pool, mr); 956*72a03627SBob Pearson if (err) 957*72a03627SBob Pearson goto err_free; 9588700e3e7SMoni Shoua 9593197706aSBob Pearson rxe_get(pd); 96058651bbbSBob Pearson mr->ibmr.pd = ibpd; 9612778b72bSBob Pearson mr->ibmr.device = ibpd->device; 9628700e3e7SMoni Shoua 96358651bbbSBob Pearson err = rxe_mr_init_fast(max_num_sg, mr); 9648700e3e7SMoni Shoua if (err) 965*72a03627SBob Pearson goto err_cleanup; 9668700e3e7SMoni Shoua 967215d0a75SBob Pearson rxe_finalize(mr); 9688700e3e7SMoni Shoua return &mr->ibmr; 9698700e3e7SMoni Shoua 970*72a03627SBob Pearson err_cleanup: 971692373d1SYunsheng Lin rxe_cleanup(mr); 972*72a03627SBob Pearson err_free: 973*72a03627SBob Pearson kfree(mr); 974*72a03627SBob Pearson err_out: 9758700e3e7SMoni Shoua return ERR_PTR(err); 9768700e3e7SMoni Shoua } 9778700e3e7SMoni Shoua 978c05d2664SKamal Heib static ssize_t parent_show(struct device *device, 9798700e3e7SMoni Shoua struct device_attribute *attr, char *buf) 9808700e3e7SMoni Shoua { 98154747231SParav Pandit struct rxe_dev *rxe = 98254747231SParav Pandit rdma_device_to_drv_device(device, struct rxe_dev, ib_dev); 9838700e3e7SMoni Shoua 9841c7fd726SJoe Perches return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1)); 9858700e3e7SMoni Shoua } 9868700e3e7SMoni Shoua 987c05d2664SKamal Heib static DEVICE_ATTR_RO(parent); 9888700e3e7SMoni Shoua 989508a523fSParav Pandit static struct attribute *rxe_dev_attributes[] = { 990508a523fSParav Pandit &dev_attr_parent.attr, 991508a523fSParav Pandit NULL 992508a523fSParav Pandit }; 993508a523fSParav Pandit 994508a523fSParav Pandit static const struct attribute_group rxe_attr_group = { 995508a523fSParav Pandit .attrs = rxe_dev_attributes, 9968700e3e7SMoni Shoua }; 9978700e3e7SMoni Shoua 998ca22354bSJason Gunthorpe static int rxe_enable_driver(struct ib_device *ib_dev) 999ca22354bSJason Gunthorpe { 1000ca22354bSJason Gunthorpe struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); 1001ca22354bSJason Gunthorpe 1002ca22354bSJason Gunthorpe rxe_set_port_state(rxe); 1003ca22354bSJason Gunthorpe dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev)); 1004ca22354bSJason Gunthorpe return 0; 1005ca22354bSJason Gunthorpe } 1006ca22354bSJason Gunthorpe 1007573efc4bSKamal Heib static const struct ib_device_ops rxe_dev_ops = { 10087a154142SJason Gunthorpe .owner = THIS_MODULE, 1009b9560a41SJason Gunthorpe .driver_id = RDMA_DRIVER_RXE, 101072c6ec18SJason Gunthorpe .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION, 1011b9560a41SJason Gunthorpe 10124b5f4d3fSJason Gunthorpe .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats, 1013573efc4bSKamal Heib .alloc_mr = rxe_alloc_mr, 1014beec0239SBob Pearson .alloc_mw = rxe_alloc_mw, 1015573efc4bSKamal Heib .alloc_pd = rxe_alloc_pd, 1016573efc4bSKamal Heib .alloc_ucontext = rxe_alloc_ucontext, 1017573efc4bSKamal Heib .attach_mcast = rxe_attach_mcast, 1018573efc4bSKamal Heib .create_ah = rxe_create_ah, 1019573efc4bSKamal Heib .create_cq = rxe_create_cq, 1020573efc4bSKamal Heib .create_qp = rxe_create_qp, 1021573efc4bSKamal Heib .create_srq = rxe_create_srq, 1022676a80adSJason Gunthorpe .create_user_ah = rxe_create_ah, 1023c367074bSJason Gunthorpe .dealloc_driver = rxe_dealloc, 1024beec0239SBob Pearson .dealloc_mw = rxe_dealloc_mw, 1025573efc4bSKamal Heib .dealloc_pd = rxe_dealloc_pd, 1026573efc4bSKamal Heib .dealloc_ucontext = rxe_dealloc_ucontext, 1027573efc4bSKamal Heib .dereg_mr = rxe_dereg_mr, 1028573efc4bSKamal Heib .destroy_ah = rxe_destroy_ah, 1029573efc4bSKamal Heib .destroy_cq = rxe_destroy_cq, 1030573efc4bSKamal Heib .destroy_qp = rxe_destroy_qp, 1031573efc4bSKamal Heib .destroy_srq = rxe_destroy_srq, 1032573efc4bSKamal Heib .detach_mcast = rxe_detach_mcast, 1033915e4af5SJason Gunthorpe .device_group = &rxe_attr_group, 1034ca22354bSJason Gunthorpe .enable_driver = rxe_enable_driver, 1035573efc4bSKamal Heib .get_dma_mr = rxe_get_dma_mr, 1036573efc4bSKamal Heib .get_hw_stats = rxe_ib_get_hw_stats, 1037573efc4bSKamal Heib .get_link_layer = rxe_get_link_layer, 1038573efc4bSKamal Heib .get_port_immutable = rxe_port_immutable, 1039573efc4bSKamal Heib .map_mr_sg = rxe_map_mr_sg, 1040573efc4bSKamal Heib .mmap = rxe_mmap, 1041573efc4bSKamal Heib .modify_ah = rxe_modify_ah, 1042573efc4bSKamal Heib .modify_device = rxe_modify_device, 1043573efc4bSKamal Heib .modify_port = rxe_modify_port, 1044573efc4bSKamal Heib .modify_qp = rxe_modify_qp, 1045573efc4bSKamal Heib .modify_srq = rxe_modify_srq, 1046573efc4bSKamal Heib .peek_cq = rxe_peek_cq, 1047573efc4bSKamal Heib .poll_cq = rxe_poll_cq, 1048573efc4bSKamal Heib .post_recv = rxe_post_recv, 1049573efc4bSKamal Heib .post_send = rxe_post_send, 1050573efc4bSKamal Heib .post_srq_recv = rxe_post_srq_recv, 1051573efc4bSKamal Heib .query_ah = rxe_query_ah, 1052573efc4bSKamal Heib .query_device = rxe_query_device, 1053573efc4bSKamal Heib .query_pkey = rxe_query_pkey, 1054573efc4bSKamal Heib .query_port = rxe_query_port, 1055573efc4bSKamal Heib .query_qp = rxe_query_qp, 1056573efc4bSKamal Heib .query_srq = rxe_query_srq, 1057573efc4bSKamal Heib .reg_user_mr = rxe_reg_user_mr, 1058573efc4bSKamal Heib .req_notify_cq = rxe_req_notify_cq, 1059573efc4bSKamal Heib .resize_cq = rxe_resize_cq, 1060d3456914SLeon Romanovsky 1061d3456914SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah), 1062e39afe3dSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq), 106321a428a0SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd), 1064514aee66SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp), 106568e326deSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq), 1066a2a074efSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc), 1067364e282cSBob Pearson INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw), 1068573efc4bSKamal Heib }; 1069573efc4bSKamal Heib 107066920e1bSSteve Wise int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name) 10718700e3e7SMoni Shoua { 10728700e3e7SMoni Shoua int err; 10738700e3e7SMoni Shoua struct ib_device *dev = &rxe->ib_dev; 10748700e3e7SMoni Shoua 1075dc78074aSBob Pearson strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc)); 10768700e3e7SMoni Shoua 10778700e3e7SMoni Shoua dev->node_type = RDMA_NODE_IB_CA; 10788700e3e7SMoni Shoua dev->phys_port_cnt = 1; 107967cf3623SSagi Grimberg dev->num_comp_vectors = num_possible_cpus(); 10808700e3e7SMoni Shoua dev->local_dma_lkey = 0; 10814d6f2859SYuval Shaia addrconf_addr_eui48((unsigned char *)&dev->node_guid, 10824d6f2859SYuval Shaia rxe->ndev->dev_addr); 10838700e3e7SMoni Shoua 10845c419366SJason Gunthorpe dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) | 10855c419366SJason Gunthorpe BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ); 10868700e3e7SMoni Shoua 1087573efc4bSKamal Heib ib_set_device_ops(dev, &rxe_dev_ops); 10884c173f59SJason Gunthorpe err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1); 10894c173f59SJason Gunthorpe if (err) 10904c173f59SJason Gunthorpe return err; 10918700e3e7SMoni Shoua 1092add2b3b8SBob Pearson err = rxe_icrc_init(rxe); 1093add2b3b8SBob Pearson if (err) 1094add2b3b8SBob Pearson return err; 1095cee2688eSyonatanc 1096e0477b34SJason Gunthorpe err = ib_register_device(dev, ibdev_name, NULL); 1097c367074bSJason Gunthorpe if (err) 109814e501fdSBob Pearson rxe_dbg(rxe, "failed with error %d\n", err); 1099cee2688eSyonatanc 1100ca22354bSJason Gunthorpe /* 1101ca22354bSJason Gunthorpe * Note that rxe may be invalid at this point if another thread 1102ca22354bSJason Gunthorpe * unregistered it. 1103ca22354bSJason Gunthorpe */ 11048700e3e7SMoni Shoua return err; 11058700e3e7SMoni Shoua } 1106