Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/phylink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "sfp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) struct sfp_quirk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	const char *vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	const char *part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	void (*modes)(const struct sfp_eeprom_id *id, unsigned long *modes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * struct sfp_bus - internal representation of a sfp bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct sfp_bus {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	/* private: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct kref kref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct fwnode_handle *fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	const struct sfp_socket_ops *socket_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct device *sfp_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct sfp *sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	const struct sfp_quirk *sfp_quirk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	const struct sfp_upstream_ops *upstream_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	void *upstream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct phy_device *phydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	bool registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	bool started;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 				unsigned long *modes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	phylink_set(modes, 2500baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				      unsigned long *modes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/* Ubiquiti U-Fiber Instant module claims that support all transceiver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * types including 10G Ethernet which is not truth. So clear all claimed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 * modes and set only one mode which module supports: 1000baseX_Full.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	phylink_zero(modes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	phylink_set(modes, 1000baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static const struct sfp_quirk sfp_quirks[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		// Alcatel Lucent G-010S-P can operate at 2500base-X, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		// incorrectly report 2500MBd NRZ in their EEPROM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		.vendor = "ALCATELLUCENT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		.part = "G010SP",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		.modes = sfp_quirk_2500basex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		// Alcatel Lucent G-010S-A can operate at 2500base-X, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		// report 3.2GBd NRZ in their EEPROM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		.vendor = "ALCATELLUCENT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		.part = "3FE46541AA",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		.modes = sfp_quirk_2500basex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		// Huawei MA5671A can operate at 2500base-X, but report 1.2GBd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		// NRZ in their EEPROM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		.vendor = "HUAWEI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		.part = "MA5671A",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		.modes = sfp_quirk_2500basex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		.vendor = "UBNT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		.part = "UF-INSTANT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		.modes = sfp_quirk_ubnt_uf_instant,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static size_t sfp_strlen(const char *str, size_t maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	size_t size, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* Trailing characters should be filled with space chars */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	for (i = 0, size = 0; i < maxlen; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (str[i] != ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			size = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static bool sfp_match(const char *qs, const char *str, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!qs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (strlen(qs) != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return !strncmp(qs, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	const struct sfp_quirk *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	size_t vs, ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (sfp_match(q->vendor, id->base.vendor_name, vs) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		    sfp_match(q->part, id->base.vendor_pn, ps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			return q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @id: a pointer to the module's &struct sfp_eeprom_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * @support: optional pointer to an array of unsigned long for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  *   ethtool support mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * Parse the EEPROM identification given in @id, and return one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * the connector type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * If the port type is not known, returns %PORT_OTHER.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		   unsigned long *support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	int port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* port is the physical connector, set this from the connector field. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	switch (id->base.connector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	case SFF8024_CONNECTOR_SC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	case SFF8024_CONNECTOR_FIBERJACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	case SFF8024_CONNECTOR_LC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	case SFF8024_CONNECTOR_MT_RJ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	case SFF8024_CONNECTOR_MU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	case SFF8024_CONNECTOR_MPO_1X12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	case SFF8024_CONNECTOR_MPO_2X16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		port = PORT_FIBRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	case SFF8024_CONNECTOR_RJ45:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		port = PORT_TP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	case SFF8024_CONNECTOR_COPPER_PIGTAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		port = PORT_DA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	case SFF8024_CONNECTOR_UNSPEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		if (id->base.e1000_base_t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			port = PORT_TP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case SFF8024_CONNECTOR_SG: /* guess */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	case SFF8024_CONNECTOR_HSSDC_II:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	case SFF8024_CONNECTOR_NOSEPARATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	case SFF8024_CONNECTOR_MXC_2X16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		port = PORT_OTHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			 id->base.connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		port = PORT_OTHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (support) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		switch (port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		case PORT_FIBRE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			phylink_set(support, FIBRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		case PORT_TP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			phylink_set(support, TP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) EXPORT_SYMBOL_GPL(sfp_parse_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * sfp_may_have_phy() - indicate whether the module may have a PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * @id: a pointer to the module's &struct sfp_eeprom_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * Parse the EEPROM identification given in @id, and return whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  * this module may have a PHY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (id->base.e1000_base_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		switch (id->base.extended_cc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		case SFF8024_ECC_10GBASE_T_SFI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		case SFF8024_ECC_10GBASE_T_SR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		case SFF8024_ECC_5GBASE_T:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		case SFF8024_ECC_2_5GBASE_T:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) EXPORT_SYMBOL_GPL(sfp_may_have_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * sfp_parse_support() - Parse the eeprom id for supported link modes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * @id: a pointer to the module's &struct sfp_eeprom_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * @support: pointer to an array of unsigned long for the ethtool support mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * Parse the EEPROM identification information and derive the supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * ethtool link modes for the module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		       unsigned long *support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	unsigned int br_min, br_nom, br_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	__ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	/* Decode the bitrate information to MBd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	br_min = br_nom = br_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (id->base.br_nominal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (id->base.br_nominal != 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			br_nom = id->base.br_nominal * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			br_min = br_nom - id->base.br_nominal * id->ext.br_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			br_max = br_nom + id->base.br_nominal * id->ext.br_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		} else if (id->ext.br_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			br_nom = 250 * id->ext.br_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			br_max = br_nom + br_nom * id->ext.br_min / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			br_min = br_nom - br_nom * id->ext.br_min / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		/* When using passive cables, in case neither BR,min nor BR,max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 * are specified, set br_min to 0 as the nominal value is then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 * used as the maximum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (br_min == br_max && id->base.sfp_ct_passive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			br_min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/* Set ethtool support from the compliance fields. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (id->base.e10g_base_sr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		phylink_set(modes, 10000baseSR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (id->base.e10g_base_lr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		phylink_set(modes, 10000baseLR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (id->base.e10g_base_lrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		phylink_set(modes, 10000baseLRM_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (id->base.e10g_base_er)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		phylink_set(modes, 10000baseER_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (id->base.e1000_base_sx ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	    id->base.e1000_base_lx ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	    id->base.e1000_base_cx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		phylink_set(modes, 1000baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (id->base.e1000_base_t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		phylink_set(modes, 1000baseT_Half);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		phylink_set(modes, 1000baseT_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	/* 1000Base-PX or 1000Base-BX10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if ((id->base.e_base_px || id->base.e_base_bx10) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	    br_min <= 1300 && br_max >= 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		phylink_set(modes, 1000baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/* For active or passive cables, select the link modes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * based on the bit rates and the cable compliance bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/* This may look odd, but some manufacturers use 12000MBd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (br_min <= 12000 && br_max >= 10300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			phylink_set(modes, 10000baseCR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		if (br_min <= 3200 && br_max >= 3100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			phylink_set(modes, 2500baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (br_min <= 1300 && br_max >= 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			phylink_set(modes, 1000baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (id->base.sfp_ct_passive) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		if (id->base.passive.sff8431_app_e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			phylink_set(modes, 10000baseCR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (id->base.sfp_ct_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (id->base.active.sff8431_app_e ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		    id->base.active.sff8431_lim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			phylink_set(modes, 10000baseCR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	switch (id->base.extended_cc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	case SFF8024_ECC_UNSPEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		phylink_set(modes, 100000baseSR4_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		phylink_set(modes, 25000baseSR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		phylink_set(modes, 100000baseLR4_ER4_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	case SFF8024_ECC_100GBASE_CR4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		phylink_set(modes, 100000baseCR4_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	case SFF8024_ECC_25GBASE_CR_S:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	case SFF8024_ECC_25GBASE_CR_N:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		phylink_set(modes, 25000baseCR_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	case SFF8024_ECC_10GBASE_T_SFI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	case SFF8024_ECC_10GBASE_T_SR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		phylink_set(modes, 10000baseT_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	case SFF8024_ECC_5GBASE_T:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		phylink_set(modes, 5000baseT_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	case SFF8024_ECC_2_5GBASE_T:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		phylink_set(modes, 2500baseT_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		dev_warn(bus->sfp_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			 "Unknown/unsupported extended compliance code: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			 id->base.extended_cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/* For fibre channel SFP, derive possible BaseX modes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (id->base.fc_speed_100 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	    id->base.fc_speed_200 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	    id->base.fc_speed_400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		if (id->base.br_nominal >= 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			phylink_set(modes, 2500baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		if (id->base.br_nominal >= 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			phylink_set(modes, 1000baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	/* If we haven't discovered any modes that this module supports, try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	 * the bitrate to determine supported modes. Some BiDi modules (eg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	 * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	 * wavelengths, so do not set any transceiver bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (bitmap_empty(modes, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		/* If the bit rate allows 1000baseX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		if (br_nom && br_min <= 1300 && br_max >= 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			phylink_set(modes, 1000baseX_Full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (bus->sfp_quirk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		bus->sfp_quirk->modes(id, modes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	bitmap_or(support, support, modes, __ETHTOOL_LINK_MODE_MASK_NBITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	phylink_set(support, Autoneg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	phylink_set(support, Pause);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	phylink_set(support, Asym_Pause);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) EXPORT_SYMBOL_GPL(sfp_parse_support);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * sfp_select_interface() - Select appropriate phy_interface_t mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * @link_modes: ethtool link modes mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  * Derive the phy_interface_t mode for the SFP module from the link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  * modes mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) phy_interface_t sfp_select_interface(struct sfp_bus *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				     unsigned long *link_modes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (phylink_test(link_modes, 10000baseCR_Full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	    phylink_test(link_modes, 10000baseSR_Full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	    phylink_test(link_modes, 10000baseLR_Full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	    phylink_test(link_modes, 10000baseLRM_Full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	    phylink_test(link_modes, 10000baseER_Full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	    phylink_test(link_modes, 10000baseT_Full))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return PHY_INTERFACE_MODE_10GBASER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (phylink_test(link_modes, 2500baseX_Full))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return PHY_INTERFACE_MODE_2500BASEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (phylink_test(link_modes, 1000baseT_Half) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	    phylink_test(link_modes, 1000baseT_Full))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return PHY_INTERFACE_MODE_SGMII;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (phylink_test(link_modes, 1000baseX_Full))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		return PHY_INTERFACE_MODE_1000BASEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return PHY_INTERFACE_MODE_NA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) EXPORT_SYMBOL_GPL(sfp_select_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static LIST_HEAD(sfp_buses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static DEFINE_MUTEX(sfp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return bus->registered ? bus->upstream_ops : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static struct sfp_bus *sfp_bus_get(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	struct sfp_bus *sfp, *new, *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	new = kzalloc(sizeof(*new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	mutex_lock(&sfp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	list_for_each_entry(sfp, &sfp_buses, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (sfp->fwnode == fwnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			kref_get(&sfp->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			found = sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (!found && new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		kref_init(&new->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		new->fwnode = fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		list_add(&new->node, &sfp_buses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		found = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	mutex_unlock(&sfp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static void sfp_bus_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	list_del(&bus->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	mutex_unlock(&sfp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	kfree(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)  * sfp_bus_put() - put a reference on the &struct sfp_bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)  * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * Put a reference on the &struct sfp_bus and free the underlying structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  * if this was the last reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) void sfp_bus_put(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) EXPORT_SYMBOL_GPL(sfp_bus_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static int sfp_register_bus(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (ops->link_down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			ops->link_down(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		if (ops->connect_phy && bus->phydev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			ret = ops->connect_phy(bus->upstream, bus->phydev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	bus->registered = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	bus->socket_ops->attach(bus->sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (bus->started)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		bus->socket_ops->start(bus->sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	bus->upstream_ops->attach(bus->upstream, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static void sfp_unregister_bus(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (bus->registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		bus->upstream_ops->detach(bus->upstream, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		if (bus->started)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			bus->socket_ops->stop(bus->sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		bus->socket_ops->detach(bus->sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		if (bus->phydev && ops && ops->disconnect_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			ops->disconnect_phy(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	bus->registered = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)  * @modinfo: a &struct ethtool_modinfo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  * Fill in the type and eeprom_len parameters in @modinfo for a module on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)  * the sfp bus specified by @bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  * Returns 0 on success or a negative errno number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	return bus->socket_ops->module_info(bus->sfp, modinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) EXPORT_SYMBOL_GPL(sfp_get_module_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)  * sfp_get_module_eeprom() - Read the SFP module EEPROM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)  * @ee: a &struct ethtool_eeprom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)  * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)  * Read the EEPROM as specified by the supplied @ee. See the documentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)  * for &struct ethtool_eeprom for the region to be read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)  * Returns 0 on success or a negative errno number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			  u8 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)  * sfp_upstream_start() - Inform the SFP that the network device is up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  * Inform the SFP socket that the network device is now up, so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  * module can be enabled by allowing TX_DISABLE to be deasserted. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)  * should be called from the network device driver's &struct net_device_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  * ndo_open() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) void sfp_upstream_start(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (bus->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		bus->socket_ops->start(bus->sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	bus->started = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) EXPORT_SYMBOL_GPL(sfp_upstream_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  * sfp_upstream_stop() - Inform the SFP that the network device is down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)  * Inform the SFP socket that the network device is now up, so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)  * module can be disabled by asserting TX_DISABLE, disabling the laser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)  * in optical modules. This should be called from the network device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  * driver's &struct net_device_ops ndo_stop() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) void sfp_upstream_stop(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	if (bus->registered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		bus->socket_ops->stop(bus->sfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	bus->started = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) EXPORT_SYMBOL_GPL(sfp_upstream_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static void sfp_upstream_clear(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	bus->upstream_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	bus->upstream = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)  * @fwnode: firmware node for the parent device (MAC or PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * Parse the parent device's firmware node for a SFP bus, and locate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  * the sfp_bus structure, incrementing its reference count.  This must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  * be put via sfp_bus_put() when done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)  * 	    - on success, a pointer to the sfp_bus structure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)  *	    - %NULL if no SFP is specified,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)  * 	    - on failure, an error pointer value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)  * 	      - corresponding to the errors detailed for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)  * 	        fwnode_property_get_reference_args().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)  * 	      - %-ENOMEM if we failed to allocate the bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)  *	      - an error from the upstream's connect_phy() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct sfp_bus *sfp_bus_find_fwnode(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct fwnode_reference_args ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct sfp_bus *bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 						 0, 0, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	else if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (!fwnode_device_is_available(ref.fwnode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		fwnode_handle_put(ref.fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	bus = sfp_bus_get(ref.fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	fwnode_handle_put(ref.fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	return bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)  * sfp_bus_add_upstream() - parse and register the neighbouring device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * @upstream: the upstream private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * @ops: the upstream's &struct sfp_upstream_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  * Add upstream driver for the SFP bus, and if the bus is complete, register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  * the SFP bus using sfp_register_upstream().  This takes a reference on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * bus, so it is safe to put the bus after this call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)  * 	    - on success, a pointer to the sfp_bus structure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)  *	    - %NULL if no SFP is specified,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)  * 	    - on failure, an error pointer value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)  * 	      - corresponding to the errors detailed for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)  * 	        fwnode_property_get_reference_args().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)  * 	      - %-ENOMEM if we failed to allocate the bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)  *	      - an error from the upstream's connect_phy() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			 const struct sfp_upstream_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	/* If no bus, return success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	kref_get(&bus->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	bus->upstream_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	bus->upstream = upstream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	if (bus->sfp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		ret = sfp_register_bus(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 			sfp_upstream_clear(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		sfp_bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)  * sfp_bus_del_upstream() - Delete a sfp bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)  * @bus: a pointer to the &struct sfp_bus structure for the sfp module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)  * Delete a previously registered upstream connection for the SFP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)  * module. @bus should have been added by sfp_bus_add_upstream().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) void sfp_bus_del_upstream(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	if (bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		if (bus->sfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 			sfp_unregister_bus(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		sfp_upstream_clear(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		sfp_bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) /* Socket driver entry points */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	if (ops && ops->connect_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		ret = ops->connect_phy(bus->upstream, phydev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		bus->phydev = phydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) EXPORT_SYMBOL_GPL(sfp_add_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) void sfp_remove_phy(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	if (ops && ops->disconnect_phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		ops->disconnect_phy(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	bus->phydev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) EXPORT_SYMBOL_GPL(sfp_remove_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) void sfp_link_up(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	if (ops && ops->link_up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		ops->link_up(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) EXPORT_SYMBOL_GPL(sfp_link_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) void sfp_link_down(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	if (ops && ops->link_down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		ops->link_down(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) EXPORT_SYMBOL_GPL(sfp_link_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	bus->sfp_quirk = sfp_lookup_quirk(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	if (ops && ops->module_insert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		ret = ops->module_insert(bus->upstream, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) EXPORT_SYMBOL_GPL(sfp_module_insert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) void sfp_module_remove(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	if (ops && ops->module_remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		ops->module_remove(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	bus->sfp_quirk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) EXPORT_SYMBOL_GPL(sfp_module_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) int sfp_module_start(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	if (ops && ops->module_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 		ret = ops->module_start(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) EXPORT_SYMBOL_GPL(sfp_module_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) void sfp_module_stop(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	if (ops && ops->module_stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		ops->module_stop(bus->upstream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) EXPORT_SYMBOL_GPL(sfp_module_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) static void sfp_socket_clear(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	bus->sfp_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	bus->sfp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	bus->socket_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 				    const struct sfp_socket_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		bus->sfp_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 		bus->sfp = sfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		bus->socket_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		if (bus->upstream_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 			ret = sfp_register_bus(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 				sfp_socket_clear(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 		sfp_bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 		bus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	return bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) EXPORT_SYMBOL_GPL(sfp_register_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) void sfp_unregister_socket(struct sfp_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	if (bus->upstream_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 		sfp_unregister_bus(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	sfp_socket_clear(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	sfp_bus_put(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) EXPORT_SYMBOL_GPL(sfp_unregister_socket);