dma.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/pci.h>
  19. #include <brcmu_utils.h>
  20. #include <aiutils.h>
  21. #include "types.h"
  22. #include "dma.h"
  23. #include "soc.h"
  24. /*
  25. * DMA hardware requires each descriptor ring to be 8kB aligned, and fit within
  26. * a contiguous 8kB physical address.
  27. */
  28. #define D64RINGALIGN_BITS 13
  29. #define D64MAXRINGSZ (1 << D64RINGALIGN_BITS)
  30. #define D64RINGALIGN (1 << D64RINGALIGN_BITS)
  31. #define D64MAXDD (D64MAXRINGSZ / sizeof(struct dma64desc))
  32. /* transmit channel control */
  33. #define D64_XC_XE 0x00000001 /* transmit enable */
  34. #define D64_XC_SE 0x00000002 /* transmit suspend request */
  35. #define D64_XC_LE 0x00000004 /* loopback enable */
  36. #define D64_XC_FL 0x00000010 /* flush request */
  37. #define D64_XC_PD 0x00000800 /* parity check disable */
  38. #define D64_XC_AE 0x00030000 /* address extension bits */
  39. #define D64_XC_AE_SHIFT 16
  40. /* transmit descriptor table pointer */
  41. #define D64_XP_LD_MASK 0x00000fff /* last valid descriptor */
  42. /* transmit channel status */
  43. #define D64_XS0_CD_MASK 0x00001fff /* current descriptor pointer */
  44. #define D64_XS0_XS_MASK 0xf0000000 /* transmit state */
  45. #define D64_XS0_XS_SHIFT 28
  46. #define D64_XS0_XS_DISABLED 0x00000000 /* disabled */
  47. #define D64_XS0_XS_ACTIVE 0x10000000 /* active */
  48. #define D64_XS0_XS_IDLE 0x20000000 /* idle wait */
  49. #define D64_XS0_XS_STOPPED 0x30000000 /* stopped */
  50. #define D64_XS0_XS_SUSP 0x40000000 /* suspend pending */
  51. #define D64_XS1_AD_MASK 0x00001fff /* active descriptor */
  52. #define D64_XS1_XE_MASK 0xf0000000 /* transmit errors */
  53. #define D64_XS1_XE_SHIFT 28
  54. #define D64_XS1_XE_NOERR 0x00000000 /* no error */
  55. #define D64_XS1_XE_DPE 0x10000000 /* descriptor protocol error */
  56. #define D64_XS1_XE_DFU 0x20000000 /* data fifo underrun */
  57. #define D64_XS1_XE_DTE 0x30000000 /* data transfer error */
  58. #define D64_XS1_XE_DESRE 0x40000000 /* descriptor read error */
  59. #define D64_XS1_XE_COREE 0x50000000 /* core error */
  60. /* receive channel control */
  61. /* receive enable */
  62. #define D64_RC_RE 0x00000001
  63. /* receive frame offset */
  64. #define D64_RC_RO_MASK 0x000000fe
  65. #define D64_RC_RO_SHIFT 1
  66. /* direct fifo receive (pio) mode */
  67. #define D64_RC_FM 0x00000100
  68. /* separate rx header descriptor enable */
  69. #define D64_RC_SH 0x00000200
  70. /* overflow continue */
  71. #define D64_RC_OC 0x00000400
  72. /* parity check disable */
  73. #define D64_RC_PD 0x00000800
  74. /* address extension bits */
  75. #define D64_RC_AE 0x00030000
  76. #define D64_RC_AE_SHIFT 16
  77. /* flags for dma controller */
  78. /* partity enable */
  79. #define DMA_CTRL_PEN (1 << 0)
  80. /* rx overflow continue */
  81. #define DMA_CTRL_ROC (1 << 1)
  82. /* allow rx scatter to multiple descriptors */
  83. #define DMA_CTRL_RXMULTI (1 << 2)
  84. /* Unframed Rx/Tx data */
  85. #define DMA_CTRL_UNFRAMED (1 << 3)
  86. /* receive descriptor table pointer */
  87. #define D64_RP_LD_MASK 0x00000fff /* last valid descriptor */
  88. /* receive channel status */
  89. #define D64_RS0_CD_MASK 0x00001fff /* current descriptor pointer */
  90. #define D64_RS0_RS_MASK 0xf0000000 /* receive state */
  91. #define D64_RS0_RS_SHIFT 28
  92. #define D64_RS0_RS_DISABLED 0x00000000 /* disabled */
  93. #define D64_RS0_RS_ACTIVE 0x10000000 /* active */
  94. #define D64_RS0_RS_IDLE 0x20000000 /* idle wait */
  95. #define D64_RS0_RS_STOPPED 0x30000000 /* stopped */
  96. #define D64_RS0_RS_SUSP 0x40000000 /* suspend pending */
  97. #define D64_RS1_AD_MASK 0x0001ffff /* active descriptor */
  98. #define D64_RS1_RE_MASK 0xf0000000 /* receive errors */
  99. #define D64_RS1_RE_SHIFT 28
  100. #define D64_RS1_RE_NOERR 0x00000000 /* no error */
  101. #define D64_RS1_RE_DPO 0x10000000 /* descriptor protocol error */
  102. #define D64_RS1_RE_DFU 0x20000000 /* data fifo overflow */
  103. #define D64_RS1_RE_DTE 0x30000000 /* data transfer error */
  104. #define D64_RS1_RE_DESRE 0x40000000 /* descriptor read error */
  105. #define D64_RS1_RE_COREE 0x50000000 /* core error */
  106. /* fifoaddr */
  107. #define D64_FA_OFF_MASK 0xffff /* offset */
  108. #define D64_FA_SEL_MASK 0xf0000 /* select */
  109. #define D64_FA_SEL_SHIFT 16
  110. #define D64_FA_SEL_XDD 0x00000 /* transmit dma data */
  111. #define D64_FA_SEL_XDP 0x10000 /* transmit dma pointers */
  112. #define D64_FA_SEL_RDD 0x40000 /* receive dma data */
  113. #define D64_FA_SEL_RDP 0x50000 /* receive dma pointers */
  114. #define D64_FA_SEL_XFD 0x80000 /* transmit fifo data */
  115. #define D64_FA_SEL_XFP 0x90000 /* transmit fifo pointers */
  116. #define D64_FA_SEL_RFD 0xc0000 /* receive fifo data */
  117. #define D64_FA_SEL_RFP 0xd0000 /* receive fifo pointers */
  118. #define D64_FA_SEL_RSD 0xe0000 /* receive frame status data */
  119. #define D64_FA_SEL_RSP 0xf0000 /* receive frame status pointers */
  120. /* descriptor control flags 1 */
  121. #define D64_CTRL_COREFLAGS 0x0ff00000 /* core specific flags */
  122. #define D64_CTRL1_EOT ((u32)1 << 28) /* end of descriptor table */
  123. #define D64_CTRL1_IOC ((u32)1 << 29) /* interrupt on completion */
  124. #define D64_CTRL1_EOF ((u32)1 << 30) /* end of frame */
  125. #define D64_CTRL1_SOF ((u32)1 << 31) /* start of frame */
  126. /* descriptor control flags 2 */
  127. /* buffer byte count. real data len must <= 16KB */
  128. #define D64_CTRL2_BC_MASK 0x00007fff
  129. /* address extension bits */
  130. #define D64_CTRL2_AE 0x00030000
  131. #define D64_CTRL2_AE_SHIFT 16
  132. /* parity bit */
  133. #define D64_CTRL2_PARITY 0x00040000
  134. /* control flags in the range [27:20] are core-specific and not defined here */
  135. #define D64_CTRL_CORE_MASK 0x0ff00000
  136. #define D64_RX_FRM_STS_LEN 0x0000ffff /* frame length mask */
  137. #define D64_RX_FRM_STS_OVFL 0x00800000 /* RxOverFlow */
  138. #define D64_RX_FRM_STS_DSCRCNT 0x0f000000 /* no. of descriptors used - 1 */
  139. #define D64_RX_FRM_STS_DATATYPE 0xf0000000 /* core-dependent data type */
  140. /*
  141. * packet headroom necessary to accommodate the largest header
  142. * in the system, (i.e TXOFF). By doing, we avoid the need to
  143. * allocate an extra buffer for the header when bridging to WL.
  144. * There is a compile time check in wlc.c which ensure that this
  145. * value is at least as big as TXOFF. This value is used in
  146. * dma_rxfill().
  147. */
  148. #define BCMEXTRAHDROOM 172
  149. /* debug/trace */
  150. #ifdef BCMDBG
  151. #define DMA_ERROR(args) \
  152. do { \
  153. if (!(*di->msg_level & 1)) \
  154. ; \
  155. else \
  156. printk args; \
  157. } while (0)
  158. #define DMA_TRACE(args) \
  159. do { \
  160. if (!(*di->msg_level & 2)) \
  161. ; \
  162. else \
  163. printk args; \
  164. } while (0)
  165. #else
  166. #define DMA_ERROR(args)
  167. #define DMA_TRACE(args)
  168. #endif /* BCMDBG */
  169. #define DMA_NONE(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 = di->dma.dmactrlflags;
  313. if (di == NULL) {
  314. DMA_ERROR(("%s: _dma_ctrlflags: NULL dma handle\n", di->name));
  315. return 0;
  316. }
  317. dmactrlflags &= ~mask;
  318. dmactrlflags |= flags;
  319. /* If trying to enable parity, check if parity is actually supported */
  320. if (dmactrlflags & DMA_CTRL_PEN) {
  321. u32 control;
  322. control = R_REG(&di->d64txregs->control);
  323. W_REG(&di->d64txregs->control,
  324. control | D64_XC_PD);
  325. if (R_REG(&di->d64txregs->control) & D64_XC_PD)
  326. /* We *can* disable it so it is supported,
  327. * restore control register
  328. */
  329. W_REG(&di->d64txregs->control,
  330. control);
  331. else
  332. /* Not supported, don't allow it to be enabled */
  333. dmactrlflags &= ~DMA_CTRL_PEN;
  334. }
  335. di->dma.dmactrlflags = dmactrlflags;
  336. return dmactrlflags;
  337. }
  338. static bool _dma64_addrext(struct dma64regs __iomem *dma64regs)
  339. {
  340. u32 w;
  341. OR_REG(&dma64regs->control, D64_XC_AE);
  342. w = R_REG(&dma64regs->control);
  343. AND_REG(&dma64regs->control, ~D64_XC_AE);
  344. return (w & D64_XC_AE) == D64_XC_AE;
  345. }
  346. /*
  347. * return true if this dma engine supports DmaExtendedAddrChanges,
  348. * otherwise false
  349. */
  350. static bool _dma_isaddrext(struct dma_info *di)
  351. {
  352. /* DMA64 supports full 32- or 64-bit operation. AE is always valid */
  353. /* not all tx or rx channel are available */
  354. if (di->d64txregs != NULL) {
  355. if (!_dma64_addrext(di->d64txregs))
  356. DMA_ERROR(("%s: _dma_isaddrext: DMA64 tx doesn't have "
  357. "AE set\n", di->name));
  358. return true;
  359. } else if (di->d64rxregs != NULL) {
  360. if (!_dma64_addrext(di->d64rxregs))
  361. DMA_ERROR(("%s: _dma_isaddrext: DMA64 rx doesn't have "
  362. "AE set\n", di->name));
  363. return true;
  364. }
  365. return false;
  366. }
  367. static bool _dma_descriptor_align(struct dma_info *di)
  368. {
  369. u32 addrl;
  370. /* Check to see if the descriptors need to be aligned on 4K/8K or not */
  371. if (di->d64txregs != NULL) {
  372. W_REG(&di->d64txregs->addrlow, 0xff0);
  373. addrl = R_REG(&di->d64txregs->addrlow);
  374. if (addrl != 0)
  375. return false;
  376. } else if (di->d64rxregs != NULL) {
  377. W_REG(&di->d64rxregs->addrlow, 0xff0);
  378. addrl = R_REG(&di->d64rxregs->addrlow);
  379. if (addrl != 0)
  380. return false;
  381. }
  382. return true;
  383. }
  384. /*
  385. * Descriptor table must start at the DMA hardware dictated alignment, so
  386. * allocated memory must be large enough to support this requirement.
  387. */
  388. static void *dma_alloc_consistent(struct pci_dev *pdev, uint size,
  389. u16 align_bits, uint *alloced,
  390. dma_addr_t *pap)
  391. {
  392. if (align_bits) {
  393. u16 align = (1 << align_bits);
  394. if (!IS_ALIGNED(PAGE_SIZE, align))
  395. size += align;
  396. *alloced = size;
  397. }
  398. return pci_alloc_consistent(pdev, size, pap);
  399. }
  400. static
  401. u8 dma_align_sizetobits(uint size)
  402. {
  403. u8 bitpos = 0;
  404. while (size >>= 1)
  405. bitpos++;
  406. return bitpos;
  407. }
  408. /* This function ensures that the DMA descriptor ring will not get allocated
  409. * across Page boundary. If the allocation is done across the page boundary
  410. * at the first time, then it is freed and the allocation is done at
  411. * descriptor ring size aligned location. This will ensure that the ring will
  412. * not cross page boundary
  413. */
  414. static void *dma_ringalloc(struct dma_info *di, u32 boundary, uint size,
  415. u16 *alignbits, uint *alloced,
  416. dma_addr_t *descpa)
  417. {
  418. void *va;
  419. u32 desc_strtaddr;
  420. u32 alignbytes = 1 << *alignbits;
  421. va = dma_alloc_consistent(di->pbus, size, *alignbits, alloced, descpa);
  422. if (NULL == va)
  423. return NULL;
  424. desc_strtaddr = (u32) roundup((unsigned long)va, alignbytes);
  425. if (((desc_strtaddr + size - 1) & boundary) != (desc_strtaddr
  426. & boundary)) {
  427. *alignbits = dma_align_sizetobits(size);
  428. pci_free_consistent(di->pbus, size, va, *descpa);
  429. va = dma_alloc_consistent(di->pbus, size, *alignbits,
  430. alloced, descpa);
  431. }
  432. return va;
  433. }
  434. static bool dma64_alloc(struct dma_info *di, uint direction)
  435. {
  436. u16 size;
  437. uint ddlen;
  438. void *va;
  439. uint alloced = 0;
  440. u16 align;
  441. u16 align_bits;
  442. ddlen = sizeof(struct dma64desc);
  443. size = (direction == DMA_TX) ? (di->ntxd * ddlen) : (di->nrxd * ddlen);
  444. align_bits = di->dmadesc_align;
  445. align = (1 << align_bits);
  446. if (direction == DMA_TX) {
  447. va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
  448. &alloced, &di->txdpaorig);
  449. if (va == NULL) {
  450. DMA_ERROR(("%s: dma64_alloc: DMA_ALLOC_CONSISTENT(ntxd)"
  451. " failed\n", di->name));
  452. return false;
  453. }
  454. align = (1 << align_bits);
  455. di->txd64 = (struct dma64desc *)
  456. roundup((unsigned long)va, align);
  457. di->txdalign = (uint) ((s8 *)di->txd64 - (s8 *) va);
  458. di->txdpa = di->txdpaorig + di->txdalign;
  459. di->txdalloc = alloced;
  460. } else {
  461. va = dma_ringalloc(di, D64RINGALIGN, size, &align_bits,
  462. &alloced, &di->rxdpaorig);
  463. if (va == NULL) {
  464. DMA_ERROR(("%s: dma64_alloc: DMA_ALLOC_CONSISTENT(nrxd)"
  465. " failed\n", di->name));
  466. return false;
  467. }
  468. align = (1 << align_bits);
  469. di->rxd64 = (struct dma64desc *)
  470. roundup((unsigned long)va, align);
  471. di->rxdalign = (uint) ((s8 *)di->rxd64 - (s8 *) va);
  472. di->rxdpa = di->rxdpaorig + di->rxdalign;
  473. di->rxdalloc = alloced;
  474. }
  475. return true;
  476. }
  477. static bool _dma_alloc(struct dma_info *di, uint direction)
  478. {
  479. return dma64_alloc(di, direction);
  480. }
  481. struct dma_pub *dma_attach(char *name, struct si_pub *sih,
  482. void __iomem *dmaregstx, void __iomem *dmaregsrx,
  483. uint ntxd, uint nrxd,
  484. uint rxbufsize, int rxextheadroom,
  485. uint nrxpost, uint rxoffset, uint *msg_level)
  486. {
  487. struct dma_info *di;
  488. uint size;
  489. /* allocate private info structure */
  490. di = kzalloc(sizeof(struct dma_info), GFP_ATOMIC);
  491. if (di == NULL)
  492. return NULL;
  493. di->msg_level = msg_level ? msg_level : &dma_msg_level;
  494. di->dma64 = ((ai_core_sflags(sih, 0, 0) & SISF_DMA64) == SISF_DMA64);
  495. /* init dma reg pointer */
  496. di->d64txregs = (struct dma64regs __iomem *) dmaregstx;
  497. di->d64rxregs = (struct dma64regs __iomem *) dmaregsrx;
  498. /*
  499. * Default flags (which can be changed by the driver calling
  500. * dma_ctrlflags before enable): For backwards compatibility
  501. * both Rx Overflow Continue and Parity are DISABLED.
  502. */
  503. _dma_ctrlflags(di, DMA_CTRL_ROC | DMA_CTRL_PEN, 0);
  504. DMA_TRACE(("%s: dma_attach: %s flags 0x%x ntxd %d nrxd %d "
  505. "rxbufsize %d rxextheadroom %d nrxpost %d rxoffset %d "
  506. "dmaregstx %p dmaregsrx %p\n", 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: dma_attach: txdpa 0x%x: addrext not "
  593. "supported\n", di->name, (u32)di->txdpa));
  594. goto fail;
  595. }
  596. if (di->rxdpa > SI_PCI_DMA_SZ) {
  597. DMA_ERROR(("%s: dma_attach: rxdpa 0x%x: addrext not "
  598. "supported\n", di->name, (u32)di->rxdpa));
  599. goto fail;
  600. }
  601. }
  602. DMA_TRACE(("ddoffsetlow 0x%x ddoffsethigh 0x%x dataoffsetlow 0x%x "
  603. "dataoffsethigh " "0x%x addrext %d\n", di->ddoffsetlow,
  604. di->ddoffsethigh, 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: dma_detach\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: dma_rxenable\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: dma_rxinit\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(("dma_rx, 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: dma_rx: 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: dma_rxidle\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: dma_rxfill: 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: dma_rxfill: out of rxbufs\n",
  875. di->name));
  876. if (i == 0 && dma64_rxidle(di)) {
  877. DMA_ERROR(("%s: rxfill64: ring is empty !\n",
  878. di->name));
  879. ring_empty = true;
  880. }
  881. di->dma.rxnobuf++;
  882. break;
  883. }
  884. /* reserve an extra headroom, if applicable */
  885. if (extra_offset)
  886. skb_pull(p, extra_offset);
  887. /* Do a cached write instead of uncached write since DMA_MAP
  888. * will flush the cache.
  889. */
  890. *(u32 *) (p->data) = 0;
  891. pa = pci_map_single(di->pbus, p->data,
  892. di->rxbufsize, PCI_DMA_FROMDEVICE);
  893. /* save the free packet pointer */
  894. di->rxp[rxout] = p;
  895. /* reset flags for each descriptor */
  896. flags = 0;
  897. if (rxout == (di->nrxd - 1))
  898. flags = D64_CTRL1_EOT;
  899. dma64_dd_upd(di, di->rxd64, pa, rxout, &flags,
  900. di->rxbufsize);
  901. rxout = nextrxd(di, rxout);
  902. }
  903. di->rxout = rxout;
  904. /* update the chip lastdscr pointer */
  905. W_REG(&di->d64rxregs->ptr,
  906. di->rcvptrbase + I2B(rxout, struct dma64desc));
  907. return ring_empty;
  908. }
  909. void dma_rxreclaim(struct dma_pub *pub)
  910. {
  911. struct dma_info *di = (struct dma_info *)pub;
  912. struct sk_buff *p;
  913. DMA_TRACE(("%s: dma_rxreclaim\n", di->name));
  914. while ((p = _dma_getnextrxp(di, true)))
  915. brcmu_pkt_buf_free_skb(p);
  916. }
  917. void dma_counterreset(struct dma_pub *pub)
  918. {
  919. /* reset all software counters */
  920. pub->rxgiants = 0;
  921. pub->rxnobuf = 0;
  922. pub->txnobuf = 0;
  923. }
  924. /* get the address of the var in order to change later */
  925. unsigned long dma_getvar(struct dma_pub *pub, const char *name)
  926. {
  927. struct dma_info *di = (struct dma_info *)pub;
  928. if (!strcmp(name, "&txavail"))
  929. return (unsigned long)&(di->dma.txavail);
  930. return 0;
  931. }
  932. /* 64-bit DMA functions */
  933. void dma_txinit(struct dma_pub *pub)
  934. {
  935. struct dma_info *di = (struct dma_info *)pub;
  936. u32 control = D64_XC_XE;
  937. DMA_TRACE(("%s: dma_txinit\n", di->name));
  938. if (di->ntxd == 0)
  939. return;
  940. di->txin = di->txout = 0;
  941. di->dma.txavail = di->ntxd - 1;
  942. /* clear tx descriptor ring */
  943. memset(di->txd64, '\0', (di->ntxd * sizeof(struct dma64desc)));
  944. /* DMA engine with out alignment requirement requires table to be inited
  945. * before enabling the engine
  946. */
  947. if (!di->aligndesc_4k)
  948. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  949. if ((di->dma.dmactrlflags & DMA_CTRL_PEN) == 0)
  950. control |= D64_XC_PD;
  951. OR_REG(&di->d64txregs->control, control);
  952. /* DMA engine with alignment requirement requires table to be inited
  953. * before enabling the engine
  954. */
  955. if (di->aligndesc_4k)
  956. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  957. }
  958. void dma_txsuspend(struct dma_pub *pub)
  959. {
  960. struct dma_info *di = (struct dma_info *)pub;
  961. DMA_TRACE(("%s: dma_txsuspend\n", di->name));
  962. if (di->ntxd == 0)
  963. return;
  964. OR_REG(&di->d64txregs->control, D64_XC_SE);
  965. }
  966. void dma_txresume(struct dma_pub *pub)
  967. {
  968. struct dma_info *di = (struct dma_info *)pub;
  969. DMA_TRACE(("%s: dma_txresume\n", di->name));
  970. if (di->ntxd == 0)
  971. return;
  972. AND_REG(&di->d64txregs->control, ~D64_XC_SE);
  973. }
  974. bool dma_txsuspended(struct dma_pub *pub)
  975. {
  976. struct dma_info *di = (struct dma_info *)pub;
  977. return (di->ntxd == 0) ||
  978. ((R_REG(&di->d64txregs->control) & D64_XC_SE) ==
  979. D64_XC_SE);
  980. }
  981. void dma_txreclaim(struct dma_pub *pub, enum txd_range range)
  982. {
  983. struct dma_info *di = (struct dma_info *)pub;
  984. struct sk_buff *p;
  985. DMA_TRACE(("%s: dma_txreclaim %s\n", di->name,
  986. (range == DMA_RANGE_ALL) ? "all" :
  987. ((range ==
  988. DMA_RANGE_TRANSMITTED) ? "transmitted" :
  989. "transferred")));
  990. if (di->txin == di->txout)
  991. return;
  992. while ((p = dma_getnexttxp(pub, range))) {
  993. /* For unframed data, we don't have any packets to free */
  994. if (!(di->dma.dmactrlflags & DMA_CTRL_UNFRAMED))
  995. brcmu_pkt_buf_free_skb(p);
  996. }
  997. }
  998. bool dma_txreset(struct dma_pub *pub)
  999. {
  1000. struct dma_info *di = (struct dma_info *)pub;
  1001. u32 status;
  1002. if (di->ntxd == 0)
  1003. return true;
  1004. /* suspend tx DMA first */
  1005. W_REG(&di->d64txregs->control, D64_XC_SE);
  1006. SPINWAIT(((status =
  1007. (R_REG(&di->d64txregs->status0) & D64_XS0_XS_MASK))
  1008. != D64_XS0_XS_DISABLED) && (status != D64_XS0_XS_IDLE)
  1009. && (status != D64_XS0_XS_STOPPED), 10000);
  1010. W_REG(&di->d64txregs->control, 0);
  1011. SPINWAIT(((status =
  1012. (R_REG(&di->d64txregs->status0) & D64_XS0_XS_MASK))
  1013. != D64_XS0_XS_DISABLED), 10000);
  1014. /* wait for the last transaction to complete */
  1015. udelay(300);
  1016. return status == D64_XS0_XS_DISABLED;
  1017. }
  1018. bool dma_rxreset(struct dma_pub *pub)
  1019. {
  1020. struct dma_info *di = (struct dma_info *)pub;
  1021. u32 status;
  1022. if (di->nrxd == 0)
  1023. return true;
  1024. W_REG(&di->d64rxregs->control, 0);
  1025. SPINWAIT(((status =
  1026. (R_REG(&di->d64rxregs->status0) & D64_RS0_RS_MASK))
  1027. != D64_RS0_RS_DISABLED), 10000);
  1028. return status == D64_RS0_RS_DISABLED;
  1029. }
  1030. /*
  1031. * !! tx entry routine
  1032. * WARNING: call must check the return value for error.
  1033. * the error(toss frames) could be fatal and cause many subsequent hard
  1034. * to debug problems
  1035. */
  1036. int dma_txfast(struct dma_pub *pub, struct sk_buff *p0, bool commit)
  1037. {
  1038. struct dma_info *di = (struct dma_info *)pub;
  1039. struct sk_buff *p, *next;
  1040. unsigned char *data;
  1041. uint len;
  1042. u16 txout;
  1043. u32 flags = 0;
  1044. dma_addr_t pa;
  1045. DMA_TRACE(("%s: dma_txfast\n", di->name));
  1046. txout = di->txout;
  1047. /*
  1048. * Walk the chain of packet buffers
  1049. * allocating and initializing transmit descriptor entries.
  1050. */
  1051. for (p = p0; p; p = next) {
  1052. data = p->data;
  1053. len = p->len;
  1054. next = p->next;
  1055. /* return nonzero if out of tx descriptors */
  1056. if (nexttxd(di, txout) == di->txin)
  1057. goto outoftxd;
  1058. if (len == 0)
  1059. continue;
  1060. /* get physical address of buffer start */
  1061. pa = pci_map_single(di->pbus, data, len, PCI_DMA_TODEVICE);
  1062. flags = 0;
  1063. if (p == p0)
  1064. flags |= D64_CTRL1_SOF;
  1065. /* With a DMA segment list, Descriptor table is filled
  1066. * using the segment list instead of looping over
  1067. * buffers in multi-chain DMA. Therefore, EOF for SGLIST
  1068. * is when end of segment list is reached.
  1069. */
  1070. if (next == NULL)
  1071. flags |= (D64_CTRL1_IOC | D64_CTRL1_EOF);
  1072. if (txout == (di->ntxd - 1))
  1073. flags |= D64_CTRL1_EOT;
  1074. dma64_dd_upd(di, di->txd64, pa, txout, &flags, len);
  1075. txout = nexttxd(di, txout);
  1076. }
  1077. /* if last txd eof not set, fix it */
  1078. if (!(flags & D64_CTRL1_EOF))
  1079. di->txd64[prevtxd(di, txout)].ctrl1 =
  1080. cpu_to_le32(flags | D64_CTRL1_IOC | D64_CTRL1_EOF);
  1081. /* save the packet */
  1082. di->txp[prevtxd(di, txout)] = p0;
  1083. /* bump the tx descriptor index */
  1084. di->txout = txout;
  1085. /* kick the chip */
  1086. if (commit)
  1087. W_REG(&di->d64txregs->ptr,
  1088. di->xmtptrbase + I2B(txout, struct dma64desc));
  1089. /* tx flow control */
  1090. di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) - 1;
  1091. return 0;
  1092. outoftxd:
  1093. DMA_ERROR(("%s: dma_txfast: out of txds !!!\n", di->name));
  1094. brcmu_pkt_buf_free_skb(p0);
  1095. di->dma.txavail = 0;
  1096. di->dma.txnobuf++;
  1097. return -1;
  1098. }
  1099. /*
  1100. * Reclaim next completed txd (txds if using chained buffers) in the range
  1101. * specified and return associated packet.
  1102. * If range is DMA_RANGE_TRANSMITTED, reclaim descriptors that have be
  1103. * transmitted as noted by the hardware "CurrDescr" pointer.
  1104. * If range is DMA_RANGE_TRANSFERED, reclaim descriptors that have be
  1105. * transferred by the DMA as noted by the hardware "ActiveDescr" pointer.
  1106. * If range is DMA_RANGE_ALL, reclaim all txd(s) posted to the ring and
  1107. * return associated packet regardless of the value of hardware pointers.
  1108. */
  1109. struct sk_buff *dma_getnexttxp(struct dma_pub *pub, enum txd_range range)
  1110. {
  1111. struct dma_info *di = (struct dma_info *)pub;
  1112. u16 start, end, i;
  1113. u16 active_desc;
  1114. struct sk_buff *txp;
  1115. DMA_TRACE(("%s: dma_getnexttxp %s\n", di->name,
  1116. (range == DMA_RANGE_ALL) ? "all" :
  1117. ((range ==
  1118. DMA_RANGE_TRANSMITTED) ? "transmitted" :
  1119. "transferred")));
  1120. if (di->ntxd == 0)
  1121. return NULL;
  1122. txp = NULL;
  1123. start = di->txin;
  1124. if (range == DMA_RANGE_ALL)
  1125. end = di->txout;
  1126. else {
  1127. struct dma64regs __iomem *dregs = di->d64txregs;
  1128. end = (u16) (B2I(((R_REG(&dregs->status0) &
  1129. D64_XS0_CD_MASK) -
  1130. di->xmtptrbase) & D64_XS0_CD_MASK,
  1131. struct dma64desc));
  1132. if (range == DMA_RANGE_TRANSFERED) {
  1133. active_desc =
  1134. (u16) (R_REG(&dregs->status1) &
  1135. D64_XS1_AD_MASK);
  1136. active_desc =
  1137. (active_desc - di->xmtptrbase) & D64_XS0_CD_MASK;
  1138. active_desc = B2I(active_desc, struct dma64desc);
  1139. if (end != active_desc)
  1140. end = prevtxd(di, active_desc);
  1141. }
  1142. }
  1143. if ((start == 0) && (end > di->txout))
  1144. goto bogus;
  1145. for (i = start; i != end && !txp; i = nexttxd(di, i)) {
  1146. dma_addr_t pa;
  1147. uint size;
  1148. pa = le32_to_cpu(di->txd64[i].addrlow) - di->dataoffsetlow;
  1149. size =
  1150. (le32_to_cpu(di->txd64[i].ctrl2) &
  1151. D64_CTRL2_BC_MASK);
  1152. di->txd64[i].addrlow = cpu_to_le32(0xdeadbeef);
  1153. di->txd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
  1154. txp = di->txp[i];
  1155. di->txp[i] = NULL;
  1156. pci_unmap_single(di->pbus, pa, size, PCI_DMA_TODEVICE);
  1157. }
  1158. di->txin = i;
  1159. /* tx flow control */
  1160. di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) - 1;
  1161. return txp;
  1162. bogus:
  1163. DMA_NONE(("dma_getnexttxp: bogus curr: start %d end %d txout %d "
  1164. "force %d\n", start, end, di->txout, forceall));
  1165. return NULL;
  1166. }
  1167. /*
  1168. * Mac80211 initiated actions sometimes require packets in the DMA queue to be
  1169. * modified. The modified portion of the packet is not under control of the DMA
  1170. * engine. This function calls a caller-supplied function for each packet in
  1171. * the caller specified dma chain.
  1172. */
  1173. void dma_walk_packets(struct dma_pub *dmah, void (*callback_fnc)
  1174. (void *pkt, void *arg_a), void *arg_a)
  1175. {
  1176. struct dma_info *di = (struct dma_info *) dmah;
  1177. uint i = di->txin;
  1178. uint end = di->txout;
  1179. struct sk_buff *skb;
  1180. struct ieee80211_tx_info *tx_info;
  1181. while (i != end) {
  1182. skb = (struct sk_buff *)di->txp[i];
  1183. if (skb != NULL) {
  1184. tx_info = (struct ieee80211_tx_info *)skb->cb;
  1185. (callback_fnc)(tx_info, arg_a);
  1186. }
  1187. i = nexttxd(di, i);
  1188. }
  1189. }