device.c revision 318798
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/11/sys/dev/cxgbe/iw_cxgbe/device.c 318798 2017-05-24 18:14:57Z np $");
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
231	rc = -c4iw_register_device(iwsc);
232	if (rc) {
233		device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
234		c4iw_dealloc(iwsc);
235		sc->iwarp_softc = NULL;
236	}
237
238	return (rc);
239}
240
241static int
242c4iw_deactivate(struct adapter *sc)
243{
244	struct c4iw_dev *iwsc = sc->iwarp_softc;
245
246	ASSERT_SYNCHRONIZED_OP(sc);
247
248	c4iw_unregister_device(iwsc);
249	c4iw_dealloc(iwsc);
250	sc->iwarp_softc = NULL;
251
252	return (0);
253}
254
255static void
256c4iw_activate_all(struct adapter *sc, void *arg __unused)
257{
258
259	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
260		return;
261
262	/* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
263	if (sc->offload_map && !uld_active(sc, ULD_IWARP))
264		(void) t4_activate_uld(sc, ULD_IWARP);
265
266	end_synchronized_op(sc, 0);
267}
268
269static void
270c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
271{
272
273	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
274		return;
275
276	if (uld_active(sc, ULD_IWARP))
277	    (void) t4_deactivate_uld(sc, ULD_IWARP);
278
279	end_synchronized_op(sc, 0);
280}
281
282static int
283c4iw_mod_load(void)
284{
285	int rc;
286
287	rc = -c4iw_cm_init();
288	if (rc != 0)
289		return (rc);
290
291	rc = t4_register_uld(&c4iw_uld_info);
292	if (rc != 0) {
293		c4iw_cm_term();
294		return (rc);
295	}
296
297	t4_iterate(c4iw_activate_all, NULL);
298
299	return (rc);
300}
301
302static int
303c4iw_mod_unload(void)
304{
305
306	t4_iterate(c4iw_deactivate_all, NULL);
307
308	c4iw_cm_term();
309
310	if (t4_unregister_uld(&c4iw_uld_info) == EBUSY)
311		return (EBUSY);
312
313	return (0);
314}
315
316#endif
317
318/*
319 * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
320 * on t4_tom ensures that it won't either.  So we don't directly check for
321 * TCP_OFFLOAD here.
322 */
323static int
324c4iw_modevent(module_t mod, int cmd, void *arg)
325{
326	int rc = 0;
327
328#ifdef TCP_OFFLOAD
329	switch (cmd) {
330	case MOD_LOAD:
331		rc = c4iw_mod_load();
332		if (rc == 0)
333			printf("iw_cxgbe: Chelsio T4/T5/T6 RDMA driver loaded.\n");
334		break;
335
336	case MOD_UNLOAD:
337		rc = c4iw_mod_unload();
338		break;
339
340	default:
341		rc = EINVAL;
342	}
343#else
344	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
345	rc = EOPNOTSUPP;
346#endif
347	return (rc);
348}
349
350static moduledata_t c4iw_mod_data = {
351	"iw_cxgbe",
352	c4iw_modevent,
353	0
354};
355
356MODULE_VERSION(iw_cxgbe, 1);
357MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
358MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
359MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
360MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
361DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
362