xref: /freebsd/lib/libifconfig/libifconfig_sfp.c (revision 709348c21351a783ff0025519d1f7cf884771077)
1 /*-
2  * Copyright (c) 2014, Alexander V. Chernikov
3  * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 
32 #include <net/if.h>
33 #include <net/sff8436.h>
34 #include <net/sff8472.h>
35 
36 #include <math.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <libifconfig.h>
47 #include <libifconfig_internal.h>
48 #include <libifconfig_sfp.h>
49 #include <libifconfig_sfp_tables_internal.h>
50 
51 #define     SFF_8636_EXT_COMPLIANCE 0x80
52 
53 struct i2c_info {
54 	struct ifreq ifr;
55 	ifconfig_handle_t *h;
56 	int error;		/* Store first error */
57 	enum sfp_id id;		/* Module type */
58 };
59 
60 static uint8_t
find_zero_bit(const struct sfp_enum_metadata * table,int value,int sz)61 find_zero_bit(const struct sfp_enum_metadata *table, int value, int sz)
62 {
63 	int v, m;
64 
65 	for (v = 1, m = 1 << (8 * sz); v < m; v <<= 1) {
66 		if ((value & v) == 0)
67 			continue;
68 		if (find_metadata(table, value & v) != NULL) {
69 			return (value & v);
70 		}
71 	}
72 	return (0);
73 }
74 
75 /*
76  * Reads i2c data from opened kernel socket.
77  */
78 static int
read_i2c(struct i2c_info * ii,uint8_t addr,uint8_t off,uint8_t len,uint8_t * buf)79 read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
80     uint8_t *buf)
81 {
82 	struct ifi2creq req;
83 	int i, l;
84 
85 	if (ii->error != 0)
86 		return (ii->error);
87 
88 	ii->ifr.ifr_data = (caddr_t)&req;
89 
90 	i = 0;
91 	l = 0;
92 	memset(&req, 0, sizeof(req));
93 	req.dev_addr = addr;
94 	req.offset = off;
95 	req.len = len;
96 
97 	while (len > 0) {
98 		l = MIN(sizeof(req.data), len);
99 		req.len = l;
100 		if (ifconfig_ioctlwrap(ii->h, AF_LOCAL, SIOCGI2C,
101 		    &ii->ifr) != 0) {
102 			ii->error = errno;
103 			return (errno);
104 		}
105 
106 		memcpy(&buf[i], req.data, l);
107 		len -= l;
108 		i += l;
109 		req.offset += l;
110 	}
111 
112 	return (0);
113 }
114 
115 static int
i2c_info_init(struct i2c_info * ii,ifconfig_handle_t * h,const char * name)116 i2c_info_init(struct i2c_info *ii, ifconfig_handle_t *h, const char *name)
117 {
118 	uint8_t id_byte;
119 
120 	memset(ii, 0, sizeof(*ii));
121 	strlcpy(ii->ifr.ifr_name, name, sizeof(ii->ifr.ifr_name));
122 	ii->h = h;
123 
124 	/*
125 	 * Try to read byte 0 from i2c:
126 	 * Both SFF-8472 and SFF-8436 use it as
127 	 * 'identification byte'.
128 	 * Stop reading status on zero as value -
129 	 * this might happen in case of empty transceiver slot.
130 	 */
131 	id_byte = 0;
132 	read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &id_byte);
133 	if (ii->error != 0)
134 		return (-1);
135 	if (id_byte == 0) {
136 		h->error.errtype = OTHER;
137 		h->error.errcode = ENOENT;
138 		return (-1);
139 	}
140 	ii->id = id_byte;
141 	return (0);
142 }
143 
144 static int
get_sfp_info(struct i2c_info * ii,struct ifconfig_sfp_info * sfp)145 get_sfp_info(struct i2c_info *ii, struct ifconfig_sfp_info *sfp)
146 {
147 	uint8_t code;
148 
149 	read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &sfp->sfp_id);
150 	read_i2c(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, &sfp->sfp_conn);
151 
152 	/* Use extended compliance code if it's valid */
153 	read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS, 1, &sfp->sfp_eth_ext);
154 	if (sfp->sfp_eth_ext == 0) {
155 		/* Next, check 10G Ethernet/IB CCs */
156 		read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, &code);
157 		sfp->sfp_eth_10g = find_zero_bit(sfp_eth_10g_table, code, 1);
158 		if (sfp->sfp_eth_10g == 0) {
159 			/* No match. Try Ethernet 1G */
160 			read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
161 			    1, &code);
162 			sfp->sfp_eth = find_zero_bit(sfp_eth_table, code, 1);
163 		}
164 	}
165 
166 	return (ii->error);
167 }
168 
169 static int
get_qsfp_info(struct i2c_info * ii,struct ifconfig_sfp_info * sfp)170 get_qsfp_info(struct i2c_info *ii, struct ifconfig_sfp_info *sfp)
171 {
172 	uint8_t code;
173 
174 	read_i2c(ii, SFF_8436_BASE, SFF_8436_ID, 1, &sfp->sfp_id);
175 	read_i2c(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, &sfp->sfp_conn);
176 
177 	read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &sfp->sfp_rev);
178 
179 	/* Check for extended specification compliance */
180 	read_i2c(ii, SFF_8436_BASE, SFF_8436_CODE_E1040100G, 1, &code);
181 	if (code & SFF_8636_EXT_COMPLIANCE) {
182 		read_i2c(ii, SFF_8436_BASE, SFF_8436_OPTIONS_START, 1,
183 		    &sfp->sfp_eth_ext);
184 		sfp->sfp_eth_1040g = code;
185 	} else {
186 		/* Check 10/40G Ethernet class only */
187 		sfp->sfp_eth_1040g =
188 		    find_zero_bit(sfp_eth_1040g_table, code, 1);
189 	}
190 
191 	return (ii->error);
192 }
193 
194 int
ifconfig_sfp_get_sfp_info(ifconfig_handle_t * h,const char * name,struct ifconfig_sfp_info * sfp)195 ifconfig_sfp_get_sfp_info(ifconfig_handle_t *h,
196     const char *name, struct ifconfig_sfp_info *sfp)
197 {
198 	struct i2c_info ii;
199 	char buf[8];
200 
201 	memset(sfp, 0, sizeof(*sfp));
202 
203 	if (i2c_info_init(&ii, h, name) != 0)
204 		return (-1);
205 
206 	/* Read bytes 3-10 at once */
207 	read_i2c(&ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, buf);
208 	if (ii.error != 0)
209 		return (ii.error);
210 
211 	/* Check 10G ethernet first */
212 	sfp->sfp_eth_10g = find_zero_bit(sfp_eth_10g_table, buf[0], 1);
213 	if (sfp->sfp_eth_10g == 0) {
214 		/* No match. Try 1G */
215 		sfp->sfp_eth = find_zero_bit(sfp_eth_table, buf[3], 1);
216 	}
217 	sfp->sfp_fc_len = find_zero_bit(sfp_fc_len_table, buf[4], 1);
218 	sfp->sfp_fc_media = find_zero_bit(sfp_fc_media_table, buf[6], 1);
219 	sfp->sfp_fc_speed = find_zero_bit(sfp_fc_speed_table, buf[7], 1);
220 	sfp->sfp_cab_tech =
221 	    find_zero_bit(sfp_cab_tech_table, (buf[4] << 8) | buf[5], 2);
222 
223 	if (ifconfig_sfp_id_is_qsfp(ii.id))
224 		return (get_qsfp_info(&ii, sfp));
225 	return (get_sfp_info(&ii, sfp));
226 }
227 
228 static size_t
channel_count(enum sfp_id id)229 channel_count(enum sfp_id id)
230 {
231 	/* TODO: other ids */
232 	switch (id) {
233 	case SFP_ID_UNKNOWN:
234 		return (0);
235 	case SFP_ID_QSFP:
236 	case SFP_ID_QSFPPLUS:
237 	case SFP_ID_QSFP28:
238 		return (4);
239 	default:
240 		return (1);
241 	}
242 }
243 
244 size_t
ifconfig_sfp_channel_count(const struct ifconfig_sfp_info * sfp)245 ifconfig_sfp_channel_count(const struct ifconfig_sfp_info *sfp)
246 {
247 	return (channel_count(sfp->sfp_id));
248 }
249 
250 /*
251  * Print SFF-8472/SFF-8436 string to supplied buffer.
252  * All (vendor-specific) strings are padded right with '0x20'.
253  */
254 static void
get_sff_string(struct i2c_info * ii,uint8_t addr,uint8_t off,char * dst)255 get_sff_string(struct i2c_info *ii, uint8_t addr, uint8_t off, char *dst)
256 {
257 	read_i2c(ii, addr, off, SFF_VENDOR_STRING_SIZE, dst);
258 	dst += SFF_VENDOR_STRING_SIZE;
259 	do { *dst-- = '\0'; } while (*dst == 0x20);
260 }
261 
262 static void
get_sff_date(struct i2c_info * ii,uint8_t addr,uint8_t off,char * dst)263 get_sff_date(struct i2c_info *ii, uint8_t addr, uint8_t off, char *dst)
264 {
265 	char buf[SFF_VENDOR_DATE_SIZE];
266 
267 	read_i2c(ii, addr, off, SFF_VENDOR_DATE_SIZE, buf);
268 	sprintf(dst, "20%c%c-%c%c-%c%c", buf[0], buf[1], buf[2], buf[3],
269 	    buf[4], buf[5]);
270 }
271 
272 static int
get_sfp_vendor_info(struct i2c_info * ii,struct ifconfig_sfp_vendor_info * vi)273 get_sfp_vendor_info(struct i2c_info *ii, struct ifconfig_sfp_vendor_info *vi)
274 {
275 	get_sff_string(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, vi->name);
276 	get_sff_string(ii, SFF_8472_BASE, SFF_8472_PN_START, vi->pn);
277 	get_sff_string(ii, SFF_8472_BASE, SFF_8472_SN_START, vi->sn);
278 	get_sff_date(ii, SFF_8472_BASE, SFF_8472_DATE_START, vi->date);
279 	return (ii->error);
280 }
281 
282 static int
get_qsfp_vendor_info(struct i2c_info * ii,struct ifconfig_sfp_vendor_info * vi)283 get_qsfp_vendor_info(struct i2c_info *ii, struct ifconfig_sfp_vendor_info *vi)
284 {
285 	get_sff_string(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, vi->name);
286 	get_sff_string(ii, SFF_8436_BASE, SFF_8436_PN_START, vi->pn);
287 	get_sff_string(ii, SFF_8436_BASE, SFF_8436_SN_START, vi->sn);
288 	get_sff_date(ii, SFF_8436_BASE, SFF_8436_DATE_START, vi->date);
289 	return (ii->error);
290 }
291 
292 int
ifconfig_sfp_get_sfp_vendor_info(ifconfig_handle_t * h,const char * name,struct ifconfig_sfp_vendor_info * vi)293 ifconfig_sfp_get_sfp_vendor_info(ifconfig_handle_t *h,
294     const char *name, struct ifconfig_sfp_vendor_info *vi)
295 {
296 	struct i2c_info ii;
297 
298 	memset(vi, 0, sizeof(*vi));
299 
300 	if (i2c_info_init(&ii, h, name) != 0)
301 		return (-1);
302 
303 	if (ifconfig_sfp_id_is_qsfp(ii.id))
304 		return (get_qsfp_vendor_info(&ii, vi));
305 	return (get_sfp_vendor_info(&ii, vi));
306 }
307 
308 /*
309  * Converts internal temperature (SFF-8472, SFF-8436)
310  * 16-bit unsigned value to human-readable representation:
311  *
312  * Internally measured Module temperature are represented
313  * as a 16-bit signed twos complement value in increments of
314  * 1/256 degrees Celsius, yielding a total range of –128C to +128C
315  * that is considered valid between –40 and +125C.
316  */
317 static double
get_sff_temp(struct i2c_info * ii,uint8_t addr,uint8_t off)318 get_sff_temp(struct i2c_info *ii, uint8_t addr, uint8_t off)
319 {
320 	double d;
321 	uint8_t buf[2];
322 
323 	read_i2c(ii, addr, off, 2, buf);
324 	d = (double)buf[0];
325 	d += (double)buf[1] / 256;
326 	return (d);
327 }
328 
329 /*
330  * Retrieves supplied voltage (SFF-8472, SFF-8436).
331  * 16-bit usigned value, treated as range 0..+6.55 Volts
332  */
333 static double
get_sff_voltage(struct i2c_info * ii,uint8_t addr,uint8_t off)334 get_sff_voltage(struct i2c_info *ii, uint8_t addr, uint8_t off)
335 {
336 	double d;
337 	uint8_t buf[2];
338 
339 	read_i2c(ii, addr, off, 2, buf);
340 	d = (double)((buf[0] << 8) | buf[1]);
341 	return (d / 10000);
342 }
343 
344 /*
345  * The following conversions assume internally-calibrated data.
346  * This is always true for SFF-8346, and explicitly checked for SFF-8472.
347  */
348 
349 double
power_mW(uint16_t power)350 power_mW(uint16_t power)
351 {
352 	/* Power is specified in units of 0.1 uW. */
353 	return (1.0 * power / 10000);
354 }
355 
356 double
power_dBm(uint16_t power)357 power_dBm(uint16_t power)
358 {
359 	return (10.0 * log10(power_mW(power)));
360 }
361 
362 double
bias_mA(uint16_t bias)363 bias_mA(uint16_t bias)
364 {
365 	/* Bias current is specified in units of 2 uA. */
366 	return (1.0 * bias / 500);
367 }
368 
369 static uint16_t
get_sff_channel(struct i2c_info * ii,uint8_t addr,uint8_t off)370 get_sff_channel(struct i2c_info *ii, uint8_t addr, uint8_t off)
371 {
372 	uint8_t buf[2];
373 
374 	read_i2c(ii, addr, off, 2, buf);
375 	if (ii->error != 0)
376 		return (0);
377 
378 	return ((buf[0] << 8) + buf[1]);
379 }
380 
381 static int
get_sfp_status(struct i2c_info * ii,struct ifconfig_sfp_status * ss)382 get_sfp_status(struct i2c_info *ii, struct ifconfig_sfp_status *ss)
383 {
384 	uint8_t diag_type, flags;
385 
386 	/* Read diagnostic monitoring type */
387 	read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
388 	if (ii->error != 0)
389 		return (-1);
390 
391 	/*
392 	 * Read monitoring data IFF it is supplied AND is
393 	 * internally calibrated
394 	 */
395 	flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
396 	if ((diag_type & flags) != flags) {
397 		ii->h->error.errtype = OTHER;
398 		ii->h->error.errcode = ENXIO;
399 		return (-1);
400 	}
401 
402 	ss->temp = get_sff_temp(ii, SFF_8472_DIAG, SFF_8472_TEMP);
403 	ss->voltage = get_sff_voltage(ii, SFF_8472_DIAG, SFF_8472_VCC);
404 	ss->channel = calloc(channel_count(ii->id), sizeof(*ss->channel));
405 	if (ss->channel == NULL) {
406 		ii->h->error.errtype = OTHER;
407 		ii->h->error.errcode = ENOMEM;
408 		return (-1);
409 	}
410 	ss->channel[0].rx = get_sff_channel(ii, SFF_8472_DIAG, SFF_8472_RX_POWER);
411 	ss->channel[0].tx = get_sff_channel(ii, SFF_8472_DIAG, SFF_8472_TX_BIAS);
412 	return (ii->error);
413 }
414 
415 static uint32_t
get_qsfp_bitrate(struct i2c_info * ii)416 get_qsfp_bitrate(struct i2c_info *ii)
417 {
418 	uint8_t code;
419 	uint32_t rate;
420 
421 	code = 0;
422 	read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &code);
423 	rate = code * 100;
424 	if (code == 0xFF) {
425 		read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &code);
426 		rate = code * 250;
427 	}
428 
429 	return (rate);
430 }
431 
432 static int
get_qsfp_status(struct i2c_info * ii,struct ifconfig_sfp_status * ss)433 get_qsfp_status(struct i2c_info *ii, struct ifconfig_sfp_status *ss)
434 {
435 	size_t channels;
436 
437 	ss->temp = get_sff_temp(ii, SFF_8436_BASE, SFF_8436_TEMP);
438 	ss->voltage = get_sff_voltage(ii, SFF_8436_BASE, SFF_8436_VCC);
439 	channels = channel_count(ii->id);
440 	ss->channel = calloc(channels, sizeof(*ss->channel));
441 	if (ss->channel == NULL) {
442 		ii->h->error.errtype = OTHER;
443 		ii->h->error.errcode = ENOMEM;
444 		return (-1);
445 	}
446 	for (size_t chan = 0; chan < channels; ++chan) {
447 		uint8_t rxoffs = SFF_8436_RX_CH1_MSB + chan * sizeof(uint16_t);
448 		uint8_t txoffs = SFF_8436_TX_CH1_MSB + chan * sizeof(uint16_t);
449 		ss->channel[chan].rx =
450 		    get_sff_channel(ii, SFF_8436_BASE, rxoffs);
451 		ss->channel[chan].tx =
452 		    get_sff_channel(ii, SFF_8436_BASE, txoffs);
453 	}
454 	ss->bitrate = get_qsfp_bitrate(ii);
455 	return (ii->error);
456 }
457 
458 int
ifconfig_sfp_get_sfp_status(ifconfig_handle_t * h,const char * name,struct ifconfig_sfp_status * ss)459 ifconfig_sfp_get_sfp_status(ifconfig_handle_t *h, const char *name,
460     struct ifconfig_sfp_status *ss)
461 {
462 	struct i2c_info ii;
463 
464 	memset(ss, 0, sizeof(*ss));
465 
466 	if (i2c_info_init(&ii, h, name) != 0)
467 		return (-1);
468 
469 	if (ifconfig_sfp_id_is_qsfp(ii.id))
470 		return (get_qsfp_status(&ii, ss));
471 	return (get_sfp_status(&ii, ss));
472 }
473 
474 void
ifconfig_sfp_free_sfp_status(struct ifconfig_sfp_status * ss)475 ifconfig_sfp_free_sfp_status(struct ifconfig_sfp_status *ss)
476 {
477 	if (ss != NULL)
478 		free(ss->channel);
479 }
480 
481 static const char *
sfp_id_string_alt(uint8_t value)482 sfp_id_string_alt(uint8_t value)
483 {
484 	const char *id;
485 
486 	if (value <= SFF_8024_ID_LAST)
487 		id = sff_8024_id[value];
488 	else if (value > 0x80)
489 		id = "Vendor specific";
490 	else
491 		id = "Reserved";
492 
493 	return (id);
494 }
495 
496 static const char *
sfp_conn_string_alt(uint8_t value)497 sfp_conn_string_alt(uint8_t value)
498 {
499 	const char *conn;
500 
501 	if (value >= 0x0D && value <= 0x1F)
502 		conn = "Unallocated";
503 	else if (value >= 0x24 && value <= 0x7F)
504 		conn = "Unallocated";
505 	else
506 		conn = "Vendor specific";
507 
508 	return (conn);
509 }
510 
511 void
ifconfig_sfp_get_sfp_info_strings(const struct ifconfig_sfp_info * sfp,struct ifconfig_sfp_info_strings * strings)512 ifconfig_sfp_get_sfp_info_strings(const struct ifconfig_sfp_info *sfp,
513     struct ifconfig_sfp_info_strings *strings)
514 {
515 	get_sfp_info_strings(sfp, strings);
516 	if (strings->sfp_id == NULL)
517 		strings->sfp_id = sfp_id_string_alt(sfp->sfp_id);
518 	if (strings->sfp_conn == NULL)
519 		strings->sfp_conn = sfp_conn_string_alt(sfp->sfp_conn);
520 	if (strings->sfp_rev == NULL)
521 		strings->sfp_rev = "Unallocated";
522 }
523 
524 const char *
ifconfig_sfp_physical_spec(const struct ifconfig_sfp_info * sfp,const struct ifconfig_sfp_info_strings * strings)525 ifconfig_sfp_physical_spec(const struct ifconfig_sfp_info *sfp,
526     const struct ifconfig_sfp_info_strings *strings)
527 {
528 	switch (sfp->sfp_id) {
529 	case SFP_ID_UNKNOWN:
530 		break;
531 	case SFP_ID_QSFP:
532 	case SFP_ID_QSFPPLUS:
533 	case SFP_ID_QSFP28:
534 		if (sfp->sfp_eth_1040g & SFP_ETH_1040G_EXTENDED)
535 			return (strings->sfp_eth_ext);
536 		else if (sfp->sfp_eth_1040g)
537 			return (strings->sfp_eth_1040g);
538 		break;
539 	default:
540 		if (sfp->sfp_eth_ext)
541 			return (strings->sfp_eth_ext);
542 		else if (sfp->sfp_eth_10g)
543 			return (strings->sfp_eth_10g);
544 		else if (sfp->sfp_eth)
545 			return (strings->sfp_eth);
546 		break;
547 	}
548 	return ("Unknown");
549 }
550 
551 int
ifconfig_sfp_get_sfp_dump(ifconfig_handle_t * h,const char * name,struct ifconfig_sfp_dump * dump)552 ifconfig_sfp_get_sfp_dump(ifconfig_handle_t *h, const char *name,
553     struct ifconfig_sfp_dump *dump)
554 {
555 	struct i2c_info ii;
556 	uint8_t *buf = dump->data;
557 
558 	memset(dump->data, 0, sizeof(dump->data));
559 
560 	if (i2c_info_init(&ii, h, name) != 0)
561 		return (-1);
562 
563 	if (ifconfig_sfp_id_is_qsfp(ii.id)) {
564 		read_i2c(&ii, SFF_8436_BASE, QSFP_DUMP0_START, QSFP_DUMP0_SIZE,
565 		    buf + QSFP_DUMP0_START);
566 		read_i2c(&ii, SFF_8436_BASE, QSFP_DUMP1_START, QSFP_DUMP1_SIZE,
567 		    buf + QSFP_DUMP1_START);
568 	} else {
569 		read_i2c(&ii, SFF_8472_BASE, SFP_DUMP_START, SFP_DUMP_SIZE,
570 		    buf + SFP_DUMP_START);
571 	}
572 
573 	return (ii.error != 0 ? -1 : 0);
574 }
575 
576 size_t
ifconfig_sfp_dump_region_count(const struct ifconfig_sfp_dump * dp)577 ifconfig_sfp_dump_region_count(const struct ifconfig_sfp_dump *dp)
578 {
579 	uint8_t id_byte = dp->data[0];
580 
581 	switch ((enum sfp_id)id_byte) {
582 	case SFP_ID_UNKNOWN:
583 		return (0);
584 	case SFP_ID_QSFP:
585 	case SFP_ID_QSFPPLUS:
586 	case SFP_ID_QSFP28:
587 		return (2);
588 	default:
589 		return (1);
590 	}
591 }
592