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