xref: /freebsd/sys/dev/mana/hw_channel.c (revision 1dc7a7b74b5ad37ff7c8dc22f1a710460a5f1dcd)
1ce110ea1SWei Hu /*-
2ce110ea1SWei Hu  * SPDX-License-Identifier: BSD-2-Clause
3ce110ea1SWei Hu  *
4ce110ea1SWei Hu  * Copyright (c) 2021 Microsoft Corp.
5ce110ea1SWei Hu  * All rights reserved.
6ce110ea1SWei Hu  *
7ce110ea1SWei Hu  * Redistribution and use in source and binary forms, with or without
8ce110ea1SWei Hu  * modification, are permitted provided that the following conditions
9ce110ea1SWei Hu  * are met:
10ce110ea1SWei Hu  *
11ce110ea1SWei Hu  * 1. Redistributions of source code must retain the above copyright
12ce110ea1SWei Hu  *    notice, this list of conditions and the following disclaimer.
13ce110ea1SWei Hu  *
14ce110ea1SWei Hu  * 2. Redistributions in binary form must reproduce the above copyright
15ce110ea1SWei Hu  *    notice, this list of conditions and the following disclaimer in the
16ce110ea1SWei Hu  *    documentation and/or other materials provided with the distribution.
17ce110ea1SWei Hu  *
18ce110ea1SWei Hu  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19ce110ea1SWei Hu  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20ce110ea1SWei Hu  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21ce110ea1SWei Hu  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22ce110ea1SWei Hu  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23ce110ea1SWei Hu  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24ce110ea1SWei Hu  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25ce110ea1SWei Hu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26ce110ea1SWei Hu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27ce110ea1SWei Hu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28ce110ea1SWei Hu  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29ce110ea1SWei Hu  */
30*fdafd315SWarner Losh 
31ce110ea1SWei Hu #include <sys/param.h>
32ce110ea1SWei Hu #include <sys/systm.h>
33ce110ea1SWei Hu #include <sys/types.h>
34ce110ea1SWei Hu #include <sys/kernel.h>
35ce110ea1SWei Hu #include <sys/kthread.h>
36ce110ea1SWei Hu #include <sys/lock.h>
37ce110ea1SWei Hu #include <sys/malloc.h>
38ce110ea1SWei Hu #include <sys/mutex.h>
39ce110ea1SWei Hu #include <sys/bus.h>
40ce110ea1SWei Hu #include <machine/bus.h>
41ce110ea1SWei Hu 
42ce110ea1SWei Hu #include "mana.h"
43ce110ea1SWei Hu #include "hw_channel.h"
44ce110ea1SWei Hu 
45ce110ea1SWei Hu static int
mana_hwc_get_msg_index(struct hw_channel_context * hwc,uint16_t * msg_id)46ce110ea1SWei Hu mana_hwc_get_msg_index(struct hw_channel_context *hwc, uint16_t *msg_id)
47ce110ea1SWei Hu {
48ce110ea1SWei Hu 	struct gdma_resource *r = &hwc->inflight_msg_res;
49ce110ea1SWei Hu 	uint32_t index;
50ce110ea1SWei Hu 
51ce110ea1SWei Hu 	sema_wait(&hwc->sema);
52ce110ea1SWei Hu 
53ce110ea1SWei Hu 	mtx_lock_spin(&r->lock_spin);
54ce110ea1SWei Hu 
55ce110ea1SWei Hu 	index = find_first_zero_bit(hwc->inflight_msg_res.map,
56ce110ea1SWei Hu 	    hwc->inflight_msg_res.size);
57ce110ea1SWei Hu 
58ce110ea1SWei Hu 	bitmap_set(hwc->inflight_msg_res.map, index, 1);
59ce110ea1SWei Hu 
60ce110ea1SWei Hu 	mtx_unlock_spin(&r->lock_spin);
61ce110ea1SWei Hu 
62ce110ea1SWei Hu 	*msg_id = index;
63ce110ea1SWei Hu 
64ce110ea1SWei Hu 	return 0;
65ce110ea1SWei Hu }
66ce110ea1SWei Hu 
67ce110ea1SWei Hu static void
mana_hwc_put_msg_index(struct hw_channel_context * hwc,uint16_t msg_id)68ce110ea1SWei Hu mana_hwc_put_msg_index(struct hw_channel_context *hwc, uint16_t msg_id)
69ce110ea1SWei Hu {
70ce110ea1SWei Hu 	struct gdma_resource *r = &hwc->inflight_msg_res;
71ce110ea1SWei Hu 
72ce110ea1SWei Hu 	mtx_lock_spin(&r->lock_spin);
73ce110ea1SWei Hu 	bitmap_clear(hwc->inflight_msg_res.map, msg_id, 1);
74ce110ea1SWei Hu 	mtx_unlock_spin(&r->lock_spin);
75ce110ea1SWei Hu 
76ce110ea1SWei Hu 	sema_post(&hwc->sema);
77ce110ea1SWei Hu }
78ce110ea1SWei Hu 
79ce110ea1SWei Hu static int
mana_hwc_verify_resp_msg(const struct hwc_caller_ctx * caller_ctx,const struct gdma_resp_hdr * resp_msg,uint32_t resp_len)80ce110ea1SWei Hu mana_hwc_verify_resp_msg(const struct hwc_caller_ctx *caller_ctx,
81ce110ea1SWei Hu     const struct gdma_resp_hdr *resp_msg,
82ce110ea1SWei Hu     uint32_t resp_len)
83ce110ea1SWei Hu {
84ce110ea1SWei Hu 	if (resp_len < sizeof(*resp_msg))
85ce110ea1SWei Hu 		return EPROTO;
86ce110ea1SWei Hu 
87ce110ea1SWei Hu 	if (resp_len > caller_ctx->output_buflen)
88ce110ea1SWei Hu 		return EPROTO;
89ce110ea1SWei Hu 
90ce110ea1SWei Hu 	return 0;
91ce110ea1SWei Hu }
92ce110ea1SWei Hu 
93ce110ea1SWei Hu static void
mana_hwc_handle_resp(struct hw_channel_context * hwc,uint32_t resp_len,const struct gdma_resp_hdr * resp_msg)94ce110ea1SWei Hu mana_hwc_handle_resp(struct hw_channel_context *hwc, uint32_t resp_len,
95ce110ea1SWei Hu     const struct gdma_resp_hdr *resp_msg)
96ce110ea1SWei Hu {
97ce110ea1SWei Hu 	struct hwc_caller_ctx *ctx;
98ce110ea1SWei Hu 	int err;
99ce110ea1SWei Hu 
100ce110ea1SWei Hu 	if (!test_bit(resp_msg->response.hwc_msg_id,
101ce110ea1SWei Hu 	    hwc->inflight_msg_res.map)) {
102ce110ea1SWei Hu 		device_printf(hwc->dev, "hwc_rx: invalid msg_id = %u\n",
103ce110ea1SWei Hu 		    resp_msg->response.hwc_msg_id);
104ce110ea1SWei Hu 		return;
105ce110ea1SWei Hu 	}
106ce110ea1SWei Hu 
107ce110ea1SWei Hu 	ctx = hwc->caller_ctx + resp_msg->response.hwc_msg_id;
108ce110ea1SWei Hu 	err = mana_hwc_verify_resp_msg(ctx, resp_msg, resp_len);
109ce110ea1SWei Hu 	if (err)
110ce110ea1SWei Hu 		goto out;
111ce110ea1SWei Hu 
112ce110ea1SWei Hu 	ctx->status_code = resp_msg->status;
113ce110ea1SWei Hu 
114ce110ea1SWei Hu 	memcpy(ctx->output_buf, resp_msg, resp_len);
115ce110ea1SWei Hu out:
116ce110ea1SWei Hu 	ctx->error = err;
117ce110ea1SWei Hu 	complete(&ctx->comp_event);
118ce110ea1SWei Hu }
119ce110ea1SWei Hu 
120ce110ea1SWei Hu static int
mana_hwc_post_rx_wqe(const struct hwc_wq * hwc_rxq,struct hwc_work_request * req)121ce110ea1SWei Hu mana_hwc_post_rx_wqe(const struct hwc_wq *hwc_rxq,
122ce110ea1SWei Hu     struct hwc_work_request *req)
123ce110ea1SWei Hu {
124ce110ea1SWei Hu 	device_t dev = hwc_rxq->hwc->dev;
125ce110ea1SWei Hu 	struct gdma_sge *sge;
126ce110ea1SWei Hu 	int err;
127ce110ea1SWei Hu 
128ce110ea1SWei Hu 	sge = &req->sge;
129c5eed414SJohn Baldwin 	sge->address = (uintptr_t)req->buf_sge_addr;
130ce110ea1SWei Hu 	sge->mem_key = hwc_rxq->msg_buf->gpa_mkey;
131ce110ea1SWei Hu 	sge->size = req->buf_len;
132ce110ea1SWei Hu 
133ce110ea1SWei Hu 	memset(&req->wqe_req, 0, sizeof(struct gdma_wqe_request));
134ce110ea1SWei Hu 	req->wqe_req.sgl = sge;
135ce110ea1SWei Hu 	req->wqe_req.num_sge = 1;
136ce110ea1SWei Hu 	req->wqe_req.client_data_unit = 0;
137ce110ea1SWei Hu 
138ce110ea1SWei Hu 	err = mana_gd_post_and_ring(hwc_rxq->gdma_wq, &req->wqe_req, NULL);
139ce110ea1SWei Hu 	if (err)
140ce110ea1SWei Hu 		device_printf(dev,
141ce110ea1SWei Hu 		    "Failed to post WQE on HWC RQ: %d\n", err);
142ce110ea1SWei Hu 	return err;
143ce110ea1SWei Hu }
144ce110ea1SWei Hu 
145ce110ea1SWei Hu static void
mana_hwc_init_event_handler(void * ctx,struct gdma_queue * q_self,struct gdma_event * event)146ce110ea1SWei Hu mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self,
147ce110ea1SWei Hu     struct gdma_event *event)
148ce110ea1SWei Hu {
149ce110ea1SWei Hu 	struct hw_channel_context *hwc = ctx;
150ce110ea1SWei Hu 	struct gdma_dev *gd = hwc->gdma_dev;
151ce110ea1SWei Hu 	union hwc_init_type_data type_data;
152ce110ea1SWei Hu 	union hwc_init_eq_id_db eq_db;
153ce110ea1SWei Hu 	uint32_t type, val;
154ce110ea1SWei Hu 
155ce110ea1SWei Hu 	switch (event->type) {
156ce110ea1SWei Hu 	case GDMA_EQE_HWC_INIT_EQ_ID_DB:
157ce110ea1SWei Hu 		eq_db.as_uint32 = event->details[0];
158ce110ea1SWei Hu 		hwc->cq->gdma_eq->id = eq_db.eq_id;
159ce110ea1SWei Hu 		gd->doorbell = eq_db.doorbell;
160ce110ea1SWei Hu 		break;
161ce110ea1SWei Hu 
162ce110ea1SWei Hu 	case GDMA_EQE_HWC_INIT_DATA:
163ce110ea1SWei Hu 		type_data.as_uint32 = event->details[0];
164ce110ea1SWei Hu 		type = type_data.type;
165ce110ea1SWei Hu 		val = type_data.value;
166ce110ea1SWei Hu 
167ce110ea1SWei Hu 		switch (type) {
168ce110ea1SWei Hu 		case HWC_INIT_DATA_CQID:
169ce110ea1SWei Hu 			hwc->cq->gdma_cq->id = val;
170ce110ea1SWei Hu 			break;
171ce110ea1SWei Hu 
172ce110ea1SWei Hu 		case HWC_INIT_DATA_RQID:
173ce110ea1SWei Hu 			hwc->rxq->gdma_wq->id = val;
174ce110ea1SWei Hu 			break;
175ce110ea1SWei Hu 
176ce110ea1SWei Hu 		case HWC_INIT_DATA_SQID:
177ce110ea1SWei Hu 			hwc->txq->gdma_wq->id = val;
178ce110ea1SWei Hu 			break;
179ce110ea1SWei Hu 
180ce110ea1SWei Hu 		case HWC_INIT_DATA_QUEUE_DEPTH:
181ce110ea1SWei Hu 			hwc->hwc_init_q_depth_max = (uint16_t)val;
182ce110ea1SWei Hu 			break;
183ce110ea1SWei Hu 
184ce110ea1SWei Hu 		case HWC_INIT_DATA_MAX_REQUEST:
185ce110ea1SWei Hu 			hwc->hwc_init_max_req_msg_size = val;
186ce110ea1SWei Hu 			break;
187ce110ea1SWei Hu 
188ce110ea1SWei Hu 		case HWC_INIT_DATA_MAX_RESPONSE:
189ce110ea1SWei Hu 			hwc->hwc_init_max_resp_msg_size = val;
190ce110ea1SWei Hu 			break;
191ce110ea1SWei Hu 
192ce110ea1SWei Hu 		case HWC_INIT_DATA_MAX_NUM_CQS:
193ce110ea1SWei Hu 			gd->gdma_context->max_num_cqs = val;
194ce110ea1SWei Hu 			break;
195ce110ea1SWei Hu 
196ce110ea1SWei Hu 		case HWC_INIT_DATA_PDID:
197ce110ea1SWei Hu 			hwc->gdma_dev->pdid = val;
198ce110ea1SWei Hu 			break;
199ce110ea1SWei Hu 
200ce110ea1SWei Hu 		case HWC_INIT_DATA_GPA_MKEY:
201ce110ea1SWei Hu 			hwc->rxq->msg_buf->gpa_mkey = val;
202ce110ea1SWei Hu 			hwc->txq->msg_buf->gpa_mkey = val;
203ce110ea1SWei Hu 			break;
204ce110ea1SWei Hu 		}
205ce110ea1SWei Hu 
206ce110ea1SWei Hu 		break;
207ce110ea1SWei Hu 
208ce110ea1SWei Hu 	case GDMA_EQE_HWC_INIT_DONE:
209ce110ea1SWei Hu 		complete(&hwc->hwc_init_eqe_comp);
210ce110ea1SWei Hu 		break;
211ce110ea1SWei Hu 
212ce110ea1SWei Hu 	default:
213ce110ea1SWei Hu 		/* Ignore unknown events, which should never happen. */
214ce110ea1SWei Hu 		break;
215ce110ea1SWei Hu 	}
216ce110ea1SWei Hu }
217ce110ea1SWei Hu 
218ce110ea1SWei Hu static void
mana_hwc_rx_event_handler(void * ctx,uint32_t gdma_rxq_id,const struct hwc_rx_oob * rx_oob)219ce110ea1SWei Hu mana_hwc_rx_event_handler(void *ctx, uint32_t gdma_rxq_id,
220ce110ea1SWei Hu     const struct hwc_rx_oob *rx_oob)
221ce110ea1SWei Hu {
222ce110ea1SWei Hu 	struct hw_channel_context *hwc = ctx;
223ce110ea1SWei Hu 	struct hwc_wq *hwc_rxq = hwc->rxq;
224ce110ea1SWei Hu 	struct hwc_work_request *rx_req;
225ce110ea1SWei Hu 	struct gdma_resp_hdr *resp;
226ce110ea1SWei Hu 	struct gdma_wqe *dma_oob;
227ce110ea1SWei Hu 	struct gdma_queue *rq;
228ce110ea1SWei Hu 	struct gdma_sge *sge;
229ce110ea1SWei Hu 	uint64_t rq_base_addr;
230ce110ea1SWei Hu 	uint64_t rx_req_idx;
231ce110ea1SWei Hu 	uint8_t *wqe;
232ce110ea1SWei Hu 
233ce110ea1SWei Hu 	if (hwc_rxq->gdma_wq->id != gdma_rxq_id) {
234ce110ea1SWei Hu 		mana_warn(NULL, "unmatched rx queue %u != %u\n",
235ce110ea1SWei Hu 		    hwc_rxq->gdma_wq->id, gdma_rxq_id);
236ce110ea1SWei Hu 		return;
237ce110ea1SWei Hu 	}
238ce110ea1SWei Hu 
239ce110ea1SWei Hu 
240ce110ea1SWei Hu 	rq = hwc_rxq->gdma_wq;
241ce110ea1SWei Hu 	wqe = mana_gd_get_wqe_ptr(rq, rx_oob->wqe_offset / GDMA_WQE_BU_SIZE);
242ce110ea1SWei Hu 	dma_oob = (struct gdma_wqe *)wqe;
243ce110ea1SWei Hu 
244ce110ea1SWei Hu 	bus_dmamap_sync(rq->mem_info.dma_tag, rq->mem_info.dma_map,
245ce110ea1SWei Hu 	    BUS_DMASYNC_POSTREAD);
246ce110ea1SWei Hu 
247ce110ea1SWei Hu 	sge = (struct gdma_sge *)(wqe + 8 + dma_oob->inline_oob_size_div4 * 4);
248ce110ea1SWei Hu 
249ce110ea1SWei Hu 	/* Select the RX work request for virtual address and for reposting. */
250ce110ea1SWei Hu 	rq_base_addr = hwc_rxq->msg_buf->mem_info.dma_handle;
251ce110ea1SWei Hu 	rx_req_idx = (sge->address - rq_base_addr) / hwc->max_req_msg_size;
252ce110ea1SWei Hu 
253ce110ea1SWei Hu 	bus_dmamap_sync(hwc_rxq->msg_buf->mem_info.dma_tag,
254ce110ea1SWei Hu 	    hwc_rxq->msg_buf->mem_info.dma_map,
255ce110ea1SWei Hu 	    BUS_DMASYNC_POSTREAD);
256ce110ea1SWei Hu 
257ce110ea1SWei Hu 	rx_req = &hwc_rxq->msg_buf->reqs[rx_req_idx];
258ce110ea1SWei Hu 	resp = (struct gdma_resp_hdr *)rx_req->buf_va;
259ce110ea1SWei Hu 
260ce110ea1SWei Hu 	if (resp->response.hwc_msg_id >= hwc->num_inflight_msg) {
261ce110ea1SWei Hu 		device_printf(hwc->dev, "HWC RX: wrong msg_id=%u\n",
262ce110ea1SWei Hu 		    resp->response.hwc_msg_id);
263ce110ea1SWei Hu 		return;
264ce110ea1SWei Hu 	}
265ce110ea1SWei Hu 
266ce110ea1SWei Hu 	mana_hwc_handle_resp(hwc, rx_oob->tx_oob_data_size, resp);
267ce110ea1SWei Hu 
268ce110ea1SWei Hu 	/* Do no longer use 'resp', because the buffer is posted to the HW
269ce110ea1SWei Hu 	 * in the below mana_hwc_post_rx_wqe().
270ce110ea1SWei Hu 	 */
271ce110ea1SWei Hu 	resp = NULL;
272ce110ea1SWei Hu 
273ce110ea1SWei Hu 	bus_dmamap_sync(hwc_rxq->msg_buf->mem_info.dma_tag,
274ce110ea1SWei Hu 	    hwc_rxq->msg_buf->mem_info.dma_map,
275ce110ea1SWei Hu 	    BUS_DMASYNC_PREREAD);
276ce110ea1SWei Hu 
277ce110ea1SWei Hu 	mana_hwc_post_rx_wqe(hwc_rxq, rx_req);
278ce110ea1SWei Hu }
279ce110ea1SWei Hu 
280ce110ea1SWei Hu static void
mana_hwc_tx_event_handler(void * ctx,uint32_t gdma_txq_id,const struct hwc_rx_oob * rx_oob)281ce110ea1SWei Hu mana_hwc_tx_event_handler(void *ctx, uint32_t gdma_txq_id,
282ce110ea1SWei Hu     const struct hwc_rx_oob *rx_oob)
283ce110ea1SWei Hu {
284ce110ea1SWei Hu 	struct hw_channel_context *hwc = ctx;
285ce110ea1SWei Hu 	struct hwc_wq *hwc_txq = hwc->txq;
286ce110ea1SWei Hu 
287ce110ea1SWei Hu 	if (!hwc_txq || hwc_txq->gdma_wq->id != gdma_txq_id) {
288ce110ea1SWei Hu 		mana_warn(NULL, "unmatched tx queue %u != %u\n",
289ce110ea1SWei Hu 		    hwc_txq->gdma_wq->id, gdma_txq_id);
290ce110ea1SWei Hu 	}
291ce110ea1SWei Hu 
292ce110ea1SWei Hu 	bus_dmamap_sync(hwc_txq->gdma_wq->mem_info.dma_tag,
293ce110ea1SWei Hu 	    hwc_txq->gdma_wq->mem_info.dma_map,
294ce110ea1SWei Hu 	    BUS_DMASYNC_POSTWRITE);
295ce110ea1SWei Hu }
296ce110ea1SWei Hu 
297ce110ea1SWei Hu static int
mana_hwc_create_gdma_wq(struct hw_channel_context * hwc,enum gdma_queue_type type,uint64_t queue_size,struct gdma_queue ** queue)298ce110ea1SWei Hu mana_hwc_create_gdma_wq(struct hw_channel_context *hwc,
299ce110ea1SWei Hu     enum gdma_queue_type type, uint64_t queue_size,
300ce110ea1SWei Hu     struct gdma_queue **queue)
301ce110ea1SWei Hu {
302ce110ea1SWei Hu 	struct gdma_queue_spec spec = {};
303ce110ea1SWei Hu 
304ce110ea1SWei Hu 	if (type != GDMA_SQ && type != GDMA_RQ)
305ce110ea1SWei Hu 		return EINVAL;
306ce110ea1SWei Hu 
307ce110ea1SWei Hu 	spec.type = type;
308ce110ea1SWei Hu 	spec.monitor_avl_buf = false;
309ce110ea1SWei Hu 	spec.queue_size = queue_size;
310ce110ea1SWei Hu 
311ce110ea1SWei Hu 	return mana_gd_create_hwc_queue(hwc->gdma_dev, &spec, queue);
312ce110ea1SWei Hu }
313ce110ea1SWei Hu 
314ce110ea1SWei Hu static int
mana_hwc_create_gdma_cq(struct hw_channel_context * hwc,uint64_t queue_size,void * ctx,gdma_cq_callback * cb,struct gdma_queue * parent_eq,struct gdma_queue ** queue)315ce110ea1SWei Hu mana_hwc_create_gdma_cq(struct hw_channel_context *hwc,
316ce110ea1SWei Hu     uint64_t queue_size,
317ce110ea1SWei Hu     void *ctx, gdma_cq_callback *cb,
318ce110ea1SWei Hu     struct gdma_queue *parent_eq,
319ce110ea1SWei Hu     struct gdma_queue **queue)
320ce110ea1SWei Hu {
321ce110ea1SWei Hu 	struct gdma_queue_spec spec = {};
322ce110ea1SWei Hu 
323ce110ea1SWei Hu 	spec.type = GDMA_CQ;
324ce110ea1SWei Hu 	spec.monitor_avl_buf = false;
325ce110ea1SWei Hu 	spec.queue_size = queue_size;
326ce110ea1SWei Hu 	spec.cq.context = ctx;
327ce110ea1SWei Hu 	spec.cq.callback = cb;
328ce110ea1SWei Hu 	spec.cq.parent_eq = parent_eq;
329ce110ea1SWei Hu 
330ce110ea1SWei Hu 	return mana_gd_create_hwc_queue(hwc->gdma_dev, &spec, queue);
331ce110ea1SWei Hu }
332ce110ea1SWei Hu 
333ce110ea1SWei Hu static int
mana_hwc_create_gdma_eq(struct hw_channel_context * hwc,uint64_t queue_size,void * ctx,gdma_eq_callback * cb,struct gdma_queue ** queue)334ce110ea1SWei Hu mana_hwc_create_gdma_eq(struct hw_channel_context *hwc,
335ce110ea1SWei Hu     uint64_t queue_size,
336ce110ea1SWei Hu     void *ctx, gdma_eq_callback *cb,
337ce110ea1SWei Hu     struct gdma_queue **queue)
338ce110ea1SWei Hu {
339ce110ea1SWei Hu 	struct gdma_queue_spec spec = {};
340ce110ea1SWei Hu 
341ce110ea1SWei Hu 	spec.type = GDMA_EQ;
342ce110ea1SWei Hu 	spec.monitor_avl_buf = false;
343ce110ea1SWei Hu 	spec.queue_size = queue_size;
344ce110ea1SWei Hu 	spec.eq.context = ctx;
345ce110ea1SWei Hu 	spec.eq.callback = cb;
346ce110ea1SWei Hu 	spec.eq.log2_throttle_limit = DEFAULT_LOG2_THROTTLING_FOR_ERROR_EQ;
347ce110ea1SWei Hu 
348ce110ea1SWei Hu 	return mana_gd_create_hwc_queue(hwc->gdma_dev, &spec, queue);
349ce110ea1SWei Hu }
350ce110ea1SWei Hu 
351ce110ea1SWei Hu static void
mana_hwc_comp_event(void * ctx,struct gdma_queue * q_self)352ce110ea1SWei Hu mana_hwc_comp_event(void *ctx, struct gdma_queue *q_self)
353ce110ea1SWei Hu {
354ce110ea1SWei Hu 	struct hwc_rx_oob comp_data = {};
355ce110ea1SWei Hu 	struct gdma_comp *completions;
356ce110ea1SWei Hu 	struct hwc_cq *hwc_cq = ctx;
357ce110ea1SWei Hu 	int comp_read, i;
358ce110ea1SWei Hu 
359ce110ea1SWei Hu 	completions = hwc_cq->comp_buf;
360ce110ea1SWei Hu 	comp_read = mana_gd_poll_cq(q_self, completions, hwc_cq->queue_depth);
361ce110ea1SWei Hu 
362ce110ea1SWei Hu 	for (i = 0; i < comp_read; ++i) {
363ce110ea1SWei Hu 		comp_data = *(struct hwc_rx_oob *)completions[i].cqe_data;
364ce110ea1SWei Hu 
365ce110ea1SWei Hu 		if (completions[i].is_sq)
366ce110ea1SWei Hu 			hwc_cq->tx_event_handler(hwc_cq->tx_event_ctx,
367ce110ea1SWei Hu 			    completions[i].wq_num,
368ce110ea1SWei Hu 			    &comp_data);
369ce110ea1SWei Hu 		else
370ce110ea1SWei Hu 			hwc_cq->rx_event_handler(hwc_cq->rx_event_ctx,
371ce110ea1SWei Hu 			    completions[i].wq_num,
372ce110ea1SWei Hu 			    &comp_data);
373ce110ea1SWei Hu 	}
374ce110ea1SWei Hu 
375ce110ea1SWei Hu 	bus_dmamap_sync(q_self->mem_info.dma_tag, q_self->mem_info.dma_map,
376ce110ea1SWei Hu 	    BUS_DMASYNC_POSTREAD);
377ce110ea1SWei Hu 
3781833cf13SWei Hu 	mana_gd_ring_cq(q_self, SET_ARM_BIT);
379ce110ea1SWei Hu }
380ce110ea1SWei Hu 
381ce110ea1SWei Hu static void
mana_hwc_destroy_cq(struct gdma_context * gc,struct hwc_cq * hwc_cq)382ce110ea1SWei Hu mana_hwc_destroy_cq(struct gdma_context *gc, struct hwc_cq *hwc_cq)
383ce110ea1SWei Hu {
384ce110ea1SWei Hu 	if (hwc_cq->comp_buf)
385ce110ea1SWei Hu 		free(hwc_cq->comp_buf, M_DEVBUF);
386ce110ea1SWei Hu 
387ce110ea1SWei Hu 	if (hwc_cq->gdma_cq)
388ce110ea1SWei Hu 		mana_gd_destroy_queue(gc, hwc_cq->gdma_cq);
389ce110ea1SWei Hu 
390ce110ea1SWei Hu 	if (hwc_cq->gdma_eq)
391ce110ea1SWei Hu 		mana_gd_destroy_queue(gc, hwc_cq->gdma_eq);
392ce110ea1SWei Hu 
393ce110ea1SWei Hu 	free(hwc_cq, M_DEVBUF);
394ce110ea1SWei Hu }
395ce110ea1SWei Hu 
396ce110ea1SWei Hu static int
mana_hwc_create_cq(struct hw_channel_context * hwc,uint16_t q_depth,gdma_eq_callback * callback,void * ctx,hwc_rx_event_handler_t * rx_ev_hdlr,void * rx_ev_ctx,hwc_tx_event_handler_t * tx_ev_hdlr,void * tx_ev_ctx,struct hwc_cq ** hwc_cq_ptr)397ce110ea1SWei Hu mana_hwc_create_cq(struct hw_channel_context *hwc,
398ce110ea1SWei Hu     uint16_t q_depth,
399ce110ea1SWei Hu     gdma_eq_callback *callback, void *ctx,
400ce110ea1SWei Hu     hwc_rx_event_handler_t *rx_ev_hdlr, void *rx_ev_ctx,
401ce110ea1SWei Hu     hwc_tx_event_handler_t *tx_ev_hdlr, void *tx_ev_ctx,
402ce110ea1SWei Hu     struct hwc_cq **hwc_cq_ptr)
403ce110ea1SWei Hu {
404ce110ea1SWei Hu 	struct gdma_queue *eq, *cq;
405ce110ea1SWei Hu 	struct gdma_comp *comp_buf;
406ce110ea1SWei Hu 	struct hwc_cq *hwc_cq;
407ce110ea1SWei Hu 	uint32_t eq_size, cq_size;
408ce110ea1SWei Hu 	int err;
409ce110ea1SWei Hu 
410ce110ea1SWei Hu 	eq_size = roundup_pow_of_two(GDMA_EQE_SIZE * q_depth);
411ce110ea1SWei Hu 	if (eq_size < MINIMUM_SUPPORTED_PAGE_SIZE)
412ce110ea1SWei Hu 		eq_size = MINIMUM_SUPPORTED_PAGE_SIZE;
413ce110ea1SWei Hu 
414ce110ea1SWei Hu 	cq_size = roundup_pow_of_two(GDMA_CQE_SIZE * q_depth);
415ce110ea1SWei Hu 	if (cq_size < MINIMUM_SUPPORTED_PAGE_SIZE)
416ce110ea1SWei Hu 		cq_size = MINIMUM_SUPPORTED_PAGE_SIZE;
417ce110ea1SWei Hu 
418ce110ea1SWei Hu 	hwc_cq = malloc(sizeof(*hwc_cq), M_DEVBUF, M_WAITOK | M_ZERO);
419ce110ea1SWei Hu 
420ce110ea1SWei Hu 	err = mana_hwc_create_gdma_eq(hwc, eq_size, ctx, callback, &eq);
421ce110ea1SWei Hu 	if (err) {
422ce110ea1SWei Hu 		device_printf(hwc->dev,
423ce110ea1SWei Hu 		    "Failed to create HWC EQ for RQ: %d\n", err);
424ce110ea1SWei Hu 		goto out;
425ce110ea1SWei Hu 	}
426ce110ea1SWei Hu 	hwc_cq->gdma_eq = eq;
427ce110ea1SWei Hu 
428ce110ea1SWei Hu 	err = mana_hwc_create_gdma_cq(hwc, cq_size, hwc_cq,
429ce110ea1SWei Hu 	    mana_hwc_comp_event, eq, &cq);
430ce110ea1SWei Hu 	if (err) {
431ce110ea1SWei Hu 		device_printf(hwc->dev,
432ce110ea1SWei Hu 		    "Failed to create HWC CQ for RQ: %d\n", err);
433ce110ea1SWei Hu 		goto out;
434ce110ea1SWei Hu 	}
435ce110ea1SWei Hu 	hwc_cq->gdma_cq = cq;
436ce110ea1SWei Hu 
437ce110ea1SWei Hu 	comp_buf = mallocarray(q_depth, sizeof(struct gdma_comp),
438ce110ea1SWei Hu 	    M_DEVBUF, M_WAITOK | M_ZERO);
439ce110ea1SWei Hu 
440ce110ea1SWei Hu 	hwc_cq->hwc = hwc;
441ce110ea1SWei Hu 	hwc_cq->comp_buf = comp_buf;
442ce110ea1SWei Hu 	hwc_cq->queue_depth = q_depth;
443ce110ea1SWei Hu 	hwc_cq->rx_event_handler = rx_ev_hdlr;
444ce110ea1SWei Hu 	hwc_cq->rx_event_ctx = rx_ev_ctx;
445ce110ea1SWei Hu 	hwc_cq->tx_event_handler = tx_ev_hdlr;
446ce110ea1SWei Hu 	hwc_cq->tx_event_ctx = tx_ev_ctx;
447ce110ea1SWei Hu 
448ce110ea1SWei Hu 	*hwc_cq_ptr = hwc_cq;
449ce110ea1SWei Hu 	return 0;
450ce110ea1SWei Hu out:
451ce110ea1SWei Hu 	mana_hwc_destroy_cq(hwc->gdma_dev->gdma_context, hwc_cq);
452ce110ea1SWei Hu 	return err;
453ce110ea1SWei Hu }
454ce110ea1SWei Hu 
455ce110ea1SWei Hu static int
mana_hwc_alloc_dma_buf(struct hw_channel_context * hwc,uint16_t q_depth,uint32_t max_msg_size,struct hwc_dma_buf ** dma_buf_ptr)456ce110ea1SWei Hu mana_hwc_alloc_dma_buf(struct hw_channel_context *hwc, uint16_t q_depth,
457ce110ea1SWei Hu     uint32_t max_msg_size,
458ce110ea1SWei Hu     struct hwc_dma_buf **dma_buf_ptr)
459ce110ea1SWei Hu {
460ce110ea1SWei Hu 	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
461ce110ea1SWei Hu 	struct hwc_work_request *hwc_wr;
462ce110ea1SWei Hu 	struct hwc_dma_buf *dma_buf;
463ce110ea1SWei Hu 	struct gdma_mem_info *gmi;
464ce110ea1SWei Hu 	uint32_t buf_size;
465ce110ea1SWei Hu 	uint8_t *base_pa;
466ce110ea1SWei Hu 	void *virt_addr;
467ce110ea1SWei Hu 	uint16_t i;
468ce110ea1SWei Hu 	int err;
469ce110ea1SWei Hu 
470ce110ea1SWei Hu 	dma_buf = malloc(sizeof(*dma_buf) +
471ce110ea1SWei Hu 	    q_depth * sizeof(struct hwc_work_request),
472ce110ea1SWei Hu 	    M_DEVBUF, M_WAITOK | M_ZERO);
473ce110ea1SWei Hu 
474ce110ea1SWei Hu 	dma_buf->num_reqs = q_depth;
475ce110ea1SWei Hu 
476ce110ea1SWei Hu 	buf_size = ALIGN(q_depth * max_msg_size, PAGE_SIZE);
477ce110ea1SWei Hu 
478ce110ea1SWei Hu 	gmi = &dma_buf->mem_info;
479ce110ea1SWei Hu 	err = mana_gd_alloc_memory(gc, buf_size, gmi);
480ce110ea1SWei Hu 	if (err) {
481ce110ea1SWei Hu 		device_printf(hwc->dev,
482ce110ea1SWei Hu 		    "Failed to allocate DMA buffer: %d\n", err);
483ce110ea1SWei Hu 		goto out;
484ce110ea1SWei Hu 	}
485ce110ea1SWei Hu 
486ce110ea1SWei Hu 	virt_addr = dma_buf->mem_info.virt_addr;
487ce110ea1SWei Hu 	base_pa = (uint8_t *)dma_buf->mem_info.dma_handle;
488ce110ea1SWei Hu 
489ce110ea1SWei Hu 	for (i = 0; i < q_depth; i++) {
490ce110ea1SWei Hu 		hwc_wr = &dma_buf->reqs[i];
491ce110ea1SWei Hu 
492ce110ea1SWei Hu 		hwc_wr->buf_va = (char *)virt_addr + i * max_msg_size;
493ce110ea1SWei Hu 		hwc_wr->buf_sge_addr = base_pa + i * max_msg_size;
494ce110ea1SWei Hu 
495ce110ea1SWei Hu 		hwc_wr->buf_len = max_msg_size;
496ce110ea1SWei Hu 	}
497ce110ea1SWei Hu 
498ce110ea1SWei Hu 	*dma_buf_ptr = dma_buf;
499ce110ea1SWei Hu 	return 0;
500ce110ea1SWei Hu out:
501ce110ea1SWei Hu 	free(dma_buf, M_DEVBUF);
502ce110ea1SWei Hu 	return err;
503ce110ea1SWei Hu }
504ce110ea1SWei Hu 
505ce110ea1SWei Hu static void
mana_hwc_dealloc_dma_buf(struct hw_channel_context * hwc,struct hwc_dma_buf * dma_buf)506ce110ea1SWei Hu mana_hwc_dealloc_dma_buf(struct hw_channel_context *hwc,
507ce110ea1SWei Hu     struct hwc_dma_buf *dma_buf)
508ce110ea1SWei Hu {
509ce110ea1SWei Hu 	if (!dma_buf)
510ce110ea1SWei Hu 		return;
511ce110ea1SWei Hu 
512ce110ea1SWei Hu 	mana_gd_free_memory(&dma_buf->mem_info);
513ce110ea1SWei Hu 
514ce110ea1SWei Hu 	free(dma_buf, M_DEVBUF);
515ce110ea1SWei Hu }
516ce110ea1SWei Hu 
517ce110ea1SWei Hu static void
mana_hwc_destroy_wq(struct hw_channel_context * hwc,struct hwc_wq * hwc_wq)518ce110ea1SWei Hu mana_hwc_destroy_wq(struct hw_channel_context *hwc,
519ce110ea1SWei Hu     struct hwc_wq *hwc_wq)
520ce110ea1SWei Hu {
521ce110ea1SWei Hu 	mana_hwc_dealloc_dma_buf(hwc, hwc_wq->msg_buf);
522ce110ea1SWei Hu 
523ce110ea1SWei Hu 	if (hwc_wq->gdma_wq)
524ce110ea1SWei Hu 		mana_gd_destroy_queue(hwc->gdma_dev->gdma_context,
525ce110ea1SWei Hu 		    hwc_wq->gdma_wq);
526ce110ea1SWei Hu 
527ce110ea1SWei Hu 	free(hwc_wq, M_DEVBUF);
528ce110ea1SWei Hu }
529ce110ea1SWei Hu 
530ce110ea1SWei Hu static int
mana_hwc_create_wq(struct hw_channel_context * hwc,enum gdma_queue_type q_type,uint16_t q_depth,uint32_t max_msg_size,struct hwc_cq * hwc_cq,struct hwc_wq ** hwc_wq_ptr)531ce110ea1SWei Hu mana_hwc_create_wq(struct hw_channel_context *hwc,
532ce110ea1SWei Hu     enum gdma_queue_type q_type, uint16_t q_depth,
533ce110ea1SWei Hu     uint32_t max_msg_size, struct hwc_cq *hwc_cq,
534ce110ea1SWei Hu     struct hwc_wq **hwc_wq_ptr)
535ce110ea1SWei Hu {
536ce110ea1SWei Hu 	struct gdma_queue *queue;
537ce110ea1SWei Hu 	struct hwc_wq *hwc_wq;
538ce110ea1SWei Hu 	uint32_t queue_size;
539ce110ea1SWei Hu 	int err;
540ce110ea1SWei Hu 
541ce110ea1SWei Hu 	if (q_type != GDMA_SQ && q_type != GDMA_RQ) {
542ce110ea1SWei Hu 		/* XXX should fail and return error? */
543ce110ea1SWei Hu 		mana_warn(NULL, "Invalid q_type %u\n", q_type);
544ce110ea1SWei Hu 	}
545ce110ea1SWei Hu 
546ce110ea1SWei Hu 	if (q_type == GDMA_RQ)
547ce110ea1SWei Hu 		queue_size = roundup_pow_of_two(GDMA_MAX_RQE_SIZE * q_depth);
548ce110ea1SWei Hu 	else
549ce110ea1SWei Hu 		queue_size = roundup_pow_of_two(GDMA_MAX_SQE_SIZE * q_depth);
550ce110ea1SWei Hu 
551ce110ea1SWei Hu 	if (queue_size < MINIMUM_SUPPORTED_PAGE_SIZE)
552ce110ea1SWei Hu 		queue_size = MINIMUM_SUPPORTED_PAGE_SIZE;
553ce110ea1SWei Hu 
554ce110ea1SWei Hu 	hwc_wq = malloc(sizeof(*hwc_wq), M_DEVBUF, M_WAITOK | M_ZERO);
555ce110ea1SWei Hu 
556ce110ea1SWei Hu 	err = mana_hwc_create_gdma_wq(hwc, q_type, queue_size, &queue);
557ce110ea1SWei Hu 	if (err)
558ce110ea1SWei Hu 		goto out;
559ce110ea1SWei Hu 
560ce110ea1SWei Hu 	hwc_wq->hwc = hwc;
561ce110ea1SWei Hu 	hwc_wq->gdma_wq = queue;
562ce110ea1SWei Hu 	hwc_wq->queue_depth = q_depth;
563ce110ea1SWei Hu 	hwc_wq->hwc_cq = hwc_cq;
564ce110ea1SWei Hu 
565027d0c1cSWei Hu 	err = mana_hwc_alloc_dma_buf(hwc, q_depth, max_msg_size,
566027d0c1cSWei Hu 	    &hwc_wq->msg_buf);
567027d0c1cSWei Hu 	if (err)
568027d0c1cSWei Hu 		goto out;
569027d0c1cSWei Hu 
570ce110ea1SWei Hu 	*hwc_wq_ptr = hwc_wq;
571ce110ea1SWei Hu 	return 0;
572ce110ea1SWei Hu out:
573ce110ea1SWei Hu 	if (err)
574ce110ea1SWei Hu 		mana_hwc_destroy_wq(hwc, hwc_wq);
575ce110ea1SWei Hu 	return err;
576ce110ea1SWei Hu }
577ce110ea1SWei Hu 
578ce110ea1SWei Hu static int
mana_hwc_post_tx_wqe(const struct hwc_wq * hwc_txq,struct hwc_work_request * req,uint32_t dest_virt_rq_id,uint32_t dest_virt_rcq_id,bool dest_pf)579ce110ea1SWei Hu mana_hwc_post_tx_wqe(const struct hwc_wq *hwc_txq,
580ce110ea1SWei Hu     struct hwc_work_request *req,
581ce110ea1SWei Hu     uint32_t dest_virt_rq_id, uint32_t dest_virt_rcq_id,
582ce110ea1SWei Hu     bool dest_pf)
583ce110ea1SWei Hu {
584ce110ea1SWei Hu 	device_t dev = hwc_txq->hwc->dev;
585ce110ea1SWei Hu 	struct hwc_tx_oob *tx_oob;
586ce110ea1SWei Hu 	struct gdma_sge *sge;
587ce110ea1SWei Hu 	int err;
588ce110ea1SWei Hu 
589ce110ea1SWei Hu 	if (req->msg_size == 0 || req->msg_size > req->buf_len) {
590ce110ea1SWei Hu 		device_printf(dev, "wrong msg_size: %u, buf_len: %u\n",
591ce110ea1SWei Hu 		    req->msg_size, req->buf_len);
592ce110ea1SWei Hu 		return EINVAL;
593ce110ea1SWei Hu 	}
594ce110ea1SWei Hu 
595ce110ea1SWei Hu 	tx_oob = &req->tx_oob;
596ce110ea1SWei Hu 
597ce110ea1SWei Hu 	tx_oob->vrq_id = dest_virt_rq_id;
598ce110ea1SWei Hu 	tx_oob->dest_vfid = 0;
599ce110ea1SWei Hu 	tx_oob->vrcq_id = dest_virt_rcq_id;
600ce110ea1SWei Hu 	tx_oob->vscq_id = hwc_txq->hwc_cq->gdma_cq->id;
601ce110ea1SWei Hu 	tx_oob->loopback = false;
602ce110ea1SWei Hu 	tx_oob->lso_override = false;
603ce110ea1SWei Hu 	tx_oob->dest_pf = dest_pf;
604ce110ea1SWei Hu 	tx_oob->vsq_id = hwc_txq->gdma_wq->id;
605ce110ea1SWei Hu 
606ce110ea1SWei Hu 	sge = &req->sge;
607c5eed414SJohn Baldwin 	sge->address = (uintptr_t)req->buf_sge_addr;
608ce110ea1SWei Hu 	sge->mem_key = hwc_txq->msg_buf->gpa_mkey;
609ce110ea1SWei Hu 	sge->size = req->msg_size;
610ce110ea1SWei Hu 
611ce110ea1SWei Hu 	memset(&req->wqe_req, 0, sizeof(struct gdma_wqe_request));
612ce110ea1SWei Hu 	req->wqe_req.sgl = sge;
613ce110ea1SWei Hu 	req->wqe_req.num_sge = 1;
614ce110ea1SWei Hu 	req->wqe_req.inline_oob_size = sizeof(struct hwc_tx_oob);
615ce110ea1SWei Hu 	req->wqe_req.inline_oob_data = tx_oob;
616ce110ea1SWei Hu 	req->wqe_req.client_data_unit = 0;
617ce110ea1SWei Hu 
618ce110ea1SWei Hu 	err = mana_gd_post_and_ring(hwc_txq->gdma_wq, &req->wqe_req, NULL);
619ce110ea1SWei Hu 	if (err)
620ce110ea1SWei Hu 		device_printf(dev,
621ce110ea1SWei Hu 		    "Failed to post WQE on HWC SQ: %d\n", err);
622ce110ea1SWei Hu 	return err;
623ce110ea1SWei Hu }
624ce110ea1SWei Hu 
625ce110ea1SWei Hu static int
mana_hwc_init_inflight_msg(struct hw_channel_context * hwc,uint16_t num_msg)626ce110ea1SWei Hu mana_hwc_init_inflight_msg(struct hw_channel_context *hwc, uint16_t num_msg)
627ce110ea1SWei Hu {
628ce110ea1SWei Hu 	int err;
629ce110ea1SWei Hu 
630ce110ea1SWei Hu 	sema_init(&hwc->sema, num_msg, "gdma hwc sema");
631ce110ea1SWei Hu 
632ce110ea1SWei Hu 	err = mana_gd_alloc_res_map(num_msg, &hwc->inflight_msg_res,
633ce110ea1SWei Hu 	    "gdma hwc res lock");
634ce110ea1SWei Hu 	if (err)
635ce110ea1SWei Hu 		device_printf(hwc->dev,
636ce110ea1SWei Hu 		    "Failed to init inflight_msg_res: %d\n", err);
637ce110ea1SWei Hu 
638ce110ea1SWei Hu 	return (err);
639ce110ea1SWei Hu }
640ce110ea1SWei Hu 
641ce110ea1SWei Hu static int
mana_hwc_test_channel(struct hw_channel_context * hwc,uint16_t q_depth,uint32_t max_req_msg_size,uint32_t max_resp_msg_size)642ce110ea1SWei Hu mana_hwc_test_channel(struct hw_channel_context *hwc, uint16_t q_depth,
643ce110ea1SWei Hu     uint32_t max_req_msg_size, uint32_t max_resp_msg_size)
644ce110ea1SWei Hu {
645ce110ea1SWei Hu 	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
646ce110ea1SWei Hu 	struct hwc_wq *hwc_rxq = hwc->rxq;
647ce110ea1SWei Hu 	struct hwc_work_request *req;
648ce110ea1SWei Hu 	struct hwc_caller_ctx *ctx;
649ce110ea1SWei Hu 	int err;
650ce110ea1SWei Hu 	int i;
651ce110ea1SWei Hu 
652ce110ea1SWei Hu 	/* Post all WQEs on the RQ */
653ce110ea1SWei Hu 	for (i = 0; i < q_depth; i++) {
654ce110ea1SWei Hu 		req = &hwc_rxq->msg_buf->reqs[i];
655ce110ea1SWei Hu 		err = mana_hwc_post_rx_wqe(hwc_rxq, req);
656ce110ea1SWei Hu 		if (err)
657ce110ea1SWei Hu 			return err;
658ce110ea1SWei Hu 	}
659ce110ea1SWei Hu 
660ce110ea1SWei Hu 	ctx = malloc(q_depth * sizeof(struct hwc_caller_ctx),
661ce110ea1SWei Hu 	    M_DEVBUF, M_WAITOK | M_ZERO);
662ce110ea1SWei Hu 
663ce110ea1SWei Hu 	for (i = 0; i < q_depth; ++i)
664ce110ea1SWei Hu 		init_completion(&ctx[i].comp_event);
665ce110ea1SWei Hu 
666ce110ea1SWei Hu 	hwc->caller_ctx = ctx;
667ce110ea1SWei Hu 
668ce110ea1SWei Hu 	return mana_gd_test_eq(gc, hwc->cq->gdma_eq);
669ce110ea1SWei Hu }
670ce110ea1SWei Hu 
671ce110ea1SWei Hu static int
mana_hwc_establish_channel(struct gdma_context * gc,uint16_t * q_depth,uint32_t * max_req_msg_size,uint32_t * max_resp_msg_size)672ce110ea1SWei Hu mana_hwc_establish_channel(struct gdma_context *gc, uint16_t *q_depth,
673ce110ea1SWei Hu     uint32_t *max_req_msg_size,
674ce110ea1SWei Hu     uint32_t *max_resp_msg_size)
675ce110ea1SWei Hu {
676ce110ea1SWei Hu 	struct hw_channel_context *hwc = gc->hwc.driver_data;
677ce110ea1SWei Hu 	struct gdma_queue *rq = hwc->rxq->gdma_wq;
678ce110ea1SWei Hu 	struct gdma_queue *sq = hwc->txq->gdma_wq;
679ce110ea1SWei Hu 	struct gdma_queue *eq = hwc->cq->gdma_eq;
680ce110ea1SWei Hu 	struct gdma_queue *cq = hwc->cq->gdma_cq;
681ce110ea1SWei Hu 	int err;
682ce110ea1SWei Hu 
683ce110ea1SWei Hu 	init_completion(&hwc->hwc_init_eqe_comp);
684ce110ea1SWei Hu 
685ce110ea1SWei Hu 	err = mana_smc_setup_hwc(&gc->shm_channel, false,
686ce110ea1SWei Hu 	    eq->mem_info.dma_handle,
687ce110ea1SWei Hu 	    cq->mem_info.dma_handle,
688ce110ea1SWei Hu 	    rq->mem_info.dma_handle,
689ce110ea1SWei Hu 	    sq->mem_info.dma_handle,
690ce110ea1SWei Hu 	    eq->eq.msix_index);
691ce110ea1SWei Hu 	if (err)
692ce110ea1SWei Hu 		return err;
693ce110ea1SWei Hu 
694ce110ea1SWei Hu 	if (wait_for_completion_timeout(&hwc->hwc_init_eqe_comp, 60 * hz))
695ce110ea1SWei Hu 		return ETIMEDOUT;
696ce110ea1SWei Hu 
697ce110ea1SWei Hu 	*q_depth = hwc->hwc_init_q_depth_max;
698ce110ea1SWei Hu 	*max_req_msg_size = hwc->hwc_init_max_req_msg_size;
699ce110ea1SWei Hu 	*max_resp_msg_size = hwc->hwc_init_max_resp_msg_size;
700ce110ea1SWei Hu 
701623918a1SWei Hu 	/* Both were set in mana_hwc_init_event_handler(). */
702ce110ea1SWei Hu 	if (cq->id >= gc->max_num_cqs) {
703ce110ea1SWei Hu 		mana_warn(NULL, "invalid cq id %u > %u\n",
704ce110ea1SWei Hu 		    cq->id, gc->max_num_cqs);
705ce110ea1SWei Hu 		return EPROTO;
706ce110ea1SWei Hu 	}
707ce110ea1SWei Hu 
708ce110ea1SWei Hu 	gc->cq_table = malloc(gc->max_num_cqs * sizeof(struct gdma_queue *),
709ce110ea1SWei Hu 	    M_DEVBUF, M_WAITOK | M_ZERO);
710ce110ea1SWei Hu 	gc->cq_table[cq->id] = cq;
711ce110ea1SWei Hu 
712ce110ea1SWei Hu 	return 0;
713ce110ea1SWei Hu }
714ce110ea1SWei Hu 
715ce110ea1SWei Hu static int
mana_hwc_init_queues(struct hw_channel_context * hwc,uint16_t q_depth,uint32_t max_req_msg_size,uint32_t max_resp_msg_size)716ce110ea1SWei Hu mana_hwc_init_queues(struct hw_channel_context *hwc, uint16_t q_depth,
717ce110ea1SWei Hu     uint32_t max_req_msg_size, uint32_t max_resp_msg_size)
718ce110ea1SWei Hu {
719ce110ea1SWei Hu 	int err;
720ce110ea1SWei Hu 
721ce110ea1SWei Hu 	err = mana_hwc_init_inflight_msg(hwc, q_depth);
722ce110ea1SWei Hu 	if (err)
723ce110ea1SWei Hu 		return err;
724ce110ea1SWei Hu 
725ce110ea1SWei Hu 	/* CQ is shared by SQ and RQ, so CQ's queue depth is the sum of SQ
726ce110ea1SWei Hu 	 * queue depth and RQ queue depth.
727ce110ea1SWei Hu 	 */
728ce110ea1SWei Hu 	err = mana_hwc_create_cq(hwc, q_depth * 2,
729ce110ea1SWei Hu 	    mana_hwc_init_event_handler, hwc,
730ce110ea1SWei Hu 	    mana_hwc_rx_event_handler, hwc,
731623918a1SWei Hu 	    mana_hwc_tx_event_handler, hwc, &hwc->cq);
732ce110ea1SWei Hu 	if (err) {
733ce110ea1SWei Hu 		device_printf(hwc->dev, "Failed to create HWC CQ: %d\n", err);
734ce110ea1SWei Hu 		goto out;
735ce110ea1SWei Hu 	}
736ce110ea1SWei Hu 
737ce110ea1SWei Hu 	err = mana_hwc_create_wq(hwc, GDMA_RQ, q_depth, max_req_msg_size,
738623918a1SWei Hu 	    hwc->cq, &hwc->rxq);
739ce110ea1SWei Hu 	if (err) {
740ce110ea1SWei Hu 		device_printf(hwc->dev, "Failed to create HWC RQ: %d\n", err);
741ce110ea1SWei Hu 		goto out;
742ce110ea1SWei Hu 	}
743ce110ea1SWei Hu 
744ce110ea1SWei Hu 	err = mana_hwc_create_wq(hwc, GDMA_SQ, q_depth, max_resp_msg_size,
745623918a1SWei Hu 	    hwc->cq, &hwc->txq);
746ce110ea1SWei Hu 	if (err) {
747ce110ea1SWei Hu 		device_printf(hwc->dev, "Failed to create HWC SQ: %d\n", err);
748ce110ea1SWei Hu 		goto out;
749ce110ea1SWei Hu 	}
750ce110ea1SWei Hu 
751ce110ea1SWei Hu 	hwc->num_inflight_msg = q_depth;
752ce110ea1SWei Hu 	hwc->max_req_msg_size = max_req_msg_size;
753ce110ea1SWei Hu 
754ce110ea1SWei Hu 	return 0;
755ce110ea1SWei Hu out:
756623918a1SWei Hu 	/* mana_hwc_create_channel() will do the cleanup.*/
757ce110ea1SWei Hu 	return err;
758ce110ea1SWei Hu }
759ce110ea1SWei Hu 
760ce110ea1SWei Hu int
mana_hwc_create_channel(struct gdma_context * gc)761ce110ea1SWei Hu mana_hwc_create_channel(struct gdma_context *gc)
762ce110ea1SWei Hu {
763ce110ea1SWei Hu 	uint32_t max_req_msg_size, max_resp_msg_size;
764ce110ea1SWei Hu 	struct gdma_dev *gd = &gc->hwc;
765ce110ea1SWei Hu 	struct hw_channel_context *hwc;
766ce110ea1SWei Hu 	uint16_t q_depth_max;
767ce110ea1SWei Hu 	int err;
768ce110ea1SWei Hu 
769ce110ea1SWei Hu 	hwc = malloc(sizeof(*hwc), M_DEVBUF, M_WAITOK | M_ZERO);
770ce110ea1SWei Hu 
771ce110ea1SWei Hu 	gd->gdma_context = gc;
772ce110ea1SWei Hu 	gd->driver_data = hwc;
773ce110ea1SWei Hu 	hwc->gdma_dev = gd;
774ce110ea1SWei Hu 	hwc->dev = gc->dev;
775ce110ea1SWei Hu 
776ce110ea1SWei Hu 	/* HWC's instance number is always 0. */
777ce110ea1SWei Hu 	gd->dev_id.as_uint32 = 0;
778ce110ea1SWei Hu 	gd->dev_id.type = GDMA_DEVICE_HWC;
779ce110ea1SWei Hu 
780ce110ea1SWei Hu 	gd->pdid = INVALID_PDID;
781ce110ea1SWei Hu 	gd->doorbell = INVALID_DOORBELL;
782ce110ea1SWei Hu 
783623918a1SWei Hu 	/*
784623918a1SWei Hu 	 * mana_hwc_init_queues() only creates the required data structures,
785623918a1SWei Hu 	 * and doesn't touch the HWC device.
786623918a1SWei Hu 	 */
787ce110ea1SWei Hu 	err = mana_hwc_init_queues(hwc, HW_CHANNEL_VF_BOOTSTRAP_QUEUE_DEPTH,
788ce110ea1SWei Hu 	    HW_CHANNEL_MAX_REQUEST_SIZE,
789ce110ea1SWei Hu 	    HW_CHANNEL_MAX_RESPONSE_SIZE);
790ce110ea1SWei Hu 	if (err) {
791ce110ea1SWei Hu 		device_printf(hwc->dev, "Failed to initialize HWC: %d\n",
792ce110ea1SWei Hu 		    err);
793ce110ea1SWei Hu 		goto out;
794ce110ea1SWei Hu 	}
795ce110ea1SWei Hu 
796ce110ea1SWei Hu 	err = mana_hwc_establish_channel(gc, &q_depth_max, &max_req_msg_size,
797ce110ea1SWei Hu 	    &max_resp_msg_size);
798ce110ea1SWei Hu 	if (err) {
799ce110ea1SWei Hu 		device_printf(hwc->dev, "Failed to establish HWC: %d\n", err);
800ce110ea1SWei Hu 		goto out;
801ce110ea1SWei Hu 	}
802ce110ea1SWei Hu 
803ce110ea1SWei Hu 	err = mana_hwc_test_channel(gc->hwc.driver_data,
804ce110ea1SWei Hu 	    HW_CHANNEL_VF_BOOTSTRAP_QUEUE_DEPTH,
805ce110ea1SWei Hu 	    max_req_msg_size, max_resp_msg_size);
806ce110ea1SWei Hu 	if (err) {
807ce110ea1SWei Hu 		/* Test failed, but the channel has been established */
808ce110ea1SWei Hu 		device_printf(hwc->dev, "Failed to test HWC: %d\n", err);
809ce110ea1SWei Hu 		return EIO;
810ce110ea1SWei Hu 	}
811ce110ea1SWei Hu 
812ce110ea1SWei Hu 	return 0;
813ce110ea1SWei Hu out:
814623918a1SWei Hu 	mana_hwc_destroy_channel(gc);
815ce110ea1SWei Hu 	return (err);
816ce110ea1SWei Hu }
817ce110ea1SWei Hu 
818ce110ea1SWei Hu void
mana_hwc_destroy_channel(struct gdma_context * gc)819ce110ea1SWei Hu mana_hwc_destroy_channel(struct gdma_context *gc)
820ce110ea1SWei Hu {
821ce110ea1SWei Hu 	struct hw_channel_context *hwc = gc->hwc.driver_data;
822ce110ea1SWei Hu 
823623918a1SWei Hu 	if (!hwc)
824623918a1SWei Hu 		return;
825623918a1SWei Hu 
826623918a1SWei Hu 	/*
827623918a1SWei Hu 	 * gc->max_num_cqs is set in mana_hwc_init_event_handler(). If it's
828623918a1SWei Hu 	 * non-zero, the HWC worked and we should tear down the HWC here.
829623918a1SWei Hu 	 */
830623918a1SWei Hu 	if (gc->max_num_cqs > 0) {
831ce110ea1SWei Hu 		mana_smc_teardown_hwc(&gc->shm_channel, false);
832623918a1SWei Hu 		gc->max_num_cqs = 0;
833623918a1SWei Hu 	}
834ce110ea1SWei Hu 
835623918a1SWei Hu 	free(hwc->caller_ctx, M_DEVBUF);
836ce110ea1SWei Hu 	hwc->caller_ctx = NULL;
837ce110ea1SWei Hu 
838623918a1SWei Hu 	if (hwc->txq)
839ce110ea1SWei Hu 		mana_hwc_destroy_wq(hwc, hwc->txq);
840ce110ea1SWei Hu 
841623918a1SWei Hu 	if (hwc->rxq)
842ce110ea1SWei Hu 		mana_hwc_destroy_wq(hwc, hwc->rxq);
843ce110ea1SWei Hu 
844623918a1SWei Hu 	if (hwc->cq)
845ce110ea1SWei Hu 		mana_hwc_destroy_cq(hwc->gdma_dev->gdma_context, hwc->cq);
846ce110ea1SWei Hu 
847ce110ea1SWei Hu 	mana_gd_free_res_map(&hwc->inflight_msg_res);
848ce110ea1SWei Hu 
849ce110ea1SWei Hu 	hwc->num_inflight_msg = 0;
850ce110ea1SWei Hu 
851ce110ea1SWei Hu 	hwc->gdma_dev->doorbell = INVALID_DOORBELL;
852ce110ea1SWei Hu 	hwc->gdma_dev->pdid = INVALID_PDID;
853ce110ea1SWei Hu 
854ce110ea1SWei Hu 	free(hwc, M_DEVBUF);
855ce110ea1SWei Hu 	gc->hwc.driver_data = NULL;
856ce110ea1SWei Hu 	gc->hwc.gdma_context = NULL;
857623918a1SWei Hu 
858623918a1SWei Hu 	free(gc->cq_table, M_DEVBUF);
859623918a1SWei Hu 	gc->cq_table = NULL;
860ce110ea1SWei Hu }
861ce110ea1SWei Hu 
862ce110ea1SWei Hu int
mana_hwc_send_request(struct hw_channel_context * hwc,uint32_t req_len,const void * req,uint32_t resp_len,void * resp)863ce110ea1SWei Hu mana_hwc_send_request(struct hw_channel_context *hwc, uint32_t req_len,
864ce110ea1SWei Hu     const void *req, uint32_t resp_len, void *resp)
865ce110ea1SWei Hu {
866ce110ea1SWei Hu 	struct hwc_work_request *tx_wr;
867ce110ea1SWei Hu 	struct hwc_wq *txq = hwc->txq;
868ce110ea1SWei Hu 	struct gdma_req_hdr *req_msg;
869ce110ea1SWei Hu 	struct hwc_caller_ctx *ctx;
870ce110ea1SWei Hu 	uint16_t msg_id;
871ce110ea1SWei Hu 	int err;
872ce110ea1SWei Hu 
873ce110ea1SWei Hu 	mana_hwc_get_msg_index(hwc, &msg_id);
874ce110ea1SWei Hu 
875ce110ea1SWei Hu 	tx_wr = &txq->msg_buf->reqs[msg_id];
876ce110ea1SWei Hu 
877ce110ea1SWei Hu 	if (req_len > tx_wr->buf_len) {
878ce110ea1SWei Hu 		device_printf(hwc->dev,
879ce110ea1SWei Hu 		    "HWC: req msg size: %d > %d\n", req_len,
880ce110ea1SWei Hu 		    tx_wr->buf_len);
881ce110ea1SWei Hu 		err = EINVAL;
882ce110ea1SWei Hu 		goto out;
883ce110ea1SWei Hu 	}
884ce110ea1SWei Hu 
885ce110ea1SWei Hu 	ctx = hwc->caller_ctx + msg_id;
886ce110ea1SWei Hu 	ctx->output_buf = resp;
887ce110ea1SWei Hu 	ctx->output_buflen = resp_len;
888ce110ea1SWei Hu 
889ce110ea1SWei Hu 	req_msg = (struct gdma_req_hdr *)tx_wr->buf_va;
890ce110ea1SWei Hu 	if (req)
891ce110ea1SWei Hu 		memcpy(req_msg, req, req_len);
892ce110ea1SWei Hu 
893ce110ea1SWei Hu 	req_msg->req.hwc_msg_id = msg_id;
894ce110ea1SWei Hu 
895ce110ea1SWei Hu 	tx_wr->msg_size = req_len;
896ce110ea1SWei Hu 
897ce110ea1SWei Hu 	err = mana_hwc_post_tx_wqe(txq, tx_wr, 0, 0, false);
898ce110ea1SWei Hu 	if (err) {
899ce110ea1SWei Hu 		device_printf(hwc->dev,
900ce110ea1SWei Hu 		    "HWC: Failed to post send WQE: %d\n", err);
901ce110ea1SWei Hu 		goto out;
902ce110ea1SWei Hu 	}
903ce110ea1SWei Hu 
904ce110ea1SWei Hu 	if (wait_for_completion_timeout(&ctx->comp_event, 30 * hz)) {
905ce110ea1SWei Hu 		device_printf(hwc->dev, "HWC: Request timed out!\n");
906ce110ea1SWei Hu 		err = ETIMEDOUT;
907ce110ea1SWei Hu 		goto out;
908ce110ea1SWei Hu 	}
909ce110ea1SWei Hu 
910ce110ea1SWei Hu 	if (ctx->error) {
911ce110ea1SWei Hu 		err = ctx->error;
912ce110ea1SWei Hu 		goto out;
913ce110ea1SWei Hu 	}
914ce110ea1SWei Hu 
915b685df31SWei Hu 	if (ctx->status_code && ctx->status_code != GDMA_STATUS_MORE_ENTRIES) {
916ce110ea1SWei Hu 		device_printf(hwc->dev,
917ce110ea1SWei Hu 		    "HWC: Failed hw_channel req: 0x%x\n", ctx->status_code);
918ce110ea1SWei Hu 		err = EPROTO;
919ce110ea1SWei Hu 		goto out;
920ce110ea1SWei Hu 	}
921ce110ea1SWei Hu out:
922ce110ea1SWei Hu 	mana_hwc_put_msg_index(hwc, msg_id);
923ce110ea1SWei Hu 	return err;
924ce110ea1SWei Hu }
925