blob: 1ab64631ba1bcc1745067b923f5429861ba5a00a [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
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.
8 *
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.
16 */
17
18#include <linux/etherdevice.h>
19#include "htt.h"
20#include "mac.h"
21#include "hif.h"
22#include "txrx.h"
23#include "debug.h"
24
25void __ath10k_htt_tx_dec_pending(struct ath10k_htt *htt)
26{
27 htt->num_pending_tx--;
28 if (htt->num_pending_tx == htt->max_num_pending_tx - 1)
29 ieee80211_wake_queues(htt->ar->hw);
30}
31
32static void ath10k_htt_tx_dec_pending(struct ath10k_htt *htt)
33{
34 spin_lock_bh(&htt->tx_lock);
35 __ath10k_htt_tx_dec_pending(htt);
36 spin_unlock_bh(&htt->tx_lock);
37}
38
39static int ath10k_htt_tx_inc_pending(struct ath10k_htt *htt)
40{
41 int ret = 0;
42
43 spin_lock_bh(&htt->tx_lock);
44
45 if (htt->num_pending_tx >= htt->max_num_pending_tx) {
46 ret = -EBUSY;
47 goto exit;
48 }
49
50 htt->num_pending_tx++;
51 if (htt->num_pending_tx == htt->max_num_pending_tx)
52 ieee80211_stop_queues(htt->ar->hw);
53
54exit:
55 spin_unlock_bh(&htt->tx_lock);
56 return ret;
57}
58
59int ath10k_htt_tx_alloc_msdu_id(struct ath10k_htt *htt)
60{
Michal Kazior7aa7a722014-08-25 12:09:38 +020061 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +030062 int msdu_id;
63
64 lockdep_assert_held(&htt->tx_lock);
65
66 msdu_id = find_first_zero_bit(htt->used_msdu_ids,
67 htt->max_num_pending_tx);
68 if (msdu_id == htt->max_num_pending_tx)
69 return -ENOBUFS;
70
Michal Kazior7aa7a722014-08-25 12:09:38 +020071 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx alloc msdu_id %d\n", msdu_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +030072 __set_bit(msdu_id, htt->used_msdu_ids);
73 return msdu_id;
74}
75
76void ath10k_htt_tx_free_msdu_id(struct ath10k_htt *htt, u16 msdu_id)
77{
Michal Kazior7aa7a722014-08-25 12:09:38 +020078 struct ath10k *ar = htt->ar;
79
Kalle Valo5e3dd152013-06-12 20:52:10 +030080 lockdep_assert_held(&htt->tx_lock);
81
82 if (!test_bit(msdu_id, htt->used_msdu_ids))
Michal Kazior7aa7a722014-08-25 12:09:38 +020083 ath10k_warn(ar, "trying to free unallocated msdu_id %d\n",
84 msdu_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +030085
Michal Kazior7aa7a722014-08-25 12:09:38 +020086 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx free msdu_id %hu\n", msdu_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +030087 __clear_bit(msdu_id, htt->used_msdu_ids);
88}
89
Michal Kazior95bf21f2014-05-16 17:15:39 +030090int ath10k_htt_tx_alloc(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +030091{
Michal Kazior7aa7a722014-08-25 12:09:38 +020092 struct ath10k *ar = htt->ar;
93
Kalle Valo5e3dd152013-06-12 20:52:10 +030094 spin_lock_init(&htt->tx_lock);
95 init_waitqueue_head(&htt->empty_tx_wq);
96
Michal Kazior60f85be2013-10-16 16:46:24 +030097 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, htt->ar->fw_features))
98 htt->max_num_pending_tx = TARGET_10X_NUM_MSDU_DESC;
99 else
100 htt->max_num_pending_tx = TARGET_NUM_MSDU_DESC;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300101
Michal Kazior7aa7a722014-08-25 12:09:38 +0200102 ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt tx max num pending tx %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300103 htt->max_num_pending_tx);
104
105 htt->pending_tx = kzalloc(sizeof(*htt->pending_tx) *
106 htt->max_num_pending_tx, GFP_KERNEL);
107 if (!htt->pending_tx)
108 return -ENOMEM;
109
110 htt->used_msdu_ids = kzalloc(sizeof(unsigned long) *
111 BITS_TO_LONGS(htt->max_num_pending_tx),
112 GFP_KERNEL);
113 if (!htt->used_msdu_ids) {
114 kfree(htt->pending_tx);
115 return -ENOMEM;
116 }
117
Michal Kaziora16942e2014-02-27 18:50:04 +0200118 htt->tx_pool = dma_pool_create("ath10k htt tx pool", htt->ar->dev,
119 sizeof(struct ath10k_htt_txbuf), 4, 0);
120 if (!htt->tx_pool) {
121 kfree(htt->used_msdu_ids);
122 kfree(htt->pending_tx);
123 return -ENOMEM;
124 }
125
Kalle Valo5e3dd152013-06-12 20:52:10 +0300126 return 0;
127}
128
Michal Kazior95bf21f2014-05-16 17:15:39 +0300129static void ath10k_htt_tx_free_pending(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300130{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200131 struct ath10k *ar = htt->ar;
Michal Kazior0a89f8a2013-09-18 14:43:20 +0200132 struct htt_tx_done tx_done = {0};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300133 int msdu_id;
134
Michal Kazior45967082014-02-27 18:50:05 +0200135 spin_lock_bh(&htt->tx_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300136 for (msdu_id = 0; msdu_id < htt->max_num_pending_tx; msdu_id++) {
137 if (!test_bit(msdu_id, htt->used_msdu_ids))
138 continue;
139
Michal Kazior7aa7a722014-08-25 12:09:38 +0200140 ath10k_dbg(ar, ATH10K_DBG_HTT, "force cleanup msdu_id %hu\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300141 msdu_id);
142
Michal Kazior0a89f8a2013-09-18 14:43:20 +0200143 tx_done.discard = 1;
144 tx_done.msdu_id = msdu_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300145
Michal Kazior0a89f8a2013-09-18 14:43:20 +0200146 ath10k_txrx_tx_unref(htt, &tx_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300147 }
Michal Kazior45967082014-02-27 18:50:05 +0200148 spin_unlock_bh(&htt->tx_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300149}
150
Michal Kazior95bf21f2014-05-16 17:15:39 +0300151void ath10k_htt_tx_free(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300152{
Michal Kazior95bf21f2014-05-16 17:15:39 +0300153 ath10k_htt_tx_free_pending(htt);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300154 kfree(htt->pending_tx);
155 kfree(htt->used_msdu_ids);
Michal Kaziora16942e2014-02-27 18:50:04 +0200156 dma_pool_destroy(htt->tx_pool);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300157 return;
158}
159
160void ath10k_htt_htc_tx_complete(struct ath10k *ar, struct sk_buff *skb)
161{
Michal Kazior0a89f8a2013-09-18 14:43:20 +0200162 dev_kfree_skb_any(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300163}
164
165int ath10k_htt_h2t_ver_req_msg(struct ath10k_htt *htt)
166{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200167 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300168 struct sk_buff *skb;
169 struct htt_cmd *cmd;
170 int len = 0;
171 int ret;
172
173 len += sizeof(cmd->hdr);
174 len += sizeof(cmd->ver_req);
175
Michal Kazior7aa7a722014-08-25 12:09:38 +0200176 skb = ath10k_htc_alloc_skb(ar, len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300177 if (!skb)
178 return -ENOMEM;
179
180 skb_put(skb, len);
181 cmd = (struct htt_cmd *)skb->data;
182 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_VERSION_REQ;
183
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300184 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300185 if (ret) {
186 dev_kfree_skb_any(skb);
187 return ret;
188 }
189
190 return 0;
191}
192
Kalle Valoa3d135e2013-09-03 11:44:10 +0300193int ath10k_htt_h2t_stats_req(struct ath10k_htt *htt, u8 mask, u64 cookie)
194{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200195 struct ath10k *ar = htt->ar;
Kalle Valoa3d135e2013-09-03 11:44:10 +0300196 struct htt_stats_req *req;
197 struct sk_buff *skb;
198 struct htt_cmd *cmd;
199 int len = 0, ret;
200
201 len += sizeof(cmd->hdr);
202 len += sizeof(cmd->stats_req);
203
Michal Kazior7aa7a722014-08-25 12:09:38 +0200204 skb = ath10k_htc_alloc_skb(ar, len);
Kalle Valoa3d135e2013-09-03 11:44:10 +0300205 if (!skb)
206 return -ENOMEM;
207
208 skb_put(skb, len);
209 cmd = (struct htt_cmd *)skb->data;
210 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_STATS_REQ;
211
212 req = &cmd->stats_req;
213
214 memset(req, 0, sizeof(*req));
215
216 /* currently we support only max 8 bit masks so no need to worry
217 * about endian support */
218 req->upload_types[0] = mask;
219 req->reset_types[0] = mask;
220 req->stat_type = HTT_STATS_REQ_CFG_STAT_TYPE_INVALID;
221 req->cookie_lsb = cpu_to_le32(cookie & 0xffffffff);
222 req->cookie_msb = cpu_to_le32((cookie & 0xffffffff00000000ULL) >> 32);
223
Kalle Valoa3d135e2013-09-03 11:44:10 +0300224 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
225 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200226 ath10k_warn(ar, "failed to send htt type stats request: %d",
227 ret);
Kalle Valoa3d135e2013-09-03 11:44:10 +0300228 dev_kfree_skb_any(skb);
229 return ret;
230 }
231
232 return 0;
233}
234
Kalle Valo5e3dd152013-06-12 20:52:10 +0300235int ath10k_htt_send_rx_ring_cfg_ll(struct ath10k_htt *htt)
236{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200237 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300238 struct sk_buff *skb;
239 struct htt_cmd *cmd;
240 struct htt_rx_ring_setup_ring *ring;
241 const int num_rx_ring = 1;
242 u16 flags;
243 u32 fw_idx;
244 int len;
245 int ret;
246
247 /*
248 * the HW expects the buffer to be an integral number of 4-byte
249 * "words"
250 */
251 BUILD_BUG_ON(!IS_ALIGNED(HTT_RX_BUF_SIZE, 4));
252 BUILD_BUG_ON((HTT_RX_BUF_SIZE & HTT_MAX_CACHE_LINE_SIZE_MASK) != 0);
253
254 len = sizeof(cmd->hdr) + sizeof(cmd->rx_setup.hdr)
255 + (sizeof(*ring) * num_rx_ring);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200256 skb = ath10k_htc_alloc_skb(ar, len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300257 if (!skb)
258 return -ENOMEM;
259
260 skb_put(skb, len);
261
262 cmd = (struct htt_cmd *)skb->data;
263 ring = &cmd->rx_setup.rings[0];
264
265 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_RX_RING_CFG;
266 cmd->rx_setup.hdr.num_rings = 1;
267
268 /* FIXME: do we need all of this? */
269 flags = 0;
270 flags |= HTT_RX_RING_FLAGS_MAC80211_HDR;
271 flags |= HTT_RX_RING_FLAGS_MSDU_PAYLOAD;
272 flags |= HTT_RX_RING_FLAGS_PPDU_START;
273 flags |= HTT_RX_RING_FLAGS_PPDU_END;
274 flags |= HTT_RX_RING_FLAGS_MPDU_START;
275 flags |= HTT_RX_RING_FLAGS_MPDU_END;
276 flags |= HTT_RX_RING_FLAGS_MSDU_START;
277 flags |= HTT_RX_RING_FLAGS_MSDU_END;
278 flags |= HTT_RX_RING_FLAGS_RX_ATTENTION;
279 flags |= HTT_RX_RING_FLAGS_FRAG_INFO;
280 flags |= HTT_RX_RING_FLAGS_UNICAST_RX;
281 flags |= HTT_RX_RING_FLAGS_MULTICAST_RX;
282 flags |= HTT_RX_RING_FLAGS_CTRL_RX;
283 flags |= HTT_RX_RING_FLAGS_MGMT_RX;
284 flags |= HTT_RX_RING_FLAGS_NULL_RX;
285 flags |= HTT_RX_RING_FLAGS_PHY_DATA_RX;
286
287 fw_idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
288
289 ring->fw_idx_shadow_reg_paddr =
290 __cpu_to_le32(htt->rx_ring.alloc_idx.paddr);
291 ring->rx_ring_base_paddr = __cpu_to_le32(htt->rx_ring.base_paddr);
292 ring->rx_ring_len = __cpu_to_le16(htt->rx_ring.size);
293 ring->rx_ring_bufsize = __cpu_to_le16(HTT_RX_BUF_SIZE);
294 ring->flags = __cpu_to_le16(flags);
295 ring->fw_idx_init_val = __cpu_to_le16(fw_idx);
296
297#define desc_offset(x) (offsetof(struct htt_rx_desc, x) / 4)
298
299 ring->mac80211_hdr_offset = __cpu_to_le16(desc_offset(rx_hdr_status));
300 ring->msdu_payload_offset = __cpu_to_le16(desc_offset(msdu_payload));
301 ring->ppdu_start_offset = __cpu_to_le16(desc_offset(ppdu_start));
302 ring->ppdu_end_offset = __cpu_to_le16(desc_offset(ppdu_end));
303 ring->mpdu_start_offset = __cpu_to_le16(desc_offset(mpdu_start));
304 ring->mpdu_end_offset = __cpu_to_le16(desc_offset(mpdu_end));
305 ring->msdu_start_offset = __cpu_to_le16(desc_offset(msdu_start));
306 ring->msdu_end_offset = __cpu_to_le16(desc_offset(msdu_end));
307 ring->rx_attention_offset = __cpu_to_le16(desc_offset(attention));
308 ring->frag_info_offset = __cpu_to_le16(desc_offset(frag_info));
309
310#undef desc_offset
311
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300312 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300313 if (ret) {
314 dev_kfree_skb_any(skb);
315 return ret;
316 }
317
318 return 0;
319}
320
Janusz Dziedzicd3856232014-06-02 21:19:46 +0300321int ath10k_htt_h2t_aggr_cfg_msg(struct ath10k_htt *htt,
322 u8 max_subfrms_ampdu,
323 u8 max_subfrms_amsdu)
324{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200325 struct ath10k *ar = htt->ar;
Janusz Dziedzicd3856232014-06-02 21:19:46 +0300326 struct htt_aggr_conf *aggr_conf;
327 struct sk_buff *skb;
328 struct htt_cmd *cmd;
329 int len;
330 int ret;
331
332 /* Firmware defaults are: amsdu = 3 and ampdu = 64 */
333
334 if (max_subfrms_ampdu == 0 || max_subfrms_ampdu > 64)
335 return -EINVAL;
336
337 if (max_subfrms_amsdu == 0 || max_subfrms_amsdu > 31)
338 return -EINVAL;
339
340 len = sizeof(cmd->hdr);
341 len += sizeof(cmd->aggr_conf);
342
Michal Kazior7aa7a722014-08-25 12:09:38 +0200343 skb = ath10k_htc_alloc_skb(ar, len);
Janusz Dziedzicd3856232014-06-02 21:19:46 +0300344 if (!skb)
345 return -ENOMEM;
346
347 skb_put(skb, len);
348 cmd = (struct htt_cmd *)skb->data;
349 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_AGGR_CFG;
350
351 aggr_conf = &cmd->aggr_conf;
352 aggr_conf->max_num_ampdu_subframes = max_subfrms_ampdu;
353 aggr_conf->max_num_amsdu_subframes = max_subfrms_amsdu;
354
Michal Kazior7aa7a722014-08-25 12:09:38 +0200355 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt h2t aggr cfg msg amsdu %d ampdu %d",
Janusz Dziedzicd3856232014-06-02 21:19:46 +0300356 aggr_conf->max_num_amsdu_subframes,
357 aggr_conf->max_num_ampdu_subframes);
358
359 ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
360 if (ret) {
361 dev_kfree_skb_any(skb);
362 return ret;
363 }
364
365 return 0;
366}
367
Kalle Valo5e3dd152013-06-12 20:52:10 +0300368int ath10k_htt_mgmt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
369{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200370 struct ath10k *ar = htt->ar;
371 struct device *dev = ar->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300372 struct sk_buff *txdesc = NULL;
373 struct htt_cmd *cmd;
Michal Kazior1f8bb152013-09-18 14:43:22 +0200374 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(msdu);
Bartosz Markowski5e00d312013-09-26 17:47:12 +0200375 u8 vdev_id = skb_cb->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300376 int len = 0;
377 int msdu_id = -1;
378 int res;
379
Kalle Valo5e3dd152013-06-12 20:52:10 +0300380 res = ath10k_htt_tx_inc_pending(htt);
381 if (res)
Michal Kazior2f3773b2013-09-18 14:43:21 +0200382 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300383
384 len += sizeof(cmd->hdr);
385 len += sizeof(cmd->mgmt_tx);
386
Kalle Valo5e3dd152013-06-12 20:52:10 +0300387 spin_lock_bh(&htt->tx_lock);
Michal Kazior2f3773b2013-09-18 14:43:21 +0200388 res = ath10k_htt_tx_alloc_msdu_id(htt);
389 if (res < 0) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300390 spin_unlock_bh(&htt->tx_lock);
Michal Kazior2f3773b2013-09-18 14:43:21 +0200391 goto err_tx_dec;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300392 }
Michal Kazior2f3773b2013-09-18 14:43:21 +0200393 msdu_id = res;
Michal Kazior0a89f8a2013-09-18 14:43:20 +0200394 htt->pending_tx[msdu_id] = msdu;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300395 spin_unlock_bh(&htt->tx_lock);
396
Michal Kazior7aa7a722014-08-25 12:09:38 +0200397 txdesc = ath10k_htc_alloc_skb(ar, len);
Michal Kazior2f3773b2013-09-18 14:43:21 +0200398 if (!txdesc) {
399 res = -ENOMEM;
400 goto err_free_msdu_id;
401 }
402
Michal Kazior767d34f2014-02-27 18:50:03 +0200403 skb_cb->paddr = dma_map_single(dev, msdu->data, msdu->len,
404 DMA_TO_DEVICE);
405 res = dma_mapping_error(dev, skb_cb->paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300406 if (res)
Michal Kazior2f3773b2013-09-18 14:43:21 +0200407 goto err_free_txdesc;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300408
409 skb_put(txdesc, len);
410 cmd = (struct htt_cmd *)txdesc->data;
411 cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_MGMT_TX;
412 cmd->mgmt_tx.msdu_paddr = __cpu_to_le32(ATH10K_SKB_CB(msdu)->paddr);
413 cmd->mgmt_tx.len = __cpu_to_le32(msdu->len);
414 cmd->mgmt_tx.desc_id = __cpu_to_le32(msdu_id);
415 cmd->mgmt_tx.vdev_id = __cpu_to_le32(vdev_id);
416 memcpy(cmd->mgmt_tx.hdr, msdu->data,
417 min_t(int, msdu->len, HTT_MGMT_FRM_HDR_DOWNLOAD_LEN));
418
Michal Kaziora16942e2014-02-27 18:50:04 +0200419 skb_cb->htt.txbuf = NULL;
Michal Kazior1f8bb152013-09-18 14:43:22 +0200420
Michal Kaziorcd003fa2013-07-05 16:15:13 +0300421 res = ath10k_htc_send(&htt->ar->htc, htt->eid, txdesc);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300422 if (res)
Michal Kazior2f3773b2013-09-18 14:43:21 +0200423 goto err_unmap_msdu;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300424
425 return 0;
426
Michal Kazior2f3773b2013-09-18 14:43:21 +0200427err_unmap_msdu:
Michal Kazior767d34f2014-02-27 18:50:03 +0200428 dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
Michal Kazior2f3773b2013-09-18 14:43:21 +0200429err_free_txdesc:
430 dev_kfree_skb_any(txdesc);
431err_free_msdu_id:
432 spin_lock_bh(&htt->tx_lock);
433 htt->pending_tx[msdu_id] = NULL;
434 ath10k_htt_tx_free_msdu_id(htt, msdu_id);
435 spin_unlock_bh(&htt->tx_lock);
436err_tx_dec:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300437 ath10k_htt_tx_dec_pending(htt);
Michal Kazior2f3773b2013-09-18 14:43:21 +0200438err:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300439 return res;
440}
441
442int ath10k_htt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
443{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200444 struct ath10k *ar = htt->ar;
445 struct device *dev = ar->dev;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300446 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msdu->data;
Michal Kazior1f8bb152013-09-18 14:43:22 +0200447 struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(msdu);
Michal Kaziora16942e2014-02-27 18:50:04 +0200448 struct ath10k_hif_sg_item sg_items[2];
449 struct htt_data_tx_desc_frag *frags;
450 u8 vdev_id = skb_cb->vdev_id;
451 u8 tid = skb_cb->htt.tid;
452 int prefetch_len;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300453 int res;
Michal Kaziora16942e2014-02-27 18:50:04 +0200454 u8 flags0 = 0;
455 u16 msdu_id, flags1 = 0;
456 dma_addr_t paddr;
457 u32 frags_paddr;
458 bool use_frags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300459
460 res = ath10k_htt_tx_inc_pending(htt);
461 if (res)
Michal Kazior2f3773b2013-09-18 14:43:21 +0200462 goto err;
463
464 spin_lock_bh(&htt->tx_lock);
465 res = ath10k_htt_tx_alloc_msdu_id(htt);
466 if (res < 0) {
467 spin_unlock_bh(&htt->tx_lock);
468 goto err_tx_dec;
469 }
470 msdu_id = res;
471 htt->pending_tx[msdu_id] = msdu;
472 spin_unlock_bh(&htt->tx_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300473
474 prefetch_len = min(htt->prefetch_len, msdu->len);
475 prefetch_len = roundup(prefetch_len, 4);
476
Michal Kazior961d4c32013-08-09 10:13:34 +0200477 /* Since HTT 3.0 there is no separate mgmt tx command. However in case
478 * of mgmt tx using TX_FRM there is not tx fragment list. Instead of tx
479 * fragment list host driver specifies directly frame pointer. */
Michal Kazior2f3773b2013-09-18 14:43:21 +0200480 use_frags = htt->target_version_major < 3 ||
481 !ieee80211_is_mgmt(hdr->frame_control);
482
Michal Kaziora16942e2014-02-27 18:50:04 +0200483 skb_cb->htt.txbuf = dma_pool_alloc(htt->tx_pool, GFP_ATOMIC,
484 &paddr);
485 if (!skb_cb->htt.txbuf)
486 goto err_free_msdu_id;
487 skb_cb->htt.txbuf_paddr = paddr;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300488
Michal Kazior767d34f2014-02-27 18:50:03 +0200489 skb_cb->paddr = dma_map_single(dev, msdu->data, msdu->len,
490 DMA_TO_DEVICE);
491 res = dma_mapping_error(dev, skb_cb->paddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300492 if (res)
Michal Kaziora16942e2014-02-27 18:50:04 +0200493 goto err_free_txbuf;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300494
Michal Kaziora16942e2014-02-27 18:50:04 +0200495 if (likely(use_frags)) {
496 frags = skb_cb->htt.txbuf->frags;
Michal Kazior1f8bb152013-09-18 14:43:22 +0200497
Michal Kaziora16942e2014-02-27 18:50:04 +0200498 frags[0].paddr = __cpu_to_le32(skb_cb->paddr);
499 frags[0].len = __cpu_to_le32(msdu->len);
500 frags[1].paddr = 0;
501 frags[1].len = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300502
Michal Kazior2f3773b2013-09-18 14:43:21 +0200503 flags0 |= SM(ATH10K_HW_TXRX_NATIVE_WIFI,
Michal Kazior961d4c32013-08-09 10:13:34 +0200504 HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
Michal Kaziora16942e2014-02-27 18:50:04 +0200505
506 frags_paddr = skb_cb->htt.txbuf_paddr;
507 } else {
Michal Kazior2f3773b2013-09-18 14:43:21 +0200508 flags0 |= SM(ATH10K_HW_TXRX_MGMT,
Michal Kazior961d4c32013-08-09 10:13:34 +0200509 HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300510
Michal Kaziora16942e2014-02-27 18:50:04 +0200511 frags_paddr = skb_cb->paddr;
512 }
513
514 /* Normally all commands go through HTC which manages tx credits for
515 * each endpoint and notifies when tx is completed.
516 *
517 * HTT endpoint is creditless so there's no need to care about HTC
518 * flags. In that case it is trivial to fill the HTC header here.
519 *
520 * MSDU transmission is considered completed upon HTT event. This
521 * implies no relevant resources can be freed until after the event is
522 * received. That's why HTC tx completion handler itself is ignored by
523 * setting NULL to transfer_context for all sg items.
524 *
525 * There is simply no point in pushing HTT TX_FRM through HTC tx path
526 * as it's a waste of resources. By bypassing HTC it is possible to
527 * avoid extra memory allocations, compress data structures and thus
528 * improve performance. */
529
530 skb_cb->htt.txbuf->htc_hdr.eid = htt->eid;
531 skb_cb->htt.txbuf->htc_hdr.len = __cpu_to_le16(
532 sizeof(skb_cb->htt.txbuf->cmd_hdr) +
533 sizeof(skb_cb->htt.txbuf->cmd_tx) +
534 prefetch_len);
535 skb_cb->htt.txbuf->htc_hdr.flags = 0;
536
537 if (!ieee80211_has_protected(hdr->frame_control))
538 flags0 |= HTT_DATA_TX_DESC_FLAGS0_NO_ENCRYPT;
539
540 flags0 |= HTT_DATA_TX_DESC_FLAGS0_MAC_HDR_PRESENT;
541
Kalle Valo5e3dd152013-06-12 20:52:10 +0300542 flags1 |= SM((u16)vdev_id, HTT_DATA_TX_DESC_FLAGS1_VDEV_ID);
543 flags1 |= SM((u16)tid, HTT_DATA_TX_DESC_FLAGS1_EXT_TID);
Michal Kazior7c199992013-07-31 10:47:57 +0200544 flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L3_OFFLOAD;
545 flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L4_OFFLOAD;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300546
Michal Kazior708b9bd2014-07-21 20:52:59 +0300547 /* Prevent firmware from sending up tx inspection requests. There's
548 * nothing ath10k can do with frames requested for inspection so force
549 * it to simply rely a regular tx completion with discard status.
550 */
551 flags1 |= HTT_DATA_TX_DESC_FLAGS1_POSTPONED;
552
Michal Kaziora16942e2014-02-27 18:50:04 +0200553 skb_cb->htt.txbuf->cmd_hdr.msg_type = HTT_H2T_MSG_TYPE_TX_FRM;
554 skb_cb->htt.txbuf->cmd_tx.flags0 = flags0;
555 skb_cb->htt.txbuf->cmd_tx.flags1 = __cpu_to_le16(flags1);
556 skb_cb->htt.txbuf->cmd_tx.len = __cpu_to_le16(msdu->len);
557 skb_cb->htt.txbuf->cmd_tx.id = __cpu_to_le16(msdu_id);
558 skb_cb->htt.txbuf->cmd_tx.frags_paddr = __cpu_to_le32(frags_paddr);
559 skb_cb->htt.txbuf->cmd_tx.peerid = __cpu_to_le32(HTT_INVALID_PEERID);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300560
Michal Kazior7aa7a722014-08-25 12:09:38 +0200561 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kaziora16942e2014-02-27 18:50:04 +0200562 "htt tx flags0 %hhu flags1 %hu len %d id %hu frags_paddr %08x, msdu_paddr %08x vdev %hhu tid %hhu\n",
563 flags0, flags1, msdu->len, msdu_id, frags_paddr,
564 (u32)skb_cb->paddr, vdev_id, tid);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200565 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt tx msdu: ",
Michal Kaziora16942e2014-02-27 18:50:04 +0200566 msdu->data, msdu->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300567
Michal Kaziora16942e2014-02-27 18:50:04 +0200568 sg_items[0].transfer_id = 0;
569 sg_items[0].transfer_context = NULL;
570 sg_items[0].vaddr = &skb_cb->htt.txbuf->htc_hdr;
571 sg_items[0].paddr = skb_cb->htt.txbuf_paddr +
572 sizeof(skb_cb->htt.txbuf->frags);
573 sg_items[0].len = sizeof(skb_cb->htt.txbuf->htc_hdr) +
574 sizeof(skb_cb->htt.txbuf->cmd_hdr) +
575 sizeof(skb_cb->htt.txbuf->cmd_tx);
576
577 sg_items[1].transfer_id = 0;
578 sg_items[1].transfer_context = NULL;
579 sg_items[1].vaddr = msdu->data;
580 sg_items[1].paddr = skb_cb->paddr;
581 sg_items[1].len = prefetch_len;
582
583 res = ath10k_hif_tx_sg(htt->ar,
584 htt->ar->htc.endpoint[htt->eid].ul_pipe_id,
585 sg_items, ARRAY_SIZE(sg_items));
Kalle Valo5e3dd152013-06-12 20:52:10 +0300586 if (res)
Michal Kazior1f8bb152013-09-18 14:43:22 +0200587 goto err_unmap_msdu;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300588
589 return 0;
Michal Kazior2f3773b2013-09-18 14:43:21 +0200590
Michal Kazior2f3773b2013-09-18 14:43:21 +0200591err_unmap_msdu:
Michal Kazior767d34f2014-02-27 18:50:03 +0200592 dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
Michal Kaziora16942e2014-02-27 18:50:04 +0200593err_free_txbuf:
594 dma_pool_free(htt->tx_pool,
595 skb_cb->htt.txbuf,
596 skb_cb->htt.txbuf_paddr);
Michal Kazior2f3773b2013-09-18 14:43:21 +0200597err_free_msdu_id:
598 spin_lock_bh(&htt->tx_lock);
599 htt->pending_tx[msdu_id] = NULL;
600 ath10k_htt_tx_free_msdu_id(htt, msdu_id);
601 spin_unlock_bh(&htt->tx_lock);
602err_tx_dec:
603 ath10k_htt_tx_dec_pending(htt);
604err:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300605 return res;
606}