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