ethernet.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * This file is based on code from OCTEON SDK by Cavium Networks.
  3. *
  4. * Copyright (c) 2003-2007 Cavium Networks
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, Version 2, as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/phy.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/of_net.h>
  19. #include <net/dst.h>
  20. #include <asm/octeon/octeon.h>
  21. #include "ethernet-defines.h"
  22. #include "octeon-ethernet.h"
  23. #include "ethernet-mem.h"
  24. #include "ethernet-rx.h"
  25. #include "ethernet-tx.h"
  26. #include "ethernet-mdio.h"
  27. #include "ethernet-util.h"
  28. #include <asm/octeon/cvmx-pip.h>
  29. #include <asm/octeon/cvmx-pko.h>
  30. #include <asm/octeon/cvmx-fau.h>
  31. #include <asm/octeon/cvmx-ipd.h>
  32. #include <asm/octeon/cvmx-helper.h>
  33. #include <asm/octeon/cvmx-gmxx-defs.h>
  34. #include <asm/octeon/cvmx-smix-defs.h>
  35. static int num_packet_buffers = 1024;
  36. module_param(num_packet_buffers, int, 0444);
  37. MODULE_PARM_DESC(num_packet_buffers, "\n"
  38. "\tNumber of packet buffers to allocate and store in the\n"
  39. "\tFPA. By default, 1024 packet buffers are used.\n");
  40. int pow_receive_group = 15;
  41. module_param(pow_receive_group, int, 0444);
  42. MODULE_PARM_DESC(pow_receive_group, "\n"
  43. "\tPOW group to receive packets from. All ethernet hardware\n"
  44. "\twill be configured to send incoming packets to this POW\n"
  45. "\tgroup. Also any other software can submit packets to this\n"
  46. "\tgroup for the kernel to process.");
  47. int pow_send_group = -1;
  48. module_param(pow_send_group, int, 0644);
  49. MODULE_PARM_DESC(pow_send_group, "\n"
  50. "\tPOW group to send packets to other software on. This\n"
  51. "\tcontrols the creation of the virtual device pow0.\n"
  52. "\talways_use_pow also depends on this value.");
  53. int always_use_pow;
  54. module_param(always_use_pow, int, 0444);
  55. MODULE_PARM_DESC(always_use_pow, "\n"
  56. "\tWhen set, always send to the pow group. This will cause\n"
  57. "\tpackets sent to real ethernet devices to be sent to the\n"
  58. "\tPOW group instead of the hardware. Unless some other\n"
  59. "\tapplication changes the config, packets will still be\n"
  60. "\treceived from the low level hardware. Use this option\n"
  61. "\tto allow a CVMX app to intercept all packets from the\n"
  62. "\tlinux kernel. You must specify pow_send_group along with\n"
  63. "\tthis option.");
  64. char pow_send_list[128] = "";
  65. module_param_string(pow_send_list, pow_send_list, sizeof(pow_send_list), 0444);
  66. MODULE_PARM_DESC(pow_send_list, "\n"
  67. "\tComma separated list of ethernet devices that should use the\n"
  68. "\tPOW for transmit instead of the actual ethernet hardware. This\n"
  69. "\tis a per port version of always_use_pow. always_use_pow takes\n"
  70. "\tprecedence over this list. For example, setting this to\n"
  71. "\t\"eth2,spi3,spi7\" would cause these three devices to transmit\n"
  72. "\tusing the pow_send_group.");
  73. int rx_napi_weight = 32;
  74. module_param(rx_napi_weight, int, 0444);
  75. MODULE_PARM_DESC(rx_napi_weight, "The NAPI WEIGHT parameter.");
  76. /*
  77. * cvm_oct_poll_queue - Workqueue for polling operations.
  78. */
  79. struct workqueue_struct *cvm_oct_poll_queue;
  80. /*
  81. * cvm_oct_poll_queue_stopping - flag to indicate polling should stop.
  82. *
  83. * Set to one right before cvm_oct_poll_queue is destroyed.
  84. */
  85. atomic_t cvm_oct_poll_queue_stopping = ATOMIC_INIT(0);
  86. /*
  87. * Array of every ethernet device owned by this driver indexed by
  88. * the ipd input port number.
  89. */
  90. struct net_device *cvm_oct_device[TOTAL_NUMBER_OF_PORTS];
  91. u64 cvm_oct_tx_poll_interval;
  92. static void cvm_oct_rx_refill_worker(struct work_struct *work);
  93. static DECLARE_DELAYED_WORK(cvm_oct_rx_refill_work, cvm_oct_rx_refill_worker);
  94. static void cvm_oct_rx_refill_worker(struct work_struct *work)
  95. {
  96. /*
  97. * FPA 0 may have been drained, try to refill it if we need
  98. * more than num_packet_buffers / 2, otherwise normal receive
  99. * processing will refill it. If it were drained, no packets
  100. * could be received so cvm_oct_napi_poll would never be
  101. * invoked to do the refill.
  102. */
  103. cvm_oct_rx_refill_pool(num_packet_buffers / 2);
  104. if (!atomic_read(&cvm_oct_poll_queue_stopping))
  105. queue_delayed_work(cvm_oct_poll_queue,
  106. &cvm_oct_rx_refill_work, HZ);
  107. }
  108. static void cvm_oct_periodic_worker(struct work_struct *work)
  109. {
  110. struct octeon_ethernet *priv = container_of(work,
  111. struct octeon_ethernet,
  112. port_periodic_work.work);
  113. if (priv->poll)
  114. priv->poll(cvm_oct_device[priv->port]);
  115. cvm_oct_device[priv->port]->netdev_ops->ndo_get_stats(
  116. cvm_oct_device[priv->port]);
  117. if (!atomic_read(&cvm_oct_poll_queue_stopping))
  118. queue_delayed_work(cvm_oct_poll_queue,
  119. &priv->port_periodic_work, HZ);
  120. }
  121. static void cvm_oct_configure_common_hw(void)
  122. {
  123. /* Setup the FPA */
  124. cvmx_fpa_enable();
  125. cvm_oct_mem_fill_fpa(CVMX_FPA_PACKET_POOL, CVMX_FPA_PACKET_POOL_SIZE,
  126. num_packet_buffers);
  127. cvm_oct_mem_fill_fpa(CVMX_FPA_WQE_POOL, CVMX_FPA_WQE_POOL_SIZE,
  128. num_packet_buffers);
  129. if (CVMX_FPA_OUTPUT_BUFFER_POOL != CVMX_FPA_PACKET_POOL)
  130. cvm_oct_mem_fill_fpa(CVMX_FPA_OUTPUT_BUFFER_POOL,
  131. CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE, 1024);
  132. #ifdef __LITTLE_ENDIAN
  133. {
  134. union cvmx_ipd_ctl_status ipd_ctl_status;
  135. ipd_ctl_status.u64 = cvmx_read_csr(CVMX_IPD_CTL_STATUS);
  136. ipd_ctl_status.s.pkt_lend = 1;
  137. ipd_ctl_status.s.wqe_lend = 1;
  138. cvmx_write_csr(CVMX_IPD_CTL_STATUS, ipd_ctl_status.u64);
  139. }
  140. #endif
  141. cvmx_helper_setup_red(num_packet_buffers / 4, num_packet_buffers / 8);
  142. }
  143. /**
  144. * cvm_oct_free_work- Free a work queue entry
  145. *
  146. * @work_queue_entry: Work queue entry to free
  147. *
  148. * Returns Zero on success, Negative on failure.
  149. */
  150. int cvm_oct_free_work(void *work_queue_entry)
  151. {
  152. cvmx_wqe_t *work = work_queue_entry;
  153. int segments = work->word2.s.bufs;
  154. union cvmx_buf_ptr segment_ptr = work->packet_ptr;
  155. while (segments--) {
  156. union cvmx_buf_ptr next_ptr = *(union cvmx_buf_ptr *)
  157. cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
  158. if (unlikely(!segment_ptr.s.i))
  159. cvmx_fpa_free(cvm_oct_get_buffer_ptr(segment_ptr),
  160. segment_ptr.s.pool,
  161. CVMX_FPA_PACKET_POOL_SIZE / 128);
  162. segment_ptr = next_ptr;
  163. }
  164. cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, 1);
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(cvm_oct_free_work);
  168. /**
  169. * cvm_oct_common_get_stats - get the low level ethernet statistics
  170. * @dev: Device to get the statistics from
  171. *
  172. * Returns Pointer to the statistics
  173. */
  174. static struct net_device_stats *cvm_oct_common_get_stats(struct net_device *dev)
  175. {
  176. cvmx_pip_port_status_t rx_status;
  177. cvmx_pko_port_status_t tx_status;
  178. struct octeon_ethernet *priv = netdev_priv(dev);
  179. if (priv->port < CVMX_PIP_NUM_INPUT_PORTS) {
  180. if (octeon_is_simulation()) {
  181. /* The simulator doesn't support statistics */
  182. memset(&rx_status, 0, sizeof(rx_status));
  183. memset(&tx_status, 0, sizeof(tx_status));
  184. } else {
  185. cvmx_pip_get_port_status(priv->port, 1, &rx_status);
  186. cvmx_pko_get_port_status(priv->port, 1, &tx_status);
  187. }
  188. priv->stats.rx_packets += rx_status.inb_packets;
  189. priv->stats.tx_packets += tx_status.packets;
  190. priv->stats.rx_bytes += rx_status.inb_octets;
  191. priv->stats.tx_bytes += tx_status.octets;
  192. priv->stats.multicast += rx_status.multicast_packets;
  193. priv->stats.rx_crc_errors += rx_status.inb_errors;
  194. priv->stats.rx_frame_errors += rx_status.fcs_align_err_packets;
  195. /*
  196. * The drop counter must be incremented atomically
  197. * since the RX tasklet also increments it.
  198. */
  199. #ifdef CONFIG_64BIT
  200. atomic64_add(rx_status.dropped_packets,
  201. (atomic64_t *)&priv->stats.rx_dropped);
  202. #else
  203. atomic_add(rx_status.dropped_packets,
  204. (atomic_t *)&priv->stats.rx_dropped);
  205. #endif
  206. }
  207. return &priv->stats;
  208. }
  209. /**
  210. * cvm_oct_common_change_mtu - change the link MTU
  211. * @dev: Device to change
  212. * @new_mtu: The new MTU
  213. *
  214. * Returns Zero on success
  215. */
  216. static int cvm_oct_common_change_mtu(struct net_device *dev, int new_mtu)
  217. {
  218. struct octeon_ethernet *priv = netdev_priv(dev);
  219. int interface = INTERFACE(priv->port);
  220. int index = INDEX(priv->port);
  221. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  222. int vlan_bytes = 4;
  223. #else
  224. int vlan_bytes = 0;
  225. #endif
  226. /*
  227. * Limit the MTU to make sure the ethernet packets are between
  228. * 64 bytes and 65535 bytes.
  229. */
  230. if ((new_mtu + 14 + 4 + vlan_bytes < 64)
  231. || (new_mtu + 14 + 4 + vlan_bytes > 65392)) {
  232. pr_err("MTU must be between %d and %d.\n",
  233. 64 - 14 - 4 - vlan_bytes, 65392 - 14 - 4 - vlan_bytes);
  234. return -EINVAL;
  235. }
  236. dev->mtu = new_mtu;
  237. if ((interface < 2)
  238. && (cvmx_helper_interface_get_mode(interface) !=
  239. CVMX_HELPER_INTERFACE_MODE_SPI)) {
  240. /* Add ethernet header and FCS, and VLAN if configured. */
  241. int max_packet = new_mtu + 14 + 4 + vlan_bytes;
  242. if (OCTEON_IS_MODEL(OCTEON_CN3XXX)
  243. || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
  244. /* Signal errors on packets larger than the MTU */
  245. cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(index, interface),
  246. max_packet);
  247. } else {
  248. /*
  249. * Set the hardware to truncate packets larger
  250. * than the MTU and smaller the 64 bytes.
  251. */
  252. union cvmx_pip_frm_len_chkx frm_len_chk;
  253. frm_len_chk.u64 = 0;
  254. frm_len_chk.s.minlen = 64;
  255. frm_len_chk.s.maxlen = max_packet;
  256. cvmx_write_csr(CVMX_PIP_FRM_LEN_CHKX(interface),
  257. frm_len_chk.u64);
  258. }
  259. /*
  260. * Set the hardware to truncate packets larger than
  261. * the MTU. The jabber register must be set to a
  262. * multiple of 8 bytes, so round up.
  263. */
  264. cvmx_write_csr(CVMX_GMXX_RXX_JABBER(index, interface),
  265. (max_packet + 7) & ~7u);
  266. }
  267. return 0;
  268. }
  269. /**
  270. * cvm_oct_common_set_multicast_list - set the multicast list
  271. * @dev: Device to work on
  272. */
  273. static void cvm_oct_common_set_multicast_list(struct net_device *dev)
  274. {
  275. union cvmx_gmxx_prtx_cfg gmx_cfg;
  276. struct octeon_ethernet *priv = netdev_priv(dev);
  277. int interface = INTERFACE(priv->port);
  278. int index = INDEX(priv->port);
  279. if ((interface < 2)
  280. && (cvmx_helper_interface_get_mode(interface) !=
  281. CVMX_HELPER_INTERFACE_MODE_SPI)) {
  282. union cvmx_gmxx_rxx_adr_ctl control;
  283. control.u64 = 0;
  284. control.s.bcst = 1; /* Allow broadcast MAC addresses */
  285. if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI) ||
  286. (dev->flags & IFF_PROMISC))
  287. /* Force accept multicast packets */
  288. control.s.mcst = 2;
  289. else
  290. /* Force reject multicast packets */
  291. control.s.mcst = 1;
  292. if (dev->flags & IFF_PROMISC)
  293. /*
  294. * Reject matches if promisc. Since CAM is
  295. * shut off, should accept everything.
  296. */
  297. control.s.cam_mode = 0;
  298. else
  299. /* Filter packets based on the CAM */
  300. control.s.cam_mode = 1;
  301. gmx_cfg.u64 =
  302. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  303. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
  304. gmx_cfg.u64 & ~1ull);
  305. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CTL(index, interface),
  306. control.u64);
  307. if (dev->flags & IFF_PROMISC)
  308. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN
  309. (index, interface), 0);
  310. else
  311. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN
  312. (index, interface), 1);
  313. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
  314. gmx_cfg.u64);
  315. }
  316. }
  317. static int cvm_oct_set_mac_filter(struct net_device *dev)
  318. {
  319. struct octeon_ethernet *priv = netdev_priv(dev);
  320. union cvmx_gmxx_prtx_cfg gmx_cfg;
  321. int interface = INTERFACE(priv->port);
  322. int index = INDEX(priv->port);
  323. if ((interface < 2)
  324. && (cvmx_helper_interface_get_mode(interface) !=
  325. CVMX_HELPER_INTERFACE_MODE_SPI)) {
  326. int i;
  327. u8 *ptr = dev->dev_addr;
  328. u64 mac = 0;
  329. for (i = 0; i < 6; i++)
  330. mac = (mac << 8) | (u64)ptr[i];
  331. gmx_cfg.u64 =
  332. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  333. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
  334. gmx_cfg.u64 & ~1ull);
  335. cvmx_write_csr(CVMX_GMXX_SMACX(index, interface), mac);
  336. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM0(index, interface),
  337. ptr[0]);
  338. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM1(index, interface),
  339. ptr[1]);
  340. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM2(index, interface),
  341. ptr[2]);
  342. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM3(index, interface),
  343. ptr[3]);
  344. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM4(index, interface),
  345. ptr[4]);
  346. cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM5(index, interface),
  347. ptr[5]);
  348. cvm_oct_common_set_multicast_list(dev);
  349. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
  350. gmx_cfg.u64);
  351. }
  352. return 0;
  353. }
  354. /**
  355. * cvm_oct_common_set_mac_address - set the hardware MAC address for a device
  356. * @dev: The device in question.
  357. * @addr: Socket address.
  358. *
  359. * Returns Zero on success
  360. */
  361. static int cvm_oct_common_set_mac_address(struct net_device *dev, void *addr)
  362. {
  363. int r = eth_mac_addr(dev, addr);
  364. if (r)
  365. return r;
  366. return cvm_oct_set_mac_filter(dev);
  367. }
  368. /**
  369. * cvm_oct_common_init - per network device initialization
  370. * @dev: Device to initialize
  371. *
  372. * Returns Zero on success
  373. */
  374. int cvm_oct_common_init(struct net_device *dev)
  375. {
  376. struct octeon_ethernet *priv = netdev_priv(dev);
  377. const u8 *mac = NULL;
  378. if (priv->of_node)
  379. mac = of_get_mac_address(priv->of_node);
  380. if (mac)
  381. ether_addr_copy(dev->dev_addr, mac);
  382. else
  383. eth_hw_addr_random(dev);
  384. /*
  385. * Force the interface to use the POW send if always_use_pow
  386. * was specified or it is in the pow send list.
  387. */
  388. if ((pow_send_group != -1)
  389. && (always_use_pow || strstr(pow_send_list, dev->name)))
  390. priv->queue = -1;
  391. if (priv->queue != -1)
  392. dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
  393. /* We do our own locking, Linux doesn't need to */
  394. dev->features |= NETIF_F_LLTX;
  395. dev->ethtool_ops = &cvm_oct_ethtool_ops;
  396. cvm_oct_set_mac_filter(dev);
  397. dev->netdev_ops->ndo_change_mtu(dev, dev->mtu);
  398. /*
  399. * Zero out stats for port so we won't mistakenly show
  400. * counters from the bootloader.
  401. */
  402. memset(dev->netdev_ops->ndo_get_stats(dev), 0,
  403. sizeof(struct net_device_stats));
  404. if (dev->netdev_ops->ndo_stop)
  405. dev->netdev_ops->ndo_stop(dev);
  406. return 0;
  407. }
  408. void cvm_oct_common_uninit(struct net_device *dev)
  409. {
  410. struct octeon_ethernet *priv = netdev_priv(dev);
  411. if (priv->phydev)
  412. phy_disconnect(priv->phydev);
  413. }
  414. int cvm_oct_common_open(struct net_device *dev,
  415. void (*link_poll)(struct net_device *))
  416. {
  417. union cvmx_gmxx_prtx_cfg gmx_cfg;
  418. struct octeon_ethernet *priv = netdev_priv(dev);
  419. int interface = INTERFACE(priv->port);
  420. int index = INDEX(priv->port);
  421. cvmx_helper_link_info_t link_info;
  422. int rv;
  423. rv = cvm_oct_phy_setup_device(dev);
  424. if (rv)
  425. return rv;
  426. gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  427. gmx_cfg.s.en = 1;
  428. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
  429. if (octeon_is_simulation())
  430. return 0;
  431. if (priv->phydev) {
  432. int r = phy_read_status(priv->phydev);
  433. if (r == 0 && priv->phydev->link == 0)
  434. netif_carrier_off(dev);
  435. cvm_oct_adjust_link(dev);
  436. } else {
  437. link_info = cvmx_helper_link_get(priv->port);
  438. if (!link_info.s.link_up)
  439. netif_carrier_off(dev);
  440. priv->poll = link_poll;
  441. link_poll(dev);
  442. }
  443. return 0;
  444. }
  445. void cvm_oct_link_poll(struct net_device *dev)
  446. {
  447. struct octeon_ethernet *priv = netdev_priv(dev);
  448. cvmx_helper_link_info_t link_info;
  449. link_info = cvmx_helper_link_get(priv->port);
  450. if (link_info.u64 == priv->link_info)
  451. return;
  452. link_info = cvmx_helper_link_autoconf(priv->port);
  453. priv->link_info = link_info.u64;
  454. if (link_info.s.link_up) {
  455. if (!netif_carrier_ok(dev))
  456. netif_carrier_on(dev);
  457. } else if (netif_carrier_ok(dev)) {
  458. netif_carrier_off(dev);
  459. }
  460. cvm_oct_note_carrier(priv, link_info);
  461. }
  462. static int cvm_oct_xaui_open(struct net_device *dev)
  463. {
  464. return cvm_oct_common_open(dev, cvm_oct_link_poll);
  465. }
  466. static const struct net_device_ops cvm_oct_npi_netdev_ops = {
  467. .ndo_init = cvm_oct_common_init,
  468. .ndo_uninit = cvm_oct_common_uninit,
  469. .ndo_start_xmit = cvm_oct_xmit,
  470. .ndo_set_rx_mode = cvm_oct_common_set_multicast_list,
  471. .ndo_set_mac_address = cvm_oct_common_set_mac_address,
  472. .ndo_do_ioctl = cvm_oct_ioctl,
  473. .ndo_change_mtu = cvm_oct_common_change_mtu,
  474. .ndo_get_stats = cvm_oct_common_get_stats,
  475. #ifdef CONFIG_NET_POLL_CONTROLLER
  476. .ndo_poll_controller = cvm_oct_poll_controller,
  477. #endif
  478. };
  479. static const struct net_device_ops cvm_oct_xaui_netdev_ops = {
  480. .ndo_init = cvm_oct_common_init,
  481. .ndo_uninit = cvm_oct_common_uninit,
  482. .ndo_open = cvm_oct_xaui_open,
  483. .ndo_stop = cvm_oct_common_stop,
  484. .ndo_start_xmit = cvm_oct_xmit,
  485. .ndo_set_rx_mode = cvm_oct_common_set_multicast_list,
  486. .ndo_set_mac_address = cvm_oct_common_set_mac_address,
  487. .ndo_do_ioctl = cvm_oct_ioctl,
  488. .ndo_change_mtu = cvm_oct_common_change_mtu,
  489. .ndo_get_stats = cvm_oct_common_get_stats,
  490. #ifdef CONFIG_NET_POLL_CONTROLLER
  491. .ndo_poll_controller = cvm_oct_poll_controller,
  492. #endif
  493. };
  494. static const struct net_device_ops cvm_oct_sgmii_netdev_ops = {
  495. .ndo_init = cvm_oct_sgmii_init,
  496. .ndo_uninit = cvm_oct_common_uninit,
  497. .ndo_open = cvm_oct_sgmii_open,
  498. .ndo_stop = cvm_oct_common_stop,
  499. .ndo_start_xmit = cvm_oct_xmit,
  500. .ndo_set_rx_mode = cvm_oct_common_set_multicast_list,
  501. .ndo_set_mac_address = cvm_oct_common_set_mac_address,
  502. .ndo_do_ioctl = cvm_oct_ioctl,
  503. .ndo_change_mtu = cvm_oct_common_change_mtu,
  504. .ndo_get_stats = cvm_oct_common_get_stats,
  505. #ifdef CONFIG_NET_POLL_CONTROLLER
  506. .ndo_poll_controller = cvm_oct_poll_controller,
  507. #endif
  508. };
  509. static const struct net_device_ops cvm_oct_spi_netdev_ops = {
  510. .ndo_init = cvm_oct_spi_init,
  511. .ndo_uninit = cvm_oct_spi_uninit,
  512. .ndo_start_xmit = cvm_oct_xmit,
  513. .ndo_set_rx_mode = cvm_oct_common_set_multicast_list,
  514. .ndo_set_mac_address = cvm_oct_common_set_mac_address,
  515. .ndo_do_ioctl = cvm_oct_ioctl,
  516. .ndo_change_mtu = cvm_oct_common_change_mtu,
  517. .ndo_get_stats = cvm_oct_common_get_stats,
  518. #ifdef CONFIG_NET_POLL_CONTROLLER
  519. .ndo_poll_controller = cvm_oct_poll_controller,
  520. #endif
  521. };
  522. static const struct net_device_ops cvm_oct_rgmii_netdev_ops = {
  523. .ndo_init = cvm_oct_rgmii_init,
  524. .ndo_uninit = cvm_oct_rgmii_uninit,
  525. .ndo_open = cvm_oct_rgmii_open,
  526. .ndo_stop = cvm_oct_common_stop,
  527. .ndo_start_xmit = cvm_oct_xmit,
  528. .ndo_set_rx_mode = cvm_oct_common_set_multicast_list,
  529. .ndo_set_mac_address = cvm_oct_common_set_mac_address,
  530. .ndo_do_ioctl = cvm_oct_ioctl,
  531. .ndo_change_mtu = cvm_oct_common_change_mtu,
  532. .ndo_get_stats = cvm_oct_common_get_stats,
  533. #ifdef CONFIG_NET_POLL_CONTROLLER
  534. .ndo_poll_controller = cvm_oct_poll_controller,
  535. #endif
  536. };
  537. static const struct net_device_ops cvm_oct_pow_netdev_ops = {
  538. .ndo_init = cvm_oct_common_init,
  539. .ndo_start_xmit = cvm_oct_xmit_pow,
  540. .ndo_set_rx_mode = cvm_oct_common_set_multicast_list,
  541. .ndo_set_mac_address = cvm_oct_common_set_mac_address,
  542. .ndo_do_ioctl = cvm_oct_ioctl,
  543. .ndo_change_mtu = cvm_oct_common_change_mtu,
  544. .ndo_get_stats = cvm_oct_common_get_stats,
  545. #ifdef CONFIG_NET_POLL_CONTROLLER
  546. .ndo_poll_controller = cvm_oct_poll_controller,
  547. #endif
  548. };
  549. static struct device_node *cvm_oct_of_get_child(
  550. const struct device_node *parent, int reg_val)
  551. {
  552. struct device_node *node = NULL;
  553. int size;
  554. const __be32 *addr;
  555. for (;;) {
  556. node = of_get_next_child(parent, node);
  557. if (!node)
  558. break;
  559. addr = of_get_property(node, "reg", &size);
  560. if (addr && (be32_to_cpu(*addr) == reg_val))
  561. break;
  562. }
  563. return node;
  564. }
  565. static struct device_node *cvm_oct_node_for_port(struct device_node *pip,
  566. int interface, int port)
  567. {
  568. struct device_node *ni, *np;
  569. ni = cvm_oct_of_get_child(pip, interface);
  570. if (!ni)
  571. return NULL;
  572. np = cvm_oct_of_get_child(ni, port);
  573. of_node_put(ni);
  574. return np;
  575. }
  576. static int cvm_oct_probe(struct platform_device *pdev)
  577. {
  578. int num_interfaces;
  579. int interface;
  580. int fau = FAU_NUM_PACKET_BUFFERS_TO_FREE;
  581. int qos;
  582. struct device_node *pip;
  583. octeon_mdiobus_force_mod_depencency();
  584. pip = pdev->dev.of_node;
  585. if (!pip) {
  586. pr_err("Error: No 'pip' in /aliases\n");
  587. return -EINVAL;
  588. }
  589. cvm_oct_poll_queue = create_singlethread_workqueue("octeon-ethernet");
  590. if (!cvm_oct_poll_queue) {
  591. pr_err("octeon-ethernet: Cannot create workqueue");
  592. return -ENOMEM;
  593. }
  594. cvm_oct_configure_common_hw();
  595. cvmx_helper_initialize_packet_io_global();
  596. /* Change the input group for all ports before input is enabled */
  597. num_interfaces = cvmx_helper_get_number_of_interfaces();
  598. for (interface = 0; interface < num_interfaces; interface++) {
  599. int num_ports = cvmx_helper_ports_on_interface(interface);
  600. int port;
  601. for (port = cvmx_helper_get_ipd_port(interface, 0);
  602. port < cvmx_helper_get_ipd_port(interface, num_ports);
  603. port++) {
  604. union cvmx_pip_prt_tagx pip_prt_tagx;
  605. pip_prt_tagx.u64 =
  606. cvmx_read_csr(CVMX_PIP_PRT_TAGX(port));
  607. pip_prt_tagx.s.grp = pow_receive_group;
  608. cvmx_write_csr(CVMX_PIP_PRT_TAGX(port),
  609. pip_prt_tagx.u64);
  610. }
  611. }
  612. cvmx_helper_ipd_and_packet_input_enable();
  613. memset(cvm_oct_device, 0, sizeof(cvm_oct_device));
  614. /*
  615. * Initialize the FAU used for counting packet buffers that
  616. * need to be freed.
  617. */
  618. cvmx_fau_atomic_write32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
  619. /* Initialize the FAU used for counting tx SKBs that need to be freed */
  620. cvmx_fau_atomic_write32(FAU_TOTAL_TX_TO_CLEAN, 0);
  621. if ((pow_send_group != -1)) {
  622. struct net_device *dev;
  623. pr_info("\tConfiguring device for POW only access\n");
  624. dev = alloc_etherdev(sizeof(struct octeon_ethernet));
  625. if (dev) {
  626. /* Initialize the device private structure. */
  627. struct octeon_ethernet *priv = netdev_priv(dev);
  628. dev->netdev_ops = &cvm_oct_pow_netdev_ops;
  629. priv->imode = CVMX_HELPER_INTERFACE_MODE_DISABLED;
  630. priv->port = CVMX_PIP_NUM_INPUT_PORTS;
  631. priv->queue = -1;
  632. strcpy(dev->name, "pow%d");
  633. for (qos = 0; qos < 16; qos++)
  634. skb_queue_head_init(&priv->tx_free_list[qos]);
  635. if (register_netdev(dev) < 0) {
  636. pr_err("Failed to register ethernet device for POW\n");
  637. free_netdev(dev);
  638. } else {
  639. cvm_oct_device[CVMX_PIP_NUM_INPUT_PORTS] = dev;
  640. pr_info("%s: POW send group %d, receive group %d\n",
  641. dev->name, pow_send_group,
  642. pow_receive_group);
  643. }
  644. } else {
  645. pr_err("Failed to allocate ethernet device for POW\n");
  646. }
  647. }
  648. num_interfaces = cvmx_helper_get_number_of_interfaces();
  649. for (interface = 0; interface < num_interfaces; interface++) {
  650. cvmx_helper_interface_mode_t imode =
  651. cvmx_helper_interface_get_mode(interface);
  652. int num_ports = cvmx_helper_ports_on_interface(interface);
  653. int port;
  654. int port_index;
  655. for (port_index = 0,
  656. port = cvmx_helper_get_ipd_port(interface, 0);
  657. port < cvmx_helper_get_ipd_port(interface, num_ports);
  658. port_index++, port++) {
  659. struct octeon_ethernet *priv;
  660. struct net_device *dev =
  661. alloc_etherdev(sizeof(struct octeon_ethernet));
  662. if (!dev) {
  663. pr_err("Failed to allocate ethernet device for port %d\n",
  664. port);
  665. continue;
  666. }
  667. /* Initialize the device private structure. */
  668. priv = netdev_priv(dev);
  669. priv->netdev = dev;
  670. priv->of_node = cvm_oct_node_for_port(pip, interface,
  671. port_index);
  672. INIT_DELAYED_WORK(&priv->port_periodic_work,
  673. cvm_oct_periodic_worker);
  674. priv->imode = imode;
  675. priv->port = port;
  676. priv->queue = cvmx_pko_get_base_queue(priv->port);
  677. priv->fau = fau - cvmx_pko_get_num_queues(port) * 4;
  678. for (qos = 0; qos < 16; qos++)
  679. skb_queue_head_init(&priv->tx_free_list[qos]);
  680. for (qos = 0; qos < cvmx_pko_get_num_queues(port);
  681. qos++)
  682. cvmx_fau_atomic_write32(priv->fau + qos * 4, 0);
  683. switch (priv->imode) {
  684. /* These types don't support ports to IPD/PKO */
  685. case CVMX_HELPER_INTERFACE_MODE_DISABLED:
  686. case CVMX_HELPER_INTERFACE_MODE_PCIE:
  687. case CVMX_HELPER_INTERFACE_MODE_PICMG:
  688. break;
  689. case CVMX_HELPER_INTERFACE_MODE_NPI:
  690. dev->netdev_ops = &cvm_oct_npi_netdev_ops;
  691. strcpy(dev->name, "npi%d");
  692. break;
  693. case CVMX_HELPER_INTERFACE_MODE_XAUI:
  694. dev->netdev_ops = &cvm_oct_xaui_netdev_ops;
  695. strcpy(dev->name, "xaui%d");
  696. break;
  697. case CVMX_HELPER_INTERFACE_MODE_LOOP:
  698. dev->netdev_ops = &cvm_oct_npi_netdev_ops;
  699. strcpy(dev->name, "loop%d");
  700. break;
  701. case CVMX_HELPER_INTERFACE_MODE_SGMII:
  702. dev->netdev_ops = &cvm_oct_sgmii_netdev_ops;
  703. strcpy(dev->name, "eth%d");
  704. break;
  705. case CVMX_HELPER_INTERFACE_MODE_SPI:
  706. dev->netdev_ops = &cvm_oct_spi_netdev_ops;
  707. strcpy(dev->name, "spi%d");
  708. break;
  709. case CVMX_HELPER_INTERFACE_MODE_RGMII:
  710. case CVMX_HELPER_INTERFACE_MODE_GMII:
  711. dev->netdev_ops = &cvm_oct_rgmii_netdev_ops;
  712. strcpy(dev->name, "eth%d");
  713. break;
  714. }
  715. if (!dev->netdev_ops) {
  716. free_netdev(dev);
  717. } else if (register_netdev(dev) < 0) {
  718. pr_err("Failed to register ethernet device for interface %d, port %d\n",
  719. interface, priv->port);
  720. free_netdev(dev);
  721. } else {
  722. cvm_oct_device[priv->port] = dev;
  723. fau -=
  724. cvmx_pko_get_num_queues(priv->port) *
  725. sizeof(u32);
  726. queue_delayed_work(cvm_oct_poll_queue,
  727. &priv->port_periodic_work, HZ);
  728. }
  729. }
  730. }
  731. cvm_oct_tx_initialize();
  732. cvm_oct_rx_initialize();
  733. /*
  734. * 150 uS: about 10 1500-byte packets at 1GE.
  735. */
  736. cvm_oct_tx_poll_interval = 150 * (octeon_get_clock_rate() / 1000000);
  737. queue_delayed_work(cvm_oct_poll_queue, &cvm_oct_rx_refill_work, HZ);
  738. return 0;
  739. }
  740. static int cvm_oct_remove(struct platform_device *pdev)
  741. {
  742. int port;
  743. /* Disable POW interrupt */
  744. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  745. cvmx_write_csr(CVMX_SSO_WQ_INT_THRX(pow_receive_group), 0);
  746. else
  747. cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group), 0);
  748. cvmx_ipd_disable();
  749. /* Free the interrupt handler */
  750. free_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group, cvm_oct_device);
  751. atomic_inc_return(&cvm_oct_poll_queue_stopping);
  752. cancel_delayed_work_sync(&cvm_oct_rx_refill_work);
  753. cvm_oct_rx_shutdown();
  754. cvm_oct_tx_shutdown();
  755. cvmx_pko_disable();
  756. /* Free the ethernet devices */
  757. for (port = 0; port < TOTAL_NUMBER_OF_PORTS; port++) {
  758. if (cvm_oct_device[port]) {
  759. struct net_device *dev = cvm_oct_device[port];
  760. struct octeon_ethernet *priv = netdev_priv(dev);
  761. cancel_delayed_work_sync(&priv->port_periodic_work);
  762. cvm_oct_tx_shutdown_dev(dev);
  763. unregister_netdev(dev);
  764. free_netdev(dev);
  765. cvm_oct_device[port] = NULL;
  766. }
  767. }
  768. destroy_workqueue(cvm_oct_poll_queue);
  769. cvmx_pko_shutdown();
  770. cvmx_ipd_free_ptr();
  771. /* Free the HW pools */
  772. cvm_oct_mem_empty_fpa(CVMX_FPA_PACKET_POOL, CVMX_FPA_PACKET_POOL_SIZE,
  773. num_packet_buffers);
  774. cvm_oct_mem_empty_fpa(CVMX_FPA_WQE_POOL, CVMX_FPA_WQE_POOL_SIZE,
  775. num_packet_buffers);
  776. if (CVMX_FPA_OUTPUT_BUFFER_POOL != CVMX_FPA_PACKET_POOL)
  777. cvm_oct_mem_empty_fpa(CVMX_FPA_OUTPUT_BUFFER_POOL,
  778. CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE, 128);
  779. return 0;
  780. }
  781. static const struct of_device_id cvm_oct_match[] = {
  782. {
  783. .compatible = "cavium,octeon-3860-pip",
  784. },
  785. {},
  786. };
  787. MODULE_DEVICE_TABLE(of, cvm_oct_match);
  788. static struct platform_driver cvm_oct_driver = {
  789. .probe = cvm_oct_probe,
  790. .remove = cvm_oct_remove,
  791. .driver = {
  792. .name = KBUILD_MODNAME,
  793. .of_match_table = cvm_oct_match,
  794. },
  795. };
  796. module_platform_driver(cvm_oct_driver);
  797. MODULE_LICENSE("GPL");
  798. MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
  799. MODULE_DESCRIPTION("Cavium Networks Octeon ethernet driver.");