octeon_mgmt.c 42 KB

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