xref: /linux/drivers/net/wireless/ath/ath12k/htc.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2022, 2024 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 #include <linux/skbuff.h>
7 #include <linux/ctype.h>
8 
9 #include "debug.h"
10 #include "hif.h"
11 
ath12k_htc_alloc_skb(struct ath12k_base * ab,int size)12 struct sk_buff *ath12k_htc_alloc_skb(struct ath12k_base *ab, int size)
13 {
14 	struct sk_buff *skb;
15 
16 	skb = dev_alloc_skb(size + sizeof(struct ath12k_htc_hdr));
17 	if (!skb)
18 		return NULL;
19 
20 	skb_reserve(skb, sizeof(struct ath12k_htc_hdr));
21 
22 	/* FW/HTC requires 4-byte aligned streams */
23 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
24 		ath12k_warn(ab, "Unaligned HTC tx skb\n");
25 
26 	return skb;
27 }
28 
ath12k_htc_control_tx_complete(struct ath12k_base * ab,struct sk_buff * skb)29 static void ath12k_htc_control_tx_complete(struct ath12k_base *ab,
30 					   struct sk_buff *skb)
31 {
32 	kfree_skb(skb);
33 }
34 
ath12k_htc_build_tx_ctrl_skb(void)35 static struct sk_buff *ath12k_htc_build_tx_ctrl_skb(void)
36 {
37 	struct sk_buff *skb;
38 	struct ath12k_skb_cb *skb_cb;
39 
40 	skb = dev_alloc_skb(ATH12K_HTC_CONTROL_BUFFER_SIZE);
41 	if (!skb)
42 		return NULL;
43 
44 	skb_reserve(skb, sizeof(struct ath12k_htc_hdr));
45 	WARN_ON_ONCE(!IS_ALIGNED((unsigned long)skb->data, 4));
46 
47 	skb_cb = ATH12K_SKB_CB(skb);
48 	memset(skb_cb, 0, sizeof(*skb_cb));
49 
50 	return skb;
51 }
52 
ath12k_htc_prepare_tx_skb(struct ath12k_htc_ep * ep,struct sk_buff * skb)53 static void ath12k_htc_prepare_tx_skb(struct ath12k_htc_ep *ep,
54 				      struct sk_buff *skb)
55 {
56 	struct ath12k_htc_hdr *hdr;
57 
58 	hdr = (struct ath12k_htc_hdr *)skb->data;
59 
60 	memset(hdr, 0, sizeof(*hdr));
61 	hdr->htc_info = le32_encode_bits(ep->eid, HTC_HDR_ENDPOINTID) |
62 			le32_encode_bits((skb->len - sizeof(*hdr)),
63 					 HTC_HDR_PAYLOADLEN);
64 
65 	if (ep->tx_credit_flow_enabled)
66 		hdr->htc_info |= le32_encode_bits(ATH12K_HTC_FLAG_NEED_CREDIT_UPDATE,
67 						  HTC_HDR_FLAGS);
68 
69 	spin_lock_bh(&ep->htc->tx_lock);
70 	hdr->ctrl_info = le32_encode_bits(ep->seq_no++, HTC_HDR_CONTROLBYTES1);
71 	spin_unlock_bh(&ep->htc->tx_lock);
72 }
73 
ath12k_htc_send(struct ath12k_htc * htc,enum ath12k_htc_ep_id eid,struct sk_buff * skb)74 int ath12k_htc_send(struct ath12k_htc *htc,
75 		    enum ath12k_htc_ep_id eid,
76 		    struct sk_buff *skb)
77 {
78 	struct ath12k_htc_ep *ep = &htc->endpoint[eid];
79 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
80 	struct device *dev = htc->ab->dev;
81 	struct ath12k_base *ab = htc->ab;
82 	int credits = 0;
83 	int ret;
84 
85 	if (eid >= ATH12K_HTC_EP_COUNT) {
86 		ath12k_warn(ab, "Invalid endpoint id: %d\n", eid);
87 		return -ENOENT;
88 	}
89 
90 	skb_push(skb, sizeof(struct ath12k_htc_hdr));
91 
92 	if (ep->tx_credit_flow_enabled) {
93 		credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
94 		spin_lock_bh(&htc->tx_lock);
95 		if (ep->tx_credits < credits) {
96 			ath12k_dbg(ab, ATH12K_DBG_HTC,
97 				   "htc insufficient credits ep %d required %d available %d\n",
98 				   eid, credits, ep->tx_credits);
99 			spin_unlock_bh(&htc->tx_lock);
100 			ret = -EAGAIN;
101 			goto err_pull;
102 		}
103 		ep->tx_credits -= credits;
104 		ath12k_dbg(ab, ATH12K_DBG_HTC,
105 			   "htc ep %d consumed %d credits (total %d)\n",
106 			   eid, credits, ep->tx_credits);
107 		spin_unlock_bh(&htc->tx_lock);
108 	}
109 
110 	ath12k_htc_prepare_tx_skb(ep, skb);
111 
112 	skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
113 	ret = dma_mapping_error(dev, skb_cb->paddr);
114 	if (ret) {
115 		ret = -EIO;
116 		goto err_credits;
117 	}
118 
119 	ret = ath12k_ce_send(htc->ab, skb, ep->ul_pipe_id, ep->eid);
120 	if (ret)
121 		goto err_unmap;
122 
123 	return 0;
124 
125 err_unmap:
126 	dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
127 err_credits:
128 	if (ep->tx_credit_flow_enabled) {
129 		spin_lock_bh(&htc->tx_lock);
130 		ep->tx_credits += credits;
131 		ath12k_dbg(ab, ATH12K_DBG_HTC,
132 			   "htc ep %d reverted %d credits back (total %d)\n",
133 			   eid, credits, ep->tx_credits);
134 		spin_unlock_bh(&htc->tx_lock);
135 
136 		if (ep->ep_ops.ep_tx_credits)
137 			ep->ep_ops.ep_tx_credits(htc->ab);
138 	}
139 err_pull:
140 	skb_pull(skb, sizeof(struct ath12k_htc_hdr));
141 	return ret;
142 }
143 
144 static void
ath12k_htc_process_credit_report(struct ath12k_htc * htc,const struct ath12k_htc_credit_report * report,int len,enum ath12k_htc_ep_id eid)145 ath12k_htc_process_credit_report(struct ath12k_htc *htc,
146 				 const struct ath12k_htc_credit_report *report,
147 				 int len,
148 				 enum ath12k_htc_ep_id eid)
149 {
150 	struct ath12k_base *ab = htc->ab;
151 	struct ath12k_htc_ep *ep;
152 	int i, n_reports;
153 
154 	if (len % sizeof(*report))
155 		ath12k_warn(ab, "Uneven credit report len %d", len);
156 
157 	n_reports = len / sizeof(*report);
158 
159 	spin_lock_bh(&htc->tx_lock);
160 	for (i = 0; i < n_reports; i++, report++) {
161 		if (report->eid >= ATH12K_HTC_EP_COUNT)
162 			break;
163 
164 		ep = &htc->endpoint[report->eid];
165 		ep->tx_credits += report->credits;
166 
167 		ath12k_dbg(ab, ATH12K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
168 			   report->eid, report->credits, ep->tx_credits);
169 
170 		if (ep->ep_ops.ep_tx_credits) {
171 			spin_unlock_bh(&htc->tx_lock);
172 			ep->ep_ops.ep_tx_credits(htc->ab);
173 			spin_lock_bh(&htc->tx_lock);
174 		}
175 	}
176 	spin_unlock_bh(&htc->tx_lock);
177 }
178 
ath12k_htc_process_trailer(struct ath12k_htc * htc,u8 * buffer,int length,enum ath12k_htc_ep_id src_eid)179 static int ath12k_htc_process_trailer(struct ath12k_htc *htc,
180 				      u8 *buffer,
181 				      int length,
182 				      enum ath12k_htc_ep_id src_eid)
183 {
184 	struct ath12k_base *ab = htc->ab;
185 	int status = 0;
186 	struct ath12k_htc_record *record;
187 	size_t len;
188 
189 	while (length > 0) {
190 		record = (struct ath12k_htc_record *)buffer;
191 
192 		if (length < sizeof(record->hdr)) {
193 			status = -EINVAL;
194 			break;
195 		}
196 
197 		if (record->hdr.len > length) {
198 			/* no room left in buffer for record */
199 			ath12k_warn(ab, "Invalid record length: %d\n",
200 				    record->hdr.len);
201 			status = -EINVAL;
202 			break;
203 		}
204 
205 		switch (record->hdr.id) {
206 		case ATH12K_HTC_RECORD_CREDITS:
207 			len = sizeof(struct ath12k_htc_credit_report);
208 			if (record->hdr.len < len) {
209 				ath12k_warn(ab, "Credit report too long\n");
210 				status = -EINVAL;
211 				break;
212 			}
213 			ath12k_htc_process_credit_report(htc,
214 							 record->credit_report,
215 							 record->hdr.len,
216 							 src_eid);
217 			break;
218 		default:
219 			ath12k_warn(ab, "Unhandled record: id:%d length:%d\n",
220 				    record->hdr.id, record->hdr.len);
221 			break;
222 		}
223 
224 		if (status)
225 			break;
226 
227 		/* multiple records may be present in a trailer */
228 		buffer += sizeof(record->hdr) + record->hdr.len;
229 		length -= sizeof(record->hdr) + record->hdr.len;
230 	}
231 
232 	return status;
233 }
234 
ath12k_htc_suspend_complete(struct ath12k_base * ab,bool ack)235 static void ath12k_htc_suspend_complete(struct ath12k_base *ab, bool ack)
236 {
237 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot suspend complete %d\n", ack);
238 
239 	if (ack)
240 		set_bit(ATH12K_FLAG_HTC_SUSPEND_COMPLETE, &ab->dev_flags);
241 	else
242 		clear_bit(ATH12K_FLAG_HTC_SUSPEND_COMPLETE, &ab->dev_flags);
243 
244 	complete(&ab->htc_suspend);
245 }
246 
ath12k_htc_wakeup_from_suspend(struct ath12k_base * ab)247 static void ath12k_htc_wakeup_from_suspend(struct ath12k_base *ab)
248 {
249 	ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot wakeup from suspend is received\n");
250 }
251 
ath12k_htc_rx_completion_handler(struct ath12k_base * ab,struct sk_buff * skb)252 void ath12k_htc_rx_completion_handler(struct ath12k_base *ab,
253 				      struct sk_buff *skb)
254 {
255 	int status = 0;
256 	struct ath12k_htc *htc = &ab->htc;
257 	struct ath12k_htc_hdr *hdr;
258 	struct ath12k_htc_ep *ep;
259 	u16 payload_len;
260 	u32 trailer_len = 0;
261 	size_t min_len;
262 	u8 eid;
263 	bool trailer_present;
264 
265 	hdr = (struct ath12k_htc_hdr *)skb->data;
266 	skb_pull(skb, sizeof(*hdr));
267 
268 	eid = le32_get_bits(hdr->htc_info, HTC_HDR_ENDPOINTID);
269 
270 	if (eid >= ATH12K_HTC_EP_COUNT) {
271 		ath12k_warn(ab, "HTC Rx: invalid eid %d\n", eid);
272 		goto out;
273 	}
274 
275 	ep = &htc->endpoint[eid];
276 
277 	payload_len = le32_get_bits(hdr->htc_info, HTC_HDR_PAYLOADLEN);
278 
279 	if (payload_len + sizeof(*hdr) > ATH12K_HTC_MAX_LEN) {
280 		ath12k_warn(ab, "HTC rx frame too long, len: %zu\n",
281 			    payload_len + sizeof(*hdr));
282 		goto out;
283 	}
284 
285 	if (skb->len < payload_len) {
286 		ath12k_warn(ab, "HTC Rx: insufficient length, got %d, expected %d\n",
287 			    skb->len, payload_len);
288 		goto out;
289 	}
290 
291 	/* get flags to check for trailer */
292 	trailer_present = le32_get_bits(hdr->htc_info, HTC_HDR_FLAGS) &
293 			  ATH12K_HTC_FLAG_TRAILER_PRESENT;
294 
295 	if (trailer_present) {
296 		u8 *trailer;
297 
298 		trailer_len = le32_get_bits(hdr->ctrl_info,
299 					    HTC_HDR_CONTROLBYTES0);
300 		min_len = sizeof(struct ath12k_htc_record_hdr);
301 
302 		if ((trailer_len < min_len) ||
303 		    (trailer_len > payload_len)) {
304 			ath12k_warn(ab, "Invalid trailer length: %d\n",
305 				    trailer_len);
306 			goto out;
307 		}
308 
309 		trailer = (u8 *)hdr;
310 		trailer += sizeof(*hdr);
311 		trailer += payload_len;
312 		trailer -= trailer_len;
313 		status = ath12k_htc_process_trailer(htc, trailer,
314 						    trailer_len, eid);
315 		if (status)
316 			goto out;
317 
318 		skb_trim(skb, skb->len - trailer_len);
319 	}
320 
321 	if (trailer_len >= payload_len)
322 		/* zero length packet with trailer data, just drop these */
323 		goto out;
324 
325 	if (eid == ATH12K_HTC_EP_0) {
326 		struct ath12k_htc_msg *msg = (struct ath12k_htc_msg *)skb->data;
327 
328 		switch (le32_get_bits(msg->msg_svc_id, HTC_MSG_MESSAGEID)) {
329 		case ATH12K_HTC_MSG_READY_ID:
330 		case ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
331 			/* handle HTC control message */
332 			if (completion_done(&htc->ctl_resp)) {
333 				/* this is a fatal error, target should not be
334 				 * sending unsolicited messages on the ep 0
335 				 */
336 				ath12k_warn(ab, "HTC rx ctrl still processing\n");
337 				complete(&htc->ctl_resp);
338 				goto out;
339 			}
340 
341 			htc->control_resp_len =
342 				min_t(int, skb->len,
343 				      ATH12K_HTC_MAX_CTRL_MSG_LEN);
344 
345 			memcpy(htc->control_resp_buffer, skb->data,
346 			       htc->control_resp_len);
347 
348 			complete(&htc->ctl_resp);
349 			break;
350 		case ATH12K_HTC_MSG_SEND_SUSPEND_COMPLETE:
351 			ath12k_htc_suspend_complete(ab, true);
352 			break;
353 		case ATH12K_HTC_MSG_NACK_SUSPEND:
354 			ath12k_htc_suspend_complete(ab, false);
355 			break;
356 		case ATH12K_HTC_MSG_WAKEUP_FROM_SUSPEND_ID:
357 			ath12k_htc_wakeup_from_suspend(ab);
358 			break;
359 		default:
360 			ath12k_warn(ab, "ignoring unsolicited htc ep0 event %u\n",
361 				    le32_get_bits(msg->msg_svc_id, HTC_MSG_MESSAGEID));
362 			break;
363 		}
364 		goto out;
365 	}
366 
367 	ath12k_dbg(ab, ATH12K_DBG_HTC, "htc rx completion ep %d skb %p\n",
368 		   eid, skb);
369 	ep->ep_ops.ep_rx_complete(ab, skb);
370 
371 	/* poll tx completion for interrupt disabled CE's */
372 	ath12k_ce_poll_send_completed(ab, ep->ul_pipe_id);
373 
374 	/* skb is now owned by the rx completion handler */
375 	skb = NULL;
376 out:
377 	kfree_skb(skb);
378 }
379 
ath12k_htc_control_rx_complete(struct ath12k_base * ab,struct sk_buff * skb)380 static void ath12k_htc_control_rx_complete(struct ath12k_base *ab,
381 					   struct sk_buff *skb)
382 {
383 	/* This is unexpected. FW is not supposed to send regular rx on this
384 	 * endpoint.
385 	 */
386 	ath12k_warn(ab, "unexpected htc rx\n");
387 	kfree_skb(skb);
388 }
389 
htc_service_name(enum ath12k_htc_svc_id id)390 static const char *htc_service_name(enum ath12k_htc_svc_id id)
391 {
392 	switch (id) {
393 	case ATH12K_HTC_SVC_ID_RESERVED:
394 		return "Reserved";
395 	case ATH12K_HTC_SVC_ID_RSVD_CTRL:
396 		return "Control";
397 	case ATH12K_HTC_SVC_ID_WMI_CONTROL:
398 		return "WMI";
399 	case ATH12K_HTC_SVC_ID_WMI_DATA_BE:
400 		return "DATA BE";
401 	case ATH12K_HTC_SVC_ID_WMI_DATA_BK:
402 		return "DATA BK";
403 	case ATH12K_HTC_SVC_ID_WMI_DATA_VI:
404 		return "DATA VI";
405 	case ATH12K_HTC_SVC_ID_WMI_DATA_VO:
406 		return "DATA VO";
407 	case ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1:
408 		return "WMI MAC1";
409 	case ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2:
410 		return "WMI MAC2";
411 	case ATH12K_HTC_SVC_ID_NMI_CONTROL:
412 		return "NMI Control";
413 	case ATH12K_HTC_SVC_ID_NMI_DATA:
414 		return "NMI Data";
415 	case ATH12K_HTC_SVC_ID_HTT_DATA_MSG:
416 		return "HTT Data";
417 	case ATH12K_HTC_SVC_ID_TEST_RAW_STREAMS:
418 		return "RAW";
419 	case ATH12K_HTC_SVC_ID_IPA_TX:
420 		return "IPA TX";
421 	case ATH12K_HTC_SVC_ID_PKT_LOG:
422 		return "PKT LOG";
423 	case ATH12K_HTC_SVC_ID_WMI_CONTROL_DIAG:
424 		return "WMI DIAG";
425 	}
426 
427 	return "Unknown";
428 }
429 
ath12k_htc_reset_endpoint_states(struct ath12k_htc * htc)430 static void ath12k_htc_reset_endpoint_states(struct ath12k_htc *htc)
431 {
432 	struct ath12k_htc_ep *ep;
433 	int i;
434 
435 	for (i = ATH12K_HTC_EP_0; i < ATH12K_HTC_EP_COUNT; i++) {
436 		ep = &htc->endpoint[i];
437 		ep->service_id = ATH12K_HTC_SVC_ID_UNUSED;
438 		ep->max_ep_message_len = 0;
439 		ep->max_tx_queue_depth = 0;
440 		ep->eid = i;
441 		ep->htc = htc;
442 		ep->tx_credit_flow_enabled = true;
443 	}
444 }
445 
ath12k_htc_get_credit_allocation(struct ath12k_htc * htc,u16 service_id)446 static u8 ath12k_htc_get_credit_allocation(struct ath12k_htc *htc,
447 					   u16 service_id)
448 {
449 	struct ath12k_htc_svc_tx_credits *serv_entry;
450 	u8 i, allocation = 0;
451 
452 	serv_entry = htc->service_alloc_table;
453 
454 	for (i = 0; i < ATH12K_HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
455 		if (serv_entry[i].service_id == service_id) {
456 			allocation = serv_entry[i].credit_allocation;
457 			break;
458 		}
459 	}
460 
461 	return allocation;
462 }
463 
ath12k_htc_setup_target_buffer_assignments(struct ath12k_htc * htc)464 static int ath12k_htc_setup_target_buffer_assignments(struct ath12k_htc *htc)
465 {
466 	struct ath12k_htc_svc_tx_credits *serv_entry;
467 	static const u32 svc_id[] = {
468 					ATH12K_HTC_SVC_ID_WMI_CONTROL,
469 					ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1,
470 					ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2,
471 				};
472 	int i, credits;
473 
474 	credits = htc->total_transmit_credits;
475 	serv_entry = htc->service_alloc_table;
476 
477 	if ((htc->wmi_ep_count == 0) ||
478 	    (htc->wmi_ep_count > ARRAY_SIZE(svc_id)))
479 		return -EINVAL;
480 
481 	/* Divide credits among number of endpoints for WMI */
482 	credits = credits / htc->wmi_ep_count;
483 	for (i = 0; i < htc->wmi_ep_count; i++) {
484 		serv_entry[i].service_id = svc_id[i];
485 		serv_entry[i].credit_allocation = credits;
486 	}
487 
488 	return 0;
489 }
490 
ath12k_htc_wait_target(struct ath12k_htc * htc)491 int ath12k_htc_wait_target(struct ath12k_htc *htc)
492 {
493 	int i, status = 0;
494 	struct ath12k_base *ab = htc->ab;
495 	unsigned long time_left;
496 	struct ath12k_htc_ready *ready;
497 	u16 message_id;
498 	u16 credit_count;
499 	u16 credit_size;
500 
501 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
502 						ATH12K_HTC_WAIT_TIMEOUT_HZ);
503 	if (!time_left) {
504 		ath12k_warn(ab, "failed to receive control response completion, polling..\n");
505 
506 		for (i = 0; i < ab->hw_params->ce_count; i++)
507 			ath12k_ce_per_engine_service(htc->ab, i);
508 
509 		time_left =
510 			wait_for_completion_timeout(&htc->ctl_resp,
511 						    ATH12K_HTC_WAIT_TIMEOUT_HZ);
512 
513 		if (!time_left)
514 			status = -ETIMEDOUT;
515 	}
516 
517 	if (status < 0) {
518 		ath12k_warn(ab, "ctl_resp never came in (%d)\n", status);
519 		return status;
520 	}
521 
522 	if (htc->control_resp_len < sizeof(*ready)) {
523 		ath12k_warn(ab, "Invalid HTC ready msg len:%d\n",
524 			    htc->control_resp_len);
525 		return -ECOMM;
526 	}
527 
528 	ready = (struct ath12k_htc_ready *)htc->control_resp_buffer;
529 	message_id = le32_get_bits(ready->id_credit_count, HTC_MSG_MESSAGEID);
530 	credit_count = le32_get_bits(ready->id_credit_count,
531 				     HTC_READY_MSG_CREDITCOUNT);
532 	credit_size = le32_get_bits(ready->size_ep, HTC_READY_MSG_CREDITSIZE);
533 
534 	if (message_id != ATH12K_HTC_MSG_READY_ID) {
535 		ath12k_warn(ab, "Invalid HTC ready msg: 0x%x\n", message_id);
536 		return -ECOMM;
537 	}
538 
539 	htc->total_transmit_credits = credit_count;
540 	htc->target_credit_size = credit_size;
541 
542 	ath12k_dbg(ab, ATH12K_DBG_HTC,
543 		   "Target ready! transmit resources: %d size:%d\n",
544 		   htc->total_transmit_credits, htc->target_credit_size);
545 
546 	if ((htc->total_transmit_credits == 0) ||
547 	    (htc->target_credit_size == 0)) {
548 		ath12k_warn(ab, "Invalid credit size received\n");
549 		return -ECOMM;
550 	}
551 
552 	ath12k_htc_setup_target_buffer_assignments(htc);
553 
554 	return 0;
555 }
556 
ath12k_htc_connect_service(struct ath12k_htc * htc,struct ath12k_htc_svc_conn_req * conn_req,struct ath12k_htc_svc_conn_resp * conn_resp)557 int ath12k_htc_connect_service(struct ath12k_htc *htc,
558 			       struct ath12k_htc_svc_conn_req *conn_req,
559 			       struct ath12k_htc_svc_conn_resp *conn_resp)
560 {
561 	struct ath12k_base *ab = htc->ab;
562 	struct ath12k_htc_conn_svc *req_msg;
563 	struct ath12k_htc_conn_svc_resp resp_msg_dummy;
564 	struct ath12k_htc_conn_svc_resp *resp_msg = &resp_msg_dummy;
565 	enum ath12k_htc_ep_id assigned_eid = ATH12K_HTC_EP_COUNT;
566 	struct ath12k_htc_ep *ep;
567 	struct sk_buff *skb;
568 	unsigned int max_msg_size = 0;
569 	int length, status;
570 	unsigned long time_left;
571 	bool disable_credit_flow_ctrl = false;
572 	u16 message_id, service_id, flags = 0;
573 	u8 tx_alloc = 0;
574 
575 	/* special case for HTC pseudo control service */
576 	if (conn_req->service_id == ATH12K_HTC_SVC_ID_RSVD_CTRL) {
577 		disable_credit_flow_ctrl = true;
578 		assigned_eid = ATH12K_HTC_EP_0;
579 		max_msg_size = ATH12K_HTC_MAX_CTRL_MSG_LEN;
580 		memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
581 		goto setup;
582 	}
583 
584 	tx_alloc = ath12k_htc_get_credit_allocation(htc,
585 						    conn_req->service_id);
586 	if (!tx_alloc)
587 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
588 			   "boot htc service %s does not allocate target credits\n",
589 			   htc_service_name(conn_req->service_id));
590 
591 	skb = ath12k_htc_build_tx_ctrl_skb();
592 	if (!skb) {
593 		ath12k_warn(ab, "Failed to allocate HTC packet\n");
594 		return -ENOMEM;
595 	}
596 
597 	length = sizeof(*req_msg);
598 	skb_put(skb, length);
599 	memset(skb->data, 0, length);
600 
601 	req_msg = (struct ath12k_htc_conn_svc *)skb->data;
602 	req_msg->msg_svc_id = le32_encode_bits(ATH12K_HTC_MSG_CONNECT_SERVICE_ID,
603 					       HTC_MSG_MESSAGEID);
604 
605 	flags |= u32_encode_bits(tx_alloc, ATH12K_HTC_CONN_FLAGS_RECV_ALLOC);
606 
607 	/* Only enable credit flow control for WMI ctrl service */
608 	if (!(conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL ||
609 	      conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1 ||
610 	      conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2)) {
611 		flags |= ATH12K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
612 		disable_credit_flow_ctrl = true;
613 	}
614 
615 	req_msg->flags_len = le32_encode_bits(flags, HTC_SVC_MSG_CONNECTIONFLAGS);
616 	req_msg->msg_svc_id |= le32_encode_bits(conn_req->service_id,
617 						HTC_SVC_MSG_SERVICE_ID);
618 
619 	reinit_completion(&htc->ctl_resp);
620 
621 	status = ath12k_htc_send(htc, ATH12K_HTC_EP_0, skb);
622 	if (status) {
623 		kfree_skb(skb);
624 		return status;
625 	}
626 
627 	/* wait for response */
628 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
629 						ATH12K_HTC_CONN_SVC_TIMEOUT_HZ);
630 	if (!time_left) {
631 		ath12k_err(ab, "Service connect timeout\n");
632 		return -ETIMEDOUT;
633 	}
634 
635 	/* we controlled the buffer creation, it's aligned */
636 	resp_msg = (struct ath12k_htc_conn_svc_resp *)htc->control_resp_buffer;
637 	message_id = le32_get_bits(resp_msg->msg_svc_id, HTC_MSG_MESSAGEID);
638 	service_id = le32_get_bits(resp_msg->msg_svc_id,
639 				   HTC_SVC_RESP_MSG_SERVICEID);
640 
641 	if ((message_id != ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
642 	    (htc->control_resp_len < sizeof(*resp_msg))) {
643 		ath12k_err(ab, "Invalid resp message ID 0x%x", message_id);
644 		return -EPROTO;
645 	}
646 
647 	ath12k_dbg(ab, ATH12K_DBG_HTC,
648 		   "HTC Service %s connect response: status: %u, assigned ep: %u\n",
649 		   htc_service_name(service_id),
650 		   le32_get_bits(resp_msg->flags_len, HTC_SVC_RESP_MSG_STATUS),
651 		   le32_get_bits(resp_msg->flags_len, HTC_SVC_RESP_MSG_ENDPOINTID));
652 
653 	conn_resp->connect_resp_code = le32_get_bits(resp_msg->flags_len,
654 						     HTC_SVC_RESP_MSG_STATUS);
655 
656 	/* check response status */
657 	if (conn_resp->connect_resp_code != ATH12K_HTC_CONN_SVC_STATUS_SUCCESS) {
658 		ath12k_err(ab, "HTC Service %s connect request failed: 0x%x)\n",
659 			   htc_service_name(service_id),
660 		       conn_resp->connect_resp_code);
661 		return -EPROTO;
662 	}
663 
664 	assigned_eid = le32_get_bits(resp_msg->flags_len,
665 				     HTC_SVC_RESP_MSG_ENDPOINTID);
666 
667 	max_msg_size = le32_get_bits(resp_msg->flags_len,
668 				     HTC_SVC_RESP_MSG_MAXMSGSIZE);
669 
670 setup:
671 
672 	if (assigned_eid >= ATH12K_HTC_EP_COUNT)
673 		return -EPROTO;
674 
675 	if (max_msg_size == 0)
676 		return -EPROTO;
677 
678 	ep = &htc->endpoint[assigned_eid];
679 	ep->eid = assigned_eid;
680 
681 	if (ep->service_id != ATH12K_HTC_SVC_ID_UNUSED)
682 		return -EPROTO;
683 
684 	/* return assigned endpoint to caller */
685 	conn_resp->eid = assigned_eid;
686 	conn_resp->max_msg_len = le32_get_bits(resp_msg->flags_len,
687 					       HTC_SVC_RESP_MSG_MAXMSGSIZE);
688 
689 	/* setup the endpoint */
690 	ep->service_id = conn_req->service_id;
691 	ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
692 	ep->max_ep_message_len = le32_get_bits(resp_msg->flags_len,
693 					       HTC_SVC_RESP_MSG_MAXMSGSIZE);
694 	ep->tx_credits = tx_alloc;
695 
696 	/* copy all the callbacks */
697 	ep->ep_ops = conn_req->ep_ops;
698 
699 	status = ath12k_hif_map_service_to_pipe(htc->ab,
700 						ep->service_id,
701 						&ep->ul_pipe_id,
702 						&ep->dl_pipe_id);
703 	if (status)
704 		return status;
705 
706 	ath12k_dbg(ab, ATH12K_DBG_BOOT,
707 		   "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
708 		   htc_service_name(ep->service_id), ep->ul_pipe_id,
709 		   ep->dl_pipe_id, ep->eid);
710 
711 	if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
712 		ep->tx_credit_flow_enabled = false;
713 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
714 			   "boot htc service '%s' eid %d TX flow control disabled\n",
715 			   htc_service_name(ep->service_id), assigned_eid);
716 	}
717 
718 	return status;
719 }
720 
ath12k_htc_start(struct ath12k_htc * htc)721 int ath12k_htc_start(struct ath12k_htc *htc)
722 {
723 	struct sk_buff *skb;
724 	int status;
725 	struct ath12k_base *ab = htc->ab;
726 	struct ath12k_htc_setup_complete_extended *msg;
727 
728 	skb = ath12k_htc_build_tx_ctrl_skb();
729 	if (!skb)
730 		return -ENOMEM;
731 
732 	skb_put(skb, sizeof(*msg));
733 	memset(skb->data, 0, skb->len);
734 
735 	msg = (struct ath12k_htc_setup_complete_extended *)skb->data;
736 	msg->msg_id = le32_encode_bits(ATH12K_HTC_MSG_SETUP_COMPLETE_EX_ID,
737 				       HTC_MSG_MESSAGEID);
738 
739 	ath12k_dbg(ab, ATH12K_DBG_HTC, "HTC is using TX credit flow control\n");
740 
741 	status = ath12k_htc_send(htc, ATH12K_HTC_EP_0, skb);
742 	if (status) {
743 		kfree_skb(skb);
744 		return status;
745 	}
746 
747 	return 0;
748 }
749 
ath12k_htc_init(struct ath12k_base * ab)750 int ath12k_htc_init(struct ath12k_base *ab)
751 {
752 	struct ath12k_htc *htc = &ab->htc;
753 	struct ath12k_htc_svc_conn_req conn_req = { };
754 	struct ath12k_htc_svc_conn_resp conn_resp = { };
755 	int ret;
756 
757 	spin_lock_init(&htc->tx_lock);
758 
759 	ath12k_htc_reset_endpoint_states(htc);
760 
761 	htc->ab = ab;
762 
763 	switch (ab->wmi_ab.preferred_hw_mode) {
764 	case WMI_HOST_HW_MODE_SINGLE:
765 		htc->wmi_ep_count = 1;
766 		break;
767 	case WMI_HOST_HW_MODE_DBS:
768 	case WMI_HOST_HW_MODE_DBS_OR_SBS:
769 		htc->wmi_ep_count = 2;
770 		break;
771 	case WMI_HOST_HW_MODE_DBS_SBS:
772 		htc->wmi_ep_count = 3;
773 		break;
774 	default:
775 		htc->wmi_ep_count = ab->hw_params->max_radios;
776 		break;
777 	}
778 
779 	/* setup our pseudo HTC control endpoint connection */
780 	conn_req.ep_ops.ep_tx_complete = ath12k_htc_control_tx_complete;
781 	conn_req.ep_ops.ep_rx_complete = ath12k_htc_control_rx_complete;
782 	conn_req.max_send_queue_depth = ATH12K_NUM_CONTROL_TX_BUFFERS;
783 	conn_req.service_id = ATH12K_HTC_SVC_ID_RSVD_CTRL;
784 
785 	/* connect fake service */
786 	ret = ath12k_htc_connect_service(htc, &conn_req, &conn_resp);
787 	if (ret) {
788 		ath12k_err(ab, "could not connect to htc service (%d)\n", ret);
789 		return ret;
790 	}
791 
792 	init_completion(&htc->ctl_resp);
793 
794 	return 0;
795 }
796