ethernet.c 28 KB

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