1aef9ec39SRoland Dreier /*
2aef9ec39SRoland Dreier  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
3aef9ec39SRoland Dreier  *
4aef9ec39SRoland Dreier  * This software is available to you under a choice of one of two
5aef9ec39SRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6aef9ec39SRoland Dreier  * General Public License (GPL) Version 2, available from the file
7aef9ec39SRoland Dreier  * COPYING in the main directory of this source tree, or the
8aef9ec39SRoland Dreier  * OpenIB.org BSD license below:
9aef9ec39SRoland Dreier  *
10aef9ec39SRoland Dreier  *     Redistribution and use in source and binary forms, with or
11aef9ec39SRoland Dreier  *     without modification, are permitted provided that the following
12aef9ec39SRoland Dreier  *     conditions are met:
13aef9ec39SRoland Dreier  *
14aef9ec39SRoland Dreier  *      - Redistributions of source code must retain the above
15aef9ec39SRoland Dreier  *        copyright notice, this list of conditions and the following
16aef9ec39SRoland Dreier  *        disclaimer.
17aef9ec39SRoland Dreier  *
18aef9ec39SRoland Dreier  *      - Redistributions in binary form must reproduce the above
19aef9ec39SRoland Dreier  *        copyright notice, this list of conditions and the following
20aef9ec39SRoland Dreier  *        disclaimer in the documentation and/or other materials
21aef9ec39SRoland Dreier  *        provided with the distribution.
22aef9ec39SRoland Dreier  *
23aef9ec39SRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24aef9ec39SRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25aef9ec39SRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26aef9ec39SRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27aef9ec39SRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28aef9ec39SRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29aef9ec39SRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30aef9ec39SRoland Dreier  * SOFTWARE.
31aef9ec39SRoland Dreier  */
32aef9ec39SRoland Dreier 
33aef9ec39SRoland Dreier #ifndef IB_SRP_H
34aef9ec39SRoland Dreier #define IB_SRP_H
35aef9ec39SRoland Dreier 
36aef9ec39SRoland Dreier #include <linux/types.h>
37aef9ec39SRoland Dreier #include <linux/list.h>
388e9e5f4fSIngo Molnar #include <linux/mutex.h>
39cf368713SRoland Dreier #include <linux/scatterlist.h>
40aef9ec39SRoland Dreier 
41aef9ec39SRoland Dreier #include <scsi/scsi_host.h>
42aef9ec39SRoland Dreier #include <scsi/scsi_cmnd.h>
43aef9ec39SRoland Dreier 
44aef9ec39SRoland Dreier #include <rdma/ib_verbs.h>
45aef9ec39SRoland Dreier #include <rdma/ib_sa.h>
46aef9ec39SRoland Dreier #include <rdma/ib_cm.h>
47f5358a17SRoland Dreier #include <rdma/ib_fmr_pool.h>
48aef9ec39SRoland Dreier 
49aef9ec39SRoland Dreier enum {
50aef9ec39SRoland Dreier 	SRP_PATH_REC_TIMEOUT_MS	= 1000,
51aef9ec39SRoland Dreier 	SRP_ABORT_TIMEOUT_MS	= 5000,
52aef9ec39SRoland Dreier 
53aef9ec39SRoland Dreier 	SRP_PORT_REDIRECT	= 1,
54aef9ec39SRoland Dreier 	SRP_DLID_REDIRECT	= 2,
559fe4bcf4SDavid Dillow 	SRP_STALE_CONN		= 3,
56aef9ec39SRoland Dreier 
575f068992SRoland Dreier 	SRP_MAX_LUN		= 512,
5874b0a15bSVu Pham 	SRP_DEF_SG_TABLESIZE	= 12,
59aef9ec39SRoland Dreier 
60aef9ec39SRoland Dreier 	SRP_RQ_SHIFT    	= 6,
61aef9ec39SRoland Dreier 	SRP_RQ_SIZE		= 1 << SRP_RQ_SHIFT,
62dd5e6e38SBart Van Assche 
63dd5e6e38SBart Van Assche 	SRP_SQ_SIZE		= SRP_RQ_SIZE,
64dd5e6e38SBart Van Assche 	SRP_RSP_SQ_SIZE		= 1,
65dd5e6e38SBart Van Assche 	SRP_REQ_SQ_SIZE		= SRP_SQ_SIZE - SRP_RSP_SQ_SIZE,
66dd5e6e38SBart Van Assche 	SRP_TSK_MGMT_SQ_SIZE	= 1,
67dd5e6e38SBart Van Assche 	SRP_CMD_SQ_SIZE		= SRP_REQ_SQ_SIZE - SRP_TSK_MGMT_SQ_SIZE,
68aef9ec39SRoland Dreier 
69f8b6e31eSDavid Dillow 	SRP_TAG_NO_REQ		= ~0U,
70f8b6e31eSDavid Dillow 	SRP_TAG_TSK_MGMT	= 1U << 31,
71f5358a17SRoland Dreier 
72be8b9814SDavid Dillow 	SRP_FMR_SIZE		= 512,
73be8b9814SDavid Dillow 	SRP_FMR_MIN_SIZE	= 128,
74f5358a17SRoland Dreier 	SRP_FMR_POOL_SIZE	= 1024,
758f26c9ffSDavid Dillow 	SRP_FMR_DIRTY_SIZE	= SRP_FMR_POOL_SIZE / 4,
768f26c9ffSDavid Dillow 
778f26c9ffSDavid Dillow 	SRP_MAP_ALLOW_FMR	= 0,
788f26c9ffSDavid Dillow 	SRP_MAP_NO_FMR		= 1,
79aef9ec39SRoland Dreier };
80aef9ec39SRoland Dreier 
81aef9ec39SRoland Dreier enum srp_target_state {
82aef9ec39SRoland Dreier 	SRP_TARGET_LIVE,
83aef9ec39SRoland Dreier 	SRP_TARGET_DEAD,
84aef9ec39SRoland Dreier 	SRP_TARGET_REMOVED
85aef9ec39SRoland Dreier };
86aef9ec39SRoland Dreier 
87bb12588aSDavid Dillow enum srp_iu_type {
88bb12588aSDavid Dillow 	SRP_IU_CMD,
89bb12588aSDavid Dillow 	SRP_IU_TSK_MGMT,
90bb12588aSDavid Dillow 	SRP_IU_RSP,
918cba2077SDavid Dillow };
928cba2077SDavid Dillow 
93f5358a17SRoland Dreier struct srp_device {
94f5358a17SRoland Dreier 	struct list_head	dev_list;
95aef9ec39SRoland Dreier 	struct ib_device       *dev;
96aef9ec39SRoland Dreier 	struct ib_pd	       *pd;
97aef9ec39SRoland Dreier 	struct ib_mr	       *mr;
98f5358a17SRoland Dreier 	struct ib_fmr_pool     *fmr_pool;
99bf628dc2SRoland Dreier 	u64			fmr_page_mask;
1008f26c9ffSDavid Dillow 	int			fmr_page_size;
1018f26c9ffSDavid Dillow 	int			fmr_max_size;
102f5358a17SRoland Dreier };
103f5358a17SRoland Dreier 
104f5358a17SRoland Dreier struct srp_host {
10505321937SGreg Kroah-Hartman 	struct srp_device      *srp_dev;
106f5358a17SRoland Dreier 	u8			port;
107ee959b00STony Jones 	struct device		dev;
108aef9ec39SRoland Dreier 	struct list_head	target_list;
109b3589fd4SMatthew Wilcox 	spinlock_t		target_lock;
110aef9ec39SRoland Dreier 	struct completion	released;
111aef9ec39SRoland Dreier 	struct list_head	list;
112aef9ec39SRoland Dreier };
113aef9ec39SRoland Dreier 
114aef9ec39SRoland Dreier struct srp_request {
115aef9ec39SRoland Dreier 	struct list_head	list;
116aef9ec39SRoland Dreier 	struct scsi_cmnd       *scmnd;
117aef9ec39SRoland Dreier 	struct srp_iu	       *cmd;
1188f26c9ffSDavid Dillow 	struct ib_pool_fmr    **fmr_list;
1198f26c9ffSDavid Dillow 	u64		       *map_page;
120c07d424dSDavid Dillow 	struct srp_direct_buf  *indirect_desc;
121c07d424dSDavid Dillow 	dma_addr_t		indirect_dma_addr;
1228f26c9ffSDavid Dillow 	short			nfmr;
123d945e1dfSRoland Dreier 	short			index;
124aef9ec39SRoland Dreier };
125aef9ec39SRoland Dreier 
126aef9ec39SRoland Dreier struct srp_target_port {
1279af76271SDavid Dillow 	/* These are RW in the hot path, and commonly used together */
1289af76271SDavid Dillow 	struct list_head	free_tx;
1299af76271SDavid Dillow 	struct list_head	free_reqs;
1309af76271SDavid Dillow 	spinlock_t		lock;
1319af76271SDavid Dillow 	s32			req_lim;
1329af76271SDavid Dillow 
1339af76271SDavid Dillow 	/* These are read-only in the hot path */
1349af76271SDavid Dillow 	struct ib_cq	       *send_cq ____cacheline_aligned_in_smp;
1359af76271SDavid Dillow 	struct ib_cq	       *recv_cq;
1369af76271SDavid Dillow 	struct ib_qp	       *qp;
1379af76271SDavid Dillow 	u32			lkey;
1389af76271SDavid Dillow 	u32			rkey;
1399af76271SDavid Dillow 	enum srp_target_state	state;
14049248644SDavid Dillow 	unsigned int		max_iu_len;
14149248644SDavid Dillow 	unsigned int		cmd_sg_cnt;
142c07d424dSDavid Dillow 	unsigned int		indirect_size;
143c07d424dSDavid Dillow 	bool			allow_ext_sg;
1449af76271SDavid Dillow 
1459af76271SDavid Dillow 	/* Everything above this point is used in the hot path of
1469af76271SDavid Dillow 	 * command processing. Try to keep them packed into cachelines.
1479af76271SDavid Dillow 	 */
1489af76271SDavid Dillow 
149aef9ec39SRoland Dreier 	__be64			id_ext;
150aef9ec39SRoland Dreier 	__be64			ioc_guid;
151aef9ec39SRoland Dreier 	__be64			service_id;
15201cb9bcbSIshai Rabinovitz 	__be64			initiator_ext;
1530c0450dbSRamachandra K 	u16			io_class;
154aef9ec39SRoland Dreier 	struct srp_host	       *srp_host;
155aef9ec39SRoland Dreier 	struct Scsi_Host       *scsi_host;
156aef9ec39SRoland Dreier 	char			target_name[32];
157aef9ec39SRoland Dreier 	unsigned int		scsi_id;
158c07d424dSDavid Dillow 	unsigned int		sg_tablesize;
159aef9ec39SRoland Dreier 
160aef9ec39SRoland Dreier 	struct ib_sa_path_rec	path;
1613633b3d0SIshai Rabinovitz 	__be16			orig_dgid[8];
162aef9ec39SRoland Dreier 	struct ib_sa_query     *path_query;
163aef9ec39SRoland Dreier 	int			path_query_id;
164aef9ec39SRoland Dreier 
165c9b03c1aSBart Van Assche 	u32			rq_tmo_jiffies;
166294c875aSBart Van Assche 	bool			connected;
167c9b03c1aSBart Van Assche 
168aef9ec39SRoland Dreier 	struct ib_cm_id	       *cm_id;
169aef9ec39SRoland Dreier 
170aef9ec39SRoland Dreier 	int			max_ti_iu_len;
171aef9ec39SRoland Dreier 
1726bfa24faSRoland Dreier 	int			zero_req_lim;
1736bfa24faSRoland Dreier 
174dd5e6e38SBart Van Assche 	struct srp_iu	       *tx_ring[SRP_SQ_SIZE];
1759af76271SDavid Dillow 	struct srp_iu	       *rx_ring[SRP_RQ_SIZE];
176dd5e6e38SBart Van Assche 	struct srp_request	req_ring[SRP_CMD_SQ_SIZE];
177aef9ec39SRoland Dreier 
178aef9ec39SRoland Dreier 	struct work_struct	work;
179aef9ec39SRoland Dreier 
180aef9ec39SRoland Dreier 	struct list_head	list;
181aef9ec39SRoland Dreier 	struct completion	done;
182aef9ec39SRoland Dreier 	int			status;
183948d1e88SBart Van Assche 	bool			qp_in_error;
184f8b6e31eSDavid Dillow 
185f8b6e31eSDavid Dillow 	struct completion	tsk_mgmt_done;
186f8b6e31eSDavid Dillow 	u8			tsk_mgmt_status;
187aef9ec39SRoland Dreier };
188aef9ec39SRoland Dreier 
189aef9ec39SRoland Dreier struct srp_iu {
190dcb4cb85SBart Van Assche 	struct list_head	list;
19185507bccSRalph Campbell 	u64			dma;
192aef9ec39SRoland Dreier 	void		       *buf;
193aef9ec39SRoland Dreier 	size_t			size;
194aef9ec39SRoland Dreier 	enum dma_data_direction	direction;
195aef9ec39SRoland Dreier };
196aef9ec39SRoland Dreier 
1978f26c9ffSDavid Dillow struct srp_map_state {
1988f26c9ffSDavid Dillow 	struct ib_pool_fmr    **next_fmr;
1998f26c9ffSDavid Dillow 	struct srp_direct_buf  *desc;
2008f26c9ffSDavid Dillow 	u64		       *pages;
2018f26c9ffSDavid Dillow 	dma_addr_t		base_dma_addr;
2028f26c9ffSDavid Dillow 	u32			fmr_len;
2038f26c9ffSDavid Dillow 	u32			total_len;
2048f26c9ffSDavid Dillow 	unsigned int		npages;
2058f26c9ffSDavid Dillow 	unsigned int		nfmr;
2068f26c9ffSDavid Dillow 	unsigned int		ndesc;
2078f26c9ffSDavid Dillow 	struct scatterlist     *unmapped_sg;
2088f26c9ffSDavid Dillow 	int			unmapped_index;
2098f26c9ffSDavid Dillow 	dma_addr_t		unmapped_addr;
2108f26c9ffSDavid Dillow };
2118f26c9ffSDavid Dillow 
212aef9ec39SRoland Dreier #endif /* IB_SRP_H */
213