1 #include <linux/export.h> 2 #include <linux/kref.h> 3 #include <linux/list.h> 4 #include <linux/mutex.h> 5 #include <linux/phylink.h> 6 #include <linux/rtnetlink.h> 7 #include <linux/slab.h> 8 9 #include "sfp.h" 10 11 struct sfp_bus { 12 struct kref kref; 13 struct list_head node; 14 struct device_node *device_node; 15 16 const struct sfp_socket_ops *socket_ops; 17 struct device *sfp_dev; 18 struct sfp *sfp; 19 20 const struct sfp_upstream_ops *upstream_ops; 21 void *upstream; 22 struct net_device *netdev; 23 struct phy_device *phydev; 24 25 bool registered; 26 bool started; 27 }; 28 29 int sfp_parse_port(struct sfp_bus *bus, const struct sfp_eeprom_id *id, 30 unsigned long *support) 31 { 32 int port; 33 34 /* port is the physical connector, set this from the connector field. */ 35 switch (id->base.connector) { 36 case SFP_CONNECTOR_SC: 37 case SFP_CONNECTOR_FIBERJACK: 38 case SFP_CONNECTOR_LC: 39 case SFP_CONNECTOR_MT_RJ: 40 case SFP_CONNECTOR_MU: 41 case SFP_CONNECTOR_OPTICAL_PIGTAIL: 42 if (support) 43 phylink_set(support, FIBRE); 44 port = PORT_FIBRE; 45 break; 46 47 case SFP_CONNECTOR_RJ45: 48 if (support) 49 phylink_set(support, TP); 50 port = PORT_TP; 51 break; 52 53 case SFP_CONNECTOR_UNSPEC: 54 if (id->base.e1000_base_t) { 55 if (support) 56 phylink_set(support, TP); 57 port = PORT_TP; 58 break; 59 } 60 /* fallthrough */ 61 case SFP_CONNECTOR_SG: /* guess */ 62 case SFP_CONNECTOR_MPO_1X12: 63 case SFP_CONNECTOR_MPO_2X16: 64 case SFP_CONNECTOR_HSSDC_II: 65 case SFP_CONNECTOR_COPPER_PIGTAIL: 66 case SFP_CONNECTOR_NOSEPARATE: 67 case SFP_CONNECTOR_MXC_2X16: 68 port = PORT_OTHER; 69 break; 70 default: 71 dev_warn(bus->sfp_dev, "SFP: unknown connector id 0x%02x\n", 72 id->base.connector); 73 port = PORT_OTHER; 74 break; 75 } 76 77 return port; 78 } 79 EXPORT_SYMBOL_GPL(sfp_parse_port); 80 81 phy_interface_t sfp_parse_interface(struct sfp_bus *bus, 82 const struct sfp_eeprom_id *id) 83 { 84 phy_interface_t iface; 85 86 /* Setting the serdes link mode is guesswork: there's no field in 87 * the EEPROM which indicates what mode should be used. 88 * 89 * If the module wants 64b66b, then it must be >= 10G. 90 * 91 * If it's a gigabit-only fiber module, it probably does not have 92 * a PHY, so switch to 802.3z negotiation mode. Otherwise, switch 93 * to SGMII mode (which is required to support non-gigabit speeds). 94 */ 95 switch (id->base.encoding) { 96 case SFP_ENCODING_8472_64B66B: 97 iface = PHY_INTERFACE_MODE_10GKR; 98 break; 99 100 case SFP_ENCODING_8B10B: 101 if (!id->base.e1000_base_t && 102 !id->base.e100_base_lx && 103 !id->base.e100_base_fx) 104 iface = PHY_INTERFACE_MODE_1000BASEX; 105 else 106 iface = PHY_INTERFACE_MODE_SGMII; 107 break; 108 109 default: 110 iface = PHY_INTERFACE_MODE_NA; 111 dev_err(bus->sfp_dev, 112 "SFP module encoding does not support 8b10b nor 64b66b\n"); 113 break; 114 } 115 116 return iface; 117 } 118 EXPORT_SYMBOL_GPL(sfp_parse_interface); 119 120 void sfp_parse_support(struct sfp_bus *bus, const struct sfp_eeprom_id *id, 121 unsigned long *support) 122 { 123 phylink_set(support, Autoneg); 124 phylink_set(support, Pause); 125 phylink_set(support, Asym_Pause); 126 127 /* Set ethtool support from the compliance fields. */ 128 if (id->base.e10g_base_sr) 129 phylink_set(support, 10000baseSR_Full); 130 if (id->base.e10g_base_lr) 131 phylink_set(support, 10000baseLR_Full); 132 if (id->base.e10g_base_lrm) 133 phylink_set(support, 10000baseLRM_Full); 134 if (id->base.e10g_base_er) 135 phylink_set(support, 10000baseER_Full); 136 if (id->base.e1000_base_sx || 137 id->base.e1000_base_lx || 138 id->base.e1000_base_cx) 139 phylink_set(support, 1000baseX_Full); 140 if (id->base.e1000_base_t) { 141 phylink_set(support, 1000baseT_Half); 142 phylink_set(support, 1000baseT_Full); 143 } 144 145 switch (id->base.extended_cc) { 146 case 0x00: /* Unspecified */ 147 break; 148 case 0x02: /* 100Gbase-SR4 or 25Gbase-SR */ 149 phylink_set(support, 100000baseSR4_Full); 150 phylink_set(support, 25000baseSR_Full); 151 break; 152 case 0x03: /* 100Gbase-LR4 or 25Gbase-LR */ 153 case 0x04: /* 100Gbase-ER4 or 25Gbase-ER */ 154 phylink_set(support, 100000baseLR4_ER4_Full); 155 break; 156 case 0x0b: /* 100Gbase-CR4 or 25Gbase-CR CA-L */ 157 case 0x0c: /* 25Gbase-CR CA-S */ 158 case 0x0d: /* 25Gbase-CR CA-N */ 159 phylink_set(support, 100000baseCR4_Full); 160 phylink_set(support, 25000baseCR_Full); 161 break; 162 default: 163 dev_warn(bus->sfp_dev, 164 "Unknown/unsupported extended compliance code: 0x%02x\n", 165 id->base.extended_cc); 166 break; 167 } 168 169 /* For fibre channel SFP, derive possible BaseX modes */ 170 if (id->base.fc_speed_100 || 171 id->base.fc_speed_200 || 172 id->base.fc_speed_400) { 173 if (id->base.br_nominal >= 31) 174 phylink_set(support, 2500baseX_Full); 175 if (id->base.br_nominal >= 12) 176 phylink_set(support, 1000baseX_Full); 177 } 178 179 switch (id->base.connector) { 180 case SFP_CONNECTOR_SC: 181 case SFP_CONNECTOR_FIBERJACK: 182 case SFP_CONNECTOR_LC: 183 case SFP_CONNECTOR_MT_RJ: 184 case SFP_CONNECTOR_MU: 185 case SFP_CONNECTOR_OPTICAL_PIGTAIL: 186 break; 187 188 case SFP_CONNECTOR_UNSPEC: 189 if (id->base.e1000_base_t) 190 break; 191 192 case SFP_CONNECTOR_SG: /* guess */ 193 case SFP_CONNECTOR_MPO_1X12: 194 case SFP_CONNECTOR_MPO_2X16: 195 case SFP_CONNECTOR_HSSDC_II: 196 case SFP_CONNECTOR_COPPER_PIGTAIL: 197 case SFP_CONNECTOR_NOSEPARATE: 198 case SFP_CONNECTOR_MXC_2X16: 199 default: 200 /* a guess at the supported link modes */ 201 dev_warn(bus->sfp_dev, 202 "Guessing link modes, please report...\n"); 203 phylink_set(support, 1000baseT_Half); 204 phylink_set(support, 1000baseT_Full); 205 break; 206 } 207 } 208 EXPORT_SYMBOL_GPL(sfp_parse_support); 209 210 static LIST_HEAD(sfp_buses); 211 static DEFINE_MUTEX(sfp_mutex); 212 213 static const struct sfp_upstream_ops *sfp_get_upstream_ops(struct sfp_bus *bus) 214 { 215 return bus->registered ? bus->upstream_ops : NULL; 216 } 217 218 static struct sfp_bus *sfp_bus_get(struct device_node *np) 219 { 220 struct sfp_bus *sfp, *new, *found = NULL; 221 222 new = kzalloc(sizeof(*new), GFP_KERNEL); 223 224 mutex_lock(&sfp_mutex); 225 226 list_for_each_entry(sfp, &sfp_buses, node) { 227 if (sfp->device_node == np) { 228 kref_get(&sfp->kref); 229 found = sfp; 230 break; 231 } 232 } 233 234 if (!found && new) { 235 kref_init(&new->kref); 236 new->device_node = np; 237 list_add(&new->node, &sfp_buses); 238 found = new; 239 new = NULL; 240 } 241 242 mutex_unlock(&sfp_mutex); 243 244 kfree(new); 245 246 return found; 247 } 248 249 static void sfp_bus_release(struct kref *kref) __releases(sfp_mutex) 250 { 251 struct sfp_bus *bus = container_of(kref, struct sfp_bus, kref); 252 253 list_del(&bus->node); 254 mutex_unlock(&sfp_mutex); 255 kfree(bus); 256 } 257 258 static void sfp_bus_put(struct sfp_bus *bus) 259 { 260 kref_put_mutex(&bus->kref, sfp_bus_release, &sfp_mutex); 261 } 262 263 static int sfp_register_bus(struct sfp_bus *bus) 264 { 265 const struct sfp_upstream_ops *ops = bus->upstream_ops; 266 int ret; 267 268 if (ops) { 269 if (ops->link_down) 270 ops->link_down(bus->upstream); 271 if (ops->connect_phy && bus->phydev) { 272 ret = ops->connect_phy(bus->upstream, bus->phydev); 273 if (ret) 274 return ret; 275 } 276 } 277 if (bus->started) 278 bus->socket_ops->start(bus->sfp); 279 bus->registered = true; 280 return 0; 281 } 282 283 static void sfp_unregister_bus(struct sfp_bus *bus) 284 { 285 const struct sfp_upstream_ops *ops = bus->upstream_ops; 286 287 if (bus->registered) { 288 if (bus->started) 289 bus->socket_ops->stop(bus->sfp); 290 if (bus->phydev && ops && ops->disconnect_phy) 291 ops->disconnect_phy(bus->upstream); 292 } 293 bus->registered = false; 294 } 295 296 int sfp_get_module_info(struct sfp_bus *bus, struct ethtool_modinfo *modinfo) 297 { 298 if (!bus->registered) 299 return -ENOIOCTLCMD; 300 return bus->socket_ops->module_info(bus->sfp, modinfo); 301 } 302 EXPORT_SYMBOL_GPL(sfp_get_module_info); 303 304 int sfp_get_module_eeprom(struct sfp_bus *bus, struct ethtool_eeprom *ee, 305 u8 *data) 306 { 307 if (!bus->registered) 308 return -ENOIOCTLCMD; 309 return bus->socket_ops->module_eeprom(bus->sfp, ee, data); 310 } 311 EXPORT_SYMBOL_GPL(sfp_get_module_eeprom); 312 313 void sfp_upstream_start(struct sfp_bus *bus) 314 { 315 if (bus->registered) 316 bus->socket_ops->start(bus->sfp); 317 bus->started = true; 318 } 319 EXPORT_SYMBOL_GPL(sfp_upstream_start); 320 321 void sfp_upstream_stop(struct sfp_bus *bus) 322 { 323 if (bus->registered) 324 bus->socket_ops->stop(bus->sfp); 325 bus->started = false; 326 } 327 EXPORT_SYMBOL_GPL(sfp_upstream_stop); 328 329 struct sfp_bus *sfp_register_upstream(struct device_node *np, 330 struct net_device *ndev, void *upstream, 331 const struct sfp_upstream_ops *ops) 332 { 333 struct sfp_bus *bus = sfp_bus_get(np); 334 int ret = 0; 335 336 if (bus) { 337 rtnl_lock(); 338 bus->upstream_ops = ops; 339 bus->upstream = upstream; 340 bus->netdev = ndev; 341 342 if (bus->sfp) 343 ret = sfp_register_bus(bus); 344 rtnl_unlock(); 345 } 346 347 if (ret) { 348 sfp_bus_put(bus); 349 bus = NULL; 350 } 351 352 return bus; 353 } 354 EXPORT_SYMBOL_GPL(sfp_register_upstream); 355 356 void sfp_unregister_upstream(struct sfp_bus *bus) 357 { 358 rtnl_lock(); 359 sfp_unregister_bus(bus); 360 bus->upstream = NULL; 361 bus->netdev = NULL; 362 rtnl_unlock(); 363 364 sfp_bus_put(bus); 365 } 366 EXPORT_SYMBOL_GPL(sfp_unregister_upstream); 367 368 /* Socket driver entry points */ 369 int sfp_add_phy(struct sfp_bus *bus, struct phy_device *phydev) 370 { 371 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 372 int ret = 0; 373 374 if (ops && ops->connect_phy) 375 ret = ops->connect_phy(bus->upstream, phydev); 376 377 if (ret == 0) 378 bus->phydev = phydev; 379 380 return ret; 381 } 382 EXPORT_SYMBOL_GPL(sfp_add_phy); 383 384 void sfp_remove_phy(struct sfp_bus *bus) 385 { 386 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 387 388 if (ops && ops->disconnect_phy) 389 ops->disconnect_phy(bus->upstream); 390 bus->phydev = NULL; 391 } 392 EXPORT_SYMBOL_GPL(sfp_remove_phy); 393 394 void sfp_link_up(struct sfp_bus *bus) 395 { 396 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 397 398 if (ops && ops->link_up) 399 ops->link_up(bus->upstream); 400 } 401 EXPORT_SYMBOL_GPL(sfp_link_up); 402 403 void sfp_link_down(struct sfp_bus *bus) 404 { 405 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 406 407 if (ops && ops->link_down) 408 ops->link_down(bus->upstream); 409 } 410 EXPORT_SYMBOL_GPL(sfp_link_down); 411 412 int sfp_module_insert(struct sfp_bus *bus, const struct sfp_eeprom_id *id) 413 { 414 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 415 int ret = 0; 416 417 if (ops && ops->module_insert) 418 ret = ops->module_insert(bus->upstream, id); 419 420 return ret; 421 } 422 EXPORT_SYMBOL_GPL(sfp_module_insert); 423 424 void sfp_module_remove(struct sfp_bus *bus) 425 { 426 const struct sfp_upstream_ops *ops = sfp_get_upstream_ops(bus); 427 428 if (ops && ops->module_remove) 429 ops->module_remove(bus->upstream); 430 } 431 EXPORT_SYMBOL_GPL(sfp_module_remove); 432 433 struct sfp_bus *sfp_register_socket(struct device *dev, struct sfp *sfp, 434 const struct sfp_socket_ops *ops) 435 { 436 struct sfp_bus *bus = sfp_bus_get(dev->of_node); 437 int ret = 0; 438 439 if (bus) { 440 rtnl_lock(); 441 bus->sfp_dev = dev; 442 bus->sfp = sfp; 443 bus->socket_ops = ops; 444 445 if (bus->netdev) 446 ret = sfp_register_bus(bus); 447 rtnl_unlock(); 448 } 449 450 if (ret) { 451 sfp_bus_put(bus); 452 bus = NULL; 453 } 454 455 return bus; 456 } 457 EXPORT_SYMBOL_GPL(sfp_register_socket); 458 459 void sfp_unregister_socket(struct sfp_bus *bus) 460 { 461 rtnl_lock(); 462 sfp_unregister_bus(bus); 463 bus->sfp_dev = NULL; 464 bus->sfp = NULL; 465 bus->socket_ops = NULL; 466 rtnl_unlock(); 467 468 sfp_bus_put(bus); 469 } 470 EXPORT_SYMBOL_GPL(sfp_unregister_socket); 471