pio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. Broadcom B43 wireless driver
  3. PIO data transfer
  4. Copyright (c) 2005-2008 Michael Buesch <mb@bu3sch.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #include "b43.h"
  19. #include "pio.h"
  20. #include "dma.h"
  21. #include "main.h"
  22. #include "xmit.h"
  23. #include <linux/delay.h>
  24. static u16 generate_cookie(struct b43_pio_txqueue *q,
  25. struct b43_pio_txpacket *pack)
  26. {
  27. u16 cookie;
  28. /* Use the upper 4 bits of the cookie as
  29. * PIO controller ID and store the packet index number
  30. * in the lower 12 bits.
  31. * Note that the cookie must never be 0, as this
  32. * is a special value used in RX path.
  33. * It can also not be 0xFFFF because that is special
  34. * for multicast frames.
  35. */
  36. cookie = (((u16)q->index + 1) << 12);
  37. cookie |= pack->index;
  38. return cookie;
  39. }
  40. static
  41. struct b43_pio_txqueue *parse_cookie(struct b43_wldev *dev,
  42. u16 cookie,
  43. struct b43_pio_txpacket **pack)
  44. {
  45. struct b43_pio *pio = &dev->pio;
  46. struct b43_pio_txqueue *q = NULL;
  47. unsigned int pack_index;
  48. switch (cookie & 0xF000) {
  49. case 0x1000:
  50. q = pio->tx_queue_AC_BK;
  51. break;
  52. case 0x2000:
  53. q = pio->tx_queue_AC_BE;
  54. break;
  55. case 0x3000:
  56. q = pio->tx_queue_AC_VI;
  57. break;
  58. case 0x4000:
  59. q = pio->tx_queue_AC_VO;
  60. break;
  61. case 0x5000:
  62. q = pio->tx_queue_mcast;
  63. break;
  64. }
  65. if (B43_WARN_ON(!q))
  66. return NULL;
  67. pack_index = (cookie & 0x0FFF);
  68. if (B43_WARN_ON(pack_index >= ARRAY_SIZE(q->packets)))
  69. return NULL;
  70. *pack = &q->packets[pack_index];
  71. return q;
  72. }
  73. static u16 index_to_pioqueue_base(struct b43_wldev *dev,
  74. unsigned int index)
  75. {
  76. static const u16 bases[] = {
  77. B43_MMIO_PIO_BASE0,
  78. B43_MMIO_PIO_BASE1,
  79. B43_MMIO_PIO_BASE2,
  80. B43_MMIO_PIO_BASE3,
  81. B43_MMIO_PIO_BASE4,
  82. B43_MMIO_PIO_BASE5,
  83. B43_MMIO_PIO_BASE6,
  84. B43_MMIO_PIO_BASE7,
  85. };
  86. static const u16 bases_rev11[] = {
  87. B43_MMIO_PIO11_BASE0,
  88. B43_MMIO_PIO11_BASE1,
  89. B43_MMIO_PIO11_BASE2,
  90. B43_MMIO_PIO11_BASE3,
  91. B43_MMIO_PIO11_BASE4,
  92. B43_MMIO_PIO11_BASE5,
  93. };
  94. if (dev->dev->id.revision >= 11) {
  95. B43_WARN_ON(index >= ARRAY_SIZE(bases_rev11));
  96. return bases_rev11[index];
  97. }
  98. B43_WARN_ON(index >= ARRAY_SIZE(bases));
  99. return bases[index];
  100. }
  101. static u16 pio_txqueue_offset(struct b43_wldev *dev)
  102. {
  103. if (dev->dev->id.revision >= 11)
  104. return 0x18;
  105. return 0;
  106. }
  107. static u16 pio_rxqueue_offset(struct b43_wldev *dev)
  108. {
  109. if (dev->dev->id.revision >= 11)
  110. return 0x38;
  111. return 8;
  112. }
  113. static struct b43_pio_txqueue *b43_setup_pioqueue_tx(struct b43_wldev *dev,
  114. unsigned int index)
  115. {
  116. struct b43_pio_txqueue *q;
  117. struct b43_pio_txpacket *p;
  118. unsigned int i;
  119. q = kzalloc(sizeof(*q), GFP_KERNEL);
  120. if (!q)
  121. return NULL;
  122. q->dev = dev;
  123. q->rev = dev->dev->id.revision;
  124. q->mmio_base = index_to_pioqueue_base(dev, index) +
  125. pio_txqueue_offset(dev);
  126. q->index = index;
  127. q->free_packet_slots = B43_PIO_MAX_NR_TXPACKETS;
  128. if (q->rev >= 8) {
  129. q->buffer_size = 1920; //FIXME this constant is wrong.
  130. } else {
  131. q->buffer_size = b43_piotx_read16(q, B43_PIO_TXQBUFSIZE);
  132. q->buffer_size -= 80;
  133. }
  134. INIT_LIST_HEAD(&q->packets_list);
  135. for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
  136. p = &(q->packets[i]);
  137. INIT_LIST_HEAD(&p->list);
  138. p->index = i;
  139. p->queue = q;
  140. list_add(&p->list, &q->packets_list);
  141. }
  142. return q;
  143. }
  144. static struct b43_pio_rxqueue *b43_setup_pioqueue_rx(struct b43_wldev *dev,
  145. unsigned int index)
  146. {
  147. struct b43_pio_rxqueue *q;
  148. q = kzalloc(sizeof(*q), GFP_KERNEL);
  149. if (!q)
  150. return NULL;
  151. q->dev = dev;
  152. q->rev = dev->dev->id.revision;
  153. q->mmio_base = index_to_pioqueue_base(dev, index) +
  154. pio_rxqueue_offset(dev);
  155. /* Enable Direct FIFO RX (PIO) on the engine. */
  156. b43_dma_direct_fifo_rx(dev, index, 1);
  157. return q;
  158. }
  159. static void b43_pio_cancel_tx_packets(struct b43_pio_txqueue *q)
  160. {
  161. struct b43_pio_txpacket *pack;
  162. unsigned int i;
  163. for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
  164. pack = &(q->packets[i]);
  165. if (pack->skb) {
  166. dev_kfree_skb_any(pack->skb);
  167. pack->skb = NULL;
  168. }
  169. }
  170. }
  171. static void b43_destroy_pioqueue_tx(struct b43_pio_txqueue *q,
  172. const char *name)
  173. {
  174. if (!q)
  175. return;
  176. b43_pio_cancel_tx_packets(q);
  177. kfree(q);
  178. }
  179. static void b43_destroy_pioqueue_rx(struct b43_pio_rxqueue *q,
  180. const char *name)
  181. {
  182. if (!q)
  183. return;
  184. kfree(q);
  185. }
  186. #define destroy_queue_tx(pio, queue) do { \
  187. b43_destroy_pioqueue_tx((pio)->queue, __stringify(queue)); \
  188. (pio)->queue = NULL; \
  189. } while (0)
  190. #define destroy_queue_rx(pio, queue) do { \
  191. b43_destroy_pioqueue_rx((pio)->queue, __stringify(queue)); \
  192. (pio)->queue = NULL; \
  193. } while (0)
  194. void b43_pio_free(struct b43_wldev *dev)
  195. {
  196. struct b43_pio *pio;
  197. if (!b43_using_pio_transfers(dev))
  198. return;
  199. pio = &dev->pio;
  200. destroy_queue_rx(pio, rx_queue);
  201. destroy_queue_tx(pio, tx_queue_mcast);
  202. destroy_queue_tx(pio, tx_queue_AC_VO);
  203. destroy_queue_tx(pio, tx_queue_AC_VI);
  204. destroy_queue_tx(pio, tx_queue_AC_BE);
  205. destroy_queue_tx(pio, tx_queue_AC_BK);
  206. }
  207. int b43_pio_init(struct b43_wldev *dev)
  208. {
  209. struct b43_pio *pio = &dev->pio;
  210. int err = -ENOMEM;
  211. b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
  212. & ~B43_MACCTL_BE);
  213. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_RXPADOFF, 0);
  214. pio->tx_queue_AC_BK = b43_setup_pioqueue_tx(dev, 0);
  215. if (!pio->tx_queue_AC_BK)
  216. goto out;
  217. pio->tx_queue_AC_BE = b43_setup_pioqueue_tx(dev, 1);
  218. if (!pio->tx_queue_AC_BE)
  219. goto err_destroy_bk;
  220. pio->tx_queue_AC_VI = b43_setup_pioqueue_tx(dev, 2);
  221. if (!pio->tx_queue_AC_VI)
  222. goto err_destroy_be;
  223. pio->tx_queue_AC_VO = b43_setup_pioqueue_tx(dev, 3);
  224. if (!pio->tx_queue_AC_VO)
  225. goto err_destroy_vi;
  226. pio->tx_queue_mcast = b43_setup_pioqueue_tx(dev, 4);
  227. if (!pio->tx_queue_mcast)
  228. goto err_destroy_vo;
  229. pio->rx_queue = b43_setup_pioqueue_rx(dev, 0);
  230. if (!pio->rx_queue)
  231. goto err_destroy_mcast;
  232. b43dbg(dev->wl, "PIO initialized\n");
  233. err = 0;
  234. out:
  235. return err;
  236. err_destroy_mcast:
  237. destroy_queue_tx(pio, tx_queue_mcast);
  238. err_destroy_vo:
  239. destroy_queue_tx(pio, tx_queue_AC_VO);
  240. err_destroy_vi:
  241. destroy_queue_tx(pio, tx_queue_AC_VI);
  242. err_destroy_be:
  243. destroy_queue_tx(pio, tx_queue_AC_BE);
  244. err_destroy_bk:
  245. destroy_queue_tx(pio, tx_queue_AC_BK);
  246. return err;
  247. }
  248. /* Static mapping of mac80211's queues (priorities) to b43 PIO queues. */
  249. static struct b43_pio_txqueue *select_queue_by_priority(struct b43_wldev *dev,
  250. u8 queue_prio)
  251. {
  252. struct b43_pio_txqueue *q;
  253. if (dev->qos_enabled) {
  254. /* 0 = highest priority */
  255. switch (queue_prio) {
  256. default:
  257. B43_WARN_ON(1);
  258. /* fallthrough */
  259. case 0:
  260. q = dev->pio.tx_queue_AC_VO;
  261. break;
  262. case 1:
  263. q = dev->pio.tx_queue_AC_VI;
  264. break;
  265. case 2:
  266. q = dev->pio.tx_queue_AC_BE;
  267. break;
  268. case 3:
  269. q = dev->pio.tx_queue_AC_BK;
  270. break;
  271. }
  272. } else
  273. q = dev->pio.tx_queue_AC_BE;
  274. return q;
  275. }
  276. static u16 tx_write_2byte_queue(struct b43_pio_txqueue *q,
  277. u16 ctl,
  278. const void *_data,
  279. unsigned int data_len)
  280. {
  281. struct b43_wldev *dev = q->dev;
  282. const u8 *data = _data;
  283. ctl |= B43_PIO_TXCTL_WRITELO | B43_PIO_TXCTL_WRITEHI;
  284. b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
  285. ssb_block_write(dev->dev, data, (data_len & ~1),
  286. q->mmio_base + B43_PIO_TXDATA,
  287. sizeof(u16));
  288. if (data_len & 1) {
  289. u8 tail[2] = { 0, };
  290. /* Write the last byte. */
  291. ctl &= ~B43_PIO_TXCTL_WRITEHI;
  292. b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
  293. tail[0] = data[data_len - 1];
  294. ssb_block_write(dev->dev, tail, 2,
  295. q->mmio_base + B43_PIO_TXDATA,
  296. sizeof(u16));
  297. }
  298. return ctl;
  299. }
  300. static void pio_tx_frame_2byte_queue(struct b43_pio_txpacket *pack,
  301. const u8 *hdr, unsigned int hdrlen)
  302. {
  303. struct b43_pio_txqueue *q = pack->queue;
  304. const char *frame = pack->skb->data;
  305. unsigned int frame_len = pack->skb->len;
  306. u16 ctl;
  307. ctl = b43_piotx_read16(q, B43_PIO_TXCTL);
  308. ctl |= B43_PIO_TXCTL_FREADY;
  309. ctl &= ~B43_PIO_TXCTL_EOF;
  310. /* Transfer the header data. */
  311. ctl = tx_write_2byte_queue(q, ctl, hdr, hdrlen);
  312. /* Transfer the frame data. */
  313. ctl = tx_write_2byte_queue(q, ctl, frame, frame_len);
  314. ctl |= B43_PIO_TXCTL_EOF;
  315. b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
  316. }
  317. static u32 tx_write_4byte_queue(struct b43_pio_txqueue *q,
  318. u32 ctl,
  319. const void *_data,
  320. unsigned int data_len)
  321. {
  322. struct b43_wldev *dev = q->dev;
  323. const u8 *data = _data;
  324. ctl |= B43_PIO8_TXCTL_0_7 | B43_PIO8_TXCTL_8_15 |
  325. B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_24_31;
  326. b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
  327. ssb_block_write(dev->dev, data, (data_len & ~3),
  328. q->mmio_base + B43_PIO8_TXDATA,
  329. sizeof(u32));
  330. if (data_len & 3) {
  331. u8 tail[4] = { 0, };
  332. /* Write the last few bytes. */
  333. ctl &= ~(B43_PIO8_TXCTL_8_15 | B43_PIO8_TXCTL_16_23 |
  334. B43_PIO8_TXCTL_24_31);
  335. switch (data_len & 3) {
  336. case 3:
  337. ctl |= B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_8_15;
  338. tail[0] = data[data_len - 3];
  339. tail[1] = data[data_len - 2];
  340. tail[2] = data[data_len - 1];
  341. break;
  342. case 2:
  343. ctl |= B43_PIO8_TXCTL_8_15;
  344. tail[0] = data[data_len - 2];
  345. tail[1] = data[data_len - 1];
  346. break;
  347. case 1:
  348. tail[0] = data[data_len - 1];
  349. break;
  350. }
  351. b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
  352. ssb_block_write(dev->dev, tail, 4,
  353. q->mmio_base + B43_PIO8_TXDATA,
  354. sizeof(u32));
  355. }
  356. return ctl;
  357. }
  358. static void pio_tx_frame_4byte_queue(struct b43_pio_txpacket *pack,
  359. const u8 *hdr, unsigned int hdrlen)
  360. {
  361. struct b43_pio_txqueue *q = pack->queue;
  362. const char *frame = pack->skb->data;
  363. unsigned int frame_len = pack->skb->len;
  364. u32 ctl;
  365. ctl = b43_piotx_read32(q, B43_PIO8_TXCTL);
  366. ctl |= B43_PIO8_TXCTL_FREADY;
  367. ctl &= ~B43_PIO8_TXCTL_EOF;
  368. /* Transfer the header data. */
  369. ctl = tx_write_4byte_queue(q, ctl, hdr, hdrlen);
  370. /* Transfer the frame data. */
  371. ctl = tx_write_4byte_queue(q, ctl, frame, frame_len);
  372. ctl |= B43_PIO8_TXCTL_EOF;
  373. b43_piotx_write32(q, B43_PIO_TXCTL, ctl);
  374. }
  375. static int pio_tx_frame(struct b43_pio_txqueue *q,
  376. struct sk_buff *skb)
  377. {
  378. struct b43_pio_txpacket *pack;
  379. struct b43_txhdr txhdr;
  380. u16 cookie;
  381. int err;
  382. unsigned int hdrlen;
  383. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  384. B43_WARN_ON(list_empty(&q->packets_list));
  385. pack = list_entry(q->packets_list.next,
  386. struct b43_pio_txpacket, list);
  387. cookie = generate_cookie(q, pack);
  388. hdrlen = b43_txhdr_size(q->dev);
  389. err = b43_generate_txhdr(q->dev, (u8 *)&txhdr, skb,
  390. info, cookie);
  391. if (err)
  392. return err;
  393. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  394. /* Tell the firmware about the cookie of the last
  395. * mcast frame, so it can clear the more-data bit in it. */
  396. b43_shm_write16(q->dev, B43_SHM_SHARED,
  397. B43_SHM_SH_MCASTCOOKIE, cookie);
  398. }
  399. pack->skb = skb;
  400. if (q->rev >= 8)
  401. pio_tx_frame_4byte_queue(pack, (const u8 *)&txhdr, hdrlen);
  402. else
  403. pio_tx_frame_2byte_queue(pack, (const u8 *)&txhdr, hdrlen);
  404. /* Remove it from the list of available packet slots.
  405. * It will be put back when we receive the status report. */
  406. list_del(&pack->list);
  407. /* Update the queue statistics. */
  408. q->buffer_used += roundup(skb->len + hdrlen, 4);
  409. q->free_packet_slots -= 1;
  410. return 0;
  411. }
  412. int b43_pio_tx(struct b43_wldev *dev, struct sk_buff *skb)
  413. {
  414. struct b43_pio_txqueue *q;
  415. struct ieee80211_hdr *hdr;
  416. unsigned int hdrlen, total_len;
  417. int err = 0;
  418. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  419. hdr = (struct ieee80211_hdr *)skb->data;
  420. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  421. /* The multicast queue will be sent after the DTIM. */
  422. q = dev->pio.tx_queue_mcast;
  423. /* Set the frame More-Data bit. Ucode will clear it
  424. * for us on the last frame. */
  425. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  426. } else {
  427. /* Decide by priority where to put this frame. */
  428. q = select_queue_by_priority(dev, skb_get_queue_mapping(skb));
  429. }
  430. hdrlen = b43_txhdr_size(dev);
  431. total_len = roundup(skb->len + hdrlen, 4);
  432. if (unlikely(total_len > q->buffer_size)) {
  433. err = -ENOBUFS;
  434. b43dbg(dev->wl, "PIO: TX packet longer than queue.\n");
  435. goto out;
  436. }
  437. if (unlikely(q->free_packet_slots == 0)) {
  438. err = -ENOBUFS;
  439. b43warn(dev->wl, "PIO: TX packet overflow.\n");
  440. goto out;
  441. }
  442. B43_WARN_ON(q->buffer_used > q->buffer_size);
  443. if (total_len > (q->buffer_size - q->buffer_used)) {
  444. /* Not enough memory on the queue. */
  445. err = -EBUSY;
  446. ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
  447. q->stopped = 1;
  448. goto out;
  449. }
  450. /* Assign the queue number to the ring (if not already done before)
  451. * so TX status handling can use it. The mac80211-queue to b43-queue
  452. * mapping is static, so we don't need to store it per frame. */
  453. q->queue_prio = skb_get_queue_mapping(skb);
  454. err = pio_tx_frame(q, skb);
  455. if (unlikely(err == -ENOKEY)) {
  456. /* Drop this packet, as we don't have the encryption key
  457. * anymore and must not transmit it unencrypted. */
  458. dev_kfree_skb_any(skb);
  459. err = 0;
  460. goto out;
  461. }
  462. if (unlikely(err)) {
  463. b43err(dev->wl, "PIO transmission failure\n");
  464. goto out;
  465. }
  466. q->nr_tx_packets++;
  467. B43_WARN_ON(q->buffer_used > q->buffer_size);
  468. if (((q->buffer_size - q->buffer_used) < roundup(2 + 2 + 6, 4)) ||
  469. (q->free_packet_slots == 0)) {
  470. /* The queue is full. */
  471. ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
  472. q->stopped = 1;
  473. }
  474. out:
  475. return err;
  476. }
  477. void b43_pio_handle_txstatus(struct b43_wldev *dev,
  478. const struct b43_txstatus *status)
  479. {
  480. struct b43_pio_txqueue *q;
  481. struct b43_pio_txpacket *pack = NULL;
  482. unsigned int total_len;
  483. struct ieee80211_tx_info *info;
  484. q = parse_cookie(dev, status->cookie, &pack);
  485. if (unlikely(!q))
  486. return;
  487. B43_WARN_ON(!pack);
  488. info = IEEE80211_SKB_CB(pack->skb);
  489. b43_fill_txstatus_report(dev, info, status);
  490. total_len = pack->skb->len + b43_txhdr_size(dev);
  491. total_len = roundup(total_len, 4);
  492. q->buffer_used -= total_len;
  493. q->free_packet_slots += 1;
  494. ieee80211_tx_status(dev->wl->hw, pack->skb);
  495. pack->skb = NULL;
  496. list_add(&pack->list, &q->packets_list);
  497. if (q->stopped) {
  498. ieee80211_wake_queue(dev->wl->hw, q->queue_prio);
  499. q->stopped = 0;
  500. }
  501. }
  502. void b43_pio_get_tx_stats(struct b43_wldev *dev,
  503. struct ieee80211_tx_queue_stats *stats)
  504. {
  505. const int nr_queues = dev->wl->hw->queues;
  506. struct b43_pio_txqueue *q;
  507. int i;
  508. for (i = 0; i < nr_queues; i++) {
  509. q = select_queue_by_priority(dev, i);
  510. stats[i].len = B43_PIO_MAX_NR_TXPACKETS - q->free_packet_slots;
  511. stats[i].limit = B43_PIO_MAX_NR_TXPACKETS;
  512. stats[i].count = q->nr_tx_packets;
  513. }
  514. }
  515. /* Returns whether we should fetch another frame. */
  516. static bool pio_rx_frame(struct b43_pio_rxqueue *q)
  517. {
  518. struct b43_wldev *dev = q->dev;
  519. struct b43_rxhdr_fw4 rxhdr;
  520. u16 len;
  521. u32 macstat;
  522. unsigned int i, padding;
  523. struct sk_buff *skb;
  524. const char *err_msg = NULL;
  525. memset(&rxhdr, 0, sizeof(rxhdr));
  526. /* Check if we have data and wait for it to get ready. */
  527. if (q->rev >= 8) {
  528. u32 ctl;
  529. ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
  530. if (!(ctl & B43_PIO8_RXCTL_FRAMERDY))
  531. return 0;
  532. b43_piorx_write32(q, B43_PIO8_RXCTL,
  533. B43_PIO8_RXCTL_FRAMERDY);
  534. for (i = 0; i < 10; i++) {
  535. ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
  536. if (ctl & B43_PIO8_RXCTL_DATARDY)
  537. goto data_ready;
  538. udelay(10);
  539. }
  540. } else {
  541. u16 ctl;
  542. ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
  543. if (!(ctl & B43_PIO_RXCTL_FRAMERDY))
  544. return 0;
  545. b43_piorx_write16(q, B43_PIO_RXCTL,
  546. B43_PIO_RXCTL_FRAMERDY);
  547. for (i = 0; i < 10; i++) {
  548. ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
  549. if (ctl & B43_PIO_RXCTL_DATARDY)
  550. goto data_ready;
  551. udelay(10);
  552. }
  553. }
  554. b43dbg(q->dev->wl, "PIO RX timed out\n");
  555. return 1;
  556. data_ready:
  557. /* Get the preamble (RX header) */
  558. if (q->rev >= 8) {
  559. ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
  560. q->mmio_base + B43_PIO8_RXDATA,
  561. sizeof(u32));
  562. } else {
  563. ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
  564. q->mmio_base + B43_PIO_RXDATA,
  565. sizeof(u16));
  566. }
  567. /* Sanity checks. */
  568. len = le16_to_cpu(rxhdr.frame_len);
  569. if (unlikely(len > 0x700)) {
  570. err_msg = "len > 0x700";
  571. goto rx_error;
  572. }
  573. if (unlikely(len == 0)) {
  574. err_msg = "len == 0";
  575. goto rx_error;
  576. }
  577. macstat = le32_to_cpu(rxhdr.mac_status);
  578. if (macstat & B43_RX_MAC_FCSERR) {
  579. if (!(q->dev->wl->filter_flags & FIF_FCSFAIL)) {
  580. /* Drop frames with failed FCS. */
  581. err_msg = "Frame FCS error";
  582. goto rx_error;
  583. }
  584. }
  585. /* We always pad 2 bytes, as that's what upstream code expects
  586. * due to the RX-header being 30 bytes. In case the frame is
  587. * unaligned, we pad another 2 bytes. */
  588. padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
  589. skb = dev_alloc_skb(len + padding + 2);
  590. if (unlikely(!skb)) {
  591. err_msg = "Out of memory";
  592. goto rx_error;
  593. }
  594. skb_reserve(skb, 2);
  595. skb_put(skb, len + padding);
  596. if (q->rev >= 8) {
  597. ssb_block_read(dev->dev, skb->data + padding, (len & ~3),
  598. q->mmio_base + B43_PIO8_RXDATA,
  599. sizeof(u32));
  600. if (len & 3) {
  601. u8 tail[4] = { 0, };
  602. /* Read the last few bytes. */
  603. ssb_block_read(dev->dev, tail, 4,
  604. q->mmio_base + B43_PIO8_RXDATA,
  605. sizeof(u32));
  606. switch (len & 3) {
  607. case 3:
  608. skb->data[len + padding - 3] = tail[0];
  609. skb->data[len + padding - 2] = tail[1];
  610. skb->data[len + padding - 1] = tail[2];
  611. break;
  612. case 2:
  613. skb->data[len + padding - 2] = tail[0];
  614. skb->data[len + padding - 1] = tail[1];
  615. break;
  616. case 1:
  617. skb->data[len + padding - 1] = tail[0];
  618. break;
  619. }
  620. }
  621. } else {
  622. ssb_block_read(dev->dev, skb->data + padding, (len & ~1),
  623. q->mmio_base + B43_PIO_RXDATA,
  624. sizeof(u16));
  625. if (len & 1) {
  626. u8 tail[2] = { 0, };
  627. /* Read the last byte. */
  628. ssb_block_read(dev->dev, tail, 2,
  629. q->mmio_base + B43_PIO_RXDATA,
  630. sizeof(u16));
  631. skb->data[len + padding - 1] = tail[0];
  632. }
  633. }
  634. b43_rx(q->dev, skb, &rxhdr);
  635. return 1;
  636. rx_error:
  637. if (err_msg)
  638. b43dbg(q->dev->wl, "PIO RX error: %s\n", err_msg);
  639. b43_piorx_write16(q, B43_PIO_RXCTL, B43_PIO_RXCTL_DATARDY);
  640. return 1;
  641. }
  642. void b43_pio_rx(struct b43_pio_rxqueue *q)
  643. {
  644. unsigned int count = 0;
  645. bool stop;
  646. while (1) {
  647. stop = (pio_rx_frame(q) == 0);
  648. if (stop)
  649. break;
  650. cond_resched();
  651. if (WARN_ON_ONCE(++count > 10000))
  652. break;
  653. }
  654. }
  655. static void b43_pio_tx_suspend_queue(struct b43_pio_txqueue *q)
  656. {
  657. if (q->rev >= 8) {
  658. b43_piotx_write32(q, B43_PIO8_TXCTL,
  659. b43_piotx_read32(q, B43_PIO8_TXCTL)
  660. | B43_PIO8_TXCTL_SUSPREQ);
  661. } else {
  662. b43_piotx_write16(q, B43_PIO_TXCTL,
  663. b43_piotx_read16(q, B43_PIO_TXCTL)
  664. | B43_PIO_TXCTL_SUSPREQ);
  665. }
  666. }
  667. static void b43_pio_tx_resume_queue(struct b43_pio_txqueue *q)
  668. {
  669. if (q->rev >= 8) {
  670. b43_piotx_write32(q, B43_PIO8_TXCTL,
  671. b43_piotx_read32(q, B43_PIO8_TXCTL)
  672. & ~B43_PIO8_TXCTL_SUSPREQ);
  673. } else {
  674. b43_piotx_write16(q, B43_PIO_TXCTL,
  675. b43_piotx_read16(q, B43_PIO_TXCTL)
  676. & ~B43_PIO_TXCTL_SUSPREQ);
  677. }
  678. }
  679. void b43_pio_tx_suspend(struct b43_wldev *dev)
  680. {
  681. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  682. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BK);
  683. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BE);
  684. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VI);
  685. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VO);
  686. b43_pio_tx_suspend_queue(dev->pio.tx_queue_mcast);
  687. }
  688. void b43_pio_tx_resume(struct b43_wldev *dev)
  689. {
  690. b43_pio_tx_resume_queue(dev->pio.tx_queue_mcast);
  691. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VO);
  692. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VI);
  693. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BE);
  694. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BK);
  695. b43_power_saving_ctl_bits(dev, 0);
  696. }