-
Notifications
You must be signed in to change notification settings - Fork 28
/
optoe.c
1007 lines (903 loc) · 30.2 KB
/
optoe.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* optoe.c - A driver to read and write the EEPROM on optical transceivers
* (SFP, QSFP, CMIS (Common Management Interface Spec)
* and similar I2C based devices)
*
* Copyright (C) 2014 Cumulus networks Inc.
* Copyright (C) 2017 Finisar Corp.
*/
/*
* Description:
* a) Optical transceiver EEPROM read/write transactions are just like
* the at24 eeproms managed by the at24.c i2c driver
* b) The register/memory layout is up to 256 128 byte pages defined by
* a "pages valid" register and switched via a "page select"
* register as explained in below diagram.
* c) 256 bytes are mapped at a time. 'Lower page 00h' is the first 128
* bytes of address space, and always references the same
* location, independent of the page select register.
* All mapped pages are mapped into the upper 128 bytes
* (offset 128-255) of the i2c address.
* d) Devices with one I2C address (eg QSFP, CMIS) use I2C address 0x50
* (A0h in the spec), and map all pages in the upper 128 bytes
* of that address.
* e) Devices with two I2C addresses (eg SFP) have 256 bytes of data
* at I2C address 0x50, and 256 bytes of data at I2C address
* 0x51 (A2h in the spec). Page selection and paged access
* only apply to this second I2C address (0x51).
* e) The address space is presented, by the driver, as a linear
* address space. For devices with one I2C client at address
* 0x50 (eg QSFP, CMIS), offset 0-127 are in the lower
* half of address 50/A0h/optoe_client. Offset 128-255 are in
* page 0, 256-383 are page 1, etc. More generally, offset
* 'n' resides in page (n/128)-1. ('page -1' is the lower
* half, offset 0-127).
* f) For devices with two I2C clients at address 0x50 and 0x51 (eg SFP),
* the address space places offset 0-127 in the lower
* half of 50/A0/optoe_client, offset 128-255 in the upper
* half. Offset 256-383 is in the lower half of 51/A2/dummy.
* Offset 384-511 is in page 0, in the upper half of 51/A2/...
* Offset 512-639 is in page 1, in the upper half of 51/A2/...
* Offset 'n' is in page (n/128)-3 (for n > 383)
*
* One I2c addressed (eg QSFP, CMIS) Memory Map
*
* 2-Wire Serial Address: 1010000x
*
* Lower Page 00h (128 bytes)
* =====================
* | |
* | |
* | |
* | |
* | |
* | |
* | |
* | |
* | |
* | |
* |Page Select Byte(127)|
* =====================
* |
* |
* V
* ------------------------------------------------------------
* | | | |
* | | | |
* V V V V
* ------------ -------------- --------------- --------------
* | | | | | | | |
* | Upper | | Upper | | Upper | | Upper |
* | Page 00h | | Page 01h | | Page 02h | | Page 03h |
* | | | (Optional) | | (Optional) | | (Optional |
* | | | | | | | for Cable |
* | | | | | | | Assemblies) |
* | ID | | AST | | User | | |
* | Fields | | Table | | EEPROM Data | | |
* | | | | | | | |
* | | | | | | | |
* | | | | | | | |
* ------------ -------------- --------------- --------------
*
* The SFF 8636 (QSFP) spec only defines the 4 pages described above.
* In anticipation of future applications and devices, this driver
* supports access to the full architected range, 256 pages.
*
* The CMIS (Common Management Interface Specification) defines use of
* considerably more pages (at least to page 0xAF), which this driver
* supports.
*
* NOTE: This version of the driver ONLY SUPPORTS BANK 0 PAGES on CMIS
* devices.
*
**/
/* #define DEBUG 1 */
#ifndef LATEST_KERNEL
/*
* Note to my dev/test partners... LATEST_KERNEL conditionally
* compiles code that only works on very new Linux kernels.
* I am trying to submit this to the upstream kernel at 5.11.
* There are NVMEM dependencies (NVMEM_DEVID_NONE) which require
* header files from 5.9 or later kernel. So, when submitting
* upstream, I will unifdef 'LATEST_KERNEL', keeping the latest
* code. For testing on any earlier version of Linux, you
* need the other side of that #ifdef. That is actually what
* I have been testing, so it should work. You don't have
* to modify this code at all. LATEST_KERNEL is not defined,
* so you will get the code that runs on older kernels.
*/
#define DEBUG 1
#endif
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/nvmem-provider.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/types.h>
/* The maximum length of a port name */
#define MAX_PORT_NAME_LEN 20
/* fundamental unit of addressing for EEPROM */
#define OPTOE_PAGE_SIZE 128
/*
* Single address devices (eg QSFP, CMIS) have 256 pages, plus the unpaged
* low 128 bytes. If the device does not support paging, it is
* only 2 'pages' long.
*/
#define OPTOE_ARCH_PAGES 256
#define ONE_ADDR_EEPROM_SIZE ((1 + OPTOE_ARCH_PAGES) * OPTOE_PAGE_SIZE)
#define ONE_ADDR_EEPROM_UNPAGED_SIZE (2 * OPTOE_PAGE_SIZE)
/*
* Dual address devices (eg SFP) have 256 pages, plus the unpaged
* low 128 bytes, plus 256 bytes at 0x50. If the device does not
* support paging, it is 4 'pages' long.
*/
#define TWO_ADDR_EEPROM_SIZE ((3 + OPTOE_ARCH_PAGES) * OPTOE_PAGE_SIZE)
#define TWO_ADDR_EEPROM_UNPAGED_SIZE (4 * OPTOE_PAGE_SIZE)
#define TWO_ADDR_NO_0X51_SIZE (2 * OPTOE_PAGE_SIZE)
/* a few constants to find our way around the EEPROM */
#define OPTOE_PAGE_SELECT_REG 0x7F
#define ONE_ADDR_PAGEABLE_REG 0x02
#define QSFP_NOT_PAGEABLE (1<<2)
#define CMIS_NOT_PAGEABLE (1<<7)
#define TWO_ADDR_PAGEABLE_REG 0x40
#define TWO_ADDR_PAGEABLE (1<<4)
#define TWO_ADDR_0X51_REG 92
#define TWO_ADDR_0X51_SUPP (1<<6)
#define OPTOE_READ_OP 0
#define OPTOE_WRITE_OP 1
#define OPTOE_EOF 0 /* used for access beyond end of device */
/* define optoe_client structure to tie the i2c client to its regmap */
struct optoe_client {
struct i2c_client *client;
struct regmap *regmap;
};
struct optoe_data {
char port_name[MAX_PORT_NAME_LEN];
u32 byte_len; /* architected size of EEPROM */
/*
* Lock protects against activities from other Linux tasks,
* but not from changes by other I2C masters.
*/
struct mutex lock;
struct attribute_group attr_group;
struct nvmem_device *nvmem;
unsigned int write_max;
/* dev_class: ONE_ADDR (QSFP), TWO_ADDR (SFP), or CMIS */
int dev_class;
/* client at 0x50 */
struct optoe_client optoe_client;
/* dummy at 0x51 for SFP devices */
struct optoe_client optoe_dummy;
};
/*
* specs often allow 5 msec for a page write, sometimes 20 msec;
* it's important to recover from write timeouts.
*/
static unsigned int write_timeout = 25;
/*
* flags to distinguish one-address (QSFP family) from two-address (SFP family)
* and one-address Common Management Interface Specification (CMIS family)
*/
#define ONE_ADDR 1
#define TWO_ADDR 2
#define CMIS_ADDR 3
static const struct i2c_device_id optoe_ids[] = {
{ "optoe1", ONE_ADDR },
{ "optoe2", TWO_ADDR },
{ "optoe3", CMIS_ADDR },
{ /* END OF LIST */ }
};
MODULE_DEVICE_TABLE(i2c, optoe_ids);
/*-------------------------------------------------------------------------*/
/*
* optoe_translate_offset() computes the addressing information to be used for
* a given r/w request.
*
* Task is to calculate the client (optoe_client for addr 50,
* optoe_dummy for addr 51) the page, and the offset.
*
* Handles both single address (QSFP), two address (SFP) and CMIS devices.
* For SFP, offset 0-255 are on optoe_client, >255 is on optoe_dummy
* Offset 256-383 are on the lower half of optoe_dummy
* Pages are accessible on the upper half of optoe_dummy
* Offset >383 are in 128 byte pages mapped into the upper half
*
* For QSFP and CMIS, all offsets are on optoe_client
* offset 0-127 are on the lower half of optoe_client (no paging)
* Pages are accessible on the upper half of optoe_client.
* Offset >127 are in 128 byte pages mapped into the upper half
*
* Callers must not read/write beyond the end of a client or a page
* without recomputing the client/page. Hence offset (within page)
* plus length must be less than or equal to 128. (Note that this
* routine does not have access to the length of the call, hence
* cannot do the validity check.)
*
* Offset within Lower Page 00h and Upper Page 00h are not recomputed
*/
static uint8_t optoe_translate_offset(struct optoe_data *optoe,
loff_t *offset, struct optoe_client **optoe_client)
{
unsigned int page = 0;
*optoe_client = &optoe->optoe_client;
/* if SFP style, offset > 255, shift to i2c addr 0x51 */
if (optoe->dev_class == TWO_ADDR) {
if (*offset > 255) {
/* like QSFP, but shifted dummy client */
*optoe_client = &optoe->optoe_dummy;
*offset -= 256;
}
}
/*
* if offset is in the range 0-128...
* page doesn't matter (using lower half), return 0.
* offset is already correct (don't add 128 to get to paged area)
*/
if (*offset < OPTOE_PAGE_SIZE)
return page;
/* note, page will always be positive since *offset >= 128 */
page = (*offset >> 7)-1;
/*
* OPTOE_PAGE_SIZE puts offset in the top half (the paged area),
* offset within the top half is last 7 bits
*/
*offset = OPTOE_PAGE_SIZE + (*offset & 0x7f);
return page; /* note also returning client and offset */
}
static int optoe_regmap_rw(struct optoe_data *optoe,
struct optoe_client *optoe_client,
char *buf,
unsigned int offset,
size_t count, int opcode)
{
unsigned long timeout, access_time;
struct i2c_client *client = optoe_client->client;
struct regmap *regmap = optoe_client->regmap;
int ret;
/*
* Accesses fail if the previous write didn't complete yet. We may
* loop a few times until this one succeeds, waiting at least
* long enough for one entire page write to work.
*/
timeout = jiffies + msecs_to_jiffies(write_timeout);
do {
access_time = jiffies;
if (opcode == OPTOE_READ_OP) {
#ifndef LATEST_KERNEL
/*
* bug in regmap SMBUS code fails on reads
* longer than OR EQUAL TO SMBUS_BLOCK_MAX
* fixed in 4.17 kernel
*/
if (count >= I2C_SMBUS_BLOCK_MAX)
count = I2C_SMBUS_BLOCK_MAX - 1;
#endif
ret = regmap_bulk_read(regmap, offset, buf, count);
} else {
/* write_max is always 1 in this driver */
if (count > optoe->write_max)
count = optoe->write_max;
ret = regmap_bulk_write(regmap, offset, buf, count);
}
dev_dbg(&client->dev, "regmap %s %zu@%d --> %d (%lu)\n",
(opcode == OPTOE_READ_OP) ? "read" : "write",
count, offset, ret, jiffies);
if (!ret) /* regmap_bulk_calls returns 0 on success */
return count;
usleep_range(1000, 2000);
} while (time_before(access_time, timeout));
return -ETIMEDOUT;
}
static int optoe_eeprom_update_client(struct optoe_data *optoe,
char *buf, loff_t off,
size_t count, int opcode)
{
struct optoe_client *optoe_client;
uint8_t page = 0;
loff_t phy_offset = off;
int retval = 0;
int ret = 0;
int status;
struct device *dev = &optoe->optoe_client.client->dev;
/* translate offset into page, 'offset within page' */
page = optoe_translate_offset(optoe, &phy_offset, &optoe_client);
dev_dbg(dev,
"%s off %lld page:%d phy_offset:%lld, count:%ld, opcode:%d\n",
__func__, off, page, phy_offset, (long) count, opcode);
/* set the page register */
if (page > 0) {
ret = optoe_regmap_rw(optoe, optoe_client, &page,
OPTOE_PAGE_SELECT_REG, 1, OPTOE_WRITE_OP);
if (ret < 0) {
dev_dbg(dev,
"Page register write, page %d failed:%d!\n",
page, ret);
return ret;
}
}
/* read/write the data */
while (count) {
status = optoe_regmap_rw(optoe, optoe_client,
buf, phy_offset, count, opcode);
if (status <= 0) {
if (retval == 0)
retval = status;
break;
}
buf += status;
phy_offset += status;
count -= status;
retval += status;
}
/*
* return the page register to page 0 - why?
* We either have to set the page register to 0 on every access
* to it, or restore it to 0 whenever we change it. Otherwise,
* accesses to page 0 would actually go to whatever the last page
* was. Assume more accesses to page 0 than all other pages
* combined, so less total accesses if we always leave it at page 0
*/
if (page > 0) {
page = 0;
ret = optoe_regmap_rw(optoe, optoe_client, &page,
OPTOE_PAGE_SELECT_REG, 1, OPTOE_WRITE_OP);
if (ret < 0) {
dev_err(dev,
"Restore page register to 0 failed:%d!\n", ret);
/* error only if nothing has been transferred */
if (retval == 0)
retval = ret;
}
}
return retval;
}
/*
* Figure out if this access is within the range of supported pages.
* Note this is called on every access because we don't know if the
* module has been replaced since the last call.
*
* Returns updated len for this access:
* - entire access is legal, original len is returned.
* - access begins legal but is too long, len is truncated to fit.
* - initial offset exceeds supported pages, return OPTOE_EOF (zero)
*/
static int optoe_page_legal(struct optoe_data *optoe,
loff_t off, size_t len)
{
struct optoe_client *optoe_client = &optoe->optoe_client;
u8 regval;
int not_pageable;
int status;
size_t maxlen;
struct device *dev = &optoe_client->client->dev;
if (off < 0)
return -EINVAL;
if (optoe->dev_class == TWO_ADDR) {
/* SFP case */
/* if only using addr 0x50 (first 256 bytes) we're good */
if ((off + len) <= TWO_ADDR_NO_0X51_SIZE)
return len;
/* if offset exceeds possible pages, we're not good */
if (off >= TWO_ADDR_EEPROM_SIZE)
return OPTOE_EOF;
/* in between, are pages supported? */
status = optoe_regmap_rw(optoe, optoe_client, ®val,
TWO_ADDR_PAGEABLE_REG, 1, OPTOE_READ_OP);
if (status < 0)
return status; /* error out (no module?) */
if (regval & TWO_ADDR_PAGEABLE) {
/* Pages supported, trim len to the end of pages */
maxlen = TWO_ADDR_EEPROM_SIZE - off;
} else {
/* pages not supported, trim len to unpaged size */
if (off >= TWO_ADDR_EEPROM_UNPAGED_SIZE)
return OPTOE_EOF;
/* will be accessing addr 0x51, is that supported? */
/* byte 92, bit 6 implies DDM support, 0x51 support */
status = optoe_regmap_rw(optoe, optoe_client, ®val,
TWO_ADDR_0X51_REG, 1,
OPTOE_READ_OP);
if (status < 0)
return status;
if (regval & TWO_ADDR_0X51_SUPP) {
/* addr 0x51 is OK */
maxlen = TWO_ADDR_EEPROM_UNPAGED_SIZE - off;
} else {
/* addr 0x51 NOT supported, trim to 256 max */
if (off >= TWO_ADDR_NO_0X51_SIZE)
return OPTOE_EOF;
maxlen = TWO_ADDR_NO_0X51_SIZE - off;
}
}
len = (len > maxlen) ? maxlen : len;
} else {
/* QSFP case, CMIS case */
/* if no pages needed, we're good */
if ((off + len) <= ONE_ADDR_EEPROM_UNPAGED_SIZE)
return len;
/* if offset exceeds possible pages, we're not good */
if (off >= ONE_ADDR_EEPROM_SIZE)
return OPTOE_EOF;
/* in between, are pages supported? */
status = optoe_regmap_rw(optoe, optoe_client, ®val,
ONE_ADDR_PAGEABLE_REG, 1, OPTOE_READ_OP);
if (status < 0)
return status; /* error out (no module?) */
/*
* note CMIS put the pageable bit in the same register
* as QSFP, but at a different bit :-(
*/
if (optoe->dev_class == ONE_ADDR)
not_pageable = QSFP_NOT_PAGEABLE;
else
not_pageable = CMIS_NOT_PAGEABLE;
if (regval & not_pageable) {
/* pages not supported, trim len to unpaged size */
if (off >= ONE_ADDR_EEPROM_UNPAGED_SIZE)
return OPTOE_EOF;
maxlen = ONE_ADDR_EEPROM_UNPAGED_SIZE - off;
} else {
/* Pages supported, trim len to the end of pages */
maxlen = ONE_ADDR_EEPROM_SIZE - off;
}
len = (len > maxlen) ? maxlen : len;
}
dev_dbg(dev, "page_legal, class %d, off %lld len %ld\n",
optoe->dev_class, off, (long) len);
return len;
}
static int optoe_read_write(struct optoe_data *optoe,
char *buf, loff_t off, size_t len, int opcode)
{
struct i2c_client *client = optoe->optoe_client.client;
int chunk;
int status = 0;
int retval;
size_t pending_len = 0, chunk_len = 0;
loff_t chunk_offset = 0, chunk_start_offset = 0;
loff_t chunk_end_offset = 0;
dev_dbg(&client->dev, "%s: off %lld len:%ld, opcode:%s\n",
__func__, off, (long) len,
(opcode == OPTOE_READ_OP) ? "r" : "w");
if (unlikely(!len))
return len;
/*
* Read data from chip, protecting against concurrent updates
* from this host, but not from other I2C masters.
*/
mutex_lock(&optoe->lock);
/*
* Confirm this access fits within the device suppored addr range
*/
status = optoe_page_legal(optoe, off, len);
/*
* returning 0 (OPTOE_EOF) on a write call gets into an infinite
* loop with the regmap/i2c code. Returning an error on a read call
* will show up as an error with 'cat <eeprom file>'. So, writing
* past EOF is an error, reading past EOF is just '0'
*/
if ((status == OPTOE_EOF) && (opcode == OPTOE_WRITE_OP))
status = -EINVAL;
if ((status == OPTOE_EOF) || (status < 0)) {
mutex_unlock(&optoe->lock);
return status;
}
len = status;
/*
* For each (128 byte) chunk involved in this request, issue a
* separate call to optoe_eeprom_update_client(), to
* ensure that each access recalculates the client/page
* and writes the page register as needed.
* Note that chunk to page mapping is confusing, is different for
* QSFP/CMIS and SFP, and never needs to be done. Don't try!
*/
pending_len = len; /* amount remaining to transfer */
retval = 0; /* amount transferred */
for (chunk = off >> 7; chunk <= (off + len - 1) >> 7; chunk++) {
/*
* Compute the offset and number of bytes to be read/written
*
* 1. start at an offset not equal to 0 (within the chunk)
* and read/write less than the rest of the chunk
* 2. start at an offset not equal to 0 and read/write the rest
* of the chunk
* 3. start at offset 0 (within the chunk) and read/write less
* than entire chunk
* 4. start at offset 0 (within the chunk), and read/write
* the entire chunk
*/
chunk_start_offset = chunk * OPTOE_PAGE_SIZE;
chunk_end_offset = chunk_start_offset + OPTOE_PAGE_SIZE;
if (chunk_start_offset < off) {
chunk_offset = off;
if ((off + pending_len) < chunk_end_offset)
chunk_len = pending_len;
else
chunk_len = chunk_end_offset - off;
} else {
chunk_offset = chunk_start_offset;
if (pending_len < OPTOE_PAGE_SIZE)
chunk_len = pending_len;
else
chunk_len = OPTOE_PAGE_SIZE;
}
/*
* note: chunk_offset is from the start of the EEPROM,
* not the start of the chunk
*/
status = optoe_eeprom_update_client(optoe, buf,
chunk_offset, chunk_len, opcode);
if (status != chunk_len) {
/* This is another 'no device present' path */
dev_dbg(&client->dev,
"o_u_c: chunk %d c_offset %lld c_len %ld failed %d!\n",
chunk, chunk_offset, (long) chunk_len, status);
if (status > 0)
retval += status;
if (retval == 0)
retval = status;
break;
}
buf += status;
pending_len -= status;
retval += status;
}
mutex_unlock(&optoe->lock);
return retval;
}
static int optoe_nvmem_read(void *priv, unsigned int off,
void *buf, size_t count)
{
struct optoe_data *optoe = priv;
return optoe_read_write(optoe, buf, off, count, OPTOE_READ_OP);
}
static int optoe_nvmem_write(void *priv, unsigned int off,
void *buf, size_t count)
{
struct optoe_data *optoe = priv;
return optoe_read_write(optoe, buf, off, count, OPTOE_WRITE_OP);
}
static int optoe_remove(struct i2c_client *client)
{
struct optoe_data *optoe;
optoe = i2c_get_clientdata(client);
sysfs_remove_group(&client->dev.kobj, &optoe->attr_group);
#ifndef LATEST_KERNEL
nvmem_unregister(optoe->nvmem);
/*
* note, optoe did not register 'client', so don't unregister it
* optoe DID register the dummy client, so unregister it
*/
if (optoe->optoe_dummy.client)
i2c_unregister_device(optoe->optoe_dummy.client);
#endif
kfree(optoe);
return 0;
}
#ifndef LATEST_KERNEL
static void null_regmap_lock(void *foo)
{
}
#endif
/*
* optoe_make_regmap creates the regmap for the client.
* IMPORTANT: Don't call the regmap read/write calls directly
* for these devices. These devices are paged, and you have to
* set the page register before accessing the data in that page.
* Use the nvmem interfaces, those read/write calls use this
* driver to manage pages correctly.
*/
static struct regmap *optoe_make_regmap(struct i2c_client *client)
{
struct regmap_config regmap_config = { };
struct regmap *regmap;
/* setup a minimal regmap - 8 bits, 8 bit addresses */
regmap_config.val_bits = 8;
regmap_config.reg_bits = 8;
/* I'll handle the locking */
#ifdef LATEST_KERNEL
regmap_config.disable_locking = true;
#else
regmap_config.lock = null_regmap_lock;
regmap_config.unlock = null_regmap_lock;
regmap_config.lock_arg = client;
#endif
regmap = devm_regmap_init_i2c(client, ®map_config);
return regmap;
}
/*
* optoe_make_nvmem() unregisters the existing optoe->nvmem if it
* exists, then registers a new one. Convenient when the size
* of an EEPROM device changes.
*/
static int optoe_make_nvmem(struct optoe_data *optoe)
{
struct nvmem_config nvmem_config = { };
struct i2c_client *client = optoe->optoe_client.client;
struct device *dev = &client->dev;
nvmem_config.name = optoe->port_name;
#ifdef LATEST_KERNEL
/* NVMEM_DEVID_NONE tells nvmem not to append '0' to name */
nvmem_config.id = NVMEM_DEVID_NONE;
#endif
nvmem_config.dev = dev;
nvmem_config.read_only = false;
nvmem_config.root_only = false;
nvmem_config.owner = THIS_MODULE;
nvmem_config.compat = true;
nvmem_config.base_dev = dev;
nvmem_config.reg_read = optoe_nvmem_read;
nvmem_config.reg_write = optoe_nvmem_write;
nvmem_config.priv = optoe;
nvmem_config.stride = 1;
nvmem_config.word_size = 1;
nvmem_config.size = optoe->byte_len;
#ifdef LATEST_KERNEL
if (optoe->nvmem)
devm_nvmem_unregister(dev, optoe->nvmem);
optoe->nvmem = devm_nvmem_register(dev, &nvmem_config);
#else
if (optoe->nvmem)
nvmem_unregister(optoe->nvmem);
optoe->nvmem = nvmem_register(&nvmem_config);
#endif
dev_info(dev, "%u byte class %d EEPROM\n",
optoe->byte_len, optoe->dev_class);
return 0;
}
static ssize_t dev_class_show(struct device *dev,
struct device_attribute *dattr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct optoe_data *optoe = i2c_get_clientdata(client);
ssize_t count;
mutex_lock(&optoe->lock);
count = sprintf(buf, "%d\n", optoe->dev_class);
mutex_unlock(&optoe->lock);
return count;
}
static ssize_t dev_class_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
struct optoe_data *optoe = i2c_get_clientdata(client);
int dev_class;
struct regmap *regmap;
ssize_t err;
/*
* dev_class is actually the number of i2c addresses used, thus
* legal values are "1" (QSFP class) and "2" (SFP class)
* And... CMIS spec is 1 i2c address, but puts the pageable
* bit in a different location, so CMIS devices are "3"
*/
if (kstrtoint(buf, 0, &dev_class) != 0 ||
dev_class < 1 || dev_class > 3)
return -EINVAL;
if (optoe->dev_class == dev_class) /* no change, NOP */
return(count);
mutex_lock(&optoe->lock);
if (dev_class == TWO_ADDR) {
/* SFP family */
/* if it doesn't exist, create 0x51 i2c address */
if (!optoe->optoe_dummy.client) {
#ifdef LATEST_KERNEL
optoe->optoe_dummy.client =
devm_i2c_new_dummy_device(dev,
client->adapter,
0x51);
#else
optoe->optoe_dummy.client =
i2c_new_dummy(client->adapter, 0x51);
#endif
if (!optoe->optoe_dummy.client) {
dev_err(&client->dev,
"address 0x51 unavailable\n");
mutex_unlock(&optoe->lock);
return -EADDRINUSE;
}
regmap = optoe_make_regmap(
optoe->optoe_dummy.client);
if (IS_ERR(regmap)) {
mutex_unlock(&optoe->lock);
return PTR_ERR(regmap);
}
optoe->optoe_dummy.regmap = regmap;
}
optoe->byte_len = TWO_ADDR_EEPROM_SIZE;
} else {
/* one-address (eg QSFP) and CMIS family */
/* note, no need to delete the dummy i2c device */
optoe->byte_len = ONE_ADDR_EEPROM_SIZE;
}
optoe->dev_class = dev_class;
optoe_make_nvmem(optoe); /* updates the reported size of EEPROM */
err = (IS_ERR(optoe->nvmem)) ? PTR_ERR(optoe->nvmem) : 0;
mutex_unlock(&optoe->lock);
return err;
}
static ssize_t port_name_show(struct device *dev,
struct device_attribute *dattr, char *buf)
{
struct i2c_client *client = to_i2c_client(dev);
struct optoe_data *optoe = i2c_get_clientdata(client);
ssize_t count;
mutex_lock(&optoe->lock);
count = sprintf(buf, "%s\n", optoe->port_name);
mutex_unlock(&optoe->lock);
return count;
}
static ssize_t port_name_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *client = to_i2c_client(dev);
struct optoe_data *optoe = i2c_get_clientdata(client);
char port_name[MAX_PORT_NAME_LEN];
/* no checking, this value is not used except by port_name_show */
if (sscanf(buf, "%19s", port_name) != 1)
return -EINVAL;
mutex_lock(&optoe->lock);
strcpy(optoe->port_name, port_name);
optoe_make_nvmem(optoe); /* updates the name of the nvmem */
mutex_unlock(&optoe->lock);
return count;
}
static DEVICE_ATTR_RW(port_name);
static DEVICE_ATTR_RW(dev_class);
static struct attribute *optoe_attrs[] = {
&dev_attr_port_name.attr,
&dev_attr_dev_class.attr,
NULL,
};
static struct attribute_group optoe_attr_group = {
.attrs = optoe_attrs,
};
#ifdef LATEST_KERNEL
static int optoe_probe(struct i2c_client *client)
#else
static int optoe_probe(struct i2c_client *client,
const struct i2c_device_id *id)
#endif
{
struct regmap *regmap;
int err;
struct optoe_data *optoe;
const char *of_port_name = NULL;
struct device *dev = &client->dev;
if (client->addr != 0x50) {
dev_dbg(dev, "probe, bad i2c addr: 0x%x\n", client->addr);
err = -EINVAL;
goto exit;
}
optoe = kzalloc(sizeof(struct optoe_data), GFP_KERNEL);
if (!optoe) {
err = -ENOMEM;
goto exit;
}
mutex_init(&optoe->lock);
/*
* if the port_name property is defined, use it, else use the
* device name as port_name
*/
if (device_property_present(dev, "port_name")) {
err = device_property_read_string(dev, "port_name",
&of_port_name);
if (err)
of_port_name = dev_name(dev);
} else {
of_port_name = dev_name(dev);
}
memcpy(optoe->port_name, of_port_name, MAX_PORT_NAME_LEN);
/* determine whether this is a one-address or two-address module */
if (strcmp(client->name, "optoe1") == 0) {
/* QSFP family */
optoe->dev_class = ONE_ADDR;
optoe->byte_len = ONE_ADDR_EEPROM_SIZE;
} else if (strcmp(client->name, "optoe2") == 0) {
/* SFP family */
optoe->dev_class = TWO_ADDR;
optoe->byte_len = TWO_ADDR_EEPROM_SIZE;
} else if (strcmp(client->name, "optoe3") == 0) {
/* CMIS spec */
optoe->dev_class = CMIS_ADDR;
optoe->byte_len = ONE_ADDR_EEPROM_SIZE;
} else { /* those were the only choices */
err = -EINVAL;
goto err_struct;
}
/*
* Old application notes recommend 1 byte writes for some
* modules. This could probably be lifted, but lacking
* a broad base of devices and systems to test, I'm leaving
* this as is. If this is ever raised, the max would automatically
* be 128 bytes as anything larger would cross page
* boundaries with wraparound effects.
* TODO: Consider making this a device tree property
*/
optoe->write_max = 1;
regmap = optoe_make_regmap(client);
if (IS_ERR(regmap)) {
err = PTR_ERR(regmap);
goto err_struct;
}
optoe->optoe_client.client = client;
optoe->optoe_client.regmap = regmap;
/* SFF-8472 spec requires that the second I2C address be 0x51 */
if (optoe->dev_class == TWO_ADDR) {
#ifdef LATEST_KERNEL
optoe->optoe_dummy.client =
devm_i2c_new_dummy_device(dev, client->adapter, 0x51);
#else
optoe->optoe_dummy.client =
i2c_new_dummy(client->adapter, 0x51);
#endif
if (!optoe->optoe_dummy.client) {
dev_err(dev, "address 0x51 unavailable\n");
err = -EADDRINUSE;
goto err_struct;
}
regmap = optoe_make_regmap(optoe->optoe_dummy.client);
if (IS_ERR(regmap)) {
err = PTR_ERR(regmap);
goto err_struct;
}
optoe->optoe_dummy.regmap = regmap;
}
optoe->attr_group = optoe_attr_group;
err = sysfs_create_group(&client->dev.kobj, &optoe->attr_group);
if (err) {
dev_err(dev, "failed to create sysfs attribute group.\n");
goto err_struct;
}
i2c_set_clientdata(client, optoe);
optoe_make_nvmem(optoe);
if (IS_ERR(optoe->nvmem)) {
err = PTR_ERR(optoe->nvmem);
goto err_struct;
}
return 0;
err_struct:
#ifndef LATEST_KERNEL
if (optoe->optoe_dummy.client)
i2c_unregister_device(optoe->optoe_dummy.client);
#endif
kfree(optoe);
exit:
dev_dbg(dev, "probe error %d\n", err);
return err;
}
/*-------------------------------------------------------------------------*/
static struct i2c_driver optoe_driver = {
.driver = {
.name = "optoe",
.owner = THIS_MODULE,
},
#ifdef LATEST_KERNEL
.probe_new = optoe_probe,
#else
.probe = optoe_probe,
#endif
.remove = optoe_remove,
.id_table = optoe_ids,
};
static int __init optoe_init(void)
{
return i2c_add_driver(&optoe_driver);
}
module_init(optoe_init);
static void __exit optoe_exit(void)
{