dsa.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright 2019-2021 NXP Semiconductors
  4. */
  5. #ifndef __DSA_H__
  6. #define __DSA_H__
  7. #include <phy.h>
  8. #include <net.h>
  9. /**
  10. * DSA stands for Distributed Switch Architecture and it is infrastructure
  11. * intended to support drivers for Switches that rely on an intermediary
  12. * Ethernet device for I/O. These switches may support cascading allowing
  13. * them to be arranged as a tree.
  14. * DSA is documented in detail in the Linux kernel documentation under
  15. * Documentation/networking/dsa/dsa.txt
  16. * The network layout of such a switch is shown below:
  17. *
  18. * |------|
  19. * | eth0 | <--- master eth device (regular eth driver)
  20. * |------|
  21. * ^ |
  22. * tag added by switch -->| |
  23. * | |
  24. * | |<-- tag added by DSA driver
  25. * | v
  26. * |--------------------------------------|
  27. * | | CPU port | | <-- DSA (switch) device
  28. * | ------------ | (DSA driver)
  29. * | _________ _________ _________ |
  30. * | | port0 | | port1 | ... | portn | | <-- ports as eth devices
  31. * |-+-------+--+-------+-------+-------+-| ('dsa-port' eth driver)
  32. *
  33. * In U-Boot the intent is to allow access to front panel ports (shown at the
  34. * bottom of the picture) through the master Ethernet dev (eth0 in the picture).
  35. * Front panel ports are presented as regular Ethernet devices in U-Boot and
  36. * they are expected to support the typical networking commands.
  37. * In general DSA switches require the use of tags, extra headers added both by
  38. * software on Tx and by the switch on Rx. These tags carry at a minimum port
  39. * information and switch information for cascaded set-ups.
  40. * In U-Boot these tags are inserted and parsed by the DSA switch driver, the
  41. * class code helps with headroom/tailroom for the extra headers.
  42. *
  43. * TODO:
  44. * - handle switch cascading, for now U-Boot only supports stand-alone switches.
  45. * - Add support to probe DSA switches connected to a MDIO bus, this is needed
  46. * to convert switch drivers that are now under drivers/net/phy.
  47. */
  48. #define DSA_PORT_NAME_LENGTH 16
  49. /* Maximum number of ports each DSA device can have */
  50. #define DSA_MAX_PORTS 12
  51. /**
  52. * struct dsa_ops - DSA operations
  53. *
  54. * @port_enable: Initialize a switch port for I/O.
  55. * @port_disable: Disable I/O for a port.
  56. * @xmit: Insert the DSA tag for transmission.
  57. * DSA drivers receive a copy of the packet with headroom and
  58. * tailroom reserved and set to 0. 'packet' points to headroom
  59. * and 'length' is updated to include both head and tailroom.
  60. * @rcv: Process the DSA tag on reception and return the port index
  61. * from the h/w provided tag. Return the index via 'portp'.
  62. * 'packet' and 'length' describe the frame as received from
  63. * master including any additional headers.
  64. */
  65. struct dsa_ops {
  66. int (*port_enable)(struct udevice *dev, int port,
  67. struct phy_device *phy);
  68. void (*port_disable)(struct udevice *dev, int port,
  69. struct phy_device *phy);
  70. int (*xmit)(struct udevice *dev, int port, void *packet, int length);
  71. int (*rcv)(struct udevice *dev, int *portp, void *packet, int length);
  72. };
  73. #define dsa_get_ops(dev) ((struct dsa_ops *)(dev)->driver->ops)
  74. /**
  75. * struct dsa_port_pdata - DSA port platform data
  76. *
  77. * @phy: PHY device associated with this port.
  78. * The uclass code attempts to set this field for all ports except CPU
  79. * port, based on DT information. It may be NULL.
  80. * @index: Port index in the DSA switch, set by the uclass code.
  81. * @name: Name of the port Eth device. If a label property is present in the
  82. * port DT node, it is used as name.
  83. */
  84. struct dsa_port_pdata {
  85. struct phy_device *phy;
  86. u32 index;
  87. char name[DSA_PORT_NAME_LENGTH];
  88. };
  89. /**
  90. * struct dsa_pdata - Per-device platform data for DSA DM
  91. *
  92. * @num_ports: Number of ports the device has, must be <= DSA_MAX_PORTS.
  93. * This number is extracted from the DT 'ports' node of this
  94. * DSA device, and it counts the CPU port and all the other
  95. * port subnodes including the disabled ones.
  96. * @cpu_port: Index of the switch port linked to the master Ethernet.
  97. * The uclass code sets this based on DT information.
  98. * @master_node: OF node of the host Ethernet controller.
  99. * @cpu_port_node: DT node of the switch's CPU port.
  100. */
  101. struct dsa_pdata {
  102. int num_ports;
  103. u32 cpu_port;
  104. ofnode master_node;
  105. ofnode cpu_port_node;
  106. };
  107. /**
  108. * dsa_set_tagging() - Configure the headroom and/or tailroom sizes
  109. *
  110. * The DSA class code allocates headroom and tailroom on Tx before
  111. * calling the DSA driver's xmit function.
  112. * All drivers must call this at probe time.
  113. *
  114. * @dev: DSA device pointer
  115. * @headroom: Size, in bytes, of headroom needed for the DSA tag.
  116. * @tailroom: Size, in bytes, of tailroom needed for the DSA tag.
  117. * Total headroom and tailroom size should not exceed
  118. * DSA_MAX_OVR.
  119. * @return 0 if OK, -ve on error
  120. */
  121. int dsa_set_tagging(struct udevice *dev, ushort headroom, ushort tailroom);
  122. /* DSA helpers */
  123. /**
  124. * dsa_get_master() - Return a reference to the master Ethernet device
  125. *
  126. * Can be called at driver probe time or later.
  127. *
  128. * @dev: DSA device pointer
  129. * @return Master Eth 'udevice' pointer if OK, NULL on error
  130. */
  131. struct udevice *dsa_get_master(struct udevice *dev);
  132. /**
  133. * dsa_port_get_pdata() - Helper that returns the platdata of an active
  134. * (non-CPU) DSA port device.
  135. *
  136. * Can be called at driver probe time or later.
  137. *
  138. * @pdev: DSA port device pointer
  139. * @return 'dsa_port_pdata' pointer if OK, NULL on error
  140. */
  141. static inline struct dsa_port_pdata *
  142. dsa_port_get_pdata(struct udevice *pdev)
  143. {
  144. struct eth_pdata *eth = dev_get_plat(pdev);
  145. if (!eth)
  146. return NULL;
  147. return eth->priv_pdata;
  148. }
  149. #endif /* __DSA_H__ */