dma.c 39 KB

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