1/*	$NetBSD: iscsic_globals.h,v 1.3 2011/10/30 18:40:06 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32
33#ifndef _ISCSIC_GLOBALS_H
34#define _ISCSIC_GLOBALS_H
35
36#include <sys/types.h>
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <sys/scsiio.h>
43
44/* iSCSI daemon ioctl interface */
45#include <iscsid.h>
46
47#include <iscsi_ioctl.h>
48
49/* -------------------------  Global Constants  ----------------------------- */
50
51/* Version information */
52
53#define VERSION_MAJOR		3
54#define VERSION_MINOR		1
55#define VERSION_STRING		"NetBSD iSCSI Software Initiator CLI 20110407 "
56
57
58#define TRUE   1
59#define FALSE  0
60
61#define BUF_SIZE     8192
62
63/* ---------------------------  Global Types  ------------------------------- */
64
65typedef int (*cmdproc_t) (int, char **);
66
67typedef struct {
68	const char	*cmd;
69	cmdproc_t	 proc;
70} command_t;
71
72
73/* -------------------------  Global Variables  ----------------------------- */
74
75int driver;						/* handle to driver (for ioctls) */
76uint8_t buf[BUF_SIZE];			/* buffer for daemon comm and driver I/O */
77
78/* -------------------------  Global Functions  ----------------------------- */
79
80#define min(a,b) ((a < b) ? a : b)
81
82/* Debugging stuff */
83
84#ifdef ISCSI_DEBUG
85
86int debug_level;				/* How much info to display */
87
88#define DEBOUT(x) printf x
89#define DEB(lev,x) {if (debug_level >= lev) printf x ;}
90
91#define STATIC static
92
93#else
94
95#define DEBOUT(x)
96#define DEB(lev,x)
97
98#define STATIC static
99
100#endif
101
102/*
103 * Convert uint64 to 6-byte string in network byte order (for ISID field)
104*/
105static __inline void
106hton6(uint8_t * d, uint64_t x)
107{
108#if BYTE_ORDER == LITTLE_ENDIAN
109	uint8_t *s = ((uint8_t *)(void *)&x) + 5;
110	*d++ = *s--;
111	*d++ = *s--;
112	*d++ = *s--;
113	*d++ = *s--;
114	*d++ = *s--;
115	*d = *s;
116#else
117	memcpy(d, &((uint8_t *)&x)[2], 6);
118#endif
119}
120
121/*
122 * Convert uint64 from network byte order (for LUN field)
123*/
124static __inline uint64_t
125ntohq(uint64_t x)
126{
127#if BYTE_ORDER == LITTLE_ENDIAN
128	uint8_t *s = (uint8_t *)(void *)&x;
129
130	return (uint64_t) ((uint64_t) s[0] << 56 | (uint64_t) s[1] << 48 |
131			(uint64_t) s[2] << 40 | (uint64_t) s[3] << 32 |
132			(uint64_t) s[4] << 24 | (uint64_t) s[5] << 16 |
133			(uint64_t) s[6] <<  8 | (uint64_t) s[7]);
134#else
135	return x;
136#endif
137}
138
139#define htonq(x)  ntohq(x)
140
141
142/* we usually have to get the id out of a message */
143#define GET_SYM_ID(x, y)	do {					\
144	iscsid_sym_id_t	*__param;					\
145	__param = (iscsid_sym_id_t *)(void *)(y);			\
146	(void) memcpy(&x, &__param->id, sizeof(x));			\
147} while (/*CONSTCOND*/0)
148
149
150/* Check whether ID is present */
151#define NO_ID(sid) (!(sid)->id && !(sid)->name[0])
152
153/* iscsic_main.c */
154
155void arg_error(char *, const char *, ...) __printflike(2, 3) __dead;
156void arg_missing(const char *) __dead;
157void io_error(const char *, ...) __printflike(1, 2) __dead;
158void gen_error(const char *, ...) __printflike(1, 2) __dead;
159void check_extra_args(int, char **);
160void status_error(unsigned) __dead;
161void status_error_slist(unsigned) __dead;
162
163void send_request(unsigned, size_t, void *);
164iscsid_response_t *get_response(int);
165void free_response(iscsid_response_t *);
166
167/* iscsic_daemonif.c */
168
169int add_target(int, char **);
170int add_portal(int, char **);
171int remove_target(int, char **);
172int slp_find_targets(int, char **);
173int refresh_targets(int, char **);
174int list_targets(int, char **);
175int add_send_target(int, char **);
176int remove_send_target(int, char **);
177int list_send_targets(int, char **);
178int add_isns_server(int, char **);
179int remove_isns_server(int, char **);
180int find_isns_servers(int, char **);
181int list_isns_servers(int, char **);
182int refresh_isns(int, char **);
183int login(int, char **);
184int logout(int, char **);
185int add_connection(int, char **);
186int remove_connection(int, char **);
187int add_initiator(int, char **);
188int remove_initiator(int, char **);
189int list_initiators(int, char **);
190int list_sessions(int, char **);
191int get_version(int, char **);
192#ifdef ISCSI_DEBUG
193int kill_daemon(int, char **);
194#endif
195
196/* iscsic_driverif.c */
197
198uint32_t get_sessid(int, char **, int);
199void dump_data(const char *, const void *, size_t);
200int do_ioctl(iscsi_iocommand_parameters_t *, int);
201int set_node_name(int, char **);
202int inquiry(int, char **);
203int test_unit_ready(int, char **);
204int read_capacity(int, char **);
205int report_luns(int, char **);
206
207/* iscsic_parse.c */
208
209int cl_get_target(iscsid_add_target_req_t **, int, char **, int);
210int cl_get_portal(iscsid_add_portal_req_t *, int, char **);
211int cl_get_isns(iscsid_add_isns_server_req_t *, int, char **);
212int cl_get_send_targets(iscsid_add_target_req_t *, int, char **);
213int cl_get_auth_opts(iscsid_set_target_authentication_req_t *, int, char **);
214int cl_get_target_opts(iscsid_get_set_target_options_t *, int, char **);
215int cl_get_id(char, iscsid_sym_id_t *, int, char **);
216int cl_get_symname(uint8_t *, int, char **);
217int cl_get_string(char, char *, int, char **);
218int cl_get_opt(char, int, char **);
219char cl_get_char(char, int, char **);
220int cl_get_int(char, int, char **);
221int cl_get_uint(char, int, char **);
222uint64_t cl_get_longlong(char, int, char **);
223
224#ifdef ISCSI_TEST_MODE
225int test(int, char **);
226#endif
227
228
229#endif /* !_ISCSIC_GLOBALS_H */
230