octeon_mgmt.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2009-2012 Cavium, Inc
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/capability.h>
  12. #include <linux/net_tstamp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/module.h>
  19. #include <linux/of_net.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/phy.h>
  23. #include <linux/io.h>
  24. #include <asm/octeon/octeon.h>
  25. #include <asm/octeon/cvmx-mixx-defs.h>
  26. #include <asm/octeon/cvmx-agl-defs.h>
  27. #define DRV_NAME "octeon_mgmt"
  28. #define DRV_VERSION "2.0"
  29. #define DRV_DESCRIPTION \
  30. "Cavium Networks Octeon MII (management) port Network Driver"
  31. #define OCTEON_MGMT_NAPI_WEIGHT 16
  32. /* Ring sizes that are powers of two allow for more efficient modulo
  33. * opertions.
  34. */
  35. #define OCTEON_MGMT_RX_RING_SIZE 512
  36. #define OCTEON_MGMT_TX_RING_SIZE 128
  37. /* Allow 8 bytes for vlan and FCS. */
  38. #define OCTEON_MGMT_RX_HEADROOM (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN)
  39. union mgmt_port_ring_entry {
  40. u64 d64;
  41. struct {
  42. #define RING_ENTRY_CODE_DONE 0xf
  43. #define RING_ENTRY_CODE_MORE 0x10
  44. #ifdef __BIG_ENDIAN_BITFIELD
  45. u64 reserved_62_63:2;
  46. /* Length of the buffer/packet in bytes */
  47. u64 len:14;
  48. /* For TX, signals that the packet should be timestamped */
  49. u64 tstamp:1;
  50. /* The RX error code */
  51. u64 code:7;
  52. /* Physical address of the buffer */
  53. u64 addr:40;
  54. #else
  55. u64 addr:40;
  56. u64 code:7;
  57. u64 tstamp:1;
  58. u64 len:14;
  59. u64 reserved_62_63:2;
  60. #endif
  61. } s;
  62. };
  63. #define MIX_ORING1 0x0
  64. #define MIX_ORING2 0x8
  65. #define MIX_IRING1 0x10
  66. #define MIX_IRING2 0x18
  67. #define MIX_CTL 0x20
  68. #define MIX_IRHWM 0x28
  69. #define MIX_IRCNT 0x30
  70. #define MIX_ORHWM 0x38
  71. #define MIX_ORCNT 0x40
  72. #define MIX_ISR 0x48
  73. #define MIX_INTENA 0x50
  74. #define MIX_REMCNT 0x58
  75. #define MIX_BIST 0x78
  76. #define AGL_GMX_PRT_CFG 0x10
  77. #define AGL_GMX_RX_FRM_CTL 0x18
  78. #define AGL_GMX_RX_FRM_MAX 0x30
  79. #define AGL_GMX_RX_JABBER 0x38
  80. #define AGL_GMX_RX_STATS_CTL 0x50
  81. #define AGL_GMX_RX_STATS_PKTS_DRP 0xb0
  82. #define AGL_GMX_RX_STATS_OCTS_DRP 0xb8
  83. #define AGL_GMX_RX_STATS_PKTS_BAD 0xc0
  84. #define AGL_GMX_RX_ADR_CTL 0x100
  85. #define AGL_GMX_RX_ADR_CAM_EN 0x108
  86. #define AGL_GMX_RX_ADR_CAM0 0x180
  87. #define AGL_GMX_RX_ADR_CAM1 0x188
  88. #define AGL_GMX_RX_ADR_CAM2 0x190
  89. #define AGL_GMX_RX_ADR_CAM3 0x198
  90. #define AGL_GMX_RX_ADR_CAM4 0x1a0
  91. #define AGL_GMX_RX_ADR_CAM5 0x1a8
  92. #define AGL_GMX_TX_CLK 0x208
  93. #define AGL_GMX_TX_STATS_CTL 0x268
  94. #define AGL_GMX_TX_CTL 0x270
  95. #define AGL_GMX_TX_STAT0 0x280
  96. #define AGL_GMX_TX_STAT1 0x288
  97. #define AGL_GMX_TX_STAT2 0x290
  98. #define AGL_GMX_TX_STAT3 0x298
  99. #define AGL_GMX_TX_STAT4 0x2a0
  100. #define AGL_GMX_TX_STAT5 0x2a8
  101. #define AGL_GMX_TX_STAT6 0x2b0
  102. #define AGL_GMX_TX_STAT7 0x2b8
  103. #define AGL_GMX_TX_STAT8 0x2c0
  104. #define AGL_GMX_TX_STAT9 0x2c8
  105. struct octeon_mgmt {
  106. struct net_device *netdev;
  107. u64 mix;
  108. u64 agl;
  109. u64 agl_prt_ctl;
  110. int port;
  111. int irq;
  112. bool has_rx_tstamp;
  113. u64 *tx_ring;
  114. dma_addr_t tx_ring_handle;
  115. unsigned int tx_next;
  116. unsigned int tx_next_clean;
  117. unsigned int tx_current_fill;
  118. /* The tx_list lock also protects the ring related variables */
  119. struct sk_buff_head tx_list;
  120. /* RX variables only touched in napi_poll. No locking necessary. */
  121. u64 *rx_ring;
  122. dma_addr_t rx_ring_handle;
  123. unsigned int rx_next;
  124. unsigned int rx_next_fill;
  125. unsigned int rx_current_fill;
  126. struct sk_buff_head rx_list;
  127. spinlock_t lock;
  128. unsigned int last_duplex;
  129. unsigned int last_link;
  130. unsigned int last_speed;
  131. struct device *dev;
  132. struct napi_struct napi;
  133. struct tasklet_struct tx_clean_tasklet;
  134. struct phy_device *phydev;
  135. struct device_node *phy_np;
  136. resource_size_t mix_phys;
  137. resource_size_t mix_size;
  138. resource_size_t agl_phys;
  139. resource_size_t agl_size;
  140. resource_size_t agl_prt_ctl_phys;
  141. resource_size_t agl_prt_ctl_size;
  142. };
  143. static void octeon_mgmt_set_rx_irq(struct octeon_mgmt *p, int enable)
  144. {
  145. union cvmx_mixx_intena mix_intena;
  146. unsigned long flags;
  147. spin_lock_irqsave(&p->lock, flags);
  148. mix_intena.u64 = cvmx_read_csr(p->mix + MIX_INTENA);
  149. mix_intena.s.ithena = enable ? 1 : 0;
  150. cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
  151. spin_unlock_irqrestore(&p->lock, flags);
  152. }
  153. static void octeon_mgmt_set_tx_irq(struct octeon_mgmt *p, int enable)
  154. {
  155. union cvmx_mixx_intena mix_intena;
  156. unsigned long flags;
  157. spin_lock_irqsave(&p->lock, flags);
  158. mix_intena.u64 = cvmx_read_csr(p->mix + MIX_INTENA);
  159. mix_intena.s.othena = enable ? 1 : 0;
  160. cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
  161. spin_unlock_irqrestore(&p->lock, flags);
  162. }
  163. static void octeon_mgmt_enable_rx_irq(struct octeon_mgmt *p)
  164. {
  165. octeon_mgmt_set_rx_irq(p, 1);
  166. }
  167. static void octeon_mgmt_disable_rx_irq(struct octeon_mgmt *p)
  168. {
  169. octeon_mgmt_set_rx_irq(p, 0);
  170. }
  171. static void octeon_mgmt_enable_tx_irq(struct octeon_mgmt *p)
  172. {
  173. octeon_mgmt_set_tx_irq(p, 1);
  174. }
  175. static void octeon_mgmt_disable_tx_irq(struct octeon_mgmt *p)
  176. {
  177. octeon_mgmt_set_tx_irq(p, 0);
  178. }
  179. static unsigned int ring_max_fill(unsigned int ring_size)
  180. {
  181. return ring_size - 8;
  182. }
  183. static unsigned int ring_size_to_bytes(unsigned int ring_size)
  184. {
  185. return ring_size * sizeof(union mgmt_port_ring_entry);
  186. }
  187. static void octeon_mgmt_rx_fill_ring(struct net_device *netdev)
  188. {
  189. struct octeon_mgmt *p = netdev_priv(netdev);
  190. while (p->rx_current_fill < ring_max_fill(OCTEON_MGMT_RX_RING_SIZE)) {
  191. unsigned int size;
  192. union mgmt_port_ring_entry re;
  193. struct sk_buff *skb;
  194. /* CN56XX pass 1 needs 8 bytes of padding. */
  195. size = netdev->mtu + OCTEON_MGMT_RX_HEADROOM + 8 + NET_IP_ALIGN;
  196. skb = netdev_alloc_skb(netdev, size);
  197. if (!skb)
  198. break;
  199. skb_reserve(skb, NET_IP_ALIGN);
  200. __skb_queue_tail(&p->rx_list, skb);
  201. re.d64 = 0;
  202. re.s.len = size;
  203. re.s.addr = dma_map_single(p->dev, skb->data,
  204. size,
  205. DMA_FROM_DEVICE);
  206. /* Put it in the ring. */
  207. p->rx_ring[p->rx_next_fill] = re.d64;
  208. dma_sync_single_for_device(p->dev, p->rx_ring_handle,
  209. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  210. DMA_BIDIRECTIONAL);
  211. p->rx_next_fill =
  212. (p->rx_next_fill + 1) % OCTEON_MGMT_RX_RING_SIZE;
  213. p->rx_current_fill++;
  214. /* Ring the bell. */
  215. cvmx_write_csr(p->mix + MIX_IRING2, 1);
  216. }
  217. }
  218. static ktime_t ptp_to_ktime(u64 ptptime)
  219. {
  220. ktime_t ktimebase;
  221. u64 ptpbase;
  222. unsigned long flags;
  223. local_irq_save(flags);
  224. /* Fill the icache with the code */
  225. ktime_get_real();
  226. /* Flush all pending operations */
  227. mb();
  228. /* Read the time and PTP clock as close together as
  229. * possible. It is important that this sequence take the same
  230. * amount of time to reduce jitter
  231. */
  232. ktimebase = ktime_get_real();
  233. ptpbase = cvmx_read_csr(CVMX_MIO_PTP_CLOCK_HI);
  234. local_irq_restore(flags);
  235. return ktime_sub_ns(ktimebase, ptpbase - ptptime);
  236. }
  237. static void octeon_mgmt_clean_tx_buffers(struct octeon_mgmt *p)
  238. {
  239. union cvmx_mixx_orcnt mix_orcnt;
  240. union mgmt_port_ring_entry re;
  241. struct sk_buff *skb;
  242. int cleaned = 0;
  243. unsigned long flags;
  244. mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT);
  245. while (mix_orcnt.s.orcnt) {
  246. spin_lock_irqsave(&p->tx_list.lock, flags);
  247. mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT);
  248. if (mix_orcnt.s.orcnt == 0) {
  249. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  250. break;
  251. }
  252. dma_sync_single_for_cpu(p->dev, p->tx_ring_handle,
  253. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  254. DMA_BIDIRECTIONAL);
  255. re.d64 = p->tx_ring[p->tx_next_clean];
  256. p->tx_next_clean =
  257. (p->tx_next_clean + 1) % OCTEON_MGMT_TX_RING_SIZE;
  258. skb = __skb_dequeue(&p->tx_list);
  259. mix_orcnt.u64 = 0;
  260. mix_orcnt.s.orcnt = 1;
  261. /* Acknowledge to hardware that we have the buffer. */
  262. cvmx_write_csr(p->mix + MIX_ORCNT, mix_orcnt.u64);
  263. p->tx_current_fill--;
  264. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  265. dma_unmap_single(p->dev, re.s.addr, re.s.len,
  266. DMA_TO_DEVICE);
  267. /* Read the hardware TX timestamp if one was recorded */
  268. if (unlikely(re.s.tstamp)) {
  269. struct skb_shared_hwtstamps ts;
  270. /* Read the timestamp */
  271. u64 ns = cvmx_read_csr(CVMX_MIXX_TSTAMP(p->port));
  272. /* Remove the timestamp from the FIFO */
  273. cvmx_write_csr(CVMX_MIXX_TSCTL(p->port), 0);
  274. /* Tell the kernel about the timestamp */
  275. ts.syststamp = ptp_to_ktime(ns);
  276. ts.hwtstamp = ns_to_ktime(ns);
  277. skb_tstamp_tx(skb, &ts);
  278. }
  279. dev_kfree_skb_any(skb);
  280. cleaned++;
  281. mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT);
  282. }
  283. if (cleaned && netif_queue_stopped(p->netdev))
  284. netif_wake_queue(p->netdev);
  285. }
  286. static void octeon_mgmt_clean_tx_tasklet(unsigned long arg)
  287. {
  288. struct octeon_mgmt *p = (struct octeon_mgmt *)arg;
  289. octeon_mgmt_clean_tx_buffers(p);
  290. octeon_mgmt_enable_tx_irq(p);
  291. }
  292. static void octeon_mgmt_update_rx_stats(struct net_device *netdev)
  293. {
  294. struct octeon_mgmt *p = netdev_priv(netdev);
  295. unsigned long flags;
  296. u64 drop, bad;
  297. /* These reads also clear the count registers. */
  298. drop = cvmx_read_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP);
  299. bad = cvmx_read_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD);
  300. if (drop || bad) {
  301. /* Do an atomic update. */
  302. spin_lock_irqsave(&p->lock, flags);
  303. netdev->stats.rx_errors += bad;
  304. netdev->stats.rx_dropped += drop;
  305. spin_unlock_irqrestore(&p->lock, flags);
  306. }
  307. }
  308. static void octeon_mgmt_update_tx_stats(struct net_device *netdev)
  309. {
  310. struct octeon_mgmt *p = netdev_priv(netdev);
  311. unsigned long flags;
  312. union cvmx_agl_gmx_txx_stat0 s0;
  313. union cvmx_agl_gmx_txx_stat1 s1;
  314. /* These reads also clear the count registers. */
  315. s0.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_STAT0);
  316. s1.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_STAT1);
  317. if (s0.s.xsdef || s0.s.xscol || s1.s.scol || s1.s.mcol) {
  318. /* Do an atomic update. */
  319. spin_lock_irqsave(&p->lock, flags);
  320. netdev->stats.tx_errors += s0.s.xsdef + s0.s.xscol;
  321. netdev->stats.collisions += s1.s.scol + s1.s.mcol;
  322. spin_unlock_irqrestore(&p->lock, flags);
  323. }
  324. }
  325. /*
  326. * Dequeue a receive skb and its corresponding ring entry. The ring
  327. * entry is returned, *pskb is updated to point to the skb.
  328. */
  329. static u64 octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt *p,
  330. struct sk_buff **pskb)
  331. {
  332. union mgmt_port_ring_entry re;
  333. dma_sync_single_for_cpu(p->dev, p->rx_ring_handle,
  334. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  335. DMA_BIDIRECTIONAL);
  336. re.d64 = p->rx_ring[p->rx_next];
  337. p->rx_next = (p->rx_next + 1) % OCTEON_MGMT_RX_RING_SIZE;
  338. p->rx_current_fill--;
  339. *pskb = __skb_dequeue(&p->rx_list);
  340. dma_unmap_single(p->dev, re.s.addr,
  341. ETH_FRAME_LEN + OCTEON_MGMT_RX_HEADROOM,
  342. DMA_FROM_DEVICE);
  343. return re.d64;
  344. }
  345. static int octeon_mgmt_receive_one(struct octeon_mgmt *p)
  346. {
  347. struct net_device *netdev = p->netdev;
  348. union cvmx_mixx_ircnt mix_ircnt;
  349. union mgmt_port_ring_entry re;
  350. struct sk_buff *skb;
  351. struct sk_buff *skb2;
  352. struct sk_buff *skb_new;
  353. union mgmt_port_ring_entry re2;
  354. int rc = 1;
  355. re.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb);
  356. if (likely(re.s.code == RING_ENTRY_CODE_DONE)) {
  357. /* A good packet, send it up. */
  358. skb_put(skb, re.s.len);
  359. good:
  360. /* Process the RX timestamp if it was recorded */
  361. if (p->has_rx_tstamp) {
  362. /* The first 8 bytes are the timestamp */
  363. u64 ns = *(u64 *)skb->data;
  364. struct skb_shared_hwtstamps *ts;
  365. ts = skb_hwtstamps(skb);
  366. ts->hwtstamp = ns_to_ktime(ns);
  367. ts->syststamp = ptp_to_ktime(ns);
  368. __skb_pull(skb, 8);
  369. }
  370. skb->protocol = eth_type_trans(skb, netdev);
  371. netdev->stats.rx_packets++;
  372. netdev->stats.rx_bytes += skb->len;
  373. netif_receive_skb(skb);
  374. rc = 0;
  375. } else if (re.s.code == RING_ENTRY_CODE_MORE) {
  376. /* Packet split across skbs. This can happen if we
  377. * increase the MTU. Buffers that are already in the
  378. * rx ring can then end up being too small. As the rx
  379. * ring is refilled, buffers sized for the new MTU
  380. * will be used and we should go back to the normal
  381. * non-split case.
  382. */
  383. skb_put(skb, re.s.len);
  384. do {
  385. re2.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb2);
  386. if (re2.s.code != RING_ENTRY_CODE_MORE
  387. && re2.s.code != RING_ENTRY_CODE_DONE)
  388. goto split_error;
  389. skb_put(skb2, re2.s.len);
  390. skb_new = skb_copy_expand(skb, 0, skb2->len,
  391. GFP_ATOMIC);
  392. if (!skb_new)
  393. goto split_error;
  394. if (skb_copy_bits(skb2, 0, skb_tail_pointer(skb_new),
  395. skb2->len))
  396. goto split_error;
  397. skb_put(skb_new, skb2->len);
  398. dev_kfree_skb_any(skb);
  399. dev_kfree_skb_any(skb2);
  400. skb = skb_new;
  401. } while (re2.s.code == RING_ENTRY_CODE_MORE);
  402. goto good;
  403. } else {
  404. /* Some other error, discard it. */
  405. dev_kfree_skb_any(skb);
  406. /* Error statistics are accumulated in
  407. * octeon_mgmt_update_rx_stats.
  408. */
  409. }
  410. goto done;
  411. split_error:
  412. /* Discard the whole mess. */
  413. dev_kfree_skb_any(skb);
  414. dev_kfree_skb_any(skb2);
  415. while (re2.s.code == RING_ENTRY_CODE_MORE) {
  416. re2.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb2);
  417. dev_kfree_skb_any(skb2);
  418. }
  419. netdev->stats.rx_errors++;
  420. done:
  421. /* Tell the hardware we processed a packet. */
  422. mix_ircnt.u64 = 0;
  423. mix_ircnt.s.ircnt = 1;
  424. cvmx_write_csr(p->mix + MIX_IRCNT, mix_ircnt.u64);
  425. return rc;
  426. }
  427. static int octeon_mgmt_receive_packets(struct octeon_mgmt *p, int budget)
  428. {
  429. unsigned int work_done = 0;
  430. union cvmx_mixx_ircnt mix_ircnt;
  431. int rc;
  432. mix_ircnt.u64 = cvmx_read_csr(p->mix + MIX_IRCNT);
  433. while (work_done < budget && mix_ircnt.s.ircnt) {
  434. rc = octeon_mgmt_receive_one(p);
  435. if (!rc)
  436. work_done++;
  437. /* Check for more packets. */
  438. mix_ircnt.u64 = cvmx_read_csr(p->mix + MIX_IRCNT);
  439. }
  440. octeon_mgmt_rx_fill_ring(p->netdev);
  441. return work_done;
  442. }
  443. static int octeon_mgmt_napi_poll(struct napi_struct *napi, int budget)
  444. {
  445. struct octeon_mgmt *p = container_of(napi, struct octeon_mgmt, napi);
  446. struct net_device *netdev = p->netdev;
  447. unsigned int work_done = 0;
  448. work_done = octeon_mgmt_receive_packets(p, budget);
  449. if (work_done < budget) {
  450. /* We stopped because no more packets were available. */
  451. napi_complete(napi);
  452. octeon_mgmt_enable_rx_irq(p);
  453. }
  454. octeon_mgmt_update_rx_stats(netdev);
  455. return work_done;
  456. }
  457. /* Reset the hardware to clean state. */
  458. static void octeon_mgmt_reset_hw(struct octeon_mgmt *p)
  459. {
  460. union cvmx_mixx_ctl mix_ctl;
  461. union cvmx_mixx_bist mix_bist;
  462. union cvmx_agl_gmx_bist agl_gmx_bist;
  463. mix_ctl.u64 = 0;
  464. cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
  465. do {
  466. mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
  467. } while (mix_ctl.s.busy);
  468. mix_ctl.s.reset = 1;
  469. cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
  470. cvmx_read_csr(p->mix + MIX_CTL);
  471. octeon_io_clk_delay(64);
  472. mix_bist.u64 = cvmx_read_csr(p->mix + MIX_BIST);
  473. if (mix_bist.u64)
  474. dev_warn(p->dev, "MIX failed BIST (0x%016llx)\n",
  475. (unsigned long long)mix_bist.u64);
  476. agl_gmx_bist.u64 = cvmx_read_csr(CVMX_AGL_GMX_BIST);
  477. if (agl_gmx_bist.u64)
  478. dev_warn(p->dev, "AGL failed BIST (0x%016llx)\n",
  479. (unsigned long long)agl_gmx_bist.u64);
  480. }
  481. struct octeon_mgmt_cam_state {
  482. u64 cam[6];
  483. u64 cam_mask;
  484. int cam_index;
  485. };
  486. static void octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state *cs,
  487. unsigned char *addr)
  488. {
  489. int i;
  490. for (i = 0; i < 6; i++)
  491. cs->cam[i] |= (u64)addr[i] << (8 * (cs->cam_index));
  492. cs->cam_mask |= (1ULL << cs->cam_index);
  493. cs->cam_index++;
  494. }
  495. static void octeon_mgmt_set_rx_filtering(struct net_device *netdev)
  496. {
  497. struct octeon_mgmt *p = netdev_priv(netdev);
  498. union cvmx_agl_gmx_rxx_adr_ctl adr_ctl;
  499. union cvmx_agl_gmx_prtx_cfg agl_gmx_prtx;
  500. unsigned long flags;
  501. unsigned int prev_packet_enable;
  502. unsigned int cam_mode = 1; /* 1 - Accept on CAM match */
  503. unsigned int multicast_mode = 1; /* 1 - Reject all multicast. */
  504. struct octeon_mgmt_cam_state cam_state;
  505. struct netdev_hw_addr *ha;
  506. int available_cam_entries;
  507. memset(&cam_state, 0, sizeof(cam_state));
  508. if ((netdev->flags & IFF_PROMISC) || netdev->uc.count > 7) {
  509. cam_mode = 0;
  510. available_cam_entries = 8;
  511. } else {
  512. /* One CAM entry for the primary address, leaves seven
  513. * for the secondary addresses.
  514. */
  515. available_cam_entries = 7 - netdev->uc.count;
  516. }
  517. if (netdev->flags & IFF_MULTICAST) {
  518. if (cam_mode == 0 || (netdev->flags & IFF_ALLMULTI) ||
  519. netdev_mc_count(netdev) > available_cam_entries)
  520. multicast_mode = 2; /* 2 - Accept all multicast. */
  521. else
  522. multicast_mode = 0; /* 0 - Use CAM. */
  523. }
  524. if (cam_mode == 1) {
  525. /* Add primary address. */
  526. octeon_mgmt_cam_state_add(&cam_state, netdev->dev_addr);
  527. netdev_for_each_uc_addr(ha, netdev)
  528. octeon_mgmt_cam_state_add(&cam_state, ha->addr);
  529. }
  530. if (multicast_mode == 0) {
  531. netdev_for_each_mc_addr(ha, netdev)
  532. octeon_mgmt_cam_state_add(&cam_state, ha->addr);
  533. }
  534. spin_lock_irqsave(&p->lock, flags);
  535. /* Disable packet I/O. */
  536. agl_gmx_prtx.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  537. prev_packet_enable = agl_gmx_prtx.s.en;
  538. agl_gmx_prtx.s.en = 0;
  539. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, agl_gmx_prtx.u64);
  540. adr_ctl.u64 = 0;
  541. adr_ctl.s.cam_mode = cam_mode;
  542. adr_ctl.s.mcst = multicast_mode;
  543. adr_ctl.s.bcst = 1; /* Allow broadcast */
  544. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CTL, adr_ctl.u64);
  545. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM0, cam_state.cam[0]);
  546. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM1, cam_state.cam[1]);
  547. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM2, cam_state.cam[2]);
  548. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM3, cam_state.cam[3]);
  549. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM4, cam_state.cam[4]);
  550. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM5, cam_state.cam[5]);
  551. cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM_EN, cam_state.cam_mask);
  552. /* Restore packet I/O. */
  553. agl_gmx_prtx.s.en = prev_packet_enable;
  554. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, agl_gmx_prtx.u64);
  555. spin_unlock_irqrestore(&p->lock, flags);
  556. }
  557. static int octeon_mgmt_set_mac_address(struct net_device *netdev, void *addr)
  558. {
  559. int r = eth_mac_addr(netdev, addr);
  560. if (r)
  561. return r;
  562. octeon_mgmt_set_rx_filtering(netdev);
  563. return 0;
  564. }
  565. static int octeon_mgmt_change_mtu(struct net_device *netdev, int new_mtu)
  566. {
  567. struct octeon_mgmt *p = netdev_priv(netdev);
  568. int size_without_fcs = new_mtu + OCTEON_MGMT_RX_HEADROOM;
  569. /* Limit the MTU to make sure the ethernet packets are between
  570. * 64 bytes and 16383 bytes.
  571. */
  572. if (size_without_fcs < 64 || size_without_fcs > 16383) {
  573. dev_warn(p->dev, "MTU must be between %d and %d.\n",
  574. 64 - OCTEON_MGMT_RX_HEADROOM,
  575. 16383 - OCTEON_MGMT_RX_HEADROOM);
  576. return -EINVAL;
  577. }
  578. netdev->mtu = new_mtu;
  579. cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_MAX, size_without_fcs);
  580. cvmx_write_csr(p->agl + AGL_GMX_RX_JABBER,
  581. (size_without_fcs + 7) & 0xfff8);
  582. return 0;
  583. }
  584. static irqreturn_t octeon_mgmt_interrupt(int cpl, void *dev_id)
  585. {
  586. struct net_device *netdev = dev_id;
  587. struct octeon_mgmt *p = netdev_priv(netdev);
  588. union cvmx_mixx_isr mixx_isr;
  589. mixx_isr.u64 = cvmx_read_csr(p->mix + MIX_ISR);
  590. /* Clear any pending interrupts */
  591. cvmx_write_csr(p->mix + MIX_ISR, mixx_isr.u64);
  592. cvmx_read_csr(p->mix + MIX_ISR);
  593. if (mixx_isr.s.irthresh) {
  594. octeon_mgmt_disable_rx_irq(p);
  595. napi_schedule(&p->napi);
  596. }
  597. if (mixx_isr.s.orthresh) {
  598. octeon_mgmt_disable_tx_irq(p);
  599. tasklet_schedule(&p->tx_clean_tasklet);
  600. }
  601. return IRQ_HANDLED;
  602. }
  603. static int octeon_mgmt_ioctl_hwtstamp(struct net_device *netdev,
  604. struct ifreq *rq, int cmd)
  605. {
  606. struct octeon_mgmt *p = netdev_priv(netdev);
  607. struct hwtstamp_config config;
  608. union cvmx_mio_ptp_clock_cfg ptp;
  609. union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl;
  610. bool have_hw_timestamps = false;
  611. if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
  612. return -EFAULT;
  613. if (config.flags) /* reserved for future extensions */
  614. return -EINVAL;
  615. /* Check the status of hardware for tiemstamps */
  616. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  617. /* Get the current state of the PTP clock */
  618. ptp.u64 = cvmx_read_csr(CVMX_MIO_PTP_CLOCK_CFG);
  619. if (!ptp.s.ext_clk_en) {
  620. /* The clock has not been configured to use an
  621. * external source. Program it to use the main clock
  622. * reference.
  623. */
  624. u64 clock_comp = (NSEC_PER_SEC << 32) / octeon_get_io_clock_rate();
  625. if (!ptp.s.ptp_en)
  626. cvmx_write_csr(CVMX_MIO_PTP_CLOCK_COMP, clock_comp);
  627. pr_info("PTP Clock: Using sclk reference at %lld Hz\n",
  628. (NSEC_PER_SEC << 32) / clock_comp);
  629. } else {
  630. /* The clock is already programmed to use a GPIO */
  631. u64 clock_comp = cvmx_read_csr(CVMX_MIO_PTP_CLOCK_COMP);
  632. pr_info("PTP Clock: Using GPIO %d at %lld Hz\n",
  633. ptp.s.ext_clk_in,
  634. (NSEC_PER_SEC << 32) / clock_comp);
  635. }
  636. /* Enable the clock if it wasn't done already */
  637. if (!ptp.s.ptp_en) {
  638. ptp.s.ptp_en = 1;
  639. cvmx_write_csr(CVMX_MIO_PTP_CLOCK_CFG, ptp.u64);
  640. }
  641. have_hw_timestamps = true;
  642. }
  643. if (!have_hw_timestamps)
  644. return -EINVAL;
  645. switch (config.tx_type) {
  646. case HWTSTAMP_TX_OFF:
  647. case HWTSTAMP_TX_ON:
  648. break;
  649. default:
  650. return -ERANGE;
  651. }
  652. switch (config.rx_filter) {
  653. case HWTSTAMP_FILTER_NONE:
  654. p->has_rx_tstamp = false;
  655. rxx_frm_ctl.u64 = cvmx_read_csr(p->agl + AGL_GMX_RX_FRM_CTL);
  656. rxx_frm_ctl.s.ptp_mode = 0;
  657. cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
  658. break;
  659. case HWTSTAMP_FILTER_ALL:
  660. case HWTSTAMP_FILTER_SOME:
  661. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  662. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  663. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  664. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  665. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  666. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  667. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  668. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  669. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  670. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  671. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  672. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  673. p->has_rx_tstamp = have_hw_timestamps;
  674. config.rx_filter = HWTSTAMP_FILTER_ALL;
  675. if (p->has_rx_tstamp) {
  676. rxx_frm_ctl.u64 = cvmx_read_csr(p->agl + AGL_GMX_RX_FRM_CTL);
  677. rxx_frm_ctl.s.ptp_mode = 1;
  678. cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
  679. }
  680. break;
  681. default:
  682. return -ERANGE;
  683. }
  684. if (copy_to_user(rq->ifr_data, &config, sizeof(config)))
  685. return -EFAULT;
  686. return 0;
  687. }
  688. static int octeon_mgmt_ioctl(struct net_device *netdev,
  689. struct ifreq *rq, int cmd)
  690. {
  691. struct octeon_mgmt *p = netdev_priv(netdev);
  692. switch (cmd) {
  693. case SIOCSHWTSTAMP:
  694. return octeon_mgmt_ioctl_hwtstamp(netdev, rq, cmd);
  695. default:
  696. if (p->phydev)
  697. return phy_mii_ioctl(p->phydev, rq, cmd);
  698. return -EINVAL;
  699. }
  700. }
  701. static void octeon_mgmt_disable_link(struct octeon_mgmt *p)
  702. {
  703. union cvmx_agl_gmx_prtx_cfg prtx_cfg;
  704. /* Disable GMX before we make any changes. */
  705. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  706. prtx_cfg.s.en = 0;
  707. prtx_cfg.s.tx_en = 0;
  708. prtx_cfg.s.rx_en = 0;
  709. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
  710. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  711. int i;
  712. for (i = 0; i < 10; i++) {
  713. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  714. if (prtx_cfg.s.tx_idle == 1 || prtx_cfg.s.rx_idle == 1)
  715. break;
  716. mdelay(1);
  717. i++;
  718. }
  719. }
  720. }
  721. static void octeon_mgmt_enable_link(struct octeon_mgmt *p)
  722. {
  723. union cvmx_agl_gmx_prtx_cfg prtx_cfg;
  724. /* Restore the GMX enable state only if link is set */
  725. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  726. prtx_cfg.s.tx_en = 1;
  727. prtx_cfg.s.rx_en = 1;
  728. prtx_cfg.s.en = 1;
  729. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
  730. }
  731. static void octeon_mgmt_update_link(struct octeon_mgmt *p)
  732. {
  733. union cvmx_agl_gmx_prtx_cfg prtx_cfg;
  734. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  735. if (!p->phydev->link)
  736. prtx_cfg.s.duplex = 1;
  737. else
  738. prtx_cfg.s.duplex = p->phydev->duplex;
  739. switch (p->phydev->speed) {
  740. case 10:
  741. prtx_cfg.s.speed = 0;
  742. prtx_cfg.s.slottime = 0;
  743. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  744. prtx_cfg.s.burst = 1;
  745. prtx_cfg.s.speed_msb = 1;
  746. }
  747. break;
  748. case 100:
  749. prtx_cfg.s.speed = 0;
  750. prtx_cfg.s.slottime = 0;
  751. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  752. prtx_cfg.s.burst = 1;
  753. prtx_cfg.s.speed_msb = 0;
  754. }
  755. break;
  756. case 1000:
  757. /* 1000 MBits is only supported on 6XXX chips */
  758. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  759. prtx_cfg.s.speed = 1;
  760. prtx_cfg.s.speed_msb = 0;
  761. /* Only matters for half-duplex */
  762. prtx_cfg.s.slottime = 1;
  763. prtx_cfg.s.burst = p->phydev->duplex;
  764. }
  765. break;
  766. case 0: /* No link */
  767. default:
  768. break;
  769. }
  770. /* Write the new GMX setting with the port still disabled. */
  771. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
  772. /* Read GMX CFG again to make sure the config is completed. */
  773. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  774. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  775. union cvmx_agl_gmx_txx_clk agl_clk;
  776. union cvmx_agl_prtx_ctl prtx_ctl;
  777. prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
  778. agl_clk.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_CLK);
  779. /* MII (both speeds) and RGMII 1000 speed. */
  780. agl_clk.s.clk_cnt = 1;
  781. if (prtx_ctl.s.mode == 0) { /* RGMII mode */
  782. if (p->phydev->speed == 10)
  783. agl_clk.s.clk_cnt = 50;
  784. else if (p->phydev->speed == 100)
  785. agl_clk.s.clk_cnt = 5;
  786. }
  787. cvmx_write_csr(p->agl + AGL_GMX_TX_CLK, agl_clk.u64);
  788. }
  789. }
  790. static void octeon_mgmt_adjust_link(struct net_device *netdev)
  791. {
  792. struct octeon_mgmt *p = netdev_priv(netdev);
  793. unsigned long flags;
  794. int link_changed = 0;
  795. if (!p->phydev)
  796. return;
  797. spin_lock_irqsave(&p->lock, flags);
  798. if (!p->phydev->link && p->last_link)
  799. link_changed = -1;
  800. if (p->phydev->link
  801. && (p->last_duplex != p->phydev->duplex
  802. || p->last_link != p->phydev->link
  803. || p->last_speed != p->phydev->speed)) {
  804. octeon_mgmt_disable_link(p);
  805. link_changed = 1;
  806. octeon_mgmt_update_link(p);
  807. octeon_mgmt_enable_link(p);
  808. }
  809. p->last_link = p->phydev->link;
  810. p->last_speed = p->phydev->speed;
  811. p->last_duplex = p->phydev->duplex;
  812. spin_unlock_irqrestore(&p->lock, flags);
  813. if (link_changed != 0) {
  814. if (link_changed > 0) {
  815. pr_info("%s: Link is up - %d/%s\n", netdev->name,
  816. p->phydev->speed,
  817. DUPLEX_FULL == p->phydev->duplex ?
  818. "Full" : "Half");
  819. } else {
  820. pr_info("%s: Link is down\n", netdev->name);
  821. }
  822. }
  823. }
  824. static int octeon_mgmt_init_phy(struct net_device *netdev)
  825. {
  826. struct octeon_mgmt *p = netdev_priv(netdev);
  827. if (octeon_is_simulation() || p->phy_np == NULL) {
  828. /* No PHYs in the simulator. */
  829. netif_carrier_on(netdev);
  830. return 0;
  831. }
  832. p->phydev = of_phy_connect(netdev, p->phy_np,
  833. octeon_mgmt_adjust_link, 0,
  834. PHY_INTERFACE_MODE_MII);
  835. if (!p->phydev)
  836. return -ENODEV;
  837. return 0;
  838. }
  839. static int octeon_mgmt_open(struct net_device *netdev)
  840. {
  841. struct octeon_mgmt *p = netdev_priv(netdev);
  842. union cvmx_mixx_ctl mix_ctl;
  843. union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode;
  844. union cvmx_mixx_oring1 oring1;
  845. union cvmx_mixx_iring1 iring1;
  846. union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl;
  847. union cvmx_mixx_irhwm mix_irhwm;
  848. union cvmx_mixx_orhwm mix_orhwm;
  849. union cvmx_mixx_intena mix_intena;
  850. struct sockaddr sa;
  851. /* Allocate ring buffers. */
  852. p->tx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  853. GFP_KERNEL);
  854. if (!p->tx_ring)
  855. return -ENOMEM;
  856. p->tx_ring_handle =
  857. dma_map_single(p->dev, p->tx_ring,
  858. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  859. DMA_BIDIRECTIONAL);
  860. p->tx_next = 0;
  861. p->tx_next_clean = 0;
  862. p->tx_current_fill = 0;
  863. p->rx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  864. GFP_KERNEL);
  865. if (!p->rx_ring)
  866. goto err_nomem;
  867. p->rx_ring_handle =
  868. dma_map_single(p->dev, p->rx_ring,
  869. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  870. DMA_BIDIRECTIONAL);
  871. p->rx_next = 0;
  872. p->rx_next_fill = 0;
  873. p->rx_current_fill = 0;
  874. octeon_mgmt_reset_hw(p);
  875. mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
  876. /* Bring it out of reset if needed. */
  877. if (mix_ctl.s.reset) {
  878. mix_ctl.s.reset = 0;
  879. cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
  880. do {
  881. mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
  882. } while (mix_ctl.s.reset);
  883. }
  884. if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
  885. agl_gmx_inf_mode.u64 = 0;
  886. agl_gmx_inf_mode.s.en = 1;
  887. cvmx_write_csr(CVMX_AGL_GMX_INF_MODE, agl_gmx_inf_mode.u64);
  888. }
  889. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X)
  890. || OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  891. /* Force compensation values, as they are not
  892. * determined properly by HW
  893. */
  894. union cvmx_agl_gmx_drv_ctl drv_ctl;
  895. drv_ctl.u64 = cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL);
  896. if (p->port) {
  897. drv_ctl.s.byp_en1 = 1;
  898. drv_ctl.s.nctl1 = 6;
  899. drv_ctl.s.pctl1 = 6;
  900. } else {
  901. drv_ctl.s.byp_en = 1;
  902. drv_ctl.s.nctl = 6;
  903. drv_ctl.s.pctl = 6;
  904. }
  905. cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL, drv_ctl.u64);
  906. }
  907. oring1.u64 = 0;
  908. oring1.s.obase = p->tx_ring_handle >> 3;
  909. oring1.s.osize = OCTEON_MGMT_TX_RING_SIZE;
  910. cvmx_write_csr(p->mix + MIX_ORING1, oring1.u64);
  911. iring1.u64 = 0;
  912. iring1.s.ibase = p->rx_ring_handle >> 3;
  913. iring1.s.isize = OCTEON_MGMT_RX_RING_SIZE;
  914. cvmx_write_csr(p->mix + MIX_IRING1, iring1.u64);
  915. memcpy(sa.sa_data, netdev->dev_addr, ETH_ALEN);
  916. octeon_mgmt_set_mac_address(netdev, &sa);
  917. octeon_mgmt_change_mtu(netdev, netdev->mtu);
  918. /* Enable the port HW. Packets are not allowed until
  919. * cvmx_mgmt_port_enable() is called.
  920. */
  921. mix_ctl.u64 = 0;
  922. mix_ctl.s.crc_strip = 1; /* Strip the ending CRC */
  923. mix_ctl.s.en = 1; /* Enable the port */
  924. mix_ctl.s.nbtarb = 0; /* Arbitration mode */
  925. /* MII CB-request FIFO programmable high watermark */
  926. mix_ctl.s.mrq_hwm = 1;
  927. #ifdef __LITTLE_ENDIAN
  928. mix_ctl.s.lendian = 1;
  929. #endif
  930. cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
  931. /* Read the PHY to find the mode of the interface. */
  932. if (octeon_mgmt_init_phy(netdev)) {
  933. dev_err(p->dev, "Cannot initialize PHY on MIX%d.\n", p->port);
  934. goto err_noirq;
  935. }
  936. /* Set the mode of the interface, RGMII/MII. */
  937. if (OCTEON_IS_MODEL(OCTEON_CN6XXX) && p->phydev) {
  938. union cvmx_agl_prtx_ctl agl_prtx_ctl;
  939. int rgmii_mode = (p->phydev->supported &
  940. (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)) != 0;
  941. agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
  942. agl_prtx_ctl.s.mode = rgmii_mode ? 0 : 1;
  943. cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64);
  944. /* MII clocks counts are based on the 125Mhz
  945. * reference, which has an 8nS period. So our delays
  946. * need to be multiplied by this factor.
  947. */
  948. #define NS_PER_PHY_CLK 8
  949. /* Take the DLL and clock tree out of reset */
  950. agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
  951. agl_prtx_ctl.s.clkrst = 0;
  952. if (rgmii_mode) {
  953. agl_prtx_ctl.s.dllrst = 0;
  954. agl_prtx_ctl.s.clktx_byp = 0;
  955. }
  956. cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64);
  957. cvmx_read_csr(p->agl_prt_ctl); /* Force write out before wait */
  958. /* Wait for the DLL to lock. External 125 MHz
  959. * reference clock must be stable at this point.
  960. */
  961. ndelay(256 * NS_PER_PHY_CLK);
  962. /* Enable the interface */
  963. agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
  964. agl_prtx_ctl.s.enable = 1;
  965. cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64);
  966. /* Read the value back to force the previous write */
  967. agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl);
  968. /* Enable the compensation controller */
  969. agl_prtx_ctl.s.comp = 1;
  970. agl_prtx_ctl.s.drv_byp = 0;
  971. cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64);
  972. /* Force write out before wait. */
  973. cvmx_read_csr(p->agl_prt_ctl);
  974. /* For compensation state to lock. */
  975. ndelay(1040 * NS_PER_PHY_CLK);
  976. /* Default Interframe Gaps are too small. Recommended
  977. * workaround is.
  978. *
  979. * AGL_GMX_TX_IFG[IFG1]=14
  980. * AGL_GMX_TX_IFG[IFG2]=10
  981. */
  982. cvmx_write_csr(CVMX_AGL_GMX_TX_IFG, 0xae);
  983. }
  984. octeon_mgmt_rx_fill_ring(netdev);
  985. /* Clear statistics. */
  986. /* Clear on read. */
  987. cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_CTL, 1);
  988. cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP, 0);
  989. cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD, 0);
  990. cvmx_write_csr(p->agl + AGL_GMX_TX_STATS_CTL, 1);
  991. cvmx_write_csr(p->agl + AGL_GMX_TX_STAT0, 0);
  992. cvmx_write_csr(p->agl + AGL_GMX_TX_STAT1, 0);
  993. /* Clear any pending interrupts */
  994. cvmx_write_csr(p->mix + MIX_ISR, cvmx_read_csr(p->mix + MIX_ISR));
  995. if (request_irq(p->irq, octeon_mgmt_interrupt, 0, netdev->name,
  996. netdev)) {
  997. dev_err(p->dev, "request_irq(%d) failed.\n", p->irq);
  998. goto err_noirq;
  999. }
  1000. /* Interrupt every single RX packet */
  1001. mix_irhwm.u64 = 0;
  1002. mix_irhwm.s.irhwm = 0;
  1003. cvmx_write_csr(p->mix + MIX_IRHWM, mix_irhwm.u64);
  1004. /* Interrupt when we have 1 or more packets to clean. */
  1005. mix_orhwm.u64 = 0;
  1006. mix_orhwm.s.orhwm = 0;
  1007. cvmx_write_csr(p->mix + MIX_ORHWM, mix_orhwm.u64);
  1008. /* Enable receive and transmit interrupts */
  1009. mix_intena.u64 = 0;
  1010. mix_intena.s.ithena = 1;
  1011. mix_intena.s.othena = 1;
  1012. cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
  1013. /* Enable packet I/O. */
  1014. rxx_frm_ctl.u64 = 0;
  1015. rxx_frm_ctl.s.ptp_mode = p->has_rx_tstamp ? 1 : 0;
  1016. rxx_frm_ctl.s.pre_align = 1;
  1017. /* When set, disables the length check for non-min sized pkts
  1018. * with padding in the client data.
  1019. */
  1020. rxx_frm_ctl.s.pad_len = 1;
  1021. /* When set, disables the length check for VLAN pkts */
  1022. rxx_frm_ctl.s.vlan_len = 1;
  1023. /* When set, PREAMBLE checking is less strict */
  1024. rxx_frm_ctl.s.pre_free = 1;
  1025. /* Control Pause Frames can match station SMAC */
  1026. rxx_frm_ctl.s.ctl_smac = 0;
  1027. /* Control Pause Frames can match globally assign Multicast address */
  1028. rxx_frm_ctl.s.ctl_mcst = 1;
  1029. /* Forward pause information to TX block */
  1030. rxx_frm_ctl.s.ctl_bck = 1;
  1031. /* Drop Control Pause Frames */
  1032. rxx_frm_ctl.s.ctl_drp = 1;
  1033. /* Strip off the preamble */
  1034. rxx_frm_ctl.s.pre_strp = 1;
  1035. /* This port is configured to send PREAMBLE+SFD to begin every
  1036. * frame. GMX checks that the PREAMBLE is sent correctly.
  1037. */
  1038. rxx_frm_ctl.s.pre_chk = 1;
  1039. cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
  1040. /* Configure the port duplex, speed and enables */
  1041. octeon_mgmt_disable_link(p);
  1042. if (p->phydev)
  1043. octeon_mgmt_update_link(p);
  1044. octeon_mgmt_enable_link(p);
  1045. p->last_link = 0;
  1046. p->last_speed = 0;
  1047. /* PHY is not present in simulator. The carrier is enabled
  1048. * while initializing the phy for simulator, leave it enabled.
  1049. */
  1050. if (p->phydev) {
  1051. netif_carrier_off(netdev);
  1052. phy_start_aneg(p->phydev);
  1053. }
  1054. netif_wake_queue(netdev);
  1055. napi_enable(&p->napi);
  1056. return 0;
  1057. err_noirq:
  1058. octeon_mgmt_reset_hw(p);
  1059. dma_unmap_single(p->dev, p->rx_ring_handle,
  1060. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  1061. DMA_BIDIRECTIONAL);
  1062. kfree(p->rx_ring);
  1063. err_nomem:
  1064. dma_unmap_single(p->dev, p->tx_ring_handle,
  1065. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  1066. DMA_BIDIRECTIONAL);
  1067. kfree(p->tx_ring);
  1068. return -ENOMEM;
  1069. }
  1070. static int octeon_mgmt_stop(struct net_device *netdev)
  1071. {
  1072. struct octeon_mgmt *p = netdev_priv(netdev);
  1073. napi_disable(&p->napi);
  1074. netif_stop_queue(netdev);
  1075. if (p->phydev)
  1076. phy_disconnect(p->phydev);
  1077. p->phydev = NULL;
  1078. netif_carrier_off(netdev);
  1079. octeon_mgmt_reset_hw(p);
  1080. free_irq(p->irq, netdev);
  1081. /* dma_unmap is a nop on Octeon, so just free everything. */
  1082. skb_queue_purge(&p->tx_list);
  1083. skb_queue_purge(&p->rx_list);
  1084. dma_unmap_single(p->dev, p->rx_ring_handle,
  1085. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  1086. DMA_BIDIRECTIONAL);
  1087. kfree(p->rx_ring);
  1088. dma_unmap_single(p->dev, p->tx_ring_handle,
  1089. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  1090. DMA_BIDIRECTIONAL);
  1091. kfree(p->tx_ring);
  1092. return 0;
  1093. }
  1094. static int octeon_mgmt_xmit(struct sk_buff *skb, struct net_device *netdev)
  1095. {
  1096. struct octeon_mgmt *p = netdev_priv(netdev);
  1097. union mgmt_port_ring_entry re;
  1098. unsigned long flags;
  1099. int rv = NETDEV_TX_BUSY;
  1100. re.d64 = 0;
  1101. re.s.tstamp = ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) != 0);
  1102. re.s.len = skb->len;
  1103. re.s.addr = dma_map_single(p->dev, skb->data,
  1104. skb->len,
  1105. DMA_TO_DEVICE);
  1106. spin_lock_irqsave(&p->tx_list.lock, flags);
  1107. if (unlikely(p->tx_current_fill >= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE) - 1)) {
  1108. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  1109. netif_stop_queue(netdev);
  1110. spin_lock_irqsave(&p->tx_list.lock, flags);
  1111. }
  1112. if (unlikely(p->tx_current_fill >=
  1113. ring_max_fill(OCTEON_MGMT_TX_RING_SIZE))) {
  1114. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  1115. dma_unmap_single(p->dev, re.s.addr, re.s.len,
  1116. DMA_TO_DEVICE);
  1117. goto out;
  1118. }
  1119. __skb_queue_tail(&p->tx_list, skb);
  1120. /* Put it in the ring. */
  1121. p->tx_ring[p->tx_next] = re.d64;
  1122. p->tx_next = (p->tx_next + 1) % OCTEON_MGMT_TX_RING_SIZE;
  1123. p->tx_current_fill++;
  1124. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  1125. dma_sync_single_for_device(p->dev, p->tx_ring_handle,
  1126. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  1127. DMA_BIDIRECTIONAL);
  1128. netdev->stats.tx_packets++;
  1129. netdev->stats.tx_bytes += skb->len;
  1130. /* Ring the bell. */
  1131. cvmx_write_csr(p->mix + MIX_ORING2, 1);
  1132. netdev->trans_start = jiffies;
  1133. rv = NETDEV_TX_OK;
  1134. out:
  1135. octeon_mgmt_update_tx_stats(netdev);
  1136. return rv;
  1137. }
  1138. #ifdef CONFIG_NET_POLL_CONTROLLER
  1139. static void octeon_mgmt_poll_controller(struct net_device *netdev)
  1140. {
  1141. struct octeon_mgmt *p = netdev_priv(netdev);
  1142. octeon_mgmt_receive_packets(p, 16);
  1143. octeon_mgmt_update_rx_stats(netdev);
  1144. }
  1145. #endif
  1146. static void octeon_mgmt_get_drvinfo(struct net_device *netdev,
  1147. struct ethtool_drvinfo *info)
  1148. {
  1149. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1150. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1151. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  1152. strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
  1153. info->n_stats = 0;
  1154. info->testinfo_len = 0;
  1155. info->regdump_len = 0;
  1156. info->eedump_len = 0;
  1157. }
  1158. static int octeon_mgmt_get_settings(struct net_device *netdev,
  1159. struct ethtool_cmd *cmd)
  1160. {
  1161. struct octeon_mgmt *p = netdev_priv(netdev);
  1162. if (p->phydev)
  1163. return phy_ethtool_gset(p->phydev, cmd);
  1164. return -EOPNOTSUPP;
  1165. }
  1166. static int octeon_mgmt_set_settings(struct net_device *netdev,
  1167. struct ethtool_cmd *cmd)
  1168. {
  1169. struct octeon_mgmt *p = netdev_priv(netdev);
  1170. if (!capable(CAP_NET_ADMIN))
  1171. return -EPERM;
  1172. if (p->phydev)
  1173. return phy_ethtool_sset(p->phydev, cmd);
  1174. return -EOPNOTSUPP;
  1175. }
  1176. static int octeon_mgmt_nway_reset(struct net_device *dev)
  1177. {
  1178. struct octeon_mgmt *p = netdev_priv(dev);
  1179. if (!capable(CAP_NET_ADMIN))
  1180. return -EPERM;
  1181. if (p->phydev)
  1182. return phy_start_aneg(p->phydev);
  1183. return -EOPNOTSUPP;
  1184. }
  1185. static const struct ethtool_ops octeon_mgmt_ethtool_ops = {
  1186. .get_drvinfo = octeon_mgmt_get_drvinfo,
  1187. .get_settings = octeon_mgmt_get_settings,
  1188. .set_settings = octeon_mgmt_set_settings,
  1189. .nway_reset = octeon_mgmt_nway_reset,
  1190. .get_link = ethtool_op_get_link,
  1191. };
  1192. static const struct net_device_ops octeon_mgmt_ops = {
  1193. .ndo_open = octeon_mgmt_open,
  1194. .ndo_stop = octeon_mgmt_stop,
  1195. .ndo_start_xmit = octeon_mgmt_xmit,
  1196. .ndo_set_rx_mode = octeon_mgmt_set_rx_filtering,
  1197. .ndo_set_mac_address = octeon_mgmt_set_mac_address,
  1198. .ndo_do_ioctl = octeon_mgmt_ioctl,
  1199. .ndo_change_mtu = octeon_mgmt_change_mtu,
  1200. #ifdef CONFIG_NET_POLL_CONTROLLER
  1201. .ndo_poll_controller = octeon_mgmt_poll_controller,
  1202. #endif
  1203. };
  1204. static int octeon_mgmt_probe(struct platform_device *pdev)
  1205. {
  1206. struct net_device *netdev;
  1207. struct octeon_mgmt *p;
  1208. const __be32 *data;
  1209. const u8 *mac;
  1210. struct resource *res_mix;
  1211. struct resource *res_agl;
  1212. struct resource *res_agl_prt_ctl;
  1213. int len;
  1214. int result;
  1215. netdev = alloc_etherdev(sizeof(struct octeon_mgmt));
  1216. if (netdev == NULL)
  1217. return -ENOMEM;
  1218. SET_NETDEV_DEV(netdev, &pdev->dev);
  1219. platform_set_drvdata(pdev, netdev);
  1220. p = netdev_priv(netdev);
  1221. netif_napi_add(netdev, &p->napi, octeon_mgmt_napi_poll,
  1222. OCTEON_MGMT_NAPI_WEIGHT);
  1223. p->netdev = netdev;
  1224. p->dev = &pdev->dev;
  1225. p->has_rx_tstamp = false;
  1226. data = of_get_property(pdev->dev.of_node, "cell-index", &len);
  1227. if (data && len == sizeof(*data)) {
  1228. p->port = be32_to_cpup(data);
  1229. } else {
  1230. dev_err(&pdev->dev, "no 'cell-index' property\n");
  1231. result = -ENXIO;
  1232. goto err;
  1233. }
  1234. snprintf(netdev->name, IFNAMSIZ, "mgmt%d", p->port);
  1235. result = platform_get_irq(pdev, 0);
  1236. if (result < 0)
  1237. goto err;
  1238. p->irq = result;
  1239. res_mix = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1240. if (res_mix == NULL) {
  1241. dev_err(&pdev->dev, "no 'reg' resource\n");
  1242. result = -ENXIO;
  1243. goto err;
  1244. }
  1245. res_agl = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1246. if (res_agl == NULL) {
  1247. dev_err(&pdev->dev, "no 'reg' resource\n");
  1248. result = -ENXIO;
  1249. goto err;
  1250. }
  1251. res_agl_prt_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1252. if (res_agl_prt_ctl == NULL) {
  1253. dev_err(&pdev->dev, "no 'reg' resource\n");
  1254. result = -ENXIO;
  1255. goto err;
  1256. }
  1257. p->mix_phys = res_mix->start;
  1258. p->mix_size = resource_size(res_mix);
  1259. p->agl_phys = res_agl->start;
  1260. p->agl_size = resource_size(res_agl);
  1261. p->agl_prt_ctl_phys = res_agl_prt_ctl->start;
  1262. p->agl_prt_ctl_size = resource_size(res_agl_prt_ctl);
  1263. if (!devm_request_mem_region(&pdev->dev, p->mix_phys, p->mix_size,
  1264. res_mix->name)) {
  1265. dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
  1266. res_mix->name);
  1267. result = -ENXIO;
  1268. goto err;
  1269. }
  1270. if (!devm_request_mem_region(&pdev->dev, p->agl_phys, p->agl_size,
  1271. res_agl->name)) {
  1272. result = -ENXIO;
  1273. dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
  1274. res_agl->name);
  1275. goto err;
  1276. }
  1277. if (!devm_request_mem_region(&pdev->dev, p->agl_prt_ctl_phys,
  1278. p->agl_prt_ctl_size, res_agl_prt_ctl->name)) {
  1279. result = -ENXIO;
  1280. dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
  1281. res_agl_prt_ctl->name);
  1282. goto err;
  1283. }
  1284. p->mix = (u64)devm_ioremap(&pdev->dev, p->mix_phys, p->mix_size);
  1285. p->agl = (u64)devm_ioremap(&pdev->dev, p->agl_phys, p->agl_size);
  1286. p->agl_prt_ctl = (u64)devm_ioremap(&pdev->dev, p->agl_prt_ctl_phys,
  1287. p->agl_prt_ctl_size);
  1288. spin_lock_init(&p->lock);
  1289. skb_queue_head_init(&p->tx_list);
  1290. skb_queue_head_init(&p->rx_list);
  1291. tasklet_init(&p->tx_clean_tasklet,
  1292. octeon_mgmt_clean_tx_tasklet, (unsigned long)p);
  1293. netdev->priv_flags |= IFF_UNICAST_FLT;
  1294. netdev->netdev_ops = &octeon_mgmt_ops;
  1295. netdev->ethtool_ops = &octeon_mgmt_ethtool_ops;
  1296. mac = of_get_mac_address(pdev->dev.of_node);
  1297. if (mac && is_valid_ether_addr(mac))
  1298. memcpy(netdev->dev_addr, mac, ETH_ALEN);
  1299. else
  1300. eth_hw_addr_random(netdev);
  1301. p->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  1302. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(64);
  1303. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  1304. netif_carrier_off(netdev);
  1305. result = register_netdev(netdev);
  1306. if (result)
  1307. goto err;
  1308. dev_info(&pdev->dev, "Version " DRV_VERSION "\n");
  1309. return 0;
  1310. err:
  1311. free_netdev(netdev);
  1312. return result;
  1313. }
  1314. static int octeon_mgmt_remove(struct platform_device *pdev)
  1315. {
  1316. struct net_device *netdev = platform_get_drvdata(pdev);
  1317. unregister_netdev(netdev);
  1318. free_netdev(netdev);
  1319. return 0;
  1320. }
  1321. static struct of_device_id octeon_mgmt_match[] = {
  1322. {
  1323. .compatible = "cavium,octeon-5750-mix",
  1324. },
  1325. {},
  1326. };
  1327. MODULE_DEVICE_TABLE(of, octeon_mgmt_match);
  1328. static struct platform_driver octeon_mgmt_driver = {
  1329. .driver = {
  1330. .name = "octeon_mgmt",
  1331. .owner = THIS_MODULE,
  1332. .of_match_table = octeon_mgmt_match,
  1333. },
  1334. .probe = octeon_mgmt_probe,
  1335. .remove = octeon_mgmt_remove,
  1336. };
  1337. extern void octeon_mdiobus_force_mod_depencency(void);
  1338. static int __init octeon_mgmt_mod_init(void)
  1339. {
  1340. /* Force our mdiobus driver module to be loaded first. */
  1341. octeon_mdiobus_force_mod_depencency();
  1342. return platform_driver_register(&octeon_mgmt_driver);
  1343. }
  1344. static void __exit octeon_mgmt_mod_exit(void)
  1345. {
  1346. platform_driver_unregister(&octeon_mgmt_driver);
  1347. }
  1348. module_init(octeon_mgmt_mod_init);
  1349. module_exit(octeon_mgmt_mod_exit);
  1350. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1351. MODULE_AUTHOR("David Daney");
  1352. MODULE_LICENSE("GPL");
  1353. MODULE_VERSION(DRV_VERSION);