octeon_mgmt.c 32 KB

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