xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/device.c (revision b5332809c633e7e37715f7823a8a8ee9799910a4)
1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4fb93f5c4SNavdeep Parhar  * Copyright (c) 2009-2013 Chelsio, Inc. All rights reserved.
5fb93f5c4SNavdeep Parhar  *
6fb93f5c4SNavdeep Parhar  * This software is available to you under a choice of one of two
7fb93f5c4SNavdeep Parhar  * licenses.  You may choose to be licensed under the terms of the GNU
8fb93f5c4SNavdeep Parhar  * General Public License (GPL) Version 2, available from the file
9fb93f5c4SNavdeep Parhar  * COPYING in the main directory of this source tree, or the
10fb93f5c4SNavdeep Parhar  * OpenIB.org BSD license below:
11fb93f5c4SNavdeep Parhar  *
12fb93f5c4SNavdeep Parhar  *     Redistribution and use in source and binary forms, with or
13fb93f5c4SNavdeep Parhar  *     without modification, are permitted provided that the following
14fb93f5c4SNavdeep Parhar  *     conditions are met:
15fb93f5c4SNavdeep Parhar  *
16fb93f5c4SNavdeep Parhar  *      - Redistributions of source code must retain the above
17fb93f5c4SNavdeep Parhar  *	  copyright notice, this list of conditions and the following
18fb93f5c4SNavdeep Parhar  *	  disclaimer.
19fb93f5c4SNavdeep Parhar  *
20fb93f5c4SNavdeep Parhar  *      - Redistributions in binary form must reproduce the above
21fb93f5c4SNavdeep Parhar  *	  copyright notice, this list of conditions and the following
22fb93f5c4SNavdeep Parhar  *	  disclaimer in the documentation and/or other materials
23fb93f5c4SNavdeep Parhar  *	  provided with the distribution.
24fb93f5c4SNavdeep Parhar  *
25fb93f5c4SNavdeep Parhar  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26fb93f5c4SNavdeep Parhar  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27fb93f5c4SNavdeep Parhar  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28fb93f5c4SNavdeep Parhar  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29fb93f5c4SNavdeep Parhar  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30fb93f5c4SNavdeep Parhar  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31fb93f5c4SNavdeep Parhar  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32fb93f5c4SNavdeep Parhar  * SOFTWARE.
33fb93f5c4SNavdeep Parhar  */
34fb93f5c4SNavdeep Parhar #include <sys/cdefs.h>
35fb93f5c4SNavdeep Parhar #include "opt_inet.h"
36fb93f5c4SNavdeep Parhar 
37fb93f5c4SNavdeep Parhar #include <sys/ktr.h>
38fb93f5c4SNavdeep Parhar 
39fb93f5c4SNavdeep Parhar #include <linux/module.h>
40fb93f5c4SNavdeep Parhar #include <linux/moduleparam.h>
41fb93f5c4SNavdeep Parhar 
42fb93f5c4SNavdeep Parhar #include <rdma/ib_verbs.h>
43fb93f5c4SNavdeep Parhar #include <linux/idr.h>
44fb93f5c4SNavdeep Parhar 
45fb93f5c4SNavdeep Parhar #ifdef TCP_OFFLOAD
46fb93f5c4SNavdeep Parhar #include "iw_cxgbe.h"
47fb93f5c4SNavdeep Parhar 
48fb93f5c4SNavdeep Parhar void
49fb93f5c4SNavdeep Parhar c4iw_release_dev_ucontext(struct c4iw_rdev *rdev,
50fb93f5c4SNavdeep Parhar     struct c4iw_dev_ucontext *uctx)
51fb93f5c4SNavdeep Parhar {
52fb93f5c4SNavdeep Parhar 	struct list_head *pos, *nxt;
53fb93f5c4SNavdeep Parhar 	struct c4iw_qid_list *entry;
54fb93f5c4SNavdeep Parhar 
55fb93f5c4SNavdeep Parhar 	mutex_lock(&uctx->lock);
56fb93f5c4SNavdeep Parhar 	list_for_each_safe(pos, nxt, &uctx->qpids) {
57fb93f5c4SNavdeep Parhar 		entry = list_entry(pos, struct c4iw_qid_list, entry);
58fb93f5c4SNavdeep Parhar 		list_del_init(&entry->entry);
59fb93f5c4SNavdeep Parhar 		if (!(entry->qid & rdev->qpmask)) {
60fb93f5c4SNavdeep Parhar 			c4iw_put_resource(&rdev->resource.qid_table,
61fb93f5c4SNavdeep Parhar 					  entry->qid);
62fb93f5c4SNavdeep Parhar 			mutex_lock(&rdev->stats.lock);
63fb93f5c4SNavdeep Parhar 			rdev->stats.qid.cur -= rdev->qpmask + 1;
64fb93f5c4SNavdeep Parhar 			mutex_unlock(&rdev->stats.lock);
65fb93f5c4SNavdeep Parhar 		}
66fb93f5c4SNavdeep Parhar 		kfree(entry);
67fb93f5c4SNavdeep Parhar 	}
68fb93f5c4SNavdeep Parhar 
695c2bacdeSNavdeep Parhar 	list_for_each_safe(pos, nxt, &uctx->cqids) {
70fb93f5c4SNavdeep Parhar 		entry = list_entry(pos, struct c4iw_qid_list, entry);
71fb93f5c4SNavdeep Parhar 		list_del_init(&entry->entry);
72fb93f5c4SNavdeep Parhar 		kfree(entry);
73fb93f5c4SNavdeep Parhar 	}
74fb93f5c4SNavdeep Parhar 	mutex_unlock(&uctx->lock);
75fb93f5c4SNavdeep Parhar }
76fb93f5c4SNavdeep Parhar 
77fb93f5c4SNavdeep Parhar void
78fb93f5c4SNavdeep Parhar c4iw_init_dev_ucontext(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
79fb93f5c4SNavdeep Parhar {
80fb93f5c4SNavdeep Parhar 
81fb93f5c4SNavdeep Parhar 	INIT_LIST_HEAD(&uctx->qpids);
82fb93f5c4SNavdeep Parhar 	INIT_LIST_HEAD(&uctx->cqids);
83fb93f5c4SNavdeep Parhar 	mutex_init(&uctx->lock);
84fb93f5c4SNavdeep Parhar }
85fb93f5c4SNavdeep Parhar 
86fb93f5c4SNavdeep Parhar static int
87fb93f5c4SNavdeep Parhar c4iw_rdev_open(struct c4iw_rdev *rdev)
88fb93f5c4SNavdeep Parhar {
89fb93f5c4SNavdeep Parhar 	struct adapter *sc = rdev->adap;
9090e7434aSNavdeep Parhar 	struct sge_params *sp = &sc->params.sge;
91fb93f5c4SNavdeep Parhar 	int rc;
925c2bacdeSNavdeep Parhar 	unsigned short ucq_density = 1 << sp->iq_s_qpp; /* # of user CQs/page */
935c2bacdeSNavdeep Parhar 	unsigned short udb_density = 1 << sp->eq_s_qpp; /* # of user DB/page */
945c2bacdeSNavdeep Parhar 
95fb93f5c4SNavdeep Parhar 
96fb93f5c4SNavdeep Parhar 	c4iw_init_dev_ucontext(rdev, &rdev->uctx);
97fb93f5c4SNavdeep Parhar 
985c2bacdeSNavdeep Parhar 	/*
995c2bacdeSNavdeep Parhar 	 * This implementation assumes udb_density == ucq_density!  Eventually
1005c2bacdeSNavdeep Parhar 	 * we might need to support this but for now fail the open. Also the
1015c2bacdeSNavdeep Parhar 	 * cqid and qpid range must match for now.
1025c2bacdeSNavdeep Parhar 	 */
1035c2bacdeSNavdeep Parhar 	if (udb_density != ucq_density) {
1045c2bacdeSNavdeep Parhar 		device_printf(sc->dev, "unsupported udb/ucq densities %u/%u\n",
1055c2bacdeSNavdeep Parhar 		    udb_density, ucq_density);
1065c2bacdeSNavdeep Parhar 		rc = -EINVAL;
1075c2bacdeSNavdeep Parhar 		goto err1;
1085c2bacdeSNavdeep Parhar 	}
1095c2bacdeSNavdeep Parhar 	if (sc->vres.qp.start != sc->vres.cq.start ||
1105c2bacdeSNavdeep Parhar 	    sc->vres.qp.size != sc->vres.cq.size) {
1115c2bacdeSNavdeep Parhar 		device_printf(sc->dev, "%s: unsupported qp and cq id ranges "
1125c2bacdeSNavdeep Parhar 			"qp start %u size %u cq start %u size %u\n", __func__,
1135c2bacdeSNavdeep Parhar 			sc->vres.qp.start, sc->vres.qp.size, sc->vres.cq.start,
1145c2bacdeSNavdeep Parhar 			sc->vres.cq.size);
115fb93f5c4SNavdeep Parhar 		rc = -EINVAL;
116fb93f5c4SNavdeep Parhar 		goto err1;
117fb93f5c4SNavdeep Parhar 	}
118fb93f5c4SNavdeep Parhar 
11990e7434aSNavdeep Parhar 	rdev->qpshift = PAGE_SHIFT - sp->eq_s_qpp;
1205c2bacdeSNavdeep Parhar 	rdev->qpmask = udb_density - 1;
12190e7434aSNavdeep Parhar 	rdev->cqshift = PAGE_SHIFT - sp->iq_s_qpp;
1225c2bacdeSNavdeep Parhar 	rdev->cqmask = ucq_density - 1;
123fb93f5c4SNavdeep Parhar 
124fb93f5c4SNavdeep Parhar 	if (c4iw_num_stags(rdev) == 0) {
125fb93f5c4SNavdeep Parhar 		rc = -EINVAL;
126fb93f5c4SNavdeep Parhar 		goto err1;
127fb93f5c4SNavdeep Parhar 	}
128fb93f5c4SNavdeep Parhar 
129fb93f5c4SNavdeep Parhar 	rdev->stats.pd.total = T4_MAX_NUM_PD;
130fb93f5c4SNavdeep Parhar 	rdev->stats.stag.total = sc->vres.stag.size;
131fb93f5c4SNavdeep Parhar 	rdev->stats.pbl.total = sc->vres.pbl.size;
132fb93f5c4SNavdeep Parhar 	rdev->stats.rqt.total = sc->vres.rq.size;
133fb93f5c4SNavdeep Parhar 	rdev->stats.qid.total = sc->vres.qp.size;
134fb93f5c4SNavdeep Parhar 
135fb93f5c4SNavdeep Parhar 	rc = c4iw_init_resource(rdev, c4iw_num_stags(rdev), T4_MAX_NUM_PD);
136fb93f5c4SNavdeep Parhar 	if (rc) {
137fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "error %d initializing resources\n", rc);
138fb93f5c4SNavdeep Parhar 		goto err1;
139fb93f5c4SNavdeep Parhar 	}
140fb93f5c4SNavdeep Parhar 	rc = c4iw_pblpool_create(rdev);
141fb93f5c4SNavdeep Parhar 	if (rc) {
142fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "error %d initializing pbl pool\n", rc);
143fb93f5c4SNavdeep Parhar 		goto err2;
144fb93f5c4SNavdeep Parhar 	}
145fb93f5c4SNavdeep Parhar 	rc = c4iw_rqtpool_create(rdev);
146fb93f5c4SNavdeep Parhar 	if (rc) {
147fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "error %d initializing rqt pool\n", rc);
148fb93f5c4SNavdeep Parhar 		goto err3;
149fb93f5c4SNavdeep Parhar 	}
1505c2bacdeSNavdeep Parhar 	rdev->status_page = (struct t4_dev_status_page *)
1515c2bacdeSNavdeep Parhar 				__get_free_page(GFP_KERNEL);
1525c2bacdeSNavdeep Parhar 	if (!rdev->status_page) {
1535c2bacdeSNavdeep Parhar 		rc = -ENOMEM;
1545c2bacdeSNavdeep Parhar 		goto err4;
1555c2bacdeSNavdeep Parhar 	}
1565c2bacdeSNavdeep Parhar 	rdev->status_page->qp_start = sc->vres.qp.start;
1575c2bacdeSNavdeep Parhar 	rdev->status_page->qp_size = sc->vres.qp.size;
1585c2bacdeSNavdeep Parhar 	rdev->status_page->cq_start = sc->vres.cq.start;
1595c2bacdeSNavdeep Parhar 	rdev->status_page->cq_size = sc->vres.cq.size;
160fb93f5c4SNavdeep Parhar 
1615c2bacdeSNavdeep Parhar 	/* T5 and above devices don't need Doorbell recovery logic,
1625c2bacdeSNavdeep Parhar 	 * so db_off is always set to '0'.
1635c2bacdeSNavdeep Parhar 	 */
1645c2bacdeSNavdeep Parhar 	rdev->status_page->db_off = 0;
1655c2bacdeSNavdeep Parhar 
1665c2bacdeSNavdeep Parhar 	rdev->status_page->wc_supported = rdev->adap->iwt.wc_en;
1675c2bacdeSNavdeep Parhar 
1685c2bacdeSNavdeep Parhar 	rdev->free_workq = create_singlethread_workqueue("iw_cxgb4_free");
1695c2bacdeSNavdeep Parhar 	if (!rdev->free_workq) {
1705c2bacdeSNavdeep Parhar 		rc = -ENOMEM;
1715c2bacdeSNavdeep Parhar 		goto err5;
1725c2bacdeSNavdeep Parhar 	}
173fb93f5c4SNavdeep Parhar 	return (0);
1745c2bacdeSNavdeep Parhar err5:
1755c2bacdeSNavdeep Parhar 	free_page((unsigned long)rdev->status_page);
1765c2bacdeSNavdeep Parhar err4:
1775c2bacdeSNavdeep Parhar 	c4iw_rqtpool_destroy(rdev);
178fb93f5c4SNavdeep Parhar err3:
179fb93f5c4SNavdeep Parhar 	c4iw_pblpool_destroy(rdev);
180fb93f5c4SNavdeep Parhar err2:
181fb93f5c4SNavdeep Parhar 	c4iw_destroy_resource(&rdev->resource);
182fb93f5c4SNavdeep Parhar err1:
183fb93f5c4SNavdeep Parhar 	return (rc);
184fb93f5c4SNavdeep Parhar }
185fb93f5c4SNavdeep Parhar 
186fb93f5c4SNavdeep Parhar static void c4iw_rdev_close(struct c4iw_rdev *rdev)
187fb93f5c4SNavdeep Parhar {
1885c2bacdeSNavdeep Parhar 	free_page((unsigned long)rdev->status_page);
189fb93f5c4SNavdeep Parhar 	c4iw_pblpool_destroy(rdev);
190fb93f5c4SNavdeep Parhar 	c4iw_rqtpool_destroy(rdev);
191fb93f5c4SNavdeep Parhar 	c4iw_destroy_resource(&rdev->resource);
192fb93f5c4SNavdeep Parhar }
193fb93f5c4SNavdeep Parhar 
194fb93f5c4SNavdeep Parhar static void
195fb93f5c4SNavdeep Parhar c4iw_dealloc(struct c4iw_dev *iwsc)
196fb93f5c4SNavdeep Parhar {
197fb93f5c4SNavdeep Parhar 
198fb93f5c4SNavdeep Parhar 	c4iw_rdev_close(&iwsc->rdev);
199fb93f5c4SNavdeep Parhar 	idr_destroy(&iwsc->cqidr);
200fb93f5c4SNavdeep Parhar 	idr_destroy(&iwsc->qpidr);
201fb93f5c4SNavdeep Parhar 	idr_destroy(&iwsc->mmidr);
202fb93f5c4SNavdeep Parhar 	ib_dealloc_device(&iwsc->ibdev);
203fb93f5c4SNavdeep Parhar }
204fb93f5c4SNavdeep Parhar 
205fb93f5c4SNavdeep Parhar static struct c4iw_dev *
206fb93f5c4SNavdeep Parhar c4iw_alloc(struct adapter *sc)
207fb93f5c4SNavdeep Parhar {
208fb93f5c4SNavdeep Parhar 	struct c4iw_dev *iwsc;
209fb93f5c4SNavdeep Parhar 	int rc;
210fb93f5c4SNavdeep Parhar 
211fb93f5c4SNavdeep Parhar 	iwsc = (struct c4iw_dev *)ib_alloc_device(sizeof(*iwsc));
212fb93f5c4SNavdeep Parhar 	if (iwsc == NULL) {
213fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "Cannot allocate ib device.\n");
214fb93f5c4SNavdeep Parhar 		return (ERR_PTR(-ENOMEM));
215fb93f5c4SNavdeep Parhar 	}
216fb93f5c4SNavdeep Parhar 	iwsc->rdev.adap = sc;
217fb93f5c4SNavdeep Parhar 
2185c2bacdeSNavdeep Parhar 	/* init various hw-queue params based on lld info */
2195c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_eq_status_entries =
2205c2bacdeSNavdeep Parhar 		sc->params.sge.spg_len / EQ_ESIZE;
2215c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_max_eq_size = 65520;
2225c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_max_iq_size = 65520;
2235c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_max_rq_size = 8192 -
2245c2bacdeSNavdeep Parhar 		iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
2255c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_max_sq_size =
2265c2bacdeSNavdeep Parhar 		iwsc->rdev.hw_queue.t4_max_eq_size -
2275c2bacdeSNavdeep Parhar 		iwsc->rdev.hw_queue.t4_eq_status_entries - 1;
2285c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_max_qp_depth =
2295c2bacdeSNavdeep Parhar 		iwsc->rdev.hw_queue.t4_max_rq_size;
2305c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_max_cq_depth =
2315c2bacdeSNavdeep Parhar 		iwsc->rdev.hw_queue.t4_max_iq_size - 2;
2325c2bacdeSNavdeep Parhar 	iwsc->rdev.hw_queue.t4_stat_len = iwsc->rdev.adap->params.sge.spg_len;
2335c2bacdeSNavdeep Parhar 
2345c2bacdeSNavdeep Parhar 	/* As T5 and above devices support BAR2 kernel doorbells & WC, we map
2355c2bacdeSNavdeep Parhar 	 * all of BAR2, for both User and Kernel Doorbells-GTS.
2365c2bacdeSNavdeep Parhar 	 */
2375c2bacdeSNavdeep Parhar 	iwsc->rdev.bar2_kva = (void __iomem *)((u64)iwsc->rdev.adap->udbs_base);
2385c2bacdeSNavdeep Parhar 	iwsc->rdev.bar2_pa = vtophys(iwsc->rdev.adap->udbs_base);
2395c2bacdeSNavdeep Parhar 	iwsc->rdev.bar2_len = rman_get_size(iwsc->rdev.adap->udbs_res);
2405c2bacdeSNavdeep Parhar 
241fb93f5c4SNavdeep Parhar 	rc = c4iw_rdev_open(&iwsc->rdev);
242fb93f5c4SNavdeep Parhar 	if (rc != 0) {
243fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "Unable to open CXIO rdev (%d)\n", rc);
244fb93f5c4SNavdeep Parhar 		ib_dealloc_device(&iwsc->ibdev);
245fb93f5c4SNavdeep Parhar 		return (ERR_PTR(rc));
246fb93f5c4SNavdeep Parhar 	}
247fb93f5c4SNavdeep Parhar 
248fb93f5c4SNavdeep Parhar 	idr_init(&iwsc->cqidr);
249fb93f5c4SNavdeep Parhar 	idr_init(&iwsc->qpidr);
250fb93f5c4SNavdeep Parhar 	idr_init(&iwsc->mmidr);
251fb93f5c4SNavdeep Parhar 	spin_lock_init(&iwsc->lock);
252fb93f5c4SNavdeep Parhar 	mutex_init(&iwsc->rdev.stats.lock);
2535c2bacdeSNavdeep Parhar 	iwsc->avail_ird = iwsc->rdev.adap->params.max_ird_adapter;
254fb93f5c4SNavdeep Parhar 
255fb93f5c4SNavdeep Parhar 	return (iwsc);
256fb93f5c4SNavdeep Parhar }
257fb93f5c4SNavdeep Parhar 
258fb93f5c4SNavdeep Parhar static int c4iw_mod_load(void);
259fb93f5c4SNavdeep Parhar static int c4iw_mod_unload(void);
260fb93f5c4SNavdeep Parhar static int c4iw_activate(struct adapter *);
261fb93f5c4SNavdeep Parhar static int c4iw_deactivate(struct adapter *);
262cf5e6370SNavdeep Parhar static int c4iw_stop(struct adapter *);
263f1c4ed15SNavdeep Parhar static int c4iw_restart(struct adapter *);
264fb93f5c4SNavdeep Parhar 
265fb93f5c4SNavdeep Parhar static struct uld_info c4iw_uld_info = {
266cf5e6370SNavdeep Parhar 	.uld_activate = c4iw_activate,
267cf5e6370SNavdeep Parhar 	.uld_deactivate = c4iw_deactivate,
268cf5e6370SNavdeep Parhar 	.uld_stop = c4iw_stop,
269f1c4ed15SNavdeep Parhar 	.uld_restart = c4iw_restart,
270fb93f5c4SNavdeep Parhar };
271fb93f5c4SNavdeep Parhar 
272fb93f5c4SNavdeep Parhar static int
273fb93f5c4SNavdeep Parhar c4iw_activate(struct adapter *sc)
274fb93f5c4SNavdeep Parhar {
275fb93f5c4SNavdeep Parhar 	struct c4iw_dev *iwsc;
276fb93f5c4SNavdeep Parhar 	int rc;
277fb93f5c4SNavdeep Parhar 
278fb93f5c4SNavdeep Parhar 	ASSERT_SYNCHRONIZED_OP(sc);
279fb93f5c4SNavdeep Parhar 
2805c2bacdeSNavdeep Parhar 	if (is_t4(sc)) {
2815c2bacdeSNavdeep Parhar 		device_printf(sc->dev, "No iWARP support for T4 devices, "
2825c2bacdeSNavdeep Parhar 				"please install T5 or above devices.\n");
2835c2bacdeSNavdeep Parhar 		return (ENOSYS);
2845c2bacdeSNavdeep Parhar 	}
2855c2bacdeSNavdeep Parhar 
286b3d44a68SNavdeep Parhar 	if (uld_active(sc, ULD_IWARP)) {
287*b5332809SNavdeep Parhar 		KASSERT(0, ("%s: RDMA already enabled on sc %p", __func__, sc));
288fb93f5c4SNavdeep Parhar 		return (0);
289fb93f5c4SNavdeep Parhar 	}
290fb93f5c4SNavdeep Parhar 
291fb93f5c4SNavdeep Parhar 	if (sc->rdmacaps == 0) {
292fb93f5c4SNavdeep Parhar 		device_printf(sc->dev,
293fb93f5c4SNavdeep Parhar 		    "RDMA not supported or RDMA cap is not enabled.\n");
294fb93f5c4SNavdeep Parhar 		return (ENOSYS);
295fb93f5c4SNavdeep Parhar 	}
296fb93f5c4SNavdeep Parhar 
297fb93f5c4SNavdeep Parhar 	iwsc = c4iw_alloc(sc);
298fb93f5c4SNavdeep Parhar 	if (IS_ERR(iwsc)) {
299fb93f5c4SNavdeep Parhar 		rc = -PTR_ERR(iwsc);
300fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "initialization failed: %d\n", rc);
301fb93f5c4SNavdeep Parhar 		return (rc);
302fb93f5c4SNavdeep Parhar 	}
303fb93f5c4SNavdeep Parhar 
304fb93f5c4SNavdeep Parhar 	sc->iwarp_softc = iwsc;
305fb93f5c4SNavdeep Parhar 
306fb93f5c4SNavdeep Parhar 	rc = -c4iw_register_device(iwsc);
307fb93f5c4SNavdeep Parhar 	if (rc) {
308fb93f5c4SNavdeep Parhar 		device_printf(sc->dev, "RDMA registration failed: %d\n", rc);
309fb93f5c4SNavdeep Parhar 		c4iw_dealloc(iwsc);
310fb93f5c4SNavdeep Parhar 		sc->iwarp_softc = NULL;
311fb93f5c4SNavdeep Parhar 	}
312fb93f5c4SNavdeep Parhar 
313fb93f5c4SNavdeep Parhar 	return (rc);
314fb93f5c4SNavdeep Parhar }
315fb93f5c4SNavdeep Parhar 
316fb93f5c4SNavdeep Parhar static int
317fb93f5c4SNavdeep Parhar c4iw_deactivate(struct adapter *sc)
318fb93f5c4SNavdeep Parhar {
319fb93f5c4SNavdeep Parhar 	struct c4iw_dev *iwsc = sc->iwarp_softc;
320fb93f5c4SNavdeep Parhar 
321fb93f5c4SNavdeep Parhar 	ASSERT_SYNCHRONIZED_OP(sc);
322fb93f5c4SNavdeep Parhar 
323fb93f5c4SNavdeep Parhar 	c4iw_unregister_device(iwsc);
324fb93f5c4SNavdeep Parhar 	c4iw_dealloc(iwsc);
325fb93f5c4SNavdeep Parhar 	sc->iwarp_softc = NULL;
326fb93f5c4SNavdeep Parhar 
327fb93f5c4SNavdeep Parhar 	return (0);
328fb93f5c4SNavdeep Parhar }
329fb93f5c4SNavdeep Parhar 
330cf5e6370SNavdeep Parhar static int
331cf5e6370SNavdeep Parhar c4iw_stop(struct adapter *sc)
332b0dede77SNavdeep Parhar {
333b0dede77SNavdeep Parhar 	struct c4iw_dev *iwsc = sc->iwarp_softc;
334b0dede77SNavdeep Parhar 
335b0dede77SNavdeep Parhar 	if (iwsc) {
336b0dede77SNavdeep Parhar 		struct ib_event event = {0};
337b0dede77SNavdeep Parhar 
338b0dede77SNavdeep Parhar 		device_printf(sc->dev,
339b0dede77SNavdeep Parhar 			      "iWARP driver received FATAL ERROR event.\n");
340b0dede77SNavdeep Parhar 		iwsc->rdev.flags |= T4_FATAL_ERROR;
341b0dede77SNavdeep Parhar 		event.event  = IB_EVENT_DEVICE_FATAL;
342b0dede77SNavdeep Parhar 		event.device = &iwsc->ibdev;
343b0dede77SNavdeep Parhar 		ib_dispatch_event(&event);
344b0dede77SNavdeep Parhar 	}
345cf5e6370SNavdeep Parhar 
346cf5e6370SNavdeep Parhar 	return (0);
347b0dede77SNavdeep Parhar }
348b0dede77SNavdeep Parhar 
349f1c4ed15SNavdeep Parhar static int
350f1c4ed15SNavdeep Parhar c4iw_restart(struct adapter *sc)
351f1c4ed15SNavdeep Parhar {
352f1c4ed15SNavdeep Parhar 	return (0);
353f1c4ed15SNavdeep Parhar }
354f1c4ed15SNavdeep Parhar 
355b0dede77SNavdeep Parhar static void
356fb93f5c4SNavdeep Parhar c4iw_activate_all(struct adapter *sc, void *arg __unused)
357fb93f5c4SNavdeep Parhar {
358fb93f5c4SNavdeep Parhar 
359fb93f5c4SNavdeep Parhar 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
360fb93f5c4SNavdeep Parhar 		return;
361fb93f5c4SNavdeep Parhar 
362b3d44a68SNavdeep Parhar 	/* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
363b3d44a68SNavdeep Parhar 	if (sc->offload_map && !uld_active(sc, ULD_IWARP))
364b3d44a68SNavdeep Parhar 		(void) t4_activate_uld(sc, ULD_IWARP);
365fb93f5c4SNavdeep Parhar 
366fb93f5c4SNavdeep Parhar 	end_synchronized_op(sc, 0);
367fb93f5c4SNavdeep Parhar }
368fb93f5c4SNavdeep Parhar 
369fb93f5c4SNavdeep Parhar static void
370fb93f5c4SNavdeep Parhar c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
371fb93f5c4SNavdeep Parhar {
372fb93f5c4SNavdeep Parhar 
373fb93f5c4SNavdeep Parhar 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
374fb93f5c4SNavdeep Parhar 		return;
375fb93f5c4SNavdeep Parhar 
376b3d44a68SNavdeep Parhar 	if (uld_active(sc, ULD_IWARP))
377b3d44a68SNavdeep Parhar 	    (void) t4_deactivate_uld(sc, ULD_IWARP);
378fb93f5c4SNavdeep Parhar 
379fb93f5c4SNavdeep Parhar 	end_synchronized_op(sc, 0);
380fb93f5c4SNavdeep Parhar }
381fb93f5c4SNavdeep Parhar 
382fb93f5c4SNavdeep Parhar static int
383fb93f5c4SNavdeep Parhar c4iw_mod_load(void)
384fb93f5c4SNavdeep Parhar {
385fb93f5c4SNavdeep Parhar 	int rc;
386fb93f5c4SNavdeep Parhar 
387fb93f5c4SNavdeep Parhar 	rc = -c4iw_cm_init();
388fb93f5c4SNavdeep Parhar 	if (rc != 0)
389fb93f5c4SNavdeep Parhar 		return (rc);
390fb93f5c4SNavdeep Parhar 
391cf5e6370SNavdeep Parhar 	rc = t4_register_uld(&c4iw_uld_info, ULD_IWARP);
392fb93f5c4SNavdeep Parhar 	if (rc != 0) {
393fb93f5c4SNavdeep Parhar 		c4iw_cm_term();
394fb93f5c4SNavdeep Parhar 		return (rc);
395fb93f5c4SNavdeep Parhar 	}
396fb93f5c4SNavdeep Parhar 
397fb93f5c4SNavdeep Parhar 	t4_iterate(c4iw_activate_all, NULL);
398fb93f5c4SNavdeep Parhar 
399fb93f5c4SNavdeep Parhar 	return (rc);
400fb93f5c4SNavdeep Parhar }
401fb93f5c4SNavdeep Parhar 
402fb93f5c4SNavdeep Parhar static int
403fb93f5c4SNavdeep Parhar c4iw_mod_unload(void)
404fb93f5c4SNavdeep Parhar {
405fb93f5c4SNavdeep Parhar 
406fb93f5c4SNavdeep Parhar 	t4_iterate(c4iw_deactivate_all, NULL);
407fb93f5c4SNavdeep Parhar 
408fb93f5c4SNavdeep Parhar 	c4iw_cm_term();
409fb93f5c4SNavdeep Parhar 
410cf5e6370SNavdeep Parhar 	if (t4_unregister_uld(&c4iw_uld_info, ULD_IWARP) == EBUSY)
411fb93f5c4SNavdeep Parhar 		return (EBUSY);
412fb93f5c4SNavdeep Parhar 
413fb93f5c4SNavdeep Parhar 	return (0);
414fb93f5c4SNavdeep Parhar }
415fb93f5c4SNavdeep Parhar 
416fb93f5c4SNavdeep Parhar #endif
417fb93f5c4SNavdeep Parhar 
418fb93f5c4SNavdeep Parhar /*
419fb93f5c4SNavdeep Parhar  * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
420fb93f5c4SNavdeep Parhar  * on t4_tom ensures that it won't either.  So we don't directly check for
421fb93f5c4SNavdeep Parhar  * TCP_OFFLOAD here.
422fb93f5c4SNavdeep Parhar  */
423fb93f5c4SNavdeep Parhar static int
424fb93f5c4SNavdeep Parhar c4iw_modevent(module_t mod, int cmd, void *arg)
425fb93f5c4SNavdeep Parhar {
426fb93f5c4SNavdeep Parhar 	int rc = 0;
427fb93f5c4SNavdeep Parhar 
428fb93f5c4SNavdeep Parhar #ifdef TCP_OFFLOAD
429fb93f5c4SNavdeep Parhar 	switch (cmd) {
430fb93f5c4SNavdeep Parhar 	case MOD_LOAD:
431fb93f5c4SNavdeep Parhar 		rc = c4iw_mod_load();
432fb93f5c4SNavdeep Parhar 		if (rc == 0)
4335c2bacdeSNavdeep Parhar 			printf("iw_cxgbe: Chelsio T5/T6 RDMA driver loaded.\n");
434fb93f5c4SNavdeep Parhar 		break;
435fb93f5c4SNavdeep Parhar 
436fb93f5c4SNavdeep Parhar 	case MOD_UNLOAD:
437fb93f5c4SNavdeep Parhar 		rc = c4iw_mod_unload();
438fb93f5c4SNavdeep Parhar 		break;
439fb93f5c4SNavdeep Parhar 
440fb93f5c4SNavdeep Parhar 	default:
441fb93f5c4SNavdeep Parhar 		rc = EINVAL;
442fb93f5c4SNavdeep Parhar 	}
443fb93f5c4SNavdeep Parhar #else
444fb93f5c4SNavdeep Parhar 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
445fb93f5c4SNavdeep Parhar 	rc = EOPNOTSUPP;
446fb93f5c4SNavdeep Parhar #endif
447fb93f5c4SNavdeep Parhar 	return (rc);
448fb93f5c4SNavdeep Parhar }
449fb93f5c4SNavdeep Parhar 
450fb93f5c4SNavdeep Parhar static moduledata_t c4iw_mod_data = {
451fb93f5c4SNavdeep Parhar 	"iw_cxgbe",
452fb93f5c4SNavdeep Parhar 	c4iw_modevent,
453fb93f5c4SNavdeep Parhar 	0
454fb93f5c4SNavdeep Parhar };
455fb93f5c4SNavdeep Parhar 
456fb93f5c4SNavdeep Parhar MODULE_VERSION(iw_cxgbe, 1);
45787f804e8SNavdeep Parhar MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
458fb93f5c4SNavdeep Parhar MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
459fb93f5c4SNavdeep Parhar MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
4602da3897dSHans Petter Selasky MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
461fb93f5c4SNavdeep Parhar DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
462