xref: /linux/drivers/thunderbolt/eeprom.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
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 /* USB4 product descriptor */
281 struct tb_drom_entry_desc {
282 	struct tb_drom_entry_header header;
283 	u16 bcdUSBSpec;
284 	u16 idVendor;
285 	u16 idProduct;
286 	u16 bcdProductFWRevision;
287 	u32 TID;
288 	u8 productHWRevision;
289 };
290 
291 /**
292  * tb_drom_read_uid_only() - Read UID directly from DROM
293  * @sw: Router whose UID to read
294  * @uid: UID is placed here
295  *
296  * Does not use the cached copy in sw->drom. Used during resume to check switch
297  * identity.
298  */
299 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
300 {
301 	u8 data[9];
302 	u8 crc;
303 	int res;
304 
305 	/* read uid */
306 	res = tb_eeprom_read_n(sw, 0, data, 9);
307 	if (res)
308 		return res;
309 
310 	crc = tb_crc8(data + 1, 8);
311 	if (crc != data[0]) {
312 		tb_sw_warn(sw, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
313 				data[0], crc);
314 		return -EIO;
315 	}
316 
317 	*uid = *(u64 *)(data+1);
318 	return 0;
319 }
320 
321 static int tb_drom_parse_entry_generic(struct tb_switch *sw,
322 		struct tb_drom_entry_header *header)
323 {
324 	const struct tb_drom_entry_generic *entry =
325 		(const struct tb_drom_entry_generic *)header;
326 
327 	switch (header->index) {
328 	case 1:
329 		/* Length includes 2 bytes header so remove it before copy */
330 		sw->vendor_name = kstrndup(entry->data,
331 			header->len - sizeof(*header), GFP_KERNEL);
332 		if (!sw->vendor_name)
333 			return -ENOMEM;
334 		break;
335 
336 	case 2:
337 		sw->device_name = kstrndup(entry->data,
338 			header->len - sizeof(*header), GFP_KERNEL);
339 		if (!sw->device_name)
340 			return -ENOMEM;
341 		break;
342 	case 9: {
343 		const struct tb_drom_entry_desc *desc =
344 			(const struct tb_drom_entry_desc *)entry;
345 
346 		if (!sw->vendor && !sw->device) {
347 			sw->vendor = desc->idVendor;
348 			sw->device = desc->idProduct;
349 		}
350 		break;
351 	}
352 	}
353 
354 	return 0;
355 }
356 
357 static int tb_drom_parse_entry_port(struct tb_switch *sw,
358 				    struct tb_drom_entry_header *header)
359 {
360 	struct tb_port *port;
361 	int res;
362 	enum tb_port_type type;
363 
364 	/*
365 	 * Some DROMs list more ports than the controller actually has
366 	 * so we skip those but allow the parser to continue.
367 	 */
368 	if (header->index > sw->config.max_port_number) {
369 		dev_info_once(&sw->dev, "ignoring unnecessary extra entries in DROM\n");
370 		return 0;
371 	}
372 
373 	port = &sw->ports[header->index];
374 	port->disabled = header->port_disabled;
375 	if (port->disabled)
376 		return 0;
377 
378 	res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
379 	if (res)
380 		return res;
381 	type &= 0xffffff;
382 
383 	if (type == TB_TYPE_PORT) {
384 		struct tb_drom_entry_port *entry = (void *) header;
385 		if (header->len != sizeof(*entry)) {
386 			tb_sw_warn(sw,
387 				"port entry has size %#x (expected %#zx)\n",
388 				header->len, sizeof(struct tb_drom_entry_port));
389 			return -EIO;
390 		}
391 		port->link_nr = entry->link_nr;
392 		if (entry->has_dual_link_port)
393 			port->dual_link_port =
394 				&port->sw->ports[entry->dual_link_port_nr];
395 	}
396 	return 0;
397 }
398 
399 /*
400  * tb_drom_parse_entries - parse the linked list of drom entries
401  *
402  * Drom must have been copied to sw->drom.
403  */
404 static int tb_drom_parse_entries(struct tb_switch *sw)
405 {
406 	struct tb_drom_header *header = (void *) sw->drom;
407 	u16 pos = sizeof(*header);
408 	u16 drom_size = header->data_len + TB_DROM_DATA_START;
409 	int res;
410 
411 	while (pos < drom_size) {
412 		struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
413 		if (pos + 1 == drom_size || pos + entry->len > drom_size
414 				|| !entry->len) {
415 			tb_sw_warn(sw, "DROM buffer overrun\n");
416 			return -EILSEQ;
417 		}
418 
419 		switch (entry->type) {
420 		case TB_DROM_ENTRY_GENERIC:
421 			res = tb_drom_parse_entry_generic(sw, entry);
422 			break;
423 		case TB_DROM_ENTRY_PORT:
424 			res = tb_drom_parse_entry_port(sw, entry);
425 			break;
426 		}
427 		if (res)
428 			return res;
429 
430 		pos += entry->len;
431 	}
432 	return 0;
433 }
434 
435 /*
436  * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
437  */
438 static int tb_drom_copy_efi(struct tb_switch *sw, u16 *size)
439 {
440 	struct device *dev = &sw->tb->nhi->pdev->dev;
441 	int len, res;
442 
443 	len = device_property_count_u8(dev, "ThunderboltDROM");
444 	if (len < 0 || len < sizeof(struct tb_drom_header))
445 		return -EINVAL;
446 
447 	sw->drom = kmalloc(len, GFP_KERNEL);
448 	if (!sw->drom)
449 		return -ENOMEM;
450 
451 	res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom,
452 									len);
453 	if (res)
454 		goto err;
455 
456 	*size = ((struct tb_drom_header *)sw->drom)->data_len +
457 							  TB_DROM_DATA_START;
458 	if (*size > len)
459 		goto err;
460 
461 	return 0;
462 
463 err:
464 	kfree(sw->drom);
465 	sw->drom = NULL;
466 	return -EINVAL;
467 }
468 
469 static int tb_drom_copy_nvm(struct tb_switch *sw, u16 *size)
470 {
471 	u32 drom_offset;
472 	int ret;
473 
474 	if (!sw->dma_port)
475 		return -ENODEV;
476 
477 	ret = tb_sw_read(sw, &drom_offset, TB_CFG_SWITCH,
478 			 sw->cap_plug_events + 12, 1);
479 	if (ret)
480 		return ret;
481 
482 	if (!drom_offset)
483 		return -ENODEV;
484 
485 	ret = dma_port_flash_read(sw->dma_port, drom_offset + 14, size,
486 				  sizeof(*size));
487 	if (ret)
488 		return ret;
489 
490 	/* Size includes CRC8 + UID + CRC32 */
491 	*size += 1 + 8 + 4;
492 	sw->drom = kzalloc(*size, GFP_KERNEL);
493 	if (!sw->drom)
494 		return -ENOMEM;
495 
496 	ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size);
497 	if (ret)
498 		goto err_free;
499 
500 	/*
501 	 * Read UID from the minimal DROM because the one in NVM is just
502 	 * a placeholder.
503 	 */
504 	tb_drom_read_uid_only(sw, &sw->uid);
505 	return 0;
506 
507 err_free:
508 	kfree(sw->drom);
509 	sw->drom = NULL;
510 	return ret;
511 }
512 
513 static int usb4_copy_host_drom(struct tb_switch *sw, u16 *size)
514 {
515 	int ret;
516 
517 	ret = usb4_switch_drom_read(sw, 14, size, sizeof(*size));
518 	if (ret)
519 		return ret;
520 
521 	/* Size includes CRC8 + UID + CRC32 */
522 	*size += 1 + 8 + 4;
523 	sw->drom = kzalloc(*size, GFP_KERNEL);
524 	if (!sw->drom)
525 		return -ENOMEM;
526 
527 	ret = usb4_switch_drom_read(sw, 0, sw->drom, *size);
528 	if (ret) {
529 		kfree(sw->drom);
530 		sw->drom = NULL;
531 	}
532 
533 	return ret;
534 }
535 
536 static int tb_drom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
537 			  size_t count)
538 {
539 	if (tb_switch_is_usb4(sw))
540 		return usb4_switch_drom_read(sw, offset, val, count);
541 	return tb_eeprom_read_n(sw, offset, val, count);
542 }
543 
544 static int tb_drom_parse(struct tb_switch *sw)
545 {
546 	const struct tb_drom_header *header =
547 		(const struct tb_drom_header *)sw->drom;
548 	u32 crc;
549 
550 	crc = tb_crc8((u8 *) &header->uid, 8);
551 	if (crc != header->uid_crc8) {
552 		tb_sw_warn(sw,
553 			"DROM UID CRC8 mismatch (expected: %#x, got: %#x), aborting\n",
554 			header->uid_crc8, crc);
555 		return -EINVAL;
556 	}
557 	if (!sw->uid)
558 		sw->uid = header->uid;
559 	sw->vendor = header->vendor_id;
560 	sw->device = header->model_id;
561 
562 	crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
563 	if (crc != header->data_crc32) {
564 		tb_sw_warn(sw,
565 			"DROM data CRC32 mismatch (expected: %#x, got: %#x), continuing\n",
566 			header->data_crc32, crc);
567 	}
568 
569 	return tb_drom_parse_entries(sw);
570 }
571 
572 static int usb4_drom_parse(struct tb_switch *sw)
573 {
574 	const struct tb_drom_header *header =
575 		(const struct tb_drom_header *)sw->drom;
576 	u32 crc;
577 
578 	crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
579 	if (crc != header->data_crc32) {
580 		tb_sw_warn(sw,
581 			   "DROM data CRC32 mismatch (expected: %#x, got: %#x), aborting\n",
582 			   header->data_crc32, crc);
583 		return -EINVAL;
584 	}
585 
586 	return tb_drom_parse_entries(sw);
587 }
588 
589 /**
590  * tb_drom_read() - Copy DROM to sw->drom and parse it
591  * @sw: Router whose DROM to read and parse
592  *
593  * This function reads router DROM and if successful parses the entries and
594  * populates the fields in @sw accordingly. Can be called for any router
595  * generation.
596  *
597  * Returns %0 in case of success and negative errno otherwise.
598  */
599 int tb_drom_read(struct tb_switch *sw)
600 {
601 	u16 size;
602 	struct tb_drom_header *header;
603 	int res, retries = 1;
604 
605 	if (sw->drom)
606 		return 0;
607 
608 	if (tb_route(sw) == 0) {
609 		/*
610 		 * Apple's NHI EFI driver supplies a DROM for the root switch
611 		 * in a device property. Use it if available.
612 		 */
613 		if (tb_drom_copy_efi(sw, &size) == 0)
614 			goto parse;
615 
616 		/* Non-Apple hardware has the DROM as part of NVM */
617 		if (tb_drom_copy_nvm(sw, &size) == 0)
618 			goto parse;
619 
620 		/*
621 		 * USB4 hosts may support reading DROM through router
622 		 * operations.
623 		 */
624 		if (tb_switch_is_usb4(sw)) {
625 			usb4_switch_read_uid(sw, &sw->uid);
626 			if (!usb4_copy_host_drom(sw, &size))
627 				goto parse;
628 		} else {
629 			/*
630 			 * The root switch contains only a dummy drom
631 			 * (header only, no entries). Hardcode the
632 			 * configuration here.
633 			 */
634 			tb_drom_read_uid_only(sw, &sw->uid);
635 		}
636 
637 		return 0;
638 	}
639 
640 	res = tb_drom_read_n(sw, 14, (u8 *) &size, 2);
641 	if (res)
642 		return res;
643 	size &= 0x3ff;
644 	size += TB_DROM_DATA_START;
645 	tb_sw_dbg(sw, "reading drom (length: %#x)\n", size);
646 	if (size < sizeof(*header)) {
647 		tb_sw_warn(sw, "drom too small, aborting\n");
648 		return -EIO;
649 	}
650 
651 	sw->drom = kzalloc(size, GFP_KERNEL);
652 	if (!sw->drom)
653 		return -ENOMEM;
654 	res = tb_drom_read_n(sw, 0, sw->drom, size);
655 	if (res)
656 		goto err;
657 
658 parse:
659 	header = (void *) sw->drom;
660 
661 	if (header->data_len + TB_DROM_DATA_START != size) {
662 		tb_sw_warn(sw, "drom size mismatch, aborting\n");
663 		goto err;
664 	}
665 
666 	tb_sw_dbg(sw, "DROM version: %d\n", header->device_rom_revision);
667 
668 	switch (header->device_rom_revision) {
669 	case 3:
670 		res = usb4_drom_parse(sw);
671 		break;
672 	default:
673 		tb_sw_warn(sw, "DROM device_rom_revision %#x unknown\n",
674 			   header->device_rom_revision);
675 		fallthrough;
676 	case 1:
677 		res = tb_drom_parse(sw);
678 		break;
679 	}
680 
681 	/* If the DROM parsing fails, wait a moment and retry once */
682 	if (res == -EILSEQ && retries--) {
683 		tb_sw_warn(sw, "parsing DROM failed, retrying\n");
684 		msleep(100);
685 		res = tb_drom_read_n(sw, 0, sw->drom, size);
686 		if (!res)
687 			goto parse;
688 	}
689 
690 	if (!res)
691 		return 0;
692 
693 err:
694 	kfree(sw->drom);
695 	sw->drom = NULL;
696 	return -EIO;
697 }
698