11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * iSCSI transport class definitions
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2004
57996a778SMike Christie  * Copyright (C) Mike Christie, 2004 - 2006
639e84790SAlex Aizman  * Copyright (C) Dmitry Yusupov, 2004 - 2005
739e84790SAlex Aizman  * Copyright (C) Alex Aizman, 2004 - 2005
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
101da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
111da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
121da177e4SLinus Torvalds  * (at your option) any later version.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
151da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
161da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171da177e4SLinus Torvalds  * GNU General Public License for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
201da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
211da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds #ifndef SCSI_TRANSPORT_ISCSI_H
241da177e4SLinus Torvalds #define SCSI_TRANSPORT_ISCSI_H
251da177e4SLinus Torvalds 
267b8631b5SMike Christie #include <linux/device.h>
2739e84790SAlex Aizman #include <scsi/iscsi_if.h>
281da177e4SLinus Torvalds 
297b8631b5SMike Christie struct scsi_transport_template;
307996a778SMike Christie struct iscsi_transport;
317b8631b5SMike Christie struct Scsi_Host;
327b8631b5SMike Christie struct mempool_zone;
337b8631b5SMike Christie struct iscsi_cls_conn;
347996a778SMike Christie struct iscsi_conn;
357996a778SMike Christie struct iscsi_cmd_task;
367996a778SMike Christie struct iscsi_mgmt_task;
37a54a52caSMike Christie struct sockaddr;
387b8631b5SMike Christie 
3939e84790SAlex Aizman /**
4039e84790SAlex Aizman  * struct iscsi_transport - iSCSI Transport template
4139e84790SAlex Aizman  *
4239e84790SAlex Aizman  * @name:		transport name
4339e84790SAlex Aizman  * @caps:		iSCSI Data-Path capabilities
4439e84790SAlex Aizman  * @create_session:	create new iSCSI session object
4539e84790SAlex Aizman  * @destroy_session:	destroy existing iSCSI session object
4639e84790SAlex Aizman  * @create_conn:	create new iSCSI connection
4739e84790SAlex Aizman  * @bind_conn:		associate this connection with existing iSCSI session
4839e84790SAlex Aizman  *			and specified transport descriptor
4939e84790SAlex Aizman  * @destroy_conn:	destroy inactive iSCSI connection
50a54a52caSMike Christie  * @set_param:		set iSCSI parameter. Return 0 on success, -ENODATA
51a54a52caSMike Christie  *			when param is not supported, and a -Exx value on other
52a54a52caSMike Christie  *			error.
53a54a52caSMike Christie  * @get_param		get iSCSI parameter. Must return number of bytes
54a54a52caSMike Christie  *			copied to buffer on success, -ENODATA when param
55a54a52caSMike Christie  *			is not supported, and a -Exx value on other error
5639e84790SAlex Aizman  * @start_conn:		set connection to be operational
5739e84790SAlex Aizman  * @stop_conn:		suspend/recover/terminate connection
5839e84790SAlex Aizman  * @send_pdu:		send iSCSI PDU, Login, Logout, NOP-Out, Reject, Text.
597996a778SMike Christie  * @session_recovery_timedout: notify LLD a block during recovery timed out
607996a778SMike Christie  * @suspend_conn_recv:	susepend the recv side of the connection
617996a778SMike Christie  * @termincate_conn:	destroy socket connection. Called with mutex lock.
627996a778SMike Christie  * @init_cmd_task:	Initialize a iscsi_cmd_task and any internal structs.
637996a778SMike Christie  *			Called from queuecommand with session lock held.
647996a778SMike Christie  * @init_mgmt_task:	Initialize a iscsi_mgmt_task and any internal structs.
657996a778SMike Christie  *			Called from iscsi_conn_send_generic with xmitmutex.
663219e529SMike Christie  * @xmit_cmd_task:	Requests LLD to transfer cmd task. Returns 0 or the
673219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
683219e529SMike Christie  *			value on error.
693219e529SMike Christie  * @xmit_mgmt_task:	Requests LLD to transfer mgmt task. Returns 0 or the
703219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
713219e529SMike Christie  *			value on error.
727996a778SMike Christie  * @cleanup_cmd_task:	requests LLD to fail cmd task. Called with xmitmutex
737996a778SMike Christie  *			and session->lock after the connection has been
747996a778SMike Christie  *			suspended and terminated during recovery. If called
757996a778SMike Christie  *			from abort task then connection is not suspended
767996a778SMike Christie  *			or terminated but sk_callback_lock is held
7739e84790SAlex Aizman  *
7839e84790SAlex Aizman  * Template API provided by iSCSI Transport
7939e84790SAlex Aizman  */
8039e84790SAlex Aizman struct iscsi_transport {
8139e84790SAlex Aizman 	struct module *owner;
8239e84790SAlex Aizman 	char *name;
8339e84790SAlex Aizman 	unsigned int caps;
84fd7255f5SMike Christie 	/* LLD sets this to indicate what values it can export to sysfs */
85fd7255f5SMike Christie 	unsigned int param_mask;
8639e84790SAlex Aizman 	struct scsi_host_template *host_template;
877b8631b5SMike Christie 	/* LLD connection data size */
887b8631b5SMike Christie 	int conndata_size;
89b5c7a12dSMike Christie 	/* LLD session data size */
90b5c7a12dSMike Christie 	int sessiondata_size;
9139e84790SAlex Aizman 	int max_lun;
9239e84790SAlex Aizman 	unsigned int max_conn;
9339e84790SAlex Aizman 	unsigned int max_cmd_len;
947996a778SMike Christie 	struct iscsi_cls_session *(*create_session) (struct iscsi_transport *it,
957996a778SMike Christie 		struct scsi_transport_template *t, uint32_t sn, uint32_t *hn);
967b7232f3SMike Christie 	void (*destroy_session) (struct iscsi_cls_session *session);
977b7232f3SMike Christie 	struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess,
987b8631b5SMike Christie 				uint32_t cid);
997b7232f3SMike Christie 	int (*bind_conn) (struct iscsi_cls_session *session,
1007b7232f3SMike Christie 			  struct iscsi_cls_conn *cls_conn,
101264faaaaSOr Gerlitz 			  uint64_t transport_eph, int is_leading);
1027b7232f3SMike Christie 	int (*start_conn) (struct iscsi_cls_conn *conn);
1037b7232f3SMike Christie 	void (*stop_conn) (struct iscsi_cls_conn *conn, int flag);
1047b8631b5SMike Christie 	void (*destroy_conn) (struct iscsi_cls_conn *conn);
1057b7232f3SMike Christie 	int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param,
106a54a52caSMike Christie 			  char *buf, int buflen);
1077b7232f3SMike Christie 	int (*get_conn_param) (struct iscsi_cls_conn *conn,
108fd7255f5SMike Christie 			       enum iscsi_param param, char *buf);
109a54a52caSMike Christie 	int (*get_session_param) (struct iscsi_cls_session *session,
110fd7255f5SMike Christie 				  enum iscsi_param param, char *buf);
1117b7232f3SMike Christie 	int (*send_pdu) (struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
11239e84790SAlex Aizman 			 char *data, uint32_t data_size);
1137b7232f3SMike Christie 	void (*get_stats) (struct iscsi_cls_conn *conn,
1147b7232f3SMike Christie 			   struct iscsi_stats *stats);
1157996a778SMike Christie 	void (*suspend_conn_recv) (struct iscsi_conn *conn);
1167996a778SMike Christie 	void (*terminate_conn) (struct iscsi_conn *conn);
1177996a778SMike Christie 	void (*init_cmd_task) (struct iscsi_cmd_task *ctask);
1187996a778SMike Christie 	void (*init_mgmt_task) (struct iscsi_conn *conn,
1197996a778SMike Christie 				struct iscsi_mgmt_task *mtask,
1207996a778SMike Christie 				char *data, uint32_t data_size);
1217996a778SMike Christie 	int (*xmit_cmd_task) (struct iscsi_conn *conn,
1227996a778SMike Christie 			      struct iscsi_cmd_task *ctask);
1237996a778SMike Christie 	void (*cleanup_cmd_task) (struct iscsi_conn *conn,
1247996a778SMike Christie 				  struct iscsi_cmd_task *ctask);
1257996a778SMike Christie 	int (*xmit_mgmt_task) (struct iscsi_conn *conn,
1267996a778SMike Christie 			       struct iscsi_mgmt_task *mtask);
12730a6c652SMike Christie 	void (*session_recovery_timedout) (struct iscsi_cls_session *session);
128264faaaaSOr Gerlitz 	int (*ep_connect) (struct sockaddr *dst_addr, int non_blocking,
129264faaaaSOr Gerlitz 			   uint64_t *ep_handle);
130264faaaaSOr Gerlitz 	int (*ep_poll) (uint64_t ep_handle, int timeout_ms);
131264faaaaSOr Gerlitz 	void (*ep_disconnect) (uint64_t ep_handle);
13201cb225dSMike Christie 	int (*tgt_dscvr) (enum iscsi_tgt_dscvr type, uint32_t host_no,
13301cb225dSMike Christie 			  uint32_t enable, struct sockaddr *dst_addr);
1341da177e4SLinus Torvalds };
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*
13739e84790SAlex Aizman  * transport registration upcalls
1381da177e4SLinus Torvalds  */
1397b8631b5SMike Christie extern struct scsi_transport_template *iscsi_register_transport(struct iscsi_transport *tt);
14039e84790SAlex Aizman extern int iscsi_unregister_transport(struct iscsi_transport *tt);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /*
14339e84790SAlex Aizman  * control plane upcalls
1441da177e4SLinus Torvalds  */
1457b7232f3SMike Christie extern void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error);
1467b7232f3SMike Christie extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
14739e84790SAlex Aizman 			  char *data, uint32_t data_size);
1481da177e4SLinus Torvalds 
1497996a778SMike Christie 
1507996a778SMike Christie /* Connection's states */
1517996a778SMike Christie #define ISCSI_CONN_INITIAL_STAGE	0
1527996a778SMike Christie #define ISCSI_CONN_STARTED		1
1537996a778SMike Christie #define ISCSI_CONN_STOPPED		2
1547996a778SMike Christie #define ISCSI_CONN_CLEANUP_WAIT		3
1557996a778SMike Christie 
1567b8631b5SMike Christie struct iscsi_cls_conn {
1577b8631b5SMike Christie 	struct list_head conn_list;	/* item in connlist */
1587b8631b5SMike Christie 	void *dd_data;			/* LLD private data */
1597b8631b5SMike Christie 	struct iscsi_transport *transport;
160b5c7a12dSMike Christie 	uint32_t cid;			/* connection id */
161fd7255f5SMike Christie 
1627b8631b5SMike Christie 	int active;			/* must be accessed with the connlock */
1637b8631b5SMike Christie 	struct device dev;		/* sysfs transport/container device */
1647b8631b5SMike Christie 	struct mempool_zone *z_error;
1657b8631b5SMike Christie 	struct mempool_zone *z_pdu;
1667b8631b5SMike Christie 	struct list_head freequeue;
1677b8631b5SMike Christie };
1687b8631b5SMike Christie 
1697b8631b5SMike Christie #define iscsi_dev_to_conn(_dev) \
1707b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_conn, dev)
1717b8631b5SMike Christie 
1727996a778SMike Christie /* Session's states */
1737996a778SMike Christie #define ISCSI_STATE_FREE		1
1747996a778SMike Christie #define ISCSI_STATE_LOGGED_IN		2
1757996a778SMike Christie #define ISCSI_STATE_FAILED		3
1767996a778SMike Christie #define ISCSI_STATE_TERMINATE		4
177656cffc9SMike Christie #define ISCSI_STATE_IN_RECOVERY		5
178656cffc9SMike Christie #define ISCSI_STATE_RECOVERY_FAILED	6
1797996a778SMike Christie 
1807b8631b5SMike Christie struct iscsi_cls_session {
1817b7232f3SMike Christie 	struct list_head sess_list;		/* item in session_list */
18230a6c652SMike Christie 	struct list_head host_list;
1837b8631b5SMike Christie 	struct iscsi_transport *transport;
184fd7255f5SMike Christie 
18530a6c652SMike Christie 	/* recovery fields */
18630a6c652SMike Christie 	int recovery_tmo;
18730a6c652SMike Christie 	struct work_struct recovery_work;
18830a6c652SMike Christie 
18930a6c652SMike Christie 	int target_id;
19030a6c652SMike Christie 	int channel;
19130a6c652SMike Christie 
192b5c7a12dSMike Christie 	int sid;				/* session id */
193b5c7a12dSMike Christie 	void *dd_data;				/* LLD private data */
1947b8631b5SMike Christie 	struct device dev;	/* sysfs transport/container device */
1957b8631b5SMike Christie };
1967b8631b5SMike Christie 
1977b8631b5SMike Christie #define iscsi_dev_to_session(_dev) \
1987b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_session, dev)
1997b8631b5SMike Christie 
2007b8631b5SMike Christie #define iscsi_session_to_shost(_session) \
2017b8631b5SMike Christie 	dev_to_shost(_session->dev.parent)
2027b8631b5SMike Christie 
20330a6c652SMike Christie struct iscsi_host {
20430a6c652SMike Christie 	int next_target_id;
20530a6c652SMike Christie 	struct list_head sessions;
20630a6c652SMike Christie 	struct mutex mutex;
20730a6c652SMike Christie };
20830a6c652SMike Christie 
2097b8631b5SMike Christie /*
2107b8631b5SMike Christie  * session and connection functions that can be used by HW iSCSI LLDs
2117b8631b5SMike Christie  */
2127b8631b5SMike Christie extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost,
21330a6c652SMike Christie 				struct iscsi_transport *t, int channel);
2147b8631b5SMike Christie extern int iscsi_destroy_session(struct iscsi_cls_session *session);
2157b8631b5SMike Christie extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess,
2167b8631b5SMike Christie 					    uint32_t cid);
2177b8631b5SMike Christie extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn);
21830a6c652SMike Christie extern void iscsi_unblock_session(struct iscsi_cls_session *session);
21930a6c652SMike Christie extern void iscsi_block_session(struct iscsi_cls_session *session);
2207b8631b5SMike Christie 
2211da177e4SLinus Torvalds #endif
222