[PATCH] IB: Add MAD helper functions
[pandora-kernel.git] / drivers / infiniband / include / ib_mad.h
1 /*
2  * Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
3  * Copyright (c) 2004 Infinicon Corporation.  All rights reserved.
4  * Copyright (c) 2004 Intel Corporation.  All rights reserved.
5  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
6  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  *
36  * $Id: ib_mad.h 1389 2004-12-27 22:56:47Z roland $
37  */
38
39 #if !defined( IB_MAD_H )
40 #define IB_MAD_H
41
42 #include <linux/pci.h>
43
44 #include <ib_verbs.h>
45
46 /* Management base version */
47 #define IB_MGMT_BASE_VERSION                    1
48
49 /* Management classes */
50 #define IB_MGMT_CLASS_SUBN_LID_ROUTED           0x01
51 #define IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE       0x81
52 #define IB_MGMT_CLASS_SUBN_ADM                  0x03
53 #define IB_MGMT_CLASS_PERF_MGMT                 0x04
54 #define IB_MGMT_CLASS_BM                        0x05
55 #define IB_MGMT_CLASS_DEVICE_MGMT               0x06
56 #define IB_MGMT_CLASS_CM                        0x07
57 #define IB_MGMT_CLASS_SNMP                      0x08
58 #define IB_MGMT_CLASS_VENDOR_RANGE2_START       0x30
59 #define IB_MGMT_CLASS_VENDOR_RANGE2_END         0x4F
60
61 /* Management methods */
62 #define IB_MGMT_METHOD_GET                      0x01
63 #define IB_MGMT_METHOD_SET                      0x02
64 #define IB_MGMT_METHOD_GET_RESP                 0x81
65 #define IB_MGMT_METHOD_SEND                     0x03
66 #define IB_MGMT_METHOD_TRAP                     0x05
67 #define IB_MGMT_METHOD_REPORT                   0x06
68 #define IB_MGMT_METHOD_REPORT_RESP              0x86
69 #define IB_MGMT_METHOD_TRAP_REPRESS             0x07
70
71 #define IB_MGMT_METHOD_RESP                     0x80
72
73 #define IB_MGMT_MAX_METHODS                     128
74
75 #define IB_QP0          0
76 #define IB_QP1          __constant_htonl(1)
77 #define IB_QP1_QKEY     0x80010000
78 #define IB_QP_SET_QKEY  0x80000000
79
80 struct ib_grh {
81         u32             version_tclass_flow;
82         u16             paylen;
83         u8              next_hdr;
84         u8              hop_limit;
85         union ib_gid    sgid;
86         union ib_gid    dgid;
87 } __attribute__ ((packed));
88
89 struct ib_mad_hdr {
90         u8      base_version;
91         u8      mgmt_class;
92         u8      class_version;
93         u8      method;
94         u16     status;
95         u16     class_specific;
96         u64     tid;
97         u16     attr_id;
98         u16     resv;
99         u32     attr_mod;
100 } __attribute__ ((packed));
101
102 struct ib_rmpp_hdr {
103         u8      rmpp_version;
104         u8      rmpp_type;
105         u8      rmpp_rtime_flags;
106         u8      rmpp_status;
107         u32     seg_num;
108         u32     paylen_newwin;
109 } __attribute__ ((packed));
110
111 struct ib_mad {
112         struct ib_mad_hdr       mad_hdr;
113         u8                      data[232];
114 } __attribute__ ((packed));
115
116 struct ib_rmpp_mad {
117         struct ib_mad_hdr       mad_hdr;
118         struct ib_rmpp_hdr      rmpp_hdr;
119         u8                      data[220];
120 } __attribute__ ((packed));
121
122 struct ib_vendor_mad {
123         struct ib_mad_hdr       mad_hdr;
124         struct ib_rmpp_hdr      rmpp_hdr;
125         u8                      reserved;
126         u8                      oui[3];
127         u8                      data[216];
128 } __attribute__ ((packed));
129
130 /**
131  * ib_mad_send_buf - MAD data buffer and work request for sends.
132  * @mad: References an allocated MAD data buffer.  The size of the data
133  *   buffer is specified in the @send_wr.length field.
134  * @mapping: DMA mapping information.
135  * @mad_agent: MAD agent that allocated the buffer.
136  * @context: User-controlled context fields.
137  * @send_wr: An initialized work request structure used when sending the MAD.
138  *   The wr_id field of the work request is initialized to reference this
139  *   data structure.
140  * @sge: A scatter-gather list referenced by the work request.
141  *
142  * Users are responsible for initializing the MAD buffer itself, with the
143  * exception of specifying the payload length field in any RMPP MAD.
144  */
145 struct ib_mad_send_buf {
146         struct ib_mad           *mad;
147         DECLARE_PCI_UNMAP_ADDR(mapping)
148         struct ib_mad_agent     *mad_agent;
149         void                    *context[2];
150         struct ib_send_wr       send_wr;
151         struct ib_sge           sge;
152 };
153
154 struct ib_mad_agent;
155 struct ib_mad_send_wc;
156 struct ib_mad_recv_wc;
157
158 /**
159  * ib_mad_send_handler - callback handler for a sent MAD.
160  * @mad_agent: MAD agent that sent the MAD.
161  * @mad_send_wc: Send work completion information on the sent MAD.
162  */
163 typedef void (*ib_mad_send_handler)(struct ib_mad_agent *mad_agent,
164                                     struct ib_mad_send_wc *mad_send_wc);
165
166 /**
167  * ib_mad_snoop_handler - Callback handler for snooping sent MADs.
168  * @mad_agent: MAD agent that snooped the MAD.
169  * @send_wr: Work request information on the sent MAD.
170  * @mad_send_wc: Work completion information on the sent MAD.  Valid
171  *   only for snooping that occurs on a send completion.
172  *
173  * Clients snooping MADs should not modify data referenced by the @send_wr
174  * or @mad_send_wc.
175  */
176 typedef void (*ib_mad_snoop_handler)(struct ib_mad_agent *mad_agent,
177                                      struct ib_send_wr *send_wr,
178                                      struct ib_mad_send_wc *mad_send_wc);
179
180 /**
181  * ib_mad_recv_handler - callback handler for a received MAD.
182  * @mad_agent: MAD agent requesting the received MAD.
183  * @mad_recv_wc: Received work completion information on the received MAD.
184  *
185  * MADs received in response to a send request operation will be handed to
186  * the user after the send operation completes.  All data buffers given
187  * to registered agents through this routine are owned by the receiving
188  * client, except for snooping agents.  Clients snooping MADs should not
189  * modify the data referenced by @mad_recv_wc.
190  */
191 typedef void (*ib_mad_recv_handler)(struct ib_mad_agent *mad_agent,
192                                     struct ib_mad_recv_wc *mad_recv_wc);
193
194 /**
195  * ib_mad_agent - Used to track MAD registration with the access layer.
196  * @device: Reference to device registration is on.
197  * @qp: Reference to QP used for sending and receiving MADs.
198  * @recv_handler: Callback handler for a received MAD.
199  * @send_handler: Callback handler for a sent MAD.
200  * @snoop_handler: Callback handler for snooped sent MADs.
201  * @context: User-specified context associated with this registration.
202  * @hi_tid: Access layer assigned transaction ID for this client.
203  *   Unsolicited MADs sent by this client will have the upper 32-bits
204  *   of their TID set to this value.
205  * @port_num: Port number on which QP is registered
206  */
207 struct ib_mad_agent {
208         struct ib_device        *device;
209         struct ib_qp            *qp;
210         struct ib_mr            *mr;
211         ib_mad_recv_handler     recv_handler;
212         ib_mad_send_handler     send_handler;
213         ib_mad_snoop_handler    snoop_handler;
214         void                    *context;
215         u32                     hi_tid;
216         u8                      port_num;
217 };
218
219 /**
220  * ib_mad_send_wc - MAD send completion information.
221  * @wr_id: Work request identifier associated with the send MAD request.
222  * @status: Completion status.
223  * @vendor_err: Optional vendor error information returned with a failed
224  *   request.
225  */
226 struct ib_mad_send_wc {
227         u64                     wr_id;
228         enum ib_wc_status       status;
229         u32                     vendor_err;
230 };
231
232 /**
233  * ib_mad_recv_buf - received MAD buffer information.
234  * @list: Reference to next data buffer for a received RMPP MAD.
235  * @grh: References a data buffer containing the global route header.
236  *   The data refereced by this buffer is only valid if the GRH is
237  *   valid.
238  * @mad: References the start of the received MAD.
239  */
240 struct ib_mad_recv_buf {
241         struct list_head        list;
242         struct ib_grh           *grh;
243         struct ib_mad           *mad;
244 };
245
246 /**
247  * ib_mad_recv_wc - received MAD information.
248  * @wc: Completion information for the received data.
249  * @recv_buf: Specifies the location of the received data buffer(s).
250  * @mad_len: The length of the received MAD, without duplicated headers.
251  *
252  * For received response, the wr_id field of the wc is set to the wr_id
253  *   for the corresponding send request.
254  */
255 struct ib_mad_recv_wc {
256         struct ib_wc            *wc;
257         struct ib_mad_recv_buf  recv_buf;
258         int                     mad_len;
259 };
260
261 /**
262  * ib_mad_reg_req - MAD registration request
263  * @mgmt_class: Indicates which management class of MADs should be receive
264  *   by the caller.  This field is only required if the user wishes to
265  *   receive unsolicited MADs, otherwise it should be 0.
266  * @mgmt_class_version: Indicates which version of MADs for the given
267  *   management class to receive.
268  * @oui: Indicates IEEE OUI when mgmt_class is a vendor class
269  *   in the range from 0x30 to 0x4f. Otherwise not used.
270  * @method_mask: The caller will receive unsolicited MADs for any method
271  *   where @method_mask = 1.
272  */
273 struct ib_mad_reg_req {
274         u8      mgmt_class;
275         u8      mgmt_class_version;
276         u8      oui[3];
277         DECLARE_BITMAP(method_mask, IB_MGMT_MAX_METHODS);
278 };
279
280 /**
281  * ib_register_mad_agent - Register to send/receive MADs.
282  * @device: The device to register with.
283  * @port_num: The port on the specified device to use.
284  * @qp_type: Specifies which QP to access.  Must be either
285  *   IB_QPT_SMI or IB_QPT_GSI.
286  * @mad_reg_req: Specifies which unsolicited MADs should be received
287  *   by the caller.  This parameter may be NULL if the caller only
288  *   wishes to receive solicited responses.
289  * @rmpp_version: If set, indicates that the client will send
290  *   and receive MADs that contain the RMPP header for the given version.
291  *   If set to 0, indicates that RMPP is not used by this client.
292  * @send_handler: The completion callback routine invoked after a send
293  *   request has completed.
294  * @recv_handler: The completion callback routine invoked for a received
295  *   MAD.
296  * @context: User specified context associated with the registration.
297  */
298 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
299                                            u8 port_num,
300                                            enum ib_qp_type qp_type,
301                                            struct ib_mad_reg_req *mad_reg_req,
302                                            u8 rmpp_version,
303                                            ib_mad_send_handler send_handler,
304                                            ib_mad_recv_handler recv_handler,
305                                            void *context);
306
307 enum ib_mad_snoop_flags {
308         /*IB_MAD_SNOOP_POSTED_SENDS        = 1,*/
309         /*IB_MAD_SNOOP_RMPP_SENDS          = (1<<1),*/
310         IB_MAD_SNOOP_SEND_COMPLETIONS      = (1<<2),
311         /*IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS = (1<<3),*/
312         IB_MAD_SNOOP_RECVS                 = (1<<4)
313         /*IB_MAD_SNOOP_RMPP_RECVS          = (1<<5),*/
314         /*IB_MAD_SNOOP_REDIRECTED_QPS      = (1<<6)*/
315 };
316
317 /**
318  * ib_register_mad_snoop - Register to snoop sent and received MADs.
319  * @device: The device to register with.
320  * @port_num: The port on the specified device to use.
321  * @qp_type: Specifies which QP traffic to snoop.  Must be either
322  *   IB_QPT_SMI or IB_QPT_GSI.
323  * @mad_snoop_flags: Specifies information where snooping occurs.
324  * @send_handler: The callback routine invoked for a snooped send.
325  * @recv_handler: The callback routine invoked for a snooped receive.
326  * @context: User specified context associated with the registration.
327  */
328 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
329                                            u8 port_num,
330                                            enum ib_qp_type qp_type,
331                                            int mad_snoop_flags,
332                                            ib_mad_snoop_handler snoop_handler,
333                                            ib_mad_recv_handler recv_handler,
334                                            void *context);
335
336 /**
337  * ib_unregister_mad_agent - Unregisters a client from using MAD services.
338  * @mad_agent: Corresponding MAD registration request to deregister.
339  *
340  * After invoking this routine, MAD services are no longer usable by the
341  * client on the associated QP.
342  */
343 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent);
344
345 /**
346  * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
347  *   with the registered client.
348  * @mad_agent: Specifies the associated registration to post the send to.
349  * @send_wr: Specifies the information needed to send the MAD(s).
350  * @bad_send_wr: Specifies the MAD on which an error was encountered.
351  *
352  * Sent MADs are not guaranteed to complete in the order that they were posted.
353  */
354 int ib_post_send_mad(struct ib_mad_agent *mad_agent,
355                      struct ib_send_wr *send_wr,
356                      struct ib_send_wr **bad_send_wr);
357
358 /**
359  * ib_coalesce_recv_mad - Coalesces received MAD data into a single buffer.
360  * @mad_recv_wc: Work completion information for a received MAD.
361  * @buf: User-provided data buffer to receive the coalesced buffers.  The
362  *   referenced buffer should be at least the size of the mad_len specified
363  *   by @mad_recv_wc.
364  *
365  * This call copies a chain of received RMPP MADs into a single data buffer,
366  * removing duplicated headers.
367  */
368 void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc,
369                           void *buf);
370
371 /**
372  * ib_free_recv_mad - Returns data buffers used to receive a MAD to the
373  *   access layer.
374  * @mad_recv_wc: Work completion information for a received MAD.
375  *
376  * Clients receiving MADs through their ib_mad_recv_handler must call this
377  * routine to return the work completion buffers to the access layer.
378  */
379 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc);
380
381 /**
382  * ib_cancel_mad - Cancels an outstanding send MAD operation.
383  * @mad_agent: Specifies the registration associated with sent MAD.
384  * @wr_id: Indicates the work request identifier of the MAD to cancel.
385  *
386  * MADs will be returned to the user through the corresponding
387  * ib_mad_send_handler.
388  */
389 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
390                    u64 wr_id);
391
392 /**
393  * ib_redirect_mad_qp - Registers a QP for MAD services.
394  * @qp: Reference to a QP that requires MAD services.
395  * @rmpp_version: If set, indicates that the client will send
396  *   and receive MADs that contain the RMPP header for the given version.
397  *   If set to 0, indicates that RMPP is not used by this client.
398  * @send_handler: The completion callback routine invoked after a send
399  *   request has completed.
400  * @recv_handler: The completion callback routine invoked for a received
401  *   MAD.
402  * @context: User specified context associated with the registration.
403  *
404  * Use of this call allows clients to use MAD services, such as RMPP,
405  * on user-owned QPs.  After calling this routine, users may send
406  * MADs on the specified QP by calling ib_mad_post_send.
407  */
408 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
409                                         u8 rmpp_version,
410                                         ib_mad_send_handler send_handler,
411                                         ib_mad_recv_handler recv_handler,
412                                         void *context);
413
414 /**
415  * ib_process_mad_wc - Processes a work completion associated with a
416  *   MAD sent or received on a redirected QP.
417  * @mad_agent: Specifies the registered MAD service using the redirected QP.
418  * @wc: References a work completion associated with a sent or received
419  *   MAD segment.
420  *
421  * This routine is used to complete or continue processing on a MAD request.
422  * If the work completion is associated with a send operation, calling
423  * this routine is required to continue an RMPP transfer or to wait for a
424  * corresponding response, if it is a request.  If the work completion is
425  * associated with a receive operation, calling this routine is required to
426  * process an inbound or outbound RMPP transfer, or to match a response MAD
427  * with its corresponding request.
428  */
429 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
430                       struct ib_wc *wc);
431
432 /**
433  * ib_create_send_mad - Allocate and initialize a data buffer and work request
434  *   for sending a MAD.
435  * @mad_agent: Specifies the registered MAD service to associate with the MAD.
436  * @remote_qpn: Specifies the QPN of the receiving node.
437  * @pkey_index: Specifies which PKey the MAD will be sent using.  This field
438  *   is valid only if the remote_qpn is QP 1.
439  * @ah: References the address handle used to transfer to the remote node.
440  * @hdr_len: Indicates the size of the data header of the MAD.  This length
441  *   should include the common MAD header, RMPP header, plus any class
442  *   specific header.
443  * @data_len: Indicates the size of any user-transfered data.  The call will
444  *   automatically adjust the allocated buffer size to account for any
445  *   additional padding that may be necessary.
446  * @gfp_mask: GFP mask used for the memory allocation.
447  *
448  * This is a helper routine that may be used to allocate a MAD.  Users are
449  * not required to allocate outbound MADs using this call.  The returned
450  * MAD send buffer will reference a data buffer usable for sending a MAD, along
451  * with an intialized work request structure.
452  */
453 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
454                                             u32 remote_qpn, u16 pkey_index,
455                                             struct ib_ah *ah,
456                                             int hdr_len, int data_len,
457                                             unsigned int __nocast gfp_mask);
458
459 /**
460  * ib_free_send_mad - Returns data buffers used to send a MAD.
461  * @send_buf: Previously allocated send data buffer.
462  */
463 void ib_free_send_mad(struct ib_mad_send_buf *send_buf);
464
465 #endif /* IB_MAD_H */