tx.c 14 KB

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