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