tx.c 14 KB

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