Bob Pearson | 63fa15d | 2020-08-27 09:54:40 -0500 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. |
| 4 | * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 5 | */ |
| 6 | |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 7 | #include <rdma/rdma_netlink.h> |
Yuval Shaia | 4d6f285 | 2017-03-14 16:01:57 +0200 | [diff] [blame] | 8 | #include <net/addrconf.h> |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 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"); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 15 | |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 16 | /* free resources for a rxe device all objects created for this device must |
| 17 | * have been destroyed |
| 18 | */ |
Jason Gunthorpe | c367074 | 2019-01-22 16:27:24 -0700 | [diff] [blame] | 19 | void rxe_dealloc(struct ib_device *ib_dev) |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 20 | { |
Jason Gunthorpe | c367074 | 2019-01-22 16:27:24 -0700 | [diff] [blame] | 21 | struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev); |
| 22 | |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 23 | rxe_pool_cleanup(&rxe->uc_pool); |
| 24 | rxe_pool_cleanup(&rxe->pd_pool); |
| 25 | rxe_pool_cleanup(&rxe->ah_pool); |
| 26 | rxe_pool_cleanup(&rxe->srq_pool); |
| 27 | rxe_pool_cleanup(&rxe->qp_pool); |
| 28 | rxe_pool_cleanup(&rxe->cq_pool); |
| 29 | rxe_pool_cleanup(&rxe->mr_pool); |
| 30 | rxe_pool_cleanup(&rxe->mw_pool); |
| 31 | rxe_pool_cleanup(&rxe->mc_grp_pool); |
| 32 | rxe_pool_cleanup(&rxe->mc_elem_pool); |
| 33 | |
Jason Gunthorpe | c367074 | 2019-01-22 16:27:24 -0700 | [diff] [blame] | 34 | if (rxe->tfm) |
| 35 | crypto_free_shash(rxe->tfm); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 36 | } |
| 37 | |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 38 | /* initialize rxe device parameters */ |
Zhu Yanjun | befd8d9 | 2018-03-07 00:47:57 -0500 | [diff] [blame] | 39 | static void rxe_init_device_param(struct rxe_dev *rxe) |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 40 | { |
| 41 | rxe->max_inline_data = RXE_MAX_INLINE_DATA; |
| 42 | |
Zhu Yanjun | 0184afd | 2020-04-06 20:35:01 +0300 | [diff] [blame] | 43 | rxe->attr.vendor_id = RXE_VENDOR_ID; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 44 | rxe->attr.max_mr_size = RXE_MAX_MR_SIZE; |
| 45 | rxe->attr.page_size_cap = RXE_PAGE_SIZE_CAP; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 46 | rxe->attr.max_qp = RXE_MAX_QP; |
| 47 | rxe->attr.max_qp_wr = RXE_MAX_QP_WR; |
| 48 | rxe->attr.device_cap_flags = RXE_DEVICE_CAP_FLAGS; |
Steve Wise | 33023fb | 2018-06-18 08:05:26 -0700 | [diff] [blame] | 49 | rxe->attr.max_send_sge = RXE_MAX_SGE; |
| 50 | rxe->attr.max_recv_sge = RXE_MAX_SGE; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 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_pd = RXE_MAX_PD; |
| 56 | rxe->attr.max_qp_rd_atom = RXE_MAX_QP_RD_ATOM; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 57 | rxe->attr.max_res_rd_atom = RXE_MAX_RES_RD_ATOM; |
| 58 | rxe->attr.max_qp_init_rd_atom = RXE_MAX_QP_INIT_RD_ATOM; |
Nathan Chancellor | 0797e6f | 2018-09-26 22:12:23 -0700 | [diff] [blame] | 59 | rxe->attr.atomic_cap = IB_ATOMIC_HCA; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 60 | rxe->attr.max_mcast_grp = RXE_MAX_MCAST_GRP; |
| 61 | rxe->attr.max_mcast_qp_attach = RXE_MAX_MCAST_QP_ATTACH; |
| 62 | rxe->attr.max_total_mcast_qp_attach = RXE_MAX_TOT_MCAST_QP_ATTACH; |
| 63 | rxe->attr.max_ah = RXE_MAX_AH; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 64 | rxe->attr.max_srq = RXE_MAX_SRQ; |
| 65 | rxe->attr.max_srq_wr = RXE_MAX_SRQ_WR; |
| 66 | rxe->attr.max_srq_sge = RXE_MAX_SRQ_SGE; |
| 67 | rxe->attr.max_fast_reg_page_list_len = RXE_MAX_FMR_PAGE_LIST_LEN; |
| 68 | rxe->attr.max_pkeys = RXE_MAX_PKEYS; |
| 69 | rxe->attr.local_ca_ack_delay = RXE_LOCAL_CA_ACK_DELAY; |
Zhu Yanjun | d0ca2c3 | 2020-03-23 13:28:00 +0200 | [diff] [blame] | 70 | addrconf_addr_eui48((unsigned char *)&rxe->attr.sys_image_guid, |
| 71 | rxe->ndev->dev_addr); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 72 | |
| 73 | rxe->max_ucontext = RXE_MAX_UCONTEXT; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* initialize port attributes */ |
Kamal Heib | 9d576ea | 2020-07-05 13:43:11 +0300 | [diff] [blame] | 77 | static void rxe_init_port_param(struct rxe_port *port) |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 78 | { |
Nathan Chancellor | 0797e6f | 2018-09-26 22:12:23 -0700 | [diff] [blame] | 79 | port->attr.state = IB_PORT_DOWN; |
| 80 | port->attr.max_mtu = IB_MTU_4096; |
| 81 | port->attr.active_mtu = IB_MTU_256; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 82 | port->attr.gid_tbl_len = RXE_PORT_GID_TBL_LEN; |
| 83 | port->attr.port_cap_flags = RXE_PORT_PORT_CAP_FLAGS; |
| 84 | port->attr.max_msg_sz = RXE_PORT_MAX_MSG_SZ; |
| 85 | port->attr.bad_pkey_cntr = RXE_PORT_BAD_PKEY_CNTR; |
| 86 | port->attr.qkey_viol_cntr = RXE_PORT_QKEY_VIOL_CNTR; |
| 87 | port->attr.pkey_tbl_len = RXE_PORT_PKEY_TBL_LEN; |
| 88 | port->attr.lid = RXE_PORT_LID; |
| 89 | port->attr.sm_lid = RXE_PORT_SM_LID; |
| 90 | port->attr.lmc = RXE_PORT_LMC; |
| 91 | port->attr.max_vl_num = RXE_PORT_MAX_VL_NUM; |
| 92 | port->attr.sm_sl = RXE_PORT_SM_SL; |
| 93 | port->attr.subnet_timeout = RXE_PORT_SUBNET_TIMEOUT; |
| 94 | port->attr.init_type_reply = RXE_PORT_INIT_TYPE_REPLY; |
| 95 | port->attr.active_width = RXE_PORT_ACTIVE_WIDTH; |
| 96 | port->attr.active_speed = RXE_PORT_ACTIVE_SPEED; |
| 97 | port->attr.phys_state = RXE_PORT_PHYS_STATE; |
Nathan Chancellor | 0797e6f | 2018-09-26 22:12:23 -0700 | [diff] [blame] | 98 | port->mtu_cap = ib_mtu_enum_to_int(IB_MTU_256); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 99 | port->subnet_prefix = cpu_to_be64(RXE_PORT_SUBNET_PREFIX); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* initialize port state, note IB convention that HCA ports are always |
| 103 | * numbered from 1 |
| 104 | */ |
Kamal Heib | 76251e1 | 2020-07-21 13:16:18 +0300 | [diff] [blame] | 105 | static void rxe_init_ports(struct rxe_dev *rxe) |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 106 | { |
| 107 | struct rxe_port *port = &rxe->port; |
| 108 | |
| 109 | rxe_init_port_param(port); |
Yuval Shaia | 4d6f285 | 2017-03-14 16:01:57 +0200 | [diff] [blame] | 110 | addrconf_addr_eui48((unsigned char *)&port->port_guid, |
| 111 | rxe->ndev->dev_addr); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 112 | spin_lock_init(&port->port_lock); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* init pools of managed objects */ |
| 116 | static int rxe_init_pools(struct rxe_dev *rxe) |
| 117 | { |
| 118 | int err; |
| 119 | |
| 120 | err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC, |
| 121 | rxe->max_ucontext); |
| 122 | if (err) |
| 123 | goto err1; |
| 124 | |
| 125 | err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD, |
| 126 | rxe->attr.max_pd); |
| 127 | if (err) |
| 128 | goto err2; |
| 129 | |
| 130 | err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH, |
| 131 | rxe->attr.max_ah); |
| 132 | if (err) |
| 133 | goto err3; |
| 134 | |
| 135 | err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ, |
| 136 | rxe->attr.max_srq); |
| 137 | if (err) |
| 138 | goto err4; |
| 139 | |
| 140 | err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP, |
| 141 | rxe->attr.max_qp); |
| 142 | if (err) |
| 143 | goto err5; |
| 144 | |
| 145 | err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ, |
| 146 | rxe->attr.max_cq); |
| 147 | if (err) |
| 148 | goto err6; |
| 149 | |
| 150 | err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR, |
| 151 | rxe->attr.max_mr); |
| 152 | if (err) |
| 153 | goto err7; |
| 154 | |
| 155 | err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW, |
| 156 | rxe->attr.max_mw); |
| 157 | if (err) |
| 158 | goto err8; |
| 159 | |
| 160 | err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP, |
| 161 | rxe->attr.max_mcast_grp); |
| 162 | if (err) |
| 163 | goto err9; |
| 164 | |
| 165 | err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM, |
| 166 | rxe->attr.max_total_mcast_qp_attach); |
| 167 | if (err) |
| 168 | goto err10; |
| 169 | |
| 170 | return 0; |
| 171 | |
| 172 | err10: |
| 173 | rxe_pool_cleanup(&rxe->mc_grp_pool); |
| 174 | err9: |
| 175 | rxe_pool_cleanup(&rxe->mw_pool); |
| 176 | err8: |
| 177 | rxe_pool_cleanup(&rxe->mr_pool); |
| 178 | err7: |
| 179 | rxe_pool_cleanup(&rxe->cq_pool); |
| 180 | err6: |
| 181 | rxe_pool_cleanup(&rxe->qp_pool); |
| 182 | err5: |
| 183 | rxe_pool_cleanup(&rxe->srq_pool); |
| 184 | err4: |
| 185 | rxe_pool_cleanup(&rxe->ah_pool); |
| 186 | err3: |
| 187 | rxe_pool_cleanup(&rxe->pd_pool); |
| 188 | err2: |
| 189 | rxe_pool_cleanup(&rxe->uc_pool); |
| 190 | err1: |
| 191 | return err; |
| 192 | } |
| 193 | |
| 194 | /* initialize rxe device state */ |
| 195 | static int rxe_init(struct rxe_dev *rxe) |
| 196 | { |
| 197 | int err; |
| 198 | |
| 199 | /* init default device parameters */ |
| 200 | rxe_init_device_param(rxe); |
| 201 | |
Kamal Heib | 76251e1 | 2020-07-21 13:16:18 +0300 | [diff] [blame] | 202 | rxe_init_ports(rxe); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 203 | |
| 204 | err = rxe_init_pools(rxe); |
| 205 | if (err) |
Kamal Heib | 76251e1 | 2020-07-21 13:16:18 +0300 | [diff] [blame] | 206 | return err; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 207 | |
| 208 | /* init pending mmap list */ |
| 209 | spin_lock_init(&rxe->mmap_offset_lock); |
| 210 | spin_lock_init(&rxe->pending_lock); |
| 211 | INIT_LIST_HEAD(&rxe->pending_mmaps); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 212 | |
| 213 | mutex_init(&rxe->usdev_lock); |
| 214 | |
| 215 | return 0; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 216 | } |
| 217 | |
Zhu Yanjun | 0dff463 | 2018-04-20 10:30:54 -0400 | [diff] [blame] | 218 | void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu) |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 219 | { |
| 220 | struct rxe_port *port = &rxe->port; |
| 221 | enum ib_mtu mtu; |
| 222 | |
| 223 | mtu = eth_mtu_int_to_enum(ndev_mtu); |
| 224 | |
| 225 | /* Make sure that new MTU in range */ |
Nathan Chancellor | 0797e6f | 2018-09-26 22:12:23 -0700 | [diff] [blame] | 226 | mtu = mtu ? min_t(enum ib_mtu, mtu, IB_MTU_4096) : IB_MTU_256; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 227 | |
| 228 | port->attr.active_mtu = mtu; |
| 229 | port->mtu_cap = ib_mtu_enum_to_int(mtu); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 230 | } |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 231 | |
| 232 | /* called by ifc layer to create new rxe device. |
| 233 | * The caller should allocate memory for rxe by calling ib_alloc_device. |
| 234 | */ |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 235 | int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name) |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 236 | { |
| 237 | int err; |
| 238 | |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 239 | err = rxe_init(rxe); |
| 240 | if (err) |
Jason Gunthorpe | c367074 | 2019-01-22 16:27:24 -0700 | [diff] [blame] | 241 | return err; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 242 | |
Zhu Yanjun | 0dff463 | 2018-04-20 10:30:54 -0400 | [diff] [blame] | 243 | rxe_set_mtu(rxe, mtu); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 244 | |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 245 | return rxe_register_device(rxe, ibdev_name); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 246 | } |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 247 | |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 248 | static int rxe_newlink(const char *ibdev_name, struct net_device *ndev) |
| 249 | { |
| 250 | struct rxe_dev *exists; |
| 251 | int err = 0; |
| 252 | |
Mohammad Heib | fd49dda | 2020-08-11 18:04:15 +0300 | [diff] [blame] | 253 | if (is_vlan_dev(ndev)) { |
| 254 | pr_err("rxe creation allowed on top of a real device only\n"); |
| 255 | err = -EPERM; |
| 256 | goto err; |
| 257 | } |
| 258 | |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 259 | exists = rxe_get_dev_from_net(ndev); |
| 260 | if (exists) { |
| 261 | ib_device_put(&exists->ib_dev); |
| 262 | pr_err("already configured on %s\n", ndev->name); |
| 263 | err = -EEXIST; |
| 264 | goto err; |
| 265 | } |
| 266 | |
| 267 | err = rxe_net_add(ibdev_name, ndev); |
| 268 | if (err) { |
| 269 | pr_err("failed to add %s\n", ndev->name); |
| 270 | goto err; |
| 271 | } |
| 272 | err: |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | static struct rdma_link_ops rxe_link_ops = { |
| 277 | .type = "rxe", |
| 278 | .newlink = rxe_newlink, |
| 279 | }; |
| 280 | |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 281 | static int __init rxe_module_init(void) |
| 282 | { |
| 283 | int err; |
| 284 | |
| 285 | /* initialize slab caches for managed objects */ |
| 286 | err = rxe_cache_init(); |
| 287 | if (err) { |
Parav Pandit | e404f94 | 2016-09-28 20:26:26 +0000 | [diff] [blame] | 288 | pr_err("unable to init object pools\n"); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 289 | return err; |
| 290 | } |
| 291 | |
Parav Pandit | e404f94 | 2016-09-28 20:26:26 +0000 | [diff] [blame] | 292 | err = rxe_net_init(); |
| 293 | if (err) |
| 294 | return err; |
Yonatan Cohen | dfdd615 | 2016-09-07 14:04:04 +0300 | [diff] [blame] | 295 | |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 296 | rdma_link_register(&rxe_link_ops); |
Parav Pandit | e404f94 | 2016-09-28 20:26:26 +0000 | [diff] [blame] | 297 | pr_info("loaded\n"); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 298 | return 0; |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static void __exit rxe_module_exit(void) |
| 302 | { |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 303 | rdma_link_unregister(&rxe_link_ops); |
Jason Gunthorpe | c367074 | 2019-01-22 16:27:24 -0700 | [diff] [blame] | 304 | ib_unregister_driver(RDMA_DRIVER_RXE); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 305 | rxe_net_exit(); |
| 306 | rxe_cache_exit(); |
| 307 | |
Parav Pandit | e404f94 | 2016-09-28 20:26:26 +0000 | [diff] [blame] | 308 | pr_info("unloaded\n"); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 309 | } |
| 310 | |
Stephen Bates | b9fe856 | 2016-09-23 09:32:11 -0600 | [diff] [blame] | 311 | late_initcall(rxe_module_init); |
Moni Shoua | 8700e3e | 2016-06-16 16:45:23 +0300 | [diff] [blame] | 312 | module_exit(rxe_module_exit); |
Steve Wise | 66920e1 | 2019-02-15 11:03:57 -0800 | [diff] [blame] | 313 | |
| 314 | MODULE_ALIAS_RDMA_LINK("rxe"); |