1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "utils.h" 37 #include "vbdev_ocf.h" 38 39 static char *cache_modes[ocf_cache_mode_max] = { 40 [ocf_cache_mode_wt] = "wt", 41 [ocf_cache_mode_wb] = "wb", 42 [ocf_cache_mode_wa] = "wa", 43 [ocf_cache_mode_pt] = "pt", 44 [ocf_cache_mode_wi] = "wi", 45 [ocf_cache_mode_wo] = "wo", 46 }; 47 48 ocf_cache_mode_t 49 ocf_get_cache_mode(const char *cache_mode) 50 { 51 int i; 52 53 for (i = 0; i < ocf_cache_mode_max; i++) { 54 if (strcmp(cache_mode, cache_modes[i]) == 0) { 55 return i; 56 } 57 } 58 59 return ocf_cache_mode_none; 60 } 61 62 const char * 63 ocf_get_cache_modename(ocf_cache_mode_t mode) 64 { 65 if (mode > ocf_cache_mode_none && mode < ocf_cache_mode_max) { 66 return cache_modes[mode]; 67 } else { 68 return NULL; 69 } 70 } 71 72 int 73 ocf_get_cache_line_size(ocf_cache_t cache) 74 { 75 return ocf_cache_get_line_size(cache) / KiB; 76 } 77 78 int 79 vbdev_ocf_mngt_start(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *path, 80 vbdev_ocf_mngt_callback cb, void *cb_arg) 81 { 82 if (vbdev->mngt_ctx.current_step) { 83 return -EBUSY; 84 } 85 86 memset(&vbdev->mngt_ctx, 0, sizeof(vbdev->mngt_ctx)); 87 88 vbdev->mngt_ctx.current_step = path; 89 vbdev->mngt_ctx.cb = cb; 90 vbdev->mngt_ctx.cb_arg = cb_arg; 91 92 (*vbdev->mngt_ctx.current_step)(vbdev); 93 94 return 0; 95 } 96 97 void 98 vbdev_ocf_mngt_stop(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *rollback_path, int status) 99 { 100 if (status) { 101 vbdev->mngt_ctx.status = status; 102 } 103 104 if (vbdev->mngt_ctx.status && rollback_path) { 105 vbdev->mngt_ctx.poller_fn = NULL; 106 vbdev->mngt_ctx.current_step = rollback_path; 107 (*vbdev->mngt_ctx.current_step)(vbdev); 108 return; 109 } 110 111 if (vbdev->mngt_ctx.cb) { 112 vbdev->mngt_ctx.cb(vbdev->mngt_ctx.status, vbdev, vbdev->mngt_ctx.cb_arg); 113 } 114 115 memset(&vbdev->mngt_ctx, 0, sizeof(vbdev->mngt_ctx)); 116 } 117 118 void 119 vbdev_ocf_mngt_continue(struct vbdev_ocf *vbdev, int status) 120 { 121 if (vbdev->mngt_ctx.current_step == NULL) { 122 return; 123 } 124 125 assert((*vbdev->mngt_ctx.current_step) != NULL); 126 127 vbdev->mngt_ctx.status = status; 128 129 vbdev->mngt_ctx.current_step++; 130 if (*vbdev->mngt_ctx.current_step) { 131 (*vbdev->mngt_ctx.current_step)(vbdev); 132 return; 133 } 134 135 vbdev_ocf_mngt_stop(vbdev, NULL, 0); 136 } 137 138 int 139 vbdev_ocf_mngt_get_status(struct vbdev_ocf *vbdev) 140 { 141 return vbdev->mngt_ctx.status; 142 } 143