libertas: move lbs_hard_start_xmit() into tx.c
[linux-2.6] / drivers / net / wireless / libertas / tx.c
1 /**
2   * This file contains the handling of TX in wlan driver.
3   */
4 #include <linux/netdevice.h>
5
6 #include "hostcmd.h"
7 #include "radiotap.h"
8 #include "decl.h"
9 #include "defs.h"
10 #include "dev.h"
11 #include "wext.h"
12
13 /**
14  *  @brief This function converts Tx/Rx rates from IEEE80211_RADIOTAP_RATE
15  *  units (500 Kb/s) into Marvell WLAN format (see Table 8 in Section 3.2.1)
16  *
17  *  @param rate    Input rate
18  *  @return      Output Rate (0 if invalid)
19  */
20 static u32 convert_radiotap_rate_to_mv(u8 rate)
21 {
22         switch (rate) {
23         case 2:         /*   1 Mbps */
24                 return 0 | (1 << 4);
25         case 4:         /*   2 Mbps */
26                 return 1 | (1 << 4);
27         case 11:                /* 5.5 Mbps */
28                 return 2 | (1 << 4);
29         case 22:                /*  11 Mbps */
30                 return 3 | (1 << 4);
31         case 12:                /*   6 Mbps */
32                 return 4 | (1 << 4);
33         case 18:                /*   9 Mbps */
34                 return 5 | (1 << 4);
35         case 24:                /*  12 Mbps */
36                 return 6 | (1 << 4);
37         case 36:                /*  18 Mbps */
38                 return 7 | (1 << 4);
39         case 48:                /*  24 Mbps */
40                 return 8 | (1 << 4);
41         case 72:                /*  36 Mbps */
42                 return 9 | (1 << 4);
43         case 96:                /*  48 Mbps */
44                 return 10 | (1 << 4);
45         case 108:               /*  54 Mbps */
46                 return 11 | (1 << 4);
47         }
48         return 0;
49 }
50
51 /**
52  *  @brief This function checks the conditions and sends packet to IF
53  *  layer if everything is ok.
54  *
55  *  @param priv    A pointer to struct lbs_private structure
56  *  @param skb     A pointer to skb which includes TX packet
57  *  @return        0 or -1
58  */
59 static int lbs_process_tx(struct lbs_private *priv, struct sk_buff *skb)
60 {
61         int ret = -1;
62         struct txpd localtxpd;
63         struct txpd *plocaltxpd = &localtxpd;
64         u8 *p802x_hdr;
65         struct tx_radiotap_hdr *pradiotap_hdr;
66         u32 new_rate;
67         u8 *ptr = priv->tmptxbuf;
68
69         lbs_deb_enter(LBS_DEB_TX);
70
71         lbs_deb_hex(LBS_DEB_TX, "TX Data", skb->data, min_t(unsigned int, skb->len, 100));
72
73         if (priv->dnld_sent) {
74                 lbs_pr_alert( "TX error: dnld_sent = %d, not sending\n",
75                        priv->dnld_sent);
76                 goto done;
77         }
78
79         if ((priv->psstate == PS_STATE_SLEEP) ||
80             (priv->psstate == PS_STATE_PRE_SLEEP)) {
81                 lbs_pr_alert("TX error: packet xmit in %ssleep mode\n",
82                              priv->psstate == PS_STATE_SLEEP?"":"pre-");
83                 goto done;
84         }
85
86         if (priv->surpriseremoved)
87                 return -1;
88
89         if (!skb->len || (skb->len > MRVDRV_ETH_TX_PACKET_BUFFER_SIZE)) {
90                 lbs_deb_tx("tx err: skb length %d 0 or > %zd\n",
91                        skb->len, MRVDRV_ETH_TX_PACKET_BUFFER_SIZE);
92                 goto done_tx;
93         }
94
95         ret = 0;
96         memset(plocaltxpd, 0, sizeof(struct txpd));
97
98         plocaltxpd->tx_packet_length = cpu_to_le16(skb->len);
99
100         /* offset of actual data */
101         plocaltxpd->tx_packet_location = cpu_to_le32(sizeof(struct txpd));
102
103         p802x_hdr = skb->data;
104         if (priv->monitormode != LBS_MONITOR_OFF) {
105
106                 /* locate radiotap header */
107                 pradiotap_hdr = (struct tx_radiotap_hdr *)skb->data;
108
109                 /* set txpd fields from the radiotap header */
110                 new_rate = convert_radiotap_rate_to_mv(pradiotap_hdr->rate);
111                 if (new_rate != 0) {
112                         /* use new tx_control[4:0] */
113                         plocaltxpd->tx_control = cpu_to_le32(new_rate);
114                 }
115
116                 /* skip the radiotap header */
117                 p802x_hdr += sizeof(struct tx_radiotap_hdr);
118                 plocaltxpd->tx_packet_length =
119                         cpu_to_le16(le16_to_cpu(plocaltxpd->tx_packet_length)
120                                     - sizeof(struct tx_radiotap_hdr));
121
122         }
123         /* copy destination address from 802.3 or 802.11 header */
124         if (priv->monitormode != LBS_MONITOR_OFF)
125                 memcpy(plocaltxpd->tx_dest_addr_high, p802x_hdr + 4, ETH_ALEN);
126         else
127                 memcpy(plocaltxpd->tx_dest_addr_high, p802x_hdr, ETH_ALEN);
128
129         lbs_deb_hex(LBS_DEB_TX, "txpd", (u8 *) plocaltxpd, sizeof(struct txpd));
130
131         if (IS_MESH_FRAME(skb)) {
132                 plocaltxpd->tx_control |= cpu_to_le32(TxPD_MESH_FRAME);
133         }
134
135         memcpy(ptr, plocaltxpd, sizeof(struct txpd));
136
137         ptr += sizeof(struct txpd);
138
139         lbs_deb_hex(LBS_DEB_TX, "Tx Data", (u8 *) p802x_hdr, le16_to_cpu(plocaltxpd->tx_packet_length));
140         memcpy(ptr, p802x_hdr, le16_to_cpu(plocaltxpd->tx_packet_length));
141         ret = priv->hw_host_to_card(priv, MVMS_DAT,
142                                     priv->tmptxbuf,
143                                     le16_to_cpu(plocaltxpd->tx_packet_length) +
144                                     sizeof(struct txpd));
145
146         if (ret) {
147                 lbs_deb_tx("tx err: hw_host_to_card returned 0x%X\n", ret);
148                 goto done_tx;
149         }
150
151         lbs_deb_tx("%s succeeds\n", __func__);
152
153 done_tx:
154         if (!ret) {
155                 priv->stats.tx_packets++;
156                 priv->stats.tx_bytes += skb->len;
157         } else {
158                 priv->stats.tx_dropped++;
159                 priv->stats.tx_errors++;
160         }
161
162         if (!ret && priv->monitormode != LBS_MONITOR_OFF) {
163                 /* Keep the skb to echo it back once Tx feedback is
164                    received from FW */
165                 skb_orphan(skb);
166                 /* stop processing outgoing pkts */
167                 netif_stop_queue(priv->dev);
168                 if (priv->mesh_dev)
169                         netif_stop_queue(priv->mesh_dev);
170
171                 /* Keep the skb around for when we get feedback */
172                 priv->currenttxskb = skb;
173         } else {
174                 dev_kfree_skb_any(skb);
175         }
176
177 done:
178         lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
179         return ret;
180 }
181
182 int lbs_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
183 {
184         int ret = 0;
185         struct lbs_private *priv = dev->priv;
186
187         lbs_deb_enter(LBS_DEB_TX);
188
189         /* We could return NETDEV_TX_BUSY here, but I'd actually 
190            like to get the point where we can BUG() */
191         if (priv->dnld_sent) {
192                 lbs_pr_err("%s while dnld_sent\n", __func__);
193                 priv->stats.tx_dropped++;
194                 goto done;
195         }
196         if (priv->currenttxskb) {
197                 lbs_pr_err("%s while TX skb pending\n", __func__);
198                 priv->stats.tx_dropped++;
199                 goto done;
200         }
201
202         netif_stop_queue(priv->dev);
203         if (priv->mesh_dev)
204                 netif_stop_queue(priv->mesh_dev);
205
206         if (lbs_process_tx(priv, skb) == 0)
207                 dev->trans_start = jiffies;
208 done:
209         lbs_deb_leave_args(LBS_DEB_TX, "ret %d", ret);
210         return ret;
211 }
212
213 /**
214  *  @brief This function sends to the host the last transmitted packet,
215  *  filling the radiotap headers with transmission information.
216  *
217  *  @param priv     A pointer to struct lbs_private structure
218  *  @param status   A 32 bit value containing transmission status.
219  *
220  *  @returns void
221  */
222 void lbs_send_tx_feedback(struct lbs_private *priv)
223 {
224         struct tx_radiotap_hdr *radiotap_hdr;
225         u32 status = priv->eventcause;
226         int txfail;
227         int try_count;
228
229         if (priv->monitormode == LBS_MONITOR_OFF ||
230             priv->currenttxskb == NULL)
231                 return;
232
233         radiotap_hdr = (struct tx_radiotap_hdr *)priv->currenttxskb->data;
234
235         txfail = (status >> 24);
236
237 #if 0
238         /* The version of roofnet that we've tested does not use this yet
239          * But it may be used in the future.
240          */
241         if (txfail)
242                 radiotap_hdr->flags &= IEEE80211_RADIOTAP_F_TX_FAIL;
243 #endif
244         try_count = (status >> 16) & 0xff;
245         radiotap_hdr->data_retries = (try_count) ?
246             (1 + priv->txretrycount - try_count) : 0;
247         lbs_upload_rx_packet(priv, priv->currenttxskb);
248         priv->currenttxskb = NULL;
249
250         if (priv->connect_status == LBS_CONNECTED)
251                 netif_wake_queue(priv->dev);
252
253         if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED))
254                 netif_wake_queue(priv->mesh_dev);
255 }
256 EXPORT_SYMBOL_GPL(lbs_send_tx_feedback);