dma.c 43 KB

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