From nobody Mon Jun 15 11:56:16 2026 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 281353C2B8D for ; Sun, 14 Jun 2026 13:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781444070; cv=none; b=e3S8wMbMozeoyFGOTTUlTb1t2+rMDXW2U+lS7f+BSwtKUixn0QD3uknFm5T996u57gO5OEMb/xFyzDwB7zQm2l64hvWMphyj4DyWC2KG1tB4/q5hnqOWPcjpzJIb2Fs9+9RHwsR/Y2oziiPC41J/JQr5XB7xL45x+apo4LzymHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781444070; c=relaxed/simple; bh=vC7gffF3nph/f7RymsI38P5G/8V/ZI0hrB57Y33Oh98=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L8gaGR7SMPLkpoNnbtiNqXzycoRMfgi4Fcs7WWgpNvIupu8rFUmRvV52oFCnCZB6nk6RVd7JicBbxjiysCuSPeLezbsWiERfmo0W+BTkuxTV22NYf6/m7PBvBmAdN2JMVd4VHjc833fJiuVmh8HPIfcjE3x6uRZrispOpBqmQAs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PTv74Etg; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PTv74Etg" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-490ace40f4bso23364555e9.3 for ; Sun, 14 Jun 2026 06:34:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781444067; x=1782048867; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eyKTqLVZ2GkXstSMIq5XRH7NFzj7gG6vhpUIFQhTa+U=; b=PTv74EtgR7h/IDwrdicV+UdkDb5WD//UcSf0s4FsTNhMiL7CYYvS/an1DeN7frH+kh Dg+hyx//17TbzPYImns4FaEMoOPJ0Qcz5AS1LRMPv6KwvUgKF2puCeAnVpkdjvUL4EVS 4JQ/YUBRIqtIGJlkHO6NRFNFDW9DGXk+183NTsmkUZbhWLKGCaQYPgA3Sndt98CaWgJh m7HCMREMQveOIwu1JkctLf2AhMbhxEnPLHjVYqU6+IVCP6nr5EXJZvXqQdjMVFNWnHlt slkIi+7TPKqOPunOklcUgUpR3gSWOvrnVYIVNoes5CsqDb+1gRUyxzRLg14n066DjyHe bDAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781444067; x=1782048867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=eyKTqLVZ2GkXstSMIq5XRH7NFzj7gG6vhpUIFQhTa+U=; b=lg1eHy2xUqq1MU9Hni+i/bID4NaFSRzuSavbMZwIWfLN0SGFWEGHx9XDxJ+hxf3ZtM gBghMJB3utIHQbdWzjS+3QA4TbaWEt7IWkr7PBxhITBGfBDGc2N/3NQxlvoUq1qtZaRA Pa8uVv+6LzXmERnIoq5dO5YCipAKO+hielz2SG0wIZSodgGS9ZdhE8pSErcsqQhBVoxR 0b8fnCQW/NkHQunmSktKdu6kQKfQsf5HRpUMOzCbfHwQRou2CelEKly0mh0/RvOz/UQB KObFT+m6QeT6KIw1weIgKyjzxHVH8drqMaW1Qx9UFjUWbOq3BDLe8xYE/ugurzV6yejf ZY6w== X-Forwarded-Encrypted: i=1; AFNElJ/arrdAaG6MgXXk2LbQp8qbk/D3BrIWk+PScIN3b/wrtopdNj6iYClkOUan2eD0wCtilDSKi3rbmCQg/rs=@vger.kernel.org X-Gm-Message-State: AOJu0YwpW7Nu92M5y//nb7iaHtt6wVGtnFlDB0vfNB0jCJTzMBsNOD50 nil6ol8hLQnR8SkGbqfg5T9AvRa/ifIcoPHNReH8NQWY7ACi28JDX8Ec X-Gm-Gg: Acq92OGrw7/Qi2igal5J61ifpz88Tqom8E5/qUE4lXadNlg22RzlYVvDcsvsdZTwa7f b/TGzJR3RnZeCNX1jQ0x11KMwExOWI+F+h5KglKxUQSfl66ZQDbNpRs6Bl+CXxJY8p5d9ov5cde Tq6EEH4maoLRY7d8/e6aMIS1Ywfc0zt9rBqAVezPkwfO4xvG9K+AEdtZLD3eBJE/9sqVTpHPCn3 3mB5XiPyWNGRwmQR617pUYLt+Hp9FRah2btVdYRSyPT6C1xmJti0p+Y/ANeaFsEGYwwSdOY/2VH hohSg1QgX3M9dvuXDPOfztzlrVIgMzgBCNhf46Al34kqC67ImiUW9C1oJl3rxW9RAlXbjw3382V OUKqZvomvc71U/G/5KRmCqtKf7nuCZpQaViCTKLL4I1aZNaLj0ILZB1Q2QPxoybQSMHn12WiyEj SFJG6KKOUMo4sZkfKnxm4GtgVhUQ== X-Received: by 2002:a05:600c:4709:b0:490:5057:f5f7 with SMTP id 5b1f17b1804b1-4922007472cmr83103395e9.11.1781444067551; Sun, 14 Jun 2026 06:34:27 -0700 (PDT) Received: from builder ([2001:9e8:f128:d516:be24:11ff:fe30:5d85]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4922037055csm169464295e9.13.2026.06.14.06.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jun 2026 06:34:26 -0700 (PDT) From: Jonas Jelonek To: Russell King , Andrew Lunn , Heiner Kallweit , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Chevallier Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Bj=C3=B8rn=20Mork?= , Simon Horman , Jonas Jelonek Subject: [PATCH net-next v11 1/2] net: sfp: apply I2C adapter quirks to limit block size Date: Sun, 14 Jun 2026 13:34:17 +0000 Message-ID: <20260614133418.2068201-2-jelonek.jonas@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260614133418.2068201-1-jelonek.jonas@gmail.com> References: <20260614133418.2068201-1-jelonek.jonas@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The SFP driver assumes all I2C adapters support reading and writing the pre-defined block size SFP_EEPROM_BLOCK_SIZE of 16 bytes. This constant was probably chosen based on good guesses and known limitations of a range of I2C adapters and SFP modules. However, I2C adapters may even support less and usually need to specify this via I2C quirks. Theoretically, such an adapter may provide full functionality but only support a read and write length of e.g. 8 bytes. Currently, the SFP driver doesn't account for that. Add handling for I2C quirks in SFP I2C configuration taking the fields max_read_len and max_write_len in struct i2c_adapter_quirks into account to further limit the maximum block size if needed. Signed-off-by: Jonas Jelonek Reviewed-by: Maxime Chevallier --- drivers/net/phy/sfp.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c index 18f2584dbe7b..3c232991faa1 100644 --- a/drivers/net/phy/sfp.c +++ b/drivers/net/phy/sfp.c @@ -810,21 +810,29 @@ static int sfp_smbus_byte_write(struct sfp *sfp, bool= a2, u8 dev_addr, =20 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) { + size_t max_block_size; + sfp->i2c =3D i2c; =20 if (i2c_check_functionality(i2c, I2C_FUNC_I2C)) { sfp->read =3D sfp_i2c_read; sfp->write =3D sfp_i2c_write; - sfp->i2c_max_block_size =3D SFP_EEPROM_BLOCK_SIZE; + max_block_size =3D SFP_EEPROM_BLOCK_SIZE; } else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) { sfp->read =3D sfp_smbus_byte_read; sfp->write =3D sfp_smbus_byte_write; - sfp->i2c_max_block_size =3D 1; + max_block_size =3D 1; } else { sfp->i2c =3D NULL; return -EINVAL; } =20 + if (i2c->quirks && i2c->quirks->max_read_len) + max_block_size =3D min(max_block_size, i2c->quirks->max_read_len); + if (i2c->quirks && i2c->quirks->max_write_len) + max_block_size =3D min(max_block_size, i2c->quirks->max_write_len); + + sfp->i2c_max_block_size =3D max_block_size; sfp->i2c_block_size =3D sfp->i2c_max_block_size; return 0; } --=20 2.51.0 From nobody Mon Jun 15 11:56:16 2026 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C8433C3BFF for ; Sun, 14 Jun 2026 13:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781444072; cv=none; b=EX7jZRt+9lmaBRvTGaDw6+EBNId3oZ7rioGEBPPYTFCcpysn6SgQuFFmwMjbu7edCMnk3gkLFQw+2c1WShJ997Es3Pz5vfLaVCq26NcWSisR5UtLkdgpbQ6xk7xH7iV2e1Z2m5JABJJ5hvfVm6RiWzFV4Ck7/3Uu4yaws/EinfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781444072; c=relaxed/simple; bh=g1OYQgDXdmu4vKLoiJxgBAh4o+NIpeubIuoEVKltD+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XdDXnKh/VwB3p7NQkxI7lLAfuhZA0zHGxp9HVE3F2x/XUmuvjtlYboDcAuK0DaPwilfSIn1OpOBBQhSQHunm6/6xHbg/nOPc8h4ySR+GoxL25snZ04jO2BEPfRbDEfrlcnF+HjvGmYGPeHc4seNVYRoGBGTRqVMvkUm+SH/n+2g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KgYKBBdg; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KgYKBBdg" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4905529b933so24797215e9.0 for ; Sun, 14 Jun 2026 06:34:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781444069; x=1782048869; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GhyiKHRi9/dLAnwgiKdl2GrPJD0Zfu0QCwVOddGKdS0=; b=KgYKBBdg9fcq+tb8Wdw0mxnq3+qnMbcDBoa6nMBPT2o3xnKN0em6abhVya0d/MbP5m GlILPSr9aYqWgkrM1iqeWmmphrBpSWPQUXMpwxptqtasRalNSzgIjyAxjpsMf/mknYTY 9uEFVbvdVvnHXFXX7wHmTT0kJK+iS8ISdTTpX7gQuaxRWj4d606RPMuWQ08kk6Fg/wGP wgHCFII4jENu/n25a/OFHIRSQUs/fULKNLohUmm+oppAhpdd5h0rxJ+4gFueg6tV2Ii/ egJfMBB7pROe8UTQwG5aGXsB1B8vq7tLTA/sgYCM6RJb9uXfp170P7kxVOe29N6URb2W fmQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781444069; x=1782048869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=GhyiKHRi9/dLAnwgiKdl2GrPJD0Zfu0QCwVOddGKdS0=; b=p6UNN4G5SKOkq3LJ1T1EFM9drgTzw6eunFiQxEa/cUWHoO9Z7KX8U9aSTtnWoJbcos vHG083rUuxCc3PSLzfO4KohfrKy/ptm5tGtWds3SS4Z+GIGZAEBUikqWg1tI+9PAumMY WH1AlYxCa+sjLUIXwFwuX33if85ekg7Ljz1eqLn5hWxo+7xuQMaaavsvXJG55niHb2RD i2PlqXrFA79mq8uqNEN/RRWavpoVnwWCtF6+R/gVo8qcYxExbzstrHUMOjH5TGZ6xHqw eoECznjvHXCoHQ2C1jbnP35e/5+2VG55sT/yZC4o1d7Msd6+5PdwsWkCCKkmP4KTQYqY WUQA== X-Forwarded-Encrypted: i=1; AFNElJ++x8AScV1nT1nOQWVPpf8xfPayiJ2gRo7O3cr9Y96eGlJBEYJZvgbErA2BoFFBooA2DL4sHI1DDyQbkrg=@vger.kernel.org X-Gm-Message-State: AOJu0YxrjVDCVS8YA6eUbrqUuGw00vFPcijTrWR8Ajj8RsDqT/Sgo82W RFHiCh0ArBsFjty33Hl/qjfO0kb6hsuWXhJ7isnZYD9ucUBIyerh64ag X-Gm-Gg: Acq92OG7gJuPdb8f7dxGWWdp6FjSAss8xqcd3utNm/UVBv7XN6aUsIFzS/copjl0ROe zeOTU/dcS2lG4HDqoiM082qaDKOUdXnYx0flJG1ock9jw1x9LLQN7y13melnG+EzJ1HDoxsCy2m goWqcCk3ZMqKUWguYHx6eLUerdXeAn9m9SLO8b2ibYHF0nFAgWr00D2QFXdAXKJpiq624vpBXmd Nyu+1tXm8Ct63TJaxkscAm9afH1zT/5YK9gNUcMBbtHelfILzg2427ls1aq5vylocrerko5KpZ5 lqwlzRFmAdmc6uVuKb248fkvrrvdRovz0ZFWwXfretM7EiidptMoF77a4/pcGr8u8mh1dXZNhlC a86ohZuk9ylhZ1oMkGmzQA0yWxbOUN9qbYogavtD5Zw6LQlPmHFUvceHnh93EBO7F0fKzno8aht Jt0urYXX764wlDD/ooCMSmYskW/g== X-Received: by 2002:a05:600c:2d53:b0:488:b187:3c with SMTP id 5b1f17b1804b1-490ec4b9023mr79949255e9.14.1781444068575; Sun, 14 Jun 2026 06:34:28 -0700 (PDT) Received: from builder ([2001:9e8:f128:d516:be24:11ff:fe30:5d85]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4922037055csm169464295e9.13.2026.06.14.06.34.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jun 2026 06:34:28 -0700 (PDT) From: Jonas Jelonek To: Russell King , Andrew Lunn , Heiner Kallweit , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Chevallier Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Bj=C3=B8rn=20Mork?= , Simon Horman , Jonas Jelonek Subject: [PATCH net-next v11 2/2] net: sfp: extend SMBus support Date: Sun, 14 Jun 2026 13:34:18 +0000 Message-ID: <20260614133418.2068201-3-jelonek.jonas@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260614133418.2068201-1-jelonek.jonas@gmail.com> References: <20260614133418.2068201-1-jelonek.jonas@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 7662abf4db94 ("net: phy: sfp: Add support for SMBus module access") added SMBus access for SFP modules, but limited it to single-byte transfers. As a side effect, hwmon is disabled (16-bit reads cannot be guaranteed atomic) and a warning is printed. Many SMBus-only I2C controllers in the wild support more than just byte access, and SFP cages are often wired to such controllers rather than to a full-featured I2C controller -- e.g. the SMBus controllers in the Realtek longan and mango SoCs, which advertise word access and I2C block reads. Today, they cannot drive an SFP at all without falling back to the byte-only path. Extend sfp_smbus_read()/sfp_smbus_write() so that, in addition to the existing byte access, they also use SMBus word access and SMBus I2C block access whenever the adapter advertises them. Both directions are handled in a single read and a single write helper that pick the largest supported transfer per chunk and fall back as needed. I2C-block is preferred unconditionally when available: the protocol carries any length 1..32, so it can serve every chunk -- including the 1- and 2-byte tails -- without help from word or byte access. Note that this requires I2C_FUNC_SMBUS_I2C_BLOCK, which reads a caller-specified number of bytes. This deviates from the official SMBus Block Read (length is supplied by the slave) but is widely supported by Linux I2C controllers/drivers. Capability matrix this implementation supports: - BYTE only: works (unchanged behaviour); 1-byte xfers, hwmon disabled. - BYTE + WORD: word for >=3D2-byte chunks, byte for trailing odd byte. - I2C_BLOCK present (with or without BYTE/WORD): block as the universal transport for every chunk. - WORD only (no BYTE/BLOCK): accepted with WARN_ONCE. Even-length transfers work; odd-length transfers (e.g. the 3-byte cotsworks fixup write) hit the BYTE branch which the adapter does not implement, so the xfer returns an error and the operation is aborted. No mainline I2C driver was found to advertise WORD without BYTE; the warning lets us learn about it if it ever shows up. Adapters with asymmetric R/W capabilities (e.g. only READ_I2C_BLOCK but not WRITE_I2C_BLOCK) remain functionally correct -- the per-iteration fallback uses the direction-specific bits -- but the shared i2c_max_block_size is sized by the all-bits-set check, so a transfer in the better-supported direction is not upgraded. None of the mainline I2C bus drivers surveyed during review advertise such asymmetry; promoting i2c_max_block_size to per-direction sizes can be revisited if needed. Signed-off-by: Jonas Jelonek Reviewed-by: Maxime Chevallier --- drivers/net/phy/sfp.c | 139 +++++++++++++++++++++++++++++++++--------- 1 file changed, 111 insertions(+), 28 deletions(-) diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c index 3c232991faa1..f8136c935808 100644 --- a/drivers/net/phy/sfp.c +++ b/drivers/net/phy/sfp.c @@ -14,6 +14,7 @@ #include #include #include +#include #include =20 #include "sfp.h" @@ -759,50 +760,113 @@ static int sfp_i2c_write(struct sfp *sfp, bool a2, u= 8 dev_addr, void *buf, return ret =3D=3D ARRAY_SIZE(msgs) ? len : 0; } =20 -static int sfp_smbus_byte_read(struct sfp *sfp, bool a2, u8 dev_addr, - void *buf, size_t len) +static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, + size_t len) { - union i2c_smbus_data smbus_data; + union i2c_smbus_data smbus_data =3D {0}; u8 bus_addr =3D a2 ? 0x51 : 0x50; + size_t this_len, transferred; + u32 functionality; u8 *data =3D buf; int ret; =20 - while (len) { - ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, - I2C_SMBUS_READ, dev_addr, - I2C_SMBUS_BYTE_DATA, &smbus_data); - if (ret < 0) - return ret; + functionality =3D i2c_get_functionality(sfp->i2c); =20 - *data =3D smbus_data.byte; + while (len) { + this_len =3D min(len, sfp->i2c_block_size); + + if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) { + smbus_data.block[0] =3D this_len; + ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, + I2C_SMBUS_READ, dev_addr, + I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data); + if (ret < 0) + return ret; + + transferred =3D min_t(size_t, smbus_data.block[0], this_len); + if (!transferred) + return -EIO; + + memcpy(data, &smbus_data.block[1], transferred); + } else if (this_len >=3D 2 && + (functionality & I2C_FUNC_SMBUS_READ_WORD_DATA)) { + ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, + I2C_SMBUS_READ, dev_addr, + I2C_SMBUS_WORD_DATA, &smbus_data); + if (ret < 0) + return ret; + + put_unaligned_le16(smbus_data.word, data); + transferred =3D 2; + } else { + ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, + I2C_SMBUS_READ, dev_addr, + I2C_SMBUS_BYTE_DATA, &smbus_data); + if (ret < 0) + return ret; + + *data =3D smbus_data.byte; + transferred =3D 1; + } =20 - len--; - data++; - dev_addr++; + data +=3D transferred; + len -=3D transferred; + dev_addr +=3D transferred; } =20 return data - (u8 *)buf; } =20 -static int sfp_smbus_byte_write(struct sfp *sfp, bool a2, u8 dev_addr, - void *buf, size_t len) +static int sfp_smbus_write(struct sfp *sfp, bool a2, u8 dev_addr, void *bu= f, + size_t len) { union i2c_smbus_data smbus_data; u8 bus_addr =3D a2 ? 0x51 : 0x50; + size_t this_len, transferred; + u32 functionality; u8 *data =3D buf; int ret; =20 + functionality =3D i2c_get_functionality(sfp->i2c); + while (len) { - smbus_data.byte =3D *data; - ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, - I2C_SMBUS_WRITE, dev_addr, - I2C_SMBUS_BYTE_DATA, &smbus_data); - if (ret) - return ret; + this_len =3D min(len, sfp->i2c_block_size); + + if (functionality & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) { + smbus_data.block[0] =3D this_len; + memcpy(&smbus_data.block[1], data, this_len); + + ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, + I2C_SMBUS_WRITE, dev_addr, + I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data); + if (ret < 0) + return ret; + + transferred =3D this_len; + } else if (this_len >=3D 2 && + (functionality & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { + smbus_data.word =3D get_unaligned_le16(data); + ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, + I2C_SMBUS_WRITE, dev_addr, + I2C_SMBUS_WORD_DATA, &smbus_data); + if (ret < 0) + return ret; + + transferred =3D 2; + } else { + smbus_data.byte =3D *data; + ret =3D i2c_smbus_xfer(sfp->i2c, bus_addr, 0, + I2C_SMBUS_WRITE, dev_addr, + I2C_SMBUS_BYTE_DATA, &smbus_data); + if (ret < 0) + return ret; + + transferred =3D 1; + } =20 - len--; - data++; - dev_addr++; + data +=3D transferred; + len -=3D transferred; + dev_addr +=3D transferred; } =20 return data - (u8 *)buf; @@ -818,10 +882,29 @@ static int sfp_i2c_configure(struct sfp *sfp, struct = i2c_adapter *i2c) sfp->read =3D sfp_i2c_read; sfp->write =3D sfp_i2c_write; max_block_size =3D SFP_EEPROM_BLOCK_SIZE; - } else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA)) { - sfp->read =3D sfp_smbus_byte_read; - sfp->write =3D sfp_smbus_byte_write; - max_block_size =3D 1; + } else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) || + i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) { + /* Either protocol alone covers any length: I2C-block carries + * 1..32 bytes per xfer, byte iterates one byte at a time. + */ + sfp->read =3D sfp_smbus_read; + sfp->write =3D sfp_smbus_write; + + if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) + max_block_size =3D SFP_EEPROM_BLOCK_SIZE; + else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA)) + max_block_size =3D 2; + else + max_block_size =3D 1; + } else if (WARN_ONCE(i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DAT= A), + "SMBus word-only adapter; odd-length transfers will fail\n")) { + /* Word-only: even-length xfers work; odd-length xfers fall + * to BYTE, which the adapter does not advertise and will + * likely fail. + */ + sfp->read =3D sfp_smbus_read; + sfp->write =3D sfp_smbus_write; + max_block_size =3D 2; } else { sfp->i2c =3D NULL; return -EINVAL; --=20 2.51.0