tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 "reg.h"
  26. #include "tx.h"
  27. #include "ps.h"
  28. #include "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. struct sk_buff *newskb = skb_copy_expand(skb, 0, 3,
  172. GFP_KERNEL);
  173. if (unlikely(newskb == NULL)) {
  174. wl1251_error("Can't allocate skb!");
  175. return -EINVAL;
  176. }
  177. tx_hdr = (struct tx_double_buffer_desc *) newskb->data;
  178. dev_kfree_skb_any(skb);
  179. wl->tx_frames[tx_hdr->id] = skb = newskb;
  180. offset = (4 - (long)skb->data) & 0x03;
  181. wl1251_debug(DEBUG_TX, "new skb offset %d", offset);
  182. }
  183. /* align the buffer on a 4-byte boundary */
  184. if (offset) {
  185. unsigned char *src = skb->data;
  186. skb_reserve(skb, offset);
  187. memmove(skb->data, src, skb->len);
  188. tx_hdr = (struct tx_double_buffer_desc *) skb->data;
  189. }
  190. }
  191. /* Our skb->data at this point includes the HW header */
  192. len = WL1251_TX_ALIGN(skb->len);
  193. if (wl->data_in_count & 0x1)
  194. addr = wl->data_path->tx_packet_ring_addr +
  195. wl->data_path->tx_packet_ring_chunk_size;
  196. else
  197. addr = wl->data_path->tx_packet_ring_addr;
  198. wl1251_mem_write(wl, addr, skb->data, len);
  199. wl1251_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x "
  200. "queue %d", tx_hdr->id, skb, tx_hdr->length,
  201. tx_hdr->rate, tx_hdr->xmit_queue);
  202. return 0;
  203. }
  204. static void wl1251_tx_trigger(struct wl1251 *wl)
  205. {
  206. u32 data, addr;
  207. if (wl->data_in_count & 0x1) {
  208. addr = ACX_REG_INTERRUPT_TRIG_H;
  209. data = INTR_TRIG_TX_PROC1;
  210. } else {
  211. addr = ACX_REG_INTERRUPT_TRIG;
  212. data = INTR_TRIG_TX_PROC0;
  213. }
  214. wl1251_reg_write32(wl, addr, data);
  215. /* Bumping data in */
  216. wl->data_in_count = (wl->data_in_count + 1) &
  217. TX_STATUS_DATA_OUT_COUNT_MASK;
  218. }
  219. /* caller must hold wl->mutex */
  220. static int wl1251_tx_frame(struct wl1251 *wl, struct sk_buff *skb)
  221. {
  222. struct ieee80211_tx_info *info;
  223. int ret = 0;
  224. u8 idx;
  225. info = IEEE80211_SKB_CB(skb);
  226. if (info->control.hw_key) {
  227. idx = info->control.hw_key->hw_key_idx;
  228. if (unlikely(wl->default_key != idx)) {
  229. ret = wl1251_acx_default_key(wl, idx);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. }
  234. ret = wl1251_tx_path_status(wl);
  235. if (ret < 0)
  236. return ret;
  237. ret = wl1251_tx_fill_hdr(wl, skb, info);
  238. if (ret < 0)
  239. return ret;
  240. ret = wl1251_tx_send_packet(wl, skb, info);
  241. if (ret < 0)
  242. return ret;
  243. wl1251_tx_trigger(wl);
  244. return ret;
  245. }
  246. void wl1251_tx_work(struct work_struct *work)
  247. {
  248. struct wl1251 *wl = container_of(work, struct wl1251, tx_work);
  249. struct sk_buff *skb;
  250. bool woken_up = false;
  251. int ret;
  252. mutex_lock(&wl->mutex);
  253. if (unlikely(wl->state == WL1251_STATE_OFF))
  254. goto out;
  255. while ((skb = skb_dequeue(&wl->tx_queue))) {
  256. if (!woken_up) {
  257. ret = wl1251_ps_elp_wakeup(wl);
  258. if (ret < 0)
  259. goto out;
  260. woken_up = true;
  261. }
  262. ret = wl1251_tx_frame(wl, skb);
  263. if (ret == -EBUSY) {
  264. skb_queue_head(&wl->tx_queue, skb);
  265. goto out;
  266. } else if (ret < 0) {
  267. dev_kfree_skb(skb);
  268. goto out;
  269. }
  270. }
  271. out:
  272. if (woken_up)
  273. wl1251_ps_elp_sleep(wl);
  274. mutex_unlock(&wl->mutex);
  275. }
  276. static const char *wl1251_tx_parse_status(u8 status)
  277. {
  278. /* 8 bit status field, one character per bit plus null */
  279. static char buf[9];
  280. int i = 0;
  281. memset(buf, 0, sizeof(buf));
  282. if (status & TX_DMA_ERROR)
  283. buf[i++] = 'm';
  284. if (status & TX_DISABLED)
  285. buf[i++] = 'd';
  286. if (status & TX_RETRY_EXCEEDED)
  287. buf[i++] = 'r';
  288. if (status & TX_TIMEOUT)
  289. buf[i++] = 't';
  290. if (status & TX_KEY_NOT_FOUND)
  291. buf[i++] = 'k';
  292. if (status & TX_ENCRYPT_FAIL)
  293. buf[i++] = 'e';
  294. if (status & TX_UNAVAILABLE_PRIORITY)
  295. buf[i++] = 'p';
  296. /* bit 0 is unused apparently */
  297. return buf;
  298. }
  299. static void wl1251_tx_packet_cb(struct wl1251 *wl,
  300. struct tx_result *result)
  301. {
  302. struct ieee80211_tx_info *info;
  303. struct sk_buff *skb;
  304. int hdrlen;
  305. u8 *frame;
  306. skb = wl->tx_frames[result->id];
  307. if (skb == NULL) {
  308. wl1251_error("SKB for packet %d is NULL", result->id);
  309. return;
  310. }
  311. info = IEEE80211_SKB_CB(skb);
  312. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  313. (result->status == TX_SUCCESS))
  314. info->flags |= IEEE80211_TX_STAT_ACK;
  315. info->status.rates[0].count = result->ack_failures + 1;
  316. wl->stats.retry_count += result->ack_failures;
  317. /*
  318. * We have to remove our private TX header before pushing
  319. * the skb back to mac80211.
  320. */
  321. frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
  322. if (info->control.hw_key &&
  323. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  324. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  325. memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
  326. skb_pull(skb, WL1251_TKIP_IV_SPACE);
  327. }
  328. wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  329. " status 0x%x (%s)",
  330. result->id, skb, result->ack_failures, result->rate,
  331. result->status, wl1251_tx_parse_status(result->status));
  332. ieee80211_tx_status(wl->hw, skb);
  333. wl->tx_frames[result->id] = NULL;
  334. }
  335. /* Called upon reception of a TX complete interrupt */
  336. void wl1251_tx_complete(struct wl1251 *wl)
  337. {
  338. int i, result_index, num_complete = 0, queue_len;
  339. struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
  340. unsigned long flags;
  341. if (unlikely(wl->state != WL1251_STATE_ON))
  342. return;
  343. /* First we read the result */
  344. wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
  345. result, sizeof(result));
  346. result_index = wl->next_tx_complete;
  347. for (i = 0; i < ARRAY_SIZE(result); i++) {
  348. result_ptr = &result[result_index];
  349. if (result_ptr->done_1 == 1 &&
  350. result_ptr->done_2 == 1) {
  351. wl1251_tx_packet_cb(wl, result_ptr);
  352. result_ptr->done_1 = 0;
  353. result_ptr->done_2 = 0;
  354. result_index = (result_index + 1) &
  355. (FW_TX_CMPLT_BLOCK_SIZE - 1);
  356. num_complete++;
  357. } else {
  358. break;
  359. }
  360. }
  361. queue_len = skb_queue_len(&wl->tx_queue);
  362. if ((num_complete > 0) && (queue_len > 0)) {
  363. /* firmware buffer has space, reschedule tx_work */
  364. wl1251_debug(DEBUG_TX, "tx_complete: reschedule tx_work");
  365. ieee80211_queue_work(wl->hw, &wl->tx_work);
  366. }
  367. if (wl->tx_queue_stopped &&
  368. queue_len <= WL1251_TX_QUEUE_LOW_WATERMARK) {
  369. /* tx_queue has space, restart queues */
  370. wl1251_debug(DEBUG_TX, "tx_complete: waking queues");
  371. spin_lock_irqsave(&wl->wl_lock, flags);
  372. ieee80211_wake_queues(wl->hw);
  373. wl->tx_queue_stopped = false;
  374. spin_unlock_irqrestore(&wl->wl_lock, flags);
  375. }
  376. /* Every completed frame needs to be acknowledged */
  377. if (num_complete) {
  378. /*
  379. * If we've wrapped, we have to clear
  380. * the results in 2 steps.
  381. */
  382. if (result_index > wl->next_tx_complete) {
  383. /* Only 1 write is needed */
  384. wl1251_mem_write(wl,
  385. wl->data_path->tx_complete_addr +
  386. (wl->next_tx_complete *
  387. sizeof(struct tx_result)),
  388. &result[wl->next_tx_complete],
  389. num_complete *
  390. sizeof(struct tx_result));
  391. } else if (result_index < wl->next_tx_complete) {
  392. /* 2 writes are 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. (FW_TX_CMPLT_BLOCK_SIZE -
  399. wl->next_tx_complete) *
  400. sizeof(struct tx_result));
  401. wl1251_mem_write(wl,
  402. wl->data_path->tx_complete_addr,
  403. result,
  404. (num_complete -
  405. FW_TX_CMPLT_BLOCK_SIZE +
  406. wl->next_tx_complete) *
  407. sizeof(struct tx_result));
  408. } else {
  409. /* We have to write the whole array */
  410. wl1251_mem_write(wl,
  411. wl->data_path->tx_complete_addr,
  412. result,
  413. FW_TX_CMPLT_BLOCK_SIZE *
  414. sizeof(struct tx_result));
  415. }
  416. }
  417. wl->next_tx_complete = result_index;
  418. }
  419. /* caller must hold wl->mutex */
  420. void wl1251_tx_flush(struct wl1251 *wl)
  421. {
  422. int i;
  423. struct sk_buff *skb;
  424. struct ieee80211_tx_info *info;
  425. /* TX failure */
  426. /* control->flags = 0; FIXME */
  427. while ((skb = skb_dequeue(&wl->tx_queue))) {
  428. info = IEEE80211_SKB_CB(skb);
  429. wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  430. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  431. continue;
  432. ieee80211_tx_status(wl->hw, skb);
  433. }
  434. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  435. if (wl->tx_frames[i] != NULL) {
  436. skb = wl->tx_frames[i];
  437. info = IEEE80211_SKB_CB(skb);
  438. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  439. continue;
  440. ieee80211_tx_status(wl->hw, skb);
  441. wl->tx_frames[i] = NULL;
  442. }
  443. }