xref: /linux/drivers/infiniband/sw/rxe/rxe.c (revision 80c4c25460849f441d35810555539aa3adc52929)
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  */
rxe_dealloc(struct ib_device * ib_dev)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(&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 
32 	WARN_ON(!RB_EMPTY_ROOT(&rxe->mcg_tree));
33 
34 	if (rxe->tfm)
35 		crypto_free_shash(rxe->tfm);
36 
37 	mutex_destroy(&rxe->usdev_lock);
38 }
39 
40 /* initialize rxe device parameters */
rxe_init_device_param(struct rxe_dev * rxe,struct net_device * ndev)41 static void rxe_init_device_param(struct rxe_dev *rxe, struct net_device *ndev)
42 {
43 	rxe->max_inline_data			= RXE_MAX_INLINE_DATA;
44 
45 	rxe->attr.vendor_id			= RXE_VENDOR_ID;
46 	rxe->attr.max_mr_size			= RXE_MAX_MR_SIZE;
47 	rxe->attr.page_size_cap			= RXE_PAGE_SIZE_CAP;
48 	rxe->attr.max_qp			= RXE_MAX_QP;
49 	rxe->attr.max_qp_wr			= RXE_MAX_QP_WR;
50 	rxe->attr.device_cap_flags		= RXE_DEVICE_CAP_FLAGS;
51 	rxe->attr.kernel_cap_flags		= IBK_ALLOW_USER_UNREG;
52 	rxe->attr.max_send_sge			= RXE_MAX_SGE;
53 	rxe->attr.max_recv_sge			= RXE_MAX_SGE;
54 	rxe->attr.max_sge_rd			= RXE_MAX_SGE_RD;
55 	rxe->attr.max_cq			= RXE_MAX_CQ;
56 	rxe->attr.max_cqe			= (1 << RXE_MAX_LOG_CQE) - 1;
57 	rxe->attr.max_mr			= RXE_MAX_MR;
58 	rxe->attr.max_mw			= RXE_MAX_MW;
59 	rxe->attr.max_pd			= RXE_MAX_PD;
60 	rxe->attr.max_qp_rd_atom		= RXE_MAX_QP_RD_ATOM;
61 	rxe->attr.max_res_rd_atom		= RXE_MAX_RES_RD_ATOM;
62 	rxe->attr.max_qp_init_rd_atom		= RXE_MAX_QP_INIT_RD_ATOM;
63 	rxe->attr.atomic_cap			= IB_ATOMIC_HCA;
64 	rxe->attr.max_mcast_grp			= RXE_MAX_MCAST_GRP;
65 	rxe->attr.max_mcast_qp_attach		= RXE_MAX_MCAST_QP_ATTACH;
66 	rxe->attr.max_total_mcast_qp_attach	= RXE_MAX_TOT_MCAST_QP_ATTACH;
67 	rxe->attr.max_ah			= RXE_MAX_AH;
68 	rxe->attr.max_srq			= RXE_MAX_SRQ;
69 	rxe->attr.max_srq_wr			= RXE_MAX_SRQ_WR;
70 	rxe->attr.max_srq_sge			= RXE_MAX_SRQ_SGE;
71 	rxe->attr.max_fast_reg_page_list_len	= RXE_MAX_FMR_PAGE_LIST_LEN;
72 	rxe->attr.max_pkeys			= RXE_MAX_PKEYS;
73 	rxe->attr.local_ca_ack_delay		= RXE_LOCAL_CA_ACK_DELAY;
74 
75 	addrconf_addr_eui48((unsigned char *)&rxe->attr.sys_image_guid,
76 			ndev->dev_addr);
77 
78 	rxe->max_ucontext			= RXE_MAX_UCONTEXT;
79 }
80 
81 /* initialize port attributes */
rxe_init_port_param(struct rxe_port * port)82 static void rxe_init_port_param(struct rxe_port *port)
83 {
84 	port->attr.state		= IB_PORT_DOWN;
85 	port->attr.max_mtu		= IB_MTU_4096;
86 	port->attr.active_mtu		= IB_MTU_256;
87 	port->attr.gid_tbl_len		= RXE_PORT_GID_TBL_LEN;
88 	port->attr.port_cap_flags	= RXE_PORT_PORT_CAP_FLAGS;
89 	port->attr.max_msg_sz		= RXE_PORT_MAX_MSG_SZ;
90 	port->attr.bad_pkey_cntr	= RXE_PORT_BAD_PKEY_CNTR;
91 	port->attr.qkey_viol_cntr	= RXE_PORT_QKEY_VIOL_CNTR;
92 	port->attr.pkey_tbl_len		= RXE_PORT_PKEY_TBL_LEN;
93 	port->attr.lid			= RXE_PORT_LID;
94 	port->attr.sm_lid		= RXE_PORT_SM_LID;
95 	port->attr.lmc			= RXE_PORT_LMC;
96 	port->attr.max_vl_num		= RXE_PORT_MAX_VL_NUM;
97 	port->attr.sm_sl		= RXE_PORT_SM_SL;
98 	port->attr.subnet_timeout	= RXE_PORT_SUBNET_TIMEOUT;
99 	port->attr.init_type_reply	= RXE_PORT_INIT_TYPE_REPLY;
100 	port->attr.active_width		= RXE_PORT_ACTIVE_WIDTH;
101 	port->attr.active_speed		= RXE_PORT_ACTIVE_SPEED;
102 	port->attr.phys_state		= RXE_PORT_PHYS_STATE;
103 	port->mtu_cap			= ib_mtu_enum_to_int(IB_MTU_256);
104 	port->subnet_prefix		= cpu_to_be64(RXE_PORT_SUBNET_PREFIX);
105 }
106 
107 /* initialize port state, note IB convention that HCA ports are always
108  * numbered from 1
109  */
rxe_init_ports(struct rxe_dev * rxe,struct net_device * ndev)110 static void rxe_init_ports(struct rxe_dev *rxe, struct net_device *ndev)
111 {
112 	struct rxe_port *port = &rxe->port;
113 
114 	rxe_init_port_param(port);
115 	addrconf_addr_eui48((unsigned char *)&port->port_guid,
116 			    ndev->dev_addr);
117 	spin_lock_init(&port->port_lock);
118 }
119 
120 /* init pools of managed objects */
rxe_init_pools(struct rxe_dev * rxe)121 static void rxe_init_pools(struct rxe_dev *rxe)
122 {
123 	rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC);
124 	rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD);
125 	rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH);
126 	rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ);
127 	rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP);
128 	rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ);
129 	rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR);
130 	rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW);
131 }
132 
133 /* initialize rxe device state */
rxe_init(struct rxe_dev * rxe,struct net_device * ndev)134 static void rxe_init(struct rxe_dev *rxe, struct net_device *ndev)
135 {
136 	/* init default device parameters */
137 	rxe_init_device_param(rxe, ndev);
138 
139 	rxe_init_ports(rxe, ndev);
140 	rxe_init_pools(rxe);
141 
142 	/* init pending mmap list */
143 	spin_lock_init(&rxe->mmap_offset_lock);
144 	spin_lock_init(&rxe->pending_lock);
145 	INIT_LIST_HEAD(&rxe->pending_mmaps);
146 
147 	/* init multicast support */
148 	spin_lock_init(&rxe->mcg_lock);
149 	rxe->mcg_tree = RB_ROOT;
150 
151 	mutex_init(&rxe->usdev_lock);
152 }
153 
rxe_set_mtu(struct rxe_dev * rxe,unsigned int ndev_mtu)154 void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu)
155 {
156 	struct rxe_port *port = &rxe->port;
157 	enum ib_mtu mtu;
158 
159 	mtu = eth_mtu_int_to_enum(ndev_mtu);
160 
161 	/* Make sure that new MTU in range */
162 	mtu = mtu ? min_t(enum ib_mtu, mtu, IB_MTU_4096) : IB_MTU_256;
163 
164 	port->attr.active_mtu = mtu;
165 	port->mtu_cap = ib_mtu_enum_to_int(mtu);
166 }
167 
168 /* called by ifc layer to create new rxe device.
169  * The caller should allocate memory for rxe by calling ib_alloc_device.
170  */
rxe_add(struct rxe_dev * rxe,unsigned int mtu,const char * ibdev_name,struct net_device * ndev)171 int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name,
172 			struct net_device *ndev)
173 {
174 	rxe_init(rxe, ndev);
175 	rxe_set_mtu(rxe, mtu);
176 
177 	return rxe_register_device(rxe, ibdev_name, ndev);
178 }
179 
rxe_newlink(const char * ibdev_name,struct net_device * ndev)180 static int rxe_newlink(const char *ibdev_name, struct net_device *ndev)
181 {
182 	struct rxe_dev *rxe;
183 	int err = 0;
184 
185 	if (is_vlan_dev(ndev)) {
186 		rxe_err("rxe creation allowed on top of a real device only\n");
187 		err = -EPERM;
188 		goto err;
189 	}
190 
191 	rxe = rxe_get_dev_from_net(ndev);
192 	if (rxe) {
193 		ib_device_put(&rxe->ib_dev);
194 		rxe_err_dev(rxe, "already configured on %s\n", ndev->name);
195 		err = -EEXIST;
196 		goto err;
197 	}
198 
199 	err = rxe_net_add(ibdev_name, ndev);
200 	if (err) {
201 		rxe_err("failed to add %s\n", ndev->name);
202 		goto err;
203 	}
204 err:
205 	return err;
206 }
207 
208 static struct rdma_link_ops rxe_link_ops = {
209 	.type = "rxe",
210 	.newlink = rxe_newlink,
211 };
212 
rxe_module_init(void)213 static int __init rxe_module_init(void)
214 {
215 	int err;
216 
217 	err = rxe_alloc_wq();
218 	if (err)
219 		return err;
220 
221 	err = rxe_net_init();
222 	if (err) {
223 		rxe_destroy_wq();
224 		return err;
225 	}
226 
227 	rdma_link_register(&rxe_link_ops);
228 	pr_info("loaded\n");
229 	return 0;
230 }
231 
rxe_module_exit(void)232 static void __exit rxe_module_exit(void)
233 {
234 	rdma_link_unregister(&rxe_link_ops);
235 	ib_unregister_driver(RDMA_DRIVER_RXE);
236 	rxe_net_exit();
237 	rxe_destroy_wq();
238 
239 	pr_info("unloaded\n");
240 }
241 
242 late_initcall(rxe_module_init);
243 module_exit(rxe_module_exit);
244 
245 MODULE_ALIAS_RDMA_LINK("rxe");
246