tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * This file is part of wl12xx
  3. *
  4. * Copyright (c) 1998-2007 Texas Instruments Incorporated
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * Contact: Kalle Valo <kalle.valo@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include "wl12xx.h"
  27. #include "reg.h"
  28. #include "spi.h"
  29. #include "tx.h"
  30. #include "ps.h"
  31. static bool wl12xx_tx_double_buffer_busy(struct wl12xx *wl, u32 data_out_count)
  32. {
  33. int used, data_in_count;
  34. data_in_count = wl->data_in_count;
  35. if (data_in_count < data_out_count)
  36. /* data_in_count has wrapped */
  37. data_in_count += TX_STATUS_DATA_OUT_COUNT_MASK + 1;
  38. used = data_in_count - data_out_count;
  39. WARN_ON(used < 0);
  40. WARN_ON(used > DP_TX_PACKET_RING_CHUNK_NUM);
  41. if (used >= DP_TX_PACKET_RING_CHUNK_NUM)
  42. return true;
  43. else
  44. return false;
  45. }
  46. static int wl12xx_tx_path_status(struct wl12xx *wl)
  47. {
  48. u32 status, addr, data_out_count;
  49. bool busy;
  50. addr = wl->data_path->tx_control_addr;
  51. status = wl12xx_mem_read32(wl, addr);
  52. data_out_count = status & TX_STATUS_DATA_OUT_COUNT_MASK;
  53. busy = wl12xx_tx_double_buffer_busy(wl, data_out_count);
  54. if (busy)
  55. return -EBUSY;
  56. return 0;
  57. }
  58. static int wl12xx_tx_id(struct wl12xx *wl, struct sk_buff *skb)
  59. {
  60. int i;
  61. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  62. if (wl->tx_frames[i] == NULL) {
  63. wl->tx_frames[i] = skb;
  64. return i;
  65. }
  66. return -EBUSY;
  67. }
  68. static void wl12xx_tx_control(struct tx_double_buffer_desc *tx_hdr,
  69. struct ieee80211_tx_info *control, u16 fc)
  70. {
  71. *(u16 *)&tx_hdr->control = 0;
  72. tx_hdr->control.rate_policy = 0;
  73. /* 802.11 packets */
  74. tx_hdr->control.packet_type = 0;
  75. if (control->flags & IEEE80211_TX_CTL_NO_ACK)
  76. tx_hdr->control.ack_policy = 1;
  77. tx_hdr->control.tx_complete = 1;
  78. if ((fc & IEEE80211_FTYPE_DATA) &&
  79. ((fc & IEEE80211_STYPE_QOS_DATA) ||
  80. (fc & IEEE80211_STYPE_QOS_NULLFUNC)))
  81. tx_hdr->control.qos = 1;
  82. }
  83. /* RSN + MIC = 8 + 8 = 16 bytes (worst case - AES). */
  84. #define MAX_MSDU_SECURITY_LENGTH 16
  85. #define MAX_MPDU_SECURITY_LENGTH 16
  86. #define WLAN_QOS_HDR_LEN 26
  87. #define MAX_MPDU_HEADER_AND_SECURITY (MAX_MPDU_SECURITY_LENGTH + \
  88. WLAN_QOS_HDR_LEN)
  89. #define HW_BLOCK_SIZE 252
  90. static void wl12xx_tx_frag_block_num(struct tx_double_buffer_desc *tx_hdr)
  91. {
  92. u16 payload_len, frag_threshold, mem_blocks;
  93. u16 num_mpdus, mem_blocks_per_frag;
  94. frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  95. tx_hdr->frag_threshold = cpu_to_le16(frag_threshold);
  96. payload_len = tx_hdr->length + MAX_MSDU_SECURITY_LENGTH;
  97. if (payload_len > frag_threshold) {
  98. mem_blocks_per_frag =
  99. ((frag_threshold + MAX_MPDU_HEADER_AND_SECURITY) /
  100. HW_BLOCK_SIZE) + 1;
  101. num_mpdus = payload_len / frag_threshold;
  102. mem_blocks = num_mpdus * mem_blocks_per_frag;
  103. payload_len -= num_mpdus * frag_threshold;
  104. num_mpdus++;
  105. } else {
  106. mem_blocks_per_frag = 0;
  107. mem_blocks = 0;
  108. num_mpdus = 1;
  109. }
  110. mem_blocks += (payload_len / HW_BLOCK_SIZE) + 1;
  111. if (num_mpdus > 1)
  112. mem_blocks += min(num_mpdus, mem_blocks_per_frag);
  113. tx_hdr->num_mem_blocks = mem_blocks;
  114. }
  115. static int wl12xx_tx_fill_hdr(struct wl12xx *wl, struct sk_buff *skb,
  116. struct ieee80211_tx_info *control)
  117. {
  118. struct tx_double_buffer_desc *tx_hdr;
  119. struct ieee80211_rate *rate;
  120. int id;
  121. u16 fc;
  122. if (!skb)
  123. return -EINVAL;
  124. id = wl12xx_tx_id(wl, skb);
  125. if (id < 0)
  126. return id;
  127. fc = *(u16 *)skb->data;
  128. tx_hdr = (struct tx_double_buffer_desc *) skb_push(skb,
  129. sizeof(*tx_hdr));
  130. tx_hdr->length = cpu_to_le16(skb->len - sizeof(*tx_hdr));
  131. rate = ieee80211_get_tx_rate(wl->hw, control);
  132. tx_hdr->rate = cpu_to_le16(rate->hw_value);
  133. tx_hdr->expiry_time = cpu_to_le32(1 << 16);
  134. tx_hdr->id = id;
  135. /* FIXME: how to get the correct queue id? */
  136. tx_hdr->xmit_queue = 0;
  137. wl12xx_tx_control(tx_hdr, control, fc);
  138. wl12xx_tx_frag_block_num(tx_hdr);
  139. return 0;
  140. }
  141. /* We copy the packet to the target */
  142. static int wl12xx_tx_send_packet(struct wl12xx *wl, struct sk_buff *skb,
  143. struct ieee80211_tx_info *control)
  144. {
  145. struct tx_double_buffer_desc *tx_hdr;
  146. int len;
  147. u32 addr;
  148. if (!skb)
  149. return -EINVAL;
  150. tx_hdr = (struct tx_double_buffer_desc *) skb->data;
  151. if (control->control.hw_key &&
  152. control->control.hw_key->alg == ALG_TKIP) {
  153. int hdrlen;
  154. u16 fc;
  155. u8 *pos;
  156. fc = *(u16 *)(skb->data + sizeof(*tx_hdr));
  157. tx_hdr->length += WL12XX_TKIP_IV_SPACE;
  158. hdrlen = ieee80211_hdrlen(fc);
  159. pos = skb_push(skb, WL12XX_TKIP_IV_SPACE);
  160. memmove(pos, pos + WL12XX_TKIP_IV_SPACE,
  161. sizeof(*tx_hdr) + hdrlen);
  162. }
  163. /* Revisit. This is a workaround for getting non-aligned packets.
  164. This happens at least with EAPOL packets from the user space.
  165. Our DMA requires packets to be aligned on a 4-byte boundary.
  166. */
  167. if (unlikely((long)skb->data & 0x03)) {
  168. int offset = (4 - (long)skb->data) & 0x03;
  169. wl12xx_debug(DEBUG_TX, "skb offset %d", offset);
  170. /* check whether the current skb can be used */
  171. if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
  172. unsigned char *src = skb->data;
  173. /* align the buffer on a 4-byte boundary */
  174. skb_reserve(skb, offset);
  175. memmove(skb->data, src, skb->len);
  176. } else {
  177. wl12xx_info("No handler, fixme!");
  178. return -EINVAL;
  179. }
  180. }
  181. /* Our skb->data at this point includes the HW header */
  182. len = WL12XX_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. wl12xx_spi_mem_write(wl, addr, skb->data, len);
  189. wl12xx_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x",
  190. tx_hdr->id, skb, tx_hdr->length, tx_hdr->rate);
  191. return 0;
  192. }
  193. static void wl12xx_tx_trigger(struct wl12xx *wl)
  194. {
  195. u32 data, addr;
  196. if (wl->data_in_count & 0x1) {
  197. addr = ACX_REG_INTERRUPT_TRIG_H;
  198. data = INTR_TRIG_TX_PROC1;
  199. } else {
  200. addr = ACX_REG_INTERRUPT_TRIG;
  201. data = INTR_TRIG_TX_PROC0;
  202. }
  203. wl12xx_reg_write32(wl, addr, data);
  204. /* Bumping data in */
  205. wl->data_in_count = (wl->data_in_count + 1) &
  206. TX_STATUS_DATA_OUT_COUNT_MASK;
  207. }
  208. /* caller must hold wl->mutex */
  209. static int wl12xx_tx_frame(struct wl12xx *wl, struct sk_buff *skb)
  210. {
  211. struct ieee80211_tx_info *info;
  212. int ret = 0;
  213. u8 idx;
  214. info = IEEE80211_SKB_CB(skb);
  215. if (info->control.hw_key) {
  216. idx = info->control.hw_key->hw_key_idx;
  217. if (unlikely(wl->default_key != idx)) {
  218. ret = wl12xx_acx_default_key(wl, idx);
  219. if (ret < 0)
  220. return ret;
  221. }
  222. }
  223. ret = wl12xx_tx_path_status(wl);
  224. if (ret < 0)
  225. return ret;
  226. ret = wl12xx_tx_fill_hdr(wl, skb, info);
  227. if (ret < 0)
  228. return ret;
  229. ret = wl12xx_tx_send_packet(wl, skb, info);
  230. if (ret < 0)
  231. return ret;
  232. wl12xx_tx_trigger(wl);
  233. return ret;
  234. }
  235. void wl12xx_tx_work(struct work_struct *work)
  236. {
  237. struct wl12xx *wl = container_of(work, struct wl12xx, tx_work);
  238. struct sk_buff *skb;
  239. bool woken_up = false;
  240. int ret;
  241. mutex_lock(&wl->mutex);
  242. if (unlikely(wl->state == WL12XX_STATE_OFF))
  243. goto out;
  244. while ((skb = skb_dequeue(&wl->tx_queue))) {
  245. if (!woken_up) {
  246. wl12xx_ps_elp_wakeup(wl);
  247. woken_up = true;
  248. }
  249. ret = wl12xx_tx_frame(wl, skb);
  250. if (ret == -EBUSY) {
  251. /* firmware buffer is full, stop queues */
  252. wl12xx_debug(DEBUG_TX, "tx_work: fw buffer full, "
  253. "stop queues");
  254. ieee80211_stop_queues(wl->hw);
  255. wl->tx_queue_stopped = true;
  256. skb_queue_head(&wl->tx_queue, skb);
  257. goto out;
  258. } else if (ret < 0) {
  259. dev_kfree_skb(skb);
  260. goto out;
  261. }
  262. }
  263. out:
  264. if (woken_up)
  265. wl12xx_ps_elp_sleep(wl);
  266. mutex_unlock(&wl->mutex);
  267. }
  268. static const char *wl12xx_tx_parse_status(u8 status)
  269. {
  270. /* 8 bit status field, one character per bit plus null */
  271. static char buf[9];
  272. int i = 0;
  273. memset(buf, 0, sizeof(buf));
  274. if (status & TX_DMA_ERROR)
  275. buf[i++] = 'm';
  276. if (status & TX_DISABLED)
  277. buf[i++] = 'd';
  278. if (status & TX_RETRY_EXCEEDED)
  279. buf[i++] = 'r';
  280. if (status & TX_TIMEOUT)
  281. buf[i++] = 't';
  282. if (status & TX_KEY_NOT_FOUND)
  283. buf[i++] = 'k';
  284. if (status & TX_ENCRYPT_FAIL)
  285. buf[i++] = 'e';
  286. if (status & TX_UNAVAILABLE_PRIORITY)
  287. buf[i++] = 'p';
  288. /* bit 0 is unused apparently */
  289. return buf;
  290. }
  291. static void wl12xx_tx_packet_cb(struct wl12xx *wl,
  292. struct tx_result *result)
  293. {
  294. struct ieee80211_tx_info *info;
  295. struct sk_buff *skb;
  296. int hdrlen, ret;
  297. u8 *frame;
  298. skb = wl->tx_frames[result->id];
  299. if (skb == NULL) {
  300. wl12xx_error("SKB for packet %d is NULL", result->id);
  301. return;
  302. }
  303. info = IEEE80211_SKB_CB(skb);
  304. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  305. (result->status == TX_SUCCESS))
  306. info->flags |= IEEE80211_TX_STAT_ACK;
  307. info->status.rates[0].count = result->ack_failures + 1;
  308. wl->stats.retry_count += result->ack_failures;
  309. /*
  310. * We have to remove our private TX header before pushing
  311. * the skb back to mac80211.
  312. */
  313. frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
  314. if (info->control.hw_key &&
  315. info->control.hw_key->alg == ALG_TKIP) {
  316. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  317. memmove(frame + WL12XX_TKIP_IV_SPACE, frame, hdrlen);
  318. skb_pull(skb, WL12XX_TKIP_IV_SPACE);
  319. }
  320. wl12xx_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  321. " status 0x%x (%s)",
  322. result->id, skb, result->ack_failures, result->rate,
  323. result->status, wl12xx_tx_parse_status(result->status));
  324. ieee80211_tx_status(wl->hw, skb);
  325. wl->tx_frames[result->id] = NULL;
  326. if (wl->tx_queue_stopped) {
  327. wl12xx_debug(DEBUG_TX, "cb: queue was stopped");
  328. skb = skb_dequeue(&wl->tx_queue);
  329. /* The skb can be NULL because tx_work might have been
  330. scheduled before the queue was stopped making the
  331. queue empty */
  332. if (skb) {
  333. ret = wl12xx_tx_frame(wl, skb);
  334. if (ret == -EBUSY) {
  335. /* firmware buffer is still full */
  336. wl12xx_debug(DEBUG_TX, "cb: fw buffer "
  337. "still full");
  338. skb_queue_head(&wl->tx_queue, skb);
  339. return;
  340. } else if (ret < 0) {
  341. dev_kfree_skb(skb);
  342. return;
  343. }
  344. }
  345. wl12xx_debug(DEBUG_TX, "cb: waking queues");
  346. ieee80211_wake_queues(wl->hw);
  347. wl->tx_queue_stopped = false;
  348. }
  349. }
  350. /* Called upon reception of a TX complete interrupt */
  351. void wl12xx_tx_complete(struct wl12xx *wl)
  352. {
  353. int i, result_index, num_complete = 0;
  354. struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
  355. if (unlikely(wl->state != WL12XX_STATE_ON))
  356. return;
  357. /* First we read the result */
  358. wl12xx_spi_mem_read(wl, wl->data_path->tx_complete_addr,
  359. result, sizeof(result));
  360. result_index = wl->next_tx_complete;
  361. for (i = 0; i < ARRAY_SIZE(result); i++) {
  362. result_ptr = &result[result_index];
  363. if (result_ptr->done_1 == 1 &&
  364. result_ptr->done_2 == 1) {
  365. wl12xx_tx_packet_cb(wl, result_ptr);
  366. result_ptr->done_1 = 0;
  367. result_ptr->done_2 = 0;
  368. result_index = (result_index + 1) &
  369. (FW_TX_CMPLT_BLOCK_SIZE - 1);
  370. num_complete++;
  371. } else {
  372. break;
  373. }
  374. }
  375. /* Every completed frame needs to be acknowledged */
  376. if (num_complete) {
  377. /*
  378. * If we've wrapped, we have to clear
  379. * the results in 2 steps.
  380. */
  381. if (result_index > wl->next_tx_complete) {
  382. /* Only 1 write is needed */
  383. wl12xx_spi_mem_write(wl,
  384. wl->data_path->tx_complete_addr +
  385. (wl->next_tx_complete *
  386. sizeof(struct tx_result)),
  387. &result[wl->next_tx_complete],
  388. num_complete *
  389. sizeof(struct tx_result));
  390. } else if (result_index < wl->next_tx_complete) {
  391. /* 2 writes are needed */
  392. wl12xx_spi_mem_write(wl,
  393. wl->data_path->tx_complete_addr +
  394. (wl->next_tx_complete *
  395. sizeof(struct tx_result)),
  396. &result[wl->next_tx_complete],
  397. (FW_TX_CMPLT_BLOCK_SIZE -
  398. wl->next_tx_complete) *
  399. sizeof(struct tx_result));
  400. wl12xx_spi_mem_write(wl,
  401. wl->data_path->tx_complete_addr,
  402. result,
  403. (num_complete -
  404. FW_TX_CMPLT_BLOCK_SIZE +
  405. wl->next_tx_complete) *
  406. sizeof(struct tx_result));
  407. } else {
  408. /* We have to write the whole array */
  409. wl12xx_spi_mem_write(wl,
  410. wl->data_path->tx_complete_addr,
  411. result,
  412. FW_TX_CMPLT_BLOCK_SIZE *
  413. sizeof(struct tx_result));
  414. }
  415. }
  416. wl->next_tx_complete = result_index;
  417. }
  418. /* caller must hold wl->mutex */
  419. void wl12xx_tx_flush(struct wl12xx *wl)
  420. {
  421. int i;
  422. struct sk_buff *skb;
  423. struct ieee80211_tx_info *info;
  424. /* TX failure */
  425. /* control->flags = 0; FIXME */
  426. while ((skb = skb_dequeue(&wl->tx_queue))) {
  427. info = IEEE80211_SKB_CB(skb);
  428. wl12xx_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  429. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  430. continue;
  431. ieee80211_tx_status(wl->hw, skb);
  432. }
  433. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  434. if (wl->tx_frames[i] != NULL) {
  435. skb = wl->tx_frames[i];
  436. info = IEEE80211_SKB_CB(skb);
  437. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  438. continue;
  439. ieee80211_tx_status(wl->hw, skb);
  440. wl->tx_frames[i] = NULL;
  441. }
  442. }