xref: /freebsd/sys/dev/cxgbe/iw_cxgbe/device.c (revision 8254a276ad893ae2a1b35fcbbad255f06e29b8c6)
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
c4iw_release_dev_ucontext(struct c4iw_rdev * rdev,struct c4iw_dev_ucontext * uctx)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
c4iw_init_dev_ucontext(struct c4iw_rdev * rdev,struct c4iw_dev_ucontext * uctx)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
c4iw_rdev_open(struct c4iw_rdev * rdev)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 
c4iw_rdev_close(struct c4iw_rdev * rdev)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
c4iw_dealloc(struct c4iw_dev * iwsc)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 *
c4iw_alloc(struct adapter * sc)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
c4iw_activate(struct adapter * sc)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)) {
287b5332809SNavdeep 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
c4iw_deactivate(struct adapter * sc)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
c4iw_stop(struct adapter * sc)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 
338*8254a276SNavdeep Parhar 		device_printf(sc->dev, "iWARP driver stopped.\n");
339*8254a276SNavdeep Parhar 		iwsc->rdev.flags |= T4_IW_STOPPED;
340b0dede77SNavdeep Parhar 		event.event  = IB_EVENT_DEVICE_FATAL;
341b0dede77SNavdeep Parhar 		event.device = &iwsc->ibdev;
342b0dede77SNavdeep Parhar 		ib_dispatch_event(&event);
343b0dede77SNavdeep Parhar 	}
344cf5e6370SNavdeep Parhar 
345cf5e6370SNavdeep Parhar 	return (0);
346b0dede77SNavdeep Parhar }
347b0dede77SNavdeep Parhar 
348f1c4ed15SNavdeep Parhar static int
c4iw_restart(struct adapter * sc)349f1c4ed15SNavdeep Parhar c4iw_restart(struct adapter *sc)
350f1c4ed15SNavdeep Parhar {
351*8254a276SNavdeep Parhar 	struct c4iw_dev *iwsc = sc->iwarp_softc;
352*8254a276SNavdeep Parhar 
353*8254a276SNavdeep Parhar 	if (iwsc) {
354*8254a276SNavdeep Parhar 		device_printf(sc->dev, "iWARP driver restarted.\n");
355*8254a276SNavdeep Parhar 		iwsc->rdev.flags &= ~T4_IW_STOPPED;
356*8254a276SNavdeep Parhar 	}
357f1c4ed15SNavdeep Parhar 	return (0);
358f1c4ed15SNavdeep Parhar }
359f1c4ed15SNavdeep Parhar 
360b0dede77SNavdeep Parhar static void
c4iw_activate_all(struct adapter * sc,void * arg __unused)361fb93f5c4SNavdeep Parhar c4iw_activate_all(struct adapter *sc, void *arg __unused)
362fb93f5c4SNavdeep Parhar {
363fb93f5c4SNavdeep Parhar 
364fb93f5c4SNavdeep Parhar 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwact") != 0)
365fb93f5c4SNavdeep Parhar 		return;
366fb93f5c4SNavdeep Parhar 
367b3d44a68SNavdeep Parhar 	/* Activate iWARP if any port on this adapter has IFCAP_TOE enabled. */
368b3d44a68SNavdeep Parhar 	if (sc->offload_map && !uld_active(sc, ULD_IWARP))
369b3d44a68SNavdeep Parhar 		(void) t4_activate_uld(sc, ULD_IWARP);
370fb93f5c4SNavdeep Parhar 
371fb93f5c4SNavdeep Parhar 	end_synchronized_op(sc, 0);
372fb93f5c4SNavdeep Parhar }
373fb93f5c4SNavdeep Parhar 
374fb93f5c4SNavdeep Parhar static void
c4iw_deactivate_all(struct adapter * sc,void * arg __unused)375fb93f5c4SNavdeep Parhar c4iw_deactivate_all(struct adapter *sc, void *arg __unused)
376fb93f5c4SNavdeep Parhar {
377fb93f5c4SNavdeep Parhar 
378fb93f5c4SNavdeep Parhar 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4iwdea") != 0)
379fb93f5c4SNavdeep Parhar 		return;
380fb93f5c4SNavdeep Parhar 
381b3d44a68SNavdeep Parhar 	if (uld_active(sc, ULD_IWARP))
382b3d44a68SNavdeep Parhar 	    (void) t4_deactivate_uld(sc, ULD_IWARP);
383fb93f5c4SNavdeep Parhar 
384fb93f5c4SNavdeep Parhar 	end_synchronized_op(sc, 0);
385fb93f5c4SNavdeep Parhar }
386fb93f5c4SNavdeep Parhar 
387fb93f5c4SNavdeep Parhar static int
c4iw_mod_load(void)388fb93f5c4SNavdeep Parhar c4iw_mod_load(void)
389fb93f5c4SNavdeep Parhar {
390fb93f5c4SNavdeep Parhar 	int rc;
391fb93f5c4SNavdeep Parhar 
392fb93f5c4SNavdeep Parhar 	rc = -c4iw_cm_init();
393fb93f5c4SNavdeep Parhar 	if (rc != 0)
394fb93f5c4SNavdeep Parhar 		return (rc);
395fb93f5c4SNavdeep Parhar 
396cf5e6370SNavdeep Parhar 	rc = t4_register_uld(&c4iw_uld_info, ULD_IWARP);
397fb93f5c4SNavdeep Parhar 	if (rc != 0) {
398fb93f5c4SNavdeep Parhar 		c4iw_cm_term();
399fb93f5c4SNavdeep Parhar 		return (rc);
400fb93f5c4SNavdeep Parhar 	}
401fb93f5c4SNavdeep Parhar 
402fb93f5c4SNavdeep Parhar 	t4_iterate(c4iw_activate_all, NULL);
403fb93f5c4SNavdeep Parhar 
404fb93f5c4SNavdeep Parhar 	return (rc);
405fb93f5c4SNavdeep Parhar }
406fb93f5c4SNavdeep Parhar 
407fb93f5c4SNavdeep Parhar static int
c4iw_mod_unload(void)408fb93f5c4SNavdeep Parhar c4iw_mod_unload(void)
409fb93f5c4SNavdeep Parhar {
410fb93f5c4SNavdeep Parhar 
411fb93f5c4SNavdeep Parhar 	t4_iterate(c4iw_deactivate_all, NULL);
412fb93f5c4SNavdeep Parhar 
413fb93f5c4SNavdeep Parhar 	c4iw_cm_term();
414fb93f5c4SNavdeep Parhar 
415cf5e6370SNavdeep Parhar 	if (t4_unregister_uld(&c4iw_uld_info, ULD_IWARP) == EBUSY)
416fb93f5c4SNavdeep Parhar 		return (EBUSY);
417fb93f5c4SNavdeep Parhar 
418fb93f5c4SNavdeep Parhar 	return (0);
419fb93f5c4SNavdeep Parhar }
420fb93f5c4SNavdeep Parhar 
421fb93f5c4SNavdeep Parhar #endif
422fb93f5c4SNavdeep Parhar 
423fb93f5c4SNavdeep Parhar /*
424fb93f5c4SNavdeep Parhar  * t4_tom won't load on kernels without TCP_OFFLOAD and this module's dependency
425fb93f5c4SNavdeep Parhar  * on t4_tom ensures that it won't either.  So we don't directly check for
426fb93f5c4SNavdeep Parhar  * TCP_OFFLOAD here.
427fb93f5c4SNavdeep Parhar  */
428fb93f5c4SNavdeep Parhar static int
c4iw_modevent(module_t mod,int cmd,void * arg)429fb93f5c4SNavdeep Parhar c4iw_modevent(module_t mod, int cmd, void *arg)
430fb93f5c4SNavdeep Parhar {
431fb93f5c4SNavdeep Parhar 	int rc = 0;
432fb93f5c4SNavdeep Parhar 
433fb93f5c4SNavdeep Parhar #ifdef TCP_OFFLOAD
434fb93f5c4SNavdeep Parhar 	switch (cmd) {
435fb93f5c4SNavdeep Parhar 	case MOD_LOAD:
436fb93f5c4SNavdeep Parhar 		rc = c4iw_mod_load();
437fb93f5c4SNavdeep Parhar 		if (rc == 0)
4385c2bacdeSNavdeep Parhar 			printf("iw_cxgbe: Chelsio T5/T6 RDMA driver loaded.\n");
439fb93f5c4SNavdeep Parhar 		break;
440fb93f5c4SNavdeep Parhar 
441fb93f5c4SNavdeep Parhar 	case MOD_UNLOAD:
442fb93f5c4SNavdeep Parhar 		rc = c4iw_mod_unload();
443fb93f5c4SNavdeep Parhar 		break;
444fb93f5c4SNavdeep Parhar 
445fb93f5c4SNavdeep Parhar 	default:
446fb93f5c4SNavdeep Parhar 		rc = EINVAL;
447fb93f5c4SNavdeep Parhar 	}
448fb93f5c4SNavdeep Parhar #else
449fb93f5c4SNavdeep Parhar 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
450fb93f5c4SNavdeep Parhar 	rc = EOPNOTSUPP;
451fb93f5c4SNavdeep Parhar #endif
452fb93f5c4SNavdeep Parhar 	return (rc);
453fb93f5c4SNavdeep Parhar }
454fb93f5c4SNavdeep Parhar 
455fb93f5c4SNavdeep Parhar static moduledata_t c4iw_mod_data = {
456fb93f5c4SNavdeep Parhar 	"iw_cxgbe",
457fb93f5c4SNavdeep Parhar 	c4iw_modevent,
458fb93f5c4SNavdeep Parhar 	0
459fb93f5c4SNavdeep Parhar };
460fb93f5c4SNavdeep Parhar 
461fb93f5c4SNavdeep Parhar MODULE_VERSION(iw_cxgbe, 1);
46287f804e8SNavdeep Parhar MODULE_DEPEND(iw_cxgbe, t4nex, 1, 1, 1);
463fb93f5c4SNavdeep Parhar MODULE_DEPEND(iw_cxgbe, t4_tom, 1, 1, 1);
464fb93f5c4SNavdeep Parhar MODULE_DEPEND(iw_cxgbe, ibcore, 1, 1, 1);
4652da3897dSHans Petter Selasky MODULE_DEPEND(iw_cxgbe, linuxkpi, 1, 1, 1);
466fb93f5c4SNavdeep Parhar DECLARE_MODULE(iw_cxgbe, c4iw_mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
467