xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/device.c (revision 19d9a9b15178ed7cfe3f463f43e28cce13fc4f94)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 
48 void
49 c4iw_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->cqids) {
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 
77 void
78 c4iw_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 
86 static int
87 c4iw_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 	unsigned short ucq_density = 1 << sp->iq_s_qpp; /* # of user CQs/page */
93 	unsigned short udb_density = 1 << sp->eq_s_qpp; /* # of user DB/page */
94 
95 
96 	c4iw_init_dev_ucontext(rdev, &rdev->uctx);
97 
98 	/*
99 	 * This implementation assumes udb_density == ucq_density!  Eventually
100 	 * we might need to support this but for now fail the open. Also the
101 	 * cqid and qpid range must match for now.
102 	 */
103 	if (udb_density != ucq_density) {
104 		device_printf(sc->dev, "unsupported udb/ucq densities %u/%u\n",
105 		    udb_density, ucq_density);
106 		rc = -EINVAL;
107 		goto err1;
108 	}
109 	if (sc->vres.qp.start != sc->vres.cq.start ||
110 	    sc->vres.qp.size != sc->vres.cq.size) {
111 		device_printf(sc->dev, "%s: unsupported qp and cq id ranges "
112 			"qp start %u size %u cq start %u size %u\n", __func__,
113 			sc->vres.qp.start, sc->vres.qp.size, sc->vres.cq.start,
114 			sc->vres.cq.size);
115 		rc = -EINVAL;
116 		goto err1;
117 	}
118 
119 	rdev->qpshift = PAGE_SHIFT - sp->eq_s_qpp;
120 	rdev->qpmask = udb_density - 1;
121 	rdev->cqshift = PAGE_SHIFT - sp->iq_s_qpp;
122 	rdev->cqmask = ucq_density - 1;
123 
124 	if (c4iw_num_stags(rdev) == 0) {
125 		rc = -EINVAL;
126 		goto err1;
127 	}
128 
129 	rdev->stats.pd.total = T4_MAX_NUM_PD;
130 	rdev->stats.stag.total = sc->vres.stag.size;
131 	rdev->stats.pbl.total = sc->vres.pbl.size;
132 	rdev->stats.rqt.total = sc->vres.rq.size;
133 	rdev->stats.qid.total = sc->vres.qp.size;
134 
135 	rc = c4iw_init_resource(rdev, T4_MAX_NUM_PD);
136 	if (rc) {
137 		device_printf(sc->dev, "error %d initializing resources\n", rc);
138 		goto err1;
139 	}
140 	rc = c4iw_rqtpool_create(rdev);
141 	if (rc) {
142 		device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
143 		goto err2;
144 	}
145 	rdev->status_page = (struct t4_dev_status_page *)
146 				__get_free_page(GFP_KERNEL);
147 	if (!rdev->status_page) {
148 		rc = -ENOMEM;
149 		goto err3;
150 	}
151 	rdev->status_page->qp_start = sc->vres.qp.start;
152 	rdev->status_page->qp_size = sc->vres.qp.size;
153 	rdev->status_page->cq_start = sc->vres.cq.start;
154 	rdev->status_page->cq_size = sc->vres.cq.size;
155 
156 	/* T5 and above devices don't need Doorbell recovery logic,
157 	 * so db_off is always set to '0'.
158 	 */
159 	rdev->status_page->db_off = 0;
160 
161 	rdev->status_page->wc_supported = rdev->adap->iwt.wc_en;
162 
163 	rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
164 	if (!rdev->free_workq) {
165 		rc = -ENOMEM;
166 		goto err4;
167 	}
168 	return (0);
169 err4:
170 	free_page((unsigned long)rdev->status_page);
171 err3:
172 	c4iw_rqtpool_destroy(rdev);
173 err2:
174 	c4iw_destroy_resource(&rdev->resource);
175 err1:
176 	return (rc);
177 }
178 
179 static void c4iw_rdev_close(struct c4iw_rdev *rdev)
180 {
181 	free_page((unsigned long)rdev->status_page);
182 	c4iw_rqtpool_destroy(rdev);
183 	c4iw_destroy_resource(&rdev->resource);
184 }
185 
186 static void
187 c4iw_dealloc(struct c4iw_dev *iwsc)
188 {
189 
190 	c4iw_rdev_close(&iwsc->rdev);
191 	idr_destroy(&iwsc->cqidr);
192 	idr_destroy(&iwsc->qpidr);
193 	idr_destroy(&iwsc->mmidr);
194 	ib_dealloc_device(&iwsc->ibdev);
195 }
196 
197 static struct c4iw_dev *
198 c4iw_alloc(struct adapter *sc)
199 {
200 	struct c4iw_dev *iwsc;
201 	int rc;
202 
203 	iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
204 	if (iwsc == NULL) {
205 		device_printf(sc->dev, "Cannot allocate ib device.\n");
206 		return (ERR_PTR(-ENOMEM));
207 	}
208 	iwsc->rdev.adap = sc;
209 
210 	/* init various hw-queue params based on lld info */
211 	iwsc->rdev.hw_queue.t4_eq_status_entries =
212 		sc->params.sge.spg_len / EQ_ESIZE;
213 	iwsc->rdev.hw_queue.t4_max_eq_size = 65520;
214 	iwsc->rdev.hw_queue.t4_max_iq_size = 65520;
215 	iwsc->rdev.hw_queue.t4_max_rq_size = 8192 -
216 		iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
217 	iwsc->rdev.hw_queue.t4_max_sq_size =
218 		iwsc->rdev.hw_queue.t4_max_eq_size -
219 		iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
220 	iwsc->rdev.hw_queue.t4_max_qp_depth =
221 		iwsc->rdev.hw_queue.t4_max_rq_size;
222 	iwsc->rdev.hw_queue.t4_max_cq_depth =
223 		iwsc->rdev.hw_queue.t4_max_iq_size - 2;
224 	iwsc->rdev.hw_queue.t4_stat_len = iwsc->rdev.adap->params.sge.spg_len;
225 
226 	/* As T5 and above devices support BAR2 kernel doorbells & WC, we map
227 	 * all of BAR2, for both User and Kernel Doorbells-GTS.
228 	 */
229 	iwsc->rdev.bar2_kva = (void __iomem *)((u64)iwsc->rdev.adap->udbs_base);
230 	iwsc->rdev.bar2_pa = vtophys(iwsc->rdev.adap->udbs_base);
231 	iwsc->rdev.bar2_len = rman_get_size(iwsc->rdev.adap->udbs_res);
232 
233 	rc = c4iw_rdev_open(&iwsc->rdev);
234 	if (rc != 0) {
235 		device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
236 		ib_dealloc_device(&iwsc->ibdev);
237 		return (ERR_PTR(rc));
238 	}
239 
240 	idr_init(&iwsc->cqidr);
241 	idr_init(&iwsc->qpidr);
242 	idr_init(&iwsc->mmidr);
243 	spin_lock_init(&iwsc->lock);
244 	mutex_init(&iwsc->rdev.stats.lock);
245 	iwsc->avail_ird = iwsc->rdev.adap->params.max_ird_adapter;
246 
247 	return (iwsc);
248 }
249 
250 static int c4iw_mod_load(void);
251 static int c4iw_mod_unload(void);
252 static int c4iw_activate(struct adapter *);
253 static int c4iw_deactivate(struct adapter *);
254 static int c4iw_stop(struct adapter *);
255 static int c4iw_restart(struct adapter *);
256 
257 static struct uld_info c4iw_uld_info = {
258 	.uld_activate = c4iw_activate,
259 	.uld_deactivate = c4iw_deactivate,
260 	.uld_stop = c4iw_stop,
261 	.uld_restart = c4iw_restart,
262 };
263 
264 static int
265 c4iw_activate(struct adapter *sc)
266 {
267 	struct c4iw_dev *iwsc;
268 	int rc;
269 
270 	ASSERT_SYNCHRONIZED_OP(sc);
271 
272 	if (is_t4(sc)) {
273 		device_printf(sc->dev, "No iWARP support for T4 devices, "
274 				"please install T5 or above devices.\n");
275 		return (ENOSYS);
276 	}
277 
278 	if (uld_active(sc, ULD_IWARP)) {
279 		KASSERT(0, ("%s: RDMA already enabled on sc %p", __func__, sc));
280 		return (0);
281 	}
282 
283 	if (sc->rdmacaps == 0) {
284 		device_printf(sc->dev,
285 		    "RDMA not supported or RDMA cap is not enabled.\n");
286 		return (ENOSYS);
287 	}
288 
289 	iwsc = c4iw_alloc(sc);
290 	if (IS_ERR(iwsc)) {
291 		rc = -PTR_ERR(iwsc);
292 		device_printf(sc->dev, "initialization failed: %d\n", rc);
293 		return (rc);
294 	}
295 
296 	sc->iwarp_softc = iwsc;
297 
298 	rc = -c4iw_register_device(iwsc);
299 	if (rc) {
300 		device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
301 		c4iw_dealloc(iwsc);
302 		sc->iwarp_softc = NULL;
303 	}
304 
305 	return (rc);
306 }
307 
308 static int
309 c4iw_deactivate(struct adapter *sc)
310 {
311 	struct c4iw_dev *iwsc = sc->iwarp_softc;
312 
313 	ASSERT_SYNCHRONIZED_OP(sc);
314 
315 	c4iw_unregister_device(iwsc);
316 	c4iw_dealloc(iwsc);
317 	sc->iwarp_softc = NULL;
318 
319 	return (0);
320 }
321 
322 static int
323 c4iw_stop(struct adapter *sc)
324 {
325 	struct c4iw_dev *iwsc = sc->iwarp_softc;
326 
327 	if (iwsc) {
328 		struct ib_event event = {0};
329 
330 		device_printf(sc->dev, "iWARP driver stopped.\n");
331 		iwsc->rdev.flags |= T4_IW_STOPPED;
332 		event.event  = IB_EVENT_DEVICE_FATAL;
333 		event.device = &iwsc->ibdev;
334 		ib_dispatch_event(&event);
335 	}
336 
337 	return (0);
338 }
339 
340 static int
341 c4iw_restart(struct adapter *sc)
342 {
343 	struct c4iw_dev *iwsc = sc->iwarp_softc;
344 
345 	if (iwsc) {
346 		device_printf(sc->dev, "iWARP driver restarted.\n");
347 		iwsc->rdev.flags &= ~T4_IW_STOPPED;
348 	}
349 	return (0);
350 }
351 
352 static void
353 c4iw_activate_all(struct adapter *sc, void *arg __unused)
354 {
355 
356 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
357 		return;
358 
359 	/* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
360 	if (sc->offload_map && !uld_active(sc, ULD_IWARP))
361 		(void) t4_activate_uld(sc, ULD_IWARP);
362 
363 	end_synchronized_op(sc, 0);
364 }
365 
366 static void
367 c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
368 {
369 
370 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
371 		return;
372 
373 	if (uld_active(sc, ULD_IWARP))
374 	    (void) t4_deactivate_uld(sc, ULD_IWARP);
375 
376 	end_synchronized_op(sc, 0);
377 }
378 
379 static int
380 c4iw_mod_load(void)
381 {
382 	int rc;
383 
384 	rc = -c4iw_cm_init();
385 	if (rc != 0)
386 		return (rc);
387 
388 	rc = t4_register_uld(&c4iw_uld_info, ULD_IWARP);
389 	if (rc != 0) {
390 		c4iw_cm_term();
391 		return (rc);
392 	}
393 
394 	t4_iterate(c4iw_activate_all, NULL);
395 
396 	return (rc);
397 }
398 
399 static int
400 c4iw_mod_unload(void)
401 {
402 
403 	t4_iterate(c4iw_deactivate_all, NULL);
404 
405 	c4iw_cm_term();
406 
407 	if (t4_unregister_uld(&c4iw_uld_info, ULD_IWARP) == EBUSY)
408 		return (EBUSY);
409 
410 	return (0);
411 }
412 
413 #endif
414 
415 /*
416  * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
417  * on t4_tom ensures that it won't either.  So we don't directly check for
418  * TCP_OFFLOAD here.
419  */
420 static int
421 c4iw_modevent(module_t mod, int cmd, void *arg)
422 {
423 	int rc = 0;
424 
425 #ifdef TCP_OFFLOAD
426 	switch (cmd) {
427 	case MOD_LOAD:
428 		rc = c4iw_mod_load();
429 		if (rc == 0)
430 			printf("iw_cxgbe: Chelsio T5/T6 RDMA driver loaded.\n");
431 		break;
432 
433 	case MOD_UNLOAD:
434 		rc = c4iw_mod_unload();
435 		break;
436 
437 	default:
438 		rc = EINVAL;
439 	}
440 #else
441 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
442 	rc = EOPNOTSUPP;
443 #endif
444 	return (rc);
445 }
446 
447 static moduledata_t c4iw_mod_data = {
448 	"iw_cxgbe",
449 	c4iw_modevent,
450 	0
451 };
452 
453 MODULE_VERSION(iw_cxgbe, 1);
454 MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
455 MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
456 MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
457 MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
458 DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
459