dma.c 39 KB

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