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