dma.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*
  2. Broadcom B43legacy wireless driver
  3. DMA ringbuffer and descriptor allocation/management
  4. Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch>
  5. Some code in this file is derived from the b44.c driver
  6. Copyright (C) 2002 David S. Miller
  7. Copyright (C) Pekka Pietikainen
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; see the file COPYING. If not, write to
  18. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  19. Boston, MA 02110-1301, USA.
  20. */
  21. #include "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 |= ring->dev->dma.translation;
  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 |= ring->dev->dma.translation;
  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 = ring->dev->dma.translation;
  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_meta *meta;
  713. int i;
  714. if (!ring->used_slots)
  715. return;
  716. for (i = 0; i < ring->nr_slots; i++) {
  717. 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 = dma_map_single(dev->dev->dma_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 = dma_map_single(dev->dev->dma_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. dma_unmap_single(dev->dev->dma_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 = dma_set_mask(dev->dev->dma_dev, mask);
  907. if (!err) {
  908. err = dma_set_coherent_mask(dev->dev->dma_dev, mask);
  909. if (!err)
  910. break;
  911. }
  912. if (mask == DMA_BIT_MASK(64)) {
  913. mask = DMA_BIT_MASK(32);
  914. fallback = 1;
  915. continue;
  916. }
  917. if (mask == DMA_BIT_MASK(32)) {
  918. mask = DMA_BIT_MASK(30);
  919. fallback = 1;
  920. continue;
  921. }
  922. b43legacyerr(dev->wl, "The machine/kernel does not support "
  923. "the required %u-bit DMA mask\n",
  924. (unsigned int)dma_mask_to_engine_type(orig_mask));
  925. return -EOPNOTSUPP;
  926. }
  927. if (fallback) {
  928. b43legacyinfo(dev->wl, "DMA mask fallback from %u-bit to %u-"
  929. "bit\n",
  930. (unsigned int)dma_mask_to_engine_type(orig_mask),
  931. (unsigned int)dma_mask_to_engine_type(mask));
  932. }
  933. return 0;
  934. }
  935. int b43legacy_dma_init(struct b43legacy_wldev *dev)
  936. {
  937. struct b43legacy_dma *dma = &dev->dma;
  938. struct b43legacy_dmaring *ring;
  939. int err;
  940. u64 dmamask;
  941. enum b43legacy_dmatype type;
  942. dmamask = supported_dma_mask(dev);
  943. type = dma_mask_to_engine_type(dmamask);
  944. err = b43legacy_dma_set_mask(dev, dmamask);
  945. if (err) {
  946. #ifdef CONFIG_B43LEGACY_PIO
  947. b43legacywarn(dev->wl, "DMA for this device not supported. "
  948. "Falling back to PIO\n");
  949. dev->__using_pio = 1;
  950. return -EAGAIN;
  951. #else
  952. b43legacyerr(dev->wl, "DMA for this device not supported and "
  953. "no PIO support compiled in\n");
  954. return -EOPNOTSUPP;
  955. #endif
  956. }
  957. dma->translation = ssb_dma_translation(dev->dev);
  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. int err = 0;
  1203. unsigned long flags;
  1204. ring = priority_to_txring(dev, skb_get_queue_mapping(skb));
  1205. spin_lock_irqsave(&ring->lock, flags);
  1206. B43legacy_WARN_ON(!ring->tx);
  1207. if (unlikely(ring->stopped)) {
  1208. /* We get here only because of a bug in mac80211.
  1209. * Because of a race, one packet may be queued after
  1210. * the queue is stopped, thus we got called when we shouldn't.
  1211. * For now, just refuse the transmit. */
  1212. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1213. b43legacyerr(dev->wl, "Packet after queue stopped\n");
  1214. err = -ENOSPC;
  1215. goto out_unlock;
  1216. }
  1217. if (unlikely(WARN_ON(free_slots(ring) < SLOTS_PER_PACKET))) {
  1218. /* If we get here, we have a real error with the queue
  1219. * full, but queues not stopped. */
  1220. b43legacyerr(dev->wl, "DMA queue overflow\n");
  1221. err = -ENOSPC;
  1222. goto out_unlock;
  1223. }
  1224. /* dma_tx_fragment might reallocate the skb, so invalidate pointers pointing
  1225. * into the skb data or cb now. */
  1226. err = dma_tx_fragment(ring, &skb);
  1227. if (unlikely(err == -ENOKEY)) {
  1228. /* Drop this packet, as we don't have the encryption key
  1229. * anymore and must not transmit it unencrypted. */
  1230. dev_kfree_skb_any(skb);
  1231. err = 0;
  1232. goto out_unlock;
  1233. }
  1234. if (unlikely(err)) {
  1235. b43legacyerr(dev->wl, "DMA tx mapping failure\n");
  1236. goto out_unlock;
  1237. }
  1238. if ((free_slots(ring) < SLOTS_PER_PACKET) ||
  1239. should_inject_overflow(ring)) {
  1240. /* This TX ring is full. */
  1241. ieee80211_stop_queue(dev->wl->hw, txring_to_priority(ring));
  1242. ring->stopped = 1;
  1243. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1244. b43legacydbg(dev->wl, "Stopped TX ring %d\n",
  1245. ring->index);
  1246. }
  1247. out_unlock:
  1248. spin_unlock_irqrestore(&ring->lock, flags);
  1249. return err;
  1250. }
  1251. void b43legacy_dma_handle_txstatus(struct b43legacy_wldev *dev,
  1252. const struct b43legacy_txstatus *status)
  1253. {
  1254. const struct b43legacy_dma_ops *ops;
  1255. struct b43legacy_dmaring *ring;
  1256. struct b43legacy_dmadesc_meta *meta;
  1257. int retry_limit;
  1258. int slot;
  1259. ring = parse_cookie(dev, status->cookie, &slot);
  1260. if (unlikely(!ring))
  1261. return;
  1262. B43legacy_WARN_ON(!irqs_disabled());
  1263. spin_lock(&ring->lock);
  1264. B43legacy_WARN_ON(!ring->tx);
  1265. ops = ring->ops;
  1266. while (1) {
  1267. B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  1268. ops->idx2desc(ring, slot, &meta);
  1269. if (meta->skb)
  1270. unmap_descbuffer(ring, meta->dmaaddr,
  1271. meta->skb->len, 1);
  1272. else
  1273. unmap_descbuffer(ring, meta->dmaaddr,
  1274. sizeof(struct b43legacy_txhdr_fw3),
  1275. 1);
  1276. if (meta->is_last_fragment) {
  1277. struct ieee80211_tx_info *info;
  1278. BUG_ON(!meta->skb);
  1279. info = IEEE80211_SKB_CB(meta->skb);
  1280. /* preserve the confiured retry limit before clearing the status
  1281. * The xmit function has overwritten the rc's value with the actual
  1282. * retry limit done by the hardware */
  1283. retry_limit = info->status.rates[0].count;
  1284. ieee80211_tx_info_clear_status(info);
  1285. if (status->acked)
  1286. info->flags |= IEEE80211_TX_STAT_ACK;
  1287. if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  1288. /*
  1289. * If the short retries (RTS, not data frame) have exceeded
  1290. * the limit, the hw will not have tried the selected rate,
  1291. * but will have used the fallback rate instead.
  1292. * Don't let the rate control count attempts for the selected
  1293. * rate in this case, otherwise the statistics will be off.
  1294. */
  1295. info->status.rates[0].count = 0;
  1296. info->status.rates[1].count = status->frame_count;
  1297. } else {
  1298. if (status->frame_count > retry_limit) {
  1299. info->status.rates[0].count = retry_limit;
  1300. info->status.rates[1].count = status->frame_count -
  1301. retry_limit;
  1302. } else {
  1303. info->status.rates[0].count = status->frame_count;
  1304. info->status.rates[1].idx = -1;
  1305. }
  1306. }
  1307. /* Call back to inform the ieee80211 subsystem about the
  1308. * status of the transmission.
  1309. * Some fields of txstat are already filled in dma_tx().
  1310. */
  1311. ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb);
  1312. /* skb is freed by ieee80211_tx_status_irqsafe() */
  1313. meta->skb = NULL;
  1314. } else {
  1315. /* No need to call free_descriptor_buffer here, as
  1316. * this is only the txhdr, which is not allocated.
  1317. */
  1318. B43legacy_WARN_ON(meta->skb != NULL);
  1319. }
  1320. /* Everything unmapped and free'd. So it's not used anymore. */
  1321. ring->used_slots--;
  1322. if (meta->is_last_fragment)
  1323. break;
  1324. slot = next_slot(ring, slot);
  1325. }
  1326. dev->stats.last_tx = jiffies;
  1327. if (ring->stopped) {
  1328. B43legacy_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
  1329. ieee80211_wake_queue(dev->wl->hw, txring_to_priority(ring));
  1330. ring->stopped = 0;
  1331. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1332. b43legacydbg(dev->wl, "Woke up TX ring %d\n",
  1333. ring->index);
  1334. }
  1335. spin_unlock(&ring->lock);
  1336. }
  1337. static void dma_rx(struct b43legacy_dmaring *ring,
  1338. int *slot)
  1339. {
  1340. const struct b43legacy_dma_ops *ops = ring->ops;
  1341. struct b43legacy_dmadesc_generic *desc;
  1342. struct b43legacy_dmadesc_meta *meta;
  1343. struct b43legacy_rxhdr_fw3 *rxhdr;
  1344. struct sk_buff *skb;
  1345. u16 len;
  1346. int err;
  1347. dma_addr_t dmaaddr;
  1348. desc = ops->idx2desc(ring, *slot, &meta);
  1349. sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
  1350. skb = meta->skb;
  1351. if (ring->index == 3) {
  1352. /* We received an xmit status. */
  1353. struct b43legacy_hwtxstatus *hw =
  1354. (struct b43legacy_hwtxstatus *)skb->data;
  1355. int i = 0;
  1356. while (hw->cookie == 0) {
  1357. if (i > 100)
  1358. break;
  1359. i++;
  1360. udelay(2);
  1361. barrier();
  1362. }
  1363. b43legacy_handle_hwtxstatus(ring->dev, hw);
  1364. /* recycle the descriptor buffer. */
  1365. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1366. ring->rx_buffersize);
  1367. return;
  1368. }
  1369. rxhdr = (struct b43legacy_rxhdr_fw3 *)skb->data;
  1370. len = le16_to_cpu(rxhdr->frame_len);
  1371. if (len == 0) {
  1372. int i = 0;
  1373. do {
  1374. udelay(2);
  1375. barrier();
  1376. len = le16_to_cpu(rxhdr->frame_len);
  1377. } while (len == 0 && i++ < 5);
  1378. if (unlikely(len == 0)) {
  1379. /* recycle the descriptor buffer. */
  1380. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1381. ring->rx_buffersize);
  1382. goto drop;
  1383. }
  1384. }
  1385. if (unlikely(len > ring->rx_buffersize)) {
  1386. /* The data did not fit into one descriptor buffer
  1387. * and is split over multiple buffers.
  1388. * This should never happen, as we try to allocate buffers
  1389. * big enough. So simply ignore this packet.
  1390. */
  1391. int cnt = 0;
  1392. s32 tmp = len;
  1393. while (1) {
  1394. desc = ops->idx2desc(ring, *slot, &meta);
  1395. /* recycle the descriptor buffer. */
  1396. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1397. ring->rx_buffersize);
  1398. *slot = next_slot(ring, *slot);
  1399. cnt++;
  1400. tmp -= ring->rx_buffersize;
  1401. if (tmp <= 0)
  1402. break;
  1403. }
  1404. b43legacyerr(ring->dev->wl, "DMA RX buffer too small "
  1405. "(len: %u, buffer: %u, nr-dropped: %d)\n",
  1406. len, ring->rx_buffersize, cnt);
  1407. goto drop;
  1408. }
  1409. dmaaddr = meta->dmaaddr;
  1410. err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
  1411. if (unlikely(err)) {
  1412. b43legacydbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer()"
  1413. " failed\n");
  1414. sync_descbuffer_for_device(ring, dmaaddr,
  1415. ring->rx_buffersize);
  1416. goto drop;
  1417. }
  1418. unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
  1419. skb_put(skb, len + ring->frameoffset);
  1420. skb_pull(skb, ring->frameoffset);
  1421. b43legacy_rx(ring->dev, skb, rxhdr);
  1422. drop:
  1423. return;
  1424. }
  1425. void b43legacy_dma_rx(struct b43legacy_dmaring *ring)
  1426. {
  1427. const struct b43legacy_dma_ops *ops = ring->ops;
  1428. int slot;
  1429. int current_slot;
  1430. int used_slots = 0;
  1431. B43legacy_WARN_ON(ring->tx);
  1432. current_slot = ops->get_current_rxslot(ring);
  1433. B43legacy_WARN_ON(!(current_slot >= 0 && current_slot <
  1434. ring->nr_slots));
  1435. slot = ring->current_slot;
  1436. for (; slot != current_slot; slot = next_slot(ring, slot)) {
  1437. dma_rx(ring, &slot);
  1438. update_max_used_slots(ring, ++used_slots);
  1439. }
  1440. ops->set_current_rxslot(ring, slot);
  1441. ring->current_slot = slot;
  1442. }
  1443. static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring *ring)
  1444. {
  1445. unsigned long flags;
  1446. spin_lock_irqsave(&ring->lock, flags);
  1447. B43legacy_WARN_ON(!ring->tx);
  1448. ring->ops->tx_suspend(ring);
  1449. spin_unlock_irqrestore(&ring->lock, flags);
  1450. }
  1451. static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring *ring)
  1452. {
  1453. unsigned long flags;
  1454. spin_lock_irqsave(&ring->lock, flags);
  1455. B43legacy_WARN_ON(!ring->tx);
  1456. ring->ops->tx_resume(ring);
  1457. spin_unlock_irqrestore(&ring->lock, flags);
  1458. }
  1459. void b43legacy_dma_tx_suspend(struct b43legacy_wldev *dev)
  1460. {
  1461. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  1462. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring0);
  1463. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring1);
  1464. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring2);
  1465. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring3);
  1466. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring4);
  1467. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring5);
  1468. }
  1469. void b43legacy_dma_tx_resume(struct b43legacy_wldev *dev)
  1470. {
  1471. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring5);
  1472. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring4);
  1473. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring3);
  1474. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring2);
  1475. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring1);
  1476. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring0);
  1477. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  1478. }