octeon_mgmt.c 41 KB

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