dma.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /*
  2. Broadcom B43 wireless driver
  3. DMA ringbuffer and descriptor allocation/management
  4. Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch>
  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 "b43.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 <linux/etherdevice.h>
  31. #include <linux/slab.h>
  32. #include <asm/div64.h>
  33. /* Required number of TX DMA slots per TX frame.
  34. * This currently is 2, because we put the header and the ieee80211 frame
  35. * into separate slots. */
  36. #define TX_SLOTS_PER_FRAME 2
  37. static u32 b43_dma_address(struct b43_dma *dma, dma_addr_t dmaaddr,
  38. enum b43_addrtype addrtype)
  39. {
  40. u32 uninitialized_var(addr);
  41. switch (addrtype) {
  42. case B43_DMA_ADDR_LOW:
  43. addr = lower_32_bits(dmaaddr);
  44. if (dma->translation_in_low) {
  45. addr &= ~SSB_DMA_TRANSLATION_MASK;
  46. addr |= dma->translation;
  47. }
  48. break;
  49. case B43_DMA_ADDR_HIGH:
  50. addr = upper_32_bits(dmaaddr);
  51. if (!dma->translation_in_low) {
  52. addr &= ~SSB_DMA_TRANSLATION_MASK;
  53. addr |= dma->translation;
  54. }
  55. break;
  56. case B43_DMA_ADDR_EXT:
  57. if (dma->translation_in_low)
  58. addr = lower_32_bits(dmaaddr);
  59. else
  60. addr = upper_32_bits(dmaaddr);
  61. addr &= SSB_DMA_TRANSLATION_MASK;
  62. addr >>= SSB_DMA_TRANSLATION_SHIFT;
  63. break;
  64. }
  65. return addr;
  66. }
  67. /* 32bit DMA ops. */
  68. static
  69. struct b43_dmadesc_generic *op32_idx2desc(struct b43_dmaring *ring,
  70. int slot,
  71. struct b43_dmadesc_meta **meta)
  72. {
  73. struct b43_dmadesc32 *desc;
  74. *meta = &(ring->meta[slot]);
  75. desc = ring->descbase;
  76. desc = &(desc[slot]);
  77. return (struct b43_dmadesc_generic *)desc;
  78. }
  79. static void op32_fill_descriptor(struct b43_dmaring *ring,
  80. struct b43_dmadesc_generic *desc,
  81. dma_addr_t dmaaddr, u16 bufsize,
  82. int start, int end, int irq)
  83. {
  84. struct b43_dmadesc32 *descbase = ring->descbase;
  85. int slot;
  86. u32 ctl;
  87. u32 addr;
  88. u32 addrext;
  89. slot = (int)(&(desc->dma32) - descbase);
  90. B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  91. addr = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_LOW);
  92. addrext = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_EXT);
  93. ctl = bufsize & B43_DMA32_DCTL_BYTECNT;
  94. if (slot == ring->nr_slots - 1)
  95. ctl |= B43_DMA32_DCTL_DTABLEEND;
  96. if (start)
  97. ctl |= B43_DMA32_DCTL_FRAMESTART;
  98. if (end)
  99. ctl |= B43_DMA32_DCTL_FRAMEEND;
  100. if (irq)
  101. ctl |= B43_DMA32_DCTL_IRQ;
  102. ctl |= (addrext << B43_DMA32_DCTL_ADDREXT_SHIFT)
  103. & B43_DMA32_DCTL_ADDREXT_MASK;
  104. desc->dma32.control = cpu_to_le32(ctl);
  105. desc->dma32.address = cpu_to_le32(addr);
  106. }
  107. static void op32_poke_tx(struct b43_dmaring *ring, int slot)
  108. {
  109. b43_dma_write(ring, B43_DMA32_TXINDEX,
  110. (u32) (slot * sizeof(struct b43_dmadesc32)));
  111. }
  112. static void op32_tx_suspend(struct b43_dmaring *ring)
  113. {
  114. b43_dma_write(ring, B43_DMA32_TXCTL, b43_dma_read(ring, B43_DMA32_TXCTL)
  115. | B43_DMA32_TXSUSPEND);
  116. }
  117. static void op32_tx_resume(struct b43_dmaring *ring)
  118. {
  119. b43_dma_write(ring, B43_DMA32_TXCTL, b43_dma_read(ring, B43_DMA32_TXCTL)
  120. & ~B43_DMA32_TXSUSPEND);
  121. }
  122. static int op32_get_current_rxslot(struct b43_dmaring *ring)
  123. {
  124. u32 val;
  125. val = b43_dma_read(ring, B43_DMA32_RXSTATUS);
  126. val &= B43_DMA32_RXDPTR;
  127. return (val / sizeof(struct b43_dmadesc32));
  128. }
  129. static void op32_set_current_rxslot(struct b43_dmaring *ring, int slot)
  130. {
  131. b43_dma_write(ring, B43_DMA32_RXINDEX,
  132. (u32) (slot * sizeof(struct b43_dmadesc32)));
  133. }
  134. static const struct b43_dma_ops dma32_ops = {
  135. .idx2desc = op32_idx2desc,
  136. .fill_descriptor = op32_fill_descriptor,
  137. .poke_tx = op32_poke_tx,
  138. .tx_suspend = op32_tx_suspend,
  139. .tx_resume = op32_tx_resume,
  140. .get_current_rxslot = op32_get_current_rxslot,
  141. .set_current_rxslot = op32_set_current_rxslot,
  142. };
  143. /* 64bit DMA ops. */
  144. static
  145. struct b43_dmadesc_generic *op64_idx2desc(struct b43_dmaring *ring,
  146. int slot,
  147. struct b43_dmadesc_meta **meta)
  148. {
  149. struct b43_dmadesc64 *desc;
  150. *meta = &(ring->meta[slot]);
  151. desc = ring->descbase;
  152. desc = &(desc[slot]);
  153. return (struct b43_dmadesc_generic *)desc;
  154. }
  155. static void op64_fill_descriptor(struct b43_dmaring *ring,
  156. struct b43_dmadesc_generic *desc,
  157. dma_addr_t dmaaddr, u16 bufsize,
  158. int start, int end, int irq)
  159. {
  160. struct b43_dmadesc64 *descbase = ring->descbase;
  161. int slot;
  162. u32 ctl0 = 0, ctl1 = 0;
  163. u32 addrlo, addrhi;
  164. u32 addrext;
  165. slot = (int)(&(desc->dma64) - descbase);
  166. B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  167. addrlo = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_LOW);
  168. addrhi = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_HIGH);
  169. addrext = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_EXT);
  170. if (slot == ring->nr_slots - 1)
  171. ctl0 |= B43_DMA64_DCTL0_DTABLEEND;
  172. if (start)
  173. ctl0 |= B43_DMA64_DCTL0_FRAMESTART;
  174. if (end)
  175. ctl0 |= B43_DMA64_DCTL0_FRAMEEND;
  176. if (irq)
  177. ctl0 |= B43_DMA64_DCTL0_IRQ;
  178. ctl1 |= bufsize & B43_DMA64_DCTL1_BYTECNT;
  179. ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT)
  180. & B43_DMA64_DCTL1_ADDREXT_MASK;
  181. desc->dma64.control0 = cpu_to_le32(ctl0);
  182. desc->dma64.control1 = cpu_to_le32(ctl1);
  183. desc->dma64.address_low = cpu_to_le32(addrlo);
  184. desc->dma64.address_high = cpu_to_le32(addrhi);
  185. }
  186. static void op64_poke_tx(struct b43_dmaring *ring, int slot)
  187. {
  188. b43_dma_write(ring, B43_DMA64_TXINDEX,
  189. (u32) (slot * sizeof(struct b43_dmadesc64)));
  190. }
  191. static void op64_tx_suspend(struct b43_dmaring *ring)
  192. {
  193. b43_dma_write(ring, B43_DMA64_TXCTL, b43_dma_read(ring, B43_DMA64_TXCTL)
  194. | B43_DMA64_TXSUSPEND);
  195. }
  196. static void op64_tx_resume(struct b43_dmaring *ring)
  197. {
  198. b43_dma_write(ring, B43_DMA64_TXCTL, b43_dma_read(ring, B43_DMA64_TXCTL)
  199. & ~B43_DMA64_TXSUSPEND);
  200. }
  201. static int op64_get_current_rxslot(struct b43_dmaring *ring)
  202. {
  203. u32 val;
  204. val = b43_dma_read(ring, B43_DMA64_RXSTATUS);
  205. val &= B43_DMA64_RXSTATDPTR;
  206. return (val / sizeof(struct b43_dmadesc64));
  207. }
  208. static void op64_set_current_rxslot(struct b43_dmaring *ring, int slot)
  209. {
  210. b43_dma_write(ring, B43_DMA64_RXINDEX,
  211. (u32) (slot * sizeof(struct b43_dmadesc64)));
  212. }
  213. static const struct b43_dma_ops dma64_ops = {
  214. .idx2desc = op64_idx2desc,
  215. .fill_descriptor = op64_fill_descriptor,
  216. .poke_tx = op64_poke_tx,
  217. .tx_suspend = op64_tx_suspend,
  218. .tx_resume = op64_tx_resume,
  219. .get_current_rxslot = op64_get_current_rxslot,
  220. .set_current_rxslot = op64_set_current_rxslot,
  221. };
  222. static inline int free_slots(struct b43_dmaring *ring)
  223. {
  224. return (ring->nr_slots - ring->used_slots);
  225. }
  226. static inline int next_slot(struct b43_dmaring *ring, int slot)
  227. {
  228. B43_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1));
  229. if (slot == ring->nr_slots - 1)
  230. return 0;
  231. return slot + 1;
  232. }
  233. static inline int prev_slot(struct b43_dmaring *ring, int slot)
  234. {
  235. B43_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1));
  236. if (slot == 0)
  237. return ring->nr_slots - 1;
  238. return slot - 1;
  239. }
  240. #ifdef CONFIG_B43_DEBUG
  241. static void update_max_used_slots(struct b43_dmaring *ring,
  242. int current_used_slots)
  243. {
  244. if (current_used_slots <= ring->max_used_slots)
  245. return;
  246. ring->max_used_slots = current_used_slots;
  247. if (b43_debug(ring->dev, B43_DBG_DMAVERBOSE)) {
  248. b43dbg(ring->dev->wl,
  249. "max_used_slots increased to %d on %s ring %d\n",
  250. ring->max_used_slots,
  251. ring->tx ? "TX" : "RX", ring->index);
  252. }
  253. }
  254. #else
  255. static inline
  256. void update_max_used_slots(struct b43_dmaring *ring, int current_used_slots)
  257. {
  258. }
  259. #endif /* DEBUG */
  260. /* Request a slot for usage. */
  261. static inline int request_slot(struct b43_dmaring *ring)
  262. {
  263. int slot;
  264. B43_WARN_ON(!ring->tx);
  265. B43_WARN_ON(ring->stopped);
  266. B43_WARN_ON(free_slots(ring) == 0);
  267. slot = next_slot(ring, ring->current_slot);
  268. ring->current_slot = slot;
  269. ring->used_slots++;
  270. update_max_used_slots(ring, ring->used_slots);
  271. return slot;
  272. }
  273. static u16 b43_dmacontroller_base(enum b43_dmatype type, int controller_idx)
  274. {
  275. static const u16 map64[] = {
  276. B43_MMIO_DMA64_BASE0,
  277. B43_MMIO_DMA64_BASE1,
  278. B43_MMIO_DMA64_BASE2,
  279. B43_MMIO_DMA64_BASE3,
  280. B43_MMIO_DMA64_BASE4,
  281. B43_MMIO_DMA64_BASE5,
  282. };
  283. static const u16 map32[] = {
  284. B43_MMIO_DMA32_BASE0,
  285. B43_MMIO_DMA32_BASE1,
  286. B43_MMIO_DMA32_BASE2,
  287. B43_MMIO_DMA32_BASE3,
  288. B43_MMIO_DMA32_BASE4,
  289. B43_MMIO_DMA32_BASE5,
  290. };
  291. if (type == B43_DMA_64BIT) {
  292. B43_WARN_ON(!(controller_idx >= 0 &&
  293. controller_idx < ARRAY_SIZE(map64)));
  294. return map64[controller_idx];
  295. }
  296. B43_WARN_ON(!(controller_idx >= 0 &&
  297. controller_idx < ARRAY_SIZE(map32)));
  298. return map32[controller_idx];
  299. }
  300. static inline
  301. dma_addr_t map_descbuffer(struct b43_dmaring *ring,
  302. unsigned char *buf, size_t len, int tx)
  303. {
  304. dma_addr_t dmaaddr;
  305. if (tx) {
  306. dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
  307. buf, len, DMA_TO_DEVICE);
  308. } else {
  309. dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
  310. buf, len, DMA_FROM_DEVICE);
  311. }
  312. return dmaaddr;
  313. }
  314. static inline
  315. void unmap_descbuffer(struct b43_dmaring *ring,
  316. dma_addr_t addr, size_t len, int tx)
  317. {
  318. if (tx) {
  319. dma_unmap_single(ring->dev->dev->dma_dev,
  320. addr, len, DMA_TO_DEVICE);
  321. } else {
  322. dma_unmap_single(ring->dev->dev->dma_dev,
  323. addr, len, DMA_FROM_DEVICE);
  324. }
  325. }
  326. static inline
  327. void sync_descbuffer_for_cpu(struct b43_dmaring *ring,
  328. dma_addr_t addr, size_t len)
  329. {
  330. B43_WARN_ON(ring->tx);
  331. dma_sync_single_for_cpu(ring->dev->dev->dma_dev,
  332. addr, len, DMA_FROM_DEVICE);
  333. }
  334. static inline
  335. void sync_descbuffer_for_device(struct b43_dmaring *ring,
  336. dma_addr_t addr, size_t len)
  337. {
  338. B43_WARN_ON(ring->tx);
  339. dma_sync_single_for_device(ring->dev->dev->dma_dev,
  340. addr, len, DMA_FROM_DEVICE);
  341. }
  342. static inline
  343. void free_descriptor_buffer(struct b43_dmaring *ring,
  344. struct b43_dmadesc_meta *meta)
  345. {
  346. if (meta->skb) {
  347. if (ring->tx)
  348. ieee80211_free_txskb(ring->dev->wl->hw, meta->skb);
  349. else
  350. dev_kfree_skb_any(meta->skb);
  351. meta->skb = NULL;
  352. }
  353. }
  354. static int alloc_ringmemory(struct b43_dmaring *ring)
  355. {
  356. gfp_t flags = GFP_KERNEL;
  357. /* The specs call for 4K buffers for 30- and 32-bit DMA with 4K
  358. * alignment and 8K buffers for 64-bit DMA with 8K alignment.
  359. * In practice we could use smaller buffers for the latter, but the
  360. * alignment is really important because of the hardware bug. If bit
  361. * 0x00001000 is used in DMA address, some hardware (like BCM4331)
  362. * copies that bit into B43_DMA64_RXSTATUS and we get false values from
  363. * B43_DMA64_RXSTATDPTR. Let's just use 8K buffers even if we don't use
  364. * more than 256 slots for ring.
  365. */
  366. u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ?
  367. B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE;
  368. ring->descbase = dma_alloc_coherent(ring->dev->dev->dma_dev,
  369. ring_mem_size, &(ring->dmabase),
  370. flags);
  371. if (!ring->descbase) {
  372. b43err(ring->dev->wl, "DMA ringmemory allocation failed\n");
  373. return -ENOMEM;
  374. }
  375. memset(ring->descbase, 0, ring_mem_size);
  376. return 0;
  377. }
  378. static void free_ringmemory(struct b43_dmaring *ring)
  379. {
  380. u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ?
  381. B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE;
  382. dma_free_coherent(ring->dev->dev->dma_dev, ring_mem_size,
  383. ring->descbase, ring->dmabase);
  384. }
  385. /* Reset the RX DMA channel */
  386. static int b43_dmacontroller_rx_reset(struct b43_wldev *dev, u16 mmio_base,
  387. enum b43_dmatype type)
  388. {
  389. int i;
  390. u32 value;
  391. u16 offset;
  392. might_sleep();
  393. offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXCTL : B43_DMA32_RXCTL;
  394. b43_write32(dev, mmio_base + offset, 0);
  395. for (i = 0; i < 10; i++) {
  396. offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXSTATUS :
  397. B43_DMA32_RXSTATUS;
  398. value = b43_read32(dev, mmio_base + offset);
  399. if (type == B43_DMA_64BIT) {
  400. value &= B43_DMA64_RXSTAT;
  401. if (value == B43_DMA64_RXSTAT_DISABLED) {
  402. i = -1;
  403. break;
  404. }
  405. } else {
  406. value &= B43_DMA32_RXSTATE;
  407. if (value == B43_DMA32_RXSTAT_DISABLED) {
  408. i = -1;
  409. break;
  410. }
  411. }
  412. msleep(1);
  413. }
  414. if (i != -1) {
  415. b43err(dev->wl, "DMA RX reset timed out\n");
  416. return -ENODEV;
  417. }
  418. return 0;
  419. }
  420. /* Reset the TX DMA channel */
  421. static int b43_dmacontroller_tx_reset(struct b43_wldev *dev, u16 mmio_base,
  422. enum b43_dmatype type)
  423. {
  424. int i;
  425. u32 value;
  426. u16 offset;
  427. might_sleep();
  428. for (i = 0; i < 10; i++) {
  429. offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS :
  430. B43_DMA32_TXSTATUS;
  431. value = b43_read32(dev, mmio_base + offset);
  432. if (type == B43_DMA_64BIT) {
  433. value &= B43_DMA64_TXSTAT;
  434. if (value == B43_DMA64_TXSTAT_DISABLED ||
  435. value == B43_DMA64_TXSTAT_IDLEWAIT ||
  436. value == B43_DMA64_TXSTAT_STOPPED)
  437. break;
  438. } else {
  439. value &= B43_DMA32_TXSTATE;
  440. if (value == B43_DMA32_TXSTAT_DISABLED ||
  441. value == B43_DMA32_TXSTAT_IDLEWAIT ||
  442. value == B43_DMA32_TXSTAT_STOPPED)
  443. break;
  444. }
  445. msleep(1);
  446. }
  447. offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXCTL : B43_DMA32_TXCTL;
  448. b43_write32(dev, mmio_base + offset, 0);
  449. for (i = 0; i < 10; i++) {
  450. offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS :
  451. B43_DMA32_TXSTATUS;
  452. value = b43_read32(dev, mmio_base + offset);
  453. if (type == B43_DMA_64BIT) {
  454. value &= B43_DMA64_TXSTAT;
  455. if (value == B43_DMA64_TXSTAT_DISABLED) {
  456. i = -1;
  457. break;
  458. }
  459. } else {
  460. value &= B43_DMA32_TXSTATE;
  461. if (value == B43_DMA32_TXSTAT_DISABLED) {
  462. i = -1;
  463. break;
  464. }
  465. }
  466. msleep(1);
  467. }
  468. if (i != -1) {
  469. b43err(dev->wl, "DMA TX reset timed out\n");
  470. return -ENODEV;
  471. }
  472. /* ensure the reset is completed. */
  473. msleep(1);
  474. return 0;
  475. }
  476. /* Check if a DMA mapping address is invalid. */
  477. static bool b43_dma_mapping_error(struct b43_dmaring *ring,
  478. dma_addr_t addr,
  479. size_t buffersize, bool dma_to_device)
  480. {
  481. if (unlikely(dma_mapping_error(ring->dev->dev->dma_dev, addr)))
  482. return 1;
  483. switch (ring->type) {
  484. case B43_DMA_30BIT:
  485. if ((u64)addr + buffersize > (1ULL << 30))
  486. goto address_error;
  487. break;
  488. case B43_DMA_32BIT:
  489. if ((u64)addr + buffersize > (1ULL << 32))
  490. goto address_error;
  491. break;
  492. case B43_DMA_64BIT:
  493. /* Currently we can't have addresses beyond
  494. * 64bit in the kernel. */
  495. break;
  496. }
  497. /* The address is OK. */
  498. return 0;
  499. address_error:
  500. /* We can't support this address. Unmap it again. */
  501. unmap_descbuffer(ring, addr, buffersize, dma_to_device);
  502. return 1;
  503. }
  504. static bool b43_rx_buffer_is_poisoned(struct b43_dmaring *ring, struct sk_buff *skb)
  505. {
  506. unsigned char *f = skb->data + ring->frameoffset;
  507. return ((f[0] & f[1] & f[2] & f[3] & f[4] & f[5] & f[6] & f[7]) == 0xFF);
  508. }
  509. static void b43_poison_rx_buffer(struct b43_dmaring *ring, struct sk_buff *skb)
  510. {
  511. struct b43_rxhdr_fw4 *rxhdr;
  512. unsigned char *frame;
  513. /* This poisons the RX buffer to detect DMA failures. */
  514. rxhdr = (struct b43_rxhdr_fw4 *)(skb->data);
  515. rxhdr->frame_len = 0;
  516. B43_WARN_ON(ring->rx_buffersize < ring->frameoffset + sizeof(struct b43_plcp_hdr6) + 2);
  517. frame = skb->data + ring->frameoffset;
  518. memset(frame, 0xFF, sizeof(struct b43_plcp_hdr6) + 2 /* padding */);
  519. }
  520. static int setup_rx_descbuffer(struct b43_dmaring *ring,
  521. struct b43_dmadesc_generic *desc,
  522. struct b43_dmadesc_meta *meta, gfp_t gfp_flags)
  523. {
  524. dma_addr_t dmaaddr;
  525. struct sk_buff *skb;
  526. B43_WARN_ON(ring->tx);
  527. skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
  528. if (unlikely(!skb))
  529. return -ENOMEM;
  530. b43_poison_rx_buffer(ring, skb);
  531. dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0);
  532. if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
  533. /* ugh. try to realloc in zone_dma */
  534. gfp_flags |= GFP_DMA;
  535. dev_kfree_skb_any(skb);
  536. skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
  537. if (unlikely(!skb))
  538. return -ENOMEM;
  539. b43_poison_rx_buffer(ring, skb);
  540. dmaaddr = map_descbuffer(ring, skb->data,
  541. ring->rx_buffersize, 0);
  542. if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
  543. b43err(ring->dev->wl, "RX DMA buffer allocation failed\n");
  544. dev_kfree_skb_any(skb);
  545. return -EIO;
  546. }
  547. }
  548. meta->skb = skb;
  549. meta->dmaaddr = dmaaddr;
  550. ring->ops->fill_descriptor(ring, desc, dmaaddr,
  551. ring->rx_buffersize, 0, 0, 0);
  552. return 0;
  553. }
  554. /* Allocate the initial descbuffers.
  555. * This is used for an RX ring only.
  556. */
  557. static int alloc_initial_descbuffers(struct b43_dmaring *ring)
  558. {
  559. int i, err = -ENOMEM;
  560. struct b43_dmadesc_generic *desc;
  561. struct b43_dmadesc_meta *meta;
  562. for (i = 0; i < ring->nr_slots; i++) {
  563. desc = ring->ops->idx2desc(ring, i, &meta);
  564. err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
  565. if (err) {
  566. b43err(ring->dev->wl,
  567. "Failed to allocate initial descbuffers\n");
  568. goto err_unwind;
  569. }
  570. }
  571. mb();
  572. ring->used_slots = ring->nr_slots;
  573. err = 0;
  574. out:
  575. return err;
  576. err_unwind:
  577. for (i--; i >= 0; i--) {
  578. desc = ring->ops->idx2desc(ring, i, &meta);
  579. unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
  580. dev_kfree_skb(meta->skb);
  581. }
  582. goto out;
  583. }
  584. /* Do initial setup of the DMA controller.
  585. * Reset the controller, write the ring busaddress
  586. * and switch the "enable" bit on.
  587. */
  588. static int dmacontroller_setup(struct b43_dmaring *ring)
  589. {
  590. int err = 0;
  591. u32 value;
  592. u32 addrext;
  593. bool parity = ring->dev->dma.parity;
  594. u32 addrlo;
  595. u32 addrhi;
  596. if (ring->tx) {
  597. if (ring->type == B43_DMA_64BIT) {
  598. u64 ringbase = (u64) (ring->dmabase);
  599. addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
  600. addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
  601. addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH);
  602. value = B43_DMA64_TXENABLE;
  603. value |= (addrext << B43_DMA64_TXADDREXT_SHIFT)
  604. & B43_DMA64_TXADDREXT_MASK;
  605. if (!parity)
  606. value |= B43_DMA64_TXPARITYDISABLE;
  607. b43_dma_write(ring, B43_DMA64_TXCTL, value);
  608. b43_dma_write(ring, B43_DMA64_TXRINGLO, addrlo);
  609. b43_dma_write(ring, B43_DMA64_TXRINGHI, addrhi);
  610. } else {
  611. u32 ringbase = (u32) (ring->dmabase);
  612. addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
  613. addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
  614. value = B43_DMA32_TXENABLE;
  615. value |= (addrext << B43_DMA32_TXADDREXT_SHIFT)
  616. & B43_DMA32_TXADDREXT_MASK;
  617. if (!parity)
  618. value |= B43_DMA32_TXPARITYDISABLE;
  619. b43_dma_write(ring, B43_DMA32_TXCTL, value);
  620. b43_dma_write(ring, B43_DMA32_TXRING, addrlo);
  621. }
  622. } else {
  623. err = alloc_initial_descbuffers(ring);
  624. if (err)
  625. goto out;
  626. if (ring->type == B43_DMA_64BIT) {
  627. u64 ringbase = (u64) (ring->dmabase);
  628. addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
  629. addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
  630. addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH);
  631. value = (ring->frameoffset << B43_DMA64_RXFROFF_SHIFT);
  632. value |= B43_DMA64_RXENABLE;
  633. value |= (addrext << B43_DMA64_RXADDREXT_SHIFT)
  634. & B43_DMA64_RXADDREXT_MASK;
  635. if (!parity)
  636. value |= B43_DMA64_RXPARITYDISABLE;
  637. b43_dma_write(ring, B43_DMA64_RXCTL, value);
  638. b43_dma_write(ring, B43_DMA64_RXRINGLO, addrlo);
  639. b43_dma_write(ring, B43_DMA64_RXRINGHI, addrhi);
  640. b43_dma_write(ring, B43_DMA64_RXINDEX, ring->nr_slots *
  641. sizeof(struct b43_dmadesc64));
  642. } else {
  643. u32 ringbase = (u32) (ring->dmabase);
  644. addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
  645. addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
  646. value = (ring->frameoffset << B43_DMA32_RXFROFF_SHIFT);
  647. value |= B43_DMA32_RXENABLE;
  648. value |= (addrext << B43_DMA32_RXADDREXT_SHIFT)
  649. & B43_DMA32_RXADDREXT_MASK;
  650. if (!parity)
  651. value |= B43_DMA32_RXPARITYDISABLE;
  652. b43_dma_write(ring, B43_DMA32_RXCTL, value);
  653. b43_dma_write(ring, B43_DMA32_RXRING, addrlo);
  654. b43_dma_write(ring, B43_DMA32_RXINDEX, ring->nr_slots *
  655. sizeof(struct b43_dmadesc32));
  656. }
  657. }
  658. out:
  659. return err;
  660. }
  661. /* Shutdown the DMA controller. */
  662. static void dmacontroller_cleanup(struct b43_dmaring *ring)
  663. {
  664. if (ring->tx) {
  665. b43_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
  666. ring->type);
  667. if (ring->type == B43_DMA_64BIT) {
  668. b43_dma_write(ring, B43_DMA64_TXRINGLO, 0);
  669. b43_dma_write(ring, B43_DMA64_TXRINGHI, 0);
  670. } else
  671. b43_dma_write(ring, B43_DMA32_TXRING, 0);
  672. } else {
  673. b43_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
  674. ring->type);
  675. if (ring->type == B43_DMA_64BIT) {
  676. b43_dma_write(ring, B43_DMA64_RXRINGLO, 0);
  677. b43_dma_write(ring, B43_DMA64_RXRINGHI, 0);
  678. } else
  679. b43_dma_write(ring, B43_DMA32_RXRING, 0);
  680. }
  681. }
  682. static void free_all_descbuffers(struct b43_dmaring *ring)
  683. {
  684. struct b43_dmadesc_meta *meta;
  685. int i;
  686. if (!ring->used_slots)
  687. return;
  688. for (i = 0; i < ring->nr_slots; i++) {
  689. /* get meta - ignore returned value */
  690. ring->ops->idx2desc(ring, i, &meta);
  691. if (!meta->skb || b43_dma_ptr_is_poisoned(meta->skb)) {
  692. B43_WARN_ON(!ring->tx);
  693. continue;
  694. }
  695. if (ring->tx) {
  696. unmap_descbuffer(ring, meta->dmaaddr,
  697. meta->skb->len, 1);
  698. } else {
  699. unmap_descbuffer(ring, meta->dmaaddr,
  700. ring->rx_buffersize, 0);
  701. }
  702. free_descriptor_buffer(ring, meta);
  703. }
  704. }
  705. static u64 supported_dma_mask(struct b43_wldev *dev)
  706. {
  707. u32 tmp;
  708. u16 mmio_base;
  709. switch (dev->dev->bus_type) {
  710. #ifdef CONFIG_B43_BCMA
  711. case B43_BUS_BCMA:
  712. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
  713. if (tmp & BCMA_IOST_DMA64)
  714. return DMA_BIT_MASK(64);
  715. break;
  716. #endif
  717. #ifdef CONFIG_B43_SSB
  718. case B43_BUS_SSB:
  719. tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
  720. if (tmp & SSB_TMSHIGH_DMA64)
  721. return DMA_BIT_MASK(64);
  722. break;
  723. #endif
  724. }
  725. mmio_base = b43_dmacontroller_base(0, 0);
  726. b43_write32(dev, mmio_base + B43_DMA32_TXCTL, B43_DMA32_TXADDREXT_MASK);
  727. tmp = b43_read32(dev, mmio_base + B43_DMA32_TXCTL);
  728. if (tmp & B43_DMA32_TXADDREXT_MASK)
  729. return DMA_BIT_MASK(32);
  730. return DMA_BIT_MASK(30);
  731. }
  732. static enum b43_dmatype dma_mask_to_engine_type(u64 dmamask)
  733. {
  734. if (dmamask == DMA_BIT_MASK(30))
  735. return B43_DMA_30BIT;
  736. if (dmamask == DMA_BIT_MASK(32))
  737. return B43_DMA_32BIT;
  738. if (dmamask == DMA_BIT_MASK(64))
  739. return B43_DMA_64BIT;
  740. B43_WARN_ON(1);
  741. return B43_DMA_30BIT;
  742. }
  743. /* Main initialization function. */
  744. static
  745. struct b43_dmaring *b43_setup_dmaring(struct b43_wldev *dev,
  746. int controller_index,
  747. int for_tx,
  748. enum b43_dmatype type)
  749. {
  750. struct b43_dmaring *ring;
  751. int i, err;
  752. dma_addr_t dma_test;
  753. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  754. if (!ring)
  755. goto out;
  756. ring->nr_slots = B43_RXRING_SLOTS;
  757. if (for_tx)
  758. ring->nr_slots = B43_TXRING_SLOTS;
  759. ring->meta = kcalloc(ring->nr_slots, sizeof(struct b43_dmadesc_meta),
  760. GFP_KERNEL);
  761. if (!ring->meta)
  762. goto err_kfree_ring;
  763. for (i = 0; i < ring->nr_slots; i++)
  764. ring->meta->skb = B43_DMA_PTR_POISON;
  765. ring->type = type;
  766. ring->dev = dev;
  767. ring->mmio_base = b43_dmacontroller_base(type, controller_index);
  768. ring->index = controller_index;
  769. if (type == B43_DMA_64BIT)
  770. ring->ops = &dma64_ops;
  771. else
  772. ring->ops = &dma32_ops;
  773. if (for_tx) {
  774. ring->tx = true;
  775. ring->current_slot = -1;
  776. } else {
  777. if (ring->index == 0) {
  778. switch (dev->fw.hdr_format) {
  779. case B43_FW_HDR_598:
  780. ring->rx_buffersize = B43_DMA0_RX_FW598_BUFSIZE;
  781. ring->frameoffset = B43_DMA0_RX_FW598_FO;
  782. break;
  783. case B43_FW_HDR_410:
  784. case B43_FW_HDR_351:
  785. ring->rx_buffersize = B43_DMA0_RX_FW351_BUFSIZE;
  786. ring->frameoffset = B43_DMA0_RX_FW351_FO;
  787. break;
  788. }
  789. } else
  790. B43_WARN_ON(1);
  791. }
  792. #ifdef CONFIG_B43_DEBUG
  793. ring->last_injected_overflow = jiffies;
  794. #endif
  795. if (for_tx) {
  796. /* Assumption: B43_TXRING_SLOTS can be divided by TX_SLOTS_PER_FRAME */
  797. BUILD_BUG_ON(B43_TXRING_SLOTS % TX_SLOTS_PER_FRAME != 0);
  798. ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME,
  799. b43_txhdr_size(dev),
  800. GFP_KERNEL);
  801. if (!ring->txhdr_cache)
  802. goto err_kfree_meta;
  803. /* test for ability to dma to txhdr_cache */
  804. dma_test = dma_map_single(dev->dev->dma_dev,
  805. ring->txhdr_cache,
  806. b43_txhdr_size(dev),
  807. DMA_TO_DEVICE);
  808. if (b43_dma_mapping_error(ring, dma_test,
  809. b43_txhdr_size(dev), 1)) {
  810. /* ugh realloc */
  811. kfree(ring->txhdr_cache);
  812. ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME,
  813. b43_txhdr_size(dev),
  814. GFP_KERNEL | GFP_DMA);
  815. if (!ring->txhdr_cache)
  816. goto err_kfree_meta;
  817. dma_test = dma_map_single(dev->dev->dma_dev,
  818. ring->txhdr_cache,
  819. b43_txhdr_size(dev),
  820. DMA_TO_DEVICE);
  821. if (b43_dma_mapping_error(ring, dma_test,
  822. b43_txhdr_size(dev), 1)) {
  823. b43err(dev->wl,
  824. "TXHDR DMA allocation failed\n");
  825. goto err_kfree_txhdr_cache;
  826. }
  827. }
  828. dma_unmap_single(dev->dev->dma_dev,
  829. dma_test, b43_txhdr_size(dev),
  830. DMA_TO_DEVICE);
  831. }
  832. err = alloc_ringmemory(ring);
  833. if (err)
  834. goto err_kfree_txhdr_cache;
  835. err = dmacontroller_setup(ring);
  836. if (err)
  837. goto err_free_ringmemory;
  838. out:
  839. return ring;
  840. err_free_ringmemory:
  841. free_ringmemory(ring);
  842. err_kfree_txhdr_cache:
  843. kfree(ring->txhdr_cache);
  844. err_kfree_meta:
  845. kfree(ring->meta);
  846. err_kfree_ring:
  847. kfree(ring);
  848. ring = NULL;
  849. goto out;
  850. }
  851. #define divide(a, b) ({ \
  852. typeof(a) __a = a; \
  853. do_div(__a, b); \
  854. __a; \
  855. })
  856. #define modulo(a, b) ({ \
  857. typeof(a) __a = a; \
  858. do_div(__a, b); \
  859. })
  860. /* Main cleanup function. */
  861. static void b43_destroy_dmaring(struct b43_dmaring *ring,
  862. const char *ringname)
  863. {
  864. if (!ring)
  865. return;
  866. #ifdef CONFIG_B43_DEBUG
  867. {
  868. /* Print some statistics. */
  869. u64 failed_packets = ring->nr_failed_tx_packets;
  870. u64 succeed_packets = ring->nr_succeed_tx_packets;
  871. u64 nr_packets = failed_packets + succeed_packets;
  872. u64 permille_failed = 0, average_tries = 0;
  873. if (nr_packets)
  874. permille_failed = divide(failed_packets * 1000, nr_packets);
  875. if (nr_packets)
  876. average_tries = divide(ring->nr_total_packet_tries * 100, nr_packets);
  877. b43dbg(ring->dev->wl, "DMA-%u %s: "
  878. "Used slots %d/%d, Failed frames %llu/%llu = %llu.%01llu%%, "
  879. "Average tries %llu.%02llu\n",
  880. (unsigned int)(ring->type), ringname,
  881. ring->max_used_slots,
  882. ring->nr_slots,
  883. (unsigned long long)failed_packets,
  884. (unsigned long long)nr_packets,
  885. (unsigned long long)divide(permille_failed, 10),
  886. (unsigned long long)modulo(permille_failed, 10),
  887. (unsigned long long)divide(average_tries, 100),
  888. (unsigned long long)modulo(average_tries, 100));
  889. }
  890. #endif /* DEBUG */
  891. /* Device IRQs are disabled prior entering this function,
  892. * so no need to take care of concurrency with rx handler stuff.
  893. */
  894. dmacontroller_cleanup(ring);
  895. free_all_descbuffers(ring);
  896. free_ringmemory(ring);
  897. kfree(ring->txhdr_cache);
  898. kfree(ring->meta);
  899. kfree(ring);
  900. }
  901. #define destroy_ring(dma, ring) do { \
  902. b43_destroy_dmaring((dma)->ring, __stringify(ring)); \
  903. (dma)->ring = NULL; \
  904. } while (0)
  905. void b43_dma_free(struct b43_wldev *dev)
  906. {
  907. struct b43_dma *dma;
  908. if (b43_using_pio_transfers(dev))
  909. return;
  910. dma = &dev->dma;
  911. destroy_ring(dma, rx_ring);
  912. destroy_ring(dma, tx_ring_AC_BK);
  913. destroy_ring(dma, tx_ring_AC_BE);
  914. destroy_ring(dma, tx_ring_AC_VI);
  915. destroy_ring(dma, tx_ring_AC_VO);
  916. destroy_ring(dma, tx_ring_mcast);
  917. }
  918. static int b43_dma_set_mask(struct b43_wldev *dev, u64 mask)
  919. {
  920. u64 orig_mask = mask;
  921. bool fallback = false;
  922. int err;
  923. /* Try to set the DMA mask. If it fails, try falling back to a
  924. * lower mask, as we can always also support a lower one. */
  925. while (1) {
  926. err = dma_set_mask(dev->dev->dma_dev, mask);
  927. if (!err) {
  928. err = dma_set_coherent_mask(dev->dev->dma_dev, mask);
  929. if (!err)
  930. break;
  931. }
  932. if (mask == DMA_BIT_MASK(64)) {
  933. mask = DMA_BIT_MASK(32);
  934. fallback = true;
  935. continue;
  936. }
  937. if (mask == DMA_BIT_MASK(32)) {
  938. mask = DMA_BIT_MASK(30);
  939. fallback = true;
  940. continue;
  941. }
  942. b43err(dev->wl, "The machine/kernel does not support "
  943. "the required %u-bit DMA mask\n",
  944. (unsigned int)dma_mask_to_engine_type(orig_mask));
  945. return -EOPNOTSUPP;
  946. }
  947. if (fallback) {
  948. b43info(dev->wl, "DMA mask fallback from %u-bit to %u-bit\n",
  949. (unsigned int)dma_mask_to_engine_type(orig_mask),
  950. (unsigned int)dma_mask_to_engine_type(mask));
  951. }
  952. return 0;
  953. }
  954. /* Some hardware with 64-bit DMA seems to be bugged and looks for translation
  955. * bit in low address word instead of high one.
  956. */
  957. static bool b43_dma_translation_in_low_word(struct b43_wldev *dev,
  958. enum b43_dmatype type)
  959. {
  960. if (type != B43_DMA_64BIT)
  961. return 1;
  962. #ifdef CONFIG_B43_SSB
  963. if (dev->dev->bus_type == B43_BUS_SSB &&
  964. dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
  965. !(dev->dev->sdev->bus->host_pci->is_pcie &&
  966. ssb_read32(dev->dev->sdev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64))
  967. return 1;
  968. #endif
  969. return 0;
  970. }
  971. int b43_dma_init(struct b43_wldev *dev)
  972. {
  973. struct b43_dma *dma = &dev->dma;
  974. int err;
  975. u64 dmamask;
  976. enum b43_dmatype type;
  977. dmamask = supported_dma_mask(dev);
  978. type = dma_mask_to_engine_type(dmamask);
  979. err = b43_dma_set_mask(dev, dmamask);
  980. if (err)
  981. return err;
  982. switch (dev->dev->bus_type) {
  983. #ifdef CONFIG_B43_BCMA
  984. case B43_BUS_BCMA:
  985. dma->translation = bcma_core_dma_translation(dev->dev->bdev);
  986. break;
  987. #endif
  988. #ifdef CONFIG_B43_SSB
  989. case B43_BUS_SSB:
  990. dma->translation = ssb_dma_translation(dev->dev->sdev);
  991. break;
  992. #endif
  993. }
  994. dma->translation_in_low = b43_dma_translation_in_low_word(dev, type);
  995. dma->parity = true;
  996. #ifdef CONFIG_B43_BCMA
  997. /* TODO: find out which SSB devices need disabling parity */
  998. if (dev->dev->bus_type == B43_BUS_BCMA)
  999. dma->parity = false;
  1000. #endif
  1001. err = -ENOMEM;
  1002. /* setup TX DMA channels. */
  1003. dma->tx_ring_AC_BK = b43_setup_dmaring(dev, 0, 1, type);
  1004. if (!dma->tx_ring_AC_BK)
  1005. goto out;
  1006. dma->tx_ring_AC_BE = b43_setup_dmaring(dev, 1, 1, type);
  1007. if (!dma->tx_ring_AC_BE)
  1008. goto err_destroy_bk;
  1009. dma->tx_ring_AC_VI = b43_setup_dmaring(dev, 2, 1, type);
  1010. if (!dma->tx_ring_AC_VI)
  1011. goto err_destroy_be;
  1012. dma->tx_ring_AC_VO = b43_setup_dmaring(dev, 3, 1, type);
  1013. if (!dma->tx_ring_AC_VO)
  1014. goto err_destroy_vi;
  1015. dma->tx_ring_mcast = b43_setup_dmaring(dev, 4, 1, type);
  1016. if (!dma->tx_ring_mcast)
  1017. goto err_destroy_vo;
  1018. /* setup RX DMA channel. */
  1019. dma->rx_ring = b43_setup_dmaring(dev, 0, 0, type);
  1020. if (!dma->rx_ring)
  1021. goto err_destroy_mcast;
  1022. /* No support for the TX status DMA ring. */
  1023. B43_WARN_ON(dev->dev->core_rev < 5);
  1024. b43dbg(dev->wl, "%u-bit DMA initialized\n",
  1025. (unsigned int)type);
  1026. err = 0;
  1027. out:
  1028. return err;
  1029. err_destroy_mcast:
  1030. destroy_ring(dma, tx_ring_mcast);
  1031. err_destroy_vo:
  1032. destroy_ring(dma, tx_ring_AC_VO);
  1033. err_destroy_vi:
  1034. destroy_ring(dma, tx_ring_AC_VI);
  1035. err_destroy_be:
  1036. destroy_ring(dma, tx_ring_AC_BE);
  1037. err_destroy_bk:
  1038. destroy_ring(dma, tx_ring_AC_BK);
  1039. return err;
  1040. }
  1041. /* Generate a cookie for the TX header. */
  1042. static u16 generate_cookie(struct b43_dmaring *ring, int slot)
  1043. {
  1044. u16 cookie;
  1045. /* Use the upper 4 bits of the cookie as
  1046. * DMA controller ID and store the slot number
  1047. * in the lower 12 bits.
  1048. * Note that the cookie must never be 0, as this
  1049. * is a special value used in RX path.
  1050. * It can also not be 0xFFFF because that is special
  1051. * for multicast frames.
  1052. */
  1053. cookie = (((u16)ring->index + 1) << 12);
  1054. B43_WARN_ON(slot & ~0x0FFF);
  1055. cookie |= (u16)slot;
  1056. return cookie;
  1057. }
  1058. /* Inspect a cookie and find out to which controller/slot it belongs. */
  1059. static
  1060. struct b43_dmaring *parse_cookie(struct b43_wldev *dev, u16 cookie, int *slot)
  1061. {
  1062. struct b43_dma *dma = &dev->dma;
  1063. struct b43_dmaring *ring = NULL;
  1064. switch (cookie & 0xF000) {
  1065. case 0x1000:
  1066. ring = dma->tx_ring_AC_BK;
  1067. break;
  1068. case 0x2000:
  1069. ring = dma->tx_ring_AC_BE;
  1070. break;
  1071. case 0x3000:
  1072. ring = dma->tx_ring_AC_VI;
  1073. break;
  1074. case 0x4000:
  1075. ring = dma->tx_ring_AC_VO;
  1076. break;
  1077. case 0x5000:
  1078. ring = dma->tx_ring_mcast;
  1079. break;
  1080. }
  1081. *slot = (cookie & 0x0FFF);
  1082. if (unlikely(!ring || *slot < 0 || *slot >= ring->nr_slots)) {
  1083. b43dbg(dev->wl, "TX-status contains "
  1084. "invalid cookie: 0x%04X\n", cookie);
  1085. return NULL;
  1086. }
  1087. return ring;
  1088. }
  1089. static int dma_tx_fragment(struct b43_dmaring *ring,
  1090. struct sk_buff *skb)
  1091. {
  1092. const struct b43_dma_ops *ops = ring->ops;
  1093. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1094. struct b43_private_tx_info *priv_info = b43_get_priv_tx_info(info);
  1095. u8 *header;
  1096. int slot, old_top_slot, old_used_slots;
  1097. int err;
  1098. struct b43_dmadesc_generic *desc;
  1099. struct b43_dmadesc_meta *meta;
  1100. struct b43_dmadesc_meta *meta_hdr;
  1101. u16 cookie;
  1102. size_t hdrsize = b43_txhdr_size(ring->dev);
  1103. /* Important note: If the number of used DMA slots per TX frame
  1104. * is changed here, the TX_SLOTS_PER_FRAME definition at the top of
  1105. * the file has to be updated, too!
  1106. */
  1107. old_top_slot = ring->current_slot;
  1108. old_used_slots = ring->used_slots;
  1109. /* Get a slot for the header. */
  1110. slot = request_slot(ring);
  1111. desc = ops->idx2desc(ring, slot, &meta_hdr);
  1112. memset(meta_hdr, 0, sizeof(*meta_hdr));
  1113. header = &(ring->txhdr_cache[(slot / TX_SLOTS_PER_FRAME) * hdrsize]);
  1114. cookie = generate_cookie(ring, slot);
  1115. err = b43_generate_txhdr(ring->dev, header,
  1116. skb, info, cookie);
  1117. if (unlikely(err)) {
  1118. ring->current_slot = old_top_slot;
  1119. ring->used_slots = old_used_slots;
  1120. return err;
  1121. }
  1122. meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
  1123. hdrsize, 1);
  1124. if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize, 1)) {
  1125. ring->current_slot = old_top_slot;
  1126. ring->used_slots = old_used_slots;
  1127. return -EIO;
  1128. }
  1129. ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr,
  1130. hdrsize, 1, 0, 0);
  1131. /* Get a slot for the payload. */
  1132. slot = request_slot(ring);
  1133. desc = ops->idx2desc(ring, slot, &meta);
  1134. memset(meta, 0, sizeof(*meta));
  1135. meta->skb = skb;
  1136. meta->is_last_fragment = true;
  1137. priv_info->bouncebuffer = NULL;
  1138. meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
  1139. /* create a bounce buffer in zone_dma on mapping failure. */
  1140. if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
  1141. priv_info->bouncebuffer = kmemdup(skb->data, skb->len,
  1142. GFP_ATOMIC | GFP_DMA);
  1143. if (!priv_info->bouncebuffer) {
  1144. ring->current_slot = old_top_slot;
  1145. ring->used_slots = old_used_slots;
  1146. err = -ENOMEM;
  1147. goto out_unmap_hdr;
  1148. }
  1149. meta->dmaaddr = map_descbuffer(ring, priv_info->bouncebuffer, skb->len, 1);
  1150. if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
  1151. kfree(priv_info->bouncebuffer);
  1152. priv_info->bouncebuffer = NULL;
  1153. ring->current_slot = old_top_slot;
  1154. ring->used_slots = old_used_slots;
  1155. err = -EIO;
  1156. goto out_unmap_hdr;
  1157. }
  1158. }
  1159. ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1);
  1160. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1161. /* Tell the firmware about the cookie of the last
  1162. * mcast frame, so it can clear the more-data bit in it. */
  1163. b43_shm_write16(ring->dev, B43_SHM_SHARED,
  1164. B43_SHM_SH_MCASTCOOKIE, cookie);
  1165. }
  1166. /* Now transfer the whole frame. */
  1167. wmb();
  1168. ops->poke_tx(ring, next_slot(ring, slot));
  1169. return 0;
  1170. out_unmap_hdr:
  1171. unmap_descbuffer(ring, meta_hdr->dmaaddr,
  1172. hdrsize, 1);
  1173. return err;
  1174. }
  1175. static inline int should_inject_overflow(struct b43_dmaring *ring)
  1176. {
  1177. #ifdef CONFIG_B43_DEBUG
  1178. if (unlikely(b43_debug(ring->dev, B43_DBG_DMAOVERFLOW))) {
  1179. /* Check if we should inject another ringbuffer overflow
  1180. * to test handling of this situation in the stack. */
  1181. unsigned long next_overflow;
  1182. next_overflow = ring->last_injected_overflow + HZ;
  1183. if (time_after(jiffies, next_overflow)) {
  1184. ring->last_injected_overflow = jiffies;
  1185. b43dbg(ring->dev->wl,
  1186. "Injecting TX ring overflow on "
  1187. "DMA controller %d\n", ring->index);
  1188. return 1;
  1189. }
  1190. }
  1191. #endif /* CONFIG_B43_DEBUG */
  1192. return 0;
  1193. }
  1194. /* Static mapping of mac80211's queues (priorities) to b43 DMA rings. */
  1195. static struct b43_dmaring *select_ring_by_priority(struct b43_wldev *dev,
  1196. u8 queue_prio)
  1197. {
  1198. struct b43_dmaring *ring;
  1199. if (dev->qos_enabled) {
  1200. /* 0 = highest priority */
  1201. switch (queue_prio) {
  1202. default:
  1203. B43_WARN_ON(1);
  1204. /* fallthrough */
  1205. case 0:
  1206. ring = dev->dma.tx_ring_AC_VO;
  1207. break;
  1208. case 1:
  1209. ring = dev->dma.tx_ring_AC_VI;
  1210. break;
  1211. case 2:
  1212. ring = dev->dma.tx_ring_AC_BE;
  1213. break;
  1214. case 3:
  1215. ring = dev->dma.tx_ring_AC_BK;
  1216. break;
  1217. }
  1218. } else
  1219. ring = dev->dma.tx_ring_AC_BE;
  1220. return ring;
  1221. }
  1222. int b43_dma_tx(struct b43_wldev *dev, struct sk_buff *skb)
  1223. {
  1224. struct b43_dmaring *ring;
  1225. struct ieee80211_hdr *hdr;
  1226. int err = 0;
  1227. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1228. hdr = (struct ieee80211_hdr *)skb->data;
  1229. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  1230. /* The multicast ring will be sent after the DTIM */
  1231. ring = dev->dma.tx_ring_mcast;
  1232. /* Set the more-data bit. Ucode will clear it on
  1233. * the last frame for us. */
  1234. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1235. } else {
  1236. /* Decide by priority where to put this frame. */
  1237. ring = select_ring_by_priority(
  1238. dev, skb_get_queue_mapping(skb));
  1239. }
  1240. B43_WARN_ON(!ring->tx);
  1241. if (unlikely(ring->stopped)) {
  1242. /* We get here only because of a bug in mac80211.
  1243. * Because of a race, one packet may be queued after
  1244. * the queue is stopped, thus we got called when we shouldn't.
  1245. * For now, just refuse the transmit. */
  1246. if (b43_debug(dev, B43_DBG_DMAVERBOSE))
  1247. b43err(dev->wl, "Packet after queue stopped\n");
  1248. err = -ENOSPC;
  1249. goto out;
  1250. }
  1251. if (unlikely(WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME))) {
  1252. /* If we get here, we have a real error with the queue
  1253. * full, but queues not stopped. */
  1254. b43err(dev->wl, "DMA queue overflow\n");
  1255. err = -ENOSPC;
  1256. goto out;
  1257. }
  1258. /* Assign the queue number to the ring (if not already done before)
  1259. * so TX status handling can use it. The queue to ring mapping is
  1260. * static, so we don't need to store it per frame. */
  1261. ring->queue_prio = skb_get_queue_mapping(skb);
  1262. err = dma_tx_fragment(ring, skb);
  1263. if (unlikely(err == -ENOKEY)) {
  1264. /* Drop this packet, as we don't have the encryption key
  1265. * anymore and must not transmit it unencrypted. */
  1266. ieee80211_free_txskb(dev->wl->hw, skb);
  1267. err = 0;
  1268. goto out;
  1269. }
  1270. if (unlikely(err)) {
  1271. b43err(dev->wl, "DMA tx mapping failure\n");
  1272. goto out;
  1273. }
  1274. if ((free_slots(ring) < TX_SLOTS_PER_FRAME) ||
  1275. should_inject_overflow(ring)) {
  1276. /* This TX ring is full. */
  1277. unsigned int skb_mapping = skb_get_queue_mapping(skb);
  1278. ieee80211_stop_queue(dev->wl->hw, skb_mapping);
  1279. dev->wl->tx_queue_stopped[skb_mapping] = 1;
  1280. ring->stopped = true;
  1281. if (b43_debug(dev, B43_DBG_DMAVERBOSE)) {
  1282. b43dbg(dev->wl, "Stopped TX ring %d\n", ring->index);
  1283. }
  1284. }
  1285. out:
  1286. return err;
  1287. }
  1288. void b43_dma_handle_txstatus(struct b43_wldev *dev,
  1289. const struct b43_txstatus *status)
  1290. {
  1291. const struct b43_dma_ops *ops;
  1292. struct b43_dmaring *ring;
  1293. struct b43_dmadesc_meta *meta;
  1294. static const struct b43_txstatus fake; /* filled with 0 */
  1295. const struct b43_txstatus *txstat;
  1296. int slot, firstused;
  1297. bool frame_succeed;
  1298. int skip;
  1299. static u8 err_out1, err_out2;
  1300. ring = parse_cookie(dev, status->cookie, &slot);
  1301. if (unlikely(!ring))
  1302. return;
  1303. B43_WARN_ON(!ring->tx);
  1304. /* Sanity check: TX packets are processed in-order on one ring.
  1305. * Check if the slot deduced from the cookie really is the first
  1306. * used slot. */
  1307. firstused = ring->current_slot - ring->used_slots + 1;
  1308. if (firstused < 0)
  1309. firstused = ring->nr_slots + firstused;
  1310. skip = 0;
  1311. if (unlikely(slot != firstused)) {
  1312. /* This possibly is a firmware bug and will result in
  1313. * malfunction, memory leaks and/or stall of DMA functionality.
  1314. */
  1315. if (slot == next_slot(ring, next_slot(ring, firstused))) {
  1316. /* If a single header/data pair was missed, skip over
  1317. * the first two slots in an attempt to recover.
  1318. */
  1319. slot = firstused;
  1320. skip = 2;
  1321. if (!err_out1) {
  1322. /* Report the error once. */
  1323. b43dbg(dev->wl,
  1324. "Skip on DMA ring %d slot %d.\n",
  1325. ring->index, slot);
  1326. err_out1 = 1;
  1327. }
  1328. } else {
  1329. /* More than a single header/data pair were missed.
  1330. * Report this error once.
  1331. */
  1332. if (!err_out2)
  1333. b43dbg(dev->wl,
  1334. "Out of order TX status report on DMA ring %d. Expected %d, but got %d\n",
  1335. ring->index, firstused, slot);
  1336. err_out2 = 1;
  1337. return;
  1338. }
  1339. }
  1340. ops = ring->ops;
  1341. while (1) {
  1342. B43_WARN_ON(slot < 0 || slot >= ring->nr_slots);
  1343. /* get meta - ignore returned value */
  1344. ops->idx2desc(ring, slot, &meta);
  1345. if (b43_dma_ptr_is_poisoned(meta->skb)) {
  1346. b43dbg(dev->wl, "Poisoned TX slot %d (first=%d) "
  1347. "on ring %d\n",
  1348. slot, firstused, ring->index);
  1349. break;
  1350. }
  1351. if (meta->skb) {
  1352. struct b43_private_tx_info *priv_info =
  1353. b43_get_priv_tx_info(IEEE80211_SKB_CB(meta->skb));
  1354. unmap_descbuffer(ring, meta->dmaaddr,
  1355. meta->skb->len, 1);
  1356. kfree(priv_info->bouncebuffer);
  1357. priv_info->bouncebuffer = NULL;
  1358. } else {
  1359. unmap_descbuffer(ring, meta->dmaaddr,
  1360. b43_txhdr_size(dev), 1);
  1361. }
  1362. if (meta->is_last_fragment) {
  1363. struct ieee80211_tx_info *info;
  1364. if (unlikely(!meta->skb)) {
  1365. /* This is a scatter-gather fragment of a frame,
  1366. * so the skb pointer must not be NULL.
  1367. */
  1368. b43dbg(dev->wl, "TX status unexpected NULL skb "
  1369. "at slot %d (first=%d) on ring %d\n",
  1370. slot, firstused, ring->index);
  1371. break;
  1372. }
  1373. info = IEEE80211_SKB_CB(meta->skb);
  1374. /*
  1375. * Call back to inform the ieee80211 subsystem about
  1376. * the status of the transmission. When skipping over
  1377. * a missed TX status report, use a status structure
  1378. * filled with zeros to indicate that the frame was not
  1379. * sent (frame_count 0) and not acknowledged
  1380. */
  1381. if (unlikely(skip))
  1382. txstat = &fake;
  1383. else
  1384. txstat = status;
  1385. frame_succeed = b43_fill_txstatus_report(dev, info,
  1386. txstat);
  1387. #ifdef CONFIG_B43_DEBUG
  1388. if (frame_succeed)
  1389. ring->nr_succeed_tx_packets++;
  1390. else
  1391. ring->nr_failed_tx_packets++;
  1392. ring->nr_total_packet_tries += status->frame_count;
  1393. #endif /* DEBUG */
  1394. ieee80211_tx_status(dev->wl->hw, meta->skb);
  1395. /* skb will be freed by ieee80211_tx_status().
  1396. * Poison our pointer. */
  1397. meta->skb = B43_DMA_PTR_POISON;
  1398. } else {
  1399. /* No need to call free_descriptor_buffer here, as
  1400. * this is only the txhdr, which is not allocated.
  1401. */
  1402. if (unlikely(meta->skb)) {
  1403. b43dbg(dev->wl, "TX status unexpected non-NULL skb "
  1404. "at slot %d (first=%d) on ring %d\n",
  1405. slot, firstused, ring->index);
  1406. break;
  1407. }
  1408. }
  1409. /* Everything unmapped and free'd. So it's not used anymore. */
  1410. ring->used_slots--;
  1411. if (meta->is_last_fragment && !skip) {
  1412. /* This is the last scatter-gather
  1413. * fragment of the frame. We are done. */
  1414. break;
  1415. }
  1416. slot = next_slot(ring, slot);
  1417. if (skip > 0)
  1418. --skip;
  1419. }
  1420. if (ring->stopped) {
  1421. B43_WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME);
  1422. ring->stopped = false;
  1423. }
  1424. if (dev->wl->tx_queue_stopped[ring->queue_prio]) {
  1425. dev->wl->tx_queue_stopped[ring->queue_prio] = 0;
  1426. } else {
  1427. /* If the driver queue is running wake the corresponding
  1428. * mac80211 queue. */
  1429. ieee80211_wake_queue(dev->wl->hw, ring->queue_prio);
  1430. if (b43_debug(dev, B43_DBG_DMAVERBOSE)) {
  1431. b43dbg(dev->wl, "Woke up TX ring %d\n", ring->index);
  1432. }
  1433. }
  1434. /* Add work to the queue. */
  1435. ieee80211_queue_work(dev->wl->hw, &dev->wl->tx_work);
  1436. }
  1437. static void dma_rx(struct b43_dmaring *ring, int *slot)
  1438. {
  1439. const struct b43_dma_ops *ops = ring->ops;
  1440. struct b43_dmadesc_generic *desc;
  1441. struct b43_dmadesc_meta *meta;
  1442. struct b43_rxhdr_fw4 *rxhdr;
  1443. struct sk_buff *skb;
  1444. u16 len;
  1445. int err;
  1446. dma_addr_t dmaaddr;
  1447. desc = ops->idx2desc(ring, *slot, &meta);
  1448. sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
  1449. skb = meta->skb;
  1450. rxhdr = (struct b43_rxhdr_fw4 *)skb->data;
  1451. len = le16_to_cpu(rxhdr->frame_len);
  1452. if (len == 0) {
  1453. int i = 0;
  1454. do {
  1455. udelay(2);
  1456. barrier();
  1457. len = le16_to_cpu(rxhdr->frame_len);
  1458. } while (len == 0 && i++ < 5);
  1459. if (unlikely(len == 0)) {
  1460. dmaaddr = meta->dmaaddr;
  1461. goto drop_recycle_buffer;
  1462. }
  1463. }
  1464. if (unlikely(b43_rx_buffer_is_poisoned(ring, skb))) {
  1465. /* Something went wrong with the DMA.
  1466. * The device did not touch the buffer and did not overwrite the poison. */
  1467. b43dbg(ring->dev->wl, "DMA RX: Dropping poisoned buffer.\n");
  1468. dmaaddr = meta->dmaaddr;
  1469. goto drop_recycle_buffer;
  1470. }
  1471. if (unlikely(len + ring->frameoffset > ring->rx_buffersize)) {
  1472. /* The data did not fit into one descriptor buffer
  1473. * and is split over multiple buffers.
  1474. * This should never happen, as we try to allocate buffers
  1475. * big enough. So simply ignore this packet.
  1476. */
  1477. int cnt = 0;
  1478. s32 tmp = len;
  1479. while (1) {
  1480. desc = ops->idx2desc(ring, *slot, &meta);
  1481. /* recycle the descriptor buffer. */
  1482. b43_poison_rx_buffer(ring, meta->skb);
  1483. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1484. ring->rx_buffersize);
  1485. *slot = next_slot(ring, *slot);
  1486. cnt++;
  1487. tmp -= ring->rx_buffersize;
  1488. if (tmp <= 0)
  1489. break;
  1490. }
  1491. b43err(ring->dev->wl, "DMA RX buffer too small "
  1492. "(len: %u, buffer: %u, nr-dropped: %d)\n",
  1493. len, ring->rx_buffersize, cnt);
  1494. goto drop;
  1495. }
  1496. dmaaddr = meta->dmaaddr;
  1497. err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
  1498. if (unlikely(err)) {
  1499. b43dbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer() failed\n");
  1500. goto drop_recycle_buffer;
  1501. }
  1502. unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
  1503. skb_put(skb, len + ring->frameoffset);
  1504. skb_pull(skb, ring->frameoffset);
  1505. b43_rx(ring->dev, skb, rxhdr);
  1506. drop:
  1507. return;
  1508. drop_recycle_buffer:
  1509. /* Poison and recycle the RX buffer. */
  1510. b43_poison_rx_buffer(ring, skb);
  1511. sync_descbuffer_for_device(ring, dmaaddr, ring->rx_buffersize);
  1512. }
  1513. void b43_dma_handle_rx_overflow(struct b43_dmaring *ring)
  1514. {
  1515. int current_slot, previous_slot;
  1516. B43_WARN_ON(ring->tx);
  1517. /* Device has filled all buffers, drop all packets and let TCP
  1518. * decrease speed.
  1519. * Decrement RX index by one will let the device to see all slots
  1520. * as free again
  1521. */
  1522. /*
  1523. *TODO: How to increase rx_drop in mac80211?
  1524. */
  1525. current_slot = ring->ops->get_current_rxslot(ring);
  1526. previous_slot = prev_slot(ring, current_slot);
  1527. ring->ops->set_current_rxslot(ring, previous_slot);
  1528. }
  1529. void b43_dma_rx(struct b43_dmaring *ring)
  1530. {
  1531. const struct b43_dma_ops *ops = ring->ops;
  1532. int slot, current_slot;
  1533. int used_slots = 0;
  1534. B43_WARN_ON(ring->tx);
  1535. current_slot = ops->get_current_rxslot(ring);
  1536. B43_WARN_ON(!(current_slot >= 0 && current_slot < ring->nr_slots));
  1537. slot = ring->current_slot;
  1538. for (; slot != current_slot; slot = next_slot(ring, slot)) {
  1539. dma_rx(ring, &slot);
  1540. update_max_used_slots(ring, ++used_slots);
  1541. }
  1542. wmb();
  1543. ops->set_current_rxslot(ring, slot);
  1544. ring->current_slot = slot;
  1545. }
  1546. static void b43_dma_tx_suspend_ring(struct b43_dmaring *ring)
  1547. {
  1548. B43_WARN_ON(!ring->tx);
  1549. ring->ops->tx_suspend(ring);
  1550. }
  1551. static void b43_dma_tx_resume_ring(struct b43_dmaring *ring)
  1552. {
  1553. B43_WARN_ON(!ring->tx);
  1554. ring->ops->tx_resume(ring);
  1555. }
  1556. void b43_dma_tx_suspend(struct b43_wldev *dev)
  1557. {
  1558. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  1559. b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BK);
  1560. b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BE);
  1561. b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VI);
  1562. b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VO);
  1563. b43_dma_tx_suspend_ring(dev->dma.tx_ring_mcast);
  1564. }
  1565. void b43_dma_tx_resume(struct b43_wldev *dev)
  1566. {
  1567. b43_dma_tx_resume_ring(dev->dma.tx_ring_mcast);
  1568. b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VO);
  1569. b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VI);
  1570. b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BE);
  1571. b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BK);
  1572. b43_power_saving_ctl_bits(dev, 0);
  1573. }
  1574. static void direct_fifo_rx(struct b43_wldev *dev, enum b43_dmatype type,
  1575. u16 mmio_base, bool enable)
  1576. {
  1577. u32 ctl;
  1578. if (type == B43_DMA_64BIT) {
  1579. ctl = b43_read32(dev, mmio_base + B43_DMA64_RXCTL);
  1580. ctl &= ~B43_DMA64_RXDIRECTFIFO;
  1581. if (enable)
  1582. ctl |= B43_DMA64_RXDIRECTFIFO;
  1583. b43_write32(dev, mmio_base + B43_DMA64_RXCTL, ctl);
  1584. } else {
  1585. ctl = b43_read32(dev, mmio_base + B43_DMA32_RXCTL);
  1586. ctl &= ~B43_DMA32_RXDIRECTFIFO;
  1587. if (enable)
  1588. ctl |= B43_DMA32_RXDIRECTFIFO;
  1589. b43_write32(dev, mmio_base + B43_DMA32_RXCTL, ctl);
  1590. }
  1591. }
  1592. /* Enable/Disable Direct FIFO Receive Mode (PIO) on a RX engine.
  1593. * This is called from PIO code, so DMA structures are not available. */
  1594. void b43_dma_direct_fifo_rx(struct b43_wldev *dev,
  1595. unsigned int engine_index, bool enable)
  1596. {
  1597. enum b43_dmatype type;
  1598. u16 mmio_base;
  1599. type = dma_mask_to_engine_type(supported_dma_mask(dev));
  1600. mmio_base = b43_dmacontroller_base(type, engine_index);
  1601. direct_fifo_rx(dev, type, mmio_base, enable);
  1602. }