Merge branch 'next' into for-linus
[linux-2.6-microblaze.git] / drivers / thunderbolt / eeprom.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thunderbolt driver - eeprom access
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2018, Intel Corporation
7  */
8
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
13 #include "tb.h"
14
15 /*
16  * tb_eeprom_ctl_write() - write control word
17  */
18 static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
19 {
20         return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
21 }
22
23 /*
24  * tb_eeprom_ctl_write() - read control word
25  */
26 static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
27 {
28         return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
29 }
30
31 enum tb_eeprom_transfer {
32         TB_EEPROM_IN,
33         TB_EEPROM_OUT,
34 };
35
36 /*
37  * tb_eeprom_active - enable rom access
38  *
39  * WARNING: Always disable access after usage. Otherwise the controller will
40  * fail to reprobe.
41  */
42 static int tb_eeprom_active(struct tb_switch *sw, bool enable)
43 {
44         struct tb_eeprom_ctl ctl;
45         int res = tb_eeprom_ctl_read(sw, &ctl);
46         if (res)
47                 return res;
48         if (enable) {
49                 ctl.access_high = 1;
50                 res = tb_eeprom_ctl_write(sw, &ctl);
51                 if (res)
52                         return res;
53                 ctl.access_low = 0;
54                 return tb_eeprom_ctl_write(sw, &ctl);
55         } else {
56                 ctl.access_low = 1;
57                 res = tb_eeprom_ctl_write(sw, &ctl);
58                 if (res)
59                         return res;
60                 ctl.access_high = 0;
61                 return tb_eeprom_ctl_write(sw, &ctl);
62         }
63 }
64
65 /*
66  * tb_eeprom_transfer - transfer one bit
67  *
68  * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
69  * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
70  */
71 static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
72                               enum tb_eeprom_transfer direction)
73 {
74         int res;
75         if (direction == TB_EEPROM_OUT) {
76                 res = tb_eeprom_ctl_write(sw, ctl);
77                 if (res)
78                         return res;
79         }
80         ctl->clock = 1;
81         res = tb_eeprom_ctl_write(sw, ctl);
82         if (res)
83                 return res;
84         if (direction == TB_EEPROM_IN) {
85                 res = tb_eeprom_ctl_read(sw, ctl);
86                 if (res)
87                         return res;
88         }
89         ctl->clock = 0;
90         return tb_eeprom_ctl_write(sw, ctl);
91 }
92
93 /*
94  * tb_eeprom_out - write one byte to the bus
95  */
96 static int tb_eeprom_out(struct tb_switch *sw, u8 val)
97 {
98         struct tb_eeprom_ctl ctl;
99         int i;
100         int res = tb_eeprom_ctl_read(sw, &ctl);
101         if (res)
102                 return res;
103         for (i = 0; i < 8; i++) {
104                 ctl.data_out = val & 0x80;
105                 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
106                 if (res)
107                         return res;
108                 val <<= 1;
109         }
110         return 0;
111 }
112
113 /*
114  * tb_eeprom_in - read one byte from the bus
115  */
116 static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
117 {
118         struct tb_eeprom_ctl ctl;
119         int i;
120         int res = tb_eeprom_ctl_read(sw, &ctl);
121         if (res)
122                 return res;
123         *val = 0;
124         for (i = 0; i < 8; i++) {
125                 *val <<= 1;
126                 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
127                 if (res)
128                         return res;
129                 *val |= ctl.data_in;
130         }
131         return 0;
132 }
133
134 /*
135  * tb_eeprom_get_drom_offset - get drom offset within eeprom
136  */
137 static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
138 {
139         struct tb_cap_plug_events cap;
140         int res;
141
142         if (!sw->cap_plug_events) {
143                 tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
144                 return -ENODEV;
145         }
146         res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
147                              sizeof(cap) / 4);
148         if (res)
149                 return res;
150
151         if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
152                 tb_sw_warn(sw, "no NVM\n");
153                 return -ENODEV;
154         }
155
156         if (cap.drom_offset > 0xffff) {
157                 tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
158                                 cap.drom_offset);
159                 return -ENXIO;
160         }
161         *offset = cap.drom_offset;
162         return 0;
163 }
164
165 /*
166  * tb_eeprom_read_n - read count bytes from offset into val
167  */
168 static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
169                 size_t count)
170 {
171         u16 drom_offset;
172         int i, res;
173
174         res = tb_eeprom_get_drom_offset(sw, &drom_offset);
175         if (res)
176                 return res;
177
178         offset += drom_offset;
179
180         res = tb_eeprom_active(sw, true);
181         if (res)
182                 return res;
183         res = tb_eeprom_out(sw, 3);
184         if (res)
185                 return res;
186         res = tb_eeprom_out(sw, offset >> 8);
187         if (res)
188                 return res;
189         res = tb_eeprom_out(sw, offset);
190         if (res)
191                 return res;
192         for (i = 0; i < count; i++) {
193                 res = tb_eeprom_in(sw, val + i);
194                 if (res)
195                         return res;
196         }
197         return tb_eeprom_active(sw, false);
198 }
199
200 static u8 tb_crc8(u8 *data, int len)
201 {
202         int i, j;
203         u8 val = 0xff;
204         for (i = 0; i < len; i++) {
205                 val ^= data[i];
206                 for (j = 0; j < 8; j++)
207                         val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
208         }
209         return val;
210 }
211
212 static u32 tb_crc32(void *data, size_t len)
213 {
214         return ~__crc32c_le(~0, data, len);
215 }
216
217 #define TB_DROM_DATA_START 13
218 struct tb_drom_header {
219         /* BYTE 0 */
220         u8 uid_crc8; /* checksum for uid */
221         /* BYTES 1-8 */
222         u64 uid;
223         /* BYTES 9-12 */
224         u32 data_crc32; /* checksum for data_len bytes starting at byte 13 */
225         /* BYTE 13 */
226         u8 device_rom_revision; /* should be <= 1 */
227         u16 data_len:10;
228         u8 __unknown1:6;
229         /* BYTES 16-21 */
230         u16 vendor_id;
231         u16 model_id;
232         u8 model_rev;
233         u8 eeprom_rev;
234 } __packed;
235
236 enum tb_drom_entry_type {
237         /* force unsigned to prevent "one-bit signed bitfield" warning */
238         TB_DROM_ENTRY_GENERIC = 0U,
239         TB_DROM_ENTRY_PORT,
240 };
241
242 struct tb_drom_entry_header {
243         u8 len;
244         u8 index:6;
245         bool port_disabled:1; /* only valid if type is TB_DROM_ENTRY_PORT */
246         enum tb_drom_entry_type type:1;
247 } __packed;
248
249 struct tb_drom_entry_generic {
250         struct tb_drom_entry_header header;
251         u8 data[];
252 } __packed;
253
254 struct tb_drom_entry_port {
255         /* BYTES 0-1 */
256         struct tb_drom_entry_header header;
257         /* BYTE 2 */
258         u8 dual_link_port_rid:4;
259         u8 link_nr:1;
260         u8 unknown1:2;
261         bool has_dual_link_port:1;
262
263         /* BYTE 3 */
264         u8 dual_link_port_nr:6;
265         u8 unknown2:2;
266
267         /* BYTES 4 - 5 TODO decode */
268         u8 micro2:4;
269         u8 micro1:4;
270         u8 micro3;
271
272         /* BYTES 6-7, TODO: verify (find hardware that has these set) */
273         u8 peer_port_rid:4;
274         u8 unknown3:3;
275         bool has_peer_port:1;
276         u8 peer_port_nr:6;
277         u8 unknown4:2;
278 } __packed;
279
280
281 /**
282  * tb_drom_read_uid_only() - Read UID directly from DROM
283  * @sw: Router whose UID to read
284  * @uid: UID is placed here
285  *
286  * Does not use the cached copy in sw->drom. Used during resume to check switch
287  * identity.
288  */
289 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
290 {
291         u8 data[9];
292         u8 crc;
293         int res;
294
295         /* read uid */
296         res = tb_eeprom_read_n(sw, 0, data, 9);
297         if (res)
298                 return res;
299
300         crc = tb_crc8(data + 1, 8);
301         if (crc != data[0]) {
302                 tb_sw_warn(sw, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
303                                 data[0], crc);
304                 return -EIO;
305         }
306
307         *uid = *(u64 *)(data+1);
308         return 0;
309 }
310
311 static int tb_drom_parse_entry_generic(struct tb_switch *sw,
312                 struct tb_drom_entry_header *header)
313 {
314         const struct tb_drom_entry_generic *entry =
315                 (const struct tb_drom_entry_generic *)header;
316
317         switch (header->index) {
318         case 1:
319                 /* Length includes 2 bytes header so remove it before copy */
320                 sw->vendor_name = kstrndup(entry->data,
321                         header->len - sizeof(*header), GFP_KERNEL);
322                 if (!sw->vendor_name)
323                         return -ENOMEM;
324                 break;
325
326         case 2:
327                 sw->device_name = kstrndup(entry->data,
328                         header->len - sizeof(*header), GFP_KERNEL);
329                 if (!sw->device_name)
330                         return -ENOMEM;
331                 break;
332         }
333
334         return 0;
335 }
336
337 static int tb_drom_parse_entry_port(struct tb_switch *sw,
338                                     struct tb_drom_entry_header *header)
339 {
340         struct tb_port *port;
341         int res;
342         enum tb_port_type type;
343
344         /*
345          * Some DROMs list more ports than the controller actually has
346          * so we skip those but allow the parser to continue.
347          */
348         if (header->index > sw->config.max_port_number) {
349                 dev_info_once(&sw->dev, "ignoring unnecessary extra entries in DROM\n");
350                 return 0;
351         }
352
353         port = &sw->ports[header->index];
354         port->disabled = header->port_disabled;
355         if (port->disabled)
356                 return 0;
357
358         res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
359         if (res)
360                 return res;
361         type &= 0xffffff;
362
363         if (type == TB_TYPE_PORT) {
364                 struct tb_drom_entry_port *entry = (void *) header;
365                 if (header->len != sizeof(*entry)) {
366                         tb_sw_warn(sw,
367                                 "port entry has size %#x (expected %#zx)\n",
368                                 header->len, sizeof(struct tb_drom_entry_port));
369                         return -EIO;
370                 }
371                 port->link_nr = entry->link_nr;
372                 if (entry->has_dual_link_port)
373                         port->dual_link_port =
374                                 &port->sw->ports[entry->dual_link_port_nr];
375         }
376         return 0;
377 }
378
379 /*
380  * tb_drom_parse_entries - parse the linked list of drom entries
381  *
382  * Drom must have been copied to sw->drom.
383  */
384 static int tb_drom_parse_entries(struct tb_switch *sw)
385 {
386         struct tb_drom_header *header = (void *) sw->drom;
387         u16 pos = sizeof(*header);
388         u16 drom_size = header->data_len + TB_DROM_DATA_START;
389         int res;
390
391         while (pos < drom_size) {
392                 struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
393                 if (pos + 1 == drom_size || pos + entry->len > drom_size
394                                 || !entry->len) {
395                         tb_sw_warn(sw, "DROM buffer overrun\n");
396                         return -EILSEQ;
397                 }
398
399                 switch (entry->type) {
400                 case TB_DROM_ENTRY_GENERIC:
401                         res = tb_drom_parse_entry_generic(sw, entry);
402                         break;
403                 case TB_DROM_ENTRY_PORT:
404                         res = tb_drom_parse_entry_port(sw, entry);
405                         break;
406                 }
407                 if (res)
408                         return res;
409
410                 pos += entry->len;
411         }
412         return 0;
413 }
414
415 /*
416  * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
417  */
418 static int tb_drom_copy_efi(struct tb_switch *sw, u16 *size)
419 {
420         struct device *dev = &sw->tb->nhi->pdev->dev;
421         int len, res;
422
423         len = device_property_count_u8(dev, "ThunderboltDROM");
424         if (len < 0 || len < sizeof(struct tb_drom_header))
425                 return -EINVAL;
426
427         sw->drom = kmalloc(len, GFP_KERNEL);
428         if (!sw->drom)
429                 return -ENOMEM;
430
431         res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom,
432                                                                         len);
433         if (res)
434                 goto err;
435
436         *size = ((struct tb_drom_header *)sw->drom)->data_len +
437                                                           TB_DROM_DATA_START;
438         if (*size > len)
439                 goto err;
440
441         return 0;
442
443 err:
444         kfree(sw->drom);
445         sw->drom = NULL;
446         return -EINVAL;
447 }
448
449 static int tb_drom_copy_nvm(struct tb_switch *sw, u16 *size)
450 {
451         u32 drom_offset;
452         int ret;
453
454         if (!sw->dma_port)
455                 return -ENODEV;
456
457         ret = tb_sw_read(sw, &drom_offset, TB_CFG_SWITCH,
458                          sw->cap_plug_events + 12, 1);
459         if (ret)
460                 return ret;
461
462         if (!drom_offset)
463                 return -ENODEV;
464
465         ret = dma_port_flash_read(sw->dma_port, drom_offset + 14, size,
466                                   sizeof(*size));
467         if (ret)
468                 return ret;
469
470         /* Size includes CRC8 + UID + CRC32 */
471         *size += 1 + 8 + 4;
472         sw->drom = kzalloc(*size, GFP_KERNEL);
473         if (!sw->drom)
474                 return -ENOMEM;
475
476         ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size);
477         if (ret)
478                 goto err_free;
479
480         /*
481          * Read UID from the minimal DROM because the one in NVM is just
482          * a placeholder.
483          */
484         tb_drom_read_uid_only(sw, &sw->uid);
485         return 0;
486
487 err_free:
488         kfree(sw->drom);
489         sw->drom = NULL;
490         return ret;
491 }
492
493 static int usb4_copy_host_drom(struct tb_switch *sw, u16 *size)
494 {
495         int ret;
496
497         ret = usb4_switch_drom_read(sw, 14, size, sizeof(*size));
498         if (ret)
499                 return ret;
500
501         /* Size includes CRC8 + UID + CRC32 */
502         *size += 1 + 8 + 4;
503         sw->drom = kzalloc(*size, GFP_KERNEL);
504         if (!sw->drom)
505                 return -ENOMEM;
506
507         ret = usb4_switch_drom_read(sw, 0, sw->drom, *size);
508         if (ret) {
509                 kfree(sw->drom);
510                 sw->drom = NULL;
511         }
512
513         return ret;
514 }
515
516 static int tb_drom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
517                           size_t count)
518 {
519         if (tb_switch_is_usb4(sw))
520                 return usb4_switch_drom_read(sw, offset, val, count);
521         return tb_eeprom_read_n(sw, offset, val, count);
522 }
523
524 /**
525  * tb_drom_read() - Copy DROM to sw->drom and parse it
526  * @sw: Router whose DROM to read and parse
527  *
528  * This function reads router DROM and if successful parses the entries and
529  * populates the fields in @sw accordingly. Can be called for any router
530  * generation.
531  *
532  * Returns %0 in case of success and negative errno otherwise.
533  */
534 int tb_drom_read(struct tb_switch *sw)
535 {
536         u16 size;
537         u32 crc;
538         struct tb_drom_header *header;
539         int res, retries = 1;
540
541         if (sw->drom)
542                 return 0;
543
544         if (tb_route(sw) == 0) {
545                 /*
546                  * Apple's NHI EFI driver supplies a DROM for the root switch
547                  * in a device property. Use it if available.
548                  */
549                 if (tb_drom_copy_efi(sw, &size) == 0)
550                         goto parse;
551
552                 /* Non-Apple hardware has the DROM as part of NVM */
553                 if (tb_drom_copy_nvm(sw, &size) == 0)
554                         goto parse;
555
556                 /*
557                  * USB4 hosts may support reading DROM through router
558                  * operations.
559                  */
560                 if (tb_switch_is_usb4(sw)) {
561                         usb4_switch_read_uid(sw, &sw->uid);
562                         if (!usb4_copy_host_drom(sw, &size))
563                                 goto parse;
564                 } else {
565                         /*
566                          * The root switch contains only a dummy drom
567                          * (header only, no entries). Hardcode the
568                          * configuration here.
569                          */
570                         tb_drom_read_uid_only(sw, &sw->uid);
571                 }
572
573                 return 0;
574         }
575
576         res = tb_drom_read_n(sw, 14, (u8 *) &size, 2);
577         if (res)
578                 return res;
579         size &= 0x3ff;
580         size += TB_DROM_DATA_START;
581         tb_sw_dbg(sw, "reading drom (length: %#x)\n", size);
582         if (size < sizeof(*header)) {
583                 tb_sw_warn(sw, "drom too small, aborting\n");
584                 return -EIO;
585         }
586
587         sw->drom = kzalloc(size, GFP_KERNEL);
588         if (!sw->drom)
589                 return -ENOMEM;
590         res = tb_drom_read_n(sw, 0, sw->drom, size);
591         if (res)
592                 goto err;
593
594 parse:
595         header = (void *) sw->drom;
596
597         if (header->data_len + TB_DROM_DATA_START != size) {
598                 tb_sw_warn(sw, "drom size mismatch, aborting\n");
599                 goto err;
600         }
601
602         crc = tb_crc8((u8 *) &header->uid, 8);
603         if (crc != header->uid_crc8) {
604                 tb_sw_warn(sw,
605                         "drom uid crc8 mismatch (expected: %#x, got: %#x), aborting\n",
606                         header->uid_crc8, crc);
607                 goto err;
608         }
609         if (!sw->uid)
610                 sw->uid = header->uid;
611         sw->vendor = header->vendor_id;
612         sw->device = header->model_id;
613         tb_check_quirks(sw);
614
615         crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
616         if (crc != header->data_crc32) {
617                 tb_sw_warn(sw,
618                         "drom data crc32 mismatch (expected: %#x, got: %#x), continuing\n",
619                         header->data_crc32, crc);
620         }
621
622         if (header->device_rom_revision > 2)
623                 tb_sw_warn(sw, "drom device_rom_revision %#x unknown\n",
624                         header->device_rom_revision);
625
626         res = tb_drom_parse_entries(sw);
627         /* If the DROM parsing fails, wait a moment and retry once */
628         if (res == -EILSEQ && retries--) {
629                 tb_sw_warn(sw, "parsing DROM failed, retrying\n");
630                 msleep(100);
631                 res = tb_drom_read_n(sw, 0, sw->drom, size);
632                 if (!res)
633                         goto parse;
634         }
635
636         return res;
637 err:
638         kfree(sw->drom);
639         sw->drom = NULL;
640         return -EIO;
641
642 }