aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/cxgbe/iw_cxgbe/device.c
blob: fa886766e3837fd02079ec31a53d8f2fee22c099 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *	  copyright notice, this list of conditions and the following
 *	  disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *	  copyright notice, this list of conditions and the following
 *	  disclaimer in the documentation and/or other materials
 *	  provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <sys/cdefs.h>
#include "opt_inet.h"

#include <sys/ktr.h>

#include <linux/module.h>
#include <linux/moduleparam.h>

#include <rdma/ib_verbs.h>
#include <linux/idr.h>

#ifdef TCP_OFFLOAD
#include "iw_cxgbe.h"

void
c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
    struct c4iw_dev_ucontext *uctx)
{
	struct list_head *pos, *nxt;
	struct c4iw_qid_list *entry;

	mutex_lock(&uctx->lock);
	list_for_each_safe(pos, nxt, &uctx->qpids) {
		entry = list_entry(pos, struct c4iw_qid_list, entry);
		list_del_init(&entry->entry);
		if (!(entry->qid & rdev->qpmask)) {
			c4iw_put_resource(&rdev->resource.qid_table,
					  entry->qid);
			mutex_lock(&rdev->stats.lock);
			rdev->stats.qid.cur -= rdev->qpmask + 1;
			mutex_unlock(&rdev->stats.lock);
		}
		kfree(entry);
	}

	list_for_each_safe(pos, nxt, &uctx->cqids) {
		entry = list_entry(pos, struct c4iw_qid_list, entry);
		list_del_init(&entry->entry);
		kfree(entry);
	}
	mutex_unlock(&uctx->lock);
}

void
c4iw_init_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
{

	INIT_LIST_HEAD(&uctx->qpids);
	INIT_LIST_HEAD(&uctx->cqids);
	mutex_init(&uctx->lock);
}

static int
c4iw_rdev_open(struct c4iw_rdev *rdev)
{
	struct adapter *sc = rdev->adap;
	struct sge_params *sp = &sc->params.sge;
	int rc;
	unsigned short ucq_density = 1 << sp->iq_s_qpp; /* # of user CQs/page */
	unsigned short udb_density = 1 << sp->eq_s_qpp; /* # of user DB/page */


	c4iw_init_dev_ucontext(rdev, &rdev->uctx);

	/*
	 * This implementation assumes udb_density == ucq_density!  Eventually
	 * we might need to support this but for now fail the open. Also the
	 * cqid and qpid range must match for now.
	 */
	if (udb_density != ucq_density) {
		device_printf(sc->dev, "unsupported udb/ucq densities %u/%u\n",
		    udb_density, ucq_density);
		rc = -EINVAL;
		goto err1;
	}
	if (sc->vres.qp.start != sc->vres.cq.start ||
	    sc->vres.qp.size != sc->vres.cq.size) {
		device_printf(sc->dev, "%s: unsupported qp and cq id ranges "
			"qp start %u size %u cq start %u size %u\n", __func__,
			sc->vres.qp.start, sc->vres.qp.size, sc->vres.cq.start,
			sc->vres.cq.size);
		rc = -EINVAL;
		goto err1;
	}

	rdev->qpshift = PAGE_SHIFT - sp->eq_s_qpp;
	rdev->qpmask = udb_density - 1;
	rdev->cqshift = PAGE_SHIFT - sp->iq_s_qpp;
	rdev->cqmask = ucq_density - 1;

	if (c4iw_num_stags(rdev) == 0) {
		rc = -EINVAL;
		goto err1;
	}

	rdev->stats.pd.total = T4_MAX_NUM_PD;
	rdev->stats.stag.total = sc->vres.stag.size;
	rdev->stats.pbl.total = sc->vres.pbl.size;
	rdev->stats.rqt.total = sc->vres.rq.size;
	rdev->stats.qid.total = sc->vres.qp.size;

	rc = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
	if (rc) {
		device_printf(sc->dev, "error %d initializing resources\n", rc);
		goto err1;
	}
	rc = c4iw_pblpool_create(rdev);
	if (rc) {
		device_printf(sc->dev, "error %d initializing pbl pool\n", rc);
		goto err2;
	}
	rc = c4iw_rqtpool_create(rdev);
	if (rc) {
		device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
		goto err3;
	}
	rdev->status_page = (struct t4_dev_status_page *)
				__get_free_page(GFP_KERNEL);
	if (!rdev->status_page) {
		rc = -ENOMEM;
		goto err4;
	}
	rdev->status_page->qp_start = sc->vres.qp.start;
	rdev->status_page->qp_size = sc->vres.qp.size;
	rdev->status_page->cq_start = sc->vres.cq.start;
	rdev->status_page->cq_size = sc->vres.cq.size;

	/* T5 and above devices don't need Doorbell recovery logic,
	 * so db_off is always set to '0'.
	 */
	rdev->status_page->db_off = 0;

	rdev->status_page->wc_supported = rdev->adap->iwt.wc_en;

	rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
	if (!rdev->free_workq) {
		rc = -ENOMEM;
		goto err5;
	}
	return (0);
err5:
	free_page((unsigned long)rdev->status_page);
err4:
	c4iw_rqtpool_destroy(rdev);
err3:
	c4iw_pblpool_destroy(rdev);
err2:
	c4iw_destroy_resource(&rdev->resource);
err1:
	return (rc);
}

static void c4iw_rdev_close(struct c4iw_rdev *rdev)
{
	free_page((unsigned long)rdev->status_page);
	c4iw_pblpool_destroy(rdev);
	c4iw_rqtpool_destroy(rdev);
	c4iw_destroy_resource(&rdev->resource);
}

static void
c4iw_dealloc(struct c4iw_dev *iwsc)
{

	c4iw_rdev_close(&iwsc->rdev);
	idr_destroy(&iwsc->cqidr);
	idr_destroy(&iwsc->qpidr);
	idr_destroy(&iwsc->mmidr);
	ib_dealloc_device(&iwsc->ibdev);
}

static struct c4iw_dev *
c4iw_alloc(struct adapter *sc)
{
	struct c4iw_dev *iwsc;
	int rc;

	iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
	if (iwsc == NULL) {
		device_printf(sc->dev, "Cannot allocate ib device.\n");
		return (ERR_PTR(-ENOMEM));
	}
	iwsc->rdev.adap = sc;

	/* init various hw-queue params based on lld info */
	iwsc->rdev.hw_queue.t4_eq_status_entries =
		sc->params.sge.spg_len / EQ_ESIZE;
	iwsc->rdev.hw_queue.t4_max_eq_size = 65520;
	iwsc->rdev.hw_queue.t4_max_iq_size = 65520;
	iwsc->rdev.hw_queue.t4_max_rq_size = 8192 -
		iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
	iwsc->rdev.hw_queue.t4_max_sq_size =
		iwsc->rdev.hw_queue.t4_max_eq_size -
		iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
	iwsc->rdev.hw_queue.t4_max_qp_depth =
		iwsc->rdev.hw_queue.t4_max_rq_size;
	iwsc->rdev.hw_queue.t4_max_cq_depth =
		iwsc->rdev.hw_queue.t4_max_iq_size - 2;
	iwsc->rdev.hw_queue.t4_stat_len = iwsc->rdev.adap->params.sge.spg_len;

	/* As T5 and above devices support BAR2 kernel doorbells & WC, we map
	 * all of BAR2, for both User and Kernel Doorbells-GTS.
	 */
	iwsc->rdev.bar2_kva = (void __iomem *)((u64)iwsc->rdev.adap->udbs_base);
	iwsc->rdev.bar2_pa = vtophys(iwsc->rdev.adap->udbs_base);
	iwsc->rdev.bar2_len = rman_get_size(iwsc->rdev.adap->udbs_res);

	rc = c4iw_rdev_open(&iwsc->rdev);
	if (rc != 0) {
		device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
		ib_dealloc_device(&iwsc->ibdev);
		return (ERR_PTR(rc));
	}

	idr_init(&iwsc->cqidr);
	idr_init(&iwsc->qpidr);
	idr_init(&iwsc->mmidr);
	spin_lock_init(&iwsc->lock);
	mutex_init(&iwsc->rdev.stats.lock);
	iwsc->avail_ird = iwsc->rdev.adap->params.max_ird_adapter;

	return (iwsc);
}

static int c4iw_mod_load(void);
static int c4iw_mod_unload(void);
static int c4iw_activate(struct adapter *);
static int c4iw_deactivate(struct adapter *);
static void c4iw_async_event(struct adapter *);

static struct uld_info c4iw_uld_info = {
	.uld_id = ULD_IWARP,
	.activate = c4iw_activate,
	.deactivate = c4iw_deactivate,
	.async_event = c4iw_async_event,
};

static int
c4iw_activate(struct adapter *sc)
{
	struct c4iw_dev *iwsc;
	int rc;

	ASSERT_SYNCHRONIZED_OP(sc);

	if (is_t4(sc)) {
		device_printf(sc->dev, "No iWARP support for T4 devices, "
				"please install T5 or above devices.\n");
		return (ENOSYS);
	}

	if (uld_active(sc, ULD_IWARP)) {
		KASSERT(0, ("%s: RDMA already eanbled on sc %p", __func__, sc));
		return (0);
	}

	if (sc->rdmacaps == 0) {
		device_printf(sc->dev,
		    "RDMA not supported or RDMA cap is not enabled.\n");
		return (ENOSYS);
	}

	iwsc = c4iw_alloc(sc);
	if (IS_ERR(iwsc)) {
		rc = -PTR_ERR(iwsc);
		device_printf(sc->dev, "initialization failed: %d\n", rc);
		return (rc);
	}

	sc->iwarp_softc = iwsc;

	rc = -c4iw_register_device(iwsc);
	if (rc) {
		device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
		c4iw_dealloc(iwsc);
		sc->iwarp_softc = NULL;
	}

	return (rc);
}

static int
c4iw_deactivate(struct adapter *sc)
{
	struct c4iw_dev *iwsc = sc->iwarp_softc;

	ASSERT_SYNCHRONIZED_OP(sc);

	c4iw_unregister_device(iwsc);
	c4iw_dealloc(iwsc);
	sc->iwarp_softc = NULL;

	return (0);
}

static void
c4iw_async_event(struct adapter *sc)
{
	struct c4iw_dev *iwsc = sc->iwarp_softc;

	if (iwsc) {
		struct ib_event event = {0};

		device_printf(sc->dev,
			      "iWARP driver received FATAL ERROR event.\n");
		iwsc->rdev.flags |= T4_FATAL_ERROR;
		event.event  = IB_EVENT_DEVICE_FATAL;
		event.device = &iwsc->ibdev;
		ib_dispatch_event(&event);
	}
}

static void
c4iw_activate_all(struct adapter *sc, void *arg __unused)
{

	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
		return;

	/* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
	if (sc->offload_map && !uld_active(sc, ULD_IWARP))
		(void) t4_activate_uld(sc, ULD_IWARP);

	end_synchronized_op(sc, 0);
}

static void
c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
{

	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
		return;

	if (uld_active(sc, ULD_IWARP))
	    (void) t4_deactivate_uld(sc, ULD_IWARP);

	end_synchronized_op(sc, 0);
}

static int
c4iw_mod_load(void)
{
	int rc;

	rc = -c4iw_cm_init();
	if (rc != 0)
		return (rc);

	rc = t4_register_uld(&c4iw_uld_info);
	if (rc != 0) {
		c4iw_cm_term();
		return (rc);
	}

	t4_iterate(c4iw_activate_all, NULL);

	return (rc);
}

static int
c4iw_mod_unload(void)
{

	t4_iterate(c4iw_deactivate_all, NULL);

	c4iw_cm_term();

	if (t4_unregister_uld(&c4iw_uld_info) == EBUSY)
		return (EBUSY);

	return (0);
}

#endif

/*
 * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
 * on t4_tom ensures that it won't either.  So we don't directly check for
 * TCP_OFFLOAD here.
 */
static int
c4iw_modevent(module_t mod, int cmd, void *arg)
{
	int rc = 0;

#ifdef TCP_OFFLOAD
	switch (cmd) {
	case MOD_LOAD:
		rc = c4iw_mod_load();
		if (rc == 0)
			printf("iw_cxgbe: Chelsio T5/T6 RDMA driver loaded.\n");
		break;

	case MOD_UNLOAD:
		rc = c4iw_mod_unload();
		break;

	default:
		rc = EINVAL;
	}
#else
	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
	rc = EOPNOTSUPP;
#endif
	return (rc);
}

static moduledata_t c4iw_mod_data = {
	"iw_cxgbe",
	c4iw_modevent,
	0
};

MODULE_VERSION(iw_cxgbe, 1);
MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);