htc_mbox.c 74.6 KB
Newer Older
K
Kalle Valo 已提交
1 2
/*
 * Copyright (c) 2007-2011 Atheros Communications Inc.
3
 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
K
Kalle Valo 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "core.h"
19
#include "hif.h"
K
Kalle Valo 已提交
20 21
#include "debug.h"
#include "hif-ops.h"
22 23
#include "trace.h"

K
Kalle Valo 已提交
24 25 26 27
#include <asm/unaligned.h>

#define CALC_TXRX_PADDED_LEN(dev, len)  (__ALIGN_MASK((len), (dev)->block_mask))

K
Kalle Valo 已提交
28 29 30 31 32 33 34 35
static void ath6kl_htc_mbox_cleanup(struct htc_target *target);
static void ath6kl_htc_mbox_stop(struct htc_target *target);
static int ath6kl_htc_mbox_add_rxbuf_multiple(struct htc_target *target,
					      struct list_head *pkt_queue);
static void ath6kl_htc_set_credit_dist(struct htc_target *target,
				       struct ath6kl_htc_credit_info *cred_info,
				       u16 svc_pri_order[], int len);

36 37 38
/* threshold to re-enable Tx bundling for an AC*/
#define TX_RESUME_BUNDLE_THRESHOLD	1500

39
/* Functions for Tx credit handling */
40 41 42
static void ath6kl_credit_deposit(struct ath6kl_htc_credit_info *cred_info,
				  struct htc_endpoint_credit_dist *ep_dist,
				  int credits)
43
{
44 45 46
	ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit deposit ep %d credits %d\n",
		   ep_dist->endpoint, credits);

47 48 49 50 51 52 53 54 55 56 57 58
	ep_dist->credits += credits;
	ep_dist->cred_assngd += credits;
	cred_info->cur_free_credits -= credits;
}

static void ath6kl_credit_init(struct ath6kl_htc_credit_info *cred_info,
			       struct list_head *ep_list,
			       int tot_credits)
{
	struct htc_endpoint_credit_dist *cur_ep_dist;
	int count;

59 60
	ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit init total %d\n", tot_credits);

61 62 63 64 65 66 67 68 69 70 71 72
	cred_info->cur_free_credits = tot_credits;
	cred_info->total_avail_credits = tot_credits;

	list_for_each_entry(cur_ep_dist, ep_list, list) {
		if (cur_ep_dist->endpoint == ENDPOINT_0)
			continue;

		cur_ep_dist->cred_min = cur_ep_dist->cred_per_msg;

		if (tot_credits > 4) {
			if ((cur_ep_dist->svc_id == WMI_DATA_BK_SVC) ||
			    (cur_ep_dist->svc_id == WMI_DATA_BE_SVC)) {
73 74 75
				ath6kl_credit_deposit(cred_info,
						      cur_ep_dist,
						      cur_ep_dist->cred_min);
76 77 78 79 80
				cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
			}
		}

		if (cur_ep_dist->svc_id == WMI_CONTROL_SVC) {
81 82
			ath6kl_credit_deposit(cred_info, cur_ep_dist,
					      cur_ep_dist->cred_min);
83 84 85 86 87
			/*
			 * Control service is always marked active, it
			 * never goes inactive EVER.
			 */
			cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
88
		}
89 90 91 92 93 94 95 96 97 98 99 100 101

		/*
		 * Streams have to be created (explicit | implicit) for all
		 * kinds of traffic. BE endpoints are also inactive in the
		 * beginning. When BE traffic starts it creates implicit
		 * streams that redistributes credits.
		 *
		 * Note: all other endpoints have minimums set but are
		 * initially given NO credits. credits will be distributed
		 * as traffic activity demands
		 */
	}

102 103 104 105 106 107 108
	/*
	 * For ath6kl_credit_seek function,
	 * it use list_for_each_entry_reverse to walk around the whole ep list.
	 * Therefore assign this lowestpri_ep_dist after walk around the ep_list
	 */
	cred_info->lowestpri_ep_dist = cur_ep_dist->list;

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	WARN_ON(cred_info->cur_free_credits <= 0);

	list_for_each_entry(cur_ep_dist, ep_list, list) {
		if (cur_ep_dist->endpoint == ENDPOINT_0)
			continue;

		if (cur_ep_dist->svc_id == WMI_CONTROL_SVC)
			cur_ep_dist->cred_norm = cur_ep_dist->cred_per_msg;
		else {
			/*
			 * For the remaining data endpoints, we assume that
			 * each cred_per_msg are the same. We use a simple
			 * calculation here, we take the remaining credits
			 * and determine how many max messages this can
			 * cover and then set each endpoint's normal value
			 * equal to 3/4 this amount.
			 */
			count = (cred_info->cur_free_credits /
				 cur_ep_dist->cred_per_msg)
				* cur_ep_dist->cred_per_msg;
			count = (count * 3) >> 2;
			count = max(count, cur_ep_dist->cred_per_msg);
			cur_ep_dist->cred_norm = count;

		}
134 135 136 137 138 139 140 141 142

		ath6kl_dbg(ATH6KL_DBG_CREDIT,
			   "credit ep %d svc_id %d credits %d per_msg %d norm %d min %d\n",
			   cur_ep_dist->endpoint,
			   cur_ep_dist->svc_id,
			   cur_ep_dist->credits,
			   cur_ep_dist->cred_per_msg,
			   cur_ep_dist->cred_norm,
			   cur_ep_dist->cred_min);
143 144 145 146
	}
}

/* initialize and setup credit distribution */
K
Kalle Valo 已提交
147 148
static int ath6kl_htc_mbox_credit_setup(struct htc_target *htc_target,
			       struct ath6kl_htc_credit_info *cred_info)
149 150 151 152 153 154 155 156 157 158 159 160
{
	u16 servicepriority[5];

	memset(cred_info, 0, sizeof(struct ath6kl_htc_credit_info));

	servicepriority[0] = WMI_CONTROL_SVC;  /* highest */
	servicepriority[1] = WMI_DATA_VO_SVC;
	servicepriority[2] = WMI_DATA_VI_SVC;
	servicepriority[3] = WMI_DATA_BE_SVC;
	servicepriority[4] = WMI_DATA_BK_SVC; /* lowest */

	/* set priority list */
161
	ath6kl_htc_set_credit_dist(htc_target, cred_info, servicepriority, 5);
162 163 164 165 166

	return 0;
}

/* reduce an ep's credits back to a set limit */
167 168 169
static void ath6kl_credit_reduce(struct ath6kl_htc_credit_info *cred_info,
				 struct htc_endpoint_credit_dist *ep_dist,
				 int limit)
170 171 172
{
	int credits;

173 174 175
	ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit reduce ep %d limit %d\n",
		   ep_dist->endpoint, limit);

176 177 178 179 180 181 182 183 184 185 186 187 188
	ep_dist->cred_assngd = limit;

	if (ep_dist->credits <= limit)
		return;

	credits = ep_dist->credits - limit;
	ep_dist->credits -= credits;
	cred_info->cur_free_credits += credits;
}

static void ath6kl_credit_update(struct ath6kl_htc_credit_info *cred_info,
				 struct list_head *epdist_list)
{
K
Kalle Valo 已提交
189
	struct htc_endpoint_credit_dist *cur_list;
190

K
Kalle Valo 已提交
191 192
	list_for_each_entry(cur_list, epdist_list, list) {
		if (cur_list->endpoint == ENDPOINT_0)
193 194
			continue;

K
Kalle Valo 已提交
195 196 197 198 199
		if (cur_list->cred_to_dist > 0) {
			cur_list->credits += cur_list->cred_to_dist;
			cur_list->cred_to_dist = 0;

			if (cur_list->credits > cur_list->cred_assngd)
200
				ath6kl_credit_reduce(cred_info,
K
Kalle Valo 已提交
201 202
						     cur_list,
						     cur_list->cred_assngd);
203

K
Kalle Valo 已提交
204 205 206
			if (cur_list->credits > cur_list->cred_norm)
				ath6kl_credit_reduce(cred_info, cur_list,
						     cur_list->cred_norm);
207

K
Kalle Valo 已提交
208 209
			if (!(cur_list->dist_flags & HTC_EP_ACTIVE)) {
				if (cur_list->txq_depth == 0)
210
					ath6kl_credit_reduce(cred_info,
K
Kalle Valo 已提交
211
							     cur_list, 0);
212 213 214 215 216 217 218 219 220
			}
		}
	}
}

/*
 * HTC has an endpoint that needs credits, ep_dist is the endpoint in
 * question.
 */
221
static void ath6kl_credit_seek(struct ath6kl_htc_credit_info *cred_info,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
				struct htc_endpoint_credit_dist *ep_dist)
{
	struct htc_endpoint_credit_dist *curdist_list;
	int credits = 0;
	int need;

	if (ep_dist->svc_id == WMI_CONTROL_SVC)
		goto out;

	if ((ep_dist->svc_id == WMI_DATA_VI_SVC) ||
	    (ep_dist->svc_id == WMI_DATA_VO_SVC))
		if ((ep_dist->cred_assngd >= ep_dist->cred_norm))
			goto out;

	/*
	 * For all other services, we follow a simple algorithm of:
	 *
	 * 1. checking the free pool for credits
	 * 2. checking lower priority endpoints for credits to take
	 */

	credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);

	if (credits >= ep_dist->seek_cred)
		goto out;

	/*
	 * We don't have enough in the free pool, try taking away from
	 * lower priority services The rule for taking away credits:
	 *
	 *   1. Only take from lower priority endpoints
	 *   2. Only take what is allocated above the minimum (never
	 *      starve an endpoint completely)
	 *   3. Only take what you need.
	 */

	list_for_each_entry_reverse(curdist_list,
				    &cred_info->lowestpri_ep_dist,
				    list) {
		if (curdist_list == ep_dist)
			break;

		need = ep_dist->seek_cred - cred_info->cur_free_credits;

		if ((curdist_list->cred_assngd - need) >=
		     curdist_list->cred_min) {
			/*
			 * The current one has been allocated more than
			 * it's minimum and it has enough credits assigned
			 * above it's minimum to fulfill our need try to
			 * take away just enough to fulfill our need.
			 */
274 275
			ath6kl_credit_reduce(cred_info, curdist_list,
					     curdist_list->cred_assngd - need);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

			if (cred_info->cur_free_credits >=
			    ep_dist->seek_cred)
				break;
		}

		if (curdist_list->endpoint == ENDPOINT_0)
			break;
	}

	credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);

out:
	/* did we find some credits? */
	if (credits)
291
		ath6kl_credit_deposit(cred_info, ep_dist, credits);
292 293 294 295 296

	ep_dist->seek_cred = 0;
}

/* redistribute credits based on activity change */
297 298
static void ath6kl_credit_redistribute(struct ath6kl_htc_credit_info *info,
				       struct list_head *ep_dist_list)
299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
	struct htc_endpoint_credit_dist *curdist_list;

	list_for_each_entry(curdist_list, ep_dist_list, list) {
		if (curdist_list->endpoint == ENDPOINT_0)
			continue;

		if ((curdist_list->svc_id == WMI_DATA_BK_SVC)  ||
		    (curdist_list->svc_id == WMI_DATA_BE_SVC))
			curdist_list->dist_flags |= HTC_EP_ACTIVE;

		if ((curdist_list->svc_id != WMI_CONTROL_SVC) &&
		    !(curdist_list->dist_flags & HTC_EP_ACTIVE)) {
			if (curdist_list->txq_depth == 0)
313
				ath6kl_credit_reduce(info, curdist_list, 0);
314
			else
315 316 317
				ath6kl_credit_reduce(info,
						     curdist_list,
						     curdist_list->cred_min);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		}
	}
}

/*
 *
 * This function is invoked whenever endpoints require credit
 * distributions. A lock is held while this function is invoked, this
 * function shall NOT block. The ep_dist_list is a list of distribution
 * structures in prioritized order as defined by the call to the
 * htc_set_credit_dist() api.
 */
static void ath6kl_credit_distribute(struct ath6kl_htc_credit_info *cred_info,
				     struct list_head *ep_dist_list,
			      enum htc_credit_dist_reason reason)
{
	switch (reason) {
	case HTC_CREDIT_DIST_SEND_COMPLETE:
		ath6kl_credit_update(cred_info, ep_dist_list);
		break;
	case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
339
		ath6kl_credit_redistribute(cred_info, ep_dist_list);
340 341 342 343 344 345 346 347 348
		break;
	default:
		break;
	}

	WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
	WARN_ON(cred_info->cur_free_credits < 0);
}

349
static void ath6kl_htc_tx_buf_align(u8 **buf, unsigned long len)
350 351 352 353 354 355 356 357 358 359
{
	u8 *align_addr;

	if (!IS_ALIGNED((unsigned long) *buf, 4)) {
		align_addr = PTR_ALIGN(*buf - 4, 4);
		memmove(align_addr, *buf, len);
		*buf = align_addr;
	}
}

360 361
static void ath6kl_htc_tx_prep_pkt(struct htc_packet *packet, u8 flags,
				   int ctrl0, int ctrl1)
K
Kalle Valo 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
{
	struct htc_frame_hdr *hdr;

	packet->buf -= HTC_HDR_LENGTH;
	hdr =  (struct htc_frame_hdr *)packet->buf;

	/* Endianess? */
	put_unaligned((u16)packet->act_len, &hdr->payld_len);
	hdr->flags = flags;
	hdr->eid = packet->endpoint;
	hdr->ctrl[0] = ctrl0;
	hdr->ctrl[1] = ctrl1;
}

static void htc_reclaim_txctrl_buf(struct htc_target *target,
				   struct htc_packet *pkt)
{
	spin_lock_bh(&target->htc_lock);
	list_add_tail(&pkt->list, &target->free_ctrl_txbuf);
	spin_unlock_bh(&target->htc_lock);
}

static struct htc_packet *htc_get_control_buf(struct htc_target *target,
					      bool tx)
{
	struct htc_packet *packet = NULL;
	struct list_head *buf_list;

	buf_list = tx ? &target->free_ctrl_txbuf : &target->free_ctrl_rxbuf;

	spin_lock_bh(&target->htc_lock);

	if (list_empty(buf_list)) {
		spin_unlock_bh(&target->htc_lock);
		return NULL;
	}

	packet = list_first_entry(buf_list, struct htc_packet, list);
	list_del(&packet->list);
	spin_unlock_bh(&target->htc_lock);

	if (tx)
		packet->buf = packet->buf_start + HTC_HDR_LENGTH;

	return packet;
}

static void htc_tx_comp_update(struct htc_target *target,
			       struct htc_endpoint *endpoint,
			       struct htc_packet *packet)
{
	packet->completion = NULL;
	packet->buf += HTC_HDR_LENGTH;

	if (!packet->status)
		return;

	ath6kl_err("req failed (status:%d, ep:%d, len:%d creds:%d)\n",
		   packet->status, packet->endpoint, packet->act_len,
		   packet->info.tx.cred_used);

	/* on failure to submit, reclaim credits for this packet */
	spin_lock_bh(&target->tx_lock);
	endpoint->cred_dist.cred_to_dist +=
				packet->info.tx.cred_used;
	endpoint->cred_dist.txq_depth = get_queue_depth(&endpoint->txq);

K
Kalle Valo 已提交
429
	ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx ctxt 0x%p dist 0x%p\n",
430
		   target->credit_info, &target->cred_dist_list);
K
Kalle Valo 已提交
431

432 433 434
	ath6kl_credit_distribute(target->credit_info,
				 &target->cred_dist_list,
				 HTC_CREDIT_DIST_SEND_COMPLETE);
K
Kalle Valo 已提交
435 436 437 438 439 440 441 442 443 444

	spin_unlock_bh(&target->tx_lock);
}

static void htc_tx_complete(struct htc_endpoint *endpoint,
			    struct list_head *txq)
{
	if (list_empty(txq))
		return;

K
Kalle Valo 已提交
445
	ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
446
		   "htc tx complete ep %d pkts %d\n",
K
Kalle Valo 已提交
447 448
		   endpoint->eid, get_queue_depth(txq));

449
	ath6kl_tx_complete(endpoint->target, txq);
K
Kalle Valo 已提交
450 451 452 453 454 455 456 457
}

static void htc_tx_comp_handler(struct htc_target *target,
				struct htc_packet *packet)
{
	struct htc_endpoint *endpoint = &target->endpoint[packet->endpoint];
	struct list_head container;

K
Kalle Valo 已提交
458 459 460
	ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx complete seqno %d\n",
		   packet->info.tx.seqno);

K
Kalle Valo 已提交
461 462 463 464 465 466 467
	htc_tx_comp_update(target, endpoint, packet);
	INIT_LIST_HEAD(&container);
	list_add_tail(&packet->list, &container);
	/* do completion */
	htc_tx_complete(endpoint, &container);
}

468 469
static void htc_async_tx_scat_complete(struct htc_target *target,
				       struct hif_scatter_req *scat_req)
K
Kalle Valo 已提交
470
{
471
	struct htc_endpoint *endpoint;
K
Kalle Valo 已提交
472 473 474 475 476 477
	struct htc_packet *packet;
	struct list_head tx_compq;
	int i;

	INIT_LIST_HEAD(&tx_compq);

K
Kalle Valo 已提交
478
	ath6kl_dbg(ATH6KL_DBG_HTC,
479 480
		   "htc tx scat complete len %d entries %d\n",
		   scat_req->len, scat_req->scat_entries);
K
Kalle Valo 已提交
481 482 483 484

	if (scat_req->status)
		ath6kl_err("send scatter req failed: %d\n", scat_req->status);

485 486 487
	packet = scat_req->scat_list[0].packet;
	endpoint = &target->endpoint[packet->endpoint];

K
Kalle Valo 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	/* walk through the scatter list and process */
	for (i = 0; i < scat_req->scat_entries; i++) {
		packet = scat_req->scat_list[i].packet;
		if (!packet) {
			WARN_ON(1);
			return;
		}

		packet->status = scat_req->status;
		htc_tx_comp_update(target, endpoint, packet);
		list_add_tail(&packet->list, &tx_compq);
	}

	/* free scatter request */
	hif_scatter_req_add(target->dev->ar, scat_req);

	/* complete all packets */
	htc_tx_complete(endpoint, &tx_compq);
}

508 509
static int ath6kl_htc_tx_issue(struct htc_target *target,
			       struct htc_packet *packet)
K
Kalle Valo 已提交
510 511 512 513 514 515 516 517 518 519
{
	int status;
	bool sync = false;
	u32 padded_len, send_len;

	if (!packet->completion)
		sync = true;

	send_len = packet->act_len + HTC_HDR_LENGTH;

520
	padded_len = CALC_TXRX_PADDED_LEN(target, send_len);
K
Kalle Valo 已提交
521

K
Kalle Valo 已提交
522
	ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
523 524
		   "htc tx issue len %d seqno %d padded_len %d mbox 0x%X %s\n",
		   send_len, packet->info.tx.seqno, padded_len,
K
Kalle Valo 已提交
525 526
		   target->dev->ar->mbox_info.htc_addr,
		   sync ? "sync" : "async");
K
Kalle Valo 已提交
527 528 529 530 531 532 533 534

	if (sync) {
		status = hif_read_write_sync(target->dev->ar,
				target->dev->ar->mbox_info.htc_addr,
				 packet->buf, padded_len,
				 HIF_WR_SYNC_BLOCK_INC);

		packet->status = status;
535
		packet->buf += HTC_HDR_LENGTH;
K
Kalle Valo 已提交
536 537 538 539 540 541
	} else
		status = hif_write_async(target->dev->ar,
				target->dev->ar->mbox_info.htc_addr,
				packet->buf, padded_len,
				HIF_WR_ASYNC_BLOCK_INC, packet);

542 543
	trace_ath6kl_htc_tx(status, packet->endpoint, packet->buf, send_len);

K
Kalle Valo 已提交
544 545 546 547 548 549 550 551 552 553 554 555
	return status;
}

static int htc_check_credits(struct htc_target *target,
			     struct htc_endpoint *ep, u8 *flags,
			     enum htc_endpoint_id eid, unsigned int len,
			     int *req_cred)
{

	*req_cred = (len > target->tgt_cred_sz) ?
		     DIV_ROUND_UP(len, target->tgt_cred_sz) : 1;

556
	ath6kl_dbg(ATH6KL_DBG_CREDIT, "credit check need %d got %d\n",
K
Kalle Valo 已提交
557 558 559 560 561 562 563 564 565
		   *req_cred, ep->cred_dist.credits);

	if (ep->cred_dist.credits < *req_cred) {
		if (eid == ENDPOINT_0)
			return -EINVAL;

		/* Seek more credits */
		ep->cred_dist.seek_cred = *req_cred - ep->cred_dist.credits;

566
		ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
K
Kalle Valo 已提交
567 568 569 570

		ep->cred_dist.seek_cred = 0;

		if (ep->cred_dist.credits < *req_cred) {
571 572
			ath6kl_dbg(ATH6KL_DBG_CREDIT,
				   "credit not found for ep %d\n",
K
Kalle Valo 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585
				   eid);
			return -EINVAL;
		}
	}

	ep->cred_dist.credits -= *req_cred;
	ep->ep_st.cred_cosumd += *req_cred;

	 /* When we are getting low on credits, ask for more */
	if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
		ep->cred_dist.seek_cred =
		ep->cred_dist.cred_per_msg - ep->cred_dist.credits;

586
		ath6kl_credit_seek(target->credit_info, &ep->cred_dist);
K
Kalle Valo 已提交
587 588 589 590 591 592

		/* see if we were successful in getting more */
		if (ep->cred_dist.credits < ep->cred_dist.cred_per_msg) {
			/* tell the target we need credits ASAP! */
			*flags |= HTC_FLAGS_NEED_CREDIT_UPDATE;
			ep->ep_st.cred_low_indicate += 1;
593 594
			ath6kl_dbg(ATH6KL_DBG_CREDIT,
				   "credit we need credits asap\n");
K
Kalle Valo 已提交
595 596 597 598 599 600
		}
	}

	return 0;
}

601 602 603
static void ath6kl_htc_tx_pkts_get(struct htc_target *target,
				   struct htc_endpoint *endpoint,
				   struct list_head *queue)
K
Kalle Valo 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
{
	int req_cred;
	u8 flags;
	struct htc_packet *packet;
	unsigned int len;

	while (true) {

		flags = 0;

		if (list_empty(&endpoint->txq))
			break;
		packet = list_first_entry(&endpoint->txq, struct htc_packet,
					  list);

K
Kalle Valo 已提交
619
		ath6kl_dbg(ATH6KL_DBG_HTC,
620 621
			   "htc tx got packet 0x%p queue depth %d\n",
			   packet, get_queue_depth(&endpoint->txq));
K
Kalle Valo 已提交
622

623
		len = CALC_TXRX_PADDED_LEN(target,
K
Kalle Valo 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
					   packet->act_len + HTC_HDR_LENGTH);

		if (htc_check_credits(target, endpoint, &flags,
				      packet->endpoint, len, &req_cred))
			break;

		/* now we can fully move onto caller's queue */
		packet = list_first_entry(&endpoint->txq, struct htc_packet,
					  list);
		list_move_tail(&packet->list, queue);

		/* save the number of credits this packet consumed */
		packet->info.tx.cred_used = req_cred;

		/* all TX packets are handled asynchronously */
		packet->completion = htc_tx_comp_handler;
		packet->context = target;
		endpoint->ep_st.tx_issued += 1;

		/* save send flags */
		packet->info.tx.flags = flags;
		packet->info.tx.seqno = endpoint->seqno;
		endpoint->seqno++;
	}
}

/* See if the padded tx length falls on a credit boundary */
static int htc_get_credit_padding(unsigned int cred_sz, int *len,
				  struct htc_endpoint *ep)
{
	int rem_cred, cred_pad;

	rem_cred = *len % cred_sz;

	/* No padding needed */
	if  (!rem_cred)
		return 0;

	if (!(ep->conn_flags & HTC_FLGS_TX_BNDL_PAD_EN))
		return -1;

	/*
	 * The transfer consumes a "partial" credit, this
	 * packet cannot be bundled unless we add
	 * additional "dummy" padding (max 255 bytes) to
	 * consume the entire credit.
	 */
	cred_pad = *len < cred_sz ? (cred_sz - *len) : rem_cred;

	if ((cred_pad > 0) && (cred_pad <= 255))
		*len += cred_pad;
	else
		/* The amount of padding is too large, send as non-bundled */
		return -1;

	return cred_pad;
}

682 683 684 685 686
static int ath6kl_htc_tx_setup_scat_list(struct htc_target *target,
					 struct htc_endpoint *endpoint,
					 struct hif_scatter_req *scat_req,
					 int n_scat,
					 struct list_head *queue)
K
Kalle Valo 已提交
687 688 689 690
{
	struct htc_packet *packet;
	int i, len, rem_scat, cred_pad;
	int status = 0;
K
Kalle Valo 已提交
691
	u8 flags;
K
Kalle Valo 已提交
692

693
	rem_scat = target->max_tx_bndl_sz;
K
Kalle Valo 已提交
694 695 696 697 698 699 700 701

	for (i = 0; i < n_scat; i++) {
		scat_req->scat_list[i].packet = NULL;

		if (list_empty(queue))
			break;

		packet = list_first_entry(queue, struct htc_packet, list);
702
		len = CALC_TXRX_PADDED_LEN(target,
K
Kalle Valo 已提交
703 704 705 706
					   packet->act_len + HTC_HDR_LENGTH);

		cred_pad = htc_get_credit_padding(target->tgt_cred_sz,
						  &len, endpoint);
707
		if (cred_pad < 0 || rem_scat < len) {
K
Kalle Valo 已提交
708 709 710 711 712 713 714 715 716 717
			status = -ENOSPC;
			break;
		}

		rem_scat -= len;
		/* now remove it from the queue */
		list_del(&packet->list);

		scat_req->scat_list[i].packet = packet;
		/* prepare packet and flag message as part of a send bundle */
K
Kalle Valo 已提交
718 719
		flags = packet->info.tx.flags | HTC_FLAGS_SEND_BUNDLE;
		ath6kl_htc_tx_prep_pkt(packet, flags,
720
				       cred_pad, packet->info.tx.seqno);
721
		/* Make sure the buffer is 4-byte aligned */
722 723
		ath6kl_htc_tx_buf_align(&packet->buf,
					packet->act_len + HTC_HDR_LENGTH);
K
Kalle Valo 已提交
724 725 726 727 728
		scat_req->scat_list[i].buf = packet->buf;
		scat_req->scat_list[i].len = len;

		scat_req->len += len;
		scat_req->scat_entries++;
K
Kalle Valo 已提交
729
		ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
730 731
			   "htc tx adding (%d) pkt 0x%p seqno %d len %d remaining %d\n",
			   i, packet, packet->info.tx.seqno, len, rem_scat);
K
Kalle Valo 已提交
732 733 734
	}

	/* Roll back scatter setup in case of any failure */
735
	if (scat_req->scat_entries < HTC_MIN_HTC_MSGS_TO_BUNDLE) {
K
Kalle Valo 已提交
736 737 738 739 740 741 742
		for (i = scat_req->scat_entries - 1; i >= 0; i--) {
			packet = scat_req->scat_list[i].packet;
			if (packet) {
				packet->buf += HTC_HDR_LENGTH;
				list_add(&packet->list, queue);
			}
		}
743
		return -EAGAIN;
K
Kalle Valo 已提交
744 745
	}

746
	return status;
K
Kalle Valo 已提交
747 748 749
}

/*
750 751
 * Drain a queue and send as bundles this function may return without fully
 * draining the queue when
K
Kalle Valo 已提交
752 753 754 755 756 757
 *
 *    1. scatter resources are exhausted
 *    2. a message that will consume a partial credit will stop the
 *    bundling process early
 *    3. we drop below the minimum number of messages for a bundle
 */
758 759 760
static void ath6kl_htc_tx_bundle(struct htc_endpoint *endpoint,
				 struct list_head *queue,
				 int *sent_bundle, int *n_bundle_pkts)
K
Kalle Valo 已提交
761 762 763
{
	struct htc_target *target = endpoint->target;
	struct hif_scatter_req *scat_req = NULL;
764 765
	int n_scat, n_sent_bundle = 0, tot_pkts_bundle = 0, i;
	struct htc_packet *packet;
766
	int status;
767 768 769
	u32 txb_mask;
	u8 ac = WMM_NUM_AC;

D
Dan Carpenter 已提交
770
	if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) &&
771
	    (WMI_CONTROL_SVC != endpoint->svc_id))
772
		ac = target->dev->ar->ep2ac_map[endpoint->eid];
K
Kalle Valo 已提交
773 774

	while (true) {
775
		status = 0;
K
Kalle Valo 已提交
776 777 778 779 780 781 782 783 784 785 786
		n_scat = get_queue_depth(queue);
		n_scat = min(n_scat, target->msg_per_bndl_max);

		if (n_scat < HTC_MIN_HTC_MSGS_TO_BUNDLE)
			/* not enough to bundle */
			break;

		scat_req = hif_scatter_req_get(target->dev->ar);

		if (!scat_req) {
			/* no scatter resources  */
K
Kalle Valo 已提交
787
			ath6kl_dbg(ATH6KL_DBG_HTC,
788
				   "htc tx no more scatter resources\n");
K
Kalle Valo 已提交
789 790 791
			break;
		}

792 793 794 795 796 797 798 799 800 801 802 803 804
		if ((ac < WMM_NUM_AC) && (ac != WMM_AC_BK)) {
			if (WMM_AC_BE == ac)
				/*
				 * BE, BK have priorities and bit
				 * positions reversed
				 */
				txb_mask = (1 << WMM_AC_BK);
			else
				/*
				 * any AC with priority lower than
				 * itself
				 */
				txb_mask = ((1 << ac) - 1);
D
Dan Carpenter 已提交
805 806 807 808 809 810 811 812 813 814 815

			/*
			 * when the scatter request resources drop below a
			 * certain threshold, disable Tx bundling for all
			 * AC's with priority lower than the current requesting
			 * AC. Otherwise re-enable Tx bundling for them
			 */
			if (scat_req->scat_q_depth < ATH6KL_SCATTER_REQS)
				target->tx_bndl_mask &= ~txb_mask;
			else
				target->tx_bndl_mask |= txb_mask;
816 817
		}

K
Kalle Valo 已提交
818
		ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx pkts to scatter: %d\n",
K
Kalle Valo 已提交
819 820 821 822 823
			   n_scat);

		scat_req->len = 0;
		scat_req->scat_entries = 0;

824 825 826
		status = ath6kl_htc_tx_setup_scat_list(target, endpoint,
						       scat_req, n_scat,
						       queue);
827
		if (status == -EAGAIN) {
K
Kalle Valo 已提交
828 829 830 831 832 833 834 835 836
			hif_scatter_req_add(target->dev->ar, scat_req);
			break;
		}

		/* send path is always asynchronous */
		scat_req->complete = htc_async_tx_scat_complete;
		n_sent_bundle++;
		tot_pkts_bundle += scat_req->scat_entries;

K
Kalle Valo 已提交
837
		ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
838
			   "htc tx scatter bytes %d entries %d\n",
K
Kalle Valo 已提交
839
			   scat_req->len, scat_req->scat_entries);
840 841 842 843 844 845 846

		for (i = 0; i < scat_req->scat_entries; i++) {
			packet = scat_req->scat_list[i].packet;
			trace_ath6kl_htc_tx(packet->status, packet->endpoint,
					    packet->buf, packet->act_len);
		}

K
Kalle Valo 已提交
847
		ath6kl_hif_submit_scat_req(target->dev, scat_req, false);
848 849 850

		if (status)
			break;
K
Kalle Valo 已提交
851 852 853 854
	}

	*sent_bundle = n_sent_bundle;
	*n_bundle_pkts = tot_pkts_bundle;
K
Kalle Valo 已提交
855 856
	ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx bundle sent %d pkts\n",
		   n_sent_bundle);
K
Kalle Valo 已提交
857 858 859 860

	return;
}

861 862
static void ath6kl_htc_tx_from_queue(struct htc_target *target,
				     struct htc_endpoint *endpoint)
K
Kalle Valo 已提交
863 864 865 866 867
{
	struct list_head txq;
	struct htc_packet *packet;
	int bundle_sent;
	int n_pkts_bundle;
868
	u8 ac = WMM_NUM_AC;
869
	int status;
K
Kalle Valo 已提交
870 871 872 873 874 875 876

	spin_lock_bh(&target->tx_lock);

	endpoint->tx_proc_cnt++;
	if (endpoint->tx_proc_cnt > 1) {
		endpoint->tx_proc_cnt--;
		spin_unlock_bh(&target->tx_lock);
K
Kalle Valo 已提交
877
		ath6kl_dbg(ATH6KL_DBG_HTC, "htc tx busy\n");
K
Kalle Valo 已提交
878 879 880 881 882 883 884 885 886
		return;
	}

	/*
	 * drain the endpoint TX queue for transmission as long
	 * as we have enough credits.
	 */
	INIT_LIST_HEAD(&txq);

D
Dan Carpenter 已提交
887
	if ((HTC_CTRL_RSVD_SVC != endpoint->svc_id) &&
888
	    (WMI_CONTROL_SVC != endpoint->svc_id))
889 890
		ac = target->dev->ar->ep2ac_map[endpoint->eid];

K
Kalle Valo 已提交
891 892 893 894 895
	while (true) {

		if (list_empty(&endpoint->txq))
			break;

896
		ath6kl_htc_tx_pkts_get(target, endpoint, &txq);
K
Kalle Valo 已提交
897 898 899 900 901 902 903 904 905 906 907

		if (list_empty(&txq))
			break;

		spin_unlock_bh(&target->tx_lock);

		bundle_sent = 0;
		n_pkts_bundle = 0;

		while (true) {
			/* try to send a bundle on each pass */
908
			if ((target->tx_bndl_mask) &&
K
Kalle Valo 已提交
909 910 911 912
			    (get_queue_depth(&txq) >=
			    HTC_MIN_HTC_MSGS_TO_BUNDLE)) {
				int temp1 = 0, temp2 = 0;

913 914 915 916 917 918 919
				/* check if bundling is enabled for an AC */
				if (target->tx_bndl_mask & (1 << ac)) {
					ath6kl_htc_tx_bundle(endpoint, &txq,
							     &temp1, &temp2);
					bundle_sent += temp1;
					n_pkts_bundle += temp2;
				}
K
Kalle Valo 已提交
920 921 922 923 924 925 926 927 928
			}

			if (list_empty(&txq))
				break;

			packet = list_first_entry(&txq, struct htc_packet,
						  list);
			list_del(&packet->list);

929 930
			ath6kl_htc_tx_prep_pkt(packet, packet->info.tx.flags,
					       0, packet->info.tx.seqno);
931 932 933 934 935 936
			status = ath6kl_htc_tx_issue(target, packet);

			if (status) {
				packet->status = status;
				packet->completion(packet->context, packet);
			}
K
Kalle Valo 已提交
937 938 939 940 941 942
		}

		spin_lock_bh(&target->tx_lock);

		endpoint->ep_st.tx_bundles += bundle_sent;
		endpoint->ep_st.tx_pkt_bundled += n_pkts_bundle;
943 944 945 946 947 948 949 950

		/*
		 * if an AC has bundling disabled and no tx bundling
		 * has occured continously for a certain number of TX,
		 * enable tx bundling for this AC
		 */
		if (!bundle_sent) {
			if (!(target->tx_bndl_mask & (1 << ac)) &&
951
			    (ac < WMM_NUM_AC)) {
952 953 954 955 956 957 958 959 960 961 962
				if (++target->ac_tx_count[ac] >=
					TX_RESUME_BUNDLE_THRESHOLD) {
					target->ac_tx_count[ac] = 0;
					target->tx_bndl_mask |= (1 << ac);
				}
			}
		} else {
			/* tx bundling will reset the counter */
			if (ac < WMM_NUM_AC)
				target->ac_tx_count[ac] = 0;
		}
K
Kalle Valo 已提交
963 964 965 966 967 968
	}

	endpoint->tx_proc_cnt = 0;
	spin_unlock_bh(&target->tx_lock);
}

969 970 971
static bool ath6kl_htc_tx_try(struct htc_target *target,
			      struct htc_endpoint *endpoint,
			      struct htc_packet *tx_pkt)
K
Kalle Valo 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
{
	struct htc_ep_callbacks ep_cb;
	int txq_depth;
	bool overflow = false;

	ep_cb = endpoint->ep_cb;

	spin_lock_bh(&target->tx_lock);
	txq_depth = get_queue_depth(&endpoint->txq);
	spin_unlock_bh(&target->tx_lock);

	if (txq_depth >= endpoint->max_txq_depth)
		overflow = true;

	if (overflow)
K
Kalle Valo 已提交
987
		ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
988 989
			   "htc tx overflow ep %d depth %d max %d\n",
			   endpoint->eid, txq_depth,
K
Kalle Valo 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
			   endpoint->max_txq_depth);

	if (overflow && ep_cb.tx_full) {
		if (ep_cb.tx_full(endpoint->target, tx_pkt) ==
		    HTC_SEND_FULL_DROP) {
			endpoint->ep_st.tx_dropped += 1;
			return false;
		}
	}

	spin_lock_bh(&target->tx_lock);
	list_add_tail(&tx_pkt->list, &endpoint->txq);
	spin_unlock_bh(&target->tx_lock);

1004
	ath6kl_htc_tx_from_queue(target, endpoint);
K
Kalle Valo 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

	return true;
}

static void htc_chk_ep_txq(struct htc_target *target)
{
	struct htc_endpoint *endpoint;
	struct htc_endpoint_credit_dist *cred_dist;

	/*
	 * Run through the credit distribution list to see if there are
	 * packets queued. NOTE: no locks need to be taken since the
	 * distribution list is not dynamic (cannot be re-ordered) and we
	 * are not modifying any state.
	 */
	list_for_each_entry(cred_dist, &target->cred_dist_list, list) {
1021
		endpoint = cred_dist->htc_ep;
K
Kalle Valo 已提交
1022 1023 1024

		spin_lock_bh(&target->tx_lock);
		if (!list_empty(&endpoint->txq)) {
K
Kalle Valo 已提交
1025
			ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
1026
				   "htc creds ep %d credits %d pkts %d\n",
K
Kalle Valo 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
				   cred_dist->endpoint,
				   endpoint->cred_dist.credits,
				   get_queue_depth(&endpoint->txq));
			spin_unlock_bh(&target->tx_lock);
			/*
			 * Try to start the stalled queue, this list is
			 * ordered by priority. If there are credits
			 * available the highest priority queue will get a
			 * chance to reclaim credits from lower priority
			 * ones.
			 */
1038
			ath6kl_htc_tx_from_queue(target, endpoint);
K
Kalle Valo 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
			spin_lock_bh(&target->tx_lock);
		}
		spin_unlock_bh(&target->tx_lock);
	}
}

static int htc_setup_tx_complete(struct htc_target *target)
{
	struct htc_packet *send_pkt = NULL;
	int status;

	send_pkt = htc_get_control_buf(target, true);

	if (!send_pkt)
		return -ENOMEM;

	if (target->htc_tgt_ver >= HTC_VERSION_2P1) {
		struct htc_setup_comp_ext_msg *setup_comp_ext;
		u32 flags = 0;

		setup_comp_ext =
		    (struct htc_setup_comp_ext_msg *)send_pkt->buf;
		memset(setup_comp_ext, 0, sizeof(*setup_comp_ext));
		setup_comp_ext->msg_id =
			cpu_to_le16(HTC_MSG_SETUP_COMPLETE_EX_ID);

		if (target->msg_per_bndl_max > 0) {
			/* Indicate HTC bundling to the target */
			flags |= HTC_SETUP_COMP_FLG_RX_BNDL_EN;
			setup_comp_ext->msg_per_rxbndl =
						target->msg_per_bndl_max;
		}

		memcpy(&setup_comp_ext->flags, &flags,
		       sizeof(setup_comp_ext->flags));
		set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp_ext,
1075 1076
				 sizeof(struct htc_setup_comp_ext_msg),
				 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
K
Kalle Valo 已提交
1077 1078 1079 1080 1081 1082 1083

	} else {
		struct htc_setup_comp_msg *setup_comp;
		setup_comp = (struct htc_setup_comp_msg *)send_pkt->buf;
		memset(setup_comp, 0, sizeof(struct htc_setup_comp_msg));
		setup_comp->msg_id = cpu_to_le16(HTC_MSG_SETUP_COMPLETE_ID);
		set_htc_pkt_info(send_pkt, NULL, (u8 *) setup_comp,
1084 1085
				 sizeof(struct htc_setup_comp_msg),
				 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);
K
Kalle Valo 已提交
1086 1087 1088 1089
	}

	/* we want synchronous operation */
	send_pkt->completion = NULL;
1090 1091
	ath6kl_htc_tx_prep_pkt(send_pkt, 0, 0, 0);
	status = ath6kl_htc_tx_issue(target, send_pkt);
K
Kalle Valo 已提交
1092 1093 1094 1095 1096 1097 1098

	if (send_pkt != NULL)
		htc_reclaim_txctrl_buf(target, send_pkt);

	return status;
}

K
Kalle Valo 已提交
1099
static void ath6kl_htc_set_credit_dist(struct htc_target *target,
1100
				struct ath6kl_htc_credit_info *credit_info,
1101
				u16 srvc_pri_order[], int list_len)
K
Kalle Valo 已提交
1102 1103 1104 1105
{
	struct htc_endpoint *endpoint;
	int i, ep;

1106
	target->credit_info = credit_info;
K
Kalle Valo 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

	list_add_tail(&target->endpoint[ENDPOINT_0].cred_dist.list,
		      &target->cred_dist_list);

	for (i = 0; i < list_len; i++) {
		for (ep = ENDPOINT_1; ep < ENDPOINT_MAX; ep++) {
			endpoint = &target->endpoint[ep];
			if (endpoint->svc_id == srvc_pri_order[i]) {
				list_add_tail(&endpoint->cred_dist.list,
					      &target->cred_dist_list);
				break;
			}
		}
		if (ep >= ENDPOINT_MAX) {
			WARN_ON(1);
			return;
		}
	}
}

K
Kalle Valo 已提交
1127 1128
static int ath6kl_htc_mbox_tx(struct htc_target *target,
			      struct htc_packet *packet)
K
Kalle Valo 已提交
1129 1130 1131 1132
{
	struct htc_endpoint *endpoint;
	struct list_head queue;

K
Kalle Valo 已提交
1133
	ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
1134
		   "htc tx ep id %d buf 0x%p len %d\n",
K
Kalle Valo 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143
		   packet->endpoint, packet->buf, packet->act_len);

	if (packet->endpoint >= ENDPOINT_MAX) {
		WARN_ON(1);
		return -EINVAL;
	}

	endpoint = &target->endpoint[packet->endpoint];

1144
	if (!ath6kl_htc_tx_try(target, endpoint, packet)) {
K
Kalle Valo 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
		packet->status = (target->htc_flags & HTC_OP_STATE_STOPPING) ?
				 -ECANCELED : -ENOSPC;
		INIT_LIST_HEAD(&queue);
		list_add(&packet->list, &queue);
		htc_tx_complete(endpoint, &queue);
	}

	return 0;
}

/* flush endpoint TX queue */
K
Kalle Valo 已提交
1156
static void ath6kl_htc_mbox_flush_txep(struct htc_target *target,
1157
			   enum htc_endpoint_id eid, u16 tag)
K
Kalle Valo 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
{
	struct htc_packet *packet, *tmp_pkt;
	struct list_head discard_q, container;
	struct htc_endpoint *endpoint = &target->endpoint[eid];

	if (!endpoint->svc_id) {
		WARN_ON(1);
		return;
	}

	/* initialize the discard queue */
	INIT_LIST_HEAD(&discard_q);

	spin_lock_bh(&target->tx_lock);

	list_for_each_entry_safe(packet, tmp_pkt, &endpoint->txq, list) {
		if ((tag == HTC_TX_PACKET_TAG_ALL) ||
		    (tag == packet->info.tx.tag))
			list_move_tail(&packet->list, &discard_q);
	}

	spin_unlock_bh(&target->tx_lock);

	list_for_each_entry_safe(packet, tmp_pkt, &discard_q, list) {
		packet->status = -ECANCELED;
		list_del(&packet->list);
K
Kalle Valo 已提交
1184
		ath6kl_dbg(ATH6KL_DBG_HTC,
1185 1186 1187
			   "htc tx flushing pkt 0x%p len %d  ep %d tag 0x%x\n",
			   packet, packet->act_len,
			   packet->endpoint, packet->info.tx.tag);
K
Kalle Valo 已提交
1188 1189 1190 1191 1192 1193 1194 1195

		INIT_LIST_HEAD(&container);
		list_add_tail(&packet->list, &container);
		htc_tx_complete(endpoint, &container);
	}

}

1196
static void ath6kl_htc_flush_txep_all(struct htc_target *target)
K
Kalle Valo 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
{
	struct htc_endpoint *endpoint;
	int i;

	dump_cred_dist_stats(target);

	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
		endpoint = &target->endpoint[i];
		if (endpoint->svc_id == 0)
			/* not in use.. */
			continue;
K
Kalle Valo 已提交
1208
		ath6kl_htc_mbox_flush_txep(target, i, HTC_TX_PACKET_TAG_ALL);
K
Kalle Valo 已提交
1209 1210 1211
	}
}

K
Kalle Valo 已提交
1212 1213 1214
static void ath6kl_htc_mbox_activity_changed(struct htc_target *target,
					     enum htc_endpoint_id eid,
					     bool active)
K
Kalle Valo 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
{
	struct htc_endpoint *endpoint = &target->endpoint[eid];
	bool dist = false;

	if (endpoint->svc_id == 0) {
		WARN_ON(1);
		return;
	}

	spin_lock_bh(&target->tx_lock);

	if (active) {
		if (!(endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE)) {
			endpoint->cred_dist.dist_flags |= HTC_EP_ACTIVE;
			dist = true;
		}
	} else {
		if (endpoint->cred_dist.dist_flags & HTC_EP_ACTIVE) {
			endpoint->cred_dist.dist_flags &= ~HTC_EP_ACTIVE;
			dist = true;
		}
	}

	if (dist) {
		endpoint->cred_dist.txq_depth =
			get_queue_depth(&endpoint->txq);

K
Kalle Valo 已提交
1242 1243
		ath6kl_dbg(ATH6KL_DBG_HTC,
			   "htc tx activity ctxt 0x%p dist 0x%p\n",
1244
			   target->credit_info, &target->cred_dist_list);
K
Kalle Valo 已提交
1245

1246
		ath6kl_credit_distribute(target->credit_info,
1247 1248
					 &target->cred_dist_list,
					 HTC_CREDIT_DIST_ACTIVITY_CHANGE);
K
Kalle Valo 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	}

	spin_unlock_bh(&target->tx_lock);

	if (dist && !active)
		htc_chk_ep_txq(target);
}

/* HTC Rx */

1259 1260
static inline void ath6kl_htc_rx_update_stats(struct htc_endpoint *endpoint,
					      int n_look_ahds)
K
Kalle Valo 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
{
	endpoint->ep_st.rx_pkts++;
	if (n_look_ahds == 1)
		endpoint->ep_st.rx_lkahds++;
	else if (n_look_ahds > 1)
		endpoint->ep_st.rx_bundle_lkahd++;
}

static inline bool htc_valid_rx_frame_len(struct htc_target *target,
					  enum htc_endpoint_id eid, int len)
{
	return (eid == target->dev->ar->ctrl_ep) ?
		len <= ATH6KL_BUFFER_SIZE : len <= ATH6KL_AMSDU_BUFFER_SIZE;
}

static int htc_add_rxbuf(struct htc_target *target, struct htc_packet *packet)
{
	struct list_head queue;

	INIT_LIST_HEAD(&queue);
	list_add_tail(&packet->list, &queue);
K
Kalle Valo 已提交
1282
	return ath6kl_htc_mbox_add_rxbuf_multiple(target, &queue);
K
Kalle Valo 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
}

static void htc_reclaim_rxbuf(struct htc_target *target,
			      struct htc_packet *packet,
			      struct htc_endpoint *ep)
{
	if (packet->info.rx.rx_flags & HTC_RX_PKT_NO_RECYCLE) {
		htc_rxpkt_reset(packet);
		packet->status = -ECANCELED;
		ep->ep_cb.rx(ep->target, packet);
	} else {
		htc_rxpkt_reset(packet);
		htc_add_rxbuf((void *)(target), packet);
	}
}

static void reclaim_rx_ctrl_buf(struct htc_target *target,
				struct htc_packet *packet)
{
	spin_lock_bh(&target->htc_lock);
	list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
	spin_unlock_bh(&target->htc_lock);
}

1307 1308 1309
static int ath6kl_htc_rx_packet(struct htc_target *target,
				struct htc_packet *packet,
				u32 rx_len)
K
Kalle Valo 已提交
1310 1311 1312 1313 1314
{
	struct ath6kl_device *dev = target->dev;
	u32 padded_len;
	int status;

1315
	padded_len = CALC_TXRX_PADDED_LEN(target, rx_len);
K
Kalle Valo 已提交
1316 1317

	if (padded_len > packet->buf_len) {
K
Kalle Valo 已提交
1318
		ath6kl_err("not enough receive space for packet - padlen %d recvlen %d bufferlen %d\n",
K
Kalle Valo 已提交
1319 1320 1321 1322
			   padded_len, rx_len, packet->buf_len);
		return -ENOMEM;
	}

K
Kalle Valo 已提交
1323
	ath6kl_dbg(ATH6KL_DBG_HTC,
1324
		   "htc rx 0x%p hdr 0x%x len %d mbox 0x%x\n",
K
Kalle Valo 已提交
1325
		   packet, packet->info.rx.exp_hdr,
K
Kalle Valo 已提交
1326
		   padded_len, dev->ar->mbox_info.htc_addr);
K
Kalle Valo 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

	status = hif_read_write_sync(dev->ar,
				     dev->ar->mbox_info.htc_addr,
				     packet->buf, padded_len,
				     HIF_RD_SYNC_BLOCK_FIX);

	packet->status = status;

	return status;
}

/*
 * optimization for recv packets, we can indicate a
 * "hint" that there are more  single-packets to fetch
 * on this endpoint.
 */
1343 1344 1345
static void ath6kl_htc_rx_set_indicate(u32 lk_ahd,
				       struct htc_endpoint *endpoint,
				       struct htc_packet *packet)
K
Kalle Valo 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
{
	struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)&lk_ahd;

	if (htc_hdr->eid == packet->endpoint) {
		if (!list_empty(&endpoint->rx_bufq))
			packet->info.rx.indicat_flags |=
					HTC_RX_FLAGS_INDICATE_MORE_PKTS;
	}
}

1356
static void ath6kl_htc_rx_chk_water_mark(struct htc_endpoint *endpoint)
K
Kalle Valo 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
{
	struct htc_ep_callbacks ep_cb = endpoint->ep_cb;

	if (ep_cb.rx_refill_thresh > 0) {
		spin_lock_bh(&endpoint->target->rx_lock);
		if (get_queue_depth(&endpoint->rx_bufq)
		    < ep_cb.rx_refill_thresh) {
			spin_unlock_bh(&endpoint->target->rx_lock);
			ep_cb.rx_refill(endpoint->target, endpoint->eid);
			return;
		}
		spin_unlock_bh(&endpoint->target->rx_lock);
	}
}

/* This function is called with rx_lock held */
1373 1374 1375
static int ath6kl_htc_rx_setup(struct htc_target *target,
			       struct htc_endpoint *ep,
			       u32 *lk_ahds, struct list_head *queue, int n_msg)
K
Kalle Valo 已提交
1376 1377 1378 1379 1380 1381 1382 1383
{
	struct htc_packet *packet;
	/* FIXME: type of lk_ahds can't be right */
	struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)lk_ahds;
	struct htc_ep_callbacks ep_cb;
	int status = 0, j, full_len;
	bool no_recycle;

1384
	full_len = CALC_TXRX_PADDED_LEN(target,
K
Kalle Valo 已提交
1385 1386 1387 1388
					le16_to_cpu(htc_hdr->payld_len) +
					sizeof(*htc_hdr));

	if (!htc_valid_rx_frame_len(target, ep->eid, full_len)) {
1389 1390 1391
		ath6kl_warn("Rx buffer requested with invalid length htc_hdr:eid %d, flags 0x%x, len %d\n",
			    htc_hdr->eid, htc_hdr->flags,
			    le16_to_cpu(htc_hdr->payld_len));
K
Kalle Valo 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
		return -EINVAL;
	}

	ep_cb = ep->ep_cb;
	for (j = 0; j < n_msg; j++) {

		/*
		 * Reset flag, any packets allocated using the
		 * rx_alloc() API cannot be recycled on
		 * cleanup,they must be explicitly returned.
		 */
		no_recycle = false;

		if (ep_cb.rx_allocthresh &&
		    (full_len > ep_cb.rx_alloc_thresh)) {
			ep->ep_st.rx_alloc_thresh_hit += 1;
			ep->ep_st.rxalloc_thresh_byte +=
				le16_to_cpu(htc_hdr->payld_len);

			spin_unlock_bh(&target->rx_lock);
			no_recycle = true;

			packet = ep_cb.rx_allocthresh(ep->target, ep->eid,
						      full_len);
			spin_lock_bh(&target->rx_lock);
		} else {
			/* refill handler is being used */
			if (list_empty(&ep->rx_bufq)) {
				if (ep_cb.rx_refill) {
					spin_unlock_bh(&target->rx_lock);
					ep_cb.rx_refill(ep->target, ep->eid);
					spin_lock_bh(&target->rx_lock);
				}
			}

			if (list_empty(&ep->rx_bufq))
				packet = NULL;
			else {
				packet = list_first_entry(&ep->rx_bufq,
						struct htc_packet, list);
				list_del(&packet->list);
			}
		}

		if (!packet) {
			target->rx_st_flags |= HTC_RECV_WAIT_BUFFERS;
			target->ep_waiting = ep->eid;
			return -ENOSPC;
		}

		/* clear flags */
		packet->info.rx.rx_flags = 0;
		packet->info.rx.indicat_flags = 0;
		packet->status = 0;

		if (no_recycle)
			/*
			 * flag that these packets cannot be
			 * recycled, they have to be returned to
			 * the user
			 */
			packet->info.rx.rx_flags |= HTC_RX_PKT_NO_RECYCLE;

		/* Caller needs to free this upon any failure */
		list_add_tail(&packet->list, queue);

		if (target->htc_flags & HTC_OP_STATE_STOPPING) {
			status = -ECANCELED;
			break;
		}

		if (j) {
			packet->info.rx.rx_flags |= HTC_RX_PKT_REFRESH_HDR;
			packet->info.rx.exp_hdr = 0xFFFFFFFF;
		} else
			/* set expected look ahead */
			packet->info.rx.exp_hdr = *lk_ahds;

		packet->act_len = le16_to_cpu(htc_hdr->payld_len) +
			HTC_HDR_LENGTH;
	}

	return status;
}

1477 1478 1479 1480
static int ath6kl_htc_rx_alloc(struct htc_target *target,
			       u32 lk_ahds[], int msg,
			       struct htc_endpoint *endpoint,
			       struct list_head *queue)
K
Kalle Valo 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
{
	int status = 0;
	struct htc_packet *packet, *tmp_pkt;
	struct htc_frame_hdr *htc_hdr;
	int i, n_msg;

	spin_lock_bh(&target->rx_lock);

	for (i = 0; i < msg; i++) {

		htc_hdr = (struct htc_frame_hdr *)&lk_ahds[i];

		if (htc_hdr->eid >= ENDPOINT_MAX) {
			ath6kl_err("invalid ep in look-ahead: %d\n",
				   htc_hdr->eid);
			status = -ENOMEM;
			break;
		}

		if (htc_hdr->eid != endpoint->eid) {
			ath6kl_err("invalid ep in look-ahead: %d should be : %d (index:%d)\n",
				   htc_hdr->eid, endpoint->eid, i);
			status = -ENOMEM;
			break;
		}

		if (le16_to_cpu(htc_hdr->payld_len) > HTC_MAX_PAYLOAD_LENGTH) {
			ath6kl_err("payload len %d exceeds max htc : %d !\n",
				   htc_hdr->payld_len,
				   (u32) HTC_MAX_PAYLOAD_LENGTH);
			status = -ENOMEM;
			break;
		}

		if (endpoint->svc_id == 0) {
			ath6kl_err("ep %d is not connected !\n", htc_hdr->eid);
			status = -ENOMEM;
			break;
		}

		if (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) {
			/*
			 * HTC header indicates that every packet to follow
			 * has the same padded length so that it can be
			 * optimally fetched as a full bundle.
			 */
			n_msg = (htc_hdr->flags & HTC_FLG_RX_BNDL_CNT) >>
				HTC_FLG_RX_BNDL_CNT_S;

			/* the count doesn't include the starter frame */
			n_msg++;
			if (n_msg > target->msg_per_bndl_max) {
				status = -ENOMEM;
				break;
			}

			endpoint->ep_st.rx_bundle_from_hdr += 1;
K
Kalle Valo 已提交
1538
			ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
1539
				   "htc rx bundle pkts %d\n",
K
Kalle Valo 已提交
1540 1541 1542 1543 1544 1545
				   n_msg);
		} else
			/* HTC header only indicates 1 message to fetch */
			n_msg = 1;

		/* Setup packet buffers for each message */
1546 1547
		status = ath6kl_htc_rx_setup(target, endpoint, &lk_ahds[i],
					     queue, n_msg);
K
Kalle Valo 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589

		/*
		 * This is due to unavailabilty of buffers to rx entire data.
		 * Return no error so that free buffers from queue can be used
		 * to receive partial data.
		 */
		if (status == -ENOSPC) {
			spin_unlock_bh(&target->rx_lock);
			return 0;
		}

		if (status)
			break;
	}

	spin_unlock_bh(&target->rx_lock);

	if (status) {
		list_for_each_entry_safe(packet, tmp_pkt, queue, list) {
			list_del(&packet->list);
			htc_reclaim_rxbuf(target, packet,
					  &target->endpoint[packet->endpoint]);
		}
	}

	return status;
}

static void htc_ctrl_rx(struct htc_target *context, struct htc_packet *packets)
{
	if (packets->endpoint != ENDPOINT_0) {
		WARN_ON(1);
		return;
	}

	if (packets->status == -ECANCELED) {
		reclaim_rx_ctrl_buf(context, packets);
		return;
	}

	if (packets->act_len > 0) {
		ath6kl_err("htc_ctrl_rx, got message with len:%zu\n",
1590
			   packets->act_len + HTC_HDR_LENGTH);
K
Kalle Valo 已提交
1591

K
Kalle Valo 已提交
1592 1593
		ath6kl_dbg_dump(ATH6KL_DBG_HTC,
				"htc rx unexpected endpoint 0 message", "",
1594 1595
				packets->buf - HTC_HDR_LENGTH,
				packets->act_len + HTC_HDR_LENGTH);
K
Kalle Valo 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	}

	htc_reclaim_rxbuf(context, packets, &context->endpoint[0]);
}

static void htc_proc_cred_rpt(struct htc_target *target,
			      struct htc_credit_report *rpt,
			      int n_entries,
			      enum htc_endpoint_id from_ep)
{
	struct htc_endpoint *endpoint;
	int tot_credits = 0, i;
	bool dist = false;

	spin_lock_bh(&target->tx_lock);

	for (i = 0; i < n_entries; i++, rpt++) {
		if (rpt->eid >= ENDPOINT_MAX) {
			WARN_ON(1);
			spin_unlock_bh(&target->tx_lock);
			return;
		}

		endpoint = &target->endpoint[rpt->eid];

1621 1622
		ath6kl_dbg(ATH6KL_DBG_CREDIT,
			   "credit report ep %d credits %d\n",
K
Kalle Valo 已提交
1623
			   rpt->eid, rpt->credits);
K
Kalle Valo 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643

		endpoint->ep_st.tx_cred_rpt += 1;
		endpoint->ep_st.cred_retnd += rpt->credits;

		if (from_ep == rpt->eid) {
			/*
			 * This credit report arrived on the same endpoint
			 * indicating it arrived in an RX packet.
			 */
			endpoint->ep_st.cred_from_rx += rpt->credits;
			endpoint->ep_st.cred_rpt_from_rx += 1;
		} else if (from_ep == ENDPOINT_0) {
			/* credit arrived on endpoint 0 as a NULL message */
			endpoint->ep_st.cred_from_ep0 += rpt->credits;
			endpoint->ep_st.cred_rpt_ep0 += 1;
		} else {
			endpoint->ep_st.cred_from_other += rpt->credits;
			endpoint->ep_st.cred_rpt_from_other += 1;
		}

1644
		if (rpt->eid == ENDPOINT_0)
K
Kalle Valo 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
			/* always give endpoint 0 credits back */
			endpoint->cred_dist.credits += rpt->credits;
		else {
			endpoint->cred_dist.cred_to_dist += rpt->credits;
			dist = true;
		}

		/*
		 * Refresh tx depth for distribution function that will
		 * recover these credits NOTE: this is only valid when
		 * there are credits to recover!
		 */
		endpoint->cred_dist.txq_depth =
			get_queue_depth(&endpoint->txq);

		tot_credits += rpt->credits;
	}

	if (dist) {
		/*
		 * This was a credit return based on a completed send
		 * operations note, this is done with the lock held
		 */
1668
		ath6kl_credit_distribute(target->credit_info,
1669 1670
					 &target->cred_dist_list,
					 HTC_CREDIT_DIST_SEND_COMPLETE);
K
Kalle Valo 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
	}

	spin_unlock_bh(&target->tx_lock);

	if (tot_credits)
		htc_chk_ep_txq(target);
}

static int htc_parse_trailer(struct htc_target *target,
			     struct htc_record_hdr *record,
			     u8 *record_buf, u32 *next_lk_ahds,
			     enum htc_endpoint_id endpoint,
			     int *n_lk_ahds)
{
	struct htc_bundle_lkahd_rpt *bundle_lkahd_rpt;
	struct htc_lookahead_report *lk_ahd;
	int len;

	switch (record->rec_id) {
	case HTC_RECORD_CREDITS:
		len = record->len / sizeof(struct htc_credit_report);
		if (!len) {
			WARN_ON(1);
			return -EINVAL;
		}

		htc_proc_cred_rpt(target,
				  (struct htc_credit_report *) record_buf,
				  len, endpoint);
		break;
	case HTC_RECORD_LOOKAHEAD:
		len = record->len / sizeof(*lk_ahd);
		if (!len) {
			WARN_ON(1);
			return -EINVAL;
		}

		lk_ahd = (struct htc_lookahead_report *) record_buf;
1709 1710
		if ((lk_ahd->pre_valid == ((~lk_ahd->post_valid) & 0xFF)) &&
		    next_lk_ahds) {
K
Kalle Valo 已提交
1711

K
Kalle Valo 已提交
1712
			ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
1713
				   "htc rx lk_ahd found pre_valid 0x%x post_valid 0x%x\n",
K
Kalle Valo 已提交
1714 1715 1716 1717 1718
				   lk_ahd->pre_valid, lk_ahd->post_valid);

			/* look ahead bytes are valid, copy them over */
			memcpy((u8 *)&next_lk_ahds[0], lk_ahd->lk_ahd, 4);

K
Kalle Valo 已提交
1719 1720
			ath6kl_dbg_dump(ATH6KL_DBG_HTC,
					"htc rx next look ahead",
1721
					"", next_lk_ahds, 4);
K
Kalle Valo 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

			*n_lk_ahds = 1;
		}
		break;
	case HTC_RECORD_LOOKAHEAD_BUNDLE:
		len = record->len / sizeof(*bundle_lkahd_rpt);
		if (!len || (len > HTC_HOST_MAX_MSG_PER_BUNDLE)) {
			WARN_ON(1);
			return -EINVAL;
		}

		if (next_lk_ahds) {
			int i;

			bundle_lkahd_rpt =
				(struct htc_bundle_lkahd_rpt *) record_buf;

K
Kalle Valo 已提交
1739
			ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bundle lk_ahd",
1740
					"", record_buf, record->len);
K
Kalle Valo 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770

			for (i = 0; i < len; i++) {
				memcpy((u8 *)&next_lk_ahds[i],
				       bundle_lkahd_rpt->lk_ahd, 4);
				bundle_lkahd_rpt++;
			}

			*n_lk_ahds = i;
		}
		break;
	default:
		ath6kl_err("unhandled record: id:%d len:%d\n",
			   record->rec_id, record->len);
		break;
	}

	return 0;

}

static int htc_proc_trailer(struct htc_target *target,
			    u8 *buf, int len, u32 *next_lk_ahds,
			    int *n_lk_ahds, enum htc_endpoint_id endpoint)
{
	struct htc_record_hdr *record;
	int orig_len;
	int status;
	u8 *record_buf;
	u8 *orig_buf;

K
Kalle Valo 已提交
1771 1772
	ath6kl_dbg(ATH6KL_DBG_HTC, "htc rx trailer len %d\n", len);
	ath6kl_dbg_dump(ATH6KL_DBG_HTC, NULL, "", buf, len);
K
Kalle Valo 已提交
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807

	orig_buf = buf;
	orig_len = len;
	status = 0;

	while (len > 0) {

		if (len < sizeof(struct htc_record_hdr)) {
			status = -ENOMEM;
			break;
		}
		/* these are byte aligned structs */
		record = (struct htc_record_hdr *) buf;
		len -= sizeof(struct htc_record_hdr);
		buf += sizeof(struct htc_record_hdr);

		if (record->len > len) {
			ath6kl_err("invalid record len: %d (id:%d) buf has: %d bytes left\n",
				   record->len, record->rec_id, len);
			status = -ENOMEM;
			break;
		}
		record_buf = buf;

		status = htc_parse_trailer(target, record, record_buf,
					   next_lk_ahds, endpoint, n_lk_ahds);

		if (status)
			break;

		/* advance buffer past this record for next time around */
		buf += record->len;
		len -= record->len;
	}

1808
	if (status)
K
Kalle Valo 已提交
1809
		ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad trailer",
1810
				"", orig_buf, orig_len);
K
Kalle Valo 已提交
1811 1812 1813 1814

	return status;
}

1815 1816 1817
static int ath6kl_htc_rx_process_hdr(struct htc_target *target,
				     struct htc_packet *packet,
				     u32 *next_lkahds, int *n_lkahds)
K
Kalle Valo 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
{
	int status = 0;
	u16 payload_len;
	u32 lk_ahd;
	struct htc_frame_hdr *htc_hdr = (struct htc_frame_hdr *)packet->buf;

	if (n_lkahds != NULL)
		*n_lkahds = 0;

	/*
	 * NOTE: we cannot assume the alignment of buf, so we use the safe
	 * macros to retrieve 16 bit fields.
	 */
	payload_len = le16_to_cpu(get_unaligned(&htc_hdr->payld_len));

	memcpy((u8 *)&lk_ahd, packet->buf, sizeof(lk_ahd));

	if (packet->info.rx.rx_flags & HTC_RX_PKT_REFRESH_HDR) {
		/*
		 * Refresh the expected header and the actual length as it
		 * was unknown when this packet was grabbed as part of the
		 * bundle.
		 */
		packet->info.rx.exp_hdr = lk_ahd;
		packet->act_len = payload_len + HTC_HDR_LENGTH;

		/* validate the actual header that was refreshed  */
		if (packet->act_len > packet->buf_len) {
			ath6kl_err("refreshed hdr payload len (%d) in bundled recv is invalid (hdr: 0x%X)\n",
				   payload_len, lk_ahd);
			/*
			 * Limit this to max buffer just to print out some
			 * of the buffer.
			 */
			packet->act_len = min(packet->act_len, packet->buf_len);
			status = -ENOMEM;
			goto fail_rx;
		}

		if (packet->endpoint != htc_hdr->eid) {
			ath6kl_err("refreshed hdr ep (%d) does not match expected ep (%d)\n",
				   htc_hdr->eid, packet->endpoint);
			status = -ENOMEM;
			goto fail_rx;
		}
	}

	if (lk_ahd != packet->info.rx.exp_hdr) {
1866 1867
		ath6kl_err("%s(): lk_ahd mismatch! (pPkt:0x%p flags:0x%X)\n",
			   __func__, packet, packet->info.rx.rx_flags);
K
Kalle Valo 已提交
1868
		ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx expected lk_ahd",
1869
				"", &packet->info.rx.exp_hdr, 4);
K
Kalle Valo 已提交
1870
		ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx current header",
1871
				"", (u8 *)&lk_ahd, sizeof(lk_ahd));
K
Kalle Valo 已提交
1872 1873 1874 1875 1876 1877 1878
		status = -ENOMEM;
		goto fail_rx;
	}

	if (htc_hdr->flags & HTC_FLG_RX_TRAILER) {
		if (htc_hdr->ctrl[0] < sizeof(struct htc_record_hdr) ||
		    htc_hdr->ctrl[0] > payload_len) {
1879 1880
			ath6kl_err("%s(): invalid hdr (payload len should be :%d, CB[0] is:%d)\n",
				   __func__, payload_len, htc_hdr->ctrl[0]);
K
Kalle Valo 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
			status = -ENOMEM;
			goto fail_rx;
		}

		if (packet->info.rx.rx_flags & HTC_RX_PKT_IGNORE_LOOKAHEAD) {
			next_lkahds = NULL;
			n_lkahds = NULL;
		}

		status = htc_proc_trailer(target, packet->buf + HTC_HDR_LENGTH
					  + payload_len - htc_hdr->ctrl[0],
					  htc_hdr->ctrl[0], next_lkahds,
					   n_lkahds, packet->endpoint);

		if (status)
			goto fail_rx;

		packet->act_len -= htc_hdr->ctrl[0];
	}

	packet->buf += HTC_HDR_LENGTH;
	packet->act_len -= HTC_HDR_LENGTH;

fail_rx:
	if (status)
K
Kalle Valo 已提交
1906 1907
		ath6kl_dbg_dump(ATH6KL_DBG_HTC, "htc rx bad packet",
				"", packet->buf, packet->act_len);
K
Kalle Valo 已提交
1908 1909 1910 1911

	return status;
}

1912 1913
static void ath6kl_htc_rx_complete(struct htc_endpoint *endpoint,
				   struct htc_packet *packet)
K
Kalle Valo 已提交
1914
{
K
Kalle Valo 已提交
1915
		ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
1916
			   "htc rx complete ep %d packet 0x%p\n",
K
Kalle Valo 已提交
1917
			   endpoint->eid, packet);
1918

K
Kalle Valo 已提交
1919 1920 1921
		endpoint->ep_cb.rx(endpoint->target, packet);
}

1922 1923 1924 1925
static int ath6kl_htc_rx_bundle(struct htc_target *target,
				struct list_head *rxq,
				struct list_head *sync_compq,
				int *n_pkt_fetched, bool part_bundle)
K
Kalle Valo 已提交
1926 1927 1928
{
	struct hif_scatter_req *scat_req;
	struct htc_packet *packet;
1929
	int rem_space = target->max_rx_bndl_sz;
K
Kalle Valo 已提交
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
	int n_scat_pkt, status = 0, i, len;

	n_scat_pkt = get_queue_depth(rxq);
	n_scat_pkt = min(n_scat_pkt, target->msg_per_bndl_max);

	if ((get_queue_depth(rxq) - n_scat_pkt) > 0) {
		/*
		 * We were forced to split this bundle receive operation
		 * all packets in this partial bundle must have their
		 * lookaheads ignored.
		 */
		part_bundle = true;

		/*
		 * This would only happen if the target ignored our max
		 * bundle limit.
		 */
1947 1948
		ath6kl_warn("%s(): partial bundle detected num:%d , %d\n",
			    __func__, get_queue_depth(rxq), n_scat_pkt);
K
Kalle Valo 已提交
1949 1950 1951 1952
	}

	len = 0;

K
Kalle Valo 已提交
1953
	ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
1954 1955
		   "htc rx bundle depth %d pkts %d\n",
		   get_queue_depth(rxq), n_scat_pkt);
K
Kalle Valo 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967

	scat_req = hif_scatter_req_get(target->dev->ar);

	if (scat_req == NULL)
		goto fail_rx_pkt;

	for (i = 0; i < n_scat_pkt; i++) {
		int pad_len;

		packet = list_first_entry(rxq, struct htc_packet, list);
		list_del(&packet->list);

1968
		pad_len = CALC_TXRX_PADDED_LEN(target,
K
Kalle Valo 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
						   packet->act_len);

		if ((rem_space - pad_len) < 0) {
			list_add(&packet->list, rxq);
			break;
		}

		rem_space -= pad_len;

		if (part_bundle || (i < (n_scat_pkt - 1)))
			/*
			 * Packet 0..n-1 cannot be checked for look-aheads
			 * since we are fetching a bundle the last packet
			 * however can have it's lookahead used
			 */
			packet->info.rx.rx_flags |=
			    HTC_RX_PKT_IGNORE_LOOKAHEAD;

		/* NOTE: 1 HTC packet per scatter entry */
		scat_req->scat_list[i].buf = packet->buf;
		scat_req->scat_list[i].len = pad_len;

		packet->info.rx.rx_flags |= HTC_RX_PKT_PART_OF_BUNDLE;

		list_add_tail(&packet->list, sync_compq);

		WARN_ON(!scat_req->scat_list[i].len);
		len += scat_req->scat_list[i].len;
	}

	scat_req->len = len;
	scat_req->scat_entries = i;

K
Kalle Valo 已提交
2002
	status = ath6kl_hif_submit_scat_req(target->dev, scat_req, true);
K
Kalle Valo 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014

	if (!status)
		*n_pkt_fetched = i;

	/* free scatter request */
	hif_scatter_req_add(target->dev->ar, scat_req);

fail_rx_pkt:

	return status;
}

2015 2016 2017 2018
static int ath6kl_htc_rx_process_packets(struct htc_target *target,
					 struct list_head *comp_pktq,
					 u32 lk_ahds[],
					 int *n_lk_ahd)
K
Kalle Valo 已提交
2019 2020 2021 2022 2023 2024 2025 2026
{
	struct htc_packet *packet, *tmp_pkt;
	struct htc_endpoint *ep;
	int status = 0;

	list_for_each_entry_safe(packet, tmp_pkt, comp_pktq, list) {
		ep = &target->endpoint[packet->endpoint];

2027 2028 2029
		trace_ath6kl_htc_rx(packet->status, packet->endpoint,
				    packet->buf, packet->act_len);

K
Kalle Valo 已提交
2030
		/* process header for each of the recv packet */
2031 2032
		status = ath6kl_htc_rx_process_hdr(target, packet, lk_ahds,
						   n_lk_ahd);
K
Kalle Valo 已提交
2033 2034 2035
		if (status)
			return status;

2036 2037
		list_del(&packet->list);

K
Kalle Valo 已提交
2038 2039 2040 2041 2042 2043
		if (list_empty(comp_pktq)) {
			/*
			 * Last packet's more packet flag is set
			 * based on the lookahead.
			 */
			if (*n_lk_ahd > 0)
2044 2045
				ath6kl_htc_rx_set_indicate(lk_ahds[0],
							   ep, packet);
K
Kalle Valo 已提交
2046 2047 2048 2049 2050 2051 2052 2053
		} else
			/*
			 * Packets in a bundle automatically have
			 * this flag set.
			 */
			packet->info.rx.indicat_flags |=
				HTC_RX_FLAGS_INDICATE_MORE_PKTS;

2054
		ath6kl_htc_rx_update_stats(ep, *n_lk_ahd);
K
Kalle Valo 已提交
2055 2056 2057 2058

		if (packet->info.rx.rx_flags & HTC_RX_PKT_PART_OF_BUNDLE)
			ep->ep_st.rx_bundl += 1;

2059
		ath6kl_htc_rx_complete(ep, packet);
K
Kalle Valo 已提交
2060 2061 2062 2063 2064
	}

	return status;
}

2065 2066 2067
static int ath6kl_htc_rx_fetch(struct htc_target *target,
			       struct list_head *rx_pktq,
			       struct list_head *comp_pktq)
K
Kalle Valo 已提交
2068 2069 2070 2071
{
	int fetched_pkts;
	bool part_bundle = false;
	int status = 0;
2072 2073
	struct list_head tmp_rxq;
	struct htc_packet *packet, *tmp_pkt;
K
Kalle Valo 已提交
2074 2075 2076 2077 2078

	/* now go fetch the list of HTC packets */
	while (!list_empty(rx_pktq)) {
		fetched_pkts = 0;

2079 2080
		INIT_LIST_HEAD(&tmp_rxq);

K
Kalle Valo 已提交
2081 2082 2083 2084 2085 2086
		if (target->rx_bndl_enable && (get_queue_depth(rx_pktq) > 1)) {
			/*
			 * There are enough packets to attempt a
			 * bundle transfer and recv bundling is
			 * allowed.
			 */
2087
			status = ath6kl_htc_rx_bundle(target, rx_pktq,
2088
						      &tmp_rxq,
2089 2090
						      &fetched_pkts,
						      part_bundle);
K
Kalle Valo 已提交
2091
			if (status)
2092
				goto fail_rx;
K
Kalle Valo 已提交
2093 2094 2095

			if (!list_empty(rx_pktq))
				part_bundle = true;
2096 2097

			list_splice_tail_init(&tmp_rxq, comp_pktq);
K
Kalle Valo 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
		}

		if (!fetched_pkts) {

			packet = list_first_entry(rx_pktq, struct htc_packet,
						   list);

			/* fully synchronous */
			packet->completion = NULL;

2108
			if (!list_is_singular(rx_pktq))
K
Kalle Valo 已提交
2109 2110 2111 2112 2113 2114 2115 2116 2117
				/*
				 * look_aheads in all packet
				 * except the last one in the
				 * bundle must be ignored
				 */
				packet->info.rx.rx_flags |=
					HTC_RX_PKT_IGNORE_LOOKAHEAD;

			/* go fetch the packet */
2118 2119
			status = ath6kl_htc_rx_packet(target, packet,
						      packet->act_len);
2120 2121 2122

			list_move_tail(&packet->list, &tmp_rxq);

K
Kalle Valo 已提交
2123
			if (status)
2124
				goto fail_rx;
K
Kalle Valo 已提交
2125

2126
			list_splice_tail_init(&tmp_rxq, comp_pktq);
K
Kalle Valo 已提交
2127 2128 2129
		}
	}

2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
	return 0;

fail_rx:

	/*
	 * Cleanup any packets we allocated but didn't use to
	 * actually fetch any packets.
	 */

	list_for_each_entry_safe(packet, tmp_pkt, rx_pktq, list) {
		list_del(&packet->list);
		htc_reclaim_rxbuf(target, packet,
2142
				  &target->endpoint[packet->endpoint]);
2143 2144 2145 2146 2147
	}

	list_for_each_entry_safe(packet, tmp_pkt, &tmp_rxq, list) {
		list_del(&packet->list);
		htc_reclaim_rxbuf(target, packet,
2148
				  &target->endpoint[packet->endpoint]);
2149 2150
	}

K
Kalle Valo 已提交
2151 2152 2153
	return status;
}

2154
int ath6kl_htc_rxmsg_pending_handler(struct htc_target *target,
2155
				     u32 msg_look_ahead, int *num_pkts)
K
Kalle Valo 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
{
	struct htc_packet *packets, *tmp_pkt;
	struct htc_endpoint *endpoint;
	struct list_head rx_pktq, comp_pktq;
	int status = 0;
	u32 look_aheads[HTC_HOST_MAX_MSG_PER_BUNDLE];
	int num_look_ahead = 1;
	enum htc_endpoint_id id;
	int n_fetched = 0;

2166
	INIT_LIST_HEAD(&comp_pktq);
K
Kalle Valo 已提交
2167 2168 2169 2170 2171 2172
	*num_pkts = 0;

	/*
	 * On first entry copy the look_aheads into our temp array for
	 * processing
	 */
2173
	look_aheads[0] = msg_look_ahead;
K
Kalle Valo 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197

	while (true) {

		/*
		 * First lookahead sets the expected endpoint IDs for all
		 * packets in a bundle.
		 */
		id = ((struct htc_frame_hdr *)&look_aheads[0])->eid;
		endpoint = &target->endpoint[id];

		if (id >= ENDPOINT_MAX) {
			ath6kl_err("MsgPend, invalid endpoint in look-ahead: %d\n",
				   id);
			status = -ENOMEM;
			break;
		}

		INIT_LIST_HEAD(&rx_pktq);
		INIT_LIST_HEAD(&comp_pktq);

		/*
		 * Try to allocate as many HTC RX packets indicated by the
		 * look_aheads.
		 */
2198 2199 2200
		status = ath6kl_htc_rx_alloc(target, look_aheads,
					     num_look_ahead, endpoint,
					     &rx_pktq);
K
Kalle Valo 已提交
2201 2202 2203 2204 2205 2206 2207 2208
		if (status)
			break;

		if (get_queue_depth(&rx_pktq) >= 2)
			/*
			 * A recv bundle was detected, force IRQ status
			 * re-check again
			 */
2209
			target->chk_irq_status_cnt = 1;
K
Kalle Valo 已提交
2210 2211 2212 2213 2214

		n_fetched += get_queue_depth(&rx_pktq);

		num_look_ahead = 0;

2215
		status = ath6kl_htc_rx_fetch(target, &rx_pktq, &comp_pktq);
K
Kalle Valo 已提交
2216 2217

		if (!status)
2218
			ath6kl_htc_rx_chk_water_mark(endpoint);
K
Kalle Valo 已提交
2219 2220

		/* Process fetched packets */
2221 2222 2223
		status = ath6kl_htc_rx_process_packets(target, &comp_pktq,
						       look_aheads,
						       &num_look_ahead);
K
Kalle Valo 已提交
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

		if (!num_look_ahead || status)
			break;

		/*
		 * For SYNCH processing, if we get here, we are running
		 * through the loop again due to a detected lookahead. Set
		 * flag that we should re-check IRQ status registers again
		 * before leaving IRQ processing, this can net better
		 * performance in high throughput situations.
		 */
2235
		target->chk_irq_status_cnt = 1;
K
Kalle Valo 已提交
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
	}

	if (status) {
		ath6kl_err("failed to get pending recv messages: %d\n",
			   status);

		/* cleanup any packets in sync completion queue */
		list_for_each_entry_safe(packets, tmp_pkt, &comp_pktq, list) {
			list_del(&packets->list);
			htc_reclaim_rxbuf(target, packets,
					  &target->endpoint[packets->endpoint]);
		}

		if (target->htc_flags & HTC_OP_STATE_STOPPING) {
			ath6kl_warn("host is going to stop blocking receiver for htc_stop\n");
K
Kalle Valo 已提交
2251
			ath6kl_hif_rx_control(target->dev, false);
K
Kalle Valo 已提交
2252 2253 2254 2255 2256 2257 2258 2259 2260
		}
	}

	/*
	 * Before leaving, check to see if host ran out of buffers and
	 * needs to stop the receiver.
	 */
	if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
		ath6kl_warn("host has no rx buffers blocking receiver to prevent overrun\n");
K
Kalle Valo 已提交
2261
		ath6kl_hif_rx_control(target->dev, false);
K
Kalle Valo 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
	}
	*num_pkts = n_fetched;

	return status;
}

/*
 * Synchronously wait for a control message from the target,
 * This function is used at initialization time ONLY.  At init messages
 * on ENDPOINT 0 are expected.
 */
static struct htc_packet *htc_wait_for_ctrl_msg(struct htc_target *target)
{
	struct htc_packet *packet = NULL;
	struct htc_frame_hdr *htc_hdr;
	u32 look_ahead;

K
Kalle Valo 已提交
2279
	if (ath6kl_hif_poll_mboxmsg_rx(target->dev, &look_ahead,
2280
				       HTC_TARGET_RESPONSE_TIMEOUT))
K
Kalle Valo 已提交
2281 2282
		return NULL;

K
Kalle Valo 已提交
2283
	ath6kl_dbg(ATH6KL_DBG_HTC,
2284
		   "htc rx wait ctrl look_ahead 0x%X\n", look_ahead);
K
Kalle Valo 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306

	htc_hdr = (struct htc_frame_hdr *)&look_ahead;

	if (htc_hdr->eid != ENDPOINT_0)
		return NULL;

	packet = htc_get_control_buf(target, false);

	if (!packet)
		return NULL;

	packet->info.rx.rx_flags = 0;
	packet->info.rx.exp_hdr = look_ahead;
	packet->act_len = le16_to_cpu(htc_hdr->payld_len) + HTC_HDR_LENGTH;

	if (packet->act_len > packet->buf_len)
		goto fail_ctrl_rx;

	/* we want synchronous operation */
	packet->completion = NULL;

	/* get the message from the device, this will block */
2307
	if (ath6kl_htc_rx_packet(target, packet, packet->act_len))
K
Kalle Valo 已提交
2308 2309
		goto fail_ctrl_rx;

2310 2311 2312
	trace_ath6kl_htc_rx(packet->status, packet->endpoint,
			    packet->buf, packet->act_len);

K
Kalle Valo 已提交
2313
	/* process receive header */
2314
	packet->status = ath6kl_htc_rx_process_hdr(target, packet, NULL, NULL);
K
Kalle Valo 已提交
2315 2316

	if (packet->status) {
2317
		ath6kl_err("htc_wait_for_ctrl_msg, ath6kl_htc_rx_process_hdr failed (status = %d)\n",
K
Kalle Valo 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
			   packet->status);
		goto fail_ctrl_rx;
	}

	return packet;

fail_ctrl_rx:
	if (packet != NULL) {
		htc_rxpkt_reset(packet);
		reclaim_rx_ctrl_buf(target, packet);
	}

	return NULL;
}

K
Kalle Valo 已提交
2333
static int ath6kl_htc_mbox_add_rxbuf_multiple(struct htc_target *target,
2334
				  struct list_head *pkt_queue)
K
Kalle Valo 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
{
	struct htc_endpoint *endpoint;
	struct htc_packet *first_pkt;
	bool rx_unblock = false;
	int status = 0, depth;

	if (list_empty(pkt_queue))
		return -ENOMEM;

	first_pkt = list_first_entry(pkt_queue, struct htc_packet, list);

	if (first_pkt->endpoint >= ENDPOINT_MAX)
		return status;

	depth = get_queue_depth(pkt_queue);

K
Kalle Valo 已提交
2351
	ath6kl_dbg(ATH6KL_DBG_HTC,
2352
		   "htc rx add multiple ep id %d cnt %d len %d\n",
K
Kalle Valo 已提交
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
		first_pkt->endpoint, depth, first_pkt->buf_len);

	endpoint = &target->endpoint[first_pkt->endpoint];

	if (target->htc_flags & HTC_OP_STATE_STOPPING) {
		struct htc_packet *packet, *tmp_pkt;

		/* walk through queue and mark each one canceled */
		list_for_each_entry_safe(packet, tmp_pkt, pkt_queue, list) {
			packet->status = -ECANCELED;
			list_del(&packet->list);
2364
			ath6kl_htc_rx_complete(endpoint, packet);
K
Kalle Valo 已提交
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
		}

		return status;
	}

	spin_lock_bh(&target->rx_lock);

	list_splice_tail_init(pkt_queue, &endpoint->rx_bufq);

	/* check if we are blocked waiting for a new buffer */
	if (target->rx_st_flags & HTC_RECV_WAIT_BUFFERS) {
		if (target->ep_waiting == first_pkt->endpoint) {
K
Kalle Valo 已提交
2377
			ath6kl_dbg(ATH6KL_DBG_HTC,
2378 2379
				   "htc rx blocked on ep %d, unblocking\n",
				   target->ep_waiting);
K
Kalle Valo 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
			target->rx_st_flags &= ~HTC_RECV_WAIT_BUFFERS;
			target->ep_waiting = ENDPOINT_MAX;
			rx_unblock = true;
		}
	}

	spin_unlock_bh(&target->rx_lock);

	if (rx_unblock && !(target->htc_flags & HTC_OP_STATE_STOPPING))
		/* TODO : implement a buffer threshold count? */
K
Kalle Valo 已提交
2390
		ath6kl_hif_rx_control(target->dev, true);
K
Kalle Valo 已提交
2391 2392 2393 2394

	return status;
}

K
Kalle Valo 已提交
2395
static void ath6kl_htc_mbox_flush_rx_buf(struct htc_target *target)
K
Kalle Valo 已提交
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
{
	struct htc_endpoint *endpoint;
	struct htc_packet *packet, *tmp_pkt;
	int i;

	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
		endpoint = &target->endpoint[i];
		if (!endpoint->svc_id)
			/* not in use.. */
			continue;

		spin_lock_bh(&target->rx_lock);
		list_for_each_entry_safe(packet, tmp_pkt,
					 &endpoint->rx_bufq, list) {
			list_del(&packet->list);
			spin_unlock_bh(&target->rx_lock);
K
Kalle Valo 已提交
2412
			ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
2413
				   "htc rx flush pkt 0x%p  len %d  ep %d\n",
K
Kalle Valo 已提交
2414 2415
				   packet, packet->buf_len,
				   packet->endpoint);
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
			/*
			 * packets in rx_bufq of endpoint 0 have originally
			 * been queued from target->free_ctrl_rxbuf where
			 * packet and packet->buf_start are allocated
			 * separately using kmalloc(). For other endpoint
			 * rx_bufq, it is allocated as skb where packet is
			 * skb->head. Take care of this difference while freeing
			 * the memory.
			 */
			if (packet->endpoint == ENDPOINT_0) {
				kfree(packet->buf_start);
				kfree(packet);
			} else {
				dev_kfree_skb(packet->pkt_cntxt);
			}
K
Kalle Valo 已提交
2431 2432 2433 2434 2435 2436
			spin_lock_bh(&target->rx_lock);
		}
		spin_unlock_bh(&target->rx_lock);
	}
}

K
Kalle Valo 已提交
2437
static int ath6kl_htc_mbox_conn_service(struct htc_target *target,
2438 2439
			    struct htc_service_connect_req *conn_req,
			    struct htc_service_connect_resp *conn_resp)
K
Kalle Valo 已提交
2440 2441 2442 2443 2444 2445 2446 2447 2448
{
	struct htc_packet *rx_pkt = NULL;
	struct htc_packet *tx_pkt = NULL;
	struct htc_conn_service_resp *resp_msg;
	struct htc_conn_service_msg *conn_msg;
	struct htc_endpoint *endpoint;
	enum htc_endpoint_id assigned_ep = ENDPOINT_MAX;
	unsigned int max_msg_sz = 0;
	int status = 0;
K
Kalle Valo 已提交
2449
	u16 msg_id;
K
Kalle Valo 已提交
2450

K
Kalle Valo 已提交
2451
	ath6kl_dbg(ATH6KL_DBG_HTC,
K
Kalle Valo 已提交
2452
		   "htc connect service target 0x%p service id 0x%x\n",
K
Kalle Valo 已提交
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
		   target, conn_req->svc_id);

	if (conn_req->svc_id == HTC_CTRL_RSVD_SVC) {
		/* special case for pseudo control service */
		assigned_ep = ENDPOINT_0;
		max_msg_sz = HTC_MAX_CTRL_MSG_LEN;
	} else {
		/* allocate a packet to send to the target */
		tx_pkt = htc_get_control_buf(target, true);

		if (!tx_pkt)
			return -ENOMEM;

		conn_msg = (struct htc_conn_service_msg *)tx_pkt->buf;
		memset(conn_msg, 0, sizeof(*conn_msg));
		conn_msg->msg_id = cpu_to_le16(HTC_MSG_CONN_SVC_ID);
		conn_msg->svc_id = cpu_to_le16(conn_req->svc_id);
		conn_msg->conn_flags = cpu_to_le16(conn_req->conn_flags);

		set_htc_pkt_info(tx_pkt, NULL, (u8 *) conn_msg,
				 sizeof(*conn_msg) + conn_msg->svc_meta_len,
				 ENDPOINT_0, HTC_SERVICE_TX_PACKET_TAG);

		/* we want synchronous operation */
		tx_pkt->completion = NULL;
2478 2479
		ath6kl_htc_tx_prep_pkt(tx_pkt, 0, 0, 0);
		status = ath6kl_htc_tx_issue(target, tx_pkt);
K
Kalle Valo 已提交
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492

		if (status)
			goto fail_tx;

		/* wait for response */
		rx_pkt = htc_wait_for_ctrl_msg(target);

		if (!rx_pkt) {
			status = -ENOMEM;
			goto fail_tx;
		}

		resp_msg = (struct htc_conn_service_resp *)rx_pkt->buf;
K
Kalle Valo 已提交
2493
		msg_id = le16_to_cpu(resp_msg->msg_id);
K
Kalle Valo 已提交
2494

K
Kalle Valo 已提交
2495
		if ((msg_id != HTC_MSG_CONN_SVC_RESP_ID) ||
2496
		    (rx_pkt->act_len < sizeof(*resp_msg))) {
K
Kalle Valo 已提交
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
			status = -ENOMEM;
			goto fail_tx;
		}

		conn_resp->resp_code = resp_msg->status;
		/* check response status */
		if (resp_msg->status != HTC_SERVICE_SUCCESS) {
			ath6kl_err("target failed service 0x%X connect request (status:%d)\n",
				   resp_msg->svc_id, resp_msg->status);
			status = -ENOMEM;
			goto fail_tx;
		}

		assigned_ep = (enum htc_endpoint_id)resp_msg->eid;
		max_msg_sz = le16_to_cpu(resp_msg->max_msg_sz);
	}

2514 2515
	if (WARN_ON_ONCE(assigned_ep == ENDPOINT_UNUSED ||
			 assigned_ep >= ENDPOINT_MAX || !max_msg_sz)) {
K
Kalle Valo 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
		status = -ENOMEM;
		goto fail_tx;
	}

	endpoint = &target->endpoint[assigned_ep];
	endpoint->eid = assigned_ep;
	if (endpoint->svc_id) {
		status = -ENOMEM;
		goto fail_tx;
	}

	/* return assigned endpoint to caller */
	conn_resp->endpoint = assigned_ep;
	conn_resp->len_max = max_msg_sz;

	/* setup the endpoint */

	/* this marks the endpoint in use */
	endpoint->svc_id = conn_req->svc_id;

	endpoint->max_txq_depth = conn_req->max_txq_depth;
	endpoint->len_max = max_msg_sz;
	endpoint->ep_cb = conn_req->ep_cb;
	endpoint->cred_dist.svc_id = conn_req->svc_id;
2540
	endpoint->cred_dist.htc_ep = endpoint;
K
Kalle Valo 已提交
2541 2542 2543
	endpoint->cred_dist.endpoint = assigned_ep;
	endpoint->cred_dist.cred_sz = target->tgt_cred_sz;

2544 2545 2546 2547 2548 2549 2550 2551 2552
	switch (endpoint->svc_id) {
	case WMI_DATA_BK_SVC:
		endpoint->tx_drop_packet_threshold = MAX_DEF_COOKIE_NUM / 3;
		break;
	default:
		endpoint->tx_drop_packet_threshold = MAX_HI_COOKIE_NUM;
		break;
	}

K
Kalle Valo 已提交
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
	if (conn_req->max_rxmsg_sz) {
		/*
		 * Override cred_per_msg calculation, this optimizes
		 * the credit-low indications since the host will actually
		 * issue smaller messages in the Send path.
		 */
		if (conn_req->max_rxmsg_sz > max_msg_sz) {
			status = -ENOMEM;
			goto fail_tx;
		}
		endpoint->cred_dist.cred_per_msg =
		    conn_req->max_rxmsg_sz / target->tgt_cred_sz;
	} else
		endpoint->cred_dist.cred_per_msg =
		    max_msg_sz / target->tgt_cred_sz;

	if (!endpoint->cred_dist.cred_per_msg)
		endpoint->cred_dist.cred_per_msg = 1;

	/* save local connection flags */
	endpoint->conn_flags = conn_req->flags;

fail_tx:
	if (tx_pkt)
		htc_reclaim_txctrl_buf(target, tx_pkt);

	if (rx_pkt) {
		htc_rxpkt_reset(rx_pkt);
		reclaim_rx_ctrl_buf(target, rx_pkt);
	}

	return status;
}

static void reset_ep_state(struct htc_target *target)
{
	struct htc_endpoint *endpoint;
	int i;

	for (i = ENDPOINT_0; i < ENDPOINT_MAX; i++) {
		endpoint = &target->endpoint[i];
		memset(&endpoint->cred_dist, 0, sizeof(endpoint->cred_dist));
		endpoint->svc_id = 0;
		endpoint->len_max = 0;
		endpoint->max_txq_depth = 0;
		memset(&endpoint->ep_st, 0,
		       sizeof(endpoint->ep_st));
		INIT_LIST_HEAD(&endpoint->rx_bufq);
		INIT_LIST_HEAD(&endpoint->txq);
		endpoint->target = target;
	}

	/* reset distribution list */
2606
	/* FIXME: free existing entries */
K
Kalle Valo 已提交
2607 2608 2609
	INIT_LIST_HEAD(&target->cred_dist_list);
}

K
Kalle Valo 已提交
2610
static int ath6kl_htc_mbox_get_rxbuf_num(struct htc_target *target,
2611
			     enum htc_endpoint_id endpoint)
K
Kalle Valo 已提交
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
{
	int num;

	spin_lock_bh(&target->rx_lock);
	num = get_queue_depth(&(target->endpoint[endpoint].rx_bufq));
	spin_unlock_bh(&target->rx_lock);
	return num;
}

static void htc_setup_msg_bndl(struct htc_target *target)
{
	/* limit what HTC can handle */
	target->msg_per_bndl_max = min(HTC_HOST_MAX_MSG_PER_BUNDLE,
				       target->msg_per_bndl_max);

2627
	if (ath6kl_hif_enable_scatter(target->dev->ar)) {
K
Kalle Valo 已提交
2628 2629 2630 2631 2632
		target->msg_per_bndl_max = 0;
		return;
	}

	/* limit bundle what the device layer can handle */
2633
	target->msg_per_bndl_max = min(target->max_scat_entries,
K
Kalle Valo 已提交
2634 2635
				       target->msg_per_bndl_max);

K
Kalle Valo 已提交
2636
	ath6kl_dbg(ATH6KL_DBG_BOOT,
K
Kalle Valo 已提交
2637
		   "htc bundling allowed msg_per_bndl_max %d\n",
K
Kalle Valo 已提交
2638 2639 2640
		   target->msg_per_bndl_max);

	/* Max rx bundle size is limited by the max tx bundle size */
2641
	target->max_rx_bndl_sz = target->max_xfer_szper_scatreq;
K
Kalle Valo 已提交
2642
	/* Max tx bundle size if limited by the extended mbox address range */
2643
	target->max_tx_bndl_sz = min(HIF_MBOX0_EXT_WIDTH,
2644
				     target->max_xfer_szper_scatreq);
K
Kalle Valo 已提交
2645

K
Kalle Valo 已提交
2646
	ath6kl_dbg(ATH6KL_DBG_BOOT, "htc max_rx_bndl_sz %d max_tx_bndl_sz %d\n",
2647
		   target->max_rx_bndl_sz, target->max_tx_bndl_sz);
K
Kalle Valo 已提交
2648

2649
	if (target->max_tx_bndl_sz)
2650 2651
		/* tx_bndl_mask is enabled per AC, each has 1 bit */
		target->tx_bndl_mask = (1 << WMM_NUM_AC) - 1;
K
Kalle Valo 已提交
2652

2653
	if (target->max_rx_bndl_sz)
K
Kalle Valo 已提交
2654 2655
		target->rx_bndl_enable = true;

2656
	if ((target->tgt_cred_sz % target->block_sz) != 0) {
K
Kalle Valo 已提交
2657 2658 2659 2660 2661 2662 2663 2664 2665
		ath6kl_warn("credit size: %d is not block aligned! Disabling send bundling\n",
			    target->tgt_cred_sz);

		/*
		 * Disallow send bundling since the credit size is
		 * not aligned to a block size the I/O block
		 * padding will spill into the next credit buffer
		 * which is fatal.
		 */
2666
		target->tx_bndl_mask = 0;
K
Kalle Valo 已提交
2667 2668 2669
	}
}

K
Kalle Valo 已提交
2670
static int ath6kl_htc_mbox_wait_target(struct htc_target *target)
K
Kalle Valo 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
{
	struct htc_packet *packet = NULL;
	struct htc_ready_ext_msg *rdy_msg;
	struct htc_service_connect_req connect;
	struct htc_service_connect_resp resp;
	int status;

	/* we should be getting 1 control message that the target is ready */
	packet = htc_wait_for_ctrl_msg(target);

	if (!packet)
		return -ENOMEM;

	/* we controlled the buffer creation so it's properly aligned */
	rdy_msg = (struct htc_ready_ext_msg *)packet->buf;

	if ((le16_to_cpu(rdy_msg->ver2_0_info.msg_id) != HTC_MSG_READY_ID) ||
	    (packet->act_len < sizeof(struct htc_ready_msg))) {
		status = -ENOMEM;
		goto fail_wait_target;
	}

	if (!rdy_msg->ver2_0_info.cred_cnt || !rdy_msg->ver2_0_info.cred_sz) {
		status = -ENOMEM;
		goto fail_wait_target;
	}

	target->tgt_creds = le16_to_cpu(rdy_msg->ver2_0_info.cred_cnt);
	target->tgt_cred_sz = le16_to_cpu(rdy_msg->ver2_0_info.cred_sz);

K
Kalle Valo 已提交
2701
	ath6kl_dbg(ATH6KL_DBG_BOOT,
K
Kalle Valo 已提交
2702
		   "htc target ready credits %d size %d\n",
K
Kalle Valo 已提交
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
		   target->tgt_creds, target->tgt_cred_sz);

	/* check if this is an extended ready message */
	if (packet->act_len >= sizeof(struct htc_ready_ext_msg)) {
		/* this is an extended message */
		target->htc_tgt_ver = rdy_msg->htc_ver;
		target->msg_per_bndl_max = rdy_msg->msg_per_htc_bndl;
	} else {
		/* legacy */
		target->htc_tgt_ver = HTC_VERSION_2P0;
		target->msg_per_bndl_max = 0;
	}

K
Kalle Valo 已提交
2716
	ath6kl_dbg(ATH6KL_DBG_BOOT, "htc using protocol %s (%d)\n",
2717 2718
		   (target->htc_tgt_ver == HTC_VERSION_2P0) ? "2.0" : ">= 2.1",
		   target->htc_tgt_ver);
K
Kalle Valo 已提交
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732

	if (target->msg_per_bndl_max > 0)
		htc_setup_msg_bndl(target);

	/* setup our pseudo HTC control endpoint connection */
	memset(&connect, 0, sizeof(connect));
	memset(&resp, 0, sizeof(resp));
	connect.ep_cb.rx = htc_ctrl_rx;
	connect.ep_cb.rx_refill = NULL;
	connect.ep_cb.tx_full = NULL;
	connect.max_txq_depth = NUM_CONTROL_BUFFERS;
	connect.svc_id = HTC_CTRL_RSVD_SVC;

	/* connect fake service */
K
Kalle Valo 已提交
2733
	status = ath6kl_htc_mbox_conn_service((void *)target, &connect, &resp);
K
Kalle Valo 已提交
2734 2735

	if (status)
2736 2737
		/*
		 * FIXME: this call doesn't make sense, the caller should
K
Kalle Valo 已提交
2738
		 * call ath6kl_htc_mbox_cleanup() when it wants remove htc
2739
		 */
K
Kalle Valo 已提交
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
		ath6kl_hif_cleanup_scatter(target->dev->ar);

fail_wait_target:
	if (packet) {
		htc_rxpkt_reset(packet);
		reclaim_rx_ctrl_buf(target, packet);
	}

	return status;
}

/*
 * Start HTC, enable interrupts and let the target know
 * host has finished setup.
 */
K
Kalle Valo 已提交
2755
static int ath6kl_htc_mbox_start(struct htc_target *target)
K
Kalle Valo 已提交
2756 2757 2758 2759
{
	struct htc_packet *packet;
	int status;

2760 2761 2762
	memset(&target->dev->irq_proc_reg, 0,
	       sizeof(target->dev->irq_proc_reg));

K
Kalle Valo 已提交
2763
	/* Disable interrupts at the chip level */
K
Kalle Valo 已提交
2764
	ath6kl_hif_disable_intrs(target->dev);
K
Kalle Valo 已提交
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776

	target->htc_flags = 0;
	target->rx_st_flags = 0;

	/* Push control receive buffers into htc control endpoint */
	while ((packet = htc_get_control_buf(target, false)) != NULL) {
		status = htc_add_rxbuf(target, packet);
		if (status)
			return status;
	}

	/* NOTE: the first entry in the distribution list is ENDPOINT_0 */
2777
	ath6kl_credit_init(target->credit_info, &target->cred_dist_list,
2778
			   target->tgt_creds);
K
Kalle Valo 已提交
2779 2780 2781 2782 2783 2784 2785 2786 2787 2788

	dump_cred_dist_stats(target);

	/* Indicate to the target of the setup completion */
	status = htc_setup_tx_complete(target);

	if (status)
		return status;

	/* unmask interrupts */
K
Kalle Valo 已提交
2789
	status = ath6kl_hif_unmask_intrs(target->dev);
K
Kalle Valo 已提交
2790 2791

	if (status)
K
Kalle Valo 已提交
2792
		ath6kl_htc_mbox_stop(target);
K
Kalle Valo 已提交
2793 2794 2795 2796

	return status;
}

K
Kalle Valo 已提交
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
static int ath6kl_htc_reset(struct htc_target *target)
{
	u32 block_size, ctrl_bufsz;
	struct htc_packet *packet;
	int i;

	reset_ep_state(target);

	block_size = target->dev->ar->mbox_info.block_size;

	ctrl_bufsz = (block_size > HTC_MAX_CTRL_MSG_LEN) ?
		      (block_size + HTC_HDR_LENGTH) :
		      (HTC_MAX_CTRL_MSG_LEN + HTC_HDR_LENGTH);

	for (i = 0; i < NUM_CONTROL_BUFFERS; i++) {
		packet = kzalloc(sizeof(*packet), GFP_KERNEL);
		if (!packet)
			return -ENOMEM;

		packet->buf_start = kzalloc(ctrl_bufsz, GFP_KERNEL);
		if (!packet->buf_start) {
			kfree(packet);
			return -ENOMEM;
		}

		packet->buf_len = ctrl_bufsz;
		if (i < NUM_CONTROL_RX_BUFFERS) {
			packet->act_len = 0;
			packet->buf = packet->buf_start;
			packet->endpoint = ENDPOINT_0;
			list_add_tail(&packet->list, &target->free_ctrl_rxbuf);
		} else
			list_add_tail(&packet->list, &target->free_ctrl_txbuf);
	}

	return 0;
}

K
Kalle Valo 已提交
2835
/* htc_stop: stop interrupt reception, and flush all queued buffers */
K
Kalle Valo 已提交
2836
static void ath6kl_htc_mbox_stop(struct htc_target *target)
K
Kalle Valo 已提交
2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
{
	spin_lock_bh(&target->htc_lock);
	target->htc_flags |= HTC_OP_STATE_STOPPING;
	spin_unlock_bh(&target->htc_lock);

	/*
	 * Masking interrupts is a synchronous operation, when this
	 * function returns all pending HIF I/O has completed, we can
	 * safely flush the queues.
	 */
K
Kalle Valo 已提交
2847
	ath6kl_hif_mask_intrs(target->dev);
K
Kalle Valo 已提交
2848

2849
	ath6kl_htc_flush_txep_all(target);
K
Kalle Valo 已提交
2850

K
Kalle Valo 已提交
2851
	ath6kl_htc_mbox_flush_rx_buf(target);
K
Kalle Valo 已提交
2852

K
Kalle Valo 已提交
2853
	ath6kl_htc_reset(target);
K
Kalle Valo 已提交
2854 2855
}

K
Kalle Valo 已提交
2856
static void *ath6kl_htc_mbox_create(struct ath6kl *ar)
K
Kalle Valo 已提交
2857 2858
{
	struct htc_target *target = NULL;
K
Kalle Valo 已提交
2859
	int status = 0;
K
Kalle Valo 已提交
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870

	target = kzalloc(sizeof(*target), GFP_KERNEL);
	if (!target) {
		ath6kl_err("unable to allocate memory\n");
		return NULL;
	}

	target->dev = kzalloc(sizeof(*target->dev), GFP_KERNEL);
	if (!target->dev) {
		ath6kl_err("unable to allocate memory\n");
		status = -ENOMEM;
K
Kalle Valo 已提交
2871
		goto err_htc_cleanup;
K
Kalle Valo 已提交
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
	}

	spin_lock_init(&target->htc_lock);
	spin_lock_init(&target->rx_lock);
	spin_lock_init(&target->tx_lock);

	INIT_LIST_HEAD(&target->free_ctrl_txbuf);
	INIT_LIST_HEAD(&target->free_ctrl_rxbuf);
	INIT_LIST_HEAD(&target->cred_dist_list);

	target->dev->ar = ar;
	target->dev->htc_cnxt = target;
	target->ep_waiting = ENDPOINT_MAX;

K
Kalle Valo 已提交
2886
	status = ath6kl_hif_setup(target->dev);
K
Kalle Valo 已提交
2887
	if (status)
K
Kalle Valo 已提交
2888
		goto err_htc_cleanup;
K
Kalle Valo 已提交
2889

K
Kalle Valo 已提交
2890 2891 2892
	status = ath6kl_htc_reset(target);
	if (status)
		goto err_htc_cleanup;
K
Kalle Valo 已提交
2893

K
Kalle Valo 已提交
2894
	return target;
K
Kalle Valo 已提交
2895

K
Kalle Valo 已提交
2896
err_htc_cleanup:
K
Kalle Valo 已提交
2897
	ath6kl_htc_mbox_cleanup(target);
K
Kalle Valo 已提交
2898

K
Kalle Valo 已提交
2899
	return NULL;
K
Kalle Valo 已提交
2900 2901 2902
}

/* cleanup the HTC instance */
K
Kalle Valo 已提交
2903
static void ath6kl_htc_mbox_cleanup(struct htc_target *target)
K
Kalle Valo 已提交
2904 2905 2906
{
	struct htc_packet *packet, *tmp_packet;

2907
	ath6kl_hif_cleanup_scatter(target->dev->ar);
K
Kalle Valo 已提交
2908 2909

	list_for_each_entry_safe(packet, tmp_packet,
2910
				 &target->free_ctrl_txbuf, list) {
K
Kalle Valo 已提交
2911 2912 2913 2914 2915 2916
		list_del(&packet->list);
		kfree(packet->buf_start);
		kfree(packet);
	}

	list_for_each_entry_safe(packet, tmp_packet,
2917
				 &target->free_ctrl_rxbuf, list) {
K
Kalle Valo 已提交
2918 2919 2920 2921 2922 2923 2924 2925
		list_del(&packet->list);
		kfree(packet->buf_start);
		kfree(packet);
	}

	kfree(target->dev);
	kfree(target);
}
K
Kalle Valo 已提交
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946

static const struct ath6kl_htc_ops ath6kl_htc_mbox_ops = {
	.create = ath6kl_htc_mbox_create,
	.wait_target = ath6kl_htc_mbox_wait_target,
	.start = ath6kl_htc_mbox_start,
	.conn_service = ath6kl_htc_mbox_conn_service,
	.tx = ath6kl_htc_mbox_tx,
	.stop = ath6kl_htc_mbox_stop,
	.cleanup = ath6kl_htc_mbox_cleanup,
	.flush_txep = ath6kl_htc_mbox_flush_txep,
	.flush_rx_buf = ath6kl_htc_mbox_flush_rx_buf,
	.activity_changed = ath6kl_htc_mbox_activity_changed,
	.get_rxbuf_num = ath6kl_htc_mbox_get_rxbuf_num,
	.add_rxbuf_multiple = ath6kl_htc_mbox_add_rxbuf_multiple,
	.credit_setup = ath6kl_htc_mbox_credit_setup,
};

void ath6kl_htc_mbox_attach(struct ath6kl *ar)
{
	ar->htc_ops = &ath6kl_htc_mbox_ops;
}