xref: /openbmc/linux/drivers/net/wireless/ath/ath12k/htc.c (revision 83146efc)
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 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 
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 
29 static void ath12k_htc_control_tx_complete(struct ath12k_base *ab,
30 					   struct sk_buff *skb)
31 {
32 	kfree_skb(skb);
33 }
34 
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 
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 
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
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 
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 
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 
247 void ath12k_htc_rx_completion_handler(struct ath12k_base *ab,
248 				      struct sk_buff *skb)
249 {
250 	int status = 0;
251 	struct ath12k_htc *htc = &ab->htc;
252 	struct ath12k_htc_hdr *hdr;
253 	struct ath12k_htc_ep *ep;
254 	u16 payload_len;
255 	u32 trailer_len = 0;
256 	size_t min_len;
257 	u8 eid;
258 	bool trailer_present;
259 
260 	hdr = (struct ath12k_htc_hdr *)skb->data;
261 	skb_pull(skb, sizeof(*hdr));
262 
263 	eid = le32_get_bits(hdr->htc_info, HTC_HDR_ENDPOINTID);
264 
265 	if (eid >= ATH12K_HTC_EP_COUNT) {
266 		ath12k_warn(ab, "HTC Rx: invalid eid %d\n", eid);
267 		goto out;
268 	}
269 
270 	ep = &htc->endpoint[eid];
271 
272 	payload_len = le32_get_bits(hdr->htc_info, HTC_HDR_PAYLOADLEN);
273 
274 	if (payload_len + sizeof(*hdr) > ATH12K_HTC_MAX_LEN) {
275 		ath12k_warn(ab, "HTC rx frame too long, len: %zu\n",
276 			    payload_len + sizeof(*hdr));
277 		goto out;
278 	}
279 
280 	if (skb->len < payload_len) {
281 		ath12k_warn(ab, "HTC Rx: insufficient length, got %d, expected %d\n",
282 			    skb->len, payload_len);
283 		goto out;
284 	}
285 
286 	/* get flags to check for trailer */
287 	trailer_present = le32_get_bits(hdr->htc_info, HTC_HDR_FLAGS) &
288 			  ATH12K_HTC_FLAG_TRAILER_PRESENT;
289 
290 	if (trailer_present) {
291 		u8 *trailer;
292 
293 		trailer_len = le32_get_bits(hdr->ctrl_info,
294 					    HTC_HDR_CONTROLBYTES0);
295 		min_len = sizeof(struct ath12k_htc_record_hdr);
296 
297 		if ((trailer_len < min_len) ||
298 		    (trailer_len > payload_len)) {
299 			ath12k_warn(ab, "Invalid trailer length: %d\n",
300 				    trailer_len);
301 			goto out;
302 		}
303 
304 		trailer = (u8 *)hdr;
305 		trailer += sizeof(*hdr);
306 		trailer += payload_len;
307 		trailer -= trailer_len;
308 		status = ath12k_htc_process_trailer(htc, trailer,
309 						    trailer_len, eid);
310 		if (status)
311 			goto out;
312 
313 		skb_trim(skb, skb->len - trailer_len);
314 	}
315 
316 	if (trailer_len >= payload_len)
317 		/* zero length packet with trailer data, just drop these */
318 		goto out;
319 
320 	if (eid == ATH12K_HTC_EP_0) {
321 		struct ath12k_htc_msg *msg = (struct ath12k_htc_msg *)skb->data;
322 
323 		switch (le32_get_bits(msg->msg_svc_id, HTC_MSG_MESSAGEID)) {
324 		case ATH12K_HTC_MSG_READY_ID:
325 		case ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
326 			/* handle HTC control message */
327 			if (completion_done(&htc->ctl_resp)) {
328 				/* this is a fatal error, target should not be
329 				 * sending unsolicited messages on the ep 0
330 				 */
331 				ath12k_warn(ab, "HTC rx ctrl still processing\n");
332 				complete(&htc->ctl_resp);
333 				goto out;
334 			}
335 
336 			htc->control_resp_len =
337 				min_t(int, skb->len,
338 				      ATH12K_HTC_MAX_CTRL_MSG_LEN);
339 
340 			memcpy(htc->control_resp_buffer, skb->data,
341 			       htc->control_resp_len);
342 
343 			complete(&htc->ctl_resp);
344 			break;
345 		case ATH12K_HTC_MSG_SEND_SUSPEND_COMPLETE:
346 			ath12k_htc_suspend_complete(ab, true);
347 			break;
348 		case ATH12K_HTC_MSG_NACK_SUSPEND:
349 			ath12k_htc_suspend_complete(ab, false);
350 			break;
351 		case ATH12K_HTC_MSG_WAKEUP_FROM_SUSPEND_ID:
352 			break;
353 		default:
354 			ath12k_warn(ab, "ignoring unsolicited htc ep0 event %u\n",
355 				    le32_get_bits(msg->msg_svc_id, HTC_MSG_MESSAGEID));
356 			break;
357 		}
358 		goto out;
359 	}
360 
361 	ath12k_dbg(ab, ATH12K_DBG_HTC, "htc rx completion ep %d skb %pK\n",
362 		   eid, skb);
363 	ep->ep_ops.ep_rx_complete(ab, skb);
364 
365 	/* poll tx completion for interrupt disabled CE's */
366 	ath12k_ce_poll_send_completed(ab, ep->ul_pipe_id);
367 
368 	/* skb is now owned by the rx completion handler */
369 	skb = NULL;
370 out:
371 	kfree_skb(skb);
372 }
373 
374 static void ath12k_htc_control_rx_complete(struct ath12k_base *ab,
375 					   struct sk_buff *skb)
376 {
377 	/* This is unexpected. FW is not supposed to send regular rx on this
378 	 * endpoint.
379 	 */
380 	ath12k_warn(ab, "unexpected htc rx\n");
381 	kfree_skb(skb);
382 }
383 
384 static const char *htc_service_name(enum ath12k_htc_svc_id id)
385 {
386 	switch (id) {
387 	case ATH12K_HTC_SVC_ID_RESERVED:
388 		return "Reserved";
389 	case ATH12K_HTC_SVC_ID_RSVD_CTRL:
390 		return "Control";
391 	case ATH12K_HTC_SVC_ID_WMI_CONTROL:
392 		return "WMI";
393 	case ATH12K_HTC_SVC_ID_WMI_DATA_BE:
394 		return "DATA BE";
395 	case ATH12K_HTC_SVC_ID_WMI_DATA_BK:
396 		return "DATA BK";
397 	case ATH12K_HTC_SVC_ID_WMI_DATA_VI:
398 		return "DATA VI";
399 	case ATH12K_HTC_SVC_ID_WMI_DATA_VO:
400 		return "DATA VO";
401 	case ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1:
402 		return "WMI MAC1";
403 	case ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2:
404 		return "WMI MAC2";
405 	case ATH12K_HTC_SVC_ID_NMI_CONTROL:
406 		return "NMI Control";
407 	case ATH12K_HTC_SVC_ID_NMI_DATA:
408 		return "NMI Data";
409 	case ATH12K_HTC_SVC_ID_HTT_DATA_MSG:
410 		return "HTT Data";
411 	case ATH12K_HTC_SVC_ID_TEST_RAW_STREAMS:
412 		return "RAW";
413 	case ATH12K_HTC_SVC_ID_IPA_TX:
414 		return "IPA TX";
415 	case ATH12K_HTC_SVC_ID_PKT_LOG:
416 		return "PKT LOG";
417 	case ATH12K_HTC_SVC_ID_WMI_CONTROL_DIAG:
418 		return "WMI DIAG";
419 	}
420 
421 	return "Unknown";
422 }
423 
424 static void ath12k_htc_reset_endpoint_states(struct ath12k_htc *htc)
425 {
426 	struct ath12k_htc_ep *ep;
427 	int i;
428 
429 	for (i = ATH12K_HTC_EP_0; i < ATH12K_HTC_EP_COUNT; i++) {
430 		ep = &htc->endpoint[i];
431 		ep->service_id = ATH12K_HTC_SVC_ID_UNUSED;
432 		ep->max_ep_message_len = 0;
433 		ep->max_tx_queue_depth = 0;
434 		ep->eid = i;
435 		ep->htc = htc;
436 		ep->tx_credit_flow_enabled = true;
437 	}
438 }
439 
440 static u8 ath12k_htc_get_credit_allocation(struct ath12k_htc *htc,
441 					   u16 service_id)
442 {
443 	struct ath12k_htc_svc_tx_credits *serv_entry;
444 	u8 i, allocation = 0;
445 
446 	serv_entry = htc->service_alloc_table;
447 
448 	for (i = 0; i < ATH12K_HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
449 		if (serv_entry[i].service_id == service_id) {
450 			allocation = serv_entry[i].credit_allocation;
451 			break;
452 		}
453 	}
454 
455 	return allocation;
456 }
457 
458 static int ath12k_htc_setup_target_buffer_assignments(struct ath12k_htc *htc)
459 {
460 	struct ath12k_htc_svc_tx_credits *serv_entry;
461 	static const u32 svc_id[] = {
462 					ATH12K_HTC_SVC_ID_WMI_CONTROL,
463 					ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1,
464 					ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2,
465 				};
466 	int i, credits;
467 
468 	credits = htc->total_transmit_credits;
469 	serv_entry = htc->service_alloc_table;
470 
471 	if ((htc->wmi_ep_count == 0) ||
472 	    (htc->wmi_ep_count > ARRAY_SIZE(svc_id)))
473 		return -EINVAL;
474 
475 	/* Divide credits among number of endpoints for WMI */
476 	credits = credits / htc->wmi_ep_count;
477 	for (i = 0; i < htc->wmi_ep_count; i++) {
478 		serv_entry[i].service_id = svc_id[i];
479 		serv_entry[i].credit_allocation = credits;
480 	}
481 
482 	return 0;
483 }
484 
485 int ath12k_htc_wait_target(struct ath12k_htc *htc)
486 {
487 	int i, status = 0;
488 	struct ath12k_base *ab = htc->ab;
489 	unsigned long time_left;
490 	struct ath12k_htc_ready *ready;
491 	u16 message_id;
492 	u16 credit_count;
493 	u16 credit_size;
494 
495 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
496 						ATH12K_HTC_WAIT_TIMEOUT_HZ);
497 	if (!time_left) {
498 		ath12k_warn(ab, "failed to receive control response completion, polling..\n");
499 
500 		for (i = 0; i < ab->hw_params->ce_count; i++)
501 			ath12k_ce_per_engine_service(htc->ab, i);
502 
503 		time_left =
504 			wait_for_completion_timeout(&htc->ctl_resp,
505 						    ATH12K_HTC_WAIT_TIMEOUT_HZ);
506 
507 		if (!time_left)
508 			status = -ETIMEDOUT;
509 	}
510 
511 	if (status < 0) {
512 		ath12k_warn(ab, "ctl_resp never came in (%d)\n", status);
513 		return status;
514 	}
515 
516 	if (htc->control_resp_len < sizeof(*ready)) {
517 		ath12k_warn(ab, "Invalid HTC ready msg len:%d\n",
518 			    htc->control_resp_len);
519 		return -ECOMM;
520 	}
521 
522 	ready = (struct ath12k_htc_ready *)htc->control_resp_buffer;
523 	message_id = le32_get_bits(ready->id_credit_count, HTC_MSG_MESSAGEID);
524 	credit_count = le32_get_bits(ready->id_credit_count,
525 				     HTC_READY_MSG_CREDITCOUNT);
526 	credit_size = le32_get_bits(ready->size_ep, HTC_READY_MSG_CREDITSIZE);
527 
528 	if (message_id != ATH12K_HTC_MSG_READY_ID) {
529 		ath12k_warn(ab, "Invalid HTC ready msg: 0x%x\n", message_id);
530 		return -ECOMM;
531 	}
532 
533 	htc->total_transmit_credits = credit_count;
534 	htc->target_credit_size = credit_size;
535 
536 	ath12k_dbg(ab, ATH12K_DBG_HTC,
537 		   "Target ready! transmit resources: %d size:%d\n",
538 		   htc->total_transmit_credits, htc->target_credit_size);
539 
540 	if ((htc->total_transmit_credits == 0) ||
541 	    (htc->target_credit_size == 0)) {
542 		ath12k_warn(ab, "Invalid credit size received\n");
543 		return -ECOMM;
544 	}
545 
546 	ath12k_htc_setup_target_buffer_assignments(htc);
547 
548 	return 0;
549 }
550 
551 int ath12k_htc_connect_service(struct ath12k_htc *htc,
552 			       struct ath12k_htc_svc_conn_req *conn_req,
553 			       struct ath12k_htc_svc_conn_resp *conn_resp)
554 {
555 	struct ath12k_base *ab = htc->ab;
556 	struct ath12k_htc_conn_svc *req_msg;
557 	struct ath12k_htc_conn_svc_resp resp_msg_dummy;
558 	struct ath12k_htc_conn_svc_resp *resp_msg = &resp_msg_dummy;
559 	enum ath12k_htc_ep_id assigned_eid = ATH12K_HTC_EP_COUNT;
560 	struct ath12k_htc_ep *ep;
561 	struct sk_buff *skb;
562 	unsigned int max_msg_size = 0;
563 	int length, status;
564 	unsigned long time_left;
565 	bool disable_credit_flow_ctrl = false;
566 	u16 message_id, service_id, flags = 0;
567 	u8 tx_alloc = 0;
568 
569 	/* special case for HTC pseudo control service */
570 	if (conn_req->service_id == ATH12K_HTC_SVC_ID_RSVD_CTRL) {
571 		disable_credit_flow_ctrl = true;
572 		assigned_eid = ATH12K_HTC_EP_0;
573 		max_msg_size = ATH12K_HTC_MAX_CTRL_MSG_LEN;
574 		memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
575 		goto setup;
576 	}
577 
578 	tx_alloc = ath12k_htc_get_credit_allocation(htc,
579 						    conn_req->service_id);
580 	if (!tx_alloc)
581 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
582 			   "boot htc service %s does not allocate target credits\n",
583 			   htc_service_name(conn_req->service_id));
584 
585 	skb = ath12k_htc_build_tx_ctrl_skb();
586 	if (!skb) {
587 		ath12k_warn(ab, "Failed to allocate HTC packet\n");
588 		return -ENOMEM;
589 	}
590 
591 	length = sizeof(*req_msg);
592 	skb_put(skb, length);
593 	memset(skb->data, 0, length);
594 
595 	req_msg = (struct ath12k_htc_conn_svc *)skb->data;
596 	req_msg->msg_svc_id = le32_encode_bits(ATH12K_HTC_MSG_CONNECT_SERVICE_ID,
597 					       HTC_MSG_MESSAGEID);
598 
599 	flags |= u32_encode_bits(tx_alloc, ATH12K_HTC_CONN_FLAGS_RECV_ALLOC);
600 
601 	/* Only enable credit flow control for WMI ctrl service */
602 	if (!(conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL ||
603 	      conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC1 ||
604 	      conn_req->service_id == ATH12K_HTC_SVC_ID_WMI_CONTROL_MAC2)) {
605 		flags |= ATH12K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
606 		disable_credit_flow_ctrl = true;
607 	}
608 
609 	req_msg->flags_len = le32_encode_bits(flags, HTC_SVC_MSG_CONNECTIONFLAGS);
610 	req_msg->msg_svc_id |= le32_encode_bits(conn_req->service_id,
611 						HTC_SVC_MSG_SERVICE_ID);
612 
613 	reinit_completion(&htc->ctl_resp);
614 
615 	status = ath12k_htc_send(htc, ATH12K_HTC_EP_0, skb);
616 	if (status) {
617 		kfree_skb(skb);
618 		return status;
619 	}
620 
621 	/* wait for response */
622 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
623 						ATH12K_HTC_CONN_SVC_TIMEOUT_HZ);
624 	if (!time_left) {
625 		ath12k_err(ab, "Service connect timeout\n");
626 		return -ETIMEDOUT;
627 	}
628 
629 	/* we controlled the buffer creation, it's aligned */
630 	resp_msg = (struct ath12k_htc_conn_svc_resp *)htc->control_resp_buffer;
631 	message_id = le32_get_bits(resp_msg->msg_svc_id, HTC_MSG_MESSAGEID);
632 	service_id = le32_get_bits(resp_msg->msg_svc_id,
633 				   HTC_SVC_RESP_MSG_SERVICEID);
634 
635 	if ((message_id != ATH12K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
636 	    (htc->control_resp_len < sizeof(*resp_msg))) {
637 		ath12k_err(ab, "Invalid resp message ID 0x%x", message_id);
638 		return -EPROTO;
639 	}
640 
641 	ath12k_dbg(ab, ATH12K_DBG_HTC,
642 		   "HTC Service %s connect response: status: %u, assigned ep: %u\n",
643 		   htc_service_name(service_id),
644 		   le32_get_bits(resp_msg->flags_len, HTC_SVC_RESP_MSG_STATUS),
645 		   le32_get_bits(resp_msg->flags_len, HTC_SVC_RESP_MSG_ENDPOINTID));
646 
647 	conn_resp->connect_resp_code = le32_get_bits(resp_msg->flags_len,
648 						     HTC_SVC_RESP_MSG_STATUS);
649 
650 	/* check response status */
651 	if (conn_resp->connect_resp_code != ATH12K_HTC_CONN_SVC_STATUS_SUCCESS) {
652 		ath12k_err(ab, "HTC Service %s connect request failed: 0x%x)\n",
653 			   htc_service_name(service_id),
654 		       conn_resp->connect_resp_code);
655 		return -EPROTO;
656 	}
657 
658 	assigned_eid = le32_get_bits(resp_msg->flags_len,
659 				     HTC_SVC_RESP_MSG_ENDPOINTID);
660 
661 	max_msg_size = le32_get_bits(resp_msg->flags_len,
662 				     HTC_SVC_RESP_MSG_MAXMSGSIZE);
663 
664 setup:
665 
666 	if (assigned_eid >= ATH12K_HTC_EP_COUNT)
667 		return -EPROTO;
668 
669 	if (max_msg_size == 0)
670 		return -EPROTO;
671 
672 	ep = &htc->endpoint[assigned_eid];
673 	ep->eid = assigned_eid;
674 
675 	if (ep->service_id != ATH12K_HTC_SVC_ID_UNUSED)
676 		return -EPROTO;
677 
678 	/* return assigned endpoint to caller */
679 	conn_resp->eid = assigned_eid;
680 	conn_resp->max_msg_len = le32_get_bits(resp_msg->flags_len,
681 					       HTC_SVC_RESP_MSG_MAXMSGSIZE);
682 
683 	/* setup the endpoint */
684 	ep->service_id = conn_req->service_id;
685 	ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
686 	ep->max_ep_message_len = le32_get_bits(resp_msg->flags_len,
687 					       HTC_SVC_RESP_MSG_MAXMSGSIZE);
688 	ep->tx_credits = tx_alloc;
689 
690 	/* copy all the callbacks */
691 	ep->ep_ops = conn_req->ep_ops;
692 
693 	status = ath12k_hif_map_service_to_pipe(htc->ab,
694 						ep->service_id,
695 						&ep->ul_pipe_id,
696 						&ep->dl_pipe_id);
697 	if (status)
698 		return status;
699 
700 	ath12k_dbg(ab, ATH12K_DBG_BOOT,
701 		   "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
702 		   htc_service_name(ep->service_id), ep->ul_pipe_id,
703 		   ep->dl_pipe_id, ep->eid);
704 
705 	if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
706 		ep->tx_credit_flow_enabled = false;
707 		ath12k_dbg(ab, ATH12K_DBG_BOOT,
708 			   "boot htc service '%s' eid %d TX flow control disabled\n",
709 			   htc_service_name(ep->service_id), assigned_eid);
710 	}
711 
712 	return status;
713 }
714 
715 int ath12k_htc_start(struct ath12k_htc *htc)
716 {
717 	struct sk_buff *skb;
718 	int status;
719 	struct ath12k_base *ab = htc->ab;
720 	struct ath12k_htc_setup_complete_extended *msg;
721 
722 	skb = ath12k_htc_build_tx_ctrl_skb();
723 	if (!skb)
724 		return -ENOMEM;
725 
726 	skb_put(skb, sizeof(*msg));
727 	memset(skb->data, 0, skb->len);
728 
729 	msg = (struct ath12k_htc_setup_complete_extended *)skb->data;
730 	msg->msg_id = le32_encode_bits(ATH12K_HTC_MSG_SETUP_COMPLETE_EX_ID,
731 				       HTC_MSG_MESSAGEID);
732 
733 	ath12k_dbg(ab, ATH12K_DBG_HTC, "HTC is using TX credit flow control\n");
734 
735 	status = ath12k_htc_send(htc, ATH12K_HTC_EP_0, skb);
736 	if (status) {
737 		kfree_skb(skb);
738 		return status;
739 	}
740 
741 	return 0;
742 }
743 
744 int ath12k_htc_init(struct ath12k_base *ab)
745 {
746 	struct ath12k_htc *htc = &ab->htc;
747 	struct ath12k_htc_svc_conn_req conn_req = { };
748 	struct ath12k_htc_svc_conn_resp conn_resp = { };
749 	int ret;
750 
751 	spin_lock_init(&htc->tx_lock);
752 
753 	ath12k_htc_reset_endpoint_states(htc);
754 
755 	htc->ab = ab;
756 
757 	switch (ab->wmi_ab.preferred_hw_mode) {
758 	case WMI_HOST_HW_MODE_SINGLE:
759 		htc->wmi_ep_count = 1;
760 		break;
761 	case WMI_HOST_HW_MODE_DBS:
762 	case WMI_HOST_HW_MODE_DBS_OR_SBS:
763 		htc->wmi_ep_count = 2;
764 		break;
765 	case WMI_HOST_HW_MODE_DBS_SBS:
766 		htc->wmi_ep_count = 3;
767 		break;
768 	default:
769 		htc->wmi_ep_count = ab->hw_params->max_radios;
770 		break;
771 	}
772 
773 	/* setup our pseudo HTC control endpoint connection */
774 	conn_req.ep_ops.ep_tx_complete = ath12k_htc_control_tx_complete;
775 	conn_req.ep_ops.ep_rx_complete = ath12k_htc_control_rx_complete;
776 	conn_req.max_send_queue_depth = ATH12K_NUM_CONTROL_TX_BUFFERS;
777 	conn_req.service_id = ATH12K_HTC_SVC_ID_RSVD_CTRL;
778 
779 	/* connect fake service */
780 	ret = ath12k_htc_connect_service(htc, &conn_req, &conn_resp);
781 	if (ret) {
782 		ath12k_err(ab, "could not connect to htc service (%d)\n", ret);
783 		return ret;
784 	}
785 
786 	init_completion(&htc->ctl_resp);
787 
788 	return 0;
789 }
790