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; 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 603e5c28adSMike Christie * @init_task: Initialize a iscsi_task and any internal structs. 613e5c28adSMike Christie * When offloading the data path, this is called from 623e5c28adSMike Christie * queuecommand with the session lock, or from the 633e5c28adSMike Christie * iscsi_conn_send_pdu context with the session lock. 643e5c28adSMike Christie * When not offloading the data path, this is called 653e5c28adSMike Christie * from the scsi work queue without the session lock. 663e5c28adSMike Christie * @xmit_task Requests LLD to transfer cmd task. Returns 0 or the 673219e529SMike Christie * the number of bytes transferred on success, and -Exyz 683e5c28adSMike Christie * value on error. When offloading the data path, this 693e5c28adSMike Christie * is called from queuecommand with the session lock, or 703e5c28adSMike Christie * from the iscsi_conn_send_pdu context with the session 713e5c28adSMike Christie * lock. When not offloading the data path, this is called 723e5c28adSMike Christie * from the scsi work queue without the session lock. 733e5c28adSMike Christie * @cleanup_task: requests LLD to fail task. Called with session lock 743e5c28adSMike Christie * and after the connection has been suspended and 753e5c28adSMike Christie * terminated during recovery. If called 767996a778SMike Christie * from abort task then connection is not suspended 777996a778SMike Christie * or terminated but sk_callback_lock is held 7839e84790SAlex Aizman * 7939e84790SAlex Aizman * Template API provided by iSCSI Transport 8039e84790SAlex Aizman */ 8139e84790SAlex Aizman struct iscsi_transport { 8239e84790SAlex Aizman struct module *owner; 8339e84790SAlex Aizman char *name; 8439e84790SAlex Aizman unsigned int caps; 85fd7255f5SMike Christie /* LLD sets this to indicate what values it can export to sysfs */ 861819dc81SMike Christie uint64_t param_mask; 871819dc81SMike Christie uint64_t host_param_mask; 8875613521SMike Christie struct iscsi_cls_session *(*create_session) (struct Scsi_Host *shost, 8975613521SMike Christie uint16_t cmds_max, uint16_t qdepth, 9075613521SMike Christie uint32_t sn, uint32_t *hn); 917b7232f3SMike Christie void (*destroy_session) (struct iscsi_cls_session *session); 927b7232f3SMike Christie struct iscsi_cls_conn *(*create_conn) (struct iscsi_cls_session *sess, 937b8631b5SMike Christie uint32_t cid); 947b7232f3SMike Christie int (*bind_conn) (struct iscsi_cls_session *session, 957b7232f3SMike Christie struct iscsi_cls_conn *cls_conn, 96264faaaaSOr Gerlitz uint64_t transport_eph, int is_leading); 977b7232f3SMike Christie int (*start_conn) (struct iscsi_cls_conn *conn); 987b7232f3SMike Christie void (*stop_conn) (struct iscsi_cls_conn *conn, int flag); 997b8631b5SMike Christie void (*destroy_conn) (struct iscsi_cls_conn *conn); 1007b7232f3SMike Christie int (*set_param) (struct iscsi_cls_conn *conn, enum iscsi_param param, 101a54a52caSMike Christie char *buf, int buflen); 1027b7232f3SMike Christie int (*get_conn_param) (struct iscsi_cls_conn *conn, 103fd7255f5SMike Christie enum iscsi_param param, char *buf); 104a54a52caSMike Christie int (*get_session_param) (struct iscsi_cls_session *session, 105fd7255f5SMike Christie enum iscsi_param param, char *buf); 1061819dc81SMike Christie int (*get_host_param) (struct Scsi_Host *shost, 1071819dc81SMike Christie enum iscsi_host_param param, char *buf); 1081d9bf13aSMike Christie int (*set_host_param) (struct Scsi_Host *shost, 1091d9bf13aSMike Christie enum iscsi_host_param param, char *buf, 1101d9bf13aSMike Christie int buflen); 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); 1153e5c28adSMike Christie int (*init_task) (struct iscsi_cmd_task *task); 1163e5c28adSMike Christie int (*xmit_task) (struct iscsi_cmd_task *task); 1173e5c28adSMike Christie void (*cleanup_task) (struct iscsi_conn *conn, 1183e5c28adSMike Christie struct iscsi_cmd_task *task); 11930a6c652SMike Christie void (*session_recovery_timedout) (struct iscsi_cls_session *session); 120264faaaaSOr Gerlitz int (*ep_connect) (struct sockaddr *dst_addr, int non_blocking, 121264faaaaSOr Gerlitz uint64_t *ep_handle); 122264faaaaSOr Gerlitz int (*ep_poll) (uint64_t ep_handle, int timeout_ms); 123264faaaaSOr Gerlitz void (*ep_disconnect) (uint64_t ep_handle); 1242174a04eSMike Christie int (*tgt_dscvr) (struct Scsi_Host *shost, enum iscsi_tgt_dscvr type, 12501cb225dSMike Christie uint32_t enable, struct sockaddr *dst_addr); 1261da177e4SLinus Torvalds }; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 12939e84790SAlex Aizman * transport registration upcalls 1301da177e4SLinus Torvalds */ 1317b8631b5SMike Christie extern struct scsi_transport_template *iscsi_register_transport(struct iscsi_transport *tt); 13239e84790SAlex Aizman extern int iscsi_unregister_transport(struct iscsi_transport *tt); 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 13539e84790SAlex Aizman * control plane upcalls 1361da177e4SLinus Torvalds */ 1377b7232f3SMike Christie extern void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error); 1387b7232f3SMike Christie extern int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr, 13939e84790SAlex Aizman char *data, uint32_t data_size); 1401da177e4SLinus Torvalds 1417b8631b5SMike Christie struct iscsi_cls_conn { 1427b8631b5SMike Christie struct list_head conn_list; /* item in connlist */ 1437b8631b5SMike Christie void *dd_data; /* LLD private data */ 1447b8631b5SMike Christie struct iscsi_transport *transport; 145b5c7a12dSMike Christie uint32_t cid; /* connection id */ 146fd7255f5SMike Christie 1477b8631b5SMike Christie int active; /* must be accessed with the connlock */ 1487b8631b5SMike Christie struct device dev; /* sysfs transport/container device */ 1497b8631b5SMike Christie }; 1507b8631b5SMike Christie 1517b8631b5SMike Christie #define iscsi_dev_to_conn(_dev) \ 1527b8631b5SMike Christie container_of(_dev, struct iscsi_cls_conn, dev) 1537b8631b5SMike Christie 1546eabafbeSMike Christie #define iscsi_conn_to_session(_conn) \ 1556eabafbeSMike Christie iscsi_dev_to_session(_conn->dev.parent) 1566eabafbeSMike Christie 1576eabafbeSMike Christie /* iscsi class session state */ 1586eabafbeSMike Christie enum { 1596eabafbeSMike Christie ISCSI_SESSION_LOGGED_IN, 1606eabafbeSMike Christie ISCSI_SESSION_FAILED, 1616eabafbeSMike Christie ISCSI_SESSION_FREE, 1626eabafbeSMike Christie }; 1637996a778SMike Christie 1647b8631b5SMike Christie struct iscsi_cls_session { 1657b7232f3SMike Christie struct list_head sess_list; /* item in session_list */ 16630a6c652SMike Christie struct list_head host_list; 1677b8631b5SMike Christie struct iscsi_transport *transport; 1686eabafbeSMike Christie spinlock_t lock; 16945ab33b6SMike Christie struct work_struct block_work; 17045ab33b6SMike Christie struct work_struct unblock_work; 171bd976f62SMike Christie struct work_struct scan_work; 172bd976f62SMike Christie struct work_struct unbind_work; 173fd7255f5SMike Christie 17430a6c652SMike Christie /* recovery fields */ 17530a6c652SMike Christie int recovery_tmo; 176c4028958SDavid Howells struct delayed_work recovery_work; 17730a6c652SMike Christie 17830a6c652SMike Christie int target_id; 17930a6c652SMike Christie 1806eabafbeSMike Christie int state; 181b5c7a12dSMike Christie int sid; /* session id */ 182b5c7a12dSMike Christie void *dd_data; /* LLD private data */ 1837b8631b5SMike Christie struct device dev; /* sysfs transport/container device */ 1847b8631b5SMike Christie }; 1857b8631b5SMike Christie 1867b8631b5SMike Christie #define iscsi_dev_to_session(_dev) \ 1877b8631b5SMike Christie container_of(_dev, struct iscsi_cls_session, dev) 1887b8631b5SMike Christie 1897b8631b5SMike Christie #define iscsi_session_to_shost(_session) \ 1907b8631b5SMike Christie dev_to_shost(_session->dev.parent) 1917b8631b5SMike Christie 1928434aa8bSMike Christie #define starget_to_session(_stgt) \ 1938434aa8bSMike Christie iscsi_dev_to_session(_stgt->dev.parent) 1948434aa8bSMike Christie 19532c6e1b9SMike Christie struct iscsi_cls_host { 19630a6c652SMike Christie struct list_head sessions; 1978aae18adSMike Christie atomic_t nr_scans; 19830a6c652SMike Christie struct mutex mutex; 199bd976f62SMike Christie struct workqueue_struct *scan_workq; 200bd976f62SMike Christie char scan_workq_name[KOBJ_NAME_LEN]; 20130a6c652SMike Christie }; 20230a6c652SMike Christie 203a4804cd6SMike Christie extern void iscsi_host_for_each_session(struct Scsi_Host *shost, 204a4804cd6SMike Christie void (*fn)(struct iscsi_cls_session *)); 205a4804cd6SMike Christie 206a4804cd6SMike Christie 2077b8631b5SMike Christie /* 2087b8631b5SMike Christie * session and connection functions that can be used by HW iSCSI LLDs 2097b8631b5SMike Christie */ 210322d739dSMike Christie #define iscsi_cls_session_printk(prefix, _cls_session, fmt, a...) \ 211322d739dSMike Christie dev_printk(prefix, &(_cls_session)->dev, fmt, ##a) 212322d739dSMike Christie 213322d739dSMike Christie #define iscsi_cls_conn_printk(prefix, _cls_conn, fmt, a...) \ 214322d739dSMike Christie dev_printk(prefix, &(_cls_conn)->dev, fmt, ##a) 215322d739dSMike Christie 2166eabafbeSMike Christie extern int iscsi_session_chkready(struct iscsi_cls_session *session); 2178434aa8bSMike Christie extern struct iscsi_cls_session *iscsi_alloc_session(struct Scsi_Host *shost, 2185d91e209SMike Christie struct iscsi_transport *transport, int dd_size); 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, 2255d91e209SMike Christie int dd_size, 2266a8a0d36SMike Christie unsigned int target_id); 2278434aa8bSMike Christie extern void iscsi_remove_session(struct iscsi_cls_session *session); 2288434aa8bSMike Christie extern void iscsi_free_session(struct iscsi_cls_session *session); 2297b8631b5SMike Christie extern int iscsi_destroy_session(struct iscsi_cls_session *session); 2307b8631b5SMike Christie extern struct iscsi_cls_conn *iscsi_create_conn(struct iscsi_cls_session *sess, 2315d91e209SMike Christie int dd_size, uint32_t cid); 2327b8631b5SMike Christie extern int iscsi_destroy_conn(struct iscsi_cls_conn *conn); 23330a6c652SMike Christie extern void iscsi_unblock_session(struct iscsi_cls_session *session); 23430a6c652SMike Christie extern void iscsi_block_session(struct iscsi_cls_session *session); 2358aae18adSMike Christie extern int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time); 23653cb8a1fSMike Christie 2371da177e4SLinus Torvalds #endif 238