octeon_mgmt.c 41 KB

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