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