tx.c 14 KB

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