dma.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/pci.h>
  19. #include <net/cfg80211.h>
  20. #include <net/mac80211.h>
  21. #include <brcmu_utils.h>
  22. #include <aiutils.h>
  23. #include "types.h"
  24. #include "main.h"
  25. #include "dma.h"
  26. #include "soc.h"
  27. #include "scb.h"
  28. #include "ampdu.h"
  29. #include "debug.h"
  30. /*
  31. * dma register field offset calculation
  32. */
  33. #define DMA64REGOFFS(field) offsetof(struct dma64regs, field)
  34. #define DMA64TXREGOFFS(di, field) (di->d64txregbase + DMA64REGOFFS(field))
  35. #define DMA64RXREGOFFS(di, field) (di->d64rxregbase + DMA64REGOFFS(field))
  36. /*
  37. * DMA hardware requires each descriptor ring to be 8kB aligned, and fit within
  38. * a contiguous 8kB physical address.
  39. */
  40. #define D64RINGALIGN_BITS 13
  41. #define D64MAXRINGSZ (1 << D64RINGALIGN_BITS)
  42. #define D64RINGALIGN (1 << D64RINGALIGN_BITS)
  43. #define D64MAXDD (D64MAXRINGSZ / sizeof(struct dma64desc))
  44. /* transmit channel control */
  45. #define D64_XC_XE 0x00000001 /* transmit enable */
  46. #define D64_XC_SE 0x00000002 /* transmit suspend request */
  47. #define D64_XC_LE 0x00000004 /* loopback enable */
  48. #define D64_XC_FL 0x00000010 /* flush request */
  49. #define D64_XC_PD 0x00000800 /* parity check disable */
  50. #define D64_XC_AE 0x00030000 /* address extension bits */
  51. #define D64_XC_AE_SHIFT 16
  52. /* transmit descriptor table pointer */
  53. #define D64_XP_LD_MASK 0x00000fff /* last valid descriptor */
  54. /* transmit channel status */
  55. #define D64_XS0_CD_MASK 0x00001fff /* current descriptor pointer */
  56. #define D64_XS0_XS_MASK 0xf0000000 /* transmit state */
  57. #define D64_XS0_XS_SHIFT 28
  58. #define D64_XS0_XS_DISABLED 0x00000000 /* disabled */
  59. #define D64_XS0_XS_ACTIVE 0x10000000 /* active */
  60. #define D64_XS0_XS_IDLE 0x20000000 /* idle wait */
  61. #define D64_XS0_XS_STOPPED 0x30000000 /* stopped */
  62. #define D64_XS0_XS_SUSP 0x40000000 /* suspend pending */
  63. #define D64_XS1_AD_MASK 0x00001fff /* active descriptor */
  64. #define D64_XS1_XE_MASK 0xf0000000 /* transmit errors */
  65. #define D64_XS1_XE_SHIFT 28
  66. #define D64_XS1_XE_NOERR 0x00000000 /* no error */
  67. #define D64_XS1_XE_DPE 0x10000000 /* descriptor protocol error */
  68. #define D64_XS1_XE_DFU 0x20000000 /* data fifo underrun */
  69. #define D64_XS1_XE_DTE 0x30000000 /* data transfer error */
  70. #define D64_XS1_XE_DESRE 0x40000000 /* descriptor read error */
  71. #define D64_XS1_XE_COREE 0x50000000 /* core error */
  72. /* receive channel control */
  73. /* receive enable */
  74. #define D64_RC_RE 0x00000001
  75. /* receive frame offset */
  76. #define D64_RC_RO_MASK 0x000000fe
  77. #define D64_RC_RO_SHIFT 1
  78. /* direct fifo receive (pio) mode */
  79. #define D64_RC_FM 0x00000100
  80. /* separate rx header descriptor enable */
  81. #define D64_RC_SH 0x00000200
  82. /* overflow continue */
  83. #define D64_RC_OC 0x00000400
  84. /* parity check disable */
  85. #define D64_RC_PD 0x00000800
  86. /* address extension bits */
  87. #define D64_RC_AE 0x00030000
  88. #define D64_RC_AE_SHIFT 16
  89. /* flags for dma controller */
  90. /* partity enable */
  91. #define DMA_CTRL_PEN (1 << 0)
  92. /* rx overflow continue */
  93. #define DMA_CTRL_ROC (1 << 1)
  94. /* allow rx scatter to multiple descriptors */
  95. #define DMA_CTRL_RXMULTI (1 << 2)
  96. /* Unframed Rx/Tx data */
  97. #define DMA_CTRL_UNFRAMED (1 << 3)
  98. /* receive descriptor table pointer */
  99. #define D64_RP_LD_MASK 0x00000fff /* last valid descriptor */
  100. /* receive channel status */
  101. #define D64_RS0_CD_MASK 0x00001fff /* current descriptor pointer */
  102. #define D64_RS0_RS_MASK 0xf0000000 /* receive state */
  103. #define D64_RS0_RS_SHIFT 28
  104. #define D64_RS0_RS_DISABLED 0x00000000 /* disabled */
  105. #define D64_RS0_RS_ACTIVE 0x10000000 /* active */
  106. #define D64_RS0_RS_IDLE 0x20000000 /* idle wait */
  107. #define D64_RS0_RS_STOPPED 0x30000000 /* stopped */
  108. #define D64_RS0_RS_SUSP 0x40000000 /* suspend pending */
  109. #define D64_RS1_AD_MASK 0x0001ffff /* active descriptor */
  110. #define D64_RS1_RE_MASK 0xf0000000 /* receive errors */
  111. #define D64_RS1_RE_SHIFT 28
  112. #define D64_RS1_RE_NOERR 0x00000000 /* no error */
  113. #define D64_RS1_RE_DPO 0x10000000 /* descriptor protocol error */
  114. #define D64_RS1_RE_DFU 0x20000000 /* data fifo overflow */
  115. #define D64_RS1_RE_DTE 0x30000000 /* data transfer error */
  116. #define D64_RS1_RE_DESRE 0x40000000 /* descriptor read error */
  117. #define D64_RS1_RE_COREE 0x50000000 /* core error */
  118. /* fifoaddr */
  119. #define D64_FA_OFF_MASK 0xffff /* offset */
  120. #define D64_FA_SEL_MASK 0xf0000 /* select */
  121. #define D64_FA_SEL_SHIFT 16
  122. #define D64_FA_SEL_XDD 0x00000 /* transmit dma data */
  123. #define D64_FA_SEL_XDP 0x10000 /* transmit dma pointers */
  124. #define D64_FA_SEL_RDD 0x40000 /* receive dma data */
  125. #define D64_FA_SEL_RDP 0x50000 /* receive dma pointers */
  126. #define D64_FA_SEL_XFD 0x80000 /* transmit fifo data */
  127. #define D64_FA_SEL_XFP 0x90000 /* transmit fifo pointers */
  128. #define D64_FA_SEL_RFD 0xc0000 /* receive fifo data */
  129. #define D64_FA_SEL_RFP 0xd0000 /* receive fifo pointers */
  130. #define D64_FA_SEL_RSD 0xe0000 /* receive frame status data */
  131. #define D64_FA_SEL_RSP 0xf0000 /* receive frame status pointers */
  132. /* descriptor control flags 1 */
  133. #define D64_CTRL_COREFLAGS 0x0ff00000 /* core specific flags */
  134. #define D64_CTRL1_EOT ((u32)1 << 28) /* end of descriptor table */
  135. #define D64_CTRL1_IOC ((u32)1 << 29) /* interrupt on completion */
  136. #define D64_CTRL1_EOF ((u32)1 << 30) /* end of frame */
  137. #define D64_CTRL1_SOF ((u32)1 << 31) /* start of frame */
  138. /* descriptor control flags 2 */
  139. /* buffer byte count. real data len must <= 16KB */
  140. #define D64_CTRL2_BC_MASK 0x00007fff
  141. /* address extension bits */
  142. #define D64_CTRL2_AE 0x00030000
  143. #define D64_CTRL2_AE_SHIFT 16
  144. /* parity bit */
  145. #define D64_CTRL2_PARITY 0x00040000
  146. /* control flags in the range [27:20] are core-specific and not defined here */
  147. #define D64_CTRL_CORE_MASK 0x0ff00000
  148. #define D64_RX_FRM_STS_LEN 0x0000ffff /* frame length mask */
  149. #define D64_RX_FRM_STS_OVFL 0x00800000 /* RxOverFlow */
  150. #define D64_RX_FRM_STS_DSCRCNT 0x0f000000 /* no. of descriptors used - 1 */
  151. #define D64_RX_FRM_STS_DATATYPE 0xf0000000 /* core-dependent data type */
  152. /*
  153. * packet headroom necessary to accommodate the largest header
  154. * in the system, (i.e TXOFF). By doing, we avoid the need to
  155. * allocate an extra buffer for the header when bridging to WL.
  156. * There is a compile time check in wlc.c which ensure that this
  157. * value is at least as big as TXOFF. This value is used in
  158. * dma_rxfill().
  159. */
  160. #define BCMEXTRAHDROOM 172
  161. #define MAXNAMEL 8 /* 8 char names */
  162. /* macros to convert between byte offsets and indexes */
  163. #define B2I(bytes, type) ((bytes) / sizeof(type))
  164. #define I2B(index, type) ((index) * sizeof(type))
  165. #define PCI32ADDR_HIGH 0xc0000000 /* address[31:30] */
  166. #define PCI32ADDR_HIGH_SHIFT 30 /* address[31:30] */
  167. #define PCI64ADDR_HIGH 0x80000000 /* address[63] */
  168. #define PCI64ADDR_HIGH_SHIFT 31 /* address[63] */
  169. /*
  170. * DMA Descriptor
  171. * Descriptors are only read by the hardware, never written back.
  172. */
  173. struct dma64desc {
  174. __le32 ctrl1; /* misc control bits & bufcount */
  175. __le32 ctrl2; /* buffer count and address extension */
  176. __le32 addrlow; /* memory address of the date buffer, bits 31:0 */
  177. __le32 addrhigh; /* memory address of the date buffer, bits 63:32 */
  178. };
  179. /* dma engine software state */
  180. struct dma_info {
  181. struct dma_pub dma; /* exported structure */
  182. char name[MAXNAMEL]; /* callers name for diag msgs */
  183. struct bcma_device *core;
  184. struct device *dmadev;
  185. /* session information for AMPDU */
  186. struct brcms_ampdu_session ampdu_session;
  187. bool dma64; /* this dma engine is operating in 64-bit mode */
  188. bool addrext; /* this dma engine supports DmaExtendedAddrChanges */
  189. /* 64-bit dma tx engine registers */
  190. uint d64txregbase;
  191. /* 64-bit dma rx engine registers */
  192. uint d64rxregbase;
  193. /* pointer to dma64 tx descriptor ring */
  194. struct dma64desc *txd64;
  195. /* pointer to dma64 rx descriptor ring */
  196. struct dma64desc *rxd64;
  197. u16 dmadesc_align; /* alignment requirement for dma descriptors */
  198. u16 ntxd; /* # tx descriptors tunable */
  199. u16 txin; /* index of next descriptor to reclaim */
  200. u16 txout; /* index of next descriptor to post */
  201. /* pointer to parallel array of pointers to packets */
  202. struct sk_buff **txp;
  203. /* Aligned physical address of descriptor ring */
  204. dma_addr_t txdpa;
  205. /* Original physical address of descriptor ring */
  206. dma_addr_t txdpaorig;
  207. u16 txdalign; /* #bytes added to alloc'd mem to align txd */
  208. u32 txdalloc; /* #bytes allocated for the ring */
  209. u32 xmtptrbase; /* When using unaligned descriptors, the ptr register
  210. * is not just an index, it needs all 13 bits to be
  211. * an offset from the addr register.
  212. */
  213. u16 nrxd; /* # rx descriptors tunable */
  214. u16 rxin; /* index of next descriptor to reclaim */
  215. u16 rxout; /* index of next descriptor to post */
  216. /* pointer to parallel array of pointers to packets */
  217. struct sk_buff **rxp;
  218. /* Aligned physical address of descriptor ring */
  219. dma_addr_t rxdpa;
  220. /* Original physical address of descriptor ring */
  221. dma_addr_t rxdpaorig;
  222. u16 rxdalign; /* #bytes added to alloc'd mem to align rxd */
  223. u32 rxdalloc; /* #bytes allocated for the ring */
  224. u32 rcvptrbase; /* Base for ptr reg when using unaligned descriptors */
  225. /* tunables */
  226. unsigned int rxbufsize; /* rx buffer size in bytes, not including
  227. * the extra headroom
  228. */
  229. uint rxextrahdrroom; /* extra rx headroom, reverseved to assist upper
  230. * stack, e.g. some rx pkt buffers will be
  231. * bridged to tx side without byte copying.
  232. * The extra headroom needs to be large enough
  233. * to fit txheader needs. Some dongle driver may
  234. * not need it.
  235. */
  236. uint nrxpost; /* # rx buffers to keep posted */
  237. unsigned int rxoffset; /* rxcontrol offset */
  238. /* add to get dma address of descriptor ring, low 32 bits */
  239. uint ddoffsetlow;
  240. /* high 32 bits */
  241. uint ddoffsethigh;
  242. /* add to get dma address of data buffer, low 32 bits */
  243. uint dataoffsetlow;
  244. /* high 32 bits */
  245. uint dataoffsethigh;
  246. /* descriptor base need to be aligned or not */
  247. bool aligndesc_4k;
  248. };
  249. /* Check for odd number of 1's */
  250. static u32 parity32(__le32 data)
  251. {
  252. /* no swap needed for counting 1's */
  253. u32 par_data = *(u32 *)&data;
  254. par_data ^= par_data >> 16;
  255. par_data ^= par_data >> 8;
  256. par_data ^= par_data >> 4;
  257. par_data ^= par_data >> 2;
  258. par_data ^= par_data >> 1;
  259. return par_data & 1;
  260. }
  261. static bool dma64_dd_parity(struct dma64desc *dd)
  262. {
  263. return parity32(dd->addrlow ^ dd->addrhigh ^ dd->ctrl1 ^ dd->ctrl2);
  264. }
  265. /* descriptor bumping functions */
  266. static uint xxd(uint x, uint n)
  267. {
  268. return x & (n - 1); /* faster than %, but n must be power of 2 */
  269. }
  270. static uint txd(struct dma_info *di, uint x)
  271. {
  272. return xxd(x, di->ntxd);
  273. }
  274. static uint rxd(struct dma_info *di, uint x)
  275. {
  276. return xxd(x, di->nrxd);
  277. }
  278. static uint nexttxd(struct dma_info *di, uint i)
  279. {
  280. return txd(di, i + 1);
  281. }
  282. static uint prevtxd(struct dma_info *di, uint i)
  283. {
  284. return txd(di, i - 1);
  285. }
  286. static uint nextrxd(struct dma_info *di, uint i)
  287. {
  288. return rxd(di, i + 1);
  289. }
  290. static uint ntxdactive(struct dma_info *di, uint h, uint t)
  291. {
  292. return txd(di, t-h);
  293. }
  294. static uint nrxdactive(struct dma_info *di, uint h, uint t)
  295. {
  296. return rxd(di, t-h);
  297. }
  298. static uint _dma_ctrlflags(struct dma_info *di, uint mask, uint flags)
  299. {
  300. uint dmactrlflags;
  301. if (di == NULL) {
  302. brcms_dbg_dma(di->core, "NULL dma handle\n");
  303. return 0;
  304. }
  305. dmactrlflags = di->dma.dmactrlflags;
  306. dmactrlflags &= ~mask;
  307. dmactrlflags |= flags;
  308. /* If trying to enable parity, check if parity is actually supported */
  309. if (dmactrlflags & DMA_CTRL_PEN) {
  310. u32 control;
  311. control = bcma_read32(di->core, DMA64TXREGOFFS(di, control));
  312. bcma_write32(di->core, DMA64TXREGOFFS(di, control),
  313. control | D64_XC_PD);
  314. if (bcma_read32(di->core, DMA64TXREGOFFS(di, control)) &
  315. D64_XC_PD)
  316. /* We *can* disable it so it is supported,
  317. * restore control register
  318. */
  319. bcma_write32(di->core, DMA64TXREGOFFS(di, control),
  320. control);
  321. else
  322. /* Not supported, don't allow it to be enabled */
  323. dmactrlflags &= ~DMA_CTRL_PEN;
  324. }
  325. di->dma.dmactrlflags = dmactrlflags;
  326. return dmactrlflags;
  327. }
  328. static bool _dma64_addrext(struct dma_info *di, uint ctrl_offset)
  329. {
  330. u32 w;
  331. bcma_set32(di->core, ctrl_offset, D64_XC_AE);
  332. w = bcma_read32(di->core, ctrl_offset);
  333. bcma_mask32(di->core, ctrl_offset, ~D64_XC_AE);
  334. return (w & D64_XC_AE) == D64_XC_AE;
  335. }
  336. /*
  337. * return true if this dma engine supports DmaExtendedAddrChanges,
  338. * otherwise false
  339. */
  340. static bool _dma_isaddrext(struct dma_info *di)
  341. {
  342. /* DMA64 supports full 32- or 64-bit operation. AE is always valid */
  343. /* not all tx or rx channel are available */
  344. if (di->d64txregbase != 0) {
  345. if (!_dma64_addrext(di, DMA64TXREGOFFS(di, control)))
  346. brcms_dbg_dma(di->core,
  347. "%s: DMA64 tx doesn't have AE set\n",
  348. di->name);
  349. return true;
  350. } else if (di->d64rxregbase != 0) {
  351. if (!_dma64_addrext(di, DMA64RXREGOFFS(di, control)))
  352. brcms_dbg_dma(di->core,
  353. "%s: DMA64 rx doesn't have AE set\n",
  354. di->name);
  355. return true;
  356. }
  357. return false;
  358. }
  359. static bool _dma_descriptor_align(struct dma_info *di)
  360. {
  361. u32 addrl;
  362. /* Check to see if the descriptors need to be aligned on 4K/8K or not */
  363. if (di->d64txregbase != 0) {
  364. bcma_write32(di->core, DMA64TXREGOFFS(di, addrlow), 0xff0);
  365. addrl = bcma_read32(di->core, DMA64TXREGOFFS(di, addrlow));
  366. if (addrl != 0)
  367. return false;
  368. } else if (di->d64rxregbase != 0) {
  369. bcma_write32(di->core, DMA64RXREGOFFS(di, addrlow), 0xff0);
  370. addrl = bcma_read32(di->core, DMA64RXREGOFFS(di, addrlow));
  371. if (addrl != 0)
  372. return false;
  373. }
  374. return true;
  375. }
  376. /*
  377. * Descriptor table must start at the DMA hardware dictated alignment, so
  378. * allocated memory must be large enough to support this requirement.
  379. */
  380. static void *dma_alloc_consistent(struct dma_info *di, uint size,
  381. u16 align_bits, uint *alloced,
  382. dma_addr_t *pap)
  383. {
  384. if (align_bits) {
  385. u16 align = (1 << align_bits);
  386. if (!IS_ALIGNED(PAGE_SIZE, align))
  387. size += align;
  388. *alloced = size;
  389. }
  390. return dma_alloc_coherent(di->dmadev, size, pap, GFP_ATOMIC);
  391. }
  392. static
  393. u8 dma_align_sizetobits(uint size)
  394. {
  395. u8 bitpos = 0;
  396. while (size >>= 1)
  397. bitpos++;
  398. return bitpos;
  399. }
  400. /* This function ensures that the DMA descriptor ring will not get allocated
  401. * across Page boundary. If the allocation is done across the page boundary
  402. * at the first time, then it is freed and the allocation is done at
  403. * descriptor ring size aligned location. This will ensure that the ring will
  404. * not cross page boundary
  405. */
  406. static void *dma_ringalloc(struct dma_info *di, u32 boundary, uint size,
  407. u16 *alignbits, uint *alloced,
  408. dma_addr_t *descpa)
  409. {
  410. void *va;
  411. u32 desc_strtaddr;
  412. u32 alignbytes = 1 << *alignbits;
  413. va = dma_alloc_consistent(di, size, *alignbits, alloced, descpa);
  414. if (NULL == va)
  415. return NULL;
  416. desc_strtaddr = (u32) roundup((unsigned long)va, alignbytes);
  417. if (((desc_strtaddr + size - 1) & boundary) != (desc_strtaddr
  418. & boundary)) {
  419. *alignbits = dma_align_sizetobits(size);
  420. dma_free_coherent(di->dmadev, size, va, *descpa);
  421. va = dma_alloc_consistent(di, size, *alignbits,
  422. alloced, descpa);
  423. }
  424. return va;
  425. }
  426. static bool dma64_alloc(struct dma_info *di, uint direction)
  427. {
  428. u16 size;
  429. uint ddlen;
  430. void *va;
  431. uint alloced = 0;
  432. u16 align;
  433. u16 align_bits;
  434. ddlen = sizeof(struct dma64desc);
  435. size = (direction == DMA_TX) ? (di->ntxd * ddlen) : (di->nrxd * ddlen);
  436. align_bits = di->dmadesc_align;
  437. align = (1 << align_bits);
  438. if (direction == DMA_TX) {
  439. va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
  440. &alloced, &di->txdpaorig);
  441. if (va == NULL) {
  442. brcms_dbg_dma(di->core,
  443. "%s: DMA_ALLOC_CONSISTENT(ntxd) failed\n",
  444. di->name);
  445. return false;
  446. }
  447. align = (1 << align_bits);
  448. di->txd64 = (struct dma64desc *)
  449. roundup((unsigned long)va, align);
  450. di->txdalign = (uint) ((s8 *)di->txd64 - (s8 *) va);
  451. di->txdpa = di->txdpaorig + di->txdalign;
  452. di->txdalloc = alloced;
  453. } else {
  454. va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
  455. &alloced, &di->rxdpaorig);
  456. if (va == NULL) {
  457. brcms_dbg_dma(di->core,
  458. "%s: DMA_ALLOC_CONSISTENT(nrxd) failed\n",
  459. di->name);
  460. return false;
  461. }
  462. align = (1 << align_bits);
  463. di->rxd64 = (struct dma64desc *)
  464. roundup((unsigned long)va, align);
  465. di->rxdalign = (uint) ((s8 *)di->rxd64 - (s8 *) va);
  466. di->rxdpa = di->rxdpaorig + di->rxdalign;
  467. di->rxdalloc = alloced;
  468. }
  469. return true;
  470. }
  471. static bool _dma_alloc(struct dma_info *di, uint direction)
  472. {
  473. return dma64_alloc(di, direction);
  474. }
  475. struct dma_pub *dma_attach(char *name, struct brcms_c_info *wlc,
  476. uint txregbase, uint rxregbase, uint ntxd, uint nrxd,
  477. uint rxbufsize, int rxextheadroom,
  478. uint nrxpost, uint rxoffset)
  479. {
  480. struct si_pub *sih = wlc->hw->sih;
  481. struct bcma_device *core = wlc->hw->d11core;
  482. struct dma_info *di;
  483. u8 rev = core->id.rev;
  484. uint size;
  485. struct si_info *sii = container_of(sih, struct si_info, pub);
  486. /* allocate private info structure */
  487. di = kzalloc(sizeof(struct dma_info), GFP_ATOMIC);
  488. if (di == NULL)
  489. return NULL;
  490. di->dma64 =
  491. ((bcma_aread32(core, BCMA_IOST) & SISF_DMA64) == SISF_DMA64);
  492. /* init dma reg info */
  493. di->core = core;
  494. di->d64txregbase = txregbase;
  495. di->d64rxregbase = rxregbase;
  496. /*
  497. * Default flags (which can be changed by the driver calling
  498. * dma_ctrlflags before enable): For backwards compatibility
  499. * both Rx Overflow Continue and Parity are DISABLED.
  500. */
  501. _dma_ctrlflags(di, DMA_CTRL_ROC | DMA_CTRL_PEN, 0);
  502. brcms_dbg_dma(di->core, "%s: %s flags 0x%x ntxd %d nrxd %d "
  503. "rxbufsize %d rxextheadroom %d nrxpost %d rxoffset %d "
  504. "txregbase %u rxregbase %u\n", name, "DMA64",
  505. di->dma.dmactrlflags, ntxd, nrxd, rxbufsize,
  506. rxextheadroom, nrxpost, rxoffset, txregbase, rxregbase);
  507. /* make a private copy of our callers name */
  508. strncpy(di->name, name, MAXNAMEL);
  509. di->name[MAXNAMEL - 1] = '\0';
  510. di->dmadev = core->dma_dev;
  511. /* save tunables */
  512. di->ntxd = (u16) ntxd;
  513. di->nrxd = (u16) nrxd;
  514. /* the actual dma size doesn't include the extra headroom */
  515. di->rxextrahdrroom =
  516. (rxextheadroom == -1) ? BCMEXTRAHDROOM : rxextheadroom;
  517. if (rxbufsize > BCMEXTRAHDROOM)
  518. di->rxbufsize = (u16) (rxbufsize - di->rxextrahdrroom);
  519. else
  520. di->rxbufsize = (u16) rxbufsize;
  521. di->nrxpost = (u16) nrxpost;
  522. di->rxoffset = (u8) rxoffset;
  523. /*
  524. * figure out the DMA physical address offset for dd and data
  525. * PCI/PCIE: they map silicon backplace address to zero
  526. * based memory, need offset
  527. * Other bus: use zero SI_BUS BIGENDIAN kludge: use sdram
  528. * swapped region for data buffer, not descriptor
  529. */
  530. di->ddoffsetlow = 0;
  531. di->dataoffsetlow = 0;
  532. /* for pci bus, add offset */
  533. if (sii->icbus->hosttype == BCMA_HOSTTYPE_PCI) {
  534. /* add offset for pcie with DMA64 bus */
  535. di->ddoffsetlow = 0;
  536. di->ddoffsethigh = SI_PCIE_DMA_H32;
  537. }
  538. di->dataoffsetlow = di->ddoffsetlow;
  539. di->dataoffsethigh = di->ddoffsethigh;
  540. /* WAR64450 : DMACtl.Addr ext fields are not supported in SDIOD core. */
  541. if ((core->id.id == BCMA_CORE_SDIO_DEV)
  542. && ((rev > 0) && (rev <= 2)))
  543. di->addrext = false;
  544. else if ((core->id.id == BCMA_CORE_I2S) &&
  545. ((rev == 0) || (rev == 1)))
  546. di->addrext = false;
  547. else
  548. di->addrext = _dma_isaddrext(di);
  549. /* does the descriptor need to be aligned and if yes, on 4K/8K or not */
  550. di->aligndesc_4k = _dma_descriptor_align(di);
  551. if (di->aligndesc_4k) {
  552. di->dmadesc_align = D64RINGALIGN_BITS;
  553. if ((ntxd < D64MAXDD / 2) && (nrxd < D64MAXDD / 2))
  554. /* for smaller dd table, HW relax alignment reqmnt */
  555. di->dmadesc_align = D64RINGALIGN_BITS - 1;
  556. } else {
  557. di->dmadesc_align = 4; /* 16 byte alignment */
  558. }
  559. brcms_dbg_dma(di->core, "DMA descriptor align_needed %d, align %d\n",
  560. di->aligndesc_4k, di->dmadesc_align);
  561. /* allocate tx packet pointer vector */
  562. if (ntxd) {
  563. size = ntxd * sizeof(void *);
  564. di->txp = kzalloc(size, GFP_ATOMIC);
  565. if (di->txp == NULL)
  566. goto fail;
  567. }
  568. /* allocate rx packet pointer vector */
  569. if (nrxd) {
  570. size = nrxd * sizeof(void *);
  571. di->rxp = kzalloc(size, GFP_ATOMIC);
  572. if (di->rxp == NULL)
  573. goto fail;
  574. }
  575. /*
  576. * allocate transmit descriptor ring, only need ntxd descriptors
  577. * but it must be aligned
  578. */
  579. if (ntxd) {
  580. if (!_dma_alloc(di, DMA_TX))
  581. goto fail;
  582. }
  583. /*
  584. * allocate receive descriptor ring, only need nrxd descriptors
  585. * but it must be aligned
  586. */
  587. if (nrxd) {
  588. if (!_dma_alloc(di, DMA_RX))
  589. goto fail;
  590. }
  591. if ((di->ddoffsetlow != 0) && !di->addrext) {
  592. if (di->txdpa > SI_PCI_DMA_SZ) {
  593. brcms_dbg_dma(di->core,
  594. "%s: txdpa 0x%x: addrext not supported\n",
  595. di->name, (u32)di->txdpa);
  596. goto fail;
  597. }
  598. if (di->rxdpa > SI_PCI_DMA_SZ) {
  599. brcms_dbg_dma(di->core,
  600. "%s: rxdpa 0x%x: addrext not supported\n",
  601. di->name, (u32)di->rxdpa);
  602. goto fail;
  603. }
  604. }
  605. /* Initialize AMPDU session */
  606. brcms_c_ampdu_reset_session(&di->ampdu_session, wlc);
  607. brcms_dbg_dma(di->core,
  608. "ddoffsetlow 0x%x ddoffsethigh 0x%x dataoffsetlow 0x%x dataoffsethigh 0x%x addrext %d\n",
  609. di->ddoffsetlow, di->ddoffsethigh,
  610. di->dataoffsetlow, di->dataoffsethigh,
  611. di->addrext);
  612. return (struct dma_pub *) di;
  613. fail:
  614. dma_detach((struct dma_pub *)di);
  615. return NULL;
  616. }
  617. static inline void
  618. dma64_dd_upd(struct dma_info *di, struct dma64desc *ddring,
  619. dma_addr_t pa, uint outidx, u32 *flags, u32 bufcount)
  620. {
  621. u32 ctrl2 = bufcount & D64_CTRL2_BC_MASK;
  622. /* PCI bus with big(>1G) physical address, use address extension */
  623. if ((di->dataoffsetlow == 0) || !(pa & PCI32ADDR_HIGH)) {
  624. ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
  625. ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
  626. ddring[outidx].ctrl1 = cpu_to_le32(*flags);
  627. ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
  628. } else {
  629. /* address extension for 32-bit PCI */
  630. u32 ae;
  631. ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
  632. pa &= ~PCI32ADDR_HIGH;
  633. ctrl2 |= (ae << D64_CTRL2_AE_SHIFT) & D64_CTRL2_AE;
  634. ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
  635. ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
  636. ddring[outidx].ctrl1 = cpu_to_le32(*flags);
  637. ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
  638. }
  639. if (di->dma.dmactrlflags & DMA_CTRL_PEN) {
  640. if (dma64_dd_parity(&ddring[outidx]))
  641. ddring[outidx].ctrl2 =
  642. cpu_to_le32(ctrl2 | D64_CTRL2_PARITY);
  643. }
  644. }
  645. /* !! may be called with core in reset */
  646. void dma_detach(struct dma_pub *pub)
  647. {
  648. struct dma_info *di = (struct dma_info *)pub;
  649. brcms_dbg_dma(di->core, "%s:\n", di->name);
  650. /* free dma descriptor rings */
  651. if (di->txd64)
  652. dma_free_coherent(di->dmadev, di->txdalloc,
  653. ((s8 *)di->txd64 - di->txdalign),
  654. (di->txdpaorig));
  655. if (di->rxd64)
  656. dma_free_coherent(di->dmadev, di->rxdalloc,
  657. ((s8 *)di->rxd64 - di->rxdalign),
  658. (di->rxdpaorig));
  659. /* free packet pointer vectors */
  660. kfree(di->txp);
  661. kfree(di->rxp);
  662. /* free our private info structure */
  663. kfree(di);
  664. }
  665. /* initialize descriptor table base address */
  666. static void
  667. _dma_ddtable_init(struct dma_info *di, uint direction, dma_addr_t pa)
  668. {
  669. if (!di->aligndesc_4k) {
  670. if (direction == DMA_TX)
  671. di->xmtptrbase = pa;
  672. else
  673. di->rcvptrbase = pa;
  674. }
  675. if ((di->ddoffsetlow == 0)
  676. || !(pa & PCI32ADDR_HIGH)) {
  677. if (direction == DMA_TX) {
  678. bcma_write32(di->core, DMA64TXREGOFFS(di, addrlow),
  679. pa + di->ddoffsetlow);
  680. bcma_write32(di->core, DMA64TXREGOFFS(di, addrhigh),
  681. di->ddoffsethigh);
  682. } else {
  683. bcma_write32(di->core, DMA64RXREGOFFS(di, addrlow),
  684. pa + di->ddoffsetlow);
  685. bcma_write32(di->core, DMA64RXREGOFFS(di, addrhigh),
  686. di->ddoffsethigh);
  687. }
  688. } else {
  689. /* DMA64 32bits address extension */
  690. u32 ae;
  691. /* shift the high bit(s) from pa to ae */
  692. ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
  693. pa &= ~PCI32ADDR_HIGH;
  694. if (direction == DMA_TX) {
  695. bcma_write32(di->core, DMA64TXREGOFFS(di, addrlow),
  696. pa + di->ddoffsetlow);
  697. bcma_write32(di->core, DMA64TXREGOFFS(di, addrhigh),
  698. di->ddoffsethigh);
  699. bcma_maskset32(di->core, DMA64TXREGOFFS(di, control),
  700. D64_XC_AE, (ae << D64_XC_AE_SHIFT));
  701. } else {
  702. bcma_write32(di->core, DMA64RXREGOFFS(di, addrlow),
  703. pa + di->ddoffsetlow);
  704. bcma_write32(di->core, DMA64RXREGOFFS(di, addrhigh),
  705. di->ddoffsethigh);
  706. bcma_maskset32(di->core, DMA64RXREGOFFS(di, control),
  707. D64_RC_AE, (ae << D64_RC_AE_SHIFT));
  708. }
  709. }
  710. }
  711. static void _dma_rxenable(struct dma_info *di)
  712. {
  713. uint dmactrlflags = di->dma.dmactrlflags;
  714. u32 control;
  715. brcms_dbg_dma(di->core, "%s:\n", di->name);
  716. control = D64_RC_RE | (bcma_read32(di->core,
  717. DMA64RXREGOFFS(di, control)) &
  718. D64_RC_AE);
  719. if ((dmactrlflags & DMA_CTRL_PEN) == 0)
  720. control |= D64_RC_PD;
  721. if (dmactrlflags & DMA_CTRL_ROC)
  722. control |= D64_RC_OC;
  723. bcma_write32(di->core, DMA64RXREGOFFS(di, control),
  724. ((di->rxoffset << D64_RC_RO_SHIFT) | control));
  725. }
  726. void dma_rxinit(struct dma_pub *pub)
  727. {
  728. struct dma_info *di = (struct dma_info *)pub;
  729. brcms_dbg_dma(di->core, "%s:\n", di->name);
  730. if (di->nrxd == 0)
  731. return;
  732. di->rxin = di->rxout = 0;
  733. /* clear rx descriptor ring */
  734. memset(di->rxd64, '\0', di->nrxd * sizeof(struct dma64desc));
  735. /* DMA engine with out alignment requirement requires table to be inited
  736. * before enabling the engine
  737. */
  738. if (!di->aligndesc_4k)
  739. _dma_ddtable_init(di, DMA_RX, di->rxdpa);
  740. _dma_rxenable(di);
  741. if (di->aligndesc_4k)
  742. _dma_ddtable_init(di, DMA_RX, di->rxdpa);
  743. }
  744. static struct sk_buff *dma64_getnextrxp(struct dma_info *di, bool forceall)
  745. {
  746. uint i, curr;
  747. struct sk_buff *rxp;
  748. dma_addr_t pa;
  749. i = di->rxin;
  750. /* return if no packets posted */
  751. if (i == di->rxout)
  752. return NULL;
  753. curr =
  754. B2I(((bcma_read32(di->core,
  755. DMA64RXREGOFFS(di, status0)) & D64_RS0_CD_MASK) -
  756. di->rcvptrbase) & D64_RS0_CD_MASK, struct dma64desc);
  757. /* ignore curr if forceall */
  758. if (!forceall && (i == curr))
  759. return NULL;
  760. /* get the packet pointer that corresponds to the rx descriptor */
  761. rxp = di->rxp[i];
  762. di->rxp[i] = NULL;
  763. pa = le32_to_cpu(di->rxd64[i].addrlow) - di->dataoffsetlow;
  764. /* clear this packet from the descriptor ring */
  765. dma_unmap_single(di->dmadev, pa, di->rxbufsize, DMA_FROM_DEVICE);
  766. di->rxd64[i].addrlow = cpu_to_le32(0xdeadbeef);
  767. di->rxd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
  768. di->rxin = nextrxd(di, i);
  769. return rxp;
  770. }
  771. static struct sk_buff *_dma_getnextrxp(struct dma_info *di, bool forceall)
  772. {
  773. if (di->nrxd == 0)
  774. return NULL;
  775. return dma64_getnextrxp(di, forceall);
  776. }
  777. /*
  778. * !! rx entry routine
  779. * returns the number packages in the next frame, or 0 if there are no more
  780. * if DMA_CTRL_RXMULTI is defined, DMA scattering(multiple buffers) is
  781. * supported with pkts chain
  782. * otherwise, it's treated as giant pkt and will be tossed.
  783. * The DMA scattering starts with normal DMA header, followed by first
  784. * buffer data. After it reaches the max size of buffer, the data continues
  785. * in next DMA descriptor buffer WITHOUT DMA header
  786. */
  787. int dma_rx(struct dma_pub *pub, struct sk_buff_head *skb_list)
  788. {
  789. struct dma_info *di = (struct dma_info *)pub;
  790. struct sk_buff_head dma_frames;
  791. struct sk_buff *p, *next;
  792. uint len;
  793. uint pkt_len;
  794. int resid = 0;
  795. int pktcnt = 1;
  796. skb_queue_head_init(&dma_frames);
  797. next_frame:
  798. p = _dma_getnextrxp(di, false);
  799. if (p == NULL)
  800. return 0;
  801. len = le16_to_cpu(*(__le16 *) (p->data));
  802. brcms_dbg_dma(di->core, "%s: dma_rx len %d\n", di->name, len);
  803. dma_spin_for_len(len, p);
  804. /* set actual length */
  805. pkt_len = min((di->rxoffset + len), di->rxbufsize);
  806. __skb_trim(p, pkt_len);
  807. skb_queue_tail(&dma_frames, p);
  808. resid = len - (di->rxbufsize - di->rxoffset);
  809. /* check for single or multi-buffer rx */
  810. if (resid > 0) {
  811. while ((resid > 0) && (p = _dma_getnextrxp(di, false))) {
  812. pkt_len = min_t(uint, resid, di->rxbufsize);
  813. __skb_trim(p, pkt_len);
  814. skb_queue_tail(&dma_frames, p);
  815. resid -= di->rxbufsize;
  816. pktcnt++;
  817. }
  818. #ifdef DEBUG
  819. if (resid > 0) {
  820. uint cur;
  821. cur =
  822. B2I(((bcma_read32(di->core,
  823. DMA64RXREGOFFS(di, status0)) &
  824. D64_RS0_CD_MASK) - di->rcvptrbase) &
  825. D64_RS0_CD_MASK, struct dma64desc);
  826. brcms_dbg_dma(di->core,
  827. "rxin %d rxout %d, hw_curr %d\n",
  828. di->rxin, di->rxout, cur);
  829. }
  830. #endif /* DEBUG */
  831. if ((di->dma.dmactrlflags & DMA_CTRL_RXMULTI) == 0) {
  832. brcms_dbg_dma(di->core, "%s: bad frame length (%d)\n",
  833. di->name, len);
  834. skb_queue_walk_safe(&dma_frames, p, next) {
  835. skb_unlink(p, &dma_frames);
  836. brcmu_pkt_buf_free_skb(p);
  837. }
  838. di->dma.rxgiants++;
  839. pktcnt = 1;
  840. goto next_frame;
  841. }
  842. }
  843. skb_queue_splice_tail(&dma_frames, skb_list);
  844. return pktcnt;
  845. }
  846. static bool dma64_rxidle(struct dma_info *di)
  847. {
  848. brcms_dbg_dma(di->core, "%s:\n", di->name);
  849. if (di->nrxd == 0)
  850. return true;
  851. return ((bcma_read32(di->core,
  852. DMA64RXREGOFFS(di, status0)) & D64_RS0_CD_MASK) ==
  853. (bcma_read32(di->core, DMA64RXREGOFFS(di, ptr)) &
  854. D64_RS0_CD_MASK));
  855. }
  856. static bool dma64_txidle(struct dma_info *di)
  857. {
  858. if (di->ntxd == 0)
  859. return true;
  860. return ((bcma_read32(di->core,
  861. DMA64TXREGOFFS(di, status0)) & D64_XS0_CD_MASK) ==
  862. (bcma_read32(di->core, DMA64TXREGOFFS(di, ptr)) &
  863. D64_XS0_CD_MASK));
  864. }
  865. /*
  866. * post receive buffers
  867. * return false is refill failed completely and ring is empty this will stall
  868. * the rx dma and user might want to call rxfill again asap. This unlikely
  869. * happens on memory-rich NIC, but often on memory-constrained dongle
  870. */
  871. bool dma_rxfill(struct dma_pub *pub)
  872. {
  873. struct dma_info *di = (struct dma_info *)pub;
  874. struct sk_buff *p;
  875. u16 rxin, rxout;
  876. u32 flags = 0;
  877. uint n;
  878. uint i;
  879. dma_addr_t pa;
  880. uint extra_offset = 0;
  881. bool ring_empty;
  882. ring_empty = false;
  883. /*
  884. * Determine how many receive buffers we're lacking
  885. * from the full complement, allocate, initialize,
  886. * and post them, then update the chip rx lastdscr.
  887. */
  888. rxin = di->rxin;
  889. rxout = di->rxout;
  890. n = di->nrxpost - nrxdactive(di, rxin, rxout);
  891. brcms_dbg_dma(di->core, "%s: post %d\n", di->name, n);
  892. if (di->rxbufsize > BCMEXTRAHDROOM)
  893. extra_offset = di->rxextrahdrroom;
  894. for (i = 0; i < n; i++) {
  895. /*
  896. * the di->rxbufsize doesn't include the extra headroom,
  897. * we need to add it to the size to be allocated
  898. */
  899. p = brcmu_pkt_buf_get_skb(di->rxbufsize + extra_offset);
  900. if (p == NULL) {
  901. brcms_dbg_dma(di->core, "%s: out of rxbufs\n",
  902. di->name);
  903. if (i == 0 && dma64_rxidle(di)) {
  904. brcms_dbg_dma(di->core, "%s: ring is empty !\n",
  905. di->name);
  906. ring_empty = true;
  907. }
  908. di->dma.rxnobuf++;
  909. break;
  910. }
  911. /* reserve an extra headroom, if applicable */
  912. if (extra_offset)
  913. skb_pull(p, extra_offset);
  914. /* Do a cached write instead of uncached write since DMA_MAP
  915. * will flush the cache.
  916. */
  917. *(u32 *) (p->data) = 0;
  918. pa = dma_map_single(di->dmadev, p->data, di->rxbufsize,
  919. DMA_FROM_DEVICE);
  920. /* save the free packet pointer */
  921. di->rxp[rxout] = p;
  922. /* reset flags for each descriptor */
  923. flags = 0;
  924. if (rxout == (di->nrxd - 1))
  925. flags = D64_CTRL1_EOT;
  926. dma64_dd_upd(di, di->rxd64, pa, rxout, &flags,
  927. di->rxbufsize);
  928. rxout = nextrxd(di, rxout);
  929. }
  930. di->rxout = rxout;
  931. /* update the chip lastdscr pointer */
  932. bcma_write32(di->core, DMA64RXREGOFFS(di, ptr),
  933. di->rcvptrbase + I2B(rxout, struct dma64desc));
  934. return ring_empty;
  935. }
  936. void dma_rxreclaim(struct dma_pub *pub)
  937. {
  938. struct dma_info *di = (struct dma_info *)pub;
  939. struct sk_buff *p;
  940. brcms_dbg_dma(di->core, "%s:\n", di->name);
  941. while ((p = _dma_getnextrxp(di, true)))
  942. brcmu_pkt_buf_free_skb(p);
  943. }
  944. void dma_counterreset(struct dma_pub *pub)
  945. {
  946. /* reset all software counters */
  947. pub->rxgiants = 0;
  948. pub->rxnobuf = 0;
  949. pub->txnobuf = 0;
  950. }
  951. /* get the address of the var in order to change later */
  952. unsigned long dma_getvar(struct dma_pub *pub, const char *name)
  953. {
  954. struct dma_info *di = (struct dma_info *)pub;
  955. if (!strcmp(name, "&txavail"))
  956. return (unsigned long)&(di->dma.txavail);
  957. return 0;
  958. }
  959. /* 64-bit DMA functions */
  960. void dma_txinit(struct dma_pub *pub)
  961. {
  962. struct dma_info *di = (struct dma_info *)pub;
  963. u32 control = D64_XC_XE;
  964. brcms_dbg_dma(di->core, "%s:\n", di->name);
  965. if (di->ntxd == 0)
  966. return;
  967. di->txin = di->txout = 0;
  968. di->dma.txavail = di->ntxd - 1;
  969. /* clear tx descriptor ring */
  970. memset(di->txd64, '\0', (di->ntxd * sizeof(struct dma64desc)));
  971. /* DMA engine with out alignment requirement requires table to be inited
  972. * before enabling the engine
  973. */
  974. if (!di->aligndesc_4k)
  975. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  976. if ((di->dma.dmactrlflags & DMA_CTRL_PEN) == 0)
  977. control |= D64_XC_PD;
  978. bcma_set32(di->core, DMA64TXREGOFFS(di, control), control);
  979. /* DMA engine with alignment requirement requires table to be inited
  980. * before enabling the engine
  981. */
  982. if (di->aligndesc_4k)
  983. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  984. }
  985. void dma_txsuspend(struct dma_pub *pub)
  986. {
  987. struct dma_info *di = (struct dma_info *)pub;
  988. brcms_dbg_dma(di->core, "%s:\n", di->name);
  989. if (di->ntxd == 0)
  990. return;
  991. bcma_set32(di->core, DMA64TXREGOFFS(di, control), D64_XC_SE);
  992. }
  993. void dma_txresume(struct dma_pub *pub)
  994. {
  995. struct dma_info *di = (struct dma_info *)pub;
  996. brcms_dbg_dma(di->core, "%s:\n", di->name);
  997. if (di->ntxd == 0)
  998. return;
  999. bcma_mask32(di->core, DMA64TXREGOFFS(di, control), ~D64_XC_SE);
  1000. }
  1001. bool dma_txsuspended(struct dma_pub *pub)
  1002. {
  1003. struct dma_info *di = (struct dma_info *)pub;
  1004. return (di->ntxd == 0) ||
  1005. ((bcma_read32(di->core,
  1006. DMA64TXREGOFFS(di, control)) & D64_XC_SE) ==
  1007. D64_XC_SE);
  1008. }
  1009. void dma_txreclaim(struct dma_pub *pub, enum txd_range range)
  1010. {
  1011. struct dma_info *di = (struct dma_info *)pub;
  1012. struct sk_buff *p;
  1013. brcms_dbg_dma(di->core, "%s: %s\n",
  1014. di->name,
  1015. range == DMA_RANGE_ALL ? "all" :
  1016. range == DMA_RANGE_TRANSMITTED ? "transmitted" :
  1017. "transferred");
  1018. if (di->txin == di->txout)
  1019. return;
  1020. while ((p = dma_getnexttxp(pub, range))) {
  1021. /* For unframed data, we don't have any packets to free */
  1022. if (!(di->dma.dmactrlflags & DMA_CTRL_UNFRAMED))
  1023. brcmu_pkt_buf_free_skb(p);
  1024. }
  1025. }
  1026. bool dma_txreset(struct dma_pub *pub)
  1027. {
  1028. struct dma_info *di = (struct dma_info *)pub;
  1029. u32 status;
  1030. if (di->ntxd == 0)
  1031. return true;
  1032. /* suspend tx DMA first */
  1033. bcma_write32(di->core, DMA64TXREGOFFS(di, control), D64_XC_SE);
  1034. SPINWAIT(((status =
  1035. (bcma_read32(di->core, DMA64TXREGOFFS(di, status0)) &
  1036. D64_XS0_XS_MASK)) != D64_XS0_XS_DISABLED) &&
  1037. (status != D64_XS0_XS_IDLE) && (status != D64_XS0_XS_STOPPED),
  1038. 10000);
  1039. bcma_write32(di->core, DMA64TXREGOFFS(di, control), 0);
  1040. SPINWAIT(((status =
  1041. (bcma_read32(di->core, DMA64TXREGOFFS(di, status0)) &
  1042. D64_XS0_XS_MASK)) != D64_XS0_XS_DISABLED), 10000);
  1043. /* wait for the last transaction to complete */
  1044. udelay(300);
  1045. return status == D64_XS0_XS_DISABLED;
  1046. }
  1047. bool dma_rxreset(struct dma_pub *pub)
  1048. {
  1049. struct dma_info *di = (struct dma_info *)pub;
  1050. u32 status;
  1051. if (di->nrxd == 0)
  1052. return true;
  1053. bcma_write32(di->core, DMA64RXREGOFFS(di, control), 0);
  1054. SPINWAIT(((status =
  1055. (bcma_read32(di->core, DMA64RXREGOFFS(di, status0)) &
  1056. D64_RS0_RS_MASK)) != D64_RS0_RS_DISABLED), 10000);
  1057. return status == D64_RS0_RS_DISABLED;
  1058. }
  1059. static void dma_txenq(struct dma_info *di, struct sk_buff *p)
  1060. {
  1061. unsigned char *data;
  1062. uint len;
  1063. u16 txout;
  1064. u32 flags = 0;
  1065. dma_addr_t pa;
  1066. txout = di->txout;
  1067. if (WARN_ON(nexttxd(di, txout) == di->txin))
  1068. return;
  1069. /*
  1070. * obtain and initialize transmit descriptor entry.
  1071. */
  1072. data = p->data;
  1073. len = p->len;
  1074. /* get physical address of buffer start */
  1075. pa = dma_map_single(di->dmadev, data, len, DMA_TO_DEVICE);
  1076. /* With a DMA segment list, Descriptor table is filled
  1077. * using the segment list instead of looping over
  1078. * buffers in multi-chain DMA. Therefore, EOF for SGLIST
  1079. * is when end of segment list is reached.
  1080. */
  1081. flags = D64_CTRL1_SOF | D64_CTRL1_IOC | D64_CTRL1_EOF;
  1082. if (txout == (di->ntxd - 1))
  1083. flags |= D64_CTRL1_EOT;
  1084. dma64_dd_upd(di, di->txd64, pa, txout, &flags, len);
  1085. txout = nexttxd(di, txout);
  1086. /* save the packet */
  1087. di->txp[prevtxd(di, txout)] = p;
  1088. /* bump the tx descriptor index */
  1089. di->txout = txout;
  1090. }
  1091. static void ampdu_finalize(struct dma_info *di)
  1092. {
  1093. struct brcms_ampdu_session *session = &di->ampdu_session;
  1094. struct sk_buff *p;
  1095. if (WARN_ON(skb_queue_empty(&session->skb_list)))
  1096. return;
  1097. brcms_c_ampdu_finalize(session);
  1098. while (!skb_queue_empty(&session->skb_list)) {
  1099. p = skb_dequeue(&session->skb_list);
  1100. dma_txenq(di, p);
  1101. }
  1102. bcma_write32(di->core, DMA64TXREGOFFS(di, ptr),
  1103. di->xmtptrbase + I2B(di->txout, struct dma64desc));
  1104. brcms_c_ampdu_reset_session(session, session->wlc);
  1105. }
  1106. static void prep_ampdu_frame(struct dma_info *di, struct sk_buff *p)
  1107. {
  1108. struct brcms_ampdu_session *session = &di->ampdu_session;
  1109. int ret;
  1110. ret = brcms_c_ampdu_add_frame(session, p);
  1111. if (ret == -ENOSPC) {
  1112. /*
  1113. * AMPDU cannot accomodate this frame. Close out the in-
  1114. * progress AMPDU session and start a new one.
  1115. */
  1116. ampdu_finalize(di);
  1117. ret = brcms_c_ampdu_add_frame(session, p);
  1118. }
  1119. WARN_ON(ret);
  1120. }
  1121. /* Update count of available tx descriptors based on current DMA state */
  1122. static void dma_update_txavail(struct dma_info *di)
  1123. {
  1124. /*
  1125. * Available space is number of descriptors less the number of
  1126. * active descriptors and the number of queued AMPDU frames.
  1127. */
  1128. di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) -
  1129. skb_queue_len(&di->ampdu_session.skb_list) - 1;
  1130. }
  1131. /*
  1132. * !! tx entry routine
  1133. * WARNING: call must check the return value for error.
  1134. * the error(toss frames) could be fatal and cause many subsequent hard
  1135. * to debug problems
  1136. */
  1137. int dma_txfast(struct brcms_c_info *wlc, struct dma_pub *pub,
  1138. struct sk_buff *p)
  1139. {
  1140. struct dma_info *di = (struct dma_info *)pub;
  1141. struct brcms_ampdu_session *session = &di->ampdu_session;
  1142. struct ieee80211_tx_info *tx_info;
  1143. bool is_ampdu;
  1144. brcms_dbg_dma(di->core, "%s:\n", di->name);
  1145. /* no use to transmit a zero length packet */
  1146. if (p->len == 0)
  1147. return 0;
  1148. /* return nonzero if out of tx descriptors */
  1149. if (di->dma.txavail == 0 || nexttxd(di, di->txout) == di->txin)
  1150. goto outoftxd;
  1151. tx_info = IEEE80211_SKB_CB(p);
  1152. is_ampdu = tx_info->flags & IEEE80211_TX_CTL_AMPDU;
  1153. if (is_ampdu)
  1154. prep_ampdu_frame(di, p);
  1155. else
  1156. dma_txenq(di, p);
  1157. /* tx flow control */
  1158. dma_update_txavail(di);
  1159. /* kick the chip */
  1160. if (is_ampdu) {
  1161. /*
  1162. * Start sending data if we've got a full AMPDU, there's
  1163. * no more space in the DMA ring, or the ring isn't
  1164. * currently transmitting.
  1165. */
  1166. if (skb_queue_len(&session->skb_list) == session->max_ampdu_frames ||
  1167. di->dma.txavail == 0 || dma64_txidle(di))
  1168. ampdu_finalize(di);
  1169. } else {
  1170. bcma_write32(di->core, DMA64TXREGOFFS(di, ptr),
  1171. di->xmtptrbase + I2B(di->txout, struct dma64desc));
  1172. }
  1173. return 0;
  1174. outoftxd:
  1175. brcms_dbg_dma(di->core, "%s: out of txds !!!\n", di->name);
  1176. brcmu_pkt_buf_free_skb(p);
  1177. di->dma.txavail = 0;
  1178. di->dma.txnobuf++;
  1179. return -ENOSPC;
  1180. }
  1181. void dma_txflush(struct dma_pub *pub)
  1182. {
  1183. struct dma_info *di = (struct dma_info *)pub;
  1184. struct brcms_ampdu_session *session = &di->ampdu_session;
  1185. if (!skb_queue_empty(&session->skb_list))
  1186. ampdu_finalize(di);
  1187. }
  1188. int dma_txpending(struct dma_pub *pub)
  1189. {
  1190. struct dma_info *di = (struct dma_info *)pub;
  1191. return ntxdactive(di, di->txin, di->txout);
  1192. }
  1193. /*
  1194. * If we have an active AMPDU session and are not transmitting,
  1195. * this function will force tx to start.
  1196. */
  1197. void dma_kick_tx(struct dma_pub *pub)
  1198. {
  1199. struct dma_info *di = (struct dma_info *)pub;
  1200. struct brcms_ampdu_session *session = &di->ampdu_session;
  1201. if (!skb_queue_empty(&session->skb_list) && dma64_txidle(di))
  1202. ampdu_finalize(di);
  1203. }
  1204. /*
  1205. * Reclaim next completed txd (txds if using chained buffers) in the range
  1206. * specified and return associated packet.
  1207. * If range is DMA_RANGE_TRANSMITTED, reclaim descriptors that have be
  1208. * transmitted as noted by the hardware "CurrDescr" pointer.
  1209. * If range is DMA_RANGE_TRANSFERED, reclaim descriptors that have be
  1210. * transferred by the DMA as noted by the hardware "ActiveDescr" pointer.
  1211. * If range is DMA_RANGE_ALL, reclaim all txd(s) posted to the ring and
  1212. * return associated packet regardless of the value of hardware pointers.
  1213. */
  1214. struct sk_buff *dma_getnexttxp(struct dma_pub *pub, enum txd_range range)
  1215. {
  1216. struct dma_info *di = (struct dma_info *)pub;
  1217. u16 start, end, i;
  1218. u16 active_desc;
  1219. struct sk_buff *txp;
  1220. brcms_dbg_dma(di->core, "%s: %s\n",
  1221. di->name,
  1222. range == DMA_RANGE_ALL ? "all" :
  1223. range == DMA_RANGE_TRANSMITTED ? "transmitted" :
  1224. "transferred");
  1225. if (di->ntxd == 0)
  1226. return NULL;
  1227. txp = NULL;
  1228. start = di->txin;
  1229. if (range == DMA_RANGE_ALL)
  1230. end = di->txout;
  1231. else {
  1232. end = (u16) (B2I(((bcma_read32(di->core,
  1233. DMA64TXREGOFFS(di, status0)) &
  1234. D64_XS0_CD_MASK) - di->xmtptrbase) &
  1235. D64_XS0_CD_MASK, struct dma64desc));
  1236. if (range == DMA_RANGE_TRANSFERED) {
  1237. active_desc =
  1238. (u16)(bcma_read32(di->core,
  1239. DMA64TXREGOFFS(di, status1)) &
  1240. D64_XS1_AD_MASK);
  1241. active_desc =
  1242. (active_desc - di->xmtptrbase) & D64_XS0_CD_MASK;
  1243. active_desc = B2I(active_desc, struct dma64desc);
  1244. if (end != active_desc)
  1245. end = prevtxd(di, active_desc);
  1246. }
  1247. }
  1248. if ((start == 0) && (end > di->txout))
  1249. goto bogus;
  1250. for (i = start; i != end && !txp; i = nexttxd(di, i)) {
  1251. dma_addr_t pa;
  1252. uint size;
  1253. pa = le32_to_cpu(di->txd64[i].addrlow) - di->dataoffsetlow;
  1254. size =
  1255. (le32_to_cpu(di->txd64[i].ctrl2) &
  1256. D64_CTRL2_BC_MASK);
  1257. di->txd64[i].addrlow = cpu_to_le32(0xdeadbeef);
  1258. di->txd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
  1259. txp = di->txp[i];
  1260. di->txp[i] = NULL;
  1261. dma_unmap_single(di->dmadev, pa, size, DMA_TO_DEVICE);
  1262. }
  1263. di->txin = i;
  1264. /* tx flow control */
  1265. dma_update_txavail(di);
  1266. return txp;
  1267. bogus:
  1268. brcms_dbg_dma(di->core, "bogus curr: start %d end %d txout %d\n",
  1269. start, end, di->txout);
  1270. return NULL;
  1271. }
  1272. /*
  1273. * Mac80211 initiated actions sometimes require packets in the DMA queue to be
  1274. * modified. The modified portion of the packet is not under control of the DMA
  1275. * engine. This function calls a caller-supplied function for each packet in
  1276. * the caller specified dma chain.
  1277. */
  1278. void dma_walk_packets(struct dma_pub *dmah, void (*callback_fnc)
  1279. (void *pkt, void *arg_a), void *arg_a)
  1280. {
  1281. struct dma_info *di = (struct dma_info *) dmah;
  1282. uint i = di->txin;
  1283. uint end = di->txout;
  1284. struct sk_buff *skb;
  1285. struct ieee80211_tx_info *tx_info;
  1286. while (i != end) {
  1287. skb = di->txp[i];
  1288. if (skb != NULL) {
  1289. tx_info = (struct ieee80211_tx_info *)skb->cb;
  1290. (callback_fnc)(tx_info, arg_a);
  1291. }
  1292. i = nexttxd(di, i);
  1293. }
  1294. }