2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
41 #include <linux/module.h>
42 #include <linux/moduleparam.h>
44 #include <rdma/ib_verbs.h>
45 #include <linux/idr.h>
51 c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
52 struct c4iw_dev_ucontext *uctx)
54 struct list_head *pos, *nxt;
55 struct c4iw_qid_list *entry;
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,
64 mutex_lock(&rdev->stats.lock);
65 rdev->stats.qid.cur -= rdev->qpmask + 1;
66 mutex_unlock(&rdev->stats.lock);
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);
76 mutex_unlock(&uctx->lock);
80 c4iw_init_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
83 INIT_LIST_HEAD(&uctx->qpids);
84 INIT_LIST_HEAD(&uctx->cqids);
85 mutex_init(&uctx->lock);
89 c4iw_rdev_open(struct c4iw_rdev *rdev)
91 struct adapter *sc = rdev->adap;
92 struct sge_params *sp = &sc->params.sge;
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 */
98 c4iw_init_dev_ucontext(rdev, &rdev->uctx);
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.
105 if (udb_density != ucq_density) {
106 device_printf(sc->dev, "unsupported udb/ucq densities %u/%u\n",
107 udb_density, ucq_density);
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,
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;
126 if (c4iw_num_stags(rdev) == 0) {
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;
137 rc = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
139 device_printf(sc->dev, "error %d initializing resources\n", rc);
142 rc = c4iw_pblpool_create(rdev);
144 device_printf(sc->dev, "error %d initializing pbl pool\n", rc);
147 rc = c4iw_rqtpool_create(rdev);
149 device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
152 rdev->status_page = (struct t4_dev_status_page *)
153 __get_free_page(GFP_KERNEL);
154 if (!rdev->status_page) {
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;
163 /* T5 and above devices don't need Doorbell recovery logic,
164 * so db_off is always set to '0'.
166 rdev->status_page->db_off = 0;
168 rdev->status_page->wc_supported = rdev->adap->iwt.wc_en;
170 rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
171 if (!rdev->free_workq) {
177 free_page((unsigned long)rdev->status_page);
179 c4iw_rqtpool_destroy(rdev);
181 c4iw_pblpool_destroy(rdev);
183 c4iw_destroy_resource(&rdev->resource);
188 static void c4iw_rdev_close(struct c4iw_rdev *rdev)
190 free_page((unsigned long)rdev->status_page);
191 c4iw_pblpool_destroy(rdev);
192 c4iw_rqtpool_destroy(rdev);
193 c4iw_destroy_resource(&rdev->resource);
197 c4iw_dealloc(struct c4iw_dev *iwsc)
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);
207 static struct c4iw_dev *
208 c4iw_alloc(struct adapter *sc)
210 struct c4iw_dev *iwsc;
213 iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
215 device_printf(sc->dev, "Cannot allocate ib device.\n");
216 return (ERR_PTR(-ENOMEM));
218 iwsc->rdev.adap = sc;
220 /* init various hw-queue params based on lld info */
221 iwsc->rdev.hw_queue.t4_eq_status_entries =
222 sc->params.sge.spg_len / EQ_ESIZE;
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 -
226 iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
227 iwsc->rdev.hw_queue.t4_max_sq_size =
228 iwsc->rdev.hw_queue.t4_max_eq_size -
229 iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
230 iwsc->rdev.hw_queue.t4_max_qp_depth =
231 iwsc->rdev.hw_queue.t4_max_rq_size;
232 iwsc->rdev.hw_queue.t4_max_cq_depth =
233 iwsc->rdev.hw_queue.t4_max_iq_size - 2;
234 iwsc->rdev.hw_queue.t4_stat_len = iwsc->rdev.adap->params.sge.spg_len;
236 /* As T5 and above devices support BAR2 kernel doorbells & WC, we map
237 * all of BAR2, for both User and Kernel Doorbells-GTS.
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);
243 rc = c4iw_rdev_open(&iwsc->rdev);
245 device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
246 ib_dealloc_device(&iwsc->ibdev);
247 return (ERR_PTR(rc));
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);
255 iwsc->avail_ird = iwsc->rdev.adap->params.max_ird_adapter;
260 static int c4iw_mod_load(void);
261 static int c4iw_mod_unload(void);
262 static int c4iw_activate(struct adapter *);
263 static int c4iw_deactivate(struct adapter *);
265 static struct uld_info c4iw_uld_info = {
267 .activate = c4iw_activate,
268 .deactivate = c4iw_deactivate,
272 c4iw_activate(struct adapter *sc)
274 struct c4iw_dev *iwsc;
277 ASSERT_SYNCHRONIZED_OP(sc);
280 device_printf(sc->dev, "No iWARP support for T4 devices, "
281 "please install T5 or above devices.\n");
285 if (uld_active(sc, ULD_IWARP)) {
286 KASSERT(0, ("%s: RDMA already eanbled on sc %p", __func__, sc));
290 if (sc->rdmacaps == 0) {
291 device_printf(sc->dev,
292 "RDMA not supported or RDMA cap is not enabled.\n");
296 iwsc = c4iw_alloc(sc);
299 device_printf(sc->dev, "initialization failed: %d\n", rc);
303 sc->iwarp_softc = iwsc;
305 rc = -c4iw_register_device(iwsc);
307 device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
309 sc->iwarp_softc = NULL;
316 c4iw_deactivate(struct adapter *sc)
318 struct c4iw_dev *iwsc = sc->iwarp_softc;
320 ASSERT_SYNCHRONIZED_OP(sc);
322 c4iw_unregister_device(iwsc);
324 sc->iwarp_softc = NULL;
330 c4iw_activate_all(struct adapter *sc, void *arg __unused)
333 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
336 /* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
337 if (sc->offload_map && !uld_active(sc, ULD_IWARP))
338 (void) t4_activate_uld(sc, ULD_IWARP);
340 end_synchronized_op(sc, 0);
344 c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
347 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
350 if (uld_active(sc, ULD_IWARP))
351 (void) t4_deactivate_uld(sc, ULD_IWARP);
353 end_synchronized_op(sc, 0);
361 rc = -c4iw_cm_init();
365 rc = t4_register_uld(&c4iw_uld_info);
371 t4_iterate(c4iw_activate_all, NULL);
377 c4iw_mod_unload(void)
380 t4_iterate(c4iw_deactivate_all, NULL);
384 if (t4_unregister_uld(&c4iw_uld_info) == EBUSY)
393 * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
394 * on t4_tom ensures that it won't either. So we don't directly check for
398 c4iw_modevent(module_t mod, int cmd, void *arg)
405 rc = c4iw_mod_load();
407 printf("iw_cxgbe: Chelsio T5/T6 RDMA driver loaded.\n");
411 rc = c4iw_mod_unload();
418 printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
424 static moduledata_t c4iw_mod_data = {
430 MODULE_VERSION(iw_cxgbe, 1);
431 MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
432 MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
433 MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
434 MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
435 DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);