FreeBSD kernel CXGBE device code
device.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include "opt_inet.h"
38
39#include <sys/ktr.h>
40
41#include <linux/module.h>
42#include <linux/moduleparam.h>
43
44#include <rdma/ib_verbs.h>
45#include <linux/idr.h>
46
47#ifdef TCP_OFFLOAD
48#include "iw_cxgbe.h"
49
50void
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)) {
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->cqids) {
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 struct sge_params *sp = &sc->params.sge;
93 int rc;
94 unsigned short ucq_density = 1 << sp->iq_s_qpp; /* # of user CQs/page */
95 unsigned short udb_density = 1 << sp->eq_s_qpp; /* # of user DB/page */
96
97
98 c4iw_init_dev_ucontext(rdev, &rdev->uctx);
99
100 /*
101 * This implementation assumes udb_density == ucq_density! Eventually
102 * we might need to support this but for now fail the open. Also the
103 * cqid and qpid range must match for now.
104 */
105 if (udb_density != ucq_density) {
106 device_printf(sc->dev, "unsupported udb/ucq densities %u/%u\n",
107 udb_density, ucq_density);
108 rc = -EINVAL;
109 goto err1;
110 }
111 if (sc->vres.qp.start != sc->vres.cq.start ||
112 sc->vres.qp.size != sc->vres.cq.size) {
113 device_printf(sc->dev, "%s: unsupported qp and cq id ranges "
114 "qp start %u size %u cq start %u size %u\n", __func__,
115 sc->vres.qp.start, sc->vres.qp.size, sc->vres.cq.start,
116 sc->vres.cq.size);
117 rc = -EINVAL;
118 goto err1;
119 }
120
121 rdev->qpshift = PAGE_SHIFT - sp->eq_s_qpp;
122 rdev->qpmask = udb_density - 1;
123 rdev->cqshift = PAGE_SHIFT - sp->iq_s_qpp;
124 rdev->cqmask = ucq_density - 1;
125
126 if (c4iw_num_stags(rdev) == 0) {
127 rc = -EINVAL;
128 goto err1;
129 }
130
131 rdev->stats.pd.total = T4_MAX_NUM_PD;
132 rdev->stats.stag.total = sc->vres.stag.size;
133 rdev->stats.pbl.total = sc->vres.pbl.size;
134 rdev->stats.rqt.total = sc->vres.rq.size;
135 rdev->stats.qid.total = sc->vres.qp.size;
136
138 if (rc) {
139 device_printf(sc->dev, "error %d initializing resources\n", rc);
140 goto err1;
141 }
142 rc = c4iw_pblpool_create(rdev);
143 if (rc) {
144 device_printf(sc->dev, "error %d initializing pbl pool\n", rc);
145 goto err2;
146 }
147 rc = c4iw_rqtpool_create(rdev);
148 if (rc) {
149 device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
150 goto err3;
151 }
152 rdev->status_page = (struct t4_dev_status_page *)
153 __get_free_page(GFP_KERNEL);
154 if (!rdev->status_page) {
155 rc = -ENOMEM;
156 goto err4;
157 }
158 rdev->status_page->qp_start = sc->vres.qp.start;
159 rdev->status_page->qp_size = sc->vres.qp.size;
160 rdev->status_page->cq_start = sc->vres.cq.start;
161 rdev->status_page->cq_size = sc->vres.cq.size;
162
163 /* T5 and above devices don't need Doorbell recovery logic,
164 * so db_off is always set to '0'.
165 */
166 rdev->status_page->db_off = 0;
167
168 rdev->status_page->wc_supported = rdev->adap->iwt.wc_en;
169
170 rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
171 if (!rdev->free_workq) {
172 rc = -ENOMEM;
173 goto err5;
174 }
175 return (0);
176err5:
177 free_page((unsigned long)rdev->status_page);
178err4:
180err3:
182err2:
184err1:
185 return (rc);
186}
187
188static void c4iw_rdev_close(struct c4iw_rdev *rdev)
189{
190 free_page((unsigned long)rdev->status_page);
194}
195
196static void
197c4iw_dealloc(struct c4iw_dev *iwsc)
198{
199
200 c4iw_rdev_close(&iwsc->rdev);
201 idr_destroy(&iwsc->cqidr);
202 idr_destroy(&iwsc->qpidr);
203 idr_destroy(&iwsc->mmidr);
204 ib_dealloc_device(&iwsc->ibdev);
205}
206
207static struct c4iw_dev *
208c4iw_alloc(struct adapter *sc)
209{
210 struct c4iw_dev *iwsc;
211 int rc;
212
213 iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
214 if (iwsc == NULL) {
215 device_printf(sc->dev, "Cannot allocate ib device.\n");
216 return (ERR_PTR(-ENOMEM));
217 }
218 iwsc->rdev.adap = sc;
219
220 /* init various hw-queue params based on lld info */
223 iwsc->rdev.hw_queue.t4_max_eq_size = 65520;
224 iwsc->rdev.hw_queue.t4_max_iq_size = 65520;
225 iwsc->rdev.hw_queue.t4_max_rq_size = 8192 -
233 iwsc->rdev.hw_queue.t4_max_iq_size - 2;
235
236 /* As T5 and above devices support BAR2 kernel doorbells & WC, we map
237 * all of BAR2, for both User and Kernel Doorbells-GTS.
238 */
239 iwsc->rdev.bar2_kva = (void __iomem *)((u64)iwsc->rdev.adap->udbs_base);
240 iwsc->rdev.bar2_pa = vtophys(iwsc->rdev.adap->udbs_base);
241 iwsc->rdev.bar2_len = rman_get_size(iwsc->rdev.adap->udbs_res);
242
243 rc = c4iw_rdev_open(&iwsc->rdev);
244 if (rc != 0) {
245 device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
246 ib_dealloc_device(&iwsc->ibdev);
247 return (ERR_PTR(rc));
248 }
249
250 idr_init(&iwsc->cqidr);
251 idr_init(&iwsc->qpidr);
252 idr_init(&iwsc->mmidr);
253 spin_lock_init(&iwsc->lock);
254 mutex_init(&iwsc->rdev.stats.lock);
256
257 return (iwsc);
258}
259
260static int c4iw_mod_load(void);
261static int c4iw_mod_unload(void);
262static int c4iw_activate(struct adapter *);
263static int c4iw_deactivate(struct adapter *);
264static void c4iw_async_event(struct adapter *);
265
266static struct uld_info c4iw_uld_info = {
267 .uld_id = ULD_IWARP,
268 .activate = c4iw_activate,
269 .deactivate = c4iw_deactivate,
270 .async_event = c4iw_async_event,
271};
272
273static int
274c4iw_activate(struct adapter *sc)
275{
276 struct c4iw_dev *iwsc;
277 int rc;
278
280
281 if (is_t4(sc)) {
282 device_printf(sc->dev, "No iWARP support for T4 devices, "
283 "please install T5 or above devices.\n");
284 return (ENOSYS);
285 }
286
287 if (uld_active(sc, ULD_IWARP)) {
288 KASSERT(0, ("%s: RDMA already eanbled on sc %p", __func__, sc));
289 return (0);
290 }
291
292 if (sc->rdmacaps == 0) {
293 device_printf(sc->dev,
294 "RDMA not supported or RDMA cap is not enabled.\n");
295 return (ENOSYS);
296 }
297
298 iwsc = c4iw_alloc(sc);
299 if (IS_ERR(iwsc)) {
300 rc = -PTR_ERR(iwsc);
301 device_printf(sc->dev, "initialization failed: %d\n", rc);
302 return (rc);
303 }
304
305 sc->iwarp_softc = iwsc;
306
307 rc = -c4iw_register_device(iwsc);
308 if (rc) {
309 device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
310 c4iw_dealloc(iwsc);
311 sc->iwarp_softc = NULL;
312 }
313
314 return (rc);
315}
316
317static int
318c4iw_deactivate(struct adapter *sc)
319{
320 struct c4iw_dev *iwsc = sc->iwarp_softc;
321
323
325 c4iw_dealloc(iwsc);
326 sc->iwarp_softc = NULL;
327
328 return (0);
329}
330
331static void
332c4iw_async_event(struct adapter *sc)
333{
334 struct c4iw_dev *iwsc = sc->iwarp_softc;
335
336 if (iwsc) {
337 struct ib_event event = {0};
338
339 device_printf(sc->dev,
340 "iWARP driver received FATAL ERROR event.\n");
341 iwsc->rdev.flags |= T4_FATAL_ERROR;
342 event.event = IB_EVENT_DEVICE_FATAL;
343 event.device = &iwsc->ibdev;
344 ib_dispatch_event(&event);
345 }
346}
347
348static void
349c4iw_activate_all(struct adapter *sc, void *arg __unused)
350{
351
352 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
353 return;
354
355 /* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
356 if (sc->offload_map && !uld_active(sc, ULD_IWARP))
357 (void) t4_activate_uld(sc, ULD_IWARP);
358
359 end_synchronized_op(sc, 0);
360}
361
362static void
363c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
364{
365
366 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
367 return;
368
369 if (uld_active(sc, ULD_IWARP))
370 (void) t4_deactivate_uld(sc, ULD_IWARP);
371
372 end_synchronized_op(sc, 0);
373}
374
375static int
376c4iw_mod_load(void)
377{
378 int rc;
379
380 rc = -c4iw_cm_init();
381 if (rc != 0)
382 return (rc);
383
384 rc = t4_register_uld(&c4iw_uld_info);
385 if (rc != 0) {
386 c4iw_cm_term();
387 return (rc);
388 }
389
390 t4_iterate(c4iw_activate_all, NULL);
391
392 return (rc);
393}
394
395static int
396c4iw_mod_unload(void)
397{
398
399 t4_iterate(c4iw_deactivate_all, NULL);
400
401 c4iw_cm_term();
402
403 if (t4_unregister_uld(&c4iw_uld_info) == EBUSY)
404 return (EBUSY);
405
406 return (0);
407}
408
409#endif
410
411/*
412 * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
413 * on t4_tom ensures that it won't either. So we don't directly check for
414 * TCP_OFFLOAD here.
415 */
416static int
417c4iw_modevent(module_t mod, int cmd, void *arg)
418{
419 int rc = 0;
420
421#ifdef TCP_OFFLOAD
422 switch (cmd) {
423 case MOD_LOAD:
424 rc = c4iw_mod_load();
425 if (rc == 0)
426 printf("iw_cxgbe: Chelsio T5/T6 RDMA driver loaded.\n");
427 break;
428
429 case MOD_UNLOAD:
430 rc = c4iw_mod_unload();
431 break;
432
433 default:
434 rc = EINVAL;
435 }
436#else
437 printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
438 rc = EOPNOTSUPP;
439#endif
440 return (rc);
441}
442
443static moduledata_t c4iw_mod_data = {
444 "iw_cxgbe",
446 0
447};
448
449MODULE_VERSION(iw_cxgbe, 1);
450MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
451MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
452MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
453MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
454DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
@ EQ_ESIZE
Definition: adapter.h:105
int begin_synchronized_op(struct adapter *, struct vi_info *, int, char *)
Definition: t4_main.c:6121
void t4_iterate(void(*)(struct adapter *, void *), void *)
Definition: t4_main.c:12190
#define ASSERT_SYNCHRONIZED_OP(sc)
Definition: adapter.h:1020
@ SLEEP_OK
Definition: adapter.h:149
@ INTR_OK
Definition: adapter.h:150
void end_synchronized_op(struct adapter *, int)
Definition: t4_main.c:6204
static int is_t4(struct adapter *adap)
Definition: common.h:522
MODULE_VERSION(iw_cxgbe, 1)
MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1)
DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY)
__FBSDID("$FreeBSD$")
static moduledata_t c4iw_mod_data
Definition: device.c:443
static int c4iw_modevent(module_t mod, int cmd, void *arg)
Definition: device.c:417
void c4iw_release_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
void c4iw_destroy_resource(struct c4iw_resource *rscp)
int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
int c4iw_init_resource(struct c4iw_rdev *rdev, u32 nr_tpt, u32 nr_pdid)
void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
int c4iw_register_device(struct c4iw_dev *dev)
@ T4_FATAL_ERROR
Definition: iw_cxgbe.h:121
void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
int __init c4iw_cm_init(void)
void c4iw_init_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
static int c4iw_num_stags(struct c4iw_rdev *rdev)
Definition: iw_cxgbe.h:177
void c4iw_unregister_device(struct c4iw_dev *dev)
void __exit c4iw_cm_term(void)
int c4iw_pblpool_create(struct c4iw_rdev *rdev)
void c4iw_put_resource(struct c4iw_id_table *id_table, u32 entry)
@ ULD_IWARP
Definition: offload.h:206
uint64_t u64
Definition: osdep.h:62
unsigned int max_ird_adapter
Definition: common.h:402
struct sge_params sge
Definition: common.h:359
void * iwarp_softc
Definition: adapter.h:926
volatile uint8_t * udbs_base
Definition: adapter.h:880
struct resource * udbs_res
Definition: adapter.h:879
struct adapter_params params
Definition: adapter.h:958
int offload_map
Definition: adapter.h:937
struct iw_tunables iwt
Definition: adapter.h:927
struct t4_virt_res vres
Definition: adapter.h:960
device_t dev
Definition: adapter.h:866
uint16_t rdmacaps
Definition: adapter.h:967
struct mutex lock
Definition: iw_cxgbe.h:117
struct list_head qpids
Definition: iw_cxgbe.h:115
struct list_head cqids
Definition: iw_cxgbe.h:116
spinlock_t lock
Definition: iw_cxgbe.h:276
struct idr qpidr
Definition: iw_cxgbe.h:274
struct c4iw_rdev rdev
Definition: iw_cxgbe.h:271
u32 avail_ird
Definition: iw_cxgbe.h:278
struct ib_device ibdev
Definition: iw_cxgbe.h:269
struct idr cqidr
Definition: iw_cxgbe.h:273
struct idr mmidr
Definition: iw_cxgbe.h:275
int t4_max_qp_depth
Definition: iw_cxgbe.h:147
int t4_stat_len
Definition: iw_cxgbe.h:149
int t4_max_cq_depth
Definition: iw_cxgbe.h:148
int t4_max_rq_size
Definition: iw_cxgbe.h:145
int t4_eq_status_entries
Definition: iw_cxgbe.h:142
int t4_max_sq_size
Definition: iw_cxgbe.h:146
int t4_max_eq_size
Definition: iw_cxgbe.h:143
int t4_max_iq_size
Definition: iw_cxgbe.h:144
struct list_head entry
Definition: iw_cxgbe.h:110
struct c4iw_resource resource
Definition: iw_cxgbe.h:154
unsigned long cqshift
Definition: iw_cxgbe.h:157
struct t4_dev_status_page * status_page
Definition: iw_cxgbe.h:165
unsigned long qpshift
Definition: iw_cxgbe.h:155
unsigned long bar2_pa
Definition: iw_cxgbe.h:166
u32 cqmask
Definition: iw_cxgbe.h:158
struct c4iw_stats stats
Definition: iw_cxgbe.h:163
struct adapter * adap
Definition: iw_cxgbe.h:153
struct c4iw_hw_queue hw_queue
Definition: iw_cxgbe.h:164
unsigned int bar2_len
Definition: iw_cxgbe.h:168
u32 flags
Definition: iw_cxgbe.h:162
u32 qpmask
Definition: iw_cxgbe.h:156
struct workqueue_struct * free_workq
Definition: iw_cxgbe.h:169
struct c4iw_dev_ucontext uctx
Definition: iw_cxgbe.h:159
void __iomem * bar2_kva
Definition: iw_cxgbe.h:167
struct c4iw_id_table qid_table
Definition: iw_cxgbe.h:105
u64 cur
Definition: iw_cxgbe.h:127
u64 total
Definition: iw_cxgbe.h:126
struct c4iw_stat pbl
Definition: iw_cxgbe.h:137
struct c4iw_stat pd
Definition: iw_cxgbe.h:135
struct c4iw_stat stag
Definition: iw_cxgbe.h:136
struct c4iw_stat qid
Definition: iw_cxgbe.h:134
struct mutex lock
Definition: iw_cxgbe.h:133
struct c4iw_stat rqt
Definition: iw_cxgbe.h:138
int wc_en
Definition: offload.h:241
int iq_s_qpp
Definition: common.h:244
int eq_s_qpp
Definition: common.h:243
int spg_len
Definition: common.h:245
u64 qp_start
Definition: t4.h:714
u8 wc_supported
Definition: t4.h:711
u64 cq_start
Definition: t4.h:716
u_int start
Definition: offload.h:186
u_int size
Definition: offload.h:187
struct t4_range pbl
Definition: offload.h:195
struct t4_range cq
Definition: offload.h:197
struct t4_range stag
Definition: offload.h:193
struct t4_range qp
Definition: offload.h:196
struct t4_range rq
Definition: offload.h:194
int uld_id
Definition: offload.h:216
#define T4_MAX_NUM_PD
Definition: t4.h:66