Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178371974
D60066.1796605707.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None
D60066.1796605707.diff
View Options
Index: sys/conf/files
===================================================================
--- sys/conf/files
+++ sys/conf/files
@@ -3171,6 +3171,7 @@
dev/sdio/sdio_subr.c optional mmccam
dev/sdio/sdiob.c optional mmccam
dev/sff/sff_if.m optional sff
+dev/sff/sff.c optional sff
dev/sff/sfp_fdt.c optional sff fdt
dev/sge/if_sge.c optional sge pci
dev/siis/siis.c optional siis pci
Index: sys/dev/sff/sff.h
===================================================================
--- /dev/null
+++ sys/dev/sff/sff.h
@@ -0,0 +1,56 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Xiangbo Kong <yarshure@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _DEV_SFF_SFF_H_
+#define _DEV_SFF_SFF_H_
+
+/*
+ * Read from an SFP module's EEPROM over i2c.
+ *
+ * dev_addr is the slave address in the form iic_msg(9) uses -- the 7-bit
+ * address shifted left by one -- which for an SFF-8472 module is 0xa0 for the
+ * base page and 0xa2 for the diagnostics page. That is also the form
+ * SIOCGI2C's struct ifi2creq carries, so a NIC driver passes what it was
+ * given.
+ *
+ * requester is a device on the iicbus the EEPROM answers on; it owns the bus
+ * for the duration of the exchange. A front-end that is itself an i2c slave
+ * (the ACPI one) passes itself; one that only holds a reference to the bus
+ * (the FDT one) has to borrow a device on it -- see sff_i2c_requester().
+ */
+int sff_read_eeprom(device_t requester, uint8_t dev_addr, uint8_t offset,
+ uint8_t *buf, int len);
+
+/*
+ * A bus request needs an owner that sits on the bus being requested. A
+ * front-end that has only the iicbus itself borrows the iic(4) child every
+ * iicbus carries. Returns NULL if there is none, which sff_read_eeprom()
+ * reports as ENXIO.
+ */
+device_t sff_i2c_requester(device_t i2c_bus);
+
+#endif /* _DEV_SFF_SFF_H_ */
Index: sys/dev/sff/sff.c
===================================================================
--- /dev/null
+++ sys/dev/sff/sff.c
@@ -0,0 +1,100 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Xiangbo Kong <yarshure@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Small Form Factor (SFF) Committee Pluggable (SFP) Transceiver: shared i2c
+ * helpers used by the bus-specific front-ends (sfp_fdt, ...) and by NIC drivers
+ * that must drive an i2c mux themselves.
+ */
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/bus.h>
+#include <sys/module.h>
+
+#include <dev/iicbus/iic.h>
+#include <dev/iicbus/iiconf.h>
+
+#include "sff.h"
+
+int
+sff_read_eeprom(device_t requester, uint8_t dev_addr, uint8_t offset,
+ uint8_t *buf, int len)
+{
+ struct iic_msg msgs[2];
+
+ if (requester == NULL)
+ return (ENXIO);
+ if (buf == NULL || len <= 0 || len > UINT16_MAX)
+ return (EINVAL);
+
+ /*
+ * Write the byte offset, then read the data back without releasing
+ * the bus in between (a repeat-start), so nothing else can move the
+ * EEPROM's internal address pointer between the two halves. Holding
+ * the bus for the whole exchange also keeps an i2c mux upstream
+ * pointed at this device throughout -- iicbus(4) switches the mux as
+ * part of granting the bus, and would switch it away again for
+ * another consumer if we let go.
+ */
+ msgs[0].slave = dev_addr;
+ msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
+ msgs[0].len = 1;
+ msgs[0].buf = &offset;
+ msgs[1].slave = dev_addr;
+ msgs[1].flags = IIC_M_RD;
+ msgs[1].len = len;
+ msgs[1].buf = buf;
+
+ return (iic2errno(iicbus_transfer_excl(requester, msgs, nitems(msgs),
+ IIC_INTRWAIT)));
+}
+
+device_t
+sff_i2c_requester(device_t i2c_bus)
+{
+
+ return (device_find_child(i2c_bus, "iic", DEVICE_UNIT_ANY));
+}
+
+static int
+sff_modevent(module_t mod __unused, int type, void *data __unused)
+{
+ switch (type) {
+ case MOD_LOAD:
+ case MOD_UNLOAD:
+ case MOD_QUIESCE:
+ return (0);
+ default:
+ return (EOPNOTSUPP);
+ }
+}
+
+static moduledata_t sff_moduledata = { "sff", sff_modevent, NULL };
+DECLARE_MODULE(sff, sff_moduledata, SI_SUB_DRIVERS, SI_ORDER_ANY);
+MODULE_VERSION(sff, 1);
+MODULE_DEPEND(sff, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
Index: sys/dev/sff/sff_if.m
===================================================================
--- sys/dev/sff/sff_if.m
+++ sys/dev/sff/sff_if.m
@@ -29,7 +29,42 @@
INTERFACE sff;
+#
+# A transceiver front-end need not implement every method: a platform may be
+# able to read the EEPROM but have no way to reach the module's control lines,
+# or the reverse. kobj(9) turns an unimplemented method without a DEFAULT into
+# a panic, so give both of them one that simply says "not here".
+#
+CODE {
+ static int
+ null_get_i2c_bus(device_t dev, device_t *i2c_bus)
+ {
+
+ return (ENXIO);
+ }
+
+ static int
+ null_read_eeprom(device_t dev, uint8_t dev_addr, uint8_t offset,
+ uint8_t *buf, int len)
+ {
+
+ return (ENXIO);
+ }
+};
+
METHOD int get_i2c_bus {
device_t dev;
device_t *i2c_bus;
-};
+} DEFAULT null_get_i2c_bus;
+
+#
+# Read from an SFP module EEPROM page (dev_addr is the 8-bit page address,
+# e.g. 0xA0 base / 0xA2 diagnostics) starting at the given byte offset.
+#
+METHOD int read_eeprom {
+ device_t dev;
+ uint8_t dev_addr;
+ uint8_t offset;
+ uint8_t *buf;
+ int len;
+} DEFAULT null_read_eeprom;
Index: sys/dev/sff/sfp_fdt.c
===================================================================
--- sys/dev/sff/sfp_fdt.c
+++ sys/dev/sff/sfp_fdt.c
@@ -38,6 +38,7 @@
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/fdt/simplebus.h>
+#include "sff.h"
#include "sff_if.h"
struct sfp_fdt_softc {
@@ -134,6 +135,30 @@
return (0);
}
+static int
+sfp_fdt_read_eeprom(device_t dev, uint8_t dev_addr, uint8_t offset,
+ uint8_t *buf, int len)
+{
+ device_t i2c_bus, requester;
+ int error;
+
+ error = sfp_fdt_get_i2c_bus(dev, &i2c_bus);
+ if (error != 0)
+ return (error);
+
+ /*
+ * The "i2c-bus" phandle points at the (possibly muxed) iicbus the
+ * module EEPROM sits on; any i2c mux is switched transparently by the
+ * i2c framework, so we just need a device on that bus to drive the
+ * transfer (no explicit channel select).
+ */
+ requester = sff_i2c_requester(i2c_bus);
+ if (requester == NULL)
+ return (ENXIO);
+
+ return (sff_read_eeprom(requester, dev_addr, offset, buf, len));
+}
+
static device_method_t sfp_fdt_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, sfp_fdt_probe),
@@ -141,6 +166,7 @@
/* SFF */
DEVMETHOD(sff_get_i2c_bus, sfp_fdt_get_i2c_bus),
+ DEVMETHOD(sff_read_eeprom, sfp_fdt_read_eeprom),
DEVMETHOD_END
};
@@ -152,3 +178,5 @@
BUS_PASS_SUPPORTDEV);
EARLY_DRIVER_MODULE(sfp_fdt, ofwbus, sfp_fdt_driver, 0, 0,
BUS_PASS_SUPPORTDEV);
+MODULE_VERSION(sfp_fdt, 1);
+MODULE_DEPEND(sfp_fdt, sff, 1, 1, 1);
Index: sys/modules/sff/Makefile
===================================================================
--- sys/modules/sff/Makefile
+++ sys/modules/sff/Makefile
@@ -2,12 +2,20 @@
KMOD= sff
+SRCS+= sff.c
SRCS+= sff_if.c sff_if.h
-SRCS+= bus_if.h device_if.h
+SRCS+= bus_if.h device_if.h iicbus_if.h
.if !empty(OPT_FDT)
SRCS+= sfp_fdt.c \
ofw_bus_if.h
.endif
+#
+# sff(4) 存在的意义就是被 NIC 驱动消费:它们需要 sff_read_eeprom() 和
+# SFF_* kobj 方法描述符。没有这行,kmod 构建会把符号全部局部化,
+# 消费者在 kldload 时失败。
+#
+EXPORT_SYMS= YES
+
.include <bsd.kmod.mk>
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 7, 1:08 AM (9 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42268843
Default Alt Text
D60066.1796605707.diff (9 KB)
Attached To
Mode
D60066: sff: add a shared EEPROM read helper and build sff(4) as a module
Attached
Detach File
Event Timeline