xref: /freebsd/sys/dev/iscsi/icl.h (revision 584e63313708455fe40568cc920f8ee9abe8e11f)
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
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  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef ICL_H
33 #define	ICL_H
34 
35 /*
36  * iSCSI Common Layer.  It's used by both the initiator and target to send
37  * and receive iSCSI PDUs.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/kobj.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 
45 SYSCTL_DECL(_kern_icl);
46 
47 extern int icl_debug;
48 
49 #define	ICL_DEBUG(X, ...)						\
50 	do {								\
51 		if (icl_debug > 1)					\
52 			printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
53 	} while (0)
54 
55 #define	ICL_WARN(X, ...)						\
56 	do {								\
57 		if (icl_debug > 0) {					\
58 			printf("WARNING: %s: " X "\n",			\
59 			    __func__, ## __VA_ARGS__);			\
60 		}							\
61 	} while (0)
62 
63 struct icl_conn;
64 struct ccb_scsiio;
65 union ctl_io;
66 
67 struct icl_pdu {
68 	STAILQ_ENTRY(icl_pdu)	ip_next;
69 	struct icl_conn		*ip_conn;
70 	struct iscsi_bhs	*ip_bhs;
71 	struct mbuf		*ip_bhs_mbuf;
72 	size_t			ip_ahs_len;
73 	struct mbuf		*ip_ahs_mbuf;
74 	size_t			ip_data_len;
75 	struct mbuf		*ip_data_mbuf;
76 
77 	/*
78 	 * User (initiator or provider) private fields.
79 	 */
80 	uint32_t		ip_prv0;
81 	uint32_t		ip_prv1;
82 	uint32_t		ip_prv2;
83 	uint32_t                ip_ofld_prv0;/* indicate iscsi-inititor that data is DDP'ed */
84 };
85 
86 #define ICL_CONN_STATE_INVALID		0
87 #define ICL_CONN_STATE_BHS		1
88 #define ICL_CONN_STATE_AHS		2
89 #define ICL_CONN_STATE_HEADER_DIGEST	3
90 #define ICL_CONN_STATE_DATA		4
91 #define ICL_CONN_STATE_DATA_DIGEST	5
92 
93 #define	ICL_MAX_DATA_SEGMENT_LENGTH	(128 * 1024)
94 
95 struct icl_conn {
96 	KOBJ_FIELDS;
97 	struct mtx		*ic_lock;
98 	struct socket		*ic_socket;
99 #ifdef DIAGNOSTIC
100 	volatile u_int		ic_outstanding_pdus;
101 #endif
102 	STAILQ_HEAD(, icl_pdu)	ic_to_send;
103 	bool			ic_check_send_space;
104 	size_t			ic_receive_len;
105 	int			ic_receive_state;
106 	struct icl_pdu		*ic_receive_pdu;
107 	struct cv		ic_send_cv;
108 	struct cv		ic_receive_cv;
109 	bool			ic_header_crc32c;
110 	bool			ic_data_crc32c;
111 	bool			ic_send_running;
112 	bool			ic_receive_running;
113 	size_t			ic_max_data_segment_length;
114 	bool			ic_disconnecting;
115 	bool			ic_iser;
116 	const char		*ic_name;
117 	const char		*ic_offload;
118 
119 	void			(*ic_receive)(struct icl_pdu *);
120 	void			(*ic_error)(struct icl_conn *);
121 
122 	/*
123 	 * User (initiator or provider) private fields.
124 	 */
125 	void			*ic_prv0;
126 	void			*ic_ofld_prv0;
127 };
128 
129 struct icl_conn	*icl_new_conn(const char *offload, const char *name,
130 		    struct mtx *lock);
131 int		icl_limits(const char *offload, size_t *limitp);
132 
133 int		icl_register(const char *offload, int priority,
134 		    int (*limits)(size_t *),
135 		    struct icl_conn *(*new_conn)(const char *, struct mtx *));
136 int		icl_unregister(const char *offload);
137 
138 #ifdef ICL_KERNEL_PROXY
139 
140 struct sockaddr;
141 struct icl_listen;
142 
143 struct icl_listen_sock {
144 	TAILQ_ENTRY(icl_listen_sock)	ils_next;
145 	struct icl_listen		*ils_listen;
146 	struct socket			*ils_socket;
147 	bool				ils_running;
148 	bool				ils_disconnecting;
149 	int				ils_id;
150 };
151 
152 struct icl_listen	{
153 	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
154 	struct sx			il_lock;
155 	void				(*il_accept)(struct socket *,
156 					    struct sockaddr *, int);
157 };
158 
159 /*
160  * Initiator part.
161  */
162 int			icl_conn_connect(struct icl_conn *ic, bool rdma,
163 			    int domain, int socktype, int protocol,
164 			    struct sockaddr *from_sa, struct sockaddr *to_sa);
165 /*
166  * Target part.
167  */
168 struct icl_listen	*icl_listen_new(void (*accept_cb)(struct socket *,
169 			    struct sockaddr *, int));
170 void			icl_listen_free(struct icl_listen *il);
171 int			icl_listen_add(struct icl_listen *il, bool rdma,
172 			    int domain, int socktype, int protocol,
173 			    struct sockaddr *sa, int portal_id);
174 int			icl_listen_remove(struct icl_listen *il, struct sockaddr *sa);
175 
176 /*
177  * This one is not a public API; only to be used by icl_proxy.c.
178  */
179 int			icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so);
180 
181 #endif /* ICL_KERNEL_PROXY */
182 
183 #endif /* !ICL_H */
184