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;
33d82ff9beSMike Christie struct iscsi_endpoint;
347b8631b5SMike Christie struct Scsi_Host;
357b8631b5SMike Christie struct iscsi_cls_conn;
367996a778SMike Christie struct iscsi_conn;
379c19a7d0SMike Christie struct iscsi_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
613e5c28adSMike Christie  * @init_task:		Initialize a iscsi_task and any internal structs.
623e5c28adSMike Christie  *			When offloading the data path, this is called from
633e5c28adSMike Christie  *			queuecommand with the session lock, or from the
643e5c28adSMike Christie  *			iscsi_conn_send_pdu context with the session lock.
653e5c28adSMike Christie  *			When not offloading the data path, this is called
663e5c28adSMike Christie  *			from the scsi work queue without the session lock.
673e5c28adSMike Christie  * @xmit_task		Requests LLD to transfer cmd task. Returns 0 or the
683219e529SMike Christie  *			the number of bytes transferred on success, and -Exyz
693e5c28adSMike Christie  *			value on error. When offloading the data path, this
703e5c28adSMike Christie  *			is called from queuecommand with the session lock, or
713e5c28adSMike Christie  *			from the iscsi_conn_send_pdu context with the session
723e5c28adSMike Christie  *			lock. When not offloading the data path, this is called
733e5c28adSMike Christie  *			from the scsi work queue without the session lock.
743e5c28adSMike Christie  * @cleanup_task:	requests LLD to fail task. Called with session lock
753e5c28adSMike Christie  *			and after the connection has been suspended and
763e5c28adSMike Christie  *			terminated during recovery. If called
777996a778SMike Christie  *			from abort task then connection is not suspended
787996a778SMike Christie  *			or terminated but sk_callback_lock is held
7939e84790SAlex Aizman  *
8039e84790SAlex Aizman  * Template API provided by iSCSI Transport
8139e84790SAlex Aizman  */
8239e84790SAlex Aizman struct iscsi_transport {
8339e84790SAlex Aizman 	struct module *owner;
8439e84790SAlex Aizman 	char *name;
8539e84790SAlex Aizman 	unsigned int caps;
86fd7255f5SMike Christie 	/* LLD sets this to indicate what values it can export to sysfs */
871819dc81SMike Christie 	uint64_t param_mask;
881819dc81SMike Christie 	uint64_t host_param_mask;
89d82ff9beSMike Christie 	struct iscsi_cls_session *(*create_session) (struct iscsi_endpoint *ep,
9075613521SMike Christie 					uint16_t cmds_max, uint16_t qdepth,
9175613521SMike Christie 					uint32_t sn, uint32_t *hn);
927b7232f3SMike Christie 	void (*destroy_session) (struct iscsi_cls_session *session);
937b7232f3SMike Christie 	struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess,
947b8631b5SMike Christie 				uint32_t cid);
957b7232f3SMike Christie 	int (*bind_conn) (struct iscsi_cls_session *session,
967b7232f3SMike Christie 			  struct iscsi_cls_conn *cls_conn,
97264faaaaSOr Gerlitz 			  uint64_t transport_eph, int is_leading);
987b7232f3SMike Christie 	int (*start_conn) (struct iscsi_cls_conn *conn);
997b7232f3SMike Christie 	void (*stop_conn) (struct iscsi_cls_conn *conn, int flag);
1007b8631b5SMike Christie 	void (*destroy_conn) (struct iscsi_cls_conn *conn);
1017b7232f3SMike Christie 	int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param,
102a54a52caSMike Christie 			  char *buf, int buflen);
1037b7232f3SMike Christie 	int (*get_conn_param) (struct iscsi_cls_conn *conn,
104fd7255f5SMike Christie 			       enum iscsi_param param, char *buf);
105a54a52caSMike Christie 	int (*get_session_param) (struct iscsi_cls_session *session,
106fd7255f5SMike Christie 				  enum iscsi_param param, char *buf);
1071819dc81SMike Christie 	int (*get_host_param) (struct Scsi_Host *shost,
1081819dc81SMike Christie 				enum iscsi_host_param param, char *buf);
1091d9bf13aSMike Christie 	int (*set_host_param) (struct Scsi_Host *shost,
1101d9bf13aSMike Christie 			       enum iscsi_host_param param, char *buf,
1111d9bf13aSMike Christie 			       int buflen);
1127b7232f3SMike Christie 	int (*send_pdu) (struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
11339e84790SAlex Aizman 			 char *data, uint32_t data_size);
1147b7232f3SMike Christie 	void (*get_stats) (struct iscsi_cls_conn *conn,
1157b7232f3SMike Christie 			   struct iscsi_stats *stats);
116577577daSMike Christie 
1179c19a7d0SMike Christie 	int (*init_task) (struct iscsi_task *task);
1189c19a7d0SMike Christie 	int (*xmit_task) (struct iscsi_task *task);
119577577daSMike Christie 	void (*cleanup_task) (struct iscsi_task *task);
120577577daSMike Christie 
1212ff79d52SMike Christie 	int (*alloc_pdu) (struct iscsi_task *task, uint8_t opcode);
122577577daSMike Christie 	int (*xmit_pdu) (struct iscsi_task *task);
123577577daSMike Christie 	int (*init_pdu) (struct iscsi_task *task, unsigned int offset,
124577577daSMike Christie 			 unsigned int count);
125262ef636SMike Christie 	void (*parse_pdu_itt) (struct iscsi_conn *conn, itt_t itt,
126262ef636SMike Christie 			       int *index, int *age);
127262ef636SMike Christie 
12830a6c652SMike Christie 	void (*session_recovery_timedout) (struct iscsi_cls_session *session);
129d82ff9beSMike Christie 	struct iscsi_endpoint *(*ep_connect) (struct sockaddr *dst_addr,
130d82ff9beSMike Christie 					      int non_blocking);
131d82ff9beSMike Christie 	int (*ep_poll) (struct iscsi_endpoint *ep, int timeout_ms);
132d82ff9beSMike Christie 	void (*ep_disconnect) (struct iscsi_endpoint *ep);
1332174a04eSMike Christie 	int (*tgt_dscvr) (struct Scsi_Host *shost, enum iscsi_tgt_dscvr type,
13401cb225dSMike Christie 			  uint32_t enable, struct sockaddr *dst_addr);
1351da177e4SLinus Torvalds };
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
13839e84790SAlex Aizman  * transport registration upcalls
1391da177e4SLinus Torvalds  */
1407b8631b5SMike Christie extern struct scsi_transport_template *iscsi_register_transport(struct iscsi_transport *tt);
14139e84790SAlex Aizman extern int iscsi_unregister_transport(struct iscsi_transport *tt);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds /*
14439e84790SAlex Aizman  * control plane upcalls
1451da177e4SLinus Torvalds  */
146e5bd7b54SMike Christie extern void iscsi_conn_error_event(struct iscsi_cls_conn *conn,
147e5bd7b54SMike Christie 				   enum iscsi_err error);
1487b7232f3SMike Christie extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
14939e84790SAlex Aizman 			  char *data, uint32_t data_size);
1501da177e4SLinus Torvalds 
1517b8631b5SMike Christie struct iscsi_cls_conn {
1527b8631b5SMike Christie 	struct list_head conn_list;	/* item in connlist */
1537b8631b5SMike Christie 	void *dd_data;			/* LLD private data */
1547b8631b5SMike Christie 	struct iscsi_transport *transport;
155b5c7a12dSMike Christie 	uint32_t cid;			/* connection id */
156fd7255f5SMike Christie 
1577b8631b5SMike Christie 	int active;			/* must be accessed with the connlock */
1587b8631b5SMike Christie 	struct device dev;		/* sysfs transport/container device */
1597b8631b5SMike Christie };
1607b8631b5SMike Christie 
1617b8631b5SMike Christie #define iscsi_dev_to_conn(_dev) \
1627b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_conn, dev)
1637b8631b5SMike Christie 
1646eabafbeSMike Christie #define iscsi_conn_to_session(_conn) \
1656eabafbeSMike Christie 	iscsi_dev_to_session(_conn->dev.parent)
1666eabafbeSMike Christie 
1676eabafbeSMike Christie /* iscsi class session state */
1686eabafbeSMike Christie enum {
1696eabafbeSMike Christie 	ISCSI_SESSION_LOGGED_IN,
1706eabafbeSMike Christie 	ISCSI_SESSION_FAILED,
1716eabafbeSMike Christie 	ISCSI_SESSION_FREE,
1726eabafbeSMike Christie };
1737996a778SMike Christie 
1747970634bSMike Christie #define ISCSI_MAX_TARGET -1
1757970634bSMike Christie 
1767b8631b5SMike Christie struct iscsi_cls_session {
1777b7232f3SMike Christie 	struct list_head sess_list;		/* item in session_list */
1787b8631b5SMike Christie 	struct iscsi_transport *transport;
1796eabafbeSMike Christie 	spinlock_t lock;
18045ab33b6SMike Christie 	struct work_struct block_work;
18145ab33b6SMike Christie 	struct work_struct unblock_work;
182bd976f62SMike Christie 	struct work_struct scan_work;
183bd976f62SMike Christie 	struct work_struct unbind_work;
184fd7255f5SMike Christie 
18530a6c652SMike Christie 	/* recovery fields */
18630a6c652SMike Christie 	int recovery_tmo;
187c4028958SDavid Howells 	struct delayed_work recovery_work;
18830a6c652SMike Christie 
1897970634bSMike Christie 	unsigned int target_id;
19030a6c652SMike Christie 
1916eabafbeSMike Christie 	int state;
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 
2038434aa8bSMike Christie #define starget_to_session(_stgt) \
2048434aa8bSMike Christie 	iscsi_dev_to_session(_stgt->dev.parent)
2058434aa8bSMike Christie 
20632c6e1b9SMike Christie struct iscsi_cls_host {
2078aae18adSMike Christie 	atomic_t nr_scans;
20830a6c652SMike Christie 	struct mutex mutex;
209bd976f62SMike Christie 	struct workqueue_struct *scan_workq;
210aab0de24SKay Sievers 	char scan_workq_name[20];
21130a6c652SMike Christie };
21230a6c652SMike Christie 
213a4804cd6SMike Christie extern void iscsi_host_for_each_session(struct Scsi_Host *shost,
214a4804cd6SMike Christie 				void (*fn)(struct iscsi_cls_session *));
215a4804cd6SMike Christie 
216d82ff9beSMike Christie struct iscsi_endpoint {
217d82ff9beSMike Christie 	void *dd_data;			/* LLD private data */
218d82ff9beSMike Christie 	struct device dev;
21921536062SMike Christie 	uint64_t id;
220d82ff9beSMike Christie };
221a4804cd6SMike Christie 
2227b8631b5SMike Christie /*
2237b8631b5SMike Christie  * session and connection functions that can be used by HW iSCSI LLDs
2247b8631b5SMike Christie  */
225322d739dSMike Christie #define iscsi_cls_session_printk(prefix, _cls_session, fmt, a...) \
226322d739dSMike Christie 	dev_printk(prefix, &(_cls_session)->dev, fmt, ##a)
227322d739dSMike Christie 
228322d739dSMike Christie #define iscsi_cls_conn_printk(prefix, _cls_conn, fmt, a...) \
229322d739dSMike Christie 	dev_printk(prefix, &(_cls_conn)->dev, fmt, ##a)
230322d739dSMike Christie 
2316eabafbeSMike Christie extern int iscsi_session_chkready(struct iscsi_cls_session *session);
2328434aa8bSMike Christie extern struct iscsi_cls_session *iscsi_alloc_session(struct Scsi_Host *shost,
2335d91e209SMike Christie 				struct iscsi_transport *transport, int dd_size);
2346a8a0d36SMike Christie extern int iscsi_add_session(struct iscsi_cls_session *session,
2356a8a0d36SMike Christie 			     unsigned int target_id);
23626974789SMike Christie extern int iscsi_session_event(struct iscsi_cls_session *session,
23726974789SMike Christie 			       enum iscsi_uevent_e event);
2387b8631b5SMike Christie extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost,
2396a8a0d36SMike Christie 						struct iscsi_transport *t,
2405d91e209SMike Christie 						int dd_size,
2416a8a0d36SMike Christie 						unsigned int target_id);
2428434aa8bSMike Christie extern void iscsi_remove_session(struct iscsi_cls_session *session);
2438434aa8bSMike Christie extern void iscsi_free_session(struct iscsi_cls_session *session);
2447b8631b5SMike Christie extern int iscsi_destroy_session(struct iscsi_cls_session *session);
2457b8631b5SMike Christie extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess,
2465d91e209SMike Christie 						int dd_size, uint32_t cid);
2477b8631b5SMike Christie extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn);
24830a6c652SMike Christie extern void iscsi_unblock_session(struct iscsi_cls_session *session);
24930a6c652SMike Christie extern void iscsi_block_session(struct iscsi_cls_session *session);
2508aae18adSMike Christie extern int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time);
251d82ff9beSMike Christie extern struct iscsi_endpoint *iscsi_create_endpoint(int dd_size);
252d82ff9beSMike Christie extern void iscsi_destroy_endpoint(struct iscsi_endpoint *ep);
253d82ff9beSMike Christie extern struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle);
25453cb8a1fSMike Christie 
2551da177e4SLinus Torvalds #endif
256