device.c revision 256713
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: head/sys/dev/cxgbe/iw_cxgbe/device.c 256713 2013-10-17 23:57:17Z 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
48int spg_creds = 2; /* Default status page size is 2 credits = 128B */
49
50void
51c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
52    struct c4iw_dev_ucontext *uctx)
53{
54	struct list_head *pos, *nxt;
55	struct c4iw_qid_list *entry;
56
57	mutex_lock(&uctx->lock);
58	list_for_each_safe(pos, nxt, &uctx->qpids) {
59		entry = list_entry(pos, struct c4iw_qid_list, entry);
60		list_del_init(&entry->entry);
61		if (!(entry->qid & rdev->qpmask)) {
62			c4iw_put_resource(&rdev->resource.qid_table,
63					  entry->qid);
64			mutex_lock(&rdev->stats.lock);
65			rdev->stats.qid.cur -= rdev->qpmask + 1;
66			mutex_unlock(&rdev->stats.lock);
67		}
68		kfree(entry);
69	}
70
71	list_for_each_safe(pos, nxt, &uctx->qpids) {
72		entry = list_entry(pos, struct c4iw_qid_list, entry);
73		list_del_init(&entry->entry);
74		kfree(entry);
75	}
76	mutex_unlock(&uctx->lock);
77}
78
79void
80c4iw_init_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
81{
82
83	INIT_LIST_HEAD(&uctx->qpids);
84	INIT_LIST_HEAD(&uctx->cqids);
85	mutex_init(&uctx->lock);
86}
87
88static int
89c4iw_rdev_open(struct c4iw_rdev *rdev)
90{
91	struct adapter *sc = rdev->adap;
92	int rc;
93
94	c4iw_init_dev_ucontext(rdev, &rdev->uctx);
95
96	/* Save the status page size set by if_cxgbe */
97	spg_creds = (t4_read_reg(sc, A_SGE_CONTROL) & F_EGRSTATUSPAGESIZE) ?
98	    2 : 1;
99
100	/* XXX: we can probably make this work */
101	if (sc->sge.eq_s_qpp > PAGE_SHIFT || sc->sge.iq_s_qpp > PAGE_SHIFT) {
102		device_printf(sc->dev,
103		    "doorbell density too high (eq %d, iq %d, pg %d).\n",
104		    sc->sge.eq_s_qpp, sc->sge.eq_s_qpp, PAGE_SHIFT);
105		rc = -EINVAL;
106		goto err1;
107	}
108
109	rdev->qpshift = PAGE_SHIFT - sc->sge.eq_s_qpp;
110	rdev->qpmask = (1 << sc->sge.eq_s_qpp) - 1;
111	rdev->cqshift = PAGE_SHIFT - sc->sge.iq_s_qpp;
112	rdev->cqmask = (1 << sc->sge.iq_s_qpp) - 1;
113
114	if (c4iw_num_stags(rdev) == 0) {
115		rc = -EINVAL;
116		goto err1;
117	}
118
119	rdev->stats.pd.total = T4_MAX_NUM_PD;
120	rdev->stats.stag.total = sc->vres.stag.size;
121	rdev->stats.pbl.total = sc->vres.pbl.size;
122	rdev->stats.rqt.total = sc->vres.rq.size;
123	rdev->stats.qid.total = sc->vres.qp.size;
124
125	rc = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
126	if (rc) {
127		device_printf(sc->dev, "error %d initializing resources\n", rc);
128		goto err1;
129	}
130	rc = c4iw_pblpool_create(rdev);
131	if (rc) {
132		device_printf(sc->dev, "error %d initializing pbl pool\n", rc);
133		goto err2;
134	}
135	rc = c4iw_rqtpool_create(rdev);
136	if (rc) {
137		device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
138		goto err3;
139	}
140
141	return (0);
142err3:
143	c4iw_pblpool_destroy(rdev);
144err2:
145	c4iw_destroy_resource(&rdev->resource);
146err1:
147	return (rc);
148}
149
150static void c4iw_rdev_close(struct c4iw_rdev *rdev)
151{
152	c4iw_pblpool_destroy(rdev);
153	c4iw_rqtpool_destroy(rdev);
154	c4iw_destroy_resource(&rdev->resource);
155}
156
157static void
158c4iw_dealloc(struct c4iw_dev *iwsc)
159{
160
161	c4iw_rdev_close(&iwsc->rdev);
162	idr_destroy(&iwsc->cqidr);
163	idr_destroy(&iwsc->qpidr);
164	idr_destroy(&iwsc->mmidr);
165	ib_dealloc_device(&iwsc->ibdev);
166}
167
168static struct c4iw_dev *
169c4iw_alloc(struct adapter *sc)
170{
171	struct c4iw_dev *iwsc;
172	int rc;
173
174	iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
175	if (iwsc == NULL) {
176		device_printf(sc->dev, "Cannot allocate ib device.\n");
177		return (ERR_PTR(-ENOMEM));
178	}
179	iwsc->rdev.adap = sc;
180
181	rc = c4iw_rdev_open(&iwsc->rdev);
182	if (rc != 0) {
183		device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
184		ib_dealloc_device(&iwsc->ibdev);
185		return (ERR_PTR(rc));
186	}
187
188	idr_init(&iwsc->cqidr);
189	idr_init(&iwsc->qpidr);
190	idr_init(&iwsc->mmidr);
191	spin_lock_init(&iwsc->lock);
192	mutex_init(&iwsc->rdev.stats.lock);
193
194	return (iwsc);
195}
196
197static int c4iw_mod_load(void);
198static int c4iw_mod_unload(void);
199static int c4iw_activate(struct adapter *);
200static int c4iw_deactivate(struct adapter *);
201
202static struct uld_info c4iw_uld_info = {
203	.uld_id = ULD_IWARP,
204	.activate = c4iw_activate,
205	.deactivate = c4iw_deactivate,
206};
207
208static int
209c4iw_activate(struct adapter *sc)
210{
211	struct c4iw_dev *iwsc;
212	int rc;
213
214	ASSERT_SYNCHRONIZED_OP(sc);
215
216	if (isset(&sc->offload_map, MAX_NPORTS)) {
217		KASSERT(0, ("%s: RDMA already eanbled on sc %p", __func__, sc));
218		return (0);
219	}
220
221	if (sc->rdmacaps == 0) {
222		device_printf(sc->dev,
223		    "RDMA not supported or RDMA cap is not enabled.\n");
224		return (ENOSYS);
225	}
226
227	iwsc = c4iw_alloc(sc);
228	if (IS_ERR(iwsc)) {
229		rc = -PTR_ERR(iwsc);
230		device_printf(sc->dev, "initialization failed: %d\n", rc);
231		return (rc);
232	}
233
234	sc->iwarp_softc = iwsc;
235	c4iw_cm_init_cpl(sc);
236
237	rc = -c4iw_register_device(iwsc);
238	if (rc) {
239		device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
240		c4iw_dealloc(iwsc);
241		sc->iwarp_softc = NULL;
242	}
243
244	return (rc);
245}
246
247static int
248c4iw_deactivate(struct adapter *sc)
249{
250	struct c4iw_dev *iwsc = sc->iwarp_softc;
251
252	ASSERT_SYNCHRONIZED_OP(sc);
253
254	c4iw_unregister_device(iwsc);
255	c4iw_dealloc(iwsc);
256	sc->iwarp_softc = NULL;
257
258	return (0);
259}
260
261static void
262c4iw_activate_all(struct adapter *sc, void *arg __unused)
263{
264
265	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
266		return;
267
268	if (!isset(&sc->offload_map, MAX_NPORTS) &&
269	    t4_activate_uld(sc, ULD_IWARP) == 0)
270		setbit(&sc->offload_map, MAX_NPORTS);
271
272	end_synchronized_op(sc, 0);
273}
274
275static void
276c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
277{
278
279	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
280		return;
281
282	if (isset(&sc->offload_map, MAX_NPORTS) &&
283	    t4_deactivate_uld(sc, ULD_IWARP) == 0)
284		clrbit(&sc->offload_map, MAX_NPORTS);
285
286	end_synchronized_op(sc, 0);
287}
288
289static int
290c4iw_mod_load(void)
291{
292	int rc;
293
294	rc = -c4iw_cm_init();
295	if (rc != 0)
296		return (rc);
297
298	rc = t4_register_uld(&c4iw_uld_info);
299	if (rc != 0) {
300		c4iw_cm_term();
301		return (rc);
302	}
303
304	t4_iterate(c4iw_activate_all, NULL);
305
306	return (rc);
307}
308
309static int
310c4iw_mod_unload(void)
311{
312
313	t4_iterate(c4iw_deactivate_all, NULL);
314
315	c4iw_cm_term();
316
317	if (t4_unregister_uld(&c4iw_uld_info) == EBUSY)
318		return (EBUSY);
319
320	return (0);
321}
322
323#endif
324#undef MODULE_VERSION
325#include <sys/module.h>
326
327/*
328 * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
329 * on t4_tom ensures that it won't either.  So we don't directly check for
330 * TCP_OFFLOAD here.
331 */
332static int
333c4iw_modevent(module_t mod, int cmd, void *arg)
334{
335	int rc = 0;
336
337#ifdef TCP_OFFLOAD
338	switch (cmd) {
339	case MOD_LOAD:
340		rc = c4iw_mod_load();
341		if (rc == 0)
342			printf("iw_cxgbe: Chelsio T4/T5 RDMA driver loaded.\n");
343		break;
344
345	case MOD_UNLOAD:
346		rc = c4iw_mod_unload();
347		break;
348
349	default:
350		rc = EINVAL;
351	}
352#else
353	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
354	rc = EOPNOTSUPP;
355#endif
356	return (rc);
357}
358
359static moduledata_t c4iw_mod_data = {
360	"iw_cxgbe",
361	c4iw_modevent,
362	0
363};
364
365MODULE_VERSION(iw_cxgbe, 1);
366MODULE_DEPEND(iw_cxgbe, t4_nex, 1, 1, 1);
367MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
368MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
369DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
370