Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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