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;
377b8631b5SMike Christie 
3839e84790SAlex Aizman /**
3939e84790SAlex Aizman  * struct iscsi_transport - iSCSI Transport template
4039e84790SAlex Aizman  *
4139e84790SAlex Aizman  * @name:		transport name
4239e84790SAlex Aizman  * @caps:		iSCSI Data-Path capabilities
4339e84790SAlex Aizman  * @create_session:	create new iSCSI session object
4439e84790SAlex Aizman  * @destroy_session:	destroy existing iSCSI session object
4539e84790SAlex Aizman  * @create_conn:	create new iSCSI connection
4639e84790SAlex Aizman  * @bind_conn:		associate this connection with existing iSCSI session
4739e84790SAlex Aizman  *			and specified transport descriptor
4839e84790SAlex Aizman  * @destroy_conn:	destroy inactive iSCSI connection
4939e84790SAlex Aizman  * @set_param:		set iSCSI Data-Path operational parameter
5039e84790SAlex Aizman  * @start_conn:		set connection to be operational
5139e84790SAlex Aizman  * @stop_conn:		suspend/recover/terminate connection
5239e84790SAlex Aizman  * @send_pdu:		send iSCSI PDU, Login, Logout, NOP-Out, Reject, Text.
537996a778SMike Christie  * @session_recovery_timedout: notify LLD a block during recovery timed out
547996a778SMike Christie  * @suspend_conn_recv:	susepend the recv side of the connection
557996a778SMike Christie  * @termincate_conn:	destroy socket connection. Called with mutex lock.
567996a778SMike Christie  * @init_cmd_task:	Initialize a iscsi_cmd_task and any internal structs.
577996a778SMike Christie  *			Called from queuecommand with session lock held.
587996a778SMike Christie  * @init_mgmt_task:	Initialize a iscsi_mgmt_task and any internal structs.
597996a778SMike Christie  *			Called from iscsi_conn_send_generic with xmitmutex.
603219e529SMike Christie  * @xmit_cmd_task:	Requests LLD to transfer cmd task. Returns 0 or the
613219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
623219e529SMike Christie  *			value on error.
633219e529SMike Christie  * @xmit_mgmt_task:	Requests LLD to transfer mgmt task. Returns 0 or the
643219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
653219e529SMike Christie  *			value on error.
667996a778SMike Christie  * @cleanup_cmd_task:	requests LLD to fail cmd task. Called with xmitmutex
677996a778SMike Christie  *			and session->lock after the connection has been
687996a778SMike Christie  *			suspended and terminated during recovery. If called
697996a778SMike Christie  *			from abort task then connection is not suspended
707996a778SMike Christie  *			or terminated but sk_callback_lock is held
7139e84790SAlex Aizman  *
7239e84790SAlex Aizman  * Template API provided by iSCSI Transport
7339e84790SAlex Aizman  */
7439e84790SAlex Aizman struct iscsi_transport {
7539e84790SAlex Aizman 	struct module *owner;
7639e84790SAlex Aizman 	char *name;
7739e84790SAlex Aizman 	unsigned int caps;
78fd7255f5SMike Christie 	/* LLD sets this to indicate what values it can export to sysfs */
79fd7255f5SMike Christie 	unsigned int param_mask;
8039e84790SAlex Aizman 	struct scsi_host_template *host_template;
817b8631b5SMike Christie 	/* LLD connection data size */
827b8631b5SMike Christie 	int conndata_size;
83b5c7a12dSMike Christie 	/* LLD session data size */
84b5c7a12dSMike Christie 	int sessiondata_size;
8539e84790SAlex Aizman 	int max_lun;
8639e84790SAlex Aizman 	unsigned int max_conn;
8739e84790SAlex Aizman 	unsigned int max_cmd_len;
887996a778SMike Christie 	struct iscsi_cls_session *(*create_session) (struct iscsi_transport *it,
897996a778SMike Christie 		struct scsi_transport_template *t, uint32_t sn, uint32_t *hn);
907b7232f3SMike Christie 	void (*destroy_session) (struct iscsi_cls_session *session);
917b7232f3SMike Christie 	struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess,
927b8631b5SMike Christie 				uint32_t cid);
937b7232f3SMike Christie 	int (*bind_conn) (struct iscsi_cls_session *session,
947b7232f3SMike Christie 			  struct iscsi_cls_conn *cls_conn,
95264faaaaSOr Gerlitz 			  uint64_t transport_eph, int is_leading);
967b7232f3SMike Christie 	int (*start_conn) (struct iscsi_cls_conn *conn);
977b7232f3SMike Christie 	void (*stop_conn) (struct iscsi_cls_conn *conn, int flag);
987b8631b5SMike Christie 	void (*destroy_conn) (struct iscsi_cls_conn *conn);
997b7232f3SMike Christie 	int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param,
10039e84790SAlex Aizman 			  uint32_t value);
1017b7232f3SMike Christie 	int (*get_conn_param) (struct iscsi_cls_conn *conn,
102fd7255f5SMike Christie 			       enum iscsi_param param, uint32_t *value);
1037b7232f3SMike Christie 	int (*get_session_param) (struct iscsi_cls_session *session,
1047b8631b5SMike Christie 				  enum iscsi_param param, uint32_t *value);
105fd7255f5SMike Christie 	int (*get_conn_str_param) (struct iscsi_cls_conn *conn,
106fd7255f5SMike Christie 				   enum iscsi_param param, char *buf);
107fd7255f5SMike Christie 	int (*get_session_str_param) (struct iscsi_cls_session *session,
108fd7255f5SMike Christie 				      enum iscsi_param param, char *buf);
1097b7232f3SMike Christie 	int (*send_pdu) (struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
11039e84790SAlex Aizman 			 char *data, uint32_t data_size);
1117b7232f3SMike Christie 	void (*get_stats) (struct iscsi_cls_conn *conn,
1127b7232f3SMike Christie 			   struct iscsi_stats *stats);
1137996a778SMike Christie 	void (*suspend_conn_recv) (struct iscsi_conn *conn);
1147996a778SMike Christie 	void (*terminate_conn) (struct iscsi_conn *conn);
1157996a778SMike Christie 	void (*init_cmd_task) (struct iscsi_cmd_task *ctask);
1167996a778SMike Christie 	void (*init_mgmt_task) (struct iscsi_conn *conn,
1177996a778SMike Christie 				struct iscsi_mgmt_task *mtask,
1187996a778SMike Christie 				char *data, uint32_t data_size);
1197996a778SMike Christie 	int (*xmit_cmd_task) (struct iscsi_conn *conn,
1207996a778SMike Christie 			      struct iscsi_cmd_task *ctask);
1217996a778SMike Christie 	void (*cleanup_cmd_task) (struct iscsi_conn *conn,
1227996a778SMike Christie 				  struct iscsi_cmd_task *ctask);
1237996a778SMike Christie 	int (*xmit_mgmt_task) (struct iscsi_conn *conn,
1247996a778SMike Christie 			       struct iscsi_mgmt_task *mtask);
12530a6c652SMike Christie 	void (*session_recovery_timedout) (struct iscsi_cls_session *session);
126264faaaaSOr Gerlitz 	int (*ep_connect) (struct sockaddr *dst_addr, int non_blocking,
127264faaaaSOr Gerlitz 			   uint64_t *ep_handle);
128264faaaaSOr Gerlitz 	int (*ep_poll) (uint64_t ep_handle, int timeout_ms);
129264faaaaSOr Gerlitz 	void (*ep_disconnect) (uint64_t ep_handle);
13001cb225dSMike Christie 	int (*tgt_dscvr) (enum iscsi_tgt_dscvr type, uint32_t host_no,
13101cb225dSMike Christie 			  uint32_t enable, struct sockaddr *dst_addr);
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
13539e84790SAlex Aizman  * transport registration upcalls
1361da177e4SLinus Torvalds  */
1377b8631b5SMike Christie extern struct scsi_transport_template *iscsi_register_transport(struct iscsi_transport *tt);
13839e84790SAlex Aizman extern int iscsi_unregister_transport(struct iscsi_transport *tt);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /*
14139e84790SAlex Aizman  * control plane upcalls
1421da177e4SLinus Torvalds  */
1437b7232f3SMike Christie extern void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error);
1447b7232f3SMike Christie extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
14539e84790SAlex Aizman 			  char *data, uint32_t data_size);
1461da177e4SLinus Torvalds 
1477996a778SMike Christie 
1487996a778SMike Christie /* Connection's states */
1497996a778SMike Christie #define ISCSI_CONN_INITIAL_STAGE	0
1507996a778SMike Christie #define ISCSI_CONN_STARTED		1
1517996a778SMike Christie #define ISCSI_CONN_STOPPED		2
1527996a778SMike Christie #define ISCSI_CONN_CLEANUP_WAIT		3
1537996a778SMike Christie 
1547b8631b5SMike Christie struct iscsi_cls_conn {
1557b8631b5SMike Christie 	struct list_head conn_list;	/* item in connlist */
1567b8631b5SMike Christie 	void *dd_data;			/* LLD private data */
1577b8631b5SMike Christie 	struct iscsi_transport *transport;
158b5c7a12dSMike Christie 	uint32_t cid;			/* connection id */
159fd7255f5SMike Christie 
160fd7255f5SMike Christie 	/* portal/group values we got during discovery */
161fd7255f5SMike Christie 	char *persistent_address;
162fd7255f5SMike Christie 	int persistent_port;
163fd7255f5SMike Christie 	/* portal/group values we are currently using */
164fd7255f5SMike Christie 	char *address;
165fd7255f5SMike Christie 	int port;
166fd7255f5SMike Christie 
1677b8631b5SMike Christie 	int active;			/* must be accessed with the connlock */
1687b8631b5SMike Christie 	struct device dev;		/* sysfs transport/container device */
1697b8631b5SMike Christie 	struct mempool_zone *z_error;
1707b8631b5SMike Christie 	struct mempool_zone *z_pdu;
1717b8631b5SMike Christie 	struct list_head freequeue;
1727b8631b5SMike Christie };
1737b8631b5SMike Christie 
1747b8631b5SMike Christie #define iscsi_dev_to_conn(_dev) \
1757b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_conn, dev)
1767b8631b5SMike Christie 
1777996a778SMike Christie /* Session's states */
1787996a778SMike Christie #define ISCSI_STATE_FREE		1
1797996a778SMike Christie #define ISCSI_STATE_LOGGED_IN		2
1807996a778SMike Christie #define ISCSI_STATE_FAILED		3
1817996a778SMike Christie #define ISCSI_STATE_TERMINATE		4
182656cffc9SMike Christie #define ISCSI_STATE_IN_RECOVERY		5
183656cffc9SMike Christie #define ISCSI_STATE_RECOVERY_FAILED	6
1847996a778SMike Christie 
1857b8631b5SMike Christie struct iscsi_cls_session {
1867b7232f3SMike Christie 	struct list_head sess_list;		/* item in session_list */
18730a6c652SMike Christie 	struct list_head host_list;
1887b8631b5SMike Christie 	struct iscsi_transport *transport;
189fd7255f5SMike Christie 
190fd7255f5SMike Christie 	/* iSCSI values used as unique id by userspace. */
191fd7255f5SMike Christie 	char *targetname;
192fd7255f5SMike Christie 	int tpgt;
193fd7255f5SMike Christie 
19430a6c652SMike Christie 	/* recovery fields */
19530a6c652SMike Christie 	int recovery_tmo;
19630a6c652SMike Christie 	struct work_struct recovery_work;
19730a6c652SMike Christie 
19830a6c652SMike Christie 	int target_id;
19930a6c652SMike Christie 	int channel;
20030a6c652SMike Christie 
201b5c7a12dSMike Christie 	int sid;				/* session id */
202b5c7a12dSMike Christie 	void *dd_data;				/* LLD private data */
2037b8631b5SMike Christie 	struct device dev;	/* sysfs transport/container device */
2047b8631b5SMike Christie };
2057b8631b5SMike Christie 
2067b8631b5SMike Christie #define iscsi_dev_to_session(_dev) \
2077b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_session, dev)
2087b8631b5SMike Christie 
2097b8631b5SMike Christie #define iscsi_session_to_shost(_session) \
2107b8631b5SMike Christie 	dev_to_shost(_session->dev.parent)
2117b8631b5SMike Christie 
21230a6c652SMike Christie struct iscsi_host {
21330a6c652SMike Christie 	int next_target_id;
21430a6c652SMike Christie 	struct list_head sessions;
21530a6c652SMike Christie 	struct mutex mutex;
21630a6c652SMike Christie };
21730a6c652SMike Christie 
2187b8631b5SMike Christie /*
2197b8631b5SMike Christie  * session and connection functions that can be used by HW iSCSI LLDs
2207b8631b5SMike Christie  */
2217b8631b5SMike Christie extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost,
22230a6c652SMike Christie 				struct iscsi_transport *t, int channel);
2237b8631b5SMike Christie extern int iscsi_destroy_session(struct iscsi_cls_session *session);
2247b8631b5SMike Christie extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess,
2257b8631b5SMike Christie 					    uint32_t cid);
2267b8631b5SMike Christie extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn);
22730a6c652SMike Christie extern void iscsi_unblock_session(struct iscsi_cls_session *session);
22830a6c652SMike Christie extern void iscsi_block_session(struct iscsi_cls_session *session);
2297b8631b5SMike Christie 
2301da177e4SLinus Torvalds #endif
231