Line data Source code
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 0 : ftl_base_device_type_get_desc(const char *name) 18 : { 19 : struct ftl_base_device_type *entry; 20 : 21 0 : TAILQ_FOREACH(entry, &g_devs, base_devs_entry) { 22 0 : if (0 == strcmp(entry->name, name)) { 23 0 : return entry; 24 : } 25 : } 26 : 27 0 : return NULL; 28 : } 29 : 30 : static bool 31 0 : ftl_base_device_valid(const struct ftl_base_device_type *type) 32 : { 33 0 : return type && type->name && strlen(type->name); 34 : } 35 : 36 : void 37 0 : ftl_base_device_register(struct ftl_base_device_type *type) 38 : { 39 0 : if (!ftl_base_device_valid(type)) { 40 0 : SPDK_ERRLOG("[FTL] Base device type is invalid\n"); 41 0 : ftl_abort(); 42 : } 43 : 44 0 : pthread_mutex_lock(&g_devs_mutex); 45 0 : if (!ftl_base_device_type_get_desc(type->name)) { 46 0 : TAILQ_INSERT_TAIL(&g_devs, type, base_devs_entry); 47 : 48 0 : SPDK_NOTICELOG("[FTL] Registered base device, name: %s\n", type->name); 49 : } else { 50 0 : SPDK_ERRLOG("[FTL] Cannot register base device, already exist, name: %s\n", type->name); 51 0 : ftl_abort(); 52 : } 53 : 54 0 : pthread_mutex_unlock(&g_devs_mutex); 55 0 : } 56 : 57 : const struct ftl_base_device_type * 58 0 : 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 0 : pthread_mutex_lock(&g_devs_mutex); 63 : 64 0 : TAILQ_FOREACH(type, &g_devs, base_devs_entry) { 65 0 : if (type->ops.is_bdev_compatible) { 66 0 : if (type->ops.is_bdev_compatible(dev, bdev)) { 67 0 : break; 68 : } 69 : } 70 : } 71 : 72 0 : pthread_mutex_unlock(&g_devs_mutex); 73 : 74 0 : return type; 75 : }