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