ath10k: cleanup HTT TX functions
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / htt_tx.c
1 /*
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
25 void __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
32 static 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
39 static 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
54 exit:
55         spin_unlock_bh(&htt->tx_lock);
56         return ret;
57 }
58
59 int ath10k_htt_tx_alloc_msdu_id(struct ath10k_htt *htt)
60 {
61         int msdu_id;
62
63         lockdep_assert_held(&htt->tx_lock);
64
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)
68                 return -ENOBUFS;
69
70         ath10k_dbg(ATH10K_DBG_HTT, "htt tx alloc msdu_id %d\n", msdu_id);
71         __set_bit(msdu_id, htt->used_msdu_ids);
72         return msdu_id;
73 }
74
75 void ath10k_htt_tx_free_msdu_id(struct ath10k_htt *htt, u16 msdu_id)
76 {
77         lockdep_assert_held(&htt->tx_lock);
78
79         if (!test_bit(msdu_id, htt->used_msdu_ids))
80                 ath10k_warn("trying to free unallocated msdu_id %d\n", msdu_id);
81
82         ath10k_dbg(ATH10K_DBG_HTT, "htt tx free msdu_id %hu\n", msdu_id);
83         __clear_bit(msdu_id, htt->used_msdu_ids);
84 }
85
86 int ath10k_htt_tx_attach(struct ath10k_htt *htt)
87 {
88         u8 pipe;
89
90         spin_lock_init(&htt->tx_lock);
91         init_waitqueue_head(&htt->empty_tx_wq);
92
93         /* At the beginning free queue number should hint us the maximum
94          * queue length */
95         pipe = htt->ar->htc.endpoint[htt->eid].ul_pipe_id;
96         htt->max_num_pending_tx = ath10k_hif_get_free_queue_number(htt->ar,
97                                                                    pipe);
98
99         ath10k_dbg(ATH10K_DBG_BOOT, "htt tx max num pending tx %d\n",
100                    htt->max_num_pending_tx);
101
102         htt->pending_tx = kzalloc(sizeof(*htt->pending_tx) *
103                                   htt->max_num_pending_tx, GFP_KERNEL);
104         if (!htt->pending_tx)
105                 return -ENOMEM;
106
107         htt->used_msdu_ids = kzalloc(sizeof(unsigned long) *
108                                      BITS_TO_LONGS(htt->max_num_pending_tx),
109                                      GFP_KERNEL);
110         if (!htt->used_msdu_ids) {
111                 kfree(htt->pending_tx);
112                 return -ENOMEM;
113         }
114
115         return 0;
116 }
117
118 static void ath10k_htt_tx_cleanup_pending(struct ath10k_htt *htt)
119 {
120         struct htt_tx_done tx_done = {0};
121         int msdu_id;
122
123         /* No locks needed. Called after communication with the device has
124          * been stopped. */
125
126         for (msdu_id = 0; msdu_id < htt->max_num_pending_tx; msdu_id++) {
127                 if (!test_bit(msdu_id, htt->used_msdu_ids))
128                         continue;
129
130                 ath10k_dbg(ATH10K_DBG_HTT, "force cleanup msdu_id %hu\n",
131                            msdu_id);
132
133                 tx_done.discard = 1;
134                 tx_done.msdu_id = msdu_id;
135
136                 ath10k_txrx_tx_unref(htt, &tx_done);
137         }
138 }
139
140 void ath10k_htt_tx_detach(struct ath10k_htt *htt)
141 {
142         ath10k_htt_tx_cleanup_pending(htt);
143         kfree(htt->pending_tx);
144         kfree(htt->used_msdu_ids);
145         return;
146 }
147
148 void ath10k_htt_htc_tx_complete(struct ath10k *ar, struct sk_buff *skb)
149 {
150         dev_kfree_skb_any(skb);
151 }
152
153 int ath10k_htt_h2t_ver_req_msg(struct ath10k_htt *htt)
154 {
155         struct sk_buff *skb;
156         struct htt_cmd *cmd;
157         int len = 0;
158         int ret;
159
160         len += sizeof(cmd->hdr);
161         len += sizeof(cmd->ver_req);
162
163         skb = ath10k_htc_alloc_skb(len);
164         if (!skb)
165                 return -ENOMEM;
166
167         skb_put(skb, len);
168         cmd = (struct htt_cmd *)skb->data;
169         cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_VERSION_REQ;
170
171         ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
172         if (ret) {
173                 dev_kfree_skb_any(skb);
174                 return ret;
175         }
176
177         return 0;
178 }
179
180 int ath10k_htt_h2t_stats_req(struct ath10k_htt *htt, u8 mask, u64 cookie)
181 {
182         struct htt_stats_req *req;
183         struct sk_buff *skb;
184         struct htt_cmd *cmd;
185         int len = 0, ret;
186
187         len += sizeof(cmd->hdr);
188         len += sizeof(cmd->stats_req);
189
190         skb = ath10k_htc_alloc_skb(len);
191         if (!skb)
192                 return -ENOMEM;
193
194         skb_put(skb, len);
195         cmd = (struct htt_cmd *)skb->data;
196         cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_STATS_REQ;
197
198         req = &cmd->stats_req;
199
200         memset(req, 0, sizeof(*req));
201
202         /* currently we support only max 8 bit masks so no need to worry
203          * about endian support */
204         req->upload_types[0] = mask;
205         req->reset_types[0] = mask;
206         req->stat_type = HTT_STATS_REQ_CFG_STAT_TYPE_INVALID;
207         req->cookie_lsb = cpu_to_le32(cookie & 0xffffffff);
208         req->cookie_msb = cpu_to_le32((cookie & 0xffffffff00000000ULL) >> 32);
209
210         ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
211         if (ret) {
212                 ath10k_warn("failed to send htt type stats request: %d", ret);
213                 dev_kfree_skb_any(skb);
214                 return ret;
215         }
216
217         return 0;
218 }
219
220 int ath10k_htt_send_rx_ring_cfg_ll(struct ath10k_htt *htt)
221 {
222         struct sk_buff *skb;
223         struct htt_cmd *cmd;
224         struct htt_rx_ring_setup_ring *ring;
225         const int num_rx_ring = 1;
226         u16 flags;
227         u32 fw_idx;
228         int len;
229         int ret;
230
231         /*
232          * the HW expects the buffer to be an integral number of 4-byte
233          * "words"
234          */
235         BUILD_BUG_ON(!IS_ALIGNED(HTT_RX_BUF_SIZE, 4));
236         BUILD_BUG_ON((HTT_RX_BUF_SIZE & HTT_MAX_CACHE_LINE_SIZE_MASK) != 0);
237
238         len = sizeof(cmd->hdr) + sizeof(cmd->rx_setup.hdr)
239             + (sizeof(*ring) * num_rx_ring);
240         skb = ath10k_htc_alloc_skb(len);
241         if (!skb)
242                 return -ENOMEM;
243
244         skb_put(skb, len);
245
246         cmd = (struct htt_cmd *)skb->data;
247         ring = &cmd->rx_setup.rings[0];
248
249         cmd->hdr.msg_type = HTT_H2T_MSG_TYPE_RX_RING_CFG;
250         cmd->rx_setup.hdr.num_rings = 1;
251
252         /* FIXME: do we need all of this? */
253         flags = 0;
254         flags |= HTT_RX_RING_FLAGS_MAC80211_HDR;
255         flags |= HTT_RX_RING_FLAGS_MSDU_PAYLOAD;
256         flags |= HTT_RX_RING_FLAGS_PPDU_START;
257         flags |= HTT_RX_RING_FLAGS_PPDU_END;
258         flags |= HTT_RX_RING_FLAGS_MPDU_START;
259         flags |= HTT_RX_RING_FLAGS_MPDU_END;
260         flags |= HTT_RX_RING_FLAGS_MSDU_START;
261         flags |= HTT_RX_RING_FLAGS_MSDU_END;
262         flags |= HTT_RX_RING_FLAGS_RX_ATTENTION;
263         flags |= HTT_RX_RING_FLAGS_FRAG_INFO;
264         flags |= HTT_RX_RING_FLAGS_UNICAST_RX;
265         flags |= HTT_RX_RING_FLAGS_MULTICAST_RX;
266         flags |= HTT_RX_RING_FLAGS_CTRL_RX;
267         flags |= HTT_RX_RING_FLAGS_MGMT_RX;
268         flags |= HTT_RX_RING_FLAGS_NULL_RX;
269         flags |= HTT_RX_RING_FLAGS_PHY_DATA_RX;
270
271         fw_idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
272
273         ring->fw_idx_shadow_reg_paddr =
274                 __cpu_to_le32(htt->rx_ring.alloc_idx.paddr);
275         ring->rx_ring_base_paddr = __cpu_to_le32(htt->rx_ring.base_paddr);
276         ring->rx_ring_len = __cpu_to_le16(htt->rx_ring.size);
277         ring->rx_ring_bufsize = __cpu_to_le16(HTT_RX_BUF_SIZE);
278         ring->flags = __cpu_to_le16(flags);
279         ring->fw_idx_init_val = __cpu_to_le16(fw_idx);
280
281 #define desc_offset(x) (offsetof(struct htt_rx_desc, x) / 4)
282
283         ring->mac80211_hdr_offset = __cpu_to_le16(desc_offset(rx_hdr_status));
284         ring->msdu_payload_offset = __cpu_to_le16(desc_offset(msdu_payload));
285         ring->ppdu_start_offset = __cpu_to_le16(desc_offset(ppdu_start));
286         ring->ppdu_end_offset = __cpu_to_le16(desc_offset(ppdu_end));
287         ring->mpdu_start_offset = __cpu_to_le16(desc_offset(mpdu_start));
288         ring->mpdu_end_offset = __cpu_to_le16(desc_offset(mpdu_end));
289         ring->msdu_start_offset = __cpu_to_le16(desc_offset(msdu_start));
290         ring->msdu_end_offset = __cpu_to_le16(desc_offset(msdu_end));
291         ring->rx_attention_offset = __cpu_to_le16(desc_offset(attention));
292         ring->frag_info_offset = __cpu_to_le16(desc_offset(frag_info));
293
294 #undef desc_offset
295
296         ret = ath10k_htc_send(&htt->ar->htc, htt->eid, skb);
297         if (ret) {
298                 dev_kfree_skb_any(skb);
299                 return ret;
300         }
301
302         return 0;
303 }
304
305 int ath10k_htt_mgmt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
306 {
307         struct device *dev = htt->ar->dev;
308         struct sk_buff *txdesc = NULL;
309         struct htt_cmd *cmd;
310         u8 vdev_id = ATH10K_SKB_CB(msdu)->htt.vdev_id;
311         int len = 0;
312         int msdu_id = -1;
313         int res;
314
315
316         res = ath10k_htt_tx_inc_pending(htt);
317         if (res)
318                 goto err;
319
320         len += sizeof(cmd->hdr);
321         len += sizeof(cmd->mgmt_tx);
322
323         spin_lock_bh(&htt->tx_lock);
324         res = ath10k_htt_tx_alloc_msdu_id(htt);
325         if (res < 0) {
326                 spin_unlock_bh(&htt->tx_lock);
327                 goto err_tx_dec;
328         }
329         msdu_id = res;
330         htt->pending_tx[msdu_id] = msdu;
331         spin_unlock_bh(&htt->tx_lock);
332
333         txdesc = ath10k_htc_alloc_skb(len);
334         if (!txdesc) {
335                 res = -ENOMEM;
336                 goto err_free_msdu_id;
337         }
338
339         res = ath10k_skb_map(dev, msdu);
340         if (res)
341                 goto err_free_txdesc;
342
343         skb_put(txdesc, len);
344         cmd = (struct htt_cmd *)txdesc->data;
345         cmd->hdr.msg_type         = HTT_H2T_MSG_TYPE_MGMT_TX;
346         cmd->mgmt_tx.msdu_paddr = __cpu_to_le32(ATH10K_SKB_CB(msdu)->paddr);
347         cmd->mgmt_tx.len        = __cpu_to_le32(msdu->len);
348         cmd->mgmt_tx.desc_id    = __cpu_to_le32(msdu_id);
349         cmd->mgmt_tx.vdev_id    = __cpu_to_le32(vdev_id);
350         memcpy(cmd->mgmt_tx.hdr, msdu->data,
351                min_t(int, msdu->len, HTT_MGMT_FRM_HDR_DOWNLOAD_LEN));
352
353         res = ath10k_htc_send(&htt->ar->htc, htt->eid, txdesc);
354         if (res)
355                 goto err_unmap_msdu;
356
357         return 0;
358
359 err_unmap_msdu:
360         ath10k_skb_unmap(dev, msdu);
361 err_free_txdesc:
362         dev_kfree_skb_any(txdesc);
363 err_free_msdu_id:
364         spin_lock_bh(&htt->tx_lock);
365         htt->pending_tx[msdu_id] = NULL;
366         ath10k_htt_tx_free_msdu_id(htt, msdu_id);
367         spin_unlock_bh(&htt->tx_lock);
368 err_tx_dec:
369         ath10k_htt_tx_dec_pending(htt);
370 err:
371         return res;
372 }
373
374 int ath10k_htt_tx(struct ath10k_htt *htt, struct sk_buff *msdu)
375 {
376         struct device *dev = htt->ar->dev;
377         struct htt_cmd *cmd;
378         struct htt_data_tx_desc_frag *tx_frags;
379         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msdu->data;
380         struct sk_buff *txdesc = NULL;
381         struct sk_buff *txfrag = NULL;
382         bool use_frags;
383         u8 vdev_id = ATH10K_SKB_CB(msdu)->htt.vdev_id;
384         u8 tid;
385         int prefetch_len, desc_len, frag_len;
386         dma_addr_t frags_paddr;
387         int msdu_id = -1;
388         int res;
389         u8 flags0;
390         u16 flags1;
391
392         res = ath10k_htt_tx_inc_pending(htt);
393         if (res)
394                 goto err;
395
396         spin_lock_bh(&htt->tx_lock);
397         res = ath10k_htt_tx_alloc_msdu_id(htt);
398         if (res < 0) {
399                 spin_unlock_bh(&htt->tx_lock);
400                 goto err_tx_dec;
401         }
402         msdu_id = res;
403         htt->pending_tx[msdu_id] = msdu;
404         spin_unlock_bh(&htt->tx_lock);
405
406         prefetch_len = min(htt->prefetch_len, msdu->len);
407         prefetch_len = roundup(prefetch_len, 4);
408
409         desc_len = sizeof(cmd->hdr) + sizeof(cmd->data_tx) + prefetch_len;
410         frag_len = sizeof(*tx_frags) * 2;
411
412         txdesc = ath10k_htc_alloc_skb(desc_len);
413         if (!txdesc) {
414                 res = -ENOMEM;
415                 goto err_free_msdu_id;
416         }
417
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);
423
424         if (use_frags) {
425                 txfrag = dev_alloc_skb(frag_len);
426                 if (!txfrag) {
427                         res = -ENOMEM;
428                         goto err_free_txdesc;
429                 }
430         }
431
432         if (!IS_ALIGNED((unsigned long)txdesc->data, 4)) {
433                 ath10k_warn("htt alignment check failed. dropping packet.\n");
434                 res = -EIO;
435                 goto err_free_txfrag;
436         }
437
438         res = ath10k_skb_map(dev, msdu);
439         if (res)
440                 goto err_free_txfrag;
441
442         if (use_frags) {
443                 /* tx fragment list must be terminated with zero-entry */
444                 skb_put(txfrag, frag_len);
445                 tx_frags = (struct htt_data_tx_desc_frag *)txfrag->data;
446                 tx_frags[0].paddr = __cpu_to_le32(ATH10K_SKB_CB(msdu)->paddr);
447                 tx_frags[0].len   = __cpu_to_le32(msdu->len);
448                 tx_frags[1].paddr = __cpu_to_le32(0);
449                 tx_frags[1].len   = __cpu_to_le32(0);
450
451                 res = ath10k_skb_map(dev, txfrag);
452                 if (res)
453                         goto err_unmap_msdu;
454
455                 ath10k_dbg(ATH10K_DBG_HTT, "txfrag 0x%llx\n",
456                            (unsigned long long) ATH10K_SKB_CB(txfrag)->paddr);
457                 ath10k_dbg_dump(ATH10K_DBG_HTT_DUMP, NULL, "txfrag: ",
458                                 txfrag->data, frag_len);
459         }
460
461         ath10k_dbg(ATH10K_DBG_HTT, "msdu 0x%llx\n",
462                    (unsigned long long) ATH10K_SKB_CB(msdu)->paddr);
463         ath10k_dbg_dump(ATH10K_DBG_HTT_DUMP, NULL, "msdu: ",
464                         msdu->data, msdu->len);
465
466         skb_put(txdesc, desc_len);
467         cmd = (struct htt_cmd *)txdesc->data;
468
469         tid = ATH10K_SKB_CB(msdu)->htt.tid;
470
471         ath10k_dbg(ATH10K_DBG_HTT, "htt data tx using tid %hhu\n", tid);
472
473         flags0  = 0;
474         if (!ieee80211_has_protected(hdr->frame_control))
475                 flags0 |= HTT_DATA_TX_DESC_FLAGS0_NO_ENCRYPT;
476         flags0 |= HTT_DATA_TX_DESC_FLAGS0_MAC_HDR_PRESENT;
477
478         if (use_frags)
479                 flags0 |= SM(ATH10K_HW_TXRX_NATIVE_WIFI,
480                              HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
481         else
482                 flags0 |= SM(ATH10K_HW_TXRX_MGMT,
483                              HTT_DATA_TX_DESC_FLAGS0_PKT_TYPE);
484
485         flags1  = 0;
486         flags1 |= SM((u16)vdev_id, HTT_DATA_TX_DESC_FLAGS1_VDEV_ID);
487         flags1 |= SM((u16)tid, HTT_DATA_TX_DESC_FLAGS1_EXT_TID);
488         flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L3_OFFLOAD;
489         flags1 |= HTT_DATA_TX_DESC_FLAGS1_CKSUM_L4_OFFLOAD;
490
491         if (use_frags)
492                 frags_paddr = ATH10K_SKB_CB(txfrag)->paddr;
493         else
494                 frags_paddr = ATH10K_SKB_CB(msdu)->paddr;
495
496         cmd->hdr.msg_type        = HTT_H2T_MSG_TYPE_TX_FRM;
497         cmd->data_tx.flags0      = flags0;
498         cmd->data_tx.flags1      = __cpu_to_le16(flags1);
499         cmd->data_tx.len         = __cpu_to_le16(msdu->len);
500         cmd->data_tx.id          = __cpu_to_le16(msdu_id);
501         cmd->data_tx.frags_paddr = __cpu_to_le32(frags_paddr);
502         cmd->data_tx.peerid      = __cpu_to_le32(HTT_INVALID_PEERID);
503
504         memcpy(cmd->data_tx.prefetch, msdu->data, prefetch_len);
505
506         res = ath10k_htc_send(&htt->ar->htc, htt->eid, txdesc);
507         if (res)
508                 goto err_restore;
509
510         return 0;
511
512 err_restore:
513         if (use_frags)
514                 ath10k_skb_unmap(dev, txfrag);
515 err_unmap_msdu:
516         ath10k_skb_unmap(dev, msdu);
517 err_free_txfrag:
518         if (use_frags)
519                 dev_kfree_skb_any(txfrag);
520 err_free_txdesc:
521         dev_kfree_skb_any(txdesc);
522 err_free_msdu_id:
523         spin_lock_bh(&htt->tx_lock);
524         htt->pending_tx[msdu_id] = NULL;
525         ath10k_htt_tx_free_msdu_id(htt, msdu_id);
526         spin_unlock_bh(&htt->tx_lock);
527 err_tx_dec:
528         ath10k_htt_tx_dec_pending(htt);
529 err:
530         return res;
531 }