wl1251_tx.c 13 KB

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