htt_tx.c 23.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (c) 2005-2011 Atheros Communications Inc.
 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
 *
 * 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 <linux/etherdevice.h>
#include "htt.h"
#include "mac.h"
#include "hif.h"
#include "txrx.h"
#include "debug.h"

25
void __ath10k_htt_tx_dec_pending(struct ath10k_htt *htt, bool limit_mgmt_desc)
26
{
27 28 29
	if (limit_mgmt_desc)
		htt->num_pending_mgmt_tx--;

30 31
	htt->num_pending_tx--;
	if (htt->num_pending_tx == htt->max_num_pending_tx - 1)
M
Michal Kazior 已提交
32
		ath10k_mac_tx_unlock(htt->ar, ATH10K_TX_PAUSE_Q_FULL);
33 34
}

35 36
static void ath10k_htt_tx_dec_pending(struct ath10k_htt *htt,
				      bool limit_mgmt_desc)
37 38
{
	spin_lock_bh(&htt->tx_lock);
39
	__ath10k_htt_tx_dec_pending(htt, limit_mgmt_desc);
40 41 42
	spin_unlock_bh(&htt->tx_lock);
}

43 44
static int ath10k_htt_tx_inc_pending(struct ath10k_htt *htt,
				     bool limit_mgmt_desc, bool is_probe_resp)
45
{
46
	struct ath10k *ar = htt->ar;
47 48 49 50 51 52 53 54 55
	int ret = 0;

	spin_lock_bh(&htt->tx_lock);

	if (htt->num_pending_tx >= htt->max_num_pending_tx) {
		ret = -EBUSY;
		goto exit;
	}

56 57 58 59 60 61 62 63 64
	if (limit_mgmt_desc) {
		if (is_probe_resp && (htt->num_pending_mgmt_tx >
		    ar->hw_params.max_probe_resp_desc_thres)) {
			ret = -EBUSY;
			goto exit;
		}
		htt->num_pending_mgmt_tx++;
	}

65 66
	htt->num_pending_tx++;
	if (htt->num_pending_tx == htt->max_num_pending_tx)
M
Michal Kazior 已提交
67
		ath10k_mac_tx_lock(htt->ar, ATH10K_TX_PAUSE_Q_FULL);
68 69 70 71 72 73

exit:
	spin_unlock_bh(&htt->tx_lock);
	return ret;
}

M
Michal Kazior 已提交
74
int ath10k_htt_tx_alloc_msdu_id(struct ath10k_htt *htt, struct sk_buff *skb)
75
{
76
	struct ath10k *ar = htt->ar;
M
Michal Kazior 已提交
77
	int ret;
78 79 80

	lockdep_assert_held(&htt->tx_lock);

81 82
	ret = idr_alloc(&htt->pending_tx, skb, 0,
			htt->max_num_pending_tx, GFP_ATOMIC);
M
Michal Kazior 已提交
83 84

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx alloc msdu_id %d\n", ret);
85

M
Michal Kazior 已提交
86
	return ret;
87 88 89 90
}

void ath10k_htt_tx_free_msdu_id(struct ath10k_htt *htt, u16 msdu_id)
{
91 92
	struct ath10k *ar = htt->ar;

93 94
	lockdep_assert_held(&htt->tx_lock);

95
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx free msdu_id %hu\n", msdu_id);
M
Michal Kazior 已提交
96 97

	idr_remove(&htt->pending_tx, msdu_id);
98 99
}

100 101 102 103 104 105 106 107 108 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 134
static void ath10k_htt_tx_free_cont_frag_desc(struct ath10k_htt *htt)
{
	size_t size;

	if (!htt->frag_desc.vaddr)
		return;

	size = htt->max_num_pending_tx * sizeof(struct htt_msdu_ext_desc);

	dma_free_coherent(htt->ar->dev,
			  size,
			  htt->frag_desc.vaddr,
			  htt->frag_desc.paddr);
}

static int ath10k_htt_tx_alloc_cont_frag_desc(struct ath10k_htt *htt)
{
	struct ath10k *ar = htt->ar;
	size_t size;

	if (!ar->hw_params.continuous_frag_desc)
		return 0;

	size = htt->max_num_pending_tx * sizeof(struct htt_msdu_ext_desc);
	htt->frag_desc.vaddr = dma_alloc_coherent(ar->dev, size,
						  &htt->frag_desc.paddr,
						  GFP_KERNEL);
	if (!htt->frag_desc.vaddr) {
		ath10k_err(ar, "failed to alloc fragment desc memory\n");
		return -ENOMEM;
	}

	return 0;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static void ath10k_htt_tx_free_txq(struct ath10k_htt *htt)
{
	struct ath10k *ar = htt->ar;
	size_t size;

	if (!test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL, ar->fw_features))
		return;

	size = sizeof(*htt->tx_q_state.vaddr);

	dma_unmap_single(ar->dev, htt->tx_q_state.paddr, size, DMA_TO_DEVICE);
	kfree(htt->tx_q_state.vaddr);
}

static int ath10k_htt_tx_alloc_txq(struct ath10k_htt *htt)
{
	struct ath10k *ar = htt->ar;
	size_t size;
	int ret;

	if (!test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL, ar->fw_features))
		return 0;

	htt->tx_q_state.num_peers = HTT_TX_Q_STATE_NUM_PEERS;
	htt->tx_q_state.num_tids = HTT_TX_Q_STATE_NUM_TIDS;
	htt->tx_q_state.type = HTT_Q_DEPTH_TYPE_BYTES;

	size = sizeof(*htt->tx_q_state.vaddr);
	htt->tx_q_state.vaddr = kzalloc(size, GFP_KERNEL);
	if (!htt->tx_q_state.vaddr)
		return -ENOMEM;

	htt->tx_q_state.paddr = dma_map_single(ar->dev, htt->tx_q_state.vaddr,
					       size, DMA_TO_DEVICE);
	ret = dma_mapping_error(ar->dev, htt->tx_q_state.paddr);
	if (ret) {
		ath10k_warn(ar, "failed to dma map tx_q_state: %d\n", ret);
		kfree(htt->tx_q_state.vaddr);
		return -EIO;
	}

	return 0;
}

M
Michal Kazior 已提交
179
int ath10k_htt_tx_alloc(struct ath10k_htt *htt)
180
{
181
	struct ath10k *ar = htt->ar;
182
	int ret, size;
183 184

	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt tx max num pending tx %d\n",
185 186
		   htt->max_num_pending_tx);

M
Michal Kazior 已提交
187 188
	spin_lock_init(&htt->tx_lock);
	idr_init(&htt->pending_tx);
189

190 191 192
	size = htt->max_num_pending_tx * sizeof(struct ath10k_htt_txbuf);
	htt->txbuf.vaddr = dma_alloc_coherent(ar->dev, size,
						  &htt->txbuf.paddr,
F
Felix Fietkau 已提交
193
						  GFP_KERNEL);
194 195
	if (!htt->txbuf.vaddr) {
		ath10k_err(ar, "failed to alloc tx buffer\n");
196 197 198 199
		ret = -ENOMEM;
		goto free_idr_pending_tx;
	}

200 201 202
	ret = ath10k_htt_tx_alloc_cont_frag_desc(htt);
	if (ret) {
		ath10k_err(ar, "failed to alloc cont frag desc: %d\n", ret);
203
		goto free_txbuf;
204 205
	}

206 207 208 209 210 211
	ret = ath10k_htt_tx_alloc_txq(htt);
	if (ret) {
		ath10k_err(ar, "failed to alloc txq: %d\n", ret);
		goto free_frag_desc;
	}

212
	return 0;
213

214 215 216
free_frag_desc:
	ath10k_htt_tx_free_cont_frag_desc(htt);

217 218 219 220 221
free_txbuf:
	size = htt->max_num_pending_tx *
			  sizeof(struct ath10k_htt_txbuf);
	dma_free_coherent(htt->ar->dev, size, htt->txbuf.vaddr,
			  htt->txbuf.paddr);
222

223 224
free_idr_pending_tx:
	idr_destroy(&htt->pending_tx);
225

226
	return ret;
227 228
}

M
Michal Kazior 已提交
229
static int ath10k_htt_tx_clean_up_pending(int msdu_id, void *skb, void *ctx)
230
{
M
Michal Kazior 已提交
231 232
	struct ath10k *ar = ctx;
	struct ath10k_htt *htt = &ar->htt;
233
	struct htt_tx_done tx_done = {0};
234

M
Michal Kazior 已提交
235
	ath10k_dbg(ar, ATH10K_DBG_HTT, "force cleanup msdu_id %hu\n", msdu_id);
236

M
Michal Kazior 已提交
237 238
	tx_done.discard = 1;
	tx_done.msdu_id = msdu_id;
239

M
Michal Kazior 已提交
240 241 242
	ath10k_txrx_tx_unref(htt, &tx_done);

	return 0;
243 244
}

M
Michal Kazior 已提交
245
void ath10k_htt_tx_free(struct ath10k_htt *htt)
246
{
247 248
	int size;

M
Michal Kazior 已提交
249 250
	idr_for_each(&htt->pending_tx, ath10k_htt_tx_clean_up_pending, htt->ar);
	idr_destroy(&htt->pending_tx);
251 252 253 254 255 256 257

	if (htt->txbuf.vaddr) {
		size = htt->max_num_pending_tx *
				  sizeof(struct ath10k_htt_txbuf);
		dma_free_coherent(htt->ar->dev, size, htt->txbuf.vaddr,
				  htt->txbuf.paddr);
	}
258

259
	ath10k_htt_tx_free_txq(htt);
260
	ath10k_htt_tx_free_cont_frag_desc(htt);
261 262 263 264
}

void ath10k_htt_htc_tx_complete(struct ath10k *ar, struct sk_buff *skb)
{
265
	dev_kfree_skb_any(skb);
266 267
}

268 269 270 271 272 273
void ath10k_htt_hif_tx_complete(struct ath10k *ar, struct sk_buff *skb)
{
	dev_kfree_skb_any(skb);
}
EXPORT_SYMBOL(ath10k_htt_hif_tx_complete);

274 275
int ath10k_htt_h2t_ver_req_msg(struct ath10k_htt *htt)
{
276
	struct ath10k *ar = htt->ar;
277 278 279 280 281 282 283 284
	struct sk_buff *skb;
	struct htt_cmd *cmd;
	int len = 0;
	int ret;

	len += sizeof(cmd->hdr);
	len += sizeof(cmd->ver_req);

285
	skb = ath10k_htc_alloc_skb(ar, len);
286 287 288 289 290 291 292
	if (!skb)
		return -ENOMEM;

	skb_put(skb, len);
	cmd = (struct htt_cmd *)skb->data;
	cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_VERSION_REQ;

293
	ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
294 295 296 297 298 299 300 301
	if (ret) {
		dev_kfree_skb_any(skb);
		return ret;
	}

	return 0;
}

302 303
int ath10k_htt_h2t_stats_req(struct ath10k_htt *htt, u8 mask, u64 cookie)
{
304
	struct ath10k *ar = htt->ar;
305 306 307 308 309 310 311 312
	struct htt_stats_req *req;
	struct sk_buff *skb;
	struct htt_cmd *cmd;
	int len = 0, ret;

	len += sizeof(cmd->hdr);
	len += sizeof(cmd->stats_req);

313
	skb = ath10k_htc_alloc_skb(ar, len);
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	if (!skb)
		return -ENOMEM;

	skb_put(skb, len);
	cmd = (struct htt_cmd *)skb->data;
	cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_STATS_REQ;

	req = &cmd->stats_req;

	memset(req, 0, sizeof(*req));

	/* currently we support only max 8 bit masks so no need to worry
	 * about endian support */
	req->upload_types[0] = mask;
	req->reset_types[0] = mask;
	req->stat_type = HTT_STATS_REQ_CFG_STAT_TYPE_INVALID;
	req->cookie_lsb = cpu_to_le32(cookie & 0xffffffff);
	req->cookie_msb = cpu_to_le32((cookie & 0xffffffff00000000ULL) >> 32);

	ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
	if (ret) {
335 336
		ath10k_warn(ar, "failed to send htt type stats request: %d",
			    ret);
337 338 339 340 341 342 343
		dev_kfree_skb_any(skb);
		return ret;
	}

	return 0;
}

344 345 346 347 348
int ath10k_htt_send_frag_desc_bank_cfg(struct ath10k_htt *htt)
{
	struct ath10k *ar = htt->ar;
	struct sk_buff *skb;
	struct htt_cmd *cmd;
349
	struct htt_frag_desc_bank_cfg *cfg;
350
	int ret, size;
351
	u8 info;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

	if (!ar->hw_params.continuous_frag_desc)
		return 0;

	if (!htt->frag_desc.paddr) {
		ath10k_warn(ar, "invalid frag desc memory\n");
		return -EINVAL;
	}

	size = sizeof(cmd->hdr) + sizeof(cmd->frag_desc_bank_cfg);
	skb = ath10k_htc_alloc_skb(ar, size);
	if (!skb)
		return -ENOMEM;

	skb_put(skb, size);
	cmd = (struct htt_cmd *)skb->data;
	cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_FRAG_DESC_BANK_CFG;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

	info = 0;
	info |= SM(htt->tx_q_state.type,
		   HTT_FRAG_DESC_BANK_CFG_INFO_Q_STATE_DEPTH_TYPE);

	if (test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL, ar->fw_features))
		info |= HTT_FRAG_DESC_BANK_CFG_INFO_Q_STATE_VALID;

	cfg = &cmd->frag_desc_bank_cfg;
	cfg->info = info;
	cfg->num_banks = 1;
	cfg->desc_size = sizeof(struct htt_msdu_ext_desc);
	cfg->bank_base_addrs[0] = __cpu_to_le32(htt->frag_desc.paddr);
	cfg->bank_id[0].bank_min_id = 0;
	cfg->bank_id[0].bank_max_id = __cpu_to_le16(htt->max_num_pending_tx -
						    1);

	cfg->q_state.paddr = cpu_to_le32(htt->tx_q_state.paddr);
	cfg->q_state.num_peers = cpu_to_le16(htt->tx_q_state.num_peers);
	cfg->q_state.num_tids = cpu_to_le16(htt->tx_q_state.num_tids);
	cfg->q_state.record_size = HTT_TX_Q_STATE_ENTRY_SIZE;
	cfg->q_state.record_multiplier = HTT_TX_Q_STATE_ENTRY_MULTIPLIER;

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt frag desc bank cmd\n");
393 394 395 396 397 398 399 400 401 402 403 404

	ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
	if (ret) {
		ath10k_warn(ar, "failed to send frag desc bank cfg request: %d\n",
			    ret);
		dev_kfree_skb_any(skb);
		return ret;
	}

	return 0;
}

405 406
int ath10k_htt_send_rx_ring_cfg_ll(struct ath10k_htt *htt)
{
407
	struct ath10k *ar = htt->ar;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	struct sk_buff *skb;
	struct htt_cmd *cmd;
	struct htt_rx_ring_setup_ring *ring;
	const int num_rx_ring = 1;
	u16 flags;
	u32 fw_idx;
	int len;
	int ret;

	/*
	 * the HW expects the buffer to be an integral number of 4-byte
	 * "words"
	 */
	BUILD_BUG_ON(!IS_ALIGNED(HTT_RX_BUF_SIZE, 4));
	BUILD_BUG_ON((HTT_RX_BUF_SIZE & HTT_MAX_CACHE_LINE_SIZE_MASK) != 0);

	len = sizeof(cmd->hdr) + sizeof(cmd->rx_setup.hdr)
	    + (sizeof(*ring) * num_rx_ring);
426
	skb = ath10k_htc_alloc_skb(ar, len);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	if (!skb)
		return -ENOMEM;

	skb_put(skb, len);

	cmd = (struct htt_cmd *)skb->data;
	ring = &cmd->rx_setup.rings[0];

	cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_RX_RING_CFG;
	cmd->rx_setup.hdr.num_rings = 1;

	/* FIXME: do we need all of this? */
	flags = 0;
	flags |= HTT_RX_RING_FLAGS_MAC80211_HDR;
	flags |= HTT_RX_RING_FLAGS_MSDU_PAYLOAD;
	flags |= HTT_RX_RING_FLAGS_PPDU_START;
	flags |= HTT_RX_RING_FLAGS_PPDU_END;
	flags |= HTT_RX_RING_FLAGS_MPDU_START;
	flags |= HTT_RX_RING_FLAGS_MPDU_END;
	flags |= HTT_RX_RING_FLAGS_MSDU_START;
	flags |= HTT_RX_RING_FLAGS_MSDU_END;
	flags |= HTT_RX_RING_FLAGS_RX_ATTENTION;
	flags |= HTT_RX_RING_FLAGS_FRAG_INFO;
	flags |= HTT_RX_RING_FLAGS_UNICAST_RX;
	flags |= HTT_RX_RING_FLAGS_MULTICAST_RX;
	flags |= HTT_RX_RING_FLAGS_CTRL_RX;
	flags |= HTT_RX_RING_FLAGS_MGMT_RX;
	flags |= HTT_RX_RING_FLAGS_NULL_RX;
	flags |= HTT_RX_RING_FLAGS_PHY_DATA_RX;

	fw_idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);

	ring->fw_idx_shadow_reg_paddr =
		__cpu_to_le32(htt->rx_ring.alloc_idx.paddr);
	ring->rx_ring_base_paddr = __cpu_to_le32(htt->rx_ring.base_paddr);
	ring->rx_ring_len = __cpu_to_le16(htt->rx_ring.size);
	ring->rx_ring_bufsize = __cpu_to_le16(HTT_RX_BUF_SIZE);
	ring->flags = __cpu_to_le16(flags);
	ring->fw_idx_init_val = __cpu_to_le16(fw_idx);

#define desc_offset(x) (offsetof(struct htt_rx_desc, x) / 4)

	ring->mac80211_hdr_offset = __cpu_to_le16(desc_offset(rx_hdr_status));
	ring->msdu_payload_offset = __cpu_to_le16(desc_offset(msdu_payload));
	ring->ppdu_start_offset = __cpu_to_le16(desc_offset(ppdu_start));
	ring->ppdu_end_offset = __cpu_to_le16(desc_offset(ppdu_end));
	ring->mpdu_start_offset = __cpu_to_le16(desc_offset(mpdu_start));
	ring->mpdu_end_offset = __cpu_to_le16(desc_offset(mpdu_end));
	ring->msdu_start_offset = __cpu_to_le16(desc_offset(msdu_start));
	ring->msdu_end_offset = __cpu_to_le16(desc_offset(msdu_end));
	ring->rx_attention_offset = __cpu_to_le16(desc_offset(attention));
	ring->frag_info_offset = __cpu_to_le16(desc_offset(frag_info));

#undef desc_offset

482
	ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
483 484 485 486 487 488 489 490
	if (ret) {
		dev_kfree_skb_any(skb);
		return ret;
	}

	return 0;
}

491 492 493 494
int ath10k_htt_h2t_aggr_cfg_msg(struct ath10k_htt *htt,
				u8 max_subfrms_ampdu,
				u8 max_subfrms_amsdu)
{
495
	struct ath10k *ar = htt->ar;
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	struct htt_aggr_conf *aggr_conf;
	struct sk_buff *skb;
	struct htt_cmd *cmd;
	int len;
	int ret;

	/* Firmware defaults are: amsdu = 3 and ampdu = 64 */

	if (max_subfrms_ampdu == 0 || max_subfrms_ampdu > 64)
		return -EINVAL;

	if (max_subfrms_amsdu == 0 || max_subfrms_amsdu > 31)
		return -EINVAL;

	len = sizeof(cmd->hdr);
	len += sizeof(cmd->aggr_conf);

513
	skb = ath10k_htc_alloc_skb(ar, len);
514 515 516 517 518 519 520 521 522 523 524
	if (!skb)
		return -ENOMEM;

	skb_put(skb, len);
	cmd = (struct htt_cmd *)skb->data;
	cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_AGGR_CFG;

	aggr_conf = &cmd->aggr_conf;
	aggr_conf->max_num_ampdu_subframes = max_subfrms_ampdu;
	aggr_conf->max_num_amsdu_subframes = max_subfrms_amsdu;

525
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt h2t aggr cfg msg amsdu %d ampdu %d",
526 527 528 529 530 531 532 533 534 535 536 537
		   aggr_conf->max_num_amsdu_subframes,
		   aggr_conf->max_num_ampdu_subframes);

	ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
	if (ret) {
		dev_kfree_skb_any(skb);
		return ret;
	}

	return 0;
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
static u8 ath10k_htt_tx_get_vdev_id(struct ath10k *ar, struct sk_buff *skb)
{
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
	struct ath10k_vif *arvif = (void *)cb->vif->drv_priv;

	if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
		return ar->scan.vdev_id;
	else if (cb->vif)
		return arvif->vdev_id;
	else if (ar->monitor_started)
		return ar->monitor_vdev_id;
	else
		return 0;
}

static u8 ath10k_htt_tx_get_tid(struct sk_buff *skb, bool is_eth)
{
	struct ieee80211_hdr *hdr = (void *)skb->data;
	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);

	if (!is_eth && ieee80211_is_mgmt(hdr->frame_control))
		return HTT_DATA_TX_EXT_TID_MGMT;
	else if (cb->flags & ATH10K_SKB_F_QOS)
		return skb->priority % IEEE80211_QOS_CTL_TID_MASK;
	else
		return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
}

567 568
int ath10k_htt_mgmt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
{
569 570
	struct ath10k *ar = htt->ar;
	struct device *dev = ar->dev;
571 572
	struct sk_buff *txdesc = NULL;
	struct htt_cmd *cmd;
573
	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(msdu);
574
	u8 vdev_id = ath10k_htt_tx_get_vdev_id(ar, msdu);
575 576 577
	int len = 0;
	int msdu_id = -1;
	int res;
578 579 580 581 582 583 584 585 586 587 588 589
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msdu->data;
	bool limit_mgmt_desc = false;
	bool is_probe_resp = false;

	if (ar->hw_params.max_probe_resp_desc_thres) {
		limit_mgmt_desc = true;

		if (ieee80211_is_probe_resp(hdr->frame_control))
			is_probe_resp = true;
	}

	res = ath10k_htt_tx_inc_pending(htt, limit_mgmt_desc, is_probe_resp);
590 591

	if (res)
M
Michal Kazior 已提交
592
		goto err;
593 594 595 596 597

	len += sizeof(cmd->hdr);
	len += sizeof(cmd->mgmt_tx);

	spin_lock_bh(&htt->tx_lock);
M
Michal Kazior 已提交
598
	res = ath10k_htt_tx_alloc_msdu_id(htt, msdu);
599
	spin_unlock_bh(&htt->tx_lock);
K
Kalle Valo 已提交
600
	if (res < 0)
M
Michal Kazior 已提交
601
		goto err_tx_dec;
K
Kalle Valo 已提交
602

M
Michal Kazior 已提交
603
	msdu_id = res;
604

605 606 607 608 609 610 611
	if ((ieee80211_is_action(hdr->frame_control) ||
	     ieee80211_is_deauth(hdr->frame_control) ||
	     ieee80211_is_disassoc(hdr->frame_control)) &&
	     ieee80211_has_protected(hdr->frame_control)) {
		skb_put(msdu, IEEE80211_CCMP_MIC_LEN);
	}

612
	txdesc = ath10k_htc_alloc_skb(ar, len);
M
Michal Kazior 已提交
613 614 615 616 617
	if (!txdesc) {
		res = -ENOMEM;
		goto err_free_msdu_id;
	}

618 619 620
	skb_cb->paddr = dma_map_single(dev, msdu->data, msdu->len,
				       DMA_TO_DEVICE);
	res = dma_mapping_error(dev, skb_cb->paddr);
621 622
	if (res) {
		res = -EIO;
M
Michal Kazior 已提交
623
		goto err_free_txdesc;
624
	}
625 626 627

	skb_put(txdesc, len);
	cmd = (struct htt_cmd *)txdesc->data;
628 629
	memset(cmd, 0, len);

630 631 632 633 634 635 636 637
	cmd->hdr.msg_type         = HTT_H2T_MSG_TYPE_MGMT_TX;
	cmd->mgmt_tx.msdu_paddr = __cpu_to_le32(ATH10K_SKB_CB(msdu)->paddr);
	cmd->mgmt_tx.len        = __cpu_to_le32(msdu->len);
	cmd->mgmt_tx.desc_id    = __cpu_to_le32(msdu_id);
	cmd->mgmt_tx.vdev_id    = __cpu_to_le32(vdev_id);
	memcpy(cmd->mgmt_tx.hdr, msdu->data,
	       min_t(int, msdu->len, HTT_MGMT_FRM_HDR_DOWNLOAD_LEN));

638
	res = ath10k_htc_send(&htt->ar->htc, htt->eid, txdesc);
639
	if (res)
M
Michal Kazior 已提交
640
		goto err_unmap_msdu;
641 642 643

	return 0;

M
Michal Kazior 已提交
644
err_unmap_msdu:
645
	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
M
Michal Kazior 已提交
646 647 648 649 650 651 652
err_free_txdesc:
	dev_kfree_skb_any(txdesc);
err_free_msdu_id:
	spin_lock_bh(&htt->tx_lock);
	ath10k_htt_tx_free_msdu_id(htt, msdu_id);
	spin_unlock_bh(&htt->tx_lock);
err_tx_dec:
653
	ath10k_htt_tx_dec_pending(htt, limit_mgmt_desc);
M
Michal Kazior 已提交
654
err:
655 656 657
	return res;
}

M
Michal Kazior 已提交
658 659
int ath10k_htt_tx(struct ath10k_htt *htt, enum ath10k_hw_txrx_mode txmode,
		  struct sk_buff *msdu)
660
{
661 662
	struct ath10k *ar = htt->ar;
	struct device *dev = ar->dev;
663
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msdu->data;
M
Michal Kazior 已提交
664
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(msdu);
665
	struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(msdu);
666
	struct ath10k_hif_sg_item sg_items[2];
667
	struct ath10k_htt_txbuf *txbuf;
668
	struct htt_data_tx_desc_frag *frags;
669 670 671
	bool is_eth = (txmode == ATH10K_HW_TXRX_ETHERNET);
	u8 vdev_id = ath10k_htt_tx_get_vdev_id(ar, msdu);
	u8 tid = ath10k_htt_tx_get_tid(msdu, is_eth);
672
	int prefetch_len;
673
	int res;
674 675
	u8 flags0 = 0;
	u16 msdu_id, flags1 = 0;
M
Michal Kazior 已提交
676
	u16 freq = 0;
677
	u32 frags_paddr = 0;
678
	u32 txbuf_paddr;
679
	struct htt_msdu_ext_desc *ext_desc = NULL;
680 681 682 683 684 685 686 687 688 689
	bool limit_mgmt_desc = false;
	bool is_probe_resp = false;

	if (unlikely(ieee80211_is_mgmt(hdr->frame_control)) &&
	    ar->hw_params.max_probe_resp_desc_thres) {
		limit_mgmt_desc = true;

		if (ieee80211_is_probe_resp(hdr->frame_control))
			is_probe_resp = true;
	}
690

691
	res = ath10k_htt_tx_inc_pending(htt, limit_mgmt_desc, is_probe_resp);
692
	if (res)
M
Michal Kazior 已提交
693 694 695
		goto err;

	spin_lock_bh(&htt->tx_lock);
M
Michal Kazior 已提交
696
	res = ath10k_htt_tx_alloc_msdu_id(htt, msdu);
697
	spin_unlock_bh(&htt->tx_lock);
K
Kalle Valo 已提交
698
	if (res < 0)
M
Michal Kazior 已提交
699
		goto err_tx_dec;
K
Kalle Valo 已提交
700

M
Michal Kazior 已提交
701
	msdu_id = res;
702 703 704 705

	prefetch_len = min(htt->prefetch_len, msdu->len);
	prefetch_len = roundup(prefetch_len, 4);

706 707 708
	txbuf = &htt->txbuf.vaddr[msdu_id];
	txbuf_paddr = htt->txbuf.paddr +
		      (sizeof(struct ath10k_htt_txbuf) * msdu_id);
709

710 711 712
	if ((ieee80211_is_action(hdr->frame_control) ||
	     ieee80211_is_deauth(hdr->frame_control) ||
	     ieee80211_is_disassoc(hdr->frame_control)) &&
713
	     ieee80211_has_protected(hdr->frame_control)) {
714
		skb_put(msdu, IEEE80211_CCMP_MIC_LEN);
M
Michal Kazior 已提交
715
	} else if (!(skb_cb->flags & ATH10K_SKB_F_NO_HWCRYPT) &&
M
Michal Kazior 已提交
716
		   txmode == ATH10K_HW_TXRX_RAW &&
717
		   ieee80211_has_protected(hdr->frame_control)) {
718 719
		skb_put(msdu, IEEE80211_CCMP_MIC_LEN);
	}
720

721 722 723
	skb_cb->paddr = dma_map_single(dev, msdu->data, msdu->len,
				       DMA_TO_DEVICE);
	res = dma_mapping_error(dev, skb_cb->paddr);
724 725
	if (res) {
		res = -EIO;
726
		goto err_free_msdu_id;
727
	}
728

M
Michal Kazior 已提交
729 730 731
	if (unlikely(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN))
		freq = ar->scan.roc_freq;

M
Michal Kazior 已提交
732
	switch (txmode) {
733 734 735 736 737
	case ATH10K_HW_TXRX_RAW:
	case ATH10K_HW_TXRX_NATIVE_WIFI:
		flags0 |= HTT_DATA_TX_DESC_FLAGS0_MAC_HDR_PRESENT;
		/* pass through */
	case ATH10K_HW_TXRX_ETHERNET:
738
		if (ar->hw_params.continuous_frag_desc) {
739 740
			memset(&htt->frag_desc.vaddr[msdu_id], 0,
			       sizeof(struct htt_msdu_ext_desc));
741 742
			frags = (struct htt_data_tx_desc_frag *)
				&htt->frag_desc.vaddr[msdu_id].frags;
743
			ext_desc = &htt->frag_desc.vaddr[msdu_id];
744 745 746 747 748 749 750 751
			frags[0].tword_addr.paddr_lo =
				__cpu_to_le32(skb_cb->paddr);
			frags[0].tword_addr.paddr_hi = 0;
			frags[0].tword_addr.len_16 = __cpu_to_le16(msdu->len);

			frags_paddr =  htt->frag_desc.paddr +
				(sizeof(struct htt_msdu_ext_desc) * msdu_id);
		} else {
752
			frags = txbuf->frags;
753 754 755 756 757 758
			frags[0].dword_addr.paddr =
				__cpu_to_le32(skb_cb->paddr);
			frags[0].dword_addr.len = __cpu_to_le32(msdu->len);
			frags[1].dword_addr.paddr = 0;
			frags[1].dword_addr.len = 0;

759
			frags_paddr = txbuf_paddr;
760
		}
M
Michal Kazior 已提交
761
		flags0 |= SM(txmode, HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
762 763
		break;
	case ATH10K_HW_TXRX_MGMT:
764 765
		flags0 |= SM(ATH10K_HW_TXRX_MGMT,
			     HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
766
		flags0 |= HTT_DATA_TX_DESC_FLAGS0_MAC_HDR_PRESENT;
767

768
		frags_paddr = skb_cb->paddr;
769
		break;
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	}

	/* Normally all commands go through HTC which manages tx credits for
	 * each endpoint and notifies when tx is completed.
	 *
	 * HTT endpoint is creditless so there's no need to care about HTC
	 * flags. In that case it is trivial to fill the HTC header here.
	 *
	 * MSDU transmission is considered completed upon HTT event. This
	 * implies no relevant resources can be freed until after the event is
	 * received. That's why HTC tx completion handler itself is ignored by
	 * setting NULL to transfer_context for all sg items.
	 *
	 * There is simply no point in pushing HTT TX_FRM through HTC tx path
	 * as it's a waste of resources. By bypassing HTC it is possible to
	 * avoid extra memory allocations, compress data structures and thus
	 * improve performance. */

788 789 790 791 792
	txbuf->htc_hdr.eid = htt->eid;
	txbuf->htc_hdr.len = __cpu_to_le16(sizeof(txbuf->cmd_hdr) +
					   sizeof(txbuf->cmd_tx) +
					   prefetch_len);
	txbuf->htc_hdr.flags = 0;
793

M
Michal Kazior 已提交
794
	if (skb_cb->flags & ATH10K_SKB_F_NO_HWCRYPT)
795 796
		flags0 |= HTT_DATA_TX_DESC_FLAGS0_NO_ENCRYPT;

797 798
	flags1 |= SM((u16)vdev_id, HTT_DATA_TX_DESC_FLAGS1_VDEV_ID);
	flags1 |= SM((u16)tid, HTT_DATA_TX_DESC_FLAGS1_EXT_TID);
799 800
	if (msdu->ip_summed == CHECKSUM_PARTIAL &&
	    !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
801 802
		flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L3_OFFLOAD;
		flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L4_OFFLOAD;
803 804
		if (ar->hw_params.continuous_frag_desc)
			ext_desc->flags |= HTT_MSDU_CHECKSUM_ENABLE;
805
	}
806

807 808 809 810 811 812
	/* Prevent firmware from sending up tx inspection requests. There's
	 * nothing ath10k can do with frames requested for inspection so force
	 * it to simply rely a regular tx completion with discard status.
	 */
	flags1 |= HTT_DATA_TX_DESC_FLAGS1_POSTPONED;

813 814 815 816 817 818
	txbuf->cmd_hdr.msg_type = HTT_H2T_MSG_TYPE_TX_FRM;
	txbuf->cmd_tx.flags0 = flags0;
	txbuf->cmd_tx.flags1 = __cpu_to_le16(flags1);
	txbuf->cmd_tx.len = __cpu_to_le16(msdu->len);
	txbuf->cmd_tx.id = __cpu_to_le16(msdu_id);
	txbuf->cmd_tx.frags_paddr = __cpu_to_le32(frags_paddr);
819
	if (ath10k_mac_tx_frm_has_freq(ar)) {
820
		txbuf->cmd_tx.offchan_tx.peerid =
821
				__cpu_to_le16(HTT_INVALID_PEERID);
822
		txbuf->cmd_tx.offchan_tx.freq =
M
Michal Kazior 已提交
823
				__cpu_to_le16(freq);
824
	} else {
825
		txbuf->cmd_tx.peerid =
826 827
				__cpu_to_le32(HTT_INVALID_PEERID);
	}
828

829
	trace_ath10k_htt_tx(ar, msdu_id, msdu->len, vdev_id, tid);
830
	ath10k_dbg(ar, ATH10K_DBG_HTT,
M
Michal Kazior 已提交
831
		   "htt tx flags0 %hhu flags1 %hu len %d id %hu frags_paddr %08x, msdu_paddr %08x vdev %hhu tid %hhu freq %hu\n",
832
		   flags0, flags1, msdu->len, msdu_id, frags_paddr,
M
Michal Kazior 已提交
833
		   (u32)skb_cb->paddr, vdev_id, tid, freq);
834
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt tx msdu: ",
835
			msdu->data, msdu->len);
836 837
	trace_ath10k_tx_hdr(ar, msdu->data, msdu->len);
	trace_ath10k_tx_payload(ar, msdu->data, msdu->len);
838

839 840
	sg_items[0].transfer_id = 0;
	sg_items[0].transfer_context = NULL;
841 842 843 844 845 846
	sg_items[0].vaddr = &txbuf->htc_hdr;
	sg_items[0].paddr = txbuf_paddr +
			    sizeof(txbuf->frags);
	sg_items[0].len = sizeof(txbuf->htc_hdr) +
			  sizeof(txbuf->cmd_hdr) +
			  sizeof(txbuf->cmd_tx);
847 848 849 850 851 852 853 854 855 856

	sg_items[1].transfer_id = 0;
	sg_items[1].transfer_context = NULL;
	sg_items[1].vaddr = msdu->data;
	sg_items[1].paddr = skb_cb->paddr;
	sg_items[1].len = prefetch_len;

	res = ath10k_hif_tx_sg(htt->ar,
			       htt->ar->htc.endpoint[htt->eid].ul_pipe_id,
			       sg_items, ARRAY_SIZE(sg_items));
857
	if (res)
858
		goto err_unmap_msdu;
859 860

	return 0;
M
Michal Kazior 已提交
861 862

err_unmap_msdu:
863
	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
M
Michal Kazior 已提交
864 865 866 867 868
err_free_msdu_id:
	spin_lock_bh(&htt->tx_lock);
	ath10k_htt_tx_free_msdu_id(htt, msdu_id);
	spin_unlock_bh(&htt->tx_lock);
err_tx_dec:
869
	ath10k_htt_tx_dec_pending(htt, limit_mgmt_desc);
M
Michal Kazior 已提交
870
err:
871 872
	return res;
}