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