xref: /spdk/lib/scsi/scsi_rpc.c (revision edbca2a67610cf6ebb9755c839e307d1d18375da)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "scsi_internal.h"
36 
37 #include "spdk/rpc.h"
38 #include "spdk/util.h"
39 
40 static void
41 spdk_rpc_get_luns(struct spdk_jsonrpc_server_conn *conn,
42 		  const struct spdk_json_val *params,
43 		  const struct spdk_json_val *id)
44 {
45 	struct spdk_json_write_ctx *w;
46 	struct spdk_lun_db_entry *current;
47 
48 	if (params != NULL) {
49 		spdk_jsonrpc_send_error_response(conn, id, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
50 						 "get_luns requires no parameters");
51 		return;
52 	}
53 
54 	if (id == NULL) {
55 		return;
56 	}
57 
58 	w = spdk_jsonrpc_begin_result(conn, id);
59 	spdk_json_write_array_begin(w);
60 
61 	current = spdk_scsi_lun_list_head;
62 	while (current != NULL) {
63 		struct spdk_scsi_lun *lun = current->lun;
64 
65 		spdk_json_write_object_begin(w);
66 		spdk_json_write_name(w, "claimed");
67 		spdk_json_write_bool(w, lun->claimed);
68 		spdk_json_write_name(w, "name");
69 		spdk_json_write_string(w, lun->name);
70 		spdk_json_write_object_end(w);
71 
72 		current = current->next;
73 	}
74 
75 	spdk_json_write_array_end(w);
76 
77 	spdk_jsonrpc_end_result(conn, w);
78 }
79 SPDK_RPC_REGISTER("get_luns", spdk_rpc_get_luns)
80 
81 static void
82 spdk_rpc_get_scsi_devices(struct spdk_jsonrpc_server_conn *conn,
83 			  const struct spdk_json_val *params,
84 			  const struct spdk_json_val *id)
85 {
86 	struct spdk_json_write_ctx *w;
87 	struct spdk_scsi_dev *devs = spdk_scsi_dev_get_list();
88 	int i;
89 
90 	if (params != NULL) {
91 		spdk_jsonrpc_send_error_response(conn, id, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
92 						 "get_scsi_devices requires no parameters");
93 		return;
94 	}
95 
96 	if (id == NULL) {
97 		return;
98 	}
99 
100 	w = spdk_jsonrpc_begin_result(conn, id);
101 	spdk_json_write_array_begin(w);
102 
103 	for (i = 0; i < SPDK_SCSI_MAX_DEVS; i++) {
104 		struct spdk_scsi_dev *dev = &devs[i];
105 
106 		if (!dev->is_allocated) {
107 			continue;
108 		}
109 
110 		spdk_json_write_object_begin(w);
111 
112 		spdk_json_write_name(w, "id");
113 		spdk_json_write_int32(w, dev->id);
114 
115 		spdk_json_write_name(w, "device_name");
116 		spdk_json_write_string(w, dev->name);
117 
118 		spdk_json_write_object_end(w);
119 	}
120 	spdk_json_write_array_end(w);
121 
122 	spdk_jsonrpc_end_result(conn, w);
123 }
124 SPDK_RPC_REGISTER("get_scsi_devices", spdk_rpc_get_scsi_devices)
125