1 /*
2  * Copyright (c) 2007-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "core.h"
18 #include "debug.h"
19 #include "hif-ops.h"
20 
21 #define HTC_PACKET_CONTAINER_ALLOCATION 32
22 #define HTC_CONTROL_BUFFER_SIZE (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH)
23 
24 static int ath6kl_htc_pipe_tx(struct htc_target *handle,
25 			      struct htc_packet *packet);
26 static void ath6kl_htc_pipe_cleanup(struct htc_target *handle);
27 
28 /* htc pipe tx path */
29 static inline void restore_tx_packet(struct htc_packet *packet)
30 {
31 	if (packet->info.tx.flags & HTC_FLAGS_TX_FIXUP_NETBUF) {
32 		skb_pull(packet->skb, sizeof(struct htc_frame_hdr));
33 		packet->info.tx.flags &= ~HTC_FLAGS_TX_FIXUP_NETBUF;
34 	}
35 }
36 
37 static void do_send_completion(struct htc_endpoint *ep,
38 			       struct list_head *queue_to_indicate)
39 {
40 	struct htc_packet *packet;
41 
42 	if (list_empty(queue_to_indicate)) {
43 		/* nothing to indicate */
44 		return;
45 	}
46 
47 	if (ep->ep_cb.tx_comp_multi != NULL) {
48 		ath6kl_dbg(ATH6KL_DBG_HTC,
49 			   "%s: calling ep %d, send complete multiple callback (%d pkts)\n",
50 			   __func__, ep->eid,
51 			   get_queue_depth(queue_to_indicate));
52 		/*
53 		 * a multiple send complete handler is being used,
54 		 * pass the queue to the handler
55 		 */
56 		ep->ep_cb.tx_comp_multi(ep->target, queue_to_indicate);
57 		/*
58 		 * all packets are now owned by the callback,
59 		 * reset queue to be safe
60 		 */
61 		INIT_LIST_HEAD(queue_to_indicate);
62 	} else {
63 		/* using legacy EpTxComplete */
64 		do {
65 			packet = list_first_entry(queue_to_indicate,
66 						  struct htc_packet, list);
67 
68 			list_del(&packet->list);
69 			ath6kl_dbg(ATH6KL_DBG_HTC,
70 				   "%s: calling ep %d send complete callback on packet 0x%p\n",
71 				   __func__, ep->eid, packet);
72 			ep->ep_cb.tx_complete(ep->target, packet);
73 		} while (!list_empty(queue_to_indicate));
74 	}
75 }
76 
77 static void send_packet_completion(struct htc_target *target,
78 				   struct htc_packet *packet)
79 {
80 	struct htc_endpoint *ep = &target->endpoint[packet->endpoint];
81 	struct list_head container;
82 
83 	restore_tx_packet(packet);
84 	INIT_LIST_HEAD(&container);
85 	list_add_tail(&packet->list, &container);
86 
87 	/* do completion */
88 	do_send_completion(ep, &container);
89 }
90 
91 static void get_htc_packet_credit_based(struct htc_target *target,
92 					struct htc_endpoint *ep,
93 					struct list_head *queue)
94 {
95 	int credits_required;
96 	int remainder;
97 	u8 send_flags;
98 	struct htc_packet *packet;
99 	unsigned int transfer_len;
100 
101 	/* NOTE : the TX lock is held when this function is called */
102 
103 	/* loop until we can grab as many packets out of the queue as we can */
104 	while (true) {
105 		send_flags = 0;
106 		if (list_empty(&ep->txq))
107 			break;
108 
109 		/* get packet at head, but don't remove it */
110 		packet = list_first_entry(&ep->txq, struct htc_packet, list);
111 
112 		ath6kl_dbg(ATH6KL_DBG_HTC,
113 			   "%s: got head packet:0x%p , queue depth: %d\n",
114 			   __func__, packet, get_queue_depth(&ep->txq));
115 
116 		transfer_len = packet->act_len + HTC_HDR_LENGTH;
117 
118 		if (transfer_len <= target->tgt_cred_sz) {
119 			credits_required = 1;
120 		} else {
121 			/* figure out how many credits this message requires */
122 			credits_required = transfer_len / target->tgt_cred_sz;
123 			remainder = transfer_len % target->tgt_cred_sz;
124 
125 			if (remainder)
126 				credits_required++;
127 		}
128 
129 		ath6kl_dbg(ATH6KL_DBG_HTC, "%s: creds required:%d got:%d\n",
130 			   __func__, credits_required, ep->cred_dist.credits);
131 
132 		if (ep->eid == ENDPOINT_0) {
133 			/*
134 			 * endpoint 0 is special, it always has a credit and
135 			 * does not require credit based flow control
136 			 */
137 			credits_required = 0;
138 
139 		} else {
140 
141 			if (ep->cred_dist.credits < credits_required)
142 				break;
143 
144 			ep->cred_dist.credits -= credits_required;
145 			ep->ep_st.cred_cosumd += credits_required;
146 
147 			/* check if we need credits back from the target */
148 			if (ep->cred_dist.credits <
149 					ep->cred_dist.cred_per_msg) {
150 				/* tell the target we need credits ASAP! */
151 				send_flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
152 				ep->ep_st.cred_low_indicate += 1;
153 				ath6kl_dbg(ATH6KL_DBG_HTC,
154 					   "%s: host needs credits\n",
155 					   __func__);
156 			}
157 		}
158 
159 		/* now we can fully dequeue */
160 		packet = list_first_entry(&ep->txq, struct htc_packet, list);
161 
162 		list_del(&packet->list);
163 		/* save the number of credits this packet consumed */
164 		packet->info.tx.cred_used = credits_required;
165 		/* save send flags */
166 		packet->info.tx.flags = send_flags;
167 		packet->info.tx.seqno = ep->seqno;
168 		ep->seqno++;
169 		/* queue this packet into the caller's queue */
170 		list_add_tail(&packet->list, queue);
171 	}
172 
173 }
174 
175 static void get_htc_packet(struct htc_target *target,
176 			   struct htc_endpoint *ep,
177 			   struct list_head *queue, int resources)
178 {
179 	struct htc_packet *packet;
180 
181 	/* NOTE : the TX lock is held when this function is called */
182 
183 	/* loop until we can grab as many packets out of the queue as we can */
184 	while (resources) {
185 		if (list_empty(&ep->txq))
186 			break;
187 
188 		packet = list_first_entry(&ep->txq, struct htc_packet, list);
189 		list_del(&packet->list);
190 
191 		ath6kl_dbg(ATH6KL_DBG_HTC,
192 			   "%s: got packet:0x%p , new queue depth: %d\n",
193 			   __func__, packet, get_queue_depth(&ep->txq));
194 		packet->info.tx.seqno = ep->seqno;
195 		packet->info.tx.flags = 0;
196 		packet->info.tx.cred_used = 0;
197 		ep->seqno++;
198 
199 		/* queue this packet into the caller's queue */
200 		list_add_tail(&packet->list, queue);
201 		resources--;
202 	}
203 }
204 
205 static int htc_issue_packets(struct htc_target *target,
206 			     struct htc_endpoint *ep,
207 			     struct list_head *pkt_queue)
208 {
209 	int status = 0;
210 	u16 payload_len;
211 	struct sk_buff *skb;
212 	struct htc_frame_hdr *htc_hdr;
213 	struct htc_packet *packet;
214 
215 	ath6kl_dbg(ATH6KL_DBG_HTC,
216 		   "%s: queue: 0x%p, pkts %d\n", __func__,
217 		   pkt_queue, get_queue_depth(pkt_queue));
218 
219 	while (!list_empty(pkt_queue)) {
220 		packet = list_first_entry(pkt_queue, struct htc_packet, list);
221 		list_del(&packet->list);
222 
223 		skb = packet->skb;
224 		if (!skb) {
225 			WARN_ON_ONCE(1);
226 			status = -EINVAL;
227 			break;
228 		}
229 
230 		payload_len = packet->act_len;
231 
232 		/* setup HTC frame header */
233 		htc_hdr = (struct htc_frame_hdr *) skb_push(skb,
234 							    sizeof(*htc_hdr));
235 		if (!htc_hdr) {
236 			WARN_ON_ONCE(1);
237 			status = -EINVAL;
238 			break;
239 		}
240 
241 		packet->info.tx.flags |= HTC_FLAGS_TX_FIXUP_NETBUF;
242 
243 		/* Endianess? */
244 		put_unaligned((u16) payload_len, &htc_hdr->payld_len);
245 		htc_hdr->flags = packet->info.tx.flags;
246 		htc_hdr->eid = (u8) packet->endpoint;
247 		htc_hdr->ctrl[0] = 0;
248 		htc_hdr->ctrl[1] = (u8) packet->info.tx.seqno;
249 
250 		spin_lock_bh(&target->tx_lock);
251 
252 		/* store in look up queue to match completions */
253 		list_add_tail(&packet->list, &ep->pipe.tx_lookup_queue);
254 		ep->ep_st.tx_issued += 1;
255 		spin_unlock_bh(&target->tx_lock);
256 
257 		status = ath6kl_hif_pipe_send(target->dev->ar,
258 					      ep->pipe.pipeid_ul, NULL, skb);
259 
260 		if (status != 0) {
261 			if (status != -ENOMEM) {
262 				/* TODO: if more than 1 endpoint maps to the
263 				 * same PipeID, it is possible to run out of
264 				 * resources in the HIF layer.
265 				 * Don't emit the error
266 				 */
267 				ath6kl_dbg(ATH6KL_DBG_HTC,
268 					   "%s: failed status:%d\n",
269 					   __func__, status);
270 			}
271 			spin_lock_bh(&target->tx_lock);
272 			list_del(&packet->list);
273 
274 			/* reclaim credits */
275 			ep->cred_dist.credits += packet->info.tx.cred_used;
276 			spin_unlock_bh(&target->tx_lock);
277 
278 			/* put it back into the callers queue */
279 			list_add(&packet->list, pkt_queue);
280 			break;
281 		}
282 
283 	}
284 
285 	if (status != 0) {
286 		while (!list_empty(pkt_queue)) {
287 			if (status != -ENOMEM) {
288 				ath6kl_dbg(ATH6KL_DBG_HTC,
289 					   "%s: failed pkt:0x%p status:%d\n",
290 					   __func__, packet, status);
291 			}
292 
293 			packet = list_first_entry(pkt_queue,
294 						  struct htc_packet, list);
295 			list_del(&packet->list);
296 			packet->status = status;
297 			send_packet_completion(target, packet);
298 		}
299 	}
300 
301 	return status;
302 }
303 
304 static enum htc_send_queue_result htc_try_send(struct htc_target *target,
305 					       struct htc_endpoint *ep,
306 					       struct list_head *txq)
307 {
308 	struct list_head send_queue;	/* temp queue to hold packets */
309 	struct htc_packet *packet, *tmp_pkt;
310 	struct ath6kl *ar = target->dev->ar;
311 	enum htc_send_full_action action;
312 	int tx_resources, overflow, txqueue_depth, i, good_pkts;
313 	u8 pipeid;
314 
315 	ath6kl_dbg(ATH6KL_DBG_HTC, "%s: (queue:0x%p depth:%d)\n",
316 		   __func__, txq,
317 		   (txq == NULL) ? 0 : get_queue_depth(txq));
318 
319 	/* init the local send queue */
320 	INIT_LIST_HEAD(&send_queue);
321 
322 	/*
323 	 * txq equals to NULL means
324 	 * caller didn't provide a queue, just wants us to
325 	 * check queues and send
326 	 */
327 	if (txq != NULL) {
328 		if (list_empty(txq)) {
329 			/* empty queue */
330 			return HTC_SEND_QUEUE_DROP;
331 		}
332 
333 		spin_lock_bh(&target->tx_lock);
334 		txqueue_depth = get_queue_depth(&ep->txq);
335 		spin_unlock_bh(&target->tx_lock);
336 
337 		if (txqueue_depth >= ep->max_txq_depth) {
338 			/* we've already overflowed */
339 			overflow = get_queue_depth(txq);
340 		} else {
341 			/* get how much we will overflow by */
342 			overflow = txqueue_depth;
343 			overflow += get_queue_depth(txq);
344 			/* get how much we will overflow the TX queue by */
345 			overflow -= ep->max_txq_depth;
346 		}
347 
348 		/* if overflow is negative or zero, we are okay */
349 		if (overflow > 0) {
350 			ath6kl_dbg(ATH6KL_DBG_HTC,
351 				   "%s: Endpoint %d, TX queue will overflow :%d, Tx Depth:%d, Max:%d\n",
352 				   __func__, ep->eid, overflow, txqueue_depth,
353 				   ep->max_txq_depth);
354 		}
355 		if ((overflow <= 0) ||
356 		    (ep->ep_cb.tx_full == NULL)) {
357 			/*
358 			 * all packets will fit or caller did not provide send
359 			 * full indication handler -- just move all of them
360 			 * to the local send_queue object
361 			 */
362 			list_splice_tail_init(txq, &send_queue);
363 		} else {
364 			good_pkts = get_queue_depth(txq) - overflow;
365 			if (good_pkts < 0) {
366 				WARN_ON_ONCE(1);
367 				return HTC_SEND_QUEUE_DROP;
368 			}
369 
370 			/* we have overflowed, and a callback is provided */
371 			/* dequeue all non-overflow packets to the sendqueue */
372 			for (i = 0; i < good_pkts; i++) {
373 				/* pop off caller's queue */
374 				packet = list_first_entry(txq,
375 							  struct htc_packet,
376 							  list);
377 				/* move to local queue */
378 				list_move_tail(&packet->list, &send_queue);
379 			}
380 
381 			/*
382 			 * the caller's queue has all the packets that won't fit
383 			 * walk through the caller's queue and indicate each to
384 			 * the send full handler
385 			 */
386 			list_for_each_entry_safe(packet, tmp_pkt,
387 						 txq, list) {
388 
389 				ath6kl_dbg(ATH6KL_DBG_HTC,
390 					   "%s: Indicat overflowed TX pkts: %p\n",
391 					   __func__, packet);
392 				action = ep->ep_cb.tx_full(ep->target, packet);
393 				if (action == HTC_SEND_FULL_DROP) {
394 					/* callback wants the packet dropped */
395 					ep->ep_st.tx_dropped += 1;
396 
397 					/* leave this one in the caller's queue
398 					 * for cleanup */
399 				} else {
400 					/* callback wants to keep this packet,
401 					 * move from caller's queue to the send
402 					 * queue */
403 					list_move_tail(&packet->list,
404 						       &send_queue);
405 				}
406 
407 			}
408 
409 			if (list_empty(&send_queue)) {
410 				/* no packets made it in, caller will cleanup */
411 				return HTC_SEND_QUEUE_DROP;
412 			}
413 		}
414 	}
415 
416 	if (!ep->pipe.tx_credit_flow_enabled) {
417 		tx_resources =
418 		    ath6kl_hif_pipe_get_free_queue_number(ar,
419 							  ep->pipe.pipeid_ul);
420 	} else {
421 		tx_resources = 0;
422 	}
423 
424 	spin_lock_bh(&target->tx_lock);
425 	if (!list_empty(&send_queue)) {
426 		/* transfer packets to tail */
427 		list_splice_tail_init(&send_queue, &ep->txq);
428 		if (!list_empty(&send_queue)) {
429 			WARN_ON_ONCE(1);
430 			spin_unlock_bh(&target->tx_lock);
431 			return HTC_SEND_QUEUE_DROP;
432 		}
433 		INIT_LIST_HEAD(&send_queue);
434 	}
435 
436 	/* increment tx processing count on entry */
437 	ep->tx_proc_cnt++;
438 
439 	if (ep->tx_proc_cnt > 1) {
440 		/*
441 		 * Another thread or task is draining the TX queues on this
442 		 * endpoint that thread will reset the tx processing count
443 		 * when the queue is drained.
444 		 */
445 		ep->tx_proc_cnt--;
446 		spin_unlock_bh(&target->tx_lock);
447 		return HTC_SEND_QUEUE_OK;
448 	}
449 
450 	/***** beyond this point only 1 thread may enter ******/
451 
452 	/*
453 	 * Now drain the endpoint TX queue for transmission as long as we have
454 	 * enough transmit resources.
455 	 */
456 	while (true) {
457 
458 		if (get_queue_depth(&ep->txq) == 0)
459 			break;
460 
461 		if (ep->pipe.tx_credit_flow_enabled) {
462 			/*
463 			 * Credit based mechanism provides flow control
464 			 * based on target transmit resource availability,
465 			 * we assume that the HIF layer will always have
466 			 * bus resources greater than target transmit
467 			 * resources.
468 			 */
469 			get_htc_packet_credit_based(target, ep, &send_queue);
470 		} else {
471 			/*
472 			 * Get all packets for this endpoint that we can
473 			 * for this pass.
474 			 */
475 			get_htc_packet(target, ep, &send_queue, tx_resources);
476 		}
477 
478 		if (get_queue_depth(&send_queue) == 0) {
479 			/*
480 			 * Didn't get packets due to out of resources or TX
481 			 * queue was drained.
482 			 */
483 			break;
484 		}
485 
486 		spin_unlock_bh(&target->tx_lock);
487 
488 		/* send what we can */
489 		htc_issue_packets(target, ep, &send_queue);
490 
491 		if (!ep->pipe.tx_credit_flow_enabled) {
492 			pipeid = ep->pipe.pipeid_ul;
493 			tx_resources =
494 			    ath6kl_hif_pipe_get_free_queue_number(ar, pipeid);
495 		}
496 
497 		spin_lock_bh(&target->tx_lock);
498 
499 	}
500 	/* done with this endpoint, we can clear the count */
501 	ep->tx_proc_cnt = 0;
502 	spin_unlock_bh(&target->tx_lock);
503 
504 	return HTC_SEND_QUEUE_OK;
505 }
506 
507 /* htc control packet manipulation */
508 static void destroy_htc_txctrl_packet(struct htc_packet *packet)
509 {
510 	struct sk_buff *skb;
511 	skb = packet->skb;
512 	dev_kfree_skb(skb);
513 	kfree(packet);
514 }
515 
516 static struct htc_packet *build_htc_txctrl_packet(void)
517 {
518 	struct htc_packet *packet = NULL;
519 	struct sk_buff *skb;
520 
521 	packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
522 	if (packet == NULL)
523 		return NULL;
524 
525 	skb = __dev_alloc_skb(HTC_CONTROL_BUFFER_SIZE, GFP_KERNEL);
526 
527 	if (skb == NULL) {
528 		kfree(packet);
529 		return NULL;
530 	}
531 	packet->skb = skb;
532 
533 	return packet;
534 }
535 
536 static void htc_free_txctrl_packet(struct htc_target *target,
537 				   struct htc_packet *packet)
538 {
539 	destroy_htc_txctrl_packet(packet);
540 }
541 
542 static struct htc_packet *htc_alloc_txctrl_packet(struct htc_target *target)
543 {
544 	return build_htc_txctrl_packet();
545 }
546 
547 static void htc_txctrl_complete(struct htc_target *target,
548 				struct htc_packet *packet)
549 {
550 	htc_free_txctrl_packet(target, packet);
551 }
552 
553 #define MAX_MESSAGE_SIZE 1536
554 
555 static int htc_setup_target_buffer_assignments(struct htc_target *target)
556 {
557 	int status, credits, credit_per_maxmsg, i;
558 	struct htc_pipe_txcredit_alloc *entry;
559 	unsigned int hif_usbaudioclass = 0;
560 
561 	credit_per_maxmsg = MAX_MESSAGE_SIZE / target->tgt_cred_sz;
562 	if (MAX_MESSAGE_SIZE % target->tgt_cred_sz)
563 		credit_per_maxmsg++;
564 
565 	/* TODO, this should be configured by the caller! */
566 
567 	credits = target->tgt_creds;
568 	entry = &target->pipe.txcredit_alloc[0];
569 
570 	status = -ENOMEM;
571 
572 	/* FIXME: hif_usbaudioclass is always zero */
573 	if (hif_usbaudioclass) {
574 		ath6kl_dbg(ATH6KL_DBG_HTC,
575 			   "%s: For USB Audio Class- Total:%d\n",
576 			   __func__, credits);
577 		entry++;
578 		entry++;
579 		/* Setup VO Service To have Max Credits */
580 		entry->service_id = WMI_DATA_VO_SVC;
581 		entry->credit_alloc = (credits - 6);
582 		if (entry->credit_alloc == 0)
583 			entry->credit_alloc++;
584 
585 		credits -= (int) entry->credit_alloc;
586 		if (credits <= 0)
587 			return status;
588 
589 		entry++;
590 		entry->service_id = WMI_CONTROL_SVC;
591 		entry->credit_alloc = credit_per_maxmsg;
592 		credits -= (int) entry->credit_alloc;
593 		if (credits <= 0)
594 			return status;
595 
596 		/* leftovers go to best effort */
597 		entry++;
598 		entry++;
599 		entry->service_id = WMI_DATA_BE_SVC;
600 		entry->credit_alloc = (u8) credits;
601 		status = 0;
602 	} else {
603 		entry++;
604 		entry->service_id = WMI_DATA_VI_SVC;
605 		entry->credit_alloc = credits / 4;
606 		if (entry->credit_alloc == 0)
607 			entry->credit_alloc++;
608 
609 		credits -= (int) entry->credit_alloc;
610 		if (credits <= 0)
611 			return status;
612 
613 		entry++;
614 		entry->service_id = WMI_DATA_VO_SVC;
615 		entry->credit_alloc = credits / 4;
616 		if (entry->credit_alloc == 0)
617 			entry->credit_alloc++;
618 
619 		credits -= (int) entry->credit_alloc;
620 		if (credits <= 0)
621 			return status;
622 
623 		entry++;
624 		entry->service_id = WMI_CONTROL_SVC;
625 		entry->credit_alloc = credit_per_maxmsg;
626 		credits -= (int) entry->credit_alloc;
627 		if (credits <= 0)
628 			return status;
629 
630 		entry++;
631 		entry->service_id = WMI_DATA_BK_SVC;
632 		entry->credit_alloc = credit_per_maxmsg;
633 		credits -= (int) entry->credit_alloc;
634 		if (credits <= 0)
635 			return status;
636 
637 		/* leftovers go to best effort */
638 		entry++;
639 		entry->service_id = WMI_DATA_BE_SVC;
640 		entry->credit_alloc = (u8) credits;
641 		status = 0;
642 	}
643 
644 	if (status == 0) {
645 		for (i = 0; i < ENDPOINT_MAX; i++) {
646 			if (target->pipe.txcredit_alloc[i].service_id != 0) {
647 				ath6kl_dbg(ATH6KL_DBG_HTC,
648 					   "HTC Service Index : %d TX : 0x%2.2X : alloc:%d\n",
649 					   i,
650 					   target->pipe.txcredit_alloc[i].
651 					   service_id,
652 					   target->pipe.txcredit_alloc[i].
653 					   credit_alloc);
654 			}
655 		}
656 	}
657 	return status;
658 }
659 
660 /* process credit reports and call distribution function */
661 static void htc_process_credit_report(struct htc_target *target,
662 				      struct htc_credit_report *rpt,
663 				      int num_entries,
664 				      enum htc_endpoint_id from_ep)
665 {
666 	int total_credits = 0, i;
667 	struct htc_endpoint *ep;
668 
669 	/* lock out TX while we update credits */
670 	spin_lock_bh(&target->tx_lock);
671 
672 	for (i = 0; i < num_entries; i++, rpt++) {
673 		if (rpt->eid >= ENDPOINT_MAX) {
674 			WARN_ON_ONCE(1);
675 			spin_unlock_bh(&target->tx_lock);
676 			return;
677 		}
678 
679 		ep = &target->endpoint[rpt->eid];
680 		ep->cred_dist.credits += rpt->credits;
681 
682 		if (ep->cred_dist.credits && get_queue_depth(&ep->txq)) {
683 			spin_unlock_bh(&target->tx_lock);
684 			htc_try_send(target, ep, NULL);
685 			spin_lock_bh(&target->tx_lock);
686 		}
687 
688 		total_credits += rpt->credits;
689 	}
690 	ath6kl_dbg(ATH6KL_DBG_HTC,
691 		   "Report indicated %d credits to distribute\n",
692 		   total_credits);
693 
694 	spin_unlock_bh(&target->tx_lock);
695 }
696 
697 /* flush endpoint TX queue */
698 static void htc_flush_tx_endpoint(struct htc_target *target,
699 				  struct htc_endpoint *ep, u16 tag)
700 {
701 	struct htc_packet *packet;
702 
703 	spin_lock_bh(&target->tx_lock);
704 	while (get_queue_depth(&ep->txq)) {
705 		packet = list_first_entry(&ep->txq, struct htc_packet, list);
706 		list_del(&packet->list);
707 		packet->status = 0;
708 		send_packet_completion(target, packet);
709 	}
710 	spin_unlock_bh(&target->tx_lock);
711 }
712 
713 /*
714  * In the adapted HIF layer, struct sk_buff * are passed between HIF and HTC,
715  * since upper layers expects struct htc_packet containers we use the completed
716  * skb and lookup it's corresponding HTC packet buffer from a lookup list.
717  * This is extra overhead that can be fixed by re-aligning HIF interfaces with
718  * HTC.
719  */
720 static struct htc_packet *htc_lookup_tx_packet(struct htc_target *target,
721 					       struct htc_endpoint *ep,
722 					       struct sk_buff *skb)
723 {
724 	struct htc_packet *packet, *tmp_pkt, *found_packet = NULL;
725 
726 	spin_lock_bh(&target->tx_lock);
727 
728 	/*
729 	 * interate from the front of tx lookup queue
730 	 * this lookup should be fast since lower layers completes in-order and
731 	 * so the completed packet should be at the head of the list generally
732 	 */
733 	list_for_each_entry_safe(packet, tmp_pkt, &ep->pipe.tx_lookup_queue,
734 				 list) {
735 		/* check for removal */
736 		if (skb == packet->skb) {
737 			/* found it */
738 			list_del(&packet->list);
739 			found_packet = packet;
740 			break;
741 		}
742 	}
743 
744 	spin_unlock_bh(&target->tx_lock);
745 
746 	return found_packet;
747 }
748 
749 static int ath6kl_htc_pipe_tx_complete(struct ath6kl *ar, struct sk_buff *skb)
750 {
751 	struct htc_target *target = ar->htc_target;
752 	struct htc_frame_hdr *htc_hdr;
753 	struct htc_endpoint *ep;
754 	struct htc_packet *packet;
755 	u8 ep_id, *netdata;
756 	u32 netlen;
757 
758 	netdata = skb->data;
759 	netlen = skb->len;
760 
761 	htc_hdr = (struct htc_frame_hdr *) netdata;
762 
763 	ep_id = htc_hdr->eid;
764 	ep = &target->endpoint[ep_id];
765 
766 	packet = htc_lookup_tx_packet(target, ep, skb);
767 	if (packet == NULL) {
768 		/* may have already been flushed and freed */
769 		ath6kl_err("HTC TX lookup failed!\n");
770 	} else {
771 		/* will be giving this buffer back to upper layers */
772 		packet->status = 0;
773 		send_packet_completion(target, packet);
774 	}
775 	skb = NULL;
776 
777 	if (!ep->pipe.tx_credit_flow_enabled) {
778 		/*
779 		 * note: when using TX credit flow, the re-checking of queues
780 		 * happens when credits flow back from the target. in the
781 		 * non-TX credit case, we recheck after the packet completes
782 		 */
783 		htc_try_send(target, ep, NULL);
784 	}
785 
786 	return 0;
787 }
788 
789 static int htc_send_packets_multiple(struct htc_target *target,
790 				     struct list_head *pkt_queue)
791 {
792 	struct htc_endpoint *ep;
793 	struct htc_packet *packet, *tmp_pkt;
794 
795 	if (list_empty(pkt_queue))
796 		return -EINVAL;
797 
798 	/* get first packet to find out which ep the packets will go into */
799 	packet = list_first_entry(pkt_queue, struct htc_packet, list);
800 
801 	if (packet->endpoint >= ENDPOINT_MAX) {
802 		WARN_ON_ONCE(1);
803 		return -EINVAL;
804 	}
805 	ep = &target->endpoint[packet->endpoint];
806 
807 	htc_try_send(target, ep, pkt_queue);
808 
809 	/* do completion on any packets that couldn't get in */
810 	if (!list_empty(pkt_queue)) {
811 		list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
812 			packet->status = -ENOMEM;
813 		}
814 
815 		do_send_completion(ep, pkt_queue);
816 	}
817 
818 	return 0;
819 }
820 
821 /* htc pipe rx path */
822 static struct htc_packet *alloc_htc_packet_container(struct htc_target *target)
823 {
824 	struct htc_packet *packet;
825 	spin_lock_bh(&target->rx_lock);
826 
827 	if (target->pipe.htc_packet_pool == NULL) {
828 		spin_unlock_bh(&target->rx_lock);
829 		return NULL;
830 	}
831 
832 	packet = target->pipe.htc_packet_pool;
833 	target->pipe.htc_packet_pool = (struct htc_packet *) packet->list.next;
834 
835 	spin_unlock_bh(&target->rx_lock);
836 
837 	packet->list.next = NULL;
838 	return packet;
839 }
840 
841 static void free_htc_packet_container(struct htc_target *target,
842 				      struct htc_packet *packet)
843 {
844 	struct list_head *lh;
845 
846 	spin_lock_bh(&target->rx_lock);
847 
848 	if (target->pipe.htc_packet_pool == NULL) {
849 		target->pipe.htc_packet_pool = packet;
850 		packet->list.next = NULL;
851 	} else {
852 		lh = (struct list_head *) target->pipe.htc_packet_pool;
853 		packet->list.next = lh;
854 		target->pipe.htc_packet_pool = packet;
855 	}
856 
857 	spin_unlock_bh(&target->rx_lock);
858 }
859 
860 static int htc_process_trailer(struct htc_target *target, u8 *buffer,
861 			       int len, enum htc_endpoint_id from_ep)
862 {
863 	struct htc_credit_report *report;
864 	struct htc_record_hdr *record;
865 	u8 *record_buf, *orig_buf;
866 	int orig_len, status;
867 
868 	orig_buf = buffer;
869 	orig_len = len;
870 	status = 0;
871 
872 	while (len > 0) {
873 		if (len < sizeof(struct htc_record_hdr)) {
874 			status = -EINVAL;
875 			break;
876 		}
877 
878 		/* these are byte aligned structs */
879 		record = (struct htc_record_hdr *) buffer;
880 		len -= sizeof(struct htc_record_hdr);
881 		buffer += sizeof(struct htc_record_hdr);
882 
883 		if (record->len > len) {
884 			/* no room left in buffer for record */
885 			ath6kl_dbg(ATH6KL_DBG_HTC,
886 				   "invalid length: %d (id:%d) buffer has: %d bytes left\n",
887 				   record->len, record->rec_id, len);
888 			status = -EINVAL;
889 			break;
890 		}
891 
892 		/* start of record follows the header */
893 		record_buf = buffer;
894 
895 		switch (record->rec_id) {
896 		case HTC_RECORD_CREDITS:
897 			if (record->len < sizeof(struct htc_credit_report)) {
898 				WARN_ON_ONCE(1);
899 				return -EINVAL;
900 			}
901 
902 			report = (struct htc_credit_report *) record_buf;
903 			htc_process_credit_report(target, report,
904 						  record->len / sizeof(*report),
905 						  from_ep);
906 			break;
907 		default:
908 			ath6kl_dbg(ATH6KL_DBG_HTC,
909 				   "unhandled record: id:%d length:%d\n",
910 				   record->rec_id, record->len);
911 			break;
912 		}
913 
914 		if (status != 0)
915 			break;
916 
917 		/* advance buffer past this record for next time around */
918 		buffer += record->len;
919 		len -= record->len;
920 	}
921 
922 	return status;
923 }
924 
925 static void do_recv_completion(struct htc_endpoint *ep,
926 			       struct list_head *queue_to_indicate)
927 {
928 	struct htc_packet *packet;
929 
930 	if (list_empty(queue_to_indicate)) {
931 		/* nothing to indicate */
932 		return;
933 	}
934 
935 	/* using legacy EpRecv */
936 	while (!list_empty(queue_to_indicate)) {
937 		packet = list_first_entry(queue_to_indicate,
938 					  struct htc_packet, list);
939 		list_del(&packet->list);
940 		ep->ep_cb.rx(ep->target, packet);
941 	}
942 
943 	return;
944 }
945 
946 static void recv_packet_completion(struct htc_target *target,
947 				   struct htc_endpoint *ep,
948 				   struct htc_packet *packet)
949 {
950 	struct list_head container;
951 	INIT_LIST_HEAD(&container);
952 	list_add_tail(&packet->list, &container);
953 
954 	/* do completion */
955 	do_recv_completion(ep, &container);
956 }
957 
958 static int ath6kl_htc_pipe_rx_complete(struct ath6kl *ar, struct sk_buff *skb,
959 				       u8 pipeid)
960 {
961 	struct htc_target *target = ar->htc_target;
962 	u8 *netdata, *trailer, hdr_info;
963 	struct htc_frame_hdr *htc_hdr;
964 	u32 netlen, trailerlen = 0;
965 	struct htc_packet *packet;
966 	struct htc_endpoint *ep;
967 	u16 payload_len;
968 	int status = 0;
969 
970 	netdata = skb->data;
971 	netlen = skb->len;
972 
973 	htc_hdr = (struct htc_frame_hdr *) netdata;
974 
975 	ep = &target->endpoint[htc_hdr->eid];
976 
977 	if (htc_hdr->eid >= ENDPOINT_MAX) {
978 		ath6kl_dbg(ATH6KL_DBG_HTC,
979 			   "HTC Rx: invalid EndpointID=%d\n",
980 			   htc_hdr->eid);
981 		status = -EINVAL;
982 		goto free_skb;
983 	}
984 
985 	payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));
986 
987 	if (netlen < (payload_len + HTC_HDR_LENGTH)) {
988 		ath6kl_dbg(ATH6KL_DBG_HTC,
989 			   "HTC Rx: insufficient length, got:%d expected =%u\n",
990 			   netlen, payload_len + HTC_HDR_LENGTH);
991 		status = -EINVAL;
992 		goto free_skb;
993 	}
994 
995 	/* get flags to check for trailer */
996 	hdr_info = htc_hdr->flags;
997 	if (hdr_info & HTC_FLG_RX_TRAILER) {
998 		/* extract the trailer length */
999 		hdr_info = htc_hdr->ctrl[0];
1000 		if ((hdr_info < sizeof(struct htc_record_hdr)) ||
1001 		    (hdr_info > payload_len)) {
1002 			ath6kl_dbg(ATH6KL_DBG_HTC,
1003 				   "invalid header: payloadlen should be %d, CB[0]: %d\n",
1004 				   payload_len, hdr_info);
1005 			status = -EINVAL;
1006 			goto free_skb;
1007 		}
1008 
1009 		trailerlen = hdr_info;
1010 		/* process trailer after hdr/apps payload */
1011 		trailer = (u8 *) htc_hdr + HTC_HDR_LENGTH +
1012 			payload_len - hdr_info;
1013 		status = htc_process_trailer(target, trailer, hdr_info,
1014 					     htc_hdr->eid);
1015 		if (status != 0)
1016 			goto free_skb;
1017 	}
1018 
1019 	if (((int) payload_len - (int) trailerlen) <= 0) {
1020 		/* zero length packet with trailer, just drop these */
1021 		goto free_skb;
1022 	}
1023 
1024 	if (htc_hdr->eid == ENDPOINT_0) {
1025 		/* handle HTC control message */
1026 		if (target->htc_flags & HTC_OP_STATE_SETUP_COMPLETE) {
1027 			/*
1028 			 * fatal: target should not send unsolicited
1029 			 * messageson the endpoint 0
1030 			 */
1031 			ath6kl_dbg(ATH6KL_DBG_HTC,
1032 				   "HTC ignores Rx Ctrl after setup complete\n");
1033 			status = -EINVAL;
1034 			goto free_skb;
1035 		}
1036 
1037 		/* remove HTC header */
1038 		skb_pull(skb, HTC_HDR_LENGTH);
1039 
1040 		netdata = skb->data;
1041 		netlen = skb->len;
1042 
1043 		spin_lock_bh(&target->rx_lock);
1044 
1045 		target->pipe.ctrl_response_valid = true;
1046 		target->pipe.ctrl_response_len = min_t(int, netlen,
1047 						       HTC_MAX_CTRL_MSG_LEN);
1048 		memcpy(target->pipe.ctrl_response_buf, netdata,
1049 		       target->pipe.ctrl_response_len);
1050 
1051 		spin_unlock_bh(&target->rx_lock);
1052 
1053 		dev_kfree_skb(skb);
1054 		skb = NULL;
1055 
1056 		goto free_skb;
1057 	}
1058 
1059 	/*
1060 	 * TODO: the message based HIF architecture allocates net bufs
1061 	 * for recv packets since it bridges that HIF to upper layers,
1062 	 * which expects HTC packets, we form the packets here
1063 	 */
1064 	packet = alloc_htc_packet_container(target);
1065 	if (packet == NULL) {
1066 		status = -ENOMEM;
1067 		goto free_skb;
1068 	}
1069 
1070 	packet->status = 0;
1071 	packet->endpoint = htc_hdr->eid;
1072 	packet->pkt_cntxt = skb;
1073 
1074 	/* TODO: for backwards compatibility */
1075 	packet->buf = skb_push(skb, 0) + HTC_HDR_LENGTH;
1076 	packet->act_len = netlen - HTC_HDR_LENGTH - trailerlen;
1077 
1078 	/*
1079 	 * TODO: this is a hack because the driver layer will set the
1080 	 * actual len of the skb again which will just double the len
1081 	 */
1082 	skb_trim(skb, 0);
1083 
1084 	recv_packet_completion(target, ep, packet);
1085 
1086 	/* recover the packet container */
1087 	free_htc_packet_container(target, packet);
1088 	skb = NULL;
1089 
1090 free_skb:
1091 	dev_kfree_skb(skb);
1092 
1093 	return status;
1094 
1095 }
1096 
1097 static void htc_flush_rx_queue(struct htc_target *target,
1098 			       struct htc_endpoint *ep)
1099 {
1100 	struct list_head container;
1101 	struct htc_packet *packet;
1102 
1103 	spin_lock_bh(&target->rx_lock);
1104 
1105 	while (1) {
1106 		if (list_empty(&ep->rx_bufq))
1107 			break;
1108 
1109 		packet = list_first_entry(&ep->rx_bufq,
1110 					  struct htc_packet, list);
1111 		list_del(&packet->list);
1112 
1113 		spin_unlock_bh(&target->rx_lock);
1114 		packet->status = -ECANCELED;
1115 		packet->act_len = 0;
1116 
1117 		ath6kl_dbg(ATH6KL_DBG_HTC,
1118 			   "Flushing RX packet:0x%p, length:%d, ep:%d\n",
1119 			   packet, packet->buf_len,
1120 			   packet->endpoint);
1121 
1122 		INIT_LIST_HEAD(&container);
1123 		list_add_tail(&packet->list, &container);
1124 
1125 		/* give the packet back */
1126 		do_recv_completion(ep, &container);
1127 		spin_lock_bh(&target->rx_lock);
1128 	}
1129 
1130 	spin_unlock_bh(&target->rx_lock);
1131 }
1132 
1133 /* polling routine to wait for a control packet to be received */
1134 static int htc_wait_recv_ctrl_message(struct htc_target *target)
1135 {
1136 	int count = HTC_TARGET_RESPONSE_POLL_COUNT;
1137 
1138 	while (count > 0) {
1139 		spin_lock_bh(&target->rx_lock);
1140 
1141 		if (target->pipe.ctrl_response_valid) {
1142 			target->pipe.ctrl_response_valid = false;
1143 			spin_unlock_bh(&target->rx_lock);
1144 			break;
1145 		}
1146 
1147 		spin_unlock_bh(&target->rx_lock);
1148 
1149 		count--;
1150 
1151 		msleep_interruptible(HTC_TARGET_RESPONSE_POLL_WAIT);
1152 	}
1153 
1154 	if (count <= 0) {
1155 		ath6kl_dbg(ATH6KL_DBG_HTC, "%s: Timeout!\n", __func__);
1156 		return -ECOMM;
1157 	}
1158 
1159 	return 0;
1160 }
1161 
1162 static void htc_rxctrl_complete(struct htc_target *context,
1163 				struct htc_packet *packet)
1164 {
1165 	/* TODO, can't really receive HTC control messages yet.... */
1166 	ath6kl_dbg(ATH6KL_DBG_HTC, "%s: invalid call function\n", __func__);
1167 }
1168 
1169 /* htc pipe initialization */
1170 static void reset_endpoint_states(struct htc_target *target)
1171 {
1172 	struct htc_endpoint *ep;
1173 	int i;
1174 
1175 	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
1176 		ep = &target->endpoint[i];
1177 		ep->svc_id = 0;
1178 		ep->len_max = 0;
1179 		ep->max_txq_depth = 0;
1180 		ep->eid = i;
1181 		INIT_LIST_HEAD(&ep->txq);
1182 		INIT_LIST_HEAD(&ep->pipe.tx_lookup_queue);
1183 		INIT_LIST_HEAD(&ep->rx_bufq);
1184 		ep->target = target;
1185 		ep->pipe.tx_credit_flow_enabled = true;
1186 	}
1187 }
1188 
1189 /* start HTC, this is called after all services are connected */
1190 static int htc_config_target_hif_pipe(struct htc_target *target)
1191 {
1192 	return 0;
1193 }
1194 
1195 /* htc service functions */
1196 static u8 htc_get_credit_alloc(struct htc_target *target, u16 service_id)
1197 {
1198 	u8 allocation = 0;
1199 	int i;
1200 
1201 	for (i = 0; i < ENDPOINT_MAX; i++) {
1202 		if (target->pipe.txcredit_alloc[i].service_id == service_id)
1203 			allocation =
1204 				target->pipe.txcredit_alloc[i].credit_alloc;
1205 	}
1206 
1207 	if (allocation == 0) {
1208 		ath6kl_dbg(ATH6KL_DBG_HTC,
1209 			   "HTC Service TX : 0x%2.2X : allocation is zero!\n",
1210 			   service_id);
1211 	}
1212 
1213 	return allocation;
1214 }
1215 
1216 static int ath6kl_htc_pipe_conn_service(struct htc_target *target,
1217 		     struct htc_service_connect_req *conn_req,
1218 		     struct htc_service_connect_resp *conn_resp)
1219 {
1220 	struct ath6kl *ar = target->dev->ar;
1221 	struct htc_packet *packet = NULL;
1222 	struct htc_conn_service_resp *resp_msg;
1223 	struct htc_conn_service_msg *conn_msg;
1224 	enum htc_endpoint_id assigned_epid = ENDPOINT_MAX;
1225 	bool disable_credit_flowctrl = false;
1226 	unsigned int max_msg_size = 0;
1227 	struct htc_endpoint *ep;
1228 	int length, status = 0;
1229 	struct sk_buff *skb;
1230 	u8 tx_alloc;
1231 	u16 flags;
1232 
1233 	if (conn_req->svc_id == 0) {
1234 		WARN_ON_ONCE(1);
1235 		status = -EINVAL;
1236 		goto free_packet;
1237 	}
1238 
1239 	if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
1240 		/* special case for pseudo control service */
1241 		assigned_epid = ENDPOINT_0;
1242 		max_msg_size = HTC_MAX_CTRL_MSG_LEN;
1243 		tx_alloc = 0;
1244 
1245 	} else {
1246 
1247 		tx_alloc = htc_get_credit_alloc(target, conn_req->svc_id);
1248 		if (tx_alloc == 0) {
1249 			status = -ENOMEM;
1250 			goto free_packet;
1251 		}
1252 
1253 		/* allocate a packet to send to the target */
1254 		packet = htc_alloc_txctrl_packet(target);
1255 
1256 		if (packet == NULL) {
1257 			WARN_ON_ONCE(1);
1258 			status = -ENOMEM;
1259 			goto free_packet;
1260 		}
1261 
1262 		skb = packet->skb;
1263 		length = sizeof(struct htc_conn_service_msg);
1264 
1265 		/* assemble connect service message */
1266 		conn_msg = (struct htc_conn_service_msg *) skb_put(skb,
1267 								   length);
1268 		if (conn_msg == NULL) {
1269 			WARN_ON_ONCE(1);
1270 			status = -EINVAL;
1271 			goto free_packet;
1272 		}
1273 
1274 		memset(conn_msg, 0,
1275 		       sizeof(struct htc_conn_service_msg));
1276 		conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
1277 		conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
1278 		conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags &
1279 					~HTC_CONN_FLGS_SET_RECV_ALLOC_MASK);
1280 
1281 		/* tell target desired recv alloc for this ep */
1282 		flags = tx_alloc << HTC_CONN_FLGS_SET_RECV_ALLOC_SHIFT;
1283 		conn_msg->conn_flags |= cpu_to_le16(flags);
1284 
1285 		if (conn_req->conn_flags &
1286 		    HTC_CONN_FLGS_DISABLE_CRED_FLOW_CTRL) {
1287 			disable_credit_flowctrl = true;
1288 		}
1289 
1290 		set_htc_pkt_info(packet, NULL, (u8 *) conn_msg,
1291 				 length,
1292 				 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
1293 
1294 		status = ath6kl_htc_pipe_tx(target, packet);
1295 
1296 		/* we don't own it anymore */
1297 		packet = NULL;
1298 		if (status != 0)
1299 			goto free_packet;
1300 
1301 		/* wait for response */
1302 		status = htc_wait_recv_ctrl_message(target);
1303 		if (status != 0)
1304 			goto free_packet;
1305 
1306 		/* we controlled the buffer creation so it has to be
1307 		 * properly aligned
1308 		 */
1309 		resp_msg = (struct htc_conn_service_resp *)
1310 		    target->pipe.ctrl_response_buf;
1311 
1312 		if (resp_msg->msg_id != cpu_to_le16(HTC_MSG_CONN_SVC_RESP_ID) ||
1313 		    (target->pipe.ctrl_response_len < sizeof(*resp_msg))) {
1314 			/* this message is not valid */
1315 			WARN_ON_ONCE(1);
1316 			status = -EINVAL;
1317 			goto free_packet;
1318 		}
1319 
1320 		ath6kl_dbg(ATH6KL_DBG_TRC,
1321 			   "%s: service 0x%X conn resp: status: %d ep: %d\n",
1322 			   __func__, resp_msg->svc_id, resp_msg->status,
1323 			   resp_msg->eid);
1324 
1325 		conn_resp->resp_code = resp_msg->status;
1326 		/* check response status */
1327 		if (resp_msg->status != HTC_SERVICE_SUCCESS) {
1328 			ath6kl_dbg(ATH6KL_DBG_HTC,
1329 				   "Target failed service 0x%X connect request (status:%d)\n",
1330 				   resp_msg->svc_id, resp_msg->status);
1331 			status = -EINVAL;
1332 			goto free_packet;
1333 		}
1334 
1335 		assigned_epid = (enum htc_endpoint_id) resp_msg->eid;
1336 		max_msg_size = le16_to_cpu(resp_msg->max_msg_sz);
1337 	}
1338 
1339 	/* the rest are parameter checks so set the error status */
1340 	status = -EINVAL;
1341 
1342 	if (assigned_epid >= ENDPOINT_MAX) {
1343 		WARN_ON_ONCE(1);
1344 		goto free_packet;
1345 	}
1346 
1347 	if (max_msg_size == 0) {
1348 		WARN_ON_ONCE(1);
1349 		goto free_packet;
1350 	}
1351 
1352 	ep = &target->endpoint[assigned_epid];
1353 	ep->eid = assigned_epid;
1354 	if (ep->svc_id != 0) {
1355 		/* endpoint already in use! */
1356 		WARN_ON_ONCE(1);
1357 		goto free_packet;
1358 	}
1359 
1360 	/* return assigned endpoint to caller */
1361 	conn_resp->endpoint = assigned_epid;
1362 	conn_resp->len_max = max_msg_size;
1363 
1364 	/* setup the endpoint */
1365 	ep->svc_id = conn_req->svc_id; /* this marks ep in use */
1366 	ep->max_txq_depth = conn_req->max_txq_depth;
1367 	ep->len_max = max_msg_size;
1368 	ep->cred_dist.credits = tx_alloc;
1369 	ep->cred_dist.cred_sz = target->tgt_cred_sz;
1370 	ep->cred_dist.cred_per_msg = max_msg_size / target->tgt_cred_sz;
1371 	if (max_msg_size % target->tgt_cred_sz)
1372 		ep->cred_dist.cred_per_msg++;
1373 
1374 	/* copy all the callbacks */
1375 	ep->ep_cb = conn_req->ep_cb;
1376 
1377 	/* initialize tx_drop_packet_threshold */
1378 	ep->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
1379 
1380 	status = ath6kl_hif_pipe_map_service(ar, ep->svc_id,
1381 					     &ep->pipe.pipeid_ul,
1382 					     &ep->pipe.pipeid_dl);
1383 	if (status != 0)
1384 		goto free_packet;
1385 
1386 	ath6kl_dbg(ATH6KL_DBG_HTC,
1387 		   "SVC Ready: 0x%4.4X: ULpipe:%d DLpipe:%d id:%d\n",
1388 		   ep->svc_id, ep->pipe.pipeid_ul,
1389 		   ep->pipe.pipeid_dl, ep->eid);
1390 
1391 	if (disable_credit_flowctrl && ep->pipe.tx_credit_flow_enabled) {
1392 		ep->pipe.tx_credit_flow_enabled = false;
1393 		ath6kl_dbg(ATH6KL_DBG_HTC,
1394 			   "SVC: 0x%4.4X ep:%d TX flow control off\n",
1395 			   ep->svc_id, assigned_epid);
1396 	}
1397 
1398 free_packet:
1399 	if (packet != NULL)
1400 		htc_free_txctrl_packet(target, packet);
1401 	return status;
1402 }
1403 
1404 /* htc export functions */
1405 static void *ath6kl_htc_pipe_create(struct ath6kl *ar)
1406 {
1407 	int status = 0;
1408 	struct htc_endpoint *ep = NULL;
1409 	struct htc_target *target = NULL;
1410 	struct htc_packet *packet;
1411 	int i;
1412 
1413 	target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
1414 	if (target == NULL) {
1415 		ath6kl_err("htc create unable to allocate memory\n");
1416 		status = -ENOMEM;
1417 		goto fail_htc_create;
1418 	}
1419 
1420 	spin_lock_init(&target->htc_lock);
1421 	spin_lock_init(&target->rx_lock);
1422 	spin_lock_init(&target->tx_lock);
1423 
1424 	reset_endpoint_states(target);
1425 
1426 	for (i = 0; i < HTC_PACKET_CONTAINER_ALLOCATION; i++) {
1427 		packet = kzalloc(sizeof(struct htc_packet), GFP_KERNEL);
1428 
1429 		if (packet != NULL)
1430 			free_htc_packet_container(target, packet);
1431 	}
1432 
1433 	target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
1434 	if (!target->dev) {
1435 		ath6kl_err("unable to allocate memory\n");
1436 		status = -ENOMEM;
1437 		goto fail_htc_create;
1438 	}
1439 	target->dev->ar = ar;
1440 	target->dev->htc_cnxt = target;
1441 
1442 	/* Get HIF default pipe for HTC message exchange */
1443 	ep = &target->endpoint[ENDPOINT_0];
1444 
1445 	ath6kl_hif_pipe_get_default(ar, &ep->pipe.pipeid_ul,
1446 				    &ep->pipe.pipeid_dl);
1447 
1448 	return target;
1449 
1450 fail_htc_create:
1451 	if (status != 0) {
1452 		if (target != NULL)
1453 			ath6kl_htc_pipe_cleanup(target);
1454 
1455 		target = NULL;
1456 	}
1457 	return target;
1458 }
1459 
1460 /* cleanup the HTC instance */
1461 static void ath6kl_htc_pipe_cleanup(struct htc_target *target)
1462 {
1463 	struct htc_packet *packet;
1464 
1465 	while (true) {
1466 		packet = alloc_htc_packet_container(target);
1467 		if (packet == NULL)
1468 			break;
1469 		kfree(packet);
1470 	}
1471 
1472 	kfree(target->dev);
1473 
1474 	/* kfree our instance */
1475 	kfree(target);
1476 }
1477 
1478 static int ath6kl_htc_pipe_start(struct htc_target *target)
1479 {
1480 	struct sk_buff *skb;
1481 	struct htc_setup_comp_ext_msg *setup;
1482 	struct htc_packet *packet;
1483 
1484 	htc_config_target_hif_pipe(target);
1485 
1486 	/* allocate a buffer to send */
1487 	packet = htc_alloc_txctrl_packet(target);
1488 	if (packet == NULL) {
1489 		WARN_ON_ONCE(1);
1490 		return -ENOMEM;
1491 	}
1492 
1493 	skb = packet->skb;
1494 
1495 	/* assemble setup complete message */
1496 	setup = (struct htc_setup_comp_ext_msg *) skb_put(skb,
1497 							  sizeof(*setup));
1498 	memset(setup, 0, sizeof(struct htc_setup_comp_ext_msg));
1499 	setup->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);
1500 
1501 	ath6kl_dbg(ATH6KL_DBG_HTC, "HTC using TX credit flow control\n");
1502 
1503 	set_htc_pkt_info(packet, NULL, (u8 *) setup,
1504 			 sizeof(struct htc_setup_comp_ext_msg),
1505 			 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
1506 
1507 	target->htc_flags |= HTC_OP_STATE_SETUP_COMPLETE;
1508 
1509 	return ath6kl_htc_pipe_tx(target, packet);
1510 }
1511 
1512 static void ath6kl_htc_pipe_stop(struct htc_target *target)
1513 {
1514 	int i;
1515 	struct htc_endpoint *ep;
1516 
1517 	/* cleanup endpoints */
1518 	for (i = 0; i < ENDPOINT_MAX; i++) {
1519 		ep = &target->endpoint[i];
1520 		htc_flush_rx_queue(target, ep);
1521 		htc_flush_tx_endpoint(target, ep, HTC_TX_PACKET_TAG_ALL);
1522 	}
1523 
1524 	reset_endpoint_states(target);
1525 	target->htc_flags &= ~HTC_OP_STATE_SETUP_COMPLETE;
1526 }
1527 
1528 static int ath6kl_htc_pipe_get_rxbuf_num(struct htc_target *target,
1529 					 enum htc_endpoint_id endpoint)
1530 {
1531 	int num;
1532 
1533 	spin_lock_bh(&target->rx_lock);
1534 	num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
1535 	spin_unlock_bh(&target->rx_lock);
1536 
1537 	return num;
1538 }
1539 
1540 static int ath6kl_htc_pipe_tx(struct htc_target *target,
1541 			      struct htc_packet *packet)
1542 {
1543 	struct list_head queue;
1544 
1545 	ath6kl_dbg(ATH6KL_DBG_HTC,
1546 		   "%s: endPointId: %d, buffer: 0x%p, length: %d\n",
1547 		   __func__, packet->endpoint, packet->buf,
1548 		   packet->act_len);
1549 
1550 	INIT_LIST_HEAD(&queue);
1551 	list_add_tail(&packet->list, &queue);
1552 
1553 	return htc_send_packets_multiple(target, &queue);
1554 }
1555 
1556 static int ath6kl_htc_pipe_wait_target(struct htc_target *target)
1557 {
1558 	struct htc_ready_ext_msg *ready_msg;
1559 	struct htc_service_connect_req connect;
1560 	struct htc_service_connect_resp resp;
1561 	int status = 0;
1562 
1563 	status = htc_wait_recv_ctrl_message(target);
1564 
1565 	if (status != 0)
1566 		return status;
1567 
1568 	if (target->pipe.ctrl_response_len < sizeof(*ready_msg)) {
1569 		ath6kl_dbg(ATH6KL_DBG_HTC, "invalid htc ready msg len:%d!\n",
1570 			   target->pipe.ctrl_response_len);
1571 		return -ECOMM;
1572 	}
1573 
1574 	ready_msg = (struct htc_ready_ext_msg *) target->pipe.ctrl_response_buf;
1575 
1576 	if (ready_msg->ver2_0_info.msg_id != cpu_to_le16(HTC_MSG_READY_ID)) {
1577 		ath6kl_dbg(ATH6KL_DBG_HTC, "invalid htc ready msg : 0x%X !\n",
1578 			   ready_msg->ver2_0_info.msg_id);
1579 		return -ECOMM;
1580 	}
1581 
1582 	ath6kl_dbg(ATH6KL_DBG_HTC,
1583 		   "Target Ready! : transmit resources : %d size:%d\n",
1584 		   ready_msg->ver2_0_info.cred_cnt,
1585 		   ready_msg->ver2_0_info.cred_sz);
1586 
1587 	target->tgt_creds = le16_to_cpu(ready_msg->ver2_0_info.cred_cnt);
1588 	target->tgt_cred_sz = le16_to_cpu(ready_msg->ver2_0_info.cred_sz);
1589 
1590 	if ((target->tgt_creds == 0) || (target->tgt_cred_sz == 0))
1591 		return -ECOMM;
1592 
1593 	htc_setup_target_buffer_assignments(target);
1594 
1595 	/* setup our pseudo HTC control endpoint connection */
1596 	memset(&connect, 0, sizeof(connect));
1597 	memset(&resp, 0, sizeof(resp));
1598 	connect.ep_cb.tx_complete = htc_txctrl_complete;
1599 	connect.ep_cb.rx = htc_rxctrl_complete;
1600 	connect.max_txq_depth = NUM_CONTROL_TX_BUFFERS;
1601 	connect.svc_id = HTC_CTRL_RSVD_SVC;
1602 
1603 	/* connect fake service */
1604 	status = ath6kl_htc_pipe_conn_service(target, &connect, &resp);
1605 
1606 	return status;
1607 }
1608 
1609 static void ath6kl_htc_pipe_flush_txep(struct htc_target *target,
1610 				       enum htc_endpoint_id endpoint, u16 tag)
1611 {
1612 	struct htc_endpoint *ep = &target->endpoint[endpoint];
1613 
1614 	if (ep->svc_id == 0) {
1615 		WARN_ON_ONCE(1);
1616 		/* not in use.. */
1617 		return;
1618 	}
1619 
1620 	htc_flush_tx_endpoint(target, ep, tag);
1621 }
1622 
1623 static int ath6kl_htc_pipe_add_rxbuf_multiple(struct htc_target *target,
1624 					      struct list_head *pkt_queue)
1625 {
1626 	struct htc_packet *packet, *tmp_pkt, *first;
1627 	struct htc_endpoint *ep;
1628 	int status = 0;
1629 
1630 	if (list_empty(pkt_queue))
1631 		return -EINVAL;
1632 
1633 	first = list_first_entry(pkt_queue, struct htc_packet, list);
1634 
1635 	if (first->endpoint >= ENDPOINT_MAX) {
1636 		WARN_ON_ONCE(1);
1637 		return -EINVAL;
1638 	}
1639 
1640 	ath6kl_dbg(ATH6KL_DBG_HTC, "%s: epid: %d, cnt:%d, len: %d\n",
1641 		   __func__, first->endpoint, get_queue_depth(pkt_queue),
1642 		   first->buf_len);
1643 
1644 	ep = &target->endpoint[first->endpoint];
1645 
1646 	spin_lock_bh(&target->rx_lock);
1647 
1648 	/* store receive packets */
1649 	list_splice_tail_init(pkt_queue, &ep->rx_bufq);
1650 
1651 	spin_unlock_bh(&target->rx_lock);
1652 
1653 	if (status != 0) {
1654 		/* walk through queue and mark each one canceled */
1655 		list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
1656 			packet->status = -ECANCELED;
1657 		}
1658 
1659 		do_recv_completion(ep, pkt_queue);
1660 	}
1661 
1662 	return status;
1663 }
1664 
1665 static void ath6kl_htc_pipe_activity_changed(struct htc_target *target,
1666 					     enum htc_endpoint_id ep,
1667 					     bool active)
1668 {
1669 	/* TODO */
1670 }
1671 
1672 static void ath6kl_htc_pipe_flush_rx_buf(struct htc_target *target)
1673 {
1674 	/* TODO */
1675 }
1676 
1677 static int ath6kl_htc_pipe_credit_setup(struct htc_target *target,
1678 					struct ath6kl_htc_credit_info *info)
1679 {
1680 	return 0;
1681 }
1682 
1683 static const struct ath6kl_htc_ops ath6kl_htc_pipe_ops = {
1684 	.create = ath6kl_htc_pipe_create,
1685 	.wait_target = ath6kl_htc_pipe_wait_target,
1686 	.start = ath6kl_htc_pipe_start,
1687 	.conn_service = ath6kl_htc_pipe_conn_service,
1688 	.tx = ath6kl_htc_pipe_tx,
1689 	.stop = ath6kl_htc_pipe_stop,
1690 	.cleanup = ath6kl_htc_pipe_cleanup,
1691 	.flush_txep = ath6kl_htc_pipe_flush_txep,
1692 	.flush_rx_buf = ath6kl_htc_pipe_flush_rx_buf,
1693 	.activity_changed = ath6kl_htc_pipe_activity_changed,
1694 	.get_rxbuf_num = ath6kl_htc_pipe_get_rxbuf_num,
1695 	.add_rxbuf_multiple = ath6kl_htc_pipe_add_rxbuf_multiple,
1696 	.credit_setup = ath6kl_htc_pipe_credit_setup,
1697 	.tx_complete = ath6kl_htc_pipe_tx_complete,
1698 	.rx_complete = ath6kl_htc_pipe_rx_complete,
1699 };
1700 
1701 void ath6kl_htc_pipe_attach(struct ath6kl *ar)
1702 {
1703 	ar->htc_ops = &ath6kl_htc_pipe_ops;
1704 }
1705