Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2018 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk_internal/cunit.h"
7 : : #include "common/lib/test_env.c"
8 : :
9 : : #include "nvme/nvme_ctrlr_ocssd_cmd.c"
10 : :
11 [ - + ]: 5 : DEFINE_STUB(spdk_nvme_ctrlr_get_first_active_ns, uint32_t,
12 : : (struct spdk_nvme_ctrlr *ctrlr), 1);
13 : :
14 : : #define DECLARE_AND_CONSTRUCT_CTRLR() \
15 : : struct spdk_nvme_ctrlr ctrlr = {}; \
16 : : struct spdk_nvme_qpair adminq = {}; \
17 : : struct nvme_request req; \
18 : : \
19 : : STAILQ_INIT(&adminq.free_req); \
20 : : STAILQ_INSERT_HEAD(&adminq.free_req, &req, stailq); \
21 : : ctrlr.adminq = &adminq; \
22 : : CU_ASSERT(pthread_mutex_init(&ctrlr.ctrlr_lock, NULL) == 0);
23 : :
24 : : #define DECONSTRUCT_CTRLR() \
25 : : CU_ASSERT(pthread_mutex_destroy(&ctrlr.ctrlr_lock) == 0);
26 : :
27 : : pid_t g_spdk_nvme_pid;
28 : : struct nvme_request g_req;
29 : : typedef void (*verify_request_fn_t)(struct nvme_request *req);
30 : : verify_request_fn_t verify_fn;
31 : :
32 : : static const uint32_t expected_geometry_ns = 1;
33 : :
34 : : static int
35 : 5 : nvme_ns_cmp(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
36 : : {
37 : 5 : return ns1->id - ns2->id;
38 : : }
39 : :
40 [ - + - + : 15 : RB_GENERATE_STATIC(nvme_ns_tree, spdk_nvme_ns, node, nvme_ns_cmp);
+ + + - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + #
# # # # #
# # # # #
# # # ]
41 : :
42 : : static struct spdk_nvme_ns g_inactive_ns = {};
43 : :
44 : : struct spdk_nvme_ns *
45 : 5 : spdk_nvme_ctrlr_get_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid)
46 : : {
47 : 4 : struct spdk_nvme_ns tmp;
48 : : struct spdk_nvme_ns *ns;
49 : :
50 [ + - - + ]: 5 : if (nsid < 1 || nsid > ctrlr->cdata.nn) {
51 : 0 : return NULL;
52 : : }
53 : :
54 : 5 : tmp.id = nsid;
55 : 5 : ns = RB_FIND(nvme_ns_tree, &ctrlr->ns, &tmp);
56 : :
57 [ - + ]: 5 : if (ns == NULL) {
58 : 0 : return &g_inactive_ns;
59 : : }
60 : :
61 : 5 : return ns;
62 : : }
63 : :
64 : : int
65 : 5 : nvme_ctrlr_submit_admin_request(struct spdk_nvme_ctrlr *ctrlr, struct nvme_request *req)
66 : : {
67 : 5 : verify_fn(req);
68 [ - + ]: 5 : memset(req, 0, sizeof(*req));
69 : 5 : return 0;
70 : : }
71 : :
72 : : struct nvme_request *
73 : 5 : nvme_allocate_request_user_copy(struct spdk_nvme_qpair *qpair, void *buffer, uint32_t payload_size,
74 : : spdk_nvme_cmd_cb cb_fn, void *cb_arg, bool host_to_controller)
75 : : {
76 : : /* For the unit test, we don't actually need to copy the buffer */
77 : 5 : return nvme_allocate_request_contig(qpair, buffer, payload_size, cb_fn, cb_arg);
78 : : }
79 : :
80 : : static void
81 : 5 : verify_geometry_cmd(struct nvme_request *req)
82 : : {
83 : 5 : CU_ASSERT(req->cmd.opc == SPDK_OCSSD_OPC_GEOMETRY);
84 : 5 : CU_ASSERT(req->cmd.nsid == expected_geometry_ns);
85 : 5 : }
86 : :
87 : : static void
88 : 5 : test_geometry_cmd(void)
89 : : {
90 [ + - - + ]: 5 : DECLARE_AND_CONSTRUCT_CTRLR();
91 : :
92 : 4 : struct spdk_ocssd_geometry_data geo;
93 : :
94 : 5 : verify_fn = verify_geometry_cmd;
95 : :
96 : 5 : spdk_nvme_ocssd_ctrlr_cmd_geometry(&ctrlr, expected_geometry_ns, &geo,
97 : : sizeof(geo), NULL, NULL);
98 : :
99 [ - + ]: 5 : DECONSTRUCT_CTRLR();
100 : 5 : }
101 : :
102 : : static void
103 : 5 : test_spdk_nvme_ctrlr_is_ocssd_supported(void)
104 : : {
105 : 5 : struct spdk_nvme_ctrlr ctrlr = {};
106 : 5 : struct spdk_nvme_ns ns = {};
107 : : bool rc;
108 : :
109 : 5 : RB_INIT(&ctrlr.ns);
110 : 5 : ns.id = 1;
111 : 5 : RB_INSERT(nvme_ns_tree, &ctrlr.ns, &ns);
112 : :
113 : 5 : ns.nsdata.vendor_specific[0] = 1;
114 : 5 : ctrlr.quirks |= NVME_QUIRK_OCSSD;
115 : 5 : ctrlr.cdata.vid = SPDK_PCI_VID_CNEXLABS;
116 : 5 : ctrlr.cdata.nn = 1;
117 : :
118 : 5 : rc = spdk_nvme_ctrlr_is_ocssd_supported(&ctrlr);
119 : 5 : CU_ASSERT(rc == true);
120 : :
121 : : /* Clear quirks`s ocssd flag. */
122 : 5 : ctrlr.quirks = 0;
123 : :
124 : 5 : rc = spdk_nvme_ctrlr_is_ocssd_supported(&ctrlr);
125 : 5 : CU_ASSERT(rc == false);
126 : :
127 : : /* NS count is 0. */
128 : 5 : ctrlr.cdata.nn = 0;
129 : :
130 : 5 : rc = spdk_nvme_ctrlr_is_ocssd_supported(&ctrlr);
131 : 5 : CU_ASSERT(rc == false);
132 : 5 : }
133 : :
134 : : int
135 : 5 : main(int argc, char **argv)
136 : : {
137 : 5 : CU_pSuite suite = NULL;
138 : : unsigned int num_failures;
139 : :
140 : 5 : CU_initialize_registry();
141 : :
142 : 5 : suite = CU_add_suite("nvme_ctrlr_cmd", NULL, NULL);
143 : :
144 : 5 : CU_ADD_TEST(suite, test_geometry_cmd);
145 : 5 : CU_ADD_TEST(suite, test_spdk_nvme_ctrlr_is_ocssd_supported);
146 : :
147 : 5 : num_failures = spdk_ut_run_tests(argc, argv, NULL);
148 : 5 : CU_cleanup_registry();
149 : 5 : return num_failures;
150 : : }
|