dma.c 43 KB

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