octeon_mgmt.c 37 KB

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