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