-
Notifications
You must be signed in to change notification settings - Fork 3
/
libsfp.c
594 lines (473 loc) · 13.1 KB
/
libsfp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
@file
@brief main libsfp implementation source file
*/
#include <stdlib.h>
#include <string.h>
#include "libsfp_int.h"
#include "libsfp_print.h"
/**
* @brief Create library handle with default parameters
* @param h - pointer to address of library handle
* @return 0 on success
*/
int libsfp_init(libsfp_t **h)
{
(*h) = malloc(sizeof(libsfp_int_t));
if (!H(*h))
return -1;
memset(H(*h), 0, sizeof(libsfp_int_t));
H(*h)->flags = LIBSFP_FLAGS_PRINT_LONGOPT;
H(*h)->a0addr = LIBSFP_DEF_A0_ADDRESS;
H(*h)->a2addr = LIBSFP_DEF_A2_ADDRESS;
/* Assign default print callbacks */
libsfp_print_callbacks_t *cbks = &(H(*h)->print_cb);
cbks->name = libsfp_printname_default;
cbks->value = libsfp_printvalue_default;
cbks->newline = libsfp_printnewline_default;
return 0;
}
/**
* @brief Create library handle with default parameters
* @return libsfp handle or 0 if error occured
*/
libsfp_t *libsfp_create()
{
libsfp_t *h;
if (libsfp_init(&h))
return 0;
return h;
}
/**
* @brief Free library handle and its memory
* @param h - pointer to library handle
* @return 0 on success
*/
int libsfp_free(libsfp_t *h)
{
free(h);
return 0;
}
/**
* @brief Assign callback function address for reading access to SFP
* @param h - pointer to library handle
* @param readregs - address of callback function
* @return 0 on success
*/
int libsfp_set_readreg_callback(libsfp_t *h, libsfp_readregs_cb_t readregs)
{
H(h)->readregs = readregs;
return 0;
}
/**
* @brief Assign name print callbacks function address
*
* @param h - pointer to library handle
* @param clbk - address struct that contain callbacks addresses
* if any of addresses is NULL then this address not changed
* @return 0 on success
*/
int libsfp_set_print_callbacks(libsfp_t *h, const libsfp_print_callbacks_t *clbk)
{
if (clbk->name)
H(h)->print_cb.name = clbk->name;
if (clbk->newline)
H(h)->print_cb.newline = clbk->newline;
if (clbk->value)
H(h)->print_cb.value = clbk->value;
return 0;
}
/**
* @brief Assign userdata pointer for callback functions
* @param h - pointer to library handle
* @param udata - pointer to user data
* @return 0 on success
*/
int libsfp_set_user_data(libsfp_t *h, void *udata)
{
H(h)->udata = udata;
return 0;
}
/**
* @brief Assign some option flags used for modify output text information
* @param h - pointer to library handle
* @param flags - file pointer
* @return 0 on success
*/
int libsfp_set_flags(libsfp_t *h, uint32_t flags)
{
H(h)->flags = flags;
return 0;
}
/**
* @brief Assign bus addresses of two SFP memory banks
* @param h - pointer to library handle
* @param a0addr - address of first bank A0
* @param a2addr - address of second bank A2
* @return 0 on success
*/
int libsfp_set_addresses(libsfp_t *h, uint8_t a0addr, uint8_t a2addr)
{
H(h)->a0addr = a0addr;
H(h)->a2addr = a2addr;
return 0;
}
int libsfp_is_laser_availble(libsfp_base_fields_t *bf)
{
if ( ((bf->connector >= 0x20) && (bf->connector <= 0x22)) ||
((bf->connector >= 0x2) && (bf->connector <= 0x6)) )
return 0;
if ((bf->transceiver[3] & 0x08) || (bf->transceiver[6] & 0xF0))
return 0;
return 1;
}
/* A2 address */
float libsfp_get_slope(libsfp_u16_field_t f)
{
return (float)f.d[0]+(float)f.d[1]/(float)256.0;
}
float libsfp_get_offset(libsfp_u16_field_t f)
{
return ((int16_t)((f.d[0]<<8) |f.d[1]));
}
float libsfp_get_rxpwr(libsfp_u32_field_t f)
{
uint32_t v = ((f.d[0])<<24) | ((f.d[1])<<16) | ((f.d[2])<<8) | (f.d[3]);
return (float)v;
}
float libsfp_get_temp(libsfp_u16_field_t tf, libsfp_calibration_fields_t *cal)
{
float f;
f = ((float)((tf.d[0])&0x7F)) + ((float)tf.d[1])/(float)256.0;
f = f*(float)((tf.d[0]&0x80)?-1.0:1.0);
if (cal)
f = (libsfp_get_slope(cal->t_slope)*f + libsfp_get_offset(cal->t_offset))/(float)1000.0;
return f;
}
float libsfp_get_voltage(libsfp_u16_field_t v, libsfp_calibration_fields_t *cal)
{
float f;
f = ((float)( (v.d[0] << 8) | v.d[1]))/(float)10000.0;
if (cal)
f = (libsfp_get_slope(cal->v_slope)*f + libsfp_get_offset(cal->v_offset))/(float)10.0;
return f;
}
float libsfp_get_biascurrent(libsfp_u16_field_t v, libsfp_calibration_fields_t *cal)
{
float f;
f = ((float)( (v.d[0] << 8) | v.d[1])*(float)0.002);
if (cal) {
f = (libsfp_get_slope(cal->txi_slope)*f +
libsfp_get_offset(cal->txi_offset))*(float)0.002;
}
return f;
}
/**
* @brief Return tx power level in mWatt
*
* @param v corresponding field value
* @param slope pointer for calibration slope field or 0 if not present
* @param ofs pointer for calibration offset field or 0 if not present
*
* @return tx level (mWatt)
*/
float libsfp_get_txpower(libsfp_u16_field_t v,
libsfp_u16_field_t *slope, libsfp_u16_field_t *ofs)
{
float f;
f = (((float)( (v.d[0] << 8) | v.d[1]))/10000.0f);
if ((slope) && (ofs))
f = (libsfp_get_slope(*slope)*f + libsfp_get_offset(*ofs))/10.0f;
return f;
}
/**
* @brief Get RX power level in mWatt's
*
* @param v coresponding field value
* @param rx_pwr pointer to calibration array value [5] or null if not present
*
* @return RX power level (mWatts)
*
*/
float libsfp_get_rxpower(libsfp_u16_field_t v, libsfp_u32_field_t *rx_pwr)
{
int i;
float f, sum = 0.0;
f = (float)((float)( (v.d[0] << 8) | v.d[1])/10000.0);
if (rx_pwr) {
for (i=0; i<4; ++i)
sum += libsfp_get_rxpwr(rx_pwr[i])*f;
sum += libsfp_get_rxpwr(rx_pwr[4]);
return sum;
}
return f;
}
/**
* @brief Calc simple check sum that used in SFP memory banks
* @param d data pointer
* @param size data size
* @return Checksum;
*/
uint8_t libsfp_calc_csum(void *d, uint16_t size)
{
uint16_t i;
uint32_t csum=0;
for (i=0; i<size; ++i)
csum += ((uint8_t*)d)[i];
return csum & 0xFF;
}
int libsfp_is_csums_correct(libsfp_t *h, libsfp_A0_t *a0, libsfp_A2_t *a2)
{
if ( !(H(h)->flags & LIBSFP_FLAGS_CSUM_CHECK) )
return 0;
if (a0) {
if (a0->base.cc_base != libsfp_calc_csum(&a0->base, sizeof(a0->base) - 1))
return 1;
if (a0->ext.cc_ext != libsfp_calc_csum(&a0->ext, sizeof(a0->ext) - 1))
return 1;
}
if (a2) {
if (a2->cc_dmi != libsfp_calc_csum(a2, sizeof(a2->th) + sizeof(a2->cl)-1))
return 1;
}
return 0;
}
/**
* @brief Read full SFP module info to memory
* @param h - library handle
* @param dump - pointer to memory to store information
* @return 0 on success
*/
int libsfp_readinfo(libsfp_t *h, libsfp_dump_t *dump)
{
if (READREG_A0(h, 0, sizeof(libsfp_A0_t), &dump->a0))
return -1;
if (dump->a0.ext.diag_mon_type & LIBSFP_A0_DIAGMON_TYPE_DDM) {
if (READREG_A2(h, 0, sizeof(libsfp_A2_t), &dump->a2))
return -1;
if (libsfp_is_csums_correct(h, &dump->a0, &dump->a2))
return -1;
} else {
if (libsfp_is_csums_correct(h, &dump->a0, 0))
return -1;
}
return 0;
}
/**
* @brief Read and output information selected by flags
* as text to specified file
* @param h - library handle
* @return 0 on success
*/
int libsfp_showinfo(libsfp_t *h)
{
libsfp_dump_t *dump;
dump = malloc(sizeof(libsfp_dump_t));
if (!dump)
return -1;
if (libsfp_readinfo(h, dump)) {
free(dump);
return -1;
}
libsfp_printinfo(h, dump);
free(dump);
return 0;
}
uint32_t libsfp_bitrate2speed_mode(uint8_t br)
{
uint32_t smode;
smode = LIBSFP_SPEED_MODE_UNKNOWN;
if ((br >= 10) && (br < 100))
smode = LIBSFP_SPEED_MODE_1G;
if ((br >= 100 ) && (br < 200))
smode = LIBSFP_SPEED_MODE_10G;
if (br >= 200)
smode = LIBSFP_SPEED_MODE_20G;
return smode;
};
/**
* @brief Read brief information for SFP module an store it to
* specified place
* @param h - library handle
* @param info - struct to store information
* @return
*/
int libsfp_readinfo_brief(libsfp_t *h, libsfp_brief_info_t *info)
{
uint8_t d[2], dmtype;
libsfp_u16_field_t tp, rp;
libsfp_u32_field_t rx_pwr[5];
libsfp_u16_field_t txpwr_slope, txpwr_offset;
info->txpower = -1;
info->rxpower = -1;
if (READREG_A0(h, LIBSFP_OFS_A0_BR_NOMINAL, 1, d))
return -1;
info->bitrate = d[0]*100;
if (libsfp_get_speed_mode(h, &info->spmode))
return -1;
if (READREG_A0(h, LIBSFP_OFS_A0_VENDOR_NAME,
LIBSFP_LEN_A0_VENDOR_NAME, &info->vendor))
return -1;
info->vendor[16] = 0;
if (READREG_A0(h, LIBSFP_OFS_A0_VENDOR_PN,
LIBSFP_LEN_A0_VENDOR_PN, &info->partnum))
return -1;
info->partnum[16] = 0;
if (READREG_A0(h, LIBSFP_OFS_A0_DIAGMON_TYPE, 1, &dmtype))
return -1;
if (!(dmtype & LIBSFP_A0_DIAGMON_TYPE_DDM))
return 0;
if (READREG_A2(h, LIBSFP_OFS_A2_DIAGNOSTICS_TXPOWER, 2, &tp))
return -1;
if (READREG_A2(h, LIBSFP_OFS_A2_DIAGNOSTICS_RXPOWER, 2, &rp))
return -1;
if (dmtype & LIBSFP_A0_DIAGMON_TYPE_EXCAL) {
/* Module power Externally calibrated
* read calibration values */
if (READREG_A2(h, LIBSFP_OFS_A2_EXT_CAL_RXPWR,
sizeof(rx_pwr), rx_pwr))
return -1;
if (READREG_A2(h, LIBSFP_OFS_A2_EXT_CAL_TXPWR_SLOPE,
sizeof(txpwr_slope), &txpwr_slope))
return -1;
if (READREG_A2(h, LIBSFP_OFS_A2_EXT_CAL_TXPWR_OFFSET,
sizeof(txpwr_offset), &txpwr_offset))
return -1;
info->txpower = libsfp_get_txpower(tp, &txpwr_slope, &txpwr_slope);
info->rxpower = libsfp_get_rxpower(rp, rx_pwr);
} else {
info->txpower = libsfp_get_txpower(tp, 0, 0);
info->rxpower = libsfp_get_rxpower(rp, 0);
}
return 0;
}
/**
* @brief Get SFP module max speed (See LIBSFP_SPEED_MODE_* constants)
* @param h - library handle
* @param smode - speed
* @return 0 on success
*/
int libsfp_get_speed_mode(libsfp_t *h, uint32_t *smode)
{
uint8_t br, tr[8];
if (READREG_A0(h, LIBSFP_OFS_A0_BR_NOMINAL, 1, &br))
return -1;
(*smode) = libsfp_bitrate2speed_mode(br);
if ((*smode) == LIBSFP_SPEED_MODE_UNKNOWN) {
if (READREG_A0(h, LIBSFP_OFS_A0_TRANSCEIVER,
LIBSFP_LEN_A0_TRANSCEIVER, tr))
return -1;
if ((tr[0]&0xF0)) {
(*smode) = LIBSFP_SPEED_MODE_10G;
return 0;
}
if ((tr[3]&0x0F))
(*smode) = LIBSFP_SPEED_MODE_1G;
}
return 0;
}
/**
* @brief Check that SFP module is a copper ethernet module
* @param h - library handle
* @param ans - answer (0/1 - Other/Copper ethernet)
* @return 0 on success
*/
int libsfp_is_copper_eth(libsfp_t *h, uint8_t *ans)
{
if (READREG_A0(h, LIBSFP_OFS_A0_TRANSCEIVER+3, 1, ans))
return -1;
(*ans) = ((*ans) & 0x08) ? 1 : 0;
return 0;
}
/**
* @brief Check that SFP module is Dirrect Attach cable module
* @param h - library handle
* @param ans - answer (0/1 - Normal/Dirrect Attach)
* @return 0 on success
*/
int libsfp_is_directattach(libsfp_t *h, uint8_t *ans)
{
(*ans) = 0;
uint8_t v;
if (READREG_A0(h, LIBSFP_OFS_A0_CONNECTOR, 1, &v))
return -1;
if (v != LIBSFP_A0_CONNECTOR_COPPER) /* Cooper */
return 0;
if (READREG_A0(h, LIBSFP_OFS_A0_TRANSCEIVER+5, 1, &v)) /* Passive cable */
return -1;
if (!(v & 4))
return 0;
(*ans) = 1;
return 0;
}
/**
* @brief Get copper length in case of copper ethernet module
* @param h - library handle
* @param ans - length of cable (m)
* @return 0 on success
*/
int libsfp_get_copper_length(libsfp_t *h, uint8_t *ans)
{
if (READREG_A0(h, LIBSFP_OFS_A0_LENGTH_CABLE, 1, ans))
return -1;
return 0;
}
/**
* @brief Get SFP module pins state (if supported)
* @param h library handle
* @param value pointer to bit value\n
* see LIBSFP_A2_STATUSCONTROL_* constants
* @return 0 on success
*/
int libsfp_get_pins_state(libsfp_t *h, uint8_t *value)
{
uint8_t dmtype;
if (READREG_A0(h, LIBSFP_OFS_A0_DIAGMON_TYPE, 1, &dmtype))
return -1;
if ((!(dmtype & LIBSFP_A0_DIAGMON_TYPE_DDM)))
return -1;
if (READREG_A2(h, LIBSFP_OFS_A2_STATUSCONTROL, 1, value))
return -1;
/* Clear bits not corresponding for pin states */
(*value) &= ~(LIBSFP_A2_STATUSCONTROL_TXD_SET | LIBSFP_A2_STATUSCONTROL_RS0_SET);
return 0;
}
/**
* @brief Set SFP module soft pins (if supported)
* @param h library handle
* @param mask bit mask to set \n
* see LIBSFP_A2_STATUSCONTROL_*_SET constants
* @param value bit value\n
* see LIBSFP_A2_STATUSCONTROL_*_SET constants
* @return 0 on success
*/
int libsfp_set_soft_pins_state(libsfp_t *h, uint8_t mask, uint8_t value)
{
uint8_t v, m;
if (READREG_A0(h, LIBSFP_OFS_A0_DIAGMON_TYPE, 1, &v))
return -1;
if (!(v & LIBSFP_A0_DIAGMON_TYPE_DDM))
return -1;
if (READREG_A0(h, LIBSFP_OFS_A0_ENHANCED_OPTIONS, 1, &v))
return -1;
if (v & LIBSFP_A0_ENHANCED_OPTIONS_TXDIS)
m |= LIBSFP_A2_STATUSCONTROL_TXD_SET;
if (v & LIBSFP_A0_ENHANCED_OPTIONS_RATESEL)
m |= LIBSFP_A2_STATUSCONTROL_RS0_SET;
/* Check that operation supported */
if (!m)
return -1;
/* only this bits can be set */
mask &= m;
value &= m;
/* if nothing to do exit */
if (!mask)
return 0;
if (READREG_A2(h, LIBSFP_OFS_A2_STATUSCONTROL, 1, &v))
return -1;
v &= ~mask;
v |= value;
if (WRITEREG_A2(h, LIBSFP_OFS_A2_STATUSCONTROL, 1, &v))
return 1;
return 0;
}