wl1251_tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. /* firmware buffer is full, stop queues */
  255. wl1251_debug(DEBUG_TX, "tx_work: fw buffer full, "
  256. "stop queues");
  257. ieee80211_stop_queues(wl->hw);
  258. wl->tx_queue_stopped = true;
  259. skb_queue_head(&wl->tx_queue, skb);
  260. goto out;
  261. } else if (ret < 0) {
  262. dev_kfree_skb(skb);
  263. goto out;
  264. }
  265. }
  266. out:
  267. if (woken_up)
  268. wl1251_ps_elp_sleep(wl);
  269. mutex_unlock(&wl->mutex);
  270. }
  271. static const char *wl1251_tx_parse_status(u8 status)
  272. {
  273. /* 8 bit status field, one character per bit plus null */
  274. static char buf[9];
  275. int i = 0;
  276. memset(buf, 0, sizeof(buf));
  277. if (status & TX_DMA_ERROR)
  278. buf[i++] = 'm';
  279. if (status & TX_DISABLED)
  280. buf[i++] = 'd';
  281. if (status & TX_RETRY_EXCEEDED)
  282. buf[i++] = 'r';
  283. if (status & TX_TIMEOUT)
  284. buf[i++] = 't';
  285. if (status & TX_KEY_NOT_FOUND)
  286. buf[i++] = 'k';
  287. if (status & TX_ENCRYPT_FAIL)
  288. buf[i++] = 'e';
  289. if (status & TX_UNAVAILABLE_PRIORITY)
  290. buf[i++] = 'p';
  291. /* bit 0 is unused apparently */
  292. return buf;
  293. }
  294. static void wl1251_tx_packet_cb(struct wl1251 *wl,
  295. struct tx_result *result)
  296. {
  297. struct ieee80211_tx_info *info;
  298. struct sk_buff *skb;
  299. int hdrlen, ret;
  300. u8 *frame;
  301. skb = wl->tx_frames[result->id];
  302. if (skb == NULL) {
  303. wl1251_error("SKB for packet %d is NULL", result->id);
  304. return;
  305. }
  306. info = IEEE80211_SKB_CB(skb);
  307. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  308. (result->status == TX_SUCCESS))
  309. info->flags |= IEEE80211_TX_STAT_ACK;
  310. info->status.rates[0].count = result->ack_failures + 1;
  311. wl->stats.retry_count += result->ack_failures;
  312. /*
  313. * We have to remove our private TX header before pushing
  314. * the skb back to mac80211.
  315. */
  316. frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
  317. if (info->control.hw_key &&
  318. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  319. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  320. memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
  321. skb_pull(skb, WL1251_TKIP_IV_SPACE);
  322. }
  323. wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  324. " status 0x%x (%s)",
  325. result->id, skb, result->ack_failures, result->rate,
  326. result->status, wl1251_tx_parse_status(result->status));
  327. ieee80211_tx_status(wl->hw, skb);
  328. wl->tx_frames[result->id] = NULL;
  329. if (wl->tx_queue_stopped) {
  330. wl1251_debug(DEBUG_TX, "cb: queue was stopped");
  331. skb = skb_dequeue(&wl->tx_queue);
  332. /* The skb can be NULL because tx_work might have been
  333. scheduled before the queue was stopped making the
  334. queue empty */
  335. if (skb) {
  336. ret = wl1251_tx_frame(wl, skb);
  337. if (ret == -EBUSY) {
  338. /* firmware buffer is still full */
  339. wl1251_debug(DEBUG_TX, "cb: fw buffer "
  340. "still full");
  341. skb_queue_head(&wl->tx_queue, skb);
  342. return;
  343. } else if (ret < 0) {
  344. dev_kfree_skb(skb);
  345. return;
  346. }
  347. }
  348. wl1251_debug(DEBUG_TX, "cb: waking queues");
  349. ieee80211_wake_queues(wl->hw);
  350. wl->tx_queue_stopped = false;
  351. }
  352. }
  353. /* Called upon reception of a TX complete interrupt */
  354. void wl1251_tx_complete(struct wl1251 *wl)
  355. {
  356. int i, result_index, num_complete = 0;
  357. struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
  358. if (unlikely(wl->state != WL1251_STATE_ON))
  359. return;
  360. /* First we read the result */
  361. wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
  362. result, sizeof(result));
  363. result_index = wl->next_tx_complete;
  364. for (i = 0; i < ARRAY_SIZE(result); i++) {
  365. result_ptr = &result[result_index];
  366. if (result_ptr->done_1 == 1 &&
  367. result_ptr->done_2 == 1) {
  368. wl1251_tx_packet_cb(wl, result_ptr);
  369. result_ptr->done_1 = 0;
  370. result_ptr->done_2 = 0;
  371. result_index = (result_index + 1) &
  372. (FW_TX_CMPLT_BLOCK_SIZE - 1);
  373. num_complete++;
  374. } else {
  375. break;
  376. }
  377. }
  378. /* Every completed frame needs to be acknowledged */
  379. if (num_complete) {
  380. /*
  381. * If we've wrapped, we have to clear
  382. * the results in 2 steps.
  383. */
  384. if (result_index > wl->next_tx_complete) {
  385. /* Only 1 write is needed */
  386. wl1251_mem_write(wl,
  387. wl->data_path->tx_complete_addr +
  388. (wl->next_tx_complete *
  389. sizeof(struct tx_result)),
  390. &result[wl->next_tx_complete],
  391. num_complete *
  392. sizeof(struct tx_result));
  393. } else if (result_index < wl->next_tx_complete) {
  394. /* 2 writes are needed */
  395. wl1251_mem_write(wl,
  396. wl->data_path->tx_complete_addr +
  397. (wl->next_tx_complete *
  398. sizeof(struct tx_result)),
  399. &result[wl->next_tx_complete],
  400. (FW_TX_CMPLT_BLOCK_SIZE -
  401. wl->next_tx_complete) *
  402. sizeof(struct tx_result));
  403. wl1251_mem_write(wl,
  404. wl->data_path->tx_complete_addr,
  405. result,
  406. (num_complete -
  407. FW_TX_CMPLT_BLOCK_SIZE +
  408. wl->next_tx_complete) *
  409. sizeof(struct tx_result));
  410. } else {
  411. /* We have to write the whole array */
  412. wl1251_mem_write(wl,
  413. wl->data_path->tx_complete_addr,
  414. result,
  415. FW_TX_CMPLT_BLOCK_SIZE *
  416. sizeof(struct tx_result));
  417. }
  418. }
  419. wl->next_tx_complete = result_index;
  420. }
  421. /* caller must hold wl->mutex */
  422. void wl1251_tx_flush(struct wl1251 *wl)
  423. {
  424. int i;
  425. struct sk_buff *skb;
  426. struct ieee80211_tx_info *info;
  427. /* TX failure */
  428. /* control->flags = 0; FIXME */
  429. while ((skb = skb_dequeue(&wl->tx_queue))) {
  430. info = IEEE80211_SKB_CB(skb);
  431. wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  432. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  433. continue;
  434. ieee80211_tx_status(wl->hw, skb);
  435. }
  436. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  437. if (wl->tx_frames[i] != NULL) {
  438. skb = wl->tx_frames[i];
  439. info = IEEE80211_SKB_CB(skb);
  440. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  441. continue;
  442. ieee80211_tx_status(wl->hw, skb);
  443. wl->tx_frames[i] = NULL;
  444. }
  445. }