xref: /freebsd/sys/contrib/dev/athk/ath10k/htc.c (revision 9954217599ce02fbf1772388e24e0b89663f4425)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2005-2011 Atheros Communications Inc.
4  * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
5  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
6  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
7  */
8 
9 #include <linux/export.h>
10 
11 #include "core.h"
12 #include "hif.h"
13 #include "debug.h"
14 #if defined(__FreeBSD__)
15 #include <linux/delay.h>
16 #endif
17 
18 /********/
19 /* Send */
20 /********/
21 
ath10k_htc_control_tx_complete(struct ath10k * ar,struct sk_buff * skb)22 static void ath10k_htc_control_tx_complete(struct ath10k *ar,
23 					   struct sk_buff *skb)
24 {
25 	kfree_skb(skb);
26 }
27 
ath10k_htc_build_tx_ctrl_skb(void * ar)28 static struct sk_buff *ath10k_htc_build_tx_ctrl_skb(void *ar)
29 {
30 	struct sk_buff *skb;
31 	struct ath10k_skb_cb *skb_cb;
32 
33 	skb = dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE);
34 	if (!skb)
35 		return NULL;
36 
37 	skb_reserve(skb, 20); /* FIXME: why 20 bytes? */
38 	WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
39 
40 	skb_cb = ATH10K_SKB_CB(skb);
41 	memset(skb_cb, 0, sizeof(*skb_cb));
42 
43 	ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: skb %p\n", __func__, skb);
44 	return skb;
45 }
46 
ath10k_htc_restore_tx_skb(struct ath10k_htc * htc,struct sk_buff * skb)47 static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
48 					     struct sk_buff *skb)
49 {
50 	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
51 
52 	if (htc->ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL)
53 		dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
54 	skb_pull(skb, sizeof(struct ath10k_htc_hdr));
55 }
56 
ath10k_htc_notify_tx_completion(struct ath10k_htc_ep * ep,struct sk_buff * skb)57 void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
58 				     struct sk_buff *skb)
59 {
60 	struct ath10k *ar = ep->htc->ar;
61 	struct ath10k_htc_hdr *hdr;
62 
63 	ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,
64 		   ep->eid, skb);
65 
66 	/* A corner case where the copy completion is reaching to host but still
67 	 * copy engine is processing it due to which host unmaps corresponding
68 	 * memory and causes SMMU fault, hence as workaround adding delay
69 	 * the unmapping memory to avoid SMMU faults.
70 	 */
71 	if (ar->hw_params.delay_unmap_buffer &&
72 	    ep->ul_pipe_id == 3)
73 		mdelay(2);
74 
75 	hdr = (struct ath10k_htc_hdr *)skb->data;
76 	ath10k_htc_restore_tx_skb(ep->htc, skb);
77 
78 	if (!ep->ep_ops.ep_tx_complete) {
79 		ath10k_warn(ar, "no tx handler for eid %d\n", ep->eid);
80 		dev_kfree_skb_any(skb);
81 		return;
82 	}
83 
84 	if (hdr->flags & ATH10K_HTC_FLAG_SEND_BUNDLE) {
85 		dev_kfree_skb_any(skb);
86 		return;
87 	}
88 
89 	ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);
90 }
91 EXPORT_SYMBOL(ath10k_htc_notify_tx_completion);
92 
ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep * ep,struct sk_buff * skb)93 static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
94 				      struct sk_buff *skb)
95 {
96 	struct ath10k_htc_hdr *hdr;
97 
98 	hdr = (struct ath10k_htc_hdr *)skb->data;
99 	memset(hdr, 0, sizeof(struct ath10k_htc_hdr));
100 
101 	hdr->eid = ep->eid;
102 	hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
103 	hdr->flags = 0;
104 	if (ep->tx_credit_flow_enabled && !ep->bundle_tx)
105 		hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
106 
107 	spin_lock_bh(&ep->htc->tx_lock);
108 	hdr->seq_no = ep->seq_no++;
109 	spin_unlock_bh(&ep->htc->tx_lock);
110 }
111 
ath10k_htc_consume_credit(struct ath10k_htc_ep * ep,unsigned int len,bool consume)112 static int ath10k_htc_consume_credit(struct ath10k_htc_ep *ep,
113 				     unsigned int len,
114 				     bool consume)
115 {
116 	struct ath10k_htc *htc = ep->htc;
117 	struct ath10k *ar = htc->ar;
118 	enum ath10k_htc_ep_id eid = ep->eid;
119 	int credits, ret = 0;
120 
121 	if (!ep->tx_credit_flow_enabled)
122 		return 0;
123 
124 	credits = DIV_ROUND_UP(len, ep->tx_credit_size);
125 	spin_lock_bh(&htc->tx_lock);
126 
127 	if (ep->tx_credits < credits) {
128 		ath10k_dbg(ar, ATH10K_DBG_HTC,
129 			   "htc insufficient credits ep %d required %d available %d consume %d\n",
130 			   eid, credits, ep->tx_credits, consume);
131 		ret = -EAGAIN;
132 		goto unlock;
133 	}
134 
135 	if (consume) {
136 		ep->tx_credits -= credits;
137 		ath10k_dbg(ar, ATH10K_DBG_HTC,
138 			   "htc ep %d consumed %d credits total %d\n",
139 			   eid, credits, ep->tx_credits);
140 	}
141 
142 unlock:
143 	spin_unlock_bh(&htc->tx_lock);
144 	return ret;
145 }
146 
ath10k_htc_release_credit(struct ath10k_htc_ep * ep,unsigned int len)147 static void ath10k_htc_release_credit(struct ath10k_htc_ep *ep, unsigned int len)
148 {
149 	struct ath10k_htc *htc = ep->htc;
150 	struct ath10k *ar = htc->ar;
151 	enum ath10k_htc_ep_id eid = ep->eid;
152 	int credits;
153 
154 	if (!ep->tx_credit_flow_enabled)
155 		return;
156 
157 	credits = DIV_ROUND_UP(len, ep->tx_credit_size);
158 	spin_lock_bh(&htc->tx_lock);
159 	ep->tx_credits += credits;
160 	ath10k_dbg(ar, ATH10K_DBG_HTC,
161 		   "htc ep %d reverted %d credits back total %d\n",
162 		   eid, credits, ep->tx_credits);
163 	spin_unlock_bh(&htc->tx_lock);
164 
165 	if (ep->ep_ops.ep_tx_credits)
166 		ep->ep_ops.ep_tx_credits(htc->ar);
167 }
168 
ath10k_htc_send(struct ath10k_htc * htc,enum ath10k_htc_ep_id eid,struct sk_buff * skb)169 int ath10k_htc_send(struct ath10k_htc *htc,
170 		    enum ath10k_htc_ep_id eid,
171 		    struct sk_buff *skb)
172 {
173 	struct ath10k *ar = htc->ar;
174 	struct ath10k_htc_ep *ep = &htc->endpoint[eid];
175 	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
176 	struct ath10k_hif_sg_item sg_item;
177 	struct device *dev = htc->ar->dev;
178 	int ret;
179 	unsigned int skb_len;
180 
181 	if (htc->ar->state == ATH10K_STATE_WEDGED)
182 		return -ECOMM;
183 
184 	if (eid >= ATH10K_HTC_EP_COUNT) {
185 		ath10k_warn(ar, "Invalid endpoint id: %d\n", eid);
186 		return -ENOENT;
187 	}
188 
189 	skb_push(skb, sizeof(struct ath10k_htc_hdr));
190 
191 	skb_len = skb->len;
192 	ret = ath10k_htc_consume_credit(ep, skb_len, true);
193 	if (ret)
194 		goto err_pull;
195 
196 	ath10k_htc_prepare_tx_skb(ep, skb);
197 
198 	skb_cb->eid = eid;
199 	if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL) {
200 		skb_cb->paddr = dma_map_single(dev, skb->data, skb->len,
201 					       DMA_TO_DEVICE);
202 		ret = dma_mapping_error(dev, skb_cb->paddr);
203 		if (ret) {
204 			ret = -EIO;
205 			goto err_credits;
206 		}
207 	}
208 
209 	sg_item.transfer_id = ep->eid;
210 	sg_item.transfer_context = skb;
211 	sg_item.vaddr = skb->data;
212 	sg_item.paddr = skb_cb->paddr;
213 	sg_item.len = skb->len;
214 
215 	ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);
216 	if (ret)
217 		goto err_unmap;
218 
219 	return 0;
220 
221 err_unmap:
222 	if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL)
223 		dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
224 err_credits:
225 	ath10k_htc_release_credit(ep, skb_len);
226 err_pull:
227 	skb_pull(skb, sizeof(struct ath10k_htc_hdr));
228 	return ret;
229 }
230 
ath10k_htc_tx_completion_handler(struct ath10k * ar,struct sk_buff * skb)231 void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb)
232 {
233 	struct ath10k_htc *htc = &ar->htc;
234 	struct ath10k_skb_cb *skb_cb;
235 	struct ath10k_htc_ep *ep;
236 
237 	if (WARN_ON_ONCE(!skb))
238 		return;
239 
240 	skb_cb = ATH10K_SKB_CB(skb);
241 	ep = &htc->endpoint[skb_cb->eid];
242 
243 	ath10k_htc_notify_tx_completion(ep, skb);
244 	/* the skb now belongs to the completion handler */
245 }
246 EXPORT_SYMBOL(ath10k_htc_tx_completion_handler);
247 
248 /***********/
249 /* Receive */
250 /***********/
251 
252 static void
ath10k_htc_process_credit_report(struct ath10k_htc * htc,const struct ath10k_htc_credit_report * report,int len,enum ath10k_htc_ep_id eid)253 ath10k_htc_process_credit_report(struct ath10k_htc *htc,
254 				 const struct ath10k_htc_credit_report *report,
255 				 int len,
256 				 enum ath10k_htc_ep_id eid)
257 {
258 	struct ath10k *ar = htc->ar;
259 	struct ath10k_htc_ep *ep;
260 	int i, n_reports;
261 
262 	if (len % sizeof(*report))
263 		ath10k_warn(ar, "Uneven credit report len %d", len);
264 
265 	n_reports = len / sizeof(*report);
266 
267 	spin_lock_bh(&htc->tx_lock);
268 	for (i = 0; i < n_reports; i++, report++) {
269 		if (report->eid >= ATH10K_HTC_EP_COUNT)
270 			break;
271 
272 		ep = &htc->endpoint[report->eid];
273 		ep->tx_credits += report->credits;
274 
275 		ath10k_dbg(ar, ATH10K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
276 			   report->eid, report->credits, ep->tx_credits);
277 
278 		if (ep->ep_ops.ep_tx_credits) {
279 			spin_unlock_bh(&htc->tx_lock);
280 			ep->ep_ops.ep_tx_credits(htc->ar);
281 			spin_lock_bh(&htc->tx_lock);
282 		}
283 	}
284 	spin_unlock_bh(&htc->tx_lock);
285 }
286 
287 static int
ath10k_htc_process_lookahead(struct ath10k_htc * htc,const struct ath10k_htc_lookahead_report * report,int len,enum ath10k_htc_ep_id eid,void * next_lookaheads,int * next_lookaheads_len)288 ath10k_htc_process_lookahead(struct ath10k_htc *htc,
289 			     const struct ath10k_htc_lookahead_report *report,
290 			     int len,
291 			     enum ath10k_htc_ep_id eid,
292 			     void *next_lookaheads,
293 			     int *next_lookaheads_len)
294 {
295 	struct ath10k *ar = htc->ar;
296 
297 	/* Invalid lookahead flags are actually transmitted by
298 	 * the target in the HTC control message.
299 	 * Since this will happen at every boot we silently ignore
300 	 * the lookahead in this case
301 	 */
302 	if (report->pre_valid != ((~report->post_valid) & 0xFF))
303 		return 0;
304 
305 	if (next_lookaheads && next_lookaheads_len) {
306 		ath10k_dbg(ar, ATH10K_DBG_HTC,
307 			   "htc rx lookahead found pre_valid 0x%x post_valid 0x%x\n",
308 			   report->pre_valid, report->post_valid);
309 
310 		/* look ahead bytes are valid, copy them over */
311 		memcpy((u8 *)next_lookaheads, report->lookahead, 4);
312 
313 		*next_lookaheads_len = 1;
314 	}
315 
316 	return 0;
317 }
318 
319 static int
ath10k_htc_process_lookahead_bundle(struct ath10k_htc * htc,const struct ath10k_htc_lookahead_bundle * report,int len,enum ath10k_htc_ep_id eid,void * next_lookaheads,int * next_lookaheads_len)320 ath10k_htc_process_lookahead_bundle(struct ath10k_htc *htc,
321 				    const struct ath10k_htc_lookahead_bundle *report,
322 				    int len,
323 				    enum ath10k_htc_ep_id eid,
324 				    void *next_lookaheads,
325 				    int *next_lookaheads_len)
326 {
327 	struct ath10k *ar = htc->ar;
328 	int bundle_cnt = len / sizeof(*report);
329 
330 	if (!bundle_cnt || (bundle_cnt > htc->max_msgs_per_htc_bundle)) {
331 		ath10k_warn(ar, "Invalid lookahead bundle count: %d\n",
332 			    bundle_cnt);
333 		return -EINVAL;
334 	}
335 
336 	if (next_lookaheads && next_lookaheads_len) {
337 		int i;
338 
339 		for (i = 0; i < bundle_cnt; i++) {
340 			memcpy(((u8 *)next_lookaheads) + 4 * i,
341 			       report->lookahead, 4);
342 			report++;
343 		}
344 
345 		*next_lookaheads_len = bundle_cnt;
346 	}
347 
348 	return 0;
349 }
350 
ath10k_htc_process_trailer(struct ath10k_htc * htc,u8 * buffer,int length,enum ath10k_htc_ep_id src_eid,void * next_lookaheads,int * next_lookaheads_len)351 int ath10k_htc_process_trailer(struct ath10k_htc *htc,
352 			       u8 *buffer,
353 			       int length,
354 			       enum ath10k_htc_ep_id src_eid,
355 			       void *next_lookaheads,
356 			       int *next_lookaheads_len)
357 {
358 	struct ath10k_htc_lookahead_bundle *bundle;
359 	struct ath10k *ar = htc->ar;
360 	int status = 0;
361 	struct ath10k_htc_record *record;
362 	u8 *orig_buffer;
363 	int orig_length;
364 	size_t len;
365 
366 	orig_buffer = buffer;
367 	orig_length = length;
368 
369 	while (length > 0) {
370 		record = (struct ath10k_htc_record *)buffer;
371 
372 		if (length < sizeof(record->hdr)) {
373 			status = -EINVAL;
374 			break;
375 		}
376 
377 		if (record->hdr.len > length) {
378 			/* no room left in buffer for record */
379 			ath10k_warn(ar, "Invalid record length: %d\n",
380 				    record->hdr.len);
381 			status = -EINVAL;
382 			break;
383 		}
384 
385 		switch (record->hdr.id) {
386 		case ATH10K_HTC_RECORD_CREDITS:
387 			len = sizeof(struct ath10k_htc_credit_report);
388 			if (record->hdr.len < len) {
389 				ath10k_warn(ar, "Credit report too long\n");
390 				status = -EINVAL;
391 				break;
392 			}
393 			ath10k_htc_process_credit_report(htc,
394 							 record->credit_report,
395 							 record->hdr.len,
396 							 src_eid);
397 			break;
398 		case ATH10K_HTC_RECORD_LOOKAHEAD:
399 			len = sizeof(struct ath10k_htc_lookahead_report);
400 			if (record->hdr.len < len) {
401 				ath10k_warn(ar, "Lookahead report too long\n");
402 				status = -EINVAL;
403 				break;
404 			}
405 			status = ath10k_htc_process_lookahead(htc,
406 							      record->lookahead_report,
407 							      record->hdr.len,
408 							      src_eid,
409 							      next_lookaheads,
410 							      next_lookaheads_len);
411 			break;
412 		case ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE:
413 			bundle = record->lookahead_bundle;
414 			status = ath10k_htc_process_lookahead_bundle(htc,
415 								     bundle,
416 								     record->hdr.len,
417 								     src_eid,
418 								     next_lookaheads,
419 								     next_lookaheads_len);
420 			break;
421 		default:
422 			ath10k_warn(ar, "Unhandled record: id:%d length:%d\n",
423 				    record->hdr.id, record->hdr.len);
424 			break;
425 		}
426 
427 		if (status)
428 			break;
429 
430 		/* multiple records may be present in a trailer */
431 		buffer += sizeof(record->hdr) + record->hdr.len;
432 		length -= sizeof(record->hdr) + record->hdr.len;
433 	}
434 
435 	if (status)
436 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc rx bad trailer", "",
437 				orig_buffer, orig_length);
438 
439 	return status;
440 }
441 EXPORT_SYMBOL(ath10k_htc_process_trailer);
442 
ath10k_htc_rx_completion_handler(struct ath10k * ar,struct sk_buff * skb)443 void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb)
444 {
445 	int status = 0;
446 	struct ath10k_htc *htc = &ar->htc;
447 	struct ath10k_htc_hdr *hdr;
448 	struct ath10k_htc_ep *ep;
449 	u16 payload_len;
450 	u32 trailer_len = 0;
451 	size_t min_len;
452 	u8 eid;
453 	bool trailer_present;
454 
455 	hdr = (struct ath10k_htc_hdr *)skb->data;
456 	skb_pull(skb, sizeof(*hdr));
457 
458 	eid = hdr->eid;
459 
460 	if (eid >= ATH10K_HTC_EP_COUNT) {
461 		ath10k_warn(ar, "HTC Rx: invalid eid %d\n", eid);
462 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad header", "",
463 				hdr, sizeof(*hdr));
464 		goto out;
465 	}
466 
467 	ep = &htc->endpoint[eid];
468 	if (ep->service_id == ATH10K_HTC_SVC_ID_UNUSED) {
469 		ath10k_warn(ar, "htc rx endpoint %d is not connected\n", eid);
470 		goto out;
471 	}
472 
473 	payload_len = __le16_to_cpu(hdr->len);
474 
475 	if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
476 		ath10k_warn(ar, "HTC rx frame too long, len: %zu\n",
477 			    payload_len + sizeof(*hdr));
478 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len", "",
479 				hdr, sizeof(*hdr));
480 		goto out;
481 	}
482 
483 	if (skb->len < payload_len) {
484 		ath10k_dbg(ar, ATH10K_DBG_HTC,
485 			   "HTC Rx: insufficient length, got %d, expected %d\n",
486 			   skb->len, payload_len);
487 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len",
488 				"", hdr, sizeof(*hdr));
489 		goto out;
490 	}
491 
492 	/* get flags to check for trailer */
493 	trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
494 	if (trailer_present) {
495 		u8 *trailer;
496 
497 		trailer_len = hdr->trailer_len;
498 		min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
499 
500 		if ((trailer_len < min_len) ||
501 		    (trailer_len > payload_len)) {
502 			ath10k_warn(ar, "Invalid trailer length: %d\n",
503 				    trailer_len);
504 			goto out;
505 		}
506 
507 		trailer = (u8 *)hdr;
508 		trailer += sizeof(*hdr);
509 		trailer += payload_len;
510 		trailer -= trailer_len;
511 		status = ath10k_htc_process_trailer(htc, trailer,
512 						    trailer_len, hdr->eid,
513 						    NULL, NULL);
514 		if (status)
515 			goto out;
516 
517 		skb_trim(skb, skb->len - trailer_len);
518 	}
519 
520 	if (((int)payload_len - (int)trailer_len) <= 0)
521 		/* zero length packet with trailer data, just drop these */
522 		goto out;
523 
524 	ath10k_dbg(ar, ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",
525 		   eid, skb);
526 	ep->ep_ops.ep_rx_complete(ar, skb);
527 
528 	/* skb is now owned by the rx completion handler */
529 	skb = NULL;
530 out:
531 	kfree_skb(skb);
532 }
533 EXPORT_SYMBOL(ath10k_htc_rx_completion_handler);
534 
ath10k_htc_control_rx_complete(struct ath10k * ar,struct sk_buff * skb)535 static void ath10k_htc_control_rx_complete(struct ath10k *ar,
536 					   struct sk_buff *skb)
537 {
538 	struct ath10k_htc *htc = &ar->htc;
539 	struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
540 
541 	switch (__le16_to_cpu(msg->hdr.message_id)) {
542 	case ATH10K_HTC_MSG_READY_ID:
543 	case ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
544 		/* handle HTC control message */
545 		if (completion_done(&htc->ctl_resp)) {
546 			/* this is a fatal error, target should not be
547 			 * sending unsolicited messages on the ep 0
548 			 */
549 			ath10k_warn(ar, "HTC rx ctrl still processing\n");
550 			complete(&htc->ctl_resp);
551 			goto out;
552 		}
553 
554 		htc->control_resp_len =
555 			min_t(int, skb->len,
556 			      ATH10K_HTC_MAX_CTRL_MSG_LEN);
557 
558 		memcpy(htc->control_resp_buffer, skb->data,
559 		       htc->control_resp_len);
560 
561 		complete(&htc->ctl_resp);
562 		break;
563 	case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
564 		htc->htc_ops.target_send_suspend_complete(ar);
565 		break;
566 	default:
567 		ath10k_warn(ar, "ignoring unsolicited htc ep0 event\n");
568 		break;
569 	}
570 
571 out:
572 	kfree_skb(skb);
573 }
574 
575 /***************/
576 /* Init/Deinit */
577 /***************/
578 
htc_service_name(enum ath10k_htc_svc_id id)579 static const char *htc_service_name(enum ath10k_htc_svc_id id)
580 {
581 	switch (id) {
582 	case ATH10K_HTC_SVC_ID_RESERVED:
583 		return "Reserved";
584 	case ATH10K_HTC_SVC_ID_RSVD_CTRL:
585 		return "Control";
586 	case ATH10K_HTC_SVC_ID_WMI_CONTROL:
587 		return "WMI";
588 	case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
589 		return "DATA BE";
590 	case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
591 		return "DATA BK";
592 	case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
593 		return "DATA VI";
594 	case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
595 		return "DATA VO";
596 	case ATH10K_HTC_SVC_ID_NMI_CONTROL:
597 		return "NMI Control";
598 	case ATH10K_HTC_SVC_ID_NMI_DATA:
599 		return "NMI Data";
600 	case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
601 		return "HTT Data";
602 	case ATH10K_HTC_SVC_ID_HTT_DATA2_MSG:
603 		return "HTT Data";
604 	case ATH10K_HTC_SVC_ID_HTT_DATA3_MSG:
605 		return "HTT Data";
606 	case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
607 		return "RAW";
608 	case ATH10K_HTC_SVC_ID_HTT_LOG_MSG:
609 		return "PKTLOG";
610 	}
611 
612 	return "Unknown";
613 }
614 
ath10k_htc_reset_endpoint_states(struct ath10k_htc * htc)615 static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
616 {
617 	struct ath10k_htc_ep *ep;
618 	int i;
619 
620 	for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
621 		ep = &htc->endpoint[i];
622 		ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
623 		ep->max_ep_message_len = 0;
624 		ep->max_tx_queue_depth = 0;
625 		ep->eid = i;
626 		ep->htc = htc;
627 		ep->tx_credit_flow_enabled = true;
628 	}
629 }
630 
ath10k_htc_get_credit_allocation(struct ath10k_htc * htc,u16 service_id)631 static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
632 					   u16 service_id)
633 {
634 	u8 allocation = 0;
635 
636 	/* The WMI control service is the only service with flow control.
637 	 * Let it have all transmit credits.
638 	 */
639 	if (service_id == ATH10K_HTC_SVC_ID_WMI_CONTROL)
640 		allocation = htc->total_transmit_credits;
641 
642 	return allocation;
643 }
644 
ath10k_htc_send_bundle(struct ath10k_htc_ep * ep,struct sk_buff * bundle_skb,struct sk_buff_head * tx_save_head)645 static int ath10k_htc_send_bundle(struct ath10k_htc_ep *ep,
646 				  struct sk_buff *bundle_skb,
647 				  struct sk_buff_head *tx_save_head)
648 {
649 	struct ath10k_hif_sg_item sg_item;
650 	struct ath10k_htc *htc = ep->htc;
651 	struct ath10k *ar = htc->ar;
652 	struct sk_buff *skb;
653 	int ret, cn = 0;
654 	unsigned int skb_len;
655 
656 	ath10k_dbg(ar, ATH10K_DBG_HTC, "bundle skb len %d\n", bundle_skb->len);
657 	skb_len = bundle_skb->len;
658 	ret = ath10k_htc_consume_credit(ep, skb_len, true);
659 
660 	if (!ret) {
661 		sg_item.transfer_id = ep->eid;
662 		sg_item.transfer_context = bundle_skb;
663 		sg_item.vaddr = bundle_skb->data;
664 		sg_item.len = bundle_skb->len;
665 
666 		ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);
667 		if (ret)
668 			ath10k_htc_release_credit(ep, skb_len);
669 	}
670 
671 	if (ret)
672 		dev_kfree_skb_any(bundle_skb);
673 
674 	for (cn = 0; (skb = skb_dequeue_tail(tx_save_head)); cn++) {
675 		if (ret) {
676 			skb_pull(skb, sizeof(struct ath10k_htc_hdr));
677 			skb_queue_head(&ep->tx_req_head, skb);
678 		} else {
679 			skb_queue_tail(&ep->tx_complete_head, skb);
680 		}
681 	}
682 
683 	if (!ret)
684 		queue_work(ar->workqueue_tx_complete, &ar->tx_complete_work);
685 
686 	ath10k_dbg(ar, ATH10K_DBG_HTC,
687 		   "bundle tx status %d eid %d req count %d count %d len %d\n",
688 		   ret, ep->eid, skb_queue_len(&ep->tx_req_head), cn, skb_len);
689 	return ret;
690 }
691 
ath10k_htc_send_one_skb(struct ath10k_htc_ep * ep,struct sk_buff * skb)692 static void ath10k_htc_send_one_skb(struct ath10k_htc_ep *ep, struct sk_buff *skb)
693 {
694 	struct ath10k_htc *htc = ep->htc;
695 	struct ath10k *ar = htc->ar;
696 	int ret;
697 
698 	ret = ath10k_htc_send(htc, ep->eid, skb);
699 
700 	if (ret)
701 		skb_queue_head(&ep->tx_req_head, skb);
702 
703 	ath10k_dbg(ar, ATH10K_DBG_HTC, "tx one status %d eid %d len %d pending count %d\n",
704 		   ret, ep->eid, skb->len, skb_queue_len(&ep->tx_req_head));
705 }
706 
ath10k_htc_send_bundle_skbs(struct ath10k_htc_ep * ep)707 static int ath10k_htc_send_bundle_skbs(struct ath10k_htc_ep *ep)
708 {
709 	struct ath10k_htc *htc = ep->htc;
710 	struct sk_buff *bundle_skb, *skb;
711 	struct sk_buff_head tx_save_head;
712 	struct ath10k_htc_hdr *hdr;
713 	u8 *bundle_buf;
714 	int ret = 0, credit_pad, credit_remainder, trans_len, bundles_left = 0;
715 
716 	if (htc->ar->state == ATH10K_STATE_WEDGED)
717 		return -ECOMM;
718 
719 	if (ep->tx_credit_flow_enabled &&
720 	    ep->tx_credits < ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE)
721 		return 0;
722 
723 	bundles_left = ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE * ep->tx_credit_size;
724 	bundle_skb = dev_alloc_skb(bundles_left);
725 
726 	if (!bundle_skb)
727 		return -ENOMEM;
728 
729 	bundle_buf = bundle_skb->data;
730 	skb_queue_head_init(&tx_save_head);
731 
732 	while (true) {
733 		skb = skb_dequeue(&ep->tx_req_head);
734 		if (!skb)
735 			break;
736 
737 		credit_pad = 0;
738 		trans_len = skb->len + sizeof(*hdr);
739 		credit_remainder = trans_len % ep->tx_credit_size;
740 
741 		if (credit_remainder != 0) {
742 			credit_pad = ep->tx_credit_size - credit_remainder;
743 			trans_len += credit_pad;
744 		}
745 
746 		ret = ath10k_htc_consume_credit(ep,
747 						bundle_buf + trans_len - bundle_skb->data,
748 						false);
749 		if (ret) {
750 			skb_queue_head(&ep->tx_req_head, skb);
751 			break;
752 		}
753 
754 		if (bundles_left < trans_len) {
755 			bundle_skb->len = bundle_buf - bundle_skb->data;
756 			ret = ath10k_htc_send_bundle(ep, bundle_skb, &tx_save_head);
757 
758 			if (ret) {
759 				skb_queue_head(&ep->tx_req_head, skb);
760 				return ret;
761 			}
762 
763 			if (skb_queue_len(&ep->tx_req_head) == 0) {
764 				ath10k_htc_send_one_skb(ep, skb);
765 				return ret;
766 			}
767 
768 			if (ep->tx_credit_flow_enabled &&
769 			    ep->tx_credits < ATH10K_MIN_CREDIT_PER_HTC_TX_BUNDLE) {
770 				skb_queue_head(&ep->tx_req_head, skb);
771 				return 0;
772 			}
773 
774 			bundles_left =
775 				ATH10K_MAX_MSG_PER_HTC_TX_BUNDLE * ep->tx_credit_size;
776 			bundle_skb = dev_alloc_skb(bundles_left);
777 
778 			if (!bundle_skb) {
779 				skb_queue_head(&ep->tx_req_head, skb);
780 				return -ENOMEM;
781 			}
782 			bundle_buf = bundle_skb->data;
783 			skb_queue_head_init(&tx_save_head);
784 		}
785 
786 		skb_push(skb, sizeof(struct ath10k_htc_hdr));
787 		ath10k_htc_prepare_tx_skb(ep, skb);
788 
789 		memcpy(bundle_buf, skb->data, skb->len);
790 		hdr = (struct ath10k_htc_hdr *)bundle_buf;
791 		hdr->flags |= ATH10K_HTC_FLAG_SEND_BUNDLE;
792 		hdr->pad_len = __cpu_to_le16(credit_pad);
793 		bundle_buf += trans_len;
794 		bundles_left -= trans_len;
795 		skb_queue_tail(&tx_save_head, skb);
796 	}
797 
798 	if (bundle_buf != bundle_skb->data) {
799 		bundle_skb->len = bundle_buf - bundle_skb->data;
800 		ret = ath10k_htc_send_bundle(ep, bundle_skb, &tx_save_head);
801 	} else {
802 		dev_kfree_skb_any(bundle_skb);
803 	}
804 
805 	return ret;
806 }
807 
ath10k_htc_bundle_tx_work(struct work_struct * work)808 static void ath10k_htc_bundle_tx_work(struct work_struct *work)
809 {
810 	struct ath10k *ar = container_of(work, struct ath10k, bundle_tx_work);
811 	struct ath10k_htc_ep *ep;
812 	struct sk_buff *skb;
813 	int i;
814 
815 	for (i = 0; i < ARRAY_SIZE(ar->htc.endpoint); i++) {
816 		ep = &ar->htc.endpoint[i];
817 
818 		if (!ep->bundle_tx)
819 			continue;
820 
821 		ath10k_dbg(ar, ATH10K_DBG_HTC, "bundle tx work eid %d count %d\n",
822 			   ep->eid, skb_queue_len(&ep->tx_req_head));
823 
824 		if (skb_queue_len(&ep->tx_req_head) >=
825 		    ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE) {
826 			ath10k_htc_send_bundle_skbs(ep);
827 		} else {
828 			skb = skb_dequeue(&ep->tx_req_head);
829 
830 			if (!skb)
831 				continue;
832 			ath10k_htc_send_one_skb(ep, skb);
833 		}
834 	}
835 }
836 
ath10k_htc_tx_complete_work(struct work_struct * work)837 static void ath10k_htc_tx_complete_work(struct work_struct *work)
838 {
839 	struct ath10k *ar = container_of(work, struct ath10k, tx_complete_work);
840 	struct ath10k_htc_ep *ep;
841 	enum ath10k_htc_ep_id eid;
842 	struct sk_buff *skb;
843 	int i;
844 
845 	for (i = 0; i < ARRAY_SIZE(ar->htc.endpoint); i++) {
846 		ep = &ar->htc.endpoint[i];
847 		eid = ep->eid;
848 		if (ep->bundle_tx && eid == ar->htt.eid) {
849 			ath10k_dbg(ar, ATH10K_DBG_HTC, "bundle tx complete eid %d pending complete count%d\n",
850 				   ep->eid, skb_queue_len(&ep->tx_complete_head));
851 
852 			while (true) {
853 				skb = skb_dequeue(&ep->tx_complete_head);
854 				if (!skb)
855 					break;
856 				ath10k_htc_notify_tx_completion(ep, skb);
857 			}
858 		}
859 	}
860 }
861 
ath10k_htc_send_hl(struct ath10k_htc * htc,enum ath10k_htc_ep_id eid,struct sk_buff * skb)862 int ath10k_htc_send_hl(struct ath10k_htc *htc,
863 		       enum ath10k_htc_ep_id eid,
864 		       struct sk_buff *skb)
865 {
866 	struct ath10k_htc_ep *ep = &htc->endpoint[eid];
867 	struct ath10k *ar = htc->ar;
868 
869 	if (sizeof(struct ath10k_htc_hdr) + skb->len > ep->tx_credit_size) {
870 		ath10k_dbg(ar, ATH10K_DBG_HTC, "tx exceed max len %d\n", skb->len);
871 		return -ENOMEM;
872 	}
873 
874 	ath10k_dbg(ar, ATH10K_DBG_HTC, "htc send hl eid %d bundle %d tx count %d len %d\n",
875 		   eid, ep->bundle_tx, skb_queue_len(&ep->tx_req_head), skb->len);
876 
877 	if (ep->bundle_tx) {
878 		skb_queue_tail(&ep->tx_req_head, skb);
879 		queue_work(ar->workqueue, &ar->bundle_tx_work);
880 		return 0;
881 	} else {
882 		return ath10k_htc_send(htc, eid, skb);
883 	}
884 }
885 
ath10k_htc_setup_tx_req(struct ath10k_htc_ep * ep)886 void ath10k_htc_setup_tx_req(struct ath10k_htc_ep *ep)
887 {
888 	if (ep->htc->max_msgs_per_htc_bundle >= ATH10K_MIN_MSG_PER_HTC_TX_BUNDLE &&
889 	    !ep->bundle_tx) {
890 		ep->bundle_tx = true;
891 		skb_queue_head_init(&ep->tx_req_head);
892 		skb_queue_head_init(&ep->tx_complete_head);
893 	}
894 }
895 
ath10k_htc_stop_hl(struct ath10k * ar)896 void ath10k_htc_stop_hl(struct ath10k *ar)
897 {
898 	struct ath10k_htc_ep *ep;
899 	int i;
900 
901 	cancel_work_sync(&ar->bundle_tx_work);
902 	cancel_work_sync(&ar->tx_complete_work);
903 
904 	for (i = 0; i < ARRAY_SIZE(ar->htc.endpoint); i++) {
905 		ep = &ar->htc.endpoint[i];
906 
907 		if (!ep->bundle_tx)
908 			continue;
909 
910 		ath10k_dbg(ar, ATH10K_DBG_HTC, "stop tx work eid %d count %d\n",
911 			   ep->eid, skb_queue_len(&ep->tx_req_head));
912 
913 		skb_queue_purge(&ep->tx_req_head);
914 	}
915 }
916 
ath10k_htc_wait_target(struct ath10k_htc * htc)917 int ath10k_htc_wait_target(struct ath10k_htc *htc)
918 {
919 	struct ath10k *ar = htc->ar;
920 	int i, status = 0;
921 	unsigned long time_left;
922 	struct ath10k_htc_msg *msg;
923 	u16 message_id;
924 
925 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
926 						ATH10K_HTC_WAIT_TIMEOUT_HZ);
927 	if (!time_left) {
928 		/* Workaround: In some cases the PCI HIF doesn't
929 		 * receive interrupt for the control response message
930 		 * even if the buffer was completed. It is suspected
931 		 * iomap writes unmasking PCI CE irqs aren't propagated
932 		 * properly in KVM PCI-passthrough sometimes.
933 		 */
934 		ath10k_warn(ar, "failed to receive control response completion, polling..\n");
935 
936 		for (i = 0; i < CE_COUNT; i++)
937 			ath10k_hif_send_complete_check(htc->ar, i, 1);
938 
939 		time_left =
940 		wait_for_completion_timeout(&htc->ctl_resp,
941 					    ATH10K_HTC_WAIT_TIMEOUT_HZ);
942 
943 		if (!time_left)
944 			status = -ETIMEDOUT;
945 	}
946 
947 	if (status < 0) {
948 		ath10k_err(ar, "ctl_resp never came in (%d)\n", status);
949 		return status;
950 	}
951 
952 	if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
953 		ath10k_err(ar, "Invalid HTC ready msg len:%d\n",
954 			   htc->control_resp_len);
955 		return -ECOMM;
956 	}
957 
958 	msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
959 	message_id   = __le16_to_cpu(msg->hdr.message_id);
960 
961 	if (message_id != ATH10K_HTC_MSG_READY_ID) {
962 		ath10k_err(ar, "Invalid HTC ready msg: 0x%x\n", message_id);
963 		return -ECOMM;
964 	}
965 
966 	if (ar->hw_params.use_fw_tx_credits)
967 		htc->total_transmit_credits = __le16_to_cpu(msg->ready.credit_count);
968 	else
969 		htc->total_transmit_credits = 1;
970 
971 	htc->target_credit_size = __le16_to_cpu(msg->ready.credit_size);
972 
973 	ath10k_dbg(ar, ATH10K_DBG_HTC,
974 		   "Target ready! transmit resources: %d size:%d actual credits:%d\n",
975 		   htc->total_transmit_credits,
976 		   htc->target_credit_size,
977 		   msg->ready.credit_count);
978 
979 	if ((htc->total_transmit_credits == 0) ||
980 	    (htc->target_credit_size == 0)) {
981 		ath10k_err(ar, "Invalid credit size received\n");
982 		return -ECOMM;
983 	}
984 
985 	/* The only way to determine if the ready message is an extended
986 	 * message is from the size.
987 	 */
988 	if (htc->control_resp_len >=
989 	    sizeof(msg->hdr) + sizeof(msg->ready_ext)) {
990 		htc->alt_data_credit_size =
991 			__le16_to_cpu(msg->ready_ext.reserved) &
992 			ATH10K_HTC_MSG_READY_EXT_ALT_DATA_MASK;
993 		htc->max_msgs_per_htc_bundle =
994 			min_t(u8, msg->ready_ext.max_msgs_per_htc_bundle,
995 			      HTC_HOST_MAX_MSG_PER_RX_BUNDLE);
996 		ath10k_dbg(ar, ATH10K_DBG_HTC,
997 			   "Extended ready message RX bundle size %d alt size %d\n",
998 			   htc->max_msgs_per_htc_bundle,
999 			   htc->alt_data_credit_size);
1000 	}
1001 
1002 	INIT_WORK(&ar->bundle_tx_work, ath10k_htc_bundle_tx_work);
1003 	INIT_WORK(&ar->tx_complete_work, ath10k_htc_tx_complete_work);
1004 
1005 	return 0;
1006 }
1007 
ath10k_htc_change_tx_credit_flow(struct ath10k_htc * htc,enum ath10k_htc_ep_id eid,bool enable)1008 void ath10k_htc_change_tx_credit_flow(struct ath10k_htc *htc,
1009 				      enum ath10k_htc_ep_id eid,
1010 				      bool enable)
1011 {
1012 	struct ath10k *ar = htc->ar;
1013 	struct ath10k_htc_ep *ep = &ar->htc.endpoint[eid];
1014 
1015 	ep->tx_credit_flow_enabled = enable;
1016 }
1017 
ath10k_htc_connect_service(struct ath10k_htc * htc,struct ath10k_htc_svc_conn_req * conn_req,struct ath10k_htc_svc_conn_resp * conn_resp)1018 int ath10k_htc_connect_service(struct ath10k_htc *htc,
1019 			       struct ath10k_htc_svc_conn_req *conn_req,
1020 			       struct ath10k_htc_svc_conn_resp *conn_resp)
1021 {
1022 	struct ath10k *ar = htc->ar;
1023 	struct ath10k_htc_msg *msg;
1024 	struct ath10k_htc_conn_svc *req_msg;
1025 	struct ath10k_htc_conn_svc_response resp_msg_dummy;
1026 	struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
1027 	enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
1028 	struct ath10k_htc_ep *ep;
1029 	struct sk_buff *skb;
1030 	unsigned int max_msg_size = 0;
1031 	int length, status;
1032 	unsigned long time_left;
1033 	bool disable_credit_flow_ctrl = false;
1034 	u16 message_id, service_id, flags = 0;
1035 	u8 tx_alloc = 0;
1036 
1037 	/* special case for HTC pseudo control service */
1038 	if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
1039 		disable_credit_flow_ctrl = true;
1040 		assigned_eid = ATH10K_HTC_EP_0;
1041 		max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
1042 		memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
1043 		goto setup;
1044 	}
1045 
1046 	tx_alloc = ath10k_htc_get_credit_allocation(htc,
1047 						    conn_req->service_id);
1048 	if (!tx_alloc)
1049 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
1050 			   "boot htc service %s does not allocate target credits\n",
1051 			   htc_service_name(conn_req->service_id));
1052 
1053 	skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
1054 	if (!skb) {
1055 		ath10k_err(ar, "Failed to allocate HTC packet\n");
1056 		return -ENOMEM;
1057 	}
1058 
1059 	length = sizeof(msg->hdr) + sizeof(msg->connect_service);
1060 	skb_put(skb, length);
1061 	memset(skb->data, 0, length);
1062 
1063 	msg = (struct ath10k_htc_msg *)skb->data;
1064 	msg->hdr.message_id =
1065 		__cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
1066 
1067 	flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
1068 
1069 	/* Only enable credit flow control for WMI ctrl service */
1070 	if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
1071 		flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
1072 		disable_credit_flow_ctrl = true;
1073 	}
1074 
1075 	req_msg = &msg->connect_service;
1076 	req_msg->flags = __cpu_to_le16(flags);
1077 	req_msg->service_id = __cpu_to_le16(conn_req->service_id);
1078 
1079 	reinit_completion(&htc->ctl_resp);
1080 
1081 	status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
1082 	if (status) {
1083 		kfree_skb(skb);
1084 		return status;
1085 	}
1086 
1087 	/* wait for response */
1088 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
1089 						ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
1090 	if (!time_left) {
1091 		ath10k_err(ar, "Service connect timeout\n");
1092 		return -ETIMEDOUT;
1093 	}
1094 
1095 	/* we controlled the buffer creation, it's aligned */
1096 	msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
1097 	resp_msg = &msg->connect_service_response;
1098 	message_id = __le16_to_cpu(msg->hdr.message_id);
1099 	service_id = __le16_to_cpu(resp_msg->service_id);
1100 
1101 	if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
1102 	    (htc->control_resp_len < sizeof(msg->hdr) +
1103 	     sizeof(msg->connect_service_response))) {
1104 		ath10k_err(ar, "Invalid resp message ID 0x%x", message_id);
1105 		return -EPROTO;
1106 	}
1107 
1108 	ath10k_dbg(ar, ATH10K_DBG_HTC,
1109 		   "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
1110 		   htc_service_name(service_id),
1111 		   resp_msg->status, resp_msg->eid);
1112 
1113 	conn_resp->connect_resp_code = resp_msg->status;
1114 
1115 	/* check response status */
1116 	if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
1117 		ath10k_err(ar, "HTC Service %s connect request failed: 0x%x)\n",
1118 			   htc_service_name(service_id),
1119 			   resp_msg->status);
1120 		return -EPROTO;
1121 	}
1122 
1123 	assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
1124 	max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
1125 
1126 setup:
1127 
1128 	if (assigned_eid >= ATH10K_HTC_EP_COUNT)
1129 		return -EPROTO;
1130 
1131 	if (max_msg_size == 0)
1132 		return -EPROTO;
1133 
1134 	ep = &htc->endpoint[assigned_eid];
1135 	ep->eid = assigned_eid;
1136 
1137 	if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
1138 		return -EPROTO;
1139 
1140 	/* return assigned endpoint to caller */
1141 	conn_resp->eid = assigned_eid;
1142 	conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
1143 
1144 	/* setup the endpoint */
1145 	ep->service_id = conn_req->service_id;
1146 	ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
1147 	ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
1148 	ep->tx_credits = tx_alloc;
1149 	ep->tx_credit_size = htc->target_credit_size;
1150 
1151 	if (conn_req->service_id == ATH10K_HTC_SVC_ID_HTT_DATA_MSG &&
1152 	    htc->alt_data_credit_size != 0)
1153 		ep->tx_credit_size = htc->alt_data_credit_size;
1154 
1155 	/* copy all the callbacks */
1156 	ep->ep_ops = conn_req->ep_ops;
1157 
1158 	status = ath10k_hif_map_service_to_pipe(htc->ar,
1159 						ep->service_id,
1160 						&ep->ul_pipe_id,
1161 						&ep->dl_pipe_id);
1162 	if (status) {
1163 		ath10k_dbg(ar, ATH10K_DBG_BOOT, "unsupported HTC service id: %d\n",
1164 			   ep->service_id);
1165 		return status;
1166 	}
1167 
1168 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1169 		   "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
1170 		   htc_service_name(ep->service_id), ep->ul_pipe_id,
1171 		   ep->dl_pipe_id, ep->eid);
1172 
1173 	if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
1174 		ep->tx_credit_flow_enabled = false;
1175 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
1176 			   "boot htc service '%s' eid %d TX flow control disabled\n",
1177 			   htc_service_name(ep->service_id), assigned_eid);
1178 	}
1179 
1180 	return status;
1181 }
1182 
ath10k_htc_alloc_skb(struct ath10k * ar,int size)1183 struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size)
1184 {
1185 	struct sk_buff *skb;
1186 
1187 	skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
1188 	if (!skb)
1189 		return NULL;
1190 
1191 	skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
1192 
1193 	/* FW/HTC requires 4-byte aligned streams */
1194 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
1195 		ath10k_warn(ar, "Unaligned HTC tx skb\n");
1196 
1197 	return skb;
1198 }
1199 
ath10k_htc_pktlog_process_rx(struct ath10k * ar,struct sk_buff * skb)1200 static void ath10k_htc_pktlog_process_rx(struct ath10k *ar, struct sk_buff *skb)
1201 {
1202 	trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
1203 	dev_kfree_skb_any(skb);
1204 }
1205 
ath10k_htc_pktlog_connect(struct ath10k * ar)1206 static int ath10k_htc_pktlog_connect(struct ath10k *ar)
1207 {
1208 	struct ath10k_htc_svc_conn_resp conn_resp;
1209 	struct ath10k_htc_svc_conn_req conn_req;
1210 	int status;
1211 
1212 	memset(&conn_req, 0, sizeof(conn_req));
1213 	memset(&conn_resp, 0, sizeof(conn_resp));
1214 
1215 	conn_req.ep_ops.ep_tx_complete = NULL;
1216 	conn_req.ep_ops.ep_rx_complete = ath10k_htc_pktlog_process_rx;
1217 	conn_req.ep_ops.ep_tx_credits = NULL;
1218 
1219 	/* connect to control service */
1220 	conn_req.service_id = ATH10K_HTC_SVC_ID_HTT_LOG_MSG;
1221 	status = ath10k_htc_connect_service(&ar->htc, &conn_req, &conn_resp);
1222 	if (status) {
1223 		ath10k_warn(ar, "failed to connect to PKTLOG service: %d\n",
1224 			    status);
1225 		return status;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
ath10k_htc_pktlog_svc_supported(struct ath10k * ar)1231 static bool ath10k_htc_pktlog_svc_supported(struct ath10k *ar)
1232 {
1233 	u8 ul_pipe_id;
1234 	u8 dl_pipe_id;
1235 	int status;
1236 
1237 	status = ath10k_hif_map_service_to_pipe(ar, ATH10K_HTC_SVC_ID_HTT_LOG_MSG,
1238 						&ul_pipe_id,
1239 						&dl_pipe_id);
1240 	if (status) {
1241 		ath10k_dbg(ar, ATH10K_DBG_BOOT, "unsupported HTC pktlog service id: %d\n",
1242 			   ATH10K_HTC_SVC_ID_HTT_LOG_MSG);
1243 
1244 		return false;
1245 	}
1246 
1247 	return true;
1248 }
1249 
ath10k_htc_start(struct ath10k_htc * htc)1250 int ath10k_htc_start(struct ath10k_htc *htc)
1251 {
1252 	struct ath10k *ar = htc->ar;
1253 	struct sk_buff *skb;
1254 	int status = 0;
1255 	struct ath10k_htc_msg *msg;
1256 
1257 	skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
1258 	if (!skb)
1259 		return -ENOMEM;
1260 
1261 	skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
1262 	memset(skb->data, 0, skb->len);
1263 
1264 	msg = (struct ath10k_htc_msg *)skb->data;
1265 	msg->hdr.message_id =
1266 		__cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
1267 
1268 	if (ar->hif.bus == ATH10K_BUS_SDIO) {
1269 		/* Extra setup params used by SDIO */
1270 		msg->setup_complete_ext.flags =
1271 			__cpu_to_le32(ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN);
1272 		msg->setup_complete_ext.max_msgs_per_bundled_recv =
1273 			htc->max_msgs_per_htc_bundle;
1274 	}
1275 	ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
1276 
1277 	status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
1278 	if (status) {
1279 		kfree_skb(skb);
1280 		return status;
1281 	}
1282 
1283 	if (ath10k_htc_pktlog_svc_supported(ar)) {
1284 		status = ath10k_htc_pktlog_connect(ar);
1285 		if (status) {
1286 			ath10k_err(ar, "failed to connect to pktlog: %d\n", status);
1287 			return status;
1288 		}
1289 	}
1290 
1291 	return 0;
1292 }
1293 
1294 /* registered target arrival callback from the HIF layer */
ath10k_htc_init(struct ath10k * ar)1295 int ath10k_htc_init(struct ath10k *ar)
1296 {
1297 	int status;
1298 	struct ath10k_htc *htc = &ar->htc;
1299 	struct ath10k_htc_svc_conn_req conn_req;
1300 	struct ath10k_htc_svc_conn_resp conn_resp;
1301 
1302 	spin_lock_init(&htc->tx_lock);
1303 
1304 	ath10k_htc_reset_endpoint_states(htc);
1305 
1306 	htc->ar = ar;
1307 
1308 	/* setup our pseudo HTC control endpoint connection */
1309 	memset(&conn_req, 0, sizeof(conn_req));
1310 	memset(&conn_resp, 0, sizeof(conn_resp));
1311 	conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
1312 	conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
1313 	conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
1314 	conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
1315 
1316 	/* connect fake service */
1317 	status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
1318 	if (status) {
1319 		ath10k_err(ar, "could not connect to htc service (%d)\n",
1320 			   status);
1321 		return status;
1322 	}
1323 
1324 	init_completion(&htc->ctl_resp);
1325 
1326 	return 0;
1327 }
1328