dma.c 42 KB

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