wl1251_tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (c) 1998-2007 Texas Instruments Incorporated
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include "wl1251.h"
  25. #include "wl1251_reg.h"
  26. #include "wl1251_tx.h"
  27. #include "wl1251_ps.h"
  28. #include "wl1251_io.h"
  29. static bool wl1251_tx_double_buffer_busy(struct wl1251 *wl, u32 data_out_count)
  30. {
  31. int used, data_in_count;
  32. data_in_count = wl->data_in_count;
  33. if (data_in_count < data_out_count)
  34. /* data_in_count has wrapped */
  35. data_in_count += TX_STATUS_DATA_OUT_COUNT_MASK + 1;
  36. used = data_in_count - data_out_count;
  37. WARN_ON(used < 0);
  38. WARN_ON(used > DP_TX_PACKET_RING_CHUNK_NUM);
  39. if (used >= DP_TX_PACKET_RING_CHUNK_NUM)
  40. return true;
  41. else
  42. return false;
  43. }
  44. static int wl1251_tx_path_status(struct wl1251 *wl)
  45. {
  46. u32 status, addr, data_out_count;
  47. bool busy;
  48. addr = wl->data_path->tx_control_addr;
  49. status = wl1251_mem_read32(wl, addr);
  50. data_out_count = status & TX_STATUS_DATA_OUT_COUNT_MASK;
  51. busy = wl1251_tx_double_buffer_busy(wl, data_out_count);
  52. if (busy)
  53. return -EBUSY;
  54. return 0;
  55. }
  56. static int wl1251_tx_id(struct wl1251 *wl, struct sk_buff *skb)
  57. {
  58. int i;
  59. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  60. if (wl->tx_frames[i] == NULL) {
  61. wl->tx_frames[i] = skb;
  62. return i;
  63. }
  64. return -EBUSY;
  65. }
  66. static void wl1251_tx_control(struct tx_double_buffer_desc *tx_hdr,
  67. struct ieee80211_tx_info *control, u16 fc)
  68. {
  69. *(u16 *)&tx_hdr->control = 0;
  70. tx_hdr->control.rate_policy = 0;
  71. /* 802.11 packets */
  72. tx_hdr->control.packet_type = 0;
  73. if (control->flags & IEEE80211_TX_CTL_NO_ACK)
  74. tx_hdr->control.ack_policy = 1;
  75. tx_hdr->control.tx_complete = 1;
  76. if ((fc & IEEE80211_FTYPE_DATA) &&
  77. ((fc & IEEE80211_STYPE_QOS_DATA) ||
  78. (fc & IEEE80211_STYPE_QOS_NULLFUNC)))
  79. tx_hdr->control.qos = 1;
  80. }
  81. /* RSN + MIC = 8 + 8 = 16 bytes (worst case - AES). */
  82. #define MAX_MSDU_SECURITY_LENGTH 16
  83. #define MAX_MPDU_SECURITY_LENGTH 16
  84. #define WLAN_QOS_HDR_LEN 26
  85. #define MAX_MPDU_HEADER_AND_SECURITY (MAX_MPDU_SECURITY_LENGTH + \
  86. WLAN_QOS_HDR_LEN)
  87. #define HW_BLOCK_SIZE 252
  88. static void wl1251_tx_frag_block_num(struct tx_double_buffer_desc *tx_hdr)
  89. {
  90. u16 payload_len, frag_threshold, mem_blocks;
  91. u16 num_mpdus, mem_blocks_per_frag;
  92. frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  93. tx_hdr->frag_threshold = cpu_to_le16(frag_threshold);
  94. payload_len = le16_to_cpu(tx_hdr->length) + MAX_MSDU_SECURITY_LENGTH;
  95. if (payload_len > frag_threshold) {
  96. mem_blocks_per_frag =
  97. ((frag_threshold + MAX_MPDU_HEADER_AND_SECURITY) /
  98. HW_BLOCK_SIZE) + 1;
  99. num_mpdus = payload_len / frag_threshold;
  100. mem_blocks = num_mpdus * mem_blocks_per_frag;
  101. payload_len -= num_mpdus * frag_threshold;
  102. num_mpdus++;
  103. } else {
  104. mem_blocks_per_frag = 0;
  105. mem_blocks = 0;
  106. num_mpdus = 1;
  107. }
  108. mem_blocks += (payload_len / HW_BLOCK_SIZE) + 1;
  109. if (num_mpdus > 1)
  110. mem_blocks += min(num_mpdus, mem_blocks_per_frag);
  111. tx_hdr->num_mem_blocks = mem_blocks;
  112. }
  113. static int wl1251_tx_fill_hdr(struct wl1251 *wl, struct sk_buff *skb,
  114. struct ieee80211_tx_info *control)
  115. {
  116. struct tx_double_buffer_desc *tx_hdr;
  117. struct ieee80211_rate *rate;
  118. int id;
  119. u16 fc;
  120. if (!skb)
  121. return -EINVAL;
  122. id = wl1251_tx_id(wl, skb);
  123. if (id < 0)
  124. return id;
  125. fc = *(u16 *)skb->data;
  126. tx_hdr = (struct tx_double_buffer_desc *) skb_push(skb,
  127. sizeof(*tx_hdr));
  128. tx_hdr->length = cpu_to_le16(skb->len - sizeof(*tx_hdr));
  129. rate = ieee80211_get_tx_rate(wl->hw, control);
  130. tx_hdr->rate = cpu_to_le16(rate->hw_value);
  131. tx_hdr->expiry_time = cpu_to_le32(1 << 16);
  132. tx_hdr->id = id;
  133. tx_hdr->xmit_queue = wl1251_tx_get_queue(skb_get_queue_mapping(skb));
  134. wl1251_tx_control(tx_hdr, control, fc);
  135. wl1251_tx_frag_block_num(tx_hdr);
  136. return 0;
  137. }
  138. /* We copy the packet to the target */
  139. static int wl1251_tx_send_packet(struct wl1251 *wl, struct sk_buff *skb,
  140. struct ieee80211_tx_info *control)
  141. {
  142. struct tx_double_buffer_desc *tx_hdr;
  143. int len;
  144. u32 addr;
  145. if (!skb)
  146. return -EINVAL;
  147. tx_hdr = (struct tx_double_buffer_desc *) skb->data;
  148. if (control->control.hw_key &&
  149. control->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  150. int hdrlen;
  151. __le16 fc;
  152. u16 length;
  153. u8 *pos;
  154. fc = *(__le16 *)(skb->data + sizeof(*tx_hdr));
  155. length = le16_to_cpu(tx_hdr->length) + WL1251_TKIP_IV_SPACE;
  156. tx_hdr->length = cpu_to_le16(length);
  157. hdrlen = ieee80211_hdrlen(fc);
  158. pos = skb_push(skb, WL1251_TKIP_IV_SPACE);
  159. memmove(pos, pos + WL1251_TKIP_IV_SPACE,
  160. sizeof(*tx_hdr) + hdrlen);
  161. }
  162. /* Revisit. This is a workaround for getting non-aligned packets.
  163. This happens at least with EAPOL packets from the user space.
  164. Our DMA requires packets to be aligned on a 4-byte boundary.
  165. */
  166. if (unlikely((long)skb->data & 0x03)) {
  167. int offset = (4 - (long)skb->data) & 0x03;
  168. wl1251_debug(DEBUG_TX, "skb offset %d", offset);
  169. /* check whether the current skb can be used */
  170. if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
  171. unsigned char *src = skb->data;
  172. /* align the buffer on a 4-byte boundary */
  173. skb_reserve(skb, offset);
  174. memmove(skb->data, src, skb->len);
  175. tx_hdr = (struct tx_double_buffer_desc *) skb->data;
  176. } else {
  177. wl1251_info("No handler, fixme!");
  178. return -EINVAL;
  179. }
  180. }
  181. /* Our skb->data at this point includes the HW header */
  182. len = WL1251_TX_ALIGN(skb->len);
  183. if (wl->data_in_count & 0x1)
  184. addr = wl->data_path->tx_packet_ring_addr +
  185. wl->data_path->tx_packet_ring_chunk_size;
  186. else
  187. addr = wl->data_path->tx_packet_ring_addr;
  188. wl1251_mem_write(wl, addr, skb->data, len);
  189. wl1251_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x "
  190. "queue %d", tx_hdr->id, skb, tx_hdr->length,
  191. tx_hdr->rate, tx_hdr->xmit_queue);
  192. return 0;
  193. }
  194. static void wl1251_tx_trigger(struct wl1251 *wl)
  195. {
  196. u32 data, addr;
  197. if (wl->data_in_count & 0x1) {
  198. addr = ACX_REG_INTERRUPT_TRIG_H;
  199. data = INTR_TRIG_TX_PROC1;
  200. } else {
  201. addr = ACX_REG_INTERRUPT_TRIG;
  202. data = INTR_TRIG_TX_PROC0;
  203. }
  204. wl1251_reg_write32(wl, addr, data);
  205. /* Bumping data in */
  206. wl->data_in_count = (wl->data_in_count + 1) &
  207. TX_STATUS_DATA_OUT_COUNT_MASK;
  208. }
  209. /* caller must hold wl->mutex */
  210. static int wl1251_tx_frame(struct wl1251 *wl, struct sk_buff *skb)
  211. {
  212. struct ieee80211_tx_info *info;
  213. int ret = 0;
  214. u8 idx;
  215. info = IEEE80211_SKB_CB(skb);
  216. if (info->control.hw_key) {
  217. idx = info->control.hw_key->hw_key_idx;
  218. if (unlikely(wl->default_key != idx)) {
  219. ret = wl1251_acx_default_key(wl, idx);
  220. if (ret < 0)
  221. return ret;
  222. }
  223. }
  224. ret = wl1251_tx_path_status(wl);
  225. if (ret < 0)
  226. return ret;
  227. ret = wl1251_tx_fill_hdr(wl, skb, info);
  228. if (ret < 0)
  229. return ret;
  230. ret = wl1251_tx_send_packet(wl, skb, info);
  231. if (ret < 0)
  232. return ret;
  233. wl1251_tx_trigger(wl);
  234. return ret;
  235. }
  236. void wl1251_tx_work(struct work_struct *work)
  237. {
  238. struct wl1251 *wl = container_of(work, struct wl1251, tx_work);
  239. struct sk_buff *skb;
  240. bool woken_up = false;
  241. int ret;
  242. mutex_lock(&wl->mutex);
  243. if (unlikely(wl->state == WL1251_STATE_OFF))
  244. goto out;
  245. while ((skb = skb_dequeue(&wl->tx_queue))) {
  246. if (!woken_up) {
  247. ret = wl1251_ps_elp_wakeup(wl);
  248. if (ret < 0)
  249. goto out;
  250. woken_up = true;
  251. }
  252. ret = wl1251_tx_frame(wl, skb);
  253. if (ret == -EBUSY) {
  254. skb_queue_head(&wl->tx_queue, skb);
  255. goto out;
  256. } else if (ret < 0) {
  257. dev_kfree_skb(skb);
  258. goto out;
  259. }
  260. }
  261. out:
  262. if (woken_up)
  263. wl1251_ps_elp_sleep(wl);
  264. mutex_unlock(&wl->mutex);
  265. }
  266. static const char *wl1251_tx_parse_status(u8 status)
  267. {
  268. /* 8 bit status field, one character per bit plus null */
  269. static char buf[9];
  270. int i = 0;
  271. memset(buf, 0, sizeof(buf));
  272. if (status & TX_DMA_ERROR)
  273. buf[i++] = 'm';
  274. if (status & TX_DISABLED)
  275. buf[i++] = 'd';
  276. if (status & TX_RETRY_EXCEEDED)
  277. buf[i++] = 'r';
  278. if (status & TX_TIMEOUT)
  279. buf[i++] = 't';
  280. if (status & TX_KEY_NOT_FOUND)
  281. buf[i++] = 'k';
  282. if (status & TX_ENCRYPT_FAIL)
  283. buf[i++] = 'e';
  284. if (status & TX_UNAVAILABLE_PRIORITY)
  285. buf[i++] = 'p';
  286. /* bit 0 is unused apparently */
  287. return buf;
  288. }
  289. static void wl1251_tx_packet_cb(struct wl1251 *wl,
  290. struct tx_result *result)
  291. {
  292. struct ieee80211_tx_info *info;
  293. struct sk_buff *skb;
  294. int hdrlen, ret;
  295. u8 *frame;
  296. skb = wl->tx_frames[result->id];
  297. if (skb == NULL) {
  298. wl1251_error("SKB for packet %d is NULL", result->id);
  299. return;
  300. }
  301. info = IEEE80211_SKB_CB(skb);
  302. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  303. (result->status == TX_SUCCESS))
  304. info->flags |= IEEE80211_TX_STAT_ACK;
  305. info->status.rates[0].count = result->ack_failures + 1;
  306. wl->stats.retry_count += result->ack_failures;
  307. /*
  308. * We have to remove our private TX header before pushing
  309. * the skb back to mac80211.
  310. */
  311. frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
  312. if (info->control.hw_key &&
  313. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  314. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  315. memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
  316. skb_pull(skb, WL1251_TKIP_IV_SPACE);
  317. }
  318. wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  319. " status 0x%x (%s)",
  320. result->id, skb, result->ack_failures, result->rate,
  321. result->status, wl1251_tx_parse_status(result->status));
  322. ieee80211_tx_status(wl->hw, skb);
  323. wl->tx_frames[result->id] = NULL;
  324. if (wl->tx_queue_stopped) {
  325. wl1251_debug(DEBUG_TX, "cb: queue was stopped");
  326. skb = skb_dequeue(&wl->tx_queue);
  327. /* The skb can be NULL because tx_work might have been
  328. scheduled before the queue was stopped making the
  329. queue empty */
  330. if (skb) {
  331. ret = wl1251_tx_frame(wl, skb);
  332. if (ret == -EBUSY) {
  333. /* firmware buffer is still full */
  334. wl1251_debug(DEBUG_TX, "cb: fw buffer "
  335. "still full");
  336. skb_queue_head(&wl->tx_queue, skb);
  337. return;
  338. } else if (ret < 0) {
  339. dev_kfree_skb(skb);
  340. return;
  341. }
  342. }
  343. wl1251_debug(DEBUG_TX, "cb: waking queues");
  344. ieee80211_wake_queues(wl->hw);
  345. wl->tx_queue_stopped = false;
  346. }
  347. }
  348. /* Called upon reception of a TX complete interrupt */
  349. void wl1251_tx_complete(struct wl1251 *wl)
  350. {
  351. int i, result_index, num_complete = 0;
  352. struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
  353. unsigned long flags;
  354. if (unlikely(wl->state != WL1251_STATE_ON))
  355. return;
  356. /* First we read the result */
  357. wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
  358. result, sizeof(result));
  359. result_index = wl->next_tx_complete;
  360. for (i = 0; i < ARRAY_SIZE(result); i++) {
  361. result_ptr = &result[result_index];
  362. if (result_ptr->done_1 == 1 &&
  363. result_ptr->done_2 == 1) {
  364. wl1251_tx_packet_cb(wl, result_ptr);
  365. result_ptr->done_1 = 0;
  366. result_ptr->done_2 = 0;
  367. result_index = (result_index + 1) &
  368. (FW_TX_CMPLT_BLOCK_SIZE - 1);
  369. num_complete++;
  370. } else {
  371. break;
  372. }
  373. }
  374. if (wl->tx_queue_stopped
  375. &&
  376. skb_queue_len(&wl->tx_queue) <= WL1251_TX_QUEUE_LOW_WATERMARK){
  377. /* firmware buffer has space, restart queues */
  378. wl1251_debug(DEBUG_TX, "tx_complete: waking queues");
  379. spin_lock_irqsave(&wl->wl_lock, flags);
  380. ieee80211_wake_queues(wl->hw);
  381. wl->tx_queue_stopped = false;
  382. spin_unlock_irqrestore(&wl->wl_lock, flags);
  383. ieee80211_queue_work(wl->hw, &wl->tx_work);
  384. }
  385. /* Every completed frame needs to be acknowledged */
  386. if (num_complete) {
  387. /*
  388. * If we've wrapped, we have to clear
  389. * the results in 2 steps.
  390. */
  391. if (result_index > wl->next_tx_complete) {
  392. /* Only 1 write is needed */
  393. wl1251_mem_write(wl,
  394. wl->data_path->tx_complete_addr +
  395. (wl->next_tx_complete *
  396. sizeof(struct tx_result)),
  397. &result[wl->next_tx_complete],
  398. num_complete *
  399. sizeof(struct tx_result));
  400. } else if (result_index < wl->next_tx_complete) {
  401. /* 2 writes are needed */
  402. wl1251_mem_write(wl,
  403. wl->data_path->tx_complete_addr +
  404. (wl->next_tx_complete *
  405. sizeof(struct tx_result)),
  406. &result[wl->next_tx_complete],
  407. (FW_TX_CMPLT_BLOCK_SIZE -
  408. wl->next_tx_complete) *
  409. sizeof(struct tx_result));
  410. wl1251_mem_write(wl,
  411. wl->data_path->tx_complete_addr,
  412. result,
  413. (num_complete -
  414. FW_TX_CMPLT_BLOCK_SIZE +
  415. wl->next_tx_complete) *
  416. sizeof(struct tx_result));
  417. } else {
  418. /* We have to write the whole array */
  419. wl1251_mem_write(wl,
  420. wl->data_path->tx_complete_addr,
  421. result,
  422. FW_TX_CMPLT_BLOCK_SIZE *
  423. sizeof(struct tx_result));
  424. }
  425. }
  426. wl->next_tx_complete = result_index;
  427. }
  428. /* caller must hold wl->mutex */
  429. void wl1251_tx_flush(struct wl1251 *wl)
  430. {
  431. int i;
  432. struct sk_buff *skb;
  433. struct ieee80211_tx_info *info;
  434. /* TX failure */
  435. /* control->flags = 0; FIXME */
  436. while ((skb = skb_dequeue(&wl->tx_queue))) {
  437. info = IEEE80211_SKB_CB(skb);
  438. wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  439. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  440. continue;
  441. ieee80211_tx_status(wl->hw, skb);
  442. }
  443. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  444. if (wl->tx_frames[i] != NULL) {
  445. skb = wl->tx_frames[i];
  446. info = IEEE80211_SKB_CB(skb);
  447. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  448. continue;
  449. ieee80211_tx_status(wl->hw, skb);
  450. wl->tx_frames[i] = NULL;
  451. }
  452. }