sfp-bus.c 23.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6
#include <linux/export.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/phylink.h>
7
#include <linux/property.h>
8 9 10 11 12
#include <linux/rtnetlink.h>
#include <linux/slab.h>

#include "sfp.h"

13 14 15
/**
 * struct sfp_bus - internal representation of a sfp bus
 */
16
struct sfp_bus {
17
	/* private: */
18 19
	struct kref kref;
	struct list_head node;
20
	const struct fwnode_handle *fwnode;
21 22 23 24

	const struct sfp_socket_ops *socket_ops;
	struct device *sfp_dev;
	struct sfp *sfp;
25
	const struct sfp_quirk *sfp_quirk;
26 27 28 29 30 31 32 33 34

	const struct sfp_upstream_ops *upstream_ops;
	void *upstream;
	struct phy_device *phydev;

	bool registered;
	bool started;
};

35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
 * sfp_parse_port() - Parse the EEPROM base ID, setting the port type
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @id: a pointer to the module's &struct sfp_eeprom_id
 * @support: optional pointer to an array of unsigned long for the
 *   ethtool support mask
 *
 * Parse the EEPROM identification given in @id, and return one of
 * %PORT_TP, %PORT_FIBRE or %PORT_OTHER. If @support is non-%NULL,
 * also set the ethtool %ETHTOOL_LINK_MODE_xxx_BIT corresponding with
 * the connector type.
 *
 * If the port type is not known, returns %PORT_OTHER.
 */
49 50 51 52 53 54 55
int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
		   unsigned long *support)
{
	int port;

	/* port is the physical connector, set this from the connector field. */
	switch (id->base.connector) {
56 57 58 59 60 61 62 63
	case SFF8024_CONNECTOR_SC:
	case SFF8024_CONNECTOR_FIBERJACK:
	case SFF8024_CONNECTOR_LC:
	case SFF8024_CONNECTOR_MT_RJ:
	case SFF8024_CONNECTOR_MU:
	case SFF8024_CONNECTOR_OPTICAL_PIGTAIL:
	case SFF8024_CONNECTOR_MPO_1X12:
	case SFF8024_CONNECTOR_MPO_2X16:
64 65 66
		port = PORT_FIBRE;
		break;

67
	case SFF8024_CONNECTOR_RJ45:
68 69 70
		port = PORT_TP;
		break;

71
	case SFF8024_CONNECTOR_COPPER_PIGTAIL:
72 73 74
		port = PORT_DA;
		break;

75
	case SFF8024_CONNECTOR_UNSPEC:
76 77 78 79
		if (id->base.e1000_base_t) {
			port = PORT_TP;
			break;
		}
80
		fallthrough;
81 82 83 84
	case SFF8024_CONNECTOR_SG: /* guess */
	case SFF8024_CONNECTOR_HSSDC_II:
	case SFF8024_CONNECTOR_NOSEPARATE:
	case SFF8024_CONNECTOR_MXC_2X16:
85 86 87 88 89 90 91 92 93
		port = PORT_OTHER;
		break;
	default:
		dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n",
			 id->base.connector);
		port = PORT_OTHER;
		break;
	}

94 95 96 97 98 99 100 101 102 103 104 105
	if (support) {
		switch (port) {
		case PORT_FIBRE:
			phylink_set(support, FIBRE);
			break;

		case PORT_TP:
			phylink_set(support, TP);
			break;
		}
	}

106 107 108 109
	return port;
}
EXPORT_SYMBOL_GPL(sfp_parse_port);

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
 * sfp_may_have_phy() - indicate whether the module may have a PHY
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @id: a pointer to the module's &struct sfp_eeprom_id
 *
 * Parse the EEPROM identification given in @id, and return whether
 * this module may have a PHY.
 */
bool sfp_may_have_phy(struct sfp_bus *bus, const struct sfp_eeprom_id *id)
{
	if (id->base.e1000_base_t)
		return true;

	if (id->base.phys_id != SFF8024_ID_DWDM_SFP) {
		switch (id->base.extended_cc) {
		case SFF8024_ECC_10GBASE_T_SFI:
		case SFF8024_ECC_10GBASE_T_SR:
		case SFF8024_ECC_5GBASE_T:
		case SFF8024_ECC_2_5GBASE_T:
			return true;
		}
	}

	return false;
}
EXPORT_SYMBOL_GPL(sfp_may_have_phy);

137 138 139 140 141
/**
 * sfp_parse_support() - Parse the eeprom id for supported link modes
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @id: a pointer to the module's &struct sfp_eeprom_id
 * @support: pointer to an array of unsigned long for the ethtool support mask
142 143
 * @interfaces: pointer to an array of unsigned long for phy interface modes
 *		mask
144 145 146 147
 *
 * Parse the EEPROM identification information and derive the supported
 * ethtool link modes for the module.
 */
148
void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
149
		       unsigned long *support, unsigned long *interfaces)
150
{
151
	unsigned int br_min, br_nom, br_max;
152
	__ETHTOOL_DECLARE_LINK_MODE_MASK(modes) = { 0, };
153

154 155 156 157
	phylink_set(modes, Autoneg);
	phylink_set(modes, Pause);
	phylink_set(modes, Asym_Pause);

158 159 160 161 162
	/* Decode the bitrate information to MBd */
	br_min = br_nom = br_max = 0;
	if (id->base.br_nominal) {
		if (id->base.br_nominal != 255) {
			br_nom = id->base.br_nominal * 100;
163
			br_min = br_nom - id->base.br_nominal * id->ext.br_min;
164 165 166 167 168 169
			br_max = br_nom + id->base.br_nominal * id->ext.br_max;
		} else if (id->ext.br_max) {
			br_nom = 250 * id->ext.br_max;
			br_max = br_nom + br_nom * id->ext.br_min / 100;
			br_min = br_nom - br_nom * id->ext.br_min / 100;
		}
170 171 172 173 174 175 176

		/* When using passive cables, in case neither BR,min nor BR,max
		 * are specified, set br_min to 0 as the nominal value is then
		 * used as the maximum.
		 */
		if (br_min == br_max && id->base.sfp_ct_passive)
			br_min = 0;
177 178
	}

179
	/* Set ethtool support from the compliance fields. */
180
	if (id->base.e10g_base_sr) {
181
		phylink_set(modes, 10000baseSR_Full);
182 183 184
		__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
	}
	if (id->base.e10g_base_lr) {
185
		phylink_set(modes, 10000baseLR_Full);
186 187 188
		__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
	}
	if (id->base.e10g_base_lrm) {
189
		phylink_set(modes, 10000baseLRM_Full);
190 191 192
		__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
	}
	if (id->base.e10g_base_er) {
193
		phylink_set(modes, 10000baseER_Full);
194 195
		__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
	}
196 197
	if (id->base.e1000_base_sx ||
	    id->base.e1000_base_lx ||
198
	    id->base.e1000_base_cx) {
199
		phylink_set(modes, 1000baseX_Full);
200 201
		__set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
	}
202
	if (id->base.e1000_base_t) {
203 204
		phylink_set(modes, 1000baseT_Half);
		phylink_set(modes, 1000baseT_Full);
205 206
		__set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
		__set_bit(PHY_INTERFACE_MODE_SGMII, interfaces);
207 208
	}

209 210
	/* 1000Base-PX or 1000Base-BX10 */
	if ((id->base.e_base_px || id->base.e_base_bx10) &&
211
	    br_min <= 1300 && br_max >= 1200) {
212
		phylink_set(modes, 1000baseX_Full);
213 214
		__set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
	}
215

216
	/* 100Base-FX, 100Base-LX, 100Base-PX, 100Base-BX10 */
217
	if (id->base.e100_base_fx || id->base.e100_base_lx) {
218
		phylink_set(modes, 100baseFX_Full);
219 220 221
		__set_bit(PHY_INTERFACE_MODE_100BASEX, interfaces);
	}
	if ((id->base.e_base_px || id->base.e_base_bx10) && br_nom == 100) {
222
		phylink_set(modes, 100baseFX_Full);
223 224
		__set_bit(PHY_INTERFACE_MODE_100BASEX, interfaces);
	}
225

226 227 228 229 230
	/* For active or passive cables, select the link modes
	 * based on the bit rates and the cable compliance bytes.
	 */
	if ((id->base.sfp_ct_passive || id->base.sfp_ct_active) && br_nom) {
		/* This may look odd, but some manufacturers use 12000MBd */
231
		if (br_min <= 12000 && br_max >= 10300) {
232
			phylink_set(modes, 10000baseCR_Full);
233 234 235
			__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
		}
		if (br_min <= 3200 && br_max >= 3100) {
236
			phylink_set(modes, 2500baseX_Full);
237 238 239
			__set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
		}
		if (br_min <= 1300 && br_max >= 1200) {
240
			phylink_set(modes, 1000baseX_Full);
241 242
			__set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
		}
243 244
	}
	if (id->base.sfp_ct_passive) {
245
		if (id->base.passive.sff8431_app_e) {
246
			phylink_set(modes, 10000baseCR_Full);
247 248
			__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
		}
249 250 251 252
	}
	if (id->base.sfp_ct_active) {
		if (id->base.active.sff8431_app_e ||
		    id->base.active.sff8431_lim) {
253
			phylink_set(modes, 10000baseCR_Full);
254
			__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
255 256 257
		}
	}

258
	switch (id->base.extended_cc) {
259
	case SFF8024_ECC_UNSPEC:
260
		break;
261 262 263 264 265 266 267 268 269 270
	case SFF8024_ECC_100G_25GAUI_C2M_AOC:
		if (br_min <= 28000 && br_max >= 25000) {
			/* 25GBASE-R, possibly with FEC */
			__set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
			/* There is currently no link mode for 25000base
			 * with unspecified range, reuse SR.
			 */
			phylink_set(modes, 25000baseSR_Full);
		}
		break;
271
	case SFF8024_ECC_100GBASE_SR4_25GBASE_SR:
272 273
		phylink_set(modes, 100000baseSR4_Full);
		phylink_set(modes, 25000baseSR_Full);
274
		__set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
275
		break;
276 277
	case SFF8024_ECC_100GBASE_LR4_25GBASE_LR:
	case SFF8024_ECC_100GBASE_ER4_25GBASE_ER:
278
		phylink_set(modes, 100000baseLR4_ER4_Full);
279
		break;
280
	case SFF8024_ECC_100GBASE_CR4:
281
		phylink_set(modes, 100000baseCR4_Full);
282
		fallthrough;
283 284
	case SFF8024_ECC_25GBASE_CR_S:
	case SFF8024_ECC_25GBASE_CR_N:
285
		phylink_set(modes, 25000baseCR_Full);
286
		__set_bit(PHY_INTERFACE_MODE_25GBASER, interfaces);
287
		break;
288 289 290
	case SFF8024_ECC_10GBASE_T_SFI:
	case SFF8024_ECC_10GBASE_T_SR:
		phylink_set(modes, 10000baseT_Full);
291
		__set_bit(PHY_INTERFACE_MODE_10GBASER, interfaces);
292 293 294
		break;
	case SFF8024_ECC_5GBASE_T:
		phylink_set(modes, 5000baseT_Full);
295
		__set_bit(PHY_INTERFACE_MODE_5GBASER, interfaces);
296 297 298
		break;
	case SFF8024_ECC_2_5GBASE_T:
		phylink_set(modes, 2500baseT_Full);
299
		__set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
300
		break;
301 302 303 304 305 306 307 308 309 310 311
	default:
		dev_warn(bus->sfp_dev,
			 "Unknown/unsupported extended compliance code: 0x%02x\n",
			 id->base.extended_cc);
		break;
	}

	/* For fibre channel SFP, derive possible BaseX modes */
	if (id->base.fc_speed_100 ||
	    id->base.fc_speed_200 ||
	    id->base.fc_speed_400) {
312
		if (id->base.br_nominal >= 31) {
313
			phylink_set(modes, 2500baseX_Full);
314 315 316
			__set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
		}
		if (id->base.br_nominal >= 12) {
317
			phylink_set(modes, 1000baseX_Full);
318 319
			__set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
		}
320
	}
321 322

	/* If we haven't discovered any modes that this module supports, try
323 324 325
	 * the bitrate to determine supported modes. Some BiDi modules (eg,
	 * 1310nm/1550nm) are not 1000BASE-BX compliant due to the differing
	 * wavelengths, so do not set any transceiver bits.
326 327 328 329
	 *
	 * Do the same for modules supporting 2500BASE-X. Note that some
	 * modules use 2500Mbaud rather than 3100 or 3200Mbaud for
	 * 2500BASE-X, so we allow some slack here.
330
	 */
331
	if (linkmode_empty(modes) && br_nom) {
332
		if (br_min <= 1300 && br_max >= 1200) {
333
			phylink_set(modes, 1000baseX_Full);
334 335 336
			__set_bit(PHY_INTERFACE_MODE_1000BASEX, interfaces);
		}
		if (br_min <= 3200 && br_max >= 2500) {
337
			phylink_set(modes, 2500baseX_Full);
338 339
			__set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces);
		}
340 341
	}

342
	if (bus->sfp_quirk && bus->sfp_quirk->modes)
343
		bus->sfp_quirk->modes(id, modes, interfaces);
344

345
	linkmode_or(support, support, modes);
346 347 348
}
EXPORT_SYMBOL_GPL(sfp_parse_support);

349 350 351 352 353
/**
 * sfp_select_interface() - Select appropriate phy_interface_t mode
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @link_modes: ethtool link modes mask
 *
354 355
 * Derive the phy_interface_t mode for the SFP module from the link
 * modes mask.
356 357 358 359
 */
phy_interface_t sfp_select_interface(struct sfp_bus *bus,
				     unsigned long *link_modes)
{
360 361 362 363 364
	if (phylink_test(link_modes, 25000baseCR_Full) ||
	    phylink_test(link_modes, 25000baseKR_Full) ||
	    phylink_test(link_modes, 25000baseSR_Full))
		return PHY_INTERFACE_MODE_25GBASER;

365 366 367 368
	if (phylink_test(link_modes, 10000baseCR_Full) ||
	    phylink_test(link_modes, 10000baseSR_Full) ||
	    phylink_test(link_modes, 10000baseLR_Full) ||
	    phylink_test(link_modes, 10000baseLRM_Full) ||
369 370
	    phylink_test(link_modes, 10000baseER_Full) ||
	    phylink_test(link_modes, 10000baseT_Full))
371
		return PHY_INTERFACE_MODE_10GBASER;
372

373 374 375
	if (phylink_test(link_modes, 5000baseT_Full))
		return PHY_INTERFACE_MODE_5GBASER;

376 377 378
	if (phylink_test(link_modes, 2500baseX_Full))
		return PHY_INTERFACE_MODE_2500BASEX;

379 380
	if (phylink_test(link_modes, 1000baseT_Half) ||
	    phylink_test(link_modes, 1000baseT_Full))
381 382 383 384 385
		return PHY_INTERFACE_MODE_SGMII;

	if (phylink_test(link_modes, 1000baseX_Full))
		return PHY_INTERFACE_MODE_1000BASEX;

386 387 388
	if (phylink_test(link_modes, 100baseFX_Full))
		return PHY_INTERFACE_MODE_100BASEX;

389 390 391 392 393 394
	dev_warn(bus->sfp_dev, "Unable to ascertain link mode\n");

	return PHY_INTERFACE_MODE_NA;
}
EXPORT_SYMBOL_GPL(sfp_select_interface);

395 396 397 398 399 400 401 402
static LIST_HEAD(sfp_buses);
static DEFINE_MUTEX(sfp_mutex);

static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus)
{
	return bus->registered ? bus->upstream_ops : NULL;
}

403
static struct sfp_bus *sfp_bus_get(const struct fwnode_handle *fwnode)
404 405 406 407 408 409 410 411
{
	struct sfp_bus *sfp, *new, *found = NULL;

	new = kzalloc(sizeof(*new), GFP_KERNEL);

	mutex_lock(&sfp_mutex);

	list_for_each_entry(sfp, &sfp_buses, node) {
Russell King's avatar
Russell King committed
412
		if (sfp->fwnode == fwnode) {
413 414 415 416 417 418 419 420
			kref_get(&sfp->kref);
			found = sfp;
			break;
		}
	}

	if (!found && new) {
		kref_init(&new->kref);
Russell King's avatar
Russell King committed
421
		new->fwnode = fwnode;
422 423 424 425 426 427 428 429 430 431 432 433
		list_add(&new->node, &sfp_buses);
		found = new;
		new = NULL;
	}

	mutex_unlock(&sfp_mutex);

	kfree(new);

	return found;
}

Russell King's avatar
Russell King committed
434
static void sfp_bus_release(struct kref *kref)
435 436 437 438 439 440 441 442
{
	struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref);

	list_del(&bus->node);
	mutex_unlock(&sfp_mutex);
	kfree(bus);
}

443 444
/**
 * sfp_bus_put() - put a reference on the &struct sfp_bus
445
 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
446 447 448 449 450
 *
 * Put a reference on the &struct sfp_bus and free the underlying structure
 * if this was the last reference.
 */
void sfp_bus_put(struct sfp_bus *bus)
451
{
452 453
	if (bus)
		kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex);
454
}
455
EXPORT_SYMBOL_GPL(sfp_bus_put);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

static int sfp_register_bus(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = bus->upstream_ops;
	int ret;

	if (ops) {
		if (ops->link_down)
			ops->link_down(bus->upstream);
		if (ops->connect_phy && bus->phydev) {
			ret = ops->connect_phy(bus->upstream, bus->phydev);
			if (ret)
				return ret;
		}
	}
471
	bus->registered = true;
472
	bus->socket_ops->attach(bus->sfp);
473 474
	if (bus->started)
		bus->socket_ops->start(bus->sfp);
475
	bus->upstream_ops->attach(bus->upstream, bus);
476 477 478 479 480 481 482 483
	return 0;
}

static void sfp_unregister_bus(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = bus->upstream_ops;

	if (bus->registered) {
484
		bus->upstream_ops->detach(bus->upstream, bus);
485 486
		if (bus->started)
			bus->socket_ops->stop(bus->sfp);
487
		bus->socket_ops->detach(bus->sfp);
488 489 490 491 492 493
		if (bus->phydev && ops && ops->disconnect_phy)
			ops->disconnect_phy(bus->upstream);
	}
	bus->registered = false;
}

494 495 496 497 498 499 500 501 502 503
/**
 * sfp_get_module_info() - Get the ethtool_modinfo for a SFP module
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @modinfo: a &struct ethtool_modinfo
 *
 * Fill in the type and eeprom_len parameters in @modinfo for a module on
 * the sfp bus specified by @bus.
 *
 * Returns 0 on success or a negative errno number.
 */
504 505 506 507 508 509
int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo)
{
	return bus->socket_ops->module_info(bus->sfp, modinfo);
}
EXPORT_SYMBOL_GPL(sfp_get_module_info);

510 511 512 513 514 515 516 517 518 519 520
/**
 * sfp_get_module_eeprom() - Read the SFP module EEPROM
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @ee: a &struct ethtool_eeprom
 * @data: buffer to contain the EEPROM data (must be at least @ee->len bytes)
 *
 * Read the EEPROM as specified by the supplied @ee. See the documentation
 * for &struct ethtool_eeprom for the region to be read.
 *
 * Returns 0 on success or a negative errno number.
 */
521
int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee,
522
			  u8 *data)
523 524 525 526 527
{
	return bus->socket_ops->module_eeprom(bus->sfp, ee, data);
}
EXPORT_SYMBOL_GPL(sfp_get_module_eeprom);

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/**
 * sfp_get_module_eeprom_by_page() - Read a page from the SFP module EEPROM
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @page: a &struct ethtool_module_eeprom
 * @extack: extack for reporting problems
 *
 * Read an EEPROM page as specified by the supplied @page. See the
 * documentation for &struct ethtool_module_eeprom for the page to be read.
 *
 * Returns 0 on success or a negative errno number. More error
 * information might be provided via extack
 */
int sfp_get_module_eeprom_by_page(struct sfp_bus *bus,
				  const struct ethtool_module_eeprom *page,
				  struct netlink_ext_ack *extack)
{
	return bus->socket_ops->module_eeprom_by_page(bus->sfp, page, extack);
}
EXPORT_SYMBOL_GPL(sfp_get_module_eeprom_by_page);

548 549 550 551 552 553 554 555 556
/**
 * sfp_upstream_start() - Inform the SFP that the network device is up
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 *
 * Inform the SFP socket that the network device is now up, so that the
 * module can be enabled by allowing TX_DISABLE to be deasserted. This
 * should be called from the network device driver's &struct net_device_ops
 * ndo_open() method.
 */
557 558 559 560 561 562 563 564
void sfp_upstream_start(struct sfp_bus *bus)
{
	if (bus->registered)
		bus->socket_ops->start(bus->sfp);
	bus->started = true;
}
EXPORT_SYMBOL_GPL(sfp_upstream_start);

565 566 567 568 569 570 571 572 573
/**
 * sfp_upstream_stop() - Inform the SFP that the network device is down
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 *
 * Inform the SFP socket that the network device is now up, so that the
 * module can be disabled by asserting TX_DISABLE, disabling the laser
 * in optical modules. This should be called from the network device
 * driver's &struct net_device_ops ndo_stop() method.
 */
574 575 576 577 578 579 580 581
void sfp_upstream_stop(struct sfp_bus *bus)
{
	if (bus->registered)
		bus->socket_ops->stop(bus->sfp);
	bus->started = false;
}
EXPORT_SYMBOL_GPL(sfp_upstream_stop);

582 583 584 585 586 587
static void sfp_upstream_clear(struct sfp_bus *bus)
{
	bus->upstream_ops = NULL;
	bus->upstream = NULL;
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
/**
 * sfp_upstream_set_signal_rate() - set data signalling rate
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 * @rate_kbd: signalling rate in units of 1000 baud
 *
 * Configure the rate select settings on the SFP module for the signalling
 * rate (not the same as the data rate).
 *
 * Locks that may be held:
 *  Phylink's state_mutex
 *  rtnl lock
 *  SFP's sm_mutex
 */
void sfp_upstream_set_signal_rate(struct sfp_bus *bus, unsigned int rate_kbd)
{
	if (bus->registered)
		bus->socket_ops->set_signal_rate(bus->sfp, rate_kbd);
}
EXPORT_SYMBOL_GPL(sfp_upstream_set_signal_rate);

608
/**
609
 * sfp_bus_find_fwnode() - parse and locate the SFP bus from fwnode
610
 * @fwnode: firmware node for the parent device (MAC or PHY)
611
 *
612 613 614
 * Parse the parent device's firmware node for a SFP bus, and locate
 * the sfp_bus structure, incrementing its reference count.  This must
 * be put via sfp_bus_put() when done.
615
 *
616
 * Returns:
617 618 619
 *	- on success, a pointer to the sfp_bus structure,
 *	- %NULL if no SFP is specified,
 *	- on failure, an error pointer value:
620
 *
621 622 623 624
 *	- corresponding to the errors detailed for
 *	  fwnode_property_get_reference_args().
 *	- %-ENOMEM if we failed to allocate the bus.
 *	- an error from the upstream's connect_phy() method.
625
 */
626
struct sfp_bus *sfp_bus_find_fwnode(const struct fwnode_handle *fwnode)
627
{
628 629 630
	struct fwnode_reference_args ref;
	struct sfp_bus *bus;
	int ret;
631

632 633 634 635 636 637
	ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
						 0, 0, &ref);
	if (ret == -ENOENT)
		return NULL;
	else if (ret < 0)
		return ERR_PTR(ret);
638

639 640 641 642 643
	if (!fwnode_device_is_available(ref.fwnode)) {
		fwnode_handle_put(ref.fwnode);
		return NULL;
	}

644 645 646 647 648
	bus = sfp_bus_get(ref.fwnode);
	fwnode_handle_put(ref.fwnode);
	if (!bus)
		return ERR_PTR(-ENOMEM);

649 650 651 652 653 654 655 656 657 658 659 660 661 662
	return bus;
}
EXPORT_SYMBOL_GPL(sfp_bus_find_fwnode);

/**
 * sfp_bus_add_upstream() - parse and register the neighbouring device
 * @bus: the &struct sfp_bus found via sfp_bus_find_fwnode()
 * @upstream: the upstream private data
 * @ops: the upstream's &struct sfp_upstream_ops
 *
 * Add upstream driver for the SFP bus, and if the bus is complete, register
 * the SFP bus using sfp_register_upstream().  This takes a reference on the
 * bus, so it is safe to put the bus after this call.
 *
663
 * Returns:
664 665 666
 *	- on success, a pointer to the sfp_bus structure,
 *	- %NULL if no SFP is specified,
 *	- on failure, an error pointer value:
667
 *
668 669 670 671
 *	- corresponding to the errors detailed for
 *	  fwnode_property_get_reference_args().
 *	- %-ENOMEM if we failed to allocate the bus.
 *	- an error from the upstream's connect_phy() method.
672 673 674 675 676 677 678 679 680 681
 */
int sfp_bus_add_upstream(struct sfp_bus *bus, void *upstream,
			 const struct sfp_upstream_ops *ops)
{
	int ret;

	/* If no bus, return success */
	if (!bus)
		return 0;

682
	rtnl_lock();
683
	kref_get(&bus->kref);
684 685 686 687 688 689 690 691 692
	bus->upstream_ops = ops;
	bus->upstream = upstream;

	if (bus->sfp) {
		ret = sfp_register_bus(bus);
		if (ret)
			sfp_upstream_clear(bus);
	} else {
		ret = 0;
693
	}
694
	rtnl_unlock();
695

696
	if (ret)
697 698
		sfp_bus_put(bus);

699
	return ret;
700
}
701
EXPORT_SYMBOL_GPL(sfp_bus_add_upstream);
702

703
/**
704
 * sfp_bus_del_upstream() - Delete a sfp bus
705 706
 * @bus: a pointer to the &struct sfp_bus structure for the sfp module
 *
707 708
 * Delete a previously registered upstream connection for the SFP
 * module. @bus should have been added by sfp_bus_add_upstream().
709
 */
710
void sfp_bus_del_upstream(struct sfp_bus *bus)
711
{
712 713 714 715 716 717
	if (bus) {
		rtnl_lock();
		if (bus->sfp)
			sfp_unregister_bus(bus);
		sfp_upstream_clear(bus);
		rtnl_unlock();
718

719 720
		sfp_bus_put(bus);
	}
721
}
722
EXPORT_SYMBOL_GPL(sfp_bus_del_upstream);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

/* Socket driver entry points */
int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
	int ret = 0;

	if (ops && ops->connect_phy)
		ret = ops->connect_phy(bus->upstream, phydev);

	if (ret == 0)
		bus->phydev = phydev;

	return ret;
}
EXPORT_SYMBOL_GPL(sfp_add_phy);

void sfp_remove_phy(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);

	if (ops && ops->disconnect_phy)
		ops->disconnect_phy(bus->upstream);
	bus->phydev = NULL;
}
EXPORT_SYMBOL_GPL(sfp_remove_phy);

void sfp_link_up(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);

	if (ops && ops->link_up)
		ops->link_up(bus->upstream);
}
EXPORT_SYMBOL_GPL(sfp_link_up);

void sfp_link_down(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);

	if (ops && ops->link_down)
		ops->link_down(bus->upstream);
}
EXPORT_SYMBOL_GPL(sfp_link_down);

768 769
int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id,
		      const struct sfp_quirk *quirk)
770 771 772 773
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
	int ret = 0;

774
	bus->sfp_quirk = quirk;
775

776 777 778 779 780 781 782 783 784 785 786 787 788
	if (ops && ops->module_insert)
		ret = ops->module_insert(bus->upstream, id);

	return ret;
}
EXPORT_SYMBOL_GPL(sfp_module_insert);

void sfp_module_remove(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);

	if (ops && ops->module_remove)
		ops->module_remove(bus->upstream);
789 790

	bus->sfp_quirk = NULL;
791 792 793
}
EXPORT_SYMBOL_GPL(sfp_module_remove);

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
int sfp_module_start(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);
	int ret = 0;

	if (ops && ops->module_start)
		ret = ops->module_start(bus->upstream);

	return ret;
}
EXPORT_SYMBOL_GPL(sfp_module_start);

void sfp_module_stop(struct sfp_bus *bus)
{
	const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus);

	if (ops && ops->module_stop)
		ops->module_stop(bus->upstream);
}
EXPORT_SYMBOL_GPL(sfp_module_stop);

815 816 817 818 819 820 821
static void sfp_socket_clear(struct sfp_bus *bus)
{
	bus->sfp_dev = NULL;
	bus->sfp = NULL;
	bus->socket_ops = NULL;
}

822 823 824
struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp,
				    const struct sfp_socket_ops *ops)
{
Russell King's avatar
Russell King committed
825
	struct sfp_bus *bus = sfp_bus_get(dev->fwnode);
826 827 828 829 830 831 832 833
	int ret = 0;

	if (bus) {
		rtnl_lock();
		bus->sfp_dev = dev;
		bus->sfp = sfp;
		bus->socket_ops = ops;

834
		if (bus->upstream_ops) {
835
			ret = sfp_register_bus(bus);
836 837 838
			if (ret)
				sfp_socket_clear(bus);
		}
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
		rtnl_unlock();
	}

	if (ret) {
		sfp_bus_put(bus);
		bus = NULL;
	}

	return bus;
}
EXPORT_SYMBOL_GPL(sfp_register_socket);

void sfp_unregister_socket(struct sfp_bus *bus)
{
	rtnl_lock();
854
	if (bus->upstream_ops)
855
		sfp_unregister_bus(bus);
856
	sfp_socket_clear(bus);
857 858 859 860 861
	rtnl_unlock();

	sfp_bus_put(bus);
}
EXPORT_SYMBOL_GPL(sfp_unregister_socket);