dma.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /*
  2. Broadcom B43legacy wireless driver
  3. DMA ringbuffer and descriptor allocation/management
  4. Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
  5. Some code in this file is derived from the b44.c driver
  6. Copyright (C) 2002 David S. Miller
  7. Copyright (C) Pekka Pietikainen
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; see the file COPYING. If not, write to
  18. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  19. Boston, MA 02110-1301, USA.
  20. */
  21. #include "b43legacy.h"
  22. #include "dma.h"
  23. #include "main.h"
  24. #include "debugfs.h"
  25. #include "xmit.h"
  26. #include <linux/dma-mapping.h>
  27. #include <linux/pci.h>
  28. #include <linux/delay.h>
  29. #include <linux/skbuff.h>
  30. #include <net/dst.h>
  31. /* 32bit DMA ops. */
  32. static
  33. struct b43legacy_dmadesc_generic *op32_idx2desc(
  34. struct b43legacy_dmaring *ring,
  35. int slot,
  36. struct b43legacy_dmadesc_meta **meta)
  37. {
  38. struct b43legacy_dmadesc32 *desc;
  39. *meta = &(ring->meta[slot]);
  40. desc = ring->descbase;
  41. desc = &(desc[slot]);
  42. return (struct b43legacy_dmadesc_generic *)desc;
  43. }
  44. static void op32_fill_descriptor(struct b43legacy_dmaring *ring,
  45. struct b43legacy_dmadesc_generic *desc,
  46. dma_addr_t dmaaddr, u16 bufsize,
  47. int start, int end, int irq)
  48. {
  49. struct b43legacy_dmadesc32 *descbase = ring->descbase;
  50. int slot;
  51. u32 ctl;
  52. u32 addr;
  53. u32 addrext;
  54. slot = (int)(&(desc->dma32) - descbase);
  55. B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  56. addr = (u32)(dmaaddr & ~SSB_DMA_TRANSLATION_MASK);
  57. addrext = (u32)(dmaaddr & SSB_DMA_TRANSLATION_MASK)
  58. >> SSB_DMA_TRANSLATION_SHIFT;
  59. addr |= ssb_dma_translation(ring->dev->dev);
  60. ctl = (bufsize - ring->frameoffset)
  61. & B43legacy_DMA32_DCTL_BYTECNT;
  62. if (slot == ring->nr_slots - 1)
  63. ctl |= B43legacy_DMA32_DCTL_DTABLEEND;
  64. if (start)
  65. ctl |= B43legacy_DMA32_DCTL_FRAMESTART;
  66. if (end)
  67. ctl |= B43legacy_DMA32_DCTL_FRAMEEND;
  68. if (irq)
  69. ctl |= B43legacy_DMA32_DCTL_IRQ;
  70. ctl |= (addrext << B43legacy_DMA32_DCTL_ADDREXT_SHIFT)
  71. & B43legacy_DMA32_DCTL_ADDREXT_MASK;
  72. desc->dma32.control = cpu_to_le32(ctl);
  73. desc->dma32.address = cpu_to_le32(addr);
  74. }
  75. static void op32_poke_tx(struct b43legacy_dmaring *ring, int slot)
  76. {
  77. b43legacy_dma_write(ring, B43legacy_DMA32_TXINDEX,
  78. (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
  79. }
  80. static void op32_tx_suspend(struct b43legacy_dmaring *ring)
  81. {
  82. b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
  83. b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
  84. | B43legacy_DMA32_TXSUSPEND);
  85. }
  86. static void op32_tx_resume(struct b43legacy_dmaring *ring)
  87. {
  88. b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
  89. b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
  90. & ~B43legacy_DMA32_TXSUSPEND);
  91. }
  92. static int op32_get_current_rxslot(struct b43legacy_dmaring *ring)
  93. {
  94. u32 val;
  95. val = b43legacy_dma_read(ring, B43legacy_DMA32_RXSTATUS);
  96. val &= B43legacy_DMA32_RXDPTR;
  97. return (val / sizeof(struct b43legacy_dmadesc32));
  98. }
  99. static void op32_set_current_rxslot(struct b43legacy_dmaring *ring,
  100. int slot)
  101. {
  102. b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX,
  103. (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
  104. }
  105. static const struct b43legacy_dma_ops dma32_ops = {
  106. .idx2desc = op32_idx2desc,
  107. .fill_descriptor = op32_fill_descriptor,
  108. .poke_tx = op32_poke_tx,
  109. .tx_suspend = op32_tx_suspend,
  110. .tx_resume = op32_tx_resume,
  111. .get_current_rxslot = op32_get_current_rxslot,
  112. .set_current_rxslot = op32_set_current_rxslot,
  113. };
  114. /* 64bit DMA ops. */
  115. static
  116. struct b43legacy_dmadesc_generic *op64_idx2desc(
  117. struct b43legacy_dmaring *ring,
  118. int slot,
  119. struct b43legacy_dmadesc_meta
  120. **meta)
  121. {
  122. struct b43legacy_dmadesc64 *desc;
  123. *meta = &(ring->meta[slot]);
  124. desc = ring->descbase;
  125. desc = &(desc[slot]);
  126. return (struct b43legacy_dmadesc_generic *)desc;
  127. }
  128. static void op64_fill_descriptor(struct b43legacy_dmaring *ring,
  129. struct b43legacy_dmadesc_generic *desc,
  130. dma_addr_t dmaaddr, u16 bufsize,
  131. int start, int end, int irq)
  132. {
  133. struct b43legacy_dmadesc64 *descbase = ring->descbase;
  134. int slot;
  135. u32 ctl0 = 0;
  136. u32 ctl1 = 0;
  137. u32 addrlo;
  138. u32 addrhi;
  139. u32 addrext;
  140. slot = (int)(&(desc->dma64) - descbase);
  141. B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  142. addrlo = (u32)(dmaaddr & 0xFFFFFFFF);
  143. addrhi = (((u64)dmaaddr >> 32) & ~SSB_DMA_TRANSLATION_MASK);
  144. addrext = (((u64)dmaaddr >> 32) & SSB_DMA_TRANSLATION_MASK)
  145. >> SSB_DMA_TRANSLATION_SHIFT;
  146. addrhi |= ssb_dma_translation(ring->dev->dev);
  147. if (slot == ring->nr_slots - 1)
  148. ctl0 |= B43legacy_DMA64_DCTL0_DTABLEEND;
  149. if (start)
  150. ctl0 |= B43legacy_DMA64_DCTL0_FRAMESTART;
  151. if (end)
  152. ctl0 |= B43legacy_DMA64_DCTL0_FRAMEEND;
  153. if (irq)
  154. ctl0 |= B43legacy_DMA64_DCTL0_IRQ;
  155. ctl1 |= (bufsize - ring->frameoffset)
  156. & B43legacy_DMA64_DCTL1_BYTECNT;
  157. ctl1 |= (addrext << B43legacy_DMA64_DCTL1_ADDREXT_SHIFT)
  158. & B43legacy_DMA64_DCTL1_ADDREXT_MASK;
  159. desc->dma64.control0 = cpu_to_le32(ctl0);
  160. desc->dma64.control1 = cpu_to_le32(ctl1);
  161. desc->dma64.address_low = cpu_to_le32(addrlo);
  162. desc->dma64.address_high = cpu_to_le32(addrhi);
  163. }
  164. static void op64_poke_tx(struct b43legacy_dmaring *ring, int slot)
  165. {
  166. b43legacy_dma_write(ring, B43legacy_DMA64_TXINDEX,
  167. (u32)(slot * sizeof(struct b43legacy_dmadesc64)));
  168. }
  169. static void op64_tx_suspend(struct b43legacy_dmaring *ring)
  170. {
  171. b43legacy_dma_write(ring, B43legacy_DMA64_TXCTL,
  172. b43legacy_dma_read(ring, B43legacy_DMA64_TXCTL)
  173. | B43legacy_DMA64_TXSUSPEND);
  174. }
  175. static void op64_tx_resume(struct b43legacy_dmaring *ring)
  176. {
  177. b43legacy_dma_write(ring, B43legacy_DMA64_TXCTL,
  178. b43legacy_dma_read(ring, B43legacy_DMA64_TXCTL)
  179. & ~B43legacy_DMA64_TXSUSPEND);
  180. }
  181. static int op64_get_current_rxslot(struct b43legacy_dmaring *ring)
  182. {
  183. u32 val;
  184. val = b43legacy_dma_read(ring, B43legacy_DMA64_RXSTATUS);
  185. val &= B43legacy_DMA64_RXSTATDPTR;
  186. return (val / sizeof(struct b43legacy_dmadesc64));
  187. }
  188. static void op64_set_current_rxslot(struct b43legacy_dmaring *ring,
  189. int slot)
  190. {
  191. b43legacy_dma_write(ring, B43legacy_DMA64_RXINDEX,
  192. (u32)(slot * sizeof(struct b43legacy_dmadesc64)));
  193. }
  194. static const struct b43legacy_dma_ops dma64_ops = {
  195. .idx2desc = op64_idx2desc,
  196. .fill_descriptor = op64_fill_descriptor,
  197. .poke_tx = op64_poke_tx,
  198. .tx_suspend = op64_tx_suspend,
  199. .tx_resume = op64_tx_resume,
  200. .get_current_rxslot = op64_get_current_rxslot,
  201. .set_current_rxslot = op64_set_current_rxslot,
  202. };
  203. static inline int free_slots(struct b43legacy_dmaring *ring)
  204. {
  205. return (ring->nr_slots - ring->used_slots);
  206. }
  207. static inline int next_slot(struct b43legacy_dmaring *ring, int slot)
  208. {
  209. B43legacy_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1));
  210. if (slot == ring->nr_slots - 1)
  211. return 0;
  212. return slot + 1;
  213. }
  214. static inline int prev_slot(struct b43legacy_dmaring *ring, int slot)
  215. {
  216. B43legacy_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1));
  217. if (slot == 0)
  218. return ring->nr_slots - 1;
  219. return slot - 1;
  220. }
  221. #ifdef CONFIG_B43LEGACY_DEBUG
  222. static void update_max_used_slots(struct b43legacy_dmaring *ring,
  223. int current_used_slots)
  224. {
  225. if (current_used_slots <= ring->max_used_slots)
  226. return;
  227. ring->max_used_slots = current_used_slots;
  228. if (b43legacy_debug(ring->dev, B43legacy_DBG_DMAVERBOSE))
  229. b43legacydbg(ring->dev->wl,
  230. "max_used_slots increased to %d on %s ring %d\n",
  231. ring->max_used_slots,
  232. ring->tx ? "TX" : "RX",
  233. ring->index);
  234. }
  235. #else
  236. static inline
  237. void update_max_used_slots(struct b43legacy_dmaring *ring,
  238. int current_used_slots)
  239. { }
  240. #endif /* DEBUG */
  241. /* Request a slot for usage. */
  242. static inline
  243. int request_slot(struct b43legacy_dmaring *ring)
  244. {
  245. int slot;
  246. B43legacy_WARN_ON(!ring->tx);
  247. B43legacy_WARN_ON(ring->stopped);
  248. B43legacy_WARN_ON(free_slots(ring) == 0);
  249. slot = next_slot(ring, ring->current_slot);
  250. ring->current_slot = slot;
  251. ring->used_slots++;
  252. update_max_used_slots(ring, ring->used_slots);
  253. return slot;
  254. }
  255. /* Mac80211-queue to b43legacy-ring mapping */
  256. static struct b43legacy_dmaring *priority_to_txring(
  257. struct b43legacy_wldev *dev,
  258. int queue_priority)
  259. {
  260. struct b43legacy_dmaring *ring;
  261. /*FIXME: For now we always run on TX-ring-1 */
  262. return dev->dma.tx_ring1;
  263. /* 0 = highest priority */
  264. switch (queue_priority) {
  265. default:
  266. B43legacy_WARN_ON(1);
  267. /* fallthrough */
  268. case 0:
  269. ring = dev->dma.tx_ring3;
  270. break;
  271. case 1:
  272. ring = dev->dma.tx_ring2;
  273. break;
  274. case 2:
  275. ring = dev->dma.tx_ring1;
  276. break;
  277. case 3:
  278. ring = dev->dma.tx_ring0;
  279. break;
  280. case 4:
  281. ring = dev->dma.tx_ring4;
  282. break;
  283. case 5:
  284. ring = dev->dma.tx_ring5;
  285. break;
  286. }
  287. return ring;
  288. }
  289. /* Bcm4301-ring to mac80211-queue mapping */
  290. static inline int txring_to_priority(struct b43legacy_dmaring *ring)
  291. {
  292. static const u8 idx_to_prio[] =
  293. { 3, 2, 1, 0, 4, 5, };
  294. /*FIXME: have only one queue, for now */
  295. return 0;
  296. return idx_to_prio[ring->index];
  297. }
  298. static u16 b43legacy_dmacontroller_base(enum b43legacy_dmatype type,
  299. int controller_idx)
  300. {
  301. static const u16 map64[] = {
  302. B43legacy_MMIO_DMA64_BASE0,
  303. B43legacy_MMIO_DMA64_BASE1,
  304. B43legacy_MMIO_DMA64_BASE2,
  305. B43legacy_MMIO_DMA64_BASE3,
  306. B43legacy_MMIO_DMA64_BASE4,
  307. B43legacy_MMIO_DMA64_BASE5,
  308. };
  309. static const u16 map32[] = {
  310. B43legacy_MMIO_DMA32_BASE0,
  311. B43legacy_MMIO_DMA32_BASE1,
  312. B43legacy_MMIO_DMA32_BASE2,
  313. B43legacy_MMIO_DMA32_BASE3,
  314. B43legacy_MMIO_DMA32_BASE4,
  315. B43legacy_MMIO_DMA32_BASE5,
  316. };
  317. if (type == B43legacy_DMA_64BIT) {
  318. B43legacy_WARN_ON(!(controller_idx >= 0 &&
  319. controller_idx < ARRAY_SIZE(map64)));
  320. return map64[controller_idx];
  321. }
  322. B43legacy_WARN_ON(!(controller_idx >= 0 &&
  323. controller_idx < ARRAY_SIZE(map32)));
  324. return map32[controller_idx];
  325. }
  326. static inline
  327. dma_addr_t map_descbuffer(struct b43legacy_dmaring *ring,
  328. unsigned char *buf,
  329. size_t len,
  330. int tx)
  331. {
  332. dma_addr_t dmaaddr;
  333. if (tx)
  334. dmaaddr = ssb_dma_map_single(ring->dev->dev,
  335. buf, len,
  336. DMA_TO_DEVICE);
  337. else
  338. dmaaddr = ssb_dma_map_single(ring->dev->dev,
  339. buf, len,
  340. DMA_FROM_DEVICE);
  341. return dmaaddr;
  342. }
  343. static inline
  344. void unmap_descbuffer(struct b43legacy_dmaring *ring,
  345. dma_addr_t addr,
  346. size_t len,
  347. int tx)
  348. {
  349. if (tx)
  350. ssb_dma_unmap_single(ring->dev->dev,
  351. addr, len,
  352. DMA_TO_DEVICE);
  353. else
  354. ssb_dma_unmap_single(ring->dev->dev,
  355. addr, len,
  356. DMA_FROM_DEVICE);
  357. }
  358. static inline
  359. void sync_descbuffer_for_cpu(struct b43legacy_dmaring *ring,
  360. dma_addr_t addr,
  361. size_t len)
  362. {
  363. B43legacy_WARN_ON(ring->tx);
  364. ssb_dma_sync_single_for_cpu(ring->dev->dev,
  365. addr, len, DMA_FROM_DEVICE);
  366. }
  367. static inline
  368. void sync_descbuffer_for_device(struct b43legacy_dmaring *ring,
  369. dma_addr_t addr,
  370. size_t len)
  371. {
  372. B43legacy_WARN_ON(ring->tx);
  373. ssb_dma_sync_single_for_device(ring->dev->dev,
  374. addr, len, DMA_FROM_DEVICE);
  375. }
  376. static inline
  377. void free_descriptor_buffer(struct b43legacy_dmaring *ring,
  378. struct b43legacy_dmadesc_meta *meta,
  379. int irq_context)
  380. {
  381. if (meta->skb) {
  382. if (irq_context)
  383. dev_kfree_skb_irq(meta->skb);
  384. else
  385. dev_kfree_skb(meta->skb);
  386. meta->skb = NULL;
  387. }
  388. }
  389. static int alloc_ringmemory(struct b43legacy_dmaring *ring)
  390. {
  391. /* GFP flags must match the flags in free_ringmemory()! */
  392. ring->descbase = ssb_dma_alloc_consistent(ring->dev->dev,
  393. B43legacy_DMA_RINGMEMSIZE,
  394. &(ring->dmabase),
  395. GFP_KERNEL);
  396. if (!ring->descbase) {
  397. b43legacyerr(ring->dev->wl, "DMA ringmemory allocation"
  398. " failed\n");
  399. return -ENOMEM;
  400. }
  401. memset(ring->descbase, 0, B43legacy_DMA_RINGMEMSIZE);
  402. return 0;
  403. }
  404. static void free_ringmemory(struct b43legacy_dmaring *ring)
  405. {
  406. ssb_dma_free_consistent(ring->dev->dev, B43legacy_DMA_RINGMEMSIZE,
  407. ring->descbase, ring->dmabase, GFP_KERNEL);
  408. }
  409. /* Reset the RX DMA channel */
  410. static int b43legacy_dmacontroller_rx_reset(struct b43legacy_wldev *dev,
  411. u16 mmio_base,
  412. enum b43legacy_dmatype type)
  413. {
  414. int i;
  415. u32 value;
  416. u16 offset;
  417. might_sleep();
  418. offset = (type == B43legacy_DMA_64BIT) ?
  419. B43legacy_DMA64_RXCTL : B43legacy_DMA32_RXCTL;
  420. b43legacy_write32(dev, mmio_base + offset, 0);
  421. for (i = 0; i < 10; i++) {
  422. offset = (type == B43legacy_DMA_64BIT) ?
  423. B43legacy_DMA64_RXSTATUS : B43legacy_DMA32_RXSTATUS;
  424. value = b43legacy_read32(dev, mmio_base + offset);
  425. if (type == B43legacy_DMA_64BIT) {
  426. value &= B43legacy_DMA64_RXSTAT;
  427. if (value == B43legacy_DMA64_RXSTAT_DISABLED) {
  428. i = -1;
  429. break;
  430. }
  431. } else {
  432. value &= B43legacy_DMA32_RXSTATE;
  433. if (value == B43legacy_DMA32_RXSTAT_DISABLED) {
  434. i = -1;
  435. break;
  436. }
  437. }
  438. msleep(1);
  439. }
  440. if (i != -1) {
  441. b43legacyerr(dev->wl, "DMA RX reset timed out\n");
  442. return -ENODEV;
  443. }
  444. return 0;
  445. }
  446. /* Reset the RX DMA channel */
  447. static int b43legacy_dmacontroller_tx_reset(struct b43legacy_wldev *dev,
  448. u16 mmio_base,
  449. enum b43legacy_dmatype type)
  450. {
  451. int i;
  452. u32 value;
  453. u16 offset;
  454. might_sleep();
  455. for (i = 0; i < 10; i++) {
  456. offset = (type == B43legacy_DMA_64BIT) ?
  457. B43legacy_DMA64_TXSTATUS : B43legacy_DMA32_TXSTATUS;
  458. value = b43legacy_read32(dev, mmio_base + offset);
  459. if (type == B43legacy_DMA_64BIT) {
  460. value &= B43legacy_DMA64_TXSTAT;
  461. if (value == B43legacy_DMA64_TXSTAT_DISABLED ||
  462. value == B43legacy_DMA64_TXSTAT_IDLEWAIT ||
  463. value == B43legacy_DMA64_TXSTAT_STOPPED)
  464. break;
  465. } else {
  466. value &= B43legacy_DMA32_TXSTATE;
  467. if (value == B43legacy_DMA32_TXSTAT_DISABLED ||
  468. value == B43legacy_DMA32_TXSTAT_IDLEWAIT ||
  469. value == B43legacy_DMA32_TXSTAT_STOPPED)
  470. break;
  471. }
  472. msleep(1);
  473. }
  474. offset = (type == B43legacy_DMA_64BIT) ? B43legacy_DMA64_TXCTL :
  475. B43legacy_DMA32_TXCTL;
  476. b43legacy_write32(dev, mmio_base + offset, 0);
  477. for (i = 0; i < 10; i++) {
  478. offset = (type == B43legacy_DMA_64BIT) ?
  479. B43legacy_DMA64_TXSTATUS : B43legacy_DMA32_TXSTATUS;
  480. value = b43legacy_read32(dev, mmio_base + offset);
  481. if (type == B43legacy_DMA_64BIT) {
  482. value &= B43legacy_DMA64_TXSTAT;
  483. if (value == B43legacy_DMA64_TXSTAT_DISABLED) {
  484. i = -1;
  485. break;
  486. }
  487. } else {
  488. value &= B43legacy_DMA32_TXSTATE;
  489. if (value == B43legacy_DMA32_TXSTAT_DISABLED) {
  490. i = -1;
  491. break;
  492. }
  493. }
  494. msleep(1);
  495. }
  496. if (i != -1) {
  497. b43legacyerr(dev->wl, "DMA TX reset timed out\n");
  498. return -ENODEV;
  499. }
  500. /* ensure the reset is completed. */
  501. msleep(1);
  502. return 0;
  503. }
  504. /* Check if a DMA mapping address is invalid. */
  505. static bool b43legacy_dma_mapping_error(struct b43legacy_dmaring *ring,
  506. dma_addr_t addr,
  507. size_t buffersize,
  508. bool dma_to_device)
  509. {
  510. if (unlikely(ssb_dma_mapping_error(ring->dev->dev, addr)))
  511. return 1;
  512. switch (ring->type) {
  513. case B43legacy_DMA_30BIT:
  514. if ((u64)addr + buffersize > (1ULL << 30))
  515. goto address_error;
  516. break;
  517. case B43legacy_DMA_32BIT:
  518. if ((u64)addr + buffersize > (1ULL << 32))
  519. goto address_error;
  520. break;
  521. case B43legacy_DMA_64BIT:
  522. /* Currently we can't have addresses beyond 64 bits in the kernel. */
  523. break;
  524. }
  525. /* The address is OK. */
  526. return 0;
  527. address_error:
  528. /* We can't support this address. Unmap it again. */
  529. unmap_descbuffer(ring, addr, buffersize, dma_to_device);
  530. return 1;
  531. }
  532. static int setup_rx_descbuffer(struct b43legacy_dmaring *ring,
  533. struct b43legacy_dmadesc_generic *desc,
  534. struct b43legacy_dmadesc_meta *meta,
  535. gfp_t gfp_flags)
  536. {
  537. struct b43legacy_rxhdr_fw3 *rxhdr;
  538. struct b43legacy_hwtxstatus *txstat;
  539. dma_addr_t dmaaddr;
  540. struct sk_buff *skb;
  541. B43legacy_WARN_ON(ring->tx);
  542. skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
  543. if (unlikely(!skb))
  544. return -ENOMEM;
  545. dmaaddr = map_descbuffer(ring, skb->data,
  546. ring->rx_buffersize, 0);
  547. if (b43legacy_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
  548. /* ugh. try to realloc in zone_dma */
  549. gfp_flags |= GFP_DMA;
  550. dev_kfree_skb_any(skb);
  551. skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
  552. if (unlikely(!skb))
  553. return -ENOMEM;
  554. dmaaddr = map_descbuffer(ring, skb->data,
  555. ring->rx_buffersize, 0);
  556. }
  557. if (b43legacy_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
  558. dev_kfree_skb_any(skb);
  559. return -EIO;
  560. }
  561. meta->skb = skb;
  562. meta->dmaaddr = dmaaddr;
  563. ring->ops->fill_descriptor(ring, desc, dmaaddr,
  564. ring->rx_buffersize, 0, 0, 0);
  565. rxhdr = (struct b43legacy_rxhdr_fw3 *)(skb->data);
  566. rxhdr->frame_len = 0;
  567. txstat = (struct b43legacy_hwtxstatus *)(skb->data);
  568. txstat->cookie = 0;
  569. return 0;
  570. }
  571. /* Allocate the initial descbuffers.
  572. * This is used for an RX ring only.
  573. */
  574. static int alloc_initial_descbuffers(struct b43legacy_dmaring *ring)
  575. {
  576. int i;
  577. int err = -ENOMEM;
  578. struct b43legacy_dmadesc_generic *desc;
  579. struct b43legacy_dmadesc_meta *meta;
  580. for (i = 0; i < ring->nr_slots; i++) {
  581. desc = ring->ops->idx2desc(ring, i, &meta);
  582. err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
  583. if (err) {
  584. b43legacyerr(ring->dev->wl,
  585. "Failed to allocate initial descbuffers\n");
  586. goto err_unwind;
  587. }
  588. }
  589. mb(); /* all descbuffer setup before next line */
  590. ring->used_slots = ring->nr_slots;
  591. err = 0;
  592. out:
  593. return err;
  594. err_unwind:
  595. for (i--; i >= 0; i--) {
  596. desc = ring->ops->idx2desc(ring, i, &meta);
  597. unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
  598. dev_kfree_skb(meta->skb);
  599. }
  600. goto out;
  601. }
  602. /* Do initial setup of the DMA controller.
  603. * Reset the controller, write the ring busaddress
  604. * and switch the "enable" bit on.
  605. */
  606. static int dmacontroller_setup(struct b43legacy_dmaring *ring)
  607. {
  608. int err = 0;
  609. u32 value;
  610. u32 addrext;
  611. u32 trans = ssb_dma_translation(ring->dev->dev);
  612. if (ring->tx) {
  613. if (ring->type == B43legacy_DMA_64BIT) {
  614. u64 ringbase = (u64)(ring->dmabase);
  615. addrext = ((ringbase >> 32) & SSB_DMA_TRANSLATION_MASK)
  616. >> SSB_DMA_TRANSLATION_SHIFT;
  617. value = B43legacy_DMA64_TXENABLE;
  618. value |= (addrext << B43legacy_DMA64_TXADDREXT_SHIFT)
  619. & B43legacy_DMA64_TXADDREXT_MASK;
  620. b43legacy_dma_write(ring, B43legacy_DMA64_TXCTL,
  621. value);
  622. b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGLO,
  623. (ringbase & 0xFFFFFFFF));
  624. b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGHI,
  625. ((ringbase >> 32)
  626. & ~SSB_DMA_TRANSLATION_MASK)
  627. | trans);
  628. } else {
  629. u32 ringbase = (u32)(ring->dmabase);
  630. addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
  631. >> SSB_DMA_TRANSLATION_SHIFT;
  632. value = B43legacy_DMA32_TXENABLE;
  633. value |= (addrext << B43legacy_DMA32_TXADDREXT_SHIFT)
  634. & B43legacy_DMA32_TXADDREXT_MASK;
  635. b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
  636. value);
  637. b43legacy_dma_write(ring, B43legacy_DMA32_TXRING,
  638. (ringbase &
  639. ~SSB_DMA_TRANSLATION_MASK)
  640. | trans);
  641. }
  642. } else {
  643. err = alloc_initial_descbuffers(ring);
  644. if (err)
  645. goto out;
  646. if (ring->type == B43legacy_DMA_64BIT) {
  647. u64 ringbase = (u64)(ring->dmabase);
  648. addrext = ((ringbase >> 32) & SSB_DMA_TRANSLATION_MASK)
  649. >> SSB_DMA_TRANSLATION_SHIFT;
  650. value = (ring->frameoffset <<
  651. B43legacy_DMA64_RXFROFF_SHIFT);
  652. value |= B43legacy_DMA64_RXENABLE;
  653. value |= (addrext << B43legacy_DMA64_RXADDREXT_SHIFT)
  654. & B43legacy_DMA64_RXADDREXT_MASK;
  655. b43legacy_dma_write(ring, B43legacy_DMA64_RXCTL,
  656. value);
  657. b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGLO,
  658. (ringbase & 0xFFFFFFFF));
  659. b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGHI,
  660. ((ringbase >> 32) &
  661. ~SSB_DMA_TRANSLATION_MASK) |
  662. trans);
  663. b43legacy_dma_write(ring, B43legacy_DMA64_RXINDEX,
  664. 200);
  665. } else {
  666. u32 ringbase = (u32)(ring->dmabase);
  667. addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
  668. >> SSB_DMA_TRANSLATION_SHIFT;
  669. value = (ring->frameoffset <<
  670. B43legacy_DMA32_RXFROFF_SHIFT);
  671. value |= B43legacy_DMA32_RXENABLE;
  672. value |= (addrext <<
  673. B43legacy_DMA32_RXADDREXT_SHIFT)
  674. & B43legacy_DMA32_RXADDREXT_MASK;
  675. b43legacy_dma_write(ring, B43legacy_DMA32_RXCTL,
  676. value);
  677. b43legacy_dma_write(ring, B43legacy_DMA32_RXRING,
  678. (ringbase &
  679. ~SSB_DMA_TRANSLATION_MASK)
  680. | trans);
  681. b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX,
  682. 200);
  683. }
  684. }
  685. out:
  686. return err;
  687. }
  688. /* Shutdown the DMA controller. */
  689. static void dmacontroller_cleanup(struct b43legacy_dmaring *ring)
  690. {
  691. if (ring->tx) {
  692. b43legacy_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
  693. ring->type);
  694. if (ring->type == B43legacy_DMA_64BIT) {
  695. b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGLO, 0);
  696. b43legacy_dma_write(ring, B43legacy_DMA64_TXRINGHI, 0);
  697. } else
  698. b43legacy_dma_write(ring, B43legacy_DMA32_TXRING, 0);
  699. } else {
  700. b43legacy_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
  701. ring->type);
  702. if (ring->type == B43legacy_DMA_64BIT) {
  703. b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGLO, 0);
  704. b43legacy_dma_write(ring, B43legacy_DMA64_RXRINGHI, 0);
  705. } else
  706. b43legacy_dma_write(ring, B43legacy_DMA32_RXRING, 0);
  707. }
  708. }
  709. static void free_all_descbuffers(struct b43legacy_dmaring *ring)
  710. {
  711. struct b43legacy_dmadesc_generic *desc;
  712. struct b43legacy_dmadesc_meta *meta;
  713. int i;
  714. if (!ring->used_slots)
  715. return;
  716. for (i = 0; i < ring->nr_slots; i++) {
  717. desc = ring->ops->idx2desc(ring, i, &meta);
  718. if (!meta->skb) {
  719. B43legacy_WARN_ON(!ring->tx);
  720. continue;
  721. }
  722. if (ring->tx)
  723. unmap_descbuffer(ring, meta->dmaaddr,
  724. meta->skb->len, 1);
  725. else
  726. unmap_descbuffer(ring, meta->dmaaddr,
  727. ring->rx_buffersize, 0);
  728. free_descriptor_buffer(ring, meta, 0);
  729. }
  730. }
  731. static u64 supported_dma_mask(struct b43legacy_wldev *dev)
  732. {
  733. u32 tmp;
  734. u16 mmio_base;
  735. tmp = b43legacy_read32(dev, SSB_TMSHIGH);
  736. if (tmp & SSB_TMSHIGH_DMA64)
  737. return DMA_BIT_MASK(64);
  738. mmio_base = b43legacy_dmacontroller_base(0, 0);
  739. b43legacy_write32(dev,
  740. mmio_base + B43legacy_DMA32_TXCTL,
  741. B43legacy_DMA32_TXADDREXT_MASK);
  742. tmp = b43legacy_read32(dev, mmio_base +
  743. B43legacy_DMA32_TXCTL);
  744. if (tmp & B43legacy_DMA32_TXADDREXT_MASK)
  745. return DMA_BIT_MASK(32);
  746. return DMA_BIT_MASK(30);
  747. }
  748. static enum b43legacy_dmatype dma_mask_to_engine_type(u64 dmamask)
  749. {
  750. if (dmamask == DMA_BIT_MASK(30))
  751. return B43legacy_DMA_30BIT;
  752. if (dmamask == DMA_BIT_MASK(32))
  753. return B43legacy_DMA_32BIT;
  754. if (dmamask == DMA_BIT_MASK(64))
  755. return B43legacy_DMA_64BIT;
  756. B43legacy_WARN_ON(1);
  757. return B43legacy_DMA_30BIT;
  758. }
  759. /* Main initialization function. */
  760. static
  761. struct b43legacy_dmaring *b43legacy_setup_dmaring(struct b43legacy_wldev *dev,
  762. int controller_index,
  763. int for_tx,
  764. enum b43legacy_dmatype type)
  765. {
  766. struct b43legacy_dmaring *ring;
  767. int err;
  768. int nr_slots;
  769. dma_addr_t dma_test;
  770. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  771. if (!ring)
  772. goto out;
  773. ring->type = type;
  774. ring->dev = dev;
  775. nr_slots = B43legacy_RXRING_SLOTS;
  776. if (for_tx)
  777. nr_slots = B43legacy_TXRING_SLOTS;
  778. ring->meta = kcalloc(nr_slots, sizeof(struct b43legacy_dmadesc_meta),
  779. GFP_KERNEL);
  780. if (!ring->meta)
  781. goto err_kfree_ring;
  782. if (for_tx) {
  783. ring->txhdr_cache = kcalloc(nr_slots,
  784. sizeof(struct b43legacy_txhdr_fw3),
  785. GFP_KERNEL);
  786. if (!ring->txhdr_cache)
  787. goto err_kfree_meta;
  788. /* test for ability to dma to txhdr_cache */
  789. dma_test = ssb_dma_map_single(dev->dev, ring->txhdr_cache,
  790. sizeof(struct b43legacy_txhdr_fw3),
  791. DMA_TO_DEVICE);
  792. if (b43legacy_dma_mapping_error(ring, dma_test,
  793. sizeof(struct b43legacy_txhdr_fw3), 1)) {
  794. /* ugh realloc */
  795. kfree(ring->txhdr_cache);
  796. ring->txhdr_cache = kcalloc(nr_slots,
  797. sizeof(struct b43legacy_txhdr_fw3),
  798. GFP_KERNEL | GFP_DMA);
  799. if (!ring->txhdr_cache)
  800. goto err_kfree_meta;
  801. dma_test = ssb_dma_map_single(dev->dev,
  802. ring->txhdr_cache,
  803. sizeof(struct b43legacy_txhdr_fw3),
  804. DMA_TO_DEVICE);
  805. if (b43legacy_dma_mapping_error(ring, dma_test,
  806. sizeof(struct b43legacy_txhdr_fw3), 1))
  807. goto err_kfree_txhdr_cache;
  808. }
  809. ssb_dma_unmap_single(dev->dev, dma_test,
  810. sizeof(struct b43legacy_txhdr_fw3),
  811. DMA_TO_DEVICE);
  812. }
  813. ring->nr_slots = nr_slots;
  814. ring->mmio_base = b43legacy_dmacontroller_base(type, controller_index);
  815. ring->index = controller_index;
  816. if (type == B43legacy_DMA_64BIT)
  817. ring->ops = &dma64_ops;
  818. else
  819. ring->ops = &dma32_ops;
  820. if (for_tx) {
  821. ring->tx = 1;
  822. ring->current_slot = -1;
  823. } else {
  824. if (ring->index == 0) {
  825. ring->rx_buffersize = B43legacy_DMA0_RX_BUFFERSIZE;
  826. ring->frameoffset = B43legacy_DMA0_RX_FRAMEOFFSET;
  827. } else if (ring->index == 3) {
  828. ring->rx_buffersize = B43legacy_DMA3_RX_BUFFERSIZE;
  829. ring->frameoffset = B43legacy_DMA3_RX_FRAMEOFFSET;
  830. } else
  831. B43legacy_WARN_ON(1);
  832. }
  833. spin_lock_init(&ring->lock);
  834. #ifdef CONFIG_B43LEGACY_DEBUG
  835. ring->last_injected_overflow = jiffies;
  836. #endif
  837. err = alloc_ringmemory(ring);
  838. if (err)
  839. goto err_kfree_txhdr_cache;
  840. err = dmacontroller_setup(ring);
  841. if (err)
  842. goto err_free_ringmemory;
  843. out:
  844. return ring;
  845. err_free_ringmemory:
  846. free_ringmemory(ring);
  847. err_kfree_txhdr_cache:
  848. kfree(ring->txhdr_cache);
  849. err_kfree_meta:
  850. kfree(ring->meta);
  851. err_kfree_ring:
  852. kfree(ring);
  853. ring = NULL;
  854. goto out;
  855. }
  856. /* Main cleanup function. */
  857. static void b43legacy_destroy_dmaring(struct b43legacy_dmaring *ring)
  858. {
  859. if (!ring)
  860. return;
  861. b43legacydbg(ring->dev->wl, "DMA-%u 0x%04X (%s) max used slots:"
  862. " %d/%d\n", (unsigned int)(ring->type), ring->mmio_base,
  863. (ring->tx) ? "TX" : "RX", ring->max_used_slots,
  864. ring->nr_slots);
  865. /* Device IRQs are disabled prior entering this function,
  866. * so no need to take care of concurrency with rx handler stuff.
  867. */
  868. dmacontroller_cleanup(ring);
  869. free_all_descbuffers(ring);
  870. free_ringmemory(ring);
  871. kfree(ring->txhdr_cache);
  872. kfree(ring->meta);
  873. kfree(ring);
  874. }
  875. void b43legacy_dma_free(struct b43legacy_wldev *dev)
  876. {
  877. struct b43legacy_dma *dma;
  878. if (b43legacy_using_pio(dev))
  879. return;
  880. dma = &dev->dma;
  881. b43legacy_destroy_dmaring(dma->rx_ring3);
  882. dma->rx_ring3 = NULL;
  883. b43legacy_destroy_dmaring(dma->rx_ring0);
  884. dma->rx_ring0 = NULL;
  885. b43legacy_destroy_dmaring(dma->tx_ring5);
  886. dma->tx_ring5 = NULL;
  887. b43legacy_destroy_dmaring(dma->tx_ring4);
  888. dma->tx_ring4 = NULL;
  889. b43legacy_destroy_dmaring(dma->tx_ring3);
  890. dma->tx_ring3 = NULL;
  891. b43legacy_destroy_dmaring(dma->tx_ring2);
  892. dma->tx_ring2 = NULL;
  893. b43legacy_destroy_dmaring(dma->tx_ring1);
  894. dma->tx_ring1 = NULL;
  895. b43legacy_destroy_dmaring(dma->tx_ring0);
  896. dma->tx_ring0 = NULL;
  897. }
  898. static int b43legacy_dma_set_mask(struct b43legacy_wldev *dev, u64 mask)
  899. {
  900. u64 orig_mask = mask;
  901. bool fallback = 0;
  902. int err;
  903. /* Try to set the DMA mask. If it fails, try falling back to a
  904. * lower mask, as we can always also support a lower one. */
  905. while (1) {
  906. err = ssb_dma_set_mask(dev->dev, mask);
  907. if (!err)
  908. break;
  909. if (mask == DMA_BIT_MASK(64)) {
  910. mask = DMA_BIT_MASK(32);
  911. fallback = 1;
  912. continue;
  913. }
  914. if (mask == DMA_BIT_MASK(32)) {
  915. mask = DMA_BIT_MASK(30);
  916. fallback = 1;
  917. continue;
  918. }
  919. b43legacyerr(dev->wl, "The machine/kernel does not support "
  920. "the required %u-bit DMA mask\n",
  921. (unsigned int)dma_mask_to_engine_type(orig_mask));
  922. return -EOPNOTSUPP;
  923. }
  924. if (fallback) {
  925. b43legacyinfo(dev->wl, "DMA mask fallback from %u-bit to %u-"
  926. "bit\n",
  927. (unsigned int)dma_mask_to_engine_type(orig_mask),
  928. (unsigned int)dma_mask_to_engine_type(mask));
  929. }
  930. return 0;
  931. }
  932. int b43legacy_dma_init(struct b43legacy_wldev *dev)
  933. {
  934. struct b43legacy_dma *dma = &dev->dma;
  935. struct b43legacy_dmaring *ring;
  936. int err;
  937. u64 dmamask;
  938. enum b43legacy_dmatype type;
  939. dmamask = supported_dma_mask(dev);
  940. type = dma_mask_to_engine_type(dmamask);
  941. err = b43legacy_dma_set_mask(dev, dmamask);
  942. if (err) {
  943. #ifdef CONFIG_B43LEGACY_PIO
  944. b43legacywarn(dev->wl, "DMA for this device not supported. "
  945. "Falling back to PIO\n");
  946. dev->__using_pio = 1;
  947. return -EAGAIN;
  948. #else
  949. b43legacyerr(dev->wl, "DMA for this device not supported and "
  950. "no PIO support compiled in\n");
  951. return -EOPNOTSUPP;
  952. #endif
  953. }
  954. err = -ENOMEM;
  955. /* setup TX DMA channels. */
  956. ring = b43legacy_setup_dmaring(dev, 0, 1, type);
  957. if (!ring)
  958. goto out;
  959. dma->tx_ring0 = ring;
  960. ring = b43legacy_setup_dmaring(dev, 1, 1, type);
  961. if (!ring)
  962. goto err_destroy_tx0;
  963. dma->tx_ring1 = ring;
  964. ring = b43legacy_setup_dmaring(dev, 2, 1, type);
  965. if (!ring)
  966. goto err_destroy_tx1;
  967. dma->tx_ring2 = ring;
  968. ring = b43legacy_setup_dmaring(dev, 3, 1, type);
  969. if (!ring)
  970. goto err_destroy_tx2;
  971. dma->tx_ring3 = ring;
  972. ring = b43legacy_setup_dmaring(dev, 4, 1, type);
  973. if (!ring)
  974. goto err_destroy_tx3;
  975. dma->tx_ring4 = ring;
  976. ring = b43legacy_setup_dmaring(dev, 5, 1, type);
  977. if (!ring)
  978. goto err_destroy_tx4;
  979. dma->tx_ring5 = ring;
  980. /* setup RX DMA channels. */
  981. ring = b43legacy_setup_dmaring(dev, 0, 0, type);
  982. if (!ring)
  983. goto err_destroy_tx5;
  984. dma->rx_ring0 = ring;
  985. if (dev->dev->id.revision < 5) {
  986. ring = b43legacy_setup_dmaring(dev, 3, 0, type);
  987. if (!ring)
  988. goto err_destroy_rx0;
  989. dma->rx_ring3 = ring;
  990. }
  991. b43legacydbg(dev->wl, "%u-bit DMA initialized\n", (unsigned int)type);
  992. err = 0;
  993. out:
  994. return err;
  995. err_destroy_rx0:
  996. b43legacy_destroy_dmaring(dma->rx_ring0);
  997. dma->rx_ring0 = NULL;
  998. err_destroy_tx5:
  999. b43legacy_destroy_dmaring(dma->tx_ring5);
  1000. dma->tx_ring5 = NULL;
  1001. err_destroy_tx4:
  1002. b43legacy_destroy_dmaring(dma->tx_ring4);
  1003. dma->tx_ring4 = NULL;
  1004. err_destroy_tx3:
  1005. b43legacy_destroy_dmaring(dma->tx_ring3);
  1006. dma->tx_ring3 = NULL;
  1007. err_destroy_tx2:
  1008. b43legacy_destroy_dmaring(dma->tx_ring2);
  1009. dma->tx_ring2 = NULL;
  1010. err_destroy_tx1:
  1011. b43legacy_destroy_dmaring(dma->tx_ring1);
  1012. dma->tx_ring1 = NULL;
  1013. err_destroy_tx0:
  1014. b43legacy_destroy_dmaring(dma->tx_ring0);
  1015. dma->tx_ring0 = NULL;
  1016. goto out;
  1017. }
  1018. /* Generate a cookie for the TX header. */
  1019. static u16 generate_cookie(struct b43legacy_dmaring *ring,
  1020. int slot)
  1021. {
  1022. u16 cookie = 0x1000;
  1023. /* Use the upper 4 bits of the cookie as
  1024. * DMA controller ID and store the slot number
  1025. * in the lower 12 bits.
  1026. * Note that the cookie must never be 0, as this
  1027. * is a special value used in RX path.
  1028. */
  1029. switch (ring->index) {
  1030. case 0:
  1031. cookie = 0xA000;
  1032. break;
  1033. case 1:
  1034. cookie = 0xB000;
  1035. break;
  1036. case 2:
  1037. cookie = 0xC000;
  1038. break;
  1039. case 3:
  1040. cookie = 0xD000;
  1041. break;
  1042. case 4:
  1043. cookie = 0xE000;
  1044. break;
  1045. case 5:
  1046. cookie = 0xF000;
  1047. break;
  1048. }
  1049. B43legacy_WARN_ON(!(((u16)slot & 0xF000) == 0x0000));
  1050. cookie |= (u16)slot;
  1051. return cookie;
  1052. }
  1053. /* Inspect a cookie and find out to which controller/slot it belongs. */
  1054. static
  1055. struct b43legacy_dmaring *parse_cookie(struct b43legacy_wldev *dev,
  1056. u16 cookie, int *slot)
  1057. {
  1058. struct b43legacy_dma *dma = &dev->dma;
  1059. struct b43legacy_dmaring *ring = NULL;
  1060. switch (cookie & 0xF000) {
  1061. case 0xA000:
  1062. ring = dma->tx_ring0;
  1063. break;
  1064. case 0xB000:
  1065. ring = dma->tx_ring1;
  1066. break;
  1067. case 0xC000:
  1068. ring = dma->tx_ring2;
  1069. break;
  1070. case 0xD000:
  1071. ring = dma->tx_ring3;
  1072. break;
  1073. case 0xE000:
  1074. ring = dma->tx_ring4;
  1075. break;
  1076. case 0xF000:
  1077. ring = dma->tx_ring5;
  1078. break;
  1079. default:
  1080. B43legacy_WARN_ON(1);
  1081. }
  1082. *slot = (cookie & 0x0FFF);
  1083. B43legacy_WARN_ON(!(ring && *slot >= 0 && *slot < ring->nr_slots));
  1084. return ring;
  1085. }
  1086. static int dma_tx_fragment(struct b43legacy_dmaring *ring,
  1087. struct sk_buff *skb)
  1088. {
  1089. const struct b43legacy_dma_ops *ops = ring->ops;
  1090. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1091. u8 *header;
  1092. int slot, old_top_slot, old_used_slots;
  1093. int err;
  1094. struct b43legacy_dmadesc_generic *desc;
  1095. struct b43legacy_dmadesc_meta *meta;
  1096. struct b43legacy_dmadesc_meta *meta_hdr;
  1097. struct sk_buff *bounce_skb;
  1098. #define SLOTS_PER_PACKET 2
  1099. B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags != 0);
  1100. old_top_slot = ring->current_slot;
  1101. old_used_slots = ring->used_slots;
  1102. /* Get a slot for the header. */
  1103. slot = request_slot(ring);
  1104. desc = ops->idx2desc(ring, slot, &meta_hdr);
  1105. memset(meta_hdr, 0, sizeof(*meta_hdr));
  1106. header = &(ring->txhdr_cache[slot * sizeof(
  1107. struct b43legacy_txhdr_fw3)]);
  1108. err = b43legacy_generate_txhdr(ring->dev, header,
  1109. skb->data, skb->len, info,
  1110. generate_cookie(ring, slot));
  1111. if (unlikely(err)) {
  1112. ring->current_slot = old_top_slot;
  1113. ring->used_slots = old_used_slots;
  1114. return err;
  1115. }
  1116. meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
  1117. sizeof(struct b43legacy_txhdr_fw3), 1);
  1118. if (b43legacy_dma_mapping_error(ring, meta_hdr->dmaaddr,
  1119. sizeof(struct b43legacy_txhdr_fw3), 1)) {
  1120. ring->current_slot = old_top_slot;
  1121. ring->used_slots = old_used_slots;
  1122. return -EIO;
  1123. }
  1124. ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr,
  1125. sizeof(struct b43legacy_txhdr_fw3), 1, 0, 0);
  1126. /* Get a slot for the payload. */
  1127. slot = request_slot(ring);
  1128. desc = ops->idx2desc(ring, slot, &meta);
  1129. memset(meta, 0, sizeof(*meta));
  1130. meta->skb = skb;
  1131. meta->is_last_fragment = 1;
  1132. meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
  1133. /* create a bounce buffer in zone_dma on mapping failure. */
  1134. if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
  1135. bounce_skb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
  1136. if (!bounce_skb) {
  1137. ring->current_slot = old_top_slot;
  1138. ring->used_slots = old_used_slots;
  1139. err = -ENOMEM;
  1140. goto out_unmap_hdr;
  1141. }
  1142. memcpy(skb_put(bounce_skb, skb->len), skb->data, skb->len);
  1143. dev_kfree_skb_any(skb);
  1144. skb = bounce_skb;
  1145. meta->skb = skb;
  1146. meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
  1147. if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
  1148. ring->current_slot = old_top_slot;
  1149. ring->used_slots = old_used_slots;
  1150. err = -EIO;
  1151. goto out_free_bounce;
  1152. }
  1153. }
  1154. ops->fill_descriptor(ring, desc, meta->dmaaddr,
  1155. skb->len, 0, 1, 1);
  1156. wmb(); /* previous stuff MUST be done */
  1157. /* Now transfer the whole frame. */
  1158. ops->poke_tx(ring, next_slot(ring, slot));
  1159. return 0;
  1160. out_free_bounce:
  1161. dev_kfree_skb_any(skb);
  1162. out_unmap_hdr:
  1163. unmap_descbuffer(ring, meta_hdr->dmaaddr,
  1164. sizeof(struct b43legacy_txhdr_fw3), 1);
  1165. return err;
  1166. }
  1167. static inline
  1168. int should_inject_overflow(struct b43legacy_dmaring *ring)
  1169. {
  1170. #ifdef CONFIG_B43LEGACY_DEBUG
  1171. if (unlikely(b43legacy_debug(ring->dev,
  1172. B43legacy_DBG_DMAOVERFLOW))) {
  1173. /* Check if we should inject another ringbuffer overflow
  1174. * to test handling of this situation in the stack. */
  1175. unsigned long next_overflow;
  1176. next_overflow = ring->last_injected_overflow + HZ;
  1177. if (time_after(jiffies, next_overflow)) {
  1178. ring->last_injected_overflow = jiffies;
  1179. b43legacydbg(ring->dev->wl,
  1180. "Injecting TX ring overflow on "
  1181. "DMA controller %d\n", ring->index);
  1182. return 1;
  1183. }
  1184. }
  1185. #endif /* CONFIG_B43LEGACY_DEBUG */
  1186. return 0;
  1187. }
  1188. int b43legacy_dma_tx(struct b43legacy_wldev *dev,
  1189. struct sk_buff *skb)
  1190. {
  1191. struct b43legacy_dmaring *ring;
  1192. int err = 0;
  1193. unsigned long flags;
  1194. ring = priority_to_txring(dev, skb_get_queue_mapping(skb));
  1195. spin_lock_irqsave(&ring->lock, flags);
  1196. B43legacy_WARN_ON(!ring->tx);
  1197. if (unlikely(free_slots(ring) < SLOTS_PER_PACKET)) {
  1198. b43legacywarn(dev->wl, "DMA queue overflow\n");
  1199. err = -ENOSPC;
  1200. goto out_unlock;
  1201. }
  1202. /* Check if the queue was stopped in mac80211,
  1203. * but we got called nevertheless.
  1204. * That would be a mac80211 bug. */
  1205. B43legacy_BUG_ON(ring->stopped);
  1206. err = dma_tx_fragment(ring, skb);
  1207. if (unlikely(err == -ENOKEY)) {
  1208. /* Drop this packet, as we don't have the encryption key
  1209. * anymore and must not transmit it unencrypted. */
  1210. dev_kfree_skb_any(skb);
  1211. err = 0;
  1212. goto out_unlock;
  1213. }
  1214. if (unlikely(err)) {
  1215. b43legacyerr(dev->wl, "DMA tx mapping failure\n");
  1216. goto out_unlock;
  1217. }
  1218. ring->nr_tx_packets++;
  1219. if ((free_slots(ring) < SLOTS_PER_PACKET) ||
  1220. should_inject_overflow(ring)) {
  1221. /* This TX ring is full. */
  1222. ieee80211_stop_queue(dev->wl->hw, txring_to_priority(ring));
  1223. ring->stopped = 1;
  1224. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1225. b43legacydbg(dev->wl, "Stopped TX ring %d\n",
  1226. ring->index);
  1227. }
  1228. out_unlock:
  1229. spin_unlock_irqrestore(&ring->lock, flags);
  1230. return err;
  1231. }
  1232. void b43legacy_dma_handle_txstatus(struct b43legacy_wldev *dev,
  1233. const struct b43legacy_txstatus *status)
  1234. {
  1235. const struct b43legacy_dma_ops *ops;
  1236. struct b43legacy_dmaring *ring;
  1237. struct b43legacy_dmadesc_generic *desc;
  1238. struct b43legacy_dmadesc_meta *meta;
  1239. int retry_limit;
  1240. int slot;
  1241. ring = parse_cookie(dev, status->cookie, &slot);
  1242. if (unlikely(!ring))
  1243. return;
  1244. B43legacy_WARN_ON(!irqs_disabled());
  1245. spin_lock(&ring->lock);
  1246. B43legacy_WARN_ON(!ring->tx);
  1247. ops = ring->ops;
  1248. while (1) {
  1249. B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  1250. desc = ops->idx2desc(ring, slot, &meta);
  1251. if (meta->skb)
  1252. unmap_descbuffer(ring, meta->dmaaddr,
  1253. meta->skb->len, 1);
  1254. else
  1255. unmap_descbuffer(ring, meta->dmaaddr,
  1256. sizeof(struct b43legacy_txhdr_fw3),
  1257. 1);
  1258. if (meta->is_last_fragment) {
  1259. struct ieee80211_tx_info *info;
  1260. BUG_ON(!meta->skb);
  1261. info = IEEE80211_SKB_CB(meta->skb);
  1262. /* preserve the confiured retry limit before clearing the status
  1263. * The xmit function has overwritten the rc's value with the actual
  1264. * retry limit done by the hardware */
  1265. retry_limit = info->status.rates[0].count;
  1266. ieee80211_tx_info_clear_status(info);
  1267. if (status->acked)
  1268. info->flags |= IEEE80211_TX_STAT_ACK;
  1269. if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  1270. /*
  1271. * If the short retries (RTS, not data frame) have exceeded
  1272. * the limit, the hw will not have tried the selected rate,
  1273. * but will have used the fallback rate instead.
  1274. * Don't let the rate control count attempts for the selected
  1275. * rate in this case, otherwise the statistics will be off.
  1276. */
  1277. info->status.rates[0].count = 0;
  1278. info->status.rates[1].count = status->frame_count;
  1279. } else {
  1280. if (status->frame_count > retry_limit) {
  1281. info->status.rates[0].count = retry_limit;
  1282. info->status.rates[1].count = status->frame_count -
  1283. retry_limit;
  1284. } else {
  1285. info->status.rates[0].count = status->frame_count;
  1286. info->status.rates[1].idx = -1;
  1287. }
  1288. }
  1289. /* Call back to inform the ieee80211 subsystem about the
  1290. * status of the transmission.
  1291. * Some fields of txstat are already filled in dma_tx().
  1292. */
  1293. ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb);
  1294. /* skb is freed by ieee80211_tx_status_irqsafe() */
  1295. meta->skb = NULL;
  1296. } else {
  1297. /* No need to call free_descriptor_buffer here, as
  1298. * this is only the txhdr, which is not allocated.
  1299. */
  1300. B43legacy_WARN_ON(meta->skb != NULL);
  1301. }
  1302. /* Everything unmapped and free'd. So it's not used anymore. */
  1303. ring->used_slots--;
  1304. if (meta->is_last_fragment)
  1305. break;
  1306. slot = next_slot(ring, slot);
  1307. }
  1308. dev->stats.last_tx = jiffies;
  1309. if (ring->stopped) {
  1310. B43legacy_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
  1311. ieee80211_wake_queue(dev->wl->hw, txring_to_priority(ring));
  1312. ring->stopped = 0;
  1313. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1314. b43legacydbg(dev->wl, "Woke up TX ring %d\n",
  1315. ring->index);
  1316. }
  1317. spin_unlock(&ring->lock);
  1318. }
  1319. void b43legacy_dma_get_tx_stats(struct b43legacy_wldev *dev,
  1320. struct ieee80211_tx_queue_stats *stats)
  1321. {
  1322. const int nr_queues = dev->wl->hw->queues;
  1323. struct b43legacy_dmaring *ring;
  1324. unsigned long flags;
  1325. int i;
  1326. for (i = 0; i < nr_queues; i++) {
  1327. ring = priority_to_txring(dev, i);
  1328. spin_lock_irqsave(&ring->lock, flags);
  1329. stats[i].len = ring->used_slots / SLOTS_PER_PACKET;
  1330. stats[i].limit = ring->nr_slots / SLOTS_PER_PACKET;
  1331. stats[i].count = ring->nr_tx_packets;
  1332. spin_unlock_irqrestore(&ring->lock, flags);
  1333. }
  1334. }
  1335. static void dma_rx(struct b43legacy_dmaring *ring,
  1336. int *slot)
  1337. {
  1338. const struct b43legacy_dma_ops *ops = ring->ops;
  1339. struct b43legacy_dmadesc_generic *desc;
  1340. struct b43legacy_dmadesc_meta *meta;
  1341. struct b43legacy_rxhdr_fw3 *rxhdr;
  1342. struct sk_buff *skb;
  1343. u16 len;
  1344. int err;
  1345. dma_addr_t dmaaddr;
  1346. desc = ops->idx2desc(ring, *slot, &meta);
  1347. sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
  1348. skb = meta->skb;
  1349. if (ring->index == 3) {
  1350. /* We received an xmit status. */
  1351. struct b43legacy_hwtxstatus *hw =
  1352. (struct b43legacy_hwtxstatus *)skb->data;
  1353. int i = 0;
  1354. while (hw->cookie == 0) {
  1355. if (i > 100)
  1356. break;
  1357. i++;
  1358. udelay(2);
  1359. barrier();
  1360. }
  1361. b43legacy_handle_hwtxstatus(ring->dev, hw);
  1362. /* recycle the descriptor buffer. */
  1363. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1364. ring->rx_buffersize);
  1365. return;
  1366. }
  1367. rxhdr = (struct b43legacy_rxhdr_fw3 *)skb->data;
  1368. len = le16_to_cpu(rxhdr->frame_len);
  1369. if (len == 0) {
  1370. int i = 0;
  1371. do {
  1372. udelay(2);
  1373. barrier();
  1374. len = le16_to_cpu(rxhdr->frame_len);
  1375. } while (len == 0 && i++ < 5);
  1376. if (unlikely(len == 0)) {
  1377. /* recycle the descriptor buffer. */
  1378. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1379. ring->rx_buffersize);
  1380. goto drop;
  1381. }
  1382. }
  1383. if (unlikely(len > ring->rx_buffersize)) {
  1384. /* The data did not fit into one descriptor buffer
  1385. * and is split over multiple buffers.
  1386. * This should never happen, as we try to allocate buffers
  1387. * big enough. So simply ignore this packet.
  1388. */
  1389. int cnt = 0;
  1390. s32 tmp = len;
  1391. while (1) {
  1392. desc = ops->idx2desc(ring, *slot, &meta);
  1393. /* recycle the descriptor buffer. */
  1394. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1395. ring->rx_buffersize);
  1396. *slot = next_slot(ring, *slot);
  1397. cnt++;
  1398. tmp -= ring->rx_buffersize;
  1399. if (tmp <= 0)
  1400. break;
  1401. }
  1402. b43legacyerr(ring->dev->wl, "DMA RX buffer too small "
  1403. "(len: %u, buffer: %u, nr-dropped: %d)\n",
  1404. len, ring->rx_buffersize, cnt);
  1405. goto drop;
  1406. }
  1407. dmaaddr = meta->dmaaddr;
  1408. err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
  1409. if (unlikely(err)) {
  1410. b43legacydbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer()"
  1411. " failed\n");
  1412. sync_descbuffer_for_device(ring, dmaaddr,
  1413. ring->rx_buffersize);
  1414. goto drop;
  1415. }
  1416. unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
  1417. skb_put(skb, len + ring->frameoffset);
  1418. skb_pull(skb, ring->frameoffset);
  1419. b43legacy_rx(ring->dev, skb, rxhdr);
  1420. drop:
  1421. return;
  1422. }
  1423. void b43legacy_dma_rx(struct b43legacy_dmaring *ring)
  1424. {
  1425. const struct b43legacy_dma_ops *ops = ring->ops;
  1426. int slot;
  1427. int current_slot;
  1428. int used_slots = 0;
  1429. B43legacy_WARN_ON(ring->tx);
  1430. current_slot = ops->get_current_rxslot(ring);
  1431. B43legacy_WARN_ON(!(current_slot >= 0 && current_slot <
  1432. ring->nr_slots));
  1433. slot = ring->current_slot;
  1434. for (; slot != current_slot; slot = next_slot(ring, slot)) {
  1435. dma_rx(ring, &slot);
  1436. update_max_used_slots(ring, ++used_slots);
  1437. }
  1438. ops->set_current_rxslot(ring, slot);
  1439. ring->current_slot = slot;
  1440. }
  1441. static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring *ring)
  1442. {
  1443. unsigned long flags;
  1444. spin_lock_irqsave(&ring->lock, flags);
  1445. B43legacy_WARN_ON(!ring->tx);
  1446. ring->ops->tx_suspend(ring);
  1447. spin_unlock_irqrestore(&ring->lock, flags);
  1448. }
  1449. static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring *ring)
  1450. {
  1451. unsigned long flags;
  1452. spin_lock_irqsave(&ring->lock, flags);
  1453. B43legacy_WARN_ON(!ring->tx);
  1454. ring->ops->tx_resume(ring);
  1455. spin_unlock_irqrestore(&ring->lock, flags);
  1456. }
  1457. void b43legacy_dma_tx_suspend(struct b43legacy_wldev *dev)
  1458. {
  1459. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  1460. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring0);
  1461. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring1);
  1462. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring2);
  1463. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring3);
  1464. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring4);
  1465. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring5);
  1466. }
  1467. void b43legacy_dma_tx_resume(struct b43legacy_wldev *dev)
  1468. {
  1469. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring5);
  1470. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring4);
  1471. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring3);
  1472. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring2);
  1473. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring1);
  1474. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring0);
  1475. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  1476. }