xref: /freebsd/sys/contrib/dev/athk/ath11k/dbring.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
4  */
5 
6 #include "core.h"
7 #include "debug.h"
8 
9 #define ATH11K_DB_MAGIC_VALUE 0xdeadbeaf
10 
11 int ath11k_dbring_validate_buffer(struct ath11k *ar, void *buffer, u32 size)
12 {
13 	u32 *temp;
14 	int idx;
15 
16 	size = size >> 2;
17 
18 	for (idx = 0, temp = buffer; idx < size; idx++, temp++) {
19 		if (*temp == ATH11K_DB_MAGIC_VALUE)
20 			return -EINVAL;
21 	}
22 
23 	return 0;
24 }
25 
26 static void ath11k_dbring_fill_magic_value(struct ath11k *ar,
27 					   void *buffer, u32 size)
28 {
29 	u32 *temp;
30 	int idx;
31 
32 	size = size >> 2;
33 
34 	for (idx = 0, temp = buffer; idx < size; idx++, temp++)
35 		*temp++ = ATH11K_DB_MAGIC_VALUE;
36 }
37 
38 static int ath11k_dbring_bufs_replenish(struct ath11k *ar,
39 					struct ath11k_dbring *ring,
40 					struct ath11k_dbring_element *buff)
41 {
42 	struct ath11k_base *ab = ar->ab;
43 	struct hal_srng *srng;
44 	dma_addr_t paddr;
45 	void *ptr_aligned, *ptr_unaligned, *desc;
46 	int ret;
47 	int buf_id;
48 	u32 cookie;
49 
50 	srng = &ab->hal.srng_list[ring->refill_srng.ring_id];
51 
52 	lockdep_assert_held(&srng->lock);
53 
54 	ath11k_hal_srng_access_begin(ab, srng);
55 
56 	ptr_unaligned = buff->payload;
57 	ptr_aligned = PTR_ALIGN(ptr_unaligned, ring->buf_align);
58 	ath11k_dbring_fill_magic_value(ar, ptr_aligned, ring->buf_sz);
59 	paddr = dma_map_single(ab->dev, ptr_aligned, ring->buf_sz,
60 			       DMA_FROM_DEVICE);
61 
62 	ret = dma_mapping_error(ab->dev, paddr);
63 	if (ret)
64 		goto err;
65 
66 	spin_lock_bh(&ring->idr_lock);
67 	buf_id = idr_alloc(&ring->bufs_idr, buff, 0, ring->bufs_max, GFP_ATOMIC);
68 	spin_unlock_bh(&ring->idr_lock);
69 	if (buf_id < 0) {
70 		ret = -ENOBUFS;
71 		goto err_dma_unmap;
72 	}
73 
74 	desc = ath11k_hal_srng_src_get_next_entry(ab, srng);
75 	if (!desc) {
76 		ret = -ENOENT;
77 		goto err_idr_remove;
78 	}
79 
80 	buff->paddr = paddr;
81 
82 	cookie = FIELD_PREP(DP_RXDMA_BUF_COOKIE_PDEV_ID, ar->pdev_idx) |
83 		 FIELD_PREP(DP_RXDMA_BUF_COOKIE_BUF_ID, buf_id);
84 
85 	ath11k_hal_rx_buf_addr_info_set(desc, paddr, cookie, 0);
86 
87 	ath11k_hal_srng_access_end(ab, srng);
88 
89 	return 0;
90 
91 err_idr_remove:
92 	spin_lock_bh(&ring->idr_lock);
93 	idr_remove(&ring->bufs_idr, buf_id);
94 	spin_unlock_bh(&ring->idr_lock);
95 err_dma_unmap:
96 	dma_unmap_single(ab->dev, paddr, ring->buf_sz,
97 			 DMA_FROM_DEVICE);
98 err:
99 	ath11k_hal_srng_access_end(ab, srng);
100 	return ret;
101 }
102 
103 static int ath11k_dbring_fill_bufs(struct ath11k *ar,
104 				   struct ath11k_dbring *ring)
105 {
106 	struct ath11k_dbring_element *buff;
107 	struct hal_srng *srng;
108 	int num_remain, req_entries, num_free;
109 	u32 align;
110 	int size, ret;
111 
112 	srng = &ar->ab->hal.srng_list[ring->refill_srng.ring_id];
113 
114 	spin_lock_bh(&srng->lock);
115 
116 	num_free = ath11k_hal_srng_src_num_free(ar->ab, srng, true);
117 	req_entries = min(num_free, ring->bufs_max);
118 	num_remain = req_entries;
119 	align = ring->buf_align;
120 	size = ring->buf_sz + align - 1;
121 
122 	while (num_remain > 0) {
123 		buff = kzalloc(sizeof(*buff), GFP_ATOMIC);
124 		if (!buff)
125 			break;
126 
127 		buff->payload = kzalloc(size, GFP_ATOMIC);
128 		if (!buff->payload) {
129 			kfree(buff);
130 			break;
131 		}
132 		ret = ath11k_dbring_bufs_replenish(ar, ring, buff);
133 		if (ret) {
134 			ath11k_warn(ar->ab, "failed to replenish db ring num_remain %d req_ent %d\n",
135 				    num_remain, req_entries);
136 			kfree(buff->payload);
137 			kfree(buff);
138 			break;
139 		}
140 		num_remain--;
141 	}
142 
143 	spin_unlock_bh(&srng->lock);
144 
145 	return num_remain;
146 }
147 
148 int ath11k_dbring_wmi_cfg_setup(struct ath11k *ar,
149 				struct ath11k_dbring *ring,
150 				enum wmi_direct_buffer_module id)
151 {
152 	struct ath11k_wmi_pdev_dma_ring_cfg_req_cmd param = {0};
153 	int ret;
154 
155 	if (id >= WMI_DIRECT_BUF_MAX)
156 		return -EINVAL;
157 
158 	param.pdev_id		= DP_SW2HW_MACID(ring->pdev_id);
159 	param.module_id		= id;
160 	param.base_paddr_lo	= lower_32_bits(ring->refill_srng.paddr);
161 	param.base_paddr_hi	= upper_32_bits(ring->refill_srng.paddr);
162 	param.head_idx_paddr_lo	= lower_32_bits(ring->hp_addr);
163 	param.head_idx_paddr_hi = upper_32_bits(ring->hp_addr);
164 	param.tail_idx_paddr_lo = lower_32_bits(ring->tp_addr);
165 	param.tail_idx_paddr_hi = upper_32_bits(ring->tp_addr);
166 	param.num_elems		= ring->bufs_max;
167 	param.buf_size		= ring->buf_sz;
168 	param.num_resp_per_event = ring->num_resp_per_event;
169 	param.event_timeout_ms	= ring->event_timeout_ms;
170 
171 	ret = ath11k_wmi_pdev_dma_ring_cfg(ar, &param);
172 	if (ret) {
173 		ath11k_warn(ar->ab, "failed to setup db ring cfg\n");
174 		return ret;
175 	}
176 
177 	return 0;
178 }
179 
180 int ath11k_dbring_set_cfg(struct ath11k *ar, struct ath11k_dbring *ring,
181 			  u32 num_resp_per_event, u32 event_timeout_ms,
182 			  int (*handler)(struct ath11k *,
183 					 struct ath11k_dbring_data *))
184 {
185 	if (WARN_ON(!ring))
186 		return -EINVAL;
187 
188 	ring->num_resp_per_event = num_resp_per_event;
189 	ring->event_timeout_ms = event_timeout_ms;
190 	ring->handler = handler;
191 
192 	return 0;
193 }
194 
195 int ath11k_dbring_buf_setup(struct ath11k *ar,
196 			    struct ath11k_dbring *ring,
197 			    struct ath11k_dbring_cap *db_cap)
198 {
199 	struct ath11k_base *ab = ar->ab;
200 	struct hal_srng *srng;
201 	int ret;
202 
203 	srng = &ab->hal.srng_list[ring->refill_srng.ring_id];
204 	ring->bufs_max = ring->refill_srng.size /
205 		ath11k_hal_srng_get_entrysize(ab, HAL_RXDMA_DIR_BUF);
206 
207 	ring->buf_sz = db_cap->min_buf_sz;
208 	ring->buf_align = db_cap->min_buf_align;
209 	ring->pdev_id = db_cap->pdev_id;
210 	ring->hp_addr = ath11k_hal_srng_get_hp_addr(ar->ab, srng);
211 	ring->tp_addr = ath11k_hal_srng_get_tp_addr(ar->ab, srng);
212 
213 	ret = ath11k_dbring_fill_bufs(ar, ring);
214 
215 	return ret;
216 }
217 
218 int ath11k_dbring_srng_setup(struct ath11k *ar, struct ath11k_dbring *ring,
219 			     int ring_num, int num_entries)
220 {
221 	int ret;
222 
223 	ret = ath11k_dp_srng_setup(ar->ab, &ring->refill_srng, HAL_RXDMA_DIR_BUF,
224 				   ring_num, ar->pdev_idx, num_entries);
225 	if (ret < 0) {
226 		ath11k_warn(ar->ab, "failed to setup srng: %d ring_id %d\n",
227 			    ret, ring_num);
228 		goto err;
229 	}
230 
231 	return 0;
232 err:
233 	ath11k_dp_srng_cleanup(ar->ab, &ring->refill_srng);
234 	return ret;
235 }
236 
237 int ath11k_dbring_get_cap(struct ath11k_base *ab,
238 			  u8 pdev_idx,
239 			  enum wmi_direct_buffer_module id,
240 			  struct ath11k_dbring_cap *db_cap)
241 {
242 	int i;
243 
244 	if (!ab->num_db_cap || !ab->db_caps)
245 		return -ENOENT;
246 
247 	if (id >= WMI_DIRECT_BUF_MAX)
248 		return -EINVAL;
249 
250 	for (i = 0; i < ab->num_db_cap; i++) {
251 		if (pdev_idx == ab->db_caps[i].pdev_id &&
252 		    id == ab->db_caps[i].id) {
253 			*db_cap = ab->db_caps[i];
254 
255 			return 0;
256 		}
257 	}
258 
259 	return -ENOENT;
260 }
261 
262 int ath11k_dbring_buffer_release_event(struct ath11k_base *ab,
263 				       struct ath11k_dbring_buf_release_event *ev)
264 {
265 	struct ath11k_dbring *ring;
266 	struct hal_srng *srng;
267 	struct ath11k *ar;
268 	struct ath11k_dbring_element *buff;
269 	struct ath11k_dbring_data handler_data;
270 	struct ath11k_buffer_addr desc;
271 	u8 *vaddr_unalign;
272 	u32 num_entry, num_buff_reaped;
273 	u8 pdev_idx, rbm;
274 	u32 cookie;
275 	int buf_id;
276 	int size;
277 	dma_addr_t paddr;
278 	int ret = 0;
279 
280 	pdev_idx = ev->fixed.pdev_id;
281 
282 	if (pdev_idx >= ab->num_radios) {
283 		ath11k_warn(ab, "Invalid pdev id %d\n", pdev_idx);
284 		return -EINVAL;
285 	}
286 
287 	if (ev->fixed.num_buf_release_entry !=
288 	    ev->fixed.num_meta_data_entry) {
289 		ath11k_warn(ab, "Buffer entry %d mismatch meta entry %d\n",
290 			    ev->fixed.num_buf_release_entry,
291 			    ev->fixed.num_meta_data_entry);
292 		return -EINVAL;
293 	}
294 
295 	ar = ab->pdevs[pdev_idx].ar;
296 
297 	rcu_read_lock();
298 	if (!rcu_dereference(ab->pdevs_active[pdev_idx])) {
299 		ret = -EINVAL;
300 		goto rcu_unlock;
301 	}
302 
303 	switch (ev->fixed.module_id) {
304 	case WMI_DIRECT_BUF_SPECTRAL:
305 		ring = ath11k_spectral_get_dbring(ar);
306 		break;
307 	default:
308 		ring = NULL;
309 		ath11k_warn(ab, "Recv dma buffer release ev on unsupp module %d\n",
310 			    ev->fixed.module_id);
311 		break;
312 	}
313 
314 	if (!ring) {
315 		ret = -EINVAL;
316 		goto rcu_unlock;
317 	}
318 
319 	srng = &ab->hal.srng_list[ring->refill_srng.ring_id];
320 	num_entry = ev->fixed.num_buf_release_entry;
321 	size = ring->buf_sz + ring->buf_align - 1;
322 	num_buff_reaped = 0;
323 
324 	spin_lock_bh(&srng->lock);
325 
326 	while (num_buff_reaped < num_entry) {
327 		desc.info0 = ev->buf_entry[num_buff_reaped].paddr_lo;
328 		desc.info1 = ev->buf_entry[num_buff_reaped].paddr_hi;
329 		handler_data.meta = ev->meta_data[num_buff_reaped];
330 
331 		num_buff_reaped++;
332 
333 		ath11k_hal_rx_buf_addr_info_get(&desc, &paddr, &cookie, &rbm);
334 
335 		buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID, cookie);
336 
337 		spin_lock_bh(&ring->idr_lock);
338 		buff = idr_find(&ring->bufs_idr, buf_id);
339 		if (!buff) {
340 			spin_unlock_bh(&ring->idr_lock);
341 			continue;
342 		}
343 		idr_remove(&ring->bufs_idr, buf_id);
344 		spin_unlock_bh(&ring->idr_lock);
345 
346 		dma_unmap_single(ab->dev, buff->paddr, ring->buf_sz,
347 				 DMA_FROM_DEVICE);
348 
349 		if (ring->handler) {
350 			vaddr_unalign = buff->payload;
351 			handler_data.data = PTR_ALIGN(vaddr_unalign,
352 						      ring->buf_align);
353 			handler_data.data_sz = ring->buf_sz;
354 
355 			ring->handler(ar, &handler_data);
356 		}
357 
358 		buff->paddr = 0;
359 		memset(buff->payload, 0, size);
360 		ath11k_dbring_bufs_replenish(ar, ring, buff);
361 	}
362 
363 	spin_unlock_bh(&srng->lock);
364 
365 rcu_unlock:
366 	rcu_read_unlock();
367 
368 	return ret;
369 }
370 
371 void ath11k_dbring_srng_cleanup(struct ath11k *ar, struct ath11k_dbring *ring)
372 {
373 	ath11k_dp_srng_cleanup(ar->ab, &ring->refill_srng);
374 }
375 
376 void ath11k_dbring_buf_cleanup(struct ath11k *ar, struct ath11k_dbring *ring)
377 {
378 	struct ath11k_dbring_element *buff;
379 	int buf_id;
380 
381 	spin_lock_bh(&ring->idr_lock);
382 	idr_for_each_entry(&ring->bufs_idr, buff, buf_id) {
383 		idr_remove(&ring->bufs_idr, buf_id);
384 		dma_unmap_single(ar->ab->dev, buff->paddr,
385 				 ring->buf_sz, DMA_FROM_DEVICE);
386 		kfree(buff->payload);
387 		kfree(buff);
388 	}
389 
390 	idr_destroy(&ring->bufs_idr);
391 	spin_unlock_bh(&ring->idr_lock);
392 }
393