xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/htc.c (revision 95acd4c7)
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "core.h"
19 #include "hif.h"
20 #include "debug.h"
21 
22 /********/
23 /* Send */
24 /********/
25 
26 static inline void ath10k_htc_send_complete_check(struct ath10k_htc_ep *ep,
27 						  int force)
28 {
29 	/*
30 	 * Check whether HIF has any prior sends that have finished,
31 	 * have not had the post-processing done.
32 	 */
33 	ath10k_hif_send_complete_check(ep->htc->ar, ep->ul_pipe_id, force);
34 }
35 
36 static void ath10k_htc_control_tx_complete(struct ath10k *ar,
37 					   struct sk_buff *skb)
38 {
39 	kfree_skb(skb);
40 }
41 
42 static struct sk_buff *ath10k_htc_build_tx_ctrl_skb(void *ar)
43 {
44 	struct sk_buff *skb;
45 	struct ath10k_skb_cb *skb_cb;
46 
47 	skb = dev_alloc_skb(ATH10K_HTC_CONTROL_BUFFER_SIZE);
48 	if (!skb)
49 		return NULL;
50 
51 	skb_reserve(skb, 20); /* FIXME: why 20 bytes? */
52 	WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
53 
54 	skb_cb = ATH10K_SKB_CB(skb);
55 	memset(skb_cb, 0, sizeof(*skb_cb));
56 
57 	ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: skb %p\n", __func__, skb);
58 	return skb;
59 }
60 
61 static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
62 					     struct sk_buff *skb)
63 {
64 	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
65 
66 	dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
67 	skb_pull(skb, sizeof(struct ath10k_htc_hdr));
68 }
69 
70 static void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
71 					    struct sk_buff *skb)
72 {
73 	struct ath10k *ar = ep->htc->ar;
74 
75 	ath10k_dbg(ar, ATH10K_DBG_HTC, "%s: ep %d skb %p\n", __func__,
76 		   ep->eid, skb);
77 
78 	ath10k_htc_restore_tx_skb(ep->htc, skb);
79 
80 	if (!ep->ep_ops.ep_tx_complete) {
81 		ath10k_warn(ar, "no tx handler for eid %d\n", ep->eid);
82 		dev_kfree_skb_any(skb);
83 		return;
84 	}
85 
86 	ep->ep_ops.ep_tx_complete(ep->htc->ar, skb);
87 }
88 
89 /* assumes tx_lock is held */
90 static bool ath10k_htc_ep_need_credit_update(struct ath10k_htc_ep *ep)
91 {
92 	struct ath10k *ar = ep->htc->ar;
93 
94 	if (!ep->tx_credit_flow_enabled)
95 		return false;
96 	if (ep->tx_credits >= ep->tx_credits_per_max_message)
97 		return false;
98 
99 	ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC: endpoint %d needs credit update\n",
100 		   ep->eid);
101 	return true;
102 }
103 
104 static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
105 				      struct sk_buff *skb)
106 {
107 	struct ath10k_htc_hdr *hdr;
108 
109 	hdr = (struct ath10k_htc_hdr *)skb->data;
110 
111 	hdr->eid = ep->eid;
112 	hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
113 	hdr->flags = 0;
114 
115 	spin_lock_bh(&ep->htc->tx_lock);
116 	hdr->seq_no = ep->seq_no++;
117 
118 	if (ath10k_htc_ep_need_credit_update(ep))
119 		hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
120 
121 	spin_unlock_bh(&ep->htc->tx_lock);
122 }
123 
124 int ath10k_htc_send(struct ath10k_htc *htc,
125 		    enum ath10k_htc_ep_id eid,
126 		    struct sk_buff *skb)
127 {
128 	struct ath10k *ar = htc->ar;
129 	struct ath10k_htc_ep *ep = &htc->endpoint[eid];
130 	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
131 	struct ath10k_hif_sg_item sg_item;
132 	struct device *dev = htc->ar->dev;
133 	int credits = 0;
134 	int ret;
135 
136 	if (htc->ar->state == ATH10K_STATE_WEDGED)
137 		return -ECOMM;
138 
139 	if (eid >= ATH10K_HTC_EP_COUNT) {
140 		ath10k_warn(ar, "Invalid endpoint id: %d\n", eid);
141 		return -ENOENT;
142 	}
143 
144 	skb_push(skb, sizeof(struct ath10k_htc_hdr));
145 
146 	if (ep->tx_credit_flow_enabled) {
147 		credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
148 		spin_lock_bh(&htc->tx_lock);
149 		if (ep->tx_credits < credits) {
150 			spin_unlock_bh(&htc->tx_lock);
151 			ret = -EAGAIN;
152 			goto err_pull;
153 		}
154 		ep->tx_credits -= credits;
155 		ath10k_dbg(ar, ATH10K_DBG_HTC,
156 			   "htc ep %d consumed %d credits (total %d)\n",
157 			   eid, credits, ep->tx_credits);
158 		spin_unlock_bh(&htc->tx_lock);
159 	}
160 
161 	ath10k_htc_prepare_tx_skb(ep, skb);
162 
163 	skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
164 	ret = dma_mapping_error(dev, skb_cb->paddr);
165 	if (ret)
166 		goto err_credits;
167 
168 	sg_item.transfer_id = ep->eid;
169 	sg_item.transfer_context = skb;
170 	sg_item.vaddr = skb->data;
171 	sg_item.paddr = skb_cb->paddr;
172 	sg_item.len = skb->len;
173 
174 	ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);
175 	if (ret)
176 		goto err_unmap;
177 
178 	return 0;
179 
180 err_unmap:
181 	dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
182 err_credits:
183 	if (ep->tx_credit_flow_enabled) {
184 		spin_lock_bh(&htc->tx_lock);
185 		ep->tx_credits += credits;
186 		ath10k_dbg(ar, ATH10K_DBG_HTC,
187 			   "htc ep %d reverted %d credits back (total %d)\n",
188 			   eid, credits, ep->tx_credits);
189 		spin_unlock_bh(&htc->tx_lock);
190 
191 		if (ep->ep_ops.ep_tx_credits)
192 			ep->ep_ops.ep_tx_credits(htc->ar);
193 	}
194 err_pull:
195 	skb_pull(skb, sizeof(struct ath10k_htc_hdr));
196 	return ret;
197 }
198 
199 static int ath10k_htc_tx_completion_handler(struct ath10k *ar,
200 					    struct sk_buff *skb,
201 					    unsigned int eid)
202 {
203 	struct ath10k_htc *htc = &ar->htc;
204 	struct ath10k_htc_ep *ep = &htc->endpoint[eid];
205 
206 	if (WARN_ON_ONCE(!skb))
207 		return 0;
208 
209 	ath10k_htc_notify_tx_completion(ep, skb);
210 	/* the skb now belongs to the completion handler */
211 
212 	return 0;
213 }
214 
215 /***********/
216 /* Receive */
217 /***********/
218 
219 static void
220 ath10k_htc_process_credit_report(struct ath10k_htc *htc,
221 				 const struct ath10k_htc_credit_report *report,
222 				 int len,
223 				 enum ath10k_htc_ep_id eid)
224 {
225 	struct ath10k *ar = htc->ar;
226 	struct ath10k_htc_ep *ep;
227 	int i, n_reports;
228 
229 	if (len % sizeof(*report))
230 		ath10k_warn(ar, "Uneven credit report len %d", len);
231 
232 	n_reports = len / sizeof(*report);
233 
234 	spin_lock_bh(&htc->tx_lock);
235 	for (i = 0; i < n_reports; i++, report++) {
236 		if (report->eid >= ATH10K_HTC_EP_COUNT)
237 			break;
238 
239 		ep = &htc->endpoint[report->eid];
240 		ep->tx_credits += report->credits;
241 
242 		ath10k_dbg(ar, ATH10K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
243 			   report->eid, report->credits, ep->tx_credits);
244 
245 		if (ep->ep_ops.ep_tx_credits) {
246 			spin_unlock_bh(&htc->tx_lock);
247 			ep->ep_ops.ep_tx_credits(htc->ar);
248 			spin_lock_bh(&htc->tx_lock);
249 		}
250 	}
251 	spin_unlock_bh(&htc->tx_lock);
252 }
253 
254 static int ath10k_htc_process_trailer(struct ath10k_htc *htc,
255 				      u8 *buffer,
256 				      int length,
257 				      enum ath10k_htc_ep_id src_eid)
258 {
259 	struct ath10k *ar = htc->ar;
260 	int status = 0;
261 	struct ath10k_htc_record *record;
262 	u8 *orig_buffer;
263 	int orig_length;
264 	size_t len;
265 
266 	orig_buffer = buffer;
267 	orig_length = length;
268 
269 	while (length > 0) {
270 		record = (struct ath10k_htc_record *)buffer;
271 
272 		if (length < sizeof(record->hdr)) {
273 			status = -EINVAL;
274 			break;
275 		}
276 
277 		if (record->hdr.len > length) {
278 			/* no room left in buffer for record */
279 			ath10k_warn(ar, "Invalid record length: %d\n",
280 				    record->hdr.len);
281 			status = -EINVAL;
282 			break;
283 		}
284 
285 		switch (record->hdr.id) {
286 		case ATH10K_HTC_RECORD_CREDITS:
287 			len = sizeof(struct ath10k_htc_credit_report);
288 			if (record->hdr.len < len) {
289 				ath10k_warn(ar, "Credit report too long\n");
290 				status = -EINVAL;
291 				break;
292 			}
293 			ath10k_htc_process_credit_report(htc,
294 							 record->credit_report,
295 							 record->hdr.len,
296 							 src_eid);
297 			break;
298 		default:
299 			ath10k_warn(ar, "Unhandled record: id:%d length:%d\n",
300 				    record->hdr.id, record->hdr.len);
301 			break;
302 		}
303 
304 		if (status)
305 			break;
306 
307 		/* multiple records may be present in a trailer */
308 		buffer += sizeof(record->hdr) + record->hdr.len;
309 		length -= sizeof(record->hdr) + record->hdr.len;
310 	}
311 
312 	if (status)
313 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc rx bad trailer", "",
314 				orig_buffer, orig_length);
315 
316 	return status;
317 }
318 
319 static int ath10k_htc_rx_completion_handler(struct ath10k *ar,
320 					    struct sk_buff *skb,
321 					    u8 pipe_id)
322 {
323 	int status = 0;
324 	struct ath10k_htc *htc = &ar->htc;
325 	struct ath10k_htc_hdr *hdr;
326 	struct ath10k_htc_ep *ep;
327 	u16 payload_len;
328 	u32 trailer_len = 0;
329 	size_t min_len;
330 	u8 eid;
331 	bool trailer_present;
332 
333 	hdr = (struct ath10k_htc_hdr *)skb->data;
334 	skb_pull(skb, sizeof(*hdr));
335 
336 	eid = hdr->eid;
337 
338 	if (eid >= ATH10K_HTC_EP_COUNT) {
339 		ath10k_warn(ar, "HTC Rx: invalid eid %d\n", eid);
340 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad header", "",
341 				hdr, sizeof(*hdr));
342 		status = -EINVAL;
343 		goto out;
344 	}
345 
346 	ep = &htc->endpoint[eid];
347 
348 	/*
349 	 * If this endpoint that received a message from the target has
350 	 * a to-target HIF pipe whose send completions are polled rather
351 	 * than interrupt-driven, this is a good point to ask HIF to check
352 	 * whether it has any completed sends to handle.
353 	 */
354 	if (ep->ul_is_polled)
355 		ath10k_htc_send_complete_check(ep, 1);
356 
357 	payload_len = __le16_to_cpu(hdr->len);
358 
359 	if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
360 		ath10k_warn(ar, "HTC rx frame too long, len: %zu\n",
361 			    payload_len + sizeof(*hdr));
362 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len", "",
363 				hdr, sizeof(*hdr));
364 		status = -EINVAL;
365 		goto out;
366 	}
367 
368 	if (skb->len < payload_len) {
369 		ath10k_dbg(ar, ATH10K_DBG_HTC,
370 			   "HTC Rx: insufficient length, got %d, expected %d\n",
371 			   skb->len, payload_len);
372 		ath10k_dbg_dump(ar, ATH10K_DBG_HTC, "htc bad rx pkt len",
373 				"", hdr, sizeof(*hdr));
374 		status = -EINVAL;
375 		goto out;
376 	}
377 
378 	/* get flags to check for trailer */
379 	trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
380 	if (trailer_present) {
381 		u8 *trailer;
382 
383 		trailer_len = hdr->trailer_len;
384 		min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
385 
386 		if ((trailer_len < min_len) ||
387 		    (trailer_len > payload_len)) {
388 			ath10k_warn(ar, "Invalid trailer length: %d\n",
389 				    trailer_len);
390 			status = -EPROTO;
391 			goto out;
392 		}
393 
394 		trailer = (u8 *)hdr;
395 		trailer += sizeof(*hdr);
396 		trailer += payload_len;
397 		trailer -= trailer_len;
398 		status = ath10k_htc_process_trailer(htc, trailer,
399 						    trailer_len, hdr->eid);
400 		if (status)
401 			goto out;
402 
403 		skb_trim(skb, skb->len - trailer_len);
404 	}
405 
406 	if (((int)payload_len - (int)trailer_len) <= 0)
407 		/* zero length packet with trailer data, just drop these */
408 		goto out;
409 
410 	if (eid == ATH10K_HTC_EP_0) {
411 		struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
412 
413 		switch (__le16_to_cpu(msg->hdr.message_id)) {
414 		default:
415 			/* handle HTC control message */
416 			if (completion_done(&htc->ctl_resp)) {
417 				/*
418 				 * this is a fatal error, target should not be
419 				 * sending unsolicited messages on the ep 0
420 				 */
421 				ath10k_warn(ar, "HTC rx ctrl still processing\n");
422 				status = -EINVAL;
423 				complete(&htc->ctl_resp);
424 				goto out;
425 			}
426 
427 			htc->control_resp_len =
428 				min_t(int, skb->len,
429 				      ATH10K_HTC_MAX_CTRL_MSG_LEN);
430 
431 			memcpy(htc->control_resp_buffer, skb->data,
432 			       htc->control_resp_len);
433 
434 			complete(&htc->ctl_resp);
435 			break;
436 		case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
437 			htc->htc_ops.target_send_suspend_complete(ar);
438 		}
439 		goto out;
440 	}
441 
442 	ath10k_dbg(ar, ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",
443 		   eid, skb);
444 	ep->ep_ops.ep_rx_complete(ar, skb);
445 
446 	/* skb is now owned by the rx completion handler */
447 	skb = NULL;
448 out:
449 	kfree_skb(skb);
450 
451 	return status;
452 }
453 
454 static void ath10k_htc_control_rx_complete(struct ath10k *ar,
455 					   struct sk_buff *skb)
456 {
457 	/* This is unexpected. FW is not supposed to send regular rx on this
458 	 * endpoint. */
459 	ath10k_warn(ar, "unexpected htc rx\n");
460 	kfree_skb(skb);
461 }
462 
463 /***************/
464 /* Init/Deinit */
465 /***************/
466 
467 static const char *htc_service_name(enum ath10k_htc_svc_id id)
468 {
469 	switch (id) {
470 	case ATH10K_HTC_SVC_ID_RESERVED:
471 		return "Reserved";
472 	case ATH10K_HTC_SVC_ID_RSVD_CTRL:
473 		return "Control";
474 	case ATH10K_HTC_SVC_ID_WMI_CONTROL:
475 		return "WMI";
476 	case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
477 		return "DATA BE";
478 	case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
479 		return "DATA BK";
480 	case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
481 		return "DATA VI";
482 	case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
483 		return "DATA VO";
484 	case ATH10K_HTC_SVC_ID_NMI_CONTROL:
485 		return "NMI Control";
486 	case ATH10K_HTC_SVC_ID_NMI_DATA:
487 		return "NMI Data";
488 	case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
489 		return "HTT Data";
490 	case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
491 		return "RAW";
492 	}
493 
494 	return "Unknown";
495 }
496 
497 static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
498 {
499 	struct ath10k_htc_ep *ep;
500 	int i;
501 
502 	for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
503 		ep = &htc->endpoint[i];
504 		ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
505 		ep->max_ep_message_len = 0;
506 		ep->max_tx_queue_depth = 0;
507 		ep->eid = i;
508 		ep->htc = htc;
509 		ep->tx_credit_flow_enabled = true;
510 	}
511 }
512 
513 static void ath10k_htc_setup_target_buffer_assignments(struct ath10k_htc *htc)
514 {
515 	struct ath10k_htc_svc_tx_credits *entry;
516 
517 	entry = &htc->service_tx_alloc[0];
518 
519 	/*
520 	 * for PCIE allocate all credists/HTC buffers to WMI.
521 	 * no buffers are used/required for data. data always
522 	 * remains on host.
523 	 */
524 	entry++;
525 	entry->service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
526 	entry->credit_allocation = htc->total_transmit_credits;
527 }
528 
529 static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
530 					   u16 service_id)
531 {
532 	u8 allocation = 0;
533 	int i;
534 
535 	for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
536 		if (htc->service_tx_alloc[i].service_id == service_id)
537 			allocation =
538 			    htc->service_tx_alloc[i].credit_allocation;
539 	}
540 
541 	return allocation;
542 }
543 
544 int ath10k_htc_wait_target(struct ath10k_htc *htc)
545 {
546 	struct ath10k *ar = htc->ar;
547 	int i, status = 0;
548 	struct ath10k_htc_svc_conn_req conn_req;
549 	struct ath10k_htc_svc_conn_resp conn_resp;
550 	struct ath10k_htc_msg *msg;
551 	u16 message_id;
552 	u16 credit_count;
553 	u16 credit_size;
554 
555 	status = wait_for_completion_timeout(&htc->ctl_resp,
556 					     ATH10K_HTC_WAIT_TIMEOUT_HZ);
557 	if (status == 0) {
558 		/* Workaround: In some cases the PCI HIF doesn't
559 		 * receive interrupt for the control response message
560 		 * even if the buffer was completed. It is suspected
561 		 * iomap writes unmasking PCI CE irqs aren't propagated
562 		 * properly in KVM PCI-passthrough sometimes.
563 		 */
564 		ath10k_warn(ar, "failed to receive control response completion, polling..\n");
565 
566 		for (i = 0; i < CE_COUNT; i++)
567 			ath10k_hif_send_complete_check(htc->ar, i, 1);
568 
569 		status = wait_for_completion_timeout(&htc->ctl_resp,
570 						     ATH10K_HTC_WAIT_TIMEOUT_HZ);
571 
572 		if (status == 0)
573 			status = -ETIMEDOUT;
574 	}
575 
576 	if (status < 0) {
577 		ath10k_err(ar, "ctl_resp never came in (%d)\n", status);
578 		return status;
579 	}
580 
581 	if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
582 		ath10k_err(ar, "Invalid HTC ready msg len:%d\n",
583 			   htc->control_resp_len);
584 		return -ECOMM;
585 	}
586 
587 	msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
588 	message_id   = __le16_to_cpu(msg->hdr.message_id);
589 	credit_count = __le16_to_cpu(msg->ready.credit_count);
590 	credit_size  = __le16_to_cpu(msg->ready.credit_size);
591 
592 	if (message_id != ATH10K_HTC_MSG_READY_ID) {
593 		ath10k_err(ar, "Invalid HTC ready msg: 0x%x\n", message_id);
594 		return -ECOMM;
595 	}
596 
597 	htc->total_transmit_credits = credit_count;
598 	htc->target_credit_size = credit_size;
599 
600 	ath10k_dbg(ar, ATH10K_DBG_HTC,
601 		   "Target ready! transmit resources: %d size:%d\n",
602 		   htc->total_transmit_credits,
603 		   htc->target_credit_size);
604 
605 	if ((htc->total_transmit_credits == 0) ||
606 	    (htc->target_credit_size == 0)) {
607 		ath10k_err(ar, "Invalid credit size received\n");
608 		return -ECOMM;
609 	}
610 
611 	ath10k_htc_setup_target_buffer_assignments(htc);
612 
613 	/* setup our pseudo HTC control endpoint connection */
614 	memset(&conn_req, 0, sizeof(conn_req));
615 	memset(&conn_resp, 0, sizeof(conn_resp));
616 	conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
617 	conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
618 	conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
619 	conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
620 
621 	/* connect fake service */
622 	status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
623 	if (status) {
624 		ath10k_err(ar, "could not connect to htc service (%d)\n",
625 			   status);
626 		return status;
627 	}
628 
629 	return 0;
630 }
631 
632 int ath10k_htc_connect_service(struct ath10k_htc *htc,
633 			       struct ath10k_htc_svc_conn_req *conn_req,
634 			       struct ath10k_htc_svc_conn_resp *conn_resp)
635 {
636 	struct ath10k *ar = htc->ar;
637 	struct ath10k_htc_msg *msg;
638 	struct ath10k_htc_conn_svc *req_msg;
639 	struct ath10k_htc_conn_svc_response resp_msg_dummy;
640 	struct ath10k_htc_conn_svc_response *resp_msg = &resp_msg_dummy;
641 	enum ath10k_htc_ep_id assigned_eid = ATH10K_HTC_EP_COUNT;
642 	struct ath10k_htc_ep *ep;
643 	struct sk_buff *skb;
644 	unsigned int max_msg_size = 0;
645 	int length, status;
646 	bool disable_credit_flow_ctrl = false;
647 	u16 message_id, service_id, flags = 0;
648 	u8 tx_alloc = 0;
649 
650 	/* special case for HTC pseudo control service */
651 	if (conn_req->service_id == ATH10K_HTC_SVC_ID_RSVD_CTRL) {
652 		disable_credit_flow_ctrl = true;
653 		assigned_eid = ATH10K_HTC_EP_0;
654 		max_msg_size = ATH10K_HTC_MAX_CTRL_MSG_LEN;
655 		memset(&resp_msg_dummy, 0, sizeof(resp_msg_dummy));
656 		goto setup;
657 	}
658 
659 	tx_alloc = ath10k_htc_get_credit_allocation(htc,
660 						    conn_req->service_id);
661 	if (!tx_alloc)
662 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
663 			   "boot htc service %s does not allocate target credits\n",
664 			   htc_service_name(conn_req->service_id));
665 
666 	skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
667 	if (!skb) {
668 		ath10k_err(ar, "Failed to allocate HTC packet\n");
669 		return -ENOMEM;
670 	}
671 
672 	length = sizeof(msg->hdr) + sizeof(msg->connect_service);
673 	skb_put(skb, length);
674 	memset(skb->data, 0, length);
675 
676 	msg = (struct ath10k_htc_msg *)skb->data;
677 	msg->hdr.message_id =
678 		__cpu_to_le16(ATH10K_HTC_MSG_CONNECT_SERVICE_ID);
679 
680 	flags |= SM(tx_alloc, ATH10K_HTC_CONN_FLAGS_RECV_ALLOC);
681 
682 	/* Only enable credit flow control for WMI ctrl service */
683 	if (conn_req->service_id != ATH10K_HTC_SVC_ID_WMI_CONTROL) {
684 		flags |= ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL;
685 		disable_credit_flow_ctrl = true;
686 	}
687 
688 	req_msg = &msg->connect_service;
689 	req_msg->flags = __cpu_to_le16(flags);
690 	req_msg->service_id = __cpu_to_le16(conn_req->service_id);
691 
692 	reinit_completion(&htc->ctl_resp);
693 
694 	status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
695 	if (status) {
696 		kfree_skb(skb);
697 		return status;
698 	}
699 
700 	/* wait for response */
701 	status = wait_for_completion_timeout(&htc->ctl_resp,
702 					     ATH10K_HTC_CONN_SVC_TIMEOUT_HZ);
703 	if (status <= 0) {
704 		if (status == 0)
705 			status = -ETIMEDOUT;
706 		ath10k_err(ar, "Service connect timeout: %d\n", status);
707 		return status;
708 	}
709 
710 	/* we controlled the buffer creation, it's aligned */
711 	msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
712 	resp_msg = &msg->connect_service_response;
713 	message_id = __le16_to_cpu(msg->hdr.message_id);
714 	service_id = __le16_to_cpu(resp_msg->service_id);
715 
716 	if ((message_id != ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID) ||
717 	    (htc->control_resp_len < sizeof(msg->hdr) +
718 	     sizeof(msg->connect_service_response))) {
719 		ath10k_err(ar, "Invalid resp message ID 0x%x", message_id);
720 		return -EPROTO;
721 	}
722 
723 	ath10k_dbg(ar, ATH10K_DBG_HTC,
724 		   "HTC Service %s connect response: status: 0x%x, assigned ep: 0x%x\n",
725 		   htc_service_name(service_id),
726 		   resp_msg->status, resp_msg->eid);
727 
728 	conn_resp->connect_resp_code = resp_msg->status;
729 
730 	/* check response status */
731 	if (resp_msg->status != ATH10K_HTC_CONN_SVC_STATUS_SUCCESS) {
732 		ath10k_err(ar, "HTC Service %s connect request failed: 0x%x)\n",
733 			   htc_service_name(service_id),
734 			   resp_msg->status);
735 		return -EPROTO;
736 	}
737 
738 	assigned_eid = (enum ath10k_htc_ep_id)resp_msg->eid;
739 	max_msg_size = __le16_to_cpu(resp_msg->max_msg_size);
740 
741 setup:
742 
743 	if (assigned_eid >= ATH10K_HTC_EP_COUNT)
744 		return -EPROTO;
745 
746 	if (max_msg_size == 0)
747 		return -EPROTO;
748 
749 	ep = &htc->endpoint[assigned_eid];
750 	ep->eid = assigned_eid;
751 
752 	if (ep->service_id != ATH10K_HTC_SVC_ID_UNUSED)
753 		return -EPROTO;
754 
755 	/* return assigned endpoint to caller */
756 	conn_resp->eid = assigned_eid;
757 	conn_resp->max_msg_len = __le16_to_cpu(resp_msg->max_msg_size);
758 
759 	/* setup the endpoint */
760 	ep->service_id = conn_req->service_id;
761 	ep->max_tx_queue_depth = conn_req->max_send_queue_depth;
762 	ep->max_ep_message_len = __le16_to_cpu(resp_msg->max_msg_size);
763 	ep->tx_credits = tx_alloc;
764 	ep->tx_credit_size = htc->target_credit_size;
765 	ep->tx_credits_per_max_message = ep->max_ep_message_len /
766 					 htc->target_credit_size;
767 
768 	if (ep->max_ep_message_len % htc->target_credit_size)
769 		ep->tx_credits_per_max_message++;
770 
771 	/* copy all the callbacks */
772 	ep->ep_ops = conn_req->ep_ops;
773 
774 	status = ath10k_hif_map_service_to_pipe(htc->ar,
775 						ep->service_id,
776 						&ep->ul_pipe_id,
777 						&ep->dl_pipe_id,
778 						&ep->ul_is_polled,
779 						&ep->dl_is_polled);
780 	if (status)
781 		return status;
782 
783 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
784 		   "boot htc service '%s' ul pipe %d dl pipe %d eid %d ready\n",
785 		   htc_service_name(ep->service_id), ep->ul_pipe_id,
786 		   ep->dl_pipe_id, ep->eid);
787 
788 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
789 		   "boot htc ep %d ul polled %d dl polled %d\n",
790 		   ep->eid, ep->ul_is_polled, ep->dl_is_polled);
791 
792 	if (disable_credit_flow_ctrl && ep->tx_credit_flow_enabled) {
793 		ep->tx_credit_flow_enabled = false;
794 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
795 			   "boot htc service '%s' eid %d TX flow control disabled\n",
796 			   htc_service_name(ep->service_id), assigned_eid);
797 	}
798 
799 	return status;
800 }
801 
802 struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size)
803 {
804 	struct sk_buff *skb;
805 
806 	skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
807 	if (!skb)
808 		return NULL;
809 
810 	skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
811 
812 	/* FW/HTC requires 4-byte aligned streams */
813 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
814 		ath10k_warn(ar, "Unaligned HTC tx skb\n");
815 
816 	return skb;
817 }
818 
819 int ath10k_htc_start(struct ath10k_htc *htc)
820 {
821 	struct ath10k *ar = htc->ar;
822 	struct sk_buff *skb;
823 	int status = 0;
824 	struct ath10k_htc_msg *msg;
825 
826 	skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
827 	if (!skb)
828 		return -ENOMEM;
829 
830 	skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
831 	memset(skb->data, 0, skb->len);
832 
833 	msg = (struct ath10k_htc_msg *)skb->data;
834 	msg->hdr.message_id =
835 		__cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
836 
837 	ath10k_dbg(ar, ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
838 
839 	status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
840 	if (status) {
841 		kfree_skb(skb);
842 		return status;
843 	}
844 
845 	return 0;
846 }
847 
848 /* registered target arrival callback from the HIF layer */
849 int ath10k_htc_init(struct ath10k *ar)
850 {
851 	struct ath10k_hif_cb htc_callbacks;
852 	struct ath10k_htc_ep *ep = NULL;
853 	struct ath10k_htc *htc = &ar->htc;
854 
855 	spin_lock_init(&htc->tx_lock);
856 
857 	ath10k_htc_reset_endpoint_states(htc);
858 
859 	/* setup HIF layer callbacks */
860 	htc_callbacks.rx_completion = ath10k_htc_rx_completion_handler;
861 	htc_callbacks.tx_completion = ath10k_htc_tx_completion_handler;
862 	htc->ar = ar;
863 
864 	/* Get HIF default pipe for HTC message exchange */
865 	ep = &htc->endpoint[ATH10K_HTC_EP_0];
866 
867 	ath10k_hif_set_callbacks(ar, &htc_callbacks);
868 	ath10k_hif_get_default_pipe(ar, &ep->ul_pipe_id, &ep->dl_pipe_id);
869 
870 	init_completion(&htc->ctl_resp);
871 
872 	return 0;
873 }
874