1 /*
2  * iSCSI lib definitions
3  *
4  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
5  * Copyright (C) 2004 - 2006 Mike Christie
6  * Copyright (C) 2004 - 2005 Dmitry Yusupov
7  * Copyright (C) 2004 - 2005 Alex Aizman
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23 #ifndef LIBISCSI_H
24 #define LIBISCSI_H
25 
26 #include <linux/types.h>
27 #include <linux/wait.h>
28 #include <linux/mutex.h>
29 #include <linux/timer.h>
30 #include <linux/workqueue.h>
31 #include <linux/kfifo.h>
32 #include <linux/refcount.h>
33 #include <scsi/iscsi_proto.h>
34 #include <scsi/iscsi_if.h>
35 #include <scsi/scsi_transport_iscsi.h>
36 
37 struct scsi_transport_template;
38 struct scsi_host_template;
39 struct scsi_device;
40 struct Scsi_Host;
41 struct scsi_target;
42 struct scsi_cmnd;
43 struct socket;
44 struct iscsi_transport;
45 struct iscsi_cls_session;
46 struct iscsi_cls_conn;
47 struct iscsi_session;
48 struct iscsi_nopin;
49 struct device;
50 
51 #define ISCSI_DEF_XMIT_CMDS_MAX	128	/* must be power of 2 */
52 #define ISCSI_MGMT_CMDS_MAX	15
53 
54 #define ISCSI_DEF_CMD_PER_LUN	32
55 
56 /* Task Mgmt states */
57 enum {
58 	TMF_INITIAL,
59 	TMF_QUEUED,
60 	TMF_SUCCESS,
61 	TMF_FAILED,
62 	TMF_TIMEDOUT,
63 	TMF_NOT_FOUND,
64 };
65 
66 #define ISID_SIZE			6
67 
68 /* Connection suspend "bit" */
69 #define ISCSI_SUSPEND_BIT		1
70 
71 #define ISCSI_ITT_MASK			0x1fff
72 #define ISCSI_TOTAL_CMDS_MAX		4096
73 /* this must be a power of two greater than ISCSI_MGMT_CMDS_MAX */
74 #define ISCSI_TOTAL_CMDS_MIN		16
75 #define ISCSI_AGE_SHIFT			28
76 #define ISCSI_AGE_MASK			0xf
77 
78 #define ISCSI_ADDRESS_BUF_LEN		64
79 
80 enum {
81 	/* this is the maximum possible storage for AHSs */
82 	ISCSI_MAX_AHS_SIZE = sizeof(struct iscsi_ecdb_ahdr) +
83 				sizeof(struct iscsi_rlength_ahdr),
84 	ISCSI_DIGEST_SIZE = sizeof(__u32),
85 };
86 
87 
88 enum {
89 	ISCSI_TASK_FREE,
90 	ISCSI_TASK_COMPLETED,
91 	ISCSI_TASK_PENDING,
92 	ISCSI_TASK_RUNNING,
93 	ISCSI_TASK_ABRT_TMF,		/* aborted due to TMF */
94 	ISCSI_TASK_ABRT_SESS_RECOV,	/* aborted due to session recovery */
95 	ISCSI_TASK_REQUEUE_SCSIQ,	/* qcmd requeueing to scsi-ml */
96 };
97 
98 struct iscsi_r2t_info {
99 	__be32			ttt;		/* copied from R2T */
100 	__be32			exp_statsn;	/* copied from R2T */
101 	uint32_t		data_length;	/* copied from R2T */
102 	uint32_t		data_offset;	/* copied from R2T */
103 	int			data_count;	/* DATA-Out payload progress */
104 	int			datasn;
105 	/* LLDs should set/update these values */
106 	int			sent;		/* R2T sequence progress */
107 };
108 
109 struct iscsi_task {
110 	/*
111 	 * Because LLDs allocate their hdr differently, this is a pointer
112 	 * and length to that storage. It must be setup at session
113 	 * creation time.
114 	 */
115 	struct iscsi_hdr	*hdr;
116 	unsigned short		hdr_max;
117 	unsigned short		hdr_len;	/* accumulated size of hdr used */
118 	/* copied values in case we need to send tmfs */
119 	itt_t			hdr_itt;
120 	__be32			cmdsn;
121 	struct scsi_lun		lun;
122 
123 	int			itt;		/* this ITT */
124 
125 	unsigned		imm_count;	/* imm-data (bytes)   */
126 	/* offset in unsolicited stream (bytes); */
127 	struct iscsi_r2t_info	unsol_r2t;
128 	char			*data;		/* mgmt payload */
129 	unsigned		data_count;
130 	struct scsi_cmnd	*sc;		/* associated SCSI cmd*/
131 	struct iscsi_conn	*conn;		/* used connection    */
132 
133 	/* data processing tracking */
134 	unsigned long		last_xfer;
135 	unsigned long		last_timeout;
136 	bool			have_checked_conn;
137 
138 	/* T10 protection information */
139 	bool			protected;
140 
141 	/* state set/tested under session->lock */
142 	int			state;
143 	refcount_t		refcount;
144 	struct list_head	running;	/* running cmd list */
145 	void			*dd_data;	/* driver/transport data */
146 };
147 
148 /* invalid scsi_task pointer */
149 #define	INVALID_SCSI_TASK	(struct iscsi_task *)-1l
150 
iscsi_task_has_unsol_data(struct iscsi_task * task)151 static inline int iscsi_task_has_unsol_data(struct iscsi_task *task)
152 {
153 	return task->unsol_r2t.data_length > task->unsol_r2t.sent;
154 }
155 
iscsi_next_hdr(struct iscsi_task * task)156 static inline void* iscsi_next_hdr(struct iscsi_task *task)
157 {
158 	return (void*)task->hdr + task->hdr_len;
159 }
160 
161 /* Connection's states */
162 enum {
163 	ISCSI_CONN_INITIAL_STAGE,
164 	ISCSI_CONN_STARTED,
165 	ISCSI_CONN_STOPPED,
166 	ISCSI_CONN_CLEANUP_WAIT,
167 };
168 
169 struct iscsi_conn {
170 	struct iscsi_cls_conn	*cls_conn;	/* ptr to class connection */
171 	void			*dd_data;	/* iscsi_transport data */
172 	struct iscsi_session	*session;	/* parent session */
173 	/*
174 	 * conn_stop() flag: stop to recover, stop to terminate
175 	 */
176         int			stop_stage;
177 	struct timer_list	transport_timer;
178 	unsigned long		last_recv;
179 	unsigned long		last_ping;
180 	int			ping_timeout;
181 	int			recv_timeout;
182 	struct iscsi_task 	*ping_task;
183 
184 	/* iSCSI connection-wide sequencing */
185 	uint32_t		exp_statsn;
186 	uint32_t		statsn;
187 
188 	/* control data */
189 	int			id;		/* CID */
190 	int			c_stage;	/* connection state */
191 	/*
192 	 * Preallocated buffer for pdus that have data but do not
193 	 * originate from scsi-ml. We never have two pdus using the
194 	 * buffer at the same time. It is only allocated to
195 	 * the default max recv size because the pdus we support
196 	 * should always fit in this buffer
197 	 */
198 	char			*data;
199 	struct iscsi_task 	*login_task;	/* mtask used for login/text */
200 	struct iscsi_task	*task;		/* xmit task in progress */
201 
202 	/* xmit */
203 	spinlock_t		taskqueuelock;  /* protects the next three lists */
204 	struct list_head	mgmtqueue;	/* mgmt (control) xmit queue */
205 	struct list_head	cmdqueue;	/* data-path cmd queue */
206 	struct list_head	requeue;	/* tasks needing another run */
207 	struct work_struct	xmitwork;	/* per-conn. xmit workqueue */
208 	unsigned long		suspend_tx;	/* suspend Tx */
209 	unsigned long		suspend_rx;	/* suspend Rx */
210 
211 	/* negotiated params */
212 	unsigned		max_recv_dlength; /* initiator_max_recv_dsl*/
213 	unsigned		max_xmit_dlength; /* target_max_recv_dsl */
214 	int			hdrdgst_en;
215 	int			datadgst_en;
216 	int			ifmarker_en;
217 	int			ofmarker_en;
218 	/* values userspace uses to id a conn */
219 	int			persistent_port;
220 	char			*persistent_address;
221 
222 	unsigned		max_segment_size;
223 	unsigned		tcp_xmit_wsf;
224 	unsigned		tcp_recv_wsf;
225 	uint16_t		keepalive_tmo;
226 	uint16_t		local_port;
227 	uint8_t			tcp_timestamp_stat;
228 	uint8_t			tcp_nagle_disable;
229 	uint8_t			tcp_wsf_disable;
230 	uint8_t			tcp_timer_scale;
231 	uint8_t			tcp_timestamp_en;
232 	uint8_t			fragment_disable;
233 	uint8_t			ipv4_tos;
234 	uint8_t			ipv6_traffic_class;
235 	uint8_t			ipv6_flow_label;
236 	uint8_t			is_fw_assigned_ipv6;
237 	char			*local_ipaddr;
238 
239 	/* MIB-statistics */
240 	uint64_t		txdata_octets;
241 	uint64_t		rxdata_octets;
242 	uint32_t		scsicmd_pdus_cnt;
243 	uint32_t		dataout_pdus_cnt;
244 	uint32_t		scsirsp_pdus_cnt;
245 	uint32_t		datain_pdus_cnt;
246 	uint32_t		r2t_pdus_cnt;
247 	uint32_t		tmfcmd_pdus_cnt;
248 	int32_t			tmfrsp_pdus_cnt;
249 
250 	/* custom statistics */
251 	uint32_t		eh_abort_cnt;
252 	uint32_t		fmr_unalign_cnt;
253 };
254 
255 struct iscsi_pool {
256 	struct kfifo		queue;		/* FIFO Queue */
257 	void			**pool;		/* Pool of elements */
258 	int			max;		/* Max number of elements */
259 };
260 
261 /* Session's states */
262 enum {
263 	ISCSI_STATE_FREE = 1,
264 	ISCSI_STATE_LOGGED_IN,
265 	ISCSI_STATE_FAILED,
266 	ISCSI_STATE_TERMINATE,
267 	ISCSI_STATE_IN_RECOVERY,
268 	ISCSI_STATE_RECOVERY_FAILED,
269 	ISCSI_STATE_LOGGING_OUT,
270 };
271 
272 struct iscsi_session {
273 	struct iscsi_cls_session *cls_session;
274 	/*
275 	 * Syncs up the scsi eh thread with the iscsi eh thread when sending
276 	 * task management functions. This must be taken before the session
277 	 * and recv lock.
278 	 */
279 	struct mutex		eh_mutex;
280 	/* abort */
281 	wait_queue_head_t	ehwait;		/* used in eh_abort() */
282 	struct iscsi_tm		tmhdr;
283 	struct timer_list	tmf_timer;
284 	int			tmf_state;	/* see TMF_INITIAL, etc.*/
285 
286 	/* iSCSI session-wide sequencing */
287 	uint32_t		cmdsn;
288 	uint32_t		exp_cmdsn;
289 	uint32_t		max_cmdsn;
290 
291 	/* This tracks the reqs queued into the initiator */
292 	uint32_t		queued_cmdsn;
293 
294 	/* configuration */
295 	int			abort_timeout;
296 	int			lu_reset_timeout;
297 	int			tgt_reset_timeout;
298 	int			initial_r2t_en;
299 	unsigned short		max_r2t;
300 	int			imm_data_en;
301 	unsigned		first_burst;
302 	unsigned		max_burst;
303 	int			time2wait;
304 	int			time2retain;
305 	int			pdu_inorder_en;
306 	int			dataseq_inorder_en;
307 	int			erl;
308 	int			fast_abort;
309 	int			tpgt;
310 	char			*username;
311 	char			*username_in;
312 	char			*password;
313 	char			*password_in;
314 	char			*targetname;
315 	char			*targetalias;
316 	char			*ifacename;
317 	char			*initiatorname;
318 	char			*boot_root;
319 	char			*boot_nic;
320 	char			*boot_target;
321 	char			*portal_type;
322 	char			*discovery_parent_type;
323 	uint16_t		discovery_parent_idx;
324 	uint16_t		def_taskmgmt_tmo;
325 	uint16_t		tsid;
326 	uint8_t			auto_snd_tgt_disable;
327 	uint8_t			discovery_sess;
328 	uint8_t			chap_auth_en;
329 	uint8_t			discovery_logout_en;
330 	uint8_t			bidi_chap_en;
331 	uint8_t			discovery_auth_optional;
332 	uint8_t			isid[ISID_SIZE];
333 
334 	/* control data */
335 	struct iscsi_transport	*tt;
336 	struct Scsi_Host	*host;
337 	struct iscsi_conn	*leadconn;	/* leading connection */
338 	/* Between the forward and the backward locks exists a strict locking
339 	 * hierarchy. The mutual exclusion zone protected by the forward lock
340 	 * can enclose the mutual exclusion zone protected by the backward lock
341 	 * but not vice versa.
342 	 */
343 	spinlock_t		frwd_lock;	/* protects session state, *
344 						 * cmdsn, queued_cmdsn     *
345 						 * session resources:      *
346 						 * - cmdpool kfifo_out ,   *
347 						 * - mgmtpool,		   */
348 	spinlock_t		back_lock;	/* protects cmdsn_exp      *
349 						 * cmdsn_max,              *
350 						 * cmdpool kfifo_in        */
351 	int			state;		/* session state           */
352 	int			age;		/* counts session re-opens */
353 
354 	int			scsi_cmds_max; 	/* max scsi commands */
355 	int			cmds_max;	/* size of cmds array */
356 	struct iscsi_task	**cmds;		/* Original Cmds arr */
357 	struct iscsi_pool	cmdpool;	/* PDU's pool */
358 	void			*dd_data;	/* LLD private data */
359 };
360 
361 enum {
362 	ISCSI_HOST_SETUP,
363 	ISCSI_HOST_REMOVED,
364 };
365 
366 struct iscsi_host {
367 	char			*initiatorname;
368 	/* hw address or netdev iscsi connection is bound to */
369 	char			*hwaddress;
370 	char			*netdev;
371 
372 	wait_queue_head_t	session_removal_wq;
373 	/* protects sessions and state */
374 	spinlock_t		lock;
375 	int			num_sessions;
376 	int			state;
377 
378 	struct workqueue_struct	*workq;
379 	char			workq_name[20];
380 };
381 
382 /*
383  * scsi host template
384  */
385 extern int iscsi_eh_abort(struct scsi_cmnd *sc);
386 extern int iscsi_eh_recover_target(struct scsi_cmnd *sc);
387 extern int iscsi_eh_session_reset(struct scsi_cmnd *sc);
388 extern int iscsi_eh_device_reset(struct scsi_cmnd *sc);
389 extern int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc);
390 extern enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc);
391 
392 /*
393  * iSCSI host helpers.
394  */
395 #define iscsi_host_priv(_shost) \
396 	(shost_priv(_shost) + sizeof(struct iscsi_host))
397 
398 extern int iscsi_host_set_param(struct Scsi_Host *shost,
399 				enum iscsi_host_param param, char *buf,
400 				int buflen);
401 extern int iscsi_host_get_param(struct Scsi_Host *shost,
402 				enum iscsi_host_param param, char *buf);
403 extern int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev);
404 extern struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
405 					  int dd_data_size,
406 					  bool xmit_can_sleep);
407 extern void iscsi_host_remove(struct Scsi_Host *shost);
408 extern void iscsi_host_free(struct Scsi_Host *shost);
409 extern int iscsi_target_alloc(struct scsi_target *starget);
410 
411 /*
412  * session management
413  */
414 extern struct iscsi_cls_session *
415 iscsi_session_setup(struct iscsi_transport *, struct Scsi_Host *shost,
416 		    uint16_t, int, int, uint32_t, unsigned int);
417 extern void iscsi_session_teardown(struct iscsi_cls_session *);
418 extern void iscsi_session_recovery_timedout(struct iscsi_cls_session *);
419 extern int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
420 			   enum iscsi_param param, char *buf, int buflen);
421 extern int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
422 				   enum iscsi_param param, char *buf);
423 
424 #define iscsi_session_printk(prefix, _sess, fmt, a...)	\
425 	iscsi_cls_session_printk(prefix, _sess->cls_session, fmt, ##a)
426 
427 /*
428  * connection management
429  */
430 extern struct iscsi_cls_conn *iscsi_conn_setup(struct iscsi_cls_session *,
431 					       int, uint32_t);
432 extern void iscsi_conn_teardown(struct iscsi_cls_conn *);
433 extern int iscsi_conn_start(struct iscsi_cls_conn *);
434 extern void iscsi_conn_stop(struct iscsi_cls_conn *, int);
435 extern int iscsi_conn_bind(struct iscsi_cls_session *, struct iscsi_cls_conn *,
436 			   int);
437 extern void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err);
438 extern void iscsi_session_failure(struct iscsi_session *session,
439 				  enum iscsi_err err);
440 extern int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
441 				enum iscsi_param param, char *buf);
442 extern int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
443 				     enum iscsi_param param, char *buf);
444 extern void iscsi_suspend_tx(struct iscsi_conn *conn);
445 extern void iscsi_suspend_queue(struct iscsi_conn *conn);
446 extern void iscsi_conn_queue_work(struct iscsi_conn *conn);
447 
448 #define iscsi_conn_printk(prefix, _c, fmt, a...) \
449 	iscsi_cls_conn_printk(prefix, ((struct iscsi_conn *)_c)->cls_conn, \
450 			      fmt, ##a)
451 
452 /*
453  * pdu and task processing
454  */
455 extern void iscsi_update_cmdsn(struct iscsi_session *, struct iscsi_nopin *);
456 extern void iscsi_prep_data_out_pdu(struct iscsi_task *task,
457 				    struct iscsi_r2t_info *r2t,
458 				    struct iscsi_data *hdr);
459 extern int iscsi_conn_send_pdu(struct iscsi_cls_conn *, struct iscsi_hdr *,
460 				char *, uint32_t);
461 extern int iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
462 			      char *, int);
463 extern int __iscsi_complete_pdu(struct iscsi_conn *, struct iscsi_hdr *,
464 				char *, int);
465 extern int iscsi_verify_itt(struct iscsi_conn *, itt_t);
466 extern struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *, itt_t);
467 extern struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *, itt_t);
468 extern void iscsi_requeue_task(struct iscsi_task *task);
469 extern void iscsi_put_task(struct iscsi_task *task);
470 extern void __iscsi_put_task(struct iscsi_task *task);
471 extern void __iscsi_get_task(struct iscsi_task *task);
472 extern void iscsi_complete_scsi_task(struct iscsi_task *task,
473 				     uint32_t exp_cmdsn, uint32_t max_cmdsn);
474 
475 /*
476  * generic helpers
477  */
478 extern void iscsi_pool_free(struct iscsi_pool *);
479 extern int iscsi_pool_init(struct iscsi_pool *, int, void ***, int);
480 extern int iscsi_switch_str_param(char **, char *);
481 
482 /*
483  * inline functions to deal with padding.
484  */
485 static inline unsigned int
iscsi_padded(unsigned int len)486 iscsi_padded(unsigned int len)
487 {
488 	return (len + ISCSI_PAD_LEN - 1) & ~(ISCSI_PAD_LEN - 1);
489 }
490 
491 static inline unsigned int
iscsi_padding(unsigned int len)492 iscsi_padding(unsigned int len)
493 {
494 	len &= (ISCSI_PAD_LEN - 1);
495 	if (len)
496 		len = ISCSI_PAD_LEN - len;
497 	return len;
498 }
499 
500 #endif
501