• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ath11k_htc_alloc_skb(struct ath11k_base * ab,int size)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 
ath11k_htc_control_tx_complete(struct ath11k_base * ab,struct sk_buff * skb)28 static void ath11k_htc_control_tx_complete(struct ath11k_base *ab,
29 					   struct sk_buff *skb)
30 {
31 	kfree_skb(skb);
32 }
33 
ath11k_htc_build_tx_ctrl_skb(void * ab)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 
ath11k_htc_prepare_tx_skb(struct ath11k_htc_ep * ep,struct sk_buff * skb)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 
ath11k_htc_send(struct ath11k_htc * htc,enum ath11k_htc_ep_id eid,struct sk_buff * skb)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
ath11k_htc_process_credit_report(struct ath11k_htc * htc,const struct ath11k_htc_credit_report * report,int len,enum ath11k_htc_ep_id eid)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 
ath11k_htc_process_trailer(struct ath11k_htc * htc,u8 * buffer,int length,enum ath11k_htc_ep_id src_eid)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 
ath11k_htc_suspend_complete(struct ath11k_base * ab,bool ack)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 
ath11k_htc_tx_completion_handler(struct ath11k_base * ab,struct sk_buff * skb)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 		dev_kfree_skb_any(skb);
263 		return;
264 	}
265 
266 	ep = &htc->endpoint[eid];
267 	spin_lock_bh(&htc->tx_lock);
268 	ep_tx_complete = ep->ep_ops.ep_tx_complete;
269 	spin_unlock_bh(&htc->tx_lock);
270 	if (!ep_tx_complete) {
271 		dev_kfree_skb_any(skb);
272 		return;
273 	}
274 	ep_tx_complete(htc->ab, skb);
275 }
276 
ath11k_htc_wakeup_from_suspend(struct ath11k_base * ab)277 static void ath11k_htc_wakeup_from_suspend(struct ath11k_base *ab)
278 {
279 	ath11k_dbg(ab, ATH11K_DBG_BOOT, "boot wakeup from suspend is received\n");
280 }
281 
ath11k_htc_rx_completion_handler(struct ath11k_base * ab,struct sk_buff * skb)282 void ath11k_htc_rx_completion_handler(struct ath11k_base *ab,
283 				      struct sk_buff *skb)
284 {
285 	int status = 0;
286 	struct ath11k_htc *htc = &ab->htc;
287 	struct ath11k_htc_hdr *hdr;
288 	struct ath11k_htc_ep *ep;
289 	u16 payload_len;
290 	u32 trailer_len = 0;
291 	size_t min_len;
292 	u8 eid;
293 	bool trailer_present;
294 
295 	hdr = (struct ath11k_htc_hdr *)skb->data;
296 	skb_pull(skb, sizeof(*hdr));
297 
298 	eid = FIELD_GET(HTC_HDR_ENDPOINTID, hdr->htc_info);
299 
300 	if (eid >= ATH11K_HTC_EP_COUNT) {
301 		ath11k_warn(ab, "HTC Rx: invalid eid %d\n", eid);
302 		goto out;
303 	}
304 
305 	ep = &htc->endpoint[eid];
306 
307 	payload_len = FIELD_GET(HTC_HDR_PAYLOADLEN, hdr->htc_info);
308 
309 	if (payload_len + sizeof(*hdr) > ATH11K_HTC_MAX_LEN) {
310 		ath11k_warn(ab, "HTC rx frame too long, len: %zu\n",
311 			    payload_len + sizeof(*hdr));
312 		goto out;
313 	}
314 
315 	if (skb->len < payload_len) {
316 		ath11k_warn(ab, "HTC Rx: insufficient length, got %d, expected %d\n",
317 			    skb->len, payload_len);
318 		goto out;
319 	}
320 
321 	/* get flags to check for trailer */
322 	trailer_present = (FIELD_GET(HTC_HDR_FLAGS, hdr->htc_info)) &
323 			  ATH11K_HTC_FLAG_TRAILER_PRESENT;
324 
325 	if (trailer_present) {
326 		u8 *trailer;
327 
328 		trailer_len = FIELD_GET(HTC_HDR_CONTROLBYTES0, hdr->ctrl_info);
329 		min_len = sizeof(struct ath11k_htc_record_hdr);
330 
331 		if ((trailer_len < min_len) ||
332 		    (trailer_len > payload_len)) {
333 			ath11k_warn(ab, "Invalid trailer length: %d\n",
334 				    trailer_len);
335 			goto out;
336 		}
337 
338 		trailer = (u8 *)hdr;
339 		trailer += sizeof(*hdr);
340 		trailer += payload_len;
341 		trailer -= trailer_len;
342 		status = ath11k_htc_process_trailer(htc, trailer,
343 						    trailer_len, eid);
344 		if (status)
345 			goto out;
346 
347 		skb_trim(skb, skb->len - trailer_len);
348 	}
349 
350 	if (trailer_len >= payload_len)
351 		/* zero length packet with trailer data, just drop these */
352 		goto out;
353 
354 	if (eid == ATH11K_HTC_EP_0) {
355 		struct ath11k_htc_msg *msg = (struct ath11k_htc_msg *)skb->data;
356 
357 		switch (FIELD_GET(HTC_MSG_MESSAGEID, msg->msg_svc_id)) {
358 		case ATH11K_HTC_MSG_READY_ID:
359 		case ATH11K_HTC_MSG_CONNECT_SERVICE_RESP_ID:
360 			/* handle HTC control message */
361 			if (completion_done(&htc->ctl_resp)) {
362 				/* this is a fatal error, target should not be
363 				 * sending unsolicited messages on the ep 0
364 				 */
365 				ath11k_warn(ab, "HTC rx ctrl still processing\n");
366 				complete(&htc->ctl_resp);
367 				goto out;
368 			}
369 
370 			htc->control_resp_len =
371 				min_t(int, skb->len,
372 				      ATH11K_HTC_MAX_CTRL_MSG_LEN);
373 
374 			memcpy(htc->control_resp_buffer, skb->data,
375 			       htc->control_resp_len);
376 
377 			complete(&htc->ctl_resp);
378 			break;
379 		case ATH11K_HTC_MSG_SEND_SUSPEND_COMPLETE:
380 			ath11k_htc_suspend_complete(ab, true);
381 			break;
382 		case ATH11K_HTC_MSG_NACK_SUSPEND:
383 			ath11k_htc_suspend_complete(ab, false);
384 			break;
385 		case ATH11K_HTC_MSG_WAKEUP_FROM_SUSPEND_ID:
386 			ath11k_htc_wakeup_from_suspend(ab);
387 			break;
388 		default:
389 			ath11k_warn(ab, "ignoring unsolicited htc ep0 event %ld\n",
390 				    FIELD_GET(HTC_MSG_MESSAGEID, msg->msg_svc_id));
391 			break;
392 		}
393 		goto out;
394 	}
395 
396 	ath11k_dbg(ab, ATH11K_DBG_HTC, "htc rx completion ep %d skb %pK\n",
397 		   eid, skb);
398 	ep->ep_ops.ep_rx_complete(ab, skb);
399 
400 	/* poll tx completion for interrupt disabled CE's */
401 	ath11k_ce_poll_send_completed(ab, ep->ul_pipe_id);
402 
403 	/* skb is now owned by the rx completion handler */
404 	skb = NULL;
405 out:
406 	kfree_skb(skb);
407 }
408 
ath11k_htc_control_rx_complete(struct ath11k_base * ab,struct sk_buff * skb)409 static void ath11k_htc_control_rx_complete(struct ath11k_base *ab,
410 					   struct sk_buff *skb)
411 {
412 	/* This is unexpected. FW is not supposed to send regular rx on this
413 	 * endpoint.
414 	 */
415 	ath11k_warn(ab, "unexpected htc rx\n");
416 	kfree_skb(skb);
417 }
418 
htc_service_name(enum ath11k_htc_svc_id id)419 static const char *htc_service_name(enum ath11k_htc_svc_id id)
420 {
421 	switch (id) {
422 	case ATH11K_HTC_SVC_ID_RESERVED:
423 		return "Reserved";
424 	case ATH11K_HTC_SVC_ID_RSVD_CTRL:
425 		return "Control";
426 	case ATH11K_HTC_SVC_ID_WMI_CONTROL:
427 		return "WMI";
428 	case ATH11K_HTC_SVC_ID_WMI_DATA_BE:
429 		return "DATA BE";
430 	case ATH11K_HTC_SVC_ID_WMI_DATA_BK:
431 		return "DATA BK";
432 	case ATH11K_HTC_SVC_ID_WMI_DATA_VI:
433 		return "DATA VI";
434 	case ATH11K_HTC_SVC_ID_WMI_DATA_VO:
435 		return "DATA VO";
436 	case ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC1:
437 		return "WMI MAC1";
438 	case ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC2:
439 		return "WMI MAC2";
440 	case ATH11K_HTC_SVC_ID_NMI_CONTROL:
441 		return "NMI Control";
442 	case ATH11K_HTC_SVC_ID_NMI_DATA:
443 		return "NMI Data";
444 	case ATH11K_HTC_SVC_ID_HTT_DATA_MSG:
445 		return "HTT Data";
446 	case ATH11K_HTC_SVC_ID_TEST_RAW_STREAMS:
447 		return "RAW";
448 	case ATH11K_HTC_SVC_ID_IPA_TX:
449 		return "IPA TX";
450 	case ATH11K_HTC_SVC_ID_PKT_LOG:
451 		return "PKT LOG";
452 	}
453 
454 	return "Unknown";
455 }
456 
ath11k_htc_reset_endpoint_states(struct ath11k_htc * htc)457 static void ath11k_htc_reset_endpoint_states(struct ath11k_htc *htc)
458 {
459 	struct ath11k_htc_ep *ep;
460 	int i;
461 
462 	for (i = ATH11K_HTC_EP_0; i < ATH11K_HTC_EP_COUNT; i++) {
463 		ep = &htc->endpoint[i];
464 		ep->service_id = ATH11K_HTC_SVC_ID_UNUSED;
465 		ep->max_ep_message_len = 0;
466 		ep->max_tx_queue_depth = 0;
467 		ep->eid = i;
468 		ep->htc = htc;
469 		ep->tx_credit_flow_enabled = true;
470 	}
471 }
472 
ath11k_htc_get_credit_allocation(struct ath11k_htc * htc,u16 service_id)473 static u8 ath11k_htc_get_credit_allocation(struct ath11k_htc *htc,
474 					   u16 service_id)
475 {
476 	u8 i, allocation = 0;
477 
478 	for (i = 0; i < ATH11K_HTC_MAX_SERVICE_ALLOC_ENTRIES; i++) {
479 		if (htc->service_alloc_table[i].service_id == service_id) {
480 			allocation =
481 				htc->service_alloc_table[i].credit_allocation;
482 		}
483 	}
484 
485 	return allocation;
486 }
487 
ath11k_htc_setup_target_buffer_assignments(struct ath11k_htc * htc)488 static int ath11k_htc_setup_target_buffer_assignments(struct ath11k_htc *htc)
489 {
490 	struct ath11k_htc_svc_tx_credits *serv_entry;
491 	u32 svc_id[] = {
492 		ATH11K_HTC_SVC_ID_WMI_CONTROL,
493 		ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC1,
494 		ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC2,
495 	};
496 	int i, credits;
497 
498 	credits =  htc->total_transmit_credits;
499 	serv_entry = htc->service_alloc_table;
500 
501 	if ((htc->wmi_ep_count == 0) ||
502 	    (htc->wmi_ep_count > ARRAY_SIZE(svc_id)))
503 		return -EINVAL;
504 
505 	/* Divide credits among number of endpoints for WMI */
506 	credits = credits / htc->wmi_ep_count;
507 	for (i = 0; i < htc->wmi_ep_count; i++) {
508 		serv_entry[i].service_id = svc_id[i];
509 		serv_entry[i].credit_allocation = credits;
510 	}
511 
512 	return 0;
513 }
514 
ath11k_htc_wait_target(struct ath11k_htc * htc)515 int ath11k_htc_wait_target(struct ath11k_htc *htc)
516 {
517 	int i, status = 0;
518 	struct ath11k_base *ab = htc->ab;
519 	unsigned long time_left;
520 	struct ath11k_htc_ready *ready;
521 	u16 message_id;
522 	u16 credit_count;
523 	u16 credit_size;
524 
525 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
526 						ATH11K_HTC_WAIT_TIMEOUT_HZ);
527 	if (!time_left) {
528 		ath11k_warn(ab, "failed to receive control response completion, polling..\n");
529 
530 		for (i = 0; i < ab->hw_params.ce_count; i++)
531 			ath11k_ce_per_engine_service(htc->ab, i);
532 
533 		time_left =
534 			wait_for_completion_timeout(&htc->ctl_resp,
535 						    ATH11K_HTC_WAIT_TIMEOUT_HZ);
536 
537 		if (!time_left)
538 			status = -ETIMEDOUT;
539 	}
540 
541 	if (status < 0) {
542 		ath11k_warn(ab, "ctl_resp never came in (%d)\n", status);
543 		return status;
544 	}
545 
546 	if (htc->control_resp_len < sizeof(*ready)) {
547 		ath11k_warn(ab, "Invalid HTC ready msg len:%d\n",
548 			    htc->control_resp_len);
549 		return -ECOMM;
550 	}
551 
552 	ready = (struct ath11k_htc_ready *)htc->control_resp_buffer;
553 	message_id   = FIELD_GET(HTC_MSG_MESSAGEID, ready->id_credit_count);
554 	credit_count = FIELD_GET(HTC_READY_MSG_CREDITCOUNT,
555 				 ready->id_credit_count);
556 	credit_size  = FIELD_GET(HTC_READY_MSG_CREDITSIZE, ready->size_ep);
557 
558 	if (message_id != ATH11K_HTC_MSG_READY_ID) {
559 		ath11k_warn(ab, "Invalid HTC ready msg: 0x%x\n", message_id);
560 		return -ECOMM;
561 	}
562 
563 	htc->total_transmit_credits = credit_count;
564 	htc->target_credit_size = credit_size;
565 
566 	ath11k_dbg(ab, ATH11K_DBG_HTC,
567 		   "Target ready! transmit resources: %d size:%d\n",
568 		   htc->total_transmit_credits, htc->target_credit_size);
569 
570 	if ((htc->total_transmit_credits == 0) ||
571 	    (htc->target_credit_size == 0)) {
572 		ath11k_warn(ab, "Invalid credit size received\n");
573 		return -ECOMM;
574 	}
575 
576 	/* For QCA6390, wmi endpoint uses 1 credit to avoid
577 	 * back-to-back write.
578 	 */
579 	if (ab->hw_params.supports_shadow_regs)
580 		htc->total_transmit_credits = 1;
581 
582 	ath11k_htc_setup_target_buffer_assignments(htc);
583 
584 	return 0;
585 }
586 
ath11k_htc_connect_service(struct ath11k_htc * htc,struct ath11k_htc_svc_conn_req * conn_req,struct ath11k_htc_svc_conn_resp * conn_resp)587 int ath11k_htc_connect_service(struct ath11k_htc *htc,
588 			       struct ath11k_htc_svc_conn_req *conn_req,
589 			       struct ath11k_htc_svc_conn_resp *conn_resp)
590 {
591 	struct ath11k_base *ab = htc->ab;
592 	struct ath11k_htc_conn_svc *req_msg;
593 	struct ath11k_htc_conn_svc_resp resp_msg_dummy;
594 	struct ath11k_htc_conn_svc_resp *resp_msg = &resp_msg_dummy;
595 	enum ath11k_htc_ep_id assigned_eid = ATH11K_HTC_EP_COUNT;
596 	struct ath11k_htc_ep *ep;
597 	struct sk_buff *skb;
598 	unsigned int max_msg_size = 0;
599 	int length, status;
600 	unsigned long time_left;
601 	bool disable_credit_flow_ctrl = false;
602 	u16 message_id, service_id, flags = 0;
603 	u8 tx_alloc = 0;
604 
605 	/* special case for HTC pseudo control service */
606 	if (conn_req->service_id == ATH11K_HTC_SVC_ID_RSVD_CTRL) {
607 		disable_credit_flow_ctrl = true;
608 		assigned_eid = ATH11K_HTC_EP_0;
609 		max_msg_size = ATH11K_HTC_MAX_CTRL_MSG_LEN;
610 		memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
611 		goto setup;
612 	}
613 
614 	tx_alloc = ath11k_htc_get_credit_allocation(htc,
615 						    conn_req->service_id);
616 	if (!tx_alloc)
617 		ath11k_dbg(ab, ATH11K_DBG_BOOT,
618 			   "boot htc service %s does not allocate target credits\n",
619 			   htc_service_name(conn_req->service_id));
620 
621 	skb = ath11k_htc_build_tx_ctrl_skb(htc->ab);
622 	if (!skb) {
623 		ath11k_warn(ab, "Failed to allocate HTC packet\n");
624 		return -ENOMEM;
625 	}
626 
627 	length = sizeof(*req_msg);
628 	skb_put(skb, length);
629 	memset(skb->data, 0, length);
630 
631 	req_msg = (struct ath11k_htc_conn_svc *)skb->data;
632 	req_msg->msg_svc_id = FIELD_PREP(HTC_MSG_MESSAGEID,
633 					 ATH11K_HTC_MSG_CONNECT_SERVICE_ID);
634 
635 	flags |= FIELD_PREP(ATH11K_HTC_CONN_FLAGS_RECV_ALLOC, tx_alloc);
636 
637 	/* Only enable credit flow control for WMI ctrl service */
638 	if (!(conn_req->service_id == ATH11K_HTC_SVC_ID_WMI_CONTROL ||
639 	      conn_req->service_id == ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC1 ||
640 	      conn_req->service_id == ATH11K_HTC_SVC_ID_WMI_CONTROL_MAC2)) {
641 		flags |= ATH11K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
642 		disable_credit_flow_ctrl = true;
643 	}
644 
645 	if (!ab->hw_params.credit_flow) {
646 		flags |= ATH11K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
647 		disable_credit_flow_ctrl = true;
648 	}
649 
650 	req_msg->flags_len = FIELD_PREP(HTC_SVC_MSG_CONNECTIONFLAGS, flags);
651 	req_msg->msg_svc_id |= FIELD_PREP(HTC_SVC_MSG_SERVICE_ID,
652 					  conn_req->service_id);
653 
654 	reinit_completion(&htc->ctl_resp);
655 
656 	status = ath11k_htc_send(htc, ATH11K_HTC_EP_0, skb);
657 	if (status) {
658 		kfree_skb(skb);
659 		return status;
660 	}
661 
662 	/* wait for response */
663 	time_left = wait_for_completion_timeout(&htc->ctl_resp,
664 						ATH11K_HTC_CONN_SVC_TIMEOUT_HZ);
665 	if (!time_left) {
666 		ath11k_err(ab, "Service connect timeout\n");
667 		return -ETIMEDOUT;
668 	}
669 
670 	/* we controlled the buffer creation, it's aligned */
671 	resp_msg = (struct ath11k_htc_conn_svc_resp *)htc->control_resp_buffer;
672 	message_id = FIELD_GET(HTC_MSG_MESSAGEID, resp_msg->msg_svc_id);
673 	service_id = FIELD_GET(HTC_SVC_RESP_MSG_SERVICEID,
674 			       resp_msg->msg_svc_id);
675 
676 	if ((message_id != ATH11K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
677 	    (htc->control_resp_len < sizeof(*resp_msg))) {
678 		ath11k_err(ab, "Invalid resp message ID 0x%x", message_id);
679 		return -EPROTO;
680 	}
681 
682 	ath11k_dbg(ab, ATH11K_DBG_HTC,
683 		   "HTC Service %s connect response: status: 0x%lx, assigned ep: 0x%lx\n",
684 		   htc_service_name(service_id),
685 		   FIELD_GET(HTC_SVC_RESP_MSG_STATUS, resp_msg->flags_len),
686 		   FIELD_GET(HTC_SVC_RESP_MSG_ENDPOINTID, resp_msg->flags_len));
687 
688 	conn_resp->connect_resp_code = FIELD_GET(HTC_SVC_RESP_MSG_STATUS,
689 						 resp_msg->flags_len);
690 
691 	/* check response status */
692 	if (conn_resp->connect_resp_code != ATH11K_HTC_CONN_SVC_STATUS_SUCCESS) {
693 		ath11k_err(ab, "HTC Service %s connect request failed: 0x%x)\n",
694 			   htc_service_name(service_id),
695 		       conn_resp->connect_resp_code);
696 		return -EPROTO;
697 	}
698 
699 	assigned_eid = (enum ath11k_htc_ep_id)FIELD_GET(
700 						HTC_SVC_RESP_MSG_ENDPOINTID,
701 						resp_msg->flags_len);
702 
703 	max_msg_size = FIELD_GET(HTC_SVC_RESP_MSG_MAXMSGSIZE,
704 				 resp_msg->flags_len);
705 
706 setup:
707 
708 	if (assigned_eid >= ATH11K_HTC_EP_COUNT)
709 		return -EPROTO;
710 
711 	if (max_msg_size == 0)
712 		return -EPROTO;
713 
714 	ep = &htc->endpoint[assigned_eid];
715 	ep->eid = assigned_eid;
716 
717 	if (ep->service_id != ATH11K_HTC_SVC_ID_UNUSED)
718 		return -EPROTO;
719 
720 	/* return assigned endpoint to caller */
721 	conn_resp->eid = assigned_eid;
722 	conn_resp->max_msg_len = FIELD_GET(HTC_SVC_RESP_MSG_MAXMSGSIZE,
723 					   resp_msg->flags_len);
724 
725 	/* setup the endpoint */
726 	ep->service_id = conn_req->service_id;
727 	ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
728 	ep->max_ep_message_len = FIELD_GET(HTC_SVC_RESP_MSG_MAXMSGSIZE,
729 					   resp_msg->flags_len);
730 	ep->tx_credits = tx_alloc;
731 
732 	/* copy all the callbacks */
733 	ep->ep_ops = conn_req->ep_ops;
734 
735 	status = ath11k_hif_map_service_to_pipe(htc->ab,
736 						ep->service_id,
737 						&ep->ul_pipe_id,
738 						&ep->dl_pipe_id);
739 	if (status)
740 		return status;
741 
742 	ath11k_dbg(ab, ATH11K_DBG_BOOT,
743 		   "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
744 		   htc_service_name(ep->service_id), ep->ul_pipe_id,
745 		   ep->dl_pipe_id, ep->eid);
746 
747 	if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
748 		ep->tx_credit_flow_enabled = false;
749 		ath11k_dbg(ab, ATH11K_DBG_BOOT,
750 			   "boot htc service '%s' eid %d TX flow control disabled\n",
751 			   htc_service_name(ep->service_id), assigned_eid);
752 	}
753 
754 	return status;
755 }
756 
ath11k_htc_start(struct ath11k_htc * htc)757 int ath11k_htc_start(struct ath11k_htc *htc)
758 {
759 	struct sk_buff *skb;
760 	int status = 0;
761 	struct ath11k_base *ab = htc->ab;
762 	struct ath11k_htc_setup_complete_extended *msg;
763 
764 	skb = ath11k_htc_build_tx_ctrl_skb(htc->ab);
765 	if (!skb)
766 		return -ENOMEM;
767 
768 	skb_put(skb, sizeof(*msg));
769 	memset(skb->data, 0, skb->len);
770 
771 	msg = (struct ath11k_htc_setup_complete_extended *)skb->data;
772 	msg->msg_id = FIELD_PREP(HTC_MSG_MESSAGEID,
773 				 ATH11K_HTC_MSG_SETUP_COMPLETE_EX_ID);
774 
775 	if (ab->hw_params.credit_flow)
776 		ath11k_dbg(ab, ATH11K_DBG_HTC, "HTC is using TX credit flow control\n");
777 	else
778 		msg->flags |= ATH11K_GLOBAL_DISABLE_CREDIT_FLOW;
779 
780 	status = ath11k_htc_send(htc, ATH11K_HTC_EP_0, skb);
781 	if (status) {
782 		kfree_skb(skb);
783 		return status;
784 	}
785 
786 	return 0;
787 }
788 
ath11k_htc_init(struct ath11k_base * ab)789 int ath11k_htc_init(struct ath11k_base *ab)
790 {
791 	struct ath11k_htc *htc = &ab->htc;
792 	struct ath11k_htc_svc_conn_req conn_req;
793 	struct ath11k_htc_svc_conn_resp conn_resp;
794 	int ret;
795 
796 	spin_lock_init(&htc->tx_lock);
797 
798 	ath11k_htc_reset_endpoint_states(htc);
799 
800 	htc->ab = ab;
801 
802 	switch (ab->wmi_ab.preferred_hw_mode) {
803 	case WMI_HOST_HW_MODE_SINGLE:
804 		htc->wmi_ep_count = 1;
805 		break;
806 	case WMI_HOST_HW_MODE_DBS:
807 	case WMI_HOST_HW_MODE_DBS_OR_SBS:
808 		htc->wmi_ep_count = 2;
809 		break;
810 	case WMI_HOST_HW_MODE_DBS_SBS:
811 		htc->wmi_ep_count = 3;
812 		break;
813 	default:
814 		htc->wmi_ep_count = ab->hw_params.max_radios;
815 		break;
816 	}
817 
818 	/* setup our pseudo HTC control endpoint connection */
819 	memset(&conn_req, 0, sizeof(conn_req));
820 	memset(&conn_resp, 0, sizeof(conn_resp));
821 	conn_req.ep_ops.ep_tx_complete = ath11k_htc_control_tx_complete;
822 	conn_req.ep_ops.ep_rx_complete = ath11k_htc_control_rx_complete;
823 	conn_req.max_send_queue_depth = ATH11K_NUM_CONTROL_TX_BUFFERS;
824 	conn_req.service_id = ATH11K_HTC_SVC_ID_RSVD_CTRL;
825 
826 	/* connect fake service */
827 	ret = ath11k_htc_connect_service(htc, &conn_req, &conn_resp);
828 	if (ret) {
829 		ath11k_err(ab, "could not connect to htc service (%d)\n", ret);
830 		return ret;
831 	}
832 
833 	init_completion(&htc->ctl_resp);
834 
835 	return 0;
836 }
837