1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2023 Solidigm All Rights Reserved 3 */ 4 5 #include "spdk/stdinc.h" 6 #include "spdk/queue.h" 7 #include "spdk/log.h" 8 9 #include "ftl_core.h" 10 #include "ftl_base_dev.h" 11 #include "utils/ftl_defs.h" 12 13 static TAILQ_HEAD(, ftl_base_device_type) g_devs = TAILQ_HEAD_INITIALIZER(g_devs); 14 static pthread_mutex_t g_devs_mutex = PTHREAD_MUTEX_INITIALIZER; 15 16 static const struct ftl_base_device_type * 17 ftl_base_device_type_get_desc(const char *name) 18 { 19 struct ftl_base_device_type *entry; 20 21 TAILQ_FOREACH(entry, &g_devs, base_devs_entry) { 22 if (0 == strcmp(entry->name, name)) { 23 return entry; 24 } 25 } 26 27 return NULL; 28 } 29 30 static bool 31 ftl_base_device_valid(const struct ftl_base_device_type *type) 32 { 33 return type && type->name && strlen(type->name); 34 } 35 36 void 37 ftl_base_device_register(struct ftl_base_device_type *type) 38 { 39 if (!ftl_base_device_valid(type)) { 40 SPDK_ERRLOG("[FTL] Base device type is invalid\n"); 41 ftl_abort(); 42 } 43 44 pthread_mutex_lock(&g_devs_mutex); 45 if (!ftl_base_device_type_get_desc(type->name)) { 46 TAILQ_INSERT_TAIL(&g_devs, type, base_devs_entry); 47 48 SPDK_NOTICELOG("[FTL] Registered base device, name: %s\n", type->name); 49 } else { 50 SPDK_ERRLOG("[FTL] Cannot register base device, already exist, name: %s\n", type->name); 51 ftl_abort(); 52 } 53 54 pthread_mutex_unlock(&g_devs_mutex); 55 } 56 57 const struct ftl_base_device_type * 58 ftl_base_device_get_type_by_bdev(struct spdk_ftl_dev *dev, struct spdk_bdev *bdev) 59 { 60 struct ftl_base_device_type *type; 61 62 pthread_mutex_lock(&g_devs_mutex); 63 64 TAILQ_FOREACH(type, &g_devs, base_devs_entry) { 65 if (type->ops.is_bdev_compatible) { 66 if (type->ops.is_bdev_compatible(dev, bdev)) { 67 break; 68 } 69 } 70 } 71 72 pthread_mutex_unlock(&g_devs_mutex); 73 74 return type; 75 } 76