wl1251_tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 = le16_to_cpu(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. tx_hdr->xmit_queue = wl1251_tx_get_queue(skb_get_queue_mapping(skb));
  136. wl1251_tx_control(tx_hdr, control, fc);
  137. wl1251_tx_frag_block_num(tx_hdr);
  138. return 0;
  139. }
  140. /* We copy the packet to the target */
  141. static int wl1251_tx_send_packet(struct wl1251 *wl, struct sk_buff *skb,
  142. struct ieee80211_tx_info *control)
  143. {
  144. struct tx_double_buffer_desc *tx_hdr;
  145. int len;
  146. u32 addr;
  147. if (!skb)
  148. return -EINVAL;
  149. tx_hdr = (struct tx_double_buffer_desc *) skb->data;
  150. if (control->control.hw_key &&
  151. control->control.hw_key->alg == ALG_TKIP) {
  152. int hdrlen;
  153. __le16 fc;
  154. u16 length;
  155. u8 *pos;
  156. fc = *(__le16 *)(skb->data + sizeof(*tx_hdr));
  157. length = le16_to_cpu(tx_hdr->length) + WL1251_TKIP_IV_SPACE;
  158. tx_hdr->length = cpu_to_le16(length);
  159. hdrlen = ieee80211_hdrlen(fc);
  160. pos = skb_push(skb, WL1251_TKIP_IV_SPACE);
  161. memmove(pos, pos + WL1251_TKIP_IV_SPACE,
  162. sizeof(*tx_hdr) + hdrlen);
  163. }
  164. /* Revisit. This is a workaround for getting non-aligned packets.
  165. This happens at least with EAPOL packets from the user space.
  166. Our DMA requires packets to be aligned on a 4-byte boundary.
  167. */
  168. if (unlikely((long)skb->data & 0x03)) {
  169. int offset = (4 - (long)skb->data) & 0x03;
  170. wl1251_debug(DEBUG_TX, "skb offset %d", offset);
  171. /* check whether the current skb can be used */
  172. if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
  173. unsigned char *src = skb->data;
  174. /* align the buffer on a 4-byte boundary */
  175. skb_reserve(skb, offset);
  176. memmove(skb->data, src, skb->len);
  177. tx_hdr = (struct tx_double_buffer_desc *) skb->data;
  178. } else {
  179. wl1251_info("No handler, fixme!");
  180. return -EINVAL;
  181. }
  182. }
  183. /* Our skb->data at this point includes the HW header */
  184. len = WL1251_TX_ALIGN(skb->len);
  185. if (wl->data_in_count & 0x1)
  186. addr = wl->data_path->tx_packet_ring_addr +
  187. wl->data_path->tx_packet_ring_chunk_size;
  188. else
  189. addr = wl->data_path->tx_packet_ring_addr;
  190. wl1251_mem_write(wl, addr, skb->data, len);
  191. wl1251_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x "
  192. "queue %d", tx_hdr->id, skb, tx_hdr->length,
  193. tx_hdr->rate, tx_hdr->xmit_queue);
  194. return 0;
  195. }
  196. static void wl1251_tx_trigger(struct wl1251 *wl)
  197. {
  198. u32 data, addr;
  199. if (wl->data_in_count & 0x1) {
  200. addr = ACX_REG_INTERRUPT_TRIG_H;
  201. data = INTR_TRIG_TX_PROC1;
  202. } else {
  203. addr = ACX_REG_INTERRUPT_TRIG;
  204. data = INTR_TRIG_TX_PROC0;
  205. }
  206. wl1251_reg_write32(wl, addr, data);
  207. /* Bumping data in */
  208. wl->data_in_count = (wl->data_in_count + 1) &
  209. TX_STATUS_DATA_OUT_COUNT_MASK;
  210. }
  211. /* caller must hold wl->mutex */
  212. static int wl1251_tx_frame(struct wl1251 *wl, struct sk_buff *skb)
  213. {
  214. struct ieee80211_tx_info *info;
  215. int ret = 0;
  216. u8 idx;
  217. info = IEEE80211_SKB_CB(skb);
  218. if (info->control.hw_key) {
  219. idx = info->control.hw_key->hw_key_idx;
  220. if (unlikely(wl->default_key != idx)) {
  221. ret = wl1251_acx_default_key(wl, idx);
  222. if (ret < 0)
  223. return ret;
  224. }
  225. }
  226. ret = wl1251_tx_path_status(wl);
  227. if (ret < 0)
  228. return ret;
  229. ret = wl1251_tx_fill_hdr(wl, skb, info);
  230. if (ret < 0)
  231. return ret;
  232. ret = wl1251_tx_send_packet(wl, skb, info);
  233. if (ret < 0)
  234. return ret;
  235. wl1251_tx_trigger(wl);
  236. return ret;
  237. }
  238. void wl1251_tx_work(struct work_struct *work)
  239. {
  240. struct wl1251 *wl = container_of(work, struct wl1251, tx_work);
  241. struct sk_buff *skb;
  242. bool woken_up = false;
  243. int ret;
  244. mutex_lock(&wl->mutex);
  245. if (unlikely(wl->state == WL1251_STATE_OFF))
  246. goto out;
  247. while ((skb = skb_dequeue(&wl->tx_queue))) {
  248. if (!woken_up) {
  249. ret = wl1251_ps_elp_wakeup(wl);
  250. if (ret < 0)
  251. goto out;
  252. woken_up = true;
  253. }
  254. ret = wl1251_tx_frame(wl, skb);
  255. if (ret == -EBUSY) {
  256. /* firmware buffer is full, stop queues */
  257. wl1251_debug(DEBUG_TX, "tx_work: fw buffer full, "
  258. "stop queues");
  259. ieee80211_stop_queues(wl->hw);
  260. wl->tx_queue_stopped = true;
  261. skb_queue_head(&wl->tx_queue, skb);
  262. goto out;
  263. } else if (ret < 0) {
  264. dev_kfree_skb(skb);
  265. goto out;
  266. }
  267. }
  268. out:
  269. if (woken_up)
  270. wl1251_ps_elp_sleep(wl);
  271. mutex_unlock(&wl->mutex);
  272. }
  273. static const char *wl1251_tx_parse_status(u8 status)
  274. {
  275. /* 8 bit status field, one character per bit plus null */
  276. static char buf[9];
  277. int i = 0;
  278. memset(buf, 0, sizeof(buf));
  279. if (status & TX_DMA_ERROR)
  280. buf[i++] = 'm';
  281. if (status & TX_DISABLED)
  282. buf[i++] = 'd';
  283. if (status & TX_RETRY_EXCEEDED)
  284. buf[i++] = 'r';
  285. if (status & TX_TIMEOUT)
  286. buf[i++] = 't';
  287. if (status & TX_KEY_NOT_FOUND)
  288. buf[i++] = 'k';
  289. if (status & TX_ENCRYPT_FAIL)
  290. buf[i++] = 'e';
  291. if (status & TX_UNAVAILABLE_PRIORITY)
  292. buf[i++] = 'p';
  293. /* bit 0 is unused apparently */
  294. return buf;
  295. }
  296. static void wl1251_tx_packet_cb(struct wl1251 *wl,
  297. struct tx_result *result)
  298. {
  299. struct ieee80211_tx_info *info;
  300. struct sk_buff *skb;
  301. int hdrlen, ret;
  302. u8 *frame;
  303. skb = wl->tx_frames[result->id];
  304. if (skb == NULL) {
  305. wl1251_error("SKB for packet %d is NULL", result->id);
  306. return;
  307. }
  308. info = IEEE80211_SKB_CB(skb);
  309. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  310. (result->status == TX_SUCCESS))
  311. info->flags |= IEEE80211_TX_STAT_ACK;
  312. info->status.rates[0].count = result->ack_failures + 1;
  313. wl->stats.retry_count += result->ack_failures;
  314. /*
  315. * We have to remove our private TX header before pushing
  316. * the skb back to mac80211.
  317. */
  318. frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
  319. if (info->control.hw_key &&
  320. info->control.hw_key->alg == ALG_TKIP) {
  321. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  322. memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
  323. skb_pull(skb, WL1251_TKIP_IV_SPACE);
  324. }
  325. wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  326. " status 0x%x (%s)",
  327. result->id, skb, result->ack_failures, result->rate,
  328. result->status, wl1251_tx_parse_status(result->status));
  329. ieee80211_tx_status(wl->hw, skb);
  330. wl->tx_frames[result->id] = NULL;
  331. if (wl->tx_queue_stopped) {
  332. wl1251_debug(DEBUG_TX, "cb: queue was stopped");
  333. skb = skb_dequeue(&wl->tx_queue);
  334. /* The skb can be NULL because tx_work might have been
  335. scheduled before the queue was stopped making the
  336. queue empty */
  337. if (skb) {
  338. ret = wl1251_tx_frame(wl, skb);
  339. if (ret == -EBUSY) {
  340. /* firmware buffer is still full */
  341. wl1251_debug(DEBUG_TX, "cb: fw buffer "
  342. "still full");
  343. skb_queue_head(&wl->tx_queue, skb);
  344. return;
  345. } else if (ret < 0) {
  346. dev_kfree_skb(skb);
  347. return;
  348. }
  349. }
  350. wl1251_debug(DEBUG_TX, "cb: waking queues");
  351. ieee80211_wake_queues(wl->hw);
  352. wl->tx_queue_stopped = false;
  353. }
  354. }
  355. /* Called upon reception of a TX complete interrupt */
  356. void wl1251_tx_complete(struct wl1251 *wl)
  357. {
  358. int i, result_index, num_complete = 0;
  359. struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
  360. if (unlikely(wl->state != WL1251_STATE_ON))
  361. return;
  362. /* First we read the result */
  363. wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
  364. result, sizeof(result));
  365. result_index = wl->next_tx_complete;
  366. for (i = 0; i < ARRAY_SIZE(result); i++) {
  367. result_ptr = &result[result_index];
  368. if (result_ptr->done_1 == 1 &&
  369. result_ptr->done_2 == 1) {
  370. wl1251_tx_packet_cb(wl, result_ptr);
  371. result_ptr->done_1 = 0;
  372. result_ptr->done_2 = 0;
  373. result_index = (result_index + 1) &
  374. (FW_TX_CMPLT_BLOCK_SIZE - 1);
  375. num_complete++;
  376. } else {
  377. break;
  378. }
  379. }
  380. /* Every completed frame needs to be acknowledged */
  381. if (num_complete) {
  382. /*
  383. * If we've wrapped, we have to clear
  384. * the results in 2 steps.
  385. */
  386. if (result_index > wl->next_tx_complete) {
  387. /* Only 1 write is needed */
  388. wl1251_mem_write(wl,
  389. wl->data_path->tx_complete_addr +
  390. (wl->next_tx_complete *
  391. sizeof(struct tx_result)),
  392. &result[wl->next_tx_complete],
  393. num_complete *
  394. sizeof(struct tx_result));
  395. } else if (result_index < wl->next_tx_complete) {
  396. /* 2 writes are needed */
  397. wl1251_mem_write(wl,
  398. wl->data_path->tx_complete_addr +
  399. (wl->next_tx_complete *
  400. sizeof(struct tx_result)),
  401. &result[wl->next_tx_complete],
  402. (FW_TX_CMPLT_BLOCK_SIZE -
  403. wl->next_tx_complete) *
  404. sizeof(struct tx_result));
  405. wl1251_mem_write(wl,
  406. wl->data_path->tx_complete_addr,
  407. result,
  408. (num_complete -
  409. FW_TX_CMPLT_BLOCK_SIZE +
  410. wl->next_tx_complete) *
  411. sizeof(struct tx_result));
  412. } else {
  413. /* We have to write the whole array */
  414. wl1251_mem_write(wl,
  415. wl->data_path->tx_complete_addr,
  416. result,
  417. FW_TX_CMPLT_BLOCK_SIZE *
  418. sizeof(struct tx_result));
  419. }
  420. }
  421. wl->next_tx_complete = result_index;
  422. }
  423. /* caller must hold wl->mutex */
  424. void wl1251_tx_flush(struct wl1251 *wl)
  425. {
  426. int i;
  427. struct sk_buff *skb;
  428. struct ieee80211_tx_info *info;
  429. /* TX failure */
  430. /* control->flags = 0; FIXME */
  431. while ((skb = skb_dequeue(&wl->tx_queue))) {
  432. info = IEEE80211_SKB_CB(skb);
  433. wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  434. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  435. continue;
  436. ieee80211_tx_status(wl->hw, skb);
  437. }
  438. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  439. if (wl->tx_frames[i] != NULL) {
  440. skb = wl->tx_frames[i];
  441. info = IEEE80211_SKB_CB(skb);
  442. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  443. continue;
  444. ieee80211_tx_status(wl->hw, skb);
  445. wl->tx_frames[i] = NULL;
  446. }
  447. }