device.c revision 308304
1/*
2 * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *	  copyright notice, this list of conditions and the following
16 *	  disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *	  copyright notice, this list of conditions and the following
20 *	  disclaimer in the documentation and/or other materials
21 *	  provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: stable/10/sys/dev/cxgbe/iw_cxgbe/device.c 308304 2016-11-04 18:45:06Z jhb $");
34
35#include "opt_inet.h"
36
37#include <sys/ktr.h>
38
39#include <linux/module.h>
40#include <linux/moduleparam.h>
41
42#include <rdma/ib_verbs.h>
43#include <linux/idr.h>
44
45#ifdef TCP_OFFLOAD
46#include "iw_cxgbe.h"
47
48void
49c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
50    struct c4iw_dev_ucontext *uctx)
51{
52	struct list_head *pos, *nxt;
53	struct c4iw_qid_list *entry;
54
55	mutex_lock(&uctx->lock);
56	list_for_each_safe(pos, nxt, &uctx->qpids) {
57		entry = list_entry(pos, struct c4iw_qid_list, entry);
58		list_del_init(&entry->entry);
59		if (!(entry->qid & rdev->qpmask)) {
60			c4iw_put_resource(&rdev->resource.qid_table,
61					  entry->qid);
62			mutex_lock(&rdev->stats.lock);
63			rdev->stats.qid.cur -= rdev->qpmask + 1;
64			mutex_unlock(&rdev->stats.lock);
65		}
66		kfree(entry);
67	}
68
69	list_for_each_safe(pos, nxt, &uctx->qpids) {
70		entry = list_entry(pos, struct c4iw_qid_list, entry);
71		list_del_init(&entry->entry);
72		kfree(entry);
73	}
74	mutex_unlock(&uctx->lock);
75}
76
77void
78c4iw_init_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
79{
80
81	INIT_LIST_HEAD(&uctx->qpids);
82	INIT_LIST_HEAD(&uctx->cqids);
83	mutex_init(&uctx->lock);
84}
85
86static int
87c4iw_rdev_open(struct c4iw_rdev *rdev)
88{
89	struct adapter *sc = rdev->adap;
90	struct sge_params *sp = &sc->params.sge;
91	int rc;
92
93	c4iw_init_dev_ucontext(rdev, &rdev->uctx);
94
95	/* XXX: we can probably make this work */
96	if (sp->eq_s_qpp > PAGE_SHIFT || sp->iq_s_qpp > PAGE_SHIFT) {
97		device_printf(sc->dev,
98		    "doorbell density too high (eq %d, iq %d, pg %d).\n",
99		    sp->eq_s_qpp, sp->eq_s_qpp, PAGE_SHIFT);
100		rc = -EINVAL;
101		goto err1;
102	}
103
104	rdev->qpshift = PAGE_SHIFT - sp->eq_s_qpp;
105	rdev->qpmask = (1 << sp->eq_s_qpp) - 1;
106	rdev->cqshift = PAGE_SHIFT - sp->iq_s_qpp;
107	rdev->cqmask = (1 << sp->iq_s_qpp) - 1;
108
109	if (c4iw_num_stags(rdev) == 0) {
110		rc = -EINVAL;
111		goto err1;
112	}
113
114	rdev->stats.pd.total = T4_MAX_NUM_PD;
115	rdev->stats.stag.total = sc->vres.stag.size;
116	rdev->stats.pbl.total = sc->vres.pbl.size;
117	rdev->stats.rqt.total = sc->vres.rq.size;
118	rdev->stats.qid.total = sc->vres.qp.size;
119
120	rc = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
121	if (rc) {
122		device_printf(sc->dev, "error %d initializing resources\n", rc);
123		goto err1;
124	}
125	rc = c4iw_pblpool_create(rdev);
126	if (rc) {
127		device_printf(sc->dev, "error %d initializing pbl pool\n", rc);
128		goto err2;
129	}
130	rc = c4iw_rqtpool_create(rdev);
131	if (rc) {
132		device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
133		goto err3;
134	}
135
136	return (0);
137err3:
138	c4iw_pblpool_destroy(rdev);
139err2:
140	c4iw_destroy_resource(&rdev->resource);
141err1:
142	return (rc);
143}
144
145static void c4iw_rdev_close(struct c4iw_rdev *rdev)
146{
147	c4iw_pblpool_destroy(rdev);
148	c4iw_rqtpool_destroy(rdev);
149	c4iw_destroy_resource(&rdev->resource);
150}
151
152static void
153c4iw_dealloc(struct c4iw_dev *iwsc)
154{
155
156	c4iw_rdev_close(&iwsc->rdev);
157	idr_destroy(&iwsc->cqidr);
158	idr_destroy(&iwsc->qpidr);
159	idr_destroy(&iwsc->mmidr);
160	ib_dealloc_device(&iwsc->ibdev);
161}
162
163static struct c4iw_dev *
164c4iw_alloc(struct adapter *sc)
165{
166	struct c4iw_dev *iwsc;
167	int rc;
168
169	iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
170	if (iwsc == NULL) {
171		device_printf(sc->dev, "Cannot allocate ib device.\n");
172		return (ERR_PTR(-ENOMEM));
173	}
174	iwsc->rdev.adap = sc;
175
176	rc = c4iw_rdev_open(&iwsc->rdev);
177	if (rc != 0) {
178		device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
179		ib_dealloc_device(&iwsc->ibdev);
180		return (ERR_PTR(rc));
181	}
182
183	idr_init(&iwsc->cqidr);
184	idr_init(&iwsc->qpidr);
185	idr_init(&iwsc->mmidr);
186	spin_lock_init(&iwsc->lock);
187	mutex_init(&iwsc->rdev.stats.lock);
188
189	return (iwsc);
190}
191
192static int c4iw_mod_load(void);
193static int c4iw_mod_unload(void);
194static int c4iw_activate(struct adapter *);
195static int c4iw_deactivate(struct adapter *);
196
197static struct uld_info c4iw_uld_info = {
198	.uld_id = ULD_IWARP,
199	.activate = c4iw_activate,
200	.deactivate = c4iw_deactivate,
201};
202
203static int
204c4iw_activate(struct adapter *sc)
205{
206	struct c4iw_dev *iwsc;
207	int rc;
208
209	ASSERT_SYNCHRONIZED_OP(sc);
210
211	if (uld_active(sc, ULD_IWARP)) {
212		KASSERT(0, ("%s: RDMA already eanbled on sc %p", __func__, sc));
213		return (0);
214	}
215
216	if (sc->rdmacaps == 0) {
217		device_printf(sc->dev,
218		    "RDMA not supported or RDMA cap is not enabled.\n");
219		return (ENOSYS);
220	}
221
222	iwsc = c4iw_alloc(sc);
223	if (IS_ERR(iwsc)) {
224		rc = -PTR_ERR(iwsc);
225		device_printf(sc->dev, "initialization failed: %d\n", rc);
226		return (rc);
227	}
228
229	sc->iwarp_softc = iwsc;
230	c4iw_cm_init_cpl(sc);
231
232	rc = -c4iw_register_device(iwsc);
233	if (rc) {
234		device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
235		c4iw_dealloc(iwsc);
236		sc->iwarp_softc = NULL;
237	}
238
239	return (rc);
240}
241
242static int
243c4iw_deactivate(struct adapter *sc)
244{
245	struct c4iw_dev *iwsc = sc->iwarp_softc;
246
247	ASSERT_SYNCHRONIZED_OP(sc);
248
249	c4iw_unregister_device(iwsc);
250	c4iw_dealloc(iwsc);
251	sc->iwarp_softc = NULL;
252
253	return (0);
254}
255
256static void
257c4iw_activate_all(struct adapter *sc, void *arg __unused)
258{
259
260	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
261		return;
262
263	/* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
264	if (sc->offload_map && !uld_active(sc, ULD_IWARP))
265		(void) t4_activate_uld(sc, ULD_IWARP);
266
267	end_synchronized_op(sc, 0);
268}
269
270static void
271c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
272{
273
274	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
275		return;
276
277	if (uld_active(sc, ULD_IWARP))
278	    (void) t4_deactivate_uld(sc, ULD_IWARP);
279
280	end_synchronized_op(sc, 0);
281}
282
283static int
284c4iw_mod_load(void)
285{
286	int rc;
287
288	rc = -c4iw_cm_init();
289	if (rc != 0)
290		return (rc);
291
292	rc = t4_register_uld(&c4iw_uld_info);
293	if (rc != 0) {
294		c4iw_cm_term();
295		return (rc);
296	}
297
298	t4_iterate(c4iw_activate_all, NULL);
299
300	return (rc);
301}
302
303static int
304c4iw_mod_unload(void)
305{
306
307	t4_iterate(c4iw_deactivate_all, NULL);
308
309	c4iw_cm_term();
310
311	if (t4_unregister_uld(&c4iw_uld_info) == EBUSY)
312		return (EBUSY);
313
314	return (0);
315}
316
317#endif
318#undef MODULE_VERSION
319#include <sys/module.h>
320
321/*
322 * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
323 * on t4_tom ensures that it won't either.  So we don't directly check for
324 * TCP_OFFLOAD here.
325 */
326static int
327c4iw_modevent(module_t mod, int cmd, void *arg)
328{
329	int rc = 0;
330
331#ifdef TCP_OFFLOAD
332	switch (cmd) {
333	case MOD_LOAD:
334		rc = c4iw_mod_load();
335		if (rc == 0)
336			printf("iw_cxgbe: Chelsio T4/T5 RDMA driver loaded.\n");
337		break;
338
339	case MOD_UNLOAD:
340		rc = c4iw_mod_unload();
341		break;
342
343	default:
344		rc = EINVAL;
345	}
346#else
347	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
348	rc = EOPNOTSUPP;
349#endif
350	return (rc);
351}
352
353static moduledata_t c4iw_mod_data = {
354	"iw_cxgbe",
355	c4iw_modevent,
356	0
357};
358
359MODULE_VERSION(iw_cxgbe, 1);
360MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
361MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
362MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
363DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
364