Lines Matching +full:sw +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - switch/port utility functions
10 #include <linux/nvmem-provider.h>
45 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) in __nvm_get_auth_status() argument
50 if (uuid_equal(&st->uuid, sw->uuid)) in __nvm_get_auth_status()
57 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) in nvm_get_auth_status() argument
62 st = __nvm_get_auth_status(sw); in nvm_get_auth_status()
65 *status = st ? st->status : 0; in nvm_get_auth_status()
68 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) in nvm_set_auth_status() argument
72 if (WARN_ON(!sw->uuid)) in nvm_set_auth_status()
76 st = __nvm_get_auth_status(sw); in nvm_set_auth_status()
83 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); in nvm_set_auth_status()
84 INIT_LIST_HEAD(&st->list); in nvm_set_auth_status()
85 list_add_tail(&st->list, &nvm_auth_status_cache); in nvm_set_auth_status()
88 st->status = status; in nvm_set_auth_status()
93 static void nvm_clear_auth_status(const struct tb_switch *sw) in nvm_clear_auth_status() argument
98 st = __nvm_get_auth_status(sw); in nvm_clear_auth_status()
100 list_del(&st->list); in nvm_clear_auth_status()
106 static int nvm_validate_and_write(struct tb_switch *sw) in nvm_validate_and_write() argument
109 const u8 *buf = sw->nvm->buf; in nvm_validate_and_write()
114 return -EINVAL; in nvm_validate_and_write()
116 image_size = sw->nvm->buf_data_size; in nvm_validate_and_write()
118 return -EINVAL; in nvm_validate_and_write()
126 return -EINVAL; in nvm_validate_and_write()
130 return -EINVAL; in nvm_validate_and_write()
138 return -EINVAL; in nvm_validate_and_write()
140 if (!sw->safe_mode) { in nvm_validate_and_write()
148 if (device_id != sw->config.device_id) in nvm_validate_and_write()
149 return -EINVAL; in nvm_validate_and_write()
151 if (sw->generation < 3) { in nvm_validate_and_write()
153 ret = dma_port_flash_write(sw->dma_port, in nvm_validate_and_write()
162 image_size -= hdr_size; in nvm_validate_and_write()
165 return dma_port_flash_write(sw->dma_port, 0, buf, image_size); in nvm_validate_and_write()
168 static int nvm_authenticate_host(struct tb_switch *sw) in nvm_authenticate_host() argument
174 * existing paths first (in case it is not in safe mode in nvm_authenticate_host()
177 if (!sw->safe_mode) { in nvm_authenticate_host()
180 ret = tb_domain_disconnect_all_paths(sw->tb); in nvm_authenticate_host()
187 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_host()
188 if (!ret || ret == -ETIMEDOUT) in nvm_authenticate_host()
195 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); in nvm_authenticate_host()
196 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) in nvm_authenticate_host()
197 nvm_set_auth_status(sw, status); in nvm_authenticate_host()
201 * From safe mode we can get out by just power cycling the in nvm_authenticate_host()
204 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_host()
208 static int nvm_authenticate_device(struct tb_switch *sw) in nvm_authenticate_device() argument
212 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_device()
215 case -ETIMEDOUT: in nvm_authenticate_device()
216 case -EACCES: in nvm_authenticate_device()
217 case -EINVAL: in nvm_authenticate_device()
233 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in nvm_authenticate_device()
234 if (ret < 0 && ret != -ETIMEDOUT) in nvm_authenticate_device()
238 tb_sw_warn(sw, "failed to authenticate NVM\n"); in nvm_authenticate_device()
239 nvm_set_auth_status(sw, status); in nvm_authenticate_device()
242 tb_sw_info(sw, "power cycling the switch now\n"); in nvm_authenticate_device()
243 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_device()
248 } while (--retries); in nvm_authenticate_device()
250 return -ETIMEDOUT; in nvm_authenticate_device()
256 struct tb_switch *sw = priv; in tb_switch_nvm_read() local
259 pm_runtime_get_sync(&sw->dev); in tb_switch_nvm_read()
260 ret = dma_port_flash_read(sw->dma_port, offset, val, bytes); in tb_switch_nvm_read()
261 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_nvm_read()
262 pm_runtime_put_autosuspend(&sw->dev); in tb_switch_nvm_read()
270 return -EPERM; in tb_switch_nvm_no_read()
276 struct tb_switch *sw = priv; in tb_switch_nvm_write() local
279 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_nvm_write()
288 if (!sw->nvm->buf) { in tb_switch_nvm_write()
289 sw->nvm->buf = vmalloc(NVM_MAX_SIZE); in tb_switch_nvm_write()
290 if (!sw->nvm->buf) { in tb_switch_nvm_write()
291 ret = -ENOMEM; in tb_switch_nvm_write()
296 sw->nvm->buf_data_size = offset + bytes; in tb_switch_nvm_write()
297 memcpy(sw->nvm->buf + offset, val, bytes); in tb_switch_nvm_write()
300 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_write()
305 static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id, in register_nvmem() argument
327 config.dev = &sw->dev; in register_nvmem()
329 config.priv = sw; in register_nvmem()
334 static int tb_switch_nvm_add(struct tb_switch *sw) in tb_switch_nvm_add() argument
341 if (!sw->dma_port) in tb_switch_nvm_add()
346 return -ENOMEM; in tb_switch_nvm_add()
348 nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL); in tb_switch_nvm_add()
351 * If the switch is in safe-mode the only accessible portion of in tb_switch_nvm_add()
352 * the NVM is the non-active one where userspace is expected to in tb_switch_nvm_add()
355 if (!sw->safe_mode) { in tb_switch_nvm_add()
358 ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val, in tb_switch_nvm_add()
363 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; in tb_switch_nvm_add()
365 nvm_size = (nvm_size - hdr_size) / 2; in tb_switch_nvm_add()
367 ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val, in tb_switch_nvm_add()
372 nvm->major = val >> 16; in tb_switch_nvm_add()
373 nvm->minor = val >> 8; in tb_switch_nvm_add()
375 nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true); in tb_switch_nvm_add()
380 nvm->active = nvm_dev; in tb_switch_nvm_add()
383 nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false); in tb_switch_nvm_add()
388 nvm->non_active = nvm_dev; in tb_switch_nvm_add()
390 sw->nvm = nvm; in tb_switch_nvm_add()
394 if (nvm->active) in tb_switch_nvm_add()
395 nvmem_unregister(nvm->active); in tb_switch_nvm_add()
397 ida_simple_remove(&nvm_ida, nvm->id); in tb_switch_nvm_add()
403 static void tb_switch_nvm_remove(struct tb_switch *sw) in tb_switch_nvm_remove() argument
407 nvm = sw->nvm; in tb_switch_nvm_remove()
408 sw->nvm = NULL; in tb_switch_nvm_remove()
414 if (!nvm->authenticating) in tb_switch_nvm_remove()
415 nvm_clear_auth_status(sw); in tb_switch_nvm_remove()
417 nvmem_unregister(nvm->non_active); in tb_switch_nvm_remove()
418 if (nvm->active) in tb_switch_nvm_remove()
419 nvmem_unregister(nvm->active); in tb_switch_nvm_remove()
420 ida_simple_remove(&nvm_ida, nvm->id); in tb_switch_nvm_remove()
421 vfree(nvm->buf); in tb_switch_nvm_remove()
429 switch (port->type >> 16) { in tb_port_type()
431 switch ((u8) port->type) { in tb_port_type()
460 port->port_number, port->vendor_id, port->device_id, in tb_dump_port()
461 port->revision, port->thunderbolt_version, tb_port_type(port), in tb_dump_port()
462 port->type); in tb_dump_port()
464 port->max_in_hop_id, port->max_out_hop_id); in tb_dump_port()
465 tb_info(tb, " Max counters: %d\n", port->max_counters); in tb_dump_port()
466 tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits); in tb_dump_port()
470 * tb_port_state() - get connectedness state of a port
480 if (port->cap_phy == 0) { in tb_port_state()
482 return -EINVAL; in tb_port_state()
484 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); in tb_port_state()
491 * tb_wait_for_port() - wait for a port to become ready
507 if (!port->cap_phy) { in tb_wait_for_port()
509 return -EINVAL; in tb_wait_for_port()
513 return -EINVAL; in tb_wait_for_port()
516 while (retries--) { in tb_wait_for_port()
542 * After plug-in the state is TB_PORT_CONNECTING. Give it some in tb_wait_for_port()
556 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
568 "adding %#x NFC credits (%#x -> %#x)", in tb_port_add_nfc_credits()
570 port->config.nfc_credits, in tb_port_add_nfc_credits()
571 port->config.nfc_credits + credits); in tb_port_add_nfc_credits()
572 port->config.nfc_credits += credits; in tb_port_add_nfc_credits()
573 return tb_port_write(port, &port->config.nfc_credits, in tb_port_add_nfc_credits()
578 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
590 * tb_init_port() - initialize a port
602 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); in tb_init_port()
607 if (port->config.type == TB_TYPE_PORT && port->port != 0) { in tb_init_port()
611 port->cap_phy = cap; in tb_init_port()
616 tb_dump_port(port->sw->tb, &port->config); in tb_init_port()
625 static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) in tb_dump_switch() argument
629 sw->vendor_id, sw->device_id, sw->revision, in tb_dump_switch()
630 sw->thunderbolt_version); in tb_dump_switch()
631 tb_info(tb, " Max Port Number: %d\n", sw->max_port_number); in tb_dump_switch()
635 sw->upstream_port_number, sw->depth, in tb_dump_switch()
636 (((u64) sw->route_hi) << 32) | sw->route_lo, in tb_dump_switch()
637 sw->enabled, sw->plug_events_delay); in tb_dump_switch()
640 sw->__unknown1, sw->__unknown4); in tb_dump_switch()
644 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
657 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route, in tb_switch_reset()
661 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT); in tb_switch_reset()
663 return -EIO; in tb_switch_reset()
668 * tb_plug_events_active() - enable/disable plug events on a switch
674 static int tb_plug_events_active(struct tb_switch *sw, bool active) in tb_plug_events_active() argument
679 if (!sw->config.enabled) in tb_plug_events_active()
682 sw->config.plug_events_delay = 0xff; in tb_plug_events_active()
683 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); in tb_plug_events_active()
687 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); in tb_plug_events_active()
693 switch (sw->config.device_id) { in tb_plug_events_active()
704 return tb_sw_write(sw, &data, TB_CFG_SWITCH, in tb_plug_events_active()
705 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
712 struct tb_switch *sw = tb_to_switch(dev); in authorized_show() local
714 return sprintf(buf, "%u\n", sw->authorized); in authorized_show()
717 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) in tb_switch_set_authorized() argument
719 int ret = -EINVAL; in tb_switch_set_authorized()
721 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_set_authorized()
724 if (sw->authorized) in tb_switch_set_authorized()
733 pm_runtime_get_sync(&sw->dev); in tb_switch_set_authorized()
738 if (sw->key) in tb_switch_set_authorized()
739 ret = tb_domain_approve_switch_key(sw->tb, sw); in tb_switch_set_authorized()
741 ret = tb_domain_approve_switch(sw->tb, sw); in tb_switch_set_authorized()
746 if (sw->key) in tb_switch_set_authorized()
747 ret = tb_domain_challenge_switch_key(sw->tb, sw); in tb_switch_set_authorized()
754 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_set_authorized()
755 pm_runtime_put_autosuspend(&sw->dev); in tb_switch_set_authorized()
759 sw->authorized = val; in tb_switch_set_authorized()
761 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_set_authorized()
765 mutex_unlock(&sw->tb->lock); in tb_switch_set_authorized()
773 struct tb_switch *sw = tb_to_switch(dev); in authorized_store() local
781 return -EINVAL; in authorized_store()
783 ret = tb_switch_set_authorized(sw, val); in authorized_store()
792 struct tb_switch *sw = tb_to_switch(dev); in boot_show() local
794 return sprintf(buf, "%u\n", sw->boot); in boot_show()
801 struct tb_switch *sw = tb_to_switch(dev); in device_show() local
803 return sprintf(buf, "%#x\n", sw->device); in device_show()
810 struct tb_switch *sw = tb_to_switch(dev); in device_name_show() local
812 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); in device_name_show()
819 struct tb_switch *sw = tb_to_switch(dev); in key_show() local
822 if (!mutex_trylock(&sw->tb->lock)) in key_show()
825 if (sw->key) in key_show()
826 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); in key_show()
830 mutex_unlock(&sw->tb->lock); in key_show()
837 struct tb_switch *sw = tb_to_switch(dev); in key_store() local
845 return -EINVAL; in key_store()
847 if (!mutex_trylock(&sw->tb->lock)) in key_store()
850 if (sw->authorized) { in key_store()
851 ret = -EBUSY; in key_store()
853 kfree(sw->key); in key_store()
855 sw->key = NULL; in key_store()
857 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); in key_store()
858 if (!sw->key) in key_store()
859 ret = -ENOMEM; in key_store()
863 mutex_unlock(&sw->tb->lock); in key_store()
868 static void nvm_authenticate_start(struct tb_switch *sw) in nvm_authenticate_start() argument
878 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev); in nvm_authenticate_start()
880 pm_runtime_get_noresume(&root_port->dev); in nvm_authenticate_start()
883 static void nvm_authenticate_complete(struct tb_switch *sw) in nvm_authenticate_complete() argument
887 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev); in nvm_authenticate_complete()
889 pm_runtime_put(&root_port->dev); in nvm_authenticate_complete()
895 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_show() local
898 nvm_get_auth_status(sw, &status); in nvm_authenticate_show()
905 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_store() local
909 if (!mutex_trylock(&sw->tb->lock)) in nvm_authenticate_store()
913 if (!sw->nvm) { in nvm_authenticate_store()
914 ret = -EAGAIN; in nvm_authenticate_store()
923 nvm_clear_auth_status(sw); in nvm_authenticate_store()
926 if (!sw->nvm->buf) { in nvm_authenticate_store()
927 ret = -EINVAL; in nvm_authenticate_store()
931 pm_runtime_get_sync(&sw->dev); in nvm_authenticate_store()
932 ret = nvm_validate_and_write(sw); in nvm_authenticate_store()
934 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_store()
935 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_store()
939 sw->nvm->authenticating = true; in nvm_authenticate_store()
941 if (!tb_route(sw)) { in nvm_authenticate_store()
946 nvm_authenticate_start(sw); in nvm_authenticate_store()
947 ret = nvm_authenticate_host(sw); in nvm_authenticate_store()
949 ret = nvm_authenticate_device(sw); in nvm_authenticate_store()
951 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_store()
952 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_store()
956 mutex_unlock(&sw->tb->lock); in nvm_authenticate_store()
967 struct tb_switch *sw = tb_to_switch(dev); in nvm_version_show() local
970 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
973 if (sw->safe_mode) in nvm_version_show()
974 ret = -ENODATA; in nvm_version_show()
975 else if (!sw->nvm) in nvm_version_show()
976 ret = -EAGAIN; in nvm_version_show()
978 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); in nvm_version_show()
980 mutex_unlock(&sw->tb->lock); in nvm_version_show()
989 struct tb_switch *sw = tb_to_switch(dev); in vendor_show() local
991 return sprintf(buf, "%#x\n", sw->vendor); in vendor_show()
998 struct tb_switch *sw = tb_to_switch(dev); in vendor_name_show() local
1000 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); in vendor_name_show()
1007 struct tb_switch *sw = tb_to_switch(dev); in unique_id_show() local
1009 return sprintf(buf, "%pUb\n", sw->uuid); in unique_id_show()
1031 struct tb_switch *sw = tb_to_switch(dev); in switch_attr_is_visible() local
1034 if (tb_route(sw) && in switch_attr_is_visible()
1035 sw->tb->security_level == TB_SECURITY_SECURE && in switch_attr_is_visible()
1036 sw->security_level == TB_SECURITY_SECURE) in switch_attr_is_visible()
1037 return attr->mode; in switch_attr_is_visible()
1041 if (sw->dma_port) in switch_attr_is_visible()
1042 return attr->mode; in switch_attr_is_visible()
1045 if (tb_route(sw)) in switch_attr_is_visible()
1046 return attr->mode; in switch_attr_is_visible()
1050 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
1065 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_release() local
1067 dma_port_free(sw->dma_port); in tb_switch_release()
1069 kfree(sw->uuid); in tb_switch_release()
1070 kfree(sw->device_name); in tb_switch_release()
1071 kfree(sw->vendor_name); in tb_switch_release()
1072 kfree(sw->ports); in tb_switch_release()
1073 kfree(sw->drom); in tb_switch_release()
1074 kfree(sw->key); in tb_switch_release()
1075 kfree(sw); in tb_switch_release()
1103 static int tb_switch_get_generation(struct tb_switch *sw) in tb_switch_get_generation() argument
1105 switch (sw->config.device_id) { in tb_switch_get_generation()
1136 tb_sw_warn(sw, "unsupported switch device id %#x\n", in tb_switch_get_generation()
1137 sw->config.device_id); in tb_switch_get_generation()
1143 * tb_switch_alloc() - allocate a switch
1160 struct tb_switch *sw; in tb_switch_alloc() local
1161 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); in tb_switch_alloc()
1165 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc()
1166 if (!sw) in tb_switch_alloc()
1169 sw->tb = tb; in tb_switch_alloc()
1170 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5)) in tb_switch_alloc()
1174 tb_dump_switch(tb, &sw->config); in tb_switch_alloc()
1177 sw->config.upstream_port_number = upstream_port; in tb_switch_alloc()
1178 sw->config.depth = tb_route_length(route); in tb_switch_alloc()
1179 sw->config.route_lo = route; in tb_switch_alloc()
1180 sw->config.route_hi = route >> 32; in tb_switch_alloc()
1181 sw->config.enabled = 0; in tb_switch_alloc()
1184 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), in tb_switch_alloc()
1186 if (!sw->ports) in tb_switch_alloc()
1189 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_alloc()
1191 sw->ports[i].sw = sw; in tb_switch_alloc()
1192 sw->ports[i].port = i; in tb_switch_alloc()
1195 sw->generation = tb_switch_get_generation(sw); in tb_switch_alloc()
1197 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); in tb_switch_alloc()
1199 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); in tb_switch_alloc()
1202 sw->cap_plug_events = cap; in tb_switch_alloc()
1206 sw->authorized = true; in tb_switch_alloc()
1208 device_initialize(&sw->dev); in tb_switch_alloc()
1209 sw->dev.parent = parent; in tb_switch_alloc()
1210 sw->dev.bus = &tb_bus_type; in tb_switch_alloc()
1211 sw->dev.type = &tb_switch_type; in tb_switch_alloc()
1212 sw->dev.groups = switch_groups; in tb_switch_alloc()
1213 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc()
1215 return sw; in tb_switch_alloc()
1218 kfree(sw->ports); in tb_switch_alloc()
1219 kfree(sw); in tb_switch_alloc()
1225 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1230 * This creates a switch in safe mode. This means the switch pretty much
1241 struct tb_switch *sw; in tb_switch_alloc_safe_mode() local
1243 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc_safe_mode()
1244 if (!sw) in tb_switch_alloc_safe_mode()
1247 sw->tb = tb; in tb_switch_alloc_safe_mode()
1248 sw->config.depth = tb_route_length(route); in tb_switch_alloc_safe_mode()
1249 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc_safe_mode()
1250 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc_safe_mode()
1251 sw->safe_mode = true; in tb_switch_alloc_safe_mode()
1253 device_initialize(&sw->dev); in tb_switch_alloc_safe_mode()
1254 sw->dev.parent = parent; in tb_switch_alloc_safe_mode()
1255 sw->dev.bus = &tb_bus_type; in tb_switch_alloc_safe_mode()
1256 sw->dev.type = &tb_switch_type; in tb_switch_alloc_safe_mode()
1257 sw->dev.groups = switch_groups; in tb_switch_alloc_safe_mode()
1258 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc_safe_mode()
1260 return sw; in tb_switch_alloc_safe_mode()
1264 * tb_switch_configure() - Uploads configuration to the switch
1265 * @sw: Switch to configure
1273 int tb_switch_configure(struct tb_switch *sw) in tb_switch_configure() argument
1275 struct tb *tb = sw->tb; in tb_switch_configure()
1279 route = tb_route(sw); in tb_switch_configure()
1282 route, tb_route_length(route), sw->config.upstream_port_number); in tb_switch_configure()
1284 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
1285 tb_sw_warn(sw, "unknown switch vendor id %#x\n", in tb_switch_configure()
1286 sw->config.vendor_id); in tb_switch_configure()
1288 sw->config.enabled = 1; in tb_switch_configure()
1291 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3); in tb_switch_configure()
1295 return tb_plug_events_active(sw, true); in tb_switch_configure()
1298 static int tb_switch_set_uuid(struct tb_switch *sw) in tb_switch_set_uuid() argument
1304 if (sw->uuid) in tb_switch_set_uuid()
1311 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); in tb_switch_set_uuid()
1313 ret = tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4); in tb_switch_set_uuid()
1323 uuid[0] = sw->uid & 0xffffffff; in tb_switch_set_uuid()
1324 uuid[1] = (sw->uid >> 32) & 0xffffffff; in tb_switch_set_uuid()
1329 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); in tb_switch_set_uuid()
1330 if (!sw->uuid) in tb_switch_set_uuid()
1331 ret = -ENOMEM; in tb_switch_set_uuid()
1335 static int tb_switch_add_dma_port(struct tb_switch *sw) in tb_switch_add_dma_port() argument
1340 switch (sw->generation) { in tb_switch_add_dma_port()
1343 if (tb_route(sw)) in tb_switch_add_dma_port()
1348 ret = tb_switch_set_uuid(sw); in tb_switch_add_dma_port()
1356 * is in safe mode. in tb_switch_add_dma_port()
1358 if (!sw->safe_mode) in tb_switch_add_dma_port()
1363 if (sw->no_nvm_upgrade) in tb_switch_add_dma_port()
1366 sw->dma_port = dma_port_alloc(sw); in tb_switch_add_dma_port()
1367 if (!sw->dma_port) in tb_switch_add_dma_port()
1376 nvm_get_auth_status(sw, &status); in tb_switch_add_dma_port()
1378 if (!tb_route(sw)) in tb_switch_add_dma_port()
1379 nvm_authenticate_complete(sw); in tb_switch_add_dma_port()
1388 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
1393 if (!tb_route(sw)) in tb_switch_add_dma_port()
1394 nvm_authenticate_complete(sw); in tb_switch_add_dma_port()
1397 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
1398 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
1401 tb_sw_info(sw, "power cycling the switch now\n"); in tb_switch_add_dma_port()
1402 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
1408 return -ESHUTDOWN; in tb_switch_add_dma_port()
1412 * tb_switch_add() - Add a switch to the domain
1413 * @sw: Switch to add
1423 int tb_switch_add(struct tb_switch *sw) in tb_switch_add() argument
1434 ret = tb_switch_add_dma_port(sw); in tb_switch_add()
1438 if (!sw->safe_mode) { in tb_switch_add()
1440 ret = tb_drom_read(sw); in tb_switch_add()
1442 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n"); in tb_switch_add()
1445 tb_sw_info(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
1447 ret = tb_switch_set_uuid(sw); in tb_switch_add()
1451 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
1452 if (sw->ports[i].disabled) { in tb_switch_add()
1453 tb_port_info(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
1456 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
1462 ret = device_add(&sw->dev); in tb_switch_add()
1466 ret = tb_switch_nvm_add(sw); in tb_switch_add()
1468 device_del(&sw->dev); in tb_switch_add()
1472 pm_runtime_set_active(&sw->dev); in tb_switch_add()
1473 if (sw->rpm) { in tb_switch_add()
1474 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
1475 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
1476 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
1477 pm_runtime_enable(&sw->dev); in tb_switch_add()
1478 pm_request_autosuspend(&sw->dev); in tb_switch_add()
1485 * tb_switch_remove() - Remove and release a switch
1486 * @sw: Switch to remove
1492 void tb_switch_remove(struct tb_switch *sw) in tb_switch_remove() argument
1496 if (sw->rpm) { in tb_switch_remove()
1497 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
1498 pm_runtime_disable(&sw->dev); in tb_switch_remove()
1502 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_remove()
1503 if (tb_is_upstream_port(&sw->ports[i])) in tb_switch_remove()
1505 if (sw->ports[i].remote) in tb_switch_remove()
1506 tb_switch_remove(sw->ports[i].remote->sw); in tb_switch_remove()
1507 sw->ports[i].remote = NULL; in tb_switch_remove()
1508 if (sw->ports[i].xdomain) in tb_switch_remove()
1509 tb_xdomain_remove(sw->ports[i].xdomain); in tb_switch_remove()
1510 sw->ports[i].xdomain = NULL; in tb_switch_remove()
1513 if (!sw->is_unplugged) in tb_switch_remove()
1514 tb_plug_events_active(sw, false); in tb_switch_remove()
1516 tb_switch_nvm_remove(sw); in tb_switch_remove()
1517 device_unregister(&sw->dev); in tb_switch_remove()
1521 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1523 void tb_sw_set_unplugged(struct tb_switch *sw) in tb_sw_set_unplugged() argument
1526 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
1527 tb_sw_WARN(sw, "cannot unplug root switch\n"); in tb_sw_set_unplugged()
1530 if (sw->is_unplugged) { in tb_sw_set_unplugged()
1531 tb_sw_WARN(sw, "is_unplugged already set\n"); in tb_sw_set_unplugged()
1534 sw->is_unplugged = true; in tb_sw_set_unplugged()
1535 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_sw_set_unplugged()
1536 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) in tb_sw_set_unplugged()
1537 tb_sw_set_unplugged(sw->ports[i].remote->sw); in tb_sw_set_unplugged()
1541 int tb_switch_resume(struct tb_switch *sw) in tb_switch_resume() argument
1544 tb_sw_info(sw, "resuming switch\n"); in tb_switch_resume()
1550 if (tb_route(sw)) { in tb_switch_resume()
1553 err = tb_drom_read_uid_only(sw, &uid); in tb_switch_resume()
1555 tb_sw_warn(sw, "uid read failed\n"); in tb_switch_resume()
1558 if (sw->uid != uid) { in tb_switch_resume()
1559 tb_sw_info(sw, in tb_switch_resume()
1560 "changed while suspended (uid %#llx -> %#llx)\n", in tb_switch_resume()
1561 sw->uid, uid); in tb_switch_resume()
1562 return -ENODEV; in tb_switch_resume()
1567 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3); in tb_switch_resume()
1571 err = tb_plug_events_active(sw, true); in tb_switch_resume()
1576 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_resume()
1577 struct tb_port *port = &sw->ports[i]; in tb_switch_resume()
1580 if (!port->remote) in tb_switch_resume()
1583 || tb_switch_resume(port->remote->sw)) { in tb_switch_resume()
1586 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
1592 void tb_switch_suspend(struct tb_switch *sw) in tb_switch_suspend() argument
1595 err = tb_plug_events_active(sw, false); in tb_switch_suspend()
1599 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_suspend()
1600 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) in tb_switch_suspend()
1601 tb_switch_suspend(sw->ports[i].remote->sw); in tb_switch_suspend()
1619 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_match() local
1622 if (!sw) in tb_switch_match()
1624 if (sw->tb != lookup->tb) in tb_switch_match()
1627 if (lookup->uuid) in tb_switch_match()
1628 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
1630 if (lookup->route) { in tb_switch_match()
1631 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
1632 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
1636 if (!lookup->depth) in tb_switch_match()
1637 return !sw->depth; in tb_switch_match()
1639 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
1643 * tb_switch_find_by_link_depth() - Find switch by link and depth
1669 * tb_switch_find_by_uuid() - Find switch by UUID
1693 * tb_switch_find_by_route() - Find switch by route string
1706 return tb_switch_get(tb->root_switch); in tb_switch_find_by_route()