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);
1169c19a7d0SMike Christie 	int (*init_task) (struct iscsi_task *task);
1179c19a7d0SMike Christie 	int (*xmit_task) (struct iscsi_task *task);
1183e5c28adSMike Christie 	void (*cleanup_task) (struct iscsi_conn *conn,
1199c19a7d0SMike Christie 				  struct iscsi_task *task);
12030a6c652SMike Christie 	void (*session_recovery_timedout) (struct iscsi_cls_session *session);
121d82ff9beSMike Christie 	struct iscsi_endpoint *(*ep_connect) (struct sockaddr *dst_addr,
122d82ff9beSMike Christie 					      int non_blocking);
123d82ff9beSMike Christie 	int (*ep_poll) (struct iscsi_endpoint *ep, int timeout_ms);
124d82ff9beSMike Christie 	void (*ep_disconnect) (struct iscsi_endpoint *ep);
1252174a04eSMike Christie 	int (*tgt_dscvr) (struct Scsi_Host *shost, enum iscsi_tgt_dscvr type,
12601cb225dSMike Christie 			  uint32_t enable, struct sockaddr *dst_addr);
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
13039e84790SAlex Aizman  * transport registration upcalls
1311da177e4SLinus Torvalds  */
1327b8631b5SMike Christie extern struct scsi_transport_template *iscsi_register_transport(struct iscsi_transport *tt);
13339e84790SAlex Aizman extern int iscsi_unregister_transport(struct iscsi_transport *tt);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
13639e84790SAlex Aizman  * control plane upcalls
1371da177e4SLinus Torvalds  */
1387b7232f3SMike Christie extern void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error);
1397b7232f3SMike Christie extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
14039e84790SAlex Aizman 			  char *data, uint32_t data_size);
1411da177e4SLinus Torvalds 
1427b8631b5SMike Christie struct iscsi_cls_conn {
1437b8631b5SMike Christie 	struct list_head conn_list;	/* item in connlist */
1447b8631b5SMike Christie 	void *dd_data;			/* LLD private data */
1457b8631b5SMike Christie 	struct iscsi_transport *transport;
146b5c7a12dSMike Christie 	uint32_t cid;			/* connection id */
147fd7255f5SMike Christie 
1487b8631b5SMike Christie 	int active;			/* must be accessed with the connlock */
1497b8631b5SMike Christie 	struct device dev;		/* sysfs transport/container device */
1507b8631b5SMike Christie };
1517b8631b5SMike Christie 
1527b8631b5SMike Christie #define iscsi_dev_to_conn(_dev) \
1537b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_conn, dev)
1547b8631b5SMike Christie 
1556eabafbeSMike Christie #define iscsi_conn_to_session(_conn) \
1566eabafbeSMike Christie 	iscsi_dev_to_session(_conn->dev.parent)
1576eabafbeSMike Christie 
1586eabafbeSMike Christie /* iscsi class session state */
1596eabafbeSMike Christie enum {
1606eabafbeSMike Christie 	ISCSI_SESSION_LOGGED_IN,
1616eabafbeSMike Christie 	ISCSI_SESSION_FAILED,
1626eabafbeSMike Christie 	ISCSI_SESSION_FREE,
1636eabafbeSMike Christie };
1647996a778SMike Christie 
1657970634bSMike Christie #define ISCSI_MAX_TARGET -1
1667970634bSMike Christie 
1677b8631b5SMike Christie struct iscsi_cls_session {
1687b7232f3SMike Christie 	struct list_head sess_list;		/* item in session_list */
1697b8631b5SMike Christie 	struct iscsi_transport *transport;
1706eabafbeSMike Christie 	spinlock_t lock;
17145ab33b6SMike Christie 	struct work_struct block_work;
17245ab33b6SMike Christie 	struct work_struct unblock_work;
173bd976f62SMike Christie 	struct work_struct scan_work;
174bd976f62SMike Christie 	struct work_struct unbind_work;
175fd7255f5SMike Christie 
17630a6c652SMike Christie 	/* recovery fields */
17730a6c652SMike Christie 	int recovery_tmo;
178c4028958SDavid Howells 	struct delayed_work recovery_work;
17930a6c652SMike Christie 
1807970634bSMike Christie 	unsigned int target_id;
18130a6c652SMike Christie 
1826eabafbeSMike Christie 	int state;
183b5c7a12dSMike Christie 	int sid;				/* session id */
184b5c7a12dSMike Christie 	void *dd_data;				/* LLD private data */
1857b8631b5SMike Christie 	struct device dev;	/* sysfs transport/container device */
1867b8631b5SMike Christie };
1877b8631b5SMike Christie 
1887b8631b5SMike Christie #define iscsi_dev_to_session(_dev) \
1897b8631b5SMike Christie 	container_of(_dev, struct iscsi_cls_session, dev)
1907b8631b5SMike Christie 
1917b8631b5SMike Christie #define iscsi_session_to_shost(_session) \
1927b8631b5SMike Christie 	dev_to_shost(_session->dev.parent)
1937b8631b5SMike Christie 
1948434aa8bSMike Christie #define starget_to_session(_stgt) \
1958434aa8bSMike Christie 	iscsi_dev_to_session(_stgt->dev.parent)
1968434aa8bSMike Christie 
19732c6e1b9SMike Christie struct iscsi_cls_host {
1988aae18adSMike Christie 	atomic_t nr_scans;
19930a6c652SMike Christie 	struct mutex mutex;
200bd976f62SMike Christie 	struct workqueue_struct *scan_workq;
201bd976f62SMike Christie 	char scan_workq_name[KOBJ_NAME_LEN];
20230a6c652SMike Christie };
20330a6c652SMike Christie 
204a4804cd6SMike Christie extern void iscsi_host_for_each_session(struct Scsi_Host *shost,
205a4804cd6SMike Christie 				void (*fn)(struct iscsi_cls_session *));
206a4804cd6SMike Christie 
207d82ff9beSMike Christie struct iscsi_endpoint {
208d82ff9beSMike Christie 	void *dd_data;			/* LLD private data */
209d82ff9beSMike Christie 	struct device dev;
210d82ff9beSMike Christie 	unsigned int id;
211d82ff9beSMike Christie };
212a4804cd6SMike Christie 
2137b8631b5SMike Christie /*
2147b8631b5SMike Christie  * session and connection functions that can be used by HW iSCSI LLDs
2157b8631b5SMike Christie  */
216322d739dSMike Christie #define iscsi_cls_session_printk(prefix, _cls_session, fmt, a...) \
217322d739dSMike Christie 	dev_printk(prefix, &(_cls_session)->dev, fmt, ##a)
218322d739dSMike Christie 
219322d739dSMike Christie #define iscsi_cls_conn_printk(prefix, _cls_conn, fmt, a...) \
220322d739dSMike Christie 	dev_printk(prefix, &(_cls_conn)->dev, fmt, ##a)
221322d739dSMike Christie 
2226eabafbeSMike Christie extern int iscsi_session_chkready(struct iscsi_cls_session *session);
2238434aa8bSMike Christie extern struct iscsi_cls_session *iscsi_alloc_session(struct Scsi_Host *shost,
2245d91e209SMike Christie 				struct iscsi_transport *transport, int dd_size);
2256a8a0d36SMike Christie extern int iscsi_add_session(struct iscsi_cls_session *session,
2266a8a0d36SMike Christie 			     unsigned int target_id);
22726974789SMike Christie extern int iscsi_session_event(struct iscsi_cls_session *session,
22826974789SMike Christie 			       enum iscsi_uevent_e event);
2297b8631b5SMike Christie extern struct iscsi_cls_session *iscsi_create_session(struct Scsi_Host *shost,
2306a8a0d36SMike Christie 						struct iscsi_transport *t,
2315d91e209SMike Christie 						int dd_size,
2326a8a0d36SMike Christie 						unsigned int target_id);
2338434aa8bSMike Christie extern void iscsi_remove_session(struct iscsi_cls_session *session);
2348434aa8bSMike Christie extern void iscsi_free_session(struct iscsi_cls_session *session);
2357b8631b5SMike Christie extern int iscsi_destroy_session(struct iscsi_cls_session *session);
2367b8631b5SMike Christie extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess,
2375d91e209SMike Christie 						int dd_size, uint32_t cid);
2387b8631b5SMike Christie extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn);
23930a6c652SMike Christie extern void iscsi_unblock_session(struct iscsi_cls_session *session);
24030a6c652SMike Christie extern void iscsi_block_session(struct iscsi_cls_session *session);
2418aae18adSMike Christie extern int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time);
242d82ff9beSMike Christie extern struct iscsi_endpoint *iscsi_create_endpoint(int dd_size);
243d82ff9beSMike Christie extern void iscsi_destroy_endpoint(struct iscsi_endpoint *ep);
244d82ff9beSMike Christie extern struct iscsi_endpoint *iscsi_lookup_endpoint(u64 handle);
24553cb8a1fSMike Christie 
2461da177e4SLinus Torvalds #endif
247