bgmac.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. /*
  2. * Driver for (BCM4706)? GBit MAC core on BCMA bus.
  3. *
  4. * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
  5. *
  6. * Licensed under the GNU/GPL. See COPYING for details.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/bcma/bcma.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/bcm47xx_nvram.h>
  12. #include "bgmac.h"
  13. static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask,
  14. u32 value, int timeout)
  15. {
  16. u32 val;
  17. int i;
  18. for (i = 0; i < timeout / 10; i++) {
  19. val = bgmac_read(bgmac, reg);
  20. if ((val & mask) == value)
  21. return true;
  22. udelay(10);
  23. }
  24. dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg);
  25. return false;
  26. }
  27. /**************************************************
  28. * DMA
  29. **************************************************/
  30. static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  31. {
  32. u32 val;
  33. int i;
  34. if (!ring->mmio_base)
  35. return;
  36. /* Suspend DMA TX ring first.
  37. * bgmac_wait_value doesn't support waiting for any of few values, so
  38. * implement whole loop here.
  39. */
  40. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
  41. BGMAC_DMA_TX_SUSPEND);
  42. for (i = 0; i < 10000 / 10; i++) {
  43. val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  44. val &= BGMAC_DMA_TX_STAT;
  45. if (val == BGMAC_DMA_TX_STAT_DISABLED ||
  46. val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
  47. val == BGMAC_DMA_TX_STAT_STOPPED) {
  48. i = 0;
  49. break;
  50. }
  51. udelay(10);
  52. }
  53. if (i)
  54. dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
  55. ring->mmio_base, val);
  56. /* Remove SUSPEND bit */
  57. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
  58. if (!bgmac_wait_value(bgmac,
  59. ring->mmio_base + BGMAC_DMA_TX_STATUS,
  60. BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
  61. 10000)) {
  62. dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
  63. ring->mmio_base);
  64. udelay(300);
  65. val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  66. if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
  67. dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n",
  68. ring->mmio_base);
  69. }
  70. }
  71. static void bgmac_dma_tx_enable(struct bgmac *bgmac,
  72. struct bgmac_dma_ring *ring)
  73. {
  74. u32 ctl;
  75. ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
  76. if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) {
  77. ctl &= ~BGMAC_DMA_TX_BL_MASK;
  78. ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
  79. ctl &= ~BGMAC_DMA_TX_MR_MASK;
  80. ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
  81. ctl &= ~BGMAC_DMA_TX_PC_MASK;
  82. ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
  83. ctl &= ~BGMAC_DMA_TX_PT_MASK;
  84. ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
  85. }
  86. ctl |= BGMAC_DMA_TX_ENABLE;
  87. ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
  88. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
  89. }
  90. static void
  91. bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
  92. int i, int len, u32 ctl0)
  93. {
  94. struct bgmac_slot_info *slot;
  95. struct bgmac_dma_desc *dma_desc;
  96. u32 ctl1;
  97. if (i == BGMAC_TX_RING_SLOTS - 1)
  98. ctl0 |= BGMAC_DESC_CTL0_EOT;
  99. ctl1 = len & BGMAC_DESC_CTL1_LEN;
  100. slot = &ring->slots[i];
  101. dma_desc = &ring->cpu_base[i];
  102. dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
  103. dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
  104. dma_desc->ctl0 = cpu_to_le32(ctl0);
  105. dma_desc->ctl1 = cpu_to_le32(ctl1);
  106. }
  107. static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
  108. struct bgmac_dma_ring *ring,
  109. struct sk_buff *skb)
  110. {
  111. struct device *dma_dev = bgmac->dma_dev;
  112. struct net_device *net_dev = bgmac->net_dev;
  113. int index = ring->end % BGMAC_TX_RING_SLOTS;
  114. struct bgmac_slot_info *slot = &ring->slots[index];
  115. int nr_frags;
  116. u32 flags;
  117. int i;
  118. if (skb->len > BGMAC_DESC_CTL1_LEN) {
  119. netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len);
  120. goto err_drop;
  121. }
  122. if (skb->ip_summed == CHECKSUM_PARTIAL)
  123. skb_checksum_help(skb);
  124. nr_frags = skb_shinfo(skb)->nr_frags;
  125. /* ring->end - ring->start will return the number of valid slots,
  126. * even when ring->end overflows
  127. */
  128. if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
  129. netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n");
  130. netif_stop_queue(net_dev);
  131. return NETDEV_TX_BUSY;
  132. }
  133. slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
  134. DMA_TO_DEVICE);
  135. if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
  136. goto err_dma_head;
  137. flags = BGMAC_DESC_CTL0_SOF;
  138. if (!nr_frags)
  139. flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
  140. bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
  141. flags = 0;
  142. for (i = 0; i < nr_frags; i++) {
  143. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  144. int len = skb_frag_size(frag);
  145. index = (index + 1) % BGMAC_TX_RING_SLOTS;
  146. slot = &ring->slots[index];
  147. slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
  148. len, DMA_TO_DEVICE);
  149. if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
  150. goto err_dma;
  151. if (i == nr_frags - 1)
  152. flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
  153. bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
  154. }
  155. slot->skb = skb;
  156. ring->end += nr_frags + 1;
  157. netdev_sent_queue(net_dev, skb->len);
  158. wmb();
  159. /* Increase ring->end to point empty slot. We tell hardware the first
  160. * slot it should *not* read.
  161. */
  162. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
  163. ring->index_base +
  164. (ring->end % BGMAC_TX_RING_SLOTS) *
  165. sizeof(struct bgmac_dma_desc));
  166. if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
  167. netif_stop_queue(net_dev);
  168. return NETDEV_TX_OK;
  169. err_dma:
  170. dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
  171. DMA_TO_DEVICE);
  172. while (i-- > 0) {
  173. int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
  174. struct bgmac_slot_info *slot = &ring->slots[index];
  175. u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
  176. int len = ctl1 & BGMAC_DESC_CTL1_LEN;
  177. dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
  178. }
  179. err_dma_head:
  180. netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n",
  181. ring->mmio_base);
  182. err_drop:
  183. dev_kfree_skb(skb);
  184. net_dev->stats.tx_dropped++;
  185. net_dev->stats.tx_errors++;
  186. return NETDEV_TX_OK;
  187. }
  188. /* Free transmitted packets */
  189. static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  190. {
  191. struct device *dma_dev = bgmac->dma_dev;
  192. int empty_slot;
  193. bool freed = false;
  194. unsigned bytes_compl = 0, pkts_compl = 0;
  195. /* The last slot that hardware didn't consume yet */
  196. empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  197. empty_slot &= BGMAC_DMA_TX_STATDPTR;
  198. empty_slot -= ring->index_base;
  199. empty_slot &= BGMAC_DMA_TX_STATDPTR;
  200. empty_slot /= sizeof(struct bgmac_dma_desc);
  201. while (ring->start != ring->end) {
  202. int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
  203. struct bgmac_slot_info *slot = &ring->slots[slot_idx];
  204. u32 ctl0, ctl1;
  205. int len;
  206. if (slot_idx == empty_slot)
  207. break;
  208. ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
  209. ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
  210. len = ctl1 & BGMAC_DESC_CTL1_LEN;
  211. if (ctl0 & BGMAC_DESC_CTL0_SOF)
  212. /* Unmap no longer used buffer */
  213. dma_unmap_single(dma_dev, slot->dma_addr, len,
  214. DMA_TO_DEVICE);
  215. else
  216. dma_unmap_page(dma_dev, slot->dma_addr, len,
  217. DMA_TO_DEVICE);
  218. if (slot->skb) {
  219. bgmac->net_dev->stats.tx_bytes += slot->skb->len;
  220. bgmac->net_dev->stats.tx_packets++;
  221. bytes_compl += slot->skb->len;
  222. pkts_compl++;
  223. /* Free memory! :) */
  224. dev_kfree_skb(slot->skb);
  225. slot->skb = NULL;
  226. }
  227. slot->dma_addr = 0;
  228. ring->start++;
  229. freed = true;
  230. }
  231. if (!pkts_compl)
  232. return;
  233. netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
  234. if (netif_queue_stopped(bgmac->net_dev))
  235. netif_wake_queue(bgmac->net_dev);
  236. }
  237. static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  238. {
  239. if (!ring->mmio_base)
  240. return;
  241. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
  242. if (!bgmac_wait_value(bgmac,
  243. ring->mmio_base + BGMAC_DMA_RX_STATUS,
  244. BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
  245. 10000))
  246. dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n",
  247. ring->mmio_base);
  248. }
  249. static void bgmac_dma_rx_enable(struct bgmac *bgmac,
  250. struct bgmac_dma_ring *ring)
  251. {
  252. u32 ctl;
  253. ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
  254. /* preserve ONLY bits 16-17 from current hardware value */
  255. ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
  256. if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) {
  257. ctl &= ~BGMAC_DMA_RX_BL_MASK;
  258. ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
  259. ctl &= ~BGMAC_DMA_RX_PC_MASK;
  260. ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
  261. ctl &= ~BGMAC_DMA_RX_PT_MASK;
  262. ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
  263. }
  264. ctl |= BGMAC_DMA_RX_ENABLE;
  265. ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
  266. ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
  267. ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
  268. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
  269. }
  270. static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
  271. struct bgmac_slot_info *slot)
  272. {
  273. struct device *dma_dev = bgmac->dma_dev;
  274. dma_addr_t dma_addr;
  275. struct bgmac_rx_header *rx;
  276. void *buf;
  277. /* Alloc skb */
  278. buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
  279. if (!buf)
  280. return -ENOMEM;
  281. /* Poison - if everything goes fine, hardware will overwrite it */
  282. rx = buf + BGMAC_RX_BUF_OFFSET;
  283. rx->len = cpu_to_le16(0xdead);
  284. rx->flags = cpu_to_le16(0xbeef);
  285. /* Map skb for the DMA */
  286. dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
  287. BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
  288. if (dma_mapping_error(dma_dev, dma_addr)) {
  289. netdev_err(bgmac->net_dev, "DMA mapping error\n");
  290. put_page(virt_to_head_page(buf));
  291. return -ENOMEM;
  292. }
  293. /* Update the slot */
  294. slot->buf = buf;
  295. slot->dma_addr = dma_addr;
  296. return 0;
  297. }
  298. static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
  299. struct bgmac_dma_ring *ring)
  300. {
  301. dma_wmb();
  302. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
  303. ring->index_base +
  304. ring->end * sizeof(struct bgmac_dma_desc));
  305. }
  306. static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
  307. struct bgmac_dma_ring *ring, int desc_idx)
  308. {
  309. struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
  310. u32 ctl0 = 0, ctl1 = 0;
  311. if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
  312. ctl0 |= BGMAC_DESC_CTL0_EOT;
  313. ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
  314. /* Is there any BGMAC device that requires extension? */
  315. /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
  316. * B43_DMA64_DCTL1_ADDREXT_MASK;
  317. */
  318. dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
  319. dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
  320. dma_desc->ctl0 = cpu_to_le32(ctl0);
  321. dma_desc->ctl1 = cpu_to_le32(ctl1);
  322. ring->end = desc_idx;
  323. }
  324. static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
  325. struct bgmac_slot_info *slot)
  326. {
  327. struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
  328. dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
  329. DMA_FROM_DEVICE);
  330. rx->len = cpu_to_le16(0xdead);
  331. rx->flags = cpu_to_le16(0xbeef);
  332. dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
  333. DMA_FROM_DEVICE);
  334. }
  335. static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
  336. int weight)
  337. {
  338. u32 end_slot;
  339. int handled = 0;
  340. end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
  341. end_slot &= BGMAC_DMA_RX_STATDPTR;
  342. end_slot -= ring->index_base;
  343. end_slot &= BGMAC_DMA_RX_STATDPTR;
  344. end_slot /= sizeof(struct bgmac_dma_desc);
  345. while (ring->start != end_slot) {
  346. struct device *dma_dev = bgmac->dma_dev;
  347. struct bgmac_slot_info *slot = &ring->slots[ring->start];
  348. struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
  349. struct sk_buff *skb;
  350. void *buf = slot->buf;
  351. dma_addr_t dma_addr = slot->dma_addr;
  352. u16 len, flags;
  353. do {
  354. /* Prepare new skb as replacement */
  355. if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
  356. bgmac_dma_rx_poison_buf(dma_dev, slot);
  357. break;
  358. }
  359. /* Unmap buffer to make it accessible to the CPU */
  360. dma_unmap_single(dma_dev, dma_addr,
  361. BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
  362. /* Get info from the header */
  363. len = le16_to_cpu(rx->len);
  364. flags = le16_to_cpu(rx->flags);
  365. /* Check for poison and drop or pass the packet */
  366. if (len == 0xdead && flags == 0xbeef) {
  367. netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n",
  368. ring->start);
  369. put_page(virt_to_head_page(buf));
  370. bgmac->net_dev->stats.rx_errors++;
  371. break;
  372. }
  373. if (len > BGMAC_RX_ALLOC_SIZE) {
  374. netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n",
  375. ring->start);
  376. put_page(virt_to_head_page(buf));
  377. bgmac->net_dev->stats.rx_length_errors++;
  378. bgmac->net_dev->stats.rx_errors++;
  379. break;
  380. }
  381. /* Omit CRC. */
  382. len -= ETH_FCS_LEN;
  383. skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
  384. if (unlikely(!skb)) {
  385. netdev_err(bgmac->net_dev, "build_skb failed\n");
  386. put_page(virt_to_head_page(buf));
  387. bgmac->net_dev->stats.rx_errors++;
  388. break;
  389. }
  390. skb_put(skb, BGMAC_RX_FRAME_OFFSET +
  391. BGMAC_RX_BUF_OFFSET + len);
  392. skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
  393. BGMAC_RX_BUF_OFFSET);
  394. skb_checksum_none_assert(skb);
  395. skb->protocol = eth_type_trans(skb, bgmac->net_dev);
  396. bgmac->net_dev->stats.rx_bytes += len;
  397. bgmac->net_dev->stats.rx_packets++;
  398. napi_gro_receive(&bgmac->napi, skb);
  399. handled++;
  400. } while (0);
  401. bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
  402. if (++ring->start >= BGMAC_RX_RING_SLOTS)
  403. ring->start = 0;
  404. if (handled >= weight) /* Should never be greater */
  405. break;
  406. }
  407. bgmac_dma_rx_update_index(bgmac, ring);
  408. return handled;
  409. }
  410. /* Does ring support unaligned addressing? */
  411. static bool bgmac_dma_unaligned(struct bgmac *bgmac,
  412. struct bgmac_dma_ring *ring,
  413. enum bgmac_dma_ring_type ring_type)
  414. {
  415. switch (ring_type) {
  416. case BGMAC_DMA_RING_TX:
  417. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
  418. 0xff0);
  419. if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
  420. return true;
  421. break;
  422. case BGMAC_DMA_RING_RX:
  423. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
  424. 0xff0);
  425. if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
  426. return true;
  427. break;
  428. }
  429. return false;
  430. }
  431. static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
  432. struct bgmac_dma_ring *ring)
  433. {
  434. struct device *dma_dev = bgmac->dma_dev;
  435. struct bgmac_dma_desc *dma_desc = ring->cpu_base;
  436. struct bgmac_slot_info *slot;
  437. int i;
  438. for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
  439. u32 ctl1 = le32_to_cpu(dma_desc[i].ctl1);
  440. unsigned int len = ctl1 & BGMAC_DESC_CTL1_LEN;
  441. slot = &ring->slots[i];
  442. dev_kfree_skb(slot->skb);
  443. if (!slot->dma_addr)
  444. continue;
  445. if (slot->skb)
  446. dma_unmap_single(dma_dev, slot->dma_addr,
  447. len, DMA_TO_DEVICE);
  448. else
  449. dma_unmap_page(dma_dev, slot->dma_addr,
  450. len, DMA_TO_DEVICE);
  451. }
  452. }
  453. static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
  454. struct bgmac_dma_ring *ring)
  455. {
  456. struct device *dma_dev = bgmac->dma_dev;
  457. struct bgmac_slot_info *slot;
  458. int i;
  459. for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
  460. slot = &ring->slots[i];
  461. if (!slot->dma_addr)
  462. continue;
  463. dma_unmap_single(dma_dev, slot->dma_addr,
  464. BGMAC_RX_BUF_SIZE,
  465. DMA_FROM_DEVICE);
  466. put_page(virt_to_head_page(slot->buf));
  467. slot->dma_addr = 0;
  468. }
  469. }
  470. static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
  471. struct bgmac_dma_ring *ring,
  472. int num_slots)
  473. {
  474. struct device *dma_dev = bgmac->dma_dev;
  475. int size;
  476. if (!ring->cpu_base)
  477. return;
  478. /* Free ring of descriptors */
  479. size = num_slots * sizeof(struct bgmac_dma_desc);
  480. dma_free_coherent(dma_dev, size, ring->cpu_base,
  481. ring->dma_base);
  482. }
  483. static void bgmac_dma_cleanup(struct bgmac *bgmac)
  484. {
  485. int i;
  486. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  487. bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
  488. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  489. bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
  490. }
  491. static void bgmac_dma_free(struct bgmac *bgmac)
  492. {
  493. int i;
  494. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  495. bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
  496. BGMAC_TX_RING_SLOTS);
  497. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  498. bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
  499. BGMAC_RX_RING_SLOTS);
  500. }
  501. static int bgmac_dma_alloc(struct bgmac *bgmac)
  502. {
  503. struct device *dma_dev = bgmac->dma_dev;
  504. struct bgmac_dma_ring *ring;
  505. static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
  506. BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
  507. int size; /* ring size: different for Tx and Rx */
  508. int err;
  509. int i;
  510. BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
  511. BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
  512. if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) {
  513. dev_err(bgmac->dev, "Core does not report 64-bit DMA\n");
  514. return -ENOTSUPP;
  515. }
  516. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
  517. ring = &bgmac->tx_ring[i];
  518. ring->mmio_base = ring_base[i];
  519. /* Alloc ring of descriptors */
  520. size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
  521. ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
  522. &ring->dma_base,
  523. GFP_KERNEL);
  524. if (!ring->cpu_base) {
  525. dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n",
  526. ring->mmio_base);
  527. goto err_dma_free;
  528. }
  529. ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
  530. BGMAC_DMA_RING_TX);
  531. if (ring->unaligned)
  532. ring->index_base = lower_32_bits(ring->dma_base);
  533. else
  534. ring->index_base = 0;
  535. /* No need to alloc TX slots yet */
  536. }
  537. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
  538. ring = &bgmac->rx_ring[i];
  539. ring->mmio_base = ring_base[i];
  540. /* Alloc ring of descriptors */
  541. size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
  542. ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
  543. &ring->dma_base,
  544. GFP_KERNEL);
  545. if (!ring->cpu_base) {
  546. dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n",
  547. ring->mmio_base);
  548. err = -ENOMEM;
  549. goto err_dma_free;
  550. }
  551. ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
  552. BGMAC_DMA_RING_RX);
  553. if (ring->unaligned)
  554. ring->index_base = lower_32_bits(ring->dma_base);
  555. else
  556. ring->index_base = 0;
  557. }
  558. return 0;
  559. err_dma_free:
  560. bgmac_dma_free(bgmac);
  561. return -ENOMEM;
  562. }
  563. static int bgmac_dma_init(struct bgmac *bgmac)
  564. {
  565. struct bgmac_dma_ring *ring;
  566. int i, err;
  567. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
  568. ring = &bgmac->tx_ring[i];
  569. if (!ring->unaligned)
  570. bgmac_dma_tx_enable(bgmac, ring);
  571. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
  572. lower_32_bits(ring->dma_base));
  573. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
  574. upper_32_bits(ring->dma_base));
  575. if (ring->unaligned)
  576. bgmac_dma_tx_enable(bgmac, ring);
  577. ring->start = 0;
  578. ring->end = 0; /* Points the slot that should *not* be read */
  579. }
  580. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
  581. int j;
  582. ring = &bgmac->rx_ring[i];
  583. if (!ring->unaligned)
  584. bgmac_dma_rx_enable(bgmac, ring);
  585. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
  586. lower_32_bits(ring->dma_base));
  587. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
  588. upper_32_bits(ring->dma_base));
  589. if (ring->unaligned)
  590. bgmac_dma_rx_enable(bgmac, ring);
  591. ring->start = 0;
  592. ring->end = 0;
  593. for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
  594. err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
  595. if (err)
  596. goto error;
  597. bgmac_dma_rx_setup_desc(bgmac, ring, j);
  598. }
  599. bgmac_dma_rx_update_index(bgmac, ring);
  600. }
  601. return 0;
  602. error:
  603. bgmac_dma_cleanup(bgmac);
  604. return err;
  605. }
  606. /**************************************************
  607. * Chip ops
  608. **************************************************/
  609. /* TODO: can we just drop @force? Can we don't reset MAC at all if there is
  610. * nothing to change? Try if after stabilizng driver.
  611. */
  612. static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
  613. bool force)
  614. {
  615. u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
  616. u32 new_val = (cmdcfg & mask) | set;
  617. u32 cmdcfg_sr;
  618. if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
  619. cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
  620. else
  621. cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
  622. bgmac_set(bgmac, BGMAC_CMDCFG, cmdcfg_sr);
  623. udelay(2);
  624. if (new_val != cmdcfg || force)
  625. bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
  626. bgmac_mask(bgmac, BGMAC_CMDCFG, ~cmdcfg_sr);
  627. udelay(2);
  628. }
  629. static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
  630. {
  631. u32 tmp;
  632. tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  633. bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
  634. tmp = (addr[4] << 8) | addr[5];
  635. bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
  636. }
  637. static void bgmac_set_rx_mode(struct net_device *net_dev)
  638. {
  639. struct bgmac *bgmac = netdev_priv(net_dev);
  640. if (net_dev->flags & IFF_PROMISC)
  641. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
  642. else
  643. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
  644. }
  645. #if 0 /* We don't use that regs yet */
  646. static void bgmac_chip_stats_update(struct bgmac *bgmac)
  647. {
  648. int i;
  649. if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) {
  650. for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
  651. bgmac->mib_tx_regs[i] =
  652. bgmac_read(bgmac,
  653. BGMAC_TX_GOOD_OCTETS + (i * 4));
  654. for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
  655. bgmac->mib_rx_regs[i] =
  656. bgmac_read(bgmac,
  657. BGMAC_RX_GOOD_OCTETS + (i * 4));
  658. }
  659. /* TODO: what else? how to handle BCM4706? Specs are needed */
  660. }
  661. #endif
  662. static void bgmac_clear_mib(struct bgmac *bgmac)
  663. {
  664. int i;
  665. if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)
  666. return;
  667. bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
  668. for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
  669. bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
  670. for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
  671. bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
  672. }
  673. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
  674. static void bgmac_mac_speed(struct bgmac *bgmac)
  675. {
  676. u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
  677. u32 set = 0;
  678. switch (bgmac->mac_speed) {
  679. case SPEED_10:
  680. set |= BGMAC_CMDCFG_ES_10;
  681. break;
  682. case SPEED_100:
  683. set |= BGMAC_CMDCFG_ES_100;
  684. break;
  685. case SPEED_1000:
  686. set |= BGMAC_CMDCFG_ES_1000;
  687. break;
  688. case SPEED_2500:
  689. set |= BGMAC_CMDCFG_ES_2500;
  690. break;
  691. default:
  692. dev_err(bgmac->dev, "Unsupported speed: %d\n",
  693. bgmac->mac_speed);
  694. }
  695. if (bgmac->mac_duplex == DUPLEX_HALF)
  696. set |= BGMAC_CMDCFG_HD;
  697. bgmac_cmdcfg_maskset(bgmac, mask, set, true);
  698. }
  699. static void bgmac_miiconfig(struct bgmac *bgmac)
  700. {
  701. if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) {
  702. bgmac_idm_write(bgmac, BCMA_IOCTL,
  703. bgmac_idm_read(bgmac, BCMA_IOCTL) | 0x40 |
  704. BGMAC_BCMA_IOCTL_SW_CLKEN);
  705. bgmac->mac_speed = SPEED_2500;
  706. bgmac->mac_duplex = DUPLEX_FULL;
  707. bgmac_mac_speed(bgmac);
  708. } else {
  709. u8 imode;
  710. imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
  711. BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
  712. if (imode == 0 || imode == 1) {
  713. bgmac->mac_speed = SPEED_100;
  714. bgmac->mac_duplex = DUPLEX_FULL;
  715. bgmac_mac_speed(bgmac);
  716. }
  717. }
  718. }
  719. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
  720. static void bgmac_chip_reset(struct bgmac *bgmac)
  721. {
  722. u32 cmdcfg_sr;
  723. u32 iost;
  724. int i;
  725. if (bgmac_clk_enabled(bgmac)) {
  726. if (!bgmac->stats_grabbed) {
  727. /* bgmac_chip_stats_update(bgmac); */
  728. bgmac->stats_grabbed = true;
  729. }
  730. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  731. bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
  732. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
  733. udelay(1);
  734. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  735. bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
  736. /* TODO: Clear software multicast filter list */
  737. }
  738. iost = bgmac_idm_read(bgmac, BCMA_IOST);
  739. if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED)
  740. iost &= ~BGMAC_BCMA_IOST_ATTACHED;
  741. /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
  742. if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) {
  743. u32 flags = 0;
  744. if (iost & BGMAC_BCMA_IOST_ATTACHED) {
  745. flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
  746. if (!bgmac->has_robosw)
  747. flags |= BGMAC_BCMA_IOCTL_SW_RESET;
  748. }
  749. bgmac_clk_enable(bgmac, flags);
  750. }
  751. /* Request Misc PLL for corerev > 2 */
  752. if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) {
  753. bgmac_set(bgmac, BCMA_CLKCTLST,
  754. BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
  755. bgmac_wait_value(bgmac, BCMA_CLKCTLST,
  756. BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
  757. BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
  758. 1000);
  759. }
  760. if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) {
  761. u8 et_swtype = 0;
  762. u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
  763. BGMAC_CHIPCTL_1_IF_TYPE_MII;
  764. char buf[4];
  765. if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
  766. if (kstrtou8(buf, 0, &et_swtype))
  767. dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
  768. buf);
  769. et_swtype &= 0x0f;
  770. et_swtype <<= 4;
  771. sw_type = et_swtype;
  772. } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) {
  773. sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII |
  774. BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
  775. } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) {
  776. sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
  777. BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
  778. }
  779. bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
  780. BGMAC_CHIPCTL_1_SW_TYPE_MASK),
  781. sw_type);
  782. } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) {
  783. u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII |
  784. BGMAC_CHIPCTL_4_SW_TYPE_EPHY;
  785. u8 et_swtype = 0;
  786. char buf[4];
  787. if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
  788. if (kstrtou8(buf, 0, &et_swtype))
  789. dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
  790. buf);
  791. sw_type = (et_swtype & 0x0f) << 12;
  792. } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) {
  793. sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII |
  794. BGMAC_CHIPCTL_4_SW_TYPE_RGMII;
  795. }
  796. bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK |
  797. BGMAC_CHIPCTL_4_SW_TYPE_MASK),
  798. sw_type);
  799. } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) {
  800. bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK,
  801. BGMAC_CHIPCTL_7_IF_TYPE_RGMII);
  802. }
  803. if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
  804. bgmac_idm_write(bgmac, BCMA_IOCTL,
  805. bgmac_idm_read(bgmac, BCMA_IOCTL) &
  806. ~BGMAC_BCMA_IOCTL_SW_RESET);
  807. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
  808. * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
  809. * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
  810. * be keps until taking MAC out of the reset.
  811. */
  812. if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
  813. cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
  814. else
  815. cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
  816. bgmac_cmdcfg_maskset(bgmac,
  817. ~(BGMAC_CMDCFG_TE |
  818. BGMAC_CMDCFG_RE |
  819. BGMAC_CMDCFG_RPI |
  820. BGMAC_CMDCFG_TAI |
  821. BGMAC_CMDCFG_HD |
  822. BGMAC_CMDCFG_ML |
  823. BGMAC_CMDCFG_CFE |
  824. BGMAC_CMDCFG_RL |
  825. BGMAC_CMDCFG_RED |
  826. BGMAC_CMDCFG_PE |
  827. BGMAC_CMDCFG_TPI |
  828. BGMAC_CMDCFG_PAD_EN |
  829. BGMAC_CMDCFG_PF),
  830. BGMAC_CMDCFG_PROM |
  831. BGMAC_CMDCFG_NLC |
  832. BGMAC_CMDCFG_CFE |
  833. cmdcfg_sr,
  834. false);
  835. bgmac->mac_speed = SPEED_UNKNOWN;
  836. bgmac->mac_duplex = DUPLEX_UNKNOWN;
  837. bgmac_clear_mib(bgmac);
  838. if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL)
  839. bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0,
  840. BCMA_GMAC_CMN_PC_MTE);
  841. else
  842. bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
  843. bgmac_miiconfig(bgmac);
  844. if (bgmac->mii_bus)
  845. bgmac->mii_bus->reset(bgmac->mii_bus);
  846. netdev_reset_queue(bgmac->net_dev);
  847. }
  848. static void bgmac_chip_intrs_on(struct bgmac *bgmac)
  849. {
  850. bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
  851. }
  852. static void bgmac_chip_intrs_off(struct bgmac *bgmac)
  853. {
  854. bgmac_write(bgmac, BGMAC_INT_MASK, 0);
  855. bgmac_read(bgmac, BGMAC_INT_MASK);
  856. }
  857. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
  858. static void bgmac_enable(struct bgmac *bgmac)
  859. {
  860. u32 cmdcfg_sr;
  861. u32 cmdcfg;
  862. u32 mode;
  863. if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
  864. cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
  865. else
  866. cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
  867. cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
  868. bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
  869. cmdcfg_sr, true);
  870. udelay(2);
  871. cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
  872. bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
  873. mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
  874. BGMAC_DS_MM_SHIFT;
  875. if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0)
  876. bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
  877. if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2)
  878. bgmac_cco_ctl_maskset(bgmac, 1, ~0,
  879. BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
  880. if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 |
  881. BGMAC_FEAT_FLW_CTRL2)) {
  882. u32 fl_ctl;
  883. if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1)
  884. fl_ctl = 0x2300e1;
  885. else
  886. fl_ctl = 0x03cb04cb;
  887. bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
  888. bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
  889. }
  890. if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) {
  891. u32 rxq_ctl;
  892. u16 bp_clk;
  893. u8 mdp;
  894. rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
  895. rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
  896. bp_clk = bgmac_get_bus_clock(bgmac) / 1000000;
  897. mdp = (bp_clk * 128 / 1000) - 3;
  898. rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
  899. bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
  900. }
  901. }
  902. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
  903. static void bgmac_chip_init(struct bgmac *bgmac)
  904. {
  905. /* 1 interrupt per received frame */
  906. bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
  907. /* Enable 802.3x tx flow control (honor received PAUSE frames) */
  908. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
  909. bgmac_set_rx_mode(bgmac->net_dev);
  910. bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
  911. if (bgmac->loopback)
  912. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
  913. else
  914. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
  915. bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
  916. bgmac_chip_intrs_on(bgmac);
  917. bgmac_enable(bgmac);
  918. }
  919. static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
  920. {
  921. struct bgmac *bgmac = netdev_priv(dev_id);
  922. u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
  923. int_status &= bgmac->int_mask;
  924. if (!int_status)
  925. return IRQ_NONE;
  926. int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
  927. if (int_status)
  928. dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status);
  929. /* Disable new interrupts until handling existing ones */
  930. bgmac_chip_intrs_off(bgmac);
  931. napi_schedule(&bgmac->napi);
  932. return IRQ_HANDLED;
  933. }
  934. static int bgmac_poll(struct napi_struct *napi, int weight)
  935. {
  936. struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
  937. int handled = 0;
  938. /* Ack */
  939. bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
  940. bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
  941. handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
  942. /* Poll again if more events arrived in the meantime */
  943. if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
  944. return weight;
  945. if (handled < weight) {
  946. napi_complete(napi);
  947. bgmac_chip_intrs_on(bgmac);
  948. }
  949. return handled;
  950. }
  951. /**************************************************
  952. * net_device_ops
  953. **************************************************/
  954. static int bgmac_open(struct net_device *net_dev)
  955. {
  956. struct bgmac *bgmac = netdev_priv(net_dev);
  957. int err = 0;
  958. bgmac_chip_reset(bgmac);
  959. err = bgmac_dma_init(bgmac);
  960. if (err)
  961. return err;
  962. /* Specs say about reclaiming rings here, but we do that in DMA init */
  963. bgmac_chip_init(bgmac);
  964. err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED,
  965. KBUILD_MODNAME, net_dev);
  966. if (err < 0) {
  967. dev_err(bgmac->dev, "IRQ request error: %d!\n", err);
  968. bgmac_dma_cleanup(bgmac);
  969. return err;
  970. }
  971. napi_enable(&bgmac->napi);
  972. phy_start(net_dev->phydev);
  973. netif_start_queue(net_dev);
  974. return 0;
  975. }
  976. static int bgmac_stop(struct net_device *net_dev)
  977. {
  978. struct bgmac *bgmac = netdev_priv(net_dev);
  979. netif_carrier_off(net_dev);
  980. phy_stop(net_dev->phydev);
  981. napi_disable(&bgmac->napi);
  982. bgmac_chip_intrs_off(bgmac);
  983. free_irq(bgmac->irq, net_dev);
  984. bgmac_chip_reset(bgmac);
  985. bgmac_dma_cleanup(bgmac);
  986. return 0;
  987. }
  988. static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
  989. struct net_device *net_dev)
  990. {
  991. struct bgmac *bgmac = netdev_priv(net_dev);
  992. struct bgmac_dma_ring *ring;
  993. /* No QOS support yet */
  994. ring = &bgmac->tx_ring[0];
  995. return bgmac_dma_tx_add(bgmac, ring, skb);
  996. }
  997. static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
  998. {
  999. struct bgmac *bgmac = netdev_priv(net_dev);
  1000. int ret;
  1001. ret = eth_prepare_mac_addr_change(net_dev, addr);
  1002. if (ret < 0)
  1003. return ret;
  1004. bgmac_write_mac_address(bgmac, (u8 *)addr);
  1005. eth_commit_mac_addr_change(net_dev, addr);
  1006. return 0;
  1007. }
  1008. static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1009. {
  1010. if (!netif_running(net_dev))
  1011. return -EINVAL;
  1012. return phy_mii_ioctl(net_dev->phydev, ifr, cmd);
  1013. }
  1014. static const struct net_device_ops bgmac_netdev_ops = {
  1015. .ndo_open = bgmac_open,
  1016. .ndo_stop = bgmac_stop,
  1017. .ndo_start_xmit = bgmac_start_xmit,
  1018. .ndo_set_rx_mode = bgmac_set_rx_mode,
  1019. .ndo_set_mac_address = bgmac_set_mac_address,
  1020. .ndo_validate_addr = eth_validate_addr,
  1021. .ndo_do_ioctl = bgmac_ioctl,
  1022. };
  1023. /**************************************************
  1024. * ethtool_ops
  1025. **************************************************/
  1026. struct bgmac_stat {
  1027. u8 size;
  1028. u32 offset;
  1029. const char *name;
  1030. };
  1031. static struct bgmac_stat bgmac_get_strings_stats[] = {
  1032. { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
  1033. { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
  1034. { 8, BGMAC_TX_OCTETS, "tx_octets" },
  1035. { 4, BGMAC_TX_PKTS, "tx_pkts" },
  1036. { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
  1037. { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
  1038. { 4, BGMAC_TX_LEN_64, "tx_64" },
  1039. { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
  1040. { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
  1041. { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
  1042. { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
  1043. { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
  1044. { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
  1045. { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
  1046. { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
  1047. { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
  1048. { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
  1049. { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
  1050. { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
  1051. { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
  1052. { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
  1053. { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
  1054. { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
  1055. { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
  1056. { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
  1057. { 4, BGMAC_TX_DEFERED, "tx_defered" },
  1058. { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
  1059. { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
  1060. { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
  1061. { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
  1062. { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
  1063. { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
  1064. { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
  1065. { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
  1066. { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
  1067. { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
  1068. { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
  1069. { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
  1070. { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
  1071. { 8, BGMAC_RX_OCTETS, "rx_octets" },
  1072. { 4, BGMAC_RX_PKTS, "rx_pkts" },
  1073. { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
  1074. { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
  1075. { 4, BGMAC_RX_LEN_64, "rx_64" },
  1076. { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
  1077. { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
  1078. { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
  1079. { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
  1080. { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
  1081. { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
  1082. { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
  1083. { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
  1084. { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
  1085. { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
  1086. { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
  1087. { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
  1088. { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
  1089. { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
  1090. { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
  1091. { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
  1092. { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
  1093. { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
  1094. { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
  1095. { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
  1096. { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
  1097. { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
  1098. };
  1099. #define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
  1100. static int bgmac_get_sset_count(struct net_device *dev, int string_set)
  1101. {
  1102. switch (string_set) {
  1103. case ETH_SS_STATS:
  1104. return BGMAC_STATS_LEN;
  1105. }
  1106. return -EOPNOTSUPP;
  1107. }
  1108. static void bgmac_get_strings(struct net_device *dev, u32 stringset,
  1109. u8 *data)
  1110. {
  1111. int i;
  1112. if (stringset != ETH_SS_STATS)
  1113. return;
  1114. for (i = 0; i < BGMAC_STATS_LEN; i++)
  1115. strlcpy(data + i * ETH_GSTRING_LEN,
  1116. bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
  1117. }
  1118. static void bgmac_get_ethtool_stats(struct net_device *dev,
  1119. struct ethtool_stats *ss, uint64_t *data)
  1120. {
  1121. struct bgmac *bgmac = netdev_priv(dev);
  1122. const struct bgmac_stat *s;
  1123. unsigned int i;
  1124. u64 val;
  1125. if (!netif_running(dev))
  1126. return;
  1127. for (i = 0; i < BGMAC_STATS_LEN; i++) {
  1128. s = &bgmac_get_strings_stats[i];
  1129. val = 0;
  1130. if (s->size == 8)
  1131. val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
  1132. val |= bgmac_read(bgmac, s->offset);
  1133. data[i] = val;
  1134. }
  1135. }
  1136. static void bgmac_get_drvinfo(struct net_device *net_dev,
  1137. struct ethtool_drvinfo *info)
  1138. {
  1139. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1140. strlcpy(info->bus_info, "AXI", sizeof(info->bus_info));
  1141. }
  1142. static const struct ethtool_ops bgmac_ethtool_ops = {
  1143. .get_strings = bgmac_get_strings,
  1144. .get_sset_count = bgmac_get_sset_count,
  1145. .get_ethtool_stats = bgmac_get_ethtool_stats,
  1146. .get_drvinfo = bgmac_get_drvinfo,
  1147. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1148. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1149. };
  1150. /**************************************************
  1151. * MII
  1152. **************************************************/
  1153. static void bgmac_adjust_link(struct net_device *net_dev)
  1154. {
  1155. struct bgmac *bgmac = netdev_priv(net_dev);
  1156. struct phy_device *phy_dev = net_dev->phydev;
  1157. bool update = false;
  1158. if (phy_dev->link) {
  1159. if (phy_dev->speed != bgmac->mac_speed) {
  1160. bgmac->mac_speed = phy_dev->speed;
  1161. update = true;
  1162. }
  1163. if (phy_dev->duplex != bgmac->mac_duplex) {
  1164. bgmac->mac_duplex = phy_dev->duplex;
  1165. update = true;
  1166. }
  1167. }
  1168. if (update) {
  1169. bgmac_mac_speed(bgmac);
  1170. phy_print_status(phy_dev);
  1171. }
  1172. }
  1173. static int bgmac_phy_connect_direct(struct bgmac *bgmac)
  1174. {
  1175. struct fixed_phy_status fphy_status = {
  1176. .link = 1,
  1177. .speed = SPEED_1000,
  1178. .duplex = DUPLEX_FULL,
  1179. };
  1180. struct phy_device *phy_dev;
  1181. int err;
  1182. phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
  1183. if (!phy_dev || IS_ERR(phy_dev)) {
  1184. dev_err(bgmac->dev, "Failed to register fixed PHY device\n");
  1185. return -ENODEV;
  1186. }
  1187. err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
  1188. PHY_INTERFACE_MODE_MII);
  1189. if (err) {
  1190. dev_err(bgmac->dev, "Connecting PHY failed\n");
  1191. return err;
  1192. }
  1193. return err;
  1194. }
  1195. static int bgmac_phy_connect(struct bgmac *bgmac)
  1196. {
  1197. struct phy_device *phy_dev;
  1198. char bus_id[MII_BUS_ID_SIZE + 3];
  1199. /* Connect to the PHY */
  1200. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, bgmac->mii_bus->id,
  1201. bgmac->phyaddr);
  1202. phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
  1203. PHY_INTERFACE_MODE_MII);
  1204. if (IS_ERR(phy_dev)) {
  1205. dev_err(bgmac->dev, "PHY connection failed\n");
  1206. return PTR_ERR(phy_dev);
  1207. }
  1208. return 0;
  1209. }
  1210. int bgmac_enet_probe(struct bgmac *info)
  1211. {
  1212. struct net_device *net_dev;
  1213. struct bgmac *bgmac;
  1214. int err;
  1215. /* Allocation and references */
  1216. net_dev = alloc_etherdev(sizeof(*bgmac));
  1217. if (!net_dev)
  1218. return -ENOMEM;
  1219. net_dev->netdev_ops = &bgmac_netdev_ops;
  1220. net_dev->ethtool_ops = &bgmac_ethtool_ops;
  1221. bgmac = netdev_priv(net_dev);
  1222. memcpy(bgmac, info, sizeof(*bgmac));
  1223. bgmac->net_dev = net_dev;
  1224. net_dev->irq = bgmac->irq;
  1225. SET_NETDEV_DEV(net_dev, bgmac->dev);
  1226. if (!is_valid_ether_addr(bgmac->mac_addr)) {
  1227. dev_err(bgmac->dev, "Invalid MAC addr: %pM\n",
  1228. bgmac->mac_addr);
  1229. eth_random_addr(bgmac->mac_addr);
  1230. dev_warn(bgmac->dev, "Using random MAC: %pM\n",
  1231. bgmac->mac_addr);
  1232. }
  1233. ether_addr_copy(net_dev->dev_addr, bgmac->mac_addr);
  1234. /* This (reset &) enable is not preset in specs or reference driver but
  1235. * Broadcom does it in arch PCI code when enabling fake PCI device.
  1236. */
  1237. bgmac_clk_enable(bgmac, 0);
  1238. /* This seems to be fixing IRQ by assigning OOB #6 to the core */
  1239. if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6)
  1240. bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86);
  1241. bgmac_chip_reset(bgmac);
  1242. err = bgmac_dma_alloc(bgmac);
  1243. if (err) {
  1244. dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
  1245. goto err_netdev_free;
  1246. }
  1247. bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
  1248. if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
  1249. bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
  1250. netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
  1251. if (!bgmac->mii_bus)
  1252. err = bgmac_phy_connect_direct(bgmac);
  1253. else
  1254. err = bgmac_phy_connect(bgmac);
  1255. if (err) {
  1256. dev_err(bgmac->dev, "Cannot connect to phy\n");
  1257. goto err_dma_free;
  1258. }
  1259. net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1260. net_dev->hw_features = net_dev->features;
  1261. net_dev->vlan_features = net_dev->features;
  1262. err = register_netdev(bgmac->net_dev);
  1263. if (err) {
  1264. dev_err(bgmac->dev, "Cannot register net device\n");
  1265. goto err_phy_disconnect;
  1266. }
  1267. netif_carrier_off(net_dev);
  1268. return 0;
  1269. err_phy_disconnect:
  1270. phy_disconnect(net_dev->phydev);
  1271. err_dma_free:
  1272. bgmac_dma_free(bgmac);
  1273. err_netdev_free:
  1274. free_netdev(net_dev);
  1275. return err;
  1276. }
  1277. EXPORT_SYMBOL_GPL(bgmac_enet_probe);
  1278. void bgmac_enet_remove(struct bgmac *bgmac)
  1279. {
  1280. unregister_netdev(bgmac->net_dev);
  1281. phy_disconnect(bgmac->net_dev->phydev);
  1282. netif_napi_del(&bgmac->napi);
  1283. bgmac_dma_free(bgmac);
  1284. free_netdev(bgmac->net_dev);
  1285. }
  1286. EXPORT_SYMBOL_GPL(bgmac_enet_remove);
  1287. MODULE_AUTHOR("Rafał Miłecki");
  1288. MODULE_LICENSE("GPL");