dma.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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/skbuff.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. /*
  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;
  313. if (di == NULL) {
  314. DMA_ERROR(("_dma_ctrlflags: 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: _dma_isaddrext: DMA64 tx doesn't have "
  358. "AE set\n", di->name));
  359. return true;
  360. } else if (di->d64rxregs != NULL) {
  361. if (!_dma64_addrext(di->d64rxregs))
  362. DMA_ERROR(("%s: _dma_isaddrext: DMA64 rx doesn't have "
  363. "AE set\n", 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: dma64_alloc: DMA_ALLOC_CONSISTENT(ntxd)"
  452. " failed\n", 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: dma64_alloc: DMA_ALLOC_CONSISTENT(nrxd)"
  466. " failed\n", 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: dma_attach: %s flags 0x%x ntxd %d nrxd %d "
  506. "rxbufsize %d rxextheadroom %d nrxpost %d rxoffset %d "
  507. "dmaregstx %p dmaregsrx %p\n", name, "DMA64",
  508. di->dma.dmactrlflags, ntxd, nrxd, rxbufsize,
  509. rxextheadroom, nrxpost, rxoffset, dmaregstx, dmaregsrx));
  510. /* make a private copy of our callers name */
  511. strncpy(di->name, name, MAXNAMEL);
  512. di->name[MAXNAMEL - 1] = '\0';
  513. di->pbus = ((struct si_info *)sih)->pbus;
  514. /* save tunables */
  515. di->ntxd = (u16) ntxd;
  516. di->nrxd = (u16) nrxd;
  517. /* the actual dma size doesn't include the extra headroom */
  518. di->rxextrahdrroom =
  519. (rxextheadroom == -1) ? BCMEXTRAHDROOM : rxextheadroom;
  520. if (rxbufsize > BCMEXTRAHDROOM)
  521. di->rxbufsize = (u16) (rxbufsize - di->rxextrahdrroom);
  522. else
  523. di->rxbufsize = (u16) rxbufsize;
  524. di->nrxpost = (u16) nrxpost;
  525. di->rxoffset = (u8) rxoffset;
  526. /*
  527. * figure out the DMA physical address offset for dd and data
  528. * PCI/PCIE: they map silicon backplace address to zero
  529. * based memory, need offset
  530. * Other bus: use zero SI_BUS BIGENDIAN kludge: use sdram
  531. * swapped region for data buffer, not descriptor
  532. */
  533. di->ddoffsetlow = 0;
  534. di->dataoffsetlow = 0;
  535. /* add offset for pcie with DMA64 bus */
  536. di->ddoffsetlow = 0;
  537. di->ddoffsethigh = SI_PCIE_DMA_H32;
  538. di->dataoffsetlow = di->ddoffsetlow;
  539. di->dataoffsethigh = di->ddoffsethigh;
  540. /* WAR64450 : DMACtl.Addr ext fields are not supported in SDIOD core. */
  541. if ((ai_coreid(sih) == SDIOD_CORE_ID)
  542. && ((ai_corerev(sih) > 0) && (ai_corerev(sih) <= 2)))
  543. di->addrext = 0;
  544. else if ((ai_coreid(sih) == I2S_CORE_ID) &&
  545. ((ai_corerev(sih) == 0) || (ai_corerev(sih) == 1)))
  546. di->addrext = 0;
  547. else
  548. di->addrext = _dma_isaddrext(di);
  549. /* does the descriptor need to be aligned and if yes, on 4K/8K or not */
  550. di->aligndesc_4k = _dma_descriptor_align(di);
  551. if (di->aligndesc_4k) {
  552. di->dmadesc_align = D64RINGALIGN_BITS;
  553. if ((ntxd < D64MAXDD / 2) && (nrxd < D64MAXDD / 2))
  554. /* for smaller dd table, HW relax alignment reqmnt */
  555. di->dmadesc_align = D64RINGALIGN_BITS - 1;
  556. } else {
  557. di->dmadesc_align = 4; /* 16 byte alignment */
  558. }
  559. DMA_NONE(("DMA descriptor align_needed %d, align %d\n",
  560. di->aligndesc_4k, di->dmadesc_align));
  561. /* allocate tx packet pointer vector */
  562. if (ntxd) {
  563. size = ntxd * sizeof(void *);
  564. di->txp = kzalloc(size, GFP_ATOMIC);
  565. if (di->txp == NULL)
  566. goto fail;
  567. }
  568. /* allocate rx packet pointer vector */
  569. if (nrxd) {
  570. size = nrxd * sizeof(void *);
  571. di->rxp = kzalloc(size, GFP_ATOMIC);
  572. if (di->rxp == NULL)
  573. goto fail;
  574. }
  575. /*
  576. * allocate transmit descriptor ring, only need ntxd descriptors
  577. * but it must be aligned
  578. */
  579. if (ntxd) {
  580. if (!_dma_alloc(di, DMA_TX))
  581. goto fail;
  582. }
  583. /*
  584. * allocate receive descriptor ring, only need nrxd descriptors
  585. * but it must be aligned
  586. */
  587. if (nrxd) {
  588. if (!_dma_alloc(di, DMA_RX))
  589. goto fail;
  590. }
  591. if ((di->ddoffsetlow != 0) && !di->addrext) {
  592. if (di->txdpa > SI_PCI_DMA_SZ) {
  593. DMA_ERROR(("%s: dma_attach: txdpa 0x%x: addrext not "
  594. "supported\n", di->name, (u32)di->txdpa));
  595. goto fail;
  596. }
  597. if (di->rxdpa > SI_PCI_DMA_SZ) {
  598. DMA_ERROR(("%s: dma_attach: rxdpa 0x%x: addrext not "
  599. "supported\n", di->name, (u32)di->rxdpa));
  600. goto fail;
  601. }
  602. }
  603. DMA_TRACE(("ddoffsetlow 0x%x ddoffsethigh 0x%x dataoffsetlow 0x%x "
  604. "dataoffsethigh " "0x%x addrext %d\n", di->ddoffsetlow,
  605. di->ddoffsethigh, di->dataoffsetlow, di->dataoffsethigh,
  606. di->addrext));
  607. return (struct dma_pub *) di;
  608. fail:
  609. dma_detach((struct dma_pub *)di);
  610. return NULL;
  611. }
  612. static inline void
  613. dma64_dd_upd(struct dma_info *di, struct dma64desc *ddring,
  614. dma_addr_t pa, uint outidx, u32 *flags, u32 bufcount)
  615. {
  616. u32 ctrl2 = bufcount & D64_CTRL2_BC_MASK;
  617. /* PCI bus with big(>1G) physical address, use address extension */
  618. if ((di->dataoffsetlow == 0) || !(pa & PCI32ADDR_HIGH)) {
  619. ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
  620. ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
  621. ddring[outidx].ctrl1 = cpu_to_le32(*flags);
  622. ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
  623. } else {
  624. /* address extension for 32-bit PCI */
  625. u32 ae;
  626. ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
  627. pa &= ~PCI32ADDR_HIGH;
  628. ctrl2 |= (ae << D64_CTRL2_AE_SHIFT) & D64_CTRL2_AE;
  629. ddring[outidx].addrlow = cpu_to_le32(pa + di->dataoffsetlow);
  630. ddring[outidx].addrhigh = cpu_to_le32(di->dataoffsethigh);
  631. ddring[outidx].ctrl1 = cpu_to_le32(*flags);
  632. ddring[outidx].ctrl2 = cpu_to_le32(ctrl2);
  633. }
  634. if (di->dma.dmactrlflags & DMA_CTRL_PEN) {
  635. if (dma64_dd_parity(&ddring[outidx]))
  636. ddring[outidx].ctrl2 =
  637. cpu_to_le32(ctrl2 | D64_CTRL2_PARITY);
  638. }
  639. }
  640. /* !! may be called with core in reset */
  641. void dma_detach(struct dma_pub *pub)
  642. {
  643. struct dma_info *di = (struct dma_info *)pub;
  644. DMA_TRACE(("%s: dma_detach\n", di->name));
  645. /* free dma descriptor rings */
  646. if (di->txd64)
  647. pci_free_consistent(di->pbus, di->txdalloc,
  648. ((s8 *)di->txd64 - di->txdalign),
  649. (di->txdpaorig));
  650. if (di->rxd64)
  651. pci_free_consistent(di->pbus, di->rxdalloc,
  652. ((s8 *)di->rxd64 - di->rxdalign),
  653. (di->rxdpaorig));
  654. /* free packet pointer vectors */
  655. kfree(di->txp);
  656. kfree(di->rxp);
  657. /* free our private info structure */
  658. kfree(di);
  659. }
  660. /* initialize descriptor table base address */
  661. static void
  662. _dma_ddtable_init(struct dma_info *di, uint direction, dma_addr_t pa)
  663. {
  664. if (!di->aligndesc_4k) {
  665. if (direction == DMA_TX)
  666. di->xmtptrbase = pa;
  667. else
  668. di->rcvptrbase = pa;
  669. }
  670. if ((di->ddoffsetlow == 0)
  671. || !(pa & PCI32ADDR_HIGH)) {
  672. if (direction == DMA_TX) {
  673. W_REG(&di->d64txregs->addrlow, pa + di->ddoffsetlow);
  674. W_REG(&di->d64txregs->addrhigh, di->ddoffsethigh);
  675. } else {
  676. W_REG(&di->d64rxregs->addrlow, pa + di->ddoffsetlow);
  677. W_REG(&di->d64rxregs->addrhigh, di->ddoffsethigh);
  678. }
  679. } else {
  680. /* DMA64 32bits address extension */
  681. u32 ae;
  682. /* shift the high bit(s) from pa to ae */
  683. ae = (pa & PCI32ADDR_HIGH) >> PCI32ADDR_HIGH_SHIFT;
  684. pa &= ~PCI32ADDR_HIGH;
  685. if (direction == DMA_TX) {
  686. W_REG(&di->d64txregs->addrlow, pa + di->ddoffsetlow);
  687. W_REG(&di->d64txregs->addrhigh, di->ddoffsethigh);
  688. SET_REG(&di->d64txregs->control,
  689. D64_XC_AE, (ae << D64_XC_AE_SHIFT));
  690. } else {
  691. W_REG(&di->d64rxregs->addrlow, pa + di->ddoffsetlow);
  692. W_REG(&di->d64rxregs->addrhigh, di->ddoffsethigh);
  693. SET_REG(&di->d64rxregs->control,
  694. D64_RC_AE, (ae << D64_RC_AE_SHIFT));
  695. }
  696. }
  697. }
  698. static void _dma_rxenable(struct dma_info *di)
  699. {
  700. uint dmactrlflags = di->dma.dmactrlflags;
  701. u32 control;
  702. DMA_TRACE(("%s: dma_rxenable\n", di->name));
  703. control =
  704. (R_REG(&di->d64rxregs->control) & D64_RC_AE) |
  705. D64_RC_RE;
  706. if ((dmactrlflags & DMA_CTRL_PEN) == 0)
  707. control |= D64_RC_PD;
  708. if (dmactrlflags & DMA_CTRL_ROC)
  709. control |= D64_RC_OC;
  710. W_REG(&di->d64rxregs->control,
  711. ((di->rxoffset << D64_RC_RO_SHIFT) | control));
  712. }
  713. void dma_rxinit(struct dma_pub *pub)
  714. {
  715. struct dma_info *di = (struct dma_info *)pub;
  716. DMA_TRACE(("%s: dma_rxinit\n", di->name));
  717. if (di->nrxd == 0)
  718. return;
  719. di->rxin = di->rxout = 0;
  720. /* clear rx descriptor ring */
  721. memset(di->rxd64, '\0', di->nrxd * sizeof(struct dma64desc));
  722. /* DMA engine with out alignment requirement requires table to be inited
  723. * before enabling the engine
  724. */
  725. if (!di->aligndesc_4k)
  726. _dma_ddtable_init(di, DMA_RX, di->rxdpa);
  727. _dma_rxenable(di);
  728. if (di->aligndesc_4k)
  729. _dma_ddtable_init(di, DMA_RX, di->rxdpa);
  730. }
  731. static struct sk_buff *dma64_getnextrxp(struct dma_info *di, bool forceall)
  732. {
  733. uint i, curr;
  734. struct sk_buff *rxp;
  735. dma_addr_t pa;
  736. i = di->rxin;
  737. /* return if no packets posted */
  738. if (i == di->rxout)
  739. return NULL;
  740. curr =
  741. B2I(((R_REG(&di->d64rxregs->status0) & D64_RS0_CD_MASK) -
  742. di->rcvptrbase) & D64_RS0_CD_MASK, struct dma64desc);
  743. /* ignore curr if forceall */
  744. if (!forceall && (i == curr))
  745. return NULL;
  746. /* get the packet pointer that corresponds to the rx descriptor */
  747. rxp = di->rxp[i];
  748. di->rxp[i] = NULL;
  749. pa = le32_to_cpu(di->rxd64[i].addrlow) - di->dataoffsetlow;
  750. /* clear this packet from the descriptor ring */
  751. pci_unmap_single(di->pbus, pa, di->rxbufsize, PCI_DMA_FROMDEVICE);
  752. di->rxd64[i].addrlow = cpu_to_le32(0xdeadbeef);
  753. di->rxd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
  754. di->rxin = nextrxd(di, i);
  755. return rxp;
  756. }
  757. static struct sk_buff *_dma_getnextrxp(struct dma_info *di, bool forceall)
  758. {
  759. if (di->nrxd == 0)
  760. return NULL;
  761. return dma64_getnextrxp(di, forceall);
  762. }
  763. /*
  764. * !! rx entry routine
  765. * returns a pointer to the next frame received, or NULL if there are no more
  766. * if DMA_CTRL_RXMULTI is defined, DMA scattering(multiple buffers) is
  767. * supported with pkts chain
  768. * otherwise, it's treated as giant pkt and will be tossed.
  769. * The DMA scattering starts with normal DMA header, followed by first
  770. * buffer data. After it reaches the max size of buffer, the data continues
  771. * in next DMA descriptor buffer WITHOUT DMA header
  772. */
  773. struct sk_buff *dma_rx(struct dma_pub *pub)
  774. {
  775. struct dma_info *di = (struct dma_info *)pub;
  776. struct sk_buff *p, *head, *tail;
  777. uint len;
  778. uint pkt_len;
  779. int resid = 0;
  780. next_frame:
  781. head = _dma_getnextrxp(di, false);
  782. if (head == NULL)
  783. return NULL;
  784. len = le16_to_cpu(*(__le16 *) (head->data));
  785. DMA_TRACE(("%s: dma_rx len %d\n", di->name, len));
  786. dma_spin_for_len(len, head);
  787. /* set actual length */
  788. pkt_len = min((di->rxoffset + len), di->rxbufsize);
  789. __skb_trim(head, pkt_len);
  790. resid = len - (di->rxbufsize - di->rxoffset);
  791. /* check for single or multi-buffer rx */
  792. if (resid > 0) {
  793. tail = head;
  794. while ((resid > 0) && (p = _dma_getnextrxp(di, false))) {
  795. tail->next = p;
  796. pkt_len = min_t(uint, resid, di->rxbufsize);
  797. __skb_trim(p, pkt_len);
  798. tail = p;
  799. resid -= di->rxbufsize;
  800. }
  801. #ifdef BCMDBG
  802. if (resid > 0) {
  803. uint cur;
  804. cur =
  805. B2I(((R_REG(&di->d64rxregs->status0) &
  806. D64_RS0_CD_MASK) -
  807. di->rcvptrbase) & D64_RS0_CD_MASK,
  808. struct dma64desc);
  809. DMA_ERROR(("dma_rx, rxin %d rxout %d, hw_curr %d\n",
  810. di->rxin, di->rxout, cur));
  811. }
  812. #endif /* BCMDBG */
  813. if ((di->dma.dmactrlflags & DMA_CTRL_RXMULTI) == 0) {
  814. DMA_ERROR(("%s: dma_rx: bad frame length (%d)\n",
  815. di->name, len));
  816. brcmu_pkt_buf_free_skb(head);
  817. di->dma.rxgiants++;
  818. goto next_frame;
  819. }
  820. }
  821. return head;
  822. }
  823. static bool dma64_rxidle(struct dma_info *di)
  824. {
  825. DMA_TRACE(("%s: dma_rxidle\n", di->name));
  826. if (di->nrxd == 0)
  827. return true;
  828. return ((R_REG(&di->d64rxregs->status0) & D64_RS0_CD_MASK) ==
  829. (R_REG(&di->d64rxregs->ptr) & D64_RS0_CD_MASK));
  830. }
  831. /*
  832. * post receive buffers
  833. * return false is refill failed completely and ring is empty this will stall
  834. * the rx dma and user might want to call rxfill again asap. This unlikely
  835. * happens on memory-rich NIC, but often on memory-constrained dongle
  836. */
  837. bool dma_rxfill(struct dma_pub *pub)
  838. {
  839. struct dma_info *di = (struct dma_info *)pub;
  840. struct sk_buff *p;
  841. u16 rxin, rxout;
  842. u32 flags = 0;
  843. uint n;
  844. uint i;
  845. dma_addr_t pa;
  846. uint extra_offset = 0;
  847. bool ring_empty;
  848. ring_empty = false;
  849. /*
  850. * Determine how many receive buffers we're lacking
  851. * from the full complement, allocate, initialize,
  852. * and post them, then update the chip rx lastdscr.
  853. */
  854. rxin = di->rxin;
  855. rxout = di->rxout;
  856. n = di->nrxpost - nrxdactive(di, rxin, rxout);
  857. DMA_TRACE(("%s: dma_rxfill: post %d\n", di->name, n));
  858. if (di->rxbufsize > BCMEXTRAHDROOM)
  859. extra_offset = di->rxextrahdrroom;
  860. for (i = 0; i < n; i++) {
  861. /*
  862. * the di->rxbufsize doesn't include the extra headroom,
  863. * we need to add it to the size to be allocated
  864. */
  865. p = brcmu_pkt_buf_get_skb(di->rxbufsize + extra_offset);
  866. if (p == NULL) {
  867. DMA_ERROR(("%s: dma_rxfill: out of rxbufs\n",
  868. di->name));
  869. if (i == 0 && dma64_rxidle(di)) {
  870. DMA_ERROR(("%s: rxfill64: ring is empty !\n",
  871. di->name));
  872. ring_empty = true;
  873. }
  874. di->dma.rxnobuf++;
  875. break;
  876. }
  877. /* reserve an extra headroom, if applicable */
  878. if (extra_offset)
  879. skb_pull(p, extra_offset);
  880. /* Do a cached write instead of uncached write since DMA_MAP
  881. * will flush the cache.
  882. */
  883. *(u32 *) (p->data) = 0;
  884. pa = pci_map_single(di->pbus, p->data,
  885. di->rxbufsize, PCI_DMA_FROMDEVICE);
  886. /* save the free packet pointer */
  887. di->rxp[rxout] = p;
  888. /* reset flags for each descriptor */
  889. flags = 0;
  890. if (rxout == (di->nrxd - 1))
  891. flags = D64_CTRL1_EOT;
  892. dma64_dd_upd(di, di->rxd64, pa, rxout, &flags,
  893. di->rxbufsize);
  894. rxout = nextrxd(di, rxout);
  895. }
  896. di->rxout = rxout;
  897. /* update the chip lastdscr pointer */
  898. W_REG(&di->d64rxregs->ptr,
  899. di->rcvptrbase + I2B(rxout, struct dma64desc));
  900. return ring_empty;
  901. }
  902. void dma_rxreclaim(struct dma_pub *pub)
  903. {
  904. struct dma_info *di = (struct dma_info *)pub;
  905. struct sk_buff *p;
  906. DMA_TRACE(("%s: dma_rxreclaim\n", di->name));
  907. while ((p = _dma_getnextrxp(di, true)))
  908. brcmu_pkt_buf_free_skb(p);
  909. }
  910. void dma_counterreset(struct dma_pub *pub)
  911. {
  912. /* reset all software counters */
  913. pub->rxgiants = 0;
  914. pub->rxnobuf = 0;
  915. pub->txnobuf = 0;
  916. }
  917. /* get the address of the var in order to change later */
  918. unsigned long dma_getvar(struct dma_pub *pub, const char *name)
  919. {
  920. struct dma_info *di = (struct dma_info *)pub;
  921. if (!strcmp(name, "&txavail"))
  922. return (unsigned long)&(di->dma.txavail);
  923. return 0;
  924. }
  925. /* 64-bit DMA functions */
  926. void dma_txinit(struct dma_pub *pub)
  927. {
  928. struct dma_info *di = (struct dma_info *)pub;
  929. u32 control = D64_XC_XE;
  930. DMA_TRACE(("%s: dma_txinit\n", di->name));
  931. if (di->ntxd == 0)
  932. return;
  933. di->txin = di->txout = 0;
  934. di->dma.txavail = di->ntxd - 1;
  935. /* clear tx descriptor ring */
  936. memset(di->txd64, '\0', (di->ntxd * sizeof(struct dma64desc)));
  937. /* DMA engine with out alignment requirement requires table to be inited
  938. * before enabling the engine
  939. */
  940. if (!di->aligndesc_4k)
  941. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  942. if ((di->dma.dmactrlflags & DMA_CTRL_PEN) == 0)
  943. control |= D64_XC_PD;
  944. OR_REG(&di->d64txregs->control, control);
  945. /* DMA engine with alignment requirement requires table to be inited
  946. * before enabling the engine
  947. */
  948. if (di->aligndesc_4k)
  949. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  950. }
  951. void dma_txsuspend(struct dma_pub *pub)
  952. {
  953. struct dma_info *di = (struct dma_info *)pub;
  954. DMA_TRACE(("%s: dma_txsuspend\n", di->name));
  955. if (di->ntxd == 0)
  956. return;
  957. OR_REG(&di->d64txregs->control, D64_XC_SE);
  958. }
  959. void dma_txresume(struct dma_pub *pub)
  960. {
  961. struct dma_info *di = (struct dma_info *)pub;
  962. DMA_TRACE(("%s: dma_txresume\n", di->name));
  963. if (di->ntxd == 0)
  964. return;
  965. AND_REG(&di->d64txregs->control, ~D64_XC_SE);
  966. }
  967. bool dma_txsuspended(struct dma_pub *pub)
  968. {
  969. struct dma_info *di = (struct dma_info *)pub;
  970. return (di->ntxd == 0) ||
  971. ((R_REG(&di->d64txregs->control) & D64_XC_SE) ==
  972. D64_XC_SE);
  973. }
  974. void dma_txreclaim(struct dma_pub *pub, enum txd_range range)
  975. {
  976. struct dma_info *di = (struct dma_info *)pub;
  977. struct sk_buff *p;
  978. DMA_TRACE(("%s: dma_txreclaim %s\n", di->name,
  979. (range == DMA_RANGE_ALL) ? "all" :
  980. ((range ==
  981. DMA_RANGE_TRANSMITTED) ? "transmitted" :
  982. "transferred")));
  983. if (di->txin == di->txout)
  984. return;
  985. while ((p = dma_getnexttxp(pub, range))) {
  986. /* For unframed data, we don't have any packets to free */
  987. if (!(di->dma.dmactrlflags & DMA_CTRL_UNFRAMED))
  988. brcmu_pkt_buf_free_skb(p);
  989. }
  990. }
  991. bool dma_txreset(struct dma_pub *pub)
  992. {
  993. struct dma_info *di = (struct dma_info *)pub;
  994. u32 status;
  995. if (di->ntxd == 0)
  996. return true;
  997. /* suspend tx DMA first */
  998. W_REG(&di->d64txregs->control, D64_XC_SE);
  999. SPINWAIT(((status =
  1000. (R_REG(&di->d64txregs->status0) & D64_XS0_XS_MASK))
  1001. != D64_XS0_XS_DISABLED) && (status != D64_XS0_XS_IDLE)
  1002. && (status != D64_XS0_XS_STOPPED), 10000);
  1003. W_REG(&di->d64txregs->control, 0);
  1004. SPINWAIT(((status =
  1005. (R_REG(&di->d64txregs->status0) & D64_XS0_XS_MASK))
  1006. != D64_XS0_XS_DISABLED), 10000);
  1007. /* wait for the last transaction to complete */
  1008. udelay(300);
  1009. return status == D64_XS0_XS_DISABLED;
  1010. }
  1011. bool dma_rxreset(struct dma_pub *pub)
  1012. {
  1013. struct dma_info *di = (struct dma_info *)pub;
  1014. u32 status;
  1015. if (di->nrxd == 0)
  1016. return true;
  1017. W_REG(&di->d64rxregs->control, 0);
  1018. SPINWAIT(((status =
  1019. (R_REG(&di->d64rxregs->status0) & D64_RS0_RS_MASK))
  1020. != D64_RS0_RS_DISABLED), 10000);
  1021. return status == D64_RS0_RS_DISABLED;
  1022. }
  1023. /*
  1024. * !! tx entry routine
  1025. * WARNING: call must check the return value for error.
  1026. * the error(toss frames) could be fatal and cause many subsequent hard
  1027. * to debug problems
  1028. */
  1029. int dma_txfast(struct dma_pub *pub, struct sk_buff *p0, bool commit)
  1030. {
  1031. struct dma_info *di = (struct dma_info *)pub;
  1032. struct sk_buff *p, *next;
  1033. unsigned char *data;
  1034. uint len;
  1035. u16 txout;
  1036. u32 flags = 0;
  1037. dma_addr_t pa;
  1038. DMA_TRACE(("%s: dma_txfast\n", di->name));
  1039. txout = di->txout;
  1040. /*
  1041. * Walk the chain of packet buffers
  1042. * allocating and initializing transmit descriptor entries.
  1043. */
  1044. for (p = p0; p; p = next) {
  1045. data = p->data;
  1046. len = p->len;
  1047. next = p->next;
  1048. /* return nonzero if out of tx descriptors */
  1049. if (nexttxd(di, txout) == di->txin)
  1050. goto outoftxd;
  1051. if (len == 0)
  1052. continue;
  1053. /* get physical address of buffer start */
  1054. pa = pci_map_single(di->pbus, data, len, PCI_DMA_TODEVICE);
  1055. flags = 0;
  1056. if (p == p0)
  1057. flags |= D64_CTRL1_SOF;
  1058. /* With a DMA segment list, Descriptor table is filled
  1059. * using the segment list instead of looping over
  1060. * buffers in multi-chain DMA. Therefore, EOF for SGLIST
  1061. * is when end of segment list is reached.
  1062. */
  1063. if (next == NULL)
  1064. flags |= (D64_CTRL1_IOC | D64_CTRL1_EOF);
  1065. if (txout == (di->ntxd - 1))
  1066. flags |= D64_CTRL1_EOT;
  1067. dma64_dd_upd(di, di->txd64, pa, txout, &flags, len);
  1068. txout = nexttxd(di, txout);
  1069. }
  1070. /* if last txd eof not set, fix it */
  1071. if (!(flags & D64_CTRL1_EOF))
  1072. di->txd64[prevtxd(di, txout)].ctrl1 =
  1073. cpu_to_le32(flags | D64_CTRL1_IOC | D64_CTRL1_EOF);
  1074. /* save the packet */
  1075. di->txp[prevtxd(di, txout)] = p0;
  1076. /* bump the tx descriptor index */
  1077. di->txout = txout;
  1078. /* kick the chip */
  1079. if (commit)
  1080. W_REG(&di->d64txregs->ptr,
  1081. di->xmtptrbase + I2B(txout, struct dma64desc));
  1082. /* tx flow control */
  1083. di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) - 1;
  1084. return 0;
  1085. outoftxd:
  1086. DMA_ERROR(("%s: dma_txfast: out of txds !!!\n", di->name));
  1087. brcmu_pkt_buf_free_skb(p0);
  1088. di->dma.txavail = 0;
  1089. di->dma.txnobuf++;
  1090. return -1;
  1091. }
  1092. /*
  1093. * Reclaim next completed txd (txds if using chained buffers) in the range
  1094. * specified and return associated packet.
  1095. * If range is DMA_RANGE_TRANSMITTED, reclaim descriptors that have be
  1096. * transmitted as noted by the hardware "CurrDescr" pointer.
  1097. * If range is DMA_RANGE_TRANSFERED, reclaim descriptors that have be
  1098. * transferred by the DMA as noted by the hardware "ActiveDescr" pointer.
  1099. * If range is DMA_RANGE_ALL, reclaim all txd(s) posted to the ring and
  1100. * return associated packet regardless of the value of hardware pointers.
  1101. */
  1102. struct sk_buff *dma_getnexttxp(struct dma_pub *pub, enum txd_range range)
  1103. {
  1104. struct dma_info *di = (struct dma_info *)pub;
  1105. u16 start, end, i;
  1106. u16 active_desc;
  1107. struct sk_buff *txp;
  1108. DMA_TRACE(("%s: dma_getnexttxp %s\n", di->name,
  1109. (range == DMA_RANGE_ALL) ? "all" :
  1110. ((range ==
  1111. DMA_RANGE_TRANSMITTED) ? "transmitted" :
  1112. "transferred")));
  1113. if (di->ntxd == 0)
  1114. return NULL;
  1115. txp = NULL;
  1116. start = di->txin;
  1117. if (range == DMA_RANGE_ALL)
  1118. end = di->txout;
  1119. else {
  1120. struct dma64regs __iomem *dregs = di->d64txregs;
  1121. end = (u16) (B2I(((R_REG(&dregs->status0) &
  1122. D64_XS0_CD_MASK) -
  1123. di->xmtptrbase) & D64_XS0_CD_MASK,
  1124. struct dma64desc));
  1125. if (range == DMA_RANGE_TRANSFERED) {
  1126. active_desc =
  1127. (u16) (R_REG(&dregs->status1) &
  1128. D64_XS1_AD_MASK);
  1129. active_desc =
  1130. (active_desc - di->xmtptrbase) & D64_XS0_CD_MASK;
  1131. active_desc = B2I(active_desc, struct dma64desc);
  1132. if (end != active_desc)
  1133. end = prevtxd(di, active_desc);
  1134. }
  1135. }
  1136. if ((start == 0) && (end > di->txout))
  1137. goto bogus;
  1138. for (i = start; i != end && !txp; i = nexttxd(di, i)) {
  1139. dma_addr_t pa;
  1140. uint size;
  1141. pa = le32_to_cpu(di->txd64[i].addrlow) - di->dataoffsetlow;
  1142. size =
  1143. (le32_to_cpu(di->txd64[i].ctrl2) &
  1144. D64_CTRL2_BC_MASK);
  1145. di->txd64[i].addrlow = cpu_to_le32(0xdeadbeef);
  1146. di->txd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
  1147. txp = di->txp[i];
  1148. di->txp[i] = NULL;
  1149. pci_unmap_single(di->pbus, pa, size, PCI_DMA_TODEVICE);
  1150. }
  1151. di->txin = i;
  1152. /* tx flow control */
  1153. di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) - 1;
  1154. return txp;
  1155. bogus:
  1156. DMA_NONE(("dma_getnexttxp: bogus curr: start %d end %d txout %d "
  1157. "force %d\n", start, end, di->txout, forceall));
  1158. return NULL;
  1159. }
  1160. /*
  1161. * Mac80211 initiated actions sometimes require packets in the DMA queue to be
  1162. * modified. The modified portion of the packet is not under control of the DMA
  1163. * engine. This function calls a caller-supplied function for each packet in
  1164. * the caller specified dma chain.
  1165. */
  1166. void dma_walk_packets(struct dma_pub *dmah, void (*callback_fnc)
  1167. (void *pkt, void *arg_a), void *arg_a)
  1168. {
  1169. struct dma_info *di = (struct dma_info *) dmah;
  1170. uint i = di->txin;
  1171. uint end = di->txout;
  1172. struct sk_buff *skb;
  1173. struct ieee80211_tx_info *tx_info;
  1174. while (i != end) {
  1175. skb = (struct sk_buff *)di->txp[i];
  1176. if (skb != NULL) {
  1177. tx_info = (struct ieee80211_tx_info *)skb->cb;
  1178. (callback_fnc)(tx_info, arg_a);
  1179. }
  1180. i = nexttxd(di, i);
  1181. }
  1182. }