xref: /linux/drivers/net/wireless/ath/ath10k/htc.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
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 		ath10k_warn("Unable to allocate ctrl skb\n");
50 		return NULL;
51 	}
52 
53 	skb_reserve(skb, 20); /* FIXME: why 20 bytes? */
54 	WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
55 
56 	skb_cb = ATH10K_SKB_CB(skb);
57 	memset(skb_cb, 0, sizeof(*skb_cb));
58 
59 	ath10k_dbg(ATH10K_DBG_HTC, "%s: skb %p\n", __func__, skb);
60 	return skb;
61 }
62 
63 static inline void ath10k_htc_restore_tx_skb(struct ath10k_htc *htc,
64 					     struct sk_buff *skb)
65 {
66 	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
67 
68 	dma_unmap_single(htc->ar->dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
69 	skb_pull(skb, sizeof(struct ath10k_htc_hdr));
70 }
71 
72 static void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep,
73 					    struct sk_buff *skb)
74 {
75 	ath10k_dbg(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("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 	if (!ep->tx_credit_flow_enabled)
93 		return false;
94 	if (ep->tx_credits >= ep->tx_credits_per_max_message)
95 		return false;
96 
97 	ath10k_dbg(ATH10K_DBG_HTC, "HTC: endpoint %d needs credit update\n",
98 		   ep->eid);
99 	return true;
100 }
101 
102 static void ath10k_htc_prepare_tx_skb(struct ath10k_htc_ep *ep,
103 				      struct sk_buff *skb)
104 {
105 	struct ath10k_htc_hdr *hdr;
106 
107 	hdr = (struct ath10k_htc_hdr *)skb->data;
108 
109 	hdr->eid = ep->eid;
110 	hdr->len = __cpu_to_le16(skb->len - sizeof(*hdr));
111 	hdr->flags = 0;
112 
113 	spin_lock_bh(&ep->htc->tx_lock);
114 	hdr->seq_no = ep->seq_no++;
115 
116 	if (ath10k_htc_ep_need_credit_update(ep))
117 		hdr->flags |= ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE;
118 
119 	spin_unlock_bh(&ep->htc->tx_lock);
120 }
121 
122 int ath10k_htc_send(struct ath10k_htc *htc,
123 		    enum ath10k_htc_ep_id eid,
124 		    struct sk_buff *skb)
125 {
126 	struct ath10k_htc_ep *ep = &htc->endpoint[eid];
127 	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
128 	struct ath10k_hif_sg_item sg_item;
129 	struct device *dev = htc->ar->dev;
130 	int credits = 0;
131 	int ret;
132 
133 	if (htc->ar->state == ATH10K_STATE_WEDGED)
134 		return -ECOMM;
135 
136 	if (eid >= ATH10K_HTC_EP_COUNT) {
137 		ath10k_warn("Invalid endpoint id: %d\n", eid);
138 		return -ENOENT;
139 	}
140 
141 	/* FIXME: This looks ugly, can we fix it? */
142 	spin_lock_bh(&htc->tx_lock);
143 	if (htc->stopped) {
144 		spin_unlock_bh(&htc->tx_lock);
145 		return -ESHUTDOWN;
146 	}
147 	spin_unlock_bh(&htc->tx_lock);
148 
149 	skb_push(skb, sizeof(struct ath10k_htc_hdr));
150 
151 	if (ep->tx_credit_flow_enabled) {
152 		credits = DIV_ROUND_UP(skb->len, htc->target_credit_size);
153 		spin_lock_bh(&htc->tx_lock);
154 		if (ep->tx_credits < credits) {
155 			spin_unlock_bh(&htc->tx_lock);
156 			ret = -EAGAIN;
157 			goto err_pull;
158 		}
159 		ep->tx_credits -= credits;
160 		ath10k_dbg(ATH10K_DBG_HTC,
161 			   "htc ep %d consumed %d credits (total %d)\n",
162 			   eid, credits, ep->tx_credits);
163 		spin_unlock_bh(&htc->tx_lock);
164 	}
165 
166 	ath10k_htc_prepare_tx_skb(ep, skb);
167 
168 	skb_cb->paddr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE);
169 	ret = dma_mapping_error(dev, skb_cb->paddr);
170 	if (ret)
171 		goto err_credits;
172 
173 	sg_item.transfer_id = ep->eid;
174 	sg_item.transfer_context = skb;
175 	sg_item.vaddr = skb->data;
176 	sg_item.paddr = skb_cb->paddr;
177 	sg_item.len = skb->len;
178 
179 	ret = ath10k_hif_tx_sg(htc->ar, ep->ul_pipe_id, &sg_item, 1);
180 	if (ret)
181 		goto err_unmap;
182 
183 	return 0;
184 
185 err_unmap:
186 	dma_unmap_single(dev, skb_cb->paddr, skb->len, DMA_TO_DEVICE);
187 err_credits:
188 	if (ep->tx_credit_flow_enabled) {
189 		spin_lock_bh(&htc->tx_lock);
190 		ep->tx_credits += credits;
191 		ath10k_dbg(ATH10K_DBG_HTC,
192 			   "htc ep %d reverted %d credits back (total %d)\n",
193 			   eid, credits, ep->tx_credits);
194 		spin_unlock_bh(&htc->tx_lock);
195 
196 		if (ep->ep_ops.ep_tx_credits)
197 			ep->ep_ops.ep_tx_credits(htc->ar);
198 	}
199 err_pull:
200 	skb_pull(skb, sizeof(struct ath10k_htc_hdr));
201 	return ret;
202 }
203 
204 static int ath10k_htc_tx_completion_handler(struct ath10k *ar,
205 					    struct sk_buff *skb,
206 					    unsigned int eid)
207 {
208 	struct ath10k_htc *htc = &ar->htc;
209 	struct ath10k_htc_ep *ep = &htc->endpoint[eid];
210 
211 	if (WARN_ON_ONCE(!skb))
212 		return 0;
213 
214 	ath10k_htc_notify_tx_completion(ep, skb);
215 	/* the skb now belongs to the completion handler */
216 
217 	return 0;
218 }
219 
220 /***********/
221 /* Receive */
222 /***********/
223 
224 static void
225 ath10k_htc_process_credit_report(struct ath10k_htc *htc,
226 				 const struct ath10k_htc_credit_report *report,
227 				 int len,
228 				 enum ath10k_htc_ep_id eid)
229 {
230 	struct ath10k_htc_ep *ep;
231 	int i, n_reports;
232 
233 	if (len % sizeof(*report))
234 		ath10k_warn("Uneven credit report len %d", len);
235 
236 	n_reports = len / sizeof(*report);
237 
238 	spin_lock_bh(&htc->tx_lock);
239 	for (i = 0; i < n_reports; i++, report++) {
240 		if (report->eid >= ATH10K_HTC_EP_COUNT)
241 			break;
242 
243 		ep = &htc->endpoint[report->eid];
244 		ep->tx_credits += report->credits;
245 
246 		ath10k_dbg(ATH10K_DBG_HTC, "htc ep %d got %d credits (total %d)\n",
247 			   report->eid, report->credits, ep->tx_credits);
248 
249 		if (ep->ep_ops.ep_tx_credits) {
250 			spin_unlock_bh(&htc->tx_lock);
251 			ep->ep_ops.ep_tx_credits(htc->ar);
252 			spin_lock_bh(&htc->tx_lock);
253 		}
254 	}
255 	spin_unlock_bh(&htc->tx_lock);
256 }
257 
258 static int ath10k_htc_process_trailer(struct ath10k_htc *htc,
259 				      u8 *buffer,
260 				      int length,
261 				      enum ath10k_htc_ep_id src_eid)
262 {
263 	int status = 0;
264 	struct ath10k_htc_record *record;
265 	u8 *orig_buffer;
266 	int orig_length;
267 	size_t len;
268 
269 	orig_buffer = buffer;
270 	orig_length = length;
271 
272 	while (length > 0) {
273 		record = (struct ath10k_htc_record *)buffer;
274 
275 		if (length < sizeof(record->hdr)) {
276 			status = -EINVAL;
277 			break;
278 		}
279 
280 		if (record->hdr.len > length) {
281 			/* no room left in buffer for record */
282 			ath10k_warn("Invalid record length: %d\n",
283 				    record->hdr.len);
284 			status = -EINVAL;
285 			break;
286 		}
287 
288 		switch (record->hdr.id) {
289 		case ATH10K_HTC_RECORD_CREDITS:
290 			len = sizeof(struct ath10k_htc_credit_report);
291 			if (record->hdr.len < len) {
292 				ath10k_warn("Credit report too long\n");
293 				status = -EINVAL;
294 				break;
295 			}
296 			ath10k_htc_process_credit_report(htc,
297 							 record->credit_report,
298 							 record->hdr.len,
299 							 src_eid);
300 			break;
301 		default:
302 			ath10k_warn("Unhandled record: id:%d length:%d\n",
303 				    record->hdr.id, record->hdr.len);
304 			break;
305 		}
306 
307 		if (status)
308 			break;
309 
310 		/* multiple records may be present in a trailer */
311 		buffer += sizeof(record->hdr) + record->hdr.len;
312 		length -= sizeof(record->hdr) + record->hdr.len;
313 	}
314 
315 	if (status)
316 		ath10k_dbg_dump(ATH10K_DBG_HTC, "htc rx bad trailer", "",
317 				orig_buffer, orig_length);
318 
319 	return status;
320 }
321 
322 static int ath10k_htc_rx_completion_handler(struct ath10k *ar,
323 					    struct sk_buff *skb,
324 					    u8 pipe_id)
325 {
326 	int status = 0;
327 	struct ath10k_htc *htc = &ar->htc;
328 	struct ath10k_htc_hdr *hdr;
329 	struct ath10k_htc_ep *ep;
330 	u16 payload_len;
331 	u32 trailer_len = 0;
332 	size_t min_len;
333 	u8 eid;
334 	bool trailer_present;
335 
336 	hdr = (struct ath10k_htc_hdr *)skb->data;
337 	skb_pull(skb, sizeof(*hdr));
338 
339 	eid = hdr->eid;
340 
341 	if (eid >= ATH10K_HTC_EP_COUNT) {
342 		ath10k_warn("HTC Rx: invalid eid %d\n", eid);
343 		ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad header", "",
344 				hdr, sizeof(*hdr));
345 		status = -EINVAL;
346 		goto out;
347 	}
348 
349 	ep = &htc->endpoint[eid];
350 
351 	/*
352 	 * If this endpoint that received a message from the target has
353 	 * a to-target HIF pipe whose send completions are polled rather
354 	 * than interrupt-driven, this is a good point to ask HIF to check
355 	 * whether it has any completed sends to handle.
356 	 */
357 	if (ep->ul_is_polled)
358 		ath10k_htc_send_complete_check(ep, 1);
359 
360 	payload_len = __le16_to_cpu(hdr->len);
361 
362 	if (payload_len + sizeof(*hdr) > ATH10K_HTC_MAX_LEN) {
363 		ath10k_warn("HTC rx frame too long, len: %zu\n",
364 			    payload_len + sizeof(*hdr));
365 		ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad rx pkt len", "",
366 				hdr, sizeof(*hdr));
367 		status = -EINVAL;
368 		goto out;
369 	}
370 
371 	if (skb->len < payload_len) {
372 		ath10k_dbg(ATH10K_DBG_HTC,
373 			   "HTC Rx: insufficient length, got %d, expected %d\n",
374 			   skb->len, payload_len);
375 		ath10k_dbg_dump(ATH10K_DBG_HTC, "htc bad rx pkt len",
376 				"", hdr, sizeof(*hdr));
377 		status = -EINVAL;
378 		goto out;
379 	}
380 
381 	/* get flags to check for trailer */
382 	trailer_present = hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
383 	if (trailer_present) {
384 		u8 *trailer;
385 
386 		trailer_len = hdr->trailer_len;
387 		min_len = sizeof(struct ath10k_ath10k_htc_record_hdr);
388 
389 		if ((trailer_len < min_len) ||
390 		    (trailer_len > payload_len)) {
391 			ath10k_warn("Invalid trailer length: %d\n",
392 				    trailer_len);
393 			status = -EPROTO;
394 			goto out;
395 		}
396 
397 		trailer = (u8 *)hdr;
398 		trailer += sizeof(*hdr);
399 		trailer += payload_len;
400 		trailer -= trailer_len;
401 		status = ath10k_htc_process_trailer(htc, trailer,
402 						    trailer_len, hdr->eid);
403 		if (status)
404 			goto out;
405 
406 		skb_trim(skb, skb->len - trailer_len);
407 	}
408 
409 	if (((int)payload_len - (int)trailer_len) <= 0)
410 		/* zero length packet with trailer data, just drop these */
411 		goto out;
412 
413 	if (eid == ATH10K_HTC_EP_0) {
414 		struct ath10k_htc_msg *msg = (struct ath10k_htc_msg *)skb->data;
415 
416 		switch (__le16_to_cpu(msg->hdr.message_id)) {
417 		default:
418 			/* handle HTC control message */
419 			if (completion_done(&htc->ctl_resp)) {
420 				/*
421 				 * this is a fatal error, target should not be
422 				 * sending unsolicited messages on the ep 0
423 				 */
424 				ath10k_warn("HTC rx ctrl still processing\n");
425 				status = -EINVAL;
426 				complete(&htc->ctl_resp);
427 				goto out;
428 			}
429 
430 			htc->control_resp_len =
431 				min_t(int, skb->len,
432 				      ATH10K_HTC_MAX_CTRL_MSG_LEN);
433 
434 			memcpy(htc->control_resp_buffer, skb->data,
435 			       htc->control_resp_len);
436 
437 			complete(&htc->ctl_resp);
438 			break;
439 		case ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE:
440 			htc->htc_ops.target_send_suspend_complete(ar);
441 		}
442 		goto out;
443 	}
444 
445 	ath10k_dbg(ATH10K_DBG_HTC, "htc rx completion ep %d skb %p\n",
446 		   eid, skb);
447 	ep->ep_ops.ep_rx_complete(ar, skb);
448 
449 	/* skb is now owned by the rx completion handler */
450 	skb = NULL;
451 out:
452 	kfree_skb(skb);
453 
454 	return status;
455 }
456 
457 static void ath10k_htc_control_rx_complete(struct ath10k *ar,
458 					   struct sk_buff *skb)
459 {
460 	/* This is unexpected. FW is not supposed to send regular rx on this
461 	 * endpoint. */
462 	ath10k_warn("unexpected htc rx\n");
463 	kfree_skb(skb);
464 }
465 
466 /***************/
467 /* Init/Deinit */
468 /***************/
469 
470 static const char *htc_service_name(enum ath10k_htc_svc_id id)
471 {
472 	switch (id) {
473 	case ATH10K_HTC_SVC_ID_RESERVED:
474 		return "Reserved";
475 	case ATH10K_HTC_SVC_ID_RSVD_CTRL:
476 		return "Control";
477 	case ATH10K_HTC_SVC_ID_WMI_CONTROL:
478 		return "WMI";
479 	case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
480 		return "DATA BE";
481 	case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
482 		return "DATA BK";
483 	case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
484 		return "DATA VI";
485 	case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
486 		return "DATA VO";
487 	case ATH10K_HTC_SVC_ID_NMI_CONTROL:
488 		return "NMI Control";
489 	case ATH10K_HTC_SVC_ID_NMI_DATA:
490 		return "NMI Data";
491 	case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
492 		return "HTT Data";
493 	case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
494 		return "RAW";
495 	}
496 
497 	return "Unknown";
498 }
499 
500 static void ath10k_htc_reset_endpoint_states(struct ath10k_htc *htc)
501 {
502 	struct ath10k_htc_ep *ep;
503 	int i;
504 
505 	for (i = ATH10K_HTC_EP_0; i < ATH10K_HTC_EP_COUNT; i++) {
506 		ep = &htc->endpoint[i];
507 		ep->service_id = ATH10K_HTC_SVC_ID_UNUSED;
508 		ep->max_ep_message_len = 0;
509 		ep->max_tx_queue_depth = 0;
510 		ep->eid = i;
511 		ep->htc = htc;
512 		ep->tx_credit_flow_enabled = true;
513 	}
514 }
515 
516 static void ath10k_htc_setup_target_buffer_assignments(struct ath10k_htc *htc)
517 {
518 	struct ath10k_htc_svc_tx_credits *entry;
519 
520 	entry = &htc->service_tx_alloc[0];
521 
522 	/*
523 	 * for PCIE allocate all credists/HTC buffers to WMI.
524 	 * no buffers are used/required for data. data always
525 	 * remains on host.
526 	 */
527 	entry++;
528 	entry->service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
529 	entry->credit_allocation = htc->total_transmit_credits;
530 }
531 
532 static u8 ath10k_htc_get_credit_allocation(struct ath10k_htc *htc,
533 					   u16 service_id)
534 {
535 	u8 allocation = 0;
536 	int i;
537 
538 	for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
539 		if (htc->service_tx_alloc[i].service_id == service_id)
540 			allocation =
541 			    htc->service_tx_alloc[i].credit_allocation;
542 	}
543 
544 	return allocation;
545 }
546 
547 int ath10k_htc_wait_target(struct ath10k_htc *htc)
548 {
549 	int i, status = 0;
550 	struct ath10k_htc_svc_conn_req conn_req;
551 	struct ath10k_htc_svc_conn_resp conn_resp;
552 	struct ath10k_htc_msg *msg;
553 	u16 message_id;
554 	u16 credit_count;
555 	u16 credit_size;
556 
557 	status = wait_for_completion_timeout(&htc->ctl_resp,
558 					     ATH10K_HTC_WAIT_TIMEOUT_HZ);
559 	if (status == 0) {
560 		/* Workaround: In some cases the PCI HIF doesn't
561 		 * receive interrupt for the control response message
562 		 * even if the buffer was completed. It is suspected
563 		 * iomap writes unmasking PCI CE irqs aren't propagated
564 		 * properly in KVM PCI-passthrough sometimes.
565 		 */
566 		ath10k_warn("failed to receive control response completion, polling..\n");
567 
568 		for (i = 0; i < CE_COUNT; i++)
569 			ath10k_hif_send_complete_check(htc->ar, i, 1);
570 
571 		status = wait_for_completion_timeout(&htc->ctl_resp,
572 					ATH10K_HTC_WAIT_TIMEOUT_HZ);
573 
574 		if (status == 0)
575 			status = -ETIMEDOUT;
576 	}
577 
578 	if (status < 0) {
579 		ath10k_err("ctl_resp never came in (%d)\n", status);
580 		return status;
581 	}
582 
583 	if (htc->control_resp_len < sizeof(msg->hdr) + sizeof(msg->ready)) {
584 		ath10k_err("Invalid HTC ready msg len:%d\n",
585 			   htc->control_resp_len);
586 		return -ECOMM;
587 	}
588 
589 	msg = (struct ath10k_htc_msg *)htc->control_resp_buffer;
590 	message_id   = __le16_to_cpu(msg->hdr.message_id);
591 	credit_count = __le16_to_cpu(msg->ready.credit_count);
592 	credit_size  = __le16_to_cpu(msg->ready.credit_size);
593 
594 	if (message_id != ATH10K_HTC_MSG_READY_ID) {
595 		ath10k_err("Invalid HTC ready msg: 0x%x\n", message_id);
596 		return -ECOMM;
597 	}
598 
599 	htc->total_transmit_credits = credit_count;
600 	htc->target_credit_size = credit_size;
601 
602 	ath10k_dbg(ATH10K_DBG_HTC,
603 		   "Target ready! transmit resources: %d size:%d\n",
604 		   htc->total_transmit_credits,
605 		   htc->target_credit_size);
606 
607 	if ((htc->total_transmit_credits == 0) ||
608 	    (htc->target_credit_size == 0)) {
609 		ath10k_err("Invalid credit size received\n");
610 		return -ECOMM;
611 	}
612 
613 	ath10k_htc_setup_target_buffer_assignments(htc);
614 
615 	/* setup our pseudo HTC control endpoint connection */
616 	memset(&conn_req, 0, sizeof(conn_req));
617 	memset(&conn_resp, 0, sizeof(conn_resp));
618 	conn_req.ep_ops.ep_tx_complete = ath10k_htc_control_tx_complete;
619 	conn_req.ep_ops.ep_rx_complete = ath10k_htc_control_rx_complete;
620 	conn_req.max_send_queue_depth = ATH10K_NUM_CONTROL_TX_BUFFERS;
621 	conn_req.service_id = ATH10K_HTC_SVC_ID_RSVD_CTRL;
622 
623 	/* connect fake service */
624 	status = ath10k_htc_connect_service(htc, &conn_req, &conn_resp);
625 	if (status) {
626 		ath10k_err("could not connect to htc service (%d)\n", status);
627 		return status;
628 	}
629 
630 	return 0;
631 }
632 
633 int ath10k_htc_connect_service(struct ath10k_htc *htc,
634 			       struct ath10k_htc_svc_conn_req *conn_req,
635 			       struct ath10k_htc_svc_conn_resp *conn_resp)
636 {
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(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("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("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("Invalid resp message ID 0x%x", message_id);
720 		return -EPROTO;
721 	}
722 
723 	ath10k_dbg(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("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(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(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(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(int size)
803 {
804 	struct sk_buff *skb;
805 
806 	skb = dev_alloc_skb(size + sizeof(struct ath10k_htc_hdr));
807 	if (!skb) {
808 		ath10k_warn("could not allocate HTC tx skb\n");
809 		return NULL;
810 	}
811 
812 	skb_reserve(skb, sizeof(struct ath10k_htc_hdr));
813 
814 	/* FW/HTC requires 4-byte aligned streams */
815 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
816 		ath10k_warn("Unaligned HTC tx skb\n");
817 
818 	return skb;
819 }
820 
821 int ath10k_htc_start(struct ath10k_htc *htc)
822 {
823 	struct sk_buff *skb;
824 	int status = 0;
825 	struct ath10k_htc_msg *msg;
826 
827 	skb = ath10k_htc_build_tx_ctrl_skb(htc->ar);
828 	if (!skb)
829 		return -ENOMEM;
830 
831 	skb_put(skb, sizeof(msg->hdr) + sizeof(msg->setup_complete_ext));
832 	memset(skb->data, 0, skb->len);
833 
834 	msg = (struct ath10k_htc_msg *)skb->data;
835 	msg->hdr.message_id =
836 		__cpu_to_le16(ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID);
837 
838 	ath10k_dbg(ATH10K_DBG_HTC, "HTC is using TX credit flow control\n");
839 
840 	status = ath10k_htc_send(htc, ATH10K_HTC_EP_0, skb);
841 	if (status) {
842 		kfree_skb(skb);
843 		return status;
844 	}
845 
846 	return 0;
847 }
848 
849 void ath10k_htc_stop(struct ath10k_htc *htc)
850 {
851 	spin_lock_bh(&htc->tx_lock);
852 	htc->stopped = true;
853 	spin_unlock_bh(&htc->tx_lock);
854 }
855 
856 /* registered target arrival callback from the HIF layer */
857 int ath10k_htc_init(struct ath10k *ar)
858 {
859 	struct ath10k_hif_cb htc_callbacks;
860 	struct ath10k_htc_ep *ep = NULL;
861 	struct ath10k_htc *htc = &ar->htc;
862 
863 	spin_lock_init(&htc->tx_lock);
864 
865 	htc->stopped = false;
866 	ath10k_htc_reset_endpoint_states(htc);
867 
868 	/* setup HIF layer callbacks */
869 	htc_callbacks.rx_completion = ath10k_htc_rx_completion_handler;
870 	htc_callbacks.tx_completion = ath10k_htc_tx_completion_handler;
871 	htc->ar = ar;
872 
873 	/* Get HIF default pipe for HTC message exchange */
874 	ep = &htc->endpoint[ATH10K_HTC_EP_0];
875 
876 	ath10k_hif_set_callbacks(ar, &htc_callbacks);
877 	ath10k_hif_get_default_pipe(ar, &ep->ul_pipe_id, &ep->dl_pipe_id);
878 
879 	init_completion(&htc->ctl_resp);
880 
881 	return 0;
882 }
883