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>
27d773c082SMichael S. Tsirkin #include <linux/list.h>
28d773c082SMichael S. Tsirkin #include <linux/mutex.h>
2939e84790SAlex Aizman #include <scsi/iscsi_if.h>
301da177e4SLinus Torvalds 
317b8631b5SMike Christie struct scsi_transport_template;
327996a778SMike Christie struct iscsi_transport;
337b8631b5SMike Christie struct Scsi_Host;
347b8631b5SMike Christie struct iscsi_cls_conn;
357996a778SMike Christie struct iscsi_conn;
367996a778SMike Christie struct iscsi_cmd_task;
377996a778SMike Christie struct iscsi_mgmt_task;
38a54a52caSMike Christie struct sockaddr;
397b8631b5SMike Christie 
4039e84790SAlex Aizman /**
4139e84790SAlex Aizman  * struct iscsi_transport - iSCSI Transport template
4239e84790SAlex Aizman  *
4339e84790SAlex Aizman  * @name:		transport name
4439e84790SAlex Aizman  * @caps:		iSCSI Data-Path capabilities
4539e84790SAlex Aizman  * @create_session:	create new iSCSI session object
4639e84790SAlex Aizman  * @destroy_session:	destroy existing iSCSI session object
4739e84790SAlex Aizman  * @create_conn:	create new iSCSI connection
4839e84790SAlex Aizman  * @bind_conn:		associate this connection with existing iSCSI session
4939e84790SAlex Aizman  *			and specified transport descriptor
5039e84790SAlex Aizman  * @destroy_conn:	destroy inactive iSCSI connection
51a54a52caSMike Christie  * @set_param:		set iSCSI parameter. Return 0 on success, -ENODATA
52a54a52caSMike Christie  *			when param is not supported, and a -Exx value on other
53a54a52caSMike Christie  *			error.
54a54a52caSMike Christie  * @get_param		get iSCSI parameter. Must return number of bytes
55a54a52caSMike Christie  *			copied to buffer on success, -ENODATA when param
56a54a52caSMike Christie  *			is not supported, and a -Exx value on other error
5739e84790SAlex Aizman  * @start_conn:		set connection to be operational
5839e84790SAlex Aizman  * @stop_conn:		suspend/recover/terminate connection
5939e84790SAlex Aizman  * @send_pdu:		send iSCSI PDU, Login, Logout, NOP-Out, Reject, Text.
607996a778SMike Christie  * @session_recovery_timedout: notify LLD a block during recovery timed out
617996a778SMike Christie  * @init_cmd_task:	Initialize a iscsi_cmd_task and any internal structs.
627996a778SMike Christie  *			Called from queuecommand with session lock held.
637996a778SMike Christie  * @init_mgmt_task:	Initialize a iscsi_mgmt_task and any internal structs.
647996a778SMike Christie  *			Called from iscsi_conn_send_generic with xmitmutex.
653219e529SMike Christie  * @xmit_cmd_task:	Requests LLD to transfer cmd task. Returns 0 or the
663219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
673219e529SMike Christie  *			value on error.
683219e529SMike Christie  * @xmit_mgmt_task:	Requests LLD to transfer mgmt task. Returns 0 or the
693219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
703219e529SMike Christie  *			value on error.
717996a778SMike Christie  * @cleanup_cmd_task:	requests LLD to fail cmd task. Called with xmitmutex
727996a778SMike Christie  *			and session->lock after the connection has been
737996a778SMike Christie  *			suspended and terminated during recovery. If called
747996a778SMike Christie  *			from abort task then connection is not suspended
757996a778SMike Christie  *			or terminated but sk_callback_lock is held
7639e84790SAlex Aizman  *
7739e84790SAlex Aizman  * Template API provided by iSCSI Transport
7839e84790SAlex Aizman  */
7939e84790SAlex Aizman struct iscsi_transport {
8039e84790SAlex Aizman 	struct module *owner;
8139e84790SAlex Aizman 	char *name;
8239e84790SAlex Aizman 	unsigned int caps;
83fd7255f5SMike Christie 	/* LLD sets this to indicate what values it can export to sysfs */
841819dc81SMike Christie 	uint64_t param_mask;
851819dc81SMike Christie 	uint64_t host_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,
951548271eSMike Christie 		struct scsi_transport_template *t, uint16_t, uint16_t,
961548271eSMike Christie 		uint32_t sn, uint32_t *hn);
977b7232f3SMike Christie 	void (*destroy_session) (struct iscsi_cls_session *session);
987b7232f3SMike Christie 	struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess,
997b8631b5SMike Christie 				uint32_t cid);
1007b7232f3SMike Christie 	int (*bind_conn) (struct iscsi_cls_session *session,
1017b7232f3SMike Christie 			  struct iscsi_cls_conn *cls_conn,
102264faaaaSOr Gerlitz 			  uint64_t transport_eph, int is_leading);
1037b7232f3SMike Christie 	int (*start_conn) (struct iscsi_cls_conn *conn);
1047b7232f3SMike Christie 	void (*stop_conn) (struct iscsi_cls_conn *conn, int flag);
1057b8631b5SMike Christie 	void (*destroy_conn) (struct iscsi_cls_conn *conn);
1067b7232f3SMike Christie 	int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param,
107a54a52caSMike Christie 			  char *buf, int buflen);
1087b7232f3SMike Christie 	int (*get_conn_param) (struct iscsi_cls_conn *conn,
109fd7255f5SMike Christie 			       enum iscsi_param param, char *buf);
110a54a52caSMike Christie 	int (*get_session_param) (struct iscsi_cls_session *session,
111fd7255f5SMike Christie 				  enum iscsi_param param, char *buf);
1121819dc81SMike Christie 	int (*get_host_param) (struct Scsi_Host *shost,
1131819dc81SMike Christie 				enum iscsi_host_param param, char *buf);
1141d9bf13aSMike Christie 	int (*set_host_param) (struct Scsi_Host *shost,
1151d9bf13aSMike Christie 			       enum iscsi_host_param param, char *buf,
1161d9bf13aSMike Christie 			       int buflen);
1177b7232f3SMike Christie 	int (*send_pdu) (struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
11839e84790SAlex Aizman 			 char *data, uint32_t data_size);
1197b7232f3SMike Christie 	void (*get_stats) (struct iscsi_cls_conn *conn,
1207b7232f3SMike Christie 			   struct iscsi_stats *stats);
1217996a778SMike Christie 	void (*init_cmd_task) (struct iscsi_cmd_task *ctask);
1227996a778SMike Christie 	void (*init_mgmt_task) (struct iscsi_conn *conn,
12377a23c21SMike Christie 				struct iscsi_mgmt_task *mtask);
1247996a778SMike Christie 	int (*xmit_cmd_task) (struct iscsi_conn *conn,
1257996a778SMike Christie 			      struct iscsi_cmd_task *ctask);
1267996a778SMike Christie 	void (*cleanup_cmd_task) (struct iscsi_conn *conn,
1277996a778SMike Christie 				  struct iscsi_cmd_task *ctask);
1287996a778SMike Christie 	int (*xmit_mgmt_task) (struct iscsi_conn *conn,
1297996a778SMike Christie 			       struct iscsi_mgmt_task *mtask);
13030a6c652SMike Christie 	void (*session_recovery_timedout) (struct iscsi_cls_session *session);
131264faaaaSOr Gerlitz 	int (*ep_connect) (struct sockaddr *dst_addr, int non_blocking,
132264faaaaSOr Gerlitz 			   uint64_t *ep_handle);
133264faaaaSOr Gerlitz 	int (*ep_poll) (uint64_t ep_handle, int timeout_ms);
134264faaaaSOr Gerlitz 	void (*ep_disconnect) (uint64_t ep_handle);
1352174a04eSMike Christie 	int (*tgt_dscvr) (struct Scsi_Host *shost, enum iscsi_tgt_dscvr type,
13601cb225dSMike Christie 			  uint32_t enable, struct sockaddr *dst_addr);
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
14039e84790SAlex Aizman  * transport registration upcalls
1411da177e4SLinus Torvalds  */
1427b8631b5SMike Christie extern struct scsi_transport_template *iscsi_register_transport(struct iscsi_transport *tt);
14339e84790SAlex Aizman extern int iscsi_unregister_transport(struct iscsi_transport *tt);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
14639e84790SAlex Aizman  * control plane upcalls
1471da177e4SLinus Torvalds  */
1487b7232f3SMike Christie extern void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error);
1497b7232f3SMike Christie extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
15039e84790SAlex Aizman 			  char *data, uint32_t data_size);
1511da177e4SLinus Torvalds 
1527996a778SMike Christie 
1537996a778SMike Christie /* Connection's states */
1547996a778SMike Christie #define ISCSI_CONN_INITIAL_STAGE	0
1557996a778SMike Christie #define ISCSI_CONN_STARTED		1
1567996a778SMike Christie #define ISCSI_CONN_STOPPED		2
1577996a778SMike Christie #define ISCSI_CONN_CLEANUP_WAIT		3
1587996a778SMike Christie 
1597b8631b5SMike Christie struct iscsi_cls_conn {
1607b8631b5SMike Christie 	struct list_head conn_list;	/* item in connlist */
1617b8631b5SMike Christie 	void *dd_data;			/* LLD private data */
1627b8631b5SMike Christie 	struct iscsi_transport *transport;
163b5c7a12dSMike Christie 	uint32_t cid;			/* connection id */
164fd7255f5SMike Christie 
1657b8631b5SMike Christie 	int active;			/* must be accessed with the connlock */
1667b8631b5SMike Christie 	struct device dev;		/* sysfs transport/container device */
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
179b3a7ea8dSMike Christie #define ISCSI_STATE_LOGGING_OUT		7
1807996a778SMike Christie 
1817b8631b5SMike Christie struct iscsi_cls_session {
1827b7232f3SMike Christie 	struct list_head sess_list;		/* item in session_list */
18330a6c652SMike Christie 	struct list_head host_list;
1847b8631b5SMike Christie 	struct iscsi_transport *transport;
185fd7255f5SMike Christie 
18630a6c652SMike Christie 	/* recovery fields */
18730a6c652SMike Christie 	int recovery_tmo;
188c4028958SDavid Howells 	struct delayed_work recovery_work;
18926974789SMike Christie 	struct work_struct unbind_work;
19030a6c652SMike Christie 
19130a6c652SMike Christie 	int target_id;
19230a6c652SMike Christie 
193b5c7a12dSMike Christie 	int sid;				/* session id */
194b5c7a12dSMike Christie 	void *dd_data;				/* LLD private data */
1957b8631b5SMike Christie 	struct device dev;	/* sysfs transport/container device */
1967b8631b5SMike Christie };
1977b8631b5SMike Christie 
1987b8631b5SMike Christie #define iscsi_dev_to_session(_dev) \
1997b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_session, dev)
2007b8631b5SMike Christie 
2017b8631b5SMike Christie #define iscsi_session_to_shost(_session) \
2027b8631b5SMike Christie 	dev_to_shost(_session->dev.parent)
2037b8631b5SMike Christie 
2048434aa8bSMike Christie #define starget_to_session(_stgt) \
2058434aa8bSMike Christie 	iscsi_dev_to_session(_stgt->dev.parent)
2068434aa8bSMike Christie 
20730a6c652SMike Christie struct iscsi_host {
20830a6c652SMike Christie 	struct list_head sessions;
20930a6c652SMike Christie 	struct mutex mutex;
21026974789SMike Christie 	struct workqueue_struct *unbind_workq;
21126974789SMike Christie 	char unbind_workq_name[KOBJ_NAME_LEN];
21230a6c652SMike Christie };
21330a6c652SMike Christie 
2147b8631b5SMike Christie /*
2157b8631b5SMike Christie  * session and connection functions that can be used by HW iSCSI LLDs
2167b8631b5SMike Christie  */
2178434aa8bSMike Christie extern struct iscsi_cls_session *iscsi_alloc_session(struct Scsi_Host *shost,
2188434aa8bSMike Christie 					struct iscsi_transport *transport);
2196a8a0d36SMike Christie extern int iscsi_add_session(struct iscsi_cls_session *session,
2206a8a0d36SMike Christie 			     unsigned int target_id);
22126974789SMike Christie extern int iscsi_session_event(struct iscsi_cls_session *session,
22226974789SMike Christie 			       enum iscsi_uevent_e event);
2237b8631b5SMike Christie extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost,
2246a8a0d36SMike Christie 						struct iscsi_transport *t,
2256a8a0d36SMike Christie 						unsigned int target_id);
2268434aa8bSMike Christie extern void iscsi_remove_session(struct iscsi_cls_session *session);
2278434aa8bSMike Christie extern void iscsi_free_session(struct iscsi_cls_session *session);
2287b8631b5SMike Christie extern int iscsi_destroy_session(struct iscsi_cls_session *session);
2297b8631b5SMike Christie extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess,
2307b8631b5SMike Christie 					    uint32_t cid);
2317b8631b5SMike Christie extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn);
23230a6c652SMike Christie extern void iscsi_unblock_session(struct iscsi_cls_session *session);
23330a6c652SMike Christie extern void iscsi_block_session(struct iscsi_cls_session *session);
2347b8631b5SMike Christie 
23553cb8a1fSMike Christie 
2361da177e4SLinus Torvalds #endif
237