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