dma.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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 = (struct dma_info *)pub;
  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 = (struct dma_info *)pub;
  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 = (struct dma_info *)pub;
  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 is refill failed completely and ring is empty this will stall
  867. * the rx dma and user might want to call rxfill again asap. This unlikely
  868. * happens on memory-rich NIC, but often on memory-constrained dongle
  869. */
  870. bool dma_rxfill(struct dma_pub *pub)
  871. {
  872. struct dma_info *di = (struct dma_info *)pub;
  873. struct sk_buff *p;
  874. u16 rxin, rxout;
  875. u32 flags = 0;
  876. uint n;
  877. uint i;
  878. dma_addr_t pa;
  879. uint extra_offset = 0;
  880. bool ring_empty;
  881. ring_empty = false;
  882. /*
  883. * Determine how many receive buffers we're lacking
  884. * from the full complement, allocate, initialize,
  885. * and post them, then update the chip rx lastdscr.
  886. */
  887. rxin = di->rxin;
  888. rxout = di->rxout;
  889. n = di->nrxpost - nrxdactive(di, rxin, rxout);
  890. brcms_dbg_dma(di->core, "%s: post %d\n", di->name, n);
  891. if (di->rxbufsize > BCMEXTRAHDROOM)
  892. extra_offset = di->rxextrahdrroom;
  893. for (i = 0; i < n; i++) {
  894. /*
  895. * the di->rxbufsize doesn't include the extra headroom,
  896. * we need to add it to the size to be allocated
  897. */
  898. p = brcmu_pkt_buf_get_skb(di->rxbufsize + extra_offset);
  899. if (p == NULL) {
  900. brcms_dbg_dma(di->core, "%s: out of rxbufs\n",
  901. di->name);
  902. if (i == 0 && dma64_rxidle(di)) {
  903. brcms_dbg_dma(di->core, "%s: ring is empty !\n",
  904. di->name);
  905. ring_empty = true;
  906. }
  907. di->dma.rxnobuf++;
  908. break;
  909. }
  910. /* reserve an extra headroom, if applicable */
  911. if (extra_offset)
  912. skb_pull(p, extra_offset);
  913. /* Do a cached write instead of uncached write since DMA_MAP
  914. * will flush the cache.
  915. */
  916. *(u32 *) (p->data) = 0;
  917. pa = dma_map_single(di->dmadev, p->data, di->rxbufsize,
  918. DMA_FROM_DEVICE);
  919. /* save the free packet pointer */
  920. di->rxp[rxout] = p;
  921. /* reset flags for each descriptor */
  922. flags = 0;
  923. if (rxout == (di->nrxd - 1))
  924. flags = D64_CTRL1_EOT;
  925. dma64_dd_upd(di, di->rxd64, pa, rxout, &flags,
  926. di->rxbufsize);
  927. rxout = nextrxd(di, rxout);
  928. }
  929. di->rxout = rxout;
  930. /* update the chip lastdscr pointer */
  931. bcma_write32(di->core, DMA64RXREGOFFS(di, ptr),
  932. di->rcvptrbase + I2B(rxout, struct dma64desc));
  933. return ring_empty;
  934. }
  935. void dma_rxreclaim(struct dma_pub *pub)
  936. {
  937. struct dma_info *di = (struct dma_info *)pub;
  938. struct sk_buff *p;
  939. brcms_dbg_dma(di->core, "%s:\n", di->name);
  940. while ((p = _dma_getnextrxp(di, true)))
  941. brcmu_pkt_buf_free_skb(p);
  942. }
  943. void dma_counterreset(struct dma_pub *pub)
  944. {
  945. /* reset all software counters */
  946. pub->rxgiants = 0;
  947. pub->rxnobuf = 0;
  948. pub->txnobuf = 0;
  949. }
  950. /* get the address of the var in order to change later */
  951. unsigned long dma_getvar(struct dma_pub *pub, const char *name)
  952. {
  953. struct dma_info *di = (struct dma_info *)pub;
  954. if (!strcmp(name, "&txavail"))
  955. return (unsigned long)&(di->dma.txavail);
  956. return 0;
  957. }
  958. /* 64-bit DMA functions */
  959. void dma_txinit(struct dma_pub *pub)
  960. {
  961. struct dma_info *di = (struct dma_info *)pub;
  962. u32 control = D64_XC_XE;
  963. brcms_dbg_dma(di->core, "%s:\n", di->name);
  964. if (di->ntxd == 0)
  965. return;
  966. di->txin = di->txout = 0;
  967. di->dma.txavail = di->ntxd - 1;
  968. /* clear tx descriptor ring */
  969. memset(di->txd64, '\0', (di->ntxd * sizeof(struct dma64desc)));
  970. /* DMA engine with out alignment requirement requires table to be inited
  971. * before enabling the engine
  972. */
  973. if (!di->aligndesc_4k)
  974. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  975. if ((di->dma.dmactrlflags & DMA_CTRL_PEN) == 0)
  976. control |= D64_XC_PD;
  977. bcma_set32(di->core, DMA64TXREGOFFS(di, control), control);
  978. /* DMA engine with alignment requirement requires table to be inited
  979. * before enabling the engine
  980. */
  981. if (di->aligndesc_4k)
  982. _dma_ddtable_init(di, DMA_TX, di->txdpa);
  983. }
  984. void dma_txsuspend(struct dma_pub *pub)
  985. {
  986. struct dma_info *di = (struct dma_info *)pub;
  987. brcms_dbg_dma(di->core, "%s:\n", di->name);
  988. if (di->ntxd == 0)
  989. return;
  990. bcma_set32(di->core, DMA64TXREGOFFS(di, control), D64_XC_SE);
  991. }
  992. void dma_txresume(struct dma_pub *pub)
  993. {
  994. struct dma_info *di = (struct dma_info *)pub;
  995. brcms_dbg_dma(di->core, "%s:\n", di->name);
  996. if (di->ntxd == 0)
  997. return;
  998. bcma_mask32(di->core, DMA64TXREGOFFS(di, control), ~D64_XC_SE);
  999. }
  1000. bool dma_txsuspended(struct dma_pub *pub)
  1001. {
  1002. struct dma_info *di = (struct dma_info *)pub;
  1003. return (di->ntxd == 0) ||
  1004. ((bcma_read32(di->core,
  1005. DMA64TXREGOFFS(di, control)) & D64_XC_SE) ==
  1006. D64_XC_SE);
  1007. }
  1008. void dma_txreclaim(struct dma_pub *pub, enum txd_range range)
  1009. {
  1010. struct dma_info *di = (struct dma_info *)pub;
  1011. struct sk_buff *p;
  1012. brcms_dbg_dma(di->core, "%s: %s\n",
  1013. di->name,
  1014. range == DMA_RANGE_ALL ? "all" :
  1015. range == DMA_RANGE_TRANSMITTED ? "transmitted" :
  1016. "transferred");
  1017. if (di->txin == di->txout)
  1018. return;
  1019. while ((p = dma_getnexttxp(pub, range))) {
  1020. /* For unframed data, we don't have any packets to free */
  1021. if (!(di->dma.dmactrlflags & DMA_CTRL_UNFRAMED))
  1022. brcmu_pkt_buf_free_skb(p);
  1023. }
  1024. }
  1025. bool dma_txreset(struct dma_pub *pub)
  1026. {
  1027. struct dma_info *di = (struct dma_info *)pub;
  1028. u32 status;
  1029. if (di->ntxd == 0)
  1030. return true;
  1031. /* suspend tx DMA first */
  1032. bcma_write32(di->core, DMA64TXREGOFFS(di, control), D64_XC_SE);
  1033. SPINWAIT(((status =
  1034. (bcma_read32(di->core, DMA64TXREGOFFS(di, status0)) &
  1035. D64_XS0_XS_MASK)) != D64_XS0_XS_DISABLED) &&
  1036. (status != D64_XS0_XS_IDLE) && (status != D64_XS0_XS_STOPPED),
  1037. 10000);
  1038. bcma_write32(di->core, DMA64TXREGOFFS(di, control), 0);
  1039. SPINWAIT(((status =
  1040. (bcma_read32(di->core, DMA64TXREGOFFS(di, status0)) &
  1041. D64_XS0_XS_MASK)) != D64_XS0_XS_DISABLED), 10000);
  1042. /* wait for the last transaction to complete */
  1043. udelay(300);
  1044. return status == D64_XS0_XS_DISABLED;
  1045. }
  1046. bool dma_rxreset(struct dma_pub *pub)
  1047. {
  1048. struct dma_info *di = (struct dma_info *)pub;
  1049. u32 status;
  1050. if (di->nrxd == 0)
  1051. return true;
  1052. bcma_write32(di->core, DMA64RXREGOFFS(di, control), 0);
  1053. SPINWAIT(((status =
  1054. (bcma_read32(di->core, DMA64RXREGOFFS(di, status0)) &
  1055. D64_RS0_RS_MASK)) != D64_RS0_RS_DISABLED), 10000);
  1056. return status == D64_RS0_RS_DISABLED;
  1057. }
  1058. static void dma_txenq(struct dma_info *di, struct sk_buff *p)
  1059. {
  1060. unsigned char *data;
  1061. uint len;
  1062. u16 txout;
  1063. u32 flags = 0;
  1064. dma_addr_t pa;
  1065. txout = di->txout;
  1066. if (WARN_ON(nexttxd(di, txout) == di->txin))
  1067. return;
  1068. /*
  1069. * obtain and initialize transmit descriptor entry.
  1070. */
  1071. data = p->data;
  1072. len = p->len;
  1073. /* get physical address of buffer start */
  1074. pa = dma_map_single(di->dmadev, data, len, DMA_TO_DEVICE);
  1075. /* With a DMA segment list, Descriptor table is filled
  1076. * using the segment list instead of looping over
  1077. * buffers in multi-chain DMA. Therefore, EOF for SGLIST
  1078. * is when end of segment list is reached.
  1079. */
  1080. flags = D64_CTRL1_SOF | D64_CTRL1_IOC | D64_CTRL1_EOF;
  1081. if (txout == (di->ntxd - 1))
  1082. flags |= D64_CTRL1_EOT;
  1083. dma64_dd_upd(di, di->txd64, pa, txout, &flags, len);
  1084. txout = nexttxd(di, txout);
  1085. /* save the packet */
  1086. di->txp[prevtxd(di, txout)] = p;
  1087. /* bump the tx descriptor index */
  1088. di->txout = txout;
  1089. }
  1090. static void ampdu_finalize(struct dma_info *di)
  1091. {
  1092. struct brcms_ampdu_session *session = &di->ampdu_session;
  1093. struct sk_buff *p;
  1094. trace_brcms_ampdu_session(&session->wlc->hw->d11core->dev,
  1095. session->max_ampdu_len,
  1096. session->max_ampdu_frames,
  1097. session->ampdu_len,
  1098. skb_queue_len(&session->skb_list),
  1099. session->dma_len);
  1100. if (WARN_ON(skb_queue_empty(&session->skb_list)))
  1101. return;
  1102. brcms_c_ampdu_finalize(session);
  1103. while (!skb_queue_empty(&session->skb_list)) {
  1104. p = skb_dequeue(&session->skb_list);
  1105. dma_txenq(di, p);
  1106. }
  1107. bcma_write32(di->core, DMA64TXREGOFFS(di, ptr),
  1108. di->xmtptrbase + I2B(di->txout, struct dma64desc));
  1109. brcms_c_ampdu_reset_session(session, session->wlc);
  1110. }
  1111. static void prep_ampdu_frame(struct dma_info *di, struct sk_buff *p)
  1112. {
  1113. struct brcms_ampdu_session *session = &di->ampdu_session;
  1114. int ret;
  1115. ret = brcms_c_ampdu_add_frame(session, p);
  1116. if (ret == -ENOSPC) {
  1117. /*
  1118. * AMPDU cannot accomodate this frame. Close out the in-
  1119. * progress AMPDU session and start a new one.
  1120. */
  1121. ampdu_finalize(di);
  1122. ret = brcms_c_ampdu_add_frame(session, p);
  1123. }
  1124. WARN_ON(ret);
  1125. }
  1126. /* Update count of available tx descriptors based on current DMA state */
  1127. static void dma_update_txavail(struct dma_info *di)
  1128. {
  1129. /*
  1130. * Available space is number of descriptors less the number of
  1131. * active descriptors and the number of queued AMPDU frames.
  1132. */
  1133. di->dma.txavail = di->ntxd - ntxdactive(di, di->txin, di->txout) -
  1134. skb_queue_len(&di->ampdu_session.skb_list) - 1;
  1135. }
  1136. /*
  1137. * !! tx entry routine
  1138. * WARNING: call must check the return value for error.
  1139. * the error(toss frames) could be fatal and cause many subsequent hard
  1140. * to debug problems
  1141. */
  1142. int dma_txfast(struct brcms_c_info *wlc, struct dma_pub *pub,
  1143. struct sk_buff *p)
  1144. {
  1145. struct dma_info *di = (struct dma_info *)pub;
  1146. struct brcms_ampdu_session *session = &di->ampdu_session;
  1147. struct ieee80211_tx_info *tx_info;
  1148. bool is_ampdu;
  1149. /* no use to transmit a zero length packet */
  1150. if (p->len == 0)
  1151. return 0;
  1152. /* return nonzero if out of tx descriptors */
  1153. if (di->dma.txavail == 0 || nexttxd(di, di->txout) == di->txin)
  1154. goto outoftxd;
  1155. tx_info = IEEE80211_SKB_CB(p);
  1156. is_ampdu = tx_info->flags & IEEE80211_TX_CTL_AMPDU;
  1157. if (is_ampdu)
  1158. prep_ampdu_frame(di, p);
  1159. else
  1160. dma_txenq(di, p);
  1161. /* tx flow control */
  1162. dma_update_txavail(di);
  1163. /* kick the chip */
  1164. if (is_ampdu) {
  1165. /*
  1166. * Start sending data if we've got a full AMPDU, there's
  1167. * no more space in the DMA ring, or the ring isn't
  1168. * currently transmitting.
  1169. */
  1170. if (skb_queue_len(&session->skb_list) == session->max_ampdu_frames ||
  1171. di->dma.txavail == 0 || dma64_txidle(di))
  1172. ampdu_finalize(di);
  1173. } else {
  1174. bcma_write32(di->core, DMA64TXREGOFFS(di, ptr),
  1175. di->xmtptrbase + I2B(di->txout, struct dma64desc));
  1176. }
  1177. return 0;
  1178. outoftxd:
  1179. brcms_dbg_dma(di->core, "%s: out of txds !!!\n", di->name);
  1180. brcmu_pkt_buf_free_skb(p);
  1181. di->dma.txavail = 0;
  1182. di->dma.txnobuf++;
  1183. return -ENOSPC;
  1184. }
  1185. void dma_txflush(struct dma_pub *pub)
  1186. {
  1187. struct dma_info *di = (struct dma_info *)pub;
  1188. struct brcms_ampdu_session *session = &di->ampdu_session;
  1189. if (!skb_queue_empty(&session->skb_list))
  1190. ampdu_finalize(di);
  1191. }
  1192. int dma_txpending(struct dma_pub *pub)
  1193. {
  1194. struct dma_info *di = (struct dma_info *)pub;
  1195. return ntxdactive(di, di->txin, di->txout);
  1196. }
  1197. /*
  1198. * If we have an active AMPDU session and are not transmitting,
  1199. * this function will force tx to start.
  1200. */
  1201. void dma_kick_tx(struct dma_pub *pub)
  1202. {
  1203. struct dma_info *di = (struct dma_info *)pub;
  1204. struct brcms_ampdu_session *session = &di->ampdu_session;
  1205. if (!skb_queue_empty(&session->skb_list) && dma64_txidle(di))
  1206. ampdu_finalize(di);
  1207. }
  1208. /*
  1209. * Reclaim next completed txd (txds if using chained buffers) in the range
  1210. * specified and return associated packet.
  1211. * If range is DMA_RANGE_TRANSMITTED, reclaim descriptors that have be
  1212. * transmitted as noted by the hardware "CurrDescr" pointer.
  1213. * If range is DMA_RANGE_TRANSFERED, reclaim descriptors that have be
  1214. * transferred by the DMA as noted by the hardware "ActiveDescr" pointer.
  1215. * If range is DMA_RANGE_ALL, reclaim all txd(s) posted to the ring and
  1216. * return associated packet regardless of the value of hardware pointers.
  1217. */
  1218. struct sk_buff *dma_getnexttxp(struct dma_pub *pub, enum txd_range range)
  1219. {
  1220. struct dma_info *di = (struct dma_info *)pub;
  1221. u16 start, end, i;
  1222. u16 active_desc;
  1223. struct sk_buff *txp;
  1224. brcms_dbg_dma(di->core, "%s: %s\n",
  1225. di->name,
  1226. range == DMA_RANGE_ALL ? "all" :
  1227. range == DMA_RANGE_TRANSMITTED ? "transmitted" :
  1228. "transferred");
  1229. if (di->ntxd == 0)
  1230. return NULL;
  1231. txp = NULL;
  1232. start = di->txin;
  1233. if (range == DMA_RANGE_ALL)
  1234. end = di->txout;
  1235. else {
  1236. end = (u16) (B2I(((bcma_read32(di->core,
  1237. DMA64TXREGOFFS(di, status0)) &
  1238. D64_XS0_CD_MASK) - di->xmtptrbase) &
  1239. D64_XS0_CD_MASK, struct dma64desc));
  1240. if (range == DMA_RANGE_TRANSFERED) {
  1241. active_desc =
  1242. (u16)(bcma_read32(di->core,
  1243. DMA64TXREGOFFS(di, status1)) &
  1244. D64_XS1_AD_MASK);
  1245. active_desc =
  1246. (active_desc - di->xmtptrbase) & D64_XS0_CD_MASK;
  1247. active_desc = B2I(active_desc, struct dma64desc);
  1248. if (end != active_desc)
  1249. end = prevtxd(di, active_desc);
  1250. }
  1251. }
  1252. if ((start == 0) && (end > di->txout))
  1253. goto bogus;
  1254. for (i = start; i != end && !txp; i = nexttxd(di, i)) {
  1255. dma_addr_t pa;
  1256. uint size;
  1257. pa = le32_to_cpu(di->txd64[i].addrlow) - di->dataoffsetlow;
  1258. size =
  1259. (le32_to_cpu(di->txd64[i].ctrl2) &
  1260. D64_CTRL2_BC_MASK);
  1261. di->txd64[i].addrlow = cpu_to_le32(0xdeadbeef);
  1262. di->txd64[i].addrhigh = cpu_to_le32(0xdeadbeef);
  1263. txp = di->txp[i];
  1264. di->txp[i] = NULL;
  1265. dma_unmap_single(di->dmadev, pa, size, DMA_TO_DEVICE);
  1266. }
  1267. di->txin = i;
  1268. /* tx flow control */
  1269. dma_update_txavail(di);
  1270. return txp;
  1271. bogus:
  1272. brcms_dbg_dma(di->core, "bogus curr: start %d end %d txout %d\n",
  1273. start, end, di->txout);
  1274. return NULL;
  1275. }
  1276. /*
  1277. * Mac80211 initiated actions sometimes require packets in the DMA queue to be
  1278. * modified. The modified portion of the packet is not under control of the DMA
  1279. * engine. This function calls a caller-supplied function for each packet in
  1280. * the caller specified dma chain.
  1281. */
  1282. void dma_walk_packets(struct dma_pub *dmah, void (*callback_fnc)
  1283. (void *pkt, void *arg_a), void *arg_a)
  1284. {
  1285. struct dma_info *di = (struct dma_info *) dmah;
  1286. uint i = di->txin;
  1287. uint end = di->txout;
  1288. struct sk_buff *skb;
  1289. struct ieee80211_tx_info *tx_info;
  1290. while (i != end) {
  1291. skb = di->txp[i];
  1292. if (skb != NULL) {
  1293. tx_info = (struct ieee80211_tx_info *)skb->cb;
  1294. (callback_fnc)(tx_info, arg_a);
  1295. }
  1296. i = nexttxd(di, i);
  1297. }
  1298. }