octeon_mgmt.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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 (!p->phydev)
  610. return -1;
  611. phy_start_aneg(p->phydev);
  612. return 0;
  613. }
  614. static int octeon_mgmt_open(struct net_device *netdev)
  615. {
  616. struct octeon_mgmt *p = netdev_priv(netdev);
  617. int port = p->port;
  618. union cvmx_mixx_ctl mix_ctl;
  619. union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode;
  620. union cvmx_mixx_oring1 oring1;
  621. union cvmx_mixx_iring1 iring1;
  622. union cvmx_agl_gmx_prtx_cfg prtx_cfg;
  623. union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl;
  624. union cvmx_mixx_irhwm mix_irhwm;
  625. union cvmx_mixx_orhwm mix_orhwm;
  626. union cvmx_mixx_intena mix_intena;
  627. struct sockaddr sa;
  628. /* Allocate ring buffers. */
  629. p->tx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  630. GFP_KERNEL);
  631. if (!p->tx_ring)
  632. return -ENOMEM;
  633. p->tx_ring_handle =
  634. dma_map_single(p->dev, p->tx_ring,
  635. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  636. DMA_BIDIRECTIONAL);
  637. p->tx_next = 0;
  638. p->tx_next_clean = 0;
  639. p->tx_current_fill = 0;
  640. p->rx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  641. GFP_KERNEL);
  642. if (!p->rx_ring)
  643. goto err_nomem;
  644. p->rx_ring_handle =
  645. dma_map_single(p->dev, p->rx_ring,
  646. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  647. DMA_BIDIRECTIONAL);
  648. p->rx_next = 0;
  649. p->rx_next_fill = 0;
  650. p->rx_current_fill = 0;
  651. octeon_mgmt_reset_hw(p);
  652. mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
  653. /* Bring it out of reset if needed. */
  654. if (mix_ctl.s.reset) {
  655. mix_ctl.s.reset = 0;
  656. cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
  657. do {
  658. mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL);
  659. } while (mix_ctl.s.reset);
  660. }
  661. agl_gmx_inf_mode.u64 = 0;
  662. agl_gmx_inf_mode.s.en = 1;
  663. cvmx_write_csr(CVMX_AGL_GMX_INF_MODE, agl_gmx_inf_mode.u64);
  664. oring1.u64 = 0;
  665. oring1.s.obase = p->tx_ring_handle >> 3;
  666. oring1.s.osize = OCTEON_MGMT_TX_RING_SIZE;
  667. cvmx_write_csr(p->mix + MIX_ORING1, oring1.u64);
  668. iring1.u64 = 0;
  669. iring1.s.ibase = p->rx_ring_handle >> 3;
  670. iring1.s.isize = OCTEON_MGMT_RX_RING_SIZE;
  671. cvmx_write_csr(p->mix + MIX_IRING1, iring1.u64);
  672. /* Disable packet I/O. */
  673. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  674. prtx_cfg.s.en = 0;
  675. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
  676. memcpy(sa.sa_data, netdev->dev_addr, ETH_ALEN);
  677. octeon_mgmt_set_mac_address(netdev, &sa);
  678. octeon_mgmt_change_mtu(netdev, netdev->mtu);
  679. /*
  680. * Enable the port HW. Packets are not allowed until
  681. * cvmx_mgmt_port_enable() is called.
  682. */
  683. mix_ctl.u64 = 0;
  684. mix_ctl.s.crc_strip = 1; /* Strip the ending CRC */
  685. mix_ctl.s.en = 1; /* Enable the port */
  686. mix_ctl.s.nbtarb = 0; /* Arbitration mode */
  687. /* MII CB-request FIFO programmable high watermark */
  688. mix_ctl.s.mrq_hwm = 1;
  689. cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64);
  690. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X)
  691. || OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  692. /*
  693. * Force compensation values, as they are not
  694. * determined properly by HW
  695. */
  696. union cvmx_agl_gmx_drv_ctl drv_ctl;
  697. drv_ctl.u64 = cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL);
  698. if (port) {
  699. drv_ctl.s.byp_en1 = 1;
  700. drv_ctl.s.nctl1 = 6;
  701. drv_ctl.s.pctl1 = 6;
  702. } else {
  703. drv_ctl.s.byp_en = 1;
  704. drv_ctl.s.nctl = 6;
  705. drv_ctl.s.pctl = 6;
  706. }
  707. cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL, drv_ctl.u64);
  708. }
  709. octeon_mgmt_rx_fill_ring(netdev);
  710. /* Clear statistics. */
  711. /* Clear on read. */
  712. cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_CTL, 1);
  713. cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP, 0);
  714. cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD, 0);
  715. cvmx_write_csr(p->agl + AGL_GMX_TX_STATS_CTL, 1);
  716. cvmx_write_csr(p->agl + AGL_GMX_TX_STAT0, 0);
  717. cvmx_write_csr(p->agl + AGL_GMX_TX_STAT1, 0);
  718. /* Clear any pending interrupts */
  719. cvmx_write_csr(p->mix + MIX_ISR, cvmx_read_csr(p->mix + MIX_ISR));
  720. if (request_irq(p->irq, octeon_mgmt_interrupt, 0, netdev->name,
  721. netdev)) {
  722. dev_err(p->dev, "request_irq(%d) failed.\n", p->irq);
  723. goto err_noirq;
  724. }
  725. /* Interrupt every single RX packet */
  726. mix_irhwm.u64 = 0;
  727. mix_irhwm.s.irhwm = 0;
  728. cvmx_write_csr(p->mix + MIX_IRHWM, mix_irhwm.u64);
  729. /* Interrupt when we have 1 or more packets to clean. */
  730. mix_orhwm.u64 = 0;
  731. mix_orhwm.s.orhwm = 1;
  732. cvmx_write_csr(p->mix + MIX_ORHWM, mix_orhwm.u64);
  733. /* Enable receive and transmit interrupts */
  734. mix_intena.u64 = 0;
  735. mix_intena.s.ithena = 1;
  736. mix_intena.s.othena = 1;
  737. cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64);
  738. /* Enable packet I/O. */
  739. rxx_frm_ctl.u64 = 0;
  740. rxx_frm_ctl.s.pre_align = 1;
  741. /*
  742. * When set, disables the length check for non-min sized pkts
  743. * with padding in the client data.
  744. */
  745. rxx_frm_ctl.s.pad_len = 1;
  746. /* When set, disables the length check for VLAN pkts */
  747. rxx_frm_ctl.s.vlan_len = 1;
  748. /* When set, PREAMBLE checking is less strict */
  749. rxx_frm_ctl.s.pre_free = 1;
  750. /* Control Pause Frames can match station SMAC */
  751. rxx_frm_ctl.s.ctl_smac = 0;
  752. /* Control Pause Frames can match globally assign Multicast address */
  753. rxx_frm_ctl.s.ctl_mcst = 1;
  754. /* Forward pause information to TX block */
  755. rxx_frm_ctl.s.ctl_bck = 1;
  756. /* Drop Control Pause Frames */
  757. rxx_frm_ctl.s.ctl_drp = 1;
  758. /* Strip off the preamble */
  759. rxx_frm_ctl.s.pre_strp = 1;
  760. /*
  761. * This port is configured to send PREAMBLE+SFD to begin every
  762. * frame. GMX checks that the PREAMBLE is sent correctly.
  763. */
  764. rxx_frm_ctl.s.pre_chk = 1;
  765. cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64);
  766. /* Enable the AGL block */
  767. agl_gmx_inf_mode.u64 = 0;
  768. agl_gmx_inf_mode.s.en = 1;
  769. cvmx_write_csr(CVMX_AGL_GMX_INF_MODE, agl_gmx_inf_mode.u64);
  770. /* Configure the port duplex and enables */
  771. prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG);
  772. prtx_cfg.s.tx_en = 1;
  773. prtx_cfg.s.rx_en = 1;
  774. prtx_cfg.s.en = 1;
  775. p->last_duplex = 1;
  776. prtx_cfg.s.duplex = p->last_duplex;
  777. cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64);
  778. p->last_link = 0;
  779. netif_carrier_off(netdev);
  780. if (octeon_mgmt_init_phy(netdev)) {
  781. dev_err(p->dev, "Cannot initialize PHY.\n");
  782. goto err_noirq;
  783. }
  784. netif_wake_queue(netdev);
  785. napi_enable(&p->napi);
  786. return 0;
  787. err_noirq:
  788. octeon_mgmt_reset_hw(p);
  789. dma_unmap_single(p->dev, p->rx_ring_handle,
  790. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  791. DMA_BIDIRECTIONAL);
  792. kfree(p->rx_ring);
  793. err_nomem:
  794. dma_unmap_single(p->dev, p->tx_ring_handle,
  795. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  796. DMA_BIDIRECTIONAL);
  797. kfree(p->tx_ring);
  798. return -ENOMEM;
  799. }
  800. static int octeon_mgmt_stop(struct net_device *netdev)
  801. {
  802. struct octeon_mgmt *p = netdev_priv(netdev);
  803. napi_disable(&p->napi);
  804. netif_stop_queue(netdev);
  805. if (p->phydev)
  806. phy_disconnect(p->phydev);
  807. netif_carrier_off(netdev);
  808. octeon_mgmt_reset_hw(p);
  809. free_irq(p->irq, netdev);
  810. /* dma_unmap is a nop on Octeon, so just free everything. */
  811. skb_queue_purge(&p->tx_list);
  812. skb_queue_purge(&p->rx_list);
  813. dma_unmap_single(p->dev, p->rx_ring_handle,
  814. ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE),
  815. DMA_BIDIRECTIONAL);
  816. kfree(p->rx_ring);
  817. dma_unmap_single(p->dev, p->tx_ring_handle,
  818. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  819. DMA_BIDIRECTIONAL);
  820. kfree(p->tx_ring);
  821. return 0;
  822. }
  823. static int octeon_mgmt_xmit(struct sk_buff *skb, struct net_device *netdev)
  824. {
  825. struct octeon_mgmt *p = netdev_priv(netdev);
  826. union mgmt_port_ring_entry re;
  827. unsigned long flags;
  828. int rv = NETDEV_TX_BUSY;
  829. re.d64 = 0;
  830. re.s.len = skb->len;
  831. re.s.addr = dma_map_single(p->dev, skb->data,
  832. skb->len,
  833. DMA_TO_DEVICE);
  834. spin_lock_irqsave(&p->tx_list.lock, flags);
  835. if (unlikely(p->tx_current_fill >= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE) - 1)) {
  836. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  837. netif_stop_queue(netdev);
  838. spin_lock_irqsave(&p->tx_list.lock, flags);
  839. }
  840. if (unlikely(p->tx_current_fill >=
  841. ring_max_fill(OCTEON_MGMT_TX_RING_SIZE))) {
  842. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  843. dma_unmap_single(p->dev, re.s.addr, re.s.len,
  844. DMA_TO_DEVICE);
  845. goto out;
  846. }
  847. __skb_queue_tail(&p->tx_list, skb);
  848. /* Put it in the ring. */
  849. p->tx_ring[p->tx_next] = re.d64;
  850. p->tx_next = (p->tx_next + 1) % OCTEON_MGMT_TX_RING_SIZE;
  851. p->tx_current_fill++;
  852. spin_unlock_irqrestore(&p->tx_list.lock, flags);
  853. dma_sync_single_for_device(p->dev, p->tx_ring_handle,
  854. ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE),
  855. DMA_BIDIRECTIONAL);
  856. netdev->stats.tx_packets++;
  857. netdev->stats.tx_bytes += skb->len;
  858. /* Ring the bell. */
  859. cvmx_write_csr(p->mix + MIX_ORING2, 1);
  860. rv = NETDEV_TX_OK;
  861. out:
  862. octeon_mgmt_update_tx_stats(netdev);
  863. return rv;
  864. }
  865. #ifdef CONFIG_NET_POLL_CONTROLLER
  866. static void octeon_mgmt_poll_controller(struct net_device *netdev)
  867. {
  868. struct octeon_mgmt *p = netdev_priv(netdev);
  869. octeon_mgmt_receive_packets(p, 16);
  870. octeon_mgmt_update_rx_stats(netdev);
  871. }
  872. #endif
  873. static void octeon_mgmt_get_drvinfo(struct net_device *netdev,
  874. struct ethtool_drvinfo *info)
  875. {
  876. strncpy(info->driver, DRV_NAME, sizeof(info->driver));
  877. strncpy(info->version, DRV_VERSION, sizeof(info->version));
  878. strncpy(info->fw_version, "N/A", sizeof(info->fw_version));
  879. strncpy(info->bus_info, "N/A", sizeof(info->bus_info));
  880. info->n_stats = 0;
  881. info->testinfo_len = 0;
  882. info->regdump_len = 0;
  883. info->eedump_len = 0;
  884. }
  885. static int octeon_mgmt_get_settings(struct net_device *netdev,
  886. struct ethtool_cmd *cmd)
  887. {
  888. struct octeon_mgmt *p = netdev_priv(netdev);
  889. if (p->phydev)
  890. return phy_ethtool_gset(p->phydev, cmd);
  891. return -EINVAL;
  892. }
  893. static int octeon_mgmt_set_settings(struct net_device *netdev,
  894. struct ethtool_cmd *cmd)
  895. {
  896. struct octeon_mgmt *p = netdev_priv(netdev);
  897. if (!capable(CAP_NET_ADMIN))
  898. return -EPERM;
  899. if (p->phydev)
  900. return phy_ethtool_sset(p->phydev, cmd);
  901. return -EINVAL;
  902. }
  903. static const struct ethtool_ops octeon_mgmt_ethtool_ops = {
  904. .get_drvinfo = octeon_mgmt_get_drvinfo,
  905. .get_link = ethtool_op_get_link,
  906. .get_settings = octeon_mgmt_get_settings,
  907. .set_settings = octeon_mgmt_set_settings
  908. };
  909. static const struct net_device_ops octeon_mgmt_ops = {
  910. .ndo_open = octeon_mgmt_open,
  911. .ndo_stop = octeon_mgmt_stop,
  912. .ndo_start_xmit = octeon_mgmt_xmit,
  913. .ndo_set_rx_mode = octeon_mgmt_set_rx_filtering,
  914. .ndo_set_mac_address = octeon_mgmt_set_mac_address,
  915. .ndo_do_ioctl = octeon_mgmt_ioctl,
  916. .ndo_change_mtu = octeon_mgmt_change_mtu,
  917. #ifdef CONFIG_NET_POLL_CONTROLLER
  918. .ndo_poll_controller = octeon_mgmt_poll_controller,
  919. #endif
  920. };
  921. static int __devinit octeon_mgmt_probe(struct platform_device *pdev)
  922. {
  923. struct net_device *netdev;
  924. struct octeon_mgmt *p;
  925. const __be32 *data;
  926. const u8 *mac;
  927. struct resource *res_mix;
  928. struct resource *res_agl;
  929. int len;
  930. int result;
  931. netdev = alloc_etherdev(sizeof(struct octeon_mgmt));
  932. if (netdev == NULL)
  933. return -ENOMEM;
  934. dev_set_drvdata(&pdev->dev, netdev);
  935. p = netdev_priv(netdev);
  936. netif_napi_add(netdev, &p->napi, octeon_mgmt_napi_poll,
  937. OCTEON_MGMT_NAPI_WEIGHT);
  938. p->netdev = netdev;
  939. p->dev = &pdev->dev;
  940. data = of_get_property(pdev->dev.of_node, "cell-index", &len);
  941. if (data && len == sizeof(*data)) {
  942. p->port = be32_to_cpup(data);
  943. } else {
  944. dev_err(&pdev->dev, "no 'cell-index' property\n");
  945. result = -ENXIO;
  946. goto err;
  947. }
  948. snprintf(netdev->name, IFNAMSIZ, "mgmt%d", p->port);
  949. result = platform_get_irq(pdev, 0);
  950. if (result < 0)
  951. goto err;
  952. p->irq = result;
  953. res_mix = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  954. if (res_mix == NULL) {
  955. dev_err(&pdev->dev, "no 'reg' resource\n");
  956. result = -ENXIO;
  957. goto err;
  958. }
  959. res_agl = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  960. if (res_agl == NULL) {
  961. dev_err(&pdev->dev, "no 'reg' resource\n");
  962. result = -ENXIO;
  963. goto err;
  964. }
  965. p->mix_phys = res_mix->start;
  966. p->mix_size = resource_size(res_mix);
  967. p->agl_phys = res_agl->start;
  968. p->agl_size = resource_size(res_agl);
  969. if (!devm_request_mem_region(&pdev->dev, p->mix_phys, p->mix_size,
  970. res_mix->name)) {
  971. dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
  972. res_mix->name);
  973. result = -ENXIO;
  974. goto err;
  975. }
  976. if (!devm_request_mem_region(&pdev->dev, p->agl_phys, p->agl_size,
  977. res_agl->name)) {
  978. result = -ENXIO;
  979. dev_err(&pdev->dev, "request_mem_region (%s) failed\n",
  980. res_agl->name);
  981. goto err;
  982. }
  983. p->mix = (u64)devm_ioremap(&pdev->dev, p->mix_phys, p->mix_size);
  984. p->agl = (u64)devm_ioremap(&pdev->dev, p->agl_phys, p->agl_size);
  985. spin_lock_init(&p->lock);
  986. skb_queue_head_init(&p->tx_list);
  987. skb_queue_head_init(&p->rx_list);
  988. tasklet_init(&p->tx_clean_tasklet,
  989. octeon_mgmt_clean_tx_tasklet, (unsigned long)p);
  990. netdev->priv_flags |= IFF_UNICAST_FLT;
  991. netdev->netdev_ops = &octeon_mgmt_ops;
  992. netdev->ethtool_ops = &octeon_mgmt_ethtool_ops;
  993. mac = of_get_mac_address(pdev->dev.of_node);
  994. if (mac)
  995. memcpy(netdev->dev_addr, mac, 6);
  996. p->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  997. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(64);
  998. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  999. result = register_netdev(netdev);
  1000. if (result)
  1001. goto err;
  1002. dev_info(&pdev->dev, "Version " DRV_VERSION "\n");
  1003. return 0;
  1004. err:
  1005. free_netdev(netdev);
  1006. return result;
  1007. }
  1008. static int __devexit octeon_mgmt_remove(struct platform_device *pdev)
  1009. {
  1010. struct net_device *netdev = dev_get_drvdata(&pdev->dev);
  1011. unregister_netdev(netdev);
  1012. free_netdev(netdev);
  1013. return 0;
  1014. }
  1015. static struct of_device_id octeon_mgmt_match[] = {
  1016. {
  1017. .compatible = "cavium,octeon-5750-mix",
  1018. },
  1019. {},
  1020. };
  1021. MODULE_DEVICE_TABLE(of, octeon_mgmt_match);
  1022. static struct platform_driver octeon_mgmt_driver = {
  1023. .driver = {
  1024. .name = "octeon_mgmt",
  1025. .owner = THIS_MODULE,
  1026. .of_match_table = octeon_mgmt_match,
  1027. },
  1028. .probe = octeon_mgmt_probe,
  1029. .remove = __devexit_p(octeon_mgmt_remove),
  1030. };
  1031. extern void octeon_mdiobus_force_mod_depencency(void);
  1032. static int __init octeon_mgmt_mod_init(void)
  1033. {
  1034. /* Force our mdiobus driver module to be loaded first. */
  1035. octeon_mdiobus_force_mod_depencency();
  1036. return platform_driver_register(&octeon_mgmt_driver);
  1037. }
  1038. static void __exit octeon_mgmt_mod_exit(void)
  1039. {
  1040. platform_driver_unregister(&octeon_mgmt_driver);
  1041. }
  1042. module_init(octeon_mgmt_mod_init);
  1043. module_exit(octeon_mgmt_mod_exit);
  1044. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1045. MODULE_AUTHOR("David Daney");
  1046. MODULE_LICENSE("GPL");
  1047. MODULE_VERSION(DRV_VERSION);