Lines Matching refs:sw

18 static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
20 return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
26 static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
28 return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
42 static int tb_eeprom_active(struct tb_switch *sw, bool enable)
45 int res = tb_eeprom_ctl_read(sw, &ctl);
50 res = tb_eeprom_ctl_write(sw, &ctl);
54 return tb_eeprom_ctl_write(sw, &ctl);
57 res = tb_eeprom_ctl_write(sw, &ctl);
61 return tb_eeprom_ctl_write(sw, &ctl);
71 static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
76 res = tb_eeprom_ctl_write(sw, ctl);
81 res = tb_eeprom_ctl_write(sw, ctl);
85 res = tb_eeprom_ctl_read(sw, ctl);
90 return tb_eeprom_ctl_write(sw, ctl);
96 static int tb_eeprom_out(struct tb_switch *sw, u8 val)
100 int res = tb_eeprom_ctl_read(sw, &ctl);
105 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
116 static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
120 int res = tb_eeprom_ctl_read(sw, &ctl);
126 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
137 static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
142 if (!sw->cap_plug_events) {
143 tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
146 res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
152 tb_sw_warn(sw, "no NVM\n");
157 tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
168 static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
174 res = tb_eeprom_get_drom_offset(sw, &drom_offset);
180 res = tb_eeprom_active(sw, true);
183 res = tb_eeprom_out(sw, 3);
186 res = tb_eeprom_out(sw, offset >> 8);
189 res = tb_eeprom_out(sw, offset);
193 res = tb_eeprom_in(sw, val + i);
197 return tb_eeprom_active(sw, false);
284 * Does not use the cached copy in sw->drom. Used during resume to check switch
287 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
294 res = tb_eeprom_read_n(sw, 0, data, 9);
300 tb_sw_warn(sw, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
309 static int tb_drom_parse_entry_generic(struct tb_switch *sw,
318 sw->vendor_name = kstrndup(entry->data,
320 if (!sw->vendor_name)
325 sw->device_name = kstrndup(entry->data,
327 if (!sw->device_name)
335 static int tb_drom_parse_entry_port(struct tb_switch *sw,
346 if (header->index > sw->config.max_port_number) {
347 dev_info_once(&sw->dev, "ignoring unnecessary extra entries in DROM\n");
351 port = &sw->ports[header->index];
364 tb_sw_warn(sw,
372 &port->sw->ports[entry->dual_link_port_nr];
380 * Drom must have been copied to sw->drom.
382 static int tb_drom_parse_entries(struct tb_switch *sw)
384 struct tb_drom_header *header = (void *) sw->drom;
390 struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
393 tb_sw_warn(sw, "DROM buffer overrun\n");
399 res = tb_drom_parse_entry_generic(sw, entry);
402 res = tb_drom_parse_entry_port(sw, entry);
414 * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
416 static int tb_drom_copy_efi(struct tb_switch *sw, u16 *size)
418 struct device *dev = &sw->tb->nhi->pdev->dev;
425 sw->drom = kmalloc(len, GFP_KERNEL);
426 if (!sw->drom)
429 res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom,
434 *size = ((struct tb_drom_header *)sw->drom)->data_len +
442 kfree(sw->drom);
443 sw->drom = NULL;
447 static int tb_drom_copy_nvm(struct tb_switch *sw, u16 *size)
452 if (!sw->dma_port)
455 ret = tb_sw_read(sw, &drom_offset, TB_CFG_SWITCH,
456 sw->cap_plug_events + 12, 1);
463 ret = dma_port_flash_read(sw->dma_port, drom_offset + 14, size,
470 sw->drom = kzalloc(*size, GFP_KERNEL);
471 if (!sw->drom)
474 ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size);
482 tb_drom_read_uid_only(sw, &sw->uid);
486 kfree(sw->drom);
487 sw->drom = NULL;
491 static int usb4_copy_host_drom(struct tb_switch *sw, u16 *size)
495 ret = usb4_switch_drom_read(sw, 14, size, sizeof(*size));
501 sw->drom = kzalloc(*size, GFP_KERNEL);
502 if (!sw->drom)
505 ret = usb4_switch_drom_read(sw, 0, sw->drom, *size);
507 kfree(sw->drom);
508 sw->drom = NULL;
514 static int tb_drom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
517 if (tb_switch_is_usb4(sw))
518 return usb4_switch_drom_read(sw, offset, val, count);
519 return tb_eeprom_read_n(sw, offset, val, count);
523 * tb_drom_read - copy drom to sw->drom and parse it
525 int tb_drom_read(struct tb_switch *sw)
532 if (sw->drom)
535 if (tb_route(sw) == 0) {
540 if (tb_drom_copy_efi(sw, &size) == 0)
544 if (tb_drom_copy_nvm(sw, &size) == 0)
551 if (tb_switch_is_usb4(sw)) {
552 usb4_switch_read_uid(sw, &sw->uid);
553 if (!usb4_copy_host_drom(sw, &size))
561 tb_drom_read_uid_only(sw, &sw->uid);
567 res = tb_drom_read_n(sw, 14, (u8 *) &size, 2);
572 tb_sw_dbg(sw, "reading drom (length: %#x)\n", size);
574 tb_sw_warn(sw, "drom too small, aborting\n");
578 sw->drom = kzalloc(size, GFP_KERNEL);
579 if (!sw->drom)
581 res = tb_drom_read_n(sw, 0, sw->drom, size);
586 header = (void *) sw->drom;
589 tb_sw_warn(sw, "drom size mismatch, aborting\n");
595 tb_sw_warn(sw,
600 if (!sw->uid)
601 sw->uid = header->uid;
602 sw->vendor = header->vendor_id;
603 sw->device = header->model_id;
604 tb_check_quirks(sw);
606 crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
608 tb_sw_warn(sw,
614 tb_sw_warn(sw, "drom device_rom_revision %#x unknown\n",
617 res = tb_drom_parse_entries(sw);
620 tb_sw_warn(sw, "parsing DROM failed, retrying\n");
622 res = tb_drom_read_n(sw, 0, sw->drom, size);
629 kfree(sw->drom);
630 sw->drom = NULL;