dma.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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 **in_skb)
  1088. {
  1089. struct sk_buff *skb = *in_skb;
  1090. const struct b43legacy_dma_ops *ops = ring->ops;
  1091. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1092. u8 *header;
  1093. int slot, old_top_slot, old_used_slots;
  1094. int err;
  1095. struct b43legacy_dmadesc_generic *desc;
  1096. struct b43legacy_dmadesc_meta *meta;
  1097. struct b43legacy_dmadesc_meta *meta_hdr;
  1098. struct sk_buff *bounce_skb;
  1099. #define SLOTS_PER_PACKET 2
  1100. B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags != 0);
  1101. old_top_slot = ring->current_slot;
  1102. old_used_slots = ring->used_slots;
  1103. /* Get a slot for the header. */
  1104. slot = request_slot(ring);
  1105. desc = ops->idx2desc(ring, slot, &meta_hdr);
  1106. memset(meta_hdr, 0, sizeof(*meta_hdr));
  1107. header = &(ring->txhdr_cache[slot * sizeof(
  1108. struct b43legacy_txhdr_fw3)]);
  1109. err = b43legacy_generate_txhdr(ring->dev, header,
  1110. skb->data, skb->len, info,
  1111. generate_cookie(ring, slot));
  1112. if (unlikely(err)) {
  1113. ring->current_slot = old_top_slot;
  1114. ring->used_slots = old_used_slots;
  1115. return err;
  1116. }
  1117. meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
  1118. sizeof(struct b43legacy_txhdr_fw3), 1);
  1119. if (b43legacy_dma_mapping_error(ring, meta_hdr->dmaaddr,
  1120. sizeof(struct b43legacy_txhdr_fw3), 1)) {
  1121. ring->current_slot = old_top_slot;
  1122. ring->used_slots = old_used_slots;
  1123. return -EIO;
  1124. }
  1125. ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr,
  1126. sizeof(struct b43legacy_txhdr_fw3), 1, 0, 0);
  1127. /* Get a slot for the payload. */
  1128. slot = request_slot(ring);
  1129. desc = ops->idx2desc(ring, slot, &meta);
  1130. memset(meta, 0, sizeof(*meta));
  1131. meta->skb = skb;
  1132. meta->is_last_fragment = 1;
  1133. meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
  1134. /* create a bounce buffer in zone_dma on mapping failure. */
  1135. if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
  1136. bounce_skb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
  1137. if (!bounce_skb) {
  1138. ring->current_slot = old_top_slot;
  1139. ring->used_slots = old_used_slots;
  1140. err = -ENOMEM;
  1141. goto out_unmap_hdr;
  1142. }
  1143. memcpy(skb_put(bounce_skb, skb->len), skb->data, skb->len);
  1144. memcpy(bounce_skb->cb, skb->cb, sizeof(skb->cb));
  1145. bounce_skb->dev = skb->dev;
  1146. skb_set_queue_mapping(bounce_skb, skb_get_queue_mapping(skb));
  1147. info = IEEE80211_SKB_CB(bounce_skb);
  1148. dev_kfree_skb_any(skb);
  1149. skb = bounce_skb;
  1150. *in_skb = bounce_skb;
  1151. meta->skb = skb;
  1152. meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
  1153. if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
  1154. ring->current_slot = old_top_slot;
  1155. ring->used_slots = old_used_slots;
  1156. err = -EIO;
  1157. goto out_free_bounce;
  1158. }
  1159. }
  1160. ops->fill_descriptor(ring, desc, meta->dmaaddr,
  1161. skb->len, 0, 1, 1);
  1162. wmb(); /* previous stuff MUST be done */
  1163. /* Now transfer the whole frame. */
  1164. ops->poke_tx(ring, next_slot(ring, slot));
  1165. return 0;
  1166. out_free_bounce:
  1167. dev_kfree_skb_any(skb);
  1168. out_unmap_hdr:
  1169. unmap_descbuffer(ring, meta_hdr->dmaaddr,
  1170. sizeof(struct b43legacy_txhdr_fw3), 1);
  1171. return err;
  1172. }
  1173. static inline
  1174. int should_inject_overflow(struct b43legacy_dmaring *ring)
  1175. {
  1176. #ifdef CONFIG_B43LEGACY_DEBUG
  1177. if (unlikely(b43legacy_debug(ring->dev,
  1178. B43legacy_DBG_DMAOVERFLOW))) {
  1179. /* Check if we should inject another ringbuffer overflow
  1180. * to test handling of this situation in the stack. */
  1181. unsigned long next_overflow;
  1182. next_overflow = ring->last_injected_overflow + HZ;
  1183. if (time_after(jiffies, next_overflow)) {
  1184. ring->last_injected_overflow = jiffies;
  1185. b43legacydbg(ring->dev->wl,
  1186. "Injecting TX ring overflow on "
  1187. "DMA controller %d\n", ring->index);
  1188. return 1;
  1189. }
  1190. }
  1191. #endif /* CONFIG_B43LEGACY_DEBUG */
  1192. return 0;
  1193. }
  1194. int b43legacy_dma_tx(struct b43legacy_wldev *dev,
  1195. struct sk_buff *skb)
  1196. {
  1197. struct b43legacy_dmaring *ring;
  1198. struct ieee80211_hdr *hdr;
  1199. int err = 0;
  1200. unsigned long flags;
  1201. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1202. ring = priority_to_txring(dev, skb_get_queue_mapping(skb));
  1203. spin_lock_irqsave(&ring->lock, flags);
  1204. B43legacy_WARN_ON(!ring->tx);
  1205. if (unlikely(ring->stopped)) {
  1206. /* We get here only because of a bug in mac80211.
  1207. * Because of a race, one packet may be queued after
  1208. * the queue is stopped, thus we got called when we shouldn't.
  1209. * For now, just refuse the transmit. */
  1210. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1211. b43legacyerr(dev->wl, "Packet after queue stopped\n");
  1212. err = -ENOSPC;
  1213. goto out_unlock;
  1214. }
  1215. if (unlikely(WARN_ON(free_slots(ring) < SLOTS_PER_PACKET))) {
  1216. /* If we get here, we have a real error with the queue
  1217. * full, but queues not stopped. */
  1218. b43legacyerr(dev->wl, "DMA queue overflow\n");
  1219. err = -ENOSPC;
  1220. goto out_unlock;
  1221. }
  1222. /* dma_tx_fragment might reallocate the skb, so invalidate pointers pointing
  1223. * into the skb data or cb now. */
  1224. hdr = NULL;
  1225. info = NULL;
  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. ring->nr_tx_packets++;
  1239. if ((free_slots(ring) < SLOTS_PER_PACKET) ||
  1240. should_inject_overflow(ring)) {
  1241. /* This TX ring is full. */
  1242. ieee80211_stop_queue(dev->wl->hw, txring_to_priority(ring));
  1243. ring->stopped = 1;
  1244. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1245. b43legacydbg(dev->wl, "Stopped TX ring %d\n",
  1246. ring->index);
  1247. }
  1248. out_unlock:
  1249. spin_unlock_irqrestore(&ring->lock, flags);
  1250. return err;
  1251. }
  1252. void b43legacy_dma_handle_txstatus(struct b43legacy_wldev *dev,
  1253. const struct b43legacy_txstatus *status)
  1254. {
  1255. const struct b43legacy_dma_ops *ops;
  1256. struct b43legacy_dmaring *ring;
  1257. struct b43legacy_dmadesc_generic *desc;
  1258. struct b43legacy_dmadesc_meta *meta;
  1259. int retry_limit;
  1260. int slot;
  1261. ring = parse_cookie(dev, status->cookie, &slot);
  1262. if (unlikely(!ring))
  1263. return;
  1264. B43legacy_WARN_ON(!irqs_disabled());
  1265. spin_lock(&ring->lock);
  1266. B43legacy_WARN_ON(!ring->tx);
  1267. ops = ring->ops;
  1268. while (1) {
  1269. B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
  1270. desc = ops->idx2desc(ring, slot, &meta);
  1271. if (meta->skb)
  1272. unmap_descbuffer(ring, meta->dmaaddr,
  1273. meta->skb->len, 1);
  1274. else
  1275. unmap_descbuffer(ring, meta->dmaaddr,
  1276. sizeof(struct b43legacy_txhdr_fw3),
  1277. 1);
  1278. if (meta->is_last_fragment) {
  1279. struct ieee80211_tx_info *info;
  1280. BUG_ON(!meta->skb);
  1281. info = IEEE80211_SKB_CB(meta->skb);
  1282. /* preserve the confiured retry limit before clearing the status
  1283. * The xmit function has overwritten the rc's value with the actual
  1284. * retry limit done by the hardware */
  1285. retry_limit = info->status.rates[0].count;
  1286. ieee80211_tx_info_clear_status(info);
  1287. if (status->acked)
  1288. info->flags |= IEEE80211_TX_STAT_ACK;
  1289. if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
  1290. /*
  1291. * If the short retries (RTS, not data frame) have exceeded
  1292. * the limit, the hw will not have tried the selected rate,
  1293. * but will have used the fallback rate instead.
  1294. * Don't let the rate control count attempts for the selected
  1295. * rate in this case, otherwise the statistics will be off.
  1296. */
  1297. info->status.rates[0].count = 0;
  1298. info->status.rates[1].count = status->frame_count;
  1299. } else {
  1300. if (status->frame_count > retry_limit) {
  1301. info->status.rates[0].count = retry_limit;
  1302. info->status.rates[1].count = status->frame_count -
  1303. retry_limit;
  1304. } else {
  1305. info->status.rates[0].count = status->frame_count;
  1306. info->status.rates[1].idx = -1;
  1307. }
  1308. }
  1309. /* Call back to inform the ieee80211 subsystem about the
  1310. * status of the transmission.
  1311. * Some fields of txstat are already filled in dma_tx().
  1312. */
  1313. ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb);
  1314. /* skb is freed by ieee80211_tx_status_irqsafe() */
  1315. meta->skb = NULL;
  1316. } else {
  1317. /* No need to call free_descriptor_buffer here, as
  1318. * this is only the txhdr, which is not allocated.
  1319. */
  1320. B43legacy_WARN_ON(meta->skb != NULL);
  1321. }
  1322. /* Everything unmapped and free'd. So it's not used anymore. */
  1323. ring->used_slots--;
  1324. if (meta->is_last_fragment)
  1325. break;
  1326. slot = next_slot(ring, slot);
  1327. }
  1328. dev->stats.last_tx = jiffies;
  1329. if (ring->stopped) {
  1330. B43legacy_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
  1331. ieee80211_wake_queue(dev->wl->hw, txring_to_priority(ring));
  1332. ring->stopped = 0;
  1333. if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
  1334. b43legacydbg(dev->wl, "Woke up TX ring %d\n",
  1335. ring->index);
  1336. }
  1337. spin_unlock(&ring->lock);
  1338. }
  1339. void b43legacy_dma_get_tx_stats(struct b43legacy_wldev *dev,
  1340. struct ieee80211_tx_queue_stats *stats)
  1341. {
  1342. const int nr_queues = dev->wl->hw->queues;
  1343. struct b43legacy_dmaring *ring;
  1344. unsigned long flags;
  1345. int i;
  1346. for (i = 0; i < nr_queues; i++) {
  1347. ring = priority_to_txring(dev, i);
  1348. spin_lock_irqsave(&ring->lock, flags);
  1349. stats[i].len = ring->used_slots / SLOTS_PER_PACKET;
  1350. stats[i].limit = ring->nr_slots / SLOTS_PER_PACKET;
  1351. stats[i].count = ring->nr_tx_packets;
  1352. spin_unlock_irqrestore(&ring->lock, flags);
  1353. }
  1354. }
  1355. static void dma_rx(struct b43legacy_dmaring *ring,
  1356. int *slot)
  1357. {
  1358. const struct b43legacy_dma_ops *ops = ring->ops;
  1359. struct b43legacy_dmadesc_generic *desc;
  1360. struct b43legacy_dmadesc_meta *meta;
  1361. struct b43legacy_rxhdr_fw3 *rxhdr;
  1362. struct sk_buff *skb;
  1363. u16 len;
  1364. int err;
  1365. dma_addr_t dmaaddr;
  1366. desc = ops->idx2desc(ring, *slot, &meta);
  1367. sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
  1368. skb = meta->skb;
  1369. if (ring->index == 3) {
  1370. /* We received an xmit status. */
  1371. struct b43legacy_hwtxstatus *hw =
  1372. (struct b43legacy_hwtxstatus *)skb->data;
  1373. int i = 0;
  1374. while (hw->cookie == 0) {
  1375. if (i > 100)
  1376. break;
  1377. i++;
  1378. udelay(2);
  1379. barrier();
  1380. }
  1381. b43legacy_handle_hwtxstatus(ring->dev, hw);
  1382. /* recycle the descriptor buffer. */
  1383. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1384. ring->rx_buffersize);
  1385. return;
  1386. }
  1387. rxhdr = (struct b43legacy_rxhdr_fw3 *)skb->data;
  1388. len = le16_to_cpu(rxhdr->frame_len);
  1389. if (len == 0) {
  1390. int i = 0;
  1391. do {
  1392. udelay(2);
  1393. barrier();
  1394. len = le16_to_cpu(rxhdr->frame_len);
  1395. } while (len == 0 && i++ < 5);
  1396. if (unlikely(len == 0)) {
  1397. /* recycle the descriptor buffer. */
  1398. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1399. ring->rx_buffersize);
  1400. goto drop;
  1401. }
  1402. }
  1403. if (unlikely(len > ring->rx_buffersize)) {
  1404. /* The data did not fit into one descriptor buffer
  1405. * and is split over multiple buffers.
  1406. * This should never happen, as we try to allocate buffers
  1407. * big enough. So simply ignore this packet.
  1408. */
  1409. int cnt = 0;
  1410. s32 tmp = len;
  1411. while (1) {
  1412. desc = ops->idx2desc(ring, *slot, &meta);
  1413. /* recycle the descriptor buffer. */
  1414. sync_descbuffer_for_device(ring, meta->dmaaddr,
  1415. ring->rx_buffersize);
  1416. *slot = next_slot(ring, *slot);
  1417. cnt++;
  1418. tmp -= ring->rx_buffersize;
  1419. if (tmp <= 0)
  1420. break;
  1421. }
  1422. b43legacyerr(ring->dev->wl, "DMA RX buffer too small "
  1423. "(len: %u, buffer: %u, nr-dropped: %d)\n",
  1424. len, ring->rx_buffersize, cnt);
  1425. goto drop;
  1426. }
  1427. dmaaddr = meta->dmaaddr;
  1428. err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
  1429. if (unlikely(err)) {
  1430. b43legacydbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer()"
  1431. " failed\n");
  1432. sync_descbuffer_for_device(ring, dmaaddr,
  1433. ring->rx_buffersize);
  1434. goto drop;
  1435. }
  1436. unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
  1437. skb_put(skb, len + ring->frameoffset);
  1438. skb_pull(skb, ring->frameoffset);
  1439. b43legacy_rx(ring->dev, skb, rxhdr);
  1440. drop:
  1441. return;
  1442. }
  1443. void b43legacy_dma_rx(struct b43legacy_dmaring *ring)
  1444. {
  1445. const struct b43legacy_dma_ops *ops = ring->ops;
  1446. int slot;
  1447. int current_slot;
  1448. int used_slots = 0;
  1449. B43legacy_WARN_ON(ring->tx);
  1450. current_slot = ops->get_current_rxslot(ring);
  1451. B43legacy_WARN_ON(!(current_slot >= 0 && current_slot <
  1452. ring->nr_slots));
  1453. slot = ring->current_slot;
  1454. for (; slot != current_slot; slot = next_slot(ring, slot)) {
  1455. dma_rx(ring, &slot);
  1456. update_max_used_slots(ring, ++used_slots);
  1457. }
  1458. ops->set_current_rxslot(ring, slot);
  1459. ring->current_slot = slot;
  1460. }
  1461. static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring *ring)
  1462. {
  1463. unsigned long flags;
  1464. spin_lock_irqsave(&ring->lock, flags);
  1465. B43legacy_WARN_ON(!ring->tx);
  1466. ring->ops->tx_suspend(ring);
  1467. spin_unlock_irqrestore(&ring->lock, flags);
  1468. }
  1469. static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring *ring)
  1470. {
  1471. unsigned long flags;
  1472. spin_lock_irqsave(&ring->lock, flags);
  1473. B43legacy_WARN_ON(!ring->tx);
  1474. ring->ops->tx_resume(ring);
  1475. spin_unlock_irqrestore(&ring->lock, flags);
  1476. }
  1477. void b43legacy_dma_tx_suspend(struct b43legacy_wldev *dev)
  1478. {
  1479. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  1480. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring0);
  1481. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring1);
  1482. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring2);
  1483. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring3);
  1484. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring4);
  1485. b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring5);
  1486. }
  1487. void b43legacy_dma_tx_resume(struct b43legacy_wldev *dev)
  1488. {
  1489. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring5);
  1490. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring4);
  1491. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring3);
  1492. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring2);
  1493. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring1);
  1494. b43legacy_dma_tx_resume_ring(dev->dma.tx_ring0);
  1495. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  1496. }