dma.c 38 KB

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