dma.c 43 KB

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