dma.c 38 KB

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