ethernet.c 27 KB

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