xref: /spdk/module/bdev/ocf/data.c (revision 94a84ae98590bea46939eb1dcd7a9876bd393b54)
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 <ocf/ocf.h>
35 #include "spdk/bdev.h"
36 #include "data.h"
37 
38 struct bdev_ocf_data *
39 vbdev_ocf_data_alloc(uint32_t iovcnt)
40 {
41 	struct bdev_ocf_data *data;
42 
43 	data = env_malloc(sizeof(*data), ENV_MEM_NOIO);
44 	if (!data) {
45 		return NULL;
46 	}
47 
48 	data->seek = 0;
49 
50 	if (iovcnt) {
51 		data->iovs = env_malloc(sizeof(*data->iovs) * iovcnt, ENV_MEM_NOIO);
52 		if (!data->iovs) {
53 			env_free(data);
54 			return NULL;
55 		}
56 	}
57 
58 	data->iovcnt = 0;
59 	data->iovalloc = iovcnt;
60 
61 	return data;
62 }
63 
64 void
65 vbdev_ocf_data_free(struct bdev_ocf_data *data)
66 {
67 	if (!data) {
68 		return;
69 	}
70 
71 	if (data->iovalloc != 0) {
72 		env_free(data->iovs);
73 	}
74 
75 	env_free(data);
76 }
77 
78 void
79 vbdev_ocf_iovs_add(struct bdev_ocf_data *data, void *base, size_t len)
80 {
81 	assert(NULL != data);
82 	assert(data->iovalloc != -1);
83 
84 	if (data->iovcnt == data->iovalloc) {
85 		/* TODO: Realloc iovs */
86 		SPDK_ERRLOG("IOV error\n");
87 	}
88 
89 	data->iovs[data->iovcnt].iov_base = base;
90 	data->iovs[data->iovcnt].iov_len = len;
91 	data->iovcnt++;
92 }
93 
94 struct bdev_ocf_data *
95 vbdev_ocf_data_from_spdk_io(struct spdk_bdev_io *bdev_io)
96 {
97 	struct bdev_ocf_data *data;
98 
99 	if (bdev_io == NULL) {
100 		return NULL;
101 	}
102 
103 	switch (bdev_io->type) {
104 	case SPDK_BDEV_IO_TYPE_WRITE:
105 	case SPDK_BDEV_IO_TYPE_READ:
106 		assert(bdev_io->u.bdev.iovs);
107 		break;
108 	case SPDK_BDEV_IO_TYPE_FLUSH:
109 	case SPDK_BDEV_IO_TYPE_UNMAP:
110 		break;
111 	default:
112 		SPDK_ERRLOG("Unsupported IO type %d\n", bdev_io->type);
113 		return NULL;
114 	}
115 
116 	data = (struct bdev_ocf_data *)bdev_io->driver_ctx;
117 	data->iovs = bdev_io->u.bdev.iovs;
118 	data->iovcnt = bdev_io->u.bdev.iovcnt;
119 	data->size = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
120 
121 	return data;
122 }
123