GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / infiniband / ulp / srpt / ib_srpt.h
1 /*
2  * Copyright (c) 2006 - 2009 Mellanox Technology Inc.  All rights reserved.
3  * Copyright (C) 2009 - 2010 Bart Van Assche <bvanassche@acm.org>.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  *
33  */
34
35 #ifndef IB_SRPT_H
36 #define IB_SRPT_H
37
38 #include <linux/types.h>
39 #include <linux/list.h>
40 #include <linux/wait.h>
41
42 #include <rdma/ib_verbs.h>
43 #include <rdma/ib_sa.h>
44 #include <rdma/ib_cm.h>
45 #include <rdma/rw.h>
46
47 #include <scsi/srp.h>
48
49 #include "ib_dm_mad.h"
50
51 /*
52  * The prefix the ServiceName field must start with in the device management
53  * ServiceEntries attribute pair. See also the SRP specification.
54  */
55 #define SRP_SERVICE_NAME_PREFIX         "SRP.T10:"
56
57 enum {
58         /*
59          * SRP IOControllerProfile attributes for SRP target ports that have
60          * not been defined in <scsi/srp.h>. Source: section B.7, table B.7
61          * in the SRP specification.
62          */
63         SRP_PROTOCOL = 0x0108,
64         SRP_PROTOCOL_VERSION = 0x0001,
65         SRP_IO_SUBCLASS = 0x609e,
66         SRP_SEND_TO_IOC = 0x01,
67         SRP_SEND_FROM_IOC = 0x02,
68         SRP_RDMA_READ_FROM_IOC = 0x08,
69         SRP_RDMA_WRITE_FROM_IOC = 0x20,
70
71         /*
72          * srp_login_cmd.req_flags bitmasks. See also table 9 in the SRP
73          * specification.
74          */
75         SRP_MTCH_ACTION = 0x03, /* MULTI-CHANNEL ACTION */
76         SRP_LOSOLNT = 0x10, /* logout solicited notification */
77         SRP_CRSOLNT = 0x20, /* credit request solicited notification */
78         SRP_AESOLNT = 0x40, /* asynchronous event solicited notification */
79
80         /*
81          * srp_cmd.sol_nt / srp_tsk_mgmt.sol_not bitmasks. See also tables
82          * 18 and 20 in the SRP specification.
83          */
84         SRP_SCSOLNT = 0x02, /* SCSOLNT = successful solicited notification */
85         SRP_UCSOLNT = 0x04, /* UCSOLNT = unsuccessful solicited notification */
86
87         /*
88          * srp_rsp.sol_not / srp_t_logout.sol_not bitmasks. See also tables
89          * 16 and 22 in the SRP specification.
90          */
91         SRP_SOLNT = 0x01, /* SOLNT = solicited notification */
92
93         /* See also table 24 in the SRP specification. */
94         SRP_TSK_MGMT_SUCCESS = 0x00,
95         SRP_TSK_MGMT_FUNC_NOT_SUPP = 0x04,
96         SRP_TSK_MGMT_FAILED = 0x05,
97
98         /* See also table 21 in the SRP specification. */
99         SRP_CMD_SIMPLE_Q = 0x0,
100         SRP_CMD_HEAD_OF_Q = 0x1,
101         SRP_CMD_ORDERED_Q = 0x2,
102         SRP_CMD_ACA = 0x4,
103
104         SRP_LOGIN_RSP_MULTICHAN_NO_CHAN = 0x0,
105         SRP_LOGIN_RSP_MULTICHAN_TERMINATED = 0x1,
106         SRP_LOGIN_RSP_MULTICHAN_MAINTAINED = 0x2,
107
108         SRPT_DEF_SG_TABLESIZE = 128,
109         /*
110          * An experimentally determined value that avoids that QP creation
111          * fails due to "swiotlb buffer is full" on systems using the swiotlb.
112          */
113         SRPT_MAX_SG_PER_WQE = 16,
114
115         MIN_SRPT_SQ_SIZE = 16,
116         DEF_SRPT_SQ_SIZE = 4096,
117         SRPT_RQ_SIZE = 128,
118         MIN_SRPT_SRQ_SIZE = 4,
119         DEFAULT_SRPT_SRQ_SIZE = 4095,
120         MAX_SRPT_SRQ_SIZE = 65535,
121         MAX_SRPT_RDMA_SIZE = 1U << 24,
122         MAX_SRPT_RSP_SIZE = 1024,
123
124         MIN_MAX_REQ_SIZE = 996,
125         DEFAULT_MAX_REQ_SIZE
126                 = sizeof(struct srp_cmd)/*48*/
127                 + sizeof(struct srp_indirect_buf)/*20*/
128                 + 128 * sizeof(struct srp_direct_buf)/*16*/,
129
130         MIN_MAX_RSP_SIZE = sizeof(struct srp_rsp)/*36*/ + 4,
131         DEFAULT_MAX_RSP_SIZE = 256, /* leaves 220 bytes for sense data */
132
133         DEFAULT_MAX_RDMA_SIZE = 65536,
134 };
135
136 /**
137  * enum srpt_command_state - SCSI command state managed by SRPT.
138  * @SRPT_STATE_NEW:           New command arrived and is being processed.
139  * @SRPT_STATE_NEED_DATA:     Processing a write or bidir command and waiting
140  *                            for data arrival.
141  * @SRPT_STATE_DATA_IN:       Data for the write or bidir command arrived and is
142  *                            being processed.
143  * @SRPT_STATE_CMD_RSP_SENT:  SRP_RSP for SRP_CMD has been sent.
144  * @SRPT_STATE_MGMT:          Processing a SCSI task management command.
145  * @SRPT_STATE_MGMT_RSP_SENT: SRP_RSP for SRP_TSK_MGMT has been sent.
146  * @SRPT_STATE_DONE:          Command processing finished successfully, command
147  *                            processing has been aborted or command processing
148  *                            failed.
149  */
150 enum srpt_command_state {
151         SRPT_STATE_NEW           = 0,
152         SRPT_STATE_NEED_DATA     = 1,
153         SRPT_STATE_DATA_IN       = 2,
154         SRPT_STATE_CMD_RSP_SENT  = 3,
155         SRPT_STATE_MGMT          = 4,
156         SRPT_STATE_MGMT_RSP_SENT = 5,
157         SRPT_STATE_DONE          = 6,
158 };
159
160 /**
161  * struct srpt_ioctx - Shared SRPT I/O context information.
162  * @buf:   Pointer to the buffer.
163  * @dma:   DMA address of the buffer.
164  * @index: Index of the I/O context in its ioctx_ring array.
165  */
166 struct srpt_ioctx {
167         struct ib_cqe           cqe;
168         void                    *buf;
169         dma_addr_t              dma;
170         uint32_t                index;
171 };
172
173 /**
174  * struct srpt_recv_ioctx - SRPT receive I/O context.
175  * @ioctx:     See above.
176  * @wait_list: Node for insertion in srpt_rdma_ch.cmd_wait_list.
177  */
178 struct srpt_recv_ioctx {
179         struct srpt_ioctx       ioctx;
180         struct list_head        wait_list;
181 };
182         
183 struct srpt_rw_ctx {
184         struct rdma_rw_ctx      rw;
185         struct scatterlist      *sg;
186         unsigned int            nents;
187 };
188
189 /**
190  * struct srpt_send_ioctx - SRPT send I/O context.
191  * @ioctx:       See above.
192  * @ch:          Channel pointer.
193  * @spinlock:    Protects 'state'.
194  * @state:       I/O context state.
195  * @cmd:         Target core command data structure.
196  * @sense_data:  SCSI sense data.
197  */
198 struct srpt_send_ioctx {
199         struct srpt_ioctx       ioctx;
200         struct srpt_rdma_ch     *ch;
201
202         struct srpt_rw_ctx      s_rw_ctx;
203         struct srpt_rw_ctx      *rw_ctxs;
204
205         struct ib_cqe           rdma_cqe;
206         struct list_head        free_list;
207         spinlock_t              spinlock;
208         enum srpt_command_state state;
209         struct se_cmd           cmd;
210         struct completion       tx_done;
211         u8                      n_rdma;
212         u8                      n_rw_ctx;
213         bool                    queue_status_only;
214         u8                      sense_data[TRANSPORT_SENSE_BUFFER];
215 };
216
217 /**
218  * enum rdma_ch_state - SRP channel state.
219  * @CH_CONNECTING:    QP is in RTR state; waiting for RTU.
220  * @CH_LIVE:          QP is in RTS state.
221  * @CH_DISCONNECTING: DREQ has been sent and waiting for DREP or DREQ has
222  *                    been received.
223  * @CH_DRAINING:      DREP has been received or waiting for DREP timed out
224  *                    and last work request has been queued.
225  * @CH_DISCONNECTED:  Last completion has been received.
226  */
227 enum rdma_ch_state {
228         CH_CONNECTING,
229         CH_LIVE,
230         CH_DISCONNECTING,
231         CH_DRAINING,
232         CH_DISCONNECTED,
233 };
234
235 /**
236  * struct srpt_rdma_ch - RDMA channel.
237  * @cm_id:         IB CM ID associated with the channel.
238  * @qp:            IB queue pair used for communicating over this channel.
239  * @cq:            IB completion queue for this channel.
240  * @rq_size:       IB receive queue size.
241  * @rsp_size       IB response message size in bytes.
242  * @sq_wr_avail:   number of work requests available in the send queue.
243  * @sport:         pointer to the information of the HCA port used by this
244  *                 channel.
245  * @i_port_id:     128-bit initiator port identifier copied from SRP_LOGIN_REQ.
246  * @t_port_id:     128-bit target port identifier copied from SRP_LOGIN_REQ.
247  * @max_ti_iu_len: maximum target-to-initiator information unit length.
248  * @req_lim:       request limit: maximum number of requests that may be sent
249  *                 by the initiator without having received a response.
250  * @req_lim_delta: Number of credits not yet sent back to the initiator.
251  * @spinlock:      Protects free_list and state.
252  * @free_list:     Head of list with free send I/O contexts.
253  * @state:         channel state. See also enum rdma_ch_state.
254  * @ioctx_ring:    Send ring.
255  * @list:          Node for insertion in the srpt_device.rch_list list.
256  * @cmd_wait_list: List of SCSI commands that arrived before the RTU event. This
257  *                 list contains struct srpt_ioctx elements and is protected
258  *                 against concurrent modification by the cm_id spinlock.
259  * @sess:          Session information associated with this SRP channel.
260  * @sess_name:     Session name.
261  * @ini_guid:      Initiator port GUID.
262  * @release_work:  Allows scheduling of srpt_release_channel().
263  * @release_done:  Enables waiting for srpt_release_channel() completion.
264  */
265 struct srpt_rdma_ch {
266         struct ib_cm_id         *cm_id;
267         struct ib_qp            *qp;
268         struct ib_cq            *cq;
269         struct ib_cqe           zw_cqe;
270         struct kref             kref;
271         int                     rq_size;
272         u32                     rsp_size;
273         atomic_t                sq_wr_avail;
274         struct srpt_port        *sport;
275         u8                      i_port_id[16];
276         u8                      t_port_id[16];
277         int                     max_ti_iu_len;
278         atomic_t                req_lim;
279         atomic_t                req_lim_delta;
280         spinlock_t              spinlock;
281         struct list_head        free_list;
282         enum rdma_ch_state      state;
283         struct srpt_send_ioctx  **ioctx_ring;
284         struct list_head        list;
285         struct list_head        cmd_wait_list;
286         struct se_session       *sess;
287         u8                      sess_name[36];
288         u8                      ini_guid[24];
289         struct work_struct      release_work;
290         struct completion       *release_done;
291 };
292
293 /**
294  * struct srpt_port_attib - Attributes for SRPT port
295  * @srp_max_rdma_size: Maximum size of SRP RDMA transfers for new connections.
296  * @srp_max_rsp_size: Maximum size of SRP response messages in bytes.
297  * @srp_sq_size: Shared receive queue (SRQ) size.
298  */
299 struct srpt_port_attrib {
300         u32                     srp_max_rdma_size;
301         u32                     srp_max_rsp_size;
302         u32                     srp_sq_size;
303 };
304
305 /**
306  * struct srpt_port - Information associated by SRPT with a single IB port.
307  * @sdev:      backpointer to the HCA information.
308  * @mad_agent: per-port management datagram processing information.
309  * @enabled:   Whether or not this target port is enabled.
310  * @port_guid: ASCII representation of Port GUID
311  * @port_gid:  ASCII representation of Port GID
312  * @port:      one-based port number.
313  * @sm_lid:    cached value of the port's sm_lid.
314  * @lid:       cached value of the port's lid.
315  * @gid:       cached value of the port's gid.
316  * @port_acl_lock spinlock for port_acl_list:
317  * @work:      work structure for refreshing the aforementioned cached values.
318  * @port_guid_tpg: TPG associated with target port GUID.
319  * @port_guid_wwn: WWN associated with target port GUID.
320  * @port_gid_tpg:  TPG associated with target port GID.
321  * @port_gid_wwn:  WWN associated with target port GID.
322  * @port_acl_list: Head of the list with all node ACLs for this port.
323  */
324 struct srpt_port {
325         struct srpt_device      *sdev;
326         struct ib_mad_agent     *mad_agent;
327         bool                    enabled;
328         u8                      port_guid[24];
329         u8                      port_gid[64];
330         u8                      port;
331         u32                     sm_lid;
332         u32                     lid;
333         union ib_gid            gid;
334         struct work_struct      work;
335         struct se_portal_group  port_guid_tpg;
336         struct se_wwn           port_guid_wwn;
337         struct se_portal_group  port_gid_tpg;
338         struct se_wwn           port_gid_wwn;
339         struct srpt_port_attrib port_attrib;
340 };
341
342 /**
343  * struct srpt_device - Information associated by SRPT with a single HCA.
344  * @device:        Backpointer to the struct ib_device managed by the IB core.
345  * @pd:            IB protection domain.
346  * @mr:            L_Key (local key) with write access to all local memory.
347  * @srq:           Per-HCA SRQ (shared receive queue).
348  * @cm_id:         Connection identifier.
349  * @srq_size:      SRQ size.
350  * @ioctx_ring:    Per-HCA SRQ.
351  * @rch_list:      Per-device channel list -- see also srpt_rdma_ch.list.
352  * @ch_releaseQ:   Enables waiting for removal from rch_list.
353  * @mutex:         Protects rch_list.
354  * @port:          Information about the ports owned by this HCA.
355  * @event_handler: Per-HCA asynchronous IB event handler.
356  * @list:          Node in srpt_dev_list.
357  */
358 struct srpt_device {
359         struct ib_device        *device;
360         struct ib_pd            *pd;
361         struct ib_srq           *srq;
362         struct ib_cm_id         *cm_id;
363         int                     srq_size;
364         struct srpt_recv_ioctx  **ioctx_ring;
365         struct list_head        rch_list;
366         wait_queue_head_t       ch_releaseQ;
367         struct mutex            mutex;
368         struct srpt_port        port[2];
369         struct ib_event_handler event_handler;
370         struct list_head        list;
371 };
372
373 #endif                          /* IB_SRPT_H */