2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/etherdevice.h>
25 void __ath10k_htt_tx_dec_pending(struct ath10k_htt *htt)
27 htt->num_pending_tx--;
28 if (htt->num_pending_tx == htt->max_num_pending_tx - 1)
29 ieee80211_wake_queues(htt->ar->hw);
32 static void ath10k_htt_tx_dec_pending(struct ath10k_htt *htt)
34 spin_lock_bh(&htt->tx_lock);
35 __ath10k_htt_tx_dec_pending(htt);
36 spin_unlock_bh(&htt->tx_lock);
39 static int ath10k_htt_tx_inc_pending(struct ath10k_htt *htt)
43 spin_lock_bh(&htt->tx_lock);
45 if (htt->num_pending_tx >= htt->max_num_pending_tx) {
50 htt->num_pending_tx++;
51 if (htt->num_pending_tx == htt->max_num_pending_tx)
52 ieee80211_stop_queues(htt->ar->hw);
55 spin_unlock_bh(&htt->tx_lock);
59 int ath10k_htt_tx_alloc_msdu_id(struct ath10k_htt *htt)
63 lockdep_assert_held(&htt->tx_lock);
65 msdu_id = find_first_zero_bit(htt->used_msdu_ids,
66 htt->max_num_pending_tx);
67 if (msdu_id == htt->max_num_pending_tx)
70 ath10k_dbg(ATH10K_DBG_HTT, "htt tx alloc msdu_id %d\n", msdu_id);
71 __set_bit(msdu_id, htt->used_msdu_ids);
75 void ath10k_htt_tx_free_msdu_id(struct ath10k_htt *htt, u16 msdu_id)
77 lockdep_assert_held(&htt->tx_lock);
79 if (!test_bit(msdu_id, htt->used_msdu_ids))
80 ath10k_warn("trying to free unallocated msdu_id %d\n", msdu_id);
82 ath10k_dbg(ATH10K_DBG_HTT, "htt tx free msdu_id %hu\n", msdu_id);
83 __clear_bit(msdu_id, htt->used_msdu_ids);
86 int ath10k_htt_tx_alloc(struct ath10k_htt *htt)
88 spin_lock_init(&htt->tx_lock);
89 init_waitqueue_head(&htt->empty_tx_wq);
91 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, htt->ar->fw_features))
92 htt->max_num_pending_tx = TARGET_10X_NUM_MSDU_DESC;
94 htt->max_num_pending_tx = TARGET_NUM_MSDU_DESC;
96 ath10k_dbg(ATH10K_DBG_BOOT, "htt tx max num pending tx %d\n",
97 htt->max_num_pending_tx);
99 htt->pending_tx = kzalloc(sizeof(*htt->pending_tx) *
100 htt->max_num_pending_tx, GFP_KERNEL);
101 if (!htt->pending_tx)
104 htt->used_msdu_ids = kzalloc(sizeof(unsigned long) *
105 BITS_TO_LONGS(htt->max_num_pending_tx),
107 if (!htt->used_msdu_ids) {
108 kfree(htt->pending_tx);
112 htt->tx_pool = dma_pool_create("ath10k htt tx pool", htt->ar->dev,
113 sizeof(struct ath10k_htt_txbuf), 4, 0);
115 kfree(htt->used_msdu_ids);
116 kfree(htt->pending_tx);
123 static void ath10k_htt_tx_free_pending(struct ath10k_htt *htt)
125 struct htt_tx_done tx_done = {0};
128 spin_lock_bh(&htt->tx_lock);
129 for (msdu_id = 0; msdu_id < htt->max_num_pending_tx; msdu_id++) {
130 if (!test_bit(msdu_id, htt->used_msdu_ids))
133 ath10k_dbg(ATH10K_DBG_HTT, "force cleanup msdu_id %hu\n",
137 tx_done.msdu_id = msdu_id;
139 ath10k_txrx_tx_unref(htt, &tx_done);
141 spin_unlock_bh(&htt->tx_lock);
144 void ath10k_htt_tx_free(struct ath10k_htt *htt)
146 ath10k_htt_tx_free_pending(htt);
147 kfree(htt->pending_tx);
148 kfree(htt->used_msdu_ids);
149 dma_pool_destroy(htt->tx_pool);
153 void ath10k_htt_htc_tx_complete(struct ath10k *ar, struct sk_buff *skb)
155 dev_kfree_skb_any(skb);
158 int ath10k_htt_h2t_ver_req_msg(struct ath10k_htt *htt)
165 len += sizeof(cmd->hdr);
166 len += sizeof(cmd->ver_req);
168 skb = ath10k_htc_alloc_skb(len);
173 cmd = (struct htt_cmd *)skb->data;
174 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_VERSION_REQ;
176 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
178 dev_kfree_skb_any(skb);
185 int ath10k_htt_h2t_stats_req(struct ath10k_htt *htt, u8 mask, u64 cookie)
187 struct htt_stats_req *req;
192 len += sizeof(cmd->hdr);
193 len += sizeof(cmd->stats_req);
195 skb = ath10k_htc_alloc_skb(len);
200 cmd = (struct htt_cmd *)skb->data;
201 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_STATS_REQ;
203 req = &cmd->stats_req;
205 memset(req, 0, sizeof(*req));
207 /* currently we support only max 8 bit masks so no need to worry
208 * about endian support */
209 req->upload_types[0] = mask;
210 req->reset_types[0] = mask;
211 req->stat_type = HTT_STATS_REQ_CFG_STAT_TYPE_INVALID;
212 req->cookie_lsb = cpu_to_le32(cookie & 0xffffffff);
213 req->cookie_msb = cpu_to_le32((cookie & 0xffffffff00000000ULL) >> 32);
215 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
217 ath10k_warn("failed to send htt type stats request: %d", ret);
218 dev_kfree_skb_any(skb);
225 int ath10k_htt_send_rx_ring_cfg_ll(struct ath10k_htt *htt)
229 struct htt_rx_ring_setup_ring *ring;
230 const int num_rx_ring = 1;
237 * the HW expects the buffer to be an integral number of 4-byte
240 BUILD_BUG_ON(!IS_ALIGNED(HTT_RX_BUF_SIZE, 4));
241 BUILD_BUG_ON((HTT_RX_BUF_SIZE & HTT_MAX_CACHE_LINE_SIZE_MASK) != 0);
243 len = sizeof(cmd->hdr) + sizeof(cmd->rx_setup.hdr)
244 + (sizeof(*ring) * num_rx_ring);
245 skb = ath10k_htc_alloc_skb(len);
251 cmd = (struct htt_cmd *)skb->data;
252 ring = &cmd->rx_setup.rings[0];
254 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_RX_RING_CFG;
255 cmd->rx_setup.hdr.num_rings = 1;
257 /* FIXME: do we need all of this? */
259 flags |= HTT_RX_RING_FLAGS_MAC80211_HDR;
260 flags |= HTT_RX_RING_FLAGS_MSDU_PAYLOAD;
261 flags |= HTT_RX_RING_FLAGS_PPDU_START;
262 flags |= HTT_RX_RING_FLAGS_PPDU_END;
263 flags |= HTT_RX_RING_FLAGS_MPDU_START;
264 flags |= HTT_RX_RING_FLAGS_MPDU_END;
265 flags |= HTT_RX_RING_FLAGS_MSDU_START;
266 flags |= HTT_RX_RING_FLAGS_MSDU_END;
267 flags |= HTT_RX_RING_FLAGS_RX_ATTENTION;
268 flags |= HTT_RX_RING_FLAGS_FRAG_INFO;
269 flags |= HTT_RX_RING_FLAGS_UNICAST_RX;
270 flags |= HTT_RX_RING_FLAGS_MULTICAST_RX;
271 flags |= HTT_RX_RING_FLAGS_CTRL_RX;
272 flags |= HTT_RX_RING_FLAGS_MGMT_RX;
273 flags |= HTT_RX_RING_FLAGS_NULL_RX;
274 flags |= HTT_RX_RING_FLAGS_PHY_DATA_RX;
276 fw_idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
278 ring->fw_idx_shadow_reg_paddr =
279 __cpu_to_le32(htt->rx_ring.alloc_idx.paddr);
280 ring->rx_ring_base_paddr = __cpu_to_le32(htt->rx_ring.base_paddr);
281 ring->rx_ring_len = __cpu_to_le16(htt->rx_ring.size);
282 ring->rx_ring_bufsize = __cpu_to_le16(HTT_RX_BUF_SIZE);
283 ring->flags = __cpu_to_le16(flags);
284 ring->fw_idx_init_val = __cpu_to_le16(fw_idx);
286 #define desc_offset(x) (offsetof(struct htt_rx_desc, x) / 4)
288 ring->mac80211_hdr_offset = __cpu_to_le16(desc_offset(rx_hdr_status));
289 ring->msdu_payload_offset = __cpu_to_le16(desc_offset(msdu_payload));
290 ring->ppdu_start_offset = __cpu_to_le16(desc_offset(ppdu_start));
291 ring->ppdu_end_offset = __cpu_to_le16(desc_offset(ppdu_end));
292 ring->mpdu_start_offset = __cpu_to_le16(desc_offset(mpdu_start));
293 ring->mpdu_end_offset = __cpu_to_le16(desc_offset(mpdu_end));
294 ring->msdu_start_offset = __cpu_to_le16(desc_offset(msdu_start));
295 ring->msdu_end_offset = __cpu_to_le16(desc_offset(msdu_end));
296 ring->rx_attention_offset = __cpu_to_le16(desc_offset(attention));
297 ring->frag_info_offset = __cpu_to_le16(desc_offset(frag_info));
301 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
303 dev_kfree_skb_any(skb);
310 int ath10k_htt_mgmt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
312 struct device *dev = htt->ar->dev;
313 struct sk_buff *txdesc = NULL;
315 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(msdu);
316 u8 vdev_id = skb_cb->vdev_id;
322 res = ath10k_htt_tx_inc_pending(htt);
326 len += sizeof(cmd->hdr);
327 len += sizeof(cmd->mgmt_tx);
329 spin_lock_bh(&htt->tx_lock);
330 res = ath10k_htt_tx_alloc_msdu_id(htt);
332 spin_unlock_bh(&htt->tx_lock);
336 htt->pending_tx[msdu_id] = msdu;
337 spin_unlock_bh(&htt->tx_lock);
339 txdesc = ath10k_htc_alloc_skb(len);
342 goto err_free_msdu_id;
345 skb_cb->paddr = dma_map_single(dev, msdu->data, msdu->len,
347 res = dma_mapping_error(dev, skb_cb->paddr);
349 goto err_free_txdesc;
351 skb_put(txdesc, len);
352 cmd = (struct htt_cmd *)txdesc->data;
353 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_MGMT_TX;
354 cmd->mgmt_tx.msdu_paddr = __cpu_to_le32(ATH10K_SKB_CB(msdu)->paddr);
355 cmd->mgmt_tx.len = __cpu_to_le32(msdu->len);
356 cmd->mgmt_tx.desc_id = __cpu_to_le32(msdu_id);
357 cmd->mgmt_tx.vdev_id = __cpu_to_le32(vdev_id);
358 memcpy(cmd->mgmt_tx.hdr, msdu->data,
359 min_t(int, msdu->len, HTT_MGMT_FRM_HDR_DOWNLOAD_LEN));
361 skb_cb->htt.txbuf = NULL;
363 res = ath10k_htc_send(&htt->ar->htc, htt->eid, txdesc);
370 dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
372 dev_kfree_skb_any(txdesc);
374 spin_lock_bh(&htt->tx_lock);
375 htt->pending_tx[msdu_id] = NULL;
376 ath10k_htt_tx_free_msdu_id(htt, msdu_id);
377 spin_unlock_bh(&htt->tx_lock);
379 ath10k_htt_tx_dec_pending(htt);
384 int ath10k_htt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
386 struct device *dev = htt->ar->dev;
387 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msdu->data;
388 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(msdu);
389 struct ath10k_hif_sg_item sg_items[2];
390 struct htt_data_tx_desc_frag *frags;
391 u8 vdev_id = skb_cb->vdev_id;
392 u8 tid = skb_cb->htt.tid;
396 u16 msdu_id, flags1 = 0;
401 res = ath10k_htt_tx_inc_pending(htt);
405 spin_lock_bh(&htt->tx_lock);
406 res = ath10k_htt_tx_alloc_msdu_id(htt);
408 spin_unlock_bh(&htt->tx_lock);
412 htt->pending_tx[msdu_id] = msdu;
413 spin_unlock_bh(&htt->tx_lock);
415 prefetch_len = min(htt->prefetch_len, msdu->len);
416 prefetch_len = roundup(prefetch_len, 4);
418 /* Since HTT 3.0 there is no separate mgmt tx command. However in case
419 * of mgmt tx using TX_FRM there is not tx fragment list. Instead of tx
420 * fragment list host driver specifies directly frame pointer. */
421 use_frags = htt->target_version_major < 3 ||
422 !ieee80211_is_mgmt(hdr->frame_control);
424 skb_cb->htt.txbuf = dma_pool_alloc(htt->tx_pool, GFP_ATOMIC,
426 if (!skb_cb->htt.txbuf)
427 goto err_free_msdu_id;
428 skb_cb->htt.txbuf_paddr = paddr;
430 skb_cb->paddr = dma_map_single(dev, msdu->data, msdu->len,
432 res = dma_mapping_error(dev, skb_cb->paddr);
436 if (likely(use_frags)) {
437 frags = skb_cb->htt.txbuf->frags;
439 frags[0].paddr = __cpu_to_le32(skb_cb->paddr);
440 frags[0].len = __cpu_to_le32(msdu->len);
444 flags0 |= SM(ATH10K_HW_TXRX_NATIVE_WIFI,
445 HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
447 frags_paddr = skb_cb->htt.txbuf_paddr;
449 flags0 |= SM(ATH10K_HW_TXRX_MGMT,
450 HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
452 frags_paddr = skb_cb->paddr;
455 /* Normally all commands go through HTC which manages tx credits for
456 * each endpoint and notifies when tx is completed.
458 * HTT endpoint is creditless so there's no need to care about HTC
459 * flags. In that case it is trivial to fill the HTC header here.
461 * MSDU transmission is considered completed upon HTT event. This
462 * implies no relevant resources can be freed until after the event is
463 * received. That's why HTC tx completion handler itself is ignored by
464 * setting NULL to transfer_context for all sg items.
466 * There is simply no point in pushing HTT TX_FRM through HTC tx path
467 * as it's a waste of resources. By bypassing HTC it is possible to
468 * avoid extra memory allocations, compress data structures and thus
469 * improve performance. */
471 skb_cb->htt.txbuf->htc_hdr.eid = htt->eid;
472 skb_cb->htt.txbuf->htc_hdr.len = __cpu_to_le16(
473 sizeof(skb_cb->htt.txbuf->cmd_hdr) +
474 sizeof(skb_cb->htt.txbuf->cmd_tx) +
476 skb_cb->htt.txbuf->htc_hdr.flags = 0;
478 if (!ieee80211_has_protected(hdr->frame_control))
479 flags0 |= HTT_DATA_TX_DESC_FLAGS0_NO_ENCRYPT;
481 flags0 |= HTT_DATA_TX_DESC_FLAGS0_MAC_HDR_PRESENT;
483 flags1 |= SM((u16)vdev_id, HTT_DATA_TX_DESC_FLAGS1_VDEV_ID);
484 flags1 |= SM((u16)tid, HTT_DATA_TX_DESC_FLAGS1_EXT_TID);
485 flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L3_OFFLOAD;
486 flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L4_OFFLOAD;
488 skb_cb->htt.txbuf->cmd_hdr.msg_type = HTT_H2T_MSG_TYPE_TX_FRM;
489 skb_cb->htt.txbuf->cmd_tx.flags0 = flags0;
490 skb_cb->htt.txbuf->cmd_tx.flags1 = __cpu_to_le16(flags1);
491 skb_cb->htt.txbuf->cmd_tx.len = __cpu_to_le16(msdu->len);
492 skb_cb->htt.txbuf->cmd_tx.id = __cpu_to_le16(msdu_id);
493 skb_cb->htt.txbuf->cmd_tx.frags_paddr = __cpu_to_le32(frags_paddr);
494 skb_cb->htt.txbuf->cmd_tx.peerid = __cpu_to_le32(HTT_INVALID_PEERID);
496 ath10k_dbg(ATH10K_DBG_HTT,
497 "htt tx flags0 %hhu flags1 %hu len %d id %hu frags_paddr %08x, msdu_paddr %08x vdev %hhu tid %hhu\n",
498 flags0, flags1, msdu->len, msdu_id, frags_paddr,
499 (u32)skb_cb->paddr, vdev_id, tid);
500 ath10k_dbg_dump(ATH10K_DBG_HTT_DUMP, NULL, "htt tx msdu: ",
501 msdu->data, msdu->len);
503 sg_items[0].transfer_id = 0;
504 sg_items[0].transfer_context = NULL;
505 sg_items[0].vaddr = &skb_cb->htt.txbuf->htc_hdr;
506 sg_items[0].paddr = skb_cb->htt.txbuf_paddr +
507 sizeof(skb_cb->htt.txbuf->frags);
508 sg_items[0].len = sizeof(skb_cb->htt.txbuf->htc_hdr) +
509 sizeof(skb_cb->htt.txbuf->cmd_hdr) +
510 sizeof(skb_cb->htt.txbuf->cmd_tx);
512 sg_items[1].transfer_id = 0;
513 sg_items[1].transfer_context = NULL;
514 sg_items[1].vaddr = msdu->data;
515 sg_items[1].paddr = skb_cb->paddr;
516 sg_items[1].len = prefetch_len;
518 res = ath10k_hif_tx_sg(htt->ar,
519 htt->ar->htc.endpoint[htt->eid].ul_pipe_id,
520 sg_items, ARRAY_SIZE(sg_items));
527 dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
529 dma_pool_free(htt->tx_pool,
531 skb_cb->htt.txbuf_paddr);
533 spin_lock_bh(&htt->tx_lock);
534 htt->pending_tx[msdu_id] = NULL;
535 ath10k_htt_tx_free_msdu_id(htt, msdu_id);
536 spin_unlock_bh(&htt->tx_lock);
538 ath10k_htt_tx_dec_pending(htt);