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