1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 5 */ 6 7 #include <rdma/rdma_netlink.h> 8 #include <net/addrconf.h> 9 #include "rxe.h" 10 #include "rxe_loc.h" 11 #include "rxe_net.h" 12 #include "rxe_ns.h" 13 14 MODULE_AUTHOR("Bob Pearson, Frank Zago, John Groves, Kamal Heib"); 15 MODULE_DESCRIPTION("Soft RDMA transport"); 16 MODULE_LICENSE("Dual BSD/GPL"); 17 18 /* free resources for a rxe device all objects created for this device must 19 * have been destroyed 20 */ 21 void rxe_dealloc(struct ib_device *ib_dev) 22 { 23 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); 24 25 rxe_pool_cleanup(&rxe->uc_pool); 26 rxe_pool_cleanup(&rxe->pd_pool); 27 rxe_pool_cleanup(&rxe->ah_pool); 28 rxe_pool_cleanup(&rxe->srq_pool); 29 rxe_pool_cleanup(&rxe->qp_pool); 30 rxe_pool_cleanup(&rxe->cq_pool); 31 rxe_pool_cleanup(&rxe->mr_pool); 32 rxe_pool_cleanup(&rxe->mw_pool); 33 34 WARN_ON(!RB_EMPTY_ROOT(&rxe->mcg_tree)); 35 36 mutex_destroy(&rxe->usdev_lock); 37 } 38 39 static const struct ib_device_ops rxe_ib_dev_odp_ops = { 40 .advise_mr = rxe_ib_advise_mr, 41 }; 42 43 /* initialize rxe device parameters */ 44 static void rxe_init_device_param(struct rxe_dev *rxe, struct net_device *ndev) 45 { 46 rxe->max_inline_data = RXE_MAX_INLINE_DATA; 47 48 rxe->attr.vendor_id = RXE_VENDOR_ID; 49 rxe->attr.max_mr_size = RXE_MAX_MR_SIZE; 50 rxe->attr.page_size_cap = RXE_PAGE_SIZE_CAP; 51 rxe->attr.max_qp = RXE_MAX_QP; 52 rxe->attr.max_qp_wr = RXE_MAX_QP_WR; 53 rxe->attr.device_cap_flags = RXE_DEVICE_CAP_FLAGS; 54 rxe->attr.kernel_cap_flags = IBK_ALLOW_USER_UNREG; 55 rxe->attr.max_send_sge = RXE_MAX_SGE; 56 rxe->attr.max_recv_sge = RXE_MAX_SGE; 57 rxe->attr.max_sge_rd = RXE_MAX_SGE_RD; 58 rxe->attr.max_cq = RXE_MAX_CQ; 59 rxe->attr.max_cqe = (1 << RXE_MAX_LOG_CQE) - 1; 60 rxe->attr.max_mr = RXE_MAX_MR; 61 rxe->attr.max_mw = RXE_MAX_MW; 62 rxe->attr.max_pd = RXE_MAX_PD; 63 rxe->attr.max_qp_rd_atom = RXE_MAX_QP_RD_ATOM; 64 rxe->attr.max_res_rd_atom = RXE_MAX_RES_RD_ATOM; 65 rxe->attr.max_qp_init_rd_atom = RXE_MAX_QP_INIT_RD_ATOM; 66 rxe->attr.atomic_cap = IB_ATOMIC_HCA; 67 rxe->attr.max_mcast_grp = RXE_MAX_MCAST_GRP; 68 rxe->attr.max_mcast_qp_attach = RXE_MAX_MCAST_QP_ATTACH; 69 rxe->attr.max_total_mcast_qp_attach = RXE_MAX_TOT_MCAST_QP_ATTACH; 70 rxe->attr.max_ah = RXE_MAX_AH; 71 rxe->attr.max_srq = RXE_MAX_SRQ; 72 rxe->attr.max_srq_wr = RXE_MAX_SRQ_WR; 73 rxe->attr.max_srq_sge = RXE_MAX_SRQ_SGE; 74 rxe->attr.max_fast_reg_page_list_len = RXE_MAX_FMR_PAGE_LIST_LEN; 75 rxe->attr.max_pkeys = RXE_MAX_PKEYS; 76 rxe->attr.local_ca_ack_delay = RXE_LOCAL_CA_ACK_DELAY; 77 78 if (ndev->addr_len) { 79 memcpy(rxe->raw_gid, ndev->dev_addr, 80 min_t(unsigned int, ndev->addr_len, ETH_ALEN)); 81 } else { 82 /* 83 * This device does not have a HW address, but 84 * connection mangagement requires a unique gid. 85 */ 86 eth_random_addr(rxe->raw_gid); 87 } 88 89 addrconf_addr_eui48((unsigned char *)&rxe->attr.sys_image_guid, 90 rxe->raw_gid); 91 92 rxe->max_ucontext = RXE_MAX_UCONTEXT; 93 94 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) { 95 rxe->attr.kernel_cap_flags |= IBK_ON_DEMAND_PAGING; 96 97 /* IB_ODP_SUPPORT_IMPLICIT is not supported right now. */ 98 rxe->attr.odp_caps.general_caps |= IB_ODP_SUPPORT; 99 100 rxe->attr.odp_caps.per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SEND; 101 rxe->attr.odp_caps.per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_RECV; 102 rxe->attr.odp_caps.per_transport_caps.ud_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV; 103 104 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SEND; 105 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_RECV; 106 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_WRITE; 107 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_READ; 108 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_ATOMIC; 109 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_SRQ_RECV; 110 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_FLUSH; 111 rxe->attr.odp_caps.per_transport_caps.rc_odp_caps |= IB_ODP_SUPPORT_ATOMIC_WRITE; 112 113 /* set handler for ODP prefetching API - ibv_advise_mr(3) */ 114 ib_set_device_ops(&rxe->ib_dev, &rxe_ib_dev_odp_ops); 115 } 116 } 117 118 /* initialize port attributes */ 119 static void rxe_init_port_param(struct rxe_port *port) 120 { 121 port->attr.state = IB_PORT_DOWN; 122 port->attr.max_mtu = IB_MTU_4096; 123 port->attr.active_mtu = IB_MTU_256; 124 port->attr.gid_tbl_len = RXE_PORT_GID_TBL_LEN; 125 port->attr.port_cap_flags = RXE_PORT_PORT_CAP_FLAGS; 126 port->attr.max_msg_sz = RXE_PORT_MAX_MSG_SZ; 127 port->attr.bad_pkey_cntr = RXE_PORT_BAD_PKEY_CNTR; 128 port->attr.qkey_viol_cntr = RXE_PORT_QKEY_VIOL_CNTR; 129 port->attr.pkey_tbl_len = RXE_PORT_PKEY_TBL_LEN; 130 port->attr.lid = RXE_PORT_LID; 131 port->attr.sm_lid = RXE_PORT_SM_LID; 132 port->attr.lmc = RXE_PORT_LMC; 133 port->attr.max_vl_num = RXE_PORT_MAX_VL_NUM; 134 port->attr.sm_sl = RXE_PORT_SM_SL; 135 port->attr.subnet_timeout = RXE_PORT_SUBNET_TIMEOUT; 136 port->attr.init_type_reply = RXE_PORT_INIT_TYPE_REPLY; 137 port->attr.active_width = RXE_PORT_ACTIVE_WIDTH; 138 port->attr.active_speed = RXE_PORT_ACTIVE_SPEED; 139 port->attr.phys_state = RXE_PORT_PHYS_STATE; 140 port->mtu_cap = ib_mtu_enum_to_int(IB_MTU_256); 141 port->subnet_prefix = cpu_to_be64(RXE_PORT_SUBNET_PREFIX); 142 } 143 144 /* initialize port state, note IB convention that HCA ports are always 145 * numbered from 1 146 */ 147 static void rxe_init_ports(struct rxe_dev *rxe, struct net_device *ndev) 148 { 149 struct rxe_port *port = &rxe->port; 150 151 rxe_init_port_param(port); 152 addrconf_addr_eui48((unsigned char *)&port->port_guid, 153 rxe->raw_gid); 154 spin_lock_init(&port->port_lock); 155 } 156 157 /* init pools of managed objects */ 158 static void rxe_init_pools(struct rxe_dev *rxe) 159 { 160 rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC); 161 rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD); 162 rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH); 163 rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ); 164 rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP); 165 rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ); 166 rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR); 167 rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW); 168 } 169 170 /* initialize rxe device state */ 171 static void rxe_init(struct rxe_dev *rxe, struct net_device *ndev) 172 { 173 /* init default device parameters */ 174 rxe_init_device_param(rxe, ndev); 175 176 rxe_init_ports(rxe, ndev); 177 rxe_init_pools(rxe); 178 179 /* init pending mmap list */ 180 spin_lock_init(&rxe->mmap_offset_lock); 181 spin_lock_init(&rxe->pending_lock); 182 INIT_LIST_HEAD(&rxe->pending_mmaps); 183 184 /* init multicast support */ 185 spin_lock_init(&rxe->mcg_lock); 186 rxe->mcg_tree = RB_ROOT; 187 188 mutex_init(&rxe->usdev_lock); 189 } 190 191 void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu) 192 { 193 struct rxe_port *port = &rxe->port; 194 enum ib_mtu mtu; 195 196 mtu = eth_mtu_int_to_enum(ndev_mtu); 197 198 /* Make sure that new MTU in range */ 199 mtu = mtu ? min_t(enum ib_mtu, mtu, IB_MTU_4096) : IB_MTU_256; 200 201 port->attr.active_mtu = mtu; 202 port->mtu_cap = ib_mtu_enum_to_int(mtu); 203 } 204 205 static struct rdma_link_ops rxe_link_ops; 206 207 /* called by ifc layer to create new rxe device. 208 * The caller should allocate memory for rxe by calling ib_alloc_device. 209 */ 210 int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name, 211 struct net_device *ndev) 212 { 213 rxe_init(rxe, ndev); 214 rxe_set_mtu(rxe, mtu); 215 rxe->ib_dev.link_ops = &rxe_link_ops; 216 217 return rxe_register_device(rxe, ibdev_name, ndev); 218 } 219 220 static int rxe_newlink(const char *ibdev_name, struct net_device *ndev) 221 { 222 struct rxe_dev *rxe; 223 int err = 0; 224 225 if (is_vlan_dev(ndev)) { 226 rxe_err("rxe creation allowed on top of a real device only\n"); 227 err = -EPERM; 228 goto err; 229 } 230 231 rxe = rxe_get_dev_from_net(ndev); 232 if (rxe) { 233 ib_device_put(&rxe->ib_dev); 234 rxe_err_dev(rxe, "already configured on %s\n", ndev->name); 235 err = -EEXIST; 236 goto err; 237 } 238 239 err = rxe_net_init(ndev); 240 if (err) 241 return err; 242 243 err = rxe_net_add(ibdev_name, ndev); 244 if (err) { 245 rxe_err("failed to add %s\n", ndev->name); 246 goto err; 247 } 248 err: 249 return err; 250 } 251 252 static int rxe_dellink(struct ib_device *dev) 253 { 254 rxe_net_del(dev); 255 256 return 0; 257 } 258 259 static struct rdma_link_ops rxe_link_ops = { 260 .type = "rxe", 261 .newlink = rxe_newlink, 262 .dellink = rxe_dellink, 263 }; 264 265 static int __init rxe_module_init(void) 266 { 267 int err; 268 269 err = rxe_alloc_wq(); 270 if (err) 271 return err; 272 273 err = rxe_namespace_init(); 274 if (err) 275 goto err_destroy_wq; 276 277 err = rxe_register_notifier(); 278 if (err) 279 goto err_namespace_exit; 280 281 rdma_link_register(&rxe_link_ops); 282 283 pr_info("loaded\n"); 284 return 0; 285 286 err_namespace_exit: 287 rxe_namespace_exit(); 288 err_destroy_wq: 289 rxe_destroy_wq(); 290 return err; 291 } 292 293 static void __exit rxe_module_exit(void) 294 { 295 rdma_link_unregister(&rxe_link_ops); 296 ib_unregister_driver(RDMA_DRIVER_RXE); 297 rxe_net_exit(); 298 rxe_destroy_wq(); 299 300 rxe_namespace_exit(); 301 302 pr_info("unloaded\n"); 303 } 304 305 late_initcall(rxe_module_init); 306 module_exit(rxe_module_exit); 307 308 MODULE_ALIAS_RDMA_LINK("rxe"); 309