xref: /freebsd/sys/dev/iscsi/icl.h (revision 6419bb52)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Edward Tomasz Napierala under sponsorship
8  * from the FreeBSD Foundation.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #ifndef ICL_H
35 #define	ICL_H
36 
37 /*
38  * iSCSI Common Layer.  It's used by both the initiator and target to send
39  * and receive iSCSI PDUs.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/kobj.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 
47 SYSCTL_DECL(_kern_icl);
48 
49 extern int icl_debug;
50 
51 #define	ICL_DEBUG(X, ...)						\
52 	do {								\
53 		if (icl_debug > 1)					\
54 			printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
55 	} while (0)
56 
57 #define	ICL_WARN(X, ...)						\
58 	do {								\
59 		if (icl_debug > 0) {					\
60 			printf("WARNING: %s: " X "\n",			\
61 			    __func__, ## __VA_ARGS__);			\
62 		}							\
63 	} while (0)
64 
65 struct icl_conn;
66 struct ccb_scsiio;
67 union ctl_io;
68 
69 struct icl_pdu {
70 	STAILQ_ENTRY(icl_pdu)	ip_next;
71 	struct icl_conn		*ip_conn;
72 	struct iscsi_bhs	*ip_bhs;
73 	struct mbuf		*ip_bhs_mbuf;
74 	size_t			ip_ahs_len;
75 	struct mbuf		*ip_ahs_mbuf;
76 	size_t			ip_data_len;
77 	struct mbuf		*ip_data_mbuf;
78 
79 	/*
80 	 * User (initiator or provider) private fields.
81 	 */
82 	void			*ip_prv0;
83 	void			*ip_prv1;
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 #define	ICL_NOCOPY			(1 << 30)
96 
97 struct icl_conn {
98 	KOBJ_FIELDS;
99 	struct mtx		*ic_lock;
100 	struct socket		*ic_socket;
101 #ifdef DIAGNOSTIC
102 	volatile u_int		ic_outstanding_pdus;
103 #endif
104 	STAILQ_HEAD(, icl_pdu)	ic_to_send;
105 	bool			ic_check_send_space;
106 	size_t			ic_receive_len;
107 	int			ic_receive_state;
108 	struct icl_pdu		*ic_receive_pdu;
109 	struct cv		ic_send_cv;
110 	struct cv		ic_receive_cv;
111 	bool			ic_header_crc32c;
112 	bool			ic_data_crc32c;
113 	bool			ic_send_running;
114 	bool			ic_receive_running;
115 	size_t			ic_max_data_segment_length;
116 	size_t			ic_maxtags;
117 	bool			ic_disconnecting;
118 	bool			ic_iser;
119 	bool			ic_unmapped;
120 	const char		*ic_name;
121 	const char		*ic_offload;
122 
123 	void			(*ic_receive)(struct icl_pdu *);
124 	void			(*ic_error)(struct icl_conn *);
125 
126 	/*
127 	 * User (initiator or provider) private fields.
128 	 */
129 	void			*ic_prv0;
130 };
131 
132 struct icl_drv_limits {
133 	int idl_max_recv_data_segment_length;
134 	int idl_max_send_data_segment_length;
135 	int idl_max_burst_length;
136 	int idl_first_burst_length;
137 	int spare[4];
138 };
139 
140 typedef void (*icl_pdu_cb)(struct icl_pdu *, int error);
141 
142 struct icl_conn	*icl_new_conn(const char *offload, bool iser, const char *name,
143 		    struct mtx *lock);
144 int		icl_limits(const char *offload, bool iser,
145 		    struct icl_drv_limits *idl);
146 int		icl_register(const char *offload, bool iser, int priority,
147 		    int (*limits)(struct icl_drv_limits *),
148 		    struct icl_conn *(*new_conn)(const char *, struct mtx *));
149 int		icl_unregister(const char *offload, bool rdma);
150 
151 #ifdef ICL_KERNEL_PROXY
152 
153 struct sockaddr;
154 struct icl_listen;
155 
156 /*
157  * Target part.
158  */
159 struct icl_listen	*icl_listen_new(void (*accept_cb)(struct socket *,
160 			    struct sockaddr *, int));
161 void			icl_listen_free(struct icl_listen *il);
162 int			icl_listen_add(struct icl_listen *il, bool rdma,
163 			    int domain, int socktype, int protocol,
164 			    struct sockaddr *sa, int portal_id);
165 int			icl_listen_remove(struct icl_listen *il, struct sockaddr *sa);
166 
167 /*
168  * Those two are not a public API; only to be used between icl_soft.c
169  * and icl_soft_proxy.c.
170  */
171 int			icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so);
172 int			icl_soft_proxy_connect(struct icl_conn *ic, int domain,
173 			    int socktype, int protocol, struct sockaddr *from_sa,
174 			    struct sockaddr *to_sa);
175 #endif /* ICL_KERNEL_PROXY */
176 #endif /* !ICL_H */
177