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 
5774b0a15bSVu Pham 	SRP_DEF_SG_TABLESIZE	= 12,
58aef9ec39SRoland Dreier 
594d73f95fSBart Van Assche 	SRP_DEFAULT_QUEUE_SIZE	= 1 << 6,
60dd5e6e38SBart Van Assche 	SRP_RSP_SQ_SIZE		= 1,
61dd5e6e38SBart Van Assche 	SRP_TSK_MGMT_SQ_SIZE	= 1,
624d73f95fSBart Van Assche 	SRP_DEFAULT_CMD_SQ_SIZE = SRP_DEFAULT_QUEUE_SIZE - SRP_RSP_SQ_SIZE -
634d73f95fSBart Van Assche 				  SRP_TSK_MGMT_SQ_SIZE,
64aef9ec39SRoland Dreier 
65f8b6e31eSDavid Dillow 	SRP_TAG_NO_REQ		= ~0U,
66f8b6e31eSDavid Dillow 	SRP_TAG_TSK_MGMT	= 1U << 31,
67f5358a17SRoland Dreier 
6852ede08fSBart Van Assche 	SRP_MAX_PAGES_PER_MR	= 512,
69aef9ec39SRoland Dreier };
70aef9ec39SRoland Dreier 
71aef9ec39SRoland Dreier enum srp_target_state {
7234aa654eSBart Van Assche 	SRP_TARGET_SCANNING,
73aef9ec39SRoland Dreier 	SRP_TARGET_LIVE,
74ef6c49d8SBart Van Assche 	SRP_TARGET_REMOVED,
75aef9ec39SRoland Dreier };
76aef9ec39SRoland Dreier 
77bb12588aSDavid Dillow enum srp_iu_type {
78bb12588aSDavid Dillow 	SRP_IU_CMD,
79bb12588aSDavid Dillow 	SRP_IU_TSK_MGMT,
80bb12588aSDavid Dillow 	SRP_IU_RSP,
818cba2077SDavid Dillow };
828cba2077SDavid Dillow 
835cfb1782SBart Van Assche /*
845cfb1782SBart Van Assche  * @mr_page_mask: HCA memory registration page mask.
855cfb1782SBart Van Assche  * @mr_page_size: HCA memory registration page size.
865cfb1782SBart Van Assche  * @mr_max_size: Maximum size in bytes of a single FMR / FR registration
875cfb1782SBart Van Assche  *   request.
885cfb1782SBart Van Assche  */
89f5358a17SRoland Dreier struct srp_device {
90f5358a17SRoland Dreier 	struct list_head	dev_list;
91aef9ec39SRoland Dreier 	struct ib_device       *dev;
92aef9ec39SRoland Dreier 	struct ib_pd	       *pd;
9352ede08fSBart Van Assche 	u64			mr_page_mask;
9452ede08fSBart Van Assche 	int			mr_page_size;
9552ede08fSBart Van Assche 	int			mr_max_size;
9652ede08fSBart Van Assche 	int			max_pages_per_mr;
97d1b4289eSBart Van Assche 	bool			has_fmr;
985cfb1782SBart Van Assche 	bool			has_fr;
99002f1567SBart Van Assche 	bool			use_fmr;
1005cfb1782SBart Van Assche 	bool			use_fast_reg;
101f5358a17SRoland Dreier };
102f5358a17SRoland Dreier 
103f5358a17SRoland Dreier struct srp_host {
10405321937SGreg Kroah-Hartman 	struct srp_device      *srp_dev;
105f5358a17SRoland Dreier 	u8			port;
106ee959b00STony Jones 	struct device		dev;
107aef9ec39SRoland Dreier 	struct list_head	target_list;
108b3589fd4SMatthew Wilcox 	spinlock_t		target_lock;
109aef9ec39SRoland Dreier 	struct completion	released;
110aef9ec39SRoland Dreier 	struct list_head	list;
1112d7091bcSBart Van Assche 	struct mutex		add_target_mutex;
112aef9ec39SRoland Dreier };
113aef9ec39SRoland Dreier 
114aef9ec39SRoland Dreier struct srp_request {
115aef9ec39SRoland Dreier 	struct scsi_cmnd       *scmnd;
116aef9ec39SRoland Dreier 	struct srp_iu	       *cmd;
1175cfb1782SBart Van Assche 	union {
1188f26c9ffSDavid Dillow 		struct ib_pool_fmr **fmr_list;
1195cfb1782SBart Van Assche 		struct srp_fr_desc **fr_list;
1205cfb1782SBart Van Assche 	};
1218f26c9ffSDavid Dillow 	u64		       *map_page;
122c07d424dSDavid Dillow 	struct srp_direct_buf  *indirect_desc;
123c07d424dSDavid Dillow 	dma_addr_t		indirect_dma_addr;
12452ede08fSBart Van Assche 	short			nmdesc;
1251dc7b1f1SChristoph Hellwig 	struct ib_cqe		reg_cqe;
126aef9ec39SRoland Dreier };
127aef9ec39SRoland Dreier 
128509c07bcSBart Van Assche /**
129509c07bcSBart Van Assche  * struct srp_rdma_ch
130509c07bcSBart Van Assche  * @comp_vector: Completion vector used by this RDMA channel.
131509c07bcSBart Van Assche  */
132509c07bcSBart Van Assche struct srp_rdma_ch {
1339af76271SDavid Dillow 	/* These are RW in the hot path, and commonly used together */
1349af76271SDavid Dillow 	struct list_head	free_tx;
1359af76271SDavid Dillow 	spinlock_t		lock;
1369af76271SDavid Dillow 	s32			req_lim;
1379af76271SDavid Dillow 
1389af76271SDavid Dillow 	/* These are read-only in the hot path */
139509c07bcSBart Van Assche 	struct srp_target_port *target ____cacheline_aligned_in_smp;
140509c07bcSBart Van Assche 	struct ib_cq	       *send_cq;
1419af76271SDavid Dillow 	struct ib_cq	       *recv_cq;
1429af76271SDavid Dillow 	struct ib_qp	       *qp;
1435cfb1782SBart Van Assche 	union {
144d1b4289eSBart Van Assche 		struct ib_fmr_pool     *fmr_pool;
1455cfb1782SBart Van Assche 		struct srp_fr_pool     *fr_pool;
1465cfb1782SBart Van Assche 	};
147509c07bcSBart Van Assche 
148509c07bcSBart Van Assche 	/* Everything above this point is used in the hot path of
149509c07bcSBart Van Assche 	 * command processing. Try to keep them packed into cachelines.
150509c07bcSBart Van Assche 	 */
151509c07bcSBart Van Assche 
152509c07bcSBart Van Assche 	struct completion	done;
153509c07bcSBart Van Assche 	int			status;
154509c07bcSBart Van Assche 
155c2f8fc4eSDasaratharaman Chandramouli 	struct sa_path_rec	path;
156509c07bcSBart Van Assche 	struct ib_sa_query     *path_query;
157509c07bcSBart Van Assche 	int			path_query_id;
158509c07bcSBart Van Assche 
159509c07bcSBart Van Assche 	struct ib_cm_id	       *cm_id;
160509c07bcSBart Van Assche 	struct srp_iu	      **tx_ring;
161509c07bcSBart Van Assche 	struct srp_iu	      **rx_ring;
162509c07bcSBart Van Assche 	struct srp_request     *req_ring;
163509c07bcSBart Van Assche 	int			max_ti_iu_len;
164509c07bcSBart Van Assche 	int			comp_vector;
165509c07bcSBart Van Assche 
1660a6fdbdeSBart Van Assche 	u64			tsk_mgmt_tag;
167509c07bcSBart Van Assche 	struct completion	tsk_mgmt_done;
168509c07bcSBart Van Assche 	u8			tsk_mgmt_status;
169c014c8cdSBart Van Assche 	bool			connected;
170509c07bcSBart Van Assche };
171509c07bcSBart Van Assche 
172509c07bcSBart Van Assche /**
173509c07bcSBart Van Assche  * struct srp_target_port
174509c07bcSBart Van Assche  * @comp_vector: Completion vector used by the first RDMA channel created for
175509c07bcSBart Van Assche  *   this target port.
176509c07bcSBart Van Assche  */
177509c07bcSBart Van Assche struct srp_target_port {
178509c07bcSBart Van Assche 	/* read and written in the hot path */
179509c07bcSBart Van Assche 	spinlock_t		lock;
180509c07bcSBart Van Assche 
181509c07bcSBart Van Assche 	/* read only in the hot path */
1825f071777SChristoph Hellwig 	struct ib_pd		*pd;
183d92c0da7SBart Van Assche 	struct srp_rdma_ch	*ch;
184d92c0da7SBart Van Assche 	u32			ch_count;
1859af76271SDavid Dillow 	u32			lkey;
1869af76271SDavid Dillow 	enum srp_target_state	state;
18749248644SDavid Dillow 	unsigned int		max_iu_len;
18849248644SDavid Dillow 	unsigned int		cmd_sg_cnt;
189c07d424dSDavid Dillow 	unsigned int		indirect_size;
190c07d424dSDavid Dillow 	bool			allow_ext_sg;
1919af76271SDavid Dillow 
192509c07bcSBart Van Assche 	/* other member variables */
193747fe000SBart Van Assche 	union ib_gid		sgid;
194aef9ec39SRoland Dreier 	__be64			id_ext;
195aef9ec39SRoland Dreier 	__be64			ioc_guid;
196aef9ec39SRoland Dreier 	__be64			service_id;
19701cb9bcbSIshai Rabinovitz 	__be64			initiator_ext;
1980c0450dbSRamachandra K 	u16			io_class;
199aef9ec39SRoland Dreier 	struct srp_host	       *srp_host;
200aef9ec39SRoland Dreier 	struct Scsi_Host       *scsi_host;
2019dd69a60SBart Van Assche 	struct srp_rport       *rport;
202aef9ec39SRoland Dreier 	char			target_name[32];
203aef9ec39SRoland Dreier 	unsigned int		scsi_id;
204c07d424dSDavid Dillow 	unsigned int		sg_tablesize;
205fa9863f8SBart Van Assche 	int			mr_pool_size;
206509c5f33SBart Van Assche 	int			mr_per_cmd;
2074d73f95fSBart Van Assche 	int			queue_size;
2084d73f95fSBart Van Assche 	int			req_ring_size;
2094b5e5f41SBart Van Assche 	int			comp_vector;
2107bb312e4SVu Pham 	int			tl_retry_count;
211aef9ec39SRoland Dreier 
212747fe000SBart Van Assche 	union ib_gid		orig_dgid;
213747fe000SBart Van Assche 	__be16			pkey;
214aef9ec39SRoland Dreier 
215c9b03c1aSBart Van Assche 	u32			rq_tmo_jiffies;
216c9b03c1aSBart Van Assche 
2176bfa24faSRoland Dreier 	int			zero_req_lim;
2186bfa24faSRoland Dreier 
219c1120f89SBart Van Assche 	struct work_struct	tl_err_work;
220ef6c49d8SBart Van Assche 	struct work_struct	remove_work;
221aef9ec39SRoland Dreier 
222aef9ec39SRoland Dreier 	struct list_head	list;
223948d1e88SBart Van Assche 	bool			qp_in_error;
224aef9ec39SRoland Dreier };
225aef9ec39SRoland Dreier 
226aef9ec39SRoland Dreier struct srp_iu {
227dcb4cb85SBart Van Assche 	struct list_head	list;
22885507bccSRalph Campbell 	u64			dma;
229aef9ec39SRoland Dreier 	void		       *buf;
230aef9ec39SRoland Dreier 	size_t			size;
231aef9ec39SRoland Dreier 	enum dma_data_direction	direction;
2321dc7b1f1SChristoph Hellwig 	struct ib_cqe		cqe;
233aef9ec39SRoland Dreier };
234aef9ec39SRoland Dreier 
2355cfb1782SBart Van Assche /**
2365cfb1782SBart Van Assche  * struct srp_fr_desc - fast registration work request arguments
2375cfb1782SBart Van Assche  * @entry: Entry in srp_fr_pool.free_list.
2385cfb1782SBart Van Assche  * @mr:    Memory region.
2395cfb1782SBart Van Assche  * @frpl:  Fast registration page list.
2405cfb1782SBart Van Assche  */
2415cfb1782SBart Van Assche struct srp_fr_desc {
2425cfb1782SBart Van Assche 	struct list_head		entry;
2435cfb1782SBart Van Assche 	struct ib_mr			*mr;
2445cfb1782SBart Van Assche };
2455cfb1782SBart Van Assche 
2465cfb1782SBart Van Assche /**
2475cfb1782SBart Van Assche  * struct srp_fr_pool - pool of fast registration descriptors
2485cfb1782SBart Van Assche  *
2495cfb1782SBart Van Assche  * An entry is available for allocation if and only if it occurs in @free_list.
2505cfb1782SBart Van Assche  *
2515cfb1782SBart Van Assche  * @size:      Number of descriptors in this pool.
2525cfb1782SBart Van Assche  * @max_page_list_len: Maximum fast registration work request page list length.
2535cfb1782SBart Van Assche  * @lock:      Protects free_list.
2545cfb1782SBart Van Assche  * @free_list: List of free descriptors.
2555cfb1782SBart Van Assche  * @desc:      Fast registration descriptor pool.
2565cfb1782SBart Van Assche  */
2575cfb1782SBart Van Assche struct srp_fr_pool {
2585cfb1782SBart Van Assche 	int			size;
2595cfb1782SBart Van Assche 	int			max_page_list_len;
2605cfb1782SBart Van Assche 	spinlock_t		lock;
2615cfb1782SBart Van Assche 	struct list_head	free_list;
2625cfb1782SBart Van Assche 	struct srp_fr_desc	desc[0];
2635cfb1782SBart Van Assche };
2645cfb1782SBart Van Assche 
2655cfb1782SBart Van Assche /**
2665cfb1782SBart Van Assche  * struct srp_map_state - per-request DMA memory mapping state
2675cfb1782SBart Van Assche  * @desc:	    Pointer to the element of the SRP buffer descriptor array
2685cfb1782SBart Van Assche  *		    that is being filled in.
2695cfb1782SBart Van Assche  * @pages:	    Array with DMA addresses of pages being considered for
2705cfb1782SBart Van Assche  *		    memory registration.
2715cfb1782SBart Van Assche  * @base_dma_addr:  DMA address of the first page that has not yet been mapped.
2725cfb1782SBart Van Assche  * @dma_len:	    Number of bytes that will be registered with the next
2735cfb1782SBart Van Assche  *		    FMR or FR memory registration call.
2745cfb1782SBart Van Assche  * @total_len:	    Total number of bytes in the sg-list being mapped.
2755cfb1782SBart Van Assche  * @npages:	    Number of page addresses in the pages[] array.
2765cfb1782SBart Van Assche  * @nmdesc:	    Number of FMR or FR memory descriptors used for mapping.
2775cfb1782SBart Van Assche  * @ndesc:	    Number of SRP buffer descriptors that have been filled in.
2785cfb1782SBart Van Assche  */
2798f26c9ffSDavid Dillow struct srp_map_state {
2805cfb1782SBart Van Assche 	union {
281f731ed62SBart Van Assche 		struct {
282f731ed62SBart Van Assche 			struct ib_pool_fmr **next;
283f731ed62SBart Van Assche 			struct ib_pool_fmr **end;
284f731ed62SBart Van Assche 		} fmr;
285f731ed62SBart Van Assche 		struct {
286f731ed62SBart Van Assche 			struct srp_fr_desc **next;
287f731ed62SBart Van Assche 			struct srp_fr_desc **end;
288f731ed62SBart Van Assche 		} fr;
289330179f2SBart Van Assche 		struct {
290330179f2SBart Van Assche 			void		   **next;
291330179f2SBart Van Assche 			void		   **end;
292330179f2SBart Van Assche 		} gen;
2935cfb1782SBart Van Assche 	};
2948f26c9ffSDavid Dillow 	struct srp_direct_buf  *desc;
295f7f7aab1SSagi Grimberg 	union {
2968f26c9ffSDavid Dillow 		u64			*pages;
297f7f7aab1SSagi Grimberg 		struct scatterlist	*sg;
298f7f7aab1SSagi Grimberg 	};
2998f26c9ffSDavid Dillow 	dma_addr_t		base_dma_addr;
30052ede08fSBart Van Assche 	u32			dma_len;
3018f26c9ffSDavid Dillow 	u32			total_len;
3028f26c9ffSDavid Dillow 	unsigned int		npages;
30352ede08fSBart Van Assche 	unsigned int		nmdesc;
3048f26c9ffSDavid Dillow 	unsigned int		ndesc;
3058f26c9ffSDavid Dillow };
3068f26c9ffSDavid Dillow 
307aef9ec39SRoland Dreier #endif /* IB_SRP_H */
308