pio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. /* Write the last byte. */
  290. ctl &= ~B43_PIO_TXCTL_WRITEHI;
  291. b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
  292. b43_piotx_write16(q, B43_PIO_TXDATA, data[data_len - 1]);
  293. }
  294. return ctl;
  295. }
  296. static void pio_tx_frame_2byte_queue(struct b43_pio_txpacket *pack,
  297. const u8 *hdr, unsigned int hdrlen)
  298. {
  299. struct b43_pio_txqueue *q = pack->queue;
  300. const char *frame = pack->skb->data;
  301. unsigned int frame_len = pack->skb->len;
  302. u16 ctl;
  303. ctl = b43_piotx_read16(q, B43_PIO_TXCTL);
  304. ctl |= B43_PIO_TXCTL_FREADY;
  305. ctl &= ~B43_PIO_TXCTL_EOF;
  306. /* Transfer the header data. */
  307. ctl = tx_write_2byte_queue(q, ctl, hdr, hdrlen);
  308. /* Transfer the frame data. */
  309. ctl = tx_write_2byte_queue(q, ctl, frame, frame_len);
  310. ctl |= B43_PIO_TXCTL_EOF;
  311. b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
  312. }
  313. static u32 tx_write_4byte_queue(struct b43_pio_txqueue *q,
  314. u32 ctl,
  315. const void *_data,
  316. unsigned int data_len)
  317. {
  318. struct b43_wldev *dev = q->dev;
  319. const u8 *data = _data;
  320. ctl |= B43_PIO8_TXCTL_0_7 | B43_PIO8_TXCTL_8_15 |
  321. B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_24_31;
  322. b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
  323. ssb_block_write(dev->dev, data, (data_len & ~3),
  324. q->mmio_base + B43_PIO8_TXDATA,
  325. sizeof(u32));
  326. if (data_len & 3) {
  327. u32 value = 0;
  328. /* Write the last few bytes. */
  329. ctl &= ~(B43_PIO8_TXCTL_8_15 | B43_PIO8_TXCTL_16_23 |
  330. B43_PIO8_TXCTL_24_31);
  331. data = &(data[data_len - 1]);
  332. switch (data_len & 3) {
  333. case 3:
  334. ctl |= B43_PIO8_TXCTL_16_23;
  335. value |= (u32)(*data) << 16;
  336. data--;
  337. case 2:
  338. ctl |= B43_PIO8_TXCTL_8_15;
  339. value |= (u32)(*data) << 8;
  340. data--;
  341. case 1:
  342. value |= (u32)(*data);
  343. }
  344. b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
  345. b43_piotx_write32(q, B43_PIO8_TXDATA, value);
  346. }
  347. return ctl;
  348. }
  349. static void pio_tx_frame_4byte_queue(struct b43_pio_txpacket *pack,
  350. const u8 *hdr, unsigned int hdrlen)
  351. {
  352. struct b43_pio_txqueue *q = pack->queue;
  353. const char *frame = pack->skb->data;
  354. unsigned int frame_len = pack->skb->len;
  355. u32 ctl;
  356. ctl = b43_piotx_read32(q, B43_PIO8_TXCTL);
  357. ctl |= B43_PIO8_TXCTL_FREADY;
  358. ctl &= ~B43_PIO8_TXCTL_EOF;
  359. /* Transfer the header data. */
  360. ctl = tx_write_4byte_queue(q, ctl, hdr, hdrlen);
  361. /* Transfer the frame data. */
  362. ctl = tx_write_4byte_queue(q, ctl, frame, frame_len);
  363. ctl |= B43_PIO8_TXCTL_EOF;
  364. b43_piotx_write32(q, B43_PIO_TXCTL, ctl);
  365. }
  366. static int pio_tx_frame(struct b43_pio_txqueue *q,
  367. struct sk_buff *skb)
  368. {
  369. struct b43_pio_txpacket *pack;
  370. struct b43_txhdr txhdr;
  371. u16 cookie;
  372. int err;
  373. unsigned int hdrlen;
  374. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  375. B43_WARN_ON(list_empty(&q->packets_list));
  376. pack = list_entry(q->packets_list.next,
  377. struct b43_pio_txpacket, list);
  378. cookie = generate_cookie(q, pack);
  379. hdrlen = b43_txhdr_size(q->dev);
  380. err = b43_generate_txhdr(q->dev, (u8 *)&txhdr, skb,
  381. info, cookie);
  382. if (err)
  383. return err;
  384. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  385. /* Tell the firmware about the cookie of the last
  386. * mcast frame, so it can clear the more-data bit in it. */
  387. b43_shm_write16(q->dev, B43_SHM_SHARED,
  388. B43_SHM_SH_MCASTCOOKIE, cookie);
  389. }
  390. pack->skb = skb;
  391. if (q->rev >= 8)
  392. pio_tx_frame_4byte_queue(pack, (const u8 *)&txhdr, hdrlen);
  393. else
  394. pio_tx_frame_2byte_queue(pack, (const u8 *)&txhdr, hdrlen);
  395. /* Remove it from the list of available packet slots.
  396. * It will be put back when we receive the status report. */
  397. list_del(&pack->list);
  398. /* Update the queue statistics. */
  399. q->buffer_used += roundup(skb->len + hdrlen, 4);
  400. q->free_packet_slots -= 1;
  401. return 0;
  402. }
  403. int b43_pio_tx(struct b43_wldev *dev, struct sk_buff *skb)
  404. {
  405. struct b43_pio_txqueue *q;
  406. struct ieee80211_hdr *hdr;
  407. unsigned int hdrlen, total_len;
  408. int err = 0;
  409. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  410. hdr = (struct ieee80211_hdr *)skb->data;
  411. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  412. /* The multicast queue will be sent after the DTIM. */
  413. q = dev->pio.tx_queue_mcast;
  414. /* Set the frame More-Data bit. Ucode will clear it
  415. * for us on the last frame. */
  416. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  417. } else {
  418. /* Decide by priority where to put this frame. */
  419. q = select_queue_by_priority(dev, skb_get_queue_mapping(skb));
  420. }
  421. hdrlen = b43_txhdr_size(dev);
  422. total_len = roundup(skb->len + hdrlen, 4);
  423. if (unlikely(total_len > q->buffer_size)) {
  424. err = -ENOBUFS;
  425. b43dbg(dev->wl, "PIO: TX packet longer than queue.\n");
  426. goto out;
  427. }
  428. if (unlikely(q->free_packet_slots == 0)) {
  429. err = -ENOBUFS;
  430. b43warn(dev->wl, "PIO: TX packet overflow.\n");
  431. goto out;
  432. }
  433. B43_WARN_ON(q->buffer_used > q->buffer_size);
  434. if (total_len > (q->buffer_size - q->buffer_used)) {
  435. /* Not enough memory on the queue. */
  436. err = -EBUSY;
  437. ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
  438. q->stopped = 1;
  439. goto out;
  440. }
  441. /* Assign the queue number to the ring (if not already done before)
  442. * so TX status handling can use it. The mac80211-queue to b43-queue
  443. * mapping is static, so we don't need to store it per frame. */
  444. q->queue_prio = skb_get_queue_mapping(skb);
  445. err = pio_tx_frame(q, skb);
  446. if (unlikely(err == -ENOKEY)) {
  447. /* Drop this packet, as we don't have the encryption key
  448. * anymore and must not transmit it unencrypted. */
  449. dev_kfree_skb_any(skb);
  450. err = 0;
  451. goto out;
  452. }
  453. if (unlikely(err)) {
  454. b43err(dev->wl, "PIO transmission failure\n");
  455. goto out;
  456. }
  457. q->nr_tx_packets++;
  458. B43_WARN_ON(q->buffer_used > q->buffer_size);
  459. if (((q->buffer_size - q->buffer_used) < roundup(2 + 2 + 6, 4)) ||
  460. (q->free_packet_slots == 0)) {
  461. /* The queue is full. */
  462. ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
  463. q->stopped = 1;
  464. }
  465. out:
  466. return err;
  467. }
  468. void b43_pio_handle_txstatus(struct b43_wldev *dev,
  469. const struct b43_txstatus *status)
  470. {
  471. struct b43_pio_txqueue *q;
  472. struct b43_pio_txpacket *pack = NULL;
  473. unsigned int total_len;
  474. struct ieee80211_tx_info *info;
  475. q = parse_cookie(dev, status->cookie, &pack);
  476. if (unlikely(!q))
  477. return;
  478. B43_WARN_ON(!pack);
  479. info = IEEE80211_SKB_CB(pack->skb);
  480. b43_fill_txstatus_report(dev, info, status);
  481. total_len = pack->skb->len + b43_txhdr_size(dev);
  482. total_len = roundup(total_len, 4);
  483. q->buffer_used -= total_len;
  484. q->free_packet_slots += 1;
  485. ieee80211_tx_status_irqsafe(dev->wl->hw, pack->skb);
  486. pack->skb = NULL;
  487. list_add(&pack->list, &q->packets_list);
  488. if (q->stopped) {
  489. ieee80211_wake_queue(dev->wl->hw, q->queue_prio);
  490. q->stopped = 0;
  491. }
  492. }
  493. void b43_pio_get_tx_stats(struct b43_wldev *dev,
  494. struct ieee80211_tx_queue_stats *stats)
  495. {
  496. const int nr_queues = dev->wl->hw->queues;
  497. struct b43_pio_txqueue *q;
  498. int i;
  499. for (i = 0; i < nr_queues; i++) {
  500. q = select_queue_by_priority(dev, i);
  501. stats[i].len = B43_PIO_MAX_NR_TXPACKETS - q->free_packet_slots;
  502. stats[i].limit = B43_PIO_MAX_NR_TXPACKETS;
  503. stats[i].count = q->nr_tx_packets;
  504. }
  505. }
  506. /* Returns whether we should fetch another frame. */
  507. static bool pio_rx_frame(struct b43_pio_rxqueue *q)
  508. {
  509. struct b43_wldev *dev = q->dev;
  510. struct b43_rxhdr_fw4 rxhdr;
  511. u16 len;
  512. u32 macstat;
  513. unsigned int i, padding;
  514. struct sk_buff *skb;
  515. const char *err_msg = NULL;
  516. memset(&rxhdr, 0, sizeof(rxhdr));
  517. /* Check if we have data and wait for it to get ready. */
  518. if (q->rev >= 8) {
  519. u32 ctl;
  520. ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
  521. if (!(ctl & B43_PIO8_RXCTL_FRAMERDY))
  522. return 0;
  523. b43_piorx_write32(q, B43_PIO8_RXCTL,
  524. B43_PIO8_RXCTL_FRAMERDY);
  525. for (i = 0; i < 10; i++) {
  526. ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
  527. if (ctl & B43_PIO8_RXCTL_DATARDY)
  528. goto data_ready;
  529. udelay(10);
  530. }
  531. } else {
  532. u16 ctl;
  533. ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
  534. if (!(ctl & B43_PIO_RXCTL_FRAMERDY))
  535. return 0;
  536. b43_piorx_write16(q, B43_PIO_RXCTL,
  537. B43_PIO_RXCTL_FRAMERDY);
  538. for (i = 0; i < 10; i++) {
  539. ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
  540. if (ctl & B43_PIO_RXCTL_DATARDY)
  541. goto data_ready;
  542. udelay(10);
  543. }
  544. }
  545. b43dbg(q->dev->wl, "PIO RX timed out\n");
  546. return 1;
  547. data_ready:
  548. /* Get the preamble (RX header) */
  549. if (q->rev >= 8) {
  550. ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
  551. q->mmio_base + B43_PIO8_RXDATA,
  552. sizeof(u32));
  553. } else {
  554. ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
  555. q->mmio_base + B43_PIO_RXDATA,
  556. sizeof(u16));
  557. }
  558. /* Sanity checks. */
  559. len = le16_to_cpu(rxhdr.frame_len);
  560. if (unlikely(len > 0x700)) {
  561. err_msg = "len > 0x700";
  562. goto rx_error;
  563. }
  564. if (unlikely(len == 0)) {
  565. err_msg = "len == 0";
  566. goto rx_error;
  567. }
  568. macstat = le32_to_cpu(rxhdr.mac_status);
  569. if (macstat & B43_RX_MAC_FCSERR) {
  570. if (!(q->dev->wl->filter_flags & FIF_FCSFAIL)) {
  571. /* Drop frames with failed FCS. */
  572. err_msg = "Frame FCS error";
  573. goto rx_error;
  574. }
  575. }
  576. /* We always pad 2 bytes, as that's what upstream code expects
  577. * due to the RX-header being 30 bytes. In case the frame is
  578. * unaligned, we pad another 2 bytes. */
  579. padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
  580. skb = dev_alloc_skb(len + padding + 2);
  581. if (unlikely(!skb)) {
  582. err_msg = "Out of memory";
  583. goto rx_error;
  584. }
  585. skb_reserve(skb, 2);
  586. skb_put(skb, len + padding);
  587. if (q->rev >= 8) {
  588. ssb_block_read(dev->dev, skb->data + padding, (len & ~3),
  589. q->mmio_base + B43_PIO8_RXDATA,
  590. sizeof(u32));
  591. if (len & 3) {
  592. u32 value;
  593. char *data;
  594. /* Read the last few bytes. */
  595. value = b43_piorx_read32(q, B43_PIO8_RXDATA);
  596. data = &(skb->data[len + padding - 1]);
  597. switch (len & 3) {
  598. case 3:
  599. *data = (value >> 16);
  600. data--;
  601. case 2:
  602. *data = (value >> 8);
  603. data--;
  604. case 1:
  605. *data = value;
  606. }
  607. }
  608. } else {
  609. ssb_block_read(dev->dev, skb->data + padding, (len & ~1),
  610. q->mmio_base + B43_PIO_RXDATA,
  611. sizeof(u16));
  612. if (len & 1) {
  613. u16 value;
  614. /* Read the last byte. */
  615. value = b43_piorx_read16(q, B43_PIO_RXDATA);
  616. skb->data[len + padding - 1] = value;
  617. }
  618. }
  619. b43_rx(q->dev, skb, &rxhdr);
  620. return 1;
  621. rx_error:
  622. if (err_msg)
  623. b43dbg(q->dev->wl, "PIO RX error: %s\n", err_msg);
  624. b43_piorx_write16(q, B43_PIO_RXCTL, B43_PIO_RXCTL_DATARDY);
  625. return 1;
  626. }
  627. void b43_pio_rx(struct b43_pio_rxqueue *q)
  628. {
  629. unsigned int count = 0;
  630. bool stop;
  631. while (1) {
  632. stop = (pio_rx_frame(q) == 0);
  633. if (stop)
  634. break;
  635. cond_resched();
  636. if (WARN_ON_ONCE(++count > 10000))
  637. break;
  638. }
  639. }
  640. static void b43_pio_tx_suspend_queue(struct b43_pio_txqueue *q)
  641. {
  642. if (q->rev >= 8) {
  643. b43_piotx_write32(q, B43_PIO8_TXCTL,
  644. b43_piotx_read32(q, B43_PIO8_TXCTL)
  645. | B43_PIO8_TXCTL_SUSPREQ);
  646. } else {
  647. b43_piotx_write16(q, B43_PIO_TXCTL,
  648. b43_piotx_read16(q, B43_PIO_TXCTL)
  649. | B43_PIO_TXCTL_SUSPREQ);
  650. }
  651. }
  652. static void b43_pio_tx_resume_queue(struct b43_pio_txqueue *q)
  653. {
  654. if (q->rev >= 8) {
  655. b43_piotx_write32(q, B43_PIO8_TXCTL,
  656. b43_piotx_read32(q, B43_PIO8_TXCTL)
  657. & ~B43_PIO8_TXCTL_SUSPREQ);
  658. } else {
  659. b43_piotx_write16(q, B43_PIO_TXCTL,
  660. b43_piotx_read16(q, B43_PIO_TXCTL)
  661. & ~B43_PIO_TXCTL_SUSPREQ);
  662. }
  663. }
  664. void b43_pio_tx_suspend(struct b43_wldev *dev)
  665. {
  666. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  667. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BK);
  668. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BE);
  669. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VI);
  670. b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VO);
  671. b43_pio_tx_suspend_queue(dev->pio.tx_queue_mcast);
  672. }
  673. void b43_pio_tx_resume(struct b43_wldev *dev)
  674. {
  675. b43_pio_tx_resume_queue(dev->pio.tx_queue_mcast);
  676. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VO);
  677. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VI);
  678. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BE);
  679. b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BK);
  680. b43_power_saving_ctl_bits(dev, 0);
  681. }