18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2007-2011 Atheros Communications Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
58c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
68c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
108c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
118c2ecf20Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
128c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
138c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
148c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "core.h"
188c2ecf20Sopenharmony_ci#include "debug.h"
198c2ecf20Sopenharmony_ci#include "hif-ops.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define HTC_PACKET_CONTAINER_ALLOCATION 32
228c2ecf20Sopenharmony_ci#define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_tx(struct htc_target *handle,
258c2ecf20Sopenharmony_ci			      struct htc_packet *packet);
268c2ecf20Sopenharmony_cistatic void ath6kl_htc_pipe_cleanup(struct htc_target *handle);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* htc pipe tx path */
298c2ecf20Sopenharmony_cistatic inline void restore_tx_packet(struct htc_packet *packet)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	if (packet->info.tx.flags & HTC_FLAGS_TX_FIXUP_NETBUF) {
328c2ecf20Sopenharmony_ci		skb_pull(packet->skb, sizeof(struct htc_frame_hdr));
338c2ecf20Sopenharmony_ci		packet->info.tx.flags &= ~HTC_FLAGS_TX_FIXUP_NETBUF;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic void do_send_completion(struct htc_endpoint *ep,
388c2ecf20Sopenharmony_ci			       struct list_head *queue_to_indicate)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct htc_packet *packet;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (list_empty(queue_to_indicate)) {
438c2ecf20Sopenharmony_ci		/* nothing to indicate */
448c2ecf20Sopenharmony_ci		return;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (ep->ep_cb.tx_comp_multi != NULL) {
488c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
498c2ecf20Sopenharmony_ci			   "%s: calling ep %d, send complete multiple callback (%d pkts)\n",
508c2ecf20Sopenharmony_ci			   __func__, ep->eid,
518c2ecf20Sopenharmony_ci			   get_queue_depth(queue_to_indicate));
528c2ecf20Sopenharmony_ci		/*
538c2ecf20Sopenharmony_ci		 * a multiple send complete handler is being used,
548c2ecf20Sopenharmony_ci		 * pass the queue to the handler
558c2ecf20Sopenharmony_ci		 */
568c2ecf20Sopenharmony_ci		ep->ep_cb.tx_comp_multi(ep->target, queue_to_indicate);
578c2ecf20Sopenharmony_ci		/*
588c2ecf20Sopenharmony_ci		 * all packets are now owned by the callback,
598c2ecf20Sopenharmony_ci		 * reset queue to be safe
608c2ecf20Sopenharmony_ci		 */
618c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(queue_to_indicate);
628c2ecf20Sopenharmony_ci	} else {
638c2ecf20Sopenharmony_ci		/* using legacy EpTxComplete */
648c2ecf20Sopenharmony_ci		do {
658c2ecf20Sopenharmony_ci			packet = list_first_entry(queue_to_indicate,
668c2ecf20Sopenharmony_ci						  struct htc_packet, list);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci			list_del(&packet->list);
698c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
708c2ecf20Sopenharmony_ci				   "%s: calling ep %d send complete callback on packet 0x%p\n",
718c2ecf20Sopenharmony_ci				   __func__, ep->eid, packet);
728c2ecf20Sopenharmony_ci			ep->ep_cb.tx_complete(ep->target, packet);
738c2ecf20Sopenharmony_ci		} while (!list_empty(queue_to_indicate));
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic void send_packet_completion(struct htc_target *target,
788c2ecf20Sopenharmony_ci				   struct htc_packet *packet)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct htc_endpoint *ep = &target->endpoint[packet->endpoint];
818c2ecf20Sopenharmony_ci	struct list_head container;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	restore_tx_packet(packet);
848c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&container);
858c2ecf20Sopenharmony_ci	list_add_tail(&packet->list, &container);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* do completion */
888c2ecf20Sopenharmony_ci	do_send_completion(ep, &container);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void get_htc_packet_credit_based(struct htc_target *target,
928c2ecf20Sopenharmony_ci					struct htc_endpoint *ep,
938c2ecf20Sopenharmony_ci					struct list_head *queue)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	int credits_required;
968c2ecf20Sopenharmony_ci	int remainder;
978c2ecf20Sopenharmony_ci	u8 send_flags;
988c2ecf20Sopenharmony_ci	struct htc_packet *packet;
998c2ecf20Sopenharmony_ci	unsigned int transfer_len;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* NOTE : the TX lock is held when this function is called */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* loop until we can grab as many packets out of the queue as we can */
1048c2ecf20Sopenharmony_ci	while (true) {
1058c2ecf20Sopenharmony_ci		send_flags = 0;
1068c2ecf20Sopenharmony_ci		if (list_empty(&ep->txq))
1078c2ecf20Sopenharmony_ci			break;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		/* get packet at head, but don't remove it */
1108c2ecf20Sopenharmony_ci		packet = list_first_entry(&ep->txq, struct htc_packet, list);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
1138c2ecf20Sopenharmony_ci			   "%s: got head packet:0x%p , queue depth: %d\n",
1148c2ecf20Sopenharmony_ci			   __func__, packet, get_queue_depth(&ep->txq));
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci		transfer_len = packet->act_len + HTC_HDR_LENGTH;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci		if (transfer_len <= target->tgt_cred_sz) {
1198c2ecf20Sopenharmony_ci			credits_required = 1;
1208c2ecf20Sopenharmony_ci		} else {
1218c2ecf20Sopenharmony_ci			/* figure out how many credits this message requires */
1228c2ecf20Sopenharmony_ci			credits_required = transfer_len / target->tgt_cred_sz;
1238c2ecf20Sopenharmony_ci			remainder = transfer_len % target->tgt_cred_sz;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			if (remainder)
1268c2ecf20Sopenharmony_ci				credits_required++;
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC, "%s: creds required:%d got:%d\n",
1308c2ecf20Sopenharmony_ci			   __func__, credits_required, ep->cred_dist.credits);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		if (ep->eid == ENDPOINT_0) {
1338c2ecf20Sopenharmony_ci			/*
1348c2ecf20Sopenharmony_ci			 * endpoint 0 is special, it always has a credit and
1358c2ecf20Sopenharmony_ci			 * does not require credit based flow control
1368c2ecf20Sopenharmony_ci			 */
1378c2ecf20Sopenharmony_ci			credits_required = 0;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci		} else {
1408c2ecf20Sopenharmony_ci			if (ep->cred_dist.credits < credits_required)
1418c2ecf20Sopenharmony_ci				break;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci			ep->cred_dist.credits -= credits_required;
1448c2ecf20Sopenharmony_ci			ep->ep_st.cred_cosumd += credits_required;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci			/* check if we need credits back from the target */
1478c2ecf20Sopenharmony_ci			if (ep->cred_dist.credits <
1488c2ecf20Sopenharmony_ci					ep->cred_dist.cred_per_msg) {
1498c2ecf20Sopenharmony_ci				/* tell the target we need credits ASAP! */
1508c2ecf20Sopenharmony_ci				send_flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
1518c2ecf20Sopenharmony_ci				ep->ep_st.cred_low_indicate += 1;
1528c2ecf20Sopenharmony_ci				ath6kl_dbg(ATH6KL_DBG_HTC,
1538c2ecf20Sopenharmony_ci					   "%s: host needs credits\n",
1548c2ecf20Sopenharmony_ci					   __func__);
1558c2ecf20Sopenharmony_ci			}
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		/* now we can fully dequeue */
1598c2ecf20Sopenharmony_ci		packet = list_first_entry(&ep->txq, struct htc_packet, list);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		list_del(&packet->list);
1628c2ecf20Sopenharmony_ci		/* save the number of credits this packet consumed */
1638c2ecf20Sopenharmony_ci		packet->info.tx.cred_used = credits_required;
1648c2ecf20Sopenharmony_ci		/* save send flags */
1658c2ecf20Sopenharmony_ci		packet->info.tx.flags = send_flags;
1668c2ecf20Sopenharmony_ci		packet->info.tx.seqno = ep->seqno;
1678c2ecf20Sopenharmony_ci		ep->seqno++;
1688c2ecf20Sopenharmony_ci		/* queue this packet into the caller's queue */
1698c2ecf20Sopenharmony_ci		list_add_tail(&packet->list, queue);
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void get_htc_packet(struct htc_target *target,
1748c2ecf20Sopenharmony_ci			   struct htc_endpoint *ep,
1758c2ecf20Sopenharmony_ci			   struct list_head *queue, int resources)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct htc_packet *packet;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/* NOTE : the TX lock is held when this function is called */
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* loop until we can grab as many packets out of the queue as we can */
1828c2ecf20Sopenharmony_ci	while (resources) {
1838c2ecf20Sopenharmony_ci		if (list_empty(&ep->txq))
1848c2ecf20Sopenharmony_ci			break;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci		packet = list_first_entry(&ep->txq, struct htc_packet, list);
1878c2ecf20Sopenharmony_ci		list_del(&packet->list);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
1908c2ecf20Sopenharmony_ci			   "%s: got packet:0x%p , new queue depth: %d\n",
1918c2ecf20Sopenharmony_ci			   __func__, packet, get_queue_depth(&ep->txq));
1928c2ecf20Sopenharmony_ci		packet->info.tx.seqno = ep->seqno;
1938c2ecf20Sopenharmony_ci		packet->info.tx.flags = 0;
1948c2ecf20Sopenharmony_ci		packet->info.tx.cred_used = 0;
1958c2ecf20Sopenharmony_ci		ep->seqno++;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci		/* queue this packet into the caller's queue */
1988c2ecf20Sopenharmony_ci		list_add_tail(&packet->list, queue);
1998c2ecf20Sopenharmony_ci		resources--;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic int htc_issue_packets(struct htc_target *target,
2048c2ecf20Sopenharmony_ci			     struct htc_endpoint *ep,
2058c2ecf20Sopenharmony_ci			     struct list_head *pkt_queue)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	int status = 0;
2088c2ecf20Sopenharmony_ci	u16 payload_len;
2098c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2108c2ecf20Sopenharmony_ci	struct htc_frame_hdr *htc_hdr;
2118c2ecf20Sopenharmony_ci	struct htc_packet *packet;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC,
2148c2ecf20Sopenharmony_ci		   "%s: queue: 0x%p, pkts %d\n", __func__,
2158c2ecf20Sopenharmony_ci		   pkt_queue, get_queue_depth(pkt_queue));
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	while (!list_empty(pkt_queue)) {
2188c2ecf20Sopenharmony_ci		packet = list_first_entry(pkt_queue, struct htc_packet, list);
2198c2ecf20Sopenharmony_ci		list_del(&packet->list);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		skb = packet->skb;
2228c2ecf20Sopenharmony_ci		if (!skb) {
2238c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
2248c2ecf20Sopenharmony_ci			status = -EINVAL;
2258c2ecf20Sopenharmony_ci			break;
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		payload_len = packet->act_len;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci		/* setup HTC frame header */
2318c2ecf20Sopenharmony_ci		htc_hdr = skb_push(skb, sizeof(*htc_hdr));
2328c2ecf20Sopenharmony_ci		if (!htc_hdr) {
2338c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
2348c2ecf20Sopenharmony_ci			status = -EINVAL;
2358c2ecf20Sopenharmony_ci			break;
2368c2ecf20Sopenharmony_ci		}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		packet->info.tx.flags |= HTC_FLAGS_TX_FIXUP_NETBUF;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		/* Endianess? */
2418c2ecf20Sopenharmony_ci		put_unaligned((u16) payload_len, &htc_hdr->payld_len);
2428c2ecf20Sopenharmony_ci		htc_hdr->flags = packet->info.tx.flags;
2438c2ecf20Sopenharmony_ci		htc_hdr->eid = (u8) packet->endpoint;
2448c2ecf20Sopenharmony_ci		htc_hdr->ctrl[0] = 0;
2458c2ecf20Sopenharmony_ci		htc_hdr->ctrl[1] = (u8) packet->info.tx.seqno;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		spin_lock_bh(&target->tx_lock);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		/* store in look up queue to match completions */
2508c2ecf20Sopenharmony_ci		list_add_tail(&packet->list, &ep->pipe.tx_lookup_queue);
2518c2ecf20Sopenharmony_ci		ep->ep_st.tx_issued += 1;
2528c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->tx_lock);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		status = ath6kl_hif_pipe_send(target->dev->ar,
2558c2ecf20Sopenharmony_ci					      ep->pipe.pipeid_ul, NULL, skb);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci		if (status != 0) {
2588c2ecf20Sopenharmony_ci			if (status != -ENOMEM) {
2598c2ecf20Sopenharmony_ci				/* TODO: if more than 1 endpoint maps to the
2608c2ecf20Sopenharmony_ci				 * same PipeID, it is possible to run out of
2618c2ecf20Sopenharmony_ci				 * resources in the HIF layer.
2628c2ecf20Sopenharmony_ci				 * Don't emit the error
2638c2ecf20Sopenharmony_ci				 */
2648c2ecf20Sopenharmony_ci				ath6kl_dbg(ATH6KL_DBG_HTC,
2658c2ecf20Sopenharmony_ci					   "%s: failed status:%d\n",
2668c2ecf20Sopenharmony_ci					   __func__, status);
2678c2ecf20Sopenharmony_ci			}
2688c2ecf20Sopenharmony_ci			spin_lock_bh(&target->tx_lock);
2698c2ecf20Sopenharmony_ci			list_del(&packet->list);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci			/* reclaim credits */
2728c2ecf20Sopenharmony_ci			ep->cred_dist.credits += packet->info.tx.cred_used;
2738c2ecf20Sopenharmony_ci			spin_unlock_bh(&target->tx_lock);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci			/* put it back into the callers queue */
2768c2ecf20Sopenharmony_ci			list_add(&packet->list, pkt_queue);
2778c2ecf20Sopenharmony_ci			break;
2788c2ecf20Sopenharmony_ci		}
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (status != 0) {
2828c2ecf20Sopenharmony_ci		while (!list_empty(pkt_queue)) {
2838c2ecf20Sopenharmony_ci			if (status != -ENOMEM) {
2848c2ecf20Sopenharmony_ci				ath6kl_dbg(ATH6KL_DBG_HTC,
2858c2ecf20Sopenharmony_ci					   "%s: failed pkt:0x%p status:%d\n",
2868c2ecf20Sopenharmony_ci					   __func__, packet, status);
2878c2ecf20Sopenharmony_ci			}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci			packet = list_first_entry(pkt_queue,
2908c2ecf20Sopenharmony_ci						  struct htc_packet, list);
2918c2ecf20Sopenharmony_ci			list_del(&packet->list);
2928c2ecf20Sopenharmony_ci			packet->status = status;
2938c2ecf20Sopenharmony_ci			send_packet_completion(target, packet);
2948c2ecf20Sopenharmony_ci		}
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return status;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic enum htc_send_queue_result htc_try_send(struct htc_target *target,
3018c2ecf20Sopenharmony_ci					       struct htc_endpoint *ep,
3028c2ecf20Sopenharmony_ci					       struct list_head *txq)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct list_head send_queue;	/* temp queue to hold packets */
3058c2ecf20Sopenharmony_ci	struct htc_packet *packet, *tmp_pkt;
3068c2ecf20Sopenharmony_ci	struct ath6kl *ar = target->dev->ar;
3078c2ecf20Sopenharmony_ci	enum htc_send_full_action action;
3088c2ecf20Sopenharmony_ci	int tx_resources, overflow, txqueue_depth, i, good_pkts;
3098c2ecf20Sopenharmony_ci	u8 pipeid;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC, "%s: (queue:0x%p depth:%d)\n",
3128c2ecf20Sopenharmony_ci		   __func__, txq,
3138c2ecf20Sopenharmony_ci		   (txq == NULL) ? 0 : get_queue_depth(txq));
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* init the local send queue */
3168c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&send_queue);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	/*
3198c2ecf20Sopenharmony_ci	 * txq equals to NULL means
3208c2ecf20Sopenharmony_ci	 * caller didn't provide a queue, just wants us to
3218c2ecf20Sopenharmony_ci	 * check queues and send
3228c2ecf20Sopenharmony_ci	 */
3238c2ecf20Sopenharmony_ci	if (txq != NULL) {
3248c2ecf20Sopenharmony_ci		if (list_empty(txq)) {
3258c2ecf20Sopenharmony_ci			/* empty queue */
3268c2ecf20Sopenharmony_ci			return HTC_SEND_QUEUE_DROP;
3278c2ecf20Sopenharmony_ci		}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci		spin_lock_bh(&target->tx_lock);
3308c2ecf20Sopenharmony_ci		txqueue_depth = get_queue_depth(&ep->txq);
3318c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->tx_lock);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci		if (txqueue_depth >= ep->max_txq_depth) {
3348c2ecf20Sopenharmony_ci			/* we've already overflowed */
3358c2ecf20Sopenharmony_ci			overflow = get_queue_depth(txq);
3368c2ecf20Sopenharmony_ci		} else {
3378c2ecf20Sopenharmony_ci			/* get how much we will overflow by */
3388c2ecf20Sopenharmony_ci			overflow = txqueue_depth;
3398c2ecf20Sopenharmony_ci			overflow += get_queue_depth(txq);
3408c2ecf20Sopenharmony_ci			/* get how much we will overflow the TX queue by */
3418c2ecf20Sopenharmony_ci			overflow -= ep->max_txq_depth;
3428c2ecf20Sopenharmony_ci		}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		/* if overflow is negative or zero, we are okay */
3458c2ecf20Sopenharmony_ci		if (overflow > 0) {
3468c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
3478c2ecf20Sopenharmony_ci				   "%s: Endpoint %d, TX queue will overflow :%d, Tx Depth:%d, Max:%d\n",
3488c2ecf20Sopenharmony_ci				   __func__, ep->eid, overflow, txqueue_depth,
3498c2ecf20Sopenharmony_ci				   ep->max_txq_depth);
3508c2ecf20Sopenharmony_ci		}
3518c2ecf20Sopenharmony_ci		if ((overflow <= 0) ||
3528c2ecf20Sopenharmony_ci		    (ep->ep_cb.tx_full == NULL)) {
3538c2ecf20Sopenharmony_ci			/*
3548c2ecf20Sopenharmony_ci			 * all packets will fit or caller did not provide send
3558c2ecf20Sopenharmony_ci			 * full indication handler -- just move all of them
3568c2ecf20Sopenharmony_ci			 * to the local send_queue object
3578c2ecf20Sopenharmony_ci			 */
3588c2ecf20Sopenharmony_ci			list_splice_tail_init(txq, &send_queue);
3598c2ecf20Sopenharmony_ci		} else {
3608c2ecf20Sopenharmony_ci			good_pkts = get_queue_depth(txq) - overflow;
3618c2ecf20Sopenharmony_ci			if (good_pkts < 0) {
3628c2ecf20Sopenharmony_ci				WARN_ON_ONCE(1);
3638c2ecf20Sopenharmony_ci				return HTC_SEND_QUEUE_DROP;
3648c2ecf20Sopenharmony_ci			}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci			/* we have overflowed, and a callback is provided */
3678c2ecf20Sopenharmony_ci			/* dequeue all non-overflow packets to the sendqueue */
3688c2ecf20Sopenharmony_ci			for (i = 0; i < good_pkts; i++) {
3698c2ecf20Sopenharmony_ci				/* pop off caller's queue */
3708c2ecf20Sopenharmony_ci				packet = list_first_entry(txq,
3718c2ecf20Sopenharmony_ci							  struct htc_packet,
3728c2ecf20Sopenharmony_ci							  list);
3738c2ecf20Sopenharmony_ci				/* move to local queue */
3748c2ecf20Sopenharmony_ci				list_move_tail(&packet->list, &send_queue);
3758c2ecf20Sopenharmony_ci			}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci			/*
3788c2ecf20Sopenharmony_ci			 * the caller's queue has all the packets that won't fit
3798c2ecf20Sopenharmony_ci			 * walk through the caller's queue and indicate each to
3808c2ecf20Sopenharmony_ci			 * the send full handler
3818c2ecf20Sopenharmony_ci			 */
3828c2ecf20Sopenharmony_ci			list_for_each_entry_safe(packet, tmp_pkt,
3838c2ecf20Sopenharmony_ci						 txq, list) {
3848c2ecf20Sopenharmony_ci				ath6kl_dbg(ATH6KL_DBG_HTC,
3858c2ecf20Sopenharmony_ci					   "%s: Indicate overflowed TX pkts: %p\n",
3868c2ecf20Sopenharmony_ci					   __func__, packet);
3878c2ecf20Sopenharmony_ci				action = ep->ep_cb.tx_full(ep->target, packet);
3888c2ecf20Sopenharmony_ci				if (action == HTC_SEND_FULL_DROP) {
3898c2ecf20Sopenharmony_ci					/* callback wants the packet dropped */
3908c2ecf20Sopenharmony_ci					ep->ep_st.tx_dropped += 1;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci					/* leave this one in the caller's queue
3938c2ecf20Sopenharmony_ci					 * for cleanup */
3948c2ecf20Sopenharmony_ci				} else {
3958c2ecf20Sopenharmony_ci					/* callback wants to keep this packet,
3968c2ecf20Sopenharmony_ci					 * move from caller's queue to the send
3978c2ecf20Sopenharmony_ci					 * queue */
3988c2ecf20Sopenharmony_ci					list_move_tail(&packet->list,
3998c2ecf20Sopenharmony_ci						       &send_queue);
4008c2ecf20Sopenharmony_ci				}
4018c2ecf20Sopenharmony_ci			}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci			if (list_empty(&send_queue)) {
4048c2ecf20Sopenharmony_ci				/* no packets made it in, caller will cleanup */
4058c2ecf20Sopenharmony_ci				return HTC_SEND_QUEUE_DROP;
4068c2ecf20Sopenharmony_ci			}
4078c2ecf20Sopenharmony_ci		}
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (!ep->pipe.tx_credit_flow_enabled) {
4118c2ecf20Sopenharmony_ci		tx_resources =
4128c2ecf20Sopenharmony_ci		    ath6kl_hif_pipe_get_free_queue_number(ar,
4138c2ecf20Sopenharmony_ci							  ep->pipe.pipeid_ul);
4148c2ecf20Sopenharmony_ci	} else {
4158c2ecf20Sopenharmony_ci		tx_resources = 0;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	spin_lock_bh(&target->tx_lock);
4198c2ecf20Sopenharmony_ci	if (!list_empty(&send_queue)) {
4208c2ecf20Sopenharmony_ci		/* transfer packets to tail */
4218c2ecf20Sopenharmony_ci		list_splice_tail_init(&send_queue, &ep->txq);
4228c2ecf20Sopenharmony_ci		if (!list_empty(&send_queue)) {
4238c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
4248c2ecf20Sopenharmony_ci			spin_unlock_bh(&target->tx_lock);
4258c2ecf20Sopenharmony_ci			return HTC_SEND_QUEUE_DROP;
4268c2ecf20Sopenharmony_ci		}
4278c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&send_queue);
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	/* increment tx processing count on entry */
4318c2ecf20Sopenharmony_ci	ep->tx_proc_cnt++;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (ep->tx_proc_cnt > 1) {
4348c2ecf20Sopenharmony_ci		/*
4358c2ecf20Sopenharmony_ci		 * Another thread or task is draining the TX queues on this
4368c2ecf20Sopenharmony_ci		 * endpoint that thread will reset the tx processing count
4378c2ecf20Sopenharmony_ci		 * when the queue is drained.
4388c2ecf20Sopenharmony_ci		 */
4398c2ecf20Sopenharmony_ci		ep->tx_proc_cnt--;
4408c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->tx_lock);
4418c2ecf20Sopenharmony_ci		return HTC_SEND_QUEUE_OK;
4428c2ecf20Sopenharmony_ci	}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/***** beyond this point only 1 thread may enter ******/
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/*
4478c2ecf20Sopenharmony_ci	 * Now drain the endpoint TX queue for transmission as long as we have
4488c2ecf20Sopenharmony_ci	 * enough transmit resources.
4498c2ecf20Sopenharmony_ci	 */
4508c2ecf20Sopenharmony_ci	while (true) {
4518c2ecf20Sopenharmony_ci		if (get_queue_depth(&ep->txq) == 0)
4528c2ecf20Sopenharmony_ci			break;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		if (ep->pipe.tx_credit_flow_enabled) {
4558c2ecf20Sopenharmony_ci			/*
4568c2ecf20Sopenharmony_ci			 * Credit based mechanism provides flow control
4578c2ecf20Sopenharmony_ci			 * based on target transmit resource availability,
4588c2ecf20Sopenharmony_ci			 * we assume that the HIF layer will always have
4598c2ecf20Sopenharmony_ci			 * bus resources greater than target transmit
4608c2ecf20Sopenharmony_ci			 * resources.
4618c2ecf20Sopenharmony_ci			 */
4628c2ecf20Sopenharmony_ci			get_htc_packet_credit_based(target, ep, &send_queue);
4638c2ecf20Sopenharmony_ci		} else {
4648c2ecf20Sopenharmony_ci			/*
4658c2ecf20Sopenharmony_ci			 * Get all packets for this endpoint that we can
4668c2ecf20Sopenharmony_ci			 * for this pass.
4678c2ecf20Sopenharmony_ci			 */
4688c2ecf20Sopenharmony_ci			get_htc_packet(target, ep, &send_queue, tx_resources);
4698c2ecf20Sopenharmony_ci		}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci		if (get_queue_depth(&send_queue) == 0) {
4728c2ecf20Sopenharmony_ci			/*
4738c2ecf20Sopenharmony_ci			 * Didn't get packets due to out of resources or TX
4748c2ecf20Sopenharmony_ci			 * queue was drained.
4758c2ecf20Sopenharmony_ci			 */
4768c2ecf20Sopenharmony_ci			break;
4778c2ecf20Sopenharmony_ci		}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->tx_lock);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci		/* send what we can */
4828c2ecf20Sopenharmony_ci		htc_issue_packets(target, ep, &send_queue);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		if (!ep->pipe.tx_credit_flow_enabled) {
4858c2ecf20Sopenharmony_ci			pipeid = ep->pipe.pipeid_ul;
4868c2ecf20Sopenharmony_ci			tx_resources =
4878c2ecf20Sopenharmony_ci			    ath6kl_hif_pipe_get_free_queue_number(ar, pipeid);
4888c2ecf20Sopenharmony_ci		}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		spin_lock_bh(&target->tx_lock);
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* done with this endpoint, we can clear the count */
4948c2ecf20Sopenharmony_ci	ep->tx_proc_cnt = 0;
4958c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->tx_lock);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	return HTC_SEND_QUEUE_OK;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci/* htc control packet manipulation */
5018c2ecf20Sopenharmony_cistatic void destroy_htc_txctrl_packet(struct htc_packet *packet)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5048c2ecf20Sopenharmony_ci	skb = packet->skb;
5058c2ecf20Sopenharmony_ci	dev_kfree_skb(skb);
5068c2ecf20Sopenharmony_ci	kfree(packet);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_cistatic struct htc_packet *build_htc_txctrl_packet(void)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct htc_packet *packet = NULL;
5128c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
5158c2ecf20Sopenharmony_ci	if (packet == NULL)
5168c2ecf20Sopenharmony_ci		return NULL;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	skb = __dev_alloc_skb(HTC_CONTROL_BUFFER_SIZE, GFP_KERNEL);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	if (skb == NULL) {
5218c2ecf20Sopenharmony_ci		kfree(packet);
5228c2ecf20Sopenharmony_ci		return NULL;
5238c2ecf20Sopenharmony_ci	}
5248c2ecf20Sopenharmony_ci	packet->skb = skb;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	return packet;
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_cistatic void htc_free_txctrl_packet(struct htc_target *target,
5308c2ecf20Sopenharmony_ci				   struct htc_packet *packet)
5318c2ecf20Sopenharmony_ci{
5328c2ecf20Sopenharmony_ci	destroy_htc_txctrl_packet(packet);
5338c2ecf20Sopenharmony_ci}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_cistatic struct htc_packet *htc_alloc_txctrl_packet(struct htc_target *target)
5368c2ecf20Sopenharmony_ci{
5378c2ecf20Sopenharmony_ci	return build_htc_txctrl_packet();
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_cistatic void htc_txctrl_complete(struct htc_target *target,
5418c2ecf20Sopenharmony_ci				struct htc_packet *packet)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	htc_free_txctrl_packet(target, packet);
5448c2ecf20Sopenharmony_ci}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci#define MAX_MESSAGE_SIZE 1536
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic int htc_setup_target_buffer_assignments(struct htc_target *target)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	int status, credits, credit_per_maxmsg, i;
5518c2ecf20Sopenharmony_ci	struct htc_pipe_txcredit_alloc *entry;
5528c2ecf20Sopenharmony_ci	unsigned int hif_usbaudioclass = 0;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	credit_per_maxmsg = MAX_MESSAGE_SIZE / target->tgt_cred_sz;
5558c2ecf20Sopenharmony_ci	if (MAX_MESSAGE_SIZE % target->tgt_cred_sz)
5568c2ecf20Sopenharmony_ci		credit_per_maxmsg++;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	/* TODO, this should be configured by the caller! */
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	credits = target->tgt_creds;
5618c2ecf20Sopenharmony_ci	entry = &target->pipe.txcredit_alloc[0];
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	status = -ENOMEM;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/* FIXME: hif_usbaudioclass is always zero */
5668c2ecf20Sopenharmony_ci	if (hif_usbaudioclass) {
5678c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
5688c2ecf20Sopenharmony_ci			   "%s: For USB Audio Class- Total:%d\n",
5698c2ecf20Sopenharmony_ci			   __func__, credits);
5708c2ecf20Sopenharmony_ci		entry++;
5718c2ecf20Sopenharmony_ci		entry++;
5728c2ecf20Sopenharmony_ci		/* Setup VO Service To have Max Credits */
5738c2ecf20Sopenharmony_ci		entry->service_id = WMI_DATA_VO_SVC;
5748c2ecf20Sopenharmony_ci		entry->credit_alloc = (credits - 6);
5758c2ecf20Sopenharmony_ci		if (entry->credit_alloc == 0)
5768c2ecf20Sopenharmony_ci			entry->credit_alloc++;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci		credits -= (int) entry->credit_alloc;
5798c2ecf20Sopenharmony_ci		if (credits <= 0)
5808c2ecf20Sopenharmony_ci			return status;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci		entry++;
5838c2ecf20Sopenharmony_ci		entry->service_id = WMI_CONTROL_SVC;
5848c2ecf20Sopenharmony_ci		entry->credit_alloc = credit_per_maxmsg;
5858c2ecf20Sopenharmony_ci		credits -= (int) entry->credit_alloc;
5868c2ecf20Sopenharmony_ci		if (credits <= 0)
5878c2ecf20Sopenharmony_ci			return status;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci		/* leftovers go to best effort */
5908c2ecf20Sopenharmony_ci		entry++;
5918c2ecf20Sopenharmony_ci		entry++;
5928c2ecf20Sopenharmony_ci		entry->service_id = WMI_DATA_BE_SVC;
5938c2ecf20Sopenharmony_ci		entry->credit_alloc = (u8) credits;
5948c2ecf20Sopenharmony_ci		status = 0;
5958c2ecf20Sopenharmony_ci	} else {
5968c2ecf20Sopenharmony_ci		entry++;
5978c2ecf20Sopenharmony_ci		entry->service_id = WMI_DATA_VI_SVC;
5988c2ecf20Sopenharmony_ci		entry->credit_alloc = credits / 4;
5998c2ecf20Sopenharmony_ci		if (entry->credit_alloc == 0)
6008c2ecf20Sopenharmony_ci			entry->credit_alloc++;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		credits -= (int) entry->credit_alloc;
6038c2ecf20Sopenharmony_ci		if (credits <= 0)
6048c2ecf20Sopenharmony_ci			return status;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci		entry++;
6078c2ecf20Sopenharmony_ci		entry->service_id = WMI_DATA_VO_SVC;
6088c2ecf20Sopenharmony_ci		entry->credit_alloc = credits / 4;
6098c2ecf20Sopenharmony_ci		if (entry->credit_alloc == 0)
6108c2ecf20Sopenharmony_ci			entry->credit_alloc++;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		credits -= (int) entry->credit_alloc;
6138c2ecf20Sopenharmony_ci		if (credits <= 0)
6148c2ecf20Sopenharmony_ci			return status;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci		entry++;
6178c2ecf20Sopenharmony_ci		entry->service_id = WMI_CONTROL_SVC;
6188c2ecf20Sopenharmony_ci		entry->credit_alloc = credit_per_maxmsg;
6198c2ecf20Sopenharmony_ci		credits -= (int) entry->credit_alloc;
6208c2ecf20Sopenharmony_ci		if (credits <= 0)
6218c2ecf20Sopenharmony_ci			return status;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci		entry++;
6248c2ecf20Sopenharmony_ci		entry->service_id = WMI_DATA_BK_SVC;
6258c2ecf20Sopenharmony_ci		entry->credit_alloc = credit_per_maxmsg;
6268c2ecf20Sopenharmony_ci		credits -= (int) entry->credit_alloc;
6278c2ecf20Sopenharmony_ci		if (credits <= 0)
6288c2ecf20Sopenharmony_ci			return status;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci		/* leftovers go to best effort */
6318c2ecf20Sopenharmony_ci		entry++;
6328c2ecf20Sopenharmony_ci		entry->service_id = WMI_DATA_BE_SVC;
6338c2ecf20Sopenharmony_ci		entry->credit_alloc = (u8) credits;
6348c2ecf20Sopenharmony_ci		status = 0;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	if (status == 0) {
6388c2ecf20Sopenharmony_ci		for (i = 0; i < ENDPOINT_MAX; i++) {
6398c2ecf20Sopenharmony_ci			if (target->pipe.txcredit_alloc[i].service_id != 0) {
6408c2ecf20Sopenharmony_ci				ath6kl_dbg(ATH6KL_DBG_HTC,
6418c2ecf20Sopenharmony_ci					   "HTC Service Index : %d TX : 0x%2.2X : alloc:%d\n",
6428c2ecf20Sopenharmony_ci					   i,
6438c2ecf20Sopenharmony_ci					   target->pipe.txcredit_alloc[i].
6448c2ecf20Sopenharmony_ci					   service_id,
6458c2ecf20Sopenharmony_ci					   target->pipe.txcredit_alloc[i].
6468c2ecf20Sopenharmony_ci					   credit_alloc);
6478c2ecf20Sopenharmony_ci			}
6488c2ecf20Sopenharmony_ci		}
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci	return status;
6518c2ecf20Sopenharmony_ci}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci/* process credit reports and call distribution function */
6548c2ecf20Sopenharmony_cistatic void htc_process_credit_report(struct htc_target *target,
6558c2ecf20Sopenharmony_ci				      struct htc_credit_report *rpt,
6568c2ecf20Sopenharmony_ci				      int num_entries,
6578c2ecf20Sopenharmony_ci				      enum htc_endpoint_id from_ep)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	int total_credits = 0, i;
6608c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	/* lock out TX while we update credits */
6638c2ecf20Sopenharmony_ci	spin_lock_bh(&target->tx_lock);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	for (i = 0; i < num_entries; i++, rpt++) {
6668c2ecf20Sopenharmony_ci		if (rpt->eid >= ENDPOINT_MAX) {
6678c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
6688c2ecf20Sopenharmony_ci			spin_unlock_bh(&target->tx_lock);
6698c2ecf20Sopenharmony_ci			return;
6708c2ecf20Sopenharmony_ci		}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		ep = &target->endpoint[rpt->eid];
6738c2ecf20Sopenharmony_ci		ep->cred_dist.credits += rpt->credits;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci		if (ep->cred_dist.credits && get_queue_depth(&ep->txq)) {
6768c2ecf20Sopenharmony_ci			spin_unlock_bh(&target->tx_lock);
6778c2ecf20Sopenharmony_ci			htc_try_send(target, ep, NULL);
6788c2ecf20Sopenharmony_ci			spin_lock_bh(&target->tx_lock);
6798c2ecf20Sopenharmony_ci		}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		total_credits += rpt->credits;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC,
6848c2ecf20Sopenharmony_ci		   "Report indicated %d credits to distribute\n",
6858c2ecf20Sopenharmony_ci		   total_credits);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->tx_lock);
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci/* flush endpoint TX queue */
6918c2ecf20Sopenharmony_cistatic void htc_flush_tx_endpoint(struct htc_target *target,
6928c2ecf20Sopenharmony_ci				  struct htc_endpoint *ep, u16 tag)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	struct htc_packet *packet;
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	spin_lock_bh(&target->tx_lock);
6978c2ecf20Sopenharmony_ci	while (get_queue_depth(&ep->txq)) {
6988c2ecf20Sopenharmony_ci		packet = list_first_entry(&ep->txq, struct htc_packet, list);
6998c2ecf20Sopenharmony_ci		list_del(&packet->list);
7008c2ecf20Sopenharmony_ci		packet->status = 0;
7018c2ecf20Sopenharmony_ci		send_packet_completion(target, packet);
7028c2ecf20Sopenharmony_ci	}
7038c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->tx_lock);
7048c2ecf20Sopenharmony_ci}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci/*
7078c2ecf20Sopenharmony_ci * In the adapted HIF layer, struct sk_buff * are passed between HIF and HTC,
7088c2ecf20Sopenharmony_ci * since upper layers expects struct htc_packet containers we use the completed
7098c2ecf20Sopenharmony_ci * skb and lookup it's corresponding HTC packet buffer from a lookup list.
7108c2ecf20Sopenharmony_ci * This is extra overhead that can be fixed by re-aligning HIF interfaces with
7118c2ecf20Sopenharmony_ci * HTC.
7128c2ecf20Sopenharmony_ci */
7138c2ecf20Sopenharmony_cistatic struct htc_packet *htc_lookup_tx_packet(struct htc_target *target,
7148c2ecf20Sopenharmony_ci					       struct htc_endpoint *ep,
7158c2ecf20Sopenharmony_ci					       struct sk_buff *skb)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	struct htc_packet *packet, *tmp_pkt, *found_packet = NULL;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	spin_lock_bh(&target->tx_lock);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	/*
7228c2ecf20Sopenharmony_ci	 * interate from the front of tx lookup queue
7238c2ecf20Sopenharmony_ci	 * this lookup should be fast since lower layers completes in-order and
7248c2ecf20Sopenharmony_ci	 * so the completed packet should be at the head of the list generally
7258c2ecf20Sopenharmony_ci	 */
7268c2ecf20Sopenharmony_ci	list_for_each_entry_safe(packet, tmp_pkt, &ep->pipe.tx_lookup_queue,
7278c2ecf20Sopenharmony_ci				 list) {
7288c2ecf20Sopenharmony_ci		/* check for removal */
7298c2ecf20Sopenharmony_ci		if (skb == packet->skb) {
7308c2ecf20Sopenharmony_ci			/* found it */
7318c2ecf20Sopenharmony_ci			list_del(&packet->list);
7328c2ecf20Sopenharmony_ci			found_packet = packet;
7338c2ecf20Sopenharmony_ci			break;
7348c2ecf20Sopenharmony_ci		}
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->tx_lock);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return found_packet;
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_tx_complete(struct ath6kl *ar, struct sk_buff *skb)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct htc_target *target = ar->htc_target;
7458c2ecf20Sopenharmony_ci	struct htc_frame_hdr *htc_hdr;
7468c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
7478c2ecf20Sopenharmony_ci	struct htc_packet *packet;
7488c2ecf20Sopenharmony_ci	u8 ep_id, *netdata;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	netdata = skb->data;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	htc_hdr = (struct htc_frame_hdr *) netdata;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	ep_id = htc_hdr->eid;
7558c2ecf20Sopenharmony_ci	ep = &target->endpoint[ep_id];
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	packet = htc_lookup_tx_packet(target, ep, skb);
7588c2ecf20Sopenharmony_ci	if (packet == NULL) {
7598c2ecf20Sopenharmony_ci		/* may have already been flushed and freed */
7608c2ecf20Sopenharmony_ci		ath6kl_err("HTC TX lookup failed!\n");
7618c2ecf20Sopenharmony_ci	} else {
7628c2ecf20Sopenharmony_ci		/* will be giving this buffer back to upper layers */
7638c2ecf20Sopenharmony_ci		packet->status = 0;
7648c2ecf20Sopenharmony_ci		send_packet_completion(target, packet);
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci	skb = NULL;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	if (!ep->pipe.tx_credit_flow_enabled) {
7698c2ecf20Sopenharmony_ci		/*
7708c2ecf20Sopenharmony_ci		 * note: when using TX credit flow, the re-checking of queues
7718c2ecf20Sopenharmony_ci		 * happens when credits flow back from the target. in the
7728c2ecf20Sopenharmony_ci		 * non-TX credit case, we recheck after the packet completes
7738c2ecf20Sopenharmony_ci		 */
7748c2ecf20Sopenharmony_ci		htc_try_send(target, ep, NULL);
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	return 0;
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic int htc_send_packets_multiple(struct htc_target *target,
7818c2ecf20Sopenharmony_ci				     struct list_head *pkt_queue)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
7848c2ecf20Sopenharmony_ci	struct htc_packet *packet, *tmp_pkt;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	if (list_empty(pkt_queue))
7878c2ecf20Sopenharmony_ci		return -EINVAL;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/* get first packet to find out which ep the packets will go into */
7908c2ecf20Sopenharmony_ci	packet = list_first_entry(pkt_queue, struct htc_packet, list);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	if (packet->endpoint >= ENDPOINT_MAX) {
7938c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
7948c2ecf20Sopenharmony_ci		return -EINVAL;
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci	ep = &target->endpoint[packet->endpoint];
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	htc_try_send(target, ep, pkt_queue);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	/* do completion on any packets that couldn't get in */
8018c2ecf20Sopenharmony_ci	if (!list_empty(pkt_queue)) {
8028c2ecf20Sopenharmony_ci		list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
8038c2ecf20Sopenharmony_ci			packet->status = -ENOMEM;
8048c2ecf20Sopenharmony_ci		}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci		do_send_completion(ep, pkt_queue);
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	return 0;
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci/* htc pipe rx path */
8138c2ecf20Sopenharmony_cistatic struct htc_packet *alloc_htc_packet_container(struct htc_target *target)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	struct htc_packet *packet;
8168c2ecf20Sopenharmony_ci	spin_lock_bh(&target->rx_lock);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	if (target->pipe.htc_packet_pool == NULL) {
8198c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->rx_lock);
8208c2ecf20Sopenharmony_ci		return NULL;
8218c2ecf20Sopenharmony_ci	}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	packet = target->pipe.htc_packet_pool;
8248c2ecf20Sopenharmony_ci	target->pipe.htc_packet_pool = (struct htc_packet *) packet->list.next;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->rx_lock);
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	packet->list.next = NULL;
8298c2ecf20Sopenharmony_ci	return packet;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_cistatic void free_htc_packet_container(struct htc_target *target,
8338c2ecf20Sopenharmony_ci				      struct htc_packet *packet)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	struct list_head *lh;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	spin_lock_bh(&target->rx_lock);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	if (target->pipe.htc_packet_pool == NULL) {
8408c2ecf20Sopenharmony_ci		target->pipe.htc_packet_pool = packet;
8418c2ecf20Sopenharmony_ci		packet->list.next = NULL;
8428c2ecf20Sopenharmony_ci	} else {
8438c2ecf20Sopenharmony_ci		lh = (struct list_head *) target->pipe.htc_packet_pool;
8448c2ecf20Sopenharmony_ci		packet->list.next = lh;
8458c2ecf20Sopenharmony_ci		target->pipe.htc_packet_pool = packet;
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->rx_lock);
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic int htc_process_trailer(struct htc_target *target, u8 *buffer,
8528c2ecf20Sopenharmony_ci			       int len, enum htc_endpoint_id from_ep)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct htc_credit_report *report;
8558c2ecf20Sopenharmony_ci	struct htc_record_hdr *record;
8568c2ecf20Sopenharmony_ci	u8 *record_buf;
8578c2ecf20Sopenharmony_ci	int status = 0;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	while (len > 0) {
8608c2ecf20Sopenharmony_ci		if (len < sizeof(struct htc_record_hdr)) {
8618c2ecf20Sopenharmony_ci			status = -EINVAL;
8628c2ecf20Sopenharmony_ci			break;
8638c2ecf20Sopenharmony_ci		}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci		/* these are byte aligned structs */
8668c2ecf20Sopenharmony_ci		record = (struct htc_record_hdr *) buffer;
8678c2ecf20Sopenharmony_ci		len -= sizeof(struct htc_record_hdr);
8688c2ecf20Sopenharmony_ci		buffer += sizeof(struct htc_record_hdr);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci		if (record->len > len) {
8718c2ecf20Sopenharmony_ci			/* no room left in buffer for record */
8728c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
8738c2ecf20Sopenharmony_ci				   "invalid length: %d (id:%d) buffer has: %d bytes left\n",
8748c2ecf20Sopenharmony_ci				   record->len, record->rec_id, len);
8758c2ecf20Sopenharmony_ci			status = -EINVAL;
8768c2ecf20Sopenharmony_ci			break;
8778c2ecf20Sopenharmony_ci		}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci		/* start of record follows the header */
8808c2ecf20Sopenharmony_ci		record_buf = buffer;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci		switch (record->rec_id) {
8838c2ecf20Sopenharmony_ci		case HTC_RECORD_CREDITS:
8848c2ecf20Sopenharmony_ci			if (record->len < sizeof(struct htc_credit_report)) {
8858c2ecf20Sopenharmony_ci				WARN_ON_ONCE(1);
8868c2ecf20Sopenharmony_ci				return -EINVAL;
8878c2ecf20Sopenharmony_ci			}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci			report = (struct htc_credit_report *) record_buf;
8908c2ecf20Sopenharmony_ci			htc_process_credit_report(target, report,
8918c2ecf20Sopenharmony_ci						  record->len / sizeof(*report),
8928c2ecf20Sopenharmony_ci						  from_ep);
8938c2ecf20Sopenharmony_ci			break;
8948c2ecf20Sopenharmony_ci		default:
8958c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
8968c2ecf20Sopenharmony_ci				   "unhandled record: id:%d length:%d\n",
8978c2ecf20Sopenharmony_ci				   record->rec_id, record->len);
8988c2ecf20Sopenharmony_ci			break;
8998c2ecf20Sopenharmony_ci		}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci		/* advance buffer past this record for next time around */
9028c2ecf20Sopenharmony_ci		buffer += record->len;
9038c2ecf20Sopenharmony_ci		len -= record->len;
9048c2ecf20Sopenharmony_ci	}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	return status;
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_cistatic void do_recv_completion(struct htc_endpoint *ep,
9108c2ecf20Sopenharmony_ci			       struct list_head *queue_to_indicate)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	struct htc_packet *packet;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	if (list_empty(queue_to_indicate)) {
9158c2ecf20Sopenharmony_ci		/* nothing to indicate */
9168c2ecf20Sopenharmony_ci		return;
9178c2ecf20Sopenharmony_ci	}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	/* using legacy EpRecv */
9208c2ecf20Sopenharmony_ci	while (!list_empty(queue_to_indicate)) {
9218c2ecf20Sopenharmony_ci		packet = list_first_entry(queue_to_indicate,
9228c2ecf20Sopenharmony_ci					  struct htc_packet, list);
9238c2ecf20Sopenharmony_ci		list_del(&packet->list);
9248c2ecf20Sopenharmony_ci		ep->ep_cb.rx(ep->target, packet);
9258c2ecf20Sopenharmony_ci	}
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	return;
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_cistatic void recv_packet_completion(struct htc_target *target,
9318c2ecf20Sopenharmony_ci				   struct htc_endpoint *ep,
9328c2ecf20Sopenharmony_ci				   struct htc_packet *packet)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	struct list_head container;
9358c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&container);
9368c2ecf20Sopenharmony_ci	list_add_tail(&packet->list, &container);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	/* do completion */
9398c2ecf20Sopenharmony_ci	do_recv_completion(ep, &container);
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_rx_complete(struct ath6kl *ar, struct sk_buff *skb,
9438c2ecf20Sopenharmony_ci				       u8 pipeid)
9448c2ecf20Sopenharmony_ci{
9458c2ecf20Sopenharmony_ci	struct htc_target *target = ar->htc_target;
9468c2ecf20Sopenharmony_ci	u8 *netdata, *trailer, hdr_info;
9478c2ecf20Sopenharmony_ci	struct htc_frame_hdr *htc_hdr;
9488c2ecf20Sopenharmony_ci	u32 netlen, trailerlen = 0;
9498c2ecf20Sopenharmony_ci	struct htc_packet *packet;
9508c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
9518c2ecf20Sopenharmony_ci	u16 payload_len;
9528c2ecf20Sopenharmony_ci	int status = 0;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	/*
9558c2ecf20Sopenharmony_ci	 * ar->htc_target can be NULL due to a race condition that can occur
9568c2ecf20Sopenharmony_ci	 * during driver initialization(we do 'ath6kl_hif_power_on' before
9578c2ecf20Sopenharmony_ci	 * initializing 'ar->htc_target' via 'ath6kl_htc_create').
9588c2ecf20Sopenharmony_ci	 * 'ath6kl_hif_power_on' assigns 'ath6kl_recv_complete' as
9598c2ecf20Sopenharmony_ci	 * usb_complete_t/callback function for 'usb_fill_bulk_urb'.
9608c2ecf20Sopenharmony_ci	 * Thus the possibility of ar->htc_target being NULL
9618c2ecf20Sopenharmony_ci	 * via ath6kl_recv_complete -> ath6kl_usb_io_comp_work.
9628c2ecf20Sopenharmony_ci	 */
9638c2ecf20Sopenharmony_ci	if (!target) {
9648c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC, "Target not yet initialized\n");
9658c2ecf20Sopenharmony_ci		status = -EINVAL;
9668c2ecf20Sopenharmony_ci		goto free_skb;
9678c2ecf20Sopenharmony_ci	}
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	netdata = skb->data;
9718c2ecf20Sopenharmony_ci	netlen = skb->len;
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	htc_hdr = (struct htc_frame_hdr *) netdata;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	if (htc_hdr->eid >= ENDPOINT_MAX) {
9768c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
9778c2ecf20Sopenharmony_ci			   "HTC Rx: invalid EndpointID=%d\n",
9788c2ecf20Sopenharmony_ci			   htc_hdr->eid);
9798c2ecf20Sopenharmony_ci		status = -EINVAL;
9808c2ecf20Sopenharmony_ci		goto free_skb;
9818c2ecf20Sopenharmony_ci	}
9828c2ecf20Sopenharmony_ci	ep = &target->endpoint[htc_hdr->eid];
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	if (netlen < (payload_len + HTC_HDR_LENGTH)) {
9878c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
9888c2ecf20Sopenharmony_ci			   "HTC Rx: insufficient length, got:%d expected =%zu\n",
9898c2ecf20Sopenharmony_ci			   netlen, payload_len + HTC_HDR_LENGTH);
9908c2ecf20Sopenharmony_ci		status = -EINVAL;
9918c2ecf20Sopenharmony_ci		goto free_skb;
9928c2ecf20Sopenharmony_ci	}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	/* get flags to check for trailer */
9958c2ecf20Sopenharmony_ci	hdr_info = htc_hdr->flags;
9968c2ecf20Sopenharmony_ci	if (hdr_info & HTC_FLG_RX_TRAILER) {
9978c2ecf20Sopenharmony_ci		/* extract the trailer length */
9988c2ecf20Sopenharmony_ci		hdr_info = htc_hdr->ctrl[0];
9998c2ecf20Sopenharmony_ci		if ((hdr_info < sizeof(struct htc_record_hdr)) ||
10008c2ecf20Sopenharmony_ci		    (hdr_info > payload_len)) {
10018c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
10028c2ecf20Sopenharmony_ci				   "invalid header: payloadlen should be %d, CB[0]: %d\n",
10038c2ecf20Sopenharmony_ci				   payload_len, hdr_info);
10048c2ecf20Sopenharmony_ci			status = -EINVAL;
10058c2ecf20Sopenharmony_ci			goto free_skb;
10068c2ecf20Sopenharmony_ci		}
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci		trailerlen = hdr_info;
10098c2ecf20Sopenharmony_ci		/* process trailer after hdr/apps payload */
10108c2ecf20Sopenharmony_ci		trailer = (u8 *) htc_hdr + HTC_HDR_LENGTH +
10118c2ecf20Sopenharmony_ci			payload_len - hdr_info;
10128c2ecf20Sopenharmony_ci		status = htc_process_trailer(target, trailer, hdr_info,
10138c2ecf20Sopenharmony_ci					     htc_hdr->eid);
10148c2ecf20Sopenharmony_ci		if (status != 0)
10158c2ecf20Sopenharmony_ci			goto free_skb;
10168c2ecf20Sopenharmony_ci	}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	if (((int) payload_len - (int) trailerlen) <= 0) {
10198c2ecf20Sopenharmony_ci		/* zero length packet with trailer, just drop these */
10208c2ecf20Sopenharmony_ci		goto free_skb;
10218c2ecf20Sopenharmony_ci	}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	if (htc_hdr->eid == ENDPOINT_0) {
10248c2ecf20Sopenharmony_ci		/* handle HTC control message */
10258c2ecf20Sopenharmony_ci		if (target->htc_flags & HTC_OP_STATE_SETUP_COMPLETE) {
10268c2ecf20Sopenharmony_ci			/*
10278c2ecf20Sopenharmony_ci			 * fatal: target should not send unsolicited
10288c2ecf20Sopenharmony_ci			 * messageson the endpoint 0
10298c2ecf20Sopenharmony_ci			 */
10308c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
10318c2ecf20Sopenharmony_ci				   "HTC ignores Rx Ctrl after setup complete\n");
10328c2ecf20Sopenharmony_ci			status = -EINVAL;
10338c2ecf20Sopenharmony_ci			goto free_skb;
10348c2ecf20Sopenharmony_ci		}
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci		/* remove HTC header */
10378c2ecf20Sopenharmony_ci		skb_pull(skb, HTC_HDR_LENGTH);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci		netdata = skb->data;
10408c2ecf20Sopenharmony_ci		netlen = skb->len;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci		spin_lock_bh(&target->rx_lock);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		target->pipe.ctrl_response_valid = true;
10458c2ecf20Sopenharmony_ci		target->pipe.ctrl_response_len = min_t(int, netlen,
10468c2ecf20Sopenharmony_ci						       HTC_MAX_CTRL_MSG_LEN);
10478c2ecf20Sopenharmony_ci		memcpy(target->pipe.ctrl_response_buf, netdata,
10488c2ecf20Sopenharmony_ci		       target->pipe.ctrl_response_len);
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->rx_lock);
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
10538c2ecf20Sopenharmony_ci		skb = NULL;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci		goto free_skb;
10568c2ecf20Sopenharmony_ci	}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	/*
10598c2ecf20Sopenharmony_ci	 * TODO: the message based HIF architecture allocates net bufs
10608c2ecf20Sopenharmony_ci	 * for recv packets since it bridges that HIF to upper layers,
10618c2ecf20Sopenharmony_ci	 * which expects HTC packets, we form the packets here
10628c2ecf20Sopenharmony_ci	 */
10638c2ecf20Sopenharmony_ci	packet = alloc_htc_packet_container(target);
10648c2ecf20Sopenharmony_ci	if (packet == NULL) {
10658c2ecf20Sopenharmony_ci		status = -ENOMEM;
10668c2ecf20Sopenharmony_ci		goto free_skb;
10678c2ecf20Sopenharmony_ci	}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	packet->status = 0;
10708c2ecf20Sopenharmony_ci	packet->endpoint = htc_hdr->eid;
10718c2ecf20Sopenharmony_ci	packet->pkt_cntxt = skb;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	/* TODO: for backwards compatibility */
10748c2ecf20Sopenharmony_ci	packet->buf = skb_push(skb, 0) + HTC_HDR_LENGTH;
10758c2ecf20Sopenharmony_ci	packet->act_len = netlen - HTC_HDR_LENGTH - trailerlen;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	/*
10788c2ecf20Sopenharmony_ci	 * TODO: this is a hack because the driver layer will set the
10798c2ecf20Sopenharmony_ci	 * actual len of the skb again which will just double the len
10808c2ecf20Sopenharmony_ci	 */
10818c2ecf20Sopenharmony_ci	skb_trim(skb, 0);
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	recv_packet_completion(target, ep, packet);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	/* recover the packet container */
10868c2ecf20Sopenharmony_ci	free_htc_packet_container(target, packet);
10878c2ecf20Sopenharmony_ci	skb = NULL;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_cifree_skb:
10908c2ecf20Sopenharmony_ci	dev_kfree_skb(skb);
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	return status;
10938c2ecf20Sopenharmony_ci}
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_cistatic void htc_flush_rx_queue(struct htc_target *target,
10968c2ecf20Sopenharmony_ci			       struct htc_endpoint *ep)
10978c2ecf20Sopenharmony_ci{
10988c2ecf20Sopenharmony_ci	struct list_head container;
10998c2ecf20Sopenharmony_ci	struct htc_packet *packet;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	spin_lock_bh(&target->rx_lock);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	while (1) {
11048c2ecf20Sopenharmony_ci		if (list_empty(&ep->rx_bufq))
11058c2ecf20Sopenharmony_ci			break;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci		packet = list_first_entry(&ep->rx_bufq,
11088c2ecf20Sopenharmony_ci					  struct htc_packet, list);
11098c2ecf20Sopenharmony_ci		list_del(&packet->list);
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->rx_lock);
11128c2ecf20Sopenharmony_ci		packet->status = -ECANCELED;
11138c2ecf20Sopenharmony_ci		packet->act_len = 0;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
11168c2ecf20Sopenharmony_ci			   "Flushing RX packet:0x%p, length:%d, ep:%d\n",
11178c2ecf20Sopenharmony_ci			   packet, packet->buf_len,
11188c2ecf20Sopenharmony_ci			   packet->endpoint);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&container);
11218c2ecf20Sopenharmony_ci		list_add_tail(&packet->list, &container);
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci		/* give the packet back */
11248c2ecf20Sopenharmony_ci		do_recv_completion(ep, &container);
11258c2ecf20Sopenharmony_ci		spin_lock_bh(&target->rx_lock);
11268c2ecf20Sopenharmony_ci	}
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->rx_lock);
11298c2ecf20Sopenharmony_ci}
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci/* polling routine to wait for a control packet to be received */
11328c2ecf20Sopenharmony_cistatic int htc_wait_recv_ctrl_message(struct htc_target *target)
11338c2ecf20Sopenharmony_ci{
11348c2ecf20Sopenharmony_ci	int count = HTC_TARGET_RESPONSE_POLL_COUNT;
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	while (count > 0) {
11378c2ecf20Sopenharmony_ci		spin_lock_bh(&target->rx_lock);
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci		if (target->pipe.ctrl_response_valid) {
11408c2ecf20Sopenharmony_ci			target->pipe.ctrl_response_valid = false;
11418c2ecf20Sopenharmony_ci			spin_unlock_bh(&target->rx_lock);
11428c2ecf20Sopenharmony_ci			break;
11438c2ecf20Sopenharmony_ci		}
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->rx_lock);
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci		count--;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci		msleep_interruptible(HTC_TARGET_RESPONSE_POLL_WAIT);
11508c2ecf20Sopenharmony_ci	}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	if (count <= 0) {
11538c2ecf20Sopenharmony_ci		ath6kl_warn("htc pipe control receive timeout!\n");
11548c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	return 0;
11588c2ecf20Sopenharmony_ci}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_cistatic void htc_rxctrl_complete(struct htc_target *context,
11618c2ecf20Sopenharmony_ci				struct htc_packet *packet)
11628c2ecf20Sopenharmony_ci{
11638c2ecf20Sopenharmony_ci	struct sk_buff *skb = packet->skb;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	if (packet->endpoint == ENDPOINT_0 &&
11668c2ecf20Sopenharmony_ci	    packet->status == -ECANCELED &&
11678c2ecf20Sopenharmony_ci	    skb != NULL)
11688c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
11698c2ecf20Sopenharmony_ci}
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci/* htc pipe initialization */
11728c2ecf20Sopenharmony_cistatic void reset_endpoint_states(struct htc_target *target)
11738c2ecf20Sopenharmony_ci{
11748c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
11758c2ecf20Sopenharmony_ci	int i;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
11788c2ecf20Sopenharmony_ci		ep = &target->endpoint[i];
11798c2ecf20Sopenharmony_ci		ep->svc_id = 0;
11808c2ecf20Sopenharmony_ci		ep->len_max = 0;
11818c2ecf20Sopenharmony_ci		ep->max_txq_depth = 0;
11828c2ecf20Sopenharmony_ci		ep->eid = i;
11838c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&ep->txq);
11848c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue);
11858c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&ep->rx_bufq);
11868c2ecf20Sopenharmony_ci		ep->target = target;
11878c2ecf20Sopenharmony_ci		ep->pipe.tx_credit_flow_enabled = true;
11888c2ecf20Sopenharmony_ci	}
11898c2ecf20Sopenharmony_ci}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci/* start HTC, this is called after all services are connected */
11928c2ecf20Sopenharmony_cistatic int htc_config_target_hif_pipe(struct htc_target *target)
11938c2ecf20Sopenharmony_ci{
11948c2ecf20Sopenharmony_ci	return 0;
11958c2ecf20Sopenharmony_ci}
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci/* htc service functions */
11988c2ecf20Sopenharmony_cistatic u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id)
11998c2ecf20Sopenharmony_ci{
12008c2ecf20Sopenharmony_ci	u8 allocation = 0;
12018c2ecf20Sopenharmony_ci	int i;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	for (i = 0; i < ENDPOINT_MAX; i++) {
12048c2ecf20Sopenharmony_ci		if (target->pipe.txcredit_alloc[i].service_id == service_id)
12058c2ecf20Sopenharmony_ci			allocation =
12068c2ecf20Sopenharmony_ci				target->pipe.txcredit_alloc[i].credit_alloc;
12078c2ecf20Sopenharmony_ci	}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	if (allocation == 0) {
12108c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
12118c2ecf20Sopenharmony_ci			   "HTC Service TX : 0x%2.2X : allocation is zero!\n",
12128c2ecf20Sopenharmony_ci			   service_id);
12138c2ecf20Sopenharmony_ci	}
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	return allocation;
12168c2ecf20Sopenharmony_ci}
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_conn_service(struct htc_target *target,
12198c2ecf20Sopenharmony_ci		     struct htc_service_connect_req *conn_req,
12208c2ecf20Sopenharmony_ci		     struct htc_service_connect_resp *conn_resp)
12218c2ecf20Sopenharmony_ci{
12228c2ecf20Sopenharmony_ci	struct ath6kl *ar = target->dev->ar;
12238c2ecf20Sopenharmony_ci	struct htc_packet *packet = NULL;
12248c2ecf20Sopenharmony_ci	struct htc_conn_service_resp *resp_msg;
12258c2ecf20Sopenharmony_ci	struct htc_conn_service_msg *conn_msg;
12268c2ecf20Sopenharmony_ci	enum htc_endpoint_id assigned_epid = ENDPOINT_MAX;
12278c2ecf20Sopenharmony_ci	bool disable_credit_flowctrl = false;
12288c2ecf20Sopenharmony_ci	unsigned int max_msg_size = 0;
12298c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
12308c2ecf20Sopenharmony_ci	int length, status = 0;
12318c2ecf20Sopenharmony_ci	struct sk_buff *skb;
12328c2ecf20Sopenharmony_ci	u8 tx_alloc;
12338c2ecf20Sopenharmony_ci	u16 flags;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	if (conn_req->svc_id == 0) {
12368c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
12378c2ecf20Sopenharmony_ci		status = -EINVAL;
12388c2ecf20Sopenharmony_ci		goto free_packet;
12398c2ecf20Sopenharmony_ci	}
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
12428c2ecf20Sopenharmony_ci		/* special case for pseudo control service */
12438c2ecf20Sopenharmony_ci		assigned_epid = ENDPOINT_0;
12448c2ecf20Sopenharmony_ci		max_msg_size = HTC_MAX_CTRL_MSG_LEN;
12458c2ecf20Sopenharmony_ci		tx_alloc = 0;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	} else {
12488c2ecf20Sopenharmony_ci		tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id);
12498c2ecf20Sopenharmony_ci		if (tx_alloc == 0) {
12508c2ecf20Sopenharmony_ci			status = -ENOMEM;
12518c2ecf20Sopenharmony_ci			goto free_packet;
12528c2ecf20Sopenharmony_ci		}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci		/* allocate a packet to send to the target */
12558c2ecf20Sopenharmony_ci		packet = htc_alloc_txctrl_packet(target);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci		if (packet == NULL) {
12588c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
12598c2ecf20Sopenharmony_ci			status = -ENOMEM;
12608c2ecf20Sopenharmony_ci			goto free_packet;
12618c2ecf20Sopenharmony_ci		}
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci		skb = packet->skb;
12648c2ecf20Sopenharmony_ci		length = sizeof(struct htc_conn_service_msg);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci		/* assemble connect service message */
12678c2ecf20Sopenharmony_ci		conn_msg = skb_put(skb, length);
12688c2ecf20Sopenharmony_ci		if (conn_msg == NULL) {
12698c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
12708c2ecf20Sopenharmony_ci			status = -EINVAL;
12718c2ecf20Sopenharmony_ci			goto free_packet;
12728c2ecf20Sopenharmony_ci		}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci		memset(conn_msg, 0,
12758c2ecf20Sopenharmony_ci		       sizeof(struct htc_conn_service_msg));
12768c2ecf20Sopenharmony_ci		conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
12778c2ecf20Sopenharmony_ci		conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
12788c2ecf20Sopenharmony_ci		conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags &
12798c2ecf20Sopenharmony_ci					~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK);
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ci		/* tell target desired recv alloc for this ep */
12828c2ecf20Sopenharmony_ci		flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT;
12838c2ecf20Sopenharmony_ci		conn_msg->conn_flags |= cpu_to_le16(flags);
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci		if (conn_req->conn_flags &
12868c2ecf20Sopenharmony_ci		    HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) {
12878c2ecf20Sopenharmony_ci			disable_credit_flowctrl = true;
12888c2ecf20Sopenharmony_ci		}
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci		set_htc_pkt_info(packet, NULL, (u8 *) conn_msg,
12918c2ecf20Sopenharmony_ci				 length,
12928c2ecf20Sopenharmony_ci				 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci		status = ath6kl_htc_pipe_tx(target, packet);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci		/* we don't own it anymore */
12978c2ecf20Sopenharmony_ci		packet = NULL;
12988c2ecf20Sopenharmony_ci		if (status != 0)
12998c2ecf20Sopenharmony_ci			goto free_packet;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci		/* wait for response */
13028c2ecf20Sopenharmony_ci		status = htc_wait_recv_ctrl_message(target);
13038c2ecf20Sopenharmony_ci		if (status != 0)
13048c2ecf20Sopenharmony_ci			goto free_packet;
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci		/* we controlled the buffer creation so it has to be
13078c2ecf20Sopenharmony_ci		 * properly aligned
13088c2ecf20Sopenharmony_ci		 */
13098c2ecf20Sopenharmony_ci		resp_msg = (struct htc_conn_service_resp *)
13108c2ecf20Sopenharmony_ci		    target->pipe.ctrl_response_buf;
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci		if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) ||
13138c2ecf20Sopenharmony_ci		    (target->pipe.ctrl_response_len < sizeof(*resp_msg))) {
13148c2ecf20Sopenharmony_ci			/* this message is not valid */
13158c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
13168c2ecf20Sopenharmony_ci			status = -EINVAL;
13178c2ecf20Sopenharmony_ci			goto free_packet;
13188c2ecf20Sopenharmony_ci		}
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_TRC,
13218c2ecf20Sopenharmony_ci			   "%s: service 0x%X conn resp: status: %d ep: %d\n",
13228c2ecf20Sopenharmony_ci			   __func__, resp_msg->svc_id, resp_msg->status,
13238c2ecf20Sopenharmony_ci			   resp_msg->eid);
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci		conn_resp->resp_code = resp_msg->status;
13268c2ecf20Sopenharmony_ci		/* check response status */
13278c2ecf20Sopenharmony_ci		if (resp_msg->status != HTC_SERVICE_SUCCESS) {
13288c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
13298c2ecf20Sopenharmony_ci				   "Target failed service 0x%X connect request (status:%d)\n",
13308c2ecf20Sopenharmony_ci				   resp_msg->svc_id, resp_msg->status);
13318c2ecf20Sopenharmony_ci			status = -EINVAL;
13328c2ecf20Sopenharmony_ci			goto free_packet;
13338c2ecf20Sopenharmony_ci		}
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci		assigned_epid = (enum htc_endpoint_id) resp_msg->eid;
13368c2ecf20Sopenharmony_ci		max_msg_size = le16_to_cpu(resp_msg->max_msg_sz);
13378c2ecf20Sopenharmony_ci	}
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	/* the rest are parameter checks so set the error status */
13408c2ecf20Sopenharmony_ci	status = -EINVAL;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	if (assigned_epid >= ENDPOINT_MAX) {
13438c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
13448c2ecf20Sopenharmony_ci		goto free_packet;
13458c2ecf20Sopenharmony_ci	}
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	if (max_msg_size == 0) {
13488c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
13498c2ecf20Sopenharmony_ci		goto free_packet;
13508c2ecf20Sopenharmony_ci	}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	ep = &target->endpoint[assigned_epid];
13538c2ecf20Sopenharmony_ci	ep->eid = assigned_epid;
13548c2ecf20Sopenharmony_ci	if (ep->svc_id != 0) {
13558c2ecf20Sopenharmony_ci		/* endpoint already in use! */
13568c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
13578c2ecf20Sopenharmony_ci		goto free_packet;
13588c2ecf20Sopenharmony_ci	}
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	/* return assigned endpoint to caller */
13618c2ecf20Sopenharmony_ci	conn_resp->endpoint = assigned_epid;
13628c2ecf20Sopenharmony_ci	conn_resp->len_max = max_msg_size;
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	/* setup the endpoint */
13658c2ecf20Sopenharmony_ci	ep->svc_id = conn_req->svc_id; /* this marks ep in use */
13668c2ecf20Sopenharmony_ci	ep->max_txq_depth = conn_req->max_txq_depth;
13678c2ecf20Sopenharmony_ci	ep->len_max = max_msg_size;
13688c2ecf20Sopenharmony_ci	ep->cred_dist.credits = tx_alloc;
13698c2ecf20Sopenharmony_ci	ep->cred_dist.cred_sz = target->tgt_cred_sz;
13708c2ecf20Sopenharmony_ci	ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz;
13718c2ecf20Sopenharmony_ci	if (max_msg_size % target->tgt_cred_sz)
13728c2ecf20Sopenharmony_ci		ep->cred_dist.cred_per_msg++;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	/* copy all the callbacks */
13758c2ecf20Sopenharmony_ci	ep->ep_cb = conn_req->ep_cb;
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	/* initialize tx_drop_packet_threshold */
13788c2ecf20Sopenharmony_ci	ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	status = ath6kl_hif_pipe_map_service(ar, ep->svc_id,
13818c2ecf20Sopenharmony_ci					     &ep->pipe.pipeid_ul,
13828c2ecf20Sopenharmony_ci					     &ep->pipe.pipeid_dl);
13838c2ecf20Sopenharmony_ci	if (status != 0)
13848c2ecf20Sopenharmony_ci		goto free_packet;
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC,
13878c2ecf20Sopenharmony_ci		   "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n",
13888c2ecf20Sopenharmony_ci		   ep->svc_id, ep->pipe.pipeid_ul,
13898c2ecf20Sopenharmony_ci		   ep->pipe.pipeid_dl, ep->eid);
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) {
13928c2ecf20Sopenharmony_ci		ep->pipe.tx_credit_flow_enabled = false;
13938c2ecf20Sopenharmony_ci		ath6kl_dbg(ATH6KL_DBG_HTC,
13948c2ecf20Sopenharmony_ci			   "SVC: 0x%4.4X ep:%d TX flow control off\n",
13958c2ecf20Sopenharmony_ci			   ep->svc_id, assigned_epid);
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_cifree_packet:
13998c2ecf20Sopenharmony_ci	if (packet != NULL)
14008c2ecf20Sopenharmony_ci		htc_free_txctrl_packet(target, packet);
14018c2ecf20Sopenharmony_ci	return status;
14028c2ecf20Sopenharmony_ci}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci/* htc export functions */
14058c2ecf20Sopenharmony_cistatic void *ath6kl_htc_pipe_create(struct ath6kl *ar)
14068c2ecf20Sopenharmony_ci{
14078c2ecf20Sopenharmony_ci	int status = 0;
14088c2ecf20Sopenharmony_ci	struct htc_endpoint *ep = NULL;
14098c2ecf20Sopenharmony_ci	struct htc_target *target = NULL;
14108c2ecf20Sopenharmony_ci	struct htc_packet *packet;
14118c2ecf20Sopenharmony_ci	int i;
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
14148c2ecf20Sopenharmony_ci	if (target == NULL) {
14158c2ecf20Sopenharmony_ci		ath6kl_err("htc create unable to allocate memory\n");
14168c2ecf20Sopenharmony_ci		status = -ENOMEM;
14178c2ecf20Sopenharmony_ci		goto fail_htc_create;
14188c2ecf20Sopenharmony_ci	}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	spin_lock_init(&target->htc_lock);
14218c2ecf20Sopenharmony_ci	spin_lock_init(&target->rx_lock);
14228c2ecf20Sopenharmony_ci	spin_lock_init(&target->tx_lock);
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	reset_endpoint_states(target);
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
14278c2ecf20Sopenharmony_ci		packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci		if (packet != NULL)
14308c2ecf20Sopenharmony_ci			free_htc_packet_container(target, packet);
14318c2ecf20Sopenharmony_ci	}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
14348c2ecf20Sopenharmony_ci	if (!target->dev) {
14358c2ecf20Sopenharmony_ci		ath6kl_err("unable to allocate memory\n");
14368c2ecf20Sopenharmony_ci		status = -ENOMEM;
14378c2ecf20Sopenharmony_ci		goto fail_htc_create;
14388c2ecf20Sopenharmony_ci	}
14398c2ecf20Sopenharmony_ci	target->dev->ar = ar;
14408c2ecf20Sopenharmony_ci	target->dev->htc_cnxt = target;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	/* Get HIF default pipe for HTC message exchange */
14438c2ecf20Sopenharmony_ci	ep = &target->endpoint[ENDPOINT_0];
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul,
14468c2ecf20Sopenharmony_ci				    &ep->pipe.pipeid_dl);
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	return target;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_cifail_htc_create:
14518c2ecf20Sopenharmony_ci	if (status != 0) {
14528c2ecf20Sopenharmony_ci		if (target != NULL)
14538c2ecf20Sopenharmony_ci			ath6kl_htc_pipe_cleanup(target);
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci		target = NULL;
14568c2ecf20Sopenharmony_ci	}
14578c2ecf20Sopenharmony_ci	return target;
14588c2ecf20Sopenharmony_ci}
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci/* cleanup the HTC instance */
14618c2ecf20Sopenharmony_cistatic void ath6kl_htc_pipe_cleanup(struct htc_target *target)
14628c2ecf20Sopenharmony_ci{
14638c2ecf20Sopenharmony_ci	struct htc_packet *packet;
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	while (true) {
14668c2ecf20Sopenharmony_ci		packet = alloc_htc_packet_container(target);
14678c2ecf20Sopenharmony_ci		if (packet == NULL)
14688c2ecf20Sopenharmony_ci			break;
14698c2ecf20Sopenharmony_ci		kfree(packet);
14708c2ecf20Sopenharmony_ci	}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	kfree(target->dev);
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	/* kfree our instance */
14758c2ecf20Sopenharmony_ci	kfree(target);
14768c2ecf20Sopenharmony_ci}
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_start(struct htc_target *target)
14798c2ecf20Sopenharmony_ci{
14808c2ecf20Sopenharmony_ci	struct sk_buff *skb;
14818c2ecf20Sopenharmony_ci	struct htc_setup_comp_ext_msg *setup;
14828c2ecf20Sopenharmony_ci	struct htc_packet *packet;
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	htc_config_target_hif_pipe(target);
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	/* allocate a buffer to send */
14878c2ecf20Sopenharmony_ci	packet = htc_alloc_txctrl_packet(target);
14888c2ecf20Sopenharmony_ci	if (packet == NULL) {
14898c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
14908c2ecf20Sopenharmony_ci		return -ENOMEM;
14918c2ecf20Sopenharmony_ci	}
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	skb = packet->skb;
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	/* assemble setup complete message */
14968c2ecf20Sopenharmony_ci	setup = skb_put(skb, sizeof(*setup));
14978c2ecf20Sopenharmony_ci	memset(setup, 0, sizeof(struct htc_setup_comp_ext_msg));
14988c2ecf20Sopenharmony_ci	setup->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC, "HTC using TX credit flow control\n");
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci	set_htc_pkt_info(packet, NULL, (u8 *) setup,
15038c2ecf20Sopenharmony_ci			 sizeof(struct htc_setup_comp_ext_msg),
15048c2ecf20Sopenharmony_ci			 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	target->htc_flags |= HTC_OP_STATE_SETUP_COMPLETE;
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	return ath6kl_htc_pipe_tx(target, packet);
15098c2ecf20Sopenharmony_ci}
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_cistatic void ath6kl_htc_pipe_stop(struct htc_target *target)
15128c2ecf20Sopenharmony_ci{
15138c2ecf20Sopenharmony_ci	int i;
15148c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci	/* cleanup endpoints */
15178c2ecf20Sopenharmony_ci	for (i = 0; i < ENDPOINT_MAX; i++) {
15188c2ecf20Sopenharmony_ci		ep = &target->endpoint[i];
15198c2ecf20Sopenharmony_ci		htc_flush_rx_queue(target, ep);
15208c2ecf20Sopenharmony_ci		htc_flush_tx_endpoint(target, ep, HTC_TX_PACKET_TAG_ALL);
15218c2ecf20Sopenharmony_ci	}
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	reset_endpoint_states(target);
15248c2ecf20Sopenharmony_ci	target->htc_flags &= ~HTC_OP_STATE_SETUP_COMPLETE;
15258c2ecf20Sopenharmony_ci}
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_get_rxbuf_num(struct htc_target *target,
15288c2ecf20Sopenharmony_ci					 enum htc_endpoint_id endpoint)
15298c2ecf20Sopenharmony_ci{
15308c2ecf20Sopenharmony_ci	int num;
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_ci	spin_lock_bh(&target->rx_lock);
15338c2ecf20Sopenharmony_ci	num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
15348c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->rx_lock);
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	return num;
15378c2ecf20Sopenharmony_ci}
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_tx(struct htc_target *target,
15408c2ecf20Sopenharmony_ci			      struct htc_packet *packet)
15418c2ecf20Sopenharmony_ci{
15428c2ecf20Sopenharmony_ci	struct list_head queue;
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC,
15458c2ecf20Sopenharmony_ci		   "%s: endPointId: %d, buffer: 0x%p, length: %d\n",
15468c2ecf20Sopenharmony_ci		   __func__, packet->endpoint, packet->buf,
15478c2ecf20Sopenharmony_ci		   packet->act_len);
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&queue);
15508c2ecf20Sopenharmony_ci	list_add_tail(&packet->list, &queue);
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	return htc_send_packets_multiple(target, &queue);
15538c2ecf20Sopenharmony_ci}
15548c2ecf20Sopenharmony_ci
15558c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_wait_target(struct htc_target *target)
15568c2ecf20Sopenharmony_ci{
15578c2ecf20Sopenharmony_ci	struct htc_ready_ext_msg *ready_msg;
15588c2ecf20Sopenharmony_ci	struct htc_service_connect_req connect;
15598c2ecf20Sopenharmony_ci	struct htc_service_connect_resp resp;
15608c2ecf20Sopenharmony_ci	int status = 0;
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	status = htc_wait_recv_ctrl_message(target);
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	if (status != 0)
15658c2ecf20Sopenharmony_ci		return status;
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	if (target->pipe.ctrl_response_len < sizeof(*ready_msg)) {
15688c2ecf20Sopenharmony_ci		ath6kl_warn("invalid htc pipe ready msg len: %d\n",
15698c2ecf20Sopenharmony_ci			    target->pipe.ctrl_response_len);
15708c2ecf20Sopenharmony_ci		return -ECOMM;
15718c2ecf20Sopenharmony_ci	}
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci	ready_msg = (struct htc_ready_ext_msg *) target->pipe.ctrl_response_buf;
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	if (ready_msg->ver2_0_info.msg_id != cpu_to_le16(HTC_MSG_READY_ID)) {
15768c2ecf20Sopenharmony_ci		ath6kl_warn("invalid htc pipe ready msg: 0x%x\n",
15778c2ecf20Sopenharmony_ci			    ready_msg->ver2_0_info.msg_id);
15788c2ecf20Sopenharmony_ci		return -ECOMM;
15798c2ecf20Sopenharmony_ci	}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC,
15828c2ecf20Sopenharmony_ci		   "Target Ready! : transmit resources : %d size:%d\n",
15838c2ecf20Sopenharmony_ci		   ready_msg->ver2_0_info.cred_cnt,
15848c2ecf20Sopenharmony_ci		   ready_msg->ver2_0_info.cred_sz);
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_ci	target->tgt_creds = le16_to_cpu(ready_msg->ver2_0_info.cred_cnt);
15878c2ecf20Sopenharmony_ci	target->tgt_cred_sz = le16_to_cpu(ready_msg->ver2_0_info.cred_sz);
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	if ((target->tgt_creds == 0) || (target->tgt_cred_sz == 0))
15908c2ecf20Sopenharmony_ci		return -ECOMM;
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	htc_setup_target_buffer_assignments(target);
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci	/* setup our pseudo HTC control endpoint connection */
15958c2ecf20Sopenharmony_ci	memset(&connect, 0, sizeof(connect));
15968c2ecf20Sopenharmony_ci	memset(&resp, 0, sizeof(resp));
15978c2ecf20Sopenharmony_ci	connect.ep_cb.tx_complete = htc_txctrl_complete;
15988c2ecf20Sopenharmony_ci	connect.ep_cb.rx = htc_rxctrl_complete;
15998c2ecf20Sopenharmony_ci	connect.max_txq_depth = NUM_CONTROL_TX_BUFFERS;
16008c2ecf20Sopenharmony_ci	connect.svc_id = HTC_CTRL_RSVD_SVC;
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci	/* connect fake service */
16038c2ecf20Sopenharmony_ci	status = ath6kl_htc_pipe_conn_service(target, &connect, &resp);
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	return status;
16068c2ecf20Sopenharmony_ci}
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_cistatic void ath6kl_htc_pipe_flush_txep(struct htc_target *target,
16098c2ecf20Sopenharmony_ci				       enum htc_endpoint_id endpoint, u16 tag)
16108c2ecf20Sopenharmony_ci{
16118c2ecf20Sopenharmony_ci	struct htc_endpoint *ep = &target->endpoint[endpoint];
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	if (ep->svc_id == 0) {
16148c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
16158c2ecf20Sopenharmony_ci		/* not in use.. */
16168c2ecf20Sopenharmony_ci		return;
16178c2ecf20Sopenharmony_ci	}
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	htc_flush_tx_endpoint(target, ep, tag);
16208c2ecf20Sopenharmony_ci}
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_add_rxbuf_multiple(struct htc_target *target,
16238c2ecf20Sopenharmony_ci					      struct list_head *pkt_queue)
16248c2ecf20Sopenharmony_ci{
16258c2ecf20Sopenharmony_ci	struct htc_packet *packet, *tmp_pkt, *first;
16268c2ecf20Sopenharmony_ci	struct htc_endpoint *ep;
16278c2ecf20Sopenharmony_ci	int status = 0;
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	if (list_empty(pkt_queue))
16308c2ecf20Sopenharmony_ci		return -EINVAL;
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	first = list_first_entry(pkt_queue, struct htc_packet, list);
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	if (first->endpoint >= ENDPOINT_MAX) {
16358c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
16368c2ecf20Sopenharmony_ci		return -EINVAL;
16378c2ecf20Sopenharmony_ci	}
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	ath6kl_dbg(ATH6KL_DBG_HTC, "%s: epid: %d, cnt:%d, len: %d\n",
16408c2ecf20Sopenharmony_ci		   __func__, first->endpoint, get_queue_depth(pkt_queue),
16418c2ecf20Sopenharmony_ci		   first->buf_len);
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	ep = &target->endpoint[first->endpoint];
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_ci	spin_lock_bh(&target->rx_lock);
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	/* store receive packets */
16488c2ecf20Sopenharmony_ci	list_splice_tail_init(pkt_queue, &ep->rx_bufq);
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ci	spin_unlock_bh(&target->rx_lock);
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	if (status != 0) {
16538c2ecf20Sopenharmony_ci		/* walk through queue and mark each one canceled */
16548c2ecf20Sopenharmony_ci		list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
16558c2ecf20Sopenharmony_ci			packet->status = -ECANCELED;
16568c2ecf20Sopenharmony_ci		}
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_ci		do_recv_completion(ep, pkt_queue);
16598c2ecf20Sopenharmony_ci	}
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	return status;
16628c2ecf20Sopenharmony_ci}
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_cistatic void ath6kl_htc_pipe_activity_changed(struct htc_target *target,
16658c2ecf20Sopenharmony_ci					     enum htc_endpoint_id ep,
16668c2ecf20Sopenharmony_ci					     bool active)
16678c2ecf20Sopenharmony_ci{
16688c2ecf20Sopenharmony_ci	/* TODO */
16698c2ecf20Sopenharmony_ci}
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_cistatic void ath6kl_htc_pipe_flush_rx_buf(struct htc_target *target)
16728c2ecf20Sopenharmony_ci{
16738c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint;
16748c2ecf20Sopenharmony_ci	struct htc_packet *packet, *tmp_pkt;
16758c2ecf20Sopenharmony_ci	int i;
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
16788c2ecf20Sopenharmony_ci		endpoint = &target->endpoint[i];
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci		spin_lock_bh(&target->rx_lock);
16818c2ecf20Sopenharmony_ci
16828c2ecf20Sopenharmony_ci		list_for_each_entry_safe(packet, tmp_pkt,
16838c2ecf20Sopenharmony_ci					 &endpoint->rx_bufq, list) {
16848c2ecf20Sopenharmony_ci			list_del(&packet->list);
16858c2ecf20Sopenharmony_ci			spin_unlock_bh(&target->rx_lock);
16868c2ecf20Sopenharmony_ci			ath6kl_dbg(ATH6KL_DBG_HTC,
16878c2ecf20Sopenharmony_ci				   "htc rx flush pkt 0x%p len %d ep %d\n",
16888c2ecf20Sopenharmony_ci				   packet, packet->buf_len,
16898c2ecf20Sopenharmony_ci				   packet->endpoint);
16908c2ecf20Sopenharmony_ci			dev_kfree_skb(packet->pkt_cntxt);
16918c2ecf20Sopenharmony_ci			spin_lock_bh(&target->rx_lock);
16928c2ecf20Sopenharmony_ci		}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci		spin_unlock_bh(&target->rx_lock);
16958c2ecf20Sopenharmony_ci	}
16968c2ecf20Sopenharmony_ci}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_cistatic int ath6kl_htc_pipe_credit_setup(struct htc_target *target,
16998c2ecf20Sopenharmony_ci					struct ath6kl_htc_credit_info *info)
17008c2ecf20Sopenharmony_ci{
17018c2ecf20Sopenharmony_ci	return 0;
17028c2ecf20Sopenharmony_ci}
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_cistatic const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = {
17058c2ecf20Sopenharmony_ci	.create = ath6kl_htc_pipe_create,
17068c2ecf20Sopenharmony_ci	.wait_target = ath6kl_htc_pipe_wait_target,
17078c2ecf20Sopenharmony_ci	.start = ath6kl_htc_pipe_start,
17088c2ecf20Sopenharmony_ci	.conn_service = ath6kl_htc_pipe_conn_service,
17098c2ecf20Sopenharmony_ci	.tx = ath6kl_htc_pipe_tx,
17108c2ecf20Sopenharmony_ci	.stop = ath6kl_htc_pipe_stop,
17118c2ecf20Sopenharmony_ci	.cleanup = ath6kl_htc_pipe_cleanup,
17128c2ecf20Sopenharmony_ci	.flush_txep = ath6kl_htc_pipe_flush_txep,
17138c2ecf20Sopenharmony_ci	.flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf,
17148c2ecf20Sopenharmony_ci	.activity_changed = ath6kl_htc_pipe_activity_changed,
17158c2ecf20Sopenharmony_ci	.get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num,
17168c2ecf20Sopenharmony_ci	.add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple,
17178c2ecf20Sopenharmony_ci	.credit_setup = ath6kl_htc_pipe_credit_setup,
17188c2ecf20Sopenharmony_ci	.tx_complete = ath6kl_htc_pipe_tx_complete,
17198c2ecf20Sopenharmony_ci	.rx_complete = ath6kl_htc_pipe_rx_complete,
17208c2ecf20Sopenharmony_ci};
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_civoid ath6kl_htc_pipe_attach(struct ath6kl *ar)
17238c2ecf20Sopenharmony_ci{
17248c2ecf20Sopenharmony_ci	ar->htc_ops = &ath6kl_htc_pipe_ops;
17258c2ecf20Sopenharmony_ci}
1726